PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/ACE_wrappers/ace/INET_Addr.inl

http://github.com/insider42/mangos
C++ Header | 255 lines | 206 code | 31 blank | 18 comment | 45 complexity | 058c6b055d4bbf45475e5a6f2b79e449 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. // -*- C++ -*-
  2. //
  3. // $Id: INET_Addr.inl 88218 2009-12-17 12:32:14Z mcorino $
  4. #include "ace/OS_NS_string.h"
  5. #include "ace/Global_Macros.h"
  6. #include "ace/OS_NS_arpa_inet.h"
  7. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  8. ACE_INLINE void
  9. ACE_INET_Addr::reset (void)
  10. {
  11. ACE_OS::memset (&this->inet_addr_, 0, sizeof (this->inet_addr_));
  12. if (this->get_type() == AF_INET)
  13. {
  14. #ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
  15. this->inet_addr_.in4_.sin_len = sizeof (this->inet_addr_.in4_);
  16. #endif
  17. this->inet_addr_.in4_.sin_family = AF_INET;
  18. }
  19. #if defined (ACE_HAS_IPV6)
  20. else if (this->get_type() == AF_INET6)
  21. {
  22. #ifdef ACE_HAS_SOCKADDR_IN6_SIN6_LEN
  23. this->inet_addr_.in6_.sin6_len = sizeof (this->inet_addr_.in6_);
  24. #endif
  25. this->inet_addr_.in6_.sin6_family = AF_INET6;
  26. }
  27. #endif /* ACE_HAS_IPV6 */
  28. }
  29. ACE_INLINE int
  30. ACE_INET_Addr::determine_type (void) const
  31. {
  32. #if defined (ACE_HAS_IPV6)
  33. # if defined (ACE_USES_IPV4_IPV6_MIGRATION)
  34. return ACE::ipv6_enabled () ? AF_INET6 : AF_INET;
  35. # else
  36. return AF_INET6;
  37. # endif /* ACE_USES_IPV4_IPV6_MIGRATION */
  38. #else
  39. return AF_INET;
  40. #endif /* ACE_HAS_IPV6 */
  41. }
  42. ACE_INLINE void *
  43. ACE_INET_Addr::ip_addr_pointer (void) const
  44. {
  45. #if defined (ACE_HAS_IPV6)
  46. if (this->get_type () == PF_INET)
  47. return (void*)&this->inet_addr_.in4_.sin_addr;
  48. else
  49. return (void*)&this->inet_addr_.in6_.sin6_addr;
  50. #else
  51. return (void*)&this->inet_addr_.in4_.sin_addr;
  52. #endif
  53. }
  54. ACE_INLINE int
  55. ACE_INET_Addr::ip_addr_size (void) const
  56. {
  57. // Since this size value is used to pass to other host db-type
  58. // functions (gethostbyaddr, etc.) the length is of int type.
  59. // Thus, cast all these sizes back to int. They're all well
  60. // within the range of an int anyway.
  61. #if defined (ACE_HAS_IPV6)
  62. if (this->get_type () == PF_INET)
  63. return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr);
  64. else
  65. return static_cast<int> (sizeof this->inet_addr_.in6_.sin6_addr);
  66. #else
  67. // These _UNICOS changes were picked up from pre-IPv6 code in
  68. // get_host_name_i... the IPv6 section above may need something
  69. // similar, so keep an eye out for it.
  70. # if !defined(_UNICOS)
  71. return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr.s_addr);
  72. # else /* _UNICOS */
  73. return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr);
  74. # endif /* ! _UNICOS */
  75. #endif /* ACE_HAS_IPV6 */
  76. }
  77. // Return the port number, converting it into host byte order...
  78. ACE_INLINE u_short
  79. ACE_INET_Addr::get_port_number (void) const
  80. {
  81. ACE_TRACE ("ACE_INET_Addr::get_port_number");
  82. #if defined (ACE_HAS_IPV6)
  83. if (this->get_type () == PF_INET)
  84. return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
  85. else
  86. return ACE_NTOHS (this->inet_addr_.in6_.sin6_port);
  87. #else
  88. return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
  89. #endif /* ACE_HAS_IPV6 */
  90. }
  91. ACE_INLINE int
  92. ACE_INET_Addr::get_addr_size (void) const
  93. {
  94. ACE_TRACE ("ACE_INET_Addr::get_addr_size");
  95. #if defined (ACE_HAS_IPV6)
  96. if (this->get_type () == PF_INET)
  97. return sizeof this->inet_addr_.in4_;
  98. else
  99. return sizeof this->inet_addr_.in6_;
  100. #else
  101. return sizeof this->inet_addr_.in4_;
  102. #endif /* ACE_HAS_IPV6 */
  103. }
  104. ACE_INLINE bool
  105. ACE_INET_Addr::operator < (const ACE_INET_Addr &rhs) const
  106. {
  107. #if defined (ACE_HAS_IPV6)
  108. if (this->get_type() != rhs.get_type())
  109. {
  110. return this->get_type() < rhs.get_type();
  111. }
  112. if (this->get_type() == PF_INET6)
  113. {
  114. int memval = ACE_OS::memcmp (this->ip_addr_pointer(),
  115. rhs.ip_addr_pointer(),
  116. this->ip_addr_size());
  117. return memval < 0
  118. || (memval == 0
  119. && (this->get_port_number() < rhs.get_port_number()
  120. || (this->get_port_number() == rhs.get_port_number()
  121. && this->inet_addr_.in6_.sin6_scope_id <
  122. rhs.inet_addr_.in6_.sin6_scope_id)));
  123. }
  124. #endif
  125. return this->get_ip_address () < rhs.get_ip_address ()
  126. || (this->get_ip_address () == rhs.get_ip_address ()
  127. && this->get_port_number () < rhs.get_port_number ());
  128. }
  129. #if defined (ACE_HAS_WCHAR)
  130. ACE_INLINE int
  131. ACE_INET_Addr::set (u_short port_number,
  132. const wchar_t host_name[],
  133. int encode,
  134. int address_family)
  135. {
  136. return this->set (port_number,
  137. ACE_Wide_To_Ascii (host_name).char_rep (),
  138. encode,
  139. address_family);
  140. }
  141. ACE_INLINE int
  142. ACE_INET_Addr::set (const wchar_t port_name[],
  143. const wchar_t host_name[],
  144. const wchar_t protocol[])
  145. {
  146. return this->set (ACE_Wide_To_Ascii (port_name).char_rep (),
  147. ACE_Wide_To_Ascii (host_name).char_rep (),
  148. ACE_Wide_To_Ascii (protocol).char_rep ());
  149. }
  150. ACE_INLINE int
  151. ACE_INET_Addr::set (const wchar_t port_name[],
  152. ACE_UINT32 ip_addr,
  153. const wchar_t protocol[])
  154. {
  155. return this->set (ACE_Wide_To_Ascii (port_name).char_rep (),
  156. ip_addr,
  157. ACE_Wide_To_Ascii (protocol).char_rep ());
  158. }
  159. ACE_INLINE int
  160. ACE_INET_Addr::set (const wchar_t addr[], int address_family)
  161. {
  162. return this->set (ACE_Wide_To_Ascii (addr).char_rep (), address_family);
  163. }
  164. #endif /* ACE_HAS_WCHAR */
  165. // Return @c true if the IP address is INADDR_ANY or IN6ADDR_ANY.
  166. ACE_INLINE bool
  167. ACE_INET_Addr::is_any (void) const
  168. {
  169. #if defined (ACE_HAS_IPV6)
  170. if (this->get_type () == AF_INET6)
  171. return IN6_IS_ADDR_UNSPECIFIED (&this->inet_addr_.in6_.sin6_addr);
  172. #endif /* ACE_HAS_IPV6 */
  173. return (this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY);
  174. }
  175. // Return @c true if the IP address is IPv4/IPv6 loopback address.
  176. ACE_INLINE bool
  177. ACE_INET_Addr::is_loopback (void) const
  178. {
  179. #if defined (ACE_HAS_IPV6)
  180. if (this->get_type () == AF_INET6)
  181. return IN6_IS_ADDR_LOOPBACK (&this->inet_addr_.in6_.sin6_addr);
  182. #endif /* ACE_HAS_IPV6 */
  183. // RFC 3330 defines loopback as any address with 127.x.x.x
  184. return ((this->get_ip_address () & 0XFF000000) == (INADDR_LOOPBACK & 0XFF000000));
  185. }
  186. // Return @c true if the IP address is IPv4/IPv6 multicast address.
  187. ACE_INLINE bool
  188. ACE_INET_Addr::is_multicast (void) const
  189. {
  190. #if defined (ACE_HAS_IPV6)
  191. if (this->get_type() == AF_INET6)
  192. return this->inet_addr_.in6_.sin6_addr.s6_addr[0] == 0xFF;
  193. #endif /* ACE_HAS_IPV6 */
  194. return
  195. (*static_cast<const unsigned char*> (
  196. static_cast<const void*> (&this->inet_addr_.in4_.sin_addr.s_addr)) & 0xf0) == 0xe0;
  197. }
  198. #if defined (ACE_HAS_IPV6)
  199. // Return @c true if the IP address is IPv6 linklocal address.
  200. ACE_INLINE bool
  201. ACE_INET_Addr::is_linklocal (void) const
  202. {
  203. if (this->get_type () == AF_INET6)
  204. return IN6_IS_ADDR_LINKLOCAL (&this->inet_addr_.in6_.sin6_addr);
  205. return false;
  206. }
  207. // Return @c true if the IP address is IPv4 mapped IPv6 address.
  208. ACE_INLINE bool
  209. ACE_INET_Addr::is_ipv4_mapped_ipv6 (void) const
  210. {
  211. if (this->get_type () == AF_INET6)
  212. return IN6_IS_ADDR_V4MAPPED (&this->inet_addr_.in6_.sin6_addr);
  213. return false;
  214. }
  215. // Return @c true if the IP address is IPv4-compatible IPv6 address.
  216. ACE_INLINE bool
  217. ACE_INET_Addr::is_ipv4_compat_ipv6 (void) const
  218. {
  219. if (this->get_type () == AF_INET6)
  220. return IN6_IS_ADDR_V4COMPAT (&this->inet_addr_.in6_.sin6_addr);
  221. return false;
  222. }
  223. #endif /* ACE_HAS_IPV6 */
  224. ACE_END_VERSIONED_NAMESPACE_DECL