Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

INET_Addr.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    INET_Addr.h
00006  *
00007  *  $Id: INET_Addr.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
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 #if defined(VXWORKS)
00026 #  include /**/ "inetLib.h"
00027 #endif /* VXWORKS */
00028 
00029 /**
00030  * @class ACE_INET_Addr
00031  *
00032  * @brief Defines a C++ wrapper facade for the Internet domain address
00033  * family format.
00034  */
00035 class ACE_Export ACE_INET_Addr : public ACE_Addr
00036 {
00037 public:
00038   // = Initialization methods.
00039 
00040   /// Default constructor.
00041   ACE_INET_Addr (void);
00042 
00043   /// Copy constructor.
00044   ACE_INET_Addr (const ACE_INET_Addr &);
00045 
00046   /// Creates an <ACE_INET_Addr> from a sockaddr_in structure.
00047   ACE_INET_Addr (const sockaddr_in *, int len);
00048 
00049   /// Creates an <ACE_INET_Addr> from a <port_number> and the remote
00050   /// <host_name>. The port number is assumed to be in host byte order.
00051   /// To set a port already in network byte order, please @see set().
00052   /// Use address_family to select IPv6 (PF_INET6) vs. IPv4 (PF_INET).
00053   ACE_INET_Addr (u_short port_number,
00054                  const char host_name[],
00055                  int address_family = AF_UNSPEC);
00056 
00057   /**
00058    * Initializes an <ACE_INET_Addr> from the <address>, which can be
00059    * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or
00060    * "128.252.166.57:1234").  If there is no ':' in the <address> it
00061    * is assumed to be a port number, with the IP address being
00062    * INADDR_ANY.
00063    */
00064   ACE_EXPLICIT ACE_INET_Addr (const char address[]);
00065 
00066   /**
00067    * Creates an <ACE_INET_Addr> from a <port_number> and an Internet
00068    * <ip_addr>.  This method assumes that <port_number> and <ip_addr>
00069    * are in host byte order. If you have addressing information in
00070    * network byte order, @see set().
00071    */
00072   ACE_INET_Addr (u_short port_number,
00073                  ACE_UINT32 ip_addr = INADDR_ANY);
00074 
00075   /// Uses <getservbyname> to create an <ACE_INET_Addr> from a
00076   /// <port_name>, the remote <host_name>, and the <protocol>.
00077   ACE_INET_Addr (const char port_name[],
00078                  const char host_name[],
00079                  const char protocol[] = "tcp");
00080 
00081   /**
00082    * Uses <getservbyname> to create an <ACE_INET_Addr> from a
00083    * <port_name>, an Internet <ip_addr>, and the <protocol>.  This
00084    * method assumes that <ip_addr> is in host byte order.
00085    */
00086   ACE_INET_Addr (const char port_name[],
00087                  ACE_UINT32 ip_addr,
00088                  const char protocol[] = "tcp");
00089 
00090 #if defined (ACE_HAS_WCHAR)
00091   ACE_INET_Addr (u_short port_number,
00092                  const wchar_t host_name[],
00093                  int address_family = AF_UNSPEC);
00094 
00095   ACE_EXPLICIT ACE_INET_Addr (const wchar_t address[]);
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 <port_number> and the
00118    * remote <host_name>.  If <encode> is non-zero then <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   int set (u_short port_number,
00137            ACE_UINT32 ip_addr = INADDR_ANY,
00138            int encode = 1);
00139 
00140   /// Uses <getservbyname> to initialize an <ACE_INET_Addr> from a
00141   /// <port_name>, the remote <host_name>, and the <protocol>.
00142   int set (const char port_name[],
00143            const char host_name[],
00144            const char protocol[] = "tcp");
00145 
00146   /**
00147    * Uses <getservbyname> to initialize an <ACE_INET_Addr> from a
00148    * <port_name>, an <ip_addr>, and the <protocol>.  This assumes that
00149    * <ip_addr> is already in network byte order.
00150    */
00151   int set (const char port_name[],
00152            ACE_UINT32 ip_addr,
00153            const char protocol[] = "tcp");
00154 
00155   /**
00156    * Initializes an <ACE_INET_Addr> from the <addr>, which can be
00157    * "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or
00158    * "128.252.166.57:1234").  If there is no ':' in the <address> it
00159    * is assumed to be a port number, with the IP address being
00160    * INADDR_ANY.
00161    */
00162   int set (const char addr[]);
00163 
00164   /// Creates an <ACE_INET_Addr> from a sockaddr_in structure.
00165   int set (const sockaddr_in *,
00166            int len);
00167 
00168 #if defined (ACE_HAS_WCHAR)
00169   int set (u_short port_number,
00170            const wchar_t host_name[],
00171            int encode = 1,
00172            int address_family = AF_UNSPEC);
00173 
00174   int set (const wchar_t port_name[],
00175            const wchar_t host_name[],
00176            const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
00177 
00178   int set (const wchar_t port_name[],
00179            ACE_UINT32 ip_addr,
00180            const wchar_t protocol[] = ACE_TEXT_WIDE ("tcp"));
00181 
00182   int set (const wchar_t addr[]);
00183 #endif /* ACE_HAS_WCHAR */
00184 
00185   /// Return a pointer to the underlying network address.
00186   virtual void *get_addr (void) const;
00187   int get_addr_size(void) const;
00188 
00189   /// Set a pointer to the address.
00190   virtual void set_addr (void *, int len);
00191 
00192   /**
00193    * Transform the current <ACE_INET_Addr> address into string format.
00194    * If <ipaddr_format> is non-0 this produces "ip-number:port-number"
00195    * (e.g., "128.252.166.57:1234"), whereas if <ipaddr_format> is 0
00196    * this produces "ip-name:port-number" (e.g.,
00197    * "tango.cs.wustl.edu:1234").  Returns -1 if the <size> of the
00198    * <buffer> is too small, else 0.
00199    */
00200   virtual int addr_to_string (ACE_TCHAR buffer[],
00201                               size_t size,
00202                               int ipaddr_format = 1) const;
00203 
00204   /**
00205    * Initializes an <ACE_INET_Addr> from the <address>, which can be
00206    * "ip-addr:port-number" (e.g., "tango.cs.wustl.edu:1234"),
00207    * "ip-addr:port-name" (e.g., "tango.cs.wustl.edu:telnet"),
00208    * "ip-number:port-number" (e.g., "128.252.166.57:1234"), or
00209    * "ip-number:port-name" (e.g., "128.252.166.57:telnet").  If there
00210    * is no ':' in the <address> it is assumed to be a port number,
00211    * with the IP address being INADDR_ANY.
00212    */
00213   virtual int string_to_addr (const char address[]);
00214 
00215 #if defined (ACE_HAS_WCHAR)
00216   /*
00217   virtual int string_to_addr (const char address[]);
00218   */
00219 #endif /* ACE_HAS_WCHAR */
00220 
00221   /**
00222    * Sets the port number without affecting the host name.  If
00223    * <encode> is enabled then <port_number> is converted into network
00224    * byte order, otherwise it is assumed to be in network byte order
00225    * already and are passed straight through.
00226    */
00227   void set_port_number (u_short,
00228                         int encode = 1);
00229 
00230   /**
00231    * Sets the address without affecting the port number.  If
00232    * <encode> is enabled then <ip_addr> is converted into network
00233    * byte order, otherwise it is assumed to be in network byte order
00234    * already and are passed straight through.  The size of the address
00235    * is specified in the <len> parameter.
00236    */
00237   int set_address (const char *ip_addr,
00238                    int len,
00239                    int encode = 1);
00240 
00241   /// Return the port number, converting it into host byte-order.
00242   u_short get_port_number (void) const;
00243 
00244   /**
00245    * Return the character representation of the name of the host,
00246    * storing it in the <hostname> (which is assumed to be
00247    * <hostnamelen> bytes long).  This version is reentrant.  If
00248    * <hostnamelen> is greater than 0 then <hostname> will be
00249    * NUL-terminated even if -1 is returned.
00250    */
00251   int get_host_name (char hostname[],
00252                      size_t hostnamelen) const;
00253 
00254 #if defined (ACE_HAS_WCHAR)
00255   int get_host_name (wchar_t hostname[],
00256                      size_t hostnamelen) const;
00257 #endif /* ACE_HAS_WCHAR */
00258 
00259   /**
00260    * Return the character representation of the hostname (this version
00261    * is non-reentrant since it returns a pointer to a static data
00262    * area).
00263    */
00264   const char *get_host_name (void) const;
00265 
00266   /// Return the "dotted decimal" Internet address.
00267   const char *get_host_addr (void) const;
00268   const char *get_host_addr (char *dst, int size) const;
00269 
00270   /// Return the 4-byte IP address, converting it into host byte
00271   /// order.
00272   ACE_UINT32 get_ip_address (void) const;
00273 
00274   /**
00275    * Returns true if <this> is less than <rhs>.  In this context,
00276    * "less than" is defined in terms of IP address and TCP port
00277    * number.  This operator makes it possible to use <ACE_INET_Addr>s
00278    * in STL maps.
00279    */
00280   int operator < (const ACE_INET_Addr &rhs) const;
00281 
00282   /// Compare two addresses for equality.  The addresses are considered
00283   /// equal if they contain the same IP address and port number.
00284   int operator == (const ACE_INET_Addr &SAP) const;
00285 
00286   /// Compare two addresses for inequality.
00287   int operator != (const ACE_INET_Addr &SAP) const;
00288 
00289   /// Computes and returns hash value.
00290   virtual u_long hash (void) const;
00291 
00292   /// Dump the state of an object.
00293   void dump (void) const;
00294 
00295   /// Declare the dynamic allocation hooks.
00296   ACE_ALLOC_HOOK_DECLARE;
00297 
00298 private:
00299   /// Insure that @a hostname is properly null-terminated.
00300   int get_host_name_i (char hostname[], size_t hostnamelen) const;
00301 
00302   // Methods to gain access to the actual address of
00303   // the underlying internet address structure.
00304   void *ip_addr_pointer (void) const;
00305   int ip_addr_size (void) const;
00306   int determine_type (void) const;
00307 
00308   /// Initialize underlying inet_addr_ to default values
00309   void reset (void);
00310 
00311   /// Underlying representation.
00312   /// This union uses the knowledge that the two structures share the
00313   /// first member, sa_family (as all sockaddr structures do).
00314   union
00315   {
00316     sockaddr_in  in4_;
00317 #if defined (ACE_HAS_IPV6)
00318     sockaddr_in6 in6_;
00319 #endif /* ACE_HAS_IPV6 */
00320   } inet_addr_;
00321 
00322 #if defined (VXWORKS)
00323   char buf_[INET_ADDR_LEN];
00324 #endif
00325 };
00326 
00327 #if defined (__ACE_INLINE__)
00328 #include "ace/INET_Addr.i"
00329 #endif /* __ACE_INLINE__ */
00330 
00331 #include "ace/post.h"
00332 #endif /* ACE_INET_ADDR_H */

Generated on Mon Jun 16 11:19:53 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002