Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

IPAddress Class Reference

#include <IPAddress.h>

List of all members.


Detailed Description

IPv4 address.


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 numMatchingPrefixBits (const IPAddress &to_cmp) const
int netmaskLength () const
bool operator== (const IPAddress &addr1) const
bool operator!= (const IPAddress &addr1) const
bool operator< (const IPAddress &addr1) const
 IPAddress ()
 IPAddress (uint32 i)
 IPAddress (int i0, int i1, int i2, int i3)
 IPAddress (const char *t)
 IPAddress (const IPAddress &obj)
 ~IPAddress ()
void set (uint32 i)
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

unsigned char addr [4]


Constructor & Destructor Documentation

IPAddress::IPAddress  )  [inline]
 

Default constructor, initializes to 0.0.0.0.

00098 {addr[0] = addr[1] = addr[2] = addr[3] = 0;}

IPAddress::IPAddress uint32  i  ) 
 

IP address as int

00046 {
00047     set(ip);
00048 }

IPAddress::IPAddress int  i0,
int  i1,
int  i2,
int  i3
 

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

00051 {
00052     addr[0] = i0;
00053     addr[1] = i1;
00054     addr[2] = i2;
00055     addr[3] = i3;
00056 }

IPAddress::IPAddress const char *  t  ) 
 

IP address given as text: "192.66.86.1"

00059 {
00060     set(text);
00061 }

IPAddress::IPAddress const IPAddress obj  ) 
 

Copy constructor

00064 {
00065     operator=(obj);
00066 }

IPAddress::~IPAddress  )  [inline]
 

00120 {}


Member Function Documentation

IPAddress IPAddress::doAnd const IPAddress ip  )  const
 

Returns binary AND of the two addresses

00167 {
00168     return IPAddress(addr[0] & ip.addr[0], addr[1] & ip.addr[1],
00169                      addr[2] & ip.addr[2], addr[3] & ip.addr[3]);
00170 }

bool IPAddress::equals const IPAddress toCmp  )  const
 

Returns true if the two addresses are equal

00161 {
00162     return (addr[0] == toCmp.addr[0]) && (addr[1] == toCmp.addr[1]) &&
00163            (addr[2] == toCmp.addr[2]) && (addr[3] == toCmp.addr[3]);
00164 }

int IPAddress::getDByte int  i  )  const [inline]
 

Returns the corresponding part of the address specified by the index ("[0].[1].[2].[3]")

00177 {return addr[i];}

uint32 IPAddress::getInt  )  const
 

Returns the address as an int.

00134 {
00135     return (addr[0] << 24)
00136         +  (addr[1] << 16)
00137         +  (addr[2] << 8)
00138         +  (addr[3]);
00139 }

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.)

00174 {
00175     if ((addr[0] & 0x80) == 0x00)       // 0xxxx
00176         return 'A';
00177     else if ((addr[0] & 0xC0) == 0x80)  // 10xxx
00178         return 'B';
00179     else if ((addr[0] & 0xE0) == 0xC0)  // 110xx
00180         return 'C';
00181     else if ((addr[0] & 0xF0) == 0xE0)  // 1110x
00182         return 'D';
00183     else if ((addr[0] & 0xF8) == 0xF0)  // 11110
00184         return 'E';
00185     else
00186         return '?';
00187 }

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.

00190 {
00191     switch (getIPClass())
00192     {
00193     case 'A':
00194         // Class A: network = 7 bits
00195         return IPAddress(addr[0], 0, 0, 0);
00196     case 'B':
00197         // Class B: network = 14 bits
00198         return IPAddress(addr[0], addr[1], 0, 0);
00199     case 'C':
00200         // Class C: network = 21 bits
00201         return IPAddress(addr[0], addr[1], addr[2], 0);
00202     default:
00203         // Class D or E
00204         return IPAddress();
00205     }
00206 }

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.

00209 {
00210     switch (getIPClass())
00211     {
00212     case 'A':
00213         // Class A: network = 7 bits
00214         return IPAddress(255, 0, 0, 0);
00215     case 'B':
00216         // Class B: network = 14 bits
00217         return IPAddress(255, 255, 0, 0);
00218     case 'C':
00219         // Class C: network = 21 bits
00220         return IPAddress(255, 255, 255, 0);
00221     default:
00222         // Class D or E: return null address
00223         return IPAddress();
00224     }
00225 }

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.

00197 {return addr[0]==224 && addr[1]==0 && addr[2]==0;}

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.

00189 {return (addr[0] & 0xF0)==0xE0;}

bool IPAddress::isNetwork const IPAddress toCmp  )  const
 

Indicates if the address is from the same network

00229 {
00230     switch (getIPClass())
00231     {
00232     case 'A':
00233         if (addr[0] == toCmp.addr[0])
00234             return true;
00235         break;
00236     case 'B':
00237         if ((addr[0] == toCmp.addr[0]) &&
00238             (addr[1] == toCmp.addr[1]))
00239             return true;
00240         break;
00241     case 'C':
00242         if ((addr[0] == toCmp.addr[0]) &&
00243             (addr[1] == toCmp.addr[1]) &&
00244             (addr[2] == toCmp.addr[2]))
00245             return true;
00246         break;
00247     default:
00248         // Class D or E
00249         return false;
00250     }
00251     // not equal
00252     return false;
00253 }

bool IPAddress::isUnspecified  )  const [inline]
 

True if all four address bytes are zero. The null value is customarily used to represent a missing, unspecified or invalid address in the simulation models.

00151 {return !addr[0] && !addr[1] && !addr[2] && !addr[3];}

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.)

00329 {
00330     unsigned char dummy[4];
00331     return parseIPAddress(text, dummy);
00332 }

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

00303 {
00304     if (n > 31) return;
00305 
00306     int len_bytes = n / 8;
00307 
00308     uint32 mask = 0xFF;
00309     mask = ~(mask >> ((n - (len_bytes * 8))));
00310 
00311     addr[len_bytes] = addr[len_bytes] & mask;
00312 
00313     for (int i = len_bytes+1; i < 4; i++)
00314         addr[i] = 0;
00315 }

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.

00321 {
00322     if (addr1.doAnd(netmask).equals(addr2.doAnd(netmask)))
00323         return true;
00324 
00325     return false;
00326 }

int IPAddress::netmaskLength  )  const
 

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

00293 {
00294     uint32 addr = getInt();
00295     int i;
00296     for (i=0; i<31; i++)
00297         if (addr & (1 << i))
00298             return 32-i;
00299     return 0;
00300 }

int IPAddress::numMatchingPrefixBits 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.

00275 {
00276     uint32 addr1 = getInt();
00277     uint32 addr2 = to_cmp.getInt();
00278 
00279     uint32 res = addr1 ^ addr2;
00280     // If the bits are equal, there is a 0, so counting
00281     // the zeros from the left
00282     int i;
00283     for (i = 31; i >= 0; i--) {
00284         if (res & (1 << i)) {
00285             // 1, means not equal, so stop
00286             return 31 - i;
00287         }
00288     }
00289     return 32;
00290 }

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

Returns !equals(addr).

00253 {return !equals(addr1);}

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

Compares two IP addresses.

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

IPAddress & IPAddress::operator= const IPAddress obj  ) 
 

Assignment

00142 {
00143     addr[0] = obj.addr[0];
00144     addr[1] = obj.addr[1];
00145     addr[2] = obj.addr[2];
00146     addr[3] = obj.addr[3];
00147     return *this;
00148 }

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

Returns equals(addr).

00248 {return equals(addr1);}

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

00086 {
00087     if (!text)
00088         return false;
00089 
00090     if (!strcmp(text,"<unspec>"))
00091     {
00092         tobytes[0] = tobytes[1] = tobytes[2] = tobytes[3] = 0;
00093         return true;
00094     }
00095 
00096     const char *s = text;
00097     int i=0;
00098     while(true)
00099     {
00100         if (*s<'0' || *s>'9')
00101             return false;  // missing number
00102 
00103         // read and store number
00104         int num = 0;
00105         while (*s>='0' && *s<='9')
00106             num = 10*num + (*s++ - '0');
00107         if (num>255)
00108             return false; // number too big
00109         tobytes[i++] = (unsigned char) num;
00110 
00111         if (!*s)
00112             break;  // end of string
00113         if (*s!='.')
00114             return false;  // invalid char after number
00115         if (i==4)
00116             return false;  // read 4th number and not yet EOS
00117 
00118         // skip '.'
00119         s++;
00120     }
00121     return i==4;  // must have all 4 numbers
00122 }

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

Compares the first numbits bits of the two addresses.

00257 {
00258     if (numbits<1)
00259         return true;
00260 
00261     uint32 addr1 = getInt();
00262     uint32 addr2 = to_cmp.getInt();
00263 
00264     if (numbits > 31)
00265         return addr1==addr2;
00266 
00267     // The right shift on an unsigned int produces 0 on the left
00268     uint32 mask = 0xFFFFFFFF;
00269     mask = ~(mask >> numbits);
00270 
00271     return (addr1 & mask) == (addr2 & mask);
00272 }

void IPAddress::set const char *  t  ) 
 

IP address given as text: "192.66.86.1"

00125 {
00126     if (!text)
00127         opp_error("IP address string is NULL");
00128     bool ok = parseIPAddress(text, addr);
00129     if (!ok)
00130         opp_error("Invalid IP address string `%s'", text);
00131 }

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

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

00077 {
00078     addr[0] = i0;
00079     addr[1] = i1;
00080     addr[2] = i2;
00081     addr[3] = i3;
00082 }

void IPAddress::set uint32  i  ) 
 

IP address as int

00069 {
00070     addr[0] = (ip >> 24) & 0xFF;
00071     addr[1] = (ip >> 16) & 0xFF;
00072     addr[2] = (ip >> 8) & 0xFF;
00073     addr[3] = ip & 0xFF;
00074 }

std::string IPAddress::str  )  const
 

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

00151 {
00152     if (isUnspecified())
00153         return std::string("<unspec>");
00154 
00155     char buf[ADDRESS_STRING_SIZE];
00156     sprintf(buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
00157     return std::string(buf);
00158 }


Member Data Documentation

unsigned char IPAddress::addr[4] [protected]
 

const IPAddress IPAddress::ALL_DVMRP_ROUTERS_MCAST [static]
 

224.0.0.4 All DVMRP routers

const IPAddress IPAddress::ALL_HOSTS_MCAST [static]
 

224.0.0.1 All hosts on a subnet

const IPAddress IPAddress::ALL_OSPF_DESIGNATED_ROUTERS_MCAST [static]
 

224.0.0.6 All OSPF Designated Routers

const IPAddress IPAddress::ALL_OSPF_ROUTERS_MCAST [static]
 

224.0.0.5 All OSPF routers (DR Others)

const IPAddress IPAddress::ALL_ROUTERS_MCAST [static]
 

224.0.0.2 All routers on a subnet

const IPAddress IPAddress::ALLONES_ADDRESS [static]
 

255.255.255.255

const IPAddress IPAddress::LOOPBACK_ADDRESS [static]
 

127.0.0.1

const IPAddress IPAddress::LOOPBACK_NETMASK [static]
 

255.0.0.0

const IPAddress IPAddress::UNSPECIFIED_ADDRESS [static]
 

0.0.0.0


The documentation for this class was generated from the following files:
Generated on Thu Oct 19 18:22:24 2006 for INET Framework for OMNeT++/OMNEST by  doxygen 1.4.0