INET Framework for OMNeT++/OMNEST
IPAddressResolver Class Reference

#include <IPAddressResolver.h>

List of all members.

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 IInterfaceTableinterfaceTableOf (cModule *host)
virtual IRoutingTableroutingTableOf (cModule *host)
virtual RoutingTable6routingTable6Of (cModule *host)
virtual NotificationBoardnotificationBoardOf (cModule *host)
virtual IInterfaceTablefindInterfaceTableOf (cModule *host)
virtual IRoutingTablefindRoutingTableOf (cModule *host)
virtual RoutingTable6findRoutingTable6Of (cModule *host)
virtual NotificationBoardfindNotificationBoardOf (cModule *host)

Protected Member Functions

virtual IPAddress getIPv4AddressFrom (IInterfaceTable *ift)
virtual IPv6Address getIPv6AddressFrom (IInterfaceTable *ift)
virtual IPv6Address getInterfaceIPv6Address (InterfaceEntry *ie)

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"

Member Enumeration Documentation

anonymous enum
Enumerator:
ADDR_PREFER_IPv4 
ADDR_PREFER_IPv6 
ADDR_IPv4 
ADDR_IPv6 

Constructor & Destructor Documentation

IPAddressResolver::IPAddressResolver ( ) [inline]
{}
virtual IPAddressResolver::~IPAddressResolver ( ) [inline, virtual]
{}

Member Function Documentation

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.

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

{
    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.

Referenced by NetworkConfigurator::extractTopology(), FlatNetworkConfigurator::extractTopology(), and FlatNetworkConfigurator6::isIPNode().

{
    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.

{
    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.

{
    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.

Referenced by NetworkConfigurator::extractTopology().

{
    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.

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).

{
    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]

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]

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]

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.

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

{
    // 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.

{
    // 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.

Referenced by getParameterIPAddressValue(), PingApp::handleMessage(), RSVP::readTrafficRouteFromXML(), UDPVideoStreamCli::requestStream(), and TCPSpoof::sendSpoofPacket().

{
    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.

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.

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

{
    // 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.

Referenced by FlatNetworkConfigurator::extractTopology(), and 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).

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();
}

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