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

Utility class for finding Ipv4 or Ipv6 address of a host or router. More...

#include <L3AddressResolver.h>

Inheritance diagram for inet::L3AddressResolver:
inet::L3NetworkConfiguratorBase inet::Ipv4NetworkConfigurator inet::NextHopNetworkConfigurator

Public Types

enum  {
  ADDR_IPv4 = 1, ADDR_IPv6 = 2, ADDR_MAC = 4, ADDR_MODULEPATH = 8,
  ADDR_MODULEID = 16, ADDR_MASK = 32
}
 

Public Member Functions

 L3AddressResolver ()
 
virtual ~L3AddressResolver ()
 
virtual L3Address resolve (const char *str, int addrType=DEFAULT_ADDR_TYPE)
 Accepts dotted decimal notation ("127.0.0.1"), module name of the host or router ("host[2]"), and empty string (""). More...
 
virtual std::vector< L3Addressresolve (std::vector< std::string > strs, int addrType=DEFAULT_ADDR_TYPE)
 Utility function: Calls resolve() for each item in the string vector, and returns the result in an address vector. More...
 
virtual bool tryResolve (const char *str, L3Address &result, int addrType=DEFAULT_ADDR_TYPE)
 Similar to resolve(), but returns false (instead of throwing an error) if the address cannot be resolved because the given host (or interface) doesn't have an address assigned yet. More...
 
Utility functions supporting resolve()
bool tryParse (L3Address &result, const char *addr, int addrType=DEFAULT_ADDR_TYPE)
 
virtual L3Address addressOf (cModule *host, int addrType=DEFAULT_ADDR_TYPE)
 Returns Ipv4 or Ipv6 address of the given host or router. More...
 
virtual L3Address addressOf (cModule *host, const char *ifname, int addrType=DEFAULT_ADDR_TYPE)
 Similar to addressOf(), but only looks at the given interface. More...
 
virtual L3Address addressOf (cModule *host, cModule *destmod, int addrType=DEFAULT_ADDR_TYPE)
 Returns Ipv4 or Ipv6 address of the given host or router. More...
 
virtual L3Address routerIdOf (cModule *host)
 Returns the router Id of the given router. More...
 
virtual L3Address getAddressFrom (IInterfaceTable *ift, int addrType=DEFAULT_ADDR_TYPE)
 Returns the Ipv4 or Ipv6 address of the given host or router, given its IInterfaceTable module. More...
 
virtual L3Address getAddressFrom (NetworkInterface *ie, int addrType=DEFAULT_ADDR_TYPE)
 Returns the Ipv4 or Ipv6 address of the given interface (of a host or router). More...
 
virtual IInterfaceTableinterfaceTableOf (cModule *host)
 The function tries to look up the IInterfaceTable module as submodule "interfaceTable" or "networkLayer.interfaceTable" within the host/router module. More...
 
virtual IIpv4RoutingTablegetIpv4RoutingTableOf (cModule *host)
 The function tries to look up the IIpv4RoutingTable module as submodule "routingTable" or "networkLayer.routingTable" within the host/router module. More...
 
virtual Ipv6RoutingTablegetIpv6RoutingTableOf (cModule *host)
 The function tries to look up the Ipv6RoutingTable module as submodule "routingTable6" or "networkLayer.routingTable6" within the host/router module. More...
 
virtual IInterfaceTablefindInterfaceTableOf (cModule *host)
 Like interfaceTableOf(), but doesn't throw error if not found. More...
 
virtual IIpv4RoutingTablefindIpv4RoutingTableOf (cModule *host)
 Like routingTableOf(), but doesn't throw error if not found. More...
 
virtual Ipv6RoutingTablefindIpv6RoutingTableOf (cModule *host)
 Like interfaceTableOf(), but doesn't throw error if not found. More...
 
virtual NextHopRoutingTablefindNextHopRoutingTableOf (cModule *host)
 Like interfaceTableOf(), but doesn't throw error if not found. More...
 
virtual std::vector< cModule * > collectNetworkNodes ()
 Collect modules that represent network nodes, as denoted by the @networkNode(true) annotation. More...
 
virtual cModule * findHostWithAddress (const L3Address &addr)
 Find the Host with the specified address. More...
 
virtual NetworkInterfacefindInterfaceWithMacAddress (const MacAddress &addr)
 Find the interface with the specified MAC address. More...
 
virtual cModule * findHostWithMacAddress (const MacAddress &addr)
 Find the host with the specified MAC address. More...
 

Protected Member Functions

virtual bool getIpv4AddressFrom (L3Address &retAddr, IInterfaceTable *ift, bool netmask)
 
virtual bool getIpv6AddressFrom (L3Address &retAddr, IInterfaceTable *ift, bool netmask)
 
virtual bool getMacAddressFrom (L3Address &retAddr, IInterfaceTable *ift, bool netmask)
 
virtual bool getModulePathAddressFrom (L3Address &retAddr, IInterfaceTable *ift, bool netmask)
 
virtual bool getModuleIdAddressFrom (L3Address &retAddr, IInterfaceTable *ift, bool netmask)
 
virtual bool getInterfaceIpv4Address (L3Address &ret, NetworkInterface *ie, bool mask)
 
virtual bool getInterfaceIpv6Address (L3Address &ret, NetworkInterface *ie, bool mask)
 
virtual bool getInterfaceMacAddress (L3Address &ret, NetworkInterface *ie, bool mask)
 
virtual bool getInterfaceModulePathAddress (L3Address &ret, NetworkInterface *ie, bool mask)
 
virtual bool getInterfaceModuleIdAddress (L3Address &ret, NetworkInterface *ie, bool mask)
 
virtual void doCollectNetworkNodes (cModule *parent, std::vector< cModule * > &result)
 

Detailed Description

Utility class for finding Ipv4 or Ipv6 address of a host or router.

Syntax variations understood:

  • literal Ipv4 address: "186.54.66.2"
  • literal Ipv6 address: "3011:7cd6:750b:5fd6:aba3:c231:e9f9:6a43"
  • module name: "server", "subnet.server[3]"
  • interface of a host or router: "server%eth0", "subnet.server[3]%eth0"
  • Ipv4 or Ipv6 address of a host or router: "server(ipv4)", "subnet.server[3](ipv6)"
  • Ipv4 or Ipv6 address of an interface of a host or router: "server%eth0(ipv4)", "subnet.server[3]%eth0(ipv6)"
  • routerId: "router1%routerId", "R1%routerId"
  • interface of a host or router toward defined another node: "client1>router"

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
ADDR_IPv4 
ADDR_IPv6 
ADDR_MAC 
ADDR_MODULEPATH 
ADDR_MODULEID 
ADDR_MASK 
69  {
70  ADDR_IPv4 = 1,
71  ADDR_IPv6 = 2,
72  ADDR_MAC = 4,
73  ADDR_MODULEPATH = 8,
74  ADDR_MODULEID = 16,
75  ADDR_MASK = 32
76  };

Constructor & Destructor Documentation

◆ L3AddressResolver()

◆ ~L3AddressResolver()

virtual inet::L3AddressResolver::~L3AddressResolver ( )
inlinevirtual
80 {}

Member Function Documentation

◆ addressOf() [1/3]

L3Address inet::L3AddressResolver::addressOf ( cModule *  host,
cModule *  destmod,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Returns Ipv4 or Ipv6 address of the given host or router.

This function find an interface of host connected to destmod then invokes getAddressFrom() to extract the IP address.

216 {
217  IInterfaceTable *ift = interfaceTableOf(host);
218  for (int i = 0; i < ift->getNumInterfaces(); i++) {
219  NetworkInterface *ie = ift->getInterface(i);
220  if (ie) {
221  int gateId = ie->getNodeOutputGateId();
222  if (gateId != -1)
223  if (host->gate(gateId)->pathContains(destmod))
224  return getAddressFrom(ie, addrType);
225 
226  }
227  }
228  throw cRuntimeError("L3AddressResolver: no interface connected to `%s' module in interface table of `%s'", destmod->getFullPath().c_str(), host->getFullPath().c_str());
229 }

◆ addressOf() [2/3]

L3Address inet::L3AddressResolver::addressOf ( cModule *  host,
const char *  ifname,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Similar to addressOf(), but only looks at the given interface.

206 {
207  IInterfaceTable *ift = interfaceTableOf(host);
208  NetworkInterface *ie = ift->findInterfaceByName(ifname);
209  if (ie)
210  return getAddressFrom(ie, addrType);
211 
212  throw cRuntimeError("L3AddressResolver: no interface called `%s' in interface table of `%s'", ifname, host->getFullPath().c_str());
213 }

◆ addressOf() [3/3]

L3Address inet::L3AddressResolver::addressOf ( cModule *  host,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Returns Ipv4 or Ipv6 address of the given host or router.

This function uses routingTableOf() to find the IIpv4RoutingTable module, then invokes getAddressFrom() to extract the IP address.

200 {
201  IInterfaceTable *ift = interfaceTableOf(host);
202  return getAddressFrom(ift, addrType);
203 }

Referenced by tryResolve().

◆ collectNetworkNodes()

std::vector< cModule * > inet::L3AddressResolver::collectNetworkNodes ( )
virtual

Collect modules that represent network nodes, as denoted by the @networkNode(true) annotation.

519 {
520  std::vector<cModule *> result;
521  doCollectNetworkNodes(getSimulation()->getSystemModule(), result);
522  return result;
523 }

Referenced by findHostWithAddress(), and findInterfaceWithMacAddress().

◆ doCollectNetworkNodes()

void inet::L3AddressResolver::doCollectNetworkNodes ( cModule *  parent,
std::vector< cModule * > &  result 
)
protectedvirtual
526 {
527  for (cModule::SubmoduleIterator it(parent); !it.end(); ++it) {
528  cModule *submodule = *it;
529  if (submodule->getProperties()->getAsBool("networkNode"))
530  result.push_back(submodule);
531  else
532  doCollectNetworkNodes(submodule, result);
533  }
534 }

Referenced by collectNetworkNodes().

◆ findHostWithAddress()

cModule * inet::L3AddressResolver::findHostWithAddress ( const L3Address addr)
virtual

Find the Host with the specified address.

537 {
538  if (add.isUnspecified() || add.isMulticast())
539  return nullptr;
540 
541  auto networkNodes = collectNetworkNodes();
542  for (cModule *mod : networkNodes) {
543  IInterfaceTable *itable = L3AddressResolver().findInterfaceTableOf(mod);
544  if (itable != nullptr) {
545  if(itable->isLocalAddress(add))
546  return mod;
547  }
548  }
549  return nullptr;
550 }

Referenced by inet::visualizer::RoutingTableVisualizerBase::addRouteVisualizations(), inet::ieee80211::Ieee80211MgmtStaSimplified::initialize(), and inet::visualizer::TransportConnectionVisualizerBase::receiveSignal().

◆ findHostWithMacAddress()

cModule * inet::L3AddressResolver::findHostWithMacAddress ( const MacAddress addr)
virtual

Find the host with the specified MAC address.

Returns nullptr if not found.

572 {
573  NetworkInterface *entry = findInterfaceWithMacAddress(addr);
574  return entry ? entry->getInterfaceTable()->getHostModule() : nullptr;
575 }

◆ findInterfaceTableOf()

◆ findInterfaceWithMacAddress()

NetworkInterface * inet::L3AddressResolver::findInterfaceWithMacAddress ( const MacAddress addr)
virtual

Find the interface with the specified MAC address.

Returns nullptr if not found.

553 {
554  if (addr.isUnspecified() || addr.isBroadcast() || addr.isMulticast())
555  return nullptr;
556 
557  auto networkNodes = collectNetworkNodes();
558  for (cModule *mod : networkNodes) {
559  IInterfaceTable *itable = L3AddressResolver().findInterfaceTableOf(mod);
560  if (itable != nullptr) {
561  for (int i = 0; i < itable->getNumInterfaces(); i++) {
562  NetworkInterface *entry = itable->getInterface(i);
563  if (entry->getMacAddress() == addr)
564  return entry;
565  }
566  }
567  }
568  return nullptr;
569 }

Referenced by findHostWithMacAddress().

◆ findIpv4RoutingTableOf()

IIpv4RoutingTable * inet::L3AddressResolver::findIpv4RoutingTableOf ( cModule *  host)
virtual

Like routingTableOf(), but doesn't throw error if not found.

492 {
493 #ifdef INET_WITH_IPv4
494  return dynamic_cast<IIpv4RoutingTable *>(host->findModuleByPath(".ipv4.routingTable"));
495 #else // ifdef INET_WITH_IPv4
496  return nullptr;
497 #endif // ifdef INET_WITH_IPv4
498 }

Referenced by inet::Ipv4FlatNetworkConfigurator::extractTopology(), inet::NetworkConfiguratorBase::extractTopology(), getIpv4RoutingTableOf(), inet::Ted::initializeTED(), and inet::visualizer::RoutingTableVisualizerBase::updateAllRouteVisualizations().

◆ findIpv6RoutingTableOf()

Ipv6RoutingTable * inet::L3AddressResolver::findIpv6RoutingTableOf ( cModule *  host)
virtual

Like interfaceTableOf(), but doesn't throw error if not found.

501 {
502 #ifdef INET_WITH_IPv6
503  return dynamic_cast<Ipv6RoutingTable *>(host->findModuleByPath(".ipv6.routingTable"));
504 #else // ifdef INET_WITH_IPv6
505  return nullptr;
506 #endif // ifdef INET_WITH_IPv6
507 }

Referenced by inet::Ipv6FlatNetworkConfigurator::addOwnAdvPrefixRoutes(), inet::Ipv6FlatNetworkConfigurator::addStaticRoutes(), inet::Ipv6FlatNetworkConfigurator::configureAdvPrefixes(), getIpv6RoutingTableOf(), and inet::Ipv6FlatNetworkConfigurator::isIPNode().

◆ findNextHopRoutingTableOf()

NextHopRoutingTable * inet::L3AddressResolver::findNextHopRoutingTableOf ( cModule *  host)
virtual

Like interfaceTableOf(), but doesn't throw error if not found.

510 {
511 #ifdef INET_WITH_NEXTHOP
512  return dynamic_cast<NextHopRoutingTable *>(host->findModuleByPath(".generic.routingTable"));
513 #else // ifdef INET_WITH_NEXTHOP
514  return nullptr;
515 #endif // ifdef INET_WITH_NEXTHOP
516 }

◆ getAddressFrom() [1/2]

L3Address inet::L3AddressResolver::getAddressFrom ( IInterfaceTable ift,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Returns the Ipv4 or Ipv6 address of the given host or router, given its IInterfaceTable module.

For Ipv4, the first usable interface address is chosen.

232 {
233  L3Address ret;
234  bool netmask = addrType & ADDR_MASK;
235  if ((addrType & ADDR_IPv4) && getIpv4AddressFrom(ret, ift, netmask))
236  return ret;
237  else if ((addrType & ADDR_IPv6) && getIpv6AddressFrom(ret, ift, netmask))
238  return ret;
239  else if ((addrType & ADDR_MAC) && getMacAddressFrom(ret, ift, netmask))
240  return ret;
241  else if ((addrType & ADDR_MODULEPATH) && getModulePathAddressFrom(ret, ift, netmask))
242  return ret;
243  else if ((addrType & ADDR_MODULEID) && getModuleIdAddressFrom(ret, ift, netmask))
244  return ret;
245  return ret;
246 }

Referenced by addressOf().

◆ getAddressFrom() [2/2]

L3Address inet::L3AddressResolver::getAddressFrom ( NetworkInterface ie,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Returns the Ipv4 or Ipv6 address of the given interface (of a host or router).

249 {
250  L3Address ret;
251  bool mask = addrType & ADDR_MASK;
252  if ((addrType & ADDR_IPv4) && getInterfaceIpv4Address(ret, ie, mask))
253  return ret;
254  else if ((addrType & ADDR_IPv6) && getInterfaceIpv6Address(ret, ie, mask))
255  return ret;
256  else if ((addrType & ADDR_MAC) && getInterfaceMacAddress(ret, ie, mask))
257  return ret;
258  else if ((addrType & ADDR_MODULEPATH) && getInterfaceModulePathAddress(ret, ie, mask))
259  return ret;
260  else if ((addrType & ADDR_MODULEID) && getInterfaceModuleIdAddress(ret, ie, mask))
261  return ret;
262  return ret;
263 }

◆ getInterfaceIpv4Address()

bool inet::L3AddressResolver::getInterfaceIpv4Address ( L3Address ret,
NetworkInterface ie,
bool  mask 
)
protectedvirtual

Reimplemented in inet::Ipv4NetworkConfigurator.

385 {
386 #ifdef INET_WITH_IPv4
387  Ipv4Address addr = ie->getIpv4Address();
388  if (!addr.isUnspecified()) {
389  ret.set(netmask ? ie->getIpv4Netmask() : addr);
390  return true;
391  }
392  else {
393  // find address in the configurator's notebook
394  // TODO how do we know where is the configurator? get the path from a NED parameter?
395  Ipv4NetworkConfigurator *configurator = dynamic_cast<Ipv4NetworkConfigurator *>(getSimulation()->findModuleByPath("configurator"));
396  if (configurator)
397  return static_cast<L3AddressResolver *>(configurator)->getInterfaceIpv4Address(ret, ie, netmask);
398  }
399 #endif // ifdef INET_WITH_IPv4
400  return false;
401 }

Referenced by getAddressFrom(), getInterfaceModuleIdAddress(), getInterfaceModulePathAddress(), and getIpv4AddressFrom().

◆ getInterfaceIpv6Address()

bool inet::L3AddressResolver::getInterfaceIpv6Address ( L3Address ret,
NetworkInterface ie,
bool  mask 
)
protectedvirtual
369 {
370 #ifdef INET_WITH_IPv6
371  if (netmask)
372  return false; // Ipv6 netmask not supported yet
373  if (auto ipv6Data = ie->findProtocolData<Ipv6InterfaceData>()) {
374  Ipv6Address addr = ipv6Data->getPreferredAddress();
375  if (!addr.isUnspecified()) {
376  ret.set(addr);
377  return true;
378  }
379  }
380 #endif // ifdef INET_WITH_IPv6
381  return false;
382 }

Referenced by getAddressFrom(), and getIpv6AddressFrom().

◆ getInterfaceMacAddress()

bool inet::L3AddressResolver::getInterfaceMacAddress ( L3Address ret,
NetworkInterface ie,
bool  mask 
)
protectedvirtual
404 {
405  if (!ie->getMacAddress().isUnspecified()) {
406  ret = ie->getMacAddress();
407  return true;
408  }
409  return false;
410 }

Referenced by getAddressFrom(), and getMacAddressFrom().

◆ getInterfaceModuleIdAddress()

bool inet::L3AddressResolver::getInterfaceModuleIdAddress ( L3Address ret,
NetworkInterface ie,
bool  mask 
)
protectedvirtual
435 {
436 #ifdef INET_WITH_NEXTHOP
437  if (auto nextHopData = ie->findProtocolData<NextHopInterfaceData>()) {
438  L3Address addr = nextHopData->getAddress();
439  if (!addr.isUnspecified() && addr.getType() == L3Address::MODULEID) {
440  ret = addr;
441  return true;
442  }
443  }
444  else {
445  // find address in the configurator's notebook
446  // TODO how do we know where is the configurator? getNextHopNetworkConfigurator the path from a NED parameter?
447  NextHopNetworkConfigurator *configurator = dynamic_cast<NextHopNetworkConfigurator *>(getSimulation()->findModuleByPath("configurator"));
448  if (configurator)
449  return static_cast<L3AddressResolver *>(configurator)->getInterfaceIpv4Address(ret, ie, netmask);
450  }
451 #endif // ifdef INET_WITH_NEXTHOP
452  ret = ie->getModuleIdAddress();
453  return true;
454 }

Referenced by getAddressFrom(), and getModuleIdAddressFrom().

◆ getInterfaceModulePathAddress()

bool inet::L3AddressResolver::getInterfaceModulePathAddress ( L3Address ret,
NetworkInterface ie,
bool  mask 
)
protectedvirtual
413 {
414 #ifdef INET_WITH_NEXTHOP
415  if (auto nextHopData = ie->findProtocolData<NextHopInterfaceData>()) {
416  L3Address addr = nextHopData->getAddress();
417  if (!addr.isUnspecified() && addr.getType() == L3Address::MODULEPATH) {
418  ret = addr;
419  return true;
420  }
421  }
422  else {
423  // find address in the configurator's notebook
424  // TODO how do we know where is the configurator? getNextHopNetworkConfigurator the path from a NED parameter?
425  NextHopNetworkConfigurator *configurator = dynamic_cast<NextHopNetworkConfigurator *>(getSimulation()->findModuleByPath("configurator"));
426  if (configurator)
427  return static_cast<L3AddressResolver *>(configurator)->getInterfaceIpv4Address(ret, ie, netmask);
428  }
429 #endif // ifdef INET_WITH_NEXTHOP
430  ret = ie->getModulePathAddress();
431  return true;
432 }

Referenced by getAddressFrom(), and getModulePathAddressFrom().

◆ getIpv4AddressFrom()

bool inet::L3AddressResolver::getIpv4AddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual
266 {
267  if (ift->getNumInterfaces() == 0)
268  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
269  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
270 
271 #ifdef INET_WITH_IPv4
272  // choose first usable interface address (configured for Ipv4, non-loopback if, addr non-null)
273  for (int i = 0; i < ift->getNumInterfaces(); i++) {
274  NetworkInterface *ie = ift->getInterface(i);
275  if (ie->isLoopback())
276  continue;
277  if (getInterfaceIpv4Address(retAddr, ie, netmask))
278  return true;
279  }
280 #endif // ifdef INET_WITH_IPv4
281  return false;
282 }

Referenced by getAddressFrom().

◆ getIpv4RoutingTableOf()

IIpv4RoutingTable * inet::L3AddressResolver::getIpv4RoutingTableOf ( cModule *  host)
virtual

The function tries to look up the IIpv4RoutingTable module as submodule "routingTable" or "networkLayer.routingTable" within the host/router module.

Throws an error if not found.

468 {
469  IIpv4RoutingTable *mod = findIpv4RoutingTableOf(host);
470  if (!mod)
471  throw cRuntimeError("L3AddressResolver: IIpv4RoutingTable not found as submodule "
472  " `routingTable' in host/router `%s'", host->getFullPath().c_str());
473  return mod;
474 }

Referenced by inet::Ipv4FlatNetworkConfigurator::extractTopology(), routerIdOf(), and inet::HostAutoConfigurator::setupNetworkLayer().

◆ getIpv6AddressFrom()

bool inet::L3AddressResolver::getIpv6AddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual
285 {
286  // browse interfaces and pick a globally routable address
287  if (ift->getNumInterfaces() == 0)
288  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
289  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
290 
291 #ifndef INET_WITH_IPv6
292  return false;
293 #else // ifndef INET_WITH_IPv6
294  if (netmask)
295  return false; // Ipv6 netmask not supported yet
296 
297  bool ret = false;
299 
300  for (int i = 0; i < ift->getNumInterfaces() && retScope != Ipv6Address::GLOBAL; i++) {
301  NetworkInterface *ie = ift->getInterface(i);
302  L3Address curAddr;
303  bool ieHasIpv6Addr = getInterfaceIpv6Address(curAddr, ie, false);
304  if (!ieHasIpv6Addr || ie->isLoopback())
305  continue;
306  Ipv6Address::Scope curScope = curAddr.toIpv6().getScope();
307  if (curScope > retScope) {
308  retAddr = curAddr;
309  retScope = curScope;
310  ret = true;
311  }
312  }
313  return ret;
314 #endif // ifndef INET_WITH_IPv6
315 }

Referenced by getAddressFrom().

◆ getIpv6RoutingTableOf()

Ipv6RoutingTable * inet::L3AddressResolver::getIpv6RoutingTableOf ( cModule *  host)
virtual

The function tries to look up the Ipv6RoutingTable module as submodule "routingTable6" or "networkLayer.routingTable6" within the host/router module.

Throws an error if not found.

477 {
478  // find Ipv6RoutingTable
479  Ipv6RoutingTable *mod = findIpv6RoutingTableOf(host);
480  if (!mod)
481  throw cRuntimeError("L3AddressResolver: Ipv6RoutingTable not found as submodule "
482  " `routingTable' in host/router `%s'", host->getFullPath().c_str());
483  return mod;
484 }

Referenced by inet::xMIPv6::initialize().

◆ getMacAddressFrom()

bool inet::L3AddressResolver::getMacAddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual
318 {
319  if (ift->getNumInterfaces() == 0)
320  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
321  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
322 
323  // choose first usable interface address (configured for generic, non-loopback if, addr non-null)
324  for (int i = 0; i < ift->getNumInterfaces(); i++) {
325  NetworkInterface *ie = ift->getInterface(i);
326  if (ie->isLoopback())
327  continue;
328  if (getInterfaceMacAddress(retAddr, ie, netmask))
329  return true;
330  }
331  return false;
332 }

Referenced by getAddressFrom().

◆ getModuleIdAddressFrom()

bool inet::L3AddressResolver::getModuleIdAddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual
352 {
353  if (ift->getNumInterfaces() == 0)
354  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
355  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
356 
357  // choose first usable interface address (configured for generic, non-loopback if, addr non-null)
358  for (int i = 0; i < ift->getNumInterfaces(); i++) {
359  NetworkInterface *ie = ift->getInterface(i);
360  if (ie->isLoopback())
361  continue;
362  if (getInterfaceModuleIdAddress(retAddr, ie, netmask))
363  return true;
364  }
365  return false;
366 }

Referenced by getAddressFrom().

◆ getModulePathAddressFrom()

bool inet::L3AddressResolver::getModulePathAddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual
335 {
336  if (ift->getNumInterfaces() == 0)
337  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
338  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
339 
340  // choose first usable interface address (configured for generic, non-loopback if, addr non-null)
341  for (int i = 0; i < ift->getNumInterfaces(); i++) {
342  NetworkInterface *ie = ift->getInterface(i);
343  if (ie->isLoopback())
344  continue;
345  if (getInterfaceModulePathAddress(retAddr, ie, netmask))
346  return true;
347  }
348  return false;
349 }

Referenced by getAddressFrom().

◆ interfaceTableOf()

IInterfaceTable * inet::L3AddressResolver::interfaceTableOf ( cModule *  host)
virtual

The function tries to look up the IInterfaceTable module as submodule "interfaceTable" or "networkLayer.interfaceTable" within the host/router module.

Throws an error if not found.

457 {
458  // find IInterfaceTable
459  cModule *mod = host->getSubmodule("interfaceTable");
460  if (!mod)
461  throw cRuntimeError("L3AddressResolver: IInterfaceTable not found as submodule "
462  " `interfaceTable' in host/router `%s'", host->getFullPath().c_str());
463 
464  return check_and_cast<IInterfaceTable *>(mod);
465 }

Referenced by inet::Ipv6FlatNetworkConfigurator::addOwnAdvPrefixRoutes(), addressOf(), inet::Ipv6FlatNetworkConfigurator::addStaticRoutes(), inet::Ipv6FlatNetworkConfigurator::configureAdvPrefixes(), inet::Ipv4FlatNetworkConfigurator::extractTopology(), and inet::Ted::initializeTED().

◆ resolve() [1/2]

L3Address inet::L3AddressResolver::resolve ( const char *  str,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Accepts dotted decimal notation ("127.0.0.1"), module name of the host or router ("host[2]"), and empty string ("").

For the latter, it returns the null address. If module name is specified, the module will be looked up using getModuleByPath(), and then addressOf() will be called to determine its IP address.

34 {
35  L3Address addr;
36  if (!tryResolve(s, addr, addrType))
37  throw cRuntimeError("L3AddressResolver: Cannot resolve address `%s'", s);
38  return addr;
39 }

Referenced by inet::MultiFieldClassifier::configureFilters(), inet::NetPerfMeter::createAndBindSocket(), inet::Define_Module(), inet::NetPerfMeter::establishConnection(), inet::xmlutils::getParameterIPAddressValue(), inet::PingApp::handleSelfMessage(), inet::DhcpServer::handleStartOperation(), inet::Forwarding::initialize(), inet::ieee80211::Ieee80211MgmtStaSimplified::initialize(), inet::VoipStreamSender::initialize(), inet::SctpNatServer::initialize(), inet::SctpClient::initialize(), inet::SctpServer::initialize(), inet::SctpPeer::initialize(), inet::dymo::Dymo::initialize(), inet::SctpNatPeer::initialize(), inet::ospfv2::ipv4AddressFromAddressString(), inet::ospfv2::ipv4NetmaskFromAddressString(), inet::PingApp::parseDestAddressesPar(), inet::UdpBasicBurst::processStart(), inet::Ipv4NetworkConfigurator::readManualMulticastRouteConfiguration(), inet::Ipv4NetworkConfigurator::readManualRouteConfiguration(), inet::RsvpTe::readTrafficRouteFromXML(), inet::UdpVideoStreamClient::requestStream(), resolve(), inet::EtherAppClient::resolveDestMacAddress(), inet::EtherTrafGen::resolveDestMacAddress(), inet::Ipv4NetworkConfigurator::resolveInterfaceAndGateway(), inet::tcp::TcpSpoof::sendSpoofPacket(), inet::SimpleVoipSender::sendVoIPPacket(), inet::UdpSink::setSocketOptions(), inet::SctpNatPeer::socketClosed(), inet::TunnelApp::socketDataArrived(), and inet::SctpNatPeer::socketPeerClosed().

◆ resolve() [2/2]

std::vector< L3Address > inet::L3AddressResolver::resolve ( std::vector< std::string >  strs,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Utility function: Calls resolve() for each item in the string vector, and returns the result in an address vector.

The string vector may come e.g. from cStringTokenizer::asVector().

42 {
43  std::vector<L3Address> result;
44  int n = strs.size();
45  result.reserve(n);
46  for (int i = 0; i < n; i++)
47  result.push_back(resolve(strs[i].c_str(), addrType));
48  return result;
49 }

◆ routerIdOf()

L3Address inet::L3AddressResolver::routerIdOf ( cModule *  host)
virtual

Returns the router Id of the given router.

Router Id is obtained from the getRouterId() method of the IIpv4RoutingTable submodule.

190 {
191 #ifdef INET_WITH_IPv4
192  IIpv4RoutingTable *rt = getIpv4RoutingTableOf(host);
193  return L3Address(rt->getRouterId());
194 #else // ifdef INET_WITH_IPv4
195  throw cRuntimeError("INET was compiled without Ipv4 support");
196 #endif // ifdef INET_WITH_IPv4
197 }

Referenced by tryResolve().

◆ tryParse()

bool inet::L3AddressResolver::tryParse ( L3Address result,
const char *  addr,
int  addrType = DEFAULT_ADDR_TYPE 
)
52 {
53  Ipv6Address ipv6;
54  MacAddress mac;
55  ModuleIdAddress moduleId;
56  ModulePathAddress modulePath;
57  if (((addrType & ADDR_IPv4) != 0) && Ipv4Address::isWellFormed(addr))
58  result.set(Ipv4Address(addr));
59  else if (((addrType & ADDR_IPv6) != 0) && ipv6.tryParse(addr))
60  result.set(ipv6);
61  else if (((addrType & ADDR_MAC) != 0) && mac.tryParse(addr))
62  result.set(mac);
63  else if (((addrType & ADDR_MODULEID) != 0) && moduleId.tryParse(addr))
64  result.set(moduleId);
65  else if (((addrType & ADDR_MODULEPATH) != 0) && modulePath.tryParse(addr))
66  result.set(modulePath);
67  else
68  return false;
69  return true;
70 }

Referenced by tryResolve().

◆ tryResolve()

bool inet::L3AddressResolver::tryResolve ( const char *  str,
L3Address result,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Similar to resolve(), but returns false (instead of throwing an error) if the address cannot be resolved because the given host (or interface) doesn't have an address assigned yet.

(It still throws an error on any other error condition).

73 {
74  // empty address
75  result = L3Address();
76  if (!s || !*s)
77  return true;
78 
79  // handle address literal
80  if (tryParse(result, s, addrType))
81  return true;
82 
83  // must be " modulename [ { '%' interfacename | '>' destnode } ] [ '(' protocol ')' ] [ '/' ] " syntax
84  // interfacename: existing_interface_of_module | 'routerId'
85  // protocol: 'ipv4' | 'ipv6' | 'mac' | 'modulepath' | 'moduleid'
86  // '/': returns mask instead address
87  std::string modname, ifname, protocol, destnodename;
88  bool netmask = addrType & ADDR_MASK;
89  const char *p = s;
90  const char *endp = strchr(p, '\0');
91  const char *nextsep = strpbrk(p, "%>(/");
92  if (!nextsep)
93  nextsep = endp;
94  modname.assign(p, nextsep - p);
95 
96  char c = *nextsep;
97 
98  if (c == '%') {
99  {
100  p = nextsep + 1;
101  nextsep = strpbrk(p, "(/");
102  if (!nextsep)
103  nextsep = endp;
104  }
105  ifname.assign(p, nextsep - p);
106  c = *nextsep;
107  }
108  else if (c == '>') {
109  {
110  p = nextsep + 1;
111  nextsep = strpbrk(p, "(/");
112  if (!nextsep)
113  nextsep = endp;
114  }
115  destnodename.assign(p, nextsep - p);
116  c = *nextsep;
117  }
118 
119  if (c == '(') {
120  {
121  p = nextsep + 1;
122  nextsep = strpbrk(p, ")");
123  if (!nextsep)
124  nextsep = endp;
125  }
126  protocol.assign(p, nextsep - p);
127  c = *nextsep;
128  if (c == ')') {
129  {
130  p = nextsep + 1;
131  nextsep = p;
132  }
133  c = *nextsep;
134  }
135  }
136 
137  if (c == '/') {
138  netmask = true;
139  {
140  p = nextsep + 1;
141  nextsep = p;
142  }
143  c = *nextsep;
144  }
145 
146  if (c)
147  throw cRuntimeError("L3AddressResolver: syntax error parsing address spec `%s'", s);
148 
149  // find module
150  cModule *mod = getSimulation()->findModuleByPath(modname.c_str());
151  if (!mod)
152  throw cRuntimeError("L3AddressResolver: module `%s' not found", modname.c_str());
153 
154  // check protocol
155  if (!protocol.empty()) {
156  if (protocol == "ipv4")
157  addrType = ADDR_IPv4;
158  else if (protocol == "ipv6")
159  addrType = ADDR_IPv6;
160  else if (protocol == "mac")
161  addrType = ADDR_MAC;
162  else if (protocol == "modulepath")
163  addrType = ADDR_MODULEPATH;
164  else if (protocol == "moduleid")
165  addrType = ADDR_MODULEID;
166  else
167  throw cRuntimeError("L3AddressResolver: error parsing address spec `%s': address type must be `(ipv4)' or `(ipv6)'", s);
168  }
169  if (netmask)
170  addrType |= ADDR_MASK;
171 
172  // find interface for dest node
173  // get address from the given module/interface
174  if (!destnodename.empty()) {
175  cModule *destnode = getSimulation()->findModuleByPath(destnodename.c_str());
176  if (!destnode)
177  throw cRuntimeError("L3AddressResolver: destination module `%s' not found", destnodename.c_str());
178  result = addressOf(mod, destnode, addrType);
179  }
180  else if (ifname.empty())
181  result = addressOf(mod, addrType);
182  else if (ifname == "routerId")
183  result = routerIdOf(mod); // addrType is meaningless here, routerId is protocol independent
184  else
185  result = addressOf(mod, ifname.c_str(), addrType);
186  return !result.isUnspecified();
187 }

Referenced by inet::UdpBasicApp::chooseDestAddr(), inet::StreamDecoder::configureMappings(), inet::TcpAppBase::connect(), inet::SctpClient::connect(), inet::SctpPeer::connect(), inet::IpvxTrafGen::handleMessageWhenUp(), inet::EthernetSocketIo::initialize(), inet::Ieee8022LlcSocketIo::initialize(), inet::TcpClientSocketIo::open(), inet::UdpBasicApp::processStart(), and resolve().


The documentation for this class was generated from the following files:
inet::math::mod
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:96
inet::Ipv6Address::UNSPECIFIED
@ UNSPECIFIED
Definition: Ipv6Address.h:43
inet::L3Address::MODULEID
@ MODULEID
Definition: L3Address.h:40
inet::units::constants::c
const value< double, compose< units::m, pow< units::s, -1 > > > c(299792458)
protocol
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down protocol
Definition: IUdp-gates.txt:25
inet::L3AddressResolver::resolve
virtual L3Address resolve(const char *str, int addrType=DEFAULT_ADDR_TYPE)
Accepts dotted decimal notation ("127.0.0.1"), module name of the host or router ("host[2]"),...
Definition: L3AddressResolver.cc:33
inet::L3AddressResolver::getInterfaceModulePathAddress
virtual bool getInterfaceModulePathAddress(L3Address &ret, NetworkInterface *ie, bool mask)
Definition: L3AddressResolver.cc:412
inet::L3AddressResolver::getIpv4RoutingTableOf
virtual IIpv4RoutingTable * getIpv4RoutingTableOf(cModule *host)
The function tries to look up the IIpv4RoutingTable module as submodule "routingTable" or "networkLay...
Definition: L3AddressResolver.cc:467
inet::L3AddressResolver::findIpv4RoutingTableOf
virtual IIpv4RoutingTable * findIpv4RoutingTableOf(cModule *host)
Like routingTableOf(), but doesn't throw error if not found.
Definition: L3AddressResolver.cc:491
inet::L3AddressResolver::routerIdOf
virtual L3Address routerIdOf(cModule *host)
Returns the router Id of the given router.
Definition: L3AddressResolver.cc:189
inet::L3AddressResolver::getModuleIdAddressFrom
virtual bool getModuleIdAddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:351
inet::L3AddressResolver::ADDR_MAC
@ ADDR_MAC
Definition: L3AddressResolver.h:72
inet::L3Address::MODULEPATH
@ MODULEPATH
Definition: L3Address.h:39
inet::L3AddressResolver::ADDR_MODULEPATH
@ ADDR_MODULEPATH
Definition: L3AddressResolver.h:73
inet::units::values::s
value< double, units::s > s
Definition: Units.h:1235
inet::L3AddressResolver::getIpv6AddressFrom
virtual bool getIpv6AddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:284
inet::L3AddressResolver::findInterfaceWithMacAddress
virtual NetworkInterface * findInterfaceWithMacAddress(const MacAddress &addr)
Find the interface with the specified MAC address.
Definition: L3AddressResolver.cc:552
inet::L3AddressResolver::addressOf
virtual L3Address addressOf(cModule *host, int addrType=DEFAULT_ADDR_TYPE)
Returns Ipv4 or Ipv6 address of the given host or router.
Definition: L3AddressResolver.cc:199
inet::L3AddressResolver::getIpv4AddressFrom
virtual bool getIpv4AddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:265
inet::L3AddressResolver::tryResolve
virtual bool tryResolve(const char *str, L3Address &result, int addrType=DEFAULT_ADDR_TYPE)
Similar to resolve(), but returns false (instead of throwing an error) if the address cannot be resol...
Definition: L3AddressResolver.cc:72
inet::L3AddressResolver::ADDR_MODULEID
@ ADDR_MODULEID
Definition: L3AddressResolver.h:74
inet::L3AddressResolver::getInterfaceModuleIdAddress
virtual bool getInterfaceModuleIdAddress(L3Address &ret, NetworkInterface *ie, bool mask)
Definition: L3AddressResolver.cc:434
inet::L3AddressResolver::findIpv6RoutingTableOf
virtual Ipv6RoutingTable * findIpv6RoutingTableOf(cModule *host)
Like interfaceTableOf(), but doesn't throw error if not found.
Definition: L3AddressResolver.cc:500
inet::L3AddressResolver::collectNetworkNodes
virtual std::vector< cModule * > collectNetworkNodes()
Collect modules that represent network nodes, as denoted by the @networkNode(true) annotation.
Definition: L3AddressResolver.cc:518
inet::L3AddressResolver::L3AddressResolver
L3AddressResolver()
Definition: L3AddressResolver.h:79
inet::L3AddressResolver::ADDR_IPv6
@ ADDR_IPv6
Definition: L3AddressResolver.h:71
inet::L3AddressResolver::interfaceTableOf
virtual IInterfaceTable * interfaceTableOf(cModule *host)
The function tries to look up the IInterfaceTable module as submodule "interfaceTable" or "networkLay...
Definition: L3AddressResolver.cc:456
inet::L3AddressResolver::getInterfaceIpv4Address
virtual bool getInterfaceIpv4Address(L3Address &ret, NetworkInterface *ie, bool mask)
Definition: L3AddressResolver.cc:384
inet::L3AddressResolver::getInterfaceIpv6Address
virtual bool getInterfaceIpv6Address(L3Address &ret, NetworkInterface *ie, bool mask)
Definition: L3AddressResolver.cc:368
inet::L3AddressResolver::doCollectNetworkNodes
virtual void doCollectNetworkNodes(cModule *parent, std::vector< cModule * > &result)
Definition: L3AddressResolver.cc:525
inet::Ipv6Address::Scope
Scope
Ipv6 address scope (RFC 3513)
Definition: Ipv6Address.h:42
inet::L3AddressResolver::tryParse
bool tryParse(L3Address &result, const char *addr, int addrType=DEFAULT_ADDR_TYPE)
Definition: L3AddressResolver.cc:51
inet::L3AddressResolver::ADDR_MASK
@ ADDR_MASK
Definition: L3AddressResolver.h:75
inet::L3AddressResolver::getInterfaceMacAddress
virtual bool getInterfaceMacAddress(L3Address &ret, NetworkInterface *ie, bool mask)
Definition: L3AddressResolver.cc:403
inet::L3AddressResolver::getModulePathAddressFrom
virtual bool getModulePathAddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:334
inet::L3AddressResolver::getMacAddressFrom
virtual bool getMacAddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:317
inet::L3AddressResolver::getAddressFrom
virtual L3Address getAddressFrom(IInterfaceTable *ift, int addrType=DEFAULT_ADDR_TYPE)
Returns the Ipv4 or Ipv6 address of the given host or router, given its IInterfaceTable module.
Definition: L3AddressResolver.cc:231
inet::Ipv6Address::GLOBAL
@ GLOBAL
Definition: Ipv6Address.h:48
inet::Ipv4Address::isWellFormed
static bool isWellFormed(const char *text)
Returns true if the format of the string corresponds to an Ipv4 address with the dotted notation ("19...
Definition: Ipv4Address.cc:258
inet::L3AddressResolver::ADDR_IPv4
@ ADDR_IPv4
Definition: L3AddressResolver.h:70