nAddress Class Reference

internet address More...

#include <nSocket.h>

Collaboration diagram for nAddress:

Collaboration graph
[legend]

List of all members.

Public Types

enum  { size = sizeof( nAddressBase ) }

Public Member Functions

 nAddress ()
 constructor
 ~nAddress ()
 destructor
nAddress const & ToString (tString &string) const
 turns address to complete string
tString ToString () const
 turns address to complete string
int FromString (const char *string)
 turns complete string into address
void FromHostent (int l, const char *addr)
 copy address from hostent data
nAddressSetHostname (const char *hostname)
 Sets the hostname part of the address (with DNS lookup).
tString GetHostname (void) const
 Gets the hostname part of the address (with DNS lookup).
nAddress const & GetHostname (tString &hostname) const
 Gets the hostname part of the address (with DNS lookup).
nAddressSetAddress (const char *address)
 Sets the hostname part of the address.
tString GetAddress (void) const
 Gets the hostname part of the address.
nAddress const & GetAddress (tString &address) const
 Gets the hostname part of the address.
nAddressSetPort (int port)
 Sets the port of the address.
int GetPort (void) const
 Gets the port of the address.
nAddress const & GetPort (int &port) const
 Gets the port of the address.
bool IsSet () const
 returns true only if address is not INETADDR_ANY
 operator struct sockaddr * ()
 conversion to sockaddr
 operator struct sockaddr const * () const
 conversion to sockaddr
bool operator== (nAddress const &other) const
 comparison operator
bool operator!= (nAddress const &other) const
 comparison operator
unsigned int GetAddressLength (void) const
 Gets the length of the stored address.

Static Public Member Functions

static int Compare (const nAddress &a1, const nAddress &a2)
 compares two addresses

Private Attributes

nAddressBase addr_
 the lowlevel network address
unsigned int addrLen_
 the length of the really stored address


Detailed Description

internet address

Definition at line 66 of file nSocket.h.


Member Enumeration Documentation

anonymous enum

Enumerator:
size 

Definition at line 112 of file nSocket.h.

00112 { size = sizeof( nAddressBase ) };


Constructor & Destructor Documentation

nAddress::nAddress ( void   ) 

constructor

Definition at line 1013 of file nSocket.cpp.

References addr_, nAddressBase::addr_in, addrLen_, and size.

Referenced by SetHostname().

01016 {
01017     // clear address data, it's only POD
01018     memset( &addr_, 0, size );
01019     addrLen_ = size;
01020 
01021     // set to unspecific IP4 address
01022     addr_.addr_in.sin_addr.s_addr = INADDR_ANY;
01023     addr_.addr_in.sin_family = AF_INET;

Here is the caller graph for this function:

nAddress::~nAddress ( void   ) 

destructor

Definition at line 1034 of file nSocket.cpp.

01037 {


Member Function Documentation

const nAddress & nAddress::ToString ( tString string  )  const

turns address to complete string

Parameters:
string the string to fill
Returns:
reference to this for chaining

Definition at line 1050 of file nSocket.cpp.

References addr_, nAddressBase::addr_in, ANET_AddrToString(), and GetPort().

Referenced by nSocket::Bind(), nSocket::Close(), nServerInfo::GetBigServerInfoCommon(), nSocket::Read(), and nSocket::Write().

01053 {
01054     if( addr_.addr_in.sin_addr.s_addr != INADDR_ANY )
01055         string = ANET_AddrToString( *this );
01056     else
01057     {
01058         string = "*.*.*.*";
01059         string << ":" << GetPort();
01060     }

Here is the call graph for this function:

Here is the caller graph for this function:

tString nAddress::ToString ( void   )  const

turns address to complete string

Returns:
the string represendation of the address

Definition at line 1072 of file nSocket.cpp.

01075 {
01076     // delegate
01077     tString ret;
01078     ToString( ret );

int nAddress::FromString ( const char *  string  ) 

turns complete string into address

Parameters:
string the string represendation of the address
Returns:
0 on success

Definition at line 1091 of file nSocket.cpp.

References nAddressBase::addr, addr_, nAddressBase::addr_in, SetHostname(), SetPort(), tString::StrPos(), tString::SubStr(), tERR_ERROR, and tString::ToInt().

Referenced by operator>>().

01094 {
01095     int ha1, ha2, ha3, ha4, hp;
01096     int ipaddr;
01097 
01098     tString source( string );
01099 
01100     // parse IP address if the passed name looks like one
01101     if (string[0] >= '0' && string[0] <= '9')
01102     {
01103         // parse IP address
01104         sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
01105         ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
01106 
01107         // store values in address
01108         addr_.addr   .sa_family = AF_INET;
01109         addr_.addr_in.sin_addr.s_addr = htonl(ipaddr);
01110         addr_.addr_in.sin_port = htons(hp);
01111         return 0;
01112     }
01113     else
01114     {
01115         addr_.addr   .sa_family = AF_INET;
01116 
01117         // find colon
01118         int colonPos = source.StrPos(":");
01119         if ( colonPos > 0 )
01120         {
01121             tString addr = source.SubStr( 0, colonPos );
01122 
01123             // extract port
01124             SetPort( source.ToInt( colonPos + 1 ) );
01125 
01126             // extract hostname
01127             if ( addr == "*.*.*.*" )
01128                 addr_.addr_in.sin_addr.s_addr = INADDR_ANY;
01129             else
01130                 SetHostname( addr );
01131 
01132             return 0;
01133         }
01134         else
01135         {
01136             tERR_ERROR( "Invalid string representation ( IP:PORT ): " << string );
01137 
01138             return -1;
01139         }

Here is the call graph for this function:

Here is the caller graph for this function:

void nAddress::FromHostent ( int  length,
const char *  addr 
)

copy address from hostent data

Parameters:
info the addrinfo structure to copy the address from
l length of address
addr pointer to address

Definition at line 1178 of file nSocket.cpp.

References nAddressBase::addr, addr_, nAddressBase::addr_in, addrLen_, and tASSERT.

Referenced by ANET_GetHostList().

01181 {
01182     // check address size
01183     tASSERT( length == 4 );
01184 
01185     // copy the address over
01186     addr_.addr   .sa_family = AF_INET;
01187     addr_.addr_in.sin_addr.s_addr = *reinterpret_cast< int const * >( addr );
01188 
01189     // store length

Here is the caller graph for this function:

nAddress & nAddress::SetHostname ( const char *  hostname  ) 

Sets the hostname part of the address (with DNS lookup).

Parameters:
hostname the hostname to set
Returns:
reference to this for chaining

Definition at line 1260 of file nSocket.cpp.

References nAddressBase::addr, addr_, nAddressBase::addr_in, GetPort(), nAddress(), PartialIPAddress(), tRecorder::PlaybackStrict(), tRecorder::Record(), section, and sn_InitOSNetworking().

Referenced by FromString().

01263 {
01264     // initialize networking at OS level
01265     sn_InitOSNetworking();
01266 
01267     // parse IP address if the passed name looks like one
01268     if (hostname[0] >= '0' && hostname[0] <= '9')
01269     {
01270         PartialIPAddress (hostname, &addr_.addr, GetPort(), 0x7f000001 );
01271         return *this;
01272     }
01273 
01274     // read address from recording
01275     static char const * section = "SINGLEHOSTNAME";
01276     if ( !tRecorder::PlaybackStrict( section, *this ) )
01277     {
01278         // look up hostname ( TODO: error handling )
01279         struct hostent *hostentry;
01280         hostentry = gethostbyname (hostname);
01281         if (hostentry)
01282         {
01283             // store values
01284             addr_.addr   .sa_family = AF_INET;
01285             addr_.addr_in.sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
01286         }
01287         else
01288         {
01289             // invalidate
01290             *this = nAddress();
01291         }
01292     }
01293 
01294     // write address to recording
01295     tRecorder::Record( section, *this );
01296 

Here is the call graph for this function:

Here is the caller graph for this function:

tString nAddress::GetHostname ( void   )  const

Gets the hostname part of the address (with DNS lookup).

Returns:
the hostname part of the address

Definition at line 1242 of file nSocket.cpp.

01245 {
01246     tString ret;
01247     GetHostname( ret );

const nAddress & nAddress::GetHostname ( tString hostname  )  const

Gets the hostname part of the address (with DNS lookup).

Parameters:
hostname the hostname to fill
Returns:
reference to this for chaining

Definition at line 1202 of file nSocket.cpp.

References addr_, nAddressBase::addr_in, tString::Clear(), tRecorder::PlaybackStrict(), tRecorder::Record(), section, size, and sn_InitOSNetworking().

01205 {
01206     // initialize networking at OS level
01207     sn_InitOSNetworking();
01208 
01209     int haddr;
01210     haddr = ntohl(addr_.addr_in.sin_addr.s_addr);
01211 
01212     hostname.Clear();
01213     hostname << ((haddr >> 24) & 0xff) << "." << ((haddr >> 16) & 0xff) << "." << ((haddr >> 8) & 0xff) << "." << (haddr & 0xff);
01214 
01215     // read hostname from recording
01216     static char const * section = "HOSTBYADDR";
01217     if ( !tRecorder::PlaybackStrict( section, hostname ) )
01218     {
01219         struct hostent *hostentry;
01220 
01221         hostentry = gethostbyaddr ( reinterpret_cast< const char * >( &addr_ ), size, AF_INET);
01222         if (hostentry)
01223         {
01224             hostname = tString( (char *)hostentry->h_name );
01225         }
01226     }
01227 
01228     // write hostname to recording
01229     tRecorder::Record( section, hostname );
01230 

Here is the call graph for this function:

nAddress & nAddress::SetAddress ( const char *  hostname  ) 

Sets the hostname part of the address.

Parameters:
address the raw IP to set
Returns:
reference to this for chaining

Definition at line 1350 of file nSocket.cpp.

References nAddressBase::addr, addr_, GetPort(), PartialIPAddress(), sn_InitOSNetworking(), and tVERIFY.

01353 {
01354     // initialize networking at OS level
01355     sn_InitOSNetworking();
01356 
01357     // parse IP address if the passed name looks like one
01358     tVERIFY(hostname[0] >= '0' && hostname[0] <= '9')
01359 
01360     PartialIPAddress (hostname, &addr_.addr, GetPort(), 0x7f000001 );

Here is the call graph for this function:

tString nAddress::GetAddress ( void   )  const

Gets the hostname part of the address.

Returns:
the raw IP part of the address

Definition at line 1332 of file nSocket.cpp.

01335 {
01336     tString ret;
01337     GetAddress( ret );

const nAddress & nAddress::GetAddress ( tString hostname  )  const

Gets the hostname part of the address.

Parameters:
address the raw IP to fill
Returns:
reference to this for chaining

Definition at line 1309 of file nSocket.cpp.

References addr_, nAddressBase::addr_in, and sn_InitOSNetworking().

01312 {
01313     // initialize networking at OS level
01314     sn_InitOSNetworking();
01315 
01316     int haddr;
01317     haddr = ntohl(addr_.addr_in.sin_addr.s_addr);
01318 
01319     hostname << ((haddr >> 24) & 0xff) << "." << ((haddr >> 16) & 0xff) << "." << ((haddr >> 8) & 0xff) << "." << (haddr & 0xff);
01320 

Here is the call graph for this function:

nAddress & nAddress::SetPort ( int  port  ) 

Sets the port of the address.

Parameters:
port the port to set
Returns:
reference to this for chaining

Definition at line 1373 of file nSocket.cpp.

References addr_, and nAddressBase::addr_in.

Referenced by ANET_GetHostList(), FromString(), nSocket::Open(), and nSocket::Reset().

01376 {
01377     // store the port
01378     addr_.addr_in.sin_port = htons(port);
01379 

Here is the caller graph for this function:

int nAddress::GetPort ( void   )  const

Gets the port of the address.

Returns:
port of this address

Definition at line 1391 of file nSocket.cpp.

References addr_, and nAddressBase::addr_in.

Referenced by nServerInfoBase::DoGetFrom(), GetPort(), nSocket::Reset(), SetAddress(), SetHostname(), and ToString().

01394 {

Here is the caller graph for this function:

const nAddress & nAddress::GetPort ( int &  port  )  const

Gets the port of the address.

Parameters:
port port of this address to fill
Returns:
reference to this for chaining

Definition at line 1407 of file nSocket.cpp.

References GetPort().

01410 {
01411     port = GetPort();
01412 

Here is the call graph for this function:

bool nAddress::IsSet (  )  const

returns true only if address is not INETADDR_ANY

Returns:
true only if address was set to something valid

Definition at line 1425 of file nSocket.cpp.

References addr_, and nAddressBase::addr_in.

Referenced by nServerInfo::TellMasterAboutMe().

01428 {

Here is the caller graph for this function:

int nAddress::Compare ( const nAddress a1,
const nAddress a2 
) [static]

compares two addresses

Parameters:
a1 first address to compare
a2 second address to compare
Returns:

Definition at line 1442 of file nSocket.cpp.

References nAddressBase::addr, addr_, and nAddressBase::addr_in.

Referenced by nSocket::Bind(), CountSameConnection(), CountSameIP(), operator!=(), operator==(), and rec_peer().

01445 {
01446     if (a1.addr_.addr.sa_family != a2.addr_.addr.sa_family)
01447         return -1;
01448 
01449     if (a1.addr_.addr_in.sin_addr.s_addr != a2.addr_.addr_in.sin_addr.s_addr)
01450         return -1;
01451 
01452     if (a1.addr_.addr_in.sin_port != a2.addr_.addr_in.sin_port)
01453         return 1;
01454 

Here is the caller graph for this function:

nAddress::operator struct sockaddr * (  )  [inline]

conversion to sockaddr

Definition at line 97 of file nSocket.h.

References nAddressBase::addr, and addr_.

nAddress::operator struct sockaddr const * (  )  const [inline]

conversion to sockaddr

Definition at line 98 of file nSocket.h.

References nAddressBase::addr, and addr_.

bool nAddress::operator== ( nAddress const &  other  )  const [inline]

comparison operator

Definition at line 101 of file nSocket.h.

References Compare().

00102     {
00103         return Compare( *this, other ) == 0;
00104     }

Here is the call graph for this function:

bool nAddress::operator!= ( nAddress const &  other  )  const [inline]

comparison operator

Definition at line 107 of file nSocket.h.

References Compare().

00108     {
00109         return Compare( *this, other ) != 0;
00110     }

Here is the call graph for this function:

unsigned int nAddress::GetAddressLength ( void   )  const

Gets the length of the stored address.

Returns:
the length of the stored address

Definition at line 1466 of file nSocket.cpp.

References addrLen_.

Referenced by nSocket::Bind(), nSocket::Read(), and nSocket::Write().

01469 {

Here is the caller graph for this function:


Member Data Documentation

nAddressBase nAddress::addr_ [private]

the lowlevel network address

Definition at line 116 of file nSocket.h.

Referenced by Compare(), FromHostent(), FromString(), GetAddress(), GetHostname(), GetPort(), IsSet(), nAddress(), operator struct sockaddr *(), operator struct sockaddr const *(), SetAddress(), SetHostname(), SetPort(), and ToString().

unsigned int nAddress::addrLen_ [private]

the length of the really stored address

Definition at line 117 of file nSocket.h.

Referenced by FromHostent(), GetAddressLength(), and nAddress().


The documentation for this class was generated from the following files:
Generated on Sat Mar 15 23:46:37 2008 for Armagetron Advanced by  doxygen 1.5.4