Debian Codegraph @

src/common/ITG.h

raw symbols SCIP
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 #include "../libITG/ITGapi.h"
31 #include "thread.h"
32 #include "debug.h"
33
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <fstream>
38 #include <iostream>
39 #include <sys/types.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <math.h>
43
44 using namespace std;
45
46 #ifdef WIN32
47 #include <winsock2.h>
48 #include <ws2tcpip.h>
49
50 #include <io.h>
51 #include <stdint.h>
52 typedef int pid_t;
53 typedef int socklen_t;
54 typedef int uid_t;
55 typedef u_long in_addr_t;
56 typedef u_short in_port_t;
57 typedef HANDLE pthread_t;
58 #define MSG_DONTWAIT 0x40
59 #ifndef IPV6_V6ONLY
60 # define IPV6_V6ONLY 27
61 #endif
62 #endif
63
64 #ifdef UNIX
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <sys/time.h>
68 #include <unistd.h>
69 #include <fcntl.h>
70 #include <netdb.h>
71 #include <sys/socket.h>
72 #include <sys/mman.h>
73 #include <sched.h>
74
75 #ifdef SCTP
76 #include <netinet/sctp.h>
77 typedef struct {
78 union {
79 unsigned int numStreams;
80 unsigned int port;
81 };
82 unsigned int parsedStreams;
83 unsigned int busyStreams;
84 int sock;
85 } sctpSession;
86 #endif
87
88 #ifdef DCCP
89 #define DCCP_SOCKOPT_PACKET_SIZE 1
90 #define DCCP_SOCKOPT_SERVICE 2
91 #ifndef SOCK_DCCP
92 #define SOCK_DCCP 6
93 #endif
94 #define TCP_CONGESTION 13
95 #define DCCP_SOCKOPT_CCID_RX_INFO 128
96 #define DCCP_SOCKOPT_CCID_TX_INFO 192
97 #define SOL_DCCP 269
98 #endif
99
100 typedef unsigned short USHORT;
101 typedef char UCHAR;
102 typedef unsigned char BYTE;
103 typedef long int ULONG;
104 #define INFINITE 60000
105 #define INVALID_HANDLE_VALUE -1
106 #ifdef BSD
107 #include <netinet/in_systm.h>
108 #define s6_addr __u6_addr.__u6_addr8
109 #define s6_addr8 __u6_addr.__u6_addr8
110 #define s6_addr16 __u6_addr.__u6_addr16
111 #define s6_addr32 __u6_addr.__u6_addr32
112 #endif
113 #endif
114
115 #if defined WIN32 || defined BSD
116 #define SOL_IP IPPROTO_IP
117 #define SOL_IPV6 IPPROTO_IPV6
118 #endif
119
120 #define MAX_L4_PORTS 65536
121
122 #define MAX(a,b) ((a) > (b) ? (a) : (b))
123
124 extern const char *meters[];
125 extern const char *l7Protocols[];
126 extern const char *l4Protocols[];
127 extern const int StandardMinPayloadSize;
128 extern const int ShortMinPayloadSize;
129 extern const int NoneMinPayloadSize;
130
131
132 #define LX_ERROR_BYTE 255
133
134 #define LX_PROTO_NONE 0
135
136
137 #define L3_PROTO_IPv4 4
138 #define L3_PROTO_IPv6 6
139
140
141 #define L4_PROTO_TCP 1
142 #define L4_PROTO_UDP 2
143 #define L4_PROTO_ICMP 3
144 #define L4_PROTO_SCTP 4
145 #define L4_PROTO_DCCP 5
146
147
148 #define L7_PROTO_TELNET 1
149 #define L7_PROTO_VOIP 2
150 #define L7_PROTO_DNS 3
151 #define L7_PROTO_AOM 4
152 #define L7_PROTO_CSATTIVA 5
153 #define L7_PROTO_CSINATTIVA 6
154 #define L7_PROTO_QUAKE 7
155
156
157 #define PL_STANDARD 0
158 #define PL_SHORT 1
159 #define PL_NONE 2
160
161
162
163
164
165 #define LOG_CONNECT 1
166
167
168 #define METER_OWDM 1
169 #define METER_RTTM 2
170
171
172
173
174 #define NO_TERMINATE 0
175
176 #define TERMINATE 1
177
178 #define ERROR_TERMINATE 2
179
180
181
182
183 #define SENDER 0
184
185 #define RECEIVER 1
186
187
188
189 #define TSP_CONNECT 1
190
191 #define TSP_ACK_CONNECT 2
192
193 #define TSP_SEND_FLOW 3
194
195 #define TSP_CLOSED_FLOW 4
196
197 #define TSP_ACK_SEND_FLOW 5
198
199 #define TSP_ACK_CLOSED_FLOW 6
200
201 #define TSP_ACK_RELEASE 7
202
203 #define TSP_DISCOVERY 8
204
205 #define TSP_ACK_DISCOVERY 9
206
207 #define TSP_CRYPTO_RSA 10
208
209 #define TSP_RELEASE 11
210
211 #define TSP_SEND_FLOW_LOG 12
212
213 #define TSP_ACK_SEND_FLOW_LOG 13
214
215 #define TSP_ERR_MSG_1 14
216
217 #define TSP_ERR_MSG_2 15
218
219 #define TSP_ERR_MSG_3 16
220
221 #define TSP_CLOSED_ERR 17
222
223 #define TSP_ACK_SEND_NAME_LOG 18
224
225 #define TSP_SEND_NAME_LOG 19
226
227 #define TSP_SENDER_DOWN 20
228
229 #define TSP_ERR_MSG_4 21
230
231 #define TSP_ERR_MSG_5 22
232
233
234
235
236
237
238 #ifdef WIN32
239
240 #define USER_ID() 1
241 #endif
242
243 #ifdef UNIX
244
245 #define USER_ID() getuid()
246 #endif
247
248
249 #define SET_PORT(host,port) \
250 if (host->ai_family == PF_INET) \
251 ((struct sockaddr_in*)(host->ai_addr))->sin_port=port; \
252 else if (host->ai_family == PF_INET6) \
253 ((struct sockaddr_in6*)(host->ai_addr))->sin6_port=port;
254
255 #define GET_PORT(host,port) \
256 if (host->ai_family == PF_INET) \
257 port=((struct sockaddr_in*)(host->ai_addr))->sin_port; \
258 else if (host->ai_family == PF_INET6) \
259 port=((struct sockaddr_in6*)(host->ai_addr))->sin6_port;
260
261
262 #ifdef WIN32
263 # ifndef IN6_ARE_ADDR_EQUAL
264 # define IN6_ARE_ADDR_EQUAL(a,b) IN6_ADDR_EQUAL(a,b)
265 # endif
266 # define ARE_INET_ADDR_EQUAL(a,b) \
267 ( (a->ai_family == b->ai_family) && \
268 ( a->ai_family == PF_INET ? \
269 ((struct sockaddr_in*)(a->ai_addr))->sin_addr.s_addr == ((struct sockaddr_in*)(b->ai_addr))->sin_addr.s_addr : \
270 IN6_ARE_ADDR_EQUAL(&(((struct sockaddr_in6*)(a->ai_addr))->sin6_addr),&(((struct sockaddr_in6*)(b->ai_addr))->sin6_addr)) ))
271 #endif
272
273 #ifdef UNIX
274 #ifdef BSD
275 #define ARE_INET_ADDR_EQUAL(a,b) \
276 ( (a->ai_family == b->ai_family) && \
277 ( a->ai_family == PF_INET ? \
278 ((struct sockaddr_in*)(a->ai_addr))->sin_addr.s_addr == ((struct sockaddr_in*)(b->ai_addr))->sin_addr.s_addr : \
279 IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6*)(a->ai_addr))->sin6_addr,&((struct sockaddr_in6*)(b->ai_addr))->sin6_addr) ))
280 #else
281 #define ARE_INET_ADDR_EQUAL(a,b) \
282 ( (a->ai_family == b->ai_family) && \
283 ( a->ai_family == PF_INET ? \
284 ((struct sockaddr_in*)(a->ai_addr))->sin_addr.s_addr == ((struct sockaddr_in*)(b->ai_addr))->sin_addr.s_addr : \
285 IN6_ARE_ADDR_EQUAL(((struct sockaddr_in6*)(a->ai_addr))->sin6_addr.s6_addr32,((struct sockaddr_in6*)(b->ai_addr))->sin6_addr.s6_addr32) ))
286 #endif
287 #endif
288
289 #ifdef UNIX
290
291 #define INET_NTOP(host,dst,cnt) \
292 if (host->ai_family == PF_INET) \
293 inet_ntop(host->ai_family,&(((struct sockaddr_in*)host->ai_addr)->sin_addr),dst,cnt); \
294 else if (host->ai_family == PF_INET6) \
295 inet_ntop(host->ai_family,&(((struct sockaddr_in6*)host->ai_addr)->sin6_addr),dst,cnt);
296 #endif
297
298
299
300 #define DEFAULT_PORT_SIGNALING 9000
301 #define DEFAULT_PORT 8999
302 #define DEFAULT_PORT_SENDER 8997
303 #define DEFAULT_LOG_PORT 9002
304 #define DEFAULT_LOG_PORT_SIGNALING 9001
305 #define DEFAULT_PORT_SENDER_MANAGER 8998
306
307 #define TIME_OUT 100
308 #define DIM_LOG_FILE 200
309 #define DIM_PAYLOAD_FILE 200
310 #define DIM_NAME_SERIAL_INTERFACE 10
311
312 extern uint16_t logbuffer_size;
313
314 #define MAX_PAYLOAD_SIZE 64000
315 #define MAX_NUM_THREAD 400
316 #define MAX_FLOW_LINE_SIZE 1460
317 #define MAX_MSG_SIZE 100
318 #define DEFAULT_PROTOCOL_TX_LOG L4_PROTO_UDP
319 #define DEFAULT_PROTOCOL_TX_LOG_OPZ L4_PROTO_UDP
320
321 #define DEFAULT_DEST_IP "127.0.0.1"
322
323 extern char programName[100];
324 extern char DEFAULT_LOG_IP[10];
325
326
327
328 struct signaling {
329 BYTE protocol;
330 bool stop;
331 char pad_1;
332 char pad_2;
333 char logFile[DIM_LOG_FILE];
334 };
335
336
337
338 struct info {
339 uint32_t flowId;
340 uint32_t seqNum;
341 uint32_t srcPort;
342 uint32_t destPort;
343 uint32_t txTime1;
344 uint32_t txTime2;
345 uint32_t rxTime1;
346 uint32_t rxTime2;
347 uint32_t size;
348 char srcAddr[INET6_ADDRSTRLEN];
349 char destAddr[INET6_ADDRSTRLEN];
350 double txTime3;
351 double rxTime3;
352 };
353
354
355 struct icmpv6 {
356 BYTE icmp_type;
357 BYTE icmp_code;
358 USHORT icmp_cksum;
359 };
360
361
362
363 struct icmp {
364 BYTE icmp_type;
365 BYTE icmp_code;
366 USHORT icmp_cksum;
367 USHORT icmp_id;
368 USHORT icmp_seq;
369
370 };
371
372 #ifdef BSD
373 struct iphdr {
374 unsigned char ihl:4;
375 unsigned char version:4;
376 unsigned char tos;
377 unsigned short tot_len;
378 unsigned short id;
379 unsigned short frag_off;
380 unsigned char ttl;
381 unsigned char protocol;
382 unsigned short check;
383 unsigned int saddr;
384 unsigned int daddr;
385 };
386 #endif
387
388 #ifdef WIN32
389
390 struct iphdr {
391 unsigned char ihl:4;
392 unsigned char version:4;
393 unsigned char tos;
394 unsigned short tot_len;
395 unsigned short id;
396 unsigned short frag_off;
397 unsigned char ttl;
398 unsigned char protocol;
399 unsigned short check;
400 unsigned int saddr;
401 unsigned int daddr;
402 };
403
404 int InitializeWinsock(WORD wVersionRequested);
405 void sleep(int tempo);
406 #endif
407
408 char *allowedLogFile(char logFile[DIM_LOG_FILE]);
409
410 void closeFileLog(ofstream * out);
411
412
413 USHORT checksum(USHORT * buffer, int size);
414
415
416 char *putValue(void* startPos,void* data,int size);
417
418 BYTE findMeter(char *s);
419 BYTE findL4Proto(char *s);
420 BYTE findL7Proto(char *s);
421
422 const char* invFindMeter(BYTE meter);
423 const char* invFindL4Proto(BYTE proto);
424 const char* invFindL7Proto(BYTE proto);
425
426
427
428
429 inline void getInfo(struct addrinfo *Host,int &tmpPort, char *HelpAddress)
430 {
431
432 GET_PORT(Host, tmpPort);
433
434
435 getnameinfo(Host->ai_addr, Host->ai_addrlen, HelpAddress, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
436 }
437
438
439
440 inline int writeInBufferStandard(info * infos,int FlowId, int ptrSeqNum, char *SrcHost,
441 char *DestHost, in_port_t tmp_portSrc, in_port_t tmp_portDest ,long time1, long time2,
442 long txTime_Usec,long rxTime_Usec, int size)
443 {
444
445
446 infos->flowId = ntohl(FlowId);
447
448
449 infos->seqNum = ntohl(ptrSeqNum);
450
451
452 strcpy(infos->srcAddr, SrcHost);
453
454
455 infos->srcPort = ntohs(tmp_portSrc);
456
457
458 strcpy(infos->destAddr, DestHost);
459
460
461 infos->destPort = ntohs(tmp_portDest);
462
463
464 infos->txTime1 = time1 / 3600;
465 infos->txTime2 = (time1 % 3600) / 60;
466 infos->txTime3 = time1 % 60 + txTime_Usec / 1000000.0;
467
468
469 infos->rxTime1 = time2 / 3600;
470 infos->rxTime2 = (time2 % 3600) / 60;
471 infos->rxTime3 = time2 % 60 + rxTime_Usec / 1000000.0;
472 infos->size = size;
473
474 PRINTD(2,"\nwriteInBufferStandard: flowId : %d\n",infos->flowId);
475 PRINTD(2,"\tSequence Number : %d\n",infos->seqNum);
476 PRINTD(2,"\tSrc : %16s/%hu ",infos->srcAddr,infos->srcPort);
477 PRINTD(2,"Dest : %16s/%hu ",infos->destAddr,infos->destPort);
478 PRINTD(2,"TxTime : %02u:%02u:%06lf ",(unsigned int)infos->txTime1,(unsigned int)infos->txTime2,infos->txTime3);
479 PRINTD(2,"RxTime : %02u:%02u:%06lf ",(unsigned int)infos->rxTime1,(unsigned int)infos->rxTime2,infos->rxTime3);
480 PRINTD(2,"Size : %5u\n\n",infos->size);
481 return 0;
482 }
483
484 inline int writeInBufferShort(info * infos,int FlowId, int ptrSeqNum, char *SrcHost,
485 char *DestHost, in_port_t tmp_portSrc, in_port_t tmp_portDest , long time2,
486 long rxTime_Usec, int size)
487 {
488
489 infos->flowId = ntohl(FlowId);
490
491
492 infos->seqNum = ntohl(ptrSeqNum);
493
494 strcpy(infos->srcAddr, SrcHost);
495
496
497 infos->srcPort = ntohs(tmp_portSrc);
498
499
500 strcpy(infos->destAddr, DestHost);
501
502
503 infos->destPort = ntohs(tmp_portDest);
504
505 infos->rxTime1 = time2 / 3600;
506 infos->rxTime2 = (time2 % 3600) / 60;
507 infos->rxTime3 = time2 % 60 + rxTime_Usec / 1000000.0;
508
509
510 infos->txTime1 = time2 / 3600;
511 infos->txTime2 = (time2 % 3600) / 60;
512 infos->txTime3 = time2 % 60 + rxTime_Usec / 1000000.0;
513
514 infos->size = size;
515
516 PRINTD(2,"\nwriteInBufferShort: flowId : %d\n",infos->flowId);
517 PRINTD(2,"\tSequence Number : %d\n",infos->seqNum);
518 PRINTD(2,"\tSrc : %16s/%hu ",infos->srcAddr,infos->srcPort);
519 PRINTD(2,"Dest : %16s/%hu ",infos->destAddr,infos->destPort);
520 PRINTD(2,"TxTime : %u:%u:%lf ",(unsigned int)infos->txTime1,(unsigned int)infos->txTime2,infos->txTime3);
521 PRINTD(2,"RxTime : %u:%u:%lf ",(unsigned int)infos->rxTime1,(unsigned int)infos->rxTime2,infos->rxTime3);
522 PRINTD(2,"Size : %5u\n\n",infos->size);
523 return 0;
524 }
525
526 inline int writeInBufferNone(info * infos,int FlowId, char *SrcHost,
527 char *DestHost, in_port_t tmp_portSrc, in_port_t tmp_portDest , long time2,
528 long rxTime_Usec, int size)
529 {
530
531
532 infos->flowId = (unsigned int)FlowId;
533
534
535 infos->seqNum = 0;
536
537 strcpy(infos->srcAddr, SrcHost);
538
539
540 infos->srcPort = ntohs(tmp_portSrc);
541
542
543 strcpy(infos->destAddr, DestHost);
544
545
546 infos->destPort = ntohs(tmp_portDest);
547
548 infos->rxTime1 = time2 / 3600;
549 infos->rxTime2 = (time2 % 3600) / 60;
550 infos->rxTime3 = time2 % 60 + rxTime_Usec / 1000000.0;
551
552
553 infos->txTime1 = time2 / 3600;
554 infos->txTime2 = (time2 % 3600) / 60;
555 infos->txTime3 = time2 % 60 + rxTime_Usec / 1000000.0;
556
557 infos->size = size;
558
559 PRINTD(2,"\nwriteInBufferNone: flowId : %d\n",infos->flowId);
560 PRINTD(2,"\tSequence Number : %d\n",infos->seqNum);
561 PRINTD(2,"\tSrc : %16s/%hu ",infos->srcAddr,infos->srcPort);
562 PRINTD(2,"Dest : %16s/%hu ",infos->destAddr,infos->destPort);
563 PRINTD(2,"TxTime : %02u:%02u:%06lf ",(unsigned int)infos->txTime1,(unsigned int)infos->txTime2,infos->txTime3);
564 PRINTD(2,"RxTime : %02u:%02u:%06lf ",(unsigned int)infos->rxTime1,(unsigned int)infos->rxTime2,infos->rxTime3);
565 PRINTD(2,"Size : %5u\n\n",infos->size);
566 return 0;
567 }
568
569 inline void infosHostToNet(struct info *infos){
570
571
572 infos->flowId = htonl(infos->flowId);
573 infos->seqNum = htonl(infos->seqNum);
574 infos->srcPort = htonl(infos->srcPort);
575 infos->destPort = htonl(infos->destPort);
576 infos->size = htonl(infos->size);
577
578 PRINTD(3,"infosHostToNet: Host Time (h:m:s): %02u:%02u:%06lf \n",(unsigned int)infos->rxTime1,(unsigned int)infos->rxTime2,infos->rxTime3);
579
580 infos->rxTime1 = htonl(infos->rxTime1 * 3600 + infos->rxTime2 * 60 + (unsigned int)floor(infos->rxTime3));
581 infos->rxTime2 = htonl((uint32_t)((infos->rxTime3 - (unsigned int)floor(infos->rxTime3)) * 1000000));
582 infos->rxTime3 = 0;
583
584 PRINTD(3,"infosHostToNet: Net Time (s:us): %lu:%06lu \n",(unsigned long)ntohl(infos->rxTime1),(unsigned long)ntohl(infos->rxTime2));
585
586 infos->txTime1 = htonl(infos->txTime1 * 3600 + infos->txTime2 * 60 + (unsigned int)floor(infos->txTime3));
587 infos->txTime2 = htonl((uint32_t)((infos->txTime3 - (unsigned int)floor(infos->txTime3)) * 1000000));
588 infos->txTime3 = 0;
589
590 }
591
592
593 inline int TCPrecvPacket(unsigned char *payload,int newSock, int preambleSize, int payloadLogType)
594 {
595
596
597
598 unsigned char *ptrToSizeInPayload = NULL;
599
600 if (payloadLogType == PL_STANDARD)
601 ptrToSizeInPayload = payload + 4 * sizeof(uint32_t);
602 else if (payloadLogType == PL_SHORT)
603 ptrToSizeInPayload = payload + 2 * sizeof(uint32_t);
604
605
606
607
608 int sizePreamble = preambleSize;
609
610
611 unsigned char buffer[MAX_PAYLOAD_SIZE];
612
613
614
615
616
617
618 int size = 0, size_p = 0, size_r = 0;
619
620 size_r = recv(newSock, (char *) payload, sizePreamble, 0);
621
622
623 if (size_r <= 0){
624 return size_r;
625 }
626
627 PRINTDS(2,"TCPrecvPacket: Received TCP Preamble first time :%d \n",size_r);
628
629
630 #if DEBUG > 3
631 printf("TCPrecvPacket: Preamble data (1): \n");
632 for(int z=0;z<size_r;z++)
633 printf("%.2X ",(int)payload[z]);
634 printf("\n");
635 fflush(stdout);
636 #endif
637
638
639
640 if (size_r < sizePreamble) {
641
642 int size_l = 0;
643
644 int size_prec = size_r;
645
646 unsigned char *ptr_payload;
647
648 ptr_payload = payload + size_prec;
649
650
651 do {
652
653 size_l = recv(newSock, (char *) buffer, (sizePreamble - size_prec), 0);
654 if (size_l <= 0){
655 return size_l;
656 }
657
658 PRINTD(2,"TCPrecvPacket: Received Other data of TCP Preamble %d\n",size_l);
659
660
661 #if DEBUG > 3
662 printf("TCPrecvPacket: Preamble data (2): \n");
663 for(int w=0;w<size_l;w++)
664 printf("%.2X ",(int)buffer[w]);
665 printf("\n");
666 fflush(stdout);
667 #endif
668
669
670
671 size_prec = size_l + size_prec;
672
673 memcpy(ptr_payload, buffer, size_l);
674
675 ptr_payload = ptr_payload + size_l;
676 } while ((sizePreamble - size_prec) > 0);
677 }
678
679
680 #if DEBUG > 3
681 printf("TCPrecvPacket: First %d byte of TCP payload (preamble included): \n",sizePreamble);
682 for(int y=0;y<sizePreamble;y++)
683 printf("%.2X ",(int)payload[y]);
684 printf("\n");
685 fflush(stdout);
686 #endif
687
688
689
690
691
692 if (payloadLogType != PL_NONE) {
693
694
695 size = ntohl(*(uint32_t *) ptrToSizeInPayload);
696 PRINTD(1,"Expected TCP packet size %d \n",size);
697
698 if (size > sizePreamble) {
699 size_p = recv(newSock, (char *) buffer, (size - sizePreamble), 0);
700 }
701 } else {
702 size_p = recv(newSock, (char *) buffer, MAX_PAYLOAD_SIZE, 0);
703 size = size_p;
704 }
705 if (size_p <= 0) {
706 return size_p;
707 }
708 PRINTD(2,"TCPrecvPacket: Received TCP Payload :%d \n",size_p);
709
710
711 if (size_p < (size - sizePreamble)) {
712
713 int size_l = 0;
714
715 int size_prec = size_p;
716
717 while (size_prec < (size - sizePreamble)) {
718
719 size_l = recv(newSock, (char *) buffer, ((size - sizePreamble) - size_prec), 0);
720
721 PRINTD(2,"TCPrecvPacket: Received payload for other times: %d \n",size_l);
722
723 if (size_l <= 0) {
724 return size_l;
725 }
726
727 PRINTD(2,"TCPrecvPacket: Received Other data of TCP Payload\n");
728
729 size_prec = size_l + size_prec;
730 }
731 }
732 return size;
733 }
734
735
736 #ifdef SCTP
737
738 inline int SCTPrecvPacket(unsigned char *payload, int newSock, unsigned int stream, int preambleSize, int payloadLogType)
739 {
740
741
742
743 PRINTD(2,"SCTPrecvPacket: Enter function SCTPrecvPacket \n");
744
745 unsigned char *ptrToSizeInPayload = NULL;
746
747 if (payloadLogType == PL_STANDARD)
748 ptrToSizeInPayload = payload + 4 * sizeof(uint32_t);
749 else if (payloadLogType == PL_SHORT)
750 ptrToSizeInPayload = payload + 2 * sizeof(uint32_t);
751
752
753
754 int sizePreamble = preambleSize;
755 PRINTD(2,"SCTPrecvPacket: sizePreamble: %d\n", sizePreamble);
756
757
758 unsigned char buffer[MAX_PAYLOAD_SIZE];
759
760
761
762
763
764
765 int size = 0, size_p = 0, size_r = 0;
766
767
768
769
770
771 size_r = recv(newSock, (char *) payload, sizePreamble, 0);
772
773
774
775 PRINTD(2,"SCTPrecvPacket: Received SCTP Preamble first time :%d \n",size_r);
776 if (size_r < 0)
777 perror("SCTPRecvPacket function");
778
779
780 if (size_r < 0)
781 return 1;
782
783
784 if (size_r < sizePreamble) {
785
786 int size_l = 0;
787
788 int size_prec = size_r;
789
790 unsigned char *ptr_payload;
791
792 ptr_payload = payload + size_prec;
793
794 unsigned char *ptr;
795
796 ptr = buffer;
797
798
799 while ((sizePreamble - size_prec) > 0) {
800
801
802
803 size_l= recv(newSock, (char *) payload, sizePreamble, 0);
804
805
806 if (size_l < 0)
807 return 1;
808 PRINTDS(2,"SCTPrecvPacket: Received Other data of SCTP Preamble %d\n",size_l);
809
810
811 size_prec = size_l + size_prec;
812
813 memcpy(ptr_payload, ptr, size_l);
814
815 ptr = ptr + size_l;
816 ptr_payload = ptr_payload + size_l;
817 }
818 }
819
820
821
822 if (payloadLogType != PL_NONE) {
823
824
825 size = ntohl(*(uint32_t *) ptrToSizeInPayload);
826
827 PRINTD(1,"Expected TCP packet size %d \n",size);
828
829
830 if (size > sizePreamble) {
831 size_p = recv(newSock, (char *) buffer, (size - sizePreamble), 0);
832 }
833 } else {
834 size_p = recv(newSock, (char *) buffer, MAX_PAYLOAD_SIZE, 0);
835 size = size_p;
836 }
837 PRINTD(2,"SCTP Pkt Receiver : Received SCTP Payload :%d \n",size_p);
838
839 if (size_p < 0)
840 return 2;
841
842
843 if (size_p < (size - sizePreamble)) {
844
845 int size_l = 0;
846
847 int size_prec = size_p;
848
849 while (size_prec < (size - sizePreamble)) {
850
851
852
853
854 size_l=recv(newSock,(char *) buffer,(size-sizePreamble),0);
855
856
857 PRINTDS(2,"SCTPrecvPacket: Received payload for other times: %d \n",size_l);
858 if (size_l < 0)
859 return 2;
860 PRINTDS(2,"SCTPrecvPacket: Received Other data of SCTP Payload\n");
861
862 size_prec = size_l + size_prec;
863 }
864 }
865
866 PRINTD(2,"SCTPrecvPacket: Exit function SCTPrecvPacket \n");
867 return size;
868 }
869 #endif
870
871 #ifdef DCCP
872
873 #endif
874
875
876 #ifdef WIN32
877 #define restrict
878 char *strtok_r (char *restrict string, const char *restrict delim, char **saveptr);
879 #endif
880
881
882
883 #define MAX_POLL 1
884
885
886 inline int ms_sleep(struct timeval wait)
887 {
888 #if defined WIN32
889 fd_set dummy;
890
891 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
892 FD_ZERO(&dummy);
893 FD_SET(s, &dummy);
894 select(0, 0, 0, &dummy, &wait);
895 closesocket(s);
896 #elif defined UNIX
897 select(0, NULL, NULL, NULL, &wait);
898 #endif
899 PRINTD(2,"ms_sleep: WakeUP: %lu.%06lu\n", wait.tv_sec, (unsigned long int)wait.tv_usec);
900
901 return 0;
902 }
903
904 void printVersion(const char* ProgramName);