[2] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | #include "renetcol.h" |
---|
| 27 | |
---|
[18] | 28 | |
---|
| 29 | struct IPFLowCache tabIP[MAX_IP_FLOW], *tabIPPtr = tabIP; |
---|
| 30 | struct MPLSFlowCache tabMPLS[MAX_MPLS_FLOW], *tabMPLSPtr = tabMPLS; |
---|
| 31 | unsigned long ipNb = 0; |
---|
| 32 | unsigned long mplsNb = 0; |
---|
| 33 | unsigned long *ipNbPtr = &ipNb; |
---|
| 34 | unsigned long *mplsNbPtr = &mplsNb; |
---|
[19] | 35 | unsigned short currentIPOutputFile = 0; |
---|
| 36 | unsigned short currentMPLSOutputFile = 0; |
---|
| 37 | char outputName[256]; |
---|
| 38 | char cof_str[20]; |
---|
[18] | 39 | FILE *fIP; |
---|
| 40 | FILE *fMPLS; |
---|
[19] | 41 | |
---|
[18] | 42 | |
---|
[2] | 43 | char *rulesFileName; |
---|
| 44 | char *configFileName; |
---|
| 45 | char *prefixV4FileName; |
---|
[8] | 46 | struct sigaction myAction; |
---|
| 47 | sigjmp_buf contextSigalrm; |
---|
| 48 | |
---|
| 49 | volatile sig_atomic_t sigusr1Up = 0; |
---|
[2] | 50 | |
---|
| 51 | unsigned short offsetV9 = 0; |
---|
| 52 | unsigned short *offsetV9Ptr = &offsetV9; |
---|
| 53 | unsigned char buffer1; |
---|
| 54 | unsigned char buffer2[2]; |
---|
| 55 | unsigned char buffer4[4]; |
---|
| 56 | unsigned char buffer6[6]; |
---|
| 57 | short currentFlowsetNumber; |
---|
| 58 | short *currentFlowsetNumberPtr = ¤tFlowsetNumber; |
---|
| 59 | short currentFlowsetId; |
---|
| 60 | short *currentFlowsetIdPtr = ¤tFlowsetId; |
---|
| 61 | TplFlowSetPtr curTplFlSetPtr; |
---|
| 62 | TplOptionPtr curTplOptionPtr; |
---|
| 63 | RouterPtr routersListPtr; |
---|
| 64 | RouterPtr currentRouterPtr; |
---|
| 65 | DatagramPtr pcktPtr; |
---|
| 66 | unsigned long sortedRouterList[ROUTER_INDEX_MAX]; |
---|
| 67 | unsigned long indexedRouterList[ROUTER_INDEX_MAX]; |
---|
| 68 | int routerNb = 0; |
---|
[17] | 69 | unsigned short v4PrefixNb = 0; |
---|
[2] | 70 | struct PrefixV4 prefixV4Tab[PREFIX_V4_MAX]; |
---|
| 71 | RulesPtr rulesListPtr; |
---|
| 72 | NetFlowV9HeaderPtr currentHeaderV9Ptr; |
---|
| 73 | key_t myKey = 0; |
---|
| 74 | int myQueue = 0; |
---|
| 75 | RuleDefPtr rulesAddress[FIELD_TYPE_NUMBER+1][MAX_RULES_PER_FIELD]; |
---|
| 76 | RuleDefPtr *rulesAddressPtr; |
---|
[8] | 77 | int reInitConf = 0; |
---|
[2] | 78 | |
---|
| 79 | static unsigned long datagramNumber; |
---|
| 80 | static time_t now; |
---|
| 81 | static unsigned long myHost; |
---|
| 82 | static struct tm *tmPtr; |
---|
| 83 | extern int errno; |
---|
| 84 | static int inputSock; |
---|
| 85 | static int sockNamelg; |
---|
[17] | 86 | static int inputSock2; |
---|
[2] | 87 | static int sockNameFromlg; |
---|
| 88 | static int rcv; |
---|
| 89 | static int sockBufSize = SOCKET_BUFFER_SIZE; |
---|
[8] | 90 | |
---|
[2] | 91 | static unsigned short receptPort = RECEPTION_PORT; |
---|
| 92 | static unsigned char *ptr_buffer; |
---|
| 93 | static char *receptAddress = RECEPTION_ADDRESS; |
---|
| 94 | static struct sockaddr_in name; |
---|
[17] | 95 | static struct sockaddr_in name2; |
---|
[2] | 96 | static struct sockaddr_in fromName; |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | int |
---|
| 103 | main (int argc, char *argv[]) |
---|
| 104 | { |
---|
| 105 | RulesPtr tmp = NULL; |
---|
| 106 | |
---|
| 107 | if ( argc != 4) { |
---|
| 108 | fprintf (stderr, |
---|
| 109 | "%s: Usage: %s <routers file> <prefix IPv4 file> <rules file>\n exp: %s routers.txt IPv4_prefix.txt rules.txt\n", |
---|
| 110 | argv[0], argv[0], argv[0]); |
---|
| 111 | exit(1); |
---|
| 112 | } |
---|
[8] | 113 | |
---|
[2] | 114 | openlog(argv[0], LOG_PID, LOG_USER); |
---|
| 115 | |
---|
| 116 | configFileName = (char *) malloc((strlen(argv[1])+1) * sizeof(char)); |
---|
| 117 | strcpy (configFileName, argv[1]); |
---|
| 118 | prefixV4FileName = (char *) malloc((strlen(argv[2])+1) * sizeof(char)); |
---|
| 119 | strcpy (prefixV4FileName, argv[2]); |
---|
| 120 | rulesFileName = (char *) malloc((strlen(argv[3])+1) * sizeof(char)); |
---|
| 121 | strcpy (rulesFileName, argv[3]); |
---|
| 122 | |
---|
| 123 | initCache(); |
---|
[8] | 124 | fprintf(stderr, "reading the routers list\n"); |
---|
[2] | 125 | routerNb = getRegisteredRouters(configFileName, &sortedRouterList[0], |
---|
| 126 | &indexedRouterList[0]); |
---|
[8] | 127 | fprintf(stderr, "reading the IPv4 prefix\n"); |
---|
[17] | 128 | if ( (v4PrefixNb = getPrefixV4(prefixV4FileName, &prefixV4Tab[0])) < 1){ |
---|
[2] | 129 | fprintf(stderr, "Error during prefix v4 reading\n"); |
---|
| 130 | exit(1); |
---|
| 131 | } |
---|
| 132 | rulesListPtr = NULL; |
---|
[8] | 133 | fprintf(stderr, "reading the rules\n"); |
---|
[2] | 134 | rulesListPtr = getLightRules(rulesListPtr, rulesFileName); |
---|
| 135 | tmp = rulesListPtr; |
---|
| 136 | for ( ; tmp->next; tmp=tmp->next) { |
---|
| 137 | if (tmp->type != 2) { |
---|
| 138 | rulesListPtr = delRule(tmp, rulesListPtr); |
---|
| 139 | } |
---|
| 140 | } |
---|
[13] | 141 | |
---|
| 142 | |
---|
[8] | 143 | |
---|
| 144 | fprintf(stderr, "I become a deamon, next messages via syslogd.\n"); |
---|
| 145 | if (fork () != 0) |
---|
| 146 | exit (0); |
---|
| 147 | if (setsid() == -1){ |
---|
| 148 | exit(4); |
---|
| 149 | } |
---|
| 150 | |
---|
[15] | 151 | |
---|
[8] | 152 | myAction.sa_handler = sigusr1Mgmt; |
---|
| 153 | myAction.sa_flags = SA_RESTART; |
---|
| 154 | sigemptyset (&(myAction.sa_mask)); |
---|
| 155 | sigaddset (&(myAction.sa_mask), SIGALRM); |
---|
| 156 | sigaction (SIGUSR1, &myAction, NULL); |
---|
| 157 | |
---|
[2] | 158 | setCache(rulesListPtr); |
---|
| 159 | rulesAddressPtr = (RuleDefPtr *)rulesAddress; |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | myKey = createKey(argv[0]); |
---|
| 163 | myQueue = createQueue(myKey); |
---|
[8] | 164 | sendMyPid(myQueue); |
---|
[2] | 165 | |
---|
| 166 | |
---|
| 167 | currentRouterPtr = routersListPtr; |
---|
| 168 | currentHeaderV9Ptr = (NetFlowV9HeaderPtr) |
---|
| 169 | malloc(sizeof(struct NetFlowV9Header)); |
---|
| 170 | pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram)); |
---|
| 171 | if (pcktPtr==NULL) { |
---|
| 172 | fprintf(stderr, "ERROR in struct Datagram allocation\n"); |
---|
| 173 | exit(1); |
---|
| 174 | } else { |
---|
| 175 | pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader)); |
---|
| 176 | pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader)); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | if (! (ptr_buffer = malloc(sockBufSize))) |
---|
| 180 | { |
---|
| 181 | printf("ERROR during socket buffer allocation\n"); |
---|
| 182 | exit(2); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | initStream(); |
---|
| 187 | |
---|
[15] | 188 | datagramNumber =0; |
---|
[2] | 189 | |
---|
| 190 | now = time((time_t *)NULL); |
---|
| 191 | tmPtr = localtime(&now); |
---|
[15] | 192 | socketLoop(); |
---|
[2] | 193 | closelog(); |
---|
| 194 | fprintf(stderr, "END\n"); |
---|
| 195 | return (0); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | |
---|
[8] | 201 | void sendMyPid(int queueID) |
---|
| 202 | { |
---|
| 203 | msgType myMsg; |
---|
| 204 | char *msgTextIndex; |
---|
| 205 | unsigned short tplMsgType = 12; |
---|
| 206 | pid_t myPID; |
---|
| 207 | |
---|
| 208 | myPID = getpid(); |
---|
| 209 | msgTextIndex = mempcpy(mempcpy(myMsg.text, |
---|
| 210 | &tplMsgType, |
---|
| 211 | sizeof (unsigned short) |
---|
| 212 | ), |
---|
| 213 | &myPID, |
---|
| 214 | sizeof(pid_t) |
---|
| 215 | ); |
---|
| 216 | myMsg.type = 1; |
---|
| 217 | msgSend(queueID, myMsg); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | void sendReadRulesSignal(int queueID) |
---|
| 224 | { |
---|
| 225 | msgType myMsg; |
---|
| 226 | char *msgTextIndex; |
---|
| 227 | unsigned short tplMsgType = 13; |
---|
| 228 | |
---|
| 229 | msgTextIndex = mempcpy(myMsg.text, |
---|
| 230 | &tplMsgType, |
---|
| 231 | sizeof (unsigned short) |
---|
| 232 | ); |
---|
| 233 | myMsg.type = 1; |
---|
| 234 | msgSend(queueID, myMsg); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | void sigusr1Mgmt(int num) |
---|
| 241 | { |
---|
| 242 | sigusr1Up = 1; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | void |
---|
| 249 | initRule() |
---|
| 250 | { |
---|
| 251 | RulesPtr tmp = NULL; |
---|
| 252 | initCache(); |
---|
| 253 | tmp = rulesListPtr; |
---|
| 254 | for ( ; tmp; tmp=tmp->next) { |
---|
| 255 | rulesListPtr = delRule(tmp, rulesListPtr); |
---|
| 256 | } |
---|
| 257 | rulesListPtr = NULL; |
---|
| 258 | rulesListPtr = getLightRules(rulesListPtr, rulesFileName); |
---|
| 259 | tmp = rulesListPtr; |
---|
| 260 | for ( ; tmp; tmp=tmp->next) { |
---|
| 261 | if (tmp->type != 2) { |
---|
| 262 | rulesListPtr = delRule(tmp, rulesListPtr); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | tmp = NULL; |
---|
| 266 | initCache(); |
---|
| 267 | setCache(rulesListPtr); |
---|
| 268 | sendReadRulesSignal(myQueue); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | |
---|
[2] | 274 | void |
---|
| 275 | initCache() |
---|
| 276 | { |
---|
| 277 | int i,j; |
---|
| 278 | for (i=0; i<FIELD_TYPE_NUMBER+1; i++){ |
---|
| 279 | for (j=0; j<MAX_RULES_PER_FIELD; j++){ |
---|
| 280 | rulesAddress[i][j] = NULL; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
[18] | 286 | |
---|
[2] | 287 | |
---|
| 288 | void |
---|
| 289 | setCache(RulesPtr rPtr) |
---|
| 290 | { |
---|
| 291 | int i=0; |
---|
| 292 | RulesPtr tmp = rPtr; |
---|
| 293 | RuleDefPtr def = NULL; |
---|
| 294 | |
---|
| 295 | while (tmp) { |
---|
| 296 | def = tmp->def; |
---|
| 297 | while (def) { |
---|
| 298 | i = 0; |
---|
| 299 | while (rulesAddress[def->fieldType][i] != NULL){ |
---|
| 300 | i++; |
---|
| 301 | } |
---|
| 302 | rulesAddress[def->fieldType][i] = def; |
---|
| 303 | def = def->next; |
---|
| 304 | } |
---|
| 305 | tmp = tmp->next; |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | void |
---|
| 313 | initStream() |
---|
| 314 | { |
---|
| 315 | static unsigned short n0, n1, n2, n3; |
---|
| 316 | |
---|
| 317 | initSocket(); |
---|
| 318 | if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) { |
---|
| 319 | perror("sscanf"); |
---|
| 320 | } |
---|
| 321 | buffer4[0] = (unsigned char)n3; |
---|
| 322 | buffer4[1] = (unsigned char)n2; |
---|
| 323 | buffer4[2] = (unsigned char)n1; |
---|
| 324 | buffer4[3] = (unsigned char)n0; |
---|
| 325 | myHost = *((unsigned long*)&buffer4[0]); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | int |
---|
| 332 | socketLoop() |
---|
| 333 | { |
---|
| 334 | unsigned short shift; |
---|
| 335 | short version = 0; |
---|
[8] | 336 | int regRouter = 0; |
---|
[18] | 337 | |
---|
[19] | 338 | time_t now = time((time_t *)NULL); |
---|
[18] | 339 | int iIP, iMPLS; |
---|
[19] | 340 | time_t lastIPOutput, lastMPLSOutput; |
---|
| 341 | time_t receptionTime; |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | now = time((time_t *)NULL); |
---|
| 345 | lastMPLSOutput = now; |
---|
| 346 | lastIPOutput = now; |
---|
[2] | 347 | do { |
---|
[8] | 348 | if (sigusr1Up == 1){ |
---|
| 349 | sigusr1Up = 0; |
---|
| 350 | initRule(); |
---|
| 351 | } |
---|
[2] | 352 | socketReading(); |
---|
| 353 | getIpHeader(pcktPtr, ptr_buffer); |
---|
[8] | 354 | regRouter = checkIpHeader(pcktPtr, &sortedRouterList[0], routerNb); |
---|
[2] | 355 | getUdpHeader(pcktPtr, ptr_buffer); |
---|
[8] | 356 | checkUdpHeader(pcktPtr, regRouter, receptPort); |
---|
[2] | 357 | |
---|
| 358 | switch( version = |
---|
| 359 | getNetFlowHeader(pcktPtr, ptr_buffer, |
---|
| 360 | currentHeaderV9Ptr, offsetV9Ptr)){ |
---|
| 361 | case 9: |
---|
| 362 | if ((currentRouterPtr=notExistRouter(routersListPtr, |
---|
| 363 | pcktPtr->ipH->srcAdd))==NULL) { |
---|
| 364 | routersListPtr = addRouter(routersListPtr, |
---|
| 365 | pcktPtr->ipH->srcAdd, |
---|
| 366 | (unsigned long) 0); |
---|
| 367 | currentRouterPtr = routersListPtr; |
---|
| 368 | } |
---|
| 369 | currentFlowsetNumber = 0; |
---|
| 370 | shift = 0; |
---|
| 371 | while ((currentFlowsetNumber < currentHeaderV9Ptr->count)) { |
---|
| 372 | curTplFlSetPtr = NULL; |
---|
| 373 | currentFlowsetId=getFlowsetId(currentFlowsetNumber, |
---|
| 374 | offsetV9Ptr, ptr_buffer); |
---|
| 375 | if ( currentFlowsetId == 0 ) { |
---|
| 376 | shift = checkTemplateFlowSet(currentRouterPtr, offsetV9Ptr, |
---|
| 377 | ptr_buffer, currentHeaderV9Ptr, |
---|
| 378 | curTplFlSetPtr, |
---|
| 379 | currentFlowsetNumberPtr, |
---|
| 380 | myQueue); |
---|
| 381 | |
---|
| 382 | } else if (currentFlowsetId == 1) { |
---|
| 383 | shift = checkTemplateOption(currentRouterPtr, offsetV9Ptr, |
---|
| 384 | ptr_buffer, currentHeaderV9Ptr, |
---|
| 385 | curTplOptionPtr, |
---|
| 386 | currentFlowsetNumberPtr); |
---|
| 387 | } else { |
---|
| 388 | shift = checkDataFlowSet(shift, currentRouterPtr, |
---|
| 389 | currentHeaderV9Ptr, offsetV9Ptr, |
---|
| 390 | ptr_buffer, currentFlowsetNumberPtr, |
---|
| 391 | currentFlowsetIdPtr, pcktPtr, |
---|
| 392 | rulesAddressPtr, rulesListPtr, |
---|
[16] | 393 | myQueue, prefixV4Tab, |
---|
[18] | 394 | (size_t) v4PrefixNb, |
---|
| 395 | tabIPPtr, |
---|
| 396 | tabMPLSPtr, |
---|
| 397 | ipNbPtr, mplsNbPtr); |
---|
[2] | 398 | } |
---|
| 399 | } |
---|
| 400 | break; |
---|
| 401 | default: |
---|
[17] | 402 | |
---|
| 403 | |
---|
| 404 | |
---|
| 405 | |
---|
| 406 | |
---|
| 407 | |
---|
| 408 | |
---|
[2] | 409 | break; |
---|
| 410 | } |
---|
[18] | 411 | |
---|
| 412 | |
---|
[19] | 413 | now = time((time_t *)NULL); |
---|
| 414 | receptionTime = now; |
---|
| 415 | if ( (ipNb > (MAX_IP_FLOW - 50)) || |
---|
| 416 | ( (now-lastIPOutput) > IP_TIME_THRESHOLD) ) { |
---|
| 417 | outputName[0] = '\0'; |
---|
| 418 | sprintf(cof_str, "%d", currentIPOutputFile); |
---|
| 419 | strcat(outputName, OUTPUT_IP); |
---|
| 420 | strcat(outputName, cof_str); |
---|
| 421 | strcat(outputName, EXTENSION); |
---|
| 422 | if (fopen(outputName, "x") == NULL) { |
---|
| 423 | currentIPOutputFile = (currentIPOutputFile + 1)%MAX_IP_OUTPUT_FILES; |
---|
| 424 | outputName[0] = '\0'; |
---|
| 425 | sprintf(cof_str, "%d", currentIPOutputFile); |
---|
| 426 | strcat(outputName, OUTPUT_IP); |
---|
| 427 | strcat(outputName, cof_str); |
---|
| 428 | strcat(outputName, EXTENSION); |
---|
| 429 | } |
---|
| 430 | if ((fIP = fopen(outputName, "w")) == NULL) { |
---|
[18] | 431 | syslog(LOG_ERR,"Error in OUTPUT_IP opening : %s", strerror(errno)); |
---|
| 432 | } else { |
---|
| 433 | for ( iIP = 0; iIP < ipNb; iIP++){ |
---|
| 434 | if ( tabIP[iIP].ipProt == 0 ) { |
---|
[19] | 435 | fprintf(fIP, "%lu;%lu;%hu;%hu;%lu.%lu.%lu.%lu;%lu.%lu.%lu.%lu;%d;%hu;%hu;%d;%d;%lu.%lu.%lu.%lu;%lu;%d\n", |
---|
[18] | 436 | tabIP[iIP].bytes, tabIP[iIP].pkts, |
---|
| 437 | tabIP[iIP].inSnmp, tabIP[iIP].outSnmp, |
---|
| 438 | tabIP[iIP].v4AdS>>24, tabIP[iIP].v4AdS<<8>>24, |
---|
| 439 | tabIP[iIP].v4AdS<<16>>24, tabIP[iIP].v4AdS<<24>>24, |
---|
| 440 | tabIP[iIP].v4AdD>>24, tabIP[iIP].v4AdD<<8>>24, |
---|
| 441 | tabIP[iIP].v4AdD<<16>>24, tabIP[iIP].v4AdD<<24>>24, |
---|
| 442 | tabIP[iIP].tProt, tabIP[iIP].sPort, |
---|
| 443 | tabIP[iIP].dPort, tabIP[iIP].maskD, |
---|
| 444 | tabIP[iIP].maskS, tabIP[iIP].routerAd>>24, |
---|
| 445 | tabIP[iIP].routerAd<<8>>24, tabIP[iIP].routerAd<<16>>24, |
---|
[19] | 446 | tabIP[iIP].routerAd<<24>>24, tabIP[iIP].liveTime, |
---|
| 447 | receptionTime |
---|
[18] | 448 | ); |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | fclose(fIP); |
---|
| 452 | } |
---|
| 453 | ipNb = 0; |
---|
[19] | 454 | lastIPOutput = now; |
---|
[18] | 455 | } |
---|
[19] | 456 | now = time((time_t *)NULL); |
---|
| 457 | if ( mplsNb > (MAX_MPLS_FLOW - 50) || |
---|
| 458 | ( (now-lastMPLSOutput) > MPLS_TIME_THRESHOLD) ) { |
---|
| 459 | outputName[0] = '\0'; |
---|
| 460 | sprintf(cof_str, "%d", currentMPLSOutputFile); |
---|
| 461 | strcat(outputName, OUTPUT_MPLS); |
---|
| 462 | strcat(outputName, cof_str); |
---|
| 463 | strcat(outputName, EXTENSION); |
---|
| 464 | if (fopen(outputName, "x") == NULL) { |
---|
| 465 | currentMPLSOutputFile = (currentMPLSOutputFile + 1)%MAX_MPLS_OUTPUT_FILES; |
---|
| 466 | outputName[0] = '\0'; |
---|
| 467 | sprintf(cof_str, "%d", currentMPLSOutputFile); |
---|
| 468 | strcat(outputName, OUTPUT_MPLS); |
---|
| 469 | strcat(outputName, cof_str); |
---|
| 470 | strcat(outputName, EXTENSION); |
---|
| 471 | } |
---|
| 472 | if ((fMPLS = fopen(outputName, "w")) == NULL) { |
---|
[18] | 473 | syslog(LOG_ERR,"Error in OUTPUT_MPLS opening : %s", strerror(errno)); |
---|
| 474 | } else { |
---|
| 475 | for ( iMPLS = 0; iMPLS < mplsNb; iMPLS++){ |
---|
[19] | 476 | if ( tabMPLS[iMPLS].ipProt == 0 ) { |
---|
| 477 | fprintf(fMPLS, "%lu.%lu.%lu.%lu;%hu;%lu.%lu.%lu.%lu;%lu.%lu.%lu.%lu;%hu;%hu;%d\n", |
---|
[18] | 478 | tabMPLS[iMPLS].routerAd>>24,tabMPLS[iMPLS].routerAd<<8>>24, |
---|
| 479 | tabMPLS[iMPLS].routerAd<<16>>24, |
---|
| 480 | tabMPLS[iMPLS].routerAd<<24>>24, |
---|
[19] | 481 | tabMPLS[iMPLS].mplsLabel1, |
---|
| 482 | tabMPLS[iMPLS].v4AdS>>24, |
---|
[18] | 483 | tabMPLS[iMPLS].v4AdS<<8>>24,tabMPLS[iMPLS].v4AdS<<16>>24, |
---|
| 484 | tabMPLS[iMPLS].v4AdS<<24>>24,tabMPLS[iMPLS].v4AdD>>24, |
---|
| 485 | tabMPLS[iMPLS].v4AdD<<8>>24,tabMPLS[iMPLS].v4AdD<<16>>24, |
---|
[19] | 486 | tabMPLS[iMPLS].v4AdD<<24>>24, |
---|
| 487 | tabMPLS[iMPLS].inSnmp,tabMPLS[iMPLS].outSnmp, |
---|
| 488 | receptionTime |
---|
[18] | 489 | ); |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | fclose(fMPLS); |
---|
| 493 | } |
---|
| 494 | mplsNb = 0; |
---|
[19] | 495 | lastMPLSOutput = now; |
---|
[18] | 496 | } |
---|
| 497 | |
---|
[2] | 498 | } while (1); |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | |
---|
| 503 | |
---|
| 504 | short |
---|
| 505 | initSocket() |
---|
| 506 | { |
---|
| 507 | inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); |
---|
| 508 | if (inputSock < 0) |
---|
| 509 | { |
---|
| 510 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
| 511 | exit(1); |
---|
| 512 | } |
---|
| 513 | memset((void*)&name, 0, sizeof(name)); |
---|
| 514 | name.sin_family = AF_INET; |
---|
| 515 | name.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
| 516 | if (name.sin_addr.s_addr == INADDR_NONE){ |
---|
| 517 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
| 518 | exit(1); |
---|
| 519 | } |
---|
| 520 | name.sin_port = htons(receptPort); |
---|
| 521 | if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0) |
---|
| 522 | { |
---|
| 523 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
| 524 | exit(1); |
---|
| 525 | } |
---|
| 526 | sockNamelg = sizeof(name); |
---|
| 527 | if (getsockname(inputSock, (struct sockaddr *) (&name), |
---|
| 528 | (socklen_t *)&sockNamelg) < 0) |
---|
| 529 | { |
---|
| 530 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
| 531 | exit(1); |
---|
| 532 | } |
---|
[17] | 533 | |
---|
| 534 | inputSock2 = socket(AF_INET, SOCK_DGRAM, 0); |
---|
| 535 | if (inputSock2 < 0) |
---|
| 536 | { |
---|
| 537 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
| 538 | exit(1); |
---|
| 539 | } |
---|
| 540 | memset((void*)&name, 0, sizeof(name)); |
---|
| 541 | name2.sin_family = AF_INET; |
---|
| 542 | name2.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
| 543 | if (name2.sin_addr.s_addr == INADDR_NONE){ |
---|
| 544 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
| 545 | exit(1); |
---|
| 546 | } |
---|
| 547 | name2.sin_port = htons(receptPort); |
---|
| 548 | if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0) |
---|
| 549 | { |
---|
| 550 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
| 551 | exit(1); |
---|
| 552 | } |
---|
| 553 | sockNamelg = sizeof(name2); |
---|
| 554 | if (getsockname(inputSock2, (struct sockaddr *) (&name2), |
---|
| 555 | (socklen_t *)&sockNamelg) < 0) |
---|
| 556 | { |
---|
| 557 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
| 558 | exit(1); |
---|
| 559 | } |
---|
[2] | 560 | return(0); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | |
---|
| 564 | |
---|
| 565 | |
---|
| 566 | |
---|
| 567 | short |
---|
| 568 | socketReading() |
---|
| 569 | { |
---|
| 570 | signed short sockLg; |
---|
| 571 | |
---|
| 572 | sockNameFromlg = sizeof(fromName); |
---|
| 573 | rcv = recvfrom(inputSock, ptr_buffer, sockBufSize, 0, |
---|
| 574 | (struct sockaddr *)(&fromName), |
---|
| 575 | (socklen_t *)&sockNameFromlg); |
---|
| 576 | sockLg = rcv; |
---|
| 577 | if (sockLg < 0) { |
---|
| 578 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
| 579 | exit(1); |
---|
| 580 | } |
---|
| 581 | if (sockLg == 0) { |
---|
| 582 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
| 583 | exit(1); |
---|
| 584 | } |
---|
| 585 | return(0); |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | |
---|
| 589 | |
---|
| 590 | |
---|
| 591 | short |
---|
| 592 | checkFlow(short flowNumber) |
---|
| 593 | { |
---|
| 594 | return(0); |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | |
---|
| 598 | |
---|
| 599 | |
---|
| 600 | |
---|
| 601 | void |
---|
| 602 | showAllTplFlSet() |
---|
| 603 | { |
---|
| 604 | RouterPtr tmp = routersListPtr; |
---|
| 605 | TplFlowSetPtr tmpFS; |
---|
| 606 | TplOptionPtr tmpOP; |
---|
| 607 | fprintf(stderr,"All template definitions: (field, size) :\n"); |
---|
| 608 | for (; tmp; tmp=tmp->next) { |
---|
| 609 | fprintf(stderr,"router %lu.%lu.%lu.%lu : \n", |
---|
| 610 | (tmp->IpAddress>>24), |
---|
| 611 | (tmp->IpAddress<<8>>24), |
---|
| 612 | (tmp->IpAddress<<16>>24), |
---|
| 613 | (tmp->IpAddress<<24>>24)); |
---|
| 614 | tmpFS = tmp->tplList; |
---|
| 615 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
| 616 | fprintf(stderr,"ID %hu: ", tmpFS->templateFlowSetId); |
---|
| 617 | printFieldSet(tmpFS->fieldSet); |
---|
| 618 | fprintf(stderr,"\n"); |
---|
| 619 | } |
---|
| 620 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
| 621 | fprintf(stderr,"template option: \n"); |
---|
| 622 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
| 623 | fprintf(stderr,"ID %hu: ", tmpOP->templateOptionId); |
---|
| 624 | printFieldSet(tmpOP->fieldSet); |
---|
| 625 | fprintf(stderr,"\n"); |
---|
| 626 | } |
---|
| 627 | fprintf(stderr,"\n"); |
---|
| 628 | } |
---|
| 629 | } |
---|
| 630 | } |
---|