libassa  3.5.1
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ASSA::INETAddress Class Reference

#include <INETAddress.h>

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

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. More...
 
 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. More...
 
 INETAddress (const char *host_, int port_)
 Constructor to create address on the client side given host name and integer port number. More...
 
 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. More...
 
 INETAddress (int port_)
 Constructor to create address of listening socket on a server side from integer port number. More...
 
 INETAddress (const char *address_, Protocol protocol_=TCP)
 Constructor to create address both client- and server-side addresses. More...
 
 INETAddress (SA_IN *address_)
 Copy constructor. More...
 
 INETAddress (SA *address_)
 Copy constructor from base address. More...
 
 ~INETAddress ()
 Destructor. More...
 
const int getLength () const
 Return address length. More...
 
SAgetAddress () const
 Get hold of address structure. More...
 
string getHostName ()
 Return host name. More...
 
int getPort () const
 Return port. More...
 
void dump ()
 Dump the address content to log file. More...
 
- Public Member Functions inherited from ASSA::Address
 Address ()
 Constructor. More...
 
virtual ~Address ()
 Destructor. More...
 
bool good () const
 Valid address is constructed. More...
 
bool bad () const
 Indicates whether there was error during address construction process i.e. More...
 
 operator void * () const
 Conversion to void * (or bool) for testing where bool is required (in conditional statements). More...
 
bool operator! () const
 Alias to bad (). More...
 

Static Public Member Functions

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

Private Member Functions

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

Private Attributes

SA_IN m_address
 Internet address structure sockaddr_in. More...
 

Static Private Attributes

static string m_fqdn_cache
 Cached fully-qualified domain name. More...
 

Additional Inherited Members

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

Detailed Description

Definition at line 27 of file INETAddress.h.

Member Enumeration Documentation

◆ Protocol

Enumerator
TCP 

TCP protocol.

UDP 

UDP protocol.

Definition at line 30 of file INETAddress.h.

30  {
31  TCP,
32  UDP
33  };
@ UDP
UDP protocol.
Definition: INETAddress.h:32
@ 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.

40 {
41 // trace_with_mask("INETAddress::INETAddress()",SOCKTRACE);
42  init ();
43 }
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.

91 {
92 // trace_with_mask("INETAddress::INETAddress(in_addr*,port)",ADDRESS);
93 
94  init ();
95  m_address.sin_addr = *haddr_;
96  m_address.sin_family = AF_INET;
97  m_address.sin_port = htons(port_);
98 }
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.

47 {
48 // trace_with_mask("INETAddress::INETAddress(host, port)",SOCKTRACE);
49  init ();
50  createHostPort (host_, htons (port_));
51 }
void createHostPort(const char *host_, int port_)
Makes socket address out of host name and port.

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.

55 {
56 // trace_with_mask("INETAddress::INETAddress(host, port, protocol)",
57 // SOCKTRACE);
58  init ();
59  createHostPort (host_, getServiceByName (service_,protocol_));
60 }
int getServiceByName(string serv_, Protocol prot_=TCP)
Lookup port by its service name found in /etc/services.

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.

64 {
65 // trace_with_mask("INETAddress::INETAddress(port)",SOCKTRACE);
66 
67  init ();
68  createHostPort ("", htons (port_));
69 }

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.

102 {
103 // trace_with_mask("INETAddress::INETAddress(address, protocol)",ADDRESS);
104 
105  init ();
106 
107  string s(address_);
108  string sPort(s);
109  int r = 0;
110  string host;
111 
112 #ifdef BLOCKED
113  const u_int HOSTNAMELEN = 64;
114  char buf[HOSTNAMELEN]; // 64 on Linux/i386
115  if (gethostname (buf, HOSTNAMELEN) == 0) {
116  host = buf;
117  }
118 #endif
119 
120  if ( (r = s.find(':')) > 0 ) { // host:service
121  host = s.substr(0, r);
122  sPort = s.substr(r+1);
123  }
124  else if ( (r = s.find('@')) > 0 ) { // service@host
125  sPort = s.substr(0, r);
126  host = s.substr(r+1);
127  }
128 
129  if ( (r = getServiceByName (sPort)) == 0 ) { // service
130  return;
131  }
132 
133  createHostPort (host.c_str(), r);
134 }
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.

73 {
74 // trace_with_mask("INETAddress::INETAddress(SA_IN*)",SOCKTRACE);
75 
76  init ();
77  ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
78 }
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.

82 {
83 // trace_with_mask("INETAddress::INETAddress(SA*)",SOCKTRACE);
84 
85  init ();
86  ::memcpy ((void*) &m_address, (const void*) address_, sizeof(SA_IN));
87 }

References init(), and m_address.

◆ ~INETAddress()

ASSA::INETAddress::~INETAddress ( )
inline

Destructor.

Definition at line 102 of file INETAddress.h.

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

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.

160 {
161 // trace_with_mask("INETAddress::createHostPort(char*,int)", ADDRESS);
162 
163  struct hostent* hp = 0;
164 
165  if (strlen (host_) == 0) {
166  m_address.sin_addr.s_addr = htonl(INADDR_ANY);
167  goto done;
168  }
169 
170  if ((hp = gethostbyname (host_)) == NULL) {
172  errno = h_errno;
173  EL((ASSAERR,"gethostbyname (\"%s\") failed\n", host_));
174  return;
175  }
176  memcpy ((char*) &m_address.sin_addr, hp->h_addr_list[0], hp->h_length);
177 
178 done:
179  m_address.sin_family = AF_INET;
180  m_address.sin_port = port_;
181 }
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
void setstate(addrstate flag_)
Set state of the Address object.
Definition: Address.h:111
@ badbit
bad state
Definition: Address.h:56
@ 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().

◆ dump()

void INETAddress::dump ( void  )
virtual

Dump the address content to log file.

Reimplemented from ASSA::Address.

Definition at line 205 of file INETAddress.cpp.

207 {
208 // trace_with_mask("INETAddress::dump", ADDRESS);
209 
210  Address::dump ();
211  DL((ADDRESS,"Family - %s\n", ntohs(m_address.sin_family) == AF_INET ?
212  "AF_INET" : "AF_UNIX"));
213  DL((ADDRESS,"host - %s\n", getHostName ().c_str()));
214  DL((ADDRESS,"port - %d\n", getPort ()));
215  DL((ADDRESS,"address - %s\n", inet_ntoa (m_address.sin_addr)));
216 }
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
virtual void dump()
Dump object state to the log file.
Definition: Address.h:101
string getHostName()
Return host name.
int getPort() const
Return port.
Definition: INETAddress.h:116
@ 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.

232 {
233 // trace_with_mask ("INETAddress::get_fully_qualified_domain_name", ADDRESS);
234 
235  if (m_fqdn_cache.length ()) {
236  return m_fqdn_cache;
237  }
238 
239  struct utsname myname;
240  struct hostent* hptr = NULL;
241 
242 #if defined(WIN32)
243  DWORD slen;
244  slen = sizeof (myname.nodename) - 1;
245  GetComputerNameA (myname.nodename, &slen);
246 #else
247  if (::uname (&myname) < 0) {
248  EL((ADDRESS,"Hostname is not set!\n"));
249  return m_fqdn_cache;
250  }
251 #endif
252 
253  if ((hptr = ::gethostbyname (myname.nodename)) == NULL) {
254  errno = h_errno;
255  EL((ADDRESS,"gethostbyname (%s) failed\n", myname.nodename));
256  return m_fqdn_cache;
257  }
258  m_fqdn_cache = hptr->h_name;
259  char** pptr = hptr->h_aliases;
260  while (*pptr != NULL) {
261  aliases_.push_back (*pptr);
262  pptr++;
263  }
264 
265  return m_fqdn_cache;
266 }
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
inlinevirtual

Get hold of address structure.

Implements ASSA::Address.

Definition at line 110 of file INETAddress.h.

110 { return (SA*) &m_address; }
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.

186 {
187  if (m_address.sin_addr.s_addr == htonl(INADDR_ANY)) {
188  return ("");
189  }
190 
191  struct hostent* hentry;
192  hentry = gethostbyaddr ((const char*) &m_address.sin_addr,
193  sizeof(m_address.sin_addr),
194  AF_INET);
195  if (hentry == NULL) {
196  errno = h_errno;
198  EL((ASSAERR,"gethostbyaddr() failed\n"));
199  return ("");
200  }
201  return hentry->h_name;
202 }

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

Referenced by dump().

◆ getLength()

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

Return address length.

Implements ASSA::Address.

Definition at line 107 of file INETAddress.h.

107 { return sizeof (m_address); }

References m_address.

◆ getPort()

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

Return port.

Definition at line 116 of file INETAddress.h.

116 { 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.

139 {
140 // trace_with_mask("INETAddress::getServiceByName", ADDRESS);
141 
142  long l = 0;
143  struct servent* sp = NULL;
144 
145  if ((l = strtol (s_.c_str(), (char**) NULL, 10))) {
146  return htons ((unsigned short int) l);
147  }
148 
149  if ((sp = getservbyname (s_.c_str(), (p_==TCP ? "tcp" : "udp")))) {
150  return sp->s_port;
151  }
152 
154  return 0;
155 }

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

Referenced by INETAddress().

◆ init()

void INETAddress::init ( )
private

Perform initialization common to all ctors.

Definition at line 32 of file INETAddress.cpp.

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

References m_address.

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

◆ m_fqdn_cache

string INETAddress::m_fqdn_cache
staticprivate

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: