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, InterfaceEntry *)
 

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

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

Constructor.

50 : ift(ift), rt(rt) {}
IIPv4RoutingTable * rt
Definition: RoutingTableParser.h:44
IInterfaceTable * ift
Definition: RoutingTableParser.h:43
virtual inet::RoutingTableParser::~RoutingTableParser ( )
inlinevirtual

Destructor.

55 {};

Member Function Documentation

char * inet::RoutingTableParser::createFilteredFile ( char *  file,
int &  charpointer,
const char *  endtoken 
)
protectedvirtual

Referenced by readRoutingTableFromFile().

129 {
130  int i = 0;
131  char *filterFile = new char[MAX_FILESIZE];
132  filterFile[0] = '\0';
133 
134  while (true) {
135  // skip blank lines and comments
136  while (!isalnum(file[charpointer]) && !isspace(file[charpointer])) {
137  while (file[charpointer++] != '\n')
138  ;
139  }
140 
141  // check for endtoken:
142  if (streq(file + charpointer, endtoken)) {
143  filterFile[i] = '\0';
144  break;
145  }
146 
147  // copy whole line to filterFile
148  while ((filterFile[i++] = file[charpointer++]) != '\n')
149  ;
150  }
151 
152  return filterFile;
153 }
static int streq(const char *str1, const char *str2)
Definition: RoutingTableParser.cc:47
const int MAX_FILESIZE
Definition: RoutingTableParser.cc:36
char * inet::RoutingTableParser::parseEntry ( char *  ifconfigFile,
const char *  tokenStr,
int &  charpointer,
char *  destStr 
)
protectedvirtual

Referenced by parseInterfaces().

281 {
282  int temp = 0;
283 
284  charpointer += strlen(tokenStr);
285  skipBlanks(ifconfigFile, charpointer);
286  temp = strcpyword(destStr, ifconfigFile + charpointer);
287  charpointer += temp;
288 
289  skipBlanks(ifconfigFile, charpointer);
290 
291  return destStr;
292 }
static int strcpyword(char *dest, const char *src)
Definition: RoutingTableParser.cc:52
static void skipBlanks(char *str, int &charptr)
Definition: RoutingTableParser.cc:61
void inet::RoutingTableParser::parseInterfaces ( char *  ifconfigFile)
protectedvirtual

Referenced by readRoutingTableFromFile().

156 {
157  char buf[MAX_ENTRY_STRING_SIZE];
158  int charpointer = 0;
159  InterfaceEntry *ie = nullptr;
160 
161  // parsing of entries in interface definition
162  while (ifconfigFile[charpointer] != '\0') {
163  // name entry
164  if (streq(ifconfigFile + charpointer, "name:")) {
165  // find existing interface with this name
166  char *name = parseEntry(ifconfigFile, "name:", charpointer, buf);
167  ie = ift->getInterfaceByName(name);
168  if (!ie)
169  throw cRuntimeError("Error in routing file: interface name `%s' not registered by any L2 module", name);
170  if (!ie->ipv4Data())
171  throw cRuntimeError("Error in routing file: interface name `%s' doesn't have IPv4 data fields", name);
172 
173  continue;
174  }
175 
176  // encap entry
177  if (streq(ifconfigFile + charpointer, "encap:")) {
178  if (!ie)
179  throw cRuntimeError("Error in routing file: missing the `name:' entry");
180  // ignore encap
181  parseEntry(ifconfigFile, "encap:", charpointer, buf);
182  continue;
183  }
184 
185  // HWaddr entry
186  if (streq(ifconfigFile + charpointer, "HWaddr:")) {
187  if (!ie)
188  throw cRuntimeError("Error in routing file: missing the `name:' entry");
189  // ignore hwAddr
190  parseEntry(ifconfigFile, "HWaddr:", charpointer, buf);
191  continue;
192  }
193 
194  // inet_addr entry
195  if (streq(ifconfigFile + charpointer, "inet_addr:")) {
196  if (!ie)
197  throw cRuntimeError("Error in routing file: missing the `name:' entry");
198  ie->ipv4Data()->setIPAddress(IPv4Address(parseEntry(ifconfigFile, "inet_addr:", charpointer, buf)));
199  continue;
200  }
201 
202  // Broadcast address entry
203  if (streq(ifconfigFile + charpointer, "Bcast:")) {
204  if (!ie)
205  throw cRuntimeError("Error in routing file: missing the `name:' entry");
206  // ignore Bcast
207  parseEntry(ifconfigFile, "Bcast:", charpointer, buf);
208  continue;
209  }
210 
211  // Mask entry
212  if (streq(ifconfigFile + charpointer, "Mask:")) {
213  if (!ie)
214  throw cRuntimeError("Error in routing file: missing the `name:' entry");
215  ie->ipv4Data()->setNetmask(IPv4Address(parseEntry(ifconfigFile, "Mask:", charpointer, buf)));
216  continue;
217  }
218 
219  // Multicast groups entry
220  if (streq(ifconfigFile + charpointer, "Groups:")) {
221  if (!ie)
222  throw cRuntimeError("Error in routing file: missing the `name:' entry");
223  char *grStr = parseEntry(ifconfigFile, "Groups:", charpointer, buf);
224  parseMulticastGroups(grStr, ie);
225  continue;
226  }
227 
228  // MTU entry
229  if (streq(ifconfigFile + charpointer, "MTU:")) {
230  if (!ie)
231  throw cRuntimeError("Error in routing file: missing the `name:' entry");
232  ie->setMtu(atoi(parseEntry(ifconfigFile, "MTU:", charpointer, buf)));
233  continue;
234  }
235 
236  // Metric entry
237  if (streq(ifconfigFile + charpointer, "Metric:")) {
238  if (!ie)
239  throw cRuntimeError("Error in routing file: missing the `name:' entry");
240  ie->ipv4Data()->setMetric(atoi(parseEntry(ifconfigFile, "Metric:", charpointer, buf)));
241  continue;
242  }
243 
244  // BROADCAST Flag
245  if (streq(ifconfigFile + charpointer, "BROADCAST")) {
246  if (!ie)
247  throw cRuntimeError("Error in routing file: missing the `name:' entry");
248  ie->setBroadcast(true);
249  charpointer += strlen("BROADCAST");
250  skipBlanks(ifconfigFile, charpointer);
251  continue;
252  }
253 
254  // MULTICAST Flag
255  if (streq(ifconfigFile + charpointer, "MULTICAST")) {
256  if (!ie)
257  throw cRuntimeError("Error in routing file: missing the `name:' entry");
258  ie->setMulticast(true);
259  charpointer += strlen("MULTICAST");
260  skipBlanks(ifconfigFile, charpointer);
261  continue;
262  }
263 
264  // POINTTOPOINT Flag
265  if (streq(ifconfigFile + charpointer, "POINTTOPOINT")) {
266  if (!ie)
267  throw cRuntimeError("Error in routing file: missing the `name:' entry");
268  ie->setPointToPoint(true);
269  charpointer += strlen("POINTTOPOINT");
270  skipBlanks(ifconfigFile, charpointer);
271  continue;
272  }
273 
274  // no entry discovered: move charpointer on
275  charpointer++;
276  }
277 }
virtual void parseMulticastGroups(char *groupStr, InterfaceEntry *)
Definition: RoutingTableParser.cc:294
static int streq(const char *str1, const char *str2)
Definition: RoutingTableParser.cc:47
virtual InterfaceEntry * getInterfaceByName(const char *name) const =0
Returns an interface given by its name.
virtual char * parseEntry(char *ifconfigFile, const char *tokenStr, int &charpointer, char *destStr)
Definition: RoutingTableParser.cc:279
IInterfaceTable * ift
Definition: RoutingTableParser.h:43
const int MAX_ENTRY_STRING_SIZE
Definition: RoutingTableParser.cc:37
static void skipBlanks(char *str, int &charptr)
Definition: RoutingTableParser.cc:61
void inet::RoutingTableParser::parseMulticastGroups ( char *  groupStr,
InterfaceEntry itf 
)
protectedvirtual

Referenced by parseInterfaces().

295 {
296  // Parse string (IPv4 addresses separated by colons)
297  cStringTokenizer tokenizer(groupStr, ":");
298  const char *token;
299  while ((token = tokenizer.nextToken()) != nullptr)
300  itf->ipv4Data()->joinMulticastGroup(IPv4Address(token));
301 }
void inet::RoutingTableParser::parseRouting ( char *  routeFile)
protectedvirtual

Referenced by readRoutingTableFromFile().

304 {
305  char *str = new char[MAX_ENTRY_STRING_SIZE];
306 
307  int pos = strlen(ROUTE_START_TOKEN);
308  skipBlanks(routeFile, pos);
309  while (routeFile[pos] != '\0') {
310  // 1st entry: Host
311  pos += strcpyword(str, routeFile + pos);
312  skipBlanks(routeFile, pos);
313  IPv4Route *e = new IPv4Route();
314  if (strcmp(str, "default:")) {
315  // if entry is not the default entry
316  if (!IPv4Address::isWellFormed(str))
317  throw cRuntimeError("Syntax error in routing file: `%s' on 1st column should be `default:' or a valid IPv4 address", str);
318 
319  e->setDestination(IPv4Address(str));
320  }
321 
322  // 2nd entry: Gateway
323  pos += strcpyword(str, routeFile + pos);
324  skipBlanks(routeFile, pos);
325  if (!strcmp(str, "*") || !strcmp(str, "0.0.0.0")) {
326  e->setGateway(IPv4Address::UNSPECIFIED_ADDRESS);
327  }
328  else {
329  if (!IPv4Address::isWellFormed(str))
330  throw cRuntimeError("Syntax error in routing file: `%s' on 2nd column should be `*' or a valid IPv4 address", str);
331 
332  e->setGateway(IPv4Address(str));
333  }
334 
335  // 3rd entry: Netmask
336  pos += strcpyword(str, routeFile + pos);
337  skipBlanks(routeFile, pos);
338  if (!IPv4Address::isWellFormed(str))
339  throw cRuntimeError("Syntax error in routing file: `%s' on 3rd column should be a valid IPv4 address", str);
340 
341  e->setNetmask(IPv4Address(str));
342 
343  // 4th entry: flags
344  pos += strcpyword(str, routeFile + pos);
345  skipBlanks(routeFile, pos);
346  // parse flag-String to set flags
347  for (int i = 0; str[i]; i++) {
348  if (str[i] == 'H') {
349  // e->setType(IPv4Route::DIRECT);
350  }
351  else if (str[i] == 'G') {
352  // e->setType(IPv4Route::REMOTE);
353  }
354  else {
355  throw cRuntimeError("Syntax error in routing file: 4th column should be `G' or `H' not `%s'", str);
356  }
357  }
358 
359  // 5th entry: metric
360  pos += strcpyword(str, routeFile + pos);
361  skipBlanks(routeFile, pos);
362  int metric = atoi(str);
363  if (metric == 0 && str[0] != '0')
364  throw cRuntimeError("Syntax error in routing file: 5th column should be numeric not `%s'", str);
365 
366  e->setMetric(metric);
367 
368  // 6th entry: interface
369  opp_string interfaceName;
370  interfaceName.reserve(MAX_ENTRY_STRING_SIZE);
371  pos += strcpyword(interfaceName.buffer(), routeFile + pos);
372  skipBlanks(routeFile, pos);
373  InterfaceEntry *ie = ift->getInterfaceByName(interfaceName.c_str());
374  if (!ie)
375  throw cRuntimeError("Syntax error in routing file: 6th column: `%s' is not an existing interface", interfaceName.c_str());
376 
377  e->setInterface(ie);
378  e->setSourceType(IRoute::MANUAL);
379 
380  // add entry
381  rt->addRoute(e);
382  }
383  delete [] str;
384 }
const char * ROUTE_START_TOKEN
Definition: RoutingTableParser.cc:44
IIPv4RoutingTable * rt
Definition: RoutingTableParser.h:44
virtual InterfaceEntry * getInterfaceByName(const char *name) const =0
Returns an interface given by its name.
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:269
static int strcpyword(char *dest, const char *src)
Definition: RoutingTableParser.cc:52
virtual void addRoute(IPv4Route *entry)=0
Adds a route to the routing table.
const value< double, units::C > e(1.602176487e-19)
IInterfaceTable * ift
Definition: RoutingTableParser.h:43
static const IPv4Address UNSPECIFIED_ADDRESS
0.0.0.0
Definition: IPv4Address.h:102
const int MAX_ENTRY_STRING_SIZE
Definition: RoutingTableParser.cc:37
manually added static route
Definition: IRoute.h:40
static void skipBlanks(char *str, int &charptr)
Definition: RoutingTableParser.cc:61
int inet::RoutingTableParser::readRoutingTableFromFile ( const char *  filename)
virtual

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

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

68 {
69  FILE *fp;
70  int charpointer;
71  char *file = new char[MAX_FILESIZE];
72  char *ifconfigFile = nullptr;
73  char *routeFile = nullptr;
74  int c;
75 
76  fp = fopen(filename, "r");
77  if (fp == nullptr)
78  throw cRuntimeError("Error opening routing table file `%s'", filename);
79 
80  // read the whole into the file[] char-array
81  for (charpointer = 0; (c = getc(fp)) != EOF; charpointer++)
82  file[charpointer] = c;
83 
84  for ( ; charpointer < MAX_FILESIZE; charpointer++)
85  file[charpointer] = '\0';
86 
87  fclose(fp);
88 
89  // copy file into specialized, filtered char arrays
90  for (charpointer = 0;
91  (charpointer < MAX_FILESIZE) && (file[charpointer] != '\0');
92  charpointer++)
93  {
94  // check for tokens at beginning of file or line
95  if (charpointer == 0 || file[charpointer - 1] == '\n') {
96  // copy into ifconfig filtered chararray
97  if (streq(file + charpointer, IFCONFIG_START_TOKEN)) {
98  ifconfigFile = createFilteredFile(file,
99  charpointer,
101  //PRINTF("Filtered File 1 created:\n%s\n", ifconfigFile);
102  }
103 
104  // copy into route filtered chararray
105  if (streq(file + charpointer, ROUTE_START_TOKEN)) {
106  routeFile = createFilteredFile(file,
107  charpointer,
109  //PRINTF("Filtered File 2 created:\n%s\n", routeFile);
110  }
111  }
112  }
113 
114  delete[] file;
115 
116  // parse filtered files
117  if (ifconfigFile)
118  parseInterfaces(ifconfigFile);
119  if (routeFile)
120  parseRouting(routeFile);
121 
122  delete[] ifconfigFile;
123  delete[] routeFile;
124 
125  return 0;
126 }
const char * ROUTE_START_TOKEN
Definition: RoutingTableParser.cc:44
virtual void parseRouting(char *routeFile)
Definition: RoutingTableParser.cc:303
static int streq(const char *str1, const char *str2)
Definition: RoutingTableParser.cc:47
const char * ROUTE_END_TOKEN
Definition: RoutingTableParser.cc:45
virtual char * createFilteredFile(char *file, int &charpointer, const char *endtoken)
Definition: RoutingTableParser.cc:128
const char * IFCONFIG_END_TOKEN
Definition: RoutingTableParser.cc:43
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
const char * IFCONFIG_START_TOKEN
Definition: RoutingTableParser.cc:42
virtual void parseInterfaces(char *ifconfigFile)
Definition: RoutingTableParser.cc:155
const int MAX_FILESIZE
Definition: RoutingTableParser.cc:36
void inet::RoutingTableParser::skipBlanks ( char *  str,
int &  charptr 
)
staticprotected

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

62 {
63  for ( ; isspace(str[charptr]); charptr++)
64  ;
65 }
int inet::RoutingTableParser::strcpyword ( char *  dest,
const char *  src 
)
staticprotected

Referenced by parseEntry(), and parseRouting().

53 {
54  int i;
55  for (i = 0; !isspace(dest[i] = src[i]); i++)
56  ;
57  dest[i] = '\0';
58  return i;
59 }
int inet::RoutingTableParser::streq ( const char *  str1,
const char *  str2 
)
staticprotected

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

48 {
49  return strncmp(str1, str2, strlen(str2)) == 0;
50 }

Member Data Documentation

IInterfaceTable* inet::RoutingTableParser::ift
protected

Referenced by parseInterfaces(), and parseRouting().

IIPv4RoutingTable* inet::RoutingTableParser::rt
protected

Referenced by parseRouting().


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