This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmod_inbounds.c
1597 lines (1367 loc) · 43.5 KB
/
mod_inbounds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1994-2004
* Ohio University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code
* distributions retain the above copyright notice and this paragraph
* in its entirety, (2) distributions including binary code include
* the above copyright notice and this paragraph in its entirety in
* the documentation or other materials provided with the
* distribution, and (3) all advertising materials mentioning features
* or use of this software display the following acknowledgment:
* ``This product includes software developed by the Ohio University
* Internetworking Research Laboratory.'' Neither the name of the
* University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Author: Shawn Ostermann
* School of Electrical Engineering and Computer Science
* Ohio University
* Athens, OH
*
* Manikantan Ramadas
*/
#ifdef LOAD_MODULE_INBOUNDS
#include "tcptrace.h"
#include <fcntl.h>
#include <limits.h>
#include "mod_inbounds.h"
static int inc_cnt=0;
static int dointer_cnt=0;
static int udp_delconn_cnt=0;
// info kept for tcp packets:
struct inbounds_tcp_conn_info
{
timeval first_time; // time of the connection's first packet
timeval first_data_time; // time of the connection's first data packet
timeval last_time; // time of the connection's last packet
timeval last_data_time; // time of the connection's last data packet
Bool had_data;
Bool closed; // has the connection been closed ?
Bool new; // is the connection new ?
tcp_pair_addrblock addr_pair;
tcp_pair *ptp;
u_long a_pkt; /* number of packets from a to b within
the time interval */
u_long b_pkt;
u_long a_byte; /* number of bytes from a to b within
the time interval */
u_long b_byte;
u_int qNum;
u_int aNum;
u_long qSum;
u_long aSum;
timeval q2aIdle;
timeval a2qIdle;
Bool dir; /* 0 - question, 1 - answer */
u_long burst_bytes;
/* for determining bursts */
tcb *tcb_lastdata;
struct inbounds_tcp_conn_info *prev; /* pointer to the next connection */
struct inbounds_tcp_conn_info *next; /* pointer to the next connection */
};
typedef struct inbounds_tcp_conn_info itcinfo;
/* structure for udp connections */
struct inbounds_udp_conn_info
{
timeval first_time; // time of the connection's first packet
timeval last_time; // time of the connection's last packet
Bool closed; // has the connection been closed ?
Bool new; // is the connection new ?
udp_pair_addrblock addr_pair;
udp_pair *pup;
u_long a_pkt; /* number of packets from a to b within
the time interval */
u_long b_pkt;
u_long a_byte; /* number of bytes from a to b within
the time interval */
u_long b_byte;
u_int qNum;
u_int aNum;
u_long qSum;
u_long aSum;
timeval q2aIdle;
timeval a2qIdle;
Bool dir; /* 0 - question, 1 - answer */
struct inbounds_udp_conn_info *prev; /* pointer to the next connection */
struct inbounds_udp_conn_info *next; /* pointer to the next connection */
};
typedef struct inbounds_udp_conn_info iucinfo;
struct inbounds_info
{
// times of the last network statistics as it should appear in ideally
// for TCP and UDP:
timeval last_tcp_scheduled_time;
timeval last_udp_scheduled_time;
// times when the last network stats actually happened for TCP and UDP:
timeval last_tcp_actual_time;
timeval last_udp_actual_time;
itcinfo *tcp_conn_head; /* head of the list of tcp connections */
itcinfo *tcp_conn_tail; /* tail of the list of tcp connections */
u_short tcp_new_conn; /* number of new connections within the
time interval */
u_short tcp_total_conn; // number of currect active connections
/* this info is for UDP conn */
iucinfo *udp_conn_head; /* head of the list of udp connections */
iucinfo *udp_conn_tail; /* tail of the list of udp connections */
u_short udp_new_conn; /* number of new connections within the
time interval */
u_short udp_total_conn; /* number of currect udp active connections */
};
typedef struct inbounds_info iinfo;
struct protocol
{
u_char ip_p;
u_llong count;
struct protocol *next;
};
#define INBOUNDS_TCP_UPDATE_INTERVAL 60
#define INBOUNDS_UDP_UPDATE_INTERVAL 60
#define INBOUNDS_DEBUG 0 /* debug flag */
#define TCB_CACHE_A2B 0
#define TCB_CACHE_B2A 1
#define UDPHDR_LEN 8
#define UDP_A2B 0
#define UDP_B2A 1
/* global variables */
static iinfo *mod_info;
static u_llong tcp_packets = 0;
static u_llong udp_packets = 0;
static u_llong nontcpudp_packets = 0;
static struct protocol *plist = NULL;
/* local routines */
static void AllTCPInteractivity(void);
static void TCPInteractivity(itcinfo *conn);
static void AllUDPInteractivity(void);
static void UDPInteractivity(iucinfo *conn);
static void PrintUDPCMsg(iucinfo *);
static void ClosedUDPConn();
static Bool IsNewBurst(itcinfo *conn, tcb *ptcb, struct tcphdr *tcp, Bool dir);
static void ipCheck(struct ip *pip, void *plast);
static void tcpCheck(struct ip *pip, tcp_pair *ptcp, void *plast);
static void udpCheck(struct ip *pip, udp_pair *pup, void *plast);
static itcinfo *Makeitcinfo(void);
static iucinfo *Makeiucinfo(void);
static void Freeitcinfo(itcinfo *);
static void Freeiucinfo(iucinfo *);
/* declarations of memory management functions for the module */
static long itcinfo_pool = -1;
static long iucinfo_pool = -1;
/* tcp packet */
static itcinfo *
Makeitcinfo(
void)
{
itcinfo *ptr = NULL;
if (itcinfo_pool < 0) {
itcinfo_pool = MakeMemPool(sizeof(itcinfo), 0);
}
ptr = PoolMalloc(itcinfo_pool, sizeof(itcinfo));
return ptr;
}
/* udp packet */
static iucinfo *
Makeiucinfo(
void)
{
iucinfo *ptr = NULL;
if (iucinfo_pool < 0) {
iucinfo_pool = MakeMemPool(sizeof(iucinfo), 0);
}
ptr = PoolMalloc(iucinfo_pool, sizeof(iucinfo));
return ptr;
}
static void
Freeitcinfo(
itcinfo *ptr)
{
PoolFree(itcinfo_pool, ptr);
}
static void
Freeiucinfo(
iucinfo *ptr)
{
PoolFree(iucinfo_pool, ptr);
}
/* Usage message for using the INBOUNDS module */
void
inbounds_usage(void)
{
printf("Use -xinbounds to call INBOUNDS and add -u for UDP conn. analysis\
\n");
}
int
inbounds_init(
int argc,
char *argv[])
{
int i, fd;
int enable=0;
/* look for "-xinbounds" */
for (i=1; i < argc; ++i) {
if (!argv[i])
continue; /* argument already taken by another module... */
if (strncmp(argv[i],"-x",2) == 0) {
if (strncasecmp(argv[i]+2,"inbounds", 8) == 0) {
/* I want to be called */
enable = 1;
// We *are* running the program in real-time mode
run_continuously=TRUE;
if(INBOUNDS_DEBUG)
fprintf(stderr, "mod_inbounds: Capturing traffic\n");
argv[i] = NULL;
}
}
}
if (!enable)
return(0); /* don't call me again */
mod_info = (iinfo *)malloc(sizeof(iinfo));
mod_info->last_tcp_scheduled_time = current_time;
mod_info->last_tcp_actual_time = current_time;
mod_info->last_udp_scheduled_time = current_time;
mod_info->last_udp_actual_time = current_time;
mod_info->tcp_conn_head = NULL;
mod_info->tcp_conn_tail = NULL;
mod_info->tcp_new_conn = 0;
mod_info->tcp_total_conn = 0;
mod_info->udp_conn_head = NULL;
mod_info->udp_conn_tail = NULL;
mod_info->udp_new_conn = 0;
mod_info->udp_total_conn = 0;
resolve_ipaddresses = FALSE;
resolve_ports = FALSE;
return(1); /* TRUE means call other inbounds routines later */
}
void
inbounds_done(void)
{
struct protocol *pp;
// When we are simulating attack, i.e feed just the attack to this module
// un-domment the following section to wash out the attack at the end
// to produce 'U' and 'C' messages.
if(do_udp) {
// iucinfo *udp_conn;
ClosedUDPConn();
/* for (udp_conn=mod_info->udp_conn_head; udp_conn!=NULL;
udp_conn=udp_conn->next) {
if(!udp_conn->closed) {
// Assume that its been UDP_REMOVE_LIVE_CONN_INTERVAL
// since we had the last message on this connection
current_time.tv_sec=udp_conn->last_time.tv_sec+
UDP_REMOVE_LIVE_CONN_INTERVAL;
UDPInteractivity(udp_conn);
udp_conn->closed=TRUE;
PrintUDPCMsg(udp_conn);
}
}*/
}
#ifdef HAVE_LONG_LONG
fprintf(stderr, "\nINBOUNDS: TCP packets - %llu\n", tcp_packets);
fprintf(stderr, "INBOUNDS: UDP packets - %llu\n", udp_packets);
fprintf(stderr, "INBOUNDS: other packets - %llu\n", nontcpudp_packets);
#else
fprintf(stderr, "\nINBOUNDS: TCP packets - %lu\n", tcp_packets);
fprintf(stderr, "INBOUNDS: UDP packets - %lu\n", udp_packets);
fprintf(stderr, "INBOUNDS: other packets - %lu\n", nontcpudp_packets);
#endif
for (pp = plist; pp; pp = pp->next) {
#ifdef HAVE_LONG_LONG
fprintf(stderr, "\tprotocol: %3u, number: %llu\n", pp->ip_p, pp->count);
#else
fprintf(stderr, "\tprotocol: %3u, number: %lu\n", pp->ip_p, pp->count);
#endif
}
fprintf(stderr, "\n");
}
/* for a new TCP connection */
void *
inbounds_tcp_newconn(
tcp_pair *ptp)
{
itcinfo *newConn = Makeitcinfo();
if (mod_info->last_tcp_scheduled_time.tv_sec == 0) {
mod_info->last_tcp_scheduled_time = current_time;
mod_info->last_tcp_actual_time = current_time;
}
newConn->first_time = current_time;
newConn->first_data_time.tv_sec = 0;
newConn->first_data_time.tv_usec = 0;
newConn->last_time = current_time;
newConn->last_data_time.tv_sec = 0;
newConn->last_data_time.tv_usec = 0;
newConn->had_data = FALSE;
newConn->new = TRUE;
newConn->closed = FALSE;
newConn->addr_pair = ptp->addr_pair;
newConn->ptp = ptp;
newConn->a_pkt = 0;
newConn->b_pkt = 0;
newConn->a_byte = 0;
newConn->b_byte = 0;
newConn->next = NULL;
newConn->prev = NULL;
newConn->tcb_lastdata = &ptp->a2b;
newConn->qNum = 0;
newConn->aNum = 0;
newConn->qSum = 0;
newConn->aSum = 0;
newConn->q2aIdle.tv_sec = 0; newConn->q2aIdle.tv_usec = 0;
newConn->a2qIdle.tv_sec = 0; newConn->a2qIdle.tv_usec = 0;
newConn->dir = TCB_CACHE_A2B;
if (mod_info->tcp_conn_head != NULL) {
mod_info->tcp_conn_tail->next = newConn;
newConn->prev = mod_info->tcp_conn_tail;
mod_info->tcp_conn_tail = newConn;
}
else { /* the list is empty */
mod_info->tcp_conn_head = newConn;
mod_info->tcp_conn_tail = newConn;
}
mod_info->tcp_total_conn++;
return newConn;
}
/* delete TCP connection */
void
inbounds_tcp_deleteconn(
tcp_pair *ptp, /* info I have about this connection */
void *mod_data) /* module specific info for this conn*/
{
itcinfo *conn = mod_data;
Bool done = FALSE;
if (conn == mod_info->tcp_conn_head) {
mod_info->tcp_conn_head = mod_info->tcp_conn_head->next;
if (mod_info->tcp_conn_head) {
mod_info->tcp_conn_head->prev = NULL;
}
done = TRUE;
}
if (conn == mod_info->tcp_conn_tail) {
mod_info->tcp_conn_tail = mod_info->tcp_conn_tail->prev;
if (mod_info->tcp_conn_tail) {
mod_info->tcp_conn_tail->next = NULL;
}
done = TRUE;
}
if (!done) {
conn->prev->next = conn->next;
conn->next->prev = conn->prev;
}
Freeitcinfo(conn);
return;
}
/* For TCP packets
* If this packet opens a new connections then output the 'O' message.
* Grab the information required to generate the update messages.
*/
void
inbounds_tcp_read(
struct ip *pip, /* the packet */
tcp_pair *ptp, /* info I have about this connection */
void *plast, /* past byte in the packet */
void *mod_data) /* module specific info for this
connection */
{
char *tmp;
struct tcphdr *tcp;/* TCP header information */
int data_len = 0; /* length of the data cargo in the packet */
itcinfo *conn = mod_data;
timeval delta;
tcb *ptcb;
int dir;
int status = 0;
double dtime = 0;
++tcp_packets;
#ifdef _MONITOR
ipCheck(pip, plast);
tcpCheck(pip, ptp, plast);
#endif
/* first, discard any connections that we aren't interested in. */
/* That means that pmodstruct is NULL */
if (conn == NULL) {
return;
}
if (0) {
printf("hash %i\t\tclosed %i, a2bfin %i, b2afin %i\n",
ptp->addr_pair.hash, conn->closed,
ptp->a2b.fin_count, ptp->b2a.fin_count);
fflush(stdout);
}
if (conn->new) {
if (ptp->a2b.syn_count > 0) {
status = 0;
} else if (ptp->b2a.syn_count > 0) {
status = 0;
conn->dir = TCB_CACHE_B2A;
} else {
status = 1;
if (conn->addr_pair.a_port < conn->addr_pair.b_port) {
conn->dir = TCB_CACHE_B2A;
}
}
dtime = current_time.tv_sec + (current_time.tv_usec / 1000000.0);
if ((tmp=(char*)calloc(MAX_LINE_LEN,sizeof(char)))==NULL) {
fprintf(stderr,"mod_inbounds: calloc() failed\n");
exit(-1);
}
sprintf(tmp, "O %.6f TCP %s %s %i\n",
dtime, ptp->a_endpoint, ptp->b_endpoint, status);
if (fwrite(tmp, strlen(tmp), 1, stdout) <= 0) {
fprintf(stderr, "Couldn't write to stdout\n");
exit(1);
}
fflush(stdout);
free(tmp);
conn->new = FALSE;
}
/* Setting a pointer to the beginning of the TCP header */
#ifdef IP_IPVHL
tcp = (struct tcphdr *) ((char *)pip + (4 * (pip->ip_vhl & 0x0f)));
#else
tcp = (struct tcphdr *) ((char *)pip + (4 * pip->ip_hl));
#endif
/* calculate the amount of user data */
data_len = ntohs(pip->ip_len) - /* size of entire IP packet (and IP header) */
#ifdef IP_IPVHL
(4 * (pip->ip_vhl & 0x0f)) - /* less the IP header */
#else
(4 * pip->ip_hl) - /* less the IP header */
#endif
#ifdef TCP_THXOFF
(4 * (tcp->th_xoff >> 4)); /* less the TCP header */
#else
(4 * tcp->th_off); /* less the TCP header */
#endif
/* see which of the 2 TCB's this goes with */
if (ptp->addr_pair.a_port == ntohs(tcp->th_sport)) {
ptcb = &ptp->a2b;
dir = TCB_CACHE_A2B;
} else {
ptcb = &ptp->b2a;
dir = TCB_CACHE_B2A;
}
if (0)
printf("INBOUNDS: %s <-> %s; dir = %i ",
ptp->a_endpoint, ptp->b_endpoint, dir);
if (debug > 2) {
printf("conn %s<->%s, my dir=%i, packet's dir=%i; IsNewBurst=",
ptp->a_endpoint, ptp->b_endpoint, conn->dir, dir);
}
if (data_len > 0) {
if (tv_lt(conn->first_data_time, conn->first_time)) {
conn->first_data_time = current_time;
conn->had_data = TRUE;
}
}
/* see if it's a new burst */
if (!conn->closed) {
if (((data_len > 0) && (IsNewBurst(conn, ptcb, tcp, dir))) ||
((FIN_SET(tcp)) && (FinCount(ptp) == 1)) ||
(RESET_SET(tcp))) {
delta = current_time;
tv_sub(&delta, conn->last_data_time);
if (FIN_SET(tcp) || RESET_SET(tcp)) {
if (conn->had_data) {
if (conn->dir == 0) { /* we had a question before */
conn->dir = 1;
conn->qNum++;
conn->qSum += conn->burst_bytes;
tv_add(&conn->q2aIdle, delta);
}
else { /* we had an answer before */
conn->dir = 0; /* we have question */
conn->aNum++; /* number of complete answers */
conn->aSum += conn->burst_bytes;
tv_add(&conn->a2qIdle, delta);
}
}
}
else {
if (dir == TCB_CACHE_A2B) {
conn->dir = 0; /* we have question */
conn->aNum++; /* number of complete answers */
conn->aSum += conn->burst_bytes;
tv_add(&conn->a2qIdle, delta);
}
else {
conn->dir = 1;
conn->qNum++;
conn->qSum += conn->burst_bytes;
tv_add(&conn->q2aIdle, delta);
}
}
conn->burst_bytes = 0;
if (0) {
fprintf(stderr, "%.6f switching direction from %s to %s, idle time is %.6f\n",
current_time.tv_sec + (current_time.tv_usec / 1000000.0),
(conn->dir == 0) ? "answer" : "question",
(conn->dir == 0) ? "question" : "answer",
delta.tv_sec + (delta.tv_usec / 100000.0));
}
if (debug > 2)
printf("true ");
}
}
if (data_len > 0) {
conn->last_data_time = current_time;
conn->burst_bytes += data_len;
}
conn->last_time = current_time;
status = 0;
if (!conn->closed) {
if ((FinCount(ptp) >= 1) || (ConnReset(ptp))) {
if (0) {
fprintf(stderr, "number of questions: %i, number of answers: %i\n",
conn->qNum, conn->aNum);
}
TCPInteractivity(conn);
if (dtime == 0) {
dtime = current_time.tv_sec + (current_time.tv_usec / 1000000.0);
}
if ((ptp->a2b.reset_count >=1) || (ptp->b2a.reset_count >= 1)) {
status = 1;
}
if ((tmp=(char*)calloc(MAX_LINE_LEN,sizeof(char)))==NULL) {
fprintf(stderr,"mod_inbounds: calloc() failed\n");
exit(-1);
}
sprintf(tmp, "C %.6f TCP %s %s %i\n",
dtime, ptp->a_endpoint, ptp->b_endpoint, status);
if (fwrite(tmp, strlen(tmp), 1, stdout) <= 0) {
fprintf(stderr, "mod_inbounds: couldn't write to stdout\n"
);
exit(1);
}
fflush(stdout);
free(tmp);
conn->closed = TRUE;
}
}
if ((elapsed(mod_info->last_tcp_scheduled_time, current_time) / 1000000.0)
>= INBOUNDS_TCP_UPDATE_INTERVAL) {
AllTCPInteractivity();
}
}
/* for new UDP connections */
void *
inbounds_udp_newconn(
udp_pair *pup)
{
iucinfo *newConn = Makeiucinfo();
inc_cnt++;
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_newconn() \n");
if (mod_info->last_udp_scheduled_time.tv_sec == 0) {
mod_info->last_udp_scheduled_time = current_time;
mod_info->last_udp_actual_time = current_time;
}
newConn->first_time = current_time;
newConn->last_time = current_time;
newConn->new = TRUE;
newConn->closed = FALSE;
newConn->addr_pair = pup->addr_pair;
newConn->pup = pup;
newConn->a_pkt = 0;
newConn->b_pkt = 0;
newConn->a_byte = 0;
newConn->b_byte = 0;
newConn->next = NULL;
newConn->prev = NULL;
newConn->qNum = 0;
newConn->aNum = 0;
newConn->qSum = 0;
newConn->aSum = 0;
// If this field remains -1, it means
// q2aIdle could not be calculated for
// INBOUNDS_UDP_UPDATE_INTERVAL.
// In that case, we shall print out
// q2aIdle as = INBOUNDS_UDP_UPDATE_INTERVAL, i.e. q2a duration is max.
newConn->q2aIdle.tv_sec = -1;
newConn->q2aIdle.tv_usec = 0;
newConn->a2qIdle.tv_sec = -1;
newConn->a2qIdle.tv_usec = 0;
newConn->dir = UDP_A2B;
if (mod_info->udp_conn_head != NULL) {
mod_info->udp_conn_tail->next = newConn;
newConn->prev = mod_info->udp_conn_tail;
mod_info->udp_conn_tail = newConn;
}
else {
mod_info->udp_conn_head = newConn;
mod_info->udp_conn_tail = newConn;
}
mod_info->udp_total_conn++;
return newConn;
}
/* This function is not invoked by tcptrace currently and is here mostly
* for the sake of completeness. You may need to fix the module definition
* in modules.h and fix tcptrace.c/trace.c to make sure this function gets
* invoked (if you need this functionality, of course) - Mani, 4 Mar 2004.
*/
/* delete timedout UDP connections */
void
inbounds_udp_deleteconn(
udp_pair *pup, // info I have about this conn.
void *mod_data)// module specific info for this
//conn.
{
iucinfo *conn = mod_data;
Bool done = FALSE;
udp_delconn_cnt++;
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_deleteconn() \n");
if (conn == mod_info->udp_conn_head) {
mod_info->udp_conn_head = mod_info->udp_conn_head->next;
if (mod_info->udp_conn_head) {
mod_info->udp_conn_head->prev = NULL;
}
done = TRUE;
}
if (conn == mod_info->udp_conn_tail) {
mod_info->udp_conn_tail = mod_info->udp_conn_tail->prev;
if (mod_info->udp_conn_tail) {
mod_info->udp_conn_tail->next = NULL;
}
done = TRUE;
}
if (!done) {
conn->prev->next = conn->next;
conn->next->prev = conn->prev;
}
if(!conn->closed) {
UDPInteractivity(conn);
PrintUDPCMsg(conn);
}
Freeiucinfo(conn);
return;
}
/* For UDP packets
* If this packet opens a new connections then output the 'O' message.
* Grab the information required to generate the update messages
*/
void
inbounds_udp_read(
struct ip *pip,
udp_pair *pup,
void *plast,
void *mod_data)
{
char *tmp;
struct udphdr *udp; /* UDP header information */
int data_len = 0; /* length of the data cargo in the packet */
iucinfo *conn = mod_data;
timeval delta;
int dir;
int status = 0;
double dtime = 0;
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_read() \n");
++udp_packets;
#ifdef _MONITOR
ipCheck(pip, plast);
udpCheck(pip, pup, plast);
#endif
/* first, discard any connections that we aren't interested in. */
/* That means that pmodstruct is NULL */
if (conn == NULL || pup == NULL) {
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_read() conn is NULL or pup \n");
return;
}
if (conn->new) {
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_read() This is new connection\n");
dtime = current_time.tv_sec + (current_time.tv_usec / 1000000.0);
if(INBOUNDS_DEBUG) {
printf("dtime: %.6f \n",dtime);
printf("pup->a_endpoint: %s \n",pup->a_endpoint);
printf("pup->b_endpoint: %s \n",pup->b_endpoint);
}
if ((tmp=(char*)calloc(MAX_LINE_LEN,sizeof(char)))==NULL) {
fprintf(stderr,"mod_inbounds: calloc() failed\n");
exit(-1);
}
sprintf(tmp, "O %.6f UDP %s %s %i\n",
dtime, pup->a_endpoint, pup->b_endpoint, status);
if (fwrite(tmp, strlen(tmp), 1, stdout) <= 0) {
fprintf(stderr, "mod_inbounds: couldn't write to stdout\n");
exit(1);
}
fflush(stdout);
free(tmp);
conn->new = FALSE;
}
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_read() datalen is being calculated \n");
/* Setting a pointer to the beginning of the TCP header */
#ifdef IP_IPVHL
udp = (struct udphdr *) ((char *)pip + (4 * (pip->ip_vhl & 0x0f)));
#else
udp = (struct udphdr *) ((char *)pip + (4 * pip->ip_hl));
#endif
/* calculate the amount of user data */
data_len = ntohs(pip->ip_len) -
/* size of entire IP packet (and IP header) */
#ifdef IP_IPVHL
(4 * (pip->ip_vhl & 0x0f)) - /* less the IP header */
#else
(4 * pip->ip_hl) - /* less the IP header */
#endif
UDPHDR_LEN;
if(INBOUNDS_DEBUG)
printf("mod_inbounds: udp_read() datalen:%d \n",data_len);
/* see in which direction this goes with */
if (INBOUNDS_DEBUG) {
printf("INBOUNDS: %d \n",
pup->addr_pair.a_port);
printf("INBOUNDS: %d \n",
ntohs(udp->uh_sport));
}
// Do anything at all if only we captured the headers fully
if(data_len >= 0) {
if (pup->addr_pair.a_port == ntohs(udp->uh_sport))
dir = UDP_A2B;
else
dir = UDP_B2A;
/* if (data_len > 0) { // this packet has data in it
*
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_read() this packet has data in it\n");
if (tv_lt(conn->first_data_time, conn->first_time)) {
conn->first_data_time = current_time;
conn->had_data = TRUE;
}
*/
delta=current_time;
tv_sub(&delta,conn->last_time);
conn->last_time = current_time;
if(dir == UDP_A2B) {// this is a question
// If what we had before was an answer, we can calculate AQIT
if(conn->dir==UDP_B2A) {
if(conn->a2qIdle.tv_sec==-1) {// First sample in the last
// INBOUNDS_UPDATE_INTERVAL
conn->a2qIdle.tv_sec=0.0;
conn->a2qIdle.tv_usec=0.0;
tv_add(&conn->a2qIdle,delta);
}
else {
tv_add(&conn->a2qIdle,delta);
}
}
conn->dir = UDP_A2B;
conn->qNum++;
conn->qSum += data_len;
}
else {// this is an answer
// If what we had before was a question, we can calculate QAIT
Bool done = FALSE;
if(conn->dir==UDP_A2B) {
if(conn->q2aIdle.tv_sec==-1) {// First sample in the last
// INBOUNDS_UPDATE_INTERVAL
conn->q2aIdle.tv_sec=0.0;
conn->q2aIdle.tv_usec=0.0;
tv_add(&conn->q2aIdle,delta);
}
else {
tv_add(&conn->q2aIdle,delta);
}
}
conn->dir = UDP_B2A;
conn->aNum++;
conn->aSum += data_len;
}
} // END: if data_len >= 0
/* Do the interactivity - it has to be done for both TCP and UDP */
if ((elapsed(mod_info->last_udp_scheduled_time, current_time) / 1000000.0)
>= INBOUNDS_UDP_UPDATE_INTERVAL) {
AllUDPInteractivity();
}
if(INBOUNDS_DEBUG)
printf("mod_inbounds:udp_read() exiting udp_read \n");
}
/* call the respective TCP and UDP routines to print the update messages */
static void
AllTCPInteractivity(void)
{
itcinfo *tcp_conn;
if(INBOUNDS_DEBUG)
printf("mod_inbounds: in AllTCPInteractivity() \n");
for (tcp_conn = mod_info->tcp_conn_head; tcp_conn != NULL;
tcp_conn = tcp_conn->next) {
if (!tcp_conn->closed) {
TCPInteractivity(tcp_conn);
}
}
mod_info->last_tcp_scheduled_time.tv_sec += INBOUNDS_TCP_UPDATE_INTERVAL;
mod_info->last_tcp_actual_time = current_time;
}
/* calculate and print out interactivity statistics for TCP connections */
static void
TCPInteractivity(
itcinfo *conn)
{
char *tmp;
double qAvg;
double aAvg;
double q2aIdle;
double a2qIdle;
double dtime;
double update_interval;
timeval first_time;
if ((tmp=(char*)calloc(MAX_LINE_LEN,sizeof(char)))==NULL) {
fprintf(stderr,"itcptrace : calloc() failed\n");
exit(-1);
}
if (conn->had_data) {
first_time = conn->first_data_time;
}
else {
first_time = conn->first_time;
}
if (tv_lt(mod_info->last_tcp_actual_time, first_time)) {
update_interval = elapsed(conn->first_data_time, current_time) /
1000000.0;
/* if this is the first packet belonging to the connection,
we don't need to print statistics */
if (update_interval == 0)
return;
}
else {
update_interval = elapsed(mod_info->last_tcp_actual_time,
current_time) / 1000000.0;
}
if (update_interval < 1.0) {
update_interval = 1.0;
}
if (conn->qNum != 0) {