#include <IPAddress.h>
Public Member Functions | |
IPAddress & | operator= (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 | |
uint32 | addr |
IPAddress::IPAddress | ( | ) | [inline] |
IPAddress::IPAddress | ( | int | i0, | |
int | i1, | |||
int | i2, | |||
int | i3 | |||
) |
IPAddress::IPAddress | ( | const char * | t | ) |
IPAddress::IPAddress | ( | const IPAddress & | obj | ) |
bool IPAddress::equals | ( | const IPAddress & | toCmp | ) | const |
int IPAddress::getDByte | ( | int | i | ) | const [inline] |
Returns the corresponding part of the address specified by the index ("[0].[1].[2].[3]")
00176 {return (addr >> (3-i)*8) & 0xFF;}
uint32 IPAddress::getInt | ( | ) | const |
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.)
00159 { 00160 unsigned char buf = getDByte(0); 00161 if ((buf & 0x80) == 0x00) // 0xxxx 00162 return 'A'; 00163 else if ((buf & 0xC0) == 0x80) // 10xxx 00164 return 'B'; 00165 else if ((buf & 0xE0) == 0xC0) // 110xx 00166 return 'C'; 00167 else if ((buf & 0xF0) == 0xE0) // 1110x 00168 return 'D'; 00169 else if ((buf & 0xF8) == 0xF0) // 11110 00170 return 'E'; 00171 else 00172 return '?'; 00173 }
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.
00176 { 00177 switch (getIPClass()) 00178 { 00179 case 'A': 00180 // Class A: network = 7 bits 00181 return IPAddress(getDByte(0), 0, 0, 0); 00182 case 'B': 00183 // Class B: network = 14 bits 00184 return IPAddress(getDByte(0), getDByte(1), 0, 0); 00185 case 'C': 00186 // Class C: network = 21 bits 00187 return IPAddress(getDByte(0), getDByte(1), getDByte(2), 0); 00188 default: 00189 // Class D or E 00190 return IPAddress(); 00191 } 00192 }
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.
00195 { 00196 switch (getIPClass()) 00197 { 00198 case 'A': 00199 // Class A: network = 7 bits 00200 return IPAddress(255, 0, 0, 0); 00201 case 'B': 00202 // Class B: network = 14 bits 00203 return IPAddress(255, 255, 0, 0); 00204 case 'C': 00205 // Class C: network = 21 bits 00206 return IPAddress(255, 255, 255, 0); 00207 default: 00208 // Class D or E: return null address 00209 return IPAddress(); 00210 } 00211 }
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.
00196 {return addr & 0xFFFFFF00 == 0xF4000000;}
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.
00188 {return (addr & 0xF0000000)==0xE0000000;}
bool IPAddress::isNetwork | ( | const IPAddress & | toCmp | ) | const |
Indicates if the address is from the same network
00215 { 00216 IPAddress netmask = getNetworkMask(); 00217 if (netmask.isUnspecified()) return false; // Class is D or E 00218 return maskedAddrAreEqual(*this, toCmp, netmask); 00219 }
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.
00150 {return addr==0;}
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.)
00281 { 00282 unsigned char dummy[4]; 00283 return parseIPAddress(text, dummy); 00284 }
void IPAddress::keepFirstBits | ( | unsigned int | n | ) | [protected] |
int IPAddress::netmaskLength | ( | ) | const |
Counts 1 bits in a netmask. E.g. for 255.255.254.0, it will return 23.
00257 { 00258 int i; 00259 for (i=0; i<31; i++) 00260 if (addr & (1 << i)) 00261 return 32-i; 00262 return 0; 00263 }
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.
00240 { 00241 uint32 addr2 = to_cmp.getInt(); 00242 00243 uint32 res = addr ^ addr2; 00244 // If the bits are equal, there is a 0, so counting 00245 // the zeros from the left 00246 int i; 00247 for (i = 31; i >= 0; i--) { 00248 if (res & (1 << i)) { 00249 // 1, means not equal, so stop 00250 return 31 - i; 00251 } 00252 } 00253 return 32; 00254 }
bool IPAddress::operator!= | ( | const IPAddress & | addr1 | ) | const [inline] |
bool IPAddress::operator< | ( | const IPAddress & | addr1 | ) | const [inline] |
bool IPAddress::operator== | ( | const IPAddress & | addr1 | ) | const [inline] |
bool IPAddress::parseIPAddress | ( | const char * | text, | |
unsigned char | tobytes[] | |||
) | [static, protected] |
00077 { 00078 if (!text) 00079 return false; 00080 00081 if (!strcmp(text,"<unspec>")) 00082 { 00083 tobytes[0] = tobytes[1] = tobytes[2] = tobytes[3] = 0; 00084 return true; 00085 } 00086 00087 const char *s = text; 00088 int i=0; 00089 while(true) 00090 { 00091 if (*s<'0' || *s>'9') 00092 return false; // missing number 00093 00094 // read and store number 00095 int num = 0; 00096 while (*s>='0' && *s<='9') 00097 num = 10*num + (*s++ - '0'); 00098 if (num>255) 00099 return false; // number too big 00100 tobytes[i++] = (unsigned char) num; 00101 00102 if (!*s) 00103 break; // end of string 00104 if (*s!='.') 00105 return false; // invalid char after number 00106 if (i==4) 00107 return false; // read 4th number and not yet EOS 00108 00109 // skip '.' 00110 s++; 00111 } 00112 return i==4; // must have all 4 numbers 00113 }
bool IPAddress::prefixMatches | ( | const IPAddress & | to_cmp, | |
int | numbits | |||
) | const |
Compares the first numbits bits of the two addresses.
00223 { 00224 if (numbits<1) 00225 return true; 00226 00227 uint32 addr2 = to_cmp.getInt(); 00228 00229 if (numbits > 31) 00230 return addr==addr2; 00231 00232 // The right shift on an unsigned int produces 0 on the left 00233 uint32 mask = 0xFFFFFFFF; 00234 mask = ~(mask >> numbits); 00235 00236 return (addr & mask) == (addr2 & mask); 00237 }
void IPAddress::set | ( | const char * | t | ) |
IP address given as text: "192.66.86.1"
00116 { 00117 unsigned char buf[4]; 00118 if (!text) 00119 opp_error("IP address string is NULL"); 00120 bool ok = parseIPAddress(text, buf); 00121 if (!ok) 00122 opp_error("Invalid IP address string `%s'", text); 00123 set(buf[0], buf[1], buf[2], buf[3]); 00124 }
void IPAddress::set | ( | int | i0, | |
int | i1, | |||
int | i2, | |||
int | i3 | |||
) |
std::string IPAddress::str | ( | ) | const |
Returns the string representation of the address (e.g. "152.66.86.92")
00138 { 00139 if (isUnspecified()) 00140 return std::string("<unspec>"); 00141 00142 char buf[ADDRESS_STRING_SIZE]; 00143 sprintf(buf, "%hhu.%hhu.%hhu.%hhu", addr>>24, addr>>16, addr>>8, addr); 00144 return std::string(buf); 00145 }
uint32 IPAddress::addr [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