libassa 3.5.0
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

ASSA::INETAddress Class Reference

#include <INETAddress.h>

Inheritance diagram for ASSA::INETAddress:
ASSA::Address

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

Enumerator:
TCP 

TCP protocol.

UDP 

UDP protocol.

Definition at line 30 of file INETAddress.h.

                  { 
        TCP,                    
        UDP                     
    };

Constructor & Destructor Documentation

INETAddress::INETAddress ( )

Default constructor.

Definition at line 39 of file INETAddress.cpp.

References init().

{
//  trace_with_mask("INETAddress::INETAddress()",SOCKTRACE);
    init ();
}
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 90 of file INETAddress.cpp.

References init(), and m_address.

{
//  trace_with_mask("INETAddress::INETAddress(in_addr*,port)",ADDRESS);

    init ();
    m_address.sin_addr = *haddr_;
    m_address.sin_family = AF_INET;
    m_address.sin_port = htons(port_);
}
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 46 of file INETAddress.cpp.

References createHostPort(), and init().

{
//  trace_with_mask("INETAddress::INETAddress(host, port)",SOCKTRACE);
    init ();
    createHostPort (host_, htons (port_));
}
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 54 of file INETAddress.cpp.

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

{
//  trace_with_mask("INETAddress::INETAddress(host, port, protocol)",
//                  SOCKTRACE);
    init ();
    createHostPort (host_, getServiceByName (service_,protocol_));
}
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 63 of file INETAddress.cpp.

References createHostPort(), and init().

{
//  trace_with_mask("INETAddress::INETAddress(port)",SOCKTRACE);

    init ();
    createHostPort ("", htons (port_));
}
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 101 of file INETAddress.cpp.

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

{
//  trace_with_mask("INETAddress::INETAddress(address, protocol)",ADDRESS);

    init ();

    string s(address_);
    string sPort(s);
    int r = 0;
    string host;

#ifdef BLOCKED
    const u_int HOSTNAMELEN = 64;
    char buf[HOSTNAMELEN]; // 64 on Linux/i386
    if (gethostname (buf, HOSTNAMELEN) == 0) {
        host = buf;
    }
#endif

    if ( (r = s.find(':')) > 0 ) { // host:service
        host = s.substr(0, r);
        sPort = s.substr(r+1);
    }
    else if ( (r = s.find('@')) > 0 ) { // service@host
        sPort = s.substr(0, r);
        host = s.substr(r+1);
    }

    if ( (r = getServiceByName (sPort)) == 0 ) { // service
        return;     
    }

    createHostPort (host.c_str(), r);
}
INETAddress::INETAddress ( SA_IN address_)

Copy constructor.

Definition at line 72 of file INETAddress.cpp.

References init(), and m_address.

{
//  trace_with_mask("INETAddress::INETAddress(SA_IN*)",SOCKTRACE);

    init ();
    ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
}
INETAddress::INETAddress ( SA address_)

Copy constructor from base address.

Definition at line 81 of file INETAddress.cpp.

References init(), and m_address.

{
//  trace_with_mask("INETAddress::INETAddress(SA*)",SOCKTRACE);

    init ();
    ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
}
ASSA::INETAddress::~INETAddress ( ) [inline]

Destructor.

Definition at line 102 of file INETAddress.h.

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

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 159 of file INETAddress.cpp.

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

Referenced by INETAddress().

{
//  trace_with_mask("INETAddress::createHostPort(char*,int)", ADDRESS);

    struct hostent* hp = 0;

    if (strlen (host_) == 0) {
        m_address.sin_addr.s_addr = htonl(INADDR_ANY);
        goto done;
    }

    if ((hp = gethostbyname (host_)) == NULL) {
        setstate (Address::badbit);
        errno = h_errno;
        EL((ASSAERR,"gethostbyname (\"%s\") failed\n", host_));
        return;
    }
    memcpy ((char*) &m_address.sin_addr, hp->h_addr_list[0], hp->h_length);

done:
    m_address.sin_family = AF_INET;
    m_address.sin_port = port_;
}
void INETAddress::dump ( void  ) [virtual]

Dump the address content to log file.

Reimplemented from ASSA::Address.

Definition at line 206 of file INETAddress.cpp.

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

{
//  trace_with_mask("INETAddress::dump", ADDRESS);

    Address::dump ();
    DL((ADDRESS,"Family  - %s\n", ntohs(m_address.sin_family) == AF_INET ? 
        "AF_INET" : "AF_UNIX"));
    DL((ADDRESS,"host    - %s\n", getHostName ().c_str()));
    DL((ADDRESS,"port    - %d\n", getPort ()));
    DL((ADDRESS,"address - %s\n", inet_ntoa (m_address.sin_addr)));
}
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 231 of file INETAddress.cpp.

References ASSA::ADDRESS, EL, and m_fqdn_cache.

{
//  trace_with_mask ("INETAddress::get_fully_qualified_domain_name", ADDRESS);

    if (m_fqdn_cache.length ()) {
        return m_fqdn_cache;
    }

    struct utsname myname;
    struct hostent* hptr = NULL;

#if defined(WIN32)
    DWORD slen;
    slen = sizeof (myname.nodename) - 1;
    GetComputerNameA (myname.nodename, &slen);
#else
    if (::uname (&myname) < 0) {
        EL((ADDRESS,"Hostname is not set!\n"));
        return m_fqdn_cache;
    }
#endif

    if ((hptr = ::gethostbyname (myname.nodename)) == NULL) {
        errno = h_errno;
        EL((ADDRESS,"gethostbyname (%s) failed\n", myname.nodename));
        return m_fqdn_cache;
    }
    m_fqdn_cache = hptr->h_name;
    char** pptr = hptr->h_aliases;
    while (*pptr != NULL) {
        aliases_.push_back (*pptr);
        pptr++;
    }

    return m_fqdn_cache;
}
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().

{ return (SA*) &m_address; }
string INETAddress::getHostName ( )

Return host name.

Definition at line 185 of file INETAddress.cpp.

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

Referenced by dump().

{
    if (m_address.sin_addr.s_addr == htonl(INADDR_ANY)) {
        return ("");
    }

    struct hostent* hentry;
    hentry = gethostbyaddr ((const char*) &m_address.sin_addr, 
                            sizeof(m_address.sin_addr),
                            AF_INET);
    if (hentry == NULL) {
        errno = h_errno;
        setstate (Address::badbit);
        EL((ASSAERR,"gethostbyaddr() failed\n"));
        return ("");
    }
    return hentry->h_name;
}
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.

{ 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().

{ 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 138 of file INETAddress.cpp.

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

Referenced by INETAddress().

{
//  trace_with_mask("INETAddress::getServiceByName", ADDRESS);

    long l = 0;
    struct servent* sp = NULL;

    if ((l = strtol (s_.c_str(), (char**) NULL, 10))) {
        return htons ((unsigned short int) l);
    }

    if ((sp = getservbyname (s_.c_str(), (p_==TCP ? "tcp" : "udp")))) {
         return sp->s_port;
    }

    setstate (Address::badbit);
    return 0;
}
void INETAddress::init ( ) [private]

Perform initialization common to all ctors.

Definition at line 33 of file INETAddress.cpp.

References m_address.

Referenced by INETAddress().

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

Member Data Documentation

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:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines