Loading...
Searching...
No Matches
ASSA::INETAddress Class Reference

`#include <INETAddress.h>`

Inheritance diagram for ASSA::INETAddress:

## Public Types

enum | Protocol { TCP , UDP }
Public Types inherited from ASSA::Address
enum | addr_state_t { goodbit =0 , badbit =1 }
| State bits. More...
typedef int | addrstate

## 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.
SA * | getAddress () 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.
Public Member Functions inherited from ASSA::Address
| Address ()
| Constructor.
virtual | ~Address ()
| Destructor.
bool | good () const
| Valid address is constructed.
bool | bad () const
| Indicates whether there was error during address construction process i.e.
| operator void * () const
| Conversion to void * (or bool) for testing where bool is required (in conditional statements).
bool | operator! () const
| Alias to bad ().

## 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.

## Additional Inherited Members

Protected Member Functions inherited from ASSA::Address
void | setstate (addrstate flag_)
| Set state of the Address object.

## Detailed Description

Definition at line 27 of file INETAddress.h.

## Member Enumeration Documentation

## Protocol

enum ASSA::INETAddress::Protocol

Enumerator
TCP | TCP protocol.
UDP | UDP protocol.

Definition at line 30 of file INETAddress.h.

{
TCP,
UDP
};
ASSA::INETAddress::UDP
@ UDP
UDP protocol.
Definition INETAddress.h:32
ASSA::INETAddress::TCP
@ TCP
TCP protocol.
Definition INETAddress.h:31

## Constructor & Destructor Documentation

## INETAddress() [1/8]

INETAddress::INETAddress | ( |  | ) |

Default constructor.

Definition at line 38 of file INETAddress.cpp.

{
// trace_with_mask("INETAddress::INETAddress()",SOCKTRACE);
init ();
}
ASSA::INETAddress::init
void init()
Perform initialization common to all ctors.
Definition INETAddress.cpp:33

References init().

## INETAddress() [2/8]

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.

{
// 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_);
}
ASSA::INETAddress::m_address
SA_IN m_address
Internet address structure sockaddr_in.
Definition INETAddress.h:172

References init(), and m_address.

## INETAddress() [3/8]

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.

{
// trace_with_mask("INETAddress::INETAddress(host, port)",SOCKTRACE);
init ();
createHostPort (host_, htons (port_));
}
ASSA::INETAddress::createHostPort
void createHostPort(const char *host_, int port_)
Makes socket address out of host name and port.
Definition INETAddress.cpp:159

References createHostPort(), and init().

## INETAddress() [4/8]

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.

{
// trace_with_mask("INETAddress::INETAddress(host, port, protocol)",
// SOCKTRACE);
init ();
createHostPort (host_, getServiceByName (service_,protocol_));
}
ASSA::INETAddress::getServiceByName
int getServiceByName(string serv_, Protocol prot_=TCP)
Lookup port by its service name found in /etc/services.
Definition INETAddress.cpp:138

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

## INETAddress() [5/8]

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.

{
// trace_with_mask("INETAddress::INETAddress(port)",SOCKTRACE);
init ();
createHostPort ("", htons (port_));
}

References createHostPort(), and init().

## INETAddress() [6/8]

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.

{
// 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);
}
u_int
unsigned int u_int
Definition Logger_Impl.h:40

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

## INETAddress() [7/8]

INETAddress::INETAddress | ( | SA_IN * | address_ | ) |

Copy constructor.

Definition at line 71 of file INETAddress.cpp.

{
// trace_with_mask("INETAddress::INETAddress(SA_IN*)",SOCKTRACE);
init ();
::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
}
ASSA::SA_IN
struct sockaddr_in SA_IN
Definition Address.h:34

References init(), and m_address.

## INETAddress() [8/8]

INETAddress::INETAddress | ( | SA * | address_ | ) |

Copy constructor from base address.

Definition at line 80 of file INETAddress.cpp.

{
// trace_with_mask("INETAddress::INETAddress(SA*)",SOCKTRACE);
init ();
::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
}

References init(), and m_address.

## ~INETAddress()

ASSA::INETAddress::~INETAddress | ( |  | ) |

inline

Destructor.

Definition at line 102 of file INETAddress.h.

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

## Member Function Documentation

## createHostPort()

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.

{
// 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_;
}
EL
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
ASSA::Address::setstate
void setstate(addrstate flag_)
Set state of the Address object.
Definition Address.h:111
ASSA::Address::badbit
@ badbit
bad state
Definition Address.h:56
ASSA::ASSAERR
@ ASSAERR
ASSA and system errors.
Definition LogMask.h:34

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

Referenced by INETAddress(), INETAddress(), INETAddress(), and INETAddress().

## dump()

void INETAddress::dump | ( |  | ) |

virtual

Dump the address content to log file.

Reimplemented from ASSA::Address.

Definition at line 205 of file INETAddress.cpp.

{
// 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)));
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::Address::dump
virtual void dump()
Dump object state to the log file.
Definition Address.h:101
ASSA::INETAddress::getHostName
string getHostName()
Return host name.
Definition INETAddress.cpp:185
ASSA::INETAddress::getPort
int getPort() const
Return port.
Definition INETAddress.h:116
ASSA::ADDRESS
@ ADDRESS
Class Address & friends messages.
Definition LogMask.h:51

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

## get_fully_qualified_domain_name()

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.

{
// 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;
}
ASSA::INETAddress::m_fqdn_cache
static string m_fqdn_cache
Cached fully-qualified domain name.
Definition INETAddress.h:168

References ASSA::ADDRESS, EL, and m_fqdn_cache.

## getAddress()

SA * ASSA::INETAddress::getAddress | ( |  | ) | const

inline virtual

Get hold of address structure.

Implements ASSA::Address.

Definition at line 110 of file INETAddress.h.

{ return (SA*) &m_address; }
ASSA::SA
struct sockaddr SA
Definition Address.h:33

References m_address.

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

## getHostName()

string INETAddress::getHostName | ( |  | ) |

Return host name.

Definition at line 184 of file INETAddress.cpp.

{
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;
}

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

Referenced by dump().

## getLength()

const int ASSA::INETAddress::getLength | ( |  | ) | const

inline virtual

Return address length.

Implements ASSA::Address.

Definition at line 107 of file INETAddress.h.

{ return sizeof (m_address); }

References m_address.

## getPort()

int ASSA::INETAddress::getPort | ( |  | ) | const

inline

Return port.

Definition at line 116 of file INETAddress.h.

{ return ntohs (m_address.sin_port); }

References m_address.

Referenced by dump().

## getServiceByName()

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.

{
// 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;
}

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

Referenced by INETAddress(), and INETAddress().

## init()

void INETAddress::init | ( |  | ) |

private

Perform initialization common to all ctors.

Definition at line 32 of file INETAddress.cpp.

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

References m_address.

Referenced by INETAddress(), INETAddress(), INETAddress(), INETAddress(), INETAddress(), INETAddress(), INETAddress(), and INETAddress().

## Member Data Documentation

## m_address

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(), INETAddress(), INETAddress(), and init().

## m_fqdn_cache

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:

- INETAddress.h
- INETAddress.cpp