ASSA::INETAddress Class Reference

#include <INETAddress.h>

Inheritance diagram for ASSA::INETAddress:

Inheritance graph
[legend]
Collaboration diagram for ASSA::INETAddress:

Collaboration graph
[legend]
List of all members.

Public Types

enum  Protocol { TCP, UDP }

Public Member Functions

 INETAddress ()
 Default constructor.
 INETAddress (struct in_addr *haddr_, int port_)
 Constructor to create address on a client side given address in struct in_addr and integer port number.
 INETAddress (const char *host_, int port_)
 Constructor to create address on the client side given host name and integer port number.
 INETAddress (const char *host_, const char *service_, Protocol protocol_=TCP)
 Constructor to create address on the client side given host name and service name (as in /etc/services) and optionally protocol type.
 INETAddress (int port_)
 Constructor to create address of listening socket on a server side from integer port number.
 INETAddress (const char *address_, Protocol protocol_=TCP)
 Constructor to create address both client- and server-side addresses.
 INETAddress (SA_IN *address_)
 Copy constructor.
 INETAddress (SA *address_)
 Copy constructor from base address.
 ~INETAddress ()
 Destructor.
const int getLength () const
 Return address length.
SAgetAddress () const
 Get hold of address structure.
string getHostName ()
 Return host name.
int getPort () const
 Return port.
void dump ()
 Dump the address content to log file.

Static Public Member Functions

static string get_fully_qualified_domain_name (vector< string > &aliases_)
 Return fully-qualified host name.

Private Member Functions

void createHostPort (const char *host_, int port_)
 Makes socket address out of host name and port.
int getServiceByName (string serv_, Protocol prot_=TCP)
 Lookup port by its service name found in /etc/services.
void init ()
 Perform initialization common to all ctors.

Private Attributes

SA_IN m_address
 Internet address structure sockaddr_in.

Static Private Attributes

static string m_fqdn_cache
 Cached fully-qualified domain name.

Detailed Description

Definition at line 27 of file INETAddress.h.


Member Enumeration Documentation

enum ASSA::INETAddress::Protocol
 

Enumerator:
TCP  TCP protocol.
UDP  UDP protocol.

Definition at line 30 of file INETAddress.h.

00030                   { 
00031         TCP,                    
00032         UDP                     
00033     };


Constructor & Destructor Documentation

INETAddress::INETAddress  ) 
 

Default constructor.

Definition at line 38 of file INETAddress.cpp.

References init().

00039 {
00040 //  trace_with_mask("INETAddress::INETAddress()",SOCKTRACE);
00041     init ();
00042 }

INETAddress::INETAddress struct in_addr *  haddr_,
int  port_
 

Constructor to create address on a client side given address in struct in_addr and integer port number.

Parameters:
haddr_ XDR-encoded server host address structure
port_ Server listening port

Definition at line 89 of file INETAddress.cpp.

References init(), and m_address.

00090 {
00091 //  trace_with_mask("INETAddress::INETAddress(in_addr*,port)",ADDRESS);
00092 
00093     init ();
00094     m_address.sin_addr = *haddr_;
00095     m_address.sin_family = AF_INET;
00096     m_address.sin_port = htons(port_);
00097 }

INETAddress::INETAddress const char *  host_,
int  port_
 

Constructor to create address on the client side given host name and integer port number.

Parameters:
host_ server host name
port_ port server listens on

Definition at line 45 of file INETAddress.cpp.

References createHostPort(), and init().

00046 {
00047 //  trace_with_mask("INETAddress::INETAddress(host, port)",SOCKTRACE);
00048     init ();
00049     createHostPort (host_, htons (port_));
00050 }

INETAddress::INETAddress const char *  host_,
const char *  service_,
Protocol  protocol_ = TCP
 

Constructor to create address on the client side given host name and service name (as in /etc/services) and optionally protocol type.

Parameters:
host_ Server host name
service_ Server listening port
protocol_ protocol: TCP (default) or UDP

Definition at line 53 of file INETAddress.cpp.

References createHostPort(), getServiceByName(), and init().

00054 {
00055 //  trace_with_mask("INETAddress::INETAddress(host, port, protocol)",
00056 //                  SOCKTRACE);
00057     init ();
00058     createHostPort (host_, getServiceByName (service_,protocol_));
00059 }

INETAddress::INETAddress int  port_  ) 
 

Constructor to create address of listening socket on a server side from integer port number.

Parameters:
port_ port to use

Definition at line 62 of file INETAddress.cpp.

References createHostPort(), and init().

00063 {
00064 //  trace_with_mask("INETAddress::INETAddress(port)",SOCKTRACE);
00065 
00066     init ();
00067     createHostPort ("", htons (port_));
00068 }

INETAddress::INETAddress const char *  address_,
Protocol  protocol_ = TCP
 

Constructor to create address both client- and server-side addresses.

Address is derived from the character string address_ which can be specified in one of the following formats:

Formats:

  • [host:]service
  • service[@host]

If host is omitted, wildcard (INADDR_ANY) address is assumed. This essentially creates an address of the server's listening socket.

To create client-side address, use localhost (or host) name instead.

service entry can be either port name as listed in /etc/services or integer port value.

Parameters:
address_ Address string.
protocol_ Protocol: TCP (by default) or UDP.

Definition at line 100 of file INETAddress.cpp.

References createHostPort(), getServiceByName(), and init().

00101 {
00102 //  trace_with_mask("INETAddress::INETAddress(address, protocol)",ADDRESS);
00103 
00104     init ();
00105 
00106     string s(address_);
00107     string sPort(s);
00108     int r = 0;
00109     string host;
00110 
00111 #ifdef BLOCKED
00112     const u_int HOSTNAMELEN = 64;
00113     char buf[HOSTNAMELEN]; // 64 on Linux/i386
00114     if (gethostname (buf, HOSTNAMELEN) == 0) {
00115         host = buf;
00116     }
00117 #endif
00118 
00119     if ( (r = s.find(':')) > 0 ) { // host:service
00120         host = s.substr(0, r);
00121         sPort = s.substr(r+1);
00122     }
00123     else if ( (r = s.find('@')) > 0 ) { // service@host
00124         sPort = s.substr(0, r);
00125         host = s.substr(r+1);
00126     }
00127 
00128     if ( (r = getServiceByName (sPort)) == 0 ) { // service
00129         return;     
00130     }
00131 
00132     createHostPort (host.c_str(), r);
00133 }

INETAddress::INETAddress SA_IN address_  ) 
 

Copy constructor.

Definition at line 71 of file INETAddress.cpp.

References init(), and m_address.

00072 {
00073 //  trace_with_mask("INETAddress::INETAddress(SA_IN*)",SOCKTRACE);
00074 
00075     init ();
00076     ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
00077 }

INETAddress::INETAddress SA address_  ) 
 

Copy constructor from base address.

Definition at line 80 of file INETAddress.cpp.

References init(), and m_address.

00081 {
00082 //  trace_with_mask("INETAddress::INETAddress(SA*)",SOCKTRACE);
00083 
00084     init ();
00085     ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
00086 }

ASSA::INETAddress::~INETAddress  )  [inline]
 

Destructor.

Definition at line 102 of file INETAddress.h.

00102                     { 
00103 //      trace_with_mask("INETAddress::~INETAddress",SOCKTRACE);
00104     }


Member Function Documentation

void INETAddress::createHostPort const char *  host_,
int  port_
[private]
 

Makes socket address out of host name and port.

Host name is either a host name, or an IPv4 address in standard dot notation, or an IPv6 address in colon (and possibly dot) notation. If it is in dot notation, no lookup is performed.

Otherwise, lookup is performed by consulting name resolution services in order specified in in /etc/host.conf file (named(8) first, then /etc/hosts, and so on).

Port port_ must be supplied in network-independent byte order. If host_ is an empty string, then local host name is assumed.

If failed, state of the object is set to bad, and errno indicates the error occured.

Definition at line 158 of file INETAddress.cpp.

References ASSA::ASSAERR, ASSA::Address::badbit, EL, h_errno, m_address, and ASSA::Address::setstate().

Referenced by INETAddress().

00159 {
00160 //  trace_with_mask("INETAddress::createHostPort(char*,int)", ADDRESS);
00161 
00162     struct hostent* hp = 0;
00163 
00164     if (strlen (host_) == 0) {
00165         m_address.sin_addr.s_addr = htonl(INADDR_ANY);
00166         goto done;
00167     }
00168 
00169     if ((hp = gethostbyname (host_)) == NULL) {
00170         setstate (Address::badbit);
00171         errno = h_errno;
00172         EL((ASSAERR,"gethostbyname (\"%s\") failed\n", host_));
00173         return;
00174     }
00175     memcpy ((char*) &m_address.sin_addr, hp->h_addr_list[0], hp->h_length);
00176 
00177 done:
00178     m_address.sin_family = AF_INET;
00179     m_address.sin_port = port_;
00180 }

void INETAddress::dump  )  [virtual]
 

Dump the address content to log file.

Reimplemented from ASSA::Address.

Definition at line 205 of file INETAddress.cpp.

References ASSA::ADDRESS, DL, ASSA::Address::dump(), getHostName(), getPort(), and m_address.

00206 {
00207 //  trace_with_mask("INETAddress::dump", ADDRESS);
00208 
00209     Address::dump ();
00210     DL((ADDRESS,"Family  - %s\n", ntohs(m_address.sin_family) == AF_INET ? 
00211         "AF_INET" : "AF_UNIX"));
00212     DL((ADDRESS,"host    - %s\n", getHostName ().c_str()));
00213     DL((ADDRESS,"port    - %d\n", getPort ()));
00214     DL((ADDRESS,"address - %s\n", inet_ntoa (m_address.sin_addr)));
00215 }

string INETAddress::get_fully_qualified_domain_name vector< string > &  aliases_  )  [static]
 

Return fully-qualified host name.

Note that a host can have name aliases. If it does, they are returned via argument.

Parameters:
aliases_ List of host aliases, if any
Returns:
fully-qualified host name.

Definition at line 230 of file INETAddress.cpp.

References m_fqdn_cache.

00231 {
00232 //  trace_with_mask ("INETAddress::get_fully_qualified_domain_name", ADDRESS);
00233 
00234     if (m_fqdn_cache.length ()) {
00235         return m_fqdn_cache;
00236     }
00237 
00238     struct utsname myname;
00239     struct hostent* hptr = NULL;
00240 
00241 #if defined(WIN32)
00242     DWORD slen;
00243     slen = sizeof (myname.nodename) - 1;
00244     GetComputerNameA (myname.nodename, &slen);
00245 #else
00246     if (::uname (&myname) < 0) {
00247         EL((ADDRESS,"Hostname is not set!\n"));
00248         return m_fqdn_cache;
00249     }
00250 #endif
00251 
00252     if ((hptr = ::gethostbyname (myname.nodename)) == NULL) {
00253         errno = h_errno;
00254         EL((ADDRESS,"gethostbyname (%s) failed\n", myname.nodename));
00255         return m_fqdn_cache;
00256     }
00257     m_fqdn_cache = hptr->h_name;
00258     char** pptr = hptr->h_aliases;
00259     while (*pptr != NULL) {
00260         aliases_.push_back (*pptr);
00261         pptr++;
00262     }
00263 
00264     return m_fqdn_cache;
00265 }

SA* ASSA::INETAddress::getAddress  )  const [inline, virtual]
 

Get hold of address structure.

Implements ASSA::Address.

Definition at line 110 of file INETAddress.h.

References m_address.

Referenced by ASSA::ConUDPSocket::unconnect().

00110 { return (SA*) &m_address; }

string INETAddress::getHostName  ) 
 

Return host name.

Definition at line 184 of file INETAddress.cpp.

References ASSA::ASSAERR, ASSA::Address::badbit, EL, h_errno, m_address, and ASSA::Address::setstate().

Referenced by dump().

00185 {
00186     if (m_address.sin_addr.s_addr == htonl(INADDR_ANY)) {
00187         return ("");
00188     }
00189 
00190     struct hostent* hentry;
00191     hentry = gethostbyaddr ((const char*) &m_address.sin_addr, 
00192                             sizeof(m_address.sin_addr),
00193                             AF_INET);
00194     if (hentry == NULL) {
00195         errno = h_errno;
00196         setstate (Address::badbit);
00197         EL((ASSAERR,"gethostbyaddr() failed\n"));
00198         return ("");
00199     }
00200     return hentry->h_name;
00201 }

const int ASSA::INETAddress::getLength  )  const [inline, virtual]
 

Return address length.

Implements ASSA::Address.

Definition at line 107 of file INETAddress.h.

References m_address.

00107 { return sizeof (m_address); }

int ASSA::INETAddress::getPort  )  const [inline]
 

Return port.

Definition at line 116 of file INETAddress.h.

References m_address.

Referenced by dump().

00116 { return ntohs (m_address.sin_port); }

int INETAddress::getServiceByName string  serv_,
Protocol  prot_ = TCP
[private]
 

Lookup port by its service name found in /etc/services.

serv_ is either service name, or integer port number. If it is integer port number, it is converted to the network-independent byte order and no lookup is performed.

Parameters:
serv_ Service name.
prot_ Protocol: tcp (default) or udp.
Returns:
Port number in the network-independent byte order, or 0 if lookup failed.

Definition at line 137 of file INETAddress.cpp.

References ASSA::Address::badbit, ASSA::Address::setstate(), and TCP.

Referenced by INETAddress().

00138 {
00139 //  trace_with_mask("INETAddress::getServiceByName", ADDRESS);
00140 
00141     long l = 0;
00142     struct servent* sp = NULL;
00143 
00144     if ((l = strtol (s_.c_str(), (char**) NULL, 10))) {
00145         return htons ((unsigned short int) l);
00146     }
00147 
00148     if ((sp = getservbyname (s_.c_str(), (p_==TCP ? "tcp" : "udp")))) {
00149          return sp->s_port;
00150     }
00151 
00152     setstate (Address::badbit);
00153     return 0;
00154 }

void INETAddress::init  )  [private]
 

Perform initialization common to all ctors.

Definition at line 32 of file INETAddress.cpp.

References m_address.

Referenced by INETAddress().

00033 {
00034     ::memset ((char*) &m_address, 0, sizeof(m_address));
00035 }


Member Data Documentation

SA_IN ASSA::INETAddress::m_address [private]
 

Internet address structure sockaddr_in.

Definition at line 172 of file INETAddress.h.

Referenced by createHostPort(), dump(), getAddress(), getHostName(), getLength(), getPort(), INETAddress(), and init().

string INETAddress::m_fqdn_cache [static, private]
 

Cached fully-qualified domain name.

Definition at line 168 of file INETAddress.h.

Referenced by get_fully_qualified_domain_name().


The documentation for this class was generated from the following files:
Generated on Sun Aug 13 15:08:20 2006 for libassa by  doxygen 1.4.6