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::NetworkConfiguratorBase inet::GenericNetworkConfigurator inet::IPv4NetworkConfigurator

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()
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 (InterfaceEntry *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 IIPv4RoutingTableroutingTableOf (cModule *host)
 The function tries to look up the IIPv4RoutingTable module as submodule "routingTable" or "networkLayer.routingTable" within the host/router module. More...
 
virtual IPv6RoutingTableroutingTable6Of (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 GenericRoutingTablefindGenericRoutingTableOf (cModule *host)
 Like interfaceTableOf(), but doesn't throw error if not found. More...
 
virtual cModule * findHostWithAddress (const L3Address &addr)
 Find the Host with the specified 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, InterfaceEntry *ie, bool mask)
 
virtual bool getInterfaceIPv6Address (L3Address &ret, InterfaceEntry *ie, bool mask)
 
virtual bool getInterfaceMACAddress (L3Address &ret, InterfaceEntry *ie, bool mask)
 
virtual bool getInterfaceModulePathAddress (L3Address &ret, InterfaceEntry *ie, bool mask)
 
virtual bool getInterfaceModuleIdAddress (L3Address &ret, InterfaceEntry *ie, bool mask)
 

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
Enumerator
ADDR_IPv4 
ADDR_IPv6 
ADDR_MAC 
ADDR_MODULEPATH 
ADDR_MODULEID 
ADDR_MASK 
79  {
80  ADDR_IPv4 = 1,
81  ADDR_IPv6 = 2,
82  ADDR_MAC = 4,
83  ADDR_MODULEPATH = 8,
84  ADDR_MODULEID = 16,
85  ADDR_MASK = 32
86  };
Definition: L3AddressResolver.h:81
Definition: L3AddressResolver.h:82
Definition: L3AddressResolver.h:85
Definition: L3AddressResolver.h:83
Definition: L3AddressResolver.h:84
Definition: L3AddressResolver.h:80

Constructor & Destructor Documentation

inet::L3AddressResolver::L3AddressResolver ( )
inline
virtual inet::L3AddressResolver::~L3AddressResolver ( )
inlinevirtual
90 {}

Member Function Documentation

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.

Referenced by tryResolve().

192 {
193  IInterfaceTable *ift = interfaceTableOf(host);
194  return getAddressFrom(ift, addrType);
195 }
virtual IInterfaceTable * interfaceTableOf(cModule *host)
The function tries to look up the IInterfaceTable module as submodule "interfaceTable" or "networkLay...
Definition: L3AddressResolver.cc:423
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:223
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.

198 {
199  IInterfaceTable *ift = interfaceTableOf(host);
200  InterfaceEntry *ie = ift->getInterfaceByName(ifname);
201  if (ie)
202  return getAddressFrom(ie, addrType);
203 
204  throw cRuntimeError("L3AddressResolver: no interface called `%s' in interface table of `%s'", ifname, host->getFullPath().c_str());
205 }
virtual IInterfaceTable * interfaceTableOf(cModule *host)
The function tries to look up the IInterfaceTable module as submodule "interfaceTable" or "networkLay...
Definition: L3AddressResolver.cc:423
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:223
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.

208 {
209  IInterfaceTable *ift = interfaceTableOf(host);
210  for (int i = 0; i < ift->getNumInterfaces(); i++) {
211  InterfaceEntry *ie = ift->getInterface(i);
212  if (ie) {
213  int gateId = ie->getNodeOutputGateId();
214  if (gateId != -1)
215  if (host->gate(gateId)->pathContains(destmod))
216  return getAddressFrom(ie, addrType);
217 
218  }
219  }
220  throw cRuntimeError("L3AddressResolver: no interface connected to `%s' module in interface table of `%s'", destmod->getFullPath().c_str(), host->getFullPath().c_str());
221 }
virtual IInterfaceTable * interfaceTableOf(cModule *host)
The function tries to look up the IInterfaceTable module as submodule "interfaceTable" or "networkLay...
Definition: L3AddressResolver.cc:423
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:223
GenericRoutingTable * inet::L3AddressResolver::findGenericRoutingTableOf ( cModule *  host)
virtual

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

485 {
486 #ifdef WITH_GENERIC
487  // KLUDGE: TODO: look deeper temporarily
488  GenericRoutingTable *rt = dynamic_cast<GenericRoutingTable *>(host->getSubmodule("routingTable"));
489  if (!rt)
490  rt = dynamic_cast<GenericRoutingTable *>(host->getModuleByPath(".routingTable.generic"));
491  return rt;
492 #else // ifdef WITH_GENERIC
493  return nullptr;
494 #endif // ifdef WITH_GENERIC
495 }
cModule * inet::L3AddressResolver::findHostWithAddress ( const L3Address addr)
virtual

Find the Host with the specified address.

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

498 {
499  if (add.isUnspecified() || add.isMulticast())
500  return nullptr;
501 
502  cTopology topo("topo");
503  topo.extractByProperty("networkNode");
504 
505  // fill in isIPNode, ift and rt members in nodeInfo[]
506 
507  for (int i = 0; i < topo.getNumNodes(); i++) {
508  cModule *mod = topo.getNode(i)->getModule();
509  IInterfaceTable *itable = L3AddressResolver().findInterfaceTableOf(mod);
510  if (itable != nullptr) {
511  for (int i = 0; i < itable->getNumInterfaces(); i++) {
512  InterfaceEntry *entry = itable->getInterface(i);
513  switch (add.getType()) {
514 #ifdef WITH_IPv6
515  case L3Address::IPv6:
516  if (entry->ipv6Data()->hasAddress(add.toIPv6()))
517  return mod;
518  break;
519 
520 #endif // ifdef WITH_IPv6
521 #ifdef WITH_IPv4
522  case L3Address::IPv4:
523  if (entry->ipv4Data() && entry->ipv4Data()->getIPAddress() == add.toIPv4())
524  return mod;
525  break;
526 
527 #endif // ifdef WITH_IPv4
528  default:
529  (void)entry; // eliminate warning: unused variable 'entry'
530  throw cRuntimeError("findHostWithAddress() doesn't accept AddressType '%s', yet", L3Address::getTypeName(add.getType()));
531  break;
532  }
533  }
534  }
535  }
536  return nullptr;
537 }
Definition: L3Address.h:47
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
static const char * getTypeName(AddressType t)
Definition: L3Address.cc:384
Definition: L3Address.h:46
L3AddressResolver()
Definition: L3AddressResolver.h:89
IIPv4RoutingTable * inet::L3AddressResolver::findIPv4RoutingTableOf ( cModule *  host)
virtual

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

Referenced by inet::FlatNetworkConfigurator::extractTopology(), inet::IPv4NodeConfigurator::initialize(), inet::TED::initializeTED(), routingTableOf(), and inet::visualizer::RoutingTableVisualizerBase::updateAllRouteVisualizations().

459 {
460 #ifdef WITH_IPv4
461  // KLUDGE: TODO: look deeper temporarily
462  IIPv4RoutingTable *rt = dynamic_cast<IIPv4RoutingTable *>(host->getSubmodule("routingTable"));
463  if (!rt)
464  rt = dynamic_cast<IIPv4RoutingTable *>(host->getModuleByPath(".routingTable.ipv4"));
465  return rt;
466 #else // ifdef WITH_IPv4
467  return nullptr;
468 #endif // ifdef WITH_IPv4
469 }
IPv6RoutingTable * inet::L3AddressResolver::findIPv6RoutingTableOf ( cModule *  host)
virtual

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

Referenced by inet::FlatNetworkConfigurator6::addOwnAdvPrefixRoutes(), inet::FlatNetworkConfigurator6::addStaticRoutes(), inet::FlatNetworkConfigurator6::configureAdvPrefixes(), inet::FlatNetworkConfigurator6::isIPNode(), and routingTable6Of().

472 {
473 #ifdef WITH_IPv6
474  // KLUDGE: TODO: look deeper temporarily
475  IPv6RoutingTable *rt = dynamic_cast<IPv6RoutingTable *>(host->getSubmodule("routingTable"));
476  if (!rt)
477  rt = dynamic_cast<IPv6RoutingTable *>(host->getModuleByPath(".routingTable.ipv6"));
478  return rt;
479 #else // ifdef WITH_IPv6
480  return nullptr;
481 #endif // ifdef WITH_IPv6
482 }
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.

Referenced by addressOf().

224 {
225  L3Address ret;
226  bool netmask = addrType & ADDR_MASK;
227  if ((addrType & ADDR_IPv4) && getIPv4AddressFrom(ret, ift, netmask))
228  return ret;
229  else if ((addrType & ADDR_IPv6) && getIPv6AddressFrom(ret, ift, netmask))
230  return ret;
231  else if ((addrType & ADDR_MAC) && getMACAddressFrom(ret, ift, netmask))
232  return ret;
233  else if ((addrType & ADDR_MODULEPATH) && getModulePathAddressFrom(ret, ift, netmask))
234  return ret;
235  else if ((addrType & ADDR_MODULEID) && getModuleIdAddressFrom(ret, ift, netmask))
236  return ret;
237  else
238  throw cRuntimeError("L3AddressResolver: unknown addrType %d", addrType);
239  return ret;
240 }
virtual bool getMACAddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:314
Definition: L3AddressResolver.h:81
virtual bool getModulePathAddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:331
Definition: L3AddressResolver.h:82
Definition: L3AddressResolver.h:85
Definition: L3AddressResolver.h:83
Definition: L3AddressResolver.h:84
Definition: L3AddressResolver.h:80
virtual bool getIPv6AddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:282
virtual bool getIPv4AddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:263
virtual bool getModuleIdAddressFrom(L3Address &retAddr, IInterfaceTable *ift, bool netmask)
Definition: L3AddressResolver.cc:348
L3Address inet::L3AddressResolver::getAddressFrom ( InterfaceEntry ie,
int  addrType = DEFAULT_ADDR_TYPE 
)
virtual

Returns the IPv4 or IPv6 address of the given interface (of a host or router).

243 {
244  L3Address ret;
245  bool mask = addrType & ADDR_MASK;
246 
247  if ((addrType & ADDR_IPv4) && getInterfaceIPv4Address(ret, ie, mask))
248  return ret;
249  else if ((addrType & ADDR_IPv6) && getInterfaceIPv6Address(ret, ie, mask))
250  return ret;
251  else if ((addrType & ADDR_MAC) && getInterfaceMACAddress(ret, ie, mask))
252  return ret;
253  else if ((addrType & ADDR_MODULEPATH) && getInterfaceModulePathAddress(ret, ie, mask))
254  return ret;
255  else if ((addrType & ADDR_MODULEID) && getInterfaceModuleIdAddress(ret, ie, mask))
256  return ret;
257  else
258  throw cRuntimeError("L3AddressResolver: unknown addrType %d at %s", addrType, ie->getFullPath().c_str());
259 
260  return ret;
261 }
virtual bool getInterfaceModuleIdAddress(L3Address &ret, InterfaceEntry *ie, bool mask)
Definition: L3AddressResolver.cc:417
Definition: L3AddressResolver.h:81
virtual bool getInterfaceIPv6Address(L3Address &ret, InterfaceEntry *ie, bool mask)
Definition: L3AddressResolver.cc:365
virtual bool getInterfaceModulePathAddress(L3Address &ret, InterfaceEntry *ie, bool mask)
Definition: L3AddressResolver.cc:411
Definition: L3AddressResolver.h:82
virtual bool getInterfaceIPv4Address(L3Address &ret, InterfaceEntry *ie, bool mask)
Definition: L3AddressResolver.cc:381
Definition: L3AddressResolver.h:85
Definition: L3AddressResolver.h:83
Definition: L3AddressResolver.h:84
Definition: L3AddressResolver.h:80
virtual bool getInterfaceMACAddress(L3Address &ret, InterfaceEntry *ie, bool mask)
Definition: L3AddressResolver.cc:402
bool inet::L3AddressResolver::getInterfaceIPv4Address ( L3Address ret,
InterfaceEntry ie,
bool  mask 
)
protectedvirtual

Reimplemented in inet::IPv4NetworkConfigurator.

Referenced by getAddressFrom(), getInterfaceIPv4Address(), and getIPv4AddressFrom().

382 {
383 #ifdef WITH_IPv4
384  if (ie->ipv4Data()) {
385  IPv4Address addr = ie->ipv4Data()->getIPAddress();
386  if (!addr.isUnspecified()) {
387  ret.set(netmask ? ie->ipv4Data()->getNetmask() : addr);
388  return true;
389  }
390  }
391  else {
392  // find address in the configurator's notebook
393  // TODO: how do we know where is the configurator? get the path from a NED parameter?
394  L3AddressResolver *configurator = dynamic_cast<L3AddressResolver *>(getSimulation()->getModuleByPath("configurator"));
395  if (configurator)
396  return configurator->getInterfaceIPv4Address(ret, ie, netmask);
397  }
398 #endif // ifdef WITH_IPv4
399  return false;
400 }
L3AddressResolver()
Definition: L3AddressResolver.h:89
bool inet::L3AddressResolver::getInterfaceIPv6Address ( L3Address ret,
InterfaceEntry ie,
bool  mask 
)
protectedvirtual

Referenced by getAddressFrom().

366 {
367 #ifdef WITH_IPv6
368  if (netmask)
369  return false; // IPv6 netmask not supported yet
370  if (ie->ipv6Data()) {
371  IPv6Address addr = ie->ipv6Data()->getPreferredAddress();
372  if (!addr.isUnspecified()) {
373  ret.set(addr);
374  return true;
375  }
376  }
377 #endif // ifdef WITH_IPv6
378  return false;
379 }
bool inet::L3AddressResolver::getInterfaceMACAddress ( L3Address ret,
InterfaceEntry ie,
bool  mask 
)
protectedvirtual

Referenced by getAddressFrom(), and getMACAddressFrom().

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

Referenced by getAddressFrom(), and getModuleIdAddressFrom().

418 {
419  ret = ie->getModuleIdAddress();
420  return true;
421 }
bool inet::L3AddressResolver::getInterfaceModulePathAddress ( L3Address ret,
InterfaceEntry ie,
bool  mask 
)
protectedvirtual

Referenced by getAddressFrom(), and getModulePathAddressFrom().

412 {
413  ret = ie->getModulePathAddress();
414  return true;
415 }
bool inet::L3AddressResolver::getIPv4AddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual

Referenced by getAddressFrom().

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

Referenced by getAddressFrom().

283 {
284  // browse interfaces and pick a globally routable address
285  if (ift->getNumInterfaces() == 0)
286  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
287  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
288 
289 #ifndef WITH_IPv6
290  return false;
291 #else // ifndef WITH_IPv6
292  if (netmask)
293  return false; // IPv6 netmask not supported yet
294 
295  bool ret = false;
297 
298  for (int i = 0; i < ift->getNumInterfaces() && retScope != IPv6Address::GLOBAL; i++) {
299  InterfaceEntry *ie = ift->getInterface(i);
300  if (!ie->ipv6Data() || ie->isLoopback())
301  continue;
302  IPv6Address curAddr = ie->ipv6Data()->getPreferredAddress();
303  IPv6Address::Scope curScope = curAddr.getScope();
304  if (curScope > retScope) {
305  retAddr = curAddr;
306  retScope = curScope;
307  ret = true;
308  }
309  }
310  return ret;
311 #endif // ifndef WITH_IPv6
312 }
Definition: IPv6Address.h:60
Scope
IPv6 address scope (RFC 3513)
Definition: IPv6Address.h:54
Definition: IPv6Address.h:55
bool inet::L3AddressResolver::getMACAddressFrom ( L3Address retAddr,
IInterfaceTable ift,
bool  netmask 
)
protectedvirtual

Referenced by getAddressFrom().

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

Referenced by getAddressFrom().

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

Referenced by getAddressFrom().

332 {
333  if (ift->getNumInterfaces() == 0)
334  throw cRuntimeError("L3AddressResolver: interface table `%s' has no interface registered "
335  "(yet? try in a later init stage!)", ift->getFullPath().c_str());
336 
337  // choose first usable interface address (configured for generic, non-loopback if, addr non-null)
338  for (int i = 0; i < ift->getNumInterfaces(); i++) {
339  InterfaceEntry *ie = ift->getInterface(i);
340  if (ie->isLoopback())
341  continue;
342  if (getInterfaceModulePathAddress(retAddr, ie, netmask))
343  return true;
344  }
345  return false;
346 }
virtual bool getInterfaceModulePathAddress(L3Address &ret, InterfaceEntry *ie, bool mask)
Definition: L3AddressResolver.cc:411
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.

Referenced by inet::FlatNetworkConfigurator6::addOwnAdvPrefixRoutes(), addressOf(), inet::FlatNetworkConfigurator6::addStaticRoutes(), inet::FlatNetworkConfigurator6::configureAdvPrefixes(), inet::FlatNetworkConfigurator::extractTopology(), and inet::HostAutoConfigurator::setupNetworkLayer().

424 {
425  // find IInterfaceTable
426  cModule *mod = host->getSubmodule("interfaceTable");
427  if (!mod)
428  throw cRuntimeError("L3AddressResolver: IInterfaceTable not found as submodule "
429  " `interfaceTable' in host/router `%s'", host->getFullPath().c_str());
430 
431  return check_and_cast<IInterfaceTable *>(mod);
432 }
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
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.

Referenced by inet::UDPBasicApp::chooseDestAddr(), inet::MultiFieldClassifier::configureFilters(), inet::NetPerfMeter::createAndBindSocket(), inet::Define_Module(), inet::NetPerfMeter::establishConnection(), inet::xmlutils::getParameterIPAddressValue(), inet::PingApp::handleMessage(), inet::VoIPStreamSender::initialize(), inet::WiseRoute::initialize(), inet::SCTPNatServer::initialize(), inet::SCTPServer::initialize(), inet::SCTPClient::initialize(), inet::SCTPNatPeer::initialize(), inet::SCTPPeer::initialize(), inet::dymo::DYMO::initialize(), inet::ospf::ipv4AddressFromAddressString(), inet::ospf::ipv4NetmaskFromAddressString(), inet::PingApp::parseDestAddressesPar(), inet::UDPBasicBurst::processStart(), inet::IPv4NetworkConfigurator::readManualMulticastRouteConfiguration(), inet::IPv4NetworkConfigurator::readManualRouteConfiguration(), inet::RSVP::readTrafficRouteFromXML(), inet::UDPVideoStreamCli::requestStream(), resolve(), inet::IPv4NetworkConfigurator::resolveInterfaceAndGateway(), inet::tcp::TCPSpoof::sendSpoofPacket(), inet::SimpleVoIPSender::sendVoIPPacket(), inet::UDPSink::setSocketOptions(), inet::SCTPNatPeer::socketClosed(), inet::SCTPNatPeer::socketPeerClosed(), and inet::DHCPServer::startApp().

47 {
48  L3Address addr;
49  if (!tryResolve(s, addr, addrType))
50  throw cRuntimeError("L3AddressResolver: address `%s' not configured (yet?)", s);
51  return addr;
52 }
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:64
value< double, units::s > s
Definition: Units.h:1049
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().

55 {
56  std::vector<L3Address> result;
57  int n = strs.size();
58  result.reserve(n);
59  for (int i = 0; i < n; i++)
60  result.push_back(resolve(strs[i].c_str(), addrType));
61  return result;
62 }
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 ("").
Definition: L3AddressResolver.cc:46
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.

Referenced by tryResolve().

182 {
183 #ifdef WITH_IPv4
184  IIPv4RoutingTable *rt = routingTableOf(host);
185  return L3Address(rt->getRouterId());
186 #else // ifdef WITH_IPv4
187  throw cRuntimeError("INET was compiled without IPv4 support");
188 #endif // ifdef WITH_IPv4
189 }
virtual IIPv4RoutingTable * routingTableOf(cModule *host)
The function tries to look up the IIPv4RoutingTable module as submodule "routingTable" or "networkLay...
Definition: L3AddressResolver.cc:434
IPv6RoutingTable * inet::L3AddressResolver::routingTable6Of ( 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.

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

444 {
445  // find IPv6RoutingTable
446  IPv6RoutingTable *mod = findIPv6RoutingTableOf(host);
447  if (!mod)
448  throw cRuntimeError("L3AddressResolver: IPv6RoutingTable not found as submodule "
449  " `routingTable' in host/router `%s'", host->getFullPath().c_str());
450  return mod;
451 }
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
virtual IPv6RoutingTable * findIPv6RoutingTableOf(cModule *host)
Like interfaceTableOf(), but doesn&#39;t throw error if not found.
Definition: L3AddressResolver.cc:471
IIPv4RoutingTable * inet::L3AddressResolver::routingTableOf ( 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.

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

435 {
436  IIPv4RoutingTable *mod = findIPv4RoutingTableOf(host);
437  if (!mod)
438  throw cRuntimeError("L3AddressResolver: IIPv4RoutingTable not found as submodule "
439  " `routingTable' in host/router `%s'", host->getFullPath().c_str());
440  return mod;
441 }
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
virtual IIPv4RoutingTable * findIPv4RoutingTableOf(cModule *host)
Like routingTableOf(), but doesn&#39;t throw error if not found.
Definition: L3AddressResolver.cc:458
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).

Referenced by inet::TCPAppBase::connect(), inet::SCTPClient::connect(), inet::SCTPPeer::connect(), inet::IPvXTrafGen::handleMessage(), inet::UDPBasicApp::processStart(), and resolve().

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

The documentation for this class was generated from the following files: