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

Configures IPv4 addresses and routing tables for a "flat" network, "flat" meaning that all hosts and routers will have the same network address. More...

#include <FlatNetworkConfigurator.h>

Inheritance diagram for inet::FlatNetworkConfigurator:

Classes

class  NodeInfo
 

Protected Types

typedef std::vector< NodeInfoNodeInfoVector
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void extractTopology (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void assignAddresses (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void addDefaultRoutes (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void fillRoutingTables (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void setDisplayString (cTopology &topo, NodeInfoVector &nodeInfo)
 

Detailed Description

Configures IPv4 addresses and routing tables for a "flat" network, "flat" meaning that all hosts and routers will have the same network address.

For more info please see the NED file.

Member Typedef Documentation

Member Function Documentation

void inet::FlatNetworkConfigurator::addDefaultRoutes ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual

Referenced by initialize().

110 {
111  // add default route to nodes with exactly one (non-loopback) interface
112  for (int i = 0; i < topo.getNumNodes(); i++) {
113  cTopology::Node *node = topo.getNode(i);
114 
115  // skip bus types
116  if (!nodeInfo[i].isIPNode)
117  continue;
118 
119  IInterfaceTable *ift = nodeInfo[i].ift;
120  IIPv4RoutingTable *rt = nodeInfo[i].rt;
121 
122  // count non-loopback interfaces
123  int numIntf = 0;
124  InterfaceEntry *ie = nullptr;
125  for (int k = 0; k < ift->getNumInterfaces(); k++)
126  if (!ift->getInterface(k)->isLoopback()) {
127  ie = ift->getInterface(k);
128  numIntf++;
129  }
130 
131  nodeInfo[i].usesDefaultRoute = (numIntf == 1);
132  if (numIntf != 1)
133  continue; // only deal with nodes with one interface plus loopback
134 
135  EV_INFO << " " << node->getModule()->getFullName() << "=" << nodeInfo[i].address
136  << " has only one (non-loopback) interface, adding default route\n";
137 
138  // add route
139  IPv4Route *e = new IPv4Route();
140  e->setDestination(IPv4Address());
141  e->setNetmask(IPv4Address());
142  e->setInterface(ie);
143  e->setSourceType(IRoute::MANUAL);
144  //e->getMetric() = 1;
145  rt->addRoute(e);
146  }
147 }
const value< double, units::C > e(1.602176487e-19)
manually added static route
Definition: IRoute.h:40
const double k
Definition: QAM16Modulation.cc:24
void inet::FlatNetworkConfigurator::assignAddresses ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual

Referenced by initialize().

80 {
81  // assign IPv4 addresses
82  uint32 networkAddress = IPv4Address(par("networkAddress").stringValue()).getInt();
83  uint32 netmask = IPv4Address(par("netmask").stringValue()).getInt();
84  int maxNodes = (~netmask) - 1; // 0 and ffff have special meaning and cannot be used
85  if (topo.getNumNodes() > maxNodes)
86  throw cRuntimeError("netmask too large, not enough addresses for all %d nodes", topo.getNumNodes());
87 
88  int numIPNodes = 0;
89  for (int i = 0; i < topo.getNumNodes(); i++) {
90  // skip bus types
91  if (!nodeInfo[i].isIPNode)
92  continue;
93 
94  uint32 addr = networkAddress | uint32(++numIPNodes);
95  nodeInfo[i].address.set(addr);
96 
97  // find interface table and assign address to all (non-loopback) interfaces
98  IInterfaceTable *ift = nodeInfo[i].ift;
99  for (int k = 0; k < ift->getNumInterfaces(); k++) {
100  InterfaceEntry *ie = ift->getInterface(k);
101  if (!ie->isLoopback()) {
102  ie->ipv4Data()->setIPAddress(IPv4Address(addr));
103  ie->ipv4Data()->setNetmask(IPv4Address::ALLONES_ADDRESS); // full address must match for local delivery
104  }
105  }
106  }
107 }
uint32_t uint32
Definition: Compat.h:30
#define stringValue()
Definition: NedFunctions.cc:24
static const IPv4Address ALLONES_ADDRESS
255.255.255.255
Definition: IPv4Address.h:105
const double k
Definition: QAM16Modulation.cc:24
void inet::FlatNetworkConfigurator::extractTopology ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual

Referenced by initialize().

60 {
61  // extract topology
62  topo.extractByProperty("networkNode");
63  EV_DEBUG << "cTopology found " << topo.getNumNodes() << " nodes\n";
64 
65  // fill in isIPNode, ift and rt members in nodeInfo[]
66  nodeInfo.resize(topo.getNumNodes());
67  for (int i = 0; i < topo.getNumNodes(); i++) {
68  cModule *mod = topo.getNode(i)->getModule();
69  nodeInfo[i].isIPNode = L3AddressResolver().findIPv4RoutingTableOf(mod) != nullptr && L3AddressResolver().findInterfaceTableOf(mod) != nullptr;
70  if (nodeInfo[i].isIPNode) {
71  nodeInfo[i].ift = L3AddressResolver().interfaceTableOf(mod);
72  nodeInfo[i].rt = L3AddressResolver().routingTableOf(mod);
73  nodeInfo[i].ipForwardEnabled = mod->hasPar("forwarding") ? mod->par("forwarding").boolValue() : false;
74  topo.getNode(i)->setWeight(nodeInfo[i].ipForwardEnabled ? 0.0 : INFINITY);
75  }
76  }
77 }
#define INFINITY
Definition: Topology.h:29
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:108
void inet::FlatNetworkConfigurator::fillRoutingTables ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual

Referenced by initialize().

150 {
151  // fill in routing tables with static routes
152  for (int i = 0; i < topo.getNumNodes(); i++) {
153  cTopology::Node *destNode = topo.getNode(i);
154 
155  // skip bus types
156  if (!nodeInfo[i].isIPNode)
157  continue;
158 
159  IPv4Address destAddr = nodeInfo[i].address;
160  std::string destModName = destNode->getModule()->getFullName();
161 
162  // calculate shortest paths from everywhere towards destNode
163  topo.calculateWeightedSingleShortestPathsTo(destNode);
164 
165  // add route (with host=destNode) to every routing table in the network
166  // (excepting nodes with only one interface -- there we'll set up a default route)
167  for (int j = 0; j < topo.getNumNodes(); j++) {
168  if (i == j)
169  continue;
170  if (!nodeInfo[j].isIPNode)
171  continue;
172 
173  cTopology::Node *atNode = topo.getNode(j);
174  if (atNode->getNumPaths() == 0)
175  continue; // not connected
176  if (nodeInfo[j].usesDefaultRoute)
177  continue; // already added default route here
178 
179  IPv4Address atAddr = nodeInfo[j].address;
180 
181  IInterfaceTable *ift = nodeInfo[j].ift;
182 
183  int outputGateId = atNode->getPath(0)->getLocalGate()->getId();
184  InterfaceEntry *ie = ift->getInterfaceByNodeOutputGateId(outputGateId);
185  if (!ie)
186  throw cRuntimeError("%s has no interface for output gate id %d", ift->getFullPath().c_str(), outputGateId);
187 
188  EV_INFO << " from " << atNode->getModule()->getFullName() << "=" << IPv4Address(atAddr);
189  EV_INFO << " towards " << destModName << "=" << IPv4Address(destAddr) << " interface " << ie->getName() << endl;
190 
191  // add route
192  IIPv4RoutingTable *rt = nodeInfo[j].rt;
193  IPv4Route *e = new IPv4Route();
194  e->setDestination(destAddr);
195  e->setNetmask(IPv4Address(255, 255, 255, 255)); // full match needed
196  e->setInterface(ie);
197  e->setSourceType(IRoute::MANUAL);
198  //e->getMetric() = 1;
199  rt->addRoute(e);
200  }
201  }
202 }
const value< double, units::C > e(1.602176487e-19)
manually added static route
Definition: IRoute.h:40
void inet::FlatNetworkConfigurator::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
205 {
206  throw cRuntimeError("this module doesn't handle messages, it runs only in initialize()");
207 }
void inet::FlatNetworkConfigurator::initialize ( int  stage)
overrideprotectedvirtual
33 {
34  cSimpleModule::initialize(stage);
35 
36  if (stage == INITSTAGE_NETWORK_LAYER_2) {
37  cTopology topo("topo");
38  NodeInfoVector nodeInfo; // will be of size topo.nodes[]
39 
40  // extract topology into the cTopology object, then fill in
41  // isIPNode, rt and ift members of nodeInfo[]
42  extractTopology(topo, nodeInfo);
43 
44  // assign addresses to IPv4 nodes, and also store result in nodeInfo[].address
45  assignAddresses(topo, nodeInfo);
46 
47  // add default routes to hosts (nodes with a single attachment);
48  // also remember result in nodeInfo[].usesDefaultRoute
49  addDefaultRoutes(topo, nodeInfo);
50 
51  // calculate shortest paths, and add corresponding static routes
52  fillRoutingTables(topo, nodeInfo);
53 
54  // update display string
55  setDisplayString(topo, nodeInfo);
56  }
57 }
virtual void assignAddresses(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: FlatNetworkConfigurator.cc:79
std::vector< NodeInfo > NodeInfoVector
Definition: FlatNetworkConfigurator.h:50
virtual void addDefaultRoutes(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: FlatNetworkConfigurator.cc:109
virtual void extractTopology(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: FlatNetworkConfigurator.cc:59
virtual void fillRoutingTables(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: FlatNetworkConfigurator.cc:149
Initialization of network-layer protocols, stage 2.
Definition: InitStages.h:78
virtual void setDisplayString(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: FlatNetworkConfigurator.cc:209
virtual int inet::FlatNetworkConfigurator::numInitStages ( ) const
inlineoverrideprotectedvirtual
53 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::FlatNetworkConfigurator::setDisplayString ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual

Referenced by initialize().

210 {
211  int numIPNodes = 0;
212  for (int i = 0; i < topo.getNumNodes(); i++)
213  if (nodeInfo[i].isIPNode)
214  numIPNodes++;
215 
216 
217  // update display string
218  char buf[80];
219  sprintf(buf, "%d IPv4 nodes\n%d non-IPv4 nodes", numIPNodes, topo.getNumNodes() - numIPNodes);
220  getDisplayString().setTagArg("t", 0, buf);
221 }

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