[2] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[28] | 4 | |
---|
[2] | 5 | |
---|
[28] | 6 | |
---|
[2] | 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | #include "renetcol.h" |
---|
| 27 | |
---|
[36] | 28 | struct MyPtrs myPtrs; |
---|
[23] | 29 | |
---|
[24] | 30 | struct SHMForAgg *shmForAgg; |
---|
| 31 | |
---|
[23] | 32 | unsigned short currentIPOutputFile = 0; |
---|
| 33 | unsigned short currentMPLSOutputFile = 0; |
---|
[19] | 34 | char outputName[256]; |
---|
[23] | 35 | char cof_str[20]; |
---|
| 36 | FILE *fIP; |
---|
| 37 | FILE *fMPLS; |
---|
[18] | 38 | |
---|
[22] | 39 | struct sigaction myAction; |
---|
| 40 | sigjmp_buf contextSigalrm; |
---|
[8] | 41 | volatile sig_atomic_t sigusr1Up = 0; |
---|
[22] | 42 | |
---|
| 43 | |
---|
[23] | 44 | unsigned short currentIntervalle = 0; |
---|
[22] | 45 | |
---|
[23] | 46 | unsigned short offsetV9 = 0; |
---|
[22] | 47 | |
---|
[2] | 48 | unsigned char buffer1; |
---|
| 49 | unsigned char buffer2[2]; |
---|
| 50 | unsigned char buffer4[4]; |
---|
| 51 | unsigned char buffer6[6]; |
---|
[22] | 52 | |
---|
[2] | 53 | short currentFlowsetNumber; |
---|
| 54 | short currentFlowsetId; |
---|
[22] | 55 | TplFlowSetPtr curTplFlSetPtr; |
---|
[2] | 56 | TplOptionPtr curTplOptionPtr; |
---|
[23] | 57 | RouterPtr routersListPtr; |
---|
| 58 | |
---|
[2] | 59 | unsigned long sortedRouterList[ROUTER_INDEX_MAX]; |
---|
| 60 | unsigned long indexedRouterList[ROUTER_INDEX_MAX]; |
---|
[23] | 61 | int routerNb = 0; |
---|
| 62 | |
---|
[36] | 63 | key_t myKey = 0; |
---|
| 64 | int myQueue = 0; |
---|
[24] | 65 | |
---|
[22] | 66 | |
---|
| 67 | RuleDefPtr rulesAddress[FIELD_TYPE_NUMBER+1][MAX_RULES_PER_FIELD]; |
---|
| 68 | |
---|
[8] | 69 | int reInitConf = 0; |
---|
[22] | 70 | |
---|
| 71 | static struct tm *tmPtr; |
---|
| 72 | |
---|
[2] | 73 | extern int errno; |
---|
| 74 | static int inputSock; |
---|
| 75 | static int sockNamelg; |
---|
[17] | 76 | static int inputSock2; |
---|
[2] | 77 | static int sockNameFromlg; |
---|
| 78 | static int rcv; |
---|
| 79 | static int sockBufSize = SOCKET_BUFFER_SIZE; |
---|
[8] | 80 | |
---|
[2] | 81 | static unsigned short receptPort = RECEPTION_PORT; |
---|
[23] | 82 | |
---|
[2] | 83 | static char *receptAddress = RECEPTION_ADDRESS; |
---|
| 84 | static struct sockaddr_in name; |
---|
[17] | 85 | static struct sockaddr_in name2; |
---|
[2] | 86 | static struct sockaddr_in fromName; |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | int |
---|
| 93 | main (int argc, char *argv[]) |
---|
| 94 | { |
---|
| 95 | RulesPtr tmp = NULL; |
---|
[24] | 96 | int shmid; |
---|
| 97 | key_t key; |
---|
[36] | 98 | int i,j; |
---|
[58] | 99 | int k; |
---|
[8] | 100 | |
---|
[76] | 101 | #if defined(IPV4AGGIDR) |
---|
[59] | 102 | fprintf(stderr, "renetcol compilation and execution with IPv4 Aggregation feature enable based on routers ID in %s file.\n", ROUTERS_LIST); |
---|
[22] | 103 | #endif |
---|
[59] | 104 | |
---|
[76] | 105 | #if defined(IPV4AGGIDSNMP) |
---|
[59] | 106 | fprintf(stderr, "renetcol compilation and execution with IPv4 Aggregation feature enable based on SNMP ID in %s file.\n", INTERFACES_TYPE_LIST); |
---|
| 107 | #endif |
---|
[22] | 108 | |
---|
[2] | 109 | openlog(argv[0], LOG_PID, LOG_USER); |
---|
| 110 | |
---|
| 111 | initCache(); |
---|
[58] | 112 | |
---|
| 113 | if ( (routerNb = getRegisteredRouters(ROUTERS_LIST, |
---|
| 114 | &sortedRouterList[0], |
---|
| 115 | &indexedRouterList[0])) == -1) { |
---|
| 116 | fprintf(stderr, |
---|
| 117 | "ERROR in getRegisteredRouters from file %s\n", |
---|
| 118 | ROUTERS_LIST); |
---|
| 119 | exit(-1); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | for (i=0; i<routerNb; i++){ |
---|
| 123 | routersListPtr = addRouter(routersListPtr, sortedRouterList[i]); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
[24] | 127 | key = 8765; |
---|
| 128 | if ((shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666)) < 0) { |
---|
| 129 | perror("shmget"); |
---|
| 130 | exit(1); |
---|
| 131 | } |
---|
| 132 | if ((shmForAgg = (struct SHMForAgg *)shmat(shmid, (void *)0, 0)) == (void *) -1) { |
---|
| 133 | perror("shmat"); |
---|
| 134 | exit(1); |
---|
| 135 | } |
---|
| 136 | |
---|
[61] | 137 | if ( (shmForAgg->v4PrefixNb = |
---|
| 138 | getPrefixV4(SUBNETS_LIST, |
---|
| 139 | &(shmForAgg->prefixV4Tab[0][0]), |
---|
| 140 | &(shmForAgg->prefixV4SubnetTab[0][0]), |
---|
| 141 | &(shmForAgg->v4SubnetNb) |
---|
| 142 | )) < 1){ |
---|
[59] | 143 | fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST); |
---|
| 144 | } else { |
---|
[61] | 145 | fprintf(stderr, "We have found %hu supernets from %s\n", |
---|
[59] | 146 | shmForAgg->v4PrefixNb, |
---|
| 147 | SUBNETS_LIST); |
---|
[61] | 148 | fprintf(stderr, "and %hu subnets\n", |
---|
| 149 | shmForAgg->v4SubnetNb); |
---|
[2] | 150 | } |
---|
[61] | 151 | if ( (shmForAgg->v4PrefixNb = |
---|
| 152 | getPrefixV4(SUBNETS_LIST, |
---|
| 153 | &(shmForAgg->prefixV4Tab[1][0]), |
---|
| 154 | &(shmForAgg->prefixV4SubnetTab[1][0]), |
---|
| 155 | &(shmForAgg->v4SubnetNb) |
---|
| 156 | )) < 1){ |
---|
[59] | 157 | fprintf(stderr, "renetcol: Error in %s\n", SUBNETS_LIST); |
---|
[24] | 158 | exit(1); |
---|
| 159 | } |
---|
[61] | 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
[29] | 166 | |
---|
| 167 | shmForAgg->currentTable = 0; |
---|
| 168 | shmForAgg->secondTable = 1; |
---|
[47] | 169 | shmForAgg->readed = 0; |
---|
[29] | 170 | |
---|
[58] | 171 | |
---|
[23] | 172 | myPtrs.rulesListPtr = NULL; |
---|
[58] | 173 | myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE); |
---|
[23] | 174 | tmp = myPtrs.rulesListPtr; |
---|
[2] | 175 | for ( ; tmp->next; tmp=tmp->next) { |
---|
| 176 | if (tmp->type != 2) { |
---|
[23] | 177 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
[2] | 178 | } |
---|
| 179 | } |
---|
[36] | 180 | tmp = NULL; |
---|
[47] | 181 | #ifdef DEBUG |
---|
| 182 | printRule(rulesListPtr); |
---|
| 183 | #endif |
---|
[8] | 184 | |
---|
[60] | 185 | #ifdef ASACC |
---|
| 186 | |
---|
| 187 | if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[0][0]))) < 1){ |
---|
| 188 | fprintf(stderr, "renetcol: Error in %s\n", AS_LIST); |
---|
| 189 | exit(1); |
---|
| 190 | } else { |
---|
| 191 | fprintf(stderr, "%hu AS are readed from %s\n", |
---|
| 192 | shmForAgg->ASNb, |
---|
| 193 | AS_LIST); |
---|
| 194 | } |
---|
| 195 | if ( (shmForAgg->ASNb = getAS(AS_LIST, &(shmForAgg->ASTab[1][0]))) < 1){ |
---|
| 196 | fprintf(stderr, "renetcol: Error in %s\n", AS_LIST); |
---|
| 197 | exit(1); |
---|
| 198 | } |
---|
| 199 | #endif |
---|
| 200 | |
---|
[58] | 201 | |
---|
[75] | 202 | fprintf(stderr, "renetcol: I become a deamon, next messages via syslogd. Bye.\n"); |
---|
[8] | 203 | if (fork () != 0) |
---|
| 204 | exit (0); |
---|
| 205 | if (setsid() == -1){ |
---|
| 206 | exit(4); |
---|
| 207 | } |
---|
| 208 | |
---|
[15] | 209 | |
---|
[8] | 210 | myAction.sa_handler = sigusr1Mgmt; |
---|
| 211 | myAction.sa_flags = SA_RESTART; |
---|
| 212 | sigemptyset (&(myAction.sa_mask)); |
---|
| 213 | sigaddset (&(myAction.sa_mask), SIGALRM); |
---|
| 214 | sigaction (SIGUSR1, &myAction, NULL); |
---|
| 215 | |
---|
[58] | 216 | |
---|
[23] | 217 | setCache(myPtrs.rulesListPtr); |
---|
| 218 | myPtrs.rulesAddressPtr = (RuleDefPtr *)rulesAddress; |
---|
[2] | 219 | |
---|
[58] | 220 | |
---|
[2] | 221 | myKey = createKey(argv[0]); |
---|
| 222 | myQueue = createQueue(myKey); |
---|
[8] | 223 | sendMyPid(myQueue); |
---|
[2] | 224 | |
---|
[58] | 225 | |
---|
[76] | 226 | #ifdef IPV4AGGIDSNMP |
---|
[58] | 227 | k = getSNMPIndexList(INTERFACES_TYPE_LIST, routersListPtr); |
---|
[76] | 228 | #endif |
---|
[2] | 229 | |
---|
[23] | 230 | myPtrs.currentRouterPtr = routersListPtr; |
---|
| 231 | myPtrs.currentHeaderV9Ptr = (NetFlowV9HeaderPtr) |
---|
[2] | 232 | malloc(sizeof(struct NetFlowV9Header)); |
---|
[23] | 233 | myPtrs.offsetV9Ptr = &offsetV9; |
---|
| 234 | myPtrs.currentFlowsetIdPtr = ¤tFlowsetId; |
---|
| 235 | myPtrs.pcktPtr = (DatagramPtr) malloc(sizeof(struct Datagram)); |
---|
| 236 | myPtrs.currentFlowsetNumberPtr = ¤tFlowsetNumber; |
---|
[59] | 237 | #ifdef IPV4AGGIDR |
---|
[27] | 238 | myPtrs.routersID = &indexedRouterList; |
---|
[59] | 239 | #endif |
---|
[29] | 240 | myPtrs.currentV4Tab = shmForAgg->prefixV4Tab[shmForAgg->currentTable]; |
---|
[36] | 241 | myPtrs.secondV4Tab = shmForAgg->prefixV4Tab[shmForAgg->secondTable]; |
---|
[60] | 242 | #ifdef ASACC |
---|
| 243 | myPtrs.asNb = shmForAgg->ASNb; |
---|
| 244 | myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable]; |
---|
| 245 | myPtrs.secondASTab = shmForAgg->ASTab[shmForAgg->secondTable]; |
---|
| 246 | #endif |
---|
[59] | 247 | #ifdef MATRIX |
---|
[36] | 248 | myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]); |
---|
[59] | 249 | #endif |
---|
[36] | 250 | |
---|
[58] | 251 | |
---|
[59] | 252 | #ifdef MATRIX |
---|
[36] | 253 | for (i=0; i<ROUTER_INDEX_MAX; i++){ |
---|
| 254 | for (j=0; j<ROUTER_INDEX_MAX; j++) { |
---|
| 255 | shmForAgg->matrixPOP[0][i][j].bytesNb = 0; |
---|
| 256 | shmForAgg->matrixPOP[0][i][j].pktsNb = 0; |
---|
| 257 | shmForAgg->matrixPOP[0][i][j].flowNb = 0; |
---|
| 258 | shmForAgg->matrixPOP[1][i][j].bytesNb = 0; |
---|
| 259 | shmForAgg->matrixPOP[1][i][j].pktsNb = 0; |
---|
| 260 | shmForAgg->matrixPOP[1][i][j].flowNb = 0; |
---|
| 261 | } |
---|
| 262 | } |
---|
[59] | 263 | #endif |
---|
[36] | 264 | |
---|
[58] | 265 | |
---|
[23] | 266 | if (myPtrs.pcktPtr==NULL) { |
---|
[2] | 267 | fprintf(stderr, "ERROR in struct Datagram allocation\n"); |
---|
| 268 | exit(1); |
---|
| 269 | } else { |
---|
[23] | 270 | myPtrs.pcktPtr->ipH = (IpHeaderPtr) malloc(sizeof(struct IpHeader)); |
---|
| 271 | myPtrs.pcktPtr->udp_header = (UdpHeaderPtr) malloc(sizeof(struct UdpHeader)); |
---|
[2] | 272 | } |
---|
[23] | 273 | if (! (myPtrs.ptr_buffer = malloc(sockBufSize))) |
---|
[2] | 274 | { |
---|
| 275 | printf("ERROR during socket buffer allocation\n"); |
---|
| 276 | exit(2); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | initStream(); |
---|
| 281 | |
---|
[23] | 282 | socketLoop(); |
---|
[2] | 283 | |
---|
| 284 | closelog(); |
---|
[23] | 285 | |
---|
[2] | 286 | fprintf(stderr, "END\n"); |
---|
[23] | 287 | |
---|
[2] | 288 | return (0); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | |
---|
[23] | 292 | |
---|
[2] | 293 | |
---|
[23] | 294 | |
---|
| 295 | |
---|
[2] | 296 | |
---|
[23] | 297 | void |
---|
| 298 | sendMyPid(int queueID) |
---|
[8] | 299 | { |
---|
| 300 | msgType myMsg; |
---|
| 301 | char *msgTextIndex; |
---|
| 302 | unsigned short tplMsgType = 12; |
---|
| 303 | pid_t myPID; |
---|
| 304 | |
---|
| 305 | myPID = getpid(); |
---|
| 306 | msgTextIndex = mempcpy(mempcpy(myMsg.text, |
---|
| 307 | &tplMsgType, |
---|
| 308 | sizeof (unsigned short) |
---|
| 309 | ), |
---|
| 310 | &myPID, |
---|
| 311 | sizeof(pid_t) |
---|
| 312 | ); |
---|
| 313 | myMsg.type = 1; |
---|
| 314 | msgSend(queueID, myMsg); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
[23] | 318 | |
---|
[8] | 319 | |
---|
[23] | 320 | |
---|
| 321 | |
---|
[8] | 322 | |
---|
| 323 | void sendReadRulesSignal(int queueID) |
---|
| 324 | { |
---|
| 325 | msgType myMsg; |
---|
| 326 | char *msgTextIndex; |
---|
| 327 | unsigned short tplMsgType = 13; |
---|
| 328 | |
---|
| 329 | msgTextIndex = mempcpy(myMsg.text, |
---|
| 330 | &tplMsgType, |
---|
| 331 | sizeof (unsigned short) |
---|
| 332 | ); |
---|
| 333 | myMsg.type = 1; |
---|
| 334 | msgSend(queueID, myMsg); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | |
---|
[23] | 338 | |
---|
[8] | 339 | |
---|
[23] | 340 | void |
---|
| 341 | sigusr1Mgmt(int num) |
---|
[8] | 342 | { |
---|
| 343 | sigusr1Up = 1; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
[23] | 347 | |
---|
[8] | 348 | |
---|
| 349 | void |
---|
| 350 | initRule() |
---|
| 351 | { |
---|
| 352 | RulesPtr tmp = NULL; |
---|
| 353 | initCache(); |
---|
[23] | 354 | tmp = myPtrs.rulesListPtr; |
---|
[8] | 355 | for ( ; tmp; tmp=tmp->next) { |
---|
[23] | 356 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
[8] | 357 | } |
---|
[23] | 358 | myPtrs.rulesListPtr = NULL; |
---|
[58] | 359 | myPtrs.rulesListPtr = getRules(myPtrs.rulesListPtr, RULES_FILE); |
---|
[23] | 360 | tmp = myPtrs.rulesListPtr; |
---|
[8] | 361 | for ( ; tmp; tmp=tmp->next) { |
---|
| 362 | if (tmp->type != 2) { |
---|
[23] | 363 | myPtrs.rulesListPtr = delRule(tmp, myPtrs.rulesListPtr); |
---|
[8] | 364 | } |
---|
| 365 | } |
---|
| 366 | tmp = NULL; |
---|
| 367 | initCache(); |
---|
[23] | 368 | setCache(myPtrs.rulesListPtr); |
---|
[8] | 369 | sendReadRulesSignal(myQueue); |
---|
[47] | 370 | #ifdef DEBUG |
---|
| 371 | printRule(myPtrs.rulesListPtr); |
---|
| 372 | #endif |
---|
[8] | 373 | } |
---|
| 374 | |
---|
| 375 | |
---|
[23] | 376 | |
---|
[8] | 377 | |
---|
[2] | 378 | void |
---|
| 379 | initCache() |
---|
| 380 | { |
---|
| 381 | int i,j; |
---|
| 382 | for (i=0; i<FIELD_TYPE_NUMBER+1; i++){ |
---|
| 383 | for (j=0; j<MAX_RULES_PER_FIELD; j++){ |
---|
| 384 | rulesAddress[i][j] = NULL; |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | |
---|
[18] | 390 | |
---|
[2] | 391 | |
---|
| 392 | void |
---|
| 393 | setCache(RulesPtr rPtr) |
---|
| 394 | { |
---|
| 395 | int i=0; |
---|
| 396 | RulesPtr tmp = rPtr; |
---|
| 397 | RuleDefPtr def = NULL; |
---|
| 398 | |
---|
[47] | 399 | #ifdef DEBUG |
---|
| 400 | fprintf(stderr,"Cache for Rules\n"); |
---|
| 401 | #endif |
---|
[2] | 402 | while (tmp) { |
---|
| 403 | def = tmp->def; |
---|
| 404 | while (def) { |
---|
| 405 | i = 0; |
---|
| 406 | while (rulesAddress[def->fieldType][i] != NULL){ |
---|
| 407 | i++; |
---|
| 408 | } |
---|
| 409 | rulesAddress[def->fieldType][i] = def; |
---|
| 410 | def = def->next; |
---|
[47] | 411 | #ifdef DEBUG |
---|
| 412 | fprintf(stderr,"%d: %d ", def->fieldType, i); |
---|
| 413 | #endif |
---|
[2] | 414 | } |
---|
[47] | 415 | #ifdef DEBUG |
---|
| 416 | fprintf(stderr,"\n"); |
---|
| 417 | #endif |
---|
[2] | 418 | tmp = tmp->next; |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | void |
---|
| 426 | initStream() |
---|
| 427 | { |
---|
| 428 | static unsigned short n0, n1, n2, n3; |
---|
[22] | 429 | |
---|
[2] | 430 | initSocket(); |
---|
| 431 | if (sscanf(receptAddress,"%hu.%hu.%hu.%hu",&n0,&n1,&n2,&n3)==0) { |
---|
| 432 | perror("sscanf"); |
---|
| 433 | } |
---|
| 434 | buffer4[0] = (unsigned char)n3; |
---|
| 435 | buffer4[1] = (unsigned char)n2; |
---|
| 436 | buffer4[2] = (unsigned char)n1; |
---|
| 437 | buffer4[3] = (unsigned char)n0; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | |
---|
| 442 | |
---|
| 443 | int |
---|
| 444 | socketLoop() |
---|
| 445 | { |
---|
[27] | 446 | short shift; |
---|
[2] | 447 | short version = 0; |
---|
[8] | 448 | int regRouter = 0; |
---|
[19] | 449 | time_t now = time((time_t *)NULL); |
---|
[23] | 450 | int loopNb = 0; |
---|
[27] | 451 | int gardeFou = 0; |
---|
[19] | 452 | time_t lastIPOutput, lastMPLSOutput; |
---|
| 453 | |
---|
| 454 | now = time((time_t *)NULL); |
---|
[22] | 455 | tmPtr = localtime(&now); |
---|
| 456 | currentIntervalle = tmPtr->tm_min; |
---|
[19] | 457 | lastMPLSOutput = now; |
---|
| 458 | lastIPOutput = now; |
---|
[2] | 459 | do { |
---|
[28] | 460 | #ifdef DEBUG |
---|
| 461 | fprintf (stderr, "["); |
---|
| 462 | #endif |
---|
[24] | 463 | if ( ((tmPtr->tm_min)%STEP == 0) && (currentIntervalle != tmPtr->tm_min)){ |
---|
[22] | 464 | currentIntervalle = tmPtr->tm_min; |
---|
[24] | 465 | if (shmForAgg->currentTable == 0) { |
---|
| 466 | shmForAgg->currentTable = 1; |
---|
[29] | 467 | shmForAgg->secondTable = 0; |
---|
[24] | 468 | }else{ |
---|
| 469 | shmForAgg->currentTable = 0; |
---|
[29] | 470 | shmForAgg->secondTable = 1; |
---|
[24] | 471 | } |
---|
| 472 | shmForAgg->readed = 1; |
---|
[60] | 473 | #ifdef ASACC |
---|
| 474 | myPtrs.currentASTab = shmForAgg->ASTab[shmForAgg->currentTable]; |
---|
| 475 | #endif |
---|
[59] | 476 | #ifdef MATRIX |
---|
[36] | 477 | myPtrs.matrixPOP = &(shmForAgg->matrixPOP[shmForAgg->currentTable][0][0]); |
---|
[59] | 478 | #endif |
---|
[22] | 479 | } |
---|
[8] | 480 | if (sigusr1Up == 1){ |
---|
| 481 | sigusr1Up = 0; |
---|
| 482 | initRule(); |
---|
| 483 | } |
---|
[2] | 484 | socketReading(); |
---|
[23] | 485 | getIpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer); |
---|
| 486 | regRouter = checkIpHeader(myPtrs.pcktPtr, &sortedRouterList[0], routerNb); |
---|
| 487 | getUdpHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer); |
---|
| 488 | if ( checkUdpHeader(myPtrs.pcktPtr, regRouter, receptPort) == 1 ){ |
---|
| 489 | continue; |
---|
| 490 | } |
---|
[2] | 491 | |
---|
| 492 | switch( version = |
---|
[23] | 493 | getNetFlowHeader(myPtrs.pcktPtr, myPtrs.ptr_buffer, |
---|
| 494 | myPtrs.currentHeaderV9Ptr, myPtrs.offsetV9Ptr)){ |
---|
[2] | 495 | case 9: |
---|
[23] | 496 | if ((myPtrs.currentRouterPtr=notExistRouter(routersListPtr, |
---|
| 497 | myPtrs.pcktPtr->ipH->srcAdd))==NULL) { |
---|
| 498 | myPtrs.currentRouterPtr = routersListPtr; |
---|
[2] | 499 | } |
---|
| 500 | currentFlowsetNumber = 0; |
---|
| 501 | shift = 0; |
---|
[27] | 502 | gardeFou=0; |
---|
[23] | 503 | while ((currentFlowsetNumber < myPtrs.currentHeaderV9Ptr->count)) { |
---|
[27] | 504 | gardeFou++; |
---|
[2] | 505 | curTplFlSetPtr = NULL; |
---|
| 506 | currentFlowsetId=getFlowsetId(currentFlowsetNumber, |
---|
[23] | 507 | myPtrs.offsetV9Ptr, myPtrs.ptr_buffer); |
---|
[2] | 508 | if ( currentFlowsetId == 0 ) { |
---|
[57] | 509 | if ( (shift = checkTemplateFlowSet(myPtrs.currentRouterPtr, |
---|
| 510 | myPtrs.offsetV9Ptr, |
---|
| 511 | myPtrs.ptr_buffer, |
---|
| 512 | myPtrs.currentHeaderV9Ptr, |
---|
[27] | 513 | curTplFlSetPtr, |
---|
| 514 | myPtrs.currentFlowsetNumberPtr, |
---|
| 515 | myQueue)) < 0 ) { |
---|
[30] | 516 | #ifdef DEBUG |
---|
[28] | 517 | fprintf (stderr, "w>\n"); |
---|
| 518 | #endif |
---|
[27] | 519 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
| 520 | }; |
---|
| 521 | writeAllTplFlSet(); |
---|
[2] | 522 | } else if (currentFlowsetId == 1) { |
---|
[57] | 523 | if ( (shift = checkTemplateOption(myPtrs.currentRouterPtr, |
---|
| 524 | myPtrs.offsetV9Ptr, |
---|
| 525 | myPtrs.ptr_buffer, |
---|
| 526 | myPtrs.currentHeaderV9Ptr, |
---|
| 527 | curTplOptionPtr, |
---|
[27] | 528 | myPtrs.currentFlowsetNumberPtr) ) < 0 ) { |
---|
[30] | 529 | #ifdef DEBUG |
---|
[28] | 530 | fprintf (stderr, "wo>\n"); |
---|
[30] | 531 | #endif |
---|
[27] | 532 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
| 533 | } |
---|
[30] | 534 | } else if (currentFlowsetId > 255) { |
---|
[27] | 535 | if ( (shift = checkDataFlowSet(shift, |
---|
| 536 | &myPtrs, |
---|
| 537 | myQueue, |
---|
| 538 | shmForAgg->prefixV4Tab[shmForAgg->currentTable], |
---|
[61] | 539 | (size_t) shmForAgg->v4PrefixNb, |
---|
| 540 | shmForAgg->prefixV4SubnetTab[shmForAgg->currentTable], |
---|
| 541 | (size_t) shmForAgg->v4SubnetNb) ) < 0 ) { |
---|
[28] | 542 | #ifdef DEBUG |
---|
| 543 | fprintf (stderr, "wd>\n"); |
---|
| 544 | #endif |
---|
[27] | 545 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
| 546 | } |
---|
[30] | 547 | } else { |
---|
| 548 | #ifdef DEBUG |
---|
| 549 | fprintf (stderr, "1<<255\n"); |
---|
| 550 | #endif |
---|
| 551 | currentFlowsetNumber = myPtrs.currentHeaderV9Ptr->count + 1; |
---|
[2] | 552 | } |
---|
[27] | 553 | if ( gardeFou > 200) { exit(-1); } |
---|
[23] | 554 | } |
---|
[2] | 555 | break; |
---|
| 556 | default: |
---|
[23] | 557 | syslog(LOG_INFO, |
---|
| 558 | "NetFlow exp. version unknown: %hu, from router: %lu.%lu.%lu.%lu", |
---|
| 559 | (unsigned short) version, |
---|
| 560 | (myPtrs.pcktPtr->ipH->srcAdd>>24), |
---|
| 561 | (myPtrs.pcktPtr->ipH->srcAdd<<8>>24), |
---|
| 562 | (myPtrs.pcktPtr->ipH->srcAdd<<16>>24), |
---|
| 563 | (myPtrs.pcktPtr->ipH->srcAdd<<24>>24)); |
---|
[2] | 564 | break; |
---|
| 565 | } |
---|
[28] | 566 | if (loopNb > 1000 ){ |
---|
[23] | 567 | |
---|
| 568 | writeAllTplFlSet(); |
---|
[28] | 569 | #ifdef DEBUG |
---|
| 570 | fprintf(stderr, "W"); |
---|
| 571 | #endif |
---|
[23] | 572 | loopNb = 0; |
---|
| 573 | } |
---|
| 574 | loopNb++; |
---|
[18] | 575 | |
---|
[19] | 576 | now = time((time_t *)NULL); |
---|
[22] | 577 | tmPtr = localtime(&now); |
---|
[28] | 578 | #ifdef DEBUG |
---|
| 579 | fprintf (stderr, "]\n"); |
---|
| 580 | #endif |
---|
[2] | 581 | } while (1); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | |
---|
| 585 | |
---|
| 586 | |
---|
| 587 | short |
---|
| 588 | initSocket() |
---|
| 589 | { |
---|
| 590 | inputSock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); |
---|
| 591 | if (inputSock < 0) |
---|
| 592 | { |
---|
| 593 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
| 594 | exit(1); |
---|
| 595 | } |
---|
| 596 | memset((void*)&name, 0, sizeof(name)); |
---|
| 597 | name.sin_family = AF_INET; |
---|
| 598 | name.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
| 599 | if (name.sin_addr.s_addr == INADDR_NONE){ |
---|
| 600 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
| 601 | exit(1); |
---|
| 602 | } |
---|
| 603 | name.sin_port = htons(receptPort); |
---|
| 604 | if (bind(inputSock,(struct sockaddr *)(&name), sizeof(name)) < 0) |
---|
| 605 | { |
---|
| 606 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
| 607 | exit(1); |
---|
| 608 | } |
---|
| 609 | sockNamelg = sizeof(name); |
---|
| 610 | if (getsockname(inputSock, (struct sockaddr *) (&name), |
---|
| 611 | (socklen_t *)&sockNamelg) < 0) |
---|
| 612 | { |
---|
| 613 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
| 614 | exit(1); |
---|
| 615 | } |
---|
[17] | 616 | |
---|
[23] | 617 | |
---|
[17] | 618 | inputSock2 = socket(AF_INET, SOCK_DGRAM, 0); |
---|
| 619 | if (inputSock2 < 0) |
---|
| 620 | { |
---|
| 621 | syslog(LOG_ERR,"socket : %s", strerror(errno)); |
---|
| 622 | exit(1); |
---|
| 623 | } |
---|
| 624 | memset((void*)&name, 0, sizeof(name)); |
---|
| 625 | name2.sin_family = AF_INET; |
---|
| 626 | name2.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
| 627 | if (name2.sin_addr.s_addr == INADDR_NONE){ |
---|
| 628 | syslog(LOG_ERR, " INADDR_NONE "); |
---|
| 629 | exit(1); |
---|
| 630 | } |
---|
| 631 | name2.sin_port = htons(receptPort); |
---|
| 632 | if (bind(inputSock2,(struct sockaddr *)(&name2), sizeof(name2)) < 0) |
---|
| 633 | { |
---|
| 634 | syslog(LOG_ERR, "bind : %s", strerror(errno)); |
---|
| 635 | exit(1); |
---|
| 636 | } |
---|
| 637 | sockNamelg = sizeof(name2); |
---|
| 638 | if (getsockname(inputSock2, (struct sockaddr *) (&name2), |
---|
| 639 | (socklen_t *)&sockNamelg) < 0) |
---|
| 640 | { |
---|
| 641 | syslog(LOG_ERR, "getsockname: %s", strerror(errno)); |
---|
| 642 | exit(1); |
---|
| 643 | } |
---|
[2] | 644 | return(0); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | |
---|
| 648 | |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | short |
---|
| 652 | socketReading() |
---|
| 653 | { |
---|
| 654 | signed short sockLg; |
---|
| 655 | |
---|
| 656 | sockNameFromlg = sizeof(fromName); |
---|
[23] | 657 | rcv = recvfrom(inputSock, myPtrs.ptr_buffer, sockBufSize, 0, |
---|
[2] | 658 | (struct sockaddr *)(&fromName), |
---|
| 659 | (socklen_t *)&sockNameFromlg); |
---|
| 660 | sockLg = rcv; |
---|
| 661 | if (sockLg < 0) { |
---|
| 662 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
| 663 | exit(1); |
---|
| 664 | } |
---|
| 665 | if (sockLg == 0) { |
---|
| 666 | syslog(LOG_ERR,"recvfrom : %s", strerror(errno)); |
---|
| 667 | exit(1); |
---|
| 668 | } |
---|
| 669 | return(0); |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | |
---|
[23] | 673 | |
---|
[2] | 674 | |
---|
| 675 | short |
---|
| 676 | checkFlow(short flowNumber) |
---|
| 677 | { |
---|
[23] | 678 | return(0); |
---|
[2] | 679 | } |
---|
| 680 | |
---|
| 681 | |
---|
| 682 | |
---|
| 683 | |
---|
[23] | 684 | |
---|
[2] | 685 | |
---|
| 686 | void |
---|
| 687 | showAllTplFlSet() |
---|
| 688 | { |
---|
| 689 | RouterPtr tmp = routersListPtr; |
---|
| 690 | TplFlowSetPtr tmpFS; |
---|
| 691 | TplOptionPtr tmpOP; |
---|
[22] | 692 | fprintf(stderr,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n"); |
---|
[2] | 693 | for (; tmp; tmp=tmp->next) { |
---|
[22] | 694 | fprintf(stderr,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n", |
---|
[2] | 695 | (tmp->IpAddress>>24), |
---|
| 696 | (tmp->IpAddress<<8>>24), |
---|
| 697 | (tmp->IpAddress<<16>>24), |
---|
| 698 | (tmp->IpAddress<<24>>24)); |
---|
| 699 | tmpFS = tmp->tplList; |
---|
| 700 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
[22] | 701 | fprintf(stderr,"TId %hu (sourceId: %lu):\n", |
---|
| 702 | tmpFS->templateFlowSetId, |
---|
| 703 | tmpFS->sourceId); |
---|
[23] | 704 | printFieldSet(stderr, tmpFS->fieldSet); |
---|
[2] | 705 | fprintf(stderr,"\n"); |
---|
| 706 | } |
---|
| 707 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
| 708 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
[22] | 709 | fprintf(stderr,"OpTId %hu (sourceId: %lu) >\n", |
---|
| 710 | tmpOP->templateOptionId, |
---|
| 711 | tmpOP->sourceId); |
---|
[23] | 712 | printFieldSet(stderr, tmpOP->fieldSet); |
---|
[2] | 713 | fprintf(stderr,"\n"); |
---|
| 714 | } |
---|
| 715 | fprintf(stderr,"\n"); |
---|
| 716 | } |
---|
| 717 | } |
---|
| 718 | } |
---|
[23] | 719 | |
---|
| 720 | |
---|
| 721 | |
---|
| 722 | |
---|
| 723 | |
---|
| 724 | |
---|
| 725 | void |
---|
| 726 | writeAllTplFlSet() |
---|
| 727 | { |
---|
| 728 | |
---|
| 729 | RouterPtr tmp = routersListPtr; |
---|
| 730 | TplFlowSetPtr tmpFS; |
---|
| 731 | TplOptionPtr tmpOP; |
---|
| 732 | FILE *TPLFILE; |
---|
| 733 | if (!(TPLFILE = fopen("/tmp/TemplateDef.txt", "w"))) { |
---|
| 734 | syslog (LOG_ERR, "error during %s opening", "/tmp/TemplateDef.txt"); |
---|
| 735 | } |
---|
| 736 | fprintf(TPLFILE,"\n*********************************************\n* All template definitions: (field, size) : *\n*********************************************\n"); |
---|
| 737 | for (; tmp; tmp=tmp->next) { |
---|
| 738 | fprintf(TPLFILE,"----------------------\nrouter %lu.%lu.%lu.%lu : \n----------------------\n", |
---|
| 739 | (tmp->IpAddress>>24), |
---|
| 740 | (tmp->IpAddress<<8>>24), |
---|
| 741 | (tmp->IpAddress<<16>>24), |
---|
| 742 | (tmp->IpAddress<<24>>24)); |
---|
| 743 | tmpFS = tmp->tplList; |
---|
| 744 | for (; tmpFS; tmpFS=tmpFS->next) { |
---|
| 745 | fprintf(TPLFILE,"TId %hu (sourceId: %lu):\n", |
---|
| 746 | tmpFS->templateFlowSetId, |
---|
| 747 | tmpFS->sourceId); |
---|
| 748 | printFieldSet(TPLFILE, tmpFS->fieldSet); |
---|
| 749 | fprintf(TPLFILE,"\n"); |
---|
| 750 | } |
---|
| 751 | if ((tmpOP = tmp->tplOptList) != NULL){ |
---|
| 752 | for (; tmpOP; tmpOP=tmpOP->next) { |
---|
[29] | 753 | fprintf(TPLFILE,"OpTId %hu (sourceId: %lu) last %d brackets are Scope >\n lg: %hu, opScopeLg: %hu, opLg: %hu\n", |
---|
[23] | 754 | tmpOP->templateOptionId, |
---|
[27] | 755 | tmpOP->sourceId, |
---|
[29] | 756 | tmpOP->optionScopeLg/4, |
---|
[27] | 757 | tmpOP->length, |
---|
| 758 | tmpOP->optionScopeLg, |
---|
| 759 | tmpOP->optionLg); |
---|
[23] | 760 | printFieldSet(TPLFILE, tmpOP->fieldSet); |
---|
| 761 | fprintf(TPLFILE,"\n"); |
---|
| 762 | } |
---|
| 763 | fprintf(TPLFILE,"\n"); |
---|
| 764 | } |
---|
| 765 | } |
---|
| 766 | fclose(TPLFILE); |
---|
| 767 | } |
---|