PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/ACE_wrappers/ace/INET_Addr.h

http://github.com/insider42/mangos
C Header | 390 lines | 139 code | 69 blank | 182 comment | 4 complexity | 63de40e29c883ae0dda2c43b411cd171 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. /* -*- C++ -*- */
  2. //=============================================================================
  3. /**
  4. * @file INET_Addr.h
  5. *
  6. * $Id: INET_Addr.h 91064 2010-07-12 10:11:24Z johnnyw $
  7. *
  8. * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
  9. */
  10. //=============================================================================
  11. #ifndef ACE_INET_ADDR_H
  12. #define ACE_INET_ADDR_H
  13. #include /**/ "ace/pre.h"
  14. #include "ace/Sock_Connect.h"
  15. #if !defined (ACE_LACKS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif /* ACE_LACKS_PRAGMA_ONCE */
  18. #include "ace/Addr.h"
  19. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  20. /**
  21. * @class ACE_INET_Addr
  22. *
  23. * @brief Defines a C++ wrapper facade for the Internet domain address
  24. * family format.
  25. */
  26. class ACE_Export ACE_INET_Addr : public ACE_Addr
  27. {
  28. public:
  29. // = Initialization methods.
  30. /// Default constructor.
  31. ACE_INET_Addr (void);
  32. /// Copy constructor.
  33. ACE_INET_Addr (const ACE_INET_Addr &);
  34. /// Creates an ACE_INET_Addr from a sockaddr_in structure.
  35. ACE_INET_Addr (const sockaddr_in *addr, int len);
  36. /// Creates an ACE_INET_Addr from a @a port_number and the remote
  37. /// @a host_name. The port number is assumed to be in host byte order.
  38. /// To set a port already in network byte order, please @see set().
  39. /// Use address_family to select IPv6 (PF_INET6) vs. IPv4 (PF_INET).
  40. ACE_INET_Addr (u_short port_number,
  41. const char host_name[],
  42. int address_family = AF_UNSPEC);
  43. /**
  44. * Initializes an ACE_INET_Addr from the @a address, which can be
  45. * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or
  46. * "128.252.166.57:1234"). If there is no ':' in the @a address it
  47. * is assumed to be a port number, with the IP address being
  48. * INADDR_ANY.
  49. */
  50. explicit ACE_INET_Addr (const char address[],
  51. int address_family = AF_UNSPEC);
  52. /**
  53. * Creates an ACE_INET_Addr from a @a port_number and an Internet
  54. * @a ip_addr. This method assumes that @a port_number and @a ip_addr
  55. * are in host byte order. If you have addressing information in
  56. * network byte order, @see set().
  57. */
  58. explicit ACE_INET_Addr (u_short port_number,
  59. ACE_UINT32 ip_addr = INADDR_ANY);
  60. /// Uses getservbyname() to create an ACE_INET_Addr from a
  61. /// @a port_name, the remote @a host_name, and the @a protocol.
  62. ACE_INET_Addr (const char port_name[],
  63. const char host_name[],
  64. const char protocol[] = "tcp");
  65. /**
  66. * Uses getservbyname() to create an ACE_INET_Addr from a
  67. * @a port_name, an Internet @a ip_addr, and the @a protocol. This
  68. * method assumes that @a ip_addr is in host byte order.
  69. */
  70. ACE_INET_Addr (const char port_name[],
  71. ACE_UINT32 ip_addr,
  72. const char protocol[] = "tcp");
  73. #if defined (ACE_HAS_WCHAR)
  74. ACE_INET_Addr (u_short port_number,
  75. const wchar_t host_name[],
  76. int address_family = AF_UNSPEC);
  77. explicit ACE_INET_Addr (const wchar_t address[],
  78. int address_family = AF_UNSPEC);
  79. ACE_INET_Addr (const wchar_t port_name[],
  80. const wchar_t host_name[],
  81. const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
  82. ACE_INET_Addr (const wchar_t port_name[],
  83. ACE_UINT32 ip_addr,
  84. const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
  85. #endif /* ACE_HAS_WCHAR */
  86. /// Default dtor.
  87. ~ACE_INET_Addr (void);
  88. // = Direct initialization methods.
  89. // These methods are useful after the object has been constructed.
  90. /// Initializes from another ACE_INET_Addr.
  91. int set (const ACE_INET_Addr &);
  92. /**
  93. * Initializes an ACE_INET_Addr from a @a port_number and the
  94. * remote @a host_name. If @a encode is non-zero then @a port_number is
  95. * converted into network byte order, otherwise it is assumed to be
  96. * in network byte order already and are passed straight through.
  97. * address_family can be used to select IPv4/IPv6 if the OS has
  98. * IPv6 capability (ACE_HAS_IPV6 is defined). To specify IPv6, use
  99. * the value AF_INET6. To specify IPv4, use AF_INET.
  100. */
  101. int set (u_short port_number,
  102. const char host_name[],
  103. int encode = 1,
  104. int address_family = AF_UNSPEC);
  105. /**
  106. * Initializes an ACE_INET_Addr from a @a port_number and an Internet
  107. * @a ip_addr. If @a encode is non-zero then the port number and IP address
  108. * are converted into network byte order, otherwise they are assumed to be
  109. * in network byte order already and are passed straight through.
  110. *
  111. * If <map> is non-zero and IPv6 support has been compiled in,
  112. * then this address will be set to the IPv4-mapped IPv6 address of it.
  113. */
  114. int set (u_short port_number,
  115. ACE_UINT32 ip_addr = INADDR_ANY,
  116. int encode = 1,
  117. int map = 0);
  118. /// Uses <getservbyname> to initialize an ACE_INET_Addr from a
  119. /// <port_name>, the remote @a host_name, and the @a protocol.
  120. int set (const char port_name[],
  121. const char host_name[],
  122. const char protocol[] = "tcp");
  123. /**
  124. * Uses <getservbyname> to initialize an ACE_INET_Addr from a
  125. * <port_name>, an @a ip_addr, and the @a protocol. This assumes that
  126. * @a ip_addr is already in network byte order.
  127. */
  128. int set (const char port_name[],
  129. ACE_UINT32 ip_addr,
  130. const char protocol[] = "tcp");
  131. /**
  132. * Initializes an ACE_INET_Addr from the @a addr, which can be
  133. * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or
  134. * "128.252.166.57:1234"). If there is no ':' in the @a address it
  135. * is assumed to be a port number, with the IP address being
  136. * INADDR_ANY.
  137. */
  138. int set (const char addr[], int address_family = AF_UNSPEC);
  139. /// Creates an ACE_INET_Addr from a sockaddr_in structure.
  140. int set (const sockaddr_in *,
  141. int len);
  142. #if defined (ACE_HAS_WCHAR)
  143. int set (u_short port_number,
  144. const wchar_t host_name[],
  145. int encode = 1,
  146. int address_family = AF_UNSPEC);
  147. int set (const wchar_t port_name[],
  148. const wchar_t host_name[],
  149. const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
  150. int set (const wchar_t port_name[],
  151. ACE_UINT32 ip_addr,
  152. const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
  153. int set (const wchar_t addr[], int address_family = AF_UNSPEC);
  154. #endif /* ACE_HAS_WCHAR */
  155. /// Return a pointer to the underlying network address.
  156. virtual void *get_addr (void) const;
  157. int get_addr_size(void) const;
  158. /// Set a pointer to the address.
  159. virtual void set_addr (void *, int len);
  160. /// Set a pointer to the address.
  161. virtual void set_addr (void *, int len, int map);
  162. /**
  163. * Transform the current ACE_INET_Addr address into string format.
  164. * If @a ipaddr_format is ttrue this produces "ip-number:port-number"
  165. * (e.g., "128.252.166.57:1234"), whereas if @a ipaddr_format is false
  166. * this produces "ip-name:port-number" (e.g.,
  167. * "tango.cs.wustl.edu:1234"). Returns -1 if the @a size of the
  168. * @a buffer is too small, else 0.
  169. */
  170. virtual int addr_to_string (ACE_TCHAR buffer[],
  171. size_t size,
  172. int ipaddr_format = 1) const;
  173. /**
  174. * Initializes an ACE_INET_Addr from the @a address, which can be
  175. * "ip-addr:port-number" (e.g., "tango.cs.wustl.edu:1234"),
  176. * "ip-addr:port-name" (e.g., "tango.cs.wustl.edu:telnet"),
  177. * "ip-number:port-number" (e.g., "128.252.166.57:1234"), or
  178. * "ip-number:port-name" (e.g., "128.252.166.57:telnet"). If there
  179. * is no ':' in the @a address it is assumed to be a port number,
  180. * with the IP address being INADDR_ANY.
  181. */
  182. virtual int string_to_addr (const char address[],
  183. int address_family = AF_UNSPEC);
  184. #if defined (ACE_HAS_WCHAR)
  185. /*
  186. virtual int string_to_addr (const char address[]);
  187. */
  188. #endif /* ACE_HAS_WCHAR */
  189. /**
  190. * Sets the port number without affecting the host name. If
  191. * @a encode is enabled then @a port_number is converted into network
  192. * byte order, otherwise it is assumed to be in network byte order
  193. * already and are passed straight through.
  194. */
  195. void set_port_number (u_short,
  196. int encode = 1);
  197. /**
  198. * Sets the address without affecting the port number. If
  199. * @a encode is enabled then @a ip_addr is converted into network
  200. * byte order, otherwise it is assumed to be in network byte order
  201. * already and are passed straight through. The size of the address
  202. * is specified in the @a len parameter.
  203. * If @a map is non-zero, IPv6 support has been compiled in, and
  204. * @a ip_addr is an IPv4 address, then this address is set to the IPv4-mapped
  205. * IPv6 address of it.
  206. */
  207. int set_address (const char *ip_addr,
  208. int len,
  209. int encode = 1,
  210. int map = 0);
  211. #if (defined (__linux__) || defined (ACE_WIN32)) && defined (ACE_HAS_IPV6)
  212. /**
  213. * Sets the interface that should be used for this address. This only has
  214. * an effect when the address is link local, otherwise it does nothing.
  215. */
  216. int set_interface (const char *intf_name);
  217. #endif /* (__linux__ || ACE_WIN32) && ACE_HAS_IPV6 */
  218. /// Return the port number, converting it into host byte-order.
  219. u_short get_port_number (void) const;
  220. /**
  221. * Return the character representation of the name of the host,
  222. * storing it in the @a hostname (which is assumed to be
  223. * @a hostnamelen bytes long). This version is reentrant. If
  224. * @a hostnamelen is greater than 0 then @a hostname will be
  225. * NUL-terminated even if -1 is returned.
  226. */
  227. int get_host_name (char hostname[],
  228. size_t hostnamelen) const;
  229. #if defined (ACE_HAS_WCHAR)
  230. int get_host_name (wchar_t hostname[],
  231. size_t hostnamelen) const;
  232. #endif /* ACE_HAS_WCHAR */
  233. /**
  234. * Return the character representation of the hostname. This
  235. * version is non-reentrant since it returns a pointer to a static
  236. * data area. You should therefore either (1) do a "deep copy" of
  237. * the address returned by get_host_name(), e.g., using strdup() or
  238. * (2) use the "reentrant" version of get_host_name() described
  239. * above.
  240. */
  241. const char *get_host_name (void) const;
  242. /**
  243. * Return the "dotted decimal" Internet address representation of
  244. * the hostname storing it in the @a addr (which is assumed to be
  245. * @a addr_size bytes long). This version is reentrant.
  246. */
  247. const char *get_host_addr (char *addr, int addr_size) const;
  248. /**
  249. * Return the "dotted decimal" Internet address representation of
  250. * the hostname. This version is non-reentrant since it returns a
  251. * pointer to a static data area. You should therefore either
  252. * (1) do a "deep copy" of the address returned by get_host_addr(), e.g.,
  253. * using strdup() or (2) use the "reentrant" version of
  254. * get_host_addr() described above.
  255. */
  256. const char *get_host_addr (void) const;
  257. /// Return the 4-byte IP address, converting it into host byte
  258. /// order.
  259. ACE_UINT32 get_ip_address (void) const;
  260. /// Return @c true if the IP address is INADDR_ANY or IN6ADDR_ANY.
  261. bool is_any (void) const;
  262. /// Return @c true if the IP address is IPv4/IPv6 loopback address.
  263. bool is_loopback (void) const;
  264. /// Return @c true if the IP address is IPv4/IPv6 multicast address.
  265. bool is_multicast (void) const;
  266. #if defined (ACE_HAS_IPV6)
  267. /// Return @c true if the IP address is IPv6 linklocal address.
  268. bool is_linklocal (void) const;
  269. /// Return @c true if the IP address is IPv4-mapped IPv6 address.
  270. bool is_ipv4_mapped_ipv6 (void) const;
  271. /// Return @c true if the IP address is IPv4-compatible IPv6 address.
  272. bool is_ipv4_compat_ipv6 (void) const;
  273. #endif /* ACE_HAS_IPV6 */
  274. /**
  275. * Returns @c true if @c this is less than @a rhs. In this context,
  276. * "less than" is defined in terms of IP address and TCP port
  277. * number. This operator makes it possible to use @c ACE_INET_Addrs
  278. * in STL maps.
  279. */
  280. bool operator < (const ACE_INET_Addr &rhs) const;
  281. /// Compare two addresses for equality. The addresses are considered
  282. /// equal if they contain the same IP address and port number.
  283. bool operator == (const ACE_INET_Addr &SAP) const;
  284. /// Compare two addresses for inequality.
  285. bool operator != (const ACE_INET_Addr &SAP) const;
  286. /// A variation of the equality operator, this method only compares the
  287. /// IP address and ignores the port number.
  288. bool is_ip_equal (const ACE_INET_Addr &SAP) const;
  289. /// Computes and returns hash value.
  290. virtual u_long hash (void) const;
  291. /// Dump the state of an object.
  292. void dump (void) const;
  293. /// Declare the dynamic allocation hooks.
  294. ACE_ALLOC_HOOK_DECLARE;
  295. private:
  296. /// Insure that @a hostname is properly null-terminated.
  297. int get_host_name_i (char hostname[], size_t hostnamelen) const;
  298. // Methods to gain access to the actual address of
  299. // the underlying internet address structure.
  300. void *ip_addr_pointer (void) const;
  301. int ip_addr_size (void) const;
  302. int determine_type (void) const;
  303. /// Initialize underlying inet_addr_ to default values
  304. void reset (void);
  305. /// Underlying representation.
  306. /// This union uses the knowledge that the two structures share the
  307. /// first member, sa_family (as all sockaddr structures do).
  308. union
  309. {
  310. sockaddr_in in4_;
  311. #if defined (ACE_HAS_IPV6)
  312. sockaddr_in6 in6_;
  313. #endif /* ACE_HAS_IPV6 */
  314. } inet_addr_;
  315. };
  316. ACE_END_VERSIONED_NAMESPACE_DECL
  317. #if defined (__ACE_INLINE__)
  318. #include "ace/INET_Addr.inl"
  319. #endif /* __ACE_INLINE__ */
  320. #include /**/ "ace/post.h"
  321. #endif /* ACE_INET_ADDR_H */