Protected Attributes

IPvXAddress Class Reference

#include <IPvXAddress.h>

List of all members.

Public Member Functions

 IPvXAddress ()
 IPvXAddress (const IPAddress &addr)
 IPvXAddress (const IPv6Address &addr)
 IPvXAddress (const char *addr)
 IPvXAddress (const IPvXAddress &addr)
 ~IPvXAddress ()

bool isIPv6 () const
IPAddress get4 () const
IPv6Address get6 () const
void set (const IPAddress &addr)
void set (const IPv6Address &addr)
void set (const IPvXAddress &addr)
void set (const char *addr)
IPvXAddressoperator= (const IPAddress &addr)
IPvXAddressoperator= (const IPv6Address &addr)
IPvXAddressoperator= (const IPvXAddress &addr)
bool tryParse (const char *addr)
std::string str () const

bool isUnspecified () const
int wordCount () const
const uint32 * words () const
bool equals (const IPAddress &addr) const
bool equals (const IPv6Address &addr) const
bool equals (const IPvXAddress &addr) const
bool operator== (const IPAddress &addr) const
bool operator!= (const IPAddress &addr) const
bool operator== (const IPv6Address &addr) const
bool operator!= (const IPv6Address &addr) const
bool operator== (const IPvXAddress &addr) const
bool operator!= (const IPvXAddress &addr) const
bool operator< (const IPvXAddress &addr) const

Protected Attributes

uint32 d [4]
bool isv6

Detailed Description

Stores an IPv4 or an IPv6 address. This class should be used everywhere in transport layer and up, to guarantee IPv4/IPv6 transparence.

Storage is efficient: an object occupies size of an IPv6 address (128bits=16 bytes) plus a bool.

Definition at line 35 of file IPvXAddress.h.


Constructor & Destructor Documentation

IPvXAddress::IPvXAddress (  )  [inline]

name Constructors, destructor Constructor for IPv4 addresses.

Definition at line 47 of file IPvXAddress.h.

{isv6 = false; d[0] = 0;}

IPvXAddress::IPvXAddress ( const IPAddress addr  )  [inline]

Constructor for IPv4 addresses.

Definition at line 52 of file IPvXAddress.h.

{set(addr);}

IPvXAddress::IPvXAddress ( const IPv6Address addr  )  [inline]

Constructor for IPv6 addresses.

Definition at line 57 of file IPvXAddress.h.

{set(addr);}

IPvXAddress::IPvXAddress ( const char *  addr  )  [inline]

Accepts string representations suuported by IPAddress (dotted decimal notation) and IPv6Address (hex string with colons). Throws an error if the format is not recognized.

Definition at line 64 of file IPvXAddress.h.

{set(addr);}

IPvXAddress::IPvXAddress ( const IPvXAddress addr  )  [inline]

Copy constructor.

Definition at line 69 of file IPvXAddress.h.

{set(addr);}

IPvXAddress::~IPvXAddress (  )  [inline]

Destructor

Definition at line 74 of file IPvXAddress.h.

{}


Member Function Documentation

bool IPvXAddress::equals ( const IPAddress addr  )  const [inline]

Returns true if the two addresses are equal

Definition at line 199 of file IPvXAddress.h.

                                             {
        return !isv6 && d[0]==addr.getInt();
    }

bool IPvXAddress::equals ( const IPv6Address addr  )  const [inline]

Returns true if the two addresses are equal

Definition at line 206 of file IPvXAddress.h.

                                               {
        uint32 *w = const_cast<IPv6Address&>(addr).words();
        return isv6 && d[0]==w[0] && d[1]==w[1] && d[2]==w[2] && d[3]==w[3];
    }

bool IPvXAddress::equals ( const IPvXAddress addr  )  const [inline]

Returns true if the two addresses are equal

Definition at line 214 of file IPvXAddress.h.

                                               {
        return (isv6 == addr.isv6) && (d[0]==addr.d[0]) && (!isv6 || (d[1]==addr.d[1] && d[2]==addr.d[2] && d[3]==addr.d[3]));
    }

IPAddress IPvXAddress::get4 (  )  const [inline]
IPv6Address IPvXAddress::get6 (  )  const [inline]

Get IPv6 address. Throws exception if this is an IPv4 address.

Definition at line 96 of file IPvXAddress.h.

Referenced by doPacking(), getLevel(), UDP::matchesSocket(), IPTrafGen::sendPacket(), PingApp::sendToICMP(), TCPSpoof::sendToIP(), tcp_old::TCPConnection::sendToIP(), TCPConnection::sendToIP(), and SCTPAssociation::sendToIP().

                             {
        if (!isv6)  {
            if (d[0]==0) // allow null address to be returned as IPv6
                return IPv6Address();
            throw cRuntimeError("IPvXAddress: cannot return IPv4 address %s as IPv6", str().c_str());
        }
        return IPv6Address(d[0], d[1], d[2], d[3]);
    }

bool IPvXAddress::isIPv6 (  )  const [inline]
bool IPvXAddress::isUnspecified (  )  const [inline]
bool IPvXAddress::operator!= ( const IPAddress addr  )  const [inline]

Returns !equals(addr).

Definition at line 226 of file IPvXAddress.h.

{return !equals(addr);}

bool IPvXAddress::operator!= ( const IPv6Address addr  )  const [inline]

Returns !equals(addr).

Definition at line 236 of file IPvXAddress.h.

{return !equals(addr);}

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

Returns !equals(addr).

Definition at line 246 of file IPvXAddress.h.

{return !equals(addr);}

bool IPvXAddress::operator< ( const IPvXAddress addr  )  const [inline]

Compares two addresses.

Definition at line 251 of file IPvXAddress.h.

                                                  {
        if (isv6!=addr.isv6)
            return !isv6;
        else if (!isv6)
            return d[0]<addr.d[0];
        else
            return memcmp(&d, &addr.d, 16) < 0;  // this provides an ordering, though not surely the one one would expect
    }

IPvXAddress& IPvXAddress::operator= ( const IPAddress addr  )  [inline]

Assignment

Definition at line 151 of file IPvXAddress.h.

{set(addr); return *this;}

IPvXAddress& IPvXAddress::operator= ( const IPv6Address addr  )  [inline]

Assignment

Definition at line 156 of file IPvXAddress.h.

{set(addr); return *this;}

IPvXAddress& IPvXAddress::operator= ( const IPvXAddress addr  )  [inline]

Assignment

Definition at line 161 of file IPvXAddress.h.

{set(addr); return *this;}

bool IPvXAddress::operator== ( const IPvXAddress addr  )  const [inline]

Returns equals(addr).

Definition at line 241 of file IPvXAddress.h.

{return equals(addr);}

bool IPvXAddress::operator== ( const IPAddress addr  )  const [inline]

Returns equals(addr).

Definition at line 221 of file IPvXAddress.h.

{return equals(addr);}

bool IPvXAddress::operator== ( const IPv6Address addr  )  const [inline]

Returns equals(addr).

Definition at line 231 of file IPvXAddress.h.

{return equals(addr);}

void IPvXAddress::set ( const IPAddress addr  )  [inline]

Set to an IPv4 address.

Definition at line 108 of file IPvXAddress.h.

Referenced by doUnpacking(), and SCTP::findAssocForMessage().

                                     {
        isv6 = false;
        d[0] = addr.getInt();
    }

void IPvXAddress::set ( const IPv6Address addr  )  [inline]

Set to an IPv6 address.

Definition at line 116 of file IPvXAddress.h.

                                       {
        if (addr.isUnspecified()) {
            // we always represent nulls as IPv4 null
            isv6 = false; d[0] = 0;
            return;
        }
        isv6 = true;
        uint32 *w = const_cast<IPv6Address&>(addr).words();
        d[0] = w[0]; d[1] = w[1]; d[2] = w[2]; d[3] = w[3];
    }

void IPvXAddress::set ( const char *  addr  )  [inline]

Accepts string representations supported by IPAddress (dotted decimal notation) and IPv6Address (hex string with colons). Throws an error if the format is not recognized.

Definition at line 143 of file IPvXAddress.h.

                               {
        if (!tryParse(addr))
            throw cRuntimeError("IPvXAddress: cannot interpret address string `%s'", addr);
    }

void IPvXAddress::set ( const IPvXAddress addr  )  [inline]

Assignment

Definition at line 130 of file IPvXAddress.h.

                                      {
        isv6 = addr.isv6;
        d[0] = addr.d[0];
        if (isv6) {
            d[1] = addr.d[1]; d[2] = addr.d[2]; d[3] = addr.d[3];
         }
    }

std::string IPvXAddress::str (  )  const [inline]

Returns the string representation of the address (e.g. "152.66.86.92")

Definition at line 172 of file IPvXAddress.h.

Referenced by tcp_old::TCP::addSockPair(), TCP::addSockPair(), operator<<(), SCTP::removeAssociation(), and SCTPPathVariables::SCTPPathVariables().

{return isv6 ? get6().str() : get4().str();}

bool IPvXAddress::tryParse ( const char *  addr  ) 

Parses and assigns the given address and returns true if the string is recognized by IPAddress or IPv6Address, otherwise just returns false.

Definition at line 21 of file IPvXAddress.cc.

Referenced by IPAddressResolver::tryResolve().

{
    // try as IPv4
    if (IPAddress::isWellFormed(addr))
    {
        set(IPAddress(addr));
        return true;
    }

    // try as IPv6
    IPv6Address ipv6;
    if (ipv6.tryParse(addr))
    {
        set(ipv6);
        return true;
    }

    // no luck
    return false;
}

int IPvXAddress::wordCount (  )  const [inline]

Returns length of internal binary representation of address, (count of 32-bit unsigned integers.)

Definition at line 188 of file IPvXAddress.h.

Referenced by TCPSerializer::checksum().

{return isv6 ? 4 : 1;}

const uint32* IPvXAddress::words (  )  const [inline]

Returns pointer to internal binary representation of address, four 32-bit unsigned integers.

Definition at line 194 of file IPvXAddress.h.

Referenced by TCPSerializer::checksum().

{return d;}


Member Data Documentation

uint32 IPvXAddress::d[4] [protected]

Definition at line 38 of file IPvXAddress.h.

Referenced by equals(), and operator<().

bool IPvXAddress::isv6 [protected]

Definition at line 39 of file IPvXAddress.h.

Referenced by equals(), and operator<().


The documentation for this class was generated from the following files: