NLNET::CInetAddress Class Reference

#include <inet_address.h>


Detailed Description

Internet address (IP + port). The structure sockaddr_in is internally in network byte order

Todo:
cado: Test big/little endian transfers to check if byte ordering is ok.
Author:
Olivier Cado

Nevrax France

Date:
2000

Definition at line 64 of file inet_address.h.

Public Member Functions

std::string asIPString () const
 Returns IP address and port as a string. (ex: "195.68.21.195:80").

std::string asString () const
 Returns hostname and port as a string. (ex: "www.nevrax.org:80 (195.68.21.195)").

 CInetAddress (const CInetAddress &other)
 Copy constructor.

 CInetAddress (const std::string &hostNameAndPort)
 CInetAddress (const std::string &hostName, uint16 port)
 Alternate constructor (calls setByName()).

 CInetAddress ()
 Default Constructor. The address is set to INADDR_ANY.

const std::string & hostName () const
 Returns hostname. (ex: "www.nevrax.org").

uint32 internalIPAddress () const
 Returns internal IP address.

uint32 internalNetAddress () const
 Returns the internal network address (it s the network address for example 192.168.0.0 for a C class).

std::string ipAddress () const
 Returns readable IP address. (ex: "195.68.21.195").

bool is127001 () const
 Returns true if this CInetAddress is 127.0.0.1.

bool isValid () const
 Returns if object (address and port) is valid.

CInetAddressoperator= (const CInetAddress &other)
 Assignment operator.

uint16 port () const
 Returns port.

void serial (NLMISC::IStream &s)
 Serialize.

CInetAddresssetByName (const std::string &hostname)
 Resolves a name.

void setNameAndPort (const std::string &hostNameAndPort)
 Sets hostname and port (ex: www.nevrax.com:80).

void setPort (uint16 port)
 Sets port.

void setSockAddr (const sockaddr_in *saddr)
const sockaddr_in * sockAddr () const
 Returns internal socket address (read only).

 ~CInetAddress ()
 Destructor.


Static Public Member Functions

std::vector< CInetAddresslocalAddresses ()
CInetAddress localHost ()
 Creates a CInetAddress object with local host address, port=0.


Static Public Attributes

bool RetrieveNames = false
 If true, setSockAddr() always tries to retrieve the host name from the address.


Protected Member Functions

 CInetAddress (const in_addr *ip)
 Constructor with ip address, port=0.


Private Member Functions

void init ()

Private Attributes

std::string _HostName
sockaddr_in * _SockAddr
bool _Valid

Friends

bool operator< (const CInetAddress &a1, const CInetAddress &a2)
 Comparison < operator.

bool operator== (const CInetAddress &a1, const CInetAddress &a2)
 Comparison == operator.


Constructor & Destructor Documentation

NLNET::CInetAddress::CInetAddress  ) 
 

Default Constructor. The address is set to INADDR_ANY.

Definition at line 64 of file inet_address.cpp.

References _SockAddr, and init().

Referenced by localAddresses(), and localHost().

00065 {
00066         init();
00067         _SockAddr->sin_port = 0; // same as htons(0)
00068         memset( &_SockAddr->sin_addr, 0, sizeof(in_addr) ); // same as htonl(INADDR_ANY)
00069 }

NLNET::CInetAddress::CInetAddress const std::string &  hostName,
uint16  port
 

Alternate constructor (calls setByName()).

Definition at line 98 of file inet_address.cpp.

References init(), setByName(), setPort(), and uint16.

00099 {
00100         init();
00101         setPort( port );
00102         setByName( hostName );
00103 }

NLNET::CInetAddress::CInetAddress const std::string &  hostNameAndPort  ) 
 

Alternate constructor (calls setByName()) example: CInetAddress("www.nevrax.com:80")

Definition at line 109 of file inet_address.cpp.

References init(), and setNameAndPort().

00110 {
00111         init();
00112         setNameAndPort( hostNameAndPort );
00113 }

NLNET::CInetAddress::CInetAddress const CInetAddress other  ) 
 

Copy constructor.

Definition at line 119 of file inet_address.cpp.

References _HostName, _SockAddr, _Valid, and init().

00120 {
00121         init();
00122         _HostName = other._HostName;
00123         memcpy( _SockAddr, other._SockAddr, sizeof( *_SockAddr ) );
00124         _Valid = other._Valid;
00125 }

NLNET::CInetAddress::~CInetAddress  ) 
 

Destructor.

Definition at line 195 of file inet_address.cpp.

References _SockAddr.

00196 {
00197         delete _SockAddr;
00198         // _Valid = false;
00199 }

NLNET::CInetAddress::CInetAddress const in_addr *  ip  )  [protected]
 

Constructor with ip address, port=0.

Definition at line 75 of file inet_address.cpp.

References _HostName, _SockAddr, _Valid, init(), and ipAddress().

00076 {
00077         init();
00078         _SockAddr->sin_port = 0;
00079         memcpy( &_SockAddr->sin_addr, ip, sizeof(in_addr) );
00080 
00081         // get the host name to be displayed
00082         hostent *phostent = gethostbyaddr( (char*)&ip->s_addr, 4,  AF_INET );
00083         if ( phostent == NULL )
00084         {
00085                 _HostName = ipAddress();
00086         }
00087         else
00088         {
00089                 _HostName = string( phostent->h_name );
00090         }
00091         _Valid = true;
00092 }


Member Function Documentation

std::string NLNET::CInetAddress::asIPString  )  const
 

Returns IP address and port as a string. (ex: "195.68.21.195:80").

Definition at line 397 of file inet_address.cpp.

References ipAddress(), port(), and NLMISC::toString().

Referenced by NLNET::CLoginServer::init(), NLNET::NLMISC_DYNVARIABLE(), serial(), and NLNET::setListenAddress().

00398 {
00399 //      stringstream ss;
00400 //      ss << ipAddress() << ":" << port();
00401 //      return ss.str();
00402         return ipAddress() + ":" + NLMISC::toString(port());
00403 }

std::string NLNET::CInetAddress::asString  )  const
 

Returns hostname and port as a string. (ex: "www.nevrax.org:80 (195.68.21.195)").

Definition at line 385 of file inet_address.cpp.

References hostName(), ipAddress(), port(), and NLMISC::toString().

Referenced by NLNET::CUdpSock::bind(), NLNET::cbServerAskUniversalTime(), NLNET::cbShardValidation(), NLNET::CTcpSock::disconnect(), NLNET::CDummyTcpSock::disconnect(), NLNET::CNamingClient::info(), NLNET::CListenSock::init(), NLNET::CNamingClient::lookupAndConnect(), NLNET::IService::main(), NLNET::CUdpSock::receive(), NLNET::CNonBlockingBufSock::receivePart(), NLNET::sendEMailCommand(), and NLNET::CUnifiedNetwork::update().

00386 {
00387 //      stringstream ss;
00388 //      ss << hostName() << ":" << port() << " (" << ipAddress() << ")";
00389 //      return ss.str();
00390         return hostName() + ":" + NLMISC::toString(port()) + " (" + ipAddress() + ")";
00391 }

const string & NLNET::CInetAddress::hostName  )  const
 

Returns hostname. (ex: "www.nevrax.org").

Definition at line 367 of file inet_address.cpp.

References _HostName.

Referenced by asString().

00368 {
00369         return _HostName;
00370 }

void NLNET::CInetAddress::init void   )  [private]
 

Definition at line 180 of file inet_address.cpp.

References _SockAddr, and _Valid.

Referenced by CInetAddress().

00181 {
00182         CSock::initNetwork(); 
00183         
00184         _Valid = false;
00185 
00186         _SockAddr = new sockaddr_in;
00187         _SockAddr->sin_family = AF_INET;
00188         memset( _SockAddr->sin_zero, 0, 8 );
00189 }

uint32 NLNET::CInetAddress::internalIPAddress  )  const
 

Returns internal IP address.

Definition at line 315 of file inet_address.cpp.

References _SockAddr, and uint32.

Referenced by internalNetAddress(), and is127001().

00316 {
00317         return _SockAddr->sin_addr.s_addr;
00318 }

uint32 NLNET::CInetAddress::internalNetAddress  )  const
 

Returns the internal network address (it s the network address for example 192.168.0.0 for a C class).

Definition at line 320 of file inet_address.cpp.

References internalIPAddress(), and uint32.

00321 {
00322         uint32 ip = internalIPAddress();
00323         if ((ip&0x00000080) == 0)
00324         {
00325                 // A class
00326                 return ip & 0x000000FF;
00327         }
00328         else if ((ip&0x00000040) == 0)
00329         {
00330                 // B class
00331                 return ip & 0x0000FFFF;
00332         }
00333         else if ((ip&0x00000020) == 0)
00334         {
00335                 // C class
00336                 return ip & 0x00FFFFFF;
00337         }
00338         else if ((ip&0x00000010) == 0)
00339         {
00340                 // D class
00341                 return ip & 0xFFFFFFFF;
00342         }
00343         else
00344         {
00345                 return ip;
00346         }
00347 }

string NLNET::CInetAddress::ipAddress  )  const
 

Returns readable IP address. (ex: "195.68.21.195").

Definition at line 355 of file inet_address.cpp.

References _SockAddr.

Referenced by asIPString(), asString(), CInetAddress(), localHost(), and setSockAddr().

00356 {
00357         /*stringstream ss; // or use inet_ntoa
00358         ss << inet_ntoa ( _SockAddr->sin_addr );
00359         return ss.str();*/
00360         return string( inet_ntoa( _SockAddr->sin_addr ) );
00361 }

bool NLNET::CInetAddress::is127001  )  const
 

Returns true if this CInetAddress is 127.0.0.1.

Definition at line 503 of file inet_address.cpp.

References internalIPAddress().

00504 {
00505         return (internalIPAddress () == htonl(0x7F000001));
00506 }

bool NLNET::CInetAddress::isValid  )  const
 

Returns if object (address and port) is valid.

Definition at line 297 of file inet_address.cpp.

References _SockAddr, and _Valid.

Referenced by NLNET::CUnifiedNetwork::init(), and NLNET::CUnifiedNetwork::update().

00298 {
00299         return ( _Valid && _SockAddr->sin_port!=0 ); // same as ntohs(0)
00300 }

std::vector< CInetAddress > NLNET::CInetAddress::localAddresses  )  [static]
 

Returns the list of the local host addresses (with port=0) (especially useful if the host is multihomed)

Definition at line 467 of file inet_address.cpp.

References CInetAddress(), and uint.

00468 {
00469         // 1. Get local host name
00470         const uint maxlength = 80;
00471         char localhost [maxlength];
00472         if ( gethostname( localhost, maxlength ) == SOCKET_ERROR )
00473         {
00474                 throw ESocket( "Unable to get local hostname" );
00475         }
00476 
00477         // 2. Get address list
00478         vector<CInetAddress> vect;
00479 
00480         uint i = 0;
00481         do
00482         {
00483                 hostent *phostent = gethostbyname( localhost );
00484                 if ( phostent == NULL )
00485                         throw ESocket( (string("Hostname resolution failed for ")+string(localhost)).c_str() );
00486 
00487                 if (phostent->h_addr_list[i] == 0)
00488                         break;
00489 
00490                 vect.push_back( CInetAddress( (const in_addr*)(phostent->h_addr_list[i]) ) );
00491                 i++;
00492         }
00493         while (true);
00494 
00495         if(vect.empty())
00496         {
00497                 throw ESocket( (string("No network card detected for ")+string(localhost)).c_str() );
00498         }
00499         
00500         return vect;
00501 }

CInetAddress NLNET::CInetAddress::localHost  )  [static]
 

Creates a CInetAddress object with local host address, port=0.

Definition at line 447 of file inet_address.cpp.

References CInetAddress(), ipAddress(), nlwarning, and uint.

00448 {
00449         const uint maxlength = 80;
00450         char localhost [maxlength];
00451         if ( gethostname( localhost, maxlength ) != 0 )
00452                 throw ESocket( "Unable to get local hostname" );
00453         CInetAddress localaddr = CInetAddress( string(localhost) );
00454 
00455         if ( localaddr.ipAddress() == "127.0.0.1" )
00456         {
00457                 nlwarning ("LNETL0: No network card detected! using localhost (127.0.0.1)");
00458         }
00459 
00460         return localaddr;
00461 }

CInetAddress & NLNET::CInetAddress::operator= const CInetAddress other  ) 
 

Assignment operator.

Definition at line 131 of file inet_address.cpp.

References _HostName, _SockAddr, and _Valid.

00132 {
00133         _HostName = other._HostName;
00134         memcpy( _SockAddr, other._SockAddr, sizeof( *_SockAddr ) );
00135         _Valid = other._Valid;
00136         return *this;
00137 }

uint16 NLNET::CInetAddress::port  )  const
 

Returns port.

Definition at line 376 of file inet_address.cpp.

References _SockAddr, and uint16.

Referenced by asIPString(), asString(), and NLNET::operator<().

00377 {
00378         return ntohs( _SockAddr->sin_port );
00379 }

void NLNET::CInetAddress::serial NLMISC::IStream s  ) 
 

Serialize.

Definition at line 409 of file inet_address.cpp.

References _SockAddr, _Valid, asIPString(), NLMISC::IStream::isReading(), s, NLMISC::CMemStream::serial(), setNameAndPort(), setSockAddr(), NLMISC::CMemStream::stringMode(), and uint8.

00410 {
00411         NLMISC::CMemStream *ms = dynamic_cast<NLMISC::CMemStream*>(&s);
00412         if ( ms && ms->stringMode() )
00413         {
00414                 // String stream
00415                 string addrs;
00416                 if ( ms->isReading() )
00417                 {
00418                         ms->serial( addrs );
00419                         setNameAndPort( addrs );
00420                 }
00421                 else
00422                 {
00423                         addrs = asIPString();
00424                         ms->serial( addrs );
00425                 }
00426                 s.serial( _Valid );
00427         }
00428         else
00429         {
00430                 // Binary stream
00431                 s.serialBuffer( (uint8*)_SockAddr, sizeof(*_SockAddr) ); // this is possible only because the contents of _SockAddr is platform-independant !
00432                 s.serial( _Valid );
00433 
00434                 if(_Valid)
00435                 {
00436                         // retreive the fullname
00437                         setSockAddr (_SockAddr);
00438                 }
00439         
00440         }
00441 }

CInetAddress & NLNET::CInetAddress::setByName const std::string &  hostname  ) 
 

Resolves a name.

Definition at line 224 of file inet_address.cpp.

References _HostName, _SockAddr, _Valid, and nldebug.

Referenced by CInetAddress(), and setNameAndPort().

00225 {
00226         // Try to convert directly for addresses such as a.b.c.d
00227         in_addr iaddr;
00228 #ifdef NL_OS_WINDOWS
00229         iaddr.S_un.S_addr = inet_addr( hostName.c_str() );
00230         if ( iaddr.S_un.S_addr == INADDR_NONE )
00231 #elif defined NL_OS_UNIX
00232         iaddr.s_addr = inet_addr( hostName.c_str() );
00233         if ( iaddr.s_addr == INADDR_NONE )
00234 #endif
00235         {
00236 
00237                 // Otherwise use the traditional DNS look-up
00238                 hostent *phostent = gethostbyname( hostName.c_str() );
00239                 if ( phostent == NULL )
00240                 {
00241                         _Valid = false;
00242                         nldebug( "LNETL0: Network error: resolution of hostname '%s' failed", hostName.c_str() );
00243                         // return *this;
00244                         throw ESocket( (string("Hostname resolution failed for ")+hostName).c_str() );
00245                 }
00246                 _HostName = string( phostent->h_name );
00247                 memcpy( &_SockAddr->sin_addr, phostent->h_addr, sizeof(in_addr) );
00248         }
00249         else
00250         {
00251                 _HostName = hostName;
00252                 memcpy( &_SockAddr->sin_addr, &iaddr, sizeof(iaddr) );
00253         }
00254         _Valid = true;
00255         return *this;
00256 }

void NLNET::CInetAddress::setNameAndPort const std::string &  hostNameAndPort  ) 
 

Sets hostname and port (ex: www.nevrax.com:80).

Definition at line 204 of file inet_address.cpp.

References setByName(), setPort(), and uint32.

Referenced by CInetAddress(), and serial().

00205 {
00206         uint32 pos = hostNameAndPort.find_first_of (':');
00207         if (pos != string::npos)
00208         {
00209                 setPort( atoi(hostNameAndPort.substr(pos + 1).c_str()) );
00210         }
00211         else
00212         {
00213                 setPort( 0 );
00214         }
00215 
00216         // if pos == -1, it will copy all the string
00217         setByName( hostNameAndPort.substr (0, pos) );
00218 }

void NLNET::CInetAddress::setPort uint16  port  ) 
 

Sets port.

Definition at line 262 of file inet_address.cpp.

References _SockAddr, and uint16.

Referenced by CInetAddress(), NLNET::CListenSock::init(), and setNameAndPort().

00263 {
00264         _SockAddr->sin_port = htons( port );
00265 
00266 }

void NLNET::CInetAddress::setSockAddr const sockaddr_in *  saddr  ) 
 

Sets internal socket address directly (contents is copied). It also retrieves the host name if CInetAddress::RetrieveNames is true.

Definition at line 272 of file inet_address.cpp.

References _HostName, _SockAddr, _Valid, and ipAddress().

Referenced by serial().

00273 {
00274         memcpy( _SockAddr, saddr, sizeof(*saddr) );
00275 
00276         // Get host name
00277         // Warning: when it can't find it, it take more than 4 seconds
00278         if ( CInetAddress::RetrieveNames )
00279         {
00280                 hostent *phostent = gethostbyaddr( (char*)&saddr->sin_addr.s_addr, 4,  AF_INET );
00281                 if ( phostent == NULL )
00282                 {
00283                         _HostName = ipAddress();
00284                 }
00285                 else
00286                 {
00287                         _HostName = string( phostent->h_name );
00288                 }
00289         }
00290         _Valid = true;
00291 }

const sockaddr_in * NLNET::CInetAddress::sockAddr  )  const
 

Returns internal socket address (read only).

Definition at line 306 of file inet_address.cpp.

References _SockAddr.

Referenced by NLNET::CUdpSock::bind().

00307 {
00308         return _SockAddr;
00309 }


Friends And Related Function Documentation

bool operator< const CInetAddress a1,
const CInetAddress a2
[friend]
 

Comparison < operator.

Definition at line 153 of file inet_address.cpp.

00154 {
00155 #ifdef NL_OS_WINDOWS
00156         if ( a1._SockAddr->sin_addr.S_un.S_addr == a2._SockAddr->sin_addr.S_un.S_addr )
00157         {
00158                 return ( a1.port() < a2.port() );
00159         }
00160         else
00161         {
00162                 return ( a1._SockAddr->sin_addr.S_un.S_addr < a2._SockAddr->sin_addr.S_un.S_addr );
00163         }
00164 #elif defined NL_OS_UNIX
00165         if ( a1._SockAddr->sin_addr.s_addr == a2._SockAddr->sin_addr.s_addr )
00166         {
00167                 return ( a1.port() < a2.port() );
00168         }
00169         else
00170         {
00171                 return ( a1._SockAddr->sin_addr.s_addr < a2._SockAddr->sin_addr.s_addr );
00172         }
00173 #endif  
00174 }

bool operator== const CInetAddress a1,
const CInetAddress a2
[friend]
 

Comparison == operator.

Definition at line 143 of file inet_address.cpp.

00144 {
00145         // Compares the sockaddr structure except the last 8 bytes equal to zero.
00146         return ( memcmp( a1._SockAddr, a2._SockAddr, sizeof(sockaddr_in)-8 ) == 0 );
00147 }


Field Documentation

std::string NLNET::CInetAddress::_HostName [private]
 

Definition at line 161 of file inet_address.h.

Referenced by CInetAddress(), hostName(), operator=(), setByName(), and setSockAddr().

sockaddr_in* NLNET::CInetAddress::_SockAddr [private]
 

Definition at line 162 of file inet_address.h.

Referenced by CInetAddress(), init(), internalIPAddress(), ipAddress(), isValid(), NLNET::operator<(), operator=(), NLNET::operator==(), port(), serial(), setByName(), setPort(), setSockAddr(), sockAddr(), and ~CInetAddress().

bool NLNET::CInetAddress::_Valid [private]
 

Definition at line 163 of file inet_address.h.

Referenced by CInetAddress(), init(), isValid(), operator=(), serial(), setByName(), and setSockAddr().

bool NLNET::CInetAddress::RetrieveNames = false [static]
 

If true, setSockAddr() always tries to retrieve the host name from the address.

Definition at line 58 of file inet_address.cpp.


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:57:36 2004 for NeL by doxygen 1.3.6