#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 |
IPv4 address.
Definition at line 44 of file IPAddress.h.
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] |
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] |
IPAddress::IPAddress | ( | const IPAddress & | obj | ) | [inline] |
IPAddress::~IPAddress | ( | ) | [inline] |
Definition at line 102 of file IPAddress.h.
{}
Returns binary AND of the two addresses
Definition at line 143 of file IPAddress.h.
Referenced by RoutingTable::addRoute(), and NetworkInfo::dumpRoutingInfo().
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().
int IPAddress::getDByte | ( | int | i | ) | const [inline] |
Returns the corresponding part of the address specified by the index ("[0].[1].[2].[3]")
Definition at line 159 of file IPAddress.h.
Referenced by RoutingTable::findBestMatchingRoute(), RoutingTable::getGatewayForDestAddr(), RoutingTable::getInterfaceByAddress(), RoutingTable::getInterfaceForDestAddr(), getIPClass(), RoutingTable::getMulticastRoutesFor(), getNetwork(), RoutingTable::isLocalAddress(), and RoutingTable::isLocalMulticastAddress().
{return (addr >> (3-i)*8) & 0xFF;}
uint32 IPAddress::getInt | ( | ) | const [inline] |
Returns the address as an int.
Definition at line 153 of file IPAddress.h.
Referenced by RoutingTable::configureRouterId(), RSVP::delSession(), doPacking(), IPvXAddress::equals(), RoutingTable::findBestMatchingRoute(), OSPF::Area::FindSameOrWorseCostRoute(), getNumMatchingPrefixBits(), TED::initialize(), OSPFRouting::initialize(), OSPF::Router::IsDestinationUnreachable(), operator<(), OSPF::Area::OriginateSummaryLSA(), prefixMatches(), LDP::processLDPHello(), SimpleClassifier::readItemFromXML(), RSVP::readTrafficSessionFromXML(), SCTPAssociation::sendInit(), and OSPF::Router::UpdateExternalRoute().
{return addr;}
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.
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] |
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.
Definition at line 133 of file IPAddress.h.
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().
{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.)
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().
bool IPAddress::operator!= | ( | const IPAddress & | addr1 | ) | const [inline] |
bool IPAddress::operator< | ( | const IPAddress & | addr1 | ) | const [inline] |
Compares two IP addresses.
Definition at line 240 of file IPAddress.h.
bool IPAddress::operator== | ( | const IPAddress & | addr1 | ) | const [inline] |
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().
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().
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); }
uint32 IPAddress::addr [protected] |
Definition at line 48 of file IPAddress.h.
Referenced by doAnd(), equals(), getNetmaskLength(), getNumMatchingPrefixBits(), keepFirstBits(), maskedAddrAreEqual(), operator=(), prefixMatches(), LDP::processLABEL_REQUEST(), LDP::processNOTIFICATION(), set(), and str().
const IPAddress IPAddress::ALL_DVMRP_ROUTERS_MCAST [static] |
224.0.0.4 All DVMRP routers
Definition at line 69 of file IPAddress.h.
const IPAddress IPAddress::ALL_HOSTS_MCAST [static] |
224.0.0.1 All hosts on a subnet
Definition at line 67 of file IPAddress.h.
Referenced by RoutingTableParser::parseMulticastGroups().
const IPAddress IPAddress::ALL_OSPF_DESIGNATED_ROUTERS_MCAST [static] |
224.0.0.6 All OSPF Designated Routers
Definition at line 71 of file IPAddress.h.
const IPAddress IPAddress::ALL_OSPF_ROUTERS_MCAST [static] |
224.0.0.5 All OSPF routers (DR Others)
Definition at line 70 of file IPAddress.h.
const IPAddress IPAddress::ALL_ROUTERS_MCAST [static] |
224.0.0.2 All routers on a subnet
Definition at line 68 of file IPAddress.h.
Referenced by LDP::handleMessage(), and RoutingTableParser::parseMulticastGroups().
const IPAddress IPAddress::ALLONES_ADDRESS [static] |
255.255.255.255
Definition at line 65 of file IPAddress.h.
Referenced by NetworkConfigurator::assignAddresses(), FlatNetworkConfigurator::assignAddresses(), RoutingTable::configureRouterId(), NetworkInfo::dumpRoutingInfo(), and RoutingTable::updateNetmaskRoutes().
const IPAddress IPAddress::LOOPBACK_ADDRESS [static] |
127.0.0.1
Definition at line 63 of file IPAddress.h.
Referenced by RoutingTable::configureLoopbackForIPv4(), and ICMP::sendErrorMessage().
const IPAddress IPAddress::LOOPBACK_NETMASK [static] |
255.0.0.0
Definition at line 64 of file IPAddress.h.
Referenced by RoutingTable::configureLoopbackForIPv4().
const IPAddress IPAddress::UNSPECIFIED_ADDRESS [static] |
0.0.0.0
Definition at line 62 of file IPAddress.h.
Referenced by RoutingTableParser::parseRouting(), RTPInnerPacket::RTPInnerPacket(), RTPInterfacePacket::RTPInterfacePacket(), and RTPParticipantInfo::RTPParticipantInfo().