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