#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 | 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 |
IPAddress::IPAddress | ( | ) | [inline] |
name Constructors, destructor Default constructor, initializes to 0.0.0.0.
Referenced by getNetwork(), and getNetworkMask().
00080 {addr = 0;}
IPAddress::IPAddress | ( | int | i0, | |
int | i1, | |||
int | i2, | |||
int | i3 | |||
) | [inline] |
IPAddress::IPAddress | ( | const char * | text | ) | [inline] |
void IPAddress::keepFirstBits | ( | unsigned int | n | ) | [protected] |
bool IPAddress::parseIPAddress | ( | const char * | text, | |
unsigned char | tobytes[] | |||
) | [static, protected] |
Referenced by isWellFormed(), and set().
00055 { 00056 if (!text) 00057 return false; 00058 00059 if (!strcmp(text,"<unspec>")) 00060 { 00061 tobytes[0] = tobytes[1] = tobytes[2] = tobytes[3] = 0; 00062 return true; 00063 } 00064 00065 const char *s = text; 00066 int i=0; 00067 while(true) 00068 { 00069 if (*s<'0' || *s>'9') 00070 return false; // missing number 00071 00072 // read and store number 00073 int num = 0; 00074 while (*s>='0' && *s<='9') 00075 num = 10*num + (*s++ - '0'); 00076 if (num>255) 00077 return false; // number too big 00078 tobytes[i++] = (unsigned char) num; 00079 00080 if (!*s) 00081 break; // end of string 00082 if (*s!='.') 00083 return false; // invalid char after number 00084 if (i==4) 00085 return false; // read 4th number and not yet EOS 00086 00087 // skip '.' 00088 s++; 00089 } 00090 return i==4; // must have all 4 numbers 00091 }
void IPAddress::set | ( | uint32 | ip | ) | [inline] |
void IPAddress::set | ( | int | i0, | |
int | i1, | |||
int | i2, | |||
int | i3 | |||
) |
void IPAddress::set | ( | const char * | t | ) |
IP address given as text: "192.66.86.1"
00094 { 00095 unsigned char buf[4]; 00096 if (!text) 00097 opp_error("IP address string is NULL"); 00098 bool ok = parseIPAddress(text, buf); 00099 if (!ok) 00100 opp_error("Invalid IP address string `%s'", text); 00101 set(buf[0], buf[1], buf[2], buf[3]); 00102 }
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.
Referenced by RoutingTable::addRoute(), RSVP::allocateResource(), NetworkConfigurator::assignAddresses(), RoutingTable::configureRouterId(), NetworkInfo::dumpRoutingInfo(), IP::encapsulate(), RoutingTable::getInterfaceByAddress(), IPAddressResolver::getIPv4AddressFrom(), IPRoute::info(), TED::initialize(), OSPF::Router::IsDestinationUnreachable(), isNetwork(), RSVP::pathProblem(), ARP::processARPPacket(), RSVP::processPathMsg(), LDP::rebuildFecList(), RSVP::refreshPath(), RoutingTable::routeMatches(), RSVP::scheduleRefreshTimer(), ARP::sendARPRequest(), str(), and RoutingTable::updateDisplayString().
00133 {return addr==0;}
bool IPAddress::equals | ( | const IPAddress & | toCmp | ) | const [inline] |
Returns true if the two addresses are equal
Referenced by RoutingTable::isLocalMulticastAddress(), and RoutingTable::routeMatches().
Returns binary AND of the two addresses
Referenced by RoutingTable::addRoute(), and NetworkInfo::dumpRoutingInfo().
std::string IPAddress::str | ( | ) | const |
Returns the string representation of the address (e.g. "152.66.86.92")
Referenced by RoutingTable::addRoute(), IP::encapsulate(), RSVP::evalNextHopInterface(), LDP::findInterfaceFromPeerAddr(), TED::getInterfaceAddrByPeerAddress(), LDP::getPeerSocket(), TED::initialize(), operator<<(), TED::peerRemoteInterface(), OSPF::Interface::SetIfIndex(), and RoutingTable::updateDisplayString().
00105 { 00106 if (isUnspecified()) 00107 return std::string("<unspec>"); 00108 00109 char buf[IPADDRESS_STRING_SIZE]; 00110 sprintf(buf, "%u.%u.%u.%u", (addr>>24)&255, (addr>>16)&255, (addr>>8)&255, addr&255); 00111 return std::string(buf); 00112 }
uint32 IPAddress::getInt | ( | ) | const [inline] |
Returns the address as an int.
Referenced by RoutingTable::configureRouterId(), RSVP::delSession(), doPacking(), IPvXAddress::equals(), RoutingTable::findBestMatchingRoute(), OSPF::Area::FindSameOrWorseCostRoute(), RoutingTable::getGatewayForDestAddr(), RoutingTable::getInterfaceByAddress(), RoutingTable::getInterfaceForDestAddr(), RoutingTable::getMulticastRoutesFor(), getNumMatchingPrefixBits(), TED::initialize(), OSPFRouting::initialize(), OSPF::Router::IsDestinationUnreachable(), RoutingTable::isLocalAddress(), RoutingTable::isLocalMulticastAddress(), operator<(), OSPF::Area::OriginateSummaryLSA(), prefixMatches(), LDP::processLDPHello(), SimpleClassifier::readItemFromXML(), RSVP::readTrafficSessionFromXML(), SCTPAssociation::sendInit(), and OSPF::Router::UpdateExternalRoute().
00153 {return addr;}
int IPAddress::getDByte | ( | int | i | ) | const [inline] |
Returns the corresponding part of the address specified by the index ("[0].[1].[2].[3]")
Referenced by getIPClass(), and getNetwork().
00159 {return (addr >> (3-i)*8) & 0xFF;}
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.)
Referenced by getNetwork(), and getNetworkMask().
00115 { 00116 unsigned char buf = getDByte(0); 00117 if ((buf & 0x80) == 0x00) // 0xxxx 00118 return 'A'; 00119 else if ((buf & 0xC0) == 0x80) // 10xxx 00120 return 'B'; 00121 else if ((buf & 0xE0) == 0xC0) // 110xx 00122 return 'C'; 00123 else if ((buf & 0xF0) == 0xE0) // 1110x 00124 return 'D'; 00125 else if ((buf & 0xF8) == 0xF0) // 11110 00126 return 'E'; 00127 else 00128 return '?'; 00129 }
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.
Referenced by RoutingTable::addRoute(), RTP::createSocket(), RTCP::createSocket(), NetworkInfo::dumpRoutingInfo(), LDP::rebuildFecList(), and TED::rebuildRoutingTable().
00171 {return (addr & 0xF0000000)==0xE0000000;}
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.
Referenced by IP::routeMulticastPacket().
00179 {return (addr & 0xFFFFFF00) == 0xE0000000;}
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.
00132 { 00133 switch (getIPClass()) 00134 { 00135 case 'A': 00136 // Class A: network = 7 bits 00137 return IPAddress(getDByte(0), 0, 0, 0); 00138 case 'B': 00139 // Class B: network = 14 bits 00140 return IPAddress(getDByte(0), getDByte(1), 0, 0); 00141 case 'C': 00142 // Class C: network = 21 bits 00143 return IPAddress(getDByte(0), getDByte(1), getDByte(2), 0); 00144 default: 00145 // Class D or E 00146 return IPAddress(); 00147 } 00148 }
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.
Referenced by OSPF::Router::IsDestinationUnreachable(), and isNetwork().
00151 { 00152 switch (getIPClass()) 00153 { 00154 case 'A': 00155 // Class A: network = 7 bits 00156 return IPAddress(255, 0, 0, 0); 00157 case 'B': 00158 // Class B: network = 14 bits 00159 return IPAddress(255, 255, 0, 0); 00160 case 'C': 00161 // Class C: network = 21 bits 00162 return IPAddress(255, 255, 255, 0); 00163 default: 00164 // Class D or E: return null address 00165 return IPAddress(); 00166 } 00167 }
bool IPAddress::isNetwork | ( | const IPAddress & | toCmp | ) | const |
Indicates if the address is from the same network
00171 { 00172 IPAddress netmask = getNetworkMask(); 00173 if (netmask.isUnspecified()) return false; // Class is D or E 00174 return maskedAddrAreEqual(*this, toCmp, netmask); 00175 }
bool IPAddress::prefixMatches | ( | const IPAddress & | to_cmp, | |
int | numbits | |||
) | const |
Compares the first numbits bits of the two addresses.
Referenced by LDP::lookupLabel().
00179 { 00180 if (numbits<1) 00181 return true; 00182 00183 uint32 addr2 = to_cmp.getInt(); 00184 00185 if (numbits > 31) 00186 return addr==addr2; 00187 00188 // The right shift on an unsigned int produces 0 on the left 00189 uint32 mask = 0xFFFFFFFF; 00190 mask = ~(mask >> numbits); 00191 00192 return (addr & mask) == (addr2 & mask); 00193 }
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.
00196 { 00197 uint32 addr2 = to_cmp.getInt(); 00198 00199 uint32 res = addr ^ addr2; 00200 // If the bits are equal, there is a 0, so counting 00201 // the zeros from the left 00202 for (int i = 31; i >= 0; i--) { 00203 if (res & (1 << i)) { 00204 // 1, means not equal, so stop 00205 return 31 - i; 00206 } 00207 } 00208 return 32; 00209 }
int IPAddress::getNetmaskLength | ( | ) | const |
Counts 1 bits in a netmask. E.g. for 255.255.254.0, it will return 23.
Referenced by IPv4InterfaceData::info(), and LDP::rebuildFecList().
00212 { 00213 int i; 00214 for (i=0; i<31; i++) 00215 if (addr & (1 << i)) 00216 return 32-i; 00217 return 0; 00218 }
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.
Referenced by RoutingTable::findBestMatchingRoute(), SCTPAssociation::getLevel(), RoutingTable::getMulticastRoutesFor(), and isNetwork().
00228 { 00229 // return addr1.doAnd(netmask).equals(addr2.doAnd(netmask)); 00230 // Looks weird, but is the same and is faster 00231 return !(bool)((addr1.addr ^ addr2.addr) & netmask.addr); 00232 }
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::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.)
Referenced by RoutingTableParser::parseRouting(), and IPvXAddress::tryParse().
00235 { 00236 unsigned char dummy[4]; 00237 return parseIPAddress(text, dummy); 00238 }
uint32 IPAddress::addr [protected] |
const IPAddress IPAddress::UNSPECIFIED_ADDRESS [static] |
const IPAddress IPAddress::LOOPBACK_ADDRESS [static] |
const IPAddress IPAddress::LOOPBACK_NETMASK [static] |
const IPAddress IPAddress::ALLONES_ADDRESS [static] |
const IPAddress IPAddress::ALL_HOSTS_MCAST [static] |
const IPAddress IPAddress::ALL_ROUTERS_MCAST [static] |
224.0.0.2 All routers on a subnet
Referenced by LDP::handleMessage(), and RoutingTableParser::parseMulticastGroups().
const IPAddress IPAddress::ALL_DVMRP_ROUTERS_MCAST [static] |
224.0.0.4 All DVMRP routers
const IPAddress IPAddress::ALL_OSPF_ROUTERS_MCAST [static] |
224.0.0.5 All OSPF routers (DR Others)
const IPAddress IPAddress::ALL_OSPF_DESIGNATED_ROUTERS_MCAST [static] |
224.0.0.6 All OSPF Designated Routers