Debian Codegraph @

src/ITGSend/ITGSend.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Component of the D-ITG 2.8.1 (r1023) platform (http://traffic.comics.unina.it/software/ITG)
3 *
4 * Copyright : (C) 2004-2013 by Alessio Botta, Walter de Donato, Alberto Dainotti,
5 * Stefano Avallone, Antonio Pescape' (PI)
6 * COMICS (COMputer for Interaction and CommunicationS) Group
7 * Department of Electrical Engineering and Information Technologies
8 * University of Napoli "Federico II".
9 * email : [email protected], [email protected], [email protected],
10 * [email protected], [email protected]
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * For commercial use please refer to D-ITG Professional.
26 */
27
28
29
30
31 #include "../common/ITG.h"
32 #include "../common/debug.h"
33
34 #ifdef UNIX
35 #include <netdb.h>
36 #include "newran/newran.h"
37 #include <sys/wait.h>
38 #include <math.h>
39 #ifdef SCTP
40 #include <netinet/sctp.h>
41 #endif
42 #include <netinet/tcp.h>
43 #include <netinet/ip.h>
44 #include <sys/shm.h>
45 #include <pthread.h>
46 #include <sys/msg.h>
47 #include <sys/ipc.h>
48 #endif
49
50 #ifdef WIN32
51 #include "newran/newran.h"
52 #include <math.h>
53 #include <sys/timeb.h>
54 #include <time.h>
55 #include <signal.h>
56 #endif
57
58 #include "../common/thread.h"
59 #include "traffic.h"
60 #include "../common/pipes.h"
61 #include "ITGSend.h"
62 #include "../common/timestamp.h"
63 #include "../common/serial.h"
64
65 #ifdef UNIX
66 const char DefaultLogFile[] = "/tmp/ITGSend.log";
67 const char DefaultRecvLogFile[] = "/tmp/ITGRecv.log";
68 #endif
69 #ifdef WIN32
70 char DefaultLogFile[] = "ITGSend.log";
71 char DefaultRecvLogFile[] = "ITGRecv.log";
72 #endif
73 const char DefaultDestIP[] = DEFAULT_DEST_IP;
74
75
76 flowDescriptor flows[MAX_NUM_THREAD];
77 signalChannel signalChannels[MAX_NUM_THREAD];
78 pthread_t h_flowParser[MAX_NUM_THREAD];
79 flowParserParams parserParams[MAX_NUM_THREAD];
80 int multiFlows = 0;
81 int flowCount = 0;
82 struct addrinfo *logHost = 0;
83 struct sockaddr_in ManagerIP;
84 socklen_t ManagerIPslen = sizeof(ManagerIP);
85 int isFirst = 1;
86 int isFirstThread = 1;
87 int isFirstThreadRttm = 1;
88 int signalChanCount = 0;
89 addrinfo hint;
90 char logFile[DIM_LOG_FILE];
91 int logging = 0;
92 uint16_t logbuffer_size = 50;
93 int logremoto = 0;
94 int logSock;
95 int logSockSignaling;
96 int namelogReceiver = 0;
97 int payloadLogType = 0;
98 int mean_adjustment = 1;
99 bool setPriority = false;
100 ofstream out;
101 BYTE protoTxLog = DEFAULT_PROTOCOL_TX_LOG;
102 BYTE protoTx_ServerLogReceiver = DEFAULT_PROTOCOL_TX_LOG_OPZ;
103 struct addrinfo *serverLogReceiver = 0;
104
105 char logFileReceiver[DIM_LOG_FILE];
106 int logServer = 0;
107 int managerMode;
108 bool cmdMode = false;
109 int managerSock;
110
111 bool passiveMode = false;
112 bool okToNewSignalingChan = true;
113 char globalSigAddr[INET6_ADDRSTRLEN];
114
115 struct timeval RifTime;
116 #ifdef WIN32
117 LARGE_INTEGER _tstart_, _tend_;
118 unsigned long _sec_ = 0, _msec_ = 0;
119 int first = 1;
120 #endif
121
122 #ifdef UNIX
123
124 #ifdef SCTP
125 typedef union {
126 struct sockaddr_in v4;
127 struct sockaddr_in6 v6;
128 struct sockaddr sa;
129 } sockaddr_storage_t;
130
131 sctpSession sctpSessions[MAX_NUM_THREAD];
132 pthread_mutex_t mutexSctp = PTHREAD_MUTEX_INITIALIZER;
133 #endif
134
135 pthread_mutex_t mutex;
136 pthread_mutex_t mutexLog;
137 pthread_mutex_t mutexErrLog;
138 pthread_mutex_t mutexLogRem;
139 pthread_mutex_t mutexBufferPayload;
140 #endif
141 #ifdef WIN32
142 HANDLE mutex;
143 HANDLE mutexLog;
144 HANDLE mutexLogRem;
145 HANDLE mutexBufferPayload;
146 #endif
147 int termsock = 0;
148 pthread_t pkey;
149 char nameProgram[]="ITGSend";
150
151 void* waitStopKey(void* s)
152 {
153
154 #ifdef WIN32
155 Sleep(1000);
156 #endif
157 PRINTD(1, "waitStopKey: Waiting for the stop key\n");
158 while (getchar() != 'C'){}
159 termsock = 1;
160 printf("Terminated by request\n");
161 Terminate(0);
162 return NULL;
163 }
164
165
166 int modeManager(int argc, char *argv[])
167 {
168
169 flowCount = 0;
170
171 managerMode = 1;
172 multiFlows = 1;
173 struct addrinfo* localHost = 0;
174 int size_r = 0;
175 int size_s = 0;
176 char buffer[MAX_FLOW_LINE_SIZE];
177
178 bool toFree;
179
180 signal(SIGINT, Terminate);
181 printf("Press Ctrl-C to terminate\n");
182 fflush(stdout);
183
184 argv++;
185 argc--;
186 parserMultiFlows(argv, argc);
187
188
189 getaddrinfo("0.0.0.0", NULL, &hint, &localHost);
190 managerSock = socket(localHost->ai_family, SOCK_DGRAM, 0);
191 if (managerSock <0) {
192 printf("error socket()!\n");
193 fflush(stdout);
194 }
195
196
197 SET_PORT(localHost, htons(DEFAULT_PORT_SENDER_MANAGER));
198
199 if (bind(managerSock, localHost->ai_addr, localHost->ai_addrlen)) {
200 printf("error bind()!\n");
201 }
202
203
204 do {
205 char *endline;
206 size_r = recvfrom(managerSock, buffer, MAX_FLOW_LINE_SIZE-3, 0,(struct sockaddr *) &ManagerIP, &ManagerIPslen);
207 PRINTD(1,"modeManager: Data received from SignalManager %d, \n",size_r);
208
209 if ((endline = strstr(buffer,"\n")) != NULL) {
210 size_r = (int)(endline - buffer);
211 }
212 buffer[size_r] = '\n';
213 buffer[size_r + 1] = '\0';
214
215 strncpy(parserParams[flowCount].line, buffer, MAX_FLOW_LINE_SIZE-3);
216
217 parserParams[flowCount].flowId = flowCount + 1;
218
219
220 memmove(buffer + 2 * sizeof(int), buffer, size_r);
221 ((uint8_t *) buffer)[0] = MNG_FLOWSTART;
222 ((uint16_t *) buffer)[1] = size_r;
223
224 size_s = sendto(managerSock, buffer, size_r + 3, 0, (struct sockaddr *) &ManagerIP, ManagerIPslen);
225 if (size_s < 0) {
226 perror("modeManager: error while sending ACK to the manager");
227 }
228 PRINTD(1,"modeManager: Notify ITGManager about the start of the generation %d, \n",size_s);
229
230 CREATE_THREAD(&parserParams[flowCount], flowParser, NULL, h_flowParser[flowCount], true);
231 if (h_flowParser[flowCount] < 0) {
232 cerr << "Cannot create a process to handle flow " << flowCount +
233 1 << endl;
234 }
235 flowCount++;
236
237
238 MUTEX_THREAD_LOCK(mutexBufferPayload);
239 for (int j = 1; j <= flowCount; j++) {
240 toFree = true;
241 if (flows[j].ptrPayloadBuffer != NULL) {
242 for (int k = 1; k <= flowCount; k++) {
243 if ((flows[j].ptrPayloadBuffer == flows[k].ptrPayloadBuffer) && (j != k)) {
244 toFree = false;
245 }
246 }
247 if (toFree) {
248 free(flows[j].ptrPayloadBuffer);
249 }
250 }
251 }
252 MUTEX_THREAD_UNLOCK(mutexBufferPayload);
253 } while (1);
254 return 0;
255 }
256
257
258 int modeScript(int argc, char *argv[])
259 {
260 char temp[MAX_SCRIPT_LINE_SIZE];
261 char line[MAX_SCRIPT_LINE_SIZE];
262
263 flowCount = 0;
264 multiFlows = 1;
265 FILE *batch;
266
267 if ((batch = fopen(argv[0], "r")) == NULL) {
268
269 perror("ITGServer Main: ");
270 memClean();
271 cerr << "Cannot open '" << argv[0] << "' for reading" << endl;
272 exit(1);
273 }
274 argv++;
275 argc--;
276 parserMultiFlows(argv, argc);
277
278 if (fgets(line, sizeof(line) - 1, batch) == NULL) {
279 cerr << "errore" << endl;
280 memClean();
281 exit(1);
282 }
283
284
285 strcpy(temp, line);
286 do {
287 if (strcmp(temp, "\n") > 0) {
288
289 strcpy(parserParams[flowCount].line, temp);
290
291 parserParams[flowCount].flowId = flowCount + 1;
292 CREATE_THREAD(&parserParams[flowCount], flowParser, NULL, h_flowParser[flowCount], false);
293 if (h_flowParser[flowCount] < 0) {
294 perror("ITGSend Main: ");
295 cerr << "Cannot create a process to handle flow " << flowCount + 1 << endl;
296 }
297 flowCount++;
298 }
299 } while (fgets(temp, sizeof(temp) - 1, batch));
300 fclose(batch);
301
302
303
304 joinThread(flowCount, h_flowParser);
305
306
307 for (int j = 1; j <= flowCount; j++) {
308 if (flows[j].ptrPayloadBuffer != NULL) {
309 free(flows[j].ptrPayloadBuffer);
310 }
311 }
312
313 return 0;
314 }
315
316
317 int modeCommandLine(int argc, char *argv[])
318 {
319 PRINTD(1,"modeCommandLine: mode started\n");
320 parserParams[0].flowId = 1;
321 cmdMode = true;
322 argvToString(argv, argc, parserParams[0].line);
323 flowParser(&parserParams[0]);
324
325 return (0);
326 }
327
328
329 int main(int argc, char *argv[])
330 {
331 printVersion("ITGSend");
332
333 strcpy(programName, argv[0]);
334 argv++;
335 argc--;
336 if (!argc) {
337 printf("\nMissing argument!!!\nTry ITGSend -h or --help for more information\n");
338 exit(1);
339 } else if (((argv[0][0] == '-') && (argv[0][1] == 'h')) || ((argv[0][0] == '-')
340 && (argv[0][1] == '-') && (argv[0][2] == 'h'))) {
341 printHelp();
342 exit(1);
343 }
344
345
346 MUTEX_THREAD_INIT(mutex);
347 MUTEX_THREAD_INIT(mutexLog);
348 MUTEX_THREAD_INIT(mutexLogRem);
349 MUTEX_THREAD_INIT(mutexBufferPayload);
350
351
352 memset(&hint, 0x00, sizeof(hint));
353 hint.ai_family = AF_UNSPEC;
354
355
356 TSTART( _tstart_, _sec_, _msec_, first, 1, 0);
357
358 GET_TIME_OF_DAY(&RifTime, _tend_, _tstart_, _sec_, _msec_, 1, 0);
359 PRINTD(1,"main: SignalingTime Time: %lu.%06lu sec\n", (long unsigned int) RifTime.tv_sec, (long unsigned int) RifTime.tv_usec);
360
361 if (strcmp(argv[0],"-gui") == 0){
362
363 CREATE_THREAD((void*)"wait", &waitStopKey, NULL, pkey, true);
364 }
365
366 strcpy(logFile, DefaultLogFile);
367 strcpy(logFileReceiver, DefaultRecvLogFile);
368 #ifdef WIN32
369 MUTEX_THREAD_INIT(mutex_numPipes);
370 if (!InitializeWinsock(MAKEWORD(1, 1))) {
371 printf("** ERROR ** WSAStartup() failed\n");
372 exit(1);
373 }
374 #endif
375
376 for (int i = 0; i < MAX_NUM_THREAD; i++) {
377 signalChannels[i].socket = -1;
378 signalChannels[i].flows = 0;
379 signalChannels[i].errorLog = false;
380
381 #ifdef SCTP
382 sctpSessions[i].parsedStreams = 0;
383 #endif
384 }
385
386
387 if ((argc > 0) && (argv[0][0] == '-') && (argv[0][1] == 'Q'))
388 modeManager(argc, argv);
389 else if ((argc > 0) && (argv[0][0] != '-') && (findL7Proto(argv[0]) == LX_ERROR_BYTE)) {
390 modeScript(argc, argv);
391 } else {
392 modeCommandLine(argc, argv);
393 }
394
395
396 Terminate(0);
397
398 }
399
400
401 void parserMultiFlows(char *argv[], int argc)
402 {
403 strcpy(logFile, DefaultLogFile);
404 getaddrinfo(DefaultDestIP, NULL, &hint, &logHost);
405 SET_PORT(logHost, htons(DEFAULT_LOG_PORT));
406 protoTxLog = DEFAULT_PROTOCOL_TX_LOG;
407
408 getaddrinfo(DefaultDestIP, NULL, &hint, &serverLogReceiver);
409 SET_PORT(serverLogReceiver, htons(DEFAULT_LOG_PORT));
410 protoTx_ServerLogReceiver = DEFAULT_PROTOCOL_TX_LOG_OPZ;
411
412 while (argc > 0) {
413 if (argv[0][0] == '-') {
414 switch (argv[0][1]) {
415 case 'h':
416 printHelp();
417 Terminate(0);
418 break;
419 case 'q':
420 if ((argc < 2) || (argv[1][0] == '-') || (findL7Proto(argv[1]) != LX_ERROR_BYTE)){
421 argc -= 1;
422 argv += 1;
423 } else {
424 logbuffer_size = atoi(argv[1]);
425 argc -= 2;
426 argv += 2;
427 }
428 break;
429 case 'l':
430 logging = 1;
431 if ((argc < 2) || (argv[1][0] == '-') || !findL7Proto(argv[1])) {
432 strcpy(logFile, DefaultLogFile);
433 argc -= 1;
434 argv += 1;
435 } else {
436 strcpy(logFile, argv[1]);
437 argc -= 2;
438 argv += 2;
439 }
440 break;
441 case 'L':
442 logging = 1;
443 logremoto = 1;
444
445 if ((argc < 2) || (argv[1][0] == '-') ||
446 (findL7Proto(argv[1]) != LX_ERROR_BYTE)){
447 argc -= 1;
448 argv += 1;
449 } else {
450
451 if (findL4Proto(argv[1]) == LX_ERROR_BYTE){
452 if (logHost)
453 freeaddrinfo(logHost);
454
455 if (getaddrinfo(argv[1], NULL, &hint, &logHost))
456 ReportErrorAndExit("General parser",
457 " Invalid log-server address or protocol", programName, 0);
458 argc -= 1;
459 argv += 1;
460 } else {
461
462 protoTxLog = findL4Proto(argv[1]);
463 argc -= 1;
464 argv += 1;
465 }
466 if ((argc >= 2) && (argv[1][0] != '-') && (findL7Proto(argv[1]) == LX_ERROR_BYTE)) {
467 if (findL4Proto(argv[1]) == LX_ERROR_BYTE){
468 if (logHost)
469 freeaddrinfo(logHost);
470
471 if (getaddrinfo(argv[1], NULL, &hint, &logHost))
472 ReportErrorAndExit("General parser",
473 " Invalid log-server address", programName, 0);
474 argc -= 2;
475 argv += 2;
476 } else {
477
478 protoTxLog = findL4Proto(argv[1]);
479 argc -= 2;
480 argv += 2;
481 }
482 }
483 else {
484 argc -= 1;
485 argv += 1;
486 }
487 }
488 break;
489 case 'X':
490 logServer = 1;
491
492 if ((argc < 2) || (argv[1][0] == '-') ||
493 (findL7Proto(argv[1]) != LX_ERROR_BYTE)){
494 argc -= 1;
495 argv += 1;
496 } else {
497
498 if (findL4Proto(argv[1]) == LX_ERROR_BYTE){
499 if (serverLogReceiver)
500 freeaddrinfo(serverLogReceiver);
501
502 if (getaddrinfo(argv[1], NULL, &hint, &serverLogReceiver))
503 ReportErrorAndExit("General parser",
504 " Invalid log-server address or protocol", programName, 0);
505 argc -= 1;
506 argv += 1;
507 } else {
508
509 protoTx_ServerLogReceiver = findL4Proto(argv[1]);
510 argc -= 1;
511 argv += 1;
512 }
513 if ((argc >= 2) && (argv[1][0] != '-') && (findL7Proto(argv[1]) == LX_ERROR_BYTE)) {
514 if (findL4Proto(argv[1]) == LX_ERROR_BYTE){
515 if (serverLogReceiver)
516 freeaddrinfo(serverLogReceiver);
517
518 if (getaddrinfo(argv[1], NULL, &hint, &serverLogReceiver))
519 ReportErrorAndExit("General parser",
520 " Invalid log-server address", programName, 0);
521 argc -= 2;
522 argv += 2;
523 } else {
524
525 protoTx_ServerLogReceiver = findL4Proto(argv[1]);
526 argc -= 2;
527 argv += 2;
528 }
529 }
530 else {
531 argc -= 1;
532 argv += 1;
533 }
534 }
535 break;
536 case 'x':
537 namelogReceiver = 1;
538 if ((argc < 2) || (argv[1][0] == '-') || !findL7Proto(argv[1])){
539 argc -= 1;
540 argv += 1;
541 } else {
542 strcpy(logFileReceiver, argv[1]);
543 argc -= 2;
544 argv += 2;
545 }
546 break;
547 default:
548 char tempS[sizeof("Option ") + sizeof(argv[0][1])+sizeof(" can not be used as a global option in the multy flow mode !")];
549 strcpy(tempS,"Option ");
550 strcat(tempS,argv[0]);
551 strcat(tempS, " can not be used as a global option in the multy flow mode !");
552 ReportErrorAndExit("General parser", tempS,programName, 0);
553 break;
554 }
555 }
556 }
557 }
558
559
560
561 void *flowParser(void *param)
562 {
563 int argc;
564 char *argv[200];
565 char *token;
566 flowParserParams *para = (flowParserParams *)param;
567 #ifdef WIN32
568 struct _timeb tstruct;
569 #endif
570 char *ManagerMsg = NULL;
571 double seed = 0.0;
572
573 int j = 0;
574 int h = 0;
575 int id = para->flowId;
576 unsigned long int Delay = DefaultDelay;
577 int chanId;
578 struct pipeMsg msg;
579
580 char *saveptr;
581
582 FILE * ptrFile = NULL;
583 char line[30];
584 int lineNumber=1;
585 uint32_t tmpInt;
586 Real tmpFloat;
587
588 bool SigDestHostSpecify=false;
589 uint16_t SigPort=0;
590
591 PRINTD(1,"flowParser: started\n");
592
593 flows[id].id = id;
594 flows[id].iface = NULL;
595 flows[id].meter = METER_OWDM;
596 flows[id].l7Proto = LX_PROTO_NONE;
597 flows[id].l4Proto = L4_PROTO_UDP;
598 flows[id].DSByte = DefaultDSByte;
599 flows[id].Duration = DefaultDuration;
600 flows[id].icmptype = 8;
601 flows[id].TTL = 0;
602 flows[id].Nagle = true;
603 flows[id].srcPortSpecify = false;
604 flows[id].srcAddrSpecify = false;
605 flows[id].dstAddrSpecify = false;
606 flows[id].dstPortSpecify = false;
607 flows[id].minPayloadSize = StandardMinPayloadSize;
608 flows[id].payloadLogType = payloadLogType;
609 flows[id].mean_adjustment = mean_adjustment;
610
611 strcpy(flows[id].serialReceiver,"noSerial");
612 flows[id].serial = INVALID_HANDLE_VALUE;
613
614 flows[id].ptrPayloadBuffer = NULL;
615 flows[id].payloadFile[0] = 0;
616 flows[id].dimPayloadBuffer = 0;
617
618 flows[id].pktSizeFile[0]='\0';
619 flows[id].vectSize.clear();
620 flows[id].timeFile[0]='\0';
621 flows[id].vectTime.clear();
622 flows[id].numPacket=0;
623
624 flows[id].KBToSend=0;
625
626 flows[id].SigSrcHost=NULL;
627 flows[id].SigSrcPort=0;
628 flows[id].SigInterface=NULL;
629
630 flows[id].pollingMode = false;
631
632
633 if (para->line[strlen(para->line) - 1] == '\n') {
634 para->line[strlen(para->line) - 1] = '\0';
635 } else {
636 cerr << " No newline at the end of file" << endl;
637 memClean();
638 exit(1);
639 }
640
641 if (managerMode) {
642 ManagerMsg = (char *) malloc(sizeof(char) * strlen(para->line) + sizeof(int) * 2);
643 strcpy(ManagerMsg, para->line);
644 }
645
646
647 getaddrinfo(DefaultDestIP, NULL, &hint, &flows[id].DestHost);
648 SET_PORT(flows[id].DestHost, htons(DEFAULT_PORT));
649
650
651 getaddrinfo(DefaultDestIP, NULL, &hint, &flows[id].SrcHost);
652
653 getaddrinfo(DefaultDestIP, NULL, &hint, &flows[id].SigDestHost);
654 SET_PORT(flows[id].SigDestHost, htons(DEFAULT_PORT_SIGNALING));
655
656
657 if (!multiFlows) {
658 strcpy(logFile, DefaultLogFile);
659 getaddrinfo(DefaultDestIP, NULL, &hint, &logHost);
660 SET_PORT(logHost, htons(DEFAULT_LOG_PORT));
661 protoTxLog = DEFAULT_PROTOCOL_TX_LOG;
662
663 getaddrinfo(DefaultDestIP, NULL, &hint, &serverLogReceiver);
664 SET_PORT(serverLogReceiver, htons(DEFAULT_LOG_PORT));
665 protoTx_ServerLogReceiver = DEFAULT_PROTOCOL_TX_LOG_OPZ;
666 }
667
668
669 flows[id].IntArrivDistro = pdConstant;
670 ConstantRV = new Constant(1);
671 flows[id].IntArriv = new SumRandom(1000.0 / DefaultPktPerSec * (*ConstantRV));
672
673
674 flows[id].PktSizeDistro = pdConstant;
675 ConstantRV = new Constant(1);
676 flows[id].PktSize = new SumRandom(DefaultPktSize * (*ConstantRV));
677
678
679 #ifdef BURSTY
680
681 flows[id].bursty = false;
682 #endif
683
684
685
686
687
688 token = strtok_r(para->line, cmdMode ? "^" : " ",&saveptr);
689
690 while (token != NULL) {
691 argv[j] = token;
692 PRINTD(1,"flowParser: TOKEN %d: %s\n", j, token);
693 j++;
694 token = strtok_r(NULL, cmdMode ? "^" : " ",&saveptr);
695 }
696 argc = j;
697
698
699 if (strcmp(argv[0],"-gui") == 0){
700 argc -= 1;
701 h += 1;
702 }
703
704 while (argc > 0) {
705 PRINTD(1,"flowParser: Parsing option %c \n",argv[h][1]);
706
707 if (argv[h][0] == '-') {
708 char *tail;
709
710 long int temp = 0;
711 Real a, b;
712 switch (argv[h][1]) {
713 case 'p':
714 if (argv[h][2] != 'o') {
715 PRINTD(1,"flowParser: Parsing of p option \n");
716 if ((argc < 2) || (argv[h+1][0] == '-'))
717 ReportErrorAndExit("General parser", "Invalid payload log type", programName, 0);
718 flows[id].payloadLogType = atoi(argv[h+1]);
719 if ((flows[id].payloadLogType < 0) || (flows[id].payloadLogType > 2)) {
720 printf("Warning !! Incorrect \"payload log type\" definition."
721 "Admissible values are: \n 0 normal\n; 1 short\n; 2 none \n. Default value %d assumed",
722 payloadLogType);
723 flows[id].payloadLogType = payloadLogType;
724 }
725 PRINTD(1,"flowParser: Payload type %d \n",flows[id].payloadLogType);
726 if ((flows[id].payloadLogType != 0) && (flows[id].meter == METER_RTTM ))
727 ReportErrorAndExit("General parser", "It is possible to use the RTT meter only with the standard payload type", programName, 0);
728 if (flows[id].payloadLogType == PL_SHORT)
729 flows[id].minPayloadSize = ShortMinPayloadSize;
730
731 else if (flows[id].payloadLogType == PL_NONE)
732 flows[id].minPayloadSize = NoneMinPayloadSize;
733
734 h += 2;
735 argc -= 2;
736 } else if (!strcmp(argv[h],"-poll")) {
737 PRINTD(1,"flowParser: Polling mode for flow n° %d\n",id);
738 flows[id].pollingMode=true;
739 h += 1;
740 argc -= 1;
741 }else{
742 ReportErrorAndExit("General parser", "Error in parsing \"p\" or \"-poll\" options", programName, id);
743 }
744 break;
745 case 'm':
746 PRINTD(1,"flowParser: Flow id %d \n ", id);
747 PRINTD(1,"flowParser: Meter option found\n");
748 flows[id].meter = findMeter(argv[h + 1]);
749 if (flows[id].meter == 255) {
750 ReportErrorAndExit("General parser", "Invalid measure type", programName, 0);
751 }
752 if ((flows[id].meter == METER_RTTM) && (flows[id].minPayloadSize < StandardMinPayloadSize)) {
753 printf("%d != %d ", flows[id].minPayloadSize, StandardMinPayloadSize);
754 ReportErrorAndExit("General parser", "It is possible to use the RTT meter only with the standard payload type",
755 programName, 0);
756 }
757 h += 2;
758 argc -= 2;
759 break;
760 case 'a':
761 {
762 in_port_t tmp_port=0;
763 if (flows[id].DestHost) {
764 GET_PORT(flows[id].DestHost, tmp_port);
765 freeaddrinfo(flows[id].DestHost);
766 }
767 if ((argc < 2) || getaddrinfo(argv[h + 1], NULL, &hint, &flows[id].DestHost))
768 ReportErrorAndExit("General parser", "Invalid destination address", programName, id);
769 #ifndef NOIPV6
770 if (flows[id].DestHost->ai_family == PF_INET6
771 && IN6_IS_ADDR_LINKLOCAL( &((struct sockaddr_in6 *) (flows[id].DestHost->ai_addr))->sin6_addr)
772 && ((struct sockaddr_in6 *) (flows[id].DestHost->ai_addr))->sin6_scope_id == 0) {
773 printf("** WARNING **: IPv6 address scope id not specified by the sender\n");
774 }
775 #endif
776
777
778 if (SigDestHostSpecify == false){
779 GET_PORT(flows[id].SigDestHost,SigPort);
780 freeaddrinfo(flows[id].SigDestHost);
781 if ((argc < 2) || getaddrinfo(argv[h + 1], NULL, &hint, &flows[id].SigDestHost))
782 ReportErrorAndExit("General parser",
783 "Invalid destination address for the signal channel",
784 programName, id);
785
786 #ifndef NOIPV6
787 if (flows[id].SigDestHost->ai_family == PF_INET6
788 && IN6_IS_ADDR_LINKLOCAL( &((struct sockaddr_in6 *) (flows[id].SigDestHost->ai_addr))->sin6_addr )
789 && ((struct sockaddr_in6 *) (flows[id].SigDestHost->ai_addr))->sin6_scope_id == 0) {
790 printf("** WARNING **: IPv6 address scope id not specified by the sender (signal channel)\n");
791 }
792 #endif
793 SET_PORT(flows[id].SigDestHost,SigPort);
794 }
795
796
797 SET_PORT(flows[id].DestHost, tmp_port);
798 flows[id].dstAddrSpecify = true;
799 h += 2;
800 argc -= 2;
801 break;
802 }
803 #ifdef UNIX
804 case 'i':
805 {
806 if (flows[id].srcAddrSpecify == false){
807 if (argc < 2)
808 ReportErrorAndExit("General parser", "Invalid interface", programName, id);
809 flows[id].iface = (char*)malloc(strlen(argv[h + 1]) + 1);
810 strcpy(flows[id].iface, argv[h + 1]);
811 h += 2;
812 argc -= 2;
813 }else
814 ReportErrorAndExit("General parser", "use -i <interface> OR -sa <source_address>", programName, id);
815 break;
816 }
817 #endif
818 case 'r':
819 if (argv[h][2] == 'p') {
820 if (argc < 2)
821 ReportErrorAndExit("General parser", "Invalid port number", programName, id);
822 SET_PORT(flows[id].DestHost, htons(atoi(argv[h + 1])));
823 flows[id].dstPortSpecify=true;
824 h += 2;
825 argc -= 2;
826 }
827 else if (argv[h][2] == 'k') {
828 if (argc < 2)
829 ReportErrorAndExit("General parser", "Invalid Serial Port", programName, id);
830 strcpy(flows[id].serialReceiver, argv[h+1]);
831 PRINTD(1,"flowParser: at Receiver Side %s is up \n", argv[h + 1]);
832 h += 2;
833 argc -= 2;
834 }
835 else {
836 char tempS[sizeof("What is ?") + sizeof(argv[h])];
837 ReportErrorAndExit("General parser", strcat(strcat(strcpy(tempS, "What is "), argv[h]), " ?"), programName, id);
838 }
839 break;
840 case 'f':
841 if (argc < 2)
842 ReportErrorAndExit("General parser", "Invalid TTL",
843 programName, id);
844 temp = strtol(argv[h + 1], &tail, 0);
845 strcpy(flows[id].serialReceiver,"noSerial");if ((tail == argv[h + 1]) || (temp < 0) || (temp > 255))
846 ReportErrorAndExit("General parser", "Invalid TTL",
847 programName, id);
848 flows[id].TTL = temp;
849 h += 2;
850 argc -= 2;
851 break;
852 case 't':
853 if ((argc < 2) || ((temp = strtol(argv[h + 1], NULL, 10)) <= 0))
854 ReportErrorAndExit("General parser", "Invalid Duration",
855 programName, id);
856 flows[id].Duration = temp;
857 h += 2;
858 argc -= 2;
859 break;
860
861 case 'z':
862 if ((argc < 2) || ((temp = strtol(argv[h + 1], NULL, 10)) <= 0))
863 ReportErrorAndExit("General parser", "Invalid number of packets",
864 programName, id);
865 flows[id].numPacket = temp;
866 h += 2;
867 argc -= 2;
868 break;
869
870
871 case 'k':
872 if ((argc < 2) || ((flows[id].KBToSend = strtoul(argv[h + 1], NULL, 10)) <= 0))
873 ReportErrorAndExit("General parser", "Invalid number of kbyte",
874 programName, id);
875 h += 2;
876 argc -= 2;
877 break;
878
879 case 'd':
880 if (argc < 2)
881 ReportErrorAndExit("General parser", "Invalid Delay",
882 programName, id);
883 temp = strtol(argv[h + 1], &tail, 10);
884 if ((tail == argv[h + 1]) || (temp < 0))
885 ReportErrorAndExit("General parser", "Invalid Delay",
886 programName, id);
887 Delay = temp;
888 h += 2;
889 argc -= 2;
890 break;
891 case 'b':
892 if (argc < 2)
893 ReportErrorAndExit("General parser", "Invalid DS Byte",
894 programName, id);
895 temp = strtol(argv[h + 1], &tail, 0);
896 if ((tail == argv[h + 1]) || (temp < 0) || (temp > 255))
897 ReportErrorAndExit("General parser", "Invalid DS Byte",
898 programName, id);
899 flows[id].DSByte = temp;
900 h += 2;
901 argc -= 2;
902 break;
903 case 'j':
904 if (argc < 2)
905 ReportErrorAndExit("General parser", "j option needs either 1 or 0",
906 programName, id);
907 flows[id].mean_adjustment = atoi(argv[h+1]);
908 if ((flows[id].mean_adjustment != 0) && (flows[id].mean_adjustment != 1))
909 ReportErrorAndExit("General parser", "j option needs either 1 or 0",
910 programName, id);
911 h += 2;
912 argc -= 2;
913 break;
914 case 'L':
915 if (multiFlows)
916 ReportErrorAndExit("General parser", "-L option not allowed into script file", programName,
917 id);
918 logging = 1;
919 logremoto = 1;
920
921 if ((argc < 2) || (argv[h + 1][0] == '-') || (findL7Proto(argv[h + 1]) != LX_ERROR_BYTE)) {
922 argc -= 1;
923 h += 1;
924 } else {
925
926 if (findL4Proto(argv[h + 1]) == LX_ERROR_BYTE) {
927 if (logHost)
928 freeaddrinfo(logHost);
929
930 if (getaddrinfo(argv[h + 1], NULL, &hint, &logHost))
931 ReportErrorAndExit("General parser", " Invalid log-server address or protocol",
932 programName, id);
933 argc -= 1;
934 h += 1;
935 } else {
936
937 protoTxLog = findL4Proto(argv[h + 1]);
938 argc -= 1;
939 h += 1;
940 }
941 if ((argc >= 2) && (argv[h + 1][0] != '-') && (findL7Proto(argv[h + 1]) == LX_ERROR_BYTE)) {
942 if (findL4Proto(argv[h + 1]) == LX_ERROR_BYTE) {
943 if (logHost)
944 freeaddrinfo(logHost);
945
946 if (getaddrinfo(argv[h + 1], NULL, &hint, &logHost))
947 ReportErrorAndExit("General parser", " Invalid log-server address",
948 programName, id);
949 argc -= 2;
950 h += 2;
951 } else {
952
953 protoTxLog = findL4Proto(argv[h + 1]);
954 argc -= 2;
955 h += 2;
956 }
957 }
958 else {
959 argc -= 1;
960 h += 1;
961 }
962 }
963 break;
964 case 'X':
965 if (multiFlows)
966 ReportErrorAndExit("General parser", "-X option not allowed into script file", programName,
967 id);
968
969
970
971 logServer = 1;
972
973 if ((argc < 2) || (argv[h + 1][0] == '-') || (findL7Proto(argv[h + 1]) != LX_ERROR_BYTE)) {
974 argc -= 1;
975 h += 1;
976 } else {
977
978 if (findL4Proto(argv[h + 1]) == LX_ERROR_BYTE) {
979 if (serverLogReceiver)
980 freeaddrinfo(serverLogReceiver);
981
982 if (getaddrinfo(argv[h + 1], NULL, &hint, &serverLogReceiver))
983 ReportErrorAndExit("General parser", " Invalid log-server address or protocol",
984 programName, id);
985 argc -= 1;
986 h += 1;
987 } else {
988
989 protoTx_ServerLogReceiver = findL4Proto(argv[h + 1]);
990 argc -= 1;
991 h += 1;
992 }
993 if ((argc >= 2) && (argv[h + 1][0] != '-') && (findL7Proto(argv[h + 1]) == LX_ERROR_BYTE)) {
994 if (findL4Proto(argv[h + 1]) == LX_ERROR_BYTE) {
995 if (serverLogReceiver)
996 freeaddrinfo(serverLogReceiver);
997
998 if (getaddrinfo(argv[h + 1], NULL, &hint, &serverLogReceiver))
999 ReportErrorAndExit("General parser", " Invalid log-server address",
1000 programName, id);
1001 argc -= 2;
1002 h += 2;
1003 } else {
1004
1005 protoTx_ServerLogReceiver = findL4Proto(argv[h + 1]);
1006 argc -= 2;
1007 h += 2;
1008 }
1009 }
1010 else {
1011 argc -= 1;
1012 h += 1;
1013 }
1014 }
1015 break;
1016 case 'T':
1017 if (argc < 2)
1018 ReportErrorAndExit("General parser", "Invalid Protocol Type", programName, id);
1019
1020 flows[id].l4Proto = findL4Proto(argv[h + 1]);
1021 PRINTD(1,"flowParser: Level 4 Protocol: %s\n", invFindL4Proto(flows[id].l4Proto));
1022
1023
1024 if (flows[id].l4Proto == LX_ERROR_BYTE)
1025 ReportErrorAndExit("General parser", "Invalid Protocol Type", programName, id);
1026
1027
1028
1029 flows[id].minPayloadSize = flows[id].minPayloadSize + sizeof(int);
1030
1031
1032 if (flows[id].l4Proto == L4_PROTO_ICMP) {
1033 if ((argc > 2) && (argv[h + 2][0] != '-')) {
1034 flows[id].icmptype = atoi(argv[h + 2]);
1035 h += 1;
1036 argc--;
1037 }
1038 }
1039 #ifndef DCCP
1040 if (flows[id].l4Proto == L4_PROTO_DCCP )
1041 ReportErrorAndExit("General parser", "Invalid protocol type.\nPlease, to generate DCCP traffic flows "
1042 "recompile D-ITG with the DCCP feature enabled "
1043 "(see manual for more details).", programName, id);
1044 #endif
1045 #ifdef SCTP
1046
1047 if (flows[id].l4Proto == L4_PROTO_SCTP) {
1048 int sctpId;
1049 int sctpStreams;
1050
1051 if (argc < 4 || (argv[h + 2][0] == '-') || (argv[h + 3][0] == '-'))
1052 ReportErrorAndExit ("General parser", "SCTP protocol needs parameters (syntax: -T SCTP <association_id> <max_streams>)",
1053 programName, id);
1054
1055
1056 if (argv[h + 2][0] > '0' || argv[h + 2][0] < '9')
1057 sctpId = atoi(argv[h + 2]);
1058 else
1059 ReportErrorAndExit ("General parser", "Invalid SCTP id option", programName, id);
1060
1061
1062 if (argv[h + 3][0] > '0' || argv[h + 3][0] < '9')
1063 sctpStreams = atoi(argv[h + 3]);
1064 else
1065 ReportErrorAndExit ("General parser", "Invalid SCTP num_streams option", programName, id);
1066
1067
1068
1069
1070 if (sctpStreams > 1)
1071 ReportErrorAndExit ("General parser", "** SCTP multi-streaming support is not yet available **", programName, id);
1072
1073 MUTEX_THREAD_LOCK(mutexSctp);
1074
1075 if (sctpSessions[sctpId].parsedStreams == 0) {
1076 sctpSessions[sctpId].numStreams = sctpStreams;
1077 sctpSessions[sctpId].busyStreams = 0;
1078 sctpSessions[sctpId].parsedStreams = 1;
1079 sctpSessions[sctpId].sock = -1;
1080 PRINTD(1,"flowParser: Rilevata sessione SCTP (id = %d) con %d streams al suo interno\n", sctpId, sctpStreams);
1081 }
1082 else sctpSessions[sctpId].parsedStreams++;
1083 flows[id].sctpId = sctpId;
1084 PRINTD(1,"flowParser: Rilevato stream #%d della sessione SCTP %d\n", sctpSessions[sctpId].parsedStreams, sctpId);
1085 MUTEX_THREAD_UNLOCK(mutexSctp);
1086
1087 if (sctpSessions[sctpId].parsedStreams > sctpSessions[sctpId].numStreams)
1088 ReportErrorAndExit ("General parser", "Too many streams defined into SCTP session", programName, id);
1089
1090 h += 2;
1091 argc -= 2;
1092 }
1093 #else
1094 if (flows[id].l4Proto == L4_PROTO_SCTP)
1095 ReportErrorAndExit("General parser", "Invalid protocol type.\nPlease, to generate SCTP traffic flows "
1096 "recompile D-ITG with the SCTP feature enabled "
1097 "(see manual for more details).", programName, id);
1098 #endif
1099 h += 2;
1100 argc -= 2;
1101 break;
1102 case 's':
1103 if (strlen(argv[h]) == 2) {
1104 if ((argc < 2) || ((seed = strtod(argv[h + 1], NULL)) >= 1.0) || (seed <= 0.0))
1105 ReportErrorAndExit("General parser", "Invalid seed (0 < seed < 1)", programName, id);
1106 h += 2;
1107 argc -= 2;
1108 } else if (argv[h][2] == 'p') {
1109 if (argc < 2)
1110 ReportErrorAndExit("General parser", "Invalid port number", programName, id);
1111 SET_PORT(flows[id].SrcHost, htons(atoi(argv[h + 1])));
1112 flows[id].srcPortSpecify = true;
1113 h += 2;
1114 argc -= 2;
1115 } else if (argv[h][2] == 'k') {
1116 if (argc < 2)
1117 ReportErrorAndExit("General parser", "Invalid Serial Port", programName, id);
1118 flows[id].serial = serialUp(argv[h + 1]);
1119 if (flows[id].serial == INVALID_HANDLE_VALUE)
1120 printf("Error opening interface \n");
1121 strcpy(flows[id].serialReceiver, "noSerial");
1122 h += 2;
1123 argc -= 2;
1124
1125 PRINTD(1,"flowParser: at Sender Side %s is up \n", argv[h + 1]);
1126 } else if (argv[h][2] == 'a') {
1127 if (flows[id].iface == NULL) {
1128 in_port_t tmp_port=0;
1129 if (flows[id].SrcHost ) {
1130 GET_PORT(flows[id].SrcHost, tmp_port);
1131 freeaddrinfo(flows[id].SrcHost);
1132 }
1133 if ((argc < 2) || getaddrinfo(argv[h + 1], NULL, &hint, &flows[id].SrcHost ))
1134 ReportErrorAndExit("General parser", "Invalid source address",programName, id);
1135 #ifndef NOIPV6
1136 if (flows[id].SrcHost->ai_family == PF_INET6
1137 && IN6_IS_ADDR_LINKLOCAL( &((struct sockaddr_in6 *) (flows[id].SrcHost->ai_addr))->sin6_addr )
1138 && ((struct sockaddr_in6 *) (flows[id].SrcHost->ai_addr))->sin6_scope_id == 0) {
1139 printf("** WARNING **: IPv6 address scope id not specified by the sender\n");
1140 }
1141 #endif
1142 SET_PORT(flows[id].SrcHost, tmp_port);
1143 flows[id].srcAddrSpecify = true;
1144
1145 h += 2;
1146 argc -= 2;
1147 } else {
1148 ReportErrorAndExit("General parser", "use -i <interface> OR -sa <source_address>", programName, id);
1149 }
1150 } else {
1151 char tempS[sizeof("What is ?") + sizeof(argv[h])];
1152 ReportErrorAndExit("General parser", strcat(strcat(strcpy(tempS, "What is "), argv[h]), " ?"),
1153 programName, id);
1154 }
1155 break;
1156 case 'l':
1157 if (multiFlows)
1158 ReportErrorAndExit("General parser", "-l option not allowed into script file", programName, id);
1159 logging = 1;
1160 if ((argc < 2) || (argv[h + 1][0] == '-') || !findL7Proto(argv[h + 1])) {
1161 strcpy(logFile, DefaultLogFile);
1162 argc -= 1;
1163 h += 1;
1164 } else {
1165 strcpy(logFile, argv[h + 1]);
1166 argc -= 2;
1167 h += 2;
1168 }
1169 break;
1170 case 'x':
1171 if (multiFlows)
1172 ReportErrorAndExit("General parser", "-x option not allowed into script file", programName, id);
1173 namelogReceiver = 1;
1174 if ((argc < 2) || (argv[h + 1][0] == '-') || !findL7Proto(argv[h + 1])){
1175 argc -= 1;
1176 h += 1;
1177 } else {
1178 strcpy(logFileReceiver, argv[h+1]);
1179 argc -= 2;
1180 h += 2;
1181 }
1182 break;
1183 case 'q':
1184 if ((argc < 2) || (argv[h + 1][0] == '-') || !findL7Proto(argv[h + 1])){
1185 argc -= 1;
1186 h += 1;
1187 } else {
1188 logbuffer_size = atoi(argv[h+1]);
1189 argc -= 2;
1190 h += 2;
1191 }
1192 break;
1193 case 'C':
1194 if ((argc < 2) || (strtod(argv[h+1],NULL) <= 0.))
1195 ReportErrorAndExit("Protocol Parser",
1196 "Invalid pkts per sec", programName, id);
1197 flows[id].IntArrivDistro = pdConstant;
1198 delete flows[id].IntArriv;
1199 ConstantRV = new Constant(1);
1200 flows[id].IntArriv =
1201 new SumRandom(1000.0 / strtod(argv[h+1],NULL) * (*ConstantRV));
1202 h += 2;
1203 argc -= 2;
1204 break;
1205 case 'U':
1206 if ((argc < 3) || (strtod(argv[h+1],NULL) <= 0.)
1207 || (strtod(argv[h+2],NULL) <= strtod(argv[h+1],NULL)) )
1208 ReportErrorAndExit("Protocol Parser",
1209 "Invalid pkts per sec", programName, id);
1210 delete flows[id].IntArriv;
1211 flows[id].IntArrivDistro = pdUniform;
1212 a = (1000.0 / strtod(argv[h+2],NULL));
1213 b = (1000.0 / strtod(argv[h+1],NULL)) - a ;
1214 UniformRV = new Uniform;
1215 flows[id].IntArriv = new SumRandom( b * (*UniformRV) + a);
1216 h += 3;
1217 argc -= 3;
1218 break;
1219 case 'E':
1220 if ((argc < 2) || (strtod(argv[h+1],NULL) <= 0.))
1221 ReportErrorAndExit("Protocol Parser",
1222 "Invalid pkts per sec", programName, id);
1223 delete flows[id].IntArriv;
1224 flows[id].IntArrivDistro = pdExponential;
1225 ExponentialRV = new Exponential;
1226 flows[id].IntArriv =
1227 new SumRandom(1000.0 / strtod(argv[h+1],NULL) * (*ExponentialRV));
1228 h += 2;
1229 argc -= 2;
1230 break;
1231 case 'V':
1232 if ((argc < 3) || (strtod(argv[h + 1], NULL) <= 0)
1233 || (strtod(argv[h + 2], NULL) <= 0))
1234 ReportErrorAndExit("Protocol Parser",
1235 "Invalid Pareto Distribution parameter values",
1236 programName, id);
1237 a = (Real) strtod(argv[h + 1], NULL);
1238 b = (Real) strtod(argv[h + 2], NULL);
1239 ParetoRV = new Pareto(a);
1240 delete flows[id].IntArriv;
1241 flows[id].IntArrivDistro = pdPareto;
1242 flows[id].IntArriv = new SumRandom(b * (*ParetoRV));
1243 h += 3;
1244 argc -= 3;
1245 break;
1246 case 'Y':
1247 if ((argc < 3) || (strtod(argv[h + 2], NULL) <= 0))
1248 ReportErrorAndExit("Protocol Parser",
1249 "Invalid pkts per sec", programName, id);
1250 flows[id].IntArrivDistro = pdCauchy;
1251 b = strtod(argv[h + 1], NULL);
1252 a = strtod(argv[h + 2], NULL);
1253 delete flows[id].IntArriv;
1254 CauchyRV = new Cauchy;
1255 flows[id].IntArriv = new SumRandom(a * (*CauchyRV) + b);
1256 h += 3;
1257 argc -= 3;
1258 break;
1259 case 'N':
1260 if ((argc < 3) || (argv[h + 2] <= 0))
1261 ReportErrorAndExit("Protocol Parser",
1262 "Invalid pkts per sec", programName, id);
1263 flows[id].IntArrivDistro = pdNormal;
1264 b = strtod(argv[h + 1], NULL);
1265 a = strtod(argv[h + 2], NULL);
1266 delete flows[id].IntArriv;
1267 NormalRV = new Normal;
1268 flows[id].IntArriv = new SumRandom(a * (*NormalRV) + b);
1269 h += 3;
1270 argc -= 3;
1271 break;
1272 case 'O':
1273 if ((argc < 2) || (strtod(argv[h + 1], NULL) <= 0))
1274 ReportErrorAndExit("Protocol Parser",
1275 "Invalid pkts per sec", programName, id);
1276 a = strtod(argv[h + 1], NULL);
1277 delete flows[id].IntArriv;
1278
1279 PoissonRV = new Poisson(a);
1280 flows[id].IntArrivDistro = pdPoisson;
1281 flows[id].IntArriv = new SumRandom(1000.0 / (*PoissonRV));
1282 h += 2;
1283 argc -= 2;
1284 break;
1285 case 'G':
1286 if ((argc < 3) || (strtod(argv[h + 1], NULL) <= 0) || (strtod(argv[h + 2], NULL) <= 0))
1287 ReportErrorAndExit("Protocol Parser",
1288 "Invalid Gamma Distribution parameter values",
1289 programName, id);
1290 a = (Real) strtod(argv[h + 1], NULL);
1291 b = (Real) strtod(argv[h + 2], NULL);
1292 delete flows[id].IntArriv;
1293
1294 GammaRV = new Gamma(a);
1295 flows[id].IntArrivDistro = pdGamma;
1296 flows[id].IntArriv = new SumRandom(b * (*GammaRV));
1297 h += 3;
1298 argc -= 3;
1299 break;
1300
1301 case 'W':
1302 if ((argc < 3) || (strtod(argv[h + 1], NULL) <= 0) || (strtod(argv[h + 2], NULL) <= 0))
1303 ReportErrorAndExit("Protocol Parser", "Invalid Weibull Distribution parameter values",
1304 programName, id);
1305 a = (Real) strtod(argv[h + 1], NULL);
1306 b = (Real) strtod(argv[h + 2], NULL);
1307 delete flows[id].IntArriv;
1308 WeibullRV = new Weibull(a, b);
1309 flows[id].IntArrivDistro = pdWeibull;
1310 flows[id].IntArriv = new SumRandom(1 * (*WeibullRV));
1311 h += 3;
1312 argc -= 3;
1313 break;
1314
1315
1316
1317 #ifdef BURSTY
1318 case 'B':
1319 if ( argc < 3 )
1320 ReportErrorAndExit("Protocol Parser",
1321 "Invalid settings for the On/Off periods1",
1322 programName, id);
1323 h += 1;
1324 argc -= 1;
1325 burstyParser(h, argv, argc, flows[id].id, &flows[id].OnPeriod, &flows[id].OffPeriod,
1326 flows[id].OnPeriodDistro, flows[id].OffPeriodDistro);
1327 flows[id].bursty = true;
1328 break;
1329 #endif
1330
1331 case 'c':
1332 if ((argc < 2) || (atoi(argv[h + 1]) < 1))
1333 ReportErrorAndExit("Protocol Parser", "Invalid pkt size",
1334 programName, id);
1335 delete flows[id].PktSize;
1336 flows[id].PktSizeDistro = pdConstant;
1337 ConstantRV = new Constant(1);
1338 flows[id].PktSize = new SumRandom(atoi(argv[h + 1]) * (*ConstantRV));
1339 h += 2;
1340 argc -= 2;
1341 break;
1342 case 'u':
1343 if ((argc < 3) || (atoi(argv[h + 1]) < 1)
1344 || (atoi(argv[h + 2]) <= atoi(argv[h + 1])))
1345 ReportErrorAndExit("Protocol Parser", "Invalid pkt size",
1346 programName, id);
1347 delete flows[id].PktSize;
1348 flows[id].PktSizeDistro = pdUniform;
1349 b = atoi(argv[h + 1]);
1350 a = atoi(argv[h + 2]) - b;
1351 UniformRV = new Uniform;
1352 flows[id].PktSize = new SumRandom(a * (*UniformRV) + b);
1353 argc -= 3;
1354 h += 3;
1355 break;
1356 case 'e':
1357 if ((argc < 2) || (atoi(argv[h + 1]) < 1))
1358 ReportErrorAndExit("Protocol Parser", "Invalid pkt size",
1359 programName, id);
1360 delete flows[id].PktSize;
1361 flows[id].PktSizeDistro = pdExponential;
1362 ExponentialRV = new Exponential;
1363 flows[id].PktSize = new SumRandom(atoi(argv[h + 1]) * (*ExponentialRV));
1364 h += 2;
1365 argc -= 2;
1366 break;
1367 case 'v':
1368 if ((argc < 3) || (strtod(argv[h + 2], NULL) <= 0))
1369 ReportErrorAndExit("Protocol Parser",
1370 "Invalid Pareto Distribution parameter values",
1371 programName, id);
1372 a = (Real) strtod(argv[h + 1], NULL);
1373 b = (Real) strtod(argv[h + 2], NULL);
1374 delete flows[id].PktSize;
1375 ParetoRV = new Pareto(a);
1376 flows[id].PktSizeDistro = pdPareto;
1377 flows[id].PktSize = new SumRandom(b * (*ParetoRV));
1378 argc -= 3;
1379 h += 3;
1380 break;
1381 case 'y':
1382 if ((argc < 3) || (atoi(argv[h + 2]) <= 0))
1383 ReportErrorAndExit("Protocol Parser",
1384 "Invalid pkts per sec", programName, id);
1385 delete flows[id].PktSize;
1386 flows[id].PktSizeDistro = pdCauchy;
1387 b = (Real) atoi(argv[h + 1]);
1388 a = (Real) atoi(argv[h + 2]);
1389 CauchyRV = new Cauchy;
1390 flows[id].PktSize = new SumRandom(a * (*CauchyRV) + b);
1391 h += 3;
1392 argc -= 3;
1393 break;
1394 case 'n':
1395 if ((argc < 3) || (argv[h + 2] <= 0))
1396 ReportErrorAndExit("Protocol Parser", "Invalid pkt size",
1397 programName, id);
1398 delete flows[id].PktSize;
1399 flows[id].PktSizeDistro = pdNormal;
1400 b = (Real) atoi(argv[h + 1]);
1401 a = (Real) atoi(argv[h + 2]);
1402 NormalRV = new Normal;
1403 flows[id].PktSize = new SumRandom(a * (*NormalRV) + b);
1404 h += 3;
1405 argc -= 3;
1406 break;
1407 case 'o':
1408 if ((argc < 2) || (atoi(argv[h + 1]) < 0))
1409 ReportErrorAndExit("Protocol Parser", "Invalid pkt size",
1410 programName, id);
1411 a = atoi(argv[h + 1]);
1412 delete flows[id].PktSize;
1413
1414 PoissonRV = new Poisson(a);
1415 flows[id].PktSizeDistro = pdPoisson;
1416 flows[id].PktSize = new SumRandom(1 * (*PoissonRV));
1417 h += 2;
1418 argc -= 2;
1419 break;
1420 case 'g':
1421 if ((argc < 3) || (strtod(argv[h + 2], NULL) <= 0))
1422 ReportErrorAndExit("Protocol Parser",
1423 "Invalid Gamma Distribution parameter values",
1424 programName, id);
1425 delete flows[id].PktSize;
1426 a = (Real) strtod(argv[h + 1], NULL);
1427 b = (Real) strtod(argv[h + 2], NULL);
1428
1429 GammaRV = new Gamma(a);
1430 flows[id].PktSizeDistro = pdGamma;
1431 flows[id].PktSize = new SumRandom(b * (*GammaRV));
1432 h += 3;
1433 argc -= 3;
1434 break;
1435
1436 case 'w':
1437 if ((argc < 3) || (strtod(argv[h + 1], NULL) <= 0) || (strtod(argv[h + 2], NULL) <= 0))
1438 ReportErrorAndExit("Protocol Parser", "Invalid Weilbull Distribution Parameter values",
1439 programName, id);
1440 a = (Real) strtod(argv[h + 1], NULL);
1441 b = (Real) strtod(argv[h + 2], NULL);
1442 WeibullRV = new Weibull(a, b);
1443 flows[id].PktSizeDistro = pdWeibull;
1444 flows[id].PktSize = new SumRandom(1 * (*WeibullRV));
1445 h += 3;
1446 argc -= 3;
1447 break;
1448
1449 case 'D':
1450
1451
1452 PRINTD(1,"flowParser: Nagle algorithm disabled\n");
1453 flows[id].Nagle=false;
1454 h += 1;
1455 argc -= 1;
1456 break;
1457 #ifdef WIN32
1458 case 'P':
1459
1460
1461 if (SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS) == 0){
1462 printf("Error - Impossible set priority class - %d \n", (int) GetLastError());
1463 fflush(stdout);
1464 }
1465 PRINTD(1,"flowParser: Enabled thread priority \n");
1466 setPriority = true;
1467 h += 1;
1468 argc -= 1;
1469 break;
1470 #endif
1471
1472 case 'H':
1473 PRINTD(1,"flowParser: Enabled passive mode \n");
1474 passiveMode = true;
1475
1476 h += 1;
1477 argc -= 1;
1478 break;
1479
1480
1481 case 'F':
1482
1483 if (argv[h][2] == 'p') {
1484 PRINTD(1,"Parsing of Fp option \n");
1485 if ((argc < 2) || (argv[h+1][0] == '-'))
1486 ReportErrorAndExit("General parser", "Invalid name file specified for -Fp option",programName, id);
1487
1488
1489 strcpy(flows[id].payloadFile,argv[h + 1]);
1490 PRINTD(1,"File name of payload: %s \n",flows[id].payloadFile);
1491
1492 if (multiFlows){
1493 PRINTD(1,"Parsing of F option in multiFlows mode \n");
1494 MUTEX_THREAD_LOCK(mutexBufferPayload);
1495
1496 for(int j = 0 ; j < flowCount ; j++)
1497
1498 if(flows[j].dimPayloadBuffer != 0 )
1499
1500 if (strcmp(flows[id].payloadFile,flows[j].payloadFile)){
1501
1502 flows[id].ptrPayloadBuffer = flows[j].ptrPayloadBuffer;
1503 flows[id].dimPayloadBuffer = flows[j].dimPayloadBuffer;
1504 PRINTD(1,"Parsing of F option: two identical file \n");
1505 }
1506 MUTEX_THREAD_UNLOCK(mutexBufferPayload);
1507 }
1508 if (flows[id].dimPayloadBuffer == 0){
1509 long int bytesRead = 0;
1510
1511
1512 ptrFile = fopen(flows[id].payloadFile,"r");
1513
1514 if (ptrFile == NULL)
1515 ReportErrorAndExit("flowParser","Error in opening the file of payload", programName, id);
1516
1517 fseek(ptrFile,0,SEEK_END);
1518 flows[id].dimPayloadBuffer = ftell(ptrFile);
1519 PRINTD(1,"Size of file of payload: %u \n",(unsigned int)flows[id].dimPayloadBuffer);
1520
1521 flows[id].ptrPayloadBuffer = malloc(flows[id].dimPayloadBuffer);
1522
1523 if (flows[id].ptrPayloadBuffer == NULL)
1524 ReportErrorAndExit("flowParser","Non enough memory for load the file", programName, id);
1525
1526 fseek(ptrFile,0,SEEK_SET);
1527 bytesRead = fread(flows[id].ptrPayloadBuffer,1,flows[id].dimPayloadBuffer,ptrFile);
1528
1529 if((bytesRead != flows[id].dimPayloadBuffer) || ferror(ptrFile))
1530 ReportErrorAndExit("flowParser","Error in reading the file of payload", programName, id);
1531 else
1532
1533 fclose(ptrFile);
1534 }
1535 } else
1536
1537 if (argv[h][2] == 's') {
1538 if ((argc < 2) || (argv[h+1][0] == '-'))
1539 ReportErrorAndExit("General parser", "Invalid name file specified for -Fs option",programName, id);
1540
1541
1542 strcpy(flows[id].pktSizeFile,argv[h + 1]);
1543 PRINTD(1,"flowParser: File name of the packets sizes: %s\n",flows[id].pktSizeFile);
1544
1545
1546 ptrFile = fopen(flows[id].pktSizeFile,"r");
1547
1548 if (ptrFile == NULL)
1549 ReportErrorAndExit("flowParser","Error in opening the file of the packets sizes", programName, id);
1550
1551 fseek(ptrFile,0,SEEK_SET);
1552
1553
1554 if (fgets(line, sizeof(line) - 1, ptrFile) == NULL) {
1555 ReportErrorAndExit("flowParser","Error in reading the file of the packets sizes", programName, id);
1556 }
1557 lineNumber=1;
1558
1559 do{
1560 if(strcmp(line,"\n")!=0){
1561 if((atoi(line) <= 0) || (atoi(line) > DIM_MAX_IP_PACKET)){
1562 printf("\nError in reading the file of the packets sizes: negative, too big or invalid number at line %d\n",lineNumber);
1563 ReportErrorAndExit("flowParser","Error in reading the file of the packets sizes: negative, too big or invalid number", programName, id);
1564 }
1565 tmpInt=atoi(line);
1566 PRINTD(4,"flowParser: Size read = %d ; value n° = %d\n",tmpInt,(int)flows[id].vectSize.size()+1);
1567 flows[id].vectSize.push_back(tmpInt);
1568 }
1569 lineNumber++;
1570 } while (fgets(line, sizeof(line) - 1, ptrFile));
1571 fclose(ptrFile);
1572
1573 }else
1574
1575 if (argv[h][2] == 't') {
1576 if ((argc < 2) || (argv[h+1][0] == '-'))
1577 ReportErrorAndExit("General parser", "Invalid name file specified for -Ft option",programName, id);
1578
1579
1580 strcpy(flows[id].timeFile ,argv[h + 1]);
1581 PRINTD(1,"flowParser: File name of the IDT: %s\n",flows[id].timeFile);
1582
1583
1584 ptrFile = fopen(flows[id].timeFile,"r");
1585
1586 if (ptrFile == NULL)
1587 ReportErrorAndExit("flowParser","Error in opening the file of the IDT", programName, id);
1588
1589 fseek(ptrFile,0,SEEK_SET);
1590
1591
1592 if (fgets(line, sizeof(line) - 1, ptrFile) == NULL) {
1593 ReportErrorAndExit("flowParser","Error in reading the file of the IDT", programName, id);
1594 }
1595 lineNumber=1;
1596
1597 do{
1598 if(strcmp(line,"\n")!=0){
1599 if((strtod(line,NULL) <= 0) || (strtod(line,NULL) == HUGE_VAL)){
1600 printf("\nError in reading the file of the IDT: negative, too big or invalid number at line %d\n",lineNumber);
1601 ReportErrorAndExit("flowParser","Error in reading the file of the IDT: negative, too big or invalid number", programName, id);
1602 }
1603 tmpFloat=strtod(line,NULL);
1604 PRINTD(2,"flowParser: Time read = %f ; value n° = %d\n",tmpFloat,(int)flows[id].vectTime.size()+1);
1605 flows[id].vectTime.push_back(tmpFloat);
1606 }
1607 lineNumber++;
1608 } while (fgets(line, sizeof(line) - 1, ptrFile));
1609 fclose(ptrFile);
1610
1611
1612 Delay = (long int) floor(flows[id].vectTime[0]);
1613 } else {
1614
1615 char tempS[sizeof("What is ?") + sizeof(argv[h])];
1616 ReportErrorAndExit("General parser", strcat(strcat(strcpy(tempS, "What is "), argv[h]), " ?"),
1617 programName, id);
1618 }
1619 argc -= 2;
1620 h += 2;
1621 break;
1622
1623
1624 case 'S':
1625 if (argv[h][2] == 's') {
1626 if (argv[h][3] == 'a') {
1627 freeaddrinfo(flows[id].SigSrcHost);
1628 if ((argc < 2) || getaddrinfo(argv[h + 1], NULL, &hint, &flows[id].SigSrcHost))
1629 ReportErrorAndExit("General parser",
1630 "Invalid source address for the signal channel", programName,
1631 id);
1632
1633 #ifndef NOIPV6
1634
1635 if (flows[id].SigSrcHost->ai_family == PF_INET6
1636 && IN6_IS_ADDR_LINKLOCAL( &((struct sockaddr_in6 *) (flows[id].SigSrcHost->ai_addr))->sin6_addr )
1637 && ((struct sockaddr_in6 *) (flows[id].SigSrcHost->ai_addr))->sin6_scope_id
1638 == 0) {
1639 printf(
1640 "** WARNING **: IPv6 address scope id not specified by the sender (signal channel)\n");
1641 }
1642 #endif
1643
1644 PRINTD(1,"\nflowParser: Signaling source address: %s\n",argv[h + 1]);
1645 } else if (argv[h][3] == 'p') {
1646 if (argc < 2)
1647 ReportErrorAndExit("General parser", "Invalid port number", programName, id);
1648 int tmp = atoi(argv[h + 1]);
1649 if ((tmp < 1) || (tmp > 65535))
1650 ReportErrorAndExit("General parser", "Invalid port number", programName, id);
1651 SigPort = tmp;
1652 PRINTD(1,"\nflowParser: Signaling source port: %d\n",SigPort);
1653 flows[id].SigSrcPort = SigPort;
1654 } else {
1655 char tempS[sizeof("What is ?") + sizeof(argv[h])];
1656 ReportErrorAndExit("General parser",
1657 strcat(strcat(strcpy(tempS, "What is "), argv[h]), " ?"), programName,
1658 id);
1659 }
1660 } else if (argv[h][2] == 'd') {
1661 if (argv[h][3] == 'a') {
1662 GET_PORT(flows[id].SigDestHost, SigPort);
1663 freeaddrinfo(flows[id].SigDestHost);
1664 if ((argc < 2) || getaddrinfo(argv[h + 1], NULL, &hint, &flows[id].SigDestHost))
1665 ReportErrorAndExit("General parser",
1666 "Invalid destination address for the signal channel",
1667 programName, id);
1668
1669 #ifndef NOIPV6
1670 if (flows[id].SigDestHost->ai_family == PF_INET6
1671 && IN6_IS_ADDR_LINKLOCAL( &((struct sockaddr_in6 *) (flows[id].SigDestHost->ai_addr))->sin6_addr)
1672 && ((struct sockaddr_in6 *) (flows[id].SigDestHost->ai_addr))->sin6_scope_id == 0)
1673 printf("** WARNING **: IPv6 address scope id not specified by the sender (signal channel)\n");
1674 #endif
1675
1676 SigDestHostSpecify=true;
1677 PRINTD(1,"\nflowParser: Signaling destination address: %s\n",argv[h + 1]);
1678 SET_PORT(flows[id].SigDestHost,SigPort);
1679 } else if (argv[h][3] == 'p') {
1680 if (argc < 2)
1681 ReportErrorAndExit("General parser", "Invalid port number", programName, id);
1682 int tmp=atoi(argv[h + 1]);
1683 if ((tmp < 1) || (tmp > 65535))
1684 ReportErrorAndExit("General parser", "Invalid port number", programName, id);
1685 SigPort=tmp;
1686 PRINTD(1,"\nflowParser: Signaling destination port: %d\n",SigPort);
1687 SET_PORT(flows[id].SigDestHost,htons(SigPort));
1688 } else {
1689 char tempS[sizeof("What is ?") + sizeof(argv[h])];
1690 ReportErrorAndExit("General parser", strcat(strcat(strcpy(tempS, "What is "), argv[h]), " ?"), programName, id);
1691 }
1692 }
1693 #ifdef UNIX
1694 else if (argv[h][2] == 'i'){
1695 if (argc < 2)
1696 ReportErrorAndExit("General parser", "Invalid interface", programName, id);
1697 flows[id].SigInterface = (char*)malloc(strlen(argv[h + 1]) + 1);
1698 strcpy(flows[id].SigInterface, argv[h + 1]);
1699 PRINTD(1,"\nflowParser: Interface for signaling: %s\n",flows[id].SigInterface);
1700 h += 2;
1701 argc -= 2;
1702 }
1703 #endif
1704 else {
1705
1706 char tempS[sizeof("What is ?") + sizeof(argv[h])];
1707 ReportErrorAndExit("General parser", strcat(strcat(strcpy(tempS, "What is "), argv[h]), " ?"), programName, id);
1708 }
1709 argc -= 2;
1710 h += 2;
1711 break;
1712
1713 default:
1714 char temp[sizeof("What is ?") + sizeof(argv[h])];
1715 ReportErrorAndExit("General parser", strcat(strcat(strcpy(temp,
1716 "What is "), argv[h]), " ?"), programName, id);
1717 break;
1718 }
1719 } else {
1720 flows[id].l7Proto = findL7Proto(argv[h]);
1721 switch (flows[id].l7Proto) {
1722 case L7_PROTO_TELNET:
1723
1724 telnetParser(&flows[id].IntArriv, &flows[id].PktSize, flows[id].IntArrivDistro, flows[id].PktSizeDistro);
1725 flows[id].l4Proto = L4_PROTO_TCP;
1726 h++;
1727 argc--;
1728 break;
1729 case L7_PROTO_VOIP:
1730
1731 voIPParser(h, argv, argc, flows[id].id, &flows[id].IntArriv, &flows[id].PktSize,
1732 flows[id].IntArrivDistro, flows[id].PktSizeDistro);
1733 #ifdef DCCP
1734 if ( flows[id].l4Proto != L4_PROTO_UDP && flows[id].l4Proto != L4_PROTO_DCCP)
1735 #endif
1736 flows[id].l4Proto = L4_PROTO_UDP;
1737 break;
1738 case L7_PROTO_DNS:
1739
1740 dnsParser(&flows[id].IntArriv, &flows[id].PktSize, flows[id].IntArrivDistro, flows[id].PktSizeDistro);
1741 h++;
1742 argc--;
1743 break;
1744
1745 case L7_PROTO_CSATTIVA:
1746 CSParsera(&flows[id].IntArriv, &flows[id].PktSize, flows[id].IntArrivDistro, flows[id].PktSizeDistro);
1747 flows[id].l4Proto= L4_PROTO_UDP;
1748 h++;
1749 argc--;
1750 break;
1751 case L7_PROTO_CSINATTIVA:
1752 CSParseri(&flows[id].IntArriv, &flows[id].PktSize, flows[id].IntArrivDistro, flows[id].PktSizeDistro);
1753 flows[id].l4Proto= L4_PROTO_UDP;
1754 h++;
1755 argc--;
1756 break;
1757 case L7_PROTO_QUAKE:
1758 QuakeParser(&flows[id].IntArriv, &flows[id].PktSize, flows[id].IntArrivDistro, flows[id].PktSizeDistro);
1759 flows[id].l4Proto= L4_PROTO_UDP;
1760 h++;
1761 argc--;
1762 break;
1763 case LX_ERROR_BYTE:
1764 char temp[sizeof("What is ?") + sizeof(argv[h])];
1765 ReportErrorAndExit("General parser", strcat(strcat(strcpy(temp, "What is "), argv[h]), " ?"), programName, id);
1766 break;
1767 }
1768 }
1769 }
1770 PRINTD(1,"flowParser: Terminate Parser flow %d\n",id);
1771
1772 #ifdef WIN32
1773 if ((flows[id].l4Proto == L4_PROTO_ICMP) && (flows[id].DestHost->ai_family == PF_INET6)) {
1774 printf("Error: traffic ICMP with protocol IPv6 is not supported \n");
1775 memClean();
1776 exit(1);
1777 }
1778 #endif
1779
1780 if (seed == 0.0) {
1781 #ifdef WIN32
1782 _ftime(&tstruct);
1783 seed = 0.49 * sin(tstruct.millitm) + 0.50;
1784 #endif
1785 #ifdef UNIX
1786 struct timeval tv;
1787 gettimeofday(&tv, NULL);
1788 seed = 0.49 * sin((double)tv.tv_usec) + 0.50;
1789 #endif
1790 }
1791
1792
1793 MotherOfAll urng(seed);
1794 Random::Set(urng);
1795
1796
1797
1798 if (createNewPipe(flows[id].parserPipe) < 0) {
1799 printf("Error in flowParser() trying to create a new pipe. [flow %d]\n", id);
1800 exitThread();
1801 }
1802
1803
1804 if ((passiveMode == true) && (multiFlows == 1) && (flows[id].dstAddrSpecify == true)){
1805 ReportErrorAndExit("flowParser","\"-a <address>\" option not work on passive-multiflow mode", programName, id);
1806 }
1807
1808
1809 if((passiveMode==true) && (((flows[id].l4Proto!=L4_PROTO_TCP) && (flows[id].l4Proto!=L4_PROTO_UDP)) || (flows[id].l7Proto!=LX_PROTO_NONE))){
1810 ReportErrorAndExit("flowParser","Passive Mode only with TCP or UDP protocol", programName, id);
1811 }
1812
1813
1814 if((passiveMode == true) && (multiFlows == 1) && ((flows[id].srcPortSpecify == false) || (flows[id].dstPortSpecify == false))){
1815 ReportErrorAndExit("flowParser","Passive-multiflow mode requires \"-sp\" and \"-rp\" options", programName, id);
1816 }
1817
1818 #ifndef MULTIPORT
1819
1820 if((passiveMode == false) && (multiFlows == 1) && (flows[id].dstPortSpecify == false)){
1821 ReportErrorAndExit("flowParser","Multiflow mode requires \"-rp\" option", programName, id);
1822 }
1823 #endif
1824
1825
1826 if(passiveMode == true) {
1827 if (flows[id].srcPortSpecify == false){
1828 SET_PORT(flows[id].SrcHost, htons(DEFAULT_PORT_SENDER));
1829 flows[id].srcPortSpecify = true;
1830 }
1831
1832 if (flows[id].dstPortSpecify == false){
1833 SET_PORT(flows[id].DestHost, htons(DEFAULT_PORT));
1834 flows[id].dstPortSpecify = true;
1835 }
1836 }
1837
1838 bool flag=flows[id].dstAddrSpecify;
1839
1840
1841
1842
1843
1844
1845
1846 int rit = identifySignalManager(id, &chanId, flows[id].SigDestHost,flows[id].dstAddrSpecify);
1847
1848 if (rit == -1) {
1849 printf("Error into function identifySignalManager() \n");
1850 exitThread();
1851 }
1852
1853
1854
1855
1856
1857 if(flag!=flows[id].dstAddrSpecify){
1858 char *buffer;
1859
1860
1861 if (flows[id].SigDestHost->ai_family==PF_INET){
1862 buffer=new char[INET_ADDRSTRLEN];
1863
1864 getnameinfo(flows[id].SigDestHost->ai_addr,flows[id].SigDestHost->ai_addrlen,buffer,INET_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
1865 }else{
1866
1867 buffer=new char[INET6_ADDRSTRLEN];
1868
1869 getnameinfo(flows[id].SigDestHost->ai_addr,flows[id].SigDestHost->ai_addrlen,buffer,INET6_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
1870 }
1871 PRINTD(1,"flowParser: address of the Receiver: %s\n",buffer);
1872
1873 GET_PORT((flows[id].DestHost),SigPort);
1874 if (flows[id].DestHost) freeaddrinfo(flows[id].DestHost);
1875 if(getaddrinfo(buffer, NULL, &hint, &(flows[id].DestHost)) < 0){
1876 perror("flowParser getaddrinfo() (1)");
1877 exitThread();
1878 }
1879 SET_PORT((flows[id].DestHost),SigPort);
1880
1881 delete[] buffer;
1882 }
1883
1884
1885 if((passiveMode==true) && (flows[id].srcAddrSpecify==false)){
1886 if (flows[id].SigDestHost->ai_family == PF_INET6){
1887 GET_PORT((flows[id].SrcHost),SigPort);
1888 PRINTD(1,"flowParser: freeaddrinfo(flows[id].SrcHost)\n");
1889
1890 #if (defined WIN32 && defined IPv6RECV) || defined UNIX || defined BSD
1891 if (flows[id].SrcHost)
1892 freeaddrinfo(flows[id].SrcHost);
1893 if(getaddrinfo("::", NULL, &hint, &(flows[id].SrcHost)) < 0){
1894 perror("flowParser getaddrinfo() (2)");
1895 exitThread();
1896 }
1897
1898 #ifndef NOIPV6
1899 if (IN6_IS_ADDR_LINKLOCAL( &((struct sockaddr_in6 *)(flows[id].SrcHost->ai_addr))->sin6_addr )
1900 && ((struct sockaddr_in6 *)(flows[id].SrcHost->ai_addr))->sin6_scope_id == 0)
1901 printf("** WARNING **: IPv6 address scope id not specified by the sender\n");
1902 #endif
1903 #endif
1904 SET_PORT((flows[id].SrcHost),SigPort);
1905 }
1906 }
1907
1908
1909
1910 if ((namelogReceiver == 1) || (logServer == 1)) {
1911 if (signalChannels[chanId].errorLog == true){
1912 printf("Error log file specified is already open \n");
1913 isChannelClosable(chanId);
1914 if (multiFlows)
1915 exitThread();
1916 else return 0;
1917 }
1918 }
1919
1920 flows[id].Delay = Delay;
1921
1922
1923
1924 msg.code = MSG_SM_NEWFLOW;
1925 msg.flowId = id;
1926 if (sendPipeMsg(signalChannels[chanId].pipe, &msg) < 0) {
1927 perror("flowParser sending msg");
1928 exitThread();
1929 }
1930
1931
1932 if (recvPipeMsg(flows[id].parserPipe, &msg) < 0) {
1933 perror("flowParser receiving msg");
1934 exitThread();
1935 }
1936
1937 PRINTD(1,"flowParser: msg received from signal manager %d\n",msg.code);
1938
1939 switch (msg.code) {
1940 case MSG_FP_END:
1941 break;
1942 case MSG_FP_ERR1:
1943 printf("Error at Receiver side \n");
1944 break;
1945 case MSG_FP_ERR2:
1946 printf("Error - FlowSender interrupted by an error\n");
1947 break;
1948 default:
1949 printf("Error undefined message received from signal manager\n");
1950 break;
1951 }
1952 printf("Finished sending packets of flow ID: %d\n\n", msg.flowId);
1953 fflush(stdout);
1954
1955
1956
1957 if (managerMode) {
1958
1959 int length = strlen(ManagerMsg);
1960 memmove(ManagerMsg + 2 * sizeof(int), ManagerMsg, length);
1961 ((int *) ManagerMsg)[0] = MNG_FLOWEND;
1962 ((int *) ManagerMsg)[1] = length;
1963 sendto(managerSock, ManagerMsg, length + sizeof(int) * 2, 0, (struct sockaddr *) &ManagerIP,
1964 ManagerIPslen);
1965 PRINTD(1,"flowParser: Notify ITGManager about the end of the generation \n");
1966 }
1967
1968
1969 closePipe(flows[id].parserPipe);
1970
1971
1972
1973
1974 if (multiFlows)
1975 exitThread();
1976 return 0;
1977 }
1978
1979 void *signalManager(void *id)
1980 {
1981 int chanId, sock, size;
1982 BYTE type;
1983 uint32_t flowId;
1984 struct pipeMsg msg;
1985 uint16_t openedPort=0;
1986 #ifdef UNIX
1987 int fd, maxfd;
1988 fd_set rset;
1989 #endif
1990 #ifdef WIN32
1991 HANDLE fd, namedPipe;
1992 HANDLE events[2];
1993 DWORD available = 0;
1994
1995 unsigned long pending;
1996 #endif
1997
1998 PRINTD(1,"signalManager: signalManager() started\n");
1999
2000
2001 chanId = *(int *)id;
2002 sock = signalChannels[chanId].socket;
2003 fd = signalChannels[chanId].pipe[0];
2004 #ifdef WIN32
2005 events[0] = WSACreateEvent();
2006 WSAEventSelect(sock, events[0], FD_READ);
2007 events[1] = signalChannels[chanId].pipe[1];
2008 namedPipe = signalChannels[chanId].pipe[2];
2009 #endif
2010
2011 #ifdef UNIX
2012
2013 maxfd = MAX(fd, sock) + 1;
2014 FD_ZERO(&rset);
2015 #endif
2016
2017
2018 for (;;) {
2019
2020 #ifdef UNIX
2021 FD_SET(sock, &rset);
2022 FD_SET(fd, &rset);
2023 if (select(maxfd + 1, &rset, NULL, NULL, NULL) == -1) {
2024 if (errno == EINTR)
2025 continue;
2026 printf("error during select in signalManager %d\n", chanId);
2027 exitThread();
2028 }
2029
2030 if (FD_ISSET(sock, &rset)) {
2031 #elif defined WIN32
2032 PRINTD(1,"signalManager: before waitformultiple...\n");
2033
2034 WaitForMultipleObjects(2, (const HANDLE *)events, FALSE, INFINITE);
2035 PRINTD(1,"signalManager: out of waitformultiple...\n");
2036
2037
2038 ResetEvent(events[0]);
2039
2040 pending = 0;
2041 ioctlsocket(sock, FIONREAD, &pending);
2042 PRINTD(1,"signalManager: pending: %lu\n", pending);
2043 while (pending > 0) {
2044 #endif
2045
2046
2047
2048 type=0xFF;
2049 size = recv(sock, (char *) &type, sizeof(type), 0);
2050 if (size <= 0) {
2051 ReportErrorAndExit("signalManager","Receiver has shut down the connection gracefully",programName,0);
2052 }
2053 PRINTD(1,"signalManager: received type %d on socket\n", type);
2054 switch (type) {
2055 case TSP_ACK_CLOSED_FLOW:
2056
2057
2058 recv(sock, (char *) &flowId, sizeof(flowId), 0);
2059 flowId = ntohl(flowId);
2060 PRINTD(1,"signalManager: received ack closed flow %d\n", flowId);
2061
2062
2063
2064
2065 if ( isChannelClosable(chanId)) {
2066
2067 msg.code = MSG_FP_END;
2068 msg.flowId = flowId;
2069 if (sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2070 perror("signalManager sending msg");
2071 exitThread();
2072 }
2073 exitThread();
2074 } else {
2075
2076 msg.code = MSG_FP_END;
2077 msg.flowId = flowId;
2078 if (sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2079 perror("signalManager sending msg");
2080 exitThread();
2081 }
2082 }
2083 break;
2084 case TSP_ACK_SEND_FLOW:
2085
2086 recv(sock, (char *) &flowId, sizeof(flowId), 0);
2087 flowId = ntohl(flowId);
2088
2089 recv(sock, (char *) &openedPort, sizeof(openedPort), 0);
2090 if (openedPort != 0) {
2091 SET_PORT((flows[flowId].DestHost),openedPort);
2092 PRINTD(1,"signalManager: received opened port %d\n", ntohs(openedPort));
2093 }
2094
2095
2096
2097 CREATE_THREAD(&(flows[flowId]), flowSender, NULL, flows[flowId].handle, true);
2098 #ifdef WIN32
2099 if (setPriority == true){
2100 if (SetThreadPriority(flows[flowId].handle, THREAD_PRIORITY_TIME_CRITICAL) == 0)
2101 printf("Error - Impossible set priority for thread - %d \n", (int) GetLastError());
2102 }
2103 #endif
2104 break;
2105 case TSP_DISCOVERY:
2106 sendType(sock, TSP_ACK_DISCOVERY);
2107 break;
2108 case TSP_ERR_MSG_2:
2109 recv(sock, (char *) &flowId, sizeof(flowId), 0);
2110 flowId = ntohl(flowId);
2111 printf("Error on bind to receiver side\n");
2112
2113 if ( isChannelClosable(chanId)) {
2114
2115 msg.code = MSG_FP_ERR1;
2116 msg.flowId = flowId;
2117 if ( sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2118 perror("signalManager sending msg");
2119 exitThread();
2120 }
2121 exitThread();
2122 } else {
2123 msg.code = MSG_FP_ERR1;
2124 msg.flowId = flowId;
2125 if ( sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2126 perror("signalManager sending msg");
2127 exitThread();
2128 }
2129 }
2130 break;
2131 case TSP_ERR_MSG_3:
2132 recv(sock, (char *) &flowId, sizeof(flowId), 0);
2133 flowId = ntohl(flowId);
2134 printf("** To generate ICMP traffic sender and receiver must be root\n");
2135
2136 if (isChannelClosable(chanId)) {
2137
2138 msg.code = MSG_FP_ERR1;
2139 msg.flowId = flowId;
2140 if (sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2141 perror("signalManager sending msg");
2142 exitThread();
2143 }
2144 exitThread();
2145 }else {
2146
2147 msg.code = MSG_FP_ERR1;
2148 msg.flowId = flowId;
2149 if (sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2150 perror("signalManager sending msg");
2151 exitThread();
2152 }
2153 }
2154 break;
2155
2156 case TSP_ERR_MSG_5:
2157 recv(sock, (char *) &flowId, sizeof(flowId), 0);
2158 flowId = ntohl(flowId);
2159 printf("** Connection closed unexpectedly\n");
2160
2161 if (isChannelClosable(chanId)) {
2162
2163 msg.code = MSG_FP_ERR1;
2164 msg.flowId = flowId;
2165 if (sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2166 perror("signalManager sending msg");
2167 exitThread();
2168 }
2169 exitThread();
2170 }else {
2171
2172 msg.code = MSG_FP_ERR1;
2173 msg.flowId = flowId;
2174 if (sendPipeMsg(flows[flowId].parserPipe, &msg) < 0) {
2175 perror("signalManager sending msg");
2176 exitThread();
2177 }
2178 }
2179 break;
2180
2181 default:
2182 ReportErrorAndExit("signalManager","Got unknown message from receiver",programName,0);
2183 break;
2184 }
2185 fflush(stdout);
2186 #ifdef WIN32
2187 ioctlsocket(sock, FIONREAD, &pending);
2188 #endif
2189 }
2190
2191 #ifdef UNIX
2192
2193 if (FD_ISSET(fd, &rset)) {
2194 #elif defined WIN32
2195
2196 if (PeekNamedPipe(namedPipe, NULL , 0 , NULL , &available , NULL) == 0) {
2197 printf("Error in peek named pipe\n");
2198 exitThread();
2199 }
2200 PRINTD(1,"signalManager: available: %d\n", (int) available);
2201
2202 while(available > 0) {
2203 #endif
2204
2205 if (recvPipeMsg(signalChannels[chanId].pipe, &msg) < 0) {
2206 perror("signalManager receiving msg");
2207 exitThread();
2208 }
2209 PRINTD(1,"signalManager: received msg code: %d\n", msg.code);
2210 switch(msg.code) {
2211 case MSG_SM_NEWFLOW:
2212 PRINTD(1,"signalManager: perform a new request to send \n");
2213 flows[msg.flowId].sigChanId = chanId;
2214 requestToSend(msg.flowId, sock);
2215 break;
2216 case MSG_SM_ENDFLOW:
2217
2218 PRINTD(1,"signalManager: sending request on signalling channel to close flow #%d\n", msg.flowId);
2219 closedFlow(msg.flowId, sock);
2220 break;
2221 case MSG_SM_ERRFLOW:
2222
2223 PRINTD(1,"signalManager: recover from a receiver error notification \n");
2224 msg.code = MSG_FP_ERR2;
2225 if (sendPipeMsg(flows[msg.flowId].parserPipe, &msg) < 0) {
2226 perror("signalManager sending msg");
2227 exitThread();
2228 }
2229 closedFlowErr(msg.flowId, sock);
2230
2231 if ( isChannelClosable(chanId))
2232 exitThread();
2233 break;
2234
2235 case MSG_SM_SNDACK:
2236 PRINTD(1,"signalManager: sendAckFlow \n");
2237 if(sendAckFlow(msg.flowId, sock)<0){
2238 perror("signalManager sending msg (MSG_SM_SNDACK)");
2239 exitThread();
2240 }
2241 break;
2242
2243 default:
2244 printf("Got unknown message from queue\n");
2245 fflush(stdout);
2246 break;
2247 }
2248 #ifdef WIN32
2249
2250 if (PeekNamedPipe(fd, NULL , 0 , NULL , &available , NULL) == 0) {
2251 printf("Error in peek named pipe\n");
2252 exitThread();
2253 }
2254 #endif
2255 }
2256
2257 }
2258 return NULL;
2259 }
2260
2261
2262
2263 int isChannelClosable(int id)
2264 {
2265 MUTEX_THREAD_LOCK(mutex);
2266 signalChannels[id].flows--;
2267 if (signalChannels[id].flows == 0) {
2268 sendRelease(signalChannels[id].socket);
2269
2270 if (closeSock(signalChannels[id].socket) < 0){
2271 printf("error closing socket\n");
2272 fflush(stdout);
2273 }
2274 signalChannels[id].socket = -1;
2275 signalChannels[id].errorLog = false;
2276
2277 closePipe(signalChannels[id].pipe);
2278
2279 MUTEX_THREAD_UNLOCK(mutex);
2280 PRINTD(1, "isChannelClosable: closing signaling channel\n");
2281 return(1);
2282 }
2283 MUTEX_THREAD_UNLOCK(mutex);
2284 return(0);
2285 }
2286
2287
2288 int identifySignalManager(int flowId, int *chanId, struct addrinfo * &DestHost,bool &dstAddrSpecify)
2289 {
2290 #ifdef UNIX
2291 int signalSock;
2292 #else
2293 unsigned int signalSock;
2294 #endif
2295 char type =0;
2296 bool checkChanID = false;
2297
2298 PRINTD(1,"identifySignalManager: flowId=%d\n",flowId);
2299
2300 MUTEX_THREAD_LOCK(mutex);
2301
2302 checkChanID = checkDestHostIP(chanId, DestHost);
2303
2304
2305
2306 if ((checkChanID == false) && (okToNewSignalingChan == true)) {
2307 PRINTD(1,"identifySignalManager: Open a new signaling channel, chanId=%d, flowId=%d\n",*chanId,flowId);
2308
2309
2310 #ifdef UNIX
2311 createTransportChan(signalSock, DestHost,dstAddrSpecify,flows[flowId].SigSrcHost,flows[flowId].SigSrcPort,flows[flowId].SigInterface);
2312 #else
2313 createTransportChan(signalSock, DestHost,dstAddrSpecify,flows[flowId].SigSrcHost,flows[flowId].SigSrcPort);
2314 #endif
2315 createSignalChan(signalSock);
2316
2317
2318
2319 if ((passiveMode == true) && (multiFlows == 1)){
2320
2321 GET_TIME_OF_DAY(&RifTime, _tend_, _tstart_, _sec_, _msec_, 1, 0);
2322 PRINTD(1,"identifySignalManager: Signaling Time: %lu.%06lu sec\n",
2323 (unsigned long int) RifTime.tv_sec, (unsigned long int) RifTime.tv_usec);
2324
2325
2326 okToNewSignalingChan=false;
2327
2328 getnameinfo(DestHost->ai_addr,DestHost->ai_addrlen,globalSigAddr,INET6_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
2329 PRINTD(1,"identifySignalManager: global signaling address: %s, flowId=%d\n",globalSigAddr,flowId);
2330 }
2331
2332
2333
2334 if ((namelogReceiver == 1) && (logServer == 0)) {
2335 if (sendNameLog(signalSock, logFileReceiver, sizeof(logFileReceiver)) < 0) {
2336 printf("sendNameLog() error \n");
2337 fflush(stdout);
2338 return (-1);
2339 }
2340 recv(signalSock, (char *) &type, sizeof(type), 0);
2341 }
2342 if (logServer == 1){
2343 if (sendLog(signalSock, serverLogReceiver, protoTx_ServerLogReceiver, logFileReceiver)<0){
2344 printf("sendLog() error \n");
2345 fflush(stdout);
2346 return(-1);
2347 }
2348 recv(signalSock, (char *) &type, sizeof(type), 0);
2349 }
2350
2351
2352
2353 *chanId = 0;
2354 while (signalChannels[*chanId].socket != -1)
2355 (*chanId)++;
2356
2357 if (createNewPipe(signalChannels[*chanId].pipe) < 0) {
2358 printf("signalManager() could not open pipe for flowId %d\n", flowId);
2359 fflush(stdout);
2360 return(-1);
2361 }
2362
2363 signalChannels[*chanId].DestAddr = DestHost;
2364 signalChannels[*chanId].flows = 1;
2365 signalChannels[*chanId].socket = signalSock;
2366
2367 if ((logServer ==1 ) || (namelogReceiver ==1)){
2368 if (type == TSP_ERR_MSG_4){
2369 printf("signalManager() TSP_ERR_MSG_4\n");
2370 fflush(stdout);
2371 signalChannels[*chanId].errorLog = true;
2372 }
2373 }
2374 if (signalChannels[*chanId].errorLog == false){
2375
2376 CREATE_THREAD(chanId, signalManager, NULL, signalChannels[*chanId].handle, true);
2377
2378
2379 signalChanCount++;
2380 }
2381 MUTEX_THREAD_UNLOCK(mutex);
2382 return(1);
2383
2384 } else {
2385
2386
2387 if ((passiveMode == true) && (multiFlows == 1)){
2388
2389
2390 if (checkChanID == false){
2391 *chanId=0;
2392 signalChannels[*chanId].flows++;
2393 }
2394 PRINTD(1,"identifySignalManager: Using a old signaling channel,\n\tchanId=%d,\n\tflow_number=%d,\n\tglobal_signaling_address=%s\n\n",
2395 *chanId,signalChannels[*chanId].flows,globalSigAddr);
2396
2397
2398 if(DestHost) freeaddrinfo(DestHost);
2399 if(getaddrinfo(globalSigAddr, NULL, &hint, &(DestHost)) < 0){
2400 perror("identifySignalManager getaddrinfo() (1)");
2401 exitThread();
2402 }
2403 dstAddrSpecify = true;
2404 }
2405
2406
2407 MUTEX_THREAD_UNLOCK(mutex);
2408 return(0);
2409 }
2410 }
2411
2412
2413
2414 void *flowSender(void *para)
2415 {
2416 flowDescriptor *param = (flowDescriptor *) para;
2417 int id = param->id;
2418
2419
2420 struct addrinfo *SrcHost = flows[id].SrcHost;
2421 struct addrinfo *DestHost = flows[id].DestHost;
2422 #if defined UNIX && ! defined BSD
2423 char* iface = flows[id].iface;
2424 #endif
2425 BYTE meter = flows[id].meter;
2426 BYTE l4Proto = flows[id].l4Proto;
2427 BYTE l7Proto = flows[id].l7Proto;
2428 int icmptype = flows[id].icmptype;
2429 int DSByte = flows[id].DSByte;
2430 unsigned long Duration = flows[id].Duration;
2431 int TTL = flows[id].TTL;
2432 SumRandom * IntArriv = flows[id].IntArriv;
2433 SumRandom * PktSize = flows[id].PktSize;
2434 int sigChanId = flows[id].sigChanId;
2435 bool Nagle = flows[id].Nagle;
2436 struct info *infos = NULL;
2437 bool SrcPortSpecify = flows[id].srcPortSpecify;
2438 bool SrcAddrSpecify = flows[id].srcAddrSpecify;
2439 unsigned int numPacket = flows[id].numPacket;
2440 unsigned long int byteToSend = 1024 * flows[id].KBToSend;
2441
2442 char dotIpV4[INET_ADDRSTRLEN] = "0.0.0.0";
2443 char dotIpV6[INET6_ADDRSTRLEN] = "::";
2444
2445
2446 unsigned char payload[MAX_PAYLOAD_SIZE];
2447
2448 unsigned char *ptrSeqNum = payload + sizeof(uint32_t);
2449 unsigned char *ptrTimeSec = ptrSeqNum + sizeof(uint32_t);
2450 unsigned char *ptrTimeUsec = ptrTimeSec + sizeof(uint32_t);
2451 unsigned char *ptrSize = NULL;
2452 unsigned char *ptrToTruePayload = NULL;
2453
2454 long int indexBuffer = 0;
2455
2456 fd_set read_set, active_set;
2457 int exitflag, size, size_r = 0;
2458 long int time, time1, time2;
2459 struct TTicker Ticker;
2460 struct timeval start_time, end_time, RcvTime, timeout;
2461
2462 int socktype;
2463 int prototype = 0;
2464 unsigned int seqNum = 1;
2465 unsigned int recvPkts = 0;
2466 Real wait;
2467 int count = 0;
2468 int sockchk = 0;
2469 struct pipeMsg msg;
2470
2471 char HelpSrcAddress[INET6_ADDRSTRLEN];
2472 char HelpDstAddress[INET6_ADDRSTRLEN];
2473 int tmpPort_SrcPort = 0;
2474 int tmpPort_DstPort = 0;
2475
2476
2477 uint32_t net_id = 0;
2478 uint32_t net_seqNum = 0;
2479 uint32_t net_TimeSec = 0;
2480 uint32_t net_TimeUsec = 0;
2481 uint32_t net_size = 0;
2482
2483
2484
2485 unsigned int indexVectSize = 0;
2486 unsigned int dimVectSize = 0;
2487 unsigned int indexVectTime = 0;
2488 unsigned int dimVectTime = 0;
2489 bool SizeFile = false;
2490 bool TimeFile = false;
2491
2492
2493 unsigned long int byteSent = 0;
2494
2495 struct addrinfo *LogAddr;
2496
2497 #ifdef WIN32
2498 LARGE_INTEGER _tstart, _tend, _tprec;
2499 unsigned long secondi = 0, microsecondi = 0;
2500 int first_update = 1;
2501 #endif
2502
2503 #ifdef SCTP
2504 int sctpId = flows[id].sctpId;
2505 int sctpStream;
2506 bool newSock = false;
2507 int ppid;
2508 #endif
2509
2510
2511 #ifdef BURSTY
2512 double OnDuration;
2513 double OffDuration;
2514 struct timeval StartOnTime, FinishOnTime, WaitTime;
2515 SumRandom * OnPeriod = flows[id].OnPeriod;
2516 SumRandom * OffPeriod = flows[id].OffPeriod;
2517 #endif
2518
2519
2520
2521 TSTART(_tstart, secondi, microsecondi, first, meter, 0);
2522
2523
2524 if (flows[id].Delay) {
2525 struct timeval ThisTime;
2526
2527 PRINTD(1, "flowSender: Delay=%lu, flowId=%d\n", flows[id].Delay, id);
2528
2529
2530 flows[id].FlowStartTime.tv_sec = RifTime.tv_sec + flows[id].Delay / 1000 +
2531 (flows[id].FlowStartTime.tv_usec = RifTime.tv_usec + (flows[id].Delay % 1000) * 1000) / 1000000;
2532
2533 flows[id].FlowStartTime.tv_usec %= 1000000;
2534
2535 PRINTD(1,"flowSender: Expected Flow Start Time: %lu.%06lu sec, flowId=%d\n",
2536 (unsigned long int) flows[id].FlowStartTime.tv_sec,
2537 (unsigned long int) flows[id].FlowStartTime.tv_usec,id);
2538
2539 GET_TIME_OF_DAY(&ThisTime, _tend_, _tstart_, _sec_, _msec_, meter, 0);
2540 while (timeval_subtract(&ThisTime,flows[id].FlowStartTime,ThisTime) == 0) {
2541 PRINTD(1,"flowSender: Timeout: %lu.%06lu sec, flowId=%d\n",
2542 (unsigned long int) ThisTime.tv_sec,
2543 (unsigned long int) ThisTime.tv_usec,id);
2544
2545 ms_sleep(ThisTime);
2546 GET_TIME_OF_DAY(&ThisTime, _tend_, _tstart_, _sec_, _msec_, meter, 0);
2547
2548 PRINTD(1,"flowSender: Real Flow Start Time: %lu.%06lu sec, flowId=%d\n",
2549 (unsigned long int) ThisTime.tv_sec,
2550 (unsigned long int) ThisTime.tv_usec,id);
2551 }
2552 }
2553
2554
2555 PRINTD(1,"flowSender: flowSender() n° %d started\n",id);
2556
2557
2558 if (logging) {
2559 infos = (struct info *) malloc(logbuffer_size * sizeof(info));
2560 }
2561
2562
2563 if (DestHost->ai_family == PF_INET6){
2564 getaddrinfo("::", NULL, &hint, &LogAddr);
2565 }else if (DestHost->ai_family == PF_INET){
2566 getaddrinfo("0.0.0.0", NULL, &hint, &LogAddr);
2567 }
2568
2569
2570
2571 dimVectSize=flows[id].vectSize.size();
2572 if (dimVectSize > 0){
2573 SizeFile=true;
2574 indexVectSize=0;
2575 PRINTD(1,"flowSender: flows[id].vectSize.size() = %d\n",dimVectSize);
2576 }
2577
2578 dimVectTime=flows[id].vectTime.size();
2579 if (dimVectTime > 0){
2580 TimeFile=true;
2581 indexVectTime=1;
2582 PRINTD(1,"flowSender: flows[id].vectTime.size() = %d\n",dimVectTime);
2583 }
2584
2585
2586 GET_TIME_OF_DAY(&start_time, _tend, _tstart, secondi, microsecondi, meter, 0);
2587
2588
2589 if (flows[id].dimPayloadBuffer == 0) {
2590
2591 srand(start_time.tv_sec);
2592 for (int i = 0; i < MAX_PAYLOAD_SIZE; i++)
2593 payload[i] = (unsigned char) (rand() % 255);
2594 } else {
2595
2596 for (int i = 0; i < MAX_PAYLOAD_SIZE; i++)
2597 payload[i] = 0;
2598
2599
2600 if (flows[id].payloadLogType == PL_STANDARD)
2601 ptrToTruePayload = ptrTimeUsec + sizeof(uint32_t);
2602 else if (flows[id].payloadLogType == PL_SHORT)
2603 ptrToTruePayload = ptrSeqNum + sizeof(uint32_t);
2604 else
2605 ptrToTruePayload = payload;
2606 }
2607
2608
2609 if (flows[id].payloadLogType != PL_NONE) {
2610 net_id = htonl(id);
2611
2612 memcpy(payload, &net_id, sizeof(net_id));
2613 }
2614
2615
2616 msg.flowId = id;
2617
2618
2619 if (SrcPortSpecify == true) {
2620 GET_PORT(flows[id].SrcHost, tmpPort_SrcPort);
2621 }
2622
2623
2624 if (SrcAddrSpecify == true){
2625 if (SrcHost->ai_family == PF_INET6){
2626 getnameinfo(SrcHost->ai_addr,SrcHost->ai_addrlen,dotIpV6,INET6_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
2627 }else{
2628 getnameinfo(SrcHost->ai_addr,SrcHost->ai_addrlen,dotIpV4,INET_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
2629 }
2630 }
2631
2632
2633
2634 switch (l4Proto) {
2635 case L4_PROTO_UDP:
2636 socktype = SOCK_DGRAM;
2637 break;
2638 case L4_PROTO_ICMP:
2639 socktype = SOCK_RAW;
2640 if (meter == METER_OWDM)
2641 prototype = (DestHost->ai_family == AF_INET) ? IPPROTO_ICMP : IPPROTO_ICMPV6;
2642 else if (meter == METER_RTTM)
2643 prototype = IPPROTO_RAW;
2644 break;
2645 #ifdef SCTP
2646 case L4_PROTO_SCTP:
2647 socktype = SOCK_STREAM;
2648 prototype = IPPROTO_SCTP;
2649 DestHost->ai_family = PF_INET;
2650 break;
2651 #endif
2652 #ifdef DCCP
2653 case L4_PROTO_DCCP:
2654 socktype = SOCK_DCCP;
2655 PRINTD(1,"flowSender: socktype DCCP \n");
2656 break;
2657 #endif
2658 default:
2659 socktype = SOCK_STREAM;
2660 break;
2661 }
2662
2663
2664 #ifdef UNIX
2665 int sock;
2666
2667 #ifndef NOIPV6
2668
2669 PRINTD(1,"flowSender: Try to create an IPv6 Socket\n");
2670 if (getaddrinfo(dotIpV6, NULL, &hint, &SrcHost) < 0) {
2671
2672 PRINTD(1,"flowSender: Try to create an IPv4 Socket\n");
2673 if (SrcHost)
2674 freeaddrinfo(SrcHost);
2675 #endif
2676 if (getaddrinfo(dotIpV4, NULL, &hint, &SrcHost) < 0) {
2677 perror("flowSender");
2678 printf("Error into getaddrinfo Flow ID: %d\n", id);
2679 msg.code = MSG_SM_ERRFLOW;
2680
2681
2682 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
2683 perror("flowSender sending msg");
2684 exitThread();
2685 }
2686 #ifndef NOIPV6
2687 }
2688 #endif
2689
2690 #ifdef SCTP
2691 else if (l4Proto == L4_PROTO_SCTP) {
2692 DestHost->ai_family = PF_INET6;
2693 SrcHost->ai_family = PF_INET6;
2694 }
2695 #endif
2696 #endif
2697
2698 #ifdef WIN32
2699 unsigned int sock;
2700 #ifdef IPv6RECV
2701 if (getaddrinfo(dotIpV6, NULL, &hint, &SrcHost) <0){
2702 #else
2703 if (getaddrinfo(dotIpV4, NULL, &hint, &SrcHost)<0){
2704 #endif
2705 perror("flowSender");
2706 printf("Error into getaddrinfo Flow ID: %d\n", id);
2707 msg.code = MSG_SM_ERRFLOW;
2708
2709 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
2710 perror("flowSender sending msg");
2711 exitThread();
2712 }
2713 #endif
2714
2715 #ifdef SCTP
2716 MUTEX_THREAD_LOCK(mutexLog);
2717 if (l4Proto == L4_PROTO_SCTP && sctpSessions[sctpId].sock != -1) {
2718
2719 sock = sctpSessions[sctpId].sock;
2720 } else {
2721
2722 sock = socket(DestHost->ai_family, socktype, prototype);
2723 sctpSessions[sctpId].sock = sock;
2724 newSock = true;
2725 }
2726 sctpStream = sctpSessions[sctpId].busyStreams++;
2727 MUTEX_THREAD_UNLOCK(mutexLog);
2728 #else
2729
2730 sock = socket(DestHost->ai_family, socktype, prototype);
2731 #endif
2732 if (sock < 0) {
2733 perror("flowSender");
2734 printf("Could not create a new socket. Flow ID: %d\n", id);
2735 msg.code = MSG_SM_ERRFLOW;
2736
2737 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
2738 perror("flowSender sending msg");
2739 exitThread();
2740 }
2741
2742 #ifdef SCTP
2743
2744 if (l4Proto == L4_PROTO_SCTP && newSock)
2745 {
2746 struct sctp_initmsg ginmsg;
2747
2748 ginmsg.sinit_num_ostreams = sctpSessions[sctpId].numStreams;
2749 ginmsg.sinit_max_instreams = sctpSessions[sctpId].numStreams;
2750 ginmsg.sinit_max_attempts = 0;
2751 ginmsg.sinit_max_init_timeo = 0;
2752 int initmsglen = sizeof(struct sctp_initmsg);
2753 if (setsockopt(sock, IPPROTO_SCTP, SCTP_INITMSG, &ginmsg, initmsglen) < 0){
2754 printf("flowSender sending msg\n");
2755 fflush(stdout);
2756 }
2757 }
2758 #endif
2759
2760 #ifdef DCCP
2761 if (l4Proto == L4_PROTO_DCCP) {
2762 int optval = 0;
2763 if (setsockopt(sock, SOL_DCCP, DCCP_SOCKOPT_SERVICE, &optval, sizeof(optval))<0)
2764 printf("Error: flowSender setsockopt DCCP_SERVICE\n");
2765 PRINTD(1,"flowSender: setsockopt DCCP_SERVICE \n");
2766 }
2767 #endif
2768
2769
2770 #ifdef UNIX
2771 int reuse = 1;
2772 int optlen = sizeof(reuse);
2773 #ifdef SCTP
2774 if (l4Proto != L4_PROTO_SCTP)
2775 #endif
2776 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, optlen)<0){
2777 printf("flowSender sending msg");
2778 }
2779 #endif
2780
2781
2782 if (SrcPortSpecify || SrcAddrSpecify){
2783
2784
2785
2786 if((passiveMode == true) && ((SrcHost->ai_family) != (DestHost->ai_family)) && (SrcHost->ai_family==PF_INET)){
2787 char oldAddr[INET_ADDRSTRLEN+1];
2788 char newAddr[INET6_ADDRSTRLEN+1];
2789
2790
2791 getnameinfo(SrcHost->ai_addr,SrcHost->ai_addrlen,oldAddr,INET_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
2792
2793 freeaddrinfo(SrcHost);
2794
2795 strcpy(newAddr,"::ffff:");
2796 strcat(newAddr,oldAddr);
2797 PRINTD(1,"flowSender: New IPv4 to IPv6 address %s\n",newAddr);
2798
2799 getaddrinfo(newAddr, NULL, &hint, &SrcHost);
2800 }
2801
2802 #ifdef DEBUG
2803 char addr[INET6_ADDRSTRLEN+1];
2804 getnameinfo(SrcHost->ai_addr,SrcHost->ai_addrlen,addr,INET6_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
2805 PRINTD(1,"flowSender: Set Address/Port on Sender Side. \n\tAddress: %s \n\tPort number: %d \n", addr, ntohs(tmpPort_SrcPort));
2806 #endif
2807
2808
2809 SET_PORT(SrcHost, tmpPort_SrcPort);
2810 #ifdef SCTP
2811 if (l4Proto != L4_PROTO_SCTP || (l4Proto == L4_PROTO_SCTP && newSock))
2812 #endif
2813 if (bind(sock, SrcHost->ai_addr, SrcHost->ai_addrlen) != 0){
2814 perror("flowSender");
2815 printf("Could not bind a new socket. Flow ID: %d\n", id);
2816 msg.code = MSG_SM_ERRFLOW;
2817
2818 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
2819 perror("flowSender sending msg");
2820 exitThread();
2821 }
2822 }
2823
2824
2825
2826 if ((l7Proto != L7_PROTO_TELNET && l7Proto != L7_PROTO_DNS) && socktype == SOCK_STREAM && Nagle == false) {
2827 printf("Nagle algorithm disabled\n");
2828 fflush(stdout);
2829 #ifdef UNIX
2830 int no_delay = 1;
2831 if (setsockopt(sock, getprotobyname("TCP")->p_proto, TCP_NODELAY, &no_delay, sizeof(no_delay)) < 0) {
2832 #elif defined WIN32
2833 bool no_delayWin=true;
2834
2835 if (setsockopt(sock, getprotobyname("TCP")->p_proto, TCP_NODELAY, (char *) no_delayWin, sizeof(no_delayWin)) < 0) {
2836 #endif
2837 printf("** WARNING ** Flow %d. Cannot disable Nagle Algorithm\n", id);
2838 fflush(stdout);
2839 }
2840 }
2841
2842 #if defined UNIX && ! defined BSD
2843
2844 if (iface) {
2845 int dontRoute = 1;
2846 printf("Binding to device %s\n",iface);
2847 if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface)) < 0) {
2848 printf("** WARNING ** Flow %d. Cannot bind to device %s (hint: you must be root)\n", id, iface);
2849 fflush(stdout);
2850 } else if (setsockopt(sock, SOL_SOCKET, SO_DONTROUTE, &dontRoute, sizeof(int)) < 0) {
2851 printf("** WARNING ** Flow %d. Cannot set don't route (hint: you must be root)\n", id);
2852 fflush(stdout);
2853 }
2854 }
2855 #endif
2856
2857 MUTEX_THREAD_LOCK(mutexLog);
2858 if (logging && isFirstThread == 1) {
2859 PRINTD(1,"flowSender: Log! \n");
2860 isFirstThread = 0;
2861 isFirstThreadRttm = 0;
2862 if (!logremoto) {
2863 out.open(logFile, ios::out | ios::binary | ios::trunc);
2864 if (!out) {
2865 printf("** WARNING ** Flow %d. Cannot create local logfile\n", id);
2866 fflush(stdout);
2867 logging = 0;
2868 }
2869 }
2870 else
2871 createRemoteLogFile(logHost, protoTxLog);
2872 }
2873 MUTEX_THREAD_UNLOCK(mutexLog);
2874
2875
2876 #ifdef SCTP
2877 if (l4Proto != L4_PROTO_SCTP || (l4Proto == L4_PROTO_SCTP && newSock)){
2878 #endif
2879
2880 if(passiveMode == false ){
2881
2882 if (connect(sock, DestHost->ai_addr, DestHost->ai_addrlen) < 0) {
2883 perror("flowSender");
2884 printf("Error in connect(). Flow ID: %d\n", id);
2885
2886 msg.code = MSG_SM_ERRFLOW;
2887 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
2888 perror("flowSender sending msg");
2889 exitThread();
2890 }
2891
2892
2893
2894 }else{
2895 if (flows[id].l4Proto == L4_PROTO_TCP){
2896 if (listen(sock, 1) < 0) {
2897 perror("flowSender");
2898 printf("Error in listen() (Passive Mode). Flow ID: %d\n", id);
2899
2900 msg.code = MSG_SM_ERRFLOW;
2901 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
2902 perror("flowSender sending msg");
2903 exitThread();
2904 }
2905 PRINTD(1,"flowSender: socket in listening. (Passive Mode)\n");
2906 }
2907 }
2908
2909
2910 #ifdef SCTP
2911 }
2912 #endif
2913
2914
2915 PRINTD(1,"flowSender: DS byte %d\n",DSByte);
2916
2917 if (DSByte && (DestHost->ai_family == AF_INET) && setsockopt(sock, SOL_IP, IP_TOS, (char *) &DSByte, sizeof(DSByte)) < 0)
2918 printf("** WARNING ** Flow %d. Could not set DS byte to: %d\n", id, DSByte);
2919
2920 if (DSByte && (DestHost->ai_family == AF_INET6))
2921 #ifdef WIN32
2922 printf("** WARNING ** Flow %d. Windows does not allow to set the Traffic Class field in IPv6 headers\n",id);
2923 #endif
2924 #ifdef UNIX
2925 #ifdef IPV6_TCLASS
2926 if (setsockopt(sock, SOL_IPV6, IPV6_TCLASS, (char *) &DSByte, sizeof(DSByte)) < 0)
2927 printf("** WARNING ** Flow %d. Could not set DS byte to: %d. Linux kernel >= 2.6.14 is required\n", id, DSByte);
2928 #else
2929 printf("** WARNING ** Flow %d. Could not set DS byte to: %d. libc >= 2.4 is required\n", id, DSByte);
2930 #endif
2931 #endif
2932
2933
2934 PRINTD(1,"flowSender: TTL %d\n", TTL);
2935 if (TTL >0 && (setsockopt(sock, IPPROTO_IP, IP_TTL, (char *) &TTL, sizeof(TTL)) < 0))
2936 printf("** WARNING ** Flow %d. Could not set TTL to: %d\n", id, TTL);
2937
2938
2939 #ifdef UNIX
2940 getsockname(sock, SrcHost->ai_addr, &SrcHost->ai_addrlen);
2941 #endif
2942 #ifdef WIN32
2943 getsockname(sock, SrcHost->ai_addr, (int *) &SrcHost->ai_addrlen);
2944 #endif
2945
2946
2947 getInfo(SrcHost, tmpPort_SrcPort, HelpSrcAddress);
2948
2949 getInfo(DestHost, tmpPort_DstPort, HelpDstAddress);
2950
2951 #ifdef SCTP
2952 if (((l4Proto == L4_PROTO_TCP) || (l4Proto == L4_PROTO_SCTP)) && (flows[id].payloadLogType != PL_NONE)) {
2953 #else
2954 if ((l4Proto == L4_PROTO_TCP) && (flows[id].payloadLogType != PL_NONE)) {
2955 #endif
2956 if (flows[id].payloadLogType == PL_STANDARD)
2957 ptrSize = ptrTimeUsec + sizeof(uint32_t);
2958 else
2959 ptrSize = ptrSeqNum + sizeof(uint32_t);
2960
2961
2962
2963
2964 ptrToTruePayload = ptrToTruePayload + sizeof(uint32_t);
2965 }
2966
2967
2968
2969
2970
2971 if ((Duration == DefaultDuration) && (numPacket > 0)) {
2972 Duration = ULONG_MAX;
2973 PRINTD(1,"flowSender: Number of packets to generate : %d \n",numPacket);
2974 } else if (numPacket == 0) {
2975 numPacket = UINT_MAX;
2976 }
2977
2978
2979
2980
2981 if ((Duration == DefaultDuration) && (byteToSend > 0)){
2982 Duration=ULONG_MAX;
2983 PRINTD(1,"flowSender: Number of Kbyte to generate : %lu \n",flows[id].KBToSend);
2984 }else if(byteToSend == 0){
2985 byteToSend=ULONG_MAX;
2986 }
2987
2988
2989
2990
2991 if (passiveMode == true){
2992 msg.code = MSG_SM_SNDACK;
2993
2994 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0){
2995 perror("flowSender sending msg");
2996 exitThread();
2997 }
2998 fflush(stdout);
2999
3000 if ((flows[id].l4Proto == L4_PROTO_TCP)){
3001 PRINTD(1,"flowSender: Waiting for incoming connection. (Passive Mode)\n");
3002
3003 int newSock=accept(sock,NULL,NULL);
3004 if (newSock < 0){
3005 perror("flowSender");
3006 printf("Error in accept() (Passive Mode). Flow ID: %d\n", id);
3007
3008 msg.code = MSG_SM_ERRFLOW;
3009 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
3010 perror("flowSender sending msg");
3011 exitThread();
3012 }
3013 closeSock(sock);
3014 sock=newSock;
3015
3016 }else if((flows[id].l4Proto == L4_PROTO_UDP)){
3017 char ch;
3018
3019 PRINTD(1,"flowSender: Waiting \"Hole punching\" packets. (Passive Mode)\n");
3020
3021
3022 #ifdef WIN32
3023 int len=flows[id].DestHost->ai_addrlen;
3024 recvfrom(sock,&ch,1,0,flows[id].DestHost->ai_addr, &len);
3025 flows[id].DestHost->ai_addrlen=len;
3026 #else
3027 if(recvfrom(sock,&ch,1,0,flows[id].DestHost->ai_addr, &flows[id].DestHost->ai_addrlen)<0){
3028 PRINTD(1,"\nflowSender: Error:%s\n",strerror(errno));
3029 ReportErrorAndExit("flowSender", "error in recvfrom(). (Passive Mode)",programName, id);
3030 }
3031 #endif
3032
3033 if (connect(sock,flows[id].DestHost->ai_addr, flows[id].DestHost->ai_addrlen) < 0) {
3034 perror("flowSender");
3035 printf("Error in connect() (Passive Mode). Flow ID: %d\n", id);
3036
3037 msg.code = MSG_SM_ERRFLOW;
3038 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
3039 perror("flowSender sending msg");
3040 exitThread();
3041 }
3042
3043 }
3044 }
3045
3046
3047 printf("Started sending packets of flow ID: %d\n", id);
3048 fflush(stdout);
3049
3050 Ticker.count = 0.0;
3051
3052
3053
3054
3055 TSTART(_tstart, secondi, microsecondi, first, meter, 0);
3056 GET_TIME_OF_DAY(&start_time, _tend, _tstart, secondi, microsecondi, meter, 0);
3057
3058
3059 end_time.tv_sec = start_time.tv_sec + Duration / 1000 + (end_time.tv_usec =
3060 start_time.tv_usec + (Duration % 1000) * 1000) / 1000000;
3061 end_time.tv_usec %= 1000000;
3062
3063
3064 if (meter == METER_RTTM) {
3065
3066 start_time.tv_sec = 0;
3067 start_time.tv_usec = 0;
3068
3069 FD_ZERO(&active_set);
3070 FD_SET(sock, &active_set);
3071 }
3072 #ifdef WIN32
3073 if (flows[id].serial != INVALID_HANDLE_VALUE){
3074 RTS_Enable (flows[id].serial);
3075 RTS_Disable(flows[id].serial);
3076 DTR_Enable (flows[id].serial);
3077 }
3078 #endif
3079
3080 #ifdef UNIX
3081 if (flows[id].serial != INVALID_HANDLE_VALUE){
3082 RTS_Disable(flows[id].serial);
3083 }
3084 #endif
3085
3086 GET_TIME_OF_DAY(&Ticker.lastTime, _tend, _tstart, secondi, microsecondi, meter, 0);
3087
3088
3089 if(SizeFile==false){
3090
3091 size = (int) PktSize->Next();
3092 }else{
3093
3094 size=flows[id].vectSize[indexVectSize];
3095
3096 indexVectSize = (indexVectSize + 1)%dimVectSize;
3097 }
3098 #ifdef SCTP
3099 if ((l4Proto == L4_PROTO_TCP || l4Proto == L4_PROTO_SCTP) && (flows[id].payloadLogType != PL_NONE)) {
3100 #else
3101 if ((l4Proto == L4_PROTO_TCP) && (flows[id].payloadLogType != PL_NONE)) {
3102 #endif
3103 flows[id].minPayloadSize = flows[id].minPayloadSize + sizeof(net_size);
3104
3105 if (size > MAX_PAYLOAD_SIZE)
3106 size = MAX_PAYLOAD_SIZE;
3107 else if (size < (flows[id].minPayloadSize))
3108 size = flows[id].minPayloadSize;
3109 } else {
3110 if (size > MAX_PAYLOAD_SIZE)
3111 size = MAX_PAYLOAD_SIZE;
3112 else if (size < flows[id].minPayloadSize)
3113 size = flows[id].minPayloadSize;
3114 }
3115 PRINTD(1,"flowSender: Payload minimum size for first packet %d \n",flows[id].minPayloadSize);
3116 PRINTD(1,"flowSender: Size for first packet %d\n",size);
3117
3118
3119 #ifdef BURSTY
3120
3121 if (flows[id].bursty) {
3122
3123 OnDuration = (double) OnPeriod->Next();
3124 PRINTD(1,"flowSender: On Duration = %lf\n",OnDuration);
3125 } else {
3126
3127 OnDuration = (double)Duration;
3128 }
3129 #endif
3130
3131
3132
3133 do {
3134 long int timeUsec;
3135
3136
3137 #ifdef BURSTY
3138 do {
3139
3140 gettimeofday(&StartOnTime, NULL);
3141
3142
3143 Ticker.count = 0.0;
3144 #endif
3145
3146
3147 time = Ticker.lastTime.tv_sec % 86400L;
3148 timeUsec = Ticker.lastTime.tv_usec;
3149
3150
3151 net_seqNum = htonl(seqNum);
3152 net_TimeSec = htonl(time);
3153 net_TimeUsec = htonl(Ticker.lastTime.tv_usec);
3154 net_size = htonl(size);
3155
3156
3157
3158 if (flows[id].payloadLogType == PL_STANDARD) {
3159 memcpy(ptrSeqNum, &net_seqNum, sizeof(net_seqNum));
3160 memcpy(ptrTimeSec, &net_TimeSec, sizeof(net_TimeSec));
3161 memcpy(ptrTimeUsec, &net_TimeUsec, sizeof(net_TimeUsec));
3162 } else if (flows[id].payloadLogType == PL_SHORT)
3163 memcpy(ptrSeqNum, &net_seqNum, sizeof(net_seqNum));
3164 #ifdef SCTP
3165 if (((l4Proto == L4_PROTO_TCP) || (l4Proto == L4_PROTO_SCTP)) && (flows[id].payloadLogType != PL_NONE))
3166 #else
3167 if ((l4Proto == L4_PROTO_TCP ) && (flows[id].payloadLogType != PL_NONE))
3168 #endif
3169 memcpy(ptrSize, &net_size, sizeof(net_size));
3170
3171
3172
3173 if (flows[id].dimPayloadBuffer != 0) {
3174
3175 int byteToCopy = size - (int) (ptrToTruePayload - payload);
3176
3177
3178 if ((indexBuffer + byteToCopy) <= flows[id].dimPayloadBuffer) {
3179
3180 memcpy(ptrToTruePayload, (void *) ((long int) flows[id].ptrPayloadBuffer + indexBuffer),
3181 byteToCopy);
3182
3183 indexBuffer += byteToCopy;
3184
3185 if (indexBuffer == flows[id].dimPayloadBuffer)
3186
3187 indexBuffer = 0;
3188 } else {
3189
3190 byteToCopy = flows[id].dimPayloadBuffer - indexBuffer;
3191 memcpy(ptrToTruePayload, (void *) ((long int) flows[id].ptrPayloadBuffer + indexBuffer),
3192 byteToCopy);
3193
3194 memset((void *) ((long int) ptrToTruePayload + byteToCopy), 0, (size - byteToCopy));
3195
3196 indexBuffer = 0;
3197
3198 }
3199 }
3200
3201
3202 if ((l4Proto == L4_PROTO_ICMP) && (meter == METER_OWDM)) {
3203 if (DestHost->ai_family == AF_INET) {
3204 icmppkt pkt;
3205 pkt.icmp_buf.icmp_type = icmptype;
3206 pkt.icmp_buf.icmp_code = 0;
3207 pkt.icmp_buf.icmp_cksum = 0;
3208 memcpy(&pkt.packet, payload, size);
3209 pkt.icmp_buf.icmp_cksum = checksum((unsigned short *) &pkt, size + sizeof(icmp));
3210 sockchk = sendto(sock, (char *) &pkt, size + sizeof(icmp), 0, DestHost->ai_addr,
3211 DestHost->ai_addrlen);
3212 } else if (DestHost->ai_family == AF_INET6) {
3213 #ifdef UNIX
3214 icmppktv6 pkt;
3215 pkt.icmp_buf.icmp_type = icmptype;
3216 pkt.icmp_buf.icmp_code = 0;
3217 pkt.icmp_buf.icmp_cksum = 0;
3218 memcpy(&pkt.packet, payload, size);
3219 pkt.icmp_buf.icmp_cksum = checksum((unsigned short *) &pkt, size + sizeof(icmpv6));
3220 struct sockaddr_in6 from;
3221 from.sin6_family = AF_INET6;
3222 from.sin6_port = 0;
3223 from.sin6_addr = ((struct sockaddr_in6*) (DestHost->ai_addr))->sin6_addr;
3224 sockchk = sendto(sock, (char *) &pkt, size + sizeof(icmpv6), 0, (sockaddr *) &from,
3225 sizeof(from));
3226 #endif
3227 }
3228 #ifndef SCTP
3229 } else {
3230 sockchk = send(sock, (char *) payload, size, 0);
3231 }
3232 #else
3233 } else if (l4Proto != L4_PROTO_SCTP) {
3234 sockchk = send(sock, (char *) payload, size, 0);
3235 } else {
3236 ppid = rand();
3237 sockchk = sctp_sendmsg(sock, (char *) payload, size, (struct sockaddr *) DestHost->ai_addr,
3238 DestHost->ai_addrlen, ppid, 0, sctpStream, 0, 0);
3239 }
3240 #endif
3241
3242 if (flows[id].serial != INVALID_HANDLE_VALUE) {
3243 DTR_Disable(flows[id].serial);
3244 DTR_Enable(flows[id].serial);
3245 }
3246
3247 if (sockchk < 0 && !termsock) {
3248 #if DEBUG > 0
3249
3250 printf("** WARNING ** flow: %d Packet: %d not sent\n", id, seqNum);
3251 perror("send()");
3252 #endif
3253 fflush(stdout);
3254 } else {
3255
3256 if ((logging) && (meter == METER_OWDM)) {
3257 if (seqNum == 1) {
3258
3259 #ifdef WIN32
3260 int len=LogAddr->ai_addrlen;
3261 getsockname(sock,LogAddr->ai_addr,&len);
3262 LogAddr->ai_addrlen=len;
3263 #else
3264 getsockname(sock, LogAddr->ai_addr, &(LogAddr->ai_addrlen));
3265 #endif
3266 getInfo(LogAddr, tmpPort_SrcPort, HelpSrcAddress);
3267
3268 #ifdef WIN32
3269 len=LogAddr->ai_addrlen;
3270 getsockname(sock,LogAddr->ai_addr,&len);
3271 LogAddr->ai_addrlen=len;
3272 #else
3273 getpeername(sock, LogAddr->ai_addr, &(LogAddr->ai_addrlen));
3274 #endif
3275 getInfo(LogAddr, tmpPort_DstPort, HelpDstAddress);
3276 }
3277
3278 if (l7Proto == L7_PROTO_TELNET)
3279 size = size - 20;
3280 if (logremoto == 0) {
3281
3282 writeInBufferStandard(&infos[count], htonl(id), htonl(seqNum), HelpSrcAddress,
3283 HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort, time, time,
3284 Ticker.lastTime.tv_usec, Ticker.lastTime.tv_usec, size);
3285 } else {
3286
3287 writeInBufferStandard(&infos[count], htonl(id), htonl(seqNum), HelpSrcAddress,
3288 HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort, time, time,
3289 Ticker.lastTime.tv_usec, Ticker.lastTime.tv_usec, size);
3290 infosHostToNet(&infos[count]);
3291 }
3292
3293 count++;
3294
3295 if (count == logbuffer_size) {
3296 flushBuffer(infos, count);
3297 count = 0;
3298 }
3299 }
3300 }
3301
3302
3303 if (SizeFile == false) {
3304
3305 size = (int) PktSize->Next();
3306 } else {
3307
3308 size = flows[id].vectSize[indexVectSize];
3309
3310 indexVectSize = (indexVectSize + 1) % dimVectSize;
3311 }
3312 #ifdef SCTP
3313 if ((l4Proto == L4_PROTO_TCP || l4Proto == L4_PROTO_SCTP) && (flows[id].payloadLogType != PL_NONE)) {
3314 #else
3315 if ((l4Proto == L4_PROTO_TCP) && (flows[id].payloadLogType != PL_NONE)) {
3316 #endif
3317 if (size > MAX_PAYLOAD_SIZE)
3318 size = MAX_PAYLOAD_SIZE;
3319 else if (size < (flows[id].minPayloadSize))
3320 size = flows[id].minPayloadSize;
3321 } else {
3322 if (size > MAX_PAYLOAD_SIZE)
3323 size = MAX_PAYLOAD_SIZE;
3324 else if (size < flows[id].minPayloadSize)
3325 size = flows[id].minPayloadSize;
3326 }
3327 PRINTD(1,"flowSender: Payload minimum size for next packet %d \n",flows[id].minPayloadSize);
3328 PRINTD(2,"flowSender: Size for next packet %d\n",size);
3329
3330
3331
3332 if (TimeFile == false) {
3333 wait = IntArriv->Next();
3334 } else {
3335
3336 wait = flows[id].vectTime[indexVectTime];
3337
3338 indexVectTime = (indexVectTime + 1) % dimVectTime;
3339 }
3340
3341 PRINTD(2,"flowSender: IDT for next packet %f ms\n",wait);
3342
3343 UPDATE_TICKER(&Ticker, _tend, _tprec, _tstart, first_update);
3344 if (flows[id].mean_adjustment == 0)
3345 Ticker.count = 0;
3346 exitflag = 0;
3347 while (!exitflag) {
3348 if (wait <= Ticker.count) {
3349 exitflag = 1;
3350 timeout.tv_sec = 0;
3351 timeout.tv_usec = 0;
3352 } else {
3353 wait -= Ticker.count;
3354 Ticker.count = 0.0;
3355
3356
3357 if (flows[id].pollingMode == false) {
3358 timeout.tv_sec = (long) wait / 1000;
3359
3360 timeout.tv_usec = ((long) (wait * 1000) % 1000000);
3361
3362 } else {
3363
3364 if (wait <= MAX_POLL) {
3365
3366 timeout.tv_sec = 0;
3367 timeout.tv_usec = 0;
3368 } else {
3369
3370 timeout.tv_sec = (long) (wait - MAX_POLL) / 1000;
3371 timeout.tv_usec = (long) ((wait - MAX_POLL) * 1000) % 1000000;
3372 }
3373 }
3374
3375 }
3376
3377 if (meter == METER_RTTM) {
3378 read_set = active_set;
3379 if (select(FD_SETSIZE, &read_set, NULL, NULL, &timeout) < 0) {
3380 perror("flowSender");
3381 printf("invalid fd or operation interrupted by signal\n");
3382
3383 msg.code = MSG_SM_ERRFLOW;
3384 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0)
3385 perror("flowSender sending msg");
3386 exitThread();
3387 }
3388
3389 if (FD_ISSET(sock, &read_set)) {
3390 GET_TIME_OF_DAY(&RcvTime, _tend, _tstart, secondi, microsecondi, meter, 0);
3391 if (l4Proto == L4_PROTO_TCP)
3392
3393 size_r = TCPrecvPacket((unsigned char *) payload, sock,
3394 flows[id].minPayloadSize, flows[id].payloadLogType);
3395 #ifdef SCTP
3396 else if (l4Proto == L4_PROTO_SCTP) {
3397 size_r = SCTPrecvPacket((unsigned char *) payload, sock, sctpId,
3398 flows[id].minPayloadSize, flows[id].payloadLogType);
3399 PRINTD(2, "flowSender: in sctp settato flows[id].minPayloadSize: %d\n",
3400 flows[id].minPayloadSize);
3401 }
3402 #endif
3403 #ifdef DCCP
3404
3405
3406 #endif
3407 else
3408 size_r = recv(sock, (char *) payload, MAX_PAYLOAD_SIZE, 0);
3409 time1 = ntohl((*(uint32_t *) ptrTimeSec));
3410 time2 = RcvTime.tv_sec % 86400L;
3411 recvPkts++;
3412
3413
3414 if ((logging) && (size_r > 0) && (meter == METER_RTTM)) {
3415 if (logremoto == 0) {
3416
3417 writeInBufferStandard(&infos[count], *(uint32_t *) payload,
3418 *(uint32_t *) ptrSeqNum, HelpSrcAddress,
3419 HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort,
3420 time1, time2, timeUsec, RcvTime.tv_usec, size_r);
3421 } else {
3422
3423 writeInBufferStandard(&infos[count], *(uint32_t *) payload,
3424 *(uint32_t *) ptrSeqNum, HelpSrcAddress,
3425 HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort,
3426 time1, time2, timeUsec, RcvTime.tv_usec, size_r);
3427 infosHostToNet(&infos[count]);
3428 }
3429 count++;
3430
3431 if (count == logbuffer_size) {
3432 flushBuffer(infos, count);
3433 count = 0;
3434 }
3435 }
3436 }
3437 }
3438 else {
3439
3440
3441 ms_sleep(timeout);
3442 }
3443
3444 if (!exitflag)
3445 UPDATE_TICKER(&Ticker, _tend, _tprec, _tstart, first_update);
3446 else
3447 Ticker.count -= wait;
3448 }
3449
3450 #ifdef WIN32
3451 GET_TIME_OF_DAY(&Ticker.lastTime, _tend, _tstart, secondi, microsecondi, meter, 0);
3452 #endif
3453 seqNum++;
3454
3455
3456 byteSent += sockchk;
3457 #if DEBUG > 2
3458 if (flows[id].KBToSend > 0)
3459 PRINTD(3,"Total bytes sent %lu = %f Kbyte \n",byteSent,((double)byteSent/1024));
3460 #endif
3461
3462
3463
3464 #ifdef BURSTY
3465
3466
3467 gettimeofday(&FinishOnTime, NULL);
3468
3469 OnDuration -= ((double) (FinishOnTime.tv_sec - StartOnTime.tv_sec) * 1000.0
3470 + (double) (FinishOnTime.tv_usec - StartOnTime.tv_usec) / 1000.0);
3471 PRINTD(2,"flowSender: onDuration = %lf\n",OnDuration);
3472
3473 } while ((OnDuration > 0)
3474 && ((Ticker.lastTime.tv_sec < end_time.tv_sec)
3475 || ((Ticker.lastTime.tv_sec == end_time.tv_sec) && (Ticker.lastTime.tv_usec < end_time.tv_usec)))
3476 && ((seqNum - 1) < numPacket) && (byteSent < byteToSend));
3477 PRINTD(1,"flowSender: Exited from while with onDuration = %lf\n",OnDuration);
3478
3479 if (flows[id].bursty) {
3480
3481 OffDuration = (double) OffPeriod->Next();
3482 WaitTime.tv_sec = (long int) OffDuration / 1000;
3483 WaitTime.tv_usec = (OffDuration - (WaitTime.tv_sec * 1000)) * 1000;
3484 PRINTD(1,"flowSender: offDuration = %lf\n",OffDuration);
3485
3486 ms_sleep(WaitTime);
3487
3488 OnDuration = (double) OnPeriod->Next();
3489 PRINTD(1,"flowSender: On Duration = %lf\n",OnDuration);
3490 }
3491 #endif
3492
3493
3494 } while ((Ticker.lastTime.tv_sec < end_time.tv_sec
3495 || (Ticker.lastTime.tv_sec == end_time.tv_sec && Ticker.lastTime.tv_usec < end_time.tv_usec))
3496 && ((seqNum - 1) < numPacket) && (byteSent < byteToSend));
3497
3498
3499 if (flows[id].serial != INVALID_HANDLE_VALUE)
3500 RTS_Enable(flows[id].serial);
3501
3502
3503
3504 if (meter == METER_RTTM) {
3505 bool flag = true;
3506 while (flag && (seqNum-1) > recvPkts) {
3507 start_time.tv_sec = rttmDelay;
3508 read_set = active_set;
3509 if (select(FD_SETSIZE, &read_set, NULL, NULL, &start_time) < 0) {
3510 perror("send_packrttm");
3511 flag = true;
3512 } else {
3513
3514 if (FD_ISSET(sock, &read_set)) {
3515 GET_TIME_OF_DAY(&RcvTime, _tend, _tstart, secondi, microsecondi, meter, 0);
3516
3517 if (l4Proto == L4_PROTO_TCP)
3518 TCPrecvPacket((unsigned char *) payload, sock, flows[id].minPayloadSize,
3519 flows[id].payloadLogType);
3520 #ifdef SCTP
3521 else if (l4Proto == L4_PROTO_SCTP) {
3522 SCTPrecvPacket((unsigned char *) payload, sock, sctpId, flows[id].minPayloadSize,
3523 flows[id].payloadLogType);
3524 }
3525 #endif
3526 #ifdef DCCP
3527
3528
3529 #endif
3530 else
3531 size_r = recv(sock, (char *) payload, MAX_PAYLOAD_SIZE, 0);
3532
3533 time1 = ntohl((*(uint32_t *) ptrTimeSec));
3534 time2 = RcvTime.tv_sec % 86400L;
3535 int net_TimeUsec = ntohl(*(uint32_t *) ptrTimeUsec);
3536 recvPkts++;
3537
3538
3539
3540 if (logging && size_r > 0) {
3541 if (logremoto == 0) {
3542
3543 writeInBufferStandard(&infos[count], *(uint32_t *) payload,
3544 *(uint32_t *) ptrSeqNum, HelpSrcAddress, HelpDstAddress,
3545 tmpPort_SrcPort, tmpPort_DstPort, time1, time2, net_TimeUsec,
3546 RcvTime.tv_usec, size_r);
3547
3548 } else {
3549
3550 writeInBufferStandard(&infos[count], *(uint32_t *) payload,
3551 *(uint32_t *) ptrSeqNum, HelpSrcAddress, HelpDstAddress,
3552 tmpPort_SrcPort, tmpPort_DstPort, time1, time2, net_TimeUsec,
3553 RcvTime.tv_usec, size_r);
3554 infosHostToNet(&infos[count]);
3555 }
3556 count++;
3557
3558
3559 if (count == logbuffer_size) {
3560 flushBuffer(infos, count);
3561 count = 0;
3562 }
3563 }
3564 } else {
3565
3566 flag = false;
3567 }
3568 }
3569 }
3570 }
3571
3572
3573 PRINTD(1,"flowSender: Finished sending packets of flow ID: %d\n", id);
3574
3575
3576 if ((logging) && (infos != NULL)) {
3577 flushBuffer(infos, count);
3578 count = 0;
3579 }
3580
3581 if (infos != NULL && flows[id].l4Proto != L4_PROTO_SCTP)
3582 free(infos);
3583
3584 #ifdef SCTP
3585 MUTEX_THREAD_LOCK(mutexSctp);
3586 sctpSessions[sctpId].busyStreams--;
3587 MUTEX_THREAD_UNLOCK(mutexSctp);
3588 if (l4Proto != L4_PROTO_SCTP || (l4Proto == L4_PROTO_SCTP && sctpSessions[sctpId].busyStreams == 0))
3589 #endif
3590 closeSock(sock);
3591
3592
3593 if (!multiFlows)
3594 free(flows[id].ptrPayloadBuffer);
3595
3596
3597 flows[id].dimPayloadBuffer = 0;
3598 flows[id].ptrPayloadBuffer = NULL;
3599
3600
3601
3602 msg.code = MSG_SM_ENDFLOW;
3603 if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0) {
3604 perror("flow Sender sending msg");
3605 exitThread();
3606 }
3607 return NULL;
3608 }
3609
3610
3611 void Terminate(int sig)
3612 {
3613
3614 if (logremoto) {
3615 signaling signaling_log;
3616 signaling_log.stop = true;
3617 sendto(logSockSignaling, (char *) &signaling_log, sizeof(signaling_log), 0,
3618 logHost->ai_addr, logHost->ai_addrlen);
3619 if (closeSock(logSock) < 0)
3620 printf("Error into close log sock\n");
3621 if (closeSock(logSockSignaling) < 0)
3622 printf("Error into close log sock signaling\n");
3623 } else if (logging) {
3624 out.close();
3625 }
3626
3627
3628
3629
3630 #ifdef UNIX
3631 memClean();
3632 exit(sig);
3633 #endif
3634 #ifdef WIN32
3635 WSACleanup();
3636 memClean();
3637 #endif
3638 }
3639
3640
3641 #ifdef UNIX
3642 # define EXEC_NAME " ./ITGSend"
3643 #endif
3644 #ifdef WIN32
3645 # define EXEC_NAME " ITGSend.exe"
3646 #endif
3647 void printHelp()
3648 {
3649 cout << "\nITGSend - Sender component of D-ITG platform\n";
3650
3651 cout << "\n Working modes\n\n"
3652
3653 " ITGSend can work in three different modes:\n\n"
3654
3655 " - Single-flow: reads from the command line the traffic flow to generate\n"
3656 " - Multi-flow: reads from a script file the traffic flows to generate\n"
3657 " - Daemon: runs as a daemon to be remotely controlled using the ITGapi\n";
3658
3659 cout << "\n Synopsis\n\n"
3660
3661 " * Single-flow mode *\n\n"
3662
3663 " "<<EXEC_NAME<<" [log_opts] [sig_opts] [flow_opts] [misc_opts]\n"
3664 " [ [idt_opts] [ps_opts] | [app_opts] ]\n\n"
3665
3666 " * Multi-flow mode *\n\n"
3667
3668 " "<<EXEC_NAME<<" <script_file> [log_opts]\n\n"
3669
3670 " where each line of the script file contains the single-flow mode options.\n\n"
3671
3672 " * Daemon mode *\n\n"
3673
3674 " "<<EXEC_NAME<<" -Q [log_opts]\n\n";
3675
3676 cout << " Options\n";
3677
3678 cout << "\n Log options (log_opts):\n\n"
3679
3680 " -l [logfile] Generate sender-side log file (default: " << DefaultLogFile << ").\n\n"
3681
3682 " -L [log_server_address] Generate sender-side log file on a remote ITGLog instance\n"
3683 " [logging_protocol] (default: <" << DEFAULT_LOG_IP << "> <" << invFindL4Proto(DEFAULT_PROTOCOL_TX_LOG) << ">).\n\n"
3684
3685 " -x [receiver_logfile] Ask ITGRecv to generate receiver-side log file (default: " << DefaultRecvLogFile << ").\n\n"
3686
3687 " -X [log_server_address] Ask ITGRecv to generate receiver-side log file on a remote ITGLog instance\n"
3688 " [logging_protocol] (default: <" << DEFAULT_LOG_IP << "> <" << invFindL4Proto(DEFAULT_PROTOCOL_TX_LOG) << ">).\n\n"
3689
3690 " -q <log_buffer_size> Number of packets to push to the log at once (default: " << logbuffer_size << ").\n\n";
3691
3692 cout << "\n Signaling options (sig_opts):\n\n"
3693
3694 " -Sda <signaling_dest_addr> Set the destination address for the signaling channel\n"
3695 " (default: equal to -a <dest_address>).\n\n"
3696
3697 " -Sdp <signaling_dest_port> Set the destination port for the signaling channel\n"
3698 " (default: " << DEFAULT_PORT_SIGNALING << ").\n\n"
3699
3700 " -Ssa <signaling_src_addr> Set the source address for the signaling channel\n"
3701 " (default: Set by O.S.).\n\n"
3702
3703 " -Ssp <signaling_src_port> Set the source port for the signaling channel\n"
3704 " (default: Set by O.S.).\n\n";
3705 #ifdef UNIX
3706 cout << " -Si <signaling_interface> Set the network interface for the signaling channel.\n\n";
3707 #endif
3708
3709 cout << "\n Flow options (flow_opts):\n\n"
3710
3711 " -H Enable NAT traversal: FTP-like passive mode\n"
3712 " (please, refer to the manual for further details).\n\n"
3713
3714 " -m <meter> Set the type of meter (default: owdm):\n"
3715 " - owdm (one-way delay meter)\n"
3716 " - rttm (round-trip time meter)\n\n"
3717
3718 " -t <duration> Set the generation duration in ms (default: " << DefaultDuration << " ms).\n\n"
3719
3720 " -z <#_of_packets> Set the number of packets to generate\n\n"
3721
3722 " -k <#_of_KBytes> Set the number of KBytes to generate\n\n"
3723
3724 " -d <delay> Start the generation after the specified delay in ms (default: " << DefaultDelay << " ms).\n\n"
3725
3726 " -b <DS_byte> Set the DS byte for QoS tests (default: " << DefaultDSByte << ").\n\n"
3727
3728 " -f <TTL byte> Set the IP Time To Live (default: 64).\n\n"
3729
3730 " -a <dest_address> Set the destination address (default: " << DefaultDestIP << ").\n\n"
3731
3732 " -sa <src_address> Set the source address (default: Set by O.S.).\n\n"
3733
3734 " -rp <dest_port> Set the destination port (default: " << DEFAULT_PORT << ").\n\n"
3735
3736 " -sp <src_port> Set the source port (default: Set by O.S.).\n\n";
3737 #ifdef UNIX
3738 cout << " -i <interface> Bind to the given interface (default: don't bind to any interface).\n\n";
3739 #endif
3740 cout << " -p <payload_metadata> Select the metadata sent in the payload of each packet (default: 2).\n"
3741 " (please, refer to the manual for further details).\n\n"
3742
3743 " -T <protocol> Layer 4 protocol (default: " << invFindL4Proto(L4_PROTO_UDP) << "):\n"
3744 " - UDP (User Datagram Protocol)\n"
3745 " - TCP (Transport Control Protocol)\n"
3746 " - ICMP [type] (Internet Control Messaging Protocol)\n";
3747 #ifdef SCTP
3748 cout << " - SCTP <association_id> (Session Control Transport Protocol)\n"
3749 " <max_streams>\n";
3750 #endif
3751 #ifdef DCCP
3752 cout << " - DCCP (Datagram Congestion Control Protocol)\n";
3753 #endif
3754 cout << "\n -D Disable TCP Nagle algorithm.\n\n";
3755
3756
3757 cout << "\n Inter-departure time options (idt_opts):\n\n"
3758
3759 " -C <rate> Constant (default: " << DefaultPktPerSec << " pkts/s).\n\n"
3760
3761 " -U <min_rate> Uniform distribution.\n"
3762 " <max_rate>\n\n"
3763
3764 " -E <mean_rate> Exponential distribution.\n\n"
3765
3766 " -N <mean> <std_dev> Normal distribution.\n\n"
3767
3768 " -O <mean> Poisson distribution.\n\n"
3769
3770 " -V <shape> <scale> Pareto distribution.\n\n"
3771
3772 " -Y <shape> <scale> Cauchy distribution.\n\n"
3773
3774 " -G <shape> <scale> Gamma distribution.\n\n"
3775
3776 " -W <shape> <scale> Weibull distribution.\n\n"
3777
3778 " -Ft <filename> Read IDTs from file.\n\n";
3779 #ifdef BURSTY
3780 cout << " -B <onDistro> <params> Generate bursty traffic:\n"
3781 " <offDistro> <params> - set the duration of both ON and OFF periods according to a\n"
3782 " supported random distribution (e.g. -B C 1000 C 1000).\n\n";
3783 #endif
3784
3785 cout << "\n Packet size options (ps_opts):\n\n"
3786
3787 " -c <pkt_size> Constant (default: " << DefaultPktSize << " bytes).\n\n"
3788
3789 " -u <min_pkt_size> Uniform distribution.\n"
3790 " <max_pkt_size>\n\n"
3791
3792 " -e <average_pkt_size> Exponential distribution.\n\n"
3793
3794 " -n <mean> <std_dev> Normal distribution.\n\n"
3795
3796 " -o <mean> Poisson distribution.\n\n"
3797
3798 " -v <shape> <scale> Pareto distribution.\n\n"
3799
3800 " -y <shape> <scale> Cauchy distribution.\n\n"
3801
3802 " -g <shape> <scale> Gamma distribution.\n\n"
3803
3804 " -w <shape> <scale> Weibull distribution.\n\n"
3805
3806 " -Fs <filename> Read payload sizes from file.\n\n";
3807
3808 cout << "\n Application layer options (app_opts): \n\n"
3809
3810 " -Fp <filename> Read payload content from file.\n\n"
3811
3812 " Telnet Emulate¹ Telnet traffic.\n\n"
3813
3814 " DNS Emulate¹ DNS traffic.\n\n"
3815
3816 " Quake3 Emulate¹ Quake 3 traffic.\n\n"
3817
3818 " CSa Emulate¹ Counterstrike traffic - active player.\n\n"
3819
3820 " CSi Emulate¹ Counterstrike traffic - idle player.\n\n"
3821
3822 " VoIP Emulate¹ Voice-over-IP traffic.\n"
3823 " -x <codec> VoIP sub-option: audio codec (default: G.711.1):\n"
3824 " - G.711.<1 or 2> (samples per pkt)\n"
3825 " - G.729.<2 or 3> (samples per pkt)\n"
3826 " - G.723.1\n"
3827 " -h <protocol> VoIP sub-option: audio transfer protocol (default: RTP).\n"
3828 " - RTP: Real Time Protocol (default)\n"
3829 " - CRTP: Real Time Protocol with header compression\n"
3830 " -VAD VoIP sub-option: enable voice activity detection\n\n"
3831
3832 " ¹ Emulation is obtained by properly replicating packet sizes and IDTs.\n\n";
3833
3834 cout << "\n Misc options (misc_opts):\n\n"
3835
3836 " -h | --help Display this help and exit.\n\n"
3837
3838 " -s <seed> Set the seed used for generating distributions (default: random).\n\n"
3839
3840 " -poll Use busy-wait loop for IDTs shorter than 1 msec.\n\n"
3841
3842 " -j <0|1> Guarantee the mean packet rate (default: " << mean_adjustment << "):\n"
3843 " - 0 (disable)\n"
3844 " - 1 (enable)\n\n"
3845
3846 " -sk <serial_iface> Raise a signal on the serial interface when sending packets.\n\n"
3847
3848 " -rk <serial_iface> Ask ITGRecv to raise a signal on the serial interface when receiving packets.\n\n";
3849 #ifdef WIN32
3850 cout << " -P Enable thread high priority.\n\n";
3851 #endif
3852
3853 cout << "\nFor more information please refer to the manual.\n";
3854
3855 exit(1);
3856 }
3857
3858 void ReportErrorAndExit(const char *function, const char *msg, char *pname, int fid)
3859 {
3860 fprintf(stderr, " ** ERROR ** Flow %d aborted: %s : %s \n ", fid, function, msg);
3861 fprintf(stderr, "Type %s -h for help \n", pname);
3862 fflush(stderr);
3863 memClean();
3864 exit(1);
3865 }
3866
3867
3868 #ifdef UNIX
3869
3870 void createTransportChan(int &signalSock, struct addrinfo* &DestHost,bool &dstAddrSpecify,struct addrinfo* &SrcHost,uint16_t SrcPort,const char * Interface)
3871 #else
3872
3873 void createTransportChan(unsigned int &signalSock, struct addrinfo* &DestHost,bool &dstAddrSpecify,struct addrinfo* &SrcHost,uint16_t SrcPort)
3874 #endif
3875 {
3876 struct addrinfo * TmpAddress = NULL;
3877 int reuse = 1;
3878 int newSock = 0;
3879 uint16_t port = 0;
3880
3881
3882
3883 #if defined UNIX && ! defined BSD
3884 if (Interface!=NULL){
3885 printf("\nBinding to device %s for signaling channel\n",Interface);
3886 if (setsockopt(signalSock, SOL_SOCKET, SO_BINDTODEVICE, Interface, strlen(Interface)) < 0) {
3887 perror("Cannot bind to device (hint: you must be root)");
3888 memClean();
3889 exit(1);
3890 }
3891 }
3892 #endif
3893
3894 if (passiveMode==false){
3895
3896
3897 signalSock = socket(DestHost->ai_family, SOCK_STREAM, 0);
3898
3899
3900 if(SrcHost!=NULL){
3901 SET_PORT(SrcHost,htons(SrcPort));
3902
3903 }else if (SrcPort != 0){
3904 #if (defined WIN32 && defined IPv6RECV) || defined UNIX || defined BSD
3905 if (DestHost->ai_family == PF_INET6){
3906 if (SrcHost) freeaddrinfo(SrcHost);
3907 getaddrinfo("::", NULL, &hint, &SrcHost);
3908 }else
3909 #endif
3910 {
3911 if (SrcHost) freeaddrinfo(SrcHost);
3912 getaddrinfo("0.0.0.0", NULL, &hint, &SrcHost);
3913 }
3914 SET_PORT(SrcHost,htons(SrcPort));
3915 }
3916
3917
3918 if((SrcHost!=NULL) || (SrcPort != 0)){
3919 if (bind(signalSock, SrcHost->ai_addr, SrcHost->ai_addrlen) != 0){
3920 perror("Bind error in createTransportChan()");
3921 memClean();
3922 exit(1);
3923 }
3924 }
3925
3926
3927 #ifdef UNIX
3928
3929 if (Interface!=NULL){
3930 int dontRoute = 1;
3931 if (setsockopt(signalSock, SOL_SOCKET, SO_DONTROUTE, &dontRoute, sizeof(int)) < 0) {
3932 perror("Cannot set don't route (hint: you must be root)");
3933 memClean();
3934 exit(1);
3935 }
3936 }
3937 #endif
3938
3939
3940 if ( connect(signalSock, DestHost->ai_addr, DestHost->ai_addrlen) < 0) {
3941 perror("Connect error in createTransportChan()");
3942 memClean();
3943 exit(1);
3944 }
3945
3946 }else{
3947
3948 if(SrcHost==NULL) {
3949 #if (defined WIN32 && defined IPv6RECV) || defined UNIX || defined BSD
3950
3951 if (SrcHost) freeaddrinfo(SrcHost);
3952 if(getaddrinfo("::", NULL, &hint,&SrcHost) < 0)
3953 #endif
3954 {
3955
3956 if(SrcHost) freeaddrinfo(SrcHost);
3957 if(getaddrinfo("0.0.0.0", NULL, &hint, &SrcHost) < 0){
3958 perror("getaddrinfo error in createTransportChan() (Passive Mode)");
3959 memClean();
3960 exit(1);
3961 }
3962 }
3963 }
3964
3965
3966 #if (defined WIN32 && defined IPv6RECV) || defined UNIX || defined BSD
3967 if (SrcHost->ai_family == PF_INET6){
3968 if (TmpAddress) freeaddrinfo(TmpAddress);
3969 getaddrinfo("::", NULL, &hint,&TmpAddress);
3970 PRINTD(1,"createTransportChan: bind to IPv6 socket\n");
3971 } else
3972 #endif
3973 {
3974 if (TmpAddress) freeaddrinfo(TmpAddress);
3975 getaddrinfo("0.0.0.0", NULL, &hint, &TmpAddress);
3976 PRINTD(1,"createTransportChan: bind to IPv4 socket\n");
3977 }
3978
3979
3980 if (SrcPort != 0){
3981 SET_PORT(SrcHost,htons(SrcPort));
3982 }else{
3983 SET_PORT(SrcHost,htons(DEFAULT_PORT_SIGNALING));
3984 }
3985
3986
3987 signalSock = socket(SrcHost->ai_family, SOCK_STREAM, 0);
3988
3989 if (setsockopt(signalSock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse))<0){
3990 perror("Setsockopt in createTransportChan() (Passive Mode)");
3991 memClean();
3992 exit(1);
3993 }
3994
3995
3996 if (bind(signalSock, SrcHost->ai_addr, SrcHost->ai_addrlen) != 0){
3997 perror("Bind error in createTransportChan() (Passive Mode)");
3998 memClean();
3999 exit(1);
4000 }
4001
4002
4003 if (listen(signalSock, SOMAXCONN) == 0){
4004 PRINTD(1,"createTransportChan: Listening\n");
4005 }else{
4006 perror("Listen in createTransportChan() (Passive Mode)");
4007 memClean();
4008 exit(1);
4009 }
4010
4011
4012 if ((newSock = accept(signalSock,TmpAddress->ai_addr, (socklen_t *) &(TmpAddress->ai_addrlen))) < 0){
4013 perror("Accept in createTransportChan() (Passive Mode)");
4014 memClean();
4015 exit(1);
4016 }
4017 PRINTD(1,"createTransportChan: New signaling connection accepted (Passive mode)\n");
4018
4019 closeSock(signalSock);
4020
4021 signalSock=newSock;
4022
4023
4024 if (dstAddrSpecify == false){
4025
4026 char *buffer=NULL;
4027
4028
4029 if (TmpAddress->ai_family==PF_INET){
4030 buffer=new char[INET_ADDRSTRLEN];
4031
4032 getnameinfo(TmpAddress->ai_addr,TmpAddress->ai_addrlen,buffer,INET_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
4033 }else if (TmpAddress->ai_family==PF_INET6){
4034
4035 buffer=new char[INET6_ADDRSTRLEN];
4036
4037 getnameinfo(TmpAddress->ai_addr,TmpAddress->ai_addrlen,buffer,INET6_ADDRSTRLEN,NULL,0,NI_NUMERICHOST);
4038 }
4039 PRINTD(1,"createTransportChan: take the address of the receiver: %s\n",buffer);
4040
4041
4042 GET_PORT(DestHost,port);
4043
4044 if (DestHost) freeaddrinfo(DestHost);
4045 getaddrinfo(buffer, NULL, &hint, &DestHost);
4046 SET_PORT(DestHost,port);
4047
4048 dstAddrSpecify = true;
4049
4050 delete buffer;
4051 if (TmpAddress) freeaddrinfo(TmpAddress);
4052 }
4053 }
4054
4055 }
4056
4057
4058 void sendType(int signalSock, BYTE type)
4059 {
4060 int err;
4061 char msg[1];
4062
4063 putValue(&msg, (void *) &type, sizeof(type));
4064
4065 #ifdef UNIX
4066 err = send(signalSock, (char *) &msg, sizeof(msg), MSG_DONTWAIT);
4067 if (err == EAGAIN) PRINTD(1,"sendType: Send type would block! **\n");
4068 #endif
4069
4070 #ifdef WIN32
4071 int timeo= 100000;
4072 if (setsockopt(signalSock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeo,sizeof(timeo)) < 0)
4073 printf("Error on setting timeout for sending 1\n");
4074 err = send(signalSock, (char *) &msg, sizeof(msg), 0);
4075 if(err<0)
4076 PRINTD(1,"sendType: Send type would block!\n");
4077 timeo=0;
4078 if (setsockopt(signalSock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeo,sizeof(timeo)) < 0)
4079 printf("Error on setting timeout for sending 2\n");
4080 #endif
4081
4082 }
4083
4084
4085 #ifdef UNIX
4086 void createSignalChan(int signalSock)
4087 #else
4088 void createSignalChan(unsigned int signalSock)
4089 #endif
4090 {
4091 timeval timeout;
4092 int numSend = 0;
4093 fd_set active_set;
4094 int exit = 0;
4095 do {
4096
4097 sendType(signalSock, TSP_CONNECT);
4098 PRINTD(1,"createSignalChan: sent TSP_CONNECT\n");
4099
4100
4101 FD_ZERO(&active_set);
4102 FD_SET(signalSock, &active_set);
4103 timeout.tv_sec = TIME_OUT;
4104 timeout.tv_usec = 0;
4105 if (select(FD_SETSIZE, &active_set, NULL, NULL, &timeout) < 0) {
4106 printf("Error ** : Invalid file descriptor\n");
4107 }
4108 if (FD_ISSET(signalSock, &active_set)) {
4109 recvAck(signalSock);
4110 exit = 1;
4111 }
4112 numSend++;
4113 } while ((exit != 1) && (numSend < 2));
4114
4115 if (numSend == 2){
4116 printf("** ERROR ** Receiver is down !\n");
4117 fflush(stdout);
4118 }
4119 }
4120
4121
4122 #ifdef UNIX
4123 void sendRelease(int signalSock)
4124 #else
4125 void sendRelease(unsigned int signalSock)
4126 #endif
4127 {
4128 timeval timeout;
4129 int numSend = 0;
4130 fd_set active_set;
4131 int exit = 0;
4132 do {
4133 sendType(signalSock, TSP_RELEASE);
4134 PRINTD(1,"sendRelease: sent TSP_RELEASE\n");
4135 FD_ZERO(&active_set);
4136 FD_SET(signalSock, &active_set);
4137 timeout.tv_sec = TIME_OUT;
4138 timeout.tv_usec = 0;
4139 if (select(FD_SETSIZE, &active_set, NULL, NULL, &timeout) < 0) {
4140 printf("Error ** : Invalid file descriptor\n");
4141 }
4142 if (FD_ISSET(signalSock, &active_set)) {
4143 recvAck(signalSock);
4144 PRINTD(1,"sendRelease: received ack to release signal channel\n");
4145 exit = 1;
4146 }
4147 numSend++;
4148 } while ((exit != 1) && (numSend < 2));
4149 if (numSend == 2) {
4150 printf("** ERROR ** Receiver is down !\n");
4151 fflush(stdout);
4152 }
4153 }
4154
4155
4156 int sendNameLog(int signalSock, char *FileName, int sizeFileName)
4157 {
4158 char Messaggio[DIM_LOG_FILE + sizeof(BYTE)];
4159 char *next;
4160
4161 PRINTD(1,"sendNameLog: sent TSP_SEND_NAME_LOG\n");
4162 BYTE type = TSP_SEND_NAME_LOG;
4163 int ret = 0;
4164 int sizeMessag;
4165 next = putValue(&Messaggio, (void *) &type, sizeof(type));
4166 next = putValue(next, (void *) FileName, sizeFileName);
4167 sizeMessag = sizeof(type) + sizeFileName;
4168 ret = send(signalSock, (char *) &Messaggio, sizeMessag, 0);
4169 return (ret);
4170 }
4171
4172
4173
4174
4175 int sendLog(int signalSock, struct addrinfo *logHost, BYTE logProtocol, char *FileName)
4176 {
4177 char Messaggio[DIM_LOG_FILE + 20];
4178 char *next;
4179 PRINTD(1,"sendLog: sent TSP_SEND_FLOW_LOG\n");
4180 BYTE type = TSP_SEND_FLOW_LOG;
4181 int ret, sizeMessag;
4182 int net_ai_family = htonl(logHost->ai_family);
4183 next = putValue(&Messaggio, (void *) &type, sizeof(type));
4184 next = putValue(next, &net_ai_family, sizeof(int));
4185 if (logHost->ai_family == PF_INET) {
4186 next =
4187 putValue(next,
4188 (void *) &(((struct sockaddr_in *) logHost->ai_addr)->sin_addr.s_addr),
4189 sizeof(in_addr_t));
4190 next =
4191 putValue(next, (void *) &(((struct sockaddr_in *) logHost->ai_addr)->sin_port),
4192 sizeof(in_port_t));
4193 } else if (logHost->ai_family == PF_INET6) {
4194 next =
4195 putValue(next,
4196 (void *) ( &((struct sockaddr_in6 *) logHost->ai_addr)->sin6_addr),
4197 4 * sizeof(in_addr_t));
4198 next =
4199 putValue(next,
4200 (void *) &((
4201 (struct sockaddr_in6 *) logHost->ai_addr)->sin6_port),
4202 sizeof(in_port_t));
4203
4204 }
4205 next = putValue(next, (void *) &logProtocol, sizeof(logProtocol));
4206 next = putValue(next, (void *) FileName, DIM_LOG_FILE);
4207
4208 sizeMessag = sizeof(type) + sizeof(int) + sizeof(in_port_t) + sizeof(BYTE) + DIM_LOG_FILE;
4209 if (logHost->ai_family == PF_INET)
4210 sizeMessag += sizeof(in_addr_t);
4211 else if (logHost->ai_family == PF_INET6)
4212 sizeMessag += 4 * sizeof(in_addr_t);
4213
4214 ret = send(signalSock, (char *) &Messaggio, sizeMessag, 0);
4215 return (ret);
4216 }
4217
4218
4219 void recvAck(int signalSock)
4220 {
4221 int i;
4222 char buffer;
4223 i = recv(signalSock, (char *) &buffer, sizeof(BYTE), 0);
4224 if (i < 0) {
4225 perror("recvAck: error while receiving ACK on the signaling channel");
4226 }
4227 }
4228
4229
4230 void closedFlowErr(int flowId, int signalSock)
4231 {
4232 char *next;
4233 PRINTD(1,"closedFlowErr: sent TSP_CLOSED_ERR\n");
4234 BYTE type = TSP_CLOSED_ERR;
4235 int sizeMessag = sizeof(int) + sizeof(type);
4236 char Messaggio[sizeMessag];
4237 int net_flowId = htonl(flowId);
4238
4239 next = putValue(&Messaggio, (void *) &type, sizeof(type));
4240 next = putValue(next, (void *) &net_flowId, sizeof(int));
4241 int size;
4242 size = send(signalSock, (char *) &Messaggio, sizeMessag, 0);
4243 if (size < 0) {
4244 printf("error into send\n");
4245 memClean();
4246 exit(1);
4247 }
4248 }
4249
4250
4251 int closedFlow(int flowId, int signalSock)
4252 {
4253 char *next;
4254 PRINTD(1,"closedFlow: sent TSP_CLOSED_FLOW\n");
4255 BYTE type = TSP_CLOSED_FLOW;
4256 int sizeMessag = sizeof(int) + sizeof(type);
4257 char Messaggio[sizeMessag];
4258 int net_flowId = htonl(flowId);
4259
4260 next = putValue(&Messaggio, (void *) &type, sizeof(type));
4261 next = putValue(next, (void *) &net_flowId, sizeof(int));
4262
4263 if (send(signalSock, (char *) &Messaggio, sizeMessag, 0) < 0) {
4264 printf("Error in request to close\n");
4265 fflush(stdout);
4266 return(-1);
4267 }
4268
4269 return (0);
4270 }
4271
4272
4273
4274 int sendAckFlow(int flowId, int signalSock)
4275 {
4276 char *next;
4277 PRINTD(1,"sendAckFlow: sent TSP_ACK_SEND_FLOW\n");
4278 BYTE type = TSP_ACK_SEND_FLOW;
4279 int sizeMessag = sizeof(int) + sizeof(type);
4280 char Messaggio[sizeMessag];
4281 int net_flowId = htonl(flowId);
4282
4283 next = putValue(&Messaggio, (void *) &type, sizeof(type));
4284 next = putValue(next, (void *) &net_flowId, sizeof(int));
4285
4286 if (send(signalSock, (char *) &Messaggio, sizeMessag, 0) < 0) {
4287 printf("Error in send ack flow\n");
4288 fflush(stdout);
4289 return(-1);
4290 }
4291
4292 return (0);
4293 }
4294
4295
4296
4297 int requestToSend(int flowId, int signalSock)
4298 {
4299 char Messaggio[100];
4300 char *next;
4301 BYTE type = TSP_SEND_FLOW;
4302 int sizeMessag = 0;
4303 uint16_t srcPort = 0;
4304 uint16_t netSrcPort = 0;
4305 uint16_t netDstPort = 0;
4306
4307 struct addrinfo *DestHost;
4308 if (passiveMode == false)
4309 DestHost = flows[flowId].DestHost;
4310 else{
4311 DestHost = flows[flowId].SrcHost;
4312 GET_PORT((flows[flowId].DestHost),srcPort);
4313 netSrcPort = htons(srcPort);
4314 }
4315
4316 BYTE l4Proto = flows[flowId].l4Proto;
4317 BYTE l7Proto = flows[flowId].l7Proto;
4318 BYTE meter = flows[flowId].meter;
4319 BYTE dsByte = flows[flowId].DSByte;
4320 BYTE l3Proto = (DestHost->ai_family == PF_INET) ? L3_PROTO_IPv4 : L3_PROTO_IPv6;
4321 BYTE srcAddrSpecify = (flows[flowId].srcAddrSpecify == true) ? 0xFF : 0x00;
4322 BYTE plType = flows[flowId].payloadLogType;
4323
4324
4325 uint32_t net_flowId = htonl(flowId);
4326
4327 PRINTD(1,"requestToSend: sent TSP_SEND_FLOW\n");
4328
4329
4330 next = putValue(&Messaggio, (void *) &type, sizeof(BYTE));
4331 next = putValue(next, (void *) &l4Proto, sizeof(BYTE));
4332 next = putValue(next, (void *) &l7Proto, sizeof(BYTE));
4333 next = putValue(next, (void *) &meter, sizeof(BYTE));
4334 next = putValue(next, (void *) &dsByte, sizeof(BYTE));
4335 next = putValue(next, (void *) &l3Proto, sizeof(BYTE));
4336 next = putValue(next, (void *) &net_flowId, sizeof(uint32_t));
4337 next = putValue(next, (void *) &plType, sizeof(BYTE));
4338 next = putValue(next, (void *) &flows[flowId].serialReceiver, DIM_NAME_SERIAL_INTERFACE);
4339 sizeMessag += 7 * sizeof(BYTE) + sizeof(uint32_t) + DIM_NAME_SERIAL_INTERFACE;
4340
4341
4342
4343
4344
4345 if (passiveMode == true){
4346 next = putValue(next, (void *) &(netSrcPort), sizeof(uint16_t));
4347 sizeMessag += sizeof(uint16_t);
4348
4349 next = putValue(next, (void *) &(srcAddrSpecify), sizeof(BYTE));
4350 sizeMessag += sizeof(BYTE);
4351 }
4352
4353 if (DestHost->ai_family == PF_INET) {
4354 in_addr_t DstAddr = ((struct sockaddr_in *) DestHost->ai_addr)->sin_addr.s_addr;
4355 netDstPort = htons(((struct sockaddr_in *) DestHost->ai_addr)->sin_port);
4356
4357
4358
4359 next = putValue(next, (void *) &netDstPort, sizeof(in_port_t));
4360
4361 next = putValue(next, (void *) &DstAddr, sizeof(in_addr_t));
4362
4363 sizeMessag += sizeof(in_port_t) + sizeof(in_addr_t);
4364 } else if (DestHost->ai_family == PF_INET6) {
4365 struct in6_addr *DstAddr = &((struct sockaddr_in6 *) DestHost->ai_addr)->sin6_addr;
4366 netDstPort = htons(((struct sockaddr_in6 *) DestHost->ai_addr)->sin6_port);
4367
4368
4369
4370 next = putValue(next, (void *) &netDstPort, sizeof(in_port_t));
4371
4372 next = putValue(next, (void *) DstAddr, 4 * sizeof(in_addr_t));
4373
4374
4375
4376
4377 sizeMessag += sizeof(in_port_t) + 4 * sizeof(in_addr_t);
4378 }
4379
4380 PRINTD(1,"requestToSend: new flow message prepared \n");
4381
4382 if (send(signalSock, (char *) &Messaggio, sizeMessag, 0) < 0) {
4383 printf("Error in request to send\n");
4384 fflush(stdout);
4385 return(-1);
4386 }
4387 PRINTD(1,"requestToSend: new flow message sent \n");
4388 return(0);
4389 }
4390
4391
4392 bool checkDestHostIP(int * chanId, struct addrinfo * DestHost)
4393 {
4394 int i = 0;
4395
4396 while (i < MAX_NUM_THREAD) {
4397
4398
4399 if ((signalChannels[i].socket != -1)
4400 && (ARE_INET_ADDR_EQUAL(DestHost, signalChannels[i].DestAddr))) {
4401 signalChannels[i].flows++;
4402 *chanId = i;
4403 return true;
4404 }
4405 i++;
4406 }
4407 return false;
4408 }
4409
4410 void argvToString(char *argv[], int argc, char line[200])
4411 {
4412 int i = 0;
4413
4414 strcpy(line, "^");
4415 while (argc > 0) {
4416 strcat(line, argv[i]);
4417 strcat(line, "^");
4418 argc--;
4419 i++;
4420 }
4421 strcat(line, "\n");
4422 }
4423
4424
4425 void flushBuffer(info *infos, int count)
4426 {
4427 if (logremoto) {
4428 MUTEX_THREAD_LOCK(mutexLogRem);
4429 if(sendto(logSock, (char *) infos, count * sizeof(struct info), 0, logHost->ai_addr,
4430 logHost->ai_addrlen) < 0) {
4431 printf("ERROR Sending infos to logger....\n");
4432 fflush(stdout);
4433 }
4434 MUTEX_THREAD_UNLOCK(mutexLogRem);
4435 } else {
4436 MUTEX_THREAD_LOCK(mutexLog);
4437 out.write((const char *) infos, count * sizeof(struct info));
4438 out.flush();
4439 MUTEX_THREAD_UNLOCK(mutexLog);
4440 }
4441 }
4442
4443
4444 void memClean()
4445 {
4446 #ifdef WIN32
4447
4448 ExitProcess(0);
4449 #endif
4450 }
4451
4452
4453
4454
4455 void createRemoteLogFile(struct addrinfo *logHost, BYTE protocolLog)
4456 {
4457
4458
4459
4460
4461
4462 uint32_t port;
4463
4464
4465 createSignalingLogChannel(protocolLog);
4466
4467
4468
4469 switch(protocolLog) {
4470 case L4_PROTO_UDP :
4471
4472 logSock = socket(logHost->ai_family, SOCK_DGRAM, 0);
4473 if (logSock < 0)
4474 ReportErrorAndExit("createRemoteLogFile","socket DATAGRAM - Cannot create socket logSock", programName, 0);
4475 break;
4476 case L4_PROTO_TCP :
4477
4478 logSock = socket(logHost->ai_family, SOCK_STREAM, 0);
4479 if (logSock < 0)
4480 ReportErrorAndExit("createRemoteLogFile","socket STREAM - Cannot create socket logSock", programName, 0);
4481 break;
4482 default :
4483 break;
4484 }
4485
4486 if ( recv(logSockSignaling, (char *) &port, sizeof(port), 0) < 0)
4487 ReportErrorAndExit("createRemoteLogFile","Cannot receive on signaling socket", programName, 0);
4488 port = ntohl(port);
4489 PRINTD(1,"createRemoteLogFile: Port received for log : %d\n",port);
4490
4491
4492
4493 SET_PORT(logHost, htons(port));
4494 sleep(1);
4495
4496 if (connect(logSock, logHost->ai_addr, logHost->ai_addrlen) < 0)
4497 ReportErrorAndExit("createRemoteLogFile","Error into connect with logsender!", programName, 0);
4498 }
4499
4500
4501
4502 void createSignalingLogChannel(BYTE protocolLog)
4503 {
4504
4505 signaling signal;
4506
4507 char buffer[1];
4508
4509
4510
4511 logSockSignaling = socket(logHost->ai_family, SOCK_STREAM, 0);
4512 if (logSockSignaling < 0)
4513 ReportErrorAndExit("createSignalingLogChannel","Cannot create socket logSockSignaling!", programName, 0);
4514
4515 SET_PORT(logHost, htons(DEFAULT_LOG_PORT_SIGNALING));
4516
4517 signal.protocol = protocolLog;
4518
4519 strcpy(signal.logFile, logFile);
4520
4521 if (connect(logSockSignaling, logHost->ai_addr, logHost->ai_addrlen) < 0)
4522 ReportErrorAndExit("createSignalingLogChannel","Error into connect !", programName, 0);
4523
4524 do {
4525 if (recv(logSockSignaling, (char *) &buffer, sizeof(BYTE), 0) < 0)
4526 ReportErrorAndExit("createSignalingLogChannel","Error into receive!", programName, 0);
4527
4528 } while (*(BYTE *) buffer != LOG_CONNECT);
4529 PRINTD(1,"createSignalingLogChannel: Received LOG_CONNECT Message\n");
4530
4531
4532 if (send(logSockSignaling, (char *) &signal, sizeof(signal), 0) < 0)
4533 ReportErrorAndExit("createSignalingLogChannel","Cannot send infos to LogServer !", programName, 0);
4534
4535 PRINTD(1,"createSignalingLogChannel: Sent to LogServer infos for creating signaling channel\n");
4536 }
4537