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

Parses a routing table file into a routing table. More...

#include <RoutingTableParser.h>

Public Member Functions

 RoutingTableParser (IInterfaceTable *ift, IIpv4RoutingTable *rt)
 Constructor. More...
 
virtual ~RoutingTableParser ()
 Destructor. More...
 
virtual int readRoutingTableFromFile (const char *filename)
 Read Routing Table file; return 0 on success, -1 on error. More...
 

Protected Member Functions

virtual char * createFilteredFile (char *file, int &charpointer, const char *endtoken)
 
virtual void parseInterfaces (char *ifconfigFile)
 
virtual void parseRouting (char *routeFile)
 
virtual char * parseEntry (char *ifconfigFile, const char *tokenStr, int &charpointer, char *destStr)
 
virtual void parseMulticastGroups (char *groupStr, NetworkInterface *)
 

Static Protected Member Functions

static int streq (const char *str1, const char *str2)
 
static void skipBlanks (char *str, int &charptr)
 
static int strcpyword (char *dest, const char *src)
 

Protected Attributes

IInterfaceTableift
 
IIpv4RoutingTablert
 

Detailed Description

Parses a routing table file into a routing table.

Constructor & Destructor Documentation

◆ RoutingTableParser()

inet::RoutingTableParser::RoutingTableParser ( IInterfaceTable ift,
IIpv4RoutingTable rt 
)
inline

Constructor.

37 : ift(ift), rt(rt) {}

◆ ~RoutingTableParser()

virtual inet::RoutingTableParser::~RoutingTableParser ( )
inlinevirtual

Destructor.

42 {}

Member Function Documentation

◆ createFilteredFile()

char * inet::RoutingTableParser::createFilteredFile ( char *  file,
int &  charpointer,
const char *  endtoken 
)
protectedvirtual
117 {
118  int i = 0;
119  char *filterFile = new char[MAX_FILESIZE];
120  filterFile[0] = '\0';
121 
122  while (true) {
123  // skip blank lines and comments
124  while (!isalnum(file[charpointer]) && !isspace(file[charpointer])) {
125  while (file[charpointer++] != '\n')
126  ;
127  }
128 
129  // check for endtoken:
130  if (streq(file + charpointer, endtoken)) {
131  filterFile[i] = '\0';
132  break;
133  }
134 
135  // copy whole line to filterFile
136  while ((filterFile[i++] = file[charpointer++]) != '\n')
137  ;
138  }
139 
140  return filterFile;
141 }

Referenced by readRoutingTableFromFile().

◆ parseEntry()

char * inet::RoutingTableParser::parseEntry ( char *  ifconfigFile,
const char *  tokenStr,
int &  charpointer,
char *  destStr 
)
protectedvirtual
269 {
270  int temp = 0;
271 
272  charpointer += strlen(tokenStr);
273  skipBlanks(ifconfigFile, charpointer);
274  temp = strcpyword(destStr, ifconfigFile + charpointer);
275  charpointer += temp;
276 
277  skipBlanks(ifconfigFile, charpointer);
278 
279  return destStr;
280 }

Referenced by parseInterfaces().

◆ parseInterfaces()

void inet::RoutingTableParser::parseInterfaces ( char *  ifconfigFile)
protectedvirtual
144 {
145  char buf[MAX_ENTRY_STRING_SIZE];
146  int charpointer = 0;
147  NetworkInterface *ie = nullptr;
148 
149  // parsing of entries in interface definition
150  while (ifconfigFile[charpointer] != '\0') {
151  // name entry
152  if (streq(ifconfigFile + charpointer, "name:")) {
153  // find existing interface with this name
154  char *name = parseEntry(ifconfigFile, "name:", charpointer, buf);
155  ie = ift->findInterfaceByName(name);
156  if (!ie)
157  throw cRuntimeError("Error in routing file: interface name `%s' not registered by any L2 module", name);
158  if (!ie->findProtocolData<Ipv4InterfaceData>())
159  throw cRuntimeError("Error in routing file: interface name `%s' doesn't have Ipv4 data fields", name);
160 
161  continue;
162  }
163 
164  // encap entry
165  if (streq(ifconfigFile + charpointer, "encap:")) {
166  if (!ie)
167  throw cRuntimeError("Error in routing file: missing the `name:' entry");
168  // ignore encap
169  parseEntry(ifconfigFile, "encap:", charpointer, buf);
170  continue;
171  }
172 
173  // HWaddr entry
174  if (streq(ifconfigFile + charpointer, "HWaddr:")) {
175  if (!ie)
176  throw cRuntimeError("Error in routing file: missing the `name:' entry");
177  // ignore hwAddr
178  parseEntry(ifconfigFile, "HWaddr:", charpointer, buf);
179  continue;
180  }
181 
182  // inet_addr entry
183  if (streq(ifconfigFile + charpointer, "inet_addr:")) {
184  if (!ie)
185  throw cRuntimeError("Error in routing file: missing the `name:' entry");
186  ie->getProtocolDataForUpdate<Ipv4InterfaceData>()->setIPAddress(Ipv4Address(parseEntry(ifconfigFile, "inet_addr:", charpointer, buf)));
187  continue;
188  }
189 
190  // Broadcast address entry
191  if (streq(ifconfigFile + charpointer, "Bcast:")) {
192  if (!ie)
193  throw cRuntimeError("Error in routing file: missing the `name:' entry");
194  // ignore Bcast
195  parseEntry(ifconfigFile, "Bcast:", charpointer, buf);
196  continue;
197  }
198 
199  // Mask entry
200  if (streq(ifconfigFile + charpointer, "Mask:")) {
201  if (!ie)
202  throw cRuntimeError("Error in routing file: missing the `name:' entry");
203  ie->getProtocolDataForUpdate<Ipv4InterfaceData>()->setNetmask(Ipv4Address(parseEntry(ifconfigFile, "Mask:", charpointer, buf)));
204  continue;
205  }
206 
207  // Multicast groups entry
208  if (streq(ifconfigFile + charpointer, "Groups:")) {
209  if (!ie)
210  throw cRuntimeError("Error in routing file: missing the `name:' entry");
211  char *grStr = parseEntry(ifconfigFile, "Groups:", charpointer, buf);
212  parseMulticastGroups(grStr, ie);
213  continue;
214  }
215 
216  // MTU entry
217  if (streq(ifconfigFile + charpointer, "MTU:")) {
218  if (!ie)
219  throw cRuntimeError("Error in routing file: missing the `name:' entry");
220  ie->setMtu(atoi(parseEntry(ifconfigFile, "MTU:", charpointer, buf)));
221  continue;
222  }
223 
224  // Metric entry
225  if (streq(ifconfigFile + charpointer, "Metric:")) {
226  if (!ie)
227  throw cRuntimeError("Error in routing file: missing the `name:' entry");
228  ie->getProtocolDataForUpdate<Ipv4InterfaceData>()->setMetric(atoi(parseEntry(ifconfigFile, "Metric:", charpointer, buf)));
229  continue;
230  }
231 
232  // BROADCAST Flag
233  if (streq(ifconfigFile + charpointer, "BROADCAST")) {
234  if (!ie)
235  throw cRuntimeError("Error in routing file: missing the `name:' entry");
236  ie->setBroadcast(true);
237  charpointer += strlen("BROADCAST");
238  skipBlanks(ifconfigFile, charpointer);
239  continue;
240  }
241 
242  // MULTICAST Flag
243  if (streq(ifconfigFile + charpointer, "MULTICAST")) {
244  if (!ie)
245  throw cRuntimeError("Error in routing file: missing the `name:' entry");
246  ie->setMulticast(true);
247  charpointer += strlen("MULTICAST");
248  skipBlanks(ifconfigFile, charpointer);
249  continue;
250  }
251 
252  // POINTTOPOINT Flag
253  if (streq(ifconfigFile + charpointer, "POINTTOPOINT")) {
254  if (!ie)
255  throw cRuntimeError("Error in routing file: missing the `name:' entry");
256  ie->setPointToPoint(true);
257  charpointer += strlen("POINTTOPOINT");
258  skipBlanks(ifconfigFile, charpointer);
259  continue;
260  }
261 
262  // no entry discovered: move charpointer on
263  charpointer++;
264  }
265 }

Referenced by readRoutingTableFromFile().

◆ parseMulticastGroups()

void inet::RoutingTableParser::parseMulticastGroups ( char *  groupStr,
NetworkInterface itf 
)
protectedvirtual
283 {
284  // Parse string (Ipv4 addresses separated by colons)
285  cStringTokenizer tokenizer(groupStr, ":");
286  const char *token;
287  while ((token = tokenizer.nextToken()) != nullptr)
288  itf->getProtocolDataForUpdate<Ipv4InterfaceData>()->joinMulticastGroup(Ipv4Address(token));
289 }

Referenced by parseInterfaces().

◆ parseRouting()

void inet::RoutingTableParser::parseRouting ( char *  routeFile)
protectedvirtual
292 {
293  char *str = new char[MAX_ENTRY_STRING_SIZE];
294 
295  int pos = strlen(ROUTE_START_TOKEN);
296  skipBlanks(routeFile, pos);
297  while (routeFile[pos] != '\0') {
298  // 1st entry: Host
299  pos += strcpyword(str, routeFile + pos);
300  skipBlanks(routeFile, pos);
301  Ipv4Route *e = new Ipv4Route();
302  if (strcmp(str, "default:")) {
303  // if entry is not the default entry
304  if (!Ipv4Address::isWellFormed(str))
305  throw cRuntimeError("Syntax error in routing file: `%s' on 1st column should be `default:' or a valid Ipv4 address", str);
306 
307  e->setDestination(Ipv4Address(str));
308  }
309 
310  // 2nd entry: Gateway
311  pos += strcpyword(str, routeFile + pos);
312  skipBlanks(routeFile, pos);
313  if (!strcmp(str, "*") || !strcmp(str, "0.0.0.0")) {
314  e->setGateway(Ipv4Address::UNSPECIFIED_ADDRESS);
315  }
316  else {
317  if (!Ipv4Address::isWellFormed(str))
318  throw cRuntimeError("Syntax error in routing file: `%s' on 2nd column should be `*' or a valid Ipv4 address", str);
319 
320  e->setGateway(Ipv4Address(str));
321  }
322 
323  // 3rd entry: Netmask
324  pos += strcpyword(str, routeFile + pos);
325  skipBlanks(routeFile, pos);
326  if (!Ipv4Address::isWellFormed(str))
327  throw cRuntimeError("Syntax error in routing file: `%s' on 3rd column should be a valid Ipv4 address", str);
328 
329  e->setNetmask(Ipv4Address(str));
330 
331  // 4th entry: flags
332  pos += strcpyword(str, routeFile + pos);
333  skipBlanks(routeFile, pos);
334  // parse flag-String to set flags
335  for (int i = 0; str[i]; i++) {
336  if (str[i] == 'H') {
337 // e->setType(Ipv4Route::DIRECT);
338  }
339  else if (str[i] == 'G') {
340 // e->setType(Ipv4Route::REMOTE);
341  }
342  else {
343  throw cRuntimeError("Syntax error in routing file: 4th column should be `G' or `H' not `%s'", str);
344  }
345  }
346 
347  // 5th entry: metric
348  pos += strcpyword(str, routeFile + pos);
349  skipBlanks(routeFile, pos);
350  int metric = atoi(str);
351  if (metric == 0 && str[0] != '0')
352  throw cRuntimeError("Syntax error in routing file: 5th column should be numeric not `%s'", str);
353 
354  e->setMetric(metric);
355 
356  // 6th entry: interface
357  opp_string interfaceName;
358  interfaceName.reserve(MAX_ENTRY_STRING_SIZE);
359  pos += strcpyword(interfaceName.buffer(), routeFile + pos);
360  skipBlanks(routeFile, pos);
361  NetworkInterface *ie = ift->findInterfaceByName(interfaceName.c_str());
362  if (!ie)
363  throw cRuntimeError("Syntax error in routing file: 6th column: `%s' is not an existing interface", interfaceName.c_str());
364 
365  e->setInterface(ie);
366  e->setSourceType(IRoute::MANUAL);
367 
368  // add entry
369  rt->addRoute(e);
370  }
371  delete[] str;
372 }

Referenced by readRoutingTableFromFile().

◆ readRoutingTableFromFile()

int inet::RoutingTableParser::readRoutingTableFromFile ( const char *  filename)
virtual

Read Routing Table file; return 0 on success, -1 on error.

57 {
58  FILE *fp = fopen(filename, "r");
59  if (fp == nullptr)
60  throw cRuntimeError("Error opening routing table file `%s'", filename);
61 
62  int charpointer;
63  char *file = new char[MAX_FILESIZE];
64  char *ifconfigFile = nullptr;
65  char *routeFile = nullptr;
66  int c;
67 
68  // read the whole into the file[] char-array
69  for (charpointer = 0; (c = getc(fp)) != EOF; charpointer++)
70  file[charpointer] = c;
71 
72  for (; charpointer < MAX_FILESIZE; charpointer++)
73  file[charpointer] = '\0';
74 
75  fclose(fp);
76 
77  // copy file into specialized, filtered char arrays
78  for (charpointer = 0;
79  (charpointer < MAX_FILESIZE) && (file[charpointer] != '\0');
80  charpointer++)
81  {
82  // check for tokens at beginning of file or line
83  if (charpointer == 0 || file[charpointer - 1] == '\n') {
84  // copy into ifconfig filtered chararray
85  if (streq(file + charpointer, IFCONFIG_START_TOKEN)) {
86  ifconfigFile = createFilteredFile(file,
87  charpointer,
89 // PRINTF("Filtered File 1 created:\n%s\n", ifconfigFile);
90  }
91 
92  // copy into route filtered chararray
93  if (streq(file + charpointer, ROUTE_START_TOKEN)) {
94  routeFile = createFilteredFile(file,
95  charpointer,
97 // PRINTF("Filtered File 2 created:\n%s\n", routeFile);
98  }
99  }
100  }
101 
102  delete[] file;
103 
104  // parse filtered files
105  if (ifconfigFile)
106  parseInterfaces(ifconfigFile);
107  if (routeFile)
108  parseRouting(routeFile);
109 
110  delete[] ifconfigFile;
111  delete[] routeFile;
112 
113  return 0;
114 }

Referenced by inet::Ipv4RoutingTable::handleOperationStage(), and inet::Ipv4RoutingTable::initialize().

◆ skipBlanks()

void inet::RoutingTableParser::skipBlanks ( char *  str,
int &  charptr 
)
staticprotected
51 {
52  for (; isspace(str[charptr]); charptr++)
53  ;
54 }

Referenced by parseEntry(), parseInterfaces(), and parseRouting().

◆ strcpyword()

int inet::RoutingTableParser::strcpyword ( char *  dest,
const char *  src 
)
staticprotected
42 {
43  int i;
44  for (i = 0; !isspace(dest[i] = src[i]); i++)
45  ;
46  dest[i] = '\0';
47  return i;
48 }

Referenced by parseEntry(), and parseRouting().

◆ streq()

int inet::RoutingTableParser::streq ( const char *  str1,
const char *  str2 
)
staticprotected
37 {
38  return strncmp(str1, str2, strlen(str2)) == 0;
39 }

Referenced by createFilteredFile(), parseInterfaces(), and readRoutingTableFromFile().

Member Data Documentation

◆ ift

IInterfaceTable* inet::RoutingTableParser::ift
protected

Referenced by parseInterfaces(), and parseRouting().

◆ rt

IIpv4RoutingTable* inet::RoutingTableParser::rt
protected

Referenced by parseRouting().


The documentation for this class was generated from the following files:
inet::RoutingTableParser::parseInterfaces
virtual void parseInterfaces(char *ifconfigFile)
Definition: RoutingTableParser.cc:143
inet::RoutingTableParser::ift
IInterfaceTable * ift
Definition: RoutingTableParser.h:30
inet::units::constants::c
const value< double, compose< units::m, pow< units::s, -1 > > > c(299792458)
inet::IFCONFIG_START_TOKEN
const char * IFCONFIG_START_TOKEN
Definition: RoutingTableParser.cc:31
inet::IRoute::MANUAL
@ MANUAL
manually added static route
Definition: IRoute.h:29
inet::MAX_ENTRY_STRING_SIZE
const int MAX_ENTRY_STRING_SIZE
Definition: RoutingTableParser.cc:26
inet::RoutingTableParser::streq
static int streq(const char *str1, const char *str2)
Definition: RoutingTableParser.cc:36
inet::units::constants::e
const value< double, units::C > e(1.602176487e-19)
inet::ROUTE_START_TOKEN
const char * ROUTE_START_TOKEN
Definition: RoutingTableParser.cc:33
inet::RoutingTableParser::rt
IIpv4RoutingTable * rt
Definition: RoutingTableParser.h:31
inet::IInterfaceTable::findInterfaceByName
virtual NetworkInterface * findInterfaceByName(const char *name) const =0
Returns an interface given by its name.
inet::RoutingTableParser::parseMulticastGroups
virtual void parseMulticastGroups(char *groupStr, NetworkInterface *)
Definition: RoutingTableParser.cc:282
inet::MAX_FILESIZE
const int MAX_FILESIZE
Definition: RoutingTableParser.cc:25
inet::RoutingTableParser::strcpyword
static int strcpyword(char *dest, const char *src)
Definition: RoutingTableParser.cc:41
inet::IIpv4RoutingTable::addRoute
virtual void addRoute(Ipv4Route *entry)=0
Adds a route to the routing table.
inet::RoutingTableParser::parseEntry
virtual char * parseEntry(char *ifconfigFile, const char *tokenStr, int &charpointer, char *destStr)
Definition: RoutingTableParser.cc:267
inet::Ipv4Address::UNSPECIFIED_ADDRESS
static const Ipv4Address UNSPECIFIED_ADDRESS
0.0.0.0
Definition: Ipv4Address.h:91
inet::IFCONFIG_END_TOKEN
const char * IFCONFIG_END_TOKEN
Definition: RoutingTableParser.cc:32
inet::ROUTE_END_TOKEN
const char * ROUTE_END_TOKEN
Definition: RoutingTableParser.cc:34
inet::RoutingTableParser::createFilteredFile
virtual char * createFilteredFile(char *file, int &charpointer, const char *endtoken)
Definition: RoutingTableParser.cc:116
inet::Ipv4Address::isWellFormed
static bool isWellFormed(const char *text)
Returns true if the format of the string corresponds to an Ipv4 address with the dotted notation ("19...
Definition: Ipv4Address.cc:258
inet::RoutingTableParser::parseRouting
virtual void parseRouting(char *routeFile)
Definition: RoutingTableParser.cc:291
inet::RoutingTableParser::skipBlanks
static void skipBlanks(char *str, int &charptr)
Definition: RoutingTableParser.cc:50