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

HostAutoConfigurator automatically assigns IP addresses and sets up routing table. More...

#include <HostAutoConfigurator.h>

Inheritance diagram for inet::HostAutoConfigurator:

Public Member Functions

virtual void initialize (int stage) override
 
virtual void finish () override
 
virtual int numInitStages () const override
 
virtual void handleMessage (cMessage *msg) override
 

Protected Member Functions

void setupNetworkLayer ()
 

Detailed Description

HostAutoConfigurator automatically assigns IP addresses and sets up routing table.

Author
Christoph Sommer

Member Function Documentation

void inet::HostAutoConfigurator::finish ( )
overridevirtual
45 {
46 }
void inet::HostAutoConfigurator::handleMessage ( cMessage *  msg)
overridevirtual
49 {
50 }
void inet::HostAutoConfigurator::initialize ( int  stage)
overridevirtual
36 {
37  cSimpleModule::initialize(stage);
38 
39  if (stage == INITSTAGE_NETWORK_LAYER_2) {
41  }
42 }
void setupNetworkLayer()
Definition: HostAutoConfigurator.cc:52
Initialization of network-layer protocols, stage 2.
Definition: InitStages.h:78
virtual int inet::HostAutoConfigurator::numInitStages ( ) const
inlineoverridevirtual
37 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::HostAutoConfigurator::setupNetworkLayer ( )
protected

Referenced by initialize().

53 {
54  EV_INFO << "host auto configuration started" << std::endl;
55 
56  std::string interfaces = par("interfaces").stringValue();
57  IPv4Address addressBase = IPv4Address(par("addressBase").stringValue());
58  IPv4Address netmask = IPv4Address(par("netmask").stringValue());
59  std::string mcastGroups = par("mcastGroups").stringValue();
60 
61  // get our host module
62  cModule *host = getContainingNode(this);
63 
64  IPv4Address myAddress = IPv4Address(addressBase.getInt() + uint32(host->getId()));
65 
66  // address test
67  if (!IPv4Address::maskedAddrAreEqual(myAddress, addressBase, netmask))
68  throw cRuntimeError("Generated IP address is out of specified address range");
69 
70  // get our routing table
71  IIPv4RoutingTable *routingTable = L3AddressResolver().routingTableOf(host);
72  if (!routingTable)
73  throw cRuntimeError("No routing table found");
74 
75  // get our interface table
76  IInterfaceTable *ift = L3AddressResolver().interfaceTableOf(host);
77  if (!ift)
78  throw cRuntimeError("No interface table found");
79 
80  // look at all interface table entries
81  cStringTokenizer interfaceTokenizer(interfaces.c_str());
82  const char *ifname;
83  while ((ifname = interfaceTokenizer.nextToken()) != nullptr) {
84  InterfaceEntry *ie = ift->getInterfaceByName(ifname);
85  if (!ie)
86  throw cRuntimeError("No such interface '%s'", ifname);
87 
88  // assign IP Address to all connected interfaces
89  if (ie->isLoopback()) {
90  EV_INFO << "interface " << ifname << " skipped (is loopback)" << std::endl;
91  continue;
92  }
93 
94  EV_INFO << "interface " << ifname << " gets " << myAddress.str() << "/" << netmask.str() << std::endl;
95 
96  ie->ipv4Data()->setIPAddress(myAddress);
97  ie->ipv4Data()->setNetmask(netmask);
98  ie->setBroadcast(true);
99 
100  // associate interface with default multicast groups
101  ie->ipv4Data()->joinMulticastGroup(IPv4Address::ALL_HOSTS_MCAST);
102  ie->ipv4Data()->joinMulticastGroup(IPv4Address::ALL_ROUTERS_MCAST);
103 
104  // associate interface with specified multicast groups
105  cStringTokenizer interfaceTokenizer(mcastGroups.c_str());
106  const char *mcastGroup_s;
107  while ((mcastGroup_s = interfaceTokenizer.nextToken()) != nullptr) {
108  IPv4Address mcastGroup(mcastGroup_s);
109  ie->ipv4Data()->joinMulticastGroup(mcastGroup);
110  }
111  }
112 }
static bool maskedAddrAreEqual(const IPv4Address &addr1, const IPv4Address &addr2, const IPv4Address &netmask)
Test if the masked addresses (ie the mask is applied to addr1 and addr2) are equal.
Definition: IPv4Address.cc:260
static const IPv4Address ALL_HOSTS_MCAST
224.0.0.1 All hosts on a subnet
Definition: IPv4Address.h:107
cModule * getContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:65
uint32_t uint32
Definition: Compat.h:30
#define stringValue()
Definition: NedFunctions.cc:24
static const IPv4Address ALL_ROUTERS_MCAST
224.0.0.2 All routers on a subnet
Definition: IPv4Address.h:108

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