#include <IPAddressResolver.h>
Public Types | |
enum | { ADDR_PREFER_IPv4, ADDR_PREFER_IPv6, ADDR_IPv4, ADDR_IPv6 } |
Public Member Functions | |
IPAddressResolver () | |
virtual | ~IPAddressResolver () |
virtual IPvXAddress | resolve (const char *str, int addrType=ADDR_PREFER_IPv6) |
virtual bool | tryResolve (const char *str, IPvXAddress &result, int addrType=ADDR_PREFER_IPv6) |
Utility functions supporting resolve() | |
virtual IPvXAddress | addressOf (cModule *host, int addrType=ADDR_PREFER_IPv6) |
virtual IPvXAddress | addressOf (cModule *host, const char *ifname, int addrType=ADDR_PREFER_IPv6) |
virtual IPAddress | routerIdOf (cModule *host) |
virtual IPvXAddress | getAddressFrom (IInterfaceTable *ift, int addrType=ADDR_PREFER_IPv6) |
virtual IPvXAddress | getAddressFrom (InterfaceEntry *ie, int addrType=ADDR_PREFER_IPv6) |
virtual IInterfaceTable * | interfaceTableOf (cModule *host) |
virtual IRoutingTable * | routingTableOf (cModule *host) |
virtual RoutingTable6 * | routingTable6Of (cModule *host) |
virtual NotificationBoard * | notificationBoardOf (cModule *host) |
virtual IInterfaceTable * | findInterfaceTableOf (cModule *host) |
virtual IRoutingTable * | findRoutingTableOf (cModule *host) |
virtual RoutingTable6 * | findRoutingTable6Of (cModule *host) |
virtual NotificationBoard * | findNotificationBoardOf (cModule *host) |
Protected Member Functions | |
virtual IPAddress | getIPv4AddressFrom (IInterfaceTable *ift) |
virtual IPv6Address | getIPv6AddressFrom (IInterfaceTable *ift) |
virtual IPv6Address | getInterfaceIPv6Address (InterfaceEntry *ie) |
Utility class for finding IPv4 or IPv6 address of a host or router.
Syntax variations understood:
Definition at line 46 of file IPAddressResolver.h.
anonymous enum |
Definition at line 59 of file IPAddressResolver.h.
{ ADDR_PREFER_IPv4, ADDR_PREFER_IPv6, ADDR_IPv4, ADDR_IPv6 };
IPAddressResolver::IPAddressResolver | ( | ) | [inline] |
Definition at line 67 of file IPAddressResolver.h.
{}
virtual IPAddressResolver::~IPAddressResolver | ( | ) | [inline, virtual] |
Definition at line 68 of file IPAddressResolver.h.
{}
IPvXAddress IPAddressResolver::addressOf | ( | cModule * | host, | |
int | addrType = ADDR_PREFER_IPv6 | |||
) | [virtual] |
Returns IPv4 or IPv6 address of the given host or router.
This function uses routingTableOf() to find the IRoutingTable module, then invokes getAddressFrom() to extract the IP address.
Definition at line 98 of file IPAddressResolver.cc.
Referenced by tryResolve().
{ IInterfaceTable *ift = interfaceTableOf(host); return getAddressFrom(ift, addrType); }
IPvXAddress IPAddressResolver::addressOf | ( | cModule * | host, | |
const char * | ifname, | |||
int | addrType = ADDR_PREFER_IPv6 | |||
) | [virtual] |
Similar to addressOf(), but only looks at the given interface
Definition at line 104 of file IPAddressResolver.cc.
{ IInterfaceTable *ift = interfaceTableOf(host); InterfaceEntry *ie = ift->getInterfaceByName(ifname); if (!ie) opp_error("IPAddressResolver: no interface called `%s' in interface table", ifname, ift->getFullPath().c_str()); return getAddressFrom(ie, addrType); }
IInterfaceTable * IPAddressResolver::findInterfaceTableOf | ( | cModule * | host | ) | [virtual] |
Like interfaceTableOf(), but doesn't throw error if not found.
Definition at line 258 of file IPAddressResolver.cc.
{ cModule *mod = host->getSubmodule("interfaceTable"); return dynamic_cast<IInterfaceTable *>(mod); }
NotificationBoard * IPAddressResolver::findNotificationBoardOf | ( | cModule * | host | ) | [virtual] |
Like notificationBoardOf(), but doesn't throw error if not found.
Definition at line 278 of file IPAddressResolver.cc.
{ cModule *mod = host->getSubmodule("notificationBoard"); return dynamic_cast<NotificationBoard *>(mod); }
RoutingTable6 * IPAddressResolver::findRoutingTable6Of | ( | cModule * | host | ) | [virtual] |
Like interfaceTableOf(), but doesn't throw error if not found.
Definition at line 271 of file IPAddressResolver.cc.
{ cModule *mod = host->getSubmodule("routingTable6"); return dynamic_cast<RoutingTable6 *>(mod); }
IRoutingTable * IPAddressResolver::findRoutingTableOf | ( | cModule * | host | ) | [virtual] |
Like routingTableOf(), but doesn't throw error if not found.
Definition at line 264 of file IPAddressResolver.cc.
{ cModule *mod = host->getSubmodule("routingTable"); return dynamic_cast<IRoutingTable *>(mod); }
IPvXAddress IPAddressResolver::getAddressFrom | ( | IInterfaceTable * | ift, | |
int | addrType = ADDR_PREFER_IPv6 | |||
) | [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.
Definition at line 113 of file IPAddressResolver.cc.
Referenced by addressOf().
{ IPvXAddress ret; if (addrType==ADDR_IPv6 || addrType==ADDR_PREFER_IPv6) { ret = getIPv6AddressFrom(ift); if (ret.isUnspecified() && addrType==ADDR_PREFER_IPv6) ret = getIPv4AddressFrom(ift); } else if (addrType==ADDR_IPv4 || addrType==ADDR_PREFER_IPv4) { ret = getIPv4AddressFrom(ift); if (ret.isUnspecified() && addrType==ADDR_PREFER_IPv4) ret = getIPv6AddressFrom(ift); } else { opp_error("IPAddressResolver: unknown addrType %d", addrType); } return ret; }
IPvXAddress IPAddressResolver::getAddressFrom | ( | InterfaceEntry * | ie, | |
int | addrType = ADDR_PREFER_IPv6 | |||
) | [virtual] |
Returns the IPv4 or IPv6 address of the given interface (of a host or router).
Definition at line 135 of file IPAddressResolver.cc.
{ IPvXAddress ret; if (addrType==ADDR_IPv6 || addrType==ADDR_PREFER_IPv6) { if (ie->ipv6Data()) ret = getInterfaceIPv6Address(ie); if (ret.isUnspecified() && addrType==ADDR_PREFER_IPv6 && ie->ipv4Data()) ret = ie->ipv4Data()->getIPAddress(); } else if (addrType==ADDR_IPv4 || addrType==ADDR_PREFER_IPv4) { if (ie->ipv4Data()) ret = ie->ipv4Data()->getIPAddress(); if (ret.isUnspecified() && addrType==ADDR_PREFER_IPv4 && ie->ipv6Data()) ret = getInterfaceIPv6Address(ie); } else { opp_error("IPAddressResolver: unknown addrType %d", addrType); } return ret; }
IPv6Address IPAddressResolver::getInterfaceIPv6Address | ( | InterfaceEntry * | ie | ) | [protected, virtual] |
Definition at line 205 of file IPAddressResolver.cc.
Referenced by getAddressFrom().
{ #ifndef WITHOUT_IPv6 if (!ie->ipv6Data()) return IPv6Address(); return ie->ipv6Data()->getPreferredAddress(); #else return IPv6Address(); #endif }
IPAddress IPAddressResolver::getIPv4AddressFrom | ( | IInterfaceTable * | ift | ) | [protected, virtual] |
Definition at line 159 of file IPAddressResolver.cc.
Referenced by getAddressFrom().
{ IPAddress addr; if (ift->getNumInterfaces()==0) opp_error("IPAddressResolver: interface table `%s' has no interface registered " "(yet? try in a later init stage!)", ift->getFullPath().c_str()); // choose first usable interface address (configured for IPv4, non-loopback if, addr non-null) for (int i=0; i<ift->getNumInterfaces(); i++) { InterfaceEntry *ie = ift->getInterface(i); if (ie->ipv4Data() && !ie->ipv4Data()->getIPAddress().isUnspecified() && !ie->isLoopback()) { addr = ie->ipv4Data()->getIPAddress(); break; } } return addr; }
IPv6Address IPAddressResolver::getIPv6AddressFrom | ( | IInterfaceTable * | ift | ) | [protected, virtual] |
Definition at line 179 of file IPAddressResolver.cc.
Referenced by getAddressFrom().
{ #ifndef WITHOUT_IPv6 // browse interfaces and pick a globally routable address if (ift->getNumInterfaces()==0) opp_error("IPAddressResolver: interface table `%s' has no interface registered " "(yet? try in a later init stage!)", ift->getFullPath().c_str()); IPv6Address addr; for (int i=0; i<ift->getNumInterfaces() && addr.isUnspecified(); i++) { InterfaceEntry *ie = ift->getInterface(i); if (!ie->ipv6Data() || ie->isLoopback()) continue; IPv6Address ifAddr = ie->ipv6Data()->getPreferredAddress(); if (addr.isGlobal() && ifAddr.isGlobal() && addr!=ifAddr) EV << ift->getFullPath() << " has at least two globally routable addresses on different interfaces\n"; if (ifAddr.isGlobal()) addr = ifAddr; } return addr; #else return IPv6Address(); #endif }
IInterfaceTable * IPAddressResolver::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.
Definition at line 216 of file IPAddressResolver.cc.
Referenced by addressOf().
{ // find IInterfaceTable cModule *mod = host->getSubmodule("interfaceTable"); if (!mod) opp_error("IPAddressResolver: IInterfaceTable not found as submodule " " `interfaceTable' in host/router `%s'", host->getFullPath().c_str()); return check_and_cast<IInterfaceTable *>(mod); }
NotificationBoard * IPAddressResolver::notificationBoardOf | ( | cModule * | host | ) | [virtual] |
The function tries to look up the NotificationBoard module as submodule "notificationBoard"
within the host/router module. Throws an error if not found.
Definition at line 248 of file IPAddressResolver.cc.
{ // find NotificationBoard cModule *mod = host->getSubmodule("notificationBoard"); if (!mod) opp_error("IPAddressResolver: NotificationBoard not found as submodule " " notificationBoard' in host/router `%s'", host->getFullPath().c_str()); return check_and_cast<NotificationBoard *>(mod); }
IPvXAddress IPAddressResolver::resolve | ( | const char * | str, | |
int | addrType = ADDR_PREFER_IPv6 | |||
) | [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 simulation.getModuleByPath()
, and then addressOf() will be called to determine its IP address.
Definition at line 30 of file IPAddressResolver.cc.
{ IPvXAddress addr; if (!tryResolve(s, addr, addrType)) opp_error("IPAddressResolver: address `%s' not configured (yet?)", s); return addr; }
IPAddress IPAddressResolver::routerIdOf | ( | cModule * | host | ) | [virtual] |
Returns the router Id of the given router. Router Id is obtained from the getRouterId() method of the IRoutingTable submodule.
Definition at line 92 of file IPAddressResolver.cc.
Referenced by tryResolve().
{ IRoutingTable *rt = routingTableOf(host); return rt->getRouterId(); }
RoutingTable6 * IPAddressResolver::routingTable6Of | ( | cModule * | host | ) | [virtual] |
The function tries to look up the RoutingTable6 module as submodule "routingTable6"
or "networkLayer.routingTable6"
within the host/router module. Throws an error if not found.
Definition at line 237 of file IPAddressResolver.cc.
{ // find IRoutingTable cModule *mod = host->getSubmodule("routingTable6"); if (!mod) opp_error("IPAddressResolver: RoutingTable6 not found as submodule " " `routingTable6' in host/router `%s'", host->getFullPath().c_str()); return check_and_cast<RoutingTable6 *>(mod); }
IRoutingTable * IPAddressResolver::routingTableOf | ( | cModule * | host | ) | [virtual] |
The function tries to look up the IRoutingTable module as submodule "routingTable"
or "networkLayer.routingTable"
within the host/router module. Throws an error if not found.
Definition at line 226 of file IPAddressResolver.cc.
Referenced by routerIdOf().
{ // find IRoutingTable cModule *mod = host->getSubmodule("routingTable"); if (!mod) opp_error("IPAddressResolver: IRoutingTable not found as submodule " " `routingTable' in host/router `%s'", host->getFullPath().c_str()); return check_and_cast<IRoutingTable *>(mod); }
bool IPAddressResolver::tryResolve | ( | const char * | str, | |
IPvXAddress & | result, | |||
int | addrType = ADDR_PREFER_IPv6 | |||
) | [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).
Definition at line 38 of file IPAddressResolver.cc.
Referenced by resolve().
{ // empty address result = IPvXAddress(); if (!s || !*s) return true; // handle address literal if (result.tryParse(s)) return true; // must be "modulename/interfacename(protocol)" syntax then, // "/interfacename" and "(protocol)" being optional const char *slashp = strchr(s,'/'); const char *leftparenp = strchr(s,'('); const char *rightparenp = strchr(s,')'); const char *endp = s+strlen(s); // rudimentary syntax check if ((slashp && leftparenp && slashp>leftparenp) || (leftparenp && !rightparenp) || (!leftparenp && rightparenp) || (rightparenp && rightparenp!=endp-1)) { opp_error("IPAddressResolver: syntax error parsing address spec `%s'", s); } // parse fields: modname, ifname, protocol std::string modname, ifname, protocol; modname.assign(s, (slashp?slashp:leftparenp?leftparenp:endp)-s); if (slashp) ifname.assign(slashp+1, (leftparenp?leftparenp:endp)-slashp-1); if (leftparenp) protocol.assign(leftparenp+1, rightparenp-leftparenp-1); // find module and check protocol cModule *mod = simulation.getModuleByPath(modname.c_str()); if (!mod) opp_error("IPAddressResolver: module `%s' not found", modname.c_str()); if (!protocol.empty() && protocol!="ipv4" && protocol!="ipv6") opp_error("IPAddressResolver: error parsing address spec `%s': address type must be `(ipv4)' or `(ipv6)'", s); if (!protocol.empty()) addrType = protocol=="ipv4" ? ADDR_IPv4 : ADDR_IPv6; // get address from the given module/interface if (ifname.empty()) result = addressOf(mod, addrType); else if (ifname == "routerId") result = IPvXAddress(routerIdOf(mod)); // addrType is meaningless here, routerId is protocol independent else result = addressOf(mod, ifname.c_str(), addrType); return !result.isUnspecified(); }