vpr::InetAddrNSPR Class Reference

Cross-platform abstraction to Internet address structures. More...

#include <vpr/IO/Socket/InetAddr.h>

Inheritance diagram for vpr::InetAddrNSPR:

Inheritance graph
[legend]
Collaboration diagram for vpr::InetAddrNSPR:

Collaboration graph
[legend]
List of all members.

Public Types

typedef vpr::InetAddrHash hash

Public Member Functions

 InetAddrNSPR ()
 Default constructor.
 InetAddrNSPR (const InetAddrNSPR &addr)
 Copy constructor.
vpr::ReturnStatus setAddress (const std::string &address)
 Sets the address for this object using the given address.
vpr::ReturnStatus setAddress (const std::string &address, const vpr::Uint16 port)
 Sets the address for this object using the given address and port number.
vpr::ReturnStatus setAddress (const vpr::Uint32 address, const vpr::Uint16 port)
 Constructs an address object using the given address and port number.
vpr::SocketTypes::Domain getFamily () const
 Gets the protocol family of this address structure.
void setFamily (const vpr::SocketTypes::Domain family)
 Sets the protocol family of this address structure.
vpr::Uint16 getPort () const
 Get this address' port in host byte order.
void setPort (const vpr::Uint16 port)
 Sets this address' port.
vpr::Uint32 getAddressValue () const
 Gets this address structure's Internet address in host byte order.
std::string getAddressString () const
 Gets the IP address associated with this structure as a human-readable string.
vpr::ReturnStatus getHostname (std::string &hostname) const
 Returns the fully qualified hostname for this address.
std::vector< std::string > getHostnames () const
 Returns the fully qualified primary hostname for this address and all known aliases.
InetAddrNSPRoperator= (const InetAddrNSPR &addr)
 Overloaded assignment operator to ensure that assignments work correctly.
bool operator== (const InetAddrNSPR &addr) const
 Overloaded equality operator.
bool operator!= (const InetAddrNSPR &addr) const
 Overloaded inequality operator.
PRNetAddr * getPRNetAddr ()
 Gets the info needed by other NSPR commands.
const PRNetAddr * getPRNetAddr () const
 Gets the info needed by other NSPR commands.

Static Public Member Functions

static vpr::ReturnStatus getLocalHost (vpr::InetAddrNSPR &hostAddr)
 Returns the local host's default address via the given object reference.
static vpr::ReturnStatus getAllLocalAddrs (std::vector< vpr::InetAddrNSPR > &hostAddrs, const bool withLoopback=false)
 Retrieves all the IPv4 addresses associated with the local machine, including the loopback address (127.0.0.1) if so indicated.

Static Public Attributes

static const InetAddrNSPR AnyAddr

Protected Member Functions

void setAddressValue (const vpr::Uint32 addrValue)
 Sets this structure's IP address.
vpr::ReturnStatus lookupAddress (const std::string &address)
 Looks up the address in mName and store the address in mAddr.

Protected Attributes

PRNetAddr mAddr
 Actual address.

Detailed Description

Cross-platform abstraction to Internet address structures.

This is typedef'd to vpr::InetAddr.

Definition at line 67 of file InetAddrNSPR.h.


Member Typedef Documentation

typedef vpr::InetAddrHash vpr::InetAddrNSPR::hash

Definition at line 72 of file InetAddrNSPR.h.


Constructor & Destructor Documentation

vpr::InetAddrNSPR::InetAddrNSPR (  )  [inline]

Default constructor.

Postcondition:
Zero out the address and set it to PR_INADDR_ANY.
Note:
This is not a valid destination address for client communication.

Definition at line 80 of file InetAddrNSPR.h.

References vpr::SocketTypes::INET.

00081    {
00082       memset(&mAddr, 0, sizeof(mAddr));
00083       setFamily(SocketTypes::INET);
00084       setAddressValue(PR_INADDR_ANY);
00085       setPort(0);
00086    }

vpr::InetAddrNSPR::InetAddrNSPR ( const InetAddrNSPR addr  )  [inline]

Copy constructor.

Postcondition:
A copy of the given vpr::InetAddr object is made in this object.
Parameters:
addr The vpr::InetAddr object to be copied into this object.

Definition at line 95 of file InetAddrNSPR.h.

References mAddr.

00096    {
00097       mAddr = addr.mAddr;
00098    }


Member Function Documentation

vpr::ReturnStatus vpr::InetAddrNSPR::getLocalHost ( vpr::InetAddrNSPR hostAddr  )  [static]

Returns the local host's default address via the given object reference.

Parameters:
hostAddr Storage for the returned address object.
Returns:
vpr::ReturnStatus::Succeed is returned if the local host has an an address. Otherwise, vpr::ReturnStatus::Fail is returned.

Definition at line 59 of file InetAddrNSPR.cpp.

References vpr::ReturnStatus::Fail, setAddress(), vpr::ReturnStatus::setCode(), and vpr::ReturnStatus::Succeed.

00060 {
00061    vpr::ReturnStatus status(vpr::ReturnStatus::Fail);
00062    char local_host_name[257];
00063    memset(local_host_name, 0, 257);
00064 
00065    if ( PR_GetSystemInfo(PR_SI_HOSTNAME, local_host_name, 256) == PR_SUCCESS )
00066    {
00067       hostAddr.setAddress(std::string(local_host_name), 0);
00068       status.setCode(vpr::ReturnStatus::Succeed);
00069    }
00070 
00071    return status;
00072 }

vpr::ReturnStatus vpr::InetAddrNSPR::getAllLocalAddrs ( std::vector< vpr::InetAddrNSPR > &  hostAddrs,
const bool  withLoopback = false 
) [static]

Retrieves all the IPv4 addresses associated with the local machine, including the loopback address (127.0.0.1) if so indicated.

Postcondition:
hostAddrs contains vpr::InetAddr objetcs holding all the local IPv4 addresses for the local machine.
Parameters:
hostAddrs Storage for the discovered local IPv4 addresses. The vector is cleared before the addresses are added, so any objects currently in the vector are lost.
withLoopback A flag indicating whether to include the loopback address (127.0.0.1) in hostAddrs. This parameter is optional and defaults to false.
Note:
This method currently supports only IPv4.
Returns:
vpr::ReturnStatus::Succeed is returned if the querying of all local addresses completes without error. Otherwise, vpr::ReturnStatus::Fail is returned.
Since:
1.0.2

Definition at line 75 of file InetAddrNSPR.cpp.

References vpr::getIfAddrs().

00077 {
00078    return vpr::getIfAddrs(hostAddrs, withLoopback);
00079 }

vpr::ReturnStatus vpr::InetAddrNSPR::setAddress ( const std::string &  address  ) 

Sets the address for this object using the given address.

It must be of the form address:port where address can be a hostname or a dotted-decimal IP address.

Parameters:
address A string giving the address and port number separated by a colon. The address can be a hostname or a dotted-decimal IP address.

Reimplemented from vpr::InetAddrBase.

Definition at line 84 of file InetAddrNSPR.cpp.

References vpr::SocketTypes::INET, lookupAddress(), setFamily(), and setPort().

Referenced by getLocalHost().

00085 {
00086    std::string::size_type pos;
00087    std::string host_addr, host_port;
00088    vpr::Uint16 port;
00089    vpr::ReturnStatus retval;
00090 
00091    // Extract the address and the port number from the given string.
00092    pos       = address.find(":");
00093    host_addr = address.substr(0, pos);
00094    host_port = address.substr(pos + 1);
00095    port      = (vpr::Uint16) atoi(host_port.c_str());
00096 
00097    retval = lookupAddress(host_addr);
00098    setPort(port);
00099    setFamily(vpr::SocketTypes::INET);
00100    return retval;
00101 }

vpr::ReturnStatus vpr::InetAddrNSPR::setAddress ( const std::string &  address,
const vpr::Uint16  port 
) [inline]

Sets the address for this object using the given address and port number.

The address string can be a hostname or a dotted-decimal IP address.

Parameters:
address A string giving the address (either hostname or IP address).
port The port to associate with the IP address.

Reimplemented from vpr::InetAddrBase.

Definition at line 157 of file InetAddrNSPR.h.

References vpr::SocketTypes::INET.

00159    {
00160       vpr::ReturnStatus retval;
00161       retval = lookupAddress(address);
00162       setFamily(SocketTypes::INET);
00163       setPort(port);
00164       return retval;
00165    }

vpr::ReturnStatus vpr::InetAddrNSPR::setAddress ( const vpr::Uint32  address,
const vpr::Uint16  port 
) [inline]

Constructs an address object using the given address and port number.

The address must be the actual 32-bit integer value.

Parameters:
address A 32-bit integer IP address.
port The port to associate with the IP address.

Reimplemented from vpr::InetAddrBase.

Definition at line 174 of file InetAddrNSPR.h.

References vpr::SocketTypes::INET.

00176    {
00177       setAddressValue(address);
00178       setPort(port);
00179       setFamily(SocketTypes::INET);
00180       return vpr::ReturnStatus();
00181    }

vpr::SocketTypes::Domain vpr::InetAddrNSPR::getFamily (  )  const

Gets the protocol family of this address structure.

Precondition:
The protocol family has been properly initialized.
Postcondition:
The protocol family is returned as a vpr::SocketTypes::Domain value.
Returns:
A vpr::SocketTypes::Domain value representing this object's protocol family.

Reimplemented from vpr::InetAddrBase.

Definition at line 104 of file InetAddrNSPR.cpp.

References vpr::SocketTypes::INET, vpr::SocketTypes::INET6, vpr::SocketTypes::LOCAL, and mAddr.

00105 {
00106    vpr::SocketTypes::Domain family;
00107 
00108    switch ( PR_NetAddrFamily(&mAddr) )
00109    {
00110       case PR_AF_INET:
00111          family = vpr::SocketTypes::INET;
00112          break;
00113       case PR_AF_LOCAL:
00114          family = vpr::SocketTypes::LOCAL;
00115          break;
00116       case PR_AF_INET6:
00117          family = vpr::SocketTypes::INET6;
00118          break;
00119    }
00120 
00121    return family;
00122 }

void vpr::InetAddrNSPR::setFamily ( const vpr::SocketTypes::Domain  family  ) 

Sets the protocol family of this address structure.

Precondition:
None.
Postcondition:
The given protocol family (a vpr::SocketTypes::Domain value) is mapped to the appropriate platform-specific protocol family value and stored.
Parameters:
family The protocol family value.

Reimplemented from vpr::InetAddrBase.

Definition at line 125 of file InetAddrNSPR.cpp.

References vpr::SocketTypes::INET, vpr::SocketTypes::INET6, vpr::SocketTypes::LOCAL, and mAddr.

Referenced by setAddress().

00126 {
00127    switch ( family )
00128    {
00129       case vpr::SocketTypes::LOCAL:
00130          PR_NetAddrFamily(&mAddr) = PR_AF_LOCAL;
00131          break;
00132       case vpr::SocketTypes::INET:
00133          PR_NetAddrFamily(&mAddr) = PR_AF_INET;
00134          break;
00135       case vpr::SocketTypes::INET6:
00136          PR_NetAddrFamily(&mAddr) = PR_AF_INET6;
00137          break;
00138       default:
00139          fprintf(stderr,
00140                  "[vpr::InetAddrNSPR] ERROR: Unknown socket family value %d\n",
00141                  family);
00142          break;
00143    }
00144 }

vpr::Uint16 vpr::InetAddrNSPR::getPort (  )  const [inline]

Get this address' port in host byte order.

Precondition:
The port has been initialized properly in network byte order.
Postcondition:
The port associated with this address structure is returned to the caller in host byte order.
Returns:
A Uint16 giving the port for this address structure in host byte order.

Reimplemented from vpr::InetAddrBase.

Definition at line 217 of file InetAddrNSPR.h.

00218    {
00219       return PR_ntohs(PR_NetAddrInetPort(&mAddr));
00220    }

void vpr::InetAddrNSPR::setPort ( const vpr::Uint16  port  )  [inline]

Sets this address' port.

The given port must be in host byte order.

Precondition:
The given port number is in host byte order.
Postcondition:
The given port number is stored in the address structure in network byte order.
Parameters:
port A Uint16 port number for this address structure in host byte order.

Reimplemented from vpr::InetAddrBase.

Definition at line 232 of file InetAddrNSPR.h.

Referenced by setAddress().

00233    {
00234       PR_NetAddrInetPort(&mAddr) = PR_htons(port);
00235    }

vpr::Uint32 vpr::InetAddrNSPR::getAddressValue (  )  const [inline]

Gets this address structure's Internet address in host byte order.

Precondition:
The IP address has been initialized properly in network byte order.
Postcondition:
The IP address associated with this address structure is returned to the caller in host byte order.
Returns:
An unsigned int (32-bit value) giving the IP address for this address structure in host byte order.

Reimplemented from vpr::InetAddrBase.

Definition at line 248 of file InetAddrNSPR.h.

00249    {
00250       return PR_ntohl(mAddr.inet.ip);
00251    }

std::string vpr::InetAddrNSPR::getAddressString (  )  const

Gets the IP address associated with this structure as a human-readable string.

Precondition:
The structure contains a valid IP address.
Postcondition:
The integer IP address is converted to dotted-decmial notation and returned as a string.
Returns:
A std::string object containing this structure's IP address in the human-readable "dotted-decimal" notation.

Reimplemented from vpr::InetAddrBase.

Definition at line 148 of file InetAddrNSPR.cpp.

References mAddr.

00149 {
00150    char ip_str[256];
00151    memset(ip_str, 0, 256);
00152    PR_NetAddrToString(&mAddr, ip_str, sizeof(PRNetAddr));
00153    std::string temp(ip_str);
00154    return temp;
00155 }

vpr::ReturnStatus vpr::InetAddrNSPR::getHostname ( std::string &  hostname  )  const

Returns the fully qualified hostname for this address.

Reimplemented from vpr::InetAddrBase.

Definition at line 157 of file InetAddrNSPR.cpp.

References vpr::ReturnStatus::Fail, mAddr, vpr::ErrorImplPosix::outputCurrentError(), and vpr::ReturnStatus::setCode().

00158 {
00159    vpr::ReturnStatus status;
00160    char buffer[PR_NETDB_BUF_SIZE];
00161    memset(buffer, 0, PR_NETDB_BUF_SIZE);
00162    PRStatus ret_status;
00163    PRHostEnt hostent;
00164 
00165    ret_status = PR_GetHostByAddr(&mAddr, buffer, sizeof(buffer), &hostent);
00166 
00167    if ( ret_status == PR_FAILURE )
00168    {
00169       vpr::Error::outputCurrentError(std::cerr, "[InetAddrNSPR::getHostname] Failed to get host by address");
00170       status.setCode(vpr::ReturnStatus::Fail);
00171    }
00172    else
00173    {
00174       hostname = hostent.h_name;
00175    }
00176 
00177    return status;
00178 }

std::vector< std::string > vpr::InetAddrNSPR::getHostnames (  )  const

Returns the fully qualified primary hostname for this address and all known aliases.

Reimplemented from vpr::InetAddrBase.

Definition at line 180 of file InetAddrNSPR.cpp.

References mAddr.

00181 {
00182    std::vector<std::string> names;
00183    char buffer[PR_NETDB_BUF_SIZE];
00184    PRStatus ret_status;
00185    PRHostEnt hostent;
00186 
00187    ret_status = PR_GetHostByAddr(&mAddr, buffer, sizeof(buffer), &hostent);
00188 
00189    if ( ret_status != PR_FAILURE )
00190    {
00191       names.push_back(std::string(hostent.h_name));
00192 
00193       for ( char** ptr = hostent.h_aliases; *ptr != NULL; ptr++ )
00194       {
00195          names.push_back(std::string(*ptr));
00196       }
00197    }
00198 
00199    return names;
00200 }

InetAddrNSPR& vpr::InetAddrNSPR::operator= ( const InetAddrNSPR addr  )  [inline]

Overloaded assignment operator to ensure that assignments work correctly.

Precondition:
None.
Postcondition:
A copy of the given vpr::InetAddr object is made in this object which is then returned to the caller.
Parameters:
addr The vpr::InetAddr object to be copied into this object.
Returns:
A reference to this object.

Definition at line 289 of file InetAddrNSPR.h.

References mAddr.

00290    {
00291       mAddr = addr.mAddr;
00292       return *this;
00293    }

bool vpr::InetAddrNSPR::operator== ( const InetAddrNSPR addr  )  const [inline]

Overloaded equality operator.

Definition at line 298 of file InetAddrNSPR.h.

References mAddr.

00299    {
00300       return((mAddr.inet.family == addr.mAddr.inet.family) &&
00301              (mAddr.inet.port == addr.mAddr.inet.port) &&
00302              (mAddr.inet.ip == addr.mAddr.inet.ip));
00303    }

bool vpr::InetAddrNSPR::operator!= ( const InetAddrNSPR addr  )  const [inline]

Overloaded inequality operator.

Definition at line 308 of file InetAddrNSPR.h.

00309    {
00310       return ! (*this == addr);
00311    }

PRNetAddr* vpr::InetAddrNSPR::getPRNetAddr (  )  [inline]

Gets the info needed by other NSPR commands.

Definition at line 316 of file InetAddrNSPR.h.

00317    {
00318       return &mAddr;
00319    }

const PRNetAddr* vpr::InetAddrNSPR::getPRNetAddr (  )  const [inline]

Gets the info needed by other NSPR commands.

Definition at line 322 of file InetAddrNSPR.h.

00323    {
00324       return &mAddr;
00325    }

void vpr::InetAddrNSPR::setAddressValue ( const vpr::Uint32  addrValue  )  [inline, protected]

Sets this structure's IP address.

The given address must be in host byte order.

Precondition:
The given IP address is in host byte order.
Postcondition:
The given IP address is stored in the address structure in network byte order.
Parameters:
addrValue An unsigned int IP address for this address object in host byte order.

Definition at line 339 of file InetAddrNSPR.h.

Referenced by lookupAddress().

00340    {
00341       mAddr.inet.ip = PR_htonl(addrValue);
00342    }

vpr::ReturnStatus vpr::InetAddrNSPR::lookupAddress ( const std::string &  address  )  [protected]

Looks up the address in mName and store the address in mAddr.

Definition at line 203 of file InetAddrNSPR.cpp.

References vpr::ReturnStatus::Fail, vpr::ReturnStatus::failure(), mAddr, vpr::ErrorImplPosix::outputCurrentError(), setAddressValue(), and vpr::ReturnStatus::setCode().

Referenced by setAddress().

00204 {
00205    vpr::ReturnStatus retval;
00206    PRStatus ret_status;
00207    PRHostEnt host_entry;
00208    char buffer[PR_NETDB_BUF_SIZE];
00209 
00210    ret_status = PR_GetHostByName(address.c_str(), buffer, sizeof(buffer),
00211                                  &host_entry);
00212 
00213    if ( ret_status == PR_FAILURE )
00214    {
00215       setAddressValue(0);           // Error on lookup, so zero the address
00216       std::string error_msg("[InetAddrNSPR::lookupAddress] Fail to look up host: ");
00217       error_msg += address;
00218 
00219       vpr::Error::outputCurrentError(std::cerr, error_msg);
00220       retval.setCode(ReturnStatus::Fail);
00221    }
00222    else
00223    {
00224       if ( PR_EnumerateHostEnt(0, &host_entry, 0, &mAddr) == -1 )
00225       {
00226          retval.setCode(ReturnStatus::Fail);
00227       }
00228 
00229       if ( retval.failure() )
00230       {
00231          vpr::Error::outputCurrentError(std::cerr, "[InetAddrNSPR::lookupAddress] Could not enumerate host entry");
00232       }
00233    }
00234 
00235    return retval;
00236 }


Member Data Documentation

const InetAddrNSPR vpr::InetAddrNSPR::AnyAddr [static]

Definition at line 70 of file InetAddrNSPR.h.

PRNetAddr vpr::InetAddrNSPR::mAddr [protected]

Actual address.

Definition at line 349 of file InetAddrNSPR.h.

Referenced by getAddressString(), getFamily(), getHostname(), getHostnames(), InetAddrNSPR(), lookupAddress(), operator=(), operator==(), and setFamily().


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:55:25 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1