#include <RoutingTableParser.h>
Public Member Functions | |
RoutingTableParser (IInterfaceTable *ift, IRoutingTable *rt) | |
virtual int | readRoutingTableFromFile (const char *filename) |
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 | |
IInterfaceTable * | ift |
IRoutingTable * | rt |
Parses a routing table file into a routing table.
Definition at line 39 of file RoutingTableParser.h.
RoutingTableParser::RoutingTableParser | ( | IInterfaceTable * | ift, | |
IRoutingTable * | rt | |||
) |
char * RoutingTableParser::createFilteredFile | ( | char * | file, | |
int & | charpointer, | |||
const char * | endtoken | |||
) | [protected, virtual] |
Definition at line 136 of file RoutingTableParser.cc.
Referenced by readRoutingTableFromFile().
{ int i = 0; char *filterFile = new char[MAX_FILESIZE]; filterFile[0] = '\0'; while(true) { // skip blank lines and comments while ( !isalnum(file[charpointer]) && !isspace(file[charpointer]) ) { while (file[charpointer++] != '\n') ; } // check for endtoken: if (streq(file + charpointer, endtoken)) { filterFile[i] = '\0'; break; } // copy whole line to filterFile while ((filterFile[i++] = file[charpointer++]) != '\n') ; } return filterFile; }
char * RoutingTableParser::parseEntry | ( | char * | ifconfigFile, | |
const char * | tokenStr, | |||
int & | charpointer, | |||
char * | destStr | |||
) | [protected, virtual] |
Definition at line 263 of file RoutingTableParser.cc.
Referenced by parseInterfaces().
{ int temp = 0; charpointer += strlen(tokenStr); skipBlanks(ifconfigFile, charpointer); temp = strcpyword(destStr, ifconfigFile + charpointer); charpointer += temp; skipBlanks(ifconfigFile, charpointer); return destStr; }
void RoutingTableParser::parseInterfaces | ( | char * | ifconfigFile | ) | [protected, virtual] |
Definition at line 162 of file RoutingTableParser.cc.
Referenced by readRoutingTableFromFile().
{ char buf[MAX_ENTRY_STRING_SIZE]; int charpointer = 0; InterfaceEntry *ie; // parsing of entries in interface definition while(ifconfigFile[charpointer] != '\0') { // name entry if (streq(ifconfigFile + charpointer, "name:")) { // find existing interface with this name char *name = parseEntry(ifconfigFile, "name:", charpointer,buf); ie = ift->getInterfaceByName(name); if (!ie) opp_error("Error in routing file: interface name `%s' not registered by any L2 module", name); continue; } // encap entry if (streq(ifconfigFile + charpointer, "encap:")) { // ignore encap parseEntry(ifconfigFile, "encap:", charpointer, buf); continue; } // HWaddr entry if (streq(ifconfigFile + charpointer, "HWaddr:")) { // ignore hwAddr parseEntry(ifconfigFile, "HWaddr:", charpointer, buf); continue; } // inet_addr entry if (streq(ifconfigFile + charpointer, "inet_addr:")) { ie->ipv4Data()->setIPAddress(IPAddress(parseEntry(ifconfigFile, "inet_addr:", charpointer,buf))); continue; } // Broadcast address entry if (streq(ifconfigFile + charpointer, "Bcast:")) { // ignore Bcast parseEntry(ifconfigFile, "Bcast:", charpointer, buf); continue; } // Mask entry if (streq(ifconfigFile + charpointer, "Mask:")) { ie->ipv4Data()->setNetmask(IPAddress(parseEntry(ifconfigFile, "Mask:", charpointer,buf))); continue; } // Multicast groups entry if (streq(ifconfigFile + charpointer, "Groups:")) { char *grStr = parseEntry(ifconfigFile, "Groups:", charpointer, buf); parseMulticastGroups(grStr, ie); continue; } // MTU entry if (streq(ifconfigFile + charpointer, "MTU:")) { ie->setMtu(atoi(parseEntry(ifconfigFile, "MTU:", charpointer,buf))); continue; } // Metric entry if (streq(ifconfigFile + charpointer, "Metric:")) { ie->ipv4Data()->setMetric(atoi(parseEntry(ifconfigFile, "Metric:", charpointer,buf))); continue; } // BROADCAST Flag if (streq(ifconfigFile + charpointer, "BROADCAST")) { ie->setBroadcast(true); charpointer += strlen("BROADCAST"); skipBlanks(ifconfigFile, charpointer); continue; } // MULTICAST Flag if (streq(ifconfigFile + charpointer, "MULTICAST")) { ie->setMulticast(true); charpointer += strlen("MULTICAST"); skipBlanks(ifconfigFile, charpointer); continue; } // POINTTOPOINT Flag if (streq(ifconfigFile + charpointer, "POINTTOPOINT")) { ie->setPointToPoint(true); charpointer += strlen("POINTTOPOINT"); skipBlanks(ifconfigFile, charpointer); continue; } // no entry discovered: move charpointer on charpointer++; } }
void RoutingTableParser::parseMulticastGroups | ( | char * | groupStr, | |
InterfaceEntry * | itf | |||
) | [protected, virtual] |
Definition at line 279 of file RoutingTableParser.cc.
Referenced by parseInterfaces().
{ IPv4InterfaceData::IPAddressVector mcg = itf->ipv4Data()->getMulticastGroups(); // add "224.0.0.1" automatically mcg.push_back(IPAddress::ALL_HOSTS_MCAST); // add 224.0.0.2" only if Router (IP forwarding enabled) if (rt->isIPForwardingEnabled()) mcg.push_back(IPAddress::ALL_ROUTERS_MCAST); // Parse string (IP addresses separated by colons) cStringTokenizer tokenizer(groupStr,":"); const char *token; while ((token = tokenizer.nextToken())!=NULL) mcg.push_back(IPAddress(token)); itf->ipv4Data()->setMulticastGroups(mcg); }
void RoutingTableParser::parseRouting | ( | char * | routeFile | ) | [protected, virtual] |
Definition at line 299 of file RoutingTableParser.cc.
Referenced by readRoutingTableFromFile().
{ char *str = new char[MAX_ENTRY_STRING_SIZE]; int pos = strlen(ROUTE_START_TOKEN); skipBlanks(routeFile, pos); while (routeFile[pos] != '\0') { // 1st entry: Host pos += strcpyword(str, routeFile + pos); skipBlanks(routeFile, pos); IPRoute *e = new IPRoute(); if (strcmp(str, "default:")) { // if entry is not the default entry if (!IPAddress::isWellFormed(str)) opp_error("Syntax error in routing file: `%s' on 1st column should be `default:' or a valid IP address", str); e->setHost(IPAddress(str)); } // 2nd entry: Gateway pos += strcpyword(str, routeFile + pos); skipBlanks(routeFile, pos); if (!strcmp(str, "*") || !strcmp(str, "0.0.0.0")) { e->setGateway(IPAddress::UNSPECIFIED_ADDRESS); } else { if (!IPAddress::isWellFormed(str)) opp_error("Syntax error in routing file: `%s' on 2nd column should be `*' or a valid IP address", str); e->setGateway(IPAddress(str)); } // 3rd entry: Netmask pos += strcpyword(str, routeFile + pos); skipBlanks(routeFile, pos); if (!IPAddress::isWellFormed(str)) opp_error("Syntax error in routing file: `%s' on 3rd column should be a valid IP address", str); e->setNetmask(IPAddress(str)); // 4th entry: flags pos += strcpyword(str, routeFile + pos); skipBlanks(routeFile, pos); // parse flag-String to set flags for (int i = 0; str[i]; i++) { if (str[i] == 'H') { e->setType(IPRoute::DIRECT); } else if (str[i] == 'G') { e->setType(IPRoute::REMOTE); } else { opp_error("Syntax error in routing file: 4th column should be `G' or `H' not `%s'", str); } } // 5th entry: metric pos += strcpyword(str, routeFile + pos); skipBlanks(routeFile, pos); int metric = atoi(str); if (metric==0 && str[0]!='0') opp_error("Syntax error in routing file: 5th column should be numeric not `%s'", str); e->setMetric(metric); // 6th entry: interface opp_string interfaceName; interfaceName.reserve(MAX_ENTRY_STRING_SIZE); pos += strcpyword(interfaceName.buffer(), routeFile + pos); skipBlanks(routeFile, pos); InterfaceEntry *ie = ift->getInterfaceByName(interfaceName.c_str()); if (!ie) opp_error("Syntax error in routing file: 6th column: `%s' is not an existing interface", interfaceName.c_str()); e->setInterface(ie); // add entry rt->addRoute(e); } }
int RoutingTableParser::readRoutingTableFromFile | ( | const char * | filename | ) | [virtual] |
Read Routing Table file; return 0 on success, -1 on error
Definition at line 72 of file RoutingTableParser.cc.
Referenced by RoutingTable::initialize().
{ FILE *fp; int charpointer; char *file = new char[MAX_FILESIZE]; char *ifconfigFile = NULL; char *routeFile = NULL; fp = fopen(filename, "r"); if (fp == NULL) opp_error("Error opening routing table file `%s'", filename); // read the whole into the file[] char-array for (charpointer = 0; (file[charpointer] = getc(fp)) != EOF; charpointer++) ; charpointer++; for (; charpointer < MAX_FILESIZE; charpointer++) file[charpointer] = '\0'; // file[++charpointer] = '\0'; fclose(fp); // copy file into specialized, filtered char arrays for (charpointer = 0; (charpointer < MAX_FILESIZE) && (file[charpointer] != EOF); charpointer++) { // check for tokens at beginning of file or line if (charpointer == 0 || file[charpointer - 1] == '\n') { // copy into ifconfig filtered chararray if (streq(file + charpointer, IFCONFIG_START_TOKEN)) { ifconfigFile = createFilteredFile(file, charpointer, IFCONFIG_END_TOKEN); //PRINTF("Filtered File 1 created:\n%s\n", ifconfigFile); } // copy into route filtered chararray if (streq(file + charpointer, ROUTE_START_TOKEN)) { routeFile = createFilteredFile(file, charpointer, ROUTE_END_TOKEN); //PRINTF("Filtered File 2 created:\n%s\n", routeFile); } } } delete file; // parse filtered files if (ifconfigFile) parseInterfaces(ifconfigFile); if (routeFile) parseRouting(routeFile); delete ifconfigFile; delete routeFile; return 0; }
void RoutingTableParser::skipBlanks | ( | char * | str, | |
int & | charptr | |||
) | [static, protected] |
Definition at line 66 of file RoutingTableParser.cc.
Referenced by parseEntry(), parseInterfaces(), and parseRouting().
{
for (;isspace(str[charptr]); charptr++) ;
}
int RoutingTableParser::strcpyword | ( | char * | dest, | |
const char * | src | |||
) | [static, protected] |
Definition at line 57 of file RoutingTableParser.cc.
Referenced by parseEntry(), and parseRouting().
{ int i; for (i = 0; !isspace(dest[i] = src[i]); i++) ; dest[i] = '\0'; return i; }
int RoutingTableParser::streq | ( | const char * | str1, | |
const char * | str2 | |||
) | [static, protected] |
Definition at line 51 of file RoutingTableParser.cc.
Referenced by createFilteredFile(), parseInterfaces(), and readRoutingTableFromFile().
{
return (strncmp(str1, str2, strlen(str2)) == 0);
}
IInterfaceTable* RoutingTableParser::ift [protected] |
Definition at line 42 of file RoutingTableParser.h.
Referenced by parseInterfaces(), parseRouting(), and RoutingTableParser().
IRoutingTable* RoutingTableParser::rt [protected] |
Definition at line 43 of file RoutingTableParser.h.
Referenced by parseMulticastGroups(), parseRouting(), and RoutingTableParser().