00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file INET_Addr.h 00006 * 00007 * $Id: INET_Addr.h 91064 2010-07-12 10:11:24Z johnnyw $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_INET_ADDR_H 00014 #define ACE_INET_ADDR_H 00015 #include /**/ "ace/pre.h" 00016 00017 #include "ace/Sock_Connect.h" 00018 00019 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00020 # pragma once 00021 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00022 00023 #include "ace/Addr.h" 00024 00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00026 00027 /** 00028 * @class ACE_INET_Addr 00029 * 00030 * @brief Defines a C++ wrapper facade for the Internet domain address 00031 * family format. 00032 */ 00033 class ACE_Export ACE_INET_Addr : public ACE_Addr 00034 { 00035 public: 00036 // = Initialization methods. 00037 00038 /// Default constructor. 00039 ACE_INET_Addr (void); 00040 00041 /// Copy constructor. 00042 ACE_INET_Addr (const ACE_INET_Addr &); 00043 00044 /// Creates an ACE_INET_Addr from a sockaddr_in structure. 00045 ACE_INET_Addr (const sockaddr_in *addr, int len); 00046 00047 /// Creates an ACE_INET_Addr from a @a port_number and the remote 00048 /// @a host_name. The port number is assumed to be in host byte order. 00049 /// To set a port already in network byte order, please @see set(). 00050 /// Use address_family to select IPv6 (PF_INET6) vs. IPv4 (PF_INET). 00051 ACE_INET_Addr (u_short port_number, 00052 const char host_name[], 00053 int address_family = AF_UNSPEC); 00054 00055 /** 00056 * Initializes an ACE_INET_Addr from the @a address, which can be 00057 * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or 00058 * "128.252.166.57:1234"). If there is no ':' in the @a address it 00059 * is assumed to be a port number, with the IP address being 00060 * INADDR_ANY. 00061 */ 00062 explicit ACE_INET_Addr (const char address[], 00063 int address_family = AF_UNSPEC); 00064 00065 /** 00066 * Creates an ACE_INET_Addr from a @a port_number and an Internet 00067 * @a ip_addr. This method assumes that @a port_number and @a ip_addr 00068 * are in host byte order. If you have addressing information in 00069 * network byte order, @see set(). 00070 */ 00071 explicit ACE_INET_Addr (u_short port_number, 00072 ACE_UINT32 ip_addr = INADDR_ANY); 00073 00074 /// Uses getservbyname() to create an ACE_INET_Addr from a 00075 /// @a port_name, the remote @a host_name, and the @a protocol. 00076 ACE_INET_Addr (const char port_name[], 00077 const char host_name[], 00078 const char protocol[] = "tcp"); 00079 00080 /** 00081 * Uses getservbyname() to create an ACE_INET_Addr from a 00082 * @a port_name, an Internet @a ip_addr, and the @a protocol. This 00083 * method assumes that @a ip_addr is in host byte order. 00084 */ 00085 ACE_INET_Addr (const char port_name[], 00086 ACE_UINT32 ip_addr, 00087 const char protocol[] = "tcp"); 00088 00089 #if defined (ACE_HAS_WCHAR) 00090 ACE_INET_Addr (u_short port_number, 00091 const wchar_t host_name[], 00092 int address_family = AF_UNSPEC); 00093 00094 explicit ACE_INET_Addr (const wchar_t address[], 00095 int address_family = AF_UNSPEC); 00096 00097 ACE_INET_Addr (const wchar_t port_name[], 00098 const wchar_t host_name[], 00099 const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp")); 00100 00101 ACE_INET_Addr (const wchar_t port_name[], 00102 ACE_UINT32 ip_addr, 00103 const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp")); 00104 #endif /* ACE_HAS_WCHAR */ 00105 00106 /// Default dtor. 00107 ~ACE_INET_Addr (void); 00108 00109 // = Direct initialization methods. 00110 00111 // These methods are useful after the object has been constructed. 00112 00113 /// Initializes from another ACE_INET_Addr. 00114 int set (const ACE_INET_Addr &); 00115 00116 /** 00117 * Initializes an ACE_INET_Addr from a @a port_number and the 00118 * remote @a host_name. If @a encode is non-zero then @a port_number is 00119 * converted into network byte order, otherwise it is assumed to be 00120 * in network byte order already and are passed straight through. 00121 * address_family can be used to select IPv4/IPv6 if the OS has 00122 * IPv6 capability (ACE_HAS_IPV6 is defined). To specify IPv6, use 00123 * the value AF_INET6. To specify IPv4, use AF_INET. 00124 */ 00125 int set (u_short port_number, 00126 const char host_name[], 00127 int encode = 1, 00128 int address_family = AF_UNSPEC); 00129 00130 /** 00131 * Initializes an ACE_INET_Addr from a @a port_number and an Internet 00132 * @a ip_addr. If @a encode is non-zero then the port number and IP address 00133 * are converted into network byte order, otherwise they are assumed to be 00134 * in network byte order already and are passed straight through. 00135 * 00136 * If <map> is non-zero and IPv6 support has been compiled in, 00137 * then this address will be set to the IPv4-mapped IPv6 address of it. 00138 */ 00139 int set (u_short port_number, 00140 ACE_UINT32 ip_addr = INADDR_ANY, 00141 int encode = 1, 00142 int map = 0); 00143 00144 /// Uses <getservbyname> to initialize an ACE_INET_Addr from a 00145 /// <port_name>, the remote @a host_name, and the @a protocol. 00146 int set (const char port_name[], 00147 const char host_name[], 00148 const char protocol[] = "tcp"); 00149 00150 /** 00151 * Uses <getservbyname> to initialize an ACE_INET_Addr from a 00152 * <port_name>, an @a ip_addr, and the @a protocol. This assumes that 00153 * @a ip_addr is already in network byte order. 00154 */ 00155 int set (const char port_name[], 00156 ACE_UINT32 ip_addr, 00157 const char protocol[] = "tcp"); 00158 00159 /** 00160 * Initializes an ACE_INET_Addr from the @a addr, which can be 00161 * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or 00162 * "128.252.166.57:1234"). If there is no ':' in the @a address it 00163 * is assumed to be a port number, with the IP address being 00164 * INADDR_ANY. 00165 */ 00166 int set (const char addr[], int address_family = AF_UNSPEC); 00167 00168 /// Creates an ACE_INET_Addr from a sockaddr_in structure. 00169 int set (const sockaddr_in *, 00170 int len); 00171 00172 #if defined (ACE_HAS_WCHAR) 00173 int set (u_short port_number, 00174 const wchar_t host_name[], 00175 int encode = 1, 00176 int address_family = AF_UNSPEC); 00177 00178 int set (const wchar_t port_name[], 00179 const wchar_t host_name[], 00180 const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp")); 00181 00182 int set (const wchar_t port_name[], 00183 ACE_UINT32 ip_addr, 00184 const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp")); 00185 00186 int set (const wchar_t addr[], int address_family = AF_UNSPEC); 00187 #endif /* ACE_HAS_WCHAR */ 00188 00189 /// Return a pointer to the underlying network address. 00190 virtual void *get_addr (void) const; 00191 int get_addr_size(void) const; 00192 00193 /// Set a pointer to the address. 00194 virtual void set_addr (void *, int len); 00195 00196 /// Set a pointer to the address. 00197 virtual void set_addr (void *, int len, int map); 00198 00199 /** 00200 * Transform the current ACE_INET_Addr address into string format. 00201 * If @a ipaddr_format is ttrue this produces "ip-number:port-number" 00202 * (e.g., "128.252.166.57:1234"), whereas if @a ipaddr_format is false 00203 * this produces "ip-name:port-number" (e.g., 00204 * "tango.cs.wustl.edu:1234"). Returns -1 if the @a size of the 00205 * @a buffer is too small, else 0. 00206 */ 00207 virtual int addr_to_string (ACE_TCHAR buffer[], 00208 size_t size, 00209 int ipaddr_format = 1) const; 00210 00211 /** 00212 * Initializes an ACE_INET_Addr from the @a address, which can be 00213 * "ip-addr:port-number" (e.g., "tango.cs.wustl.edu:1234"), 00214 * "ip-addr:port-name" (e.g., "tango.cs.wustl.edu:telnet"), 00215 * "ip-number:port-number" (e.g., "128.252.166.57:1234"), or 00216 * "ip-number:port-name" (e.g., "128.252.166.57:telnet"). If there 00217 * is no ':' in the @a address it is assumed to be a port number, 00218 * with the IP address being INADDR_ANY. 00219 */ 00220 virtual int string_to_addr (const char address[], 00221 int address_family = AF_UNSPEC); 00222 00223 #if defined (ACE_HAS_WCHAR) 00224 /* 00225 virtual int string_to_addr (const char address[]); 00226 */ 00227 #endif /* ACE_HAS_WCHAR */ 00228 00229 /** 00230 * Sets the port number without affecting the host name. If 00231 * @a encode is enabled then @a port_number is converted into network 00232 * byte order, otherwise it is assumed to be in network byte order 00233 * already and are passed straight through. 00234 */ 00235 void set_port_number (u_short, 00236 int encode = 1); 00237 00238 /** 00239 * Sets the address without affecting the port number. If 00240 * @a encode is enabled then @a ip_addr is converted into network 00241 * byte order, otherwise it is assumed to be in network byte order 00242 * already and are passed straight through. The size of the address 00243 * is specified in the @a len parameter. 00244 * If @a map is non-zero, IPv6 support has been compiled in, and 00245 * @a ip_addr is an IPv4 address, then this address is set to the IPv4-mapped 00246 * IPv6 address of it. 00247 */ 00248 int set_address (const char *ip_addr, 00249 int len, 00250 int encode = 1, 00251 int map = 0); 00252 00253 #if (defined (__linux__) || defined (ACE_WIN32)) && defined (ACE_HAS_IPV6) 00254 /** 00255 * Sets the interface that should be used for this address. This only has 00256 * an effect when the address is link local, otherwise it does nothing. 00257 */ 00258 int set_interface (const char *intf_name); 00259 #endif /* (__linux__ || ACE_WIN32) && ACE_HAS_IPV6 */ 00260 00261 /// Return the port number, converting it into host byte-order. 00262 u_short get_port_number (void) const; 00263 00264 /** 00265 * Return the character representation of the name of the host, 00266 * storing it in the @a hostname (which is assumed to be 00267 * @a hostnamelen bytes long). This version is reentrant. If 00268 * @a hostnamelen is greater than 0 then @a hostname will be 00269 * NUL-terminated even if -1 is returned. 00270 */ 00271 int get_host_name (char hostname[], 00272 size_t hostnamelen) const; 00273 00274 #if defined (ACE_HAS_WCHAR) 00275 int get_host_name (wchar_t hostname[], 00276 size_t hostnamelen) const; 00277 #endif /* ACE_HAS_WCHAR */ 00278 00279 /** 00280 * Return the character representation of the hostname. This 00281 * version is non-reentrant since it returns a pointer to a static 00282 * data area. You should therefore either (1) do a "deep copy" of 00283 * the address returned by get_host_name(), e.g., using strdup() or 00284 * (2) use the "reentrant" version of get_host_name() described 00285 * above. 00286 */ 00287 const char *get_host_name (void) const; 00288 00289 /** 00290 * Return the "dotted decimal" Internet address representation of 00291 * the hostname storing it in the @a addr (which is assumed to be 00292 * @a addr_size bytes long). This version is reentrant. 00293 */ 00294 const char *get_host_addr (char *addr, int addr_size) const; 00295 00296 /** 00297 * Return the "dotted decimal" Internet address representation of 00298 * the hostname. This version is non-reentrant since it returns a 00299 * pointer to a static data area. You should therefore either 00300 * (1) do a "deep copy" of the address returned by get_host_addr(), e.g., 00301 * using strdup() or (2) use the "reentrant" version of 00302 * get_host_addr() described above. 00303 */ 00304 const char *get_host_addr (void) const; 00305 00306 /// Return the 4-byte IP address, converting it into host byte 00307 /// order. 00308 ACE_UINT32 get_ip_address (void) const; 00309 00310 /// Return @c true if the IP address is INADDR_ANY or IN6ADDR_ANY. 00311 bool is_any (void) const; 00312 00313 /// Return @c true if the IP address is IPv4/IPv6 loopback address. 00314 bool is_loopback (void) const; 00315 00316 /// Return @c true if the IP address is IPv4/IPv6 multicast address. 00317 bool is_multicast (void) const; 00318 00319 #if defined (ACE_HAS_IPV6) 00320 /// Return @c true if the IP address is IPv6 linklocal address. 00321 bool is_linklocal (void) const; 00322 00323 /// Return @c true if the IP address is IPv4-mapped IPv6 address. 00324 bool is_ipv4_mapped_ipv6 (void) const; 00325 00326 /// Return @c true if the IP address is IPv4-compatible IPv6 address. 00327 bool is_ipv4_compat_ipv6 (void) const; 00328 #endif /* ACE_HAS_IPV6 */ 00329 00330 /** 00331 * Returns @c true if @c this is less than @a rhs. In this context, 00332 * "less than" is defined in terms of IP address and TCP port 00333 * number. This operator makes it possible to use @c ACE_INET_Addrs 00334 * in STL maps. 00335 */ 00336 bool operator < (const ACE_INET_Addr &rhs) const; 00337 00338 /// Compare two addresses for equality. The addresses are considered 00339 /// equal if they contain the same IP address and port number. 00340 bool operator == (const ACE_INET_Addr &SAP) const; 00341 00342 /// Compare two addresses for inequality. 00343 bool operator != (const ACE_INET_Addr &SAP) const; 00344 00345 /// A variation of the equality operator, this method only compares the 00346 /// IP address and ignores the port number. 00347 bool is_ip_equal (const ACE_INET_Addr &SAP) const; 00348 00349 /// Computes and returns hash value. 00350 virtual u_long hash (void) const; 00351 00352 /// Dump the state of an object. 00353 void dump (void) const; 00354 00355 /// Declare the dynamic allocation hooks. 00356 ACE_ALLOC_HOOK_DECLARE; 00357 00358 private: 00359 /// Insure that @a hostname is properly null-terminated. 00360 int get_host_name_i (char hostname[], size_t hostnamelen) const; 00361 00362 // Methods to gain access to the actual address of 00363 // the underlying internet address structure. 00364 void *ip_addr_pointer (void) const; 00365 int ip_addr_size (void) const; 00366 int determine_type (void) const; 00367 00368 /// Initialize underlying inet_addr_ to default values 00369 void reset (void); 00370 00371 /// Underlying representation. 00372 /// This union uses the knowledge that the two structures share the 00373 /// first member, sa_family (as all sockaddr structures do). 00374 union 00375 { 00376 sockaddr_in in4_; 00377 #if defined (ACE_HAS_IPV6) 00378 sockaddr_in6 in6_; 00379 #endif /* ACE_HAS_IPV6 */ 00380 } inet_addr_; 00381 }; 00382 00383 ACE_END_VERSIONED_NAMESPACE_DECL 00384 00385 #if defined (__ACE_INLINE__) 00386 #include "ace/INET_Addr.inl" 00387 #endif /* __ACE_INLINE__ */ 00388 00389 #include /**/ "ace/post.h" 00390 #endif /* ACE_INET_ADDR_H */