#include <RoutingTableParser.h>
Public Member Functions | |
RoutingTableParser (InterfaceTable *ift, RoutingTable *rt) | |
int | readRoutingTableFromFile (const char *filename) |
Private Member Functions | |
char * | createFilteredFile (char *file, int &charpointer, const char *endtoken) |
void | parseInterfaces (char *ifconfigFile) |
void | parseRouting (char *routeFile) |
char * | parseEntry (char *ifconfigFile, const char *tokenStr, int &charpointer, char *destStr) |
void | parseMulticastGroups (char *groupStr, InterfaceEntry *) |
Static Private 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) |
Private Attributes | |
InterfaceTable * | ift |
RoutingTable * | rt |
RoutingTableParser::RoutingTableParser | ( | InterfaceTable * | ift, | |
RoutingTable * | rt | |||
) |
char * RoutingTableParser::createFilteredFile | ( | char * | file, | |
int & | charpointer, | |||
const char * | endtoken | |||
) | [private] |
00140 { 00141 int i = 0; 00142 char *filterFile = new char[MAX_FILESIZE]; 00143 filterFile[0] = '\0'; 00144 00145 while(true) { 00146 // skip blank lines and comments 00147 while ( !isalnum(file[charpointer]) && !isspace(file[charpointer]) ) { 00148 while (file[charpointer++] != '\n') ; 00149 } 00150 00151 // check for endtoken: 00152 if (streq(file + charpointer, endtoken)) { 00153 filterFile[i] = '\0'; 00154 break; 00155 } 00156 00157 // copy whole line to filterFile 00158 while ((filterFile[i++] = file[charpointer++]) != '\n') ; 00159 } 00160 00161 return filterFile; 00162 }
char * RoutingTableParser::parseEntry | ( | char * | ifconfigFile, | |
const char * | tokenStr, | |||
int & | charpointer, | |||
char * | destStr | |||
) | [private] |
00270 { 00271 int temp = 0; 00272 00273 charpointer += strlen(tokenStr); 00274 skipBlanks(ifconfigFile, charpointer); 00275 temp = strcpyword(destStr, ifconfigFile + charpointer); 00276 charpointer += temp; 00277 00278 skipBlanks(ifconfigFile, charpointer); 00279 00280 return destStr; 00281 }
void RoutingTableParser::parseInterfaces | ( | char * | ifconfigFile | ) | [private] |
00166 { 00167 char buf[MAX_ENTRY_STRING_SIZE]; 00168 int charpointer = 0; 00169 InterfaceEntry *ie; 00170 00171 // parsing of entries in interface definition 00172 while(ifconfigFile[charpointer] != '\0') 00173 { 00174 // name entry 00175 if (streq(ifconfigFile + charpointer, "name:")) { 00176 // find existing interface with this name 00177 char *name = parseEntry(ifconfigFile, "name:", charpointer,buf); 00178 ie = ift->interfaceByName(name); 00179 if (!ie) 00180 opp_error("Error in routing file: interface name `%s' not registered by any L2 module", name); 00181 continue; 00182 } 00183 00184 // encap entry 00185 if (streq(ifconfigFile + charpointer, "encap:")) { 00186 // ignore encap 00187 parseEntry(ifconfigFile, "encap:", charpointer, buf); 00188 continue; 00189 } 00190 00191 // HWaddr entry 00192 if (streq(ifconfigFile + charpointer, "HWaddr:")) { 00193 // ignore hwAddr 00194 parseEntry(ifconfigFile, "HWaddr:", charpointer, buf); 00195 continue; 00196 } 00197 00198 // inet_addr entry 00199 if (streq(ifconfigFile + charpointer, "inet_addr:")) { 00200 ie->ipv4()->setInetAddress(IPAddress(parseEntry(ifconfigFile, "inet_addr:", charpointer,buf))); 00201 continue; 00202 } 00203 00204 // Broadcast address entry 00205 if (streq(ifconfigFile + charpointer, "Bcast:")) { 00206 // ignore Bcast 00207 parseEntry(ifconfigFile, "Bcast:", charpointer, buf); 00208 continue; 00209 } 00210 00211 // Mask entry 00212 if (streq(ifconfigFile + charpointer, "Mask:")) { 00213 ie->ipv4()->setNetmask(IPAddress(parseEntry(ifconfigFile, "Mask:", charpointer,buf))); 00214 continue; 00215 } 00216 00217 // Multicast groups entry 00218 if (streq(ifconfigFile + charpointer, "Groups:")) { 00219 char *grStr = parseEntry(ifconfigFile, "Groups:", charpointer, buf); 00220 parseMulticastGroups(grStr, ie); 00221 continue; 00222 } 00223 00224 // MTU entry 00225 if (streq(ifconfigFile + charpointer, "MTU:")) { 00226 ie->setMtu(atoi(parseEntry(ifconfigFile, "MTU:", charpointer,buf))); 00227 continue; 00228 } 00229 00230 // Metric entry 00231 if (streq(ifconfigFile + charpointer, "Metric:")) { 00232 ie->ipv4()->setMetric(atoi(parseEntry(ifconfigFile, "Metric:", charpointer,buf))); 00233 continue; 00234 } 00235 00236 // BROADCAST Flag 00237 if (streq(ifconfigFile + charpointer, "BROADCAST")) { 00238 ie->setBroadcast(true); 00239 charpointer += strlen("BROADCAST"); 00240 skipBlanks(ifconfigFile, charpointer); 00241 continue; 00242 } 00243 00244 // MULTICAST Flag 00245 if (streq(ifconfigFile + charpointer, "MULTICAST")) { 00246 ie->setMulticast(true); 00247 charpointer += strlen("MULTICAST"); 00248 skipBlanks(ifconfigFile, charpointer); 00249 continue; 00250 } 00251 00252 // POINTTOPOINT Flag 00253 if (streq(ifconfigFile + charpointer, "POINTTOPOINT")) { 00254 ie->setPointToPoint(true); 00255 charpointer += strlen("POINTTOPOINT"); 00256 skipBlanks(ifconfigFile, charpointer); 00257 continue; 00258 } 00259 00260 // no entry discovered: move charpointer on 00261 charpointer++; 00262 } 00263 }
void RoutingTableParser::parseMulticastGroups | ( | char * | groupStr, | |
InterfaceEntry * | ||||
) | [private] |
00286 { 00287 IPv4InterfaceData::IPAddressVector mcg = itf->ipv4()->multicastGroups(); 00288 00289 // add "224.0.0.1" automatically 00290 mcg.push_back(IPAddress::ALL_HOSTS_MCAST); 00291 00292 // add 224.0.0.2" only if Router (IP forwarding enabled) 00293 if (rt->ipForward()) 00294 mcg.push_back(IPAddress::ALL_ROUTERS_MCAST); 00295 00296 // Parse string (IP addresses separated by colons) 00297 cStringTokenizer tokenizer(groupStr,":"); 00298 const char *token; 00299 while ((token = tokenizer.nextToken())!=NULL) 00300 mcg.push_back(IPAddress(token)); 00301 00302 itf->ipv4()->setMulticastGroups(mcg); 00303 }
void RoutingTableParser::parseRouting | ( | char * | routeFile | ) | [private] |
00306 { 00307 char *str = new char[MAX_ENTRY_STRING_SIZE]; 00308 00309 int pos = strlen(ROUTE_START_TOKEN); 00310 skipBlanks(routeFile, pos); 00311 while (routeFile[pos] != '\0') 00312 { 00313 // 1st entry: Host 00314 pos += strcpyword(str, routeFile + pos); 00315 skipBlanks(routeFile, pos); 00316 RoutingEntry *e = new RoutingEntry(); 00317 if (strcmp(str, "default:")) 00318 { 00319 // if entry is not the default entry 00320 if (!IPAddress::isWellFormed(str)) 00321 opp_error("Syntax error in routing file: `%s' on 1st column should be `default:' or a valid IP address", str); 00322 e->host = IPAddress(str); 00323 } 00324 00325 // 2nd entry: Gateway 00326 pos += strcpyword(str, routeFile + pos); 00327 skipBlanks(routeFile, pos); 00328 if (!strcmp(str, "*") || !strcmp(str, "0.0.0.0")) 00329 { 00330 e->gateway = IPAddress::UNSPECIFIED_ADDRESS; 00331 } 00332 else 00333 { 00334 if (!IPAddress::isWellFormed(str)) 00335 opp_error("Syntax error in routing file: `%s' on 2nd column should be `*' or a valid IP address", str); 00336 e->gateway = IPAddress(str); 00337 } 00338 00339 // 3rd entry: Netmask 00340 pos += strcpyword(str, routeFile + pos); 00341 skipBlanks(routeFile, pos); 00342 if (!IPAddress::isWellFormed(str)) 00343 opp_error("Syntax error in routing file: `%s' on 3rd column should be a valid IP address", str); 00344 e->netmask = IPAddress(str); 00345 00346 // 4th entry: flags 00347 pos += strcpyword(str, routeFile + pos); 00348 skipBlanks(routeFile, pos); 00349 // parse flag-String to set flags 00350 for (int i = 0; str[i]; i++) 00351 { 00352 if (str[i] == 'H') { 00353 e->type = RoutingEntry::DIRECT; 00354 } else if (str[i] == 'G') { 00355 e->type = RoutingEntry::REMOTE; 00356 } else { 00357 opp_error("Syntax error in routing file: 4th column should be `G' or `H' not `%s'", str); 00358 } 00359 } 00360 00361 // 5th entry: metric 00362 pos += strcpyword(str, routeFile + pos); 00363 skipBlanks(routeFile, pos); 00364 int metric = atoi(str); 00365 if (metric==0 && str[0]!='0') 00366 opp_error("Syntax error in routing file: 5th column should be numeric not `%s'", str); 00367 e->metric = metric; 00368 00369 // 6th entry: interfaceName 00370 e->interfaceName.reserve(MAX_ENTRY_STRING_SIZE); 00371 pos += strcpyword(e->interfaceName.buffer(), routeFile + pos); 00372 skipBlanks(routeFile, pos); 00373 e->interfacePtr = ift->interfaceByName(e->interfaceName.c_str()); 00374 if (e->interfacePtr==NULL) 00375 opp_error("Syntax error in routing file: 6th column should be an existing " 00376 "interface name not `%s'", e->interfaceName.c_str()); 00377 00378 // add entry 00379 rt->addRoutingEntry(e); 00380 } 00381 }
int RoutingTableParser::readRoutingTableFromFile | ( | const char * | filename | ) |
Read Routing Table file; return 0 on success, -1 on error
00074 { 00075 FILE *fp; 00076 int charpointer; 00077 char *file = new char[MAX_FILESIZE]; 00078 char *ifconfigFile = NULL; 00079 char *routeFile = NULL; 00080 00081 fp = fopen(filename, "r"); 00082 if (fp == NULL) 00083 opp_error("Error opening routing table file `%s'", filename); 00084 00085 // read the whole into the file[] char-array 00086 for (charpointer = 0; 00087 (file[charpointer] = getc(fp)) != EOF; 00088 charpointer++) ; 00089 00090 charpointer++; 00091 for (; charpointer < MAX_FILESIZE; charpointer++) 00092 file[charpointer] = '\0'; 00093 // file[++charpointer] = '\0'; 00094 00095 fclose(fp); 00096 00097 00098 // copy file into specialized, filtered char arrays 00099 for (charpointer = 0; 00100 (charpointer < MAX_FILESIZE) && (file[charpointer] != EOF); 00101 charpointer++) { 00102 // check for tokens at beginning of file or line 00103 if (charpointer == 0 || file[charpointer - 1] == '\n') { 00104 // copy into ifconfig filtered chararray 00105 if (streq(file + charpointer, IFCONFIG_START_TOKEN)) { 00106 ifconfigFile = createFilteredFile(file, 00107 charpointer, 00108 IFCONFIG_END_TOKEN); 00109 //PRINTF("Filtered File 1 created:\n%s\n", ifconfigFile); 00110 } 00111 00112 // copy into route filtered chararray 00113 if (streq(file + charpointer, ROUTE_START_TOKEN)) { 00114 routeFile = createFilteredFile(file, 00115 charpointer, 00116 ROUTE_END_TOKEN); 00117 //PRINTF("Filtered File 2 created:\n%s\n", routeFile); 00118 } 00119 } 00120 } 00121 00122 delete file; 00123 00124 // parse filtered files 00125 if (ifconfigFile) 00126 parseInterfaces(ifconfigFile); 00127 if (routeFile) 00128 parseRouting(routeFile); 00129 00130 delete ifconfigFile; 00131 delete routeFile; 00132 00133 return 0; 00134 00135 }
void RoutingTableParser::skipBlanks | ( | char * | str, | |
int & | charptr | |||
) | [static, private] |
int RoutingTableParser::strcpyword | ( | char * | dest, | |
const char * | src | |||
) | [static, private] |
int RoutingTableParser::streq | ( | const char * | str1, | |
const char * | str2 | |||
) | [static, private] |
InterfaceTable* RoutingTableParser::ift [private] |
RoutingTable* RoutingTableParser::rt [private] |