PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/acelite/ace/INET_Addr.inl

https://bitbucket.org/oregon/oregoncore/
C++ Header | 252 lines | 208 code | 31 blank | 13 comment | 45 complexity | 48556446c492ba23b757ec8ff22d37b3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0, BSD-2-Clause
  1. // -*- C++ -*-
  2. #include "ace/OS_NS_string.h"
  3. #include "ace/Global_Macros.h"
  4. #include "ace/OS_NS_arpa_inet.h"
  5. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  6. ACE_INLINE void
  7. ACE_INET_Addr::reset_i (void)
  8. {
  9. ACE_OS::memset (&this->inet_addr_, 0, sizeof (this->inet_addr_));
  10. if (this->get_type() == AF_INET)
  11. {
  12. #ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
  13. this->inet_addr_.in4_.sin_len = sizeof (this->inet_addr_.in4_);
  14. #endif
  15. this->inet_addr_.in4_.sin_family = AF_INET;
  16. }
  17. #if defined (ACE_HAS_IPV6)
  18. else if (this->get_type() == AF_INET6)
  19. {
  20. #ifdef ACE_HAS_SOCKADDR_IN6_SIN6_LEN
  21. this->inet_addr_.in6_.sin6_len = sizeof (this->inet_addr_.in6_);
  22. #endif
  23. this->inet_addr_.in6_.sin6_family = AF_INET6;
  24. }
  25. #endif /* ACE_HAS_IPV6 */
  26. this->inet_addrs_.clear ();
  27. this->inet_addrs_iter_ = this->inet_addrs_.end ();
  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. return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr.s_addr);
  68. #endif /* ACE_HAS_IPV6 */
  69. }
  70. // Return the port number, converting it into host byte order...
  71. ACE_INLINE u_short
  72. ACE_INET_Addr::get_port_number (void) const
  73. {
  74. ACE_TRACE ("ACE_INET_Addr::get_port_number");
  75. #if defined (ACE_HAS_IPV6)
  76. if (this->get_type () == PF_INET)
  77. return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
  78. else
  79. return ACE_NTOHS (this->inet_addr_.in6_.sin6_port);
  80. #else
  81. # if defined (ACE_VXWORKS) && ACE_VXWORKS >= 0x690
  82. return static_cast<u_short> (ACE_NTOHS (this->inet_addr_.in4_.sin_port));
  83. # else
  84. return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
  85. # endif
  86. #endif /* ACE_HAS_IPV6 */
  87. }
  88. ACE_INLINE int
  89. ACE_INET_Addr::get_addr_size (void) const
  90. {
  91. ACE_TRACE ("ACE_INET_Addr::get_addr_size");
  92. #if defined (ACE_HAS_IPV6)
  93. if (this->get_type () == PF_INET)
  94. return sizeof this->inet_addr_.in4_;
  95. else
  96. return sizeof this->inet_addr_.in6_;
  97. #else
  98. return sizeof this->inet_addr_.in4_;
  99. #endif /* ACE_HAS_IPV6 */
  100. }
  101. ACE_INLINE bool
  102. ACE_INET_Addr::operator < (const ACE_INET_Addr &rhs) const
  103. {
  104. #if defined (ACE_HAS_IPV6)
  105. if (this->get_type() != rhs.get_type())
  106. {
  107. return this->get_type() < rhs.get_type();
  108. }
  109. if (this->get_type() == PF_INET6)
  110. {
  111. int memval = ACE_OS::memcmp (this->ip_addr_pointer(),
  112. rhs.ip_addr_pointer(),
  113. this->ip_addr_size());
  114. return memval < 0
  115. || (memval == 0
  116. && (this->get_port_number() < rhs.get_port_number()
  117. || (this->get_port_number() == rhs.get_port_number()
  118. && this->inet_addr_.in6_.sin6_scope_id <
  119. rhs.inet_addr_.in6_.sin6_scope_id)));
  120. }
  121. #endif
  122. return this->get_ip_address () < rhs.get_ip_address ()
  123. || (this->get_ip_address () == rhs.get_ip_address ()
  124. && this->get_port_number () < rhs.get_port_number ());
  125. }
  126. #if defined (ACE_HAS_WCHAR)
  127. ACE_INLINE int
  128. ACE_INET_Addr::set (u_short port_number,
  129. const wchar_t host_name[],
  130. int encode,
  131. int address_family)
  132. {
  133. return this->set (port_number,
  134. ACE_Wide_To_Ascii (host_name).char_rep (),
  135. encode,
  136. address_family);
  137. }
  138. ACE_INLINE int
  139. ACE_INET_Addr::set (const wchar_t port_name[],
  140. const wchar_t host_name[],
  141. const wchar_t protocol[])
  142. {
  143. return this->set (ACE_Wide_To_Ascii (port_name).char_rep (),
  144. ACE_Wide_To_Ascii (host_name).char_rep (),
  145. ACE_Wide_To_Ascii (protocol).char_rep ());
  146. }
  147. ACE_INLINE int
  148. ACE_INET_Addr::set (const wchar_t port_name[],
  149. ACE_UINT32 ip_addr,
  150. const wchar_t protocol[])
  151. {
  152. return this->set (ACE_Wide_To_Ascii (port_name).char_rep (),
  153. ip_addr,
  154. ACE_Wide_To_Ascii (protocol).char_rep ());
  155. }
  156. ACE_INLINE int
  157. ACE_INET_Addr::set (const wchar_t addr[], int address_family)
  158. {
  159. return this->set (ACE_Wide_To_Ascii (addr).char_rep (), address_family);
  160. }
  161. #endif /* ACE_HAS_WCHAR */
  162. // Return @c true if the IP address is INADDR_ANY or IN6ADDR_ANY.
  163. ACE_INLINE bool
  164. ACE_INET_Addr::is_any (void) const
  165. {
  166. #if defined (ACE_HAS_IPV6)
  167. if (this->get_type () == AF_INET6)
  168. return IN6_IS_ADDR_UNSPECIFIED (&this->inet_addr_.in6_.sin6_addr);
  169. #endif /* ACE_HAS_IPV6 */
  170. return (this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY);
  171. }
  172. // Return @c true if the IP address is IPv4/IPv6 loopback address.
  173. ACE_INLINE bool
  174. ACE_INET_Addr::is_loopback (void) const
  175. {
  176. #if defined (ACE_HAS_IPV6)
  177. if (this->get_type () == AF_INET6)
  178. return IN6_IS_ADDR_LOOPBACK (&this->inet_addr_.in6_.sin6_addr);
  179. #endif /* ACE_HAS_IPV6 */
  180. // RFC 3330 defines loopback as any address with 127.x.x.x
  181. return ((this->get_ip_address () & 0XFF000000) == (INADDR_LOOPBACK & 0XFF000000));
  182. }
  183. // Return @c true if the IP address is IPv4/IPv6 multicast address.
  184. ACE_INLINE bool
  185. ACE_INET_Addr::is_multicast (void) const
  186. {
  187. #if defined (ACE_HAS_IPV6)
  188. if (this->get_type() == AF_INET6)
  189. return this->inet_addr_.in6_.sin6_addr.s6_addr[0] == 0xFF;
  190. #endif /* ACE_HAS_IPV6 */
  191. return
  192. (*static_cast<const unsigned char*> (
  193. static_cast<const void*> (&this->inet_addr_.in4_.sin_addr.s_addr)) & 0xf0) == 0xe0;
  194. }
  195. #if defined (ACE_HAS_IPV6)
  196. // Return @c true if the IP address is IPv6 linklocal address.
  197. ACE_INLINE bool
  198. ACE_INET_Addr::is_linklocal (void) const
  199. {
  200. if (this->get_type () == AF_INET6)
  201. return IN6_IS_ADDR_LINKLOCAL (&this->inet_addr_.in6_.sin6_addr);
  202. return false;
  203. }
  204. // Return @c true if the IP address is IPv4 mapped IPv6 address.
  205. ACE_INLINE bool
  206. ACE_INET_Addr::is_ipv4_mapped_ipv6 (void) const
  207. {
  208. if (this->get_type () == AF_INET6)
  209. return IN6_IS_ADDR_V4MAPPED (&this->inet_addr_.in6_.sin6_addr);
  210. return false;
  211. }
  212. // Return @c true if the IP address is IPv4-compatible IPv6 address.
  213. ACE_INLINE bool
  214. ACE_INET_Addr::is_ipv4_compat_ipv6 (void) const
  215. {
  216. if (this->get_type () == AF_INET6)
  217. return IN6_IS_ADDR_V4COMPAT (&this->inet_addr_.in6_.sin6_addr);
  218. return false;
  219. }
  220. #endif /* ACE_HAS_IPV6 */
  221. ACE_END_VERSIONED_NAMESPACE_DECL