3 #include "ace/OS_NS_string.h"
4 #include "ace/Global_Macros.h"
5 #include "ace/OS_NS_arpa_inet.h"
7 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
10 ACE_INET_Addr::reset_i ()
12 ACE_OS::memset (&this->inet_addr_, 0, sizeof (this->inet_addr_));
13 if (this->get_type() == AF_INET)
15 #ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
16 this->inet_addr_.in4_.sin_len = sizeof (this->inet_addr_.in4_);
18 this->inet_addr_.in4_.sin_family = AF_INET;
20 #if defined (ACE_HAS_IPV6)
21 else if (this->get_type() == AF_INET6)
23 #ifdef ACE_HAS_SOCKADDR_IN6_SIN6_LEN
24 this->inet_addr_.in6_.sin6_len = sizeof (this->inet_addr_.in6_);
26 this->inet_addr_.in6_.sin6_family = AF_INET6;
28 #endif /* ACE_HAS_IPV6 */
29 this->inet_addrs_.clear ();
30 this->inet_addrs_iter_ = this->inet_addrs_.end ();
35 ACE_INET_Addr::determine_type () const
37 #if defined (ACE_HAS_IPV6)
38 # if defined (ACE_USES_IPV4_IPV6_MIGRATION)
39 return ACE::ipv6_enabled () ? AF_INET6 : AF_INET;
42 # endif /* ACE_USES_IPV4_IPV6_MIGRATION */
45 #endif /* ACE_HAS_IPV6 */
49 ACE_INET_Addr::ip_addr_pointer () const
51 #if defined (ACE_HAS_IPV6)
52 if (this->get_type () == PF_INET)
53 return (void*)&this->inet_addr_.in4_.sin_addr;
55 return (void*)&this->inet_addr_.in6_.sin6_addr;
57 return (void*)&this->inet_addr_.in4_.sin_addr;
62 ACE_INET_Addr::ip_addr_size () const
64 // Since this size value is used to pass to other host db-type
65 // functions (gethostbyaddr, etc.) the length is of int type.
66 // Thus, cast all these sizes back to int. They're all well
67 // within the range of an int anyway.
68 #if defined (ACE_HAS_IPV6)
69 if (this->get_type () == PF_INET)
70 return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr);
72 return static_cast<int> (sizeof this->inet_addr_.in6_.sin6_addr);
74 return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr.s_addr);
75 #endif /* ACE_HAS_IPV6 */
78 // Return the port number, converting it into host byte order...
81 ACE_INET_Addr::get_port_number () const
83 ACE_TRACE ("ACE_INET_Addr::get_port_number");
84 #if defined (ACE_HAS_IPV6)
85 if (this->get_type () == PF_INET)
86 return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
88 return ACE_NTOHS (this->inet_addr_.in6_.sin6_port);
90 # if defined (ACE_VXWORKS) && ACE_VXWORKS >= 0x690
91 return static_cast<u_short> (ACE_NTOHS (this->inet_addr_.in4_.sin_port));
93 return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
95 #endif /* ACE_HAS_IPV6 */
99 ACE_INET_Addr::get_addr_size () const
101 ACE_TRACE ("ACE_INET_Addr::get_addr_size");
102 #if defined (ACE_HAS_IPV6)
103 if (this->get_type () == PF_INET)
104 return sizeof this->inet_addr_.in4_;
106 return sizeof this->inet_addr_.in6_;
108 return sizeof this->inet_addr_.in4_;
109 #endif /* ACE_HAS_IPV6 */
113 ACE_INET_Addr::operator < (const ACE_INET_Addr &rhs) const
115 #if defined (ACE_HAS_IPV6)
116 if (this->get_type() != rhs.get_type())
118 return this->get_type() < rhs.get_type();
121 if (this->get_type() == PF_INET6)
123 int memval = ACE_OS::memcmp (this->ip_addr_pointer(),
124 rhs.ip_addr_pointer(),
125 this->ip_addr_size());
129 && (this->get_port_number() < rhs.get_port_number()
130 || (this->get_port_number() == rhs.get_port_number()
131 && this->inet_addr_.in6_.sin6_scope_id <
132 rhs.inet_addr_.in6_.sin6_scope_id)));
136 return this->get_ip_address () < rhs.get_ip_address ()
137 || (this->get_ip_address () == rhs.get_ip_address ()
138 && this->get_port_number () < rhs.get_port_number ());
141 #if defined (ACE_HAS_WCHAR)
143 ACE_INET_Addr::set (u_short port_number,
144 const wchar_t host_name[],
148 return this->set (port_number,
149 ACE_Wide_To_Ascii (host_name).char_rep (),
155 ACE_INET_Addr::set (const wchar_t port_name[],
156 const wchar_t host_name[],
157 const wchar_t protocol[])
159 return this->set (ACE_Wide_To_Ascii (port_name).char_rep (),
160 ACE_Wide_To_Ascii (host_name).char_rep (),
161 ACE_Wide_To_Ascii (protocol).char_rep ());
165 ACE_INET_Addr::set (const wchar_t port_name[],
167 const wchar_t protocol[])
169 return this->set (ACE_Wide_To_Ascii (port_name).char_rep (),
171 ACE_Wide_To_Ascii (protocol).char_rep ());
175 ACE_INET_Addr::set (const wchar_t addr[], int address_family)
177 return this->set (ACE_Wide_To_Ascii (addr).char_rep (), address_family);
180 #endif /* ACE_HAS_WCHAR */
182 // Return @c true if the IP address is INADDR_ANY or IN6ADDR_ANY.
184 ACE_INET_Addr::is_any () const
186 #if defined (ACE_HAS_IPV6)
187 if (this->get_type () == AF_INET6)
188 return IN6_IS_ADDR_UNSPECIFIED (&this->inet_addr_.in6_.sin6_addr);
189 #endif /* ACE_HAS_IPV6 */
191 return (this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY);
194 // Return @c true if the IP address is IPv4/IPv6 loopback address.
196 ACE_INET_Addr::is_loopback () const
198 #if defined (ACE_HAS_IPV6)
199 if (this->get_type () == AF_INET6)
200 return IN6_IS_ADDR_LOOPBACK (&this->inet_addr_.in6_.sin6_addr);
201 #endif /* ACE_HAS_IPV6 */
203 // RFC 3330 defines loopback as any address with 127.x.x.x
204 return ((this->get_ip_address () & 0XFF000000) == (INADDR_LOOPBACK & 0XFF000000));
207 // Return @c true if the IP address is IPv4/IPv6 multicast address.
209 ACE_INET_Addr::is_multicast () const
211 #if defined (ACE_HAS_IPV6)
212 if (this->get_type() == AF_INET6)
213 return this->inet_addr_.in6_.sin6_addr.s6_addr[0] == 0xFF;
214 #endif /* ACE_HAS_IPV6 */
216 (*static_cast<const unsigned char*> (
217 static_cast<const void*> (&this->inet_addr_.in4_.sin_addr.s_addr)) & 0xf0) == 0xe0;
220 #if defined (ACE_HAS_IPV6)
221 // Return @c true if the IP address is IPv6 linklocal address.
223 ACE_INET_Addr::is_linklocal () const
225 if (this->get_type () == AF_INET6)
226 return IN6_IS_ADDR_LINKLOCAL (&this->inet_addr_.in6_.sin6_addr);
231 // Return @c true if the IP address is IPv6 sitelocal address.
233 ACE_INET_Addr::is_sitelocal () const
235 if (this->get_type () == AF_INET6)
236 return IN6_IS_ADDR_SITELOCAL (&this->inet_addr_.in6_.sin6_addr);
241 // Return @c true if the IP address is IPv4 mapped IPv6 address.
243 ACE_INET_Addr::is_ipv4_mapped_ipv6 () const
245 if (this->get_type () == AF_INET6)
246 return IN6_IS_ADDR_V4MAPPED (&this->inet_addr_.in6_.sin6_addr);
251 // Return @c true if the IP address is IPv4-compatible IPv6 address.
253 ACE_INET_Addr::is_ipv4_compat_ipv6 () const
255 if (this->get_type () == AF_INET6)
256 return IN6_IS_ADDR_V4COMPAT (&this->inet_addr_.in6_.sin6_addr);
260 #endif /* ACE_HAS_IPV6 */
262 ACE_END_VERSIONED_NAMESPACE_DECL