INET Framework for OMNeT++/OMNEST
inet::Ipv4Address Class Reference

Ipv4 address. More...

#include <Ipv4Address.h>

Public Types

enum  AddressCategory {
  UNSPECIFIED, THIS_NETWORK, LOOPBACK, MULTICAST,
  BROADCAST, IETF, TEST_NET, IPv6_TO_IPv4_RELAY,
  BENCHMARK, RESERVED, LINKLOCAL, PRIVATE_NETWORK,
  GLOBAL
}
 Ipv4 address category. More...
 

Static Protected Member Functions

static bool parseIPAddress (const char *text, unsigned char tobytes[])
 
static void _checkNetmaskLength (int length)
 
static uint32_t _makeNetmask (int length)
 

Protected Attributes

uint32_t addr
 

Predefined addresses

static const Ipv4Address UNSPECIFIED_ADDRESS
 0.0.0.0 More...
 
static const Ipv4Address LOOPBACK_ADDRESS
 127.0.0.1 More...
 
static const Ipv4Address LOOPBACK_NETMASK
 255.0.0.0 More...
 
static const Ipv4Address ALLONES_ADDRESS
 255.255.255.255 More...
 
static const Ipv4Address ALL_HOSTS_MCAST
 224.0.0.1 All hosts on a subnet More...
 
static const Ipv4Address ALL_ROUTERS_MCAST
 224.0.0.2 All routers on a subnet More...
 
static const Ipv4Address ALL_DVMRP_ROUTERS_MCAST
 224.0.0.4 All DVMRP routers More...
 
static const Ipv4Address ALL_OSPF_ROUTERS_MCAST
 224.0.0.5 All OSPF routers (DR Others) More...
 
static const Ipv4Address ALL_OSPF_DESIGNATED_ROUTERS_MCAST
 224.0.0.6 All OSPF Designated Routers More...
 
static const Ipv4Address ALL_IGMPV3_ROUTERS_MCAST
 224.0.0.22 All IGMPv3 routers More...
 
static const Ipv4Address LL_MANET_ROUTERS
 224.0.0.109 Manet all designated routers More...
 
static const Ipv4Address ALL_RIP_ROUTERS_MCAST
 
 Ipv4Address ()
 name Constructors, destructor More...
 
 Ipv4Address (uint32_t ip)
 Ipv4 address as int. More...
 
 Ipv4Address (int i0, int i1, int i2, int i3)
 Ipv4 address bytes: "i0.i1.i2.i3" format. More...
 
 Ipv4Address (const char *text)
 Ipv4 address given as text: "192.66.86.1". More...
 
 Ipv4Address (const Ipv4Address &obj)
 Copy constructor. More...
 
 ~Ipv4Address ()
 
void set (uint32_t ip)
 name Setting the address More...
 
void set (int i0, int i1, int i2, int i3)
 Ipv4 address bytes: "i0.i1.i2.i3" format. More...
 
void set (const char *t)
 Ipv4 address given as text: "192.66.86.1". More...
 
Ipv4Addressoperator= (const Ipv4Address &obj)
 Assignment. More...
 
bool isUnspecified () const
 True if all four address bytes are zero. More...
 
bool equals (const Ipv4Address &toCmp) const
 Returns true if the two addresses are equal. More...
 
Ipv4Address doAnd (const Ipv4Address &ip) const
 Returns binary AND of the two addresses. More...
 
std::string str (bool printUnspec=true) const
 Returns the string representation of the address (e.g. More...
 
uint32_t getInt () const
 Returns the address as an uint32_t in host byte order (e.g. More...
 
int getDByte (int i) const
 Returns the corresponding part of the address specified by the index ("[0].[1].[2].[3]") More...
 
char 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.) More...
 
AddressCategory getAddressCategory () const
 Get the Ipv4 address category. More...
 
bool isLimitedBroadcastAddress () const
 Returns true if this address is the limited broadcast address, i.e. More...
 
bool isMulticast () const
 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. More...
 
bool isUnicast () const
 Returns true if this address is unicast address. More...
 
bool isLinkLocalMulticast () const
 Returns true if this address is in the range 224.0.0.0 to 224.0.0.255. More...
 
Ipv4Address getNetwork () const
 Returns an address with the network part of the address (the bits of the hosts part are to 0). More...
 
Ipv4Address getNetworkMask () const
 Returns an address with the network mask corresponding to the address class. More...
 
bool isNetwork (const Ipv4Address &toCmp) const
 Indicates if the address is from the same network. More...
 
bool prefixMatches (const Ipv4Address &to_cmp, int numbits) const
 Compares the first numbits bits of the two addresses. More...
 
int getNumMatchingPrefixBits (const Ipv4Address &to_cmp) const
 Indicates how many bits from the to_cmp address, starting counting from the left, matches the address. More...
 
int getNetmaskLength () const
 Counts 1 bits in a netmask. More...
 
Ipv4Address getPrefix (int prefixLength) const
 Get the first prefixLength bits of the address, with the rest set to zero. More...
 
bool isValidNetmask () const
 Returns true if the address is a valid netmask, i.e. More...
 
Ipv4Address makeBroadcastAddress (Ipv4Address netmask) const
 Returns the broadcast address for the given netmask. More...
 
bool operator== (const Ipv4Address &addr1) const
 Returns equals(addr). More...
 
bool operator!= (const Ipv4Address &addr1) const
 Returns !equals(addr). More...
 
bool operator< (const Ipv4Address &addr1) const
 Compares two Ipv4 addresses. More...
 
bool operator<= (const Ipv4Address &addr1) const
 
bool operator> (const Ipv4Address &addr1) const
 
bool operator>= (const Ipv4Address &addr1) const
 
MacAddress mapToMulticastMacAddress () const
 Form a MAC address for a multicast IPv4 address, see RFC 1112, section 6.4. More...
 
static bool maskedAddrAreEqual (const Ipv4Address &addr1, const Ipv4Address &addr2, const Ipv4Address &netmask)
 Test if the masked addresses (ie the mask is applied to addr1 and addr2) are equal. More...
 
static bool isWellFormed (const char *text)
 Returns true if the format of the string corresponds to an Ipv4 address with the dotted notation ("192.66.86.1"), and false otherwise. More...
 
static Ipv4Address makeNetmask (int length)
 Creates and returns a netmask with the given length. More...
 

Detailed Description

Ipv4 address.

Member Enumeration Documentation

◆ AddressCategory

Ipv4 address category.

RFC 5735 Special Use Ipv4 Addresses January 2010

  1. Summary Table

Address Block Present Use Reference

0.0.0.0/8 "This" Network RFC 1122, Section 3.2.1.3 10.0.0.0/8 Private-Use Networks RFC 1918 127.0.0.0/8 Loopback RFC 1122, Section 3.2.1.3 169.254.0.0/16 Link Local RFC 3927 172.16.0.0/12 Private-Use Networks RFC 1918 192.0.0.0/24 IETF Protocol Assignments RFC 5736 192.0.2.0/24 TEST-NET-1 RFC 5737 192.88.99.0/24 6to4 Relay Anycast RFC 3068 192.168.0.0/16 Private-Use Networks RFC 1918 198.18.0.0/15 Network Interconnect Device Benchmark Testing RFC 2544 198.51.100.0/24 TEST-NET-2 RFC 5737 203.0.113.0/24 TEST-NET-3 RFC 5737 224.0.0.0/4 Multicast RFC 3171 240.0.0.0/4 Reserved for Future Use RFC 1112, Section 4 255.255.255.255/32 Limited Broadcast RFC 919, Section 7; RFC 922, Section 7

Enumerator
UNSPECIFIED 
THIS_NETWORK 
LOOPBACK 
MULTICAST 
BROADCAST 
IETF 
TEST_NET 
IPv6_TO_IPv4_RELAY 
BENCHMARK 
RESERVED 
LINKLOCAL 
PRIVATE_NETWORK 
GLOBAL 
73  {
74  UNSPECIFIED, // 0.0.0.0
75  THIS_NETWORK, // 0.0.0.0/8
76  LOOPBACK, // 127.0.0.0/8
77  MULTICAST, // 224.0.0.0/4
78  BROADCAST, // 255.255.255.255/32
79  IETF, // 192.0.0.0/24
80  TEST_NET, // 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24
81  IPv6_TO_IPv4_RELAY, // 192.88.99.0/24
82  BENCHMARK, // 198.18.0.0/15
83  RESERVED, // 240.0.0.0/4
84  LINKLOCAL, // 169.254.0.0/16
85  PRIVATE_NETWORK, // 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
86  GLOBAL
87  };

Constructor & Destructor Documentation

◆ Ipv4Address() [1/5]

inet::Ipv4Address::Ipv4Address ( )
inline

name Constructors, destructor

Default constructor, initializes to 0.0.0.0.

112 { addr = 0; }

Referenced by getNetwork(), and getNetworkMask().

◆ Ipv4Address() [2/5]

inet::Ipv4Address::Ipv4Address ( uint32_t  ip)
inlineexplicit

Ipv4 address as int.

117 { addr = ip; }

◆ Ipv4Address() [3/5]

inet::Ipv4Address::Ipv4Address ( int  i0,
int  i1,
int  i2,
int  i3 
)
inline

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

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

◆ Ipv4Address() [4/5]

inet::Ipv4Address::Ipv4Address ( const char *  text)
inlineexplicit

Ipv4 address given as text: "192.66.86.1".

127 { set(text); }

◆ Ipv4Address() [5/5]

inet::Ipv4Address::Ipv4Address ( const Ipv4Address obj)
inline

Copy constructor.

132 { addr = obj.addr; }

◆ ~Ipv4Address()

inet::Ipv4Address::~Ipv4Address ( )
inline
134 {}

Member Function Documentation

◆ _checkNetmaskLength()

void inet::Ipv4Address::_checkNetmaskLength ( int  length)
staticprotected
244 {
245  if (length < 0 || length > 32)
246  throw cRuntimeError("Ipv4Address: wrong netmask length %d (not in 0..32)", length);
247 }

◆ _makeNetmask()

static uint32_t inet::Ipv4Address::_makeNetmask ( int  length)
inlinestaticprotected
46 { return length >= 32 ? 0xffffffffu : ~(0xffffffffu >> length); }

Referenced by prefixMatches().

◆ doAnd()

◆ equals()

bool inet::Ipv4Address::equals ( const Ipv4Address toCmp) const
inline

Returns true if the two addresses are equal.

170 { return addr == toCmp.addr; }

Referenced by inet::bgp::BgpConfigReader::loadEbgpSessionConfig().

◆ getAddressCategory()

Ipv4Address::AddressCategory inet::Ipv4Address::getAddressCategory ( ) const

Get the Ipv4 address category.

126 {
127  if (isUnspecified())
128  return UNSPECIFIED; // 0.0.0.0
129  if ((addr & 0xFF000000u) == 0)
130  return THIS_NETWORK; // 0.0.0.0/8
131  if ((addr & 0xFF000000u) == 0x7F000000u)
132  return LOOPBACK; // 127.0.0.0/8
133  if (isMulticast())
134  return MULTICAST; // 224.0.0.0/4
135  if (addr == 0xFFFFFFFFu)
136  return BROADCAST; // 255.255.255.255/32
137  uint32_t addr24 = addr & 0xFFFFFF00u;
138  if (addr24 == 0xC0000000u)
139  return IETF; // 192.0.0.0/24
140  if ((addr24 == 0xC0000200u) || (addr24 == 0xC6336400u) || (addr24 == 0xCB007100u))
141  return TEST_NET; // 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24
142  if (addr24 == 0xC0586300u)
143  return IPv6_TO_IPv4_RELAY; // 192.88.99.0/24
144  if ((addr & 0xFFFE0000u) == 0xC6120000u)
145  return BENCHMARK; // 198.18.0.0/15
146  if ((addr & 0xF0000000u) == 0xF0000000u)
147  return RESERVED; // 240.0.0.0/4
148  if ((addr & 0xFFFF0000u) == 0xA9FE0000u)
149  return LINKLOCAL; // 169.254.0.0/16
150  if (((addr & 0xFF000000u) == 0x0A000000u)
151  || ((addr & 0xFFF00000u) == 0xAC100000u)
152  || ((addr & 0xFFFF0000u) == 0xC0A80000u))
153  return PRIVATE_NETWORK; // 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
154  return GLOBAL;
155 }

Referenced by inet::sctp::SctpAssociation::getAddressLevel().

◆ getDByte()

◆ getInt()

uint32_t inet::Ipv4Address::getInt ( ) const
inline

Returns the address as an uint32_t in host byte order (e.g.

"10.0.0.1" is 0x0A000001).

186 { return addr; }

Referenced by inet::SctpNatPeer::addressAddedArrived(), inet::ospfv3::Ospfv3Neighbor::addToRetransmissionList(), inet::ospfv2::Neighbor::addToRetransmissionList(), inet::Ipv4FlatNetworkConfigurator::assignAddresses(), inet::ExtLowerUdp::bind(), inet::ospfv2::Ospfv2Area::calculateShortestPathTree(), inet::eigrp::EigrpIpv4Pdm::checkNeighborshipRules(), inet::ExtLowerUdp::connect(), inet::bgp::BgpRouter::createEbgpSession(), inet::bgp::BgpRouter::createIbgpSession(), inet::Ipv4NetworkConfigurator::createInterfaceInfo(), inet::bgp::BgpRouter::deleteBGPRoutingEntry(), inet::RsvpTe::delSession(), inet::ospfv3::Ospfv3Interface::detailedInfo(), inet::doParsimPacking(), inet::Ipv4NetworkConfigurator::findInterfaceOnLinkByNodeAddress(), inet::DhcpServer::getAvailableLease(), inet::eigrp::EigrpIpv4Pdm::getDestIpAddress(), inet::ospfv3::Ospfv3Instance::getNewInterAreaPrefixLinkStateID(), inet::ospfv3::Ospfv3Area::getNewIntraAreaPrefixLinkStateID(), inet::ospfv3::Ospfv3Area::getNewNetworkLinkStateID(), inet::ospfv3::Ospfv3Area::getNewRouterLinkStateID(), getNumMatchingPrefixBits(), inet::DhcpServer::handleStartOperation(), inet::Ted::initializeTED(), inet::ospfv2::Router::installASExternalLSA(), inet::Ipv4RoutingTable::internalAddMulticastRoute(), inet::Ipv4RoutingTable::internalAddRoute(), inet::ospfv2::Ospfv2Area::isAllZero(), inet::ospfv2::Ospfv2Area::isDefaultRoute(), inet::bgp::BgpRouter::isDefaultRoute(), inet::bgp::BgpRouter::isInRoutingTable(), inet::bgp::BgpRouter::isInTable(), inet::ospfv2::Router::lookup(), makeBroadcastAddress(), inet::ospfv2::Router::notifyAboutRoutingTableChanges(), inet::ospfv2::operator&(), inet::ospfv3::operator&(), operator<(), operator<=(), operator>(), operator>=(), inet::ospfv2::operator|(), inet::ospfv3::operator|(), inet::ospfv2::Ospfv2Area::originateRouterLSA(), inet::Ipv4NetworkConfigurator::parseAddressAndSpecifiedBits(), inet::Icmp::possiblyLocalBroadcast(), inet::LinkStateRouting::processLINK_STATE_MESSAGE(), inet::Ipv4NetworkConfigurator::readInterfaceConfiguration(), inet::RsvpClassifier::readItemFromXML(), inet::SctpNatServer::sendInfo(), inet::sctp::SctpAssociation::sendInit(), inet::sctp::SctpHeaderSerializer::serialize(), inet::L3Address::set(), inet::HostAutoConfigurator::setupNetworkLayer(), inet::SctpNatPeer::socketEstablished(), inet::ospfv3::Ospfv3Instance::subtractInterAreaPrefixLinkStateID(), inet::ospfv3::Ospfv3Area::subtractIntraAreaPrefixLinkStateID(), inet::eigrp::EigrpDeviceConfigurator::wildcardToMask(), and inet::MemoryOutputStream::writeIpv4Address().

◆ getIPClass()

char inet::Ipv4Address::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.)

109 {
110  unsigned char buf = getDByte(0);
111  if ((buf & 0x80) == 0x00) // 0xxxx
112  return 'A';
113  else if ((buf & 0xC0) == 0x80) // 10xxx
114  return 'B';
115  else if ((buf & 0xE0) == 0xC0) // 110xx
116  return 'C';
117  else if ((buf & 0xF0) == 0xE0) // 1110x
118  return 'D';
119  else if ((buf & 0xF8) == 0xF0) // 11110
120  return 'E';
121  else
122  return '?';
123 }

Referenced by getNetwork(), and getNetworkMask().

◆ getNetmaskLength()

◆ getNetwork()

Ipv4Address inet::Ipv4Address::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.

158 {
159  switch (getIPClass()) {
160  case 'A':
161  // Class A: network = 7 bits
162  return Ipv4Address(getDByte(0), 0, 0, 0);
163 
164  case 'B':
165  // Class B: network = 14 bits
166  return Ipv4Address(getDByte(0), getDByte(1), 0, 0);
167 
168  case 'C':
169  // Class C: network = 21 bits
170  return Ipv4Address(getDByte(0), getDByte(1), getDByte(2), 0);
171 
172  default:
173  // Class D or E
174  return Ipv4Address();
175  }
176 }

Referenced by inet::eigrp::EigrpDeviceConfigurator::loadEigrpIPv4Networks().

◆ getNetworkMask()

Ipv4Address inet::Ipv4Address::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.

179 {
180  switch (getIPClass()) {
181  case 'A':
182  // Class A: network = 7 bits
183  return Ipv4Address(255, 0, 0, 0);
184 
185  case 'B':
186  // Class B: network = 14 bits
187  return Ipv4Address(255, 255, 0, 0);
188 
189  case 'C':
190  // Class C: network = 21 bits
191  return Ipv4Address(255, 255, 255, 0);
192 
193  default:
194  // Class D or E: return null address
195  return Ipv4Address();
196  }
197 }

Referenced by inet::eigrp::EigrpDeviceConfigurator::isEigrpInterface(), inet::eigrp::EigrpNetworkTable< inet::Ipv4Address >::isInterfaceIncluded(), isNetwork(), and inet::ospfv2::HelloHandler::processPacket().

◆ getNumMatchingPrefixBits()

int inet::Ipv4Address::getNumMatchingPrefixBits ( const Ipv4Address 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 Ipv4 prefixes.

219 {
220  uint32_t addr2 = to_cmp.getInt();
221 
222  uint32_t res = addr ^ addr2;
223  // If the bits are equal, there is a 0, so counting
224  // the zeros from the left
225  for (int i = 31; i >= 0; i--) {
226  if (res & (1 << i)) {
227  // 1, means not equal, so stop
228  return 31 - i;
229  }
230  }
231  return 32;
232 }

◆ getPrefix()

Ipv4Address inet::Ipv4Address::getPrefix ( int  prefixLength) const
inline

Get the first prefixLength bits of the address, with the rest set to zero.

271 { return doAnd(makeNetmask(prefixLength)); }

Referenced by inet::ospfv3::Ospfv3Area::InterAreaPrefixLSAAlreadyExists().

◆ isLimitedBroadcastAddress()

bool inet::Ipv4Address::isLimitedBroadcastAddress ( ) const
inline

Returns true if this address is the limited broadcast address, i.e.

255.255.255.255.

209 { return addr == 0xFFFFFFFF; }

Referenced by inet::Ipv4::datagramLocalOut(), inet::L3Address::isBroadcast(), inet::L3Address::isUnicast(), inet::Icmp::maySendErrorMessage(), inet::Ipv4::preroutingFinish(), and inet::Ipv4::resolveNextHopMacAddress().

◆ isLinkLocalMulticast()

bool inet::Ipv4Address::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.

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

Referenced by inet::PimSm::addNewRouteSG(), inet::Ipv4::forwardMulticastPacket(), inet::Igmpv2::multicastGroupJoined(), inet::Igmpv2::multicastGroupLeft(), inet::Igmpv3::multicastSourceListChanged(), inet::Ipv4::preroutingFinish(), inet::Igmpv3::sendGroupReport(), inet::Igmpv2::sendLeave(), inet::Igmpv2::sendQuery(), and inet::Igmpv2::sendReport().

◆ isMulticast()

bool inet::Ipv4Address::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.

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

Referenced by inet::Ipv4InterfaceData::addMulticastListener(), inet::PimSm::addNewRouteSG(), inet::Ipv4InterfaceData::changeMulticastGroupMembership(), inet::Ipv4InterfaceData::findHostGroupData(), inet::Ipv4InterfaceData::findRouterGroupData(), inet::Ipv4::forwardMulticastPacket(), getAddressCategory(), inet::Igmpv2::HostGroupData::HostGroupData(), inet::Igmpv3::HostGroupData::HostGroupData(), inet::Ipv4RoutingTable::internalAddMulticastRoute(), inet::L3Address::isMulticast(), inet::L3Address::isUnicast(), mapToMulticastMacAddress(), inet::Icmp::maySendErrorMessage(), inet::Igmpv2::multicastGroupJoined(), inet::Igmpv2::multicastGroupLeft(), inet::Ipv4::preroutingFinish(), inet::Ipv4NetworkConfigurator::readManualMulticastRouteConfiguration(), inet::Ipv4NetworkConfigurator::readMulticastGroupConfiguration(), inet::Ldp::rebuildFecList(), inet::Ted::rebuildRoutingTable(), inet::Ipv4InterfaceData::removeHostGroupData(), inet::Ipv4InterfaceData::removeRouterGroupData(), inet::Ipv4::resolveNextHopMacAddress(), inet::Igmpv2::RouterGroupData::RouterGroupData(), inet::Igmpv3::RouterGroupData::RouterGroupData(), inet::Igmpv3::sendGroupReport(), inet::Ldp::sendHelloTo(), inet::PimDm::sendJoinPacket(), inet::Igmpv2::sendLeave(), inet::PimDm::sendPrunePacket(), inet::Igmpv2::sendQuery(), inet::Igmpv2::sendReport(), inet::Ipv4InterfaceData::setMulticastListeners(), and inet::PimDm::unroutableMulticastPacketArrived().

◆ isNetwork()

bool inet::Ipv4Address::isNetwork ( const Ipv4Address toCmp) const

Indicates if the address is from the same network.

200 {
201  Ipv4Address netmask = getNetworkMask();
202  if (netmask.isUnspecified())
203  return false; // Class is D or E
204  return maskedAddrAreEqual(*this, toCmp, netmask);
205 }

Referenced by inet::eigrp::EigrpDeviceConfigurator::isEigrpInterface(), and inet::eigrp::EigrpNetworkTable< inet::Ipv4Address >::isInterfaceIncluded().

◆ isUnicast()

bool inet::Ipv4Address::isUnicast ( ) const
inline

Returns true if this address is unicast address.

220 { return !isMulticast() && !isLimitedBroadcastAddress(); }

◆ isUnspecified()

bool inet::Ipv4Address::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.

165 { return addr == 0; }

Referenced by inet::PimSm::addNewRouteSG(), inet::Ipv4NetworkConfigurator::addStaticRoutes(), inet::RsvpTe::allocateResource(), inet::ospfv2::Router::calculateASExternalRoutes(), inet::DhcpClient::chooseInterface(), inet::Ipv4NetworkConfigurator::createInterfaceInfo(), inet::sctp::SctpNatHook::datagramForwardHook(), inet::Ipv4NetworkConfigurator::dumpConfig(), inet::PimSm::findRouteSG(), inet::Ipv4::fragmentAndSend(), inet::Ipv4::fragmentPostRouting(), getAddressCategory(), inet::PingApp::getAllAddresses(), inet::DhcpServer::getAvailableLease(), inet::ospfv2::Router::getDefaultRoute(), inet::L3AddressResolver::getInterfaceIpv4Address(), inet::eigrp::EigrpIpv4Pdm::getNextHopAddr(), inet::ospfv2::Router::getPreferredEntry(), inet::ospfv2::Router::getRoutesToASBoundaryRouter(), inet::Ipv4Route::getSourceTypeAbbreviation(), inet::PimSm::handleStartOperation(), inet::ospfv2::Router::hasRouteToASBoundaryRouter(), inet::PimSm::IamDR(), inet::GlobalArp::initialize(), inet::Ted::initializeTED(), inet::Ipv4RoutingTable::internalAddMulticastRoute(), inet::Ipv4RoutingTable::internalAddRoute(), inet::ospfv2::Router::isDestinationUnreachable(), inet::ospfv2::Router::isDirectRoute(), inet::eigrp::EigrpDeviceConfigurator::isEigrpInterface(), inet::eigrp::EigrpNetworkTable< inet::Ipv4Address >::isInterfaceIncluded(), inet::Ipv4RoutingTable::isLocalBroadcastAddress(), isNetwork(), inet::bgp::BgpRouter::isReachable(), inet::L3Address::isUnspecified(), inet::bgp::BgpConfigReader::loadEbgpSessionConfig(), inet::eigrp::EigrpDeviceConfigurator::loadEigrpIPv4Networks(), inet::Ipv4MulticastRoute::matches(), inet::bgp::operator<<(), inet::ospfv2::operator<<(), inet::operator<<(), inet::RsvpTe::pathProblem(), inet::Icmp::possiblyLocalBroadcast(), inet::Ipv4::preroutingFinish(), inet::Ipv4RoutingTable::printMulticastRoutingTable(), inet::Ipv4RoutingTable::printRoutingTable(), inet::Arp::processArpPacket(), inet::PimSm::processAssertPacket(), inet::RsvpTe::processPathMsg(), inet::Igmpv2::processQuery(), inet::Igmpv3::processQuery(), inet::Ldp::rebuildFecList(), inet::RsvpTe::refreshPath(), inet::Ipv4NetworkConfigurator::resolveInterfaceAndGateway(), inet::Ipv4::routeUnicastPacket(), inet::RsvpTe::scheduleRefreshTimer(), inet::Arp::sendArpGratuitous(), inet::Arp::sendArpProbe(), inet::Arp::sendArpRequest(), inet::PimDm::sendJoinPacket(), inet::Icmp::sendOrProcessIcmpPacket(), inet::PimDm::sendPrunePacket(), inet::Igmpv2::sendQuery(), inet::DhcpMessageSerializer::serialize(), inet::Ipv4Route::str(), inet::Ipv4MulticastRoute::str(), str(), inet::PimDm::unroutableMulticastPacketArrived(), inet::PimSm::unroutableMulticastPacketArrived(), and inet::PimSm::updateDesignatedRouterAddress().

◆ isValidNetmask()

◆ isWellFormed()

bool inet::Ipv4Address::isWellFormed ( const char *  text)
static

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

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

259 {
260  unsigned char dummy[4];
261  return parseIPAddress(text, dummy);
262 }

Referenced by inet::Ipv4NetworkConfigurator::parseAddressAndSpecifiedBits(), inet::RoutingTableParser::parseRouting(), inet::Ipv4NetworkConfigurator::resolveInterfaceAndGateway(), inet::L3Address::tryParse(), and inet::L3AddressResolver::tryParse().

◆ makeBroadcastAddress()

◆ makeNetmask()

◆ mapToMulticastMacAddress()

MacAddress inet::Ipv4Address::mapToMulticastMacAddress ( ) const

Form a MAC address for a multicast IPv4 address, see RFC 1112, section 6.4.

272 {
273  ASSERT(isMulticast());
274 
275  MacAddress macAddr;
276  macAddr.setAddressByte(0, 0x01);
277  macAddr.setAddressByte(1, 0x00);
278  macAddr.setAddressByte(2, 0x5e);
279  macAddr.setAddressByte(3, (addr >> 16) & 0x7F);
280  macAddr.setAddressByte(4, (addr >> 8) & 0xFF);
281  macAddr.setAddressByte(5, (addr & 0xFF));
282  return macAddr;
283 }

Referenced by inet::L3Address::mapToMulticastMacAddress(), and inet::Ipv4::resolveNextHopMacAddress().

◆ maskedAddrAreEqual()

◆ operator!=()

bool inet::Ipv4Address::operator!= ( const Ipv4Address addr1) const
inline

Returns !equals(addr).

300 { return !equals(addr1); }

◆ operator<()

bool inet::Ipv4Address::operator< ( const Ipv4Address addr1) const
inline

Compares two Ipv4 addresses.

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

◆ operator<=()

bool inet::Ipv4Address::operator<= ( const Ipv4Address addr1) const
inline
306 { return getInt() <= addr1.getInt(); }

◆ operator=()

Ipv4Address& inet::Ipv4Address::operator= ( const Ipv4Address obj)
inline

Assignment.

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

◆ operator==()

bool inet::Ipv4Address::operator== ( const Ipv4Address addr1) const
inline

Returns equals(addr).

295 { return equals(addr1); }

◆ operator>()

bool inet::Ipv4Address::operator> ( const Ipv4Address addr1) const
inline
307 { return getInt() > addr1.getInt(); }

◆ operator>=()

bool inet::Ipv4Address::operator>= ( const Ipv4Address addr1) const
inline
308 { return getInt() >= addr1.getInt(); }

◆ parseIPAddress()

bool inet::Ipv4Address::parseIPAddress ( const char *  text,
unsigned char  tobytes[] 
)
staticprotected
49 {
50  if (!text)
51  return false;
52 
53  if (!strcmp(text, "<unspec>")) {
54  tobytes[0] = tobytes[1] = tobytes[2] = tobytes[3] = 0;
55  return true;
56  }
57 
58  const char *s = text;
59  int i = 0;
60  while (true) {
61  if (*s < '0' || *s > '9')
62  return false; // missing number
63 
64  // read and store number
65  int num = 0;
66  while (*s >= '0' && *s <= '9')
67  num = 10 * num + (*s++ - '0');
68  if (num > 255)
69  return false; // number too big
70  tobytes[i++] = (unsigned char)num;
71 
72  if (!*s)
73  break; // end of string
74  if (*s != '.')
75  return false; // invalid char after number
76  if (i == 4)
77  return false; // read 4th number and not yet EOS
78 
79  // skip '.'
80  s++;
81  }
82  return i == 4; // must have all 4 numbers
83 }

Referenced by isWellFormed(), and set().

◆ prefixMatches()

bool inet::Ipv4Address::prefixMatches ( const Ipv4Address to_cmp,
int  numbits 
) const

Compares the first numbits bits of the two addresses.

208 {
209  if (length < 1)
210  return true;
211  if (length > 31)
212  return addr == other.addr;
213 
214  uint32_t mask = _makeNetmask(length);
215  return (addr & mask) == (other.addr & mask);
216 }

◆ set() [1/3]

void inet::Ipv4Address::set ( const char *  t)

Ipv4 address given as text: "192.66.86.1".

86 {
87  unsigned char buf[4];
88  if (!text)
89  throw cRuntimeError("Ipv4 address string is nullptr");
90 
91  bool ok = parseIPAddress(text, buf);
92  if (!ok)
93  throw cRuntimeError("Invalid Ipv4 address string `%s'", text);
94 
95  set(buf[0], buf[1], buf[2], buf[3]);
96 }

◆ set() [2/3]

void inet::Ipv4Address::set ( int  i0,
int  i1,
int  i2,
int  i3 
)

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

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

◆ set() [3/3]

◆ str()

std::string inet::Ipv4Address::str ( bool  printUnspec = true) const

Returns the string representation of the address (e.g.

"152.66.86.92")

Parameters
printUnspecshow 0.0.0.0 as "<unspec>" if true
99 {
100  if (printUnspec && isUnspecified())
101  return std::string("<unspec>");
102 
103  char buf[IPADDRESS_STRING_SIZE];
104  sprintf(buf, "%u.%u.%u.%u", (addr >> 24) & 255, (addr >> 16) & 255, (addr >> 8) & 255, addr & 255);
105  return std::string(buf);
106 }

Referenced by inet::ospfv2::Ospfv2Area::addAddressRange(), inet::Ipv4InterfaceData::addMulticastListener(), inet::bgp::BgpRouter::addToAdvertiseList(), inet::DhcpClient::bindLease(), inet::PacketFilter::DynamicExpressionResolver::callMethod(), inet::Ipv4InterfaceData::changeMulticastGroupMembership(), inet::ospfv2::Neighbor::changeState(), inet::ospfv3::Ospfv3Area::detailedInfo(), inet::ospfv3::Ospfv3Interface::detailedInfo(), inet::Ipv4NetworkConfigurator::dumpConfig(), inet::RsvpTe::evalNextHopInterface(), inet::Ldp::findInterfaceFromPeerAddr(), inet::Ted::getInterfaceAddrByPeerAddress(), inet::Ldp::getPeerSocket(), inet::SctpNatServer::handleMessage(), inet::SctpNatPeer::handleMessage(), inet::ospfv2::Ospfv2Area::info(), inet::Ted::initializeTED(), inet::Ipv4RoutingTable::internalAddMulticastRoute(), inet::Ipv4RoutingTable::internalAddRoute(), inet::ospfv2::Ospfv2ConfigReader::loadAreaFromXML(), inet::bgp::BgpConfigReader::loadConfigFromXML(), inet::bgp::BgpConfigReader::loadEbgpSessionConfig(), inet::ospfv2::Ospfv2ConfigReader::loadHostRoute(), inet::ospfv2::Ospfv2ConfigReader::loadInterfaceParameters(), inet::ospfv2::Ospfv2ConfigReader::loadLoopbackParameters(), inet::ospfv2::Ospfv2ConfigReader::loadVirtualLink(), inet::ospfv3::operator<<(), inet::bgp::operator<<(), inet::ospfv2::operator<<(), inet::operator<<(), inet::Ted::peerRemoteInterface(), inet::ospfv2::Router::printAsExternalLsa(), inet::ospfv2::Ospfv2InterfaceState::printElectionResult(), inet::ospfv2::Ospfv2Area::printLSDB(), inet::Ipv4RoutingTable::printMulticastRoutingTable(), inet::Ipv4RoutingTable::printRoutingTable(), inet::bgp::BgpRouter::printSessionSummary(), inet::ospfv2::Ospfv2Area::printSummaryLsa(), inet::PimSm::processAssertPacket(), inet::bgp::BgpRouter::processMessage(), inet::Ipv4NetworkConfigurator::readManualMulticastRouteConfiguration(), inet::Ipv4NetworkConfigurator::readManualRouteConfiguration(), inet::Ipv4NetworkConfigurator::readMulticastGroupConfiguration(), inet::rtp::Rtp::resolveMTU(), inet::SctpNatServer::sendInfo(), inet::bgp::BgpSession::sendKeepAliveMessage(), inet::bgp::BgpSession::sendOpenMessage(), inet::PimSm::sendPIMJoin(), inet::PimSm::sendPIMPrune(), inet::bgp::BgpSession::sendUpdateMessage(), inet::bgp::BgpRouter::setLocalPreference(), inet::Ipv4InterfaceData::setMulticastListeners(), inet::bgp::BgpRouter::setNextHopSelf(), inet::HostAutoConfigurator::setupNetworkLayer(), inet::Igmpv2::startHostTimer(), inet::L3Address::str(), inet::ospfv2::Ospfv2RoutingTableEntry::str(), inet::ospfv2::Ipv4AddressRange::str(), inet::ospfv2::Ospfv2Area::str(), inet::ospfv3::Ipv4AddressRange::str(), and inet::InterfaceTable::updateLinkDisplayString().

Member Data Documentation

◆ addr

◆ ALL_DVMRP_ROUTERS_MCAST

const Ipv4Address inet::Ipv4Address::ALL_DVMRP_ROUTERS_MCAST
static

224.0.0.4 All DVMRP routers

◆ ALL_HOSTS_MCAST

const Ipv4Address inet::Ipv4Address::ALL_HOSTS_MCAST
static

◆ ALL_IGMPV3_ROUTERS_MCAST

const Ipv4Address inet::Ipv4Address::ALL_IGMPV3_ROUTERS_MCAST
static

◆ ALL_OSPF_DESIGNATED_ROUTERS_MCAST

◆ ALL_OSPF_ROUTERS_MCAST

◆ ALL_RIP_ROUTERS_MCAST

const Ipv4Address inet::Ipv4Address::ALL_RIP_ROUTERS_MCAST
static

◆ ALL_ROUTERS_MCAST

const Ipv4Address inet::Ipv4Address::ALL_ROUTERS_MCAST
static

◆ ALLONES_ADDRESS

◆ LL_MANET_ROUTERS

const Ipv4Address inet::Ipv4Address::LL_MANET_ROUTERS
static

224.0.0.109 Manet all designated routers

Referenced by inet::Ipv4AddressType::getLinkLocalManetRoutersMulticastAddress(), and inet::Dsdv::start().

◆ LOOPBACK_ADDRESS

◆ LOOPBACK_NETMASK

const Ipv4Address inet::Ipv4Address::LOOPBACK_NETMASK
static

◆ UNSPECIFIED_ADDRESS

const Ipv4Address inet::Ipv4Address::UNSPECIFIED_ADDRESS
static

0.0.0.0

Referenced by inet::ospfv3::Ospfv3Interface::acknowledgeLSA(), inet::ospfv3::Ospfv3Interface::addDelayedAcknowledgement(), inet::PimSm::addNewRouteG(), inet::Ipv4NetworkConfigurator::addStaticRoutes(), inet::ospfv2::Ospfv2Area::calculateNextHops(), inet::ospfv3::Ospfv3Area::calculateNextHops(), inet::ospfv3::Ospfv3Area::calculateShortestPathTree(), inet::PimSm::createIpv4Route(), inet::eigrp::EigrpIpv4Pdm::createRouteTlv(), inet::ospfv3::Ospfv3Instance::detailedInfo(), inet::PimSm::findRouteG(), inet::ospfv3::Ospfv3Interface::floodLSA(), inet::NetworkInterface::getIpv4Address(), inet::NetworkInterface::getIpv4Netmask(), inet::GlobalArp::getL3AddressFor(), inet::Arp::getL3AddressFor(), inet::Ipv4AddressType::getLinkLocalAddress(), inet::Ipv4::getNextHop(), inet::Ipv4AddressType::getUnspecifiedAddress(), inet::Ipv4::handlePacketFromHL(), inet::TunnelApp::initialize(), inet::eigrp::EigrpDeviceConfigurator::loadEigrpIPv4Networks(), inet::bgp::BgpRouter::openTCPConnectionToPeer(), inet::ospfv3::Ospfv3Neighbor::Ospfv3Neighbor(), inet::RoutingTableParser::parseRouting(), inet::PimSm::processAssertG(), inet::ospfv3::Ospfv3Interface::processLSR(), inet::ospfv3::Ospfv3Interface::processLSU(), inet::ospfv3::Ospfv3Instance::processPacket(), inet::Ipv4NetworkConfigurator::readManualMulticastRouteConfiguration(), inet::ospfv3::Ospfv3Process::rebuildRoutingTable(), inet::Arp::sendArpProbe(), inet::PimDm::sendAssertPacket(), inet::ospfv3::Ospfv3Interface::sendDelayedAcknowledgements(), inet::PimDm::sendGraftPacket(), inet::PimDm::sendJoinPacket(), inet::PimSm::sendPIMAssert(), inet::PimSm::sendPIMJoin(), inet::PimSm::sendPIMPrune(), inet::PimSm::sendPIMRegister(), inet::PimSm::sendPIMRegisterNull(), inet::PimDm::sendPrunePacket(), inet::PimDm::sendStateRefreshPacket(), and inet::PimDm::unroutableMulticastPacketArrived().


The documentation for this class was generated from the following files:
inet::Ipv4Address::getInt
uint32_t getInt() const
Returns the address as an uint32_t in host byte order (e.g.
Definition: Ipv4Address.h:186
inet::Ipv4Address::_checkNetmaskLength
static void _checkNetmaskLength(int length)
Definition: Ipv4Address.cc:243
inet::Ipv4Address::isMulticast
bool isMulticast() const
Returns true if this address is in the multicast address range, 224.0.0.0 thru 239....
Definition: Ipv4Address.h:215
inet::Ipv4Address::getNetmaskLength
int getNetmaskLength() const
Counts 1 bits in a netmask.
Definition: Ipv4Address.cc:234
inet::Ipv4Address::IETF
@ IETF
Definition: Ipv4Address.h:79
inet::Ipv4Address::UNSPECIFIED
@ UNSPECIFIED
Definition: Ipv4Address.h:74
inet::Ipv4Address::GLOBAL
@ GLOBAL
Definition: Ipv4Address.h:86
inet::Ipv4Address::BENCHMARK
@ BENCHMARK
Definition: Ipv4Address.h:82
inet::Ipv4Address::_makeNetmask
static uint32_t _makeNetmask(int length)
Definition: Ipv4Address.h:46
inet::Ipv4Address::LOOPBACK
@ LOOPBACK
Definition: Ipv4Address.h:76
inet::Ipv4Address::doAnd
Ipv4Address doAnd(const Ipv4Address &ip) const
Returns binary AND of the two addresses.
Definition: Ipv4Address.h:175
inet::Ipv4Address::getDByte
int getDByte(int i) const
Returns the corresponding part of the address specified by the index ("[0].[1].[2]....
Definition: Ipv4Address.h:192
inet::Ipv4Address::equals
bool equals(const Ipv4Address &toCmp) const
Returns true if the two addresses are equal.
Definition: Ipv4Address.h:170
inet::units::values::s
value< double, units::s > s
Definition: Units.h:1235
inet::Ipv4Address::BROADCAST
@ BROADCAST
Definition: Ipv4Address.h:78
inet::Ipv4Address::maskedAddrAreEqual
static bool maskedAddrAreEqual(const Ipv4Address &addr1, const Ipv4Address &addr2, const Ipv4Address &netmask)
Test if the masked addresses (ie the mask is applied to addr1 and addr2) are equal.
Definition: Ipv4Address.cc:249
inet::Ipv4Address::makeNetmask
static Ipv4Address makeNetmask(int length)
Creates and returns a netmask with the given length.
Definition: Ipv4Address.h:329
inet::Ipv4Address::Ipv4Address
Ipv4Address()
name Constructors, destructor
Definition: Ipv4Address.h:112
inet::Ipv4Address::RESERVED
@ RESERVED
Definition: Ipv4Address.h:83
inet::Ipv4Address::set
void set(uint32_t ip)
name Setting the address
Definition: Ipv4Address.h:142
inet::Ipv4Address::getIPClass
char getIPClass() const
Returns the network class of the address: char 'A', 'B', 'C', 'D', 'E', or '?' (returned when the add...
Definition: Ipv4Address.cc:108
inet::Ipv4Address::isLimitedBroadcastAddress
bool isLimitedBroadcastAddress() const
Returns true if this address is the limited broadcast address, i.e.
Definition: Ipv4Address.h:209
inet::Ipv4Address::MULTICAST
@ MULTICAST
Definition: Ipv4Address.h:77
inet::Ipv4Address::LINKLOCAL
@ LINKLOCAL
Definition: Ipv4Address.h:84
inet::Ipv4Address::getNetworkMask
Ipv4Address getNetworkMask() const
Returns an address with the network mask corresponding to the address class.
Definition: Ipv4Address.cc:178
inet::Ipv4Address::parseIPAddress
static bool parseIPAddress(const char *text, unsigned char tobytes[])
Definition: Ipv4Address.cc:48
inet::Ipv4Address::addr
uint32_t addr
Definition: Ipv4Address.h:38
inet::Ipv4Address::TEST_NET
@ TEST_NET
Definition: Ipv4Address.h:80
inet::Ipv4Address::isUnspecified
bool isUnspecified() const
True if all four address bytes are zero.
Definition: Ipv4Address.h:165
inet::Ipv4Address::PRIVATE_NETWORK
@ PRIVATE_NETWORK
Definition: Ipv4Address.h:85
inet::Ipv4Address::IPv6_TO_IPv4_RELAY
@ IPv6_TO_IPv4_RELAY
Definition: Ipv4Address.h:81
inet::Ipv4Address::THIS_NETWORK
@ THIS_NETWORK
Definition: Ipv4Address.h:75