Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes

IPAddress Class Reference

#include <IPAddress.h>

List of all members.

Public Member Functions

IPAddressoperator= (const IPAddress &obj)
bool isUnspecified () const
bool equals (const IPAddress &toCmp) const
IPAddress doAnd (const IPAddress &ip) const
std::string str () const
uint32 getInt () const
int getDByte (int i) const
char getIPClass () const
bool isMulticast () const
bool isLinkLocalMulticast () const
IPAddress getNetwork () const
IPAddress getNetworkMask () const
bool isNetwork (const IPAddress &toCmp) const
bool prefixMatches (const IPAddress &to_cmp, int numbits) const
int getNumMatchingPrefixBits (const IPAddress &to_cmp) const
int getNetmaskLength () const
bool operator== (const IPAddress &addr1) const
bool operator!= (const IPAddress &addr1) const
bool operator< (const IPAddress &addr1) const

 IPAddress ()
 IPAddress (uint32 ip)
 IPAddress (int i0, int i1, int i2, int i3)
 IPAddress (const char *text)
 IPAddress (const IPAddress &obj)
 ~IPAddress ()

void set (uint32 ip)
void set (int i0, int i1, int i2, int i3)
void set (const char *t)

Static Public Member Functions

static bool maskedAddrAreEqual (const IPAddress &addr1, const IPAddress &addr2, const IPAddress &netmask)
static bool isWellFormed (const char *text)

Static Public Attributes

Predefined addresses

static const IPAddress UNSPECIFIED_ADDRESS
 0.0.0.0
static const IPAddress LOOPBACK_ADDRESS
 127.0.0.1
static const IPAddress LOOPBACK_NETMASK
 255.0.0.0
static const IPAddress ALLONES_ADDRESS
 255.255.255.255
static const IPAddress ALL_HOSTS_MCAST
 224.0.0.1 All hosts on a subnet
static const IPAddress ALL_ROUTERS_MCAST
 224.0.0.2 All routers on a subnet
static const IPAddress ALL_DVMRP_ROUTERS_MCAST
 224.0.0.4 All DVMRP routers
static const IPAddress ALL_OSPF_ROUTERS_MCAST
 224.0.0.5 All OSPF routers (DR Others)
static const IPAddress ALL_OSPF_DESIGNATED_ROUTERS_MCAST
 224.0.0.6 All OSPF Designated Routers

Protected Member Functions

void keepFirstBits (unsigned int n)

Static Protected Member Functions

static bool parseIPAddress (const char *text, unsigned char tobytes[])

Protected Attributes

uint32 addr

Detailed Description

IPv4 address.

Definition at line 44 of file IPAddress.h.


Constructor & Destructor Documentation

IPAddress::IPAddress (  )  [inline]

name Constructors, destructor Default constructor, initializes to 0.0.0.0.

Definition at line 80 of file IPAddress.h.

Referenced by getNetwork(), and getNetworkMask().

{addr = 0;}

IPAddress::IPAddress ( uint32  ip  )  [inline]

IP address as int

Definition at line 85 of file IPAddress.h.

{addr = ip;}

IPAddress::IPAddress ( int  i0,
int  i1,
int  i2,
int  i3 
) [inline]

IP address bytes: "i0.i1.i2.i3" format

Definition at line 90 of file IPAddress.h.

{set(i0, i1, i2, i3);}

IPAddress::IPAddress ( const char *  text  )  [inline]

IP address given as text: "192.66.86.1"

Definition at line 95 of file IPAddress.h.

{set(text);}

IPAddress::IPAddress ( const IPAddress obj  )  [inline]

Copy constructor

Definition at line 100 of file IPAddress.h.

{operator=(obj);}

IPAddress::~IPAddress (  )  [inline]

Definition at line 102 of file IPAddress.h.

{}


Member Function Documentation

IPAddress IPAddress::doAnd ( const IPAddress ip  )  const [inline]

Returns binary AND of the two addresses

Definition at line 143 of file IPAddress.h.

Referenced by RoutingTable::addRoute(), and NetworkInfo::dumpRoutingInfo().

{return addr & ip.addr;}

bool IPAddress::equals ( const IPAddress toCmp  )  const [inline]

Returns true if the two addresses are equal

Definition at line 138 of file IPAddress.h.

Referenced by IPv4InterfaceData::isMemberOfMulticastGroup(), and RoutingTable::routeMatches().

{return addr == toCmp.addr;}

int IPAddress::getDByte ( int  i  )  const [inline]
uint32 IPAddress::getInt (  )  const [inline]
char IPAddress::getIPClass (  )  const

Returns the network class of the address: char 'A', 'B', 'C', 'D', 'E', or '?' (returned when the address begins with at least five 1 bits.)

Definition at line 114 of file IPAddress.cc.

Referenced by getNetwork(), and getNetworkMask().

{
    unsigned char buf = getDByte(0);
    if ((buf & 0x80) == 0x00)       // 0xxxx
        return 'A';
    else if ((buf & 0xC0) == 0x80)  // 10xxx
        return 'B';
    else if ((buf & 0xE0) == 0xC0)  // 110xx
        return 'C';
    else if ((buf & 0xF0) == 0xE0)  // 1110x
        return 'D';
    else if ((buf & 0xF8) == 0xF0)  // 11110
        return 'E';
    else
        return '?';
}

int IPAddress::getNetmaskLength (  )  const

Counts 1 bits in a netmask. E.g. for 255.255.254.0, it will return 23.

Definition at line 211 of file IPAddress.cc.

Referenced by IPv4InterfaceData::info(), and LDP::rebuildFecList().

{
    int i;
    for (i=0; i<31; i++)
        if (addr & (1 << i))
            return 32-i;
    return 0;
}

IPAddress IPAddress::getNetwork (  )  const

Returns an address with the network part of the address (the bits of the hosts part are to 0). For D and E class addresses, it returns a null address.

Definition at line 131 of file IPAddress.cc.

{
    switch (getIPClass())
    {
    case 'A':
        // Class A: network = 7 bits
        return IPAddress(getDByte(0), 0, 0, 0);
    case 'B':
        // Class B: network = 14 bits
        return IPAddress(getDByte(0), getDByte(1), 0, 0);
    case 'C':
        // Class C: network = 21 bits
        return IPAddress(getDByte(0), getDByte(1), getDByte(2), 0);
    default:
        // Class D or E
        return IPAddress();
    }
}

IPAddress IPAddress::getNetworkMask (  )  const

Returns an address with the network mask corresponding to the address class. For D and E class addresses, it returns a null address.

Definition at line 150 of file IPAddress.cc.

Referenced by OSPF::Router::IsDestinationUnreachable(), and isNetwork().

{
    switch (getIPClass())
    {
    case 'A':
        // Class A: network = 7 bits
        return IPAddress(255, 0, 0, 0);
    case 'B':
        // Class B: network = 14 bits
        return IPAddress(255, 255, 0, 0);
    case 'C':
        // Class C: network = 21 bits
        return IPAddress(255, 255, 255, 0);
    default:
        // Class D or E: return null address
        return IPAddress();
    }
}

int IPAddress::getNumMatchingPrefixBits ( const IPAddress to_cmp  )  const

Indicates how many bits from the to_cmp address, starting counting from the left, matches the address. E.g. if the address is 130.206.72.237, and to_cmp 130.206.72.0, 24 will be returned.

Typical usage for comparing IP prefixes.

Definition at line 195 of file IPAddress.cc.

{
    uint32 addr2 = to_cmp.getInt();

    uint32 res = addr ^ addr2;
    // If the bits are equal, there is a 0, so counting
    // the zeros from the left
    for (int i = 31; i >= 0; i--) {
        if (res & (1 << i)) {
            // 1, means not equal, so stop
            return 31 - i;
        }
    }
    return 32;
}

bool IPAddress::isLinkLocalMulticast (  )  const [inline]

Returns true if this address is in the range 224.0.0.0 to 224.0.0.255. These addresses are reserved for local purposes meaning, that routers should not forward these datagrams since the applications that use these addresses do not need the datagrams to go further than one hop.

Definition at line 179 of file IPAddress.h.

Referenced by IP::routeMulticastPacket().

{return (addr & 0xFFFFFF00) == 0xE0000000;}

bool IPAddress::isMulticast (  )  const [inline]

Returns true if this address is in the multicast address range, 224.0.0.0 thru 239.255.255.255, that is, it's a class D address.

Definition at line 171 of file IPAddress.h.

Referenced by RoutingTable::addRoute(), RTP::createSocket(), RTCP::createSocket(), NetworkInfo::dumpRoutingInfo(), LDP::rebuildFecList(), and TED::rebuildRoutingTable().

{return (addr & 0xF0000000)==0xE0000000;}

bool IPAddress::isNetwork ( const IPAddress toCmp  )  const

Indicates if the address is from the same network

Definition at line 170 of file IPAddress.cc.

{
    IPAddress netmask = getNetworkMask();
    if (netmask.isUnspecified()) return false; // Class is D or E
    return maskedAddrAreEqual(*this, toCmp, netmask);
}

bool IPAddress::isUnspecified (  )  const [inline]
bool IPAddress::isWellFormed ( const char *  text  )  [static]

Returns true if the format of the string corresponds to an IP address with the dotted notation ("192.66.86.1"), and false otherwise.

This function can be used to verify an IP address string before assigning it to an IPAddress object (both its ctor and set() function raises an error if the string has invalid format.)

Definition at line 234 of file IPAddress.cc.

Referenced by RoutingTableParser::parseRouting(), and IPvXAddress::tryParse().

{
    unsigned char dummy[4];
    return parseIPAddress(text, dummy);
}

void IPAddress::keepFirstBits ( unsigned int  n  )  [protected]

Definition at line 220 of file IPAddress.cc.

{
    addr &= 0xFFFFFFFF << n;
}

bool IPAddress::maskedAddrAreEqual ( const IPAddress addr1,
const IPAddress addr2,
const IPAddress netmask 
) [static]

Test if the masked addresses (ie the mask is applied to addr1 and addr2) are equal.

Definition at line 225 of file IPAddress.cc.

Referenced by RoutingTable::findBestMatchingRoute(), getLevel(), RoutingTable::getMulticastRoutesFor(), and isNetwork().

{
    // return addr1.doAnd(netmask).equals(addr2.doAnd(netmask));
    // Looks weird, but is the same and is faster
    return !(bool)((addr1.addr ^ addr2.addr) & netmask.addr);
}

bool IPAddress::operator!= ( const IPAddress addr1  )  const [inline]

Returns !equals(addr).

Definition at line 235 of file IPAddress.h.

{return !equals(addr1);}

bool IPAddress::operator< ( const IPAddress addr1  )  const [inline]

Compares two IP addresses.

Definition at line 240 of file IPAddress.h.

{return getInt()<addr1.getInt();}

IPAddress& IPAddress::operator= ( const IPAddress obj  )  [inline]

Assignment

Definition at line 126 of file IPAddress.h.

{addr = obj.addr; return *this;}

bool IPAddress::operator== ( const IPAddress addr1  )  const [inline]

Returns equals(addr).

Definition at line 230 of file IPAddress.h.

{return equals(addr1);}

bool IPAddress::parseIPAddress ( const char *  text,
unsigned char  tobytes[] 
) [static, protected]

Definition at line 54 of file IPAddress.cc.

Referenced by isWellFormed(), and set().

{
    if (!text)
        return false;

    if (!strcmp(text,"<unspec>"))
    {
        tobytes[0] = tobytes[1] = tobytes[2] = tobytes[3] = 0;
        return true;
    }

    const char *s = text;
    int i=0;
    while(true)
    {
        if (*s<'0' || *s>'9')
            return false;  // missing number

        // read and store number
        int num = 0;
        while (*s>='0' && *s<='9')
            num = 10*num + (*s++ - '0');
        if (num>255)
            return false; // number too big
        tobytes[i++] = (unsigned char) num;

        if (!*s)
            break;  // end of string
        if (*s!='.')
            return false;  // invalid char after number
        if (i==4)
            return false;  // read 4th number and not yet EOS

        // skip '.'
        s++;
    }
    return i==4;  // must have all 4 numbers
}

bool IPAddress::prefixMatches ( const IPAddress to_cmp,
int  numbits 
) const

Compares the first numbits bits of the two addresses.

Definition at line 178 of file IPAddress.cc.

Referenced by LDP::lookupLabel().

{
    if (numbits<1)
        return true;

    uint32 addr2 = to_cmp.getInt();

    if (numbits > 31)
        return addr==addr2;

    // The right shift on an unsigned int produces 0 on the left
    uint32 mask = 0xFFFFFFFF;
    mask = ~(mask >> numbits);

    return (addr & mask) == (addr2 & mask);
}

void IPAddress::set ( const char *  t  ) 

IP address given as text: "192.66.86.1"

Definition at line 93 of file IPAddress.cc.

{
    unsigned char buf[4];
    if (!text)
        opp_error("IP address string is NULL");
    bool ok = parseIPAddress(text, buf);
    if (!ok)
        opp_error("Invalid IP address string `%s'", text);
    set(buf[0], buf[1], buf[2], buf[3]);
}

void IPAddress::set ( uint32  ip  )  [inline]

name Setting the address IP address as int

Definition at line 110 of file IPAddress.h.

Referenced by doUnpacking().

{addr = ip;}

void IPAddress::set ( int  i0,
int  i1,
int  i2,
int  i3 
)

IP address bytes: "i0.i1.i2.i3" format

Definition at line 49 of file IPAddress.cc.

{
    addr = (i0 << 24) | (i1 << 16) | (i2 << 8) | i3;
}

std::string IPAddress::str (  )  const

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

Definition at line 104 of file IPAddress.cc.

Referenced by RoutingTable::addRoute(), IP::encapsulate(), RSVP::evalNextHopInterface(), LDP::findInterfaceFromPeerAddr(), TED::getInterfaceAddrByPeerAddress(), LDP::getPeerSocket(), TED::initialize(), operator<<(), TED::peerRemoteInterface(), SCTPPathVariables::SCTPPathVariables(), and OSPF::Interface::SetIfIndex().

{
    if (isUnspecified())
        return std::string("<unspec>");

    char buf[IPADDRESS_STRING_SIZE];
    sprintf(buf, "%u.%u.%u.%u", (addr>>24)&255, (addr>>16)&255, (addr>>8)&255, addr&255);
    return std::string(buf);
}


Member Data Documentation

uint32 IPAddress::addr [protected]

224.0.0.4 All DVMRP routers

Definition at line 69 of file IPAddress.h.

224.0.0.1 All hosts on a subnet

Definition at line 67 of file IPAddress.h.

Referenced by RoutingTableParser::parseMulticastGroups().

224.0.0.6 All OSPF Designated Routers

Definition at line 71 of file IPAddress.h.

224.0.0.5 All OSPF routers (DR Others)

Definition at line 70 of file IPAddress.h.

224.0.0.2 All routers on a subnet

Definition at line 68 of file IPAddress.h.

Referenced by LDP::handleMessage(), and RoutingTableParser::parseMulticastGroups().

127.0.0.1

Definition at line 63 of file IPAddress.h.

Referenced by RoutingTable::configureLoopbackForIPv4(), and ICMP::sendErrorMessage().

255.0.0.0

Definition at line 64 of file IPAddress.h.

Referenced by RoutingTable::configureLoopbackForIPv4().


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