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
/
mod_traffic.c
1376 lines (1169 loc) · 35.8 KB
/
mod_traffic.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, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003, 2004
* Ohio University.
*
* ---
*
* Starting with the release of tcptrace version 6 in 2001, tcptrace
* is licensed under the GNU General Public License (GPL). We believe
* that, among the available licenses, the GPL will do the best job of
* allowing tcptrace to continue to be a valuable, freely-available
* and well-maintained tool for the networking community.
*
* Previous versions of tcptrace were released under a license that
* was much less restrictive with respect to how tcptrace could be
* used in commercial products. Because of this, I am willing to
* consider alternate license arrangements as allowed in Section 10 of
* the GNU GPL. Before I would consider licensing tcptrace under an
* alternate agreement with a particular individual or company,
* however, I would have to be convinced that such an alternative
* would be to the greater benefit of the networking community.
*
* ---
*
* This file is part of Tcptrace.
*
* Tcptrace was originally written and continues to be maintained by
* Shawn Ostermann with the help of a group of devoted students and
* users (see the file 'THANKS'). The work on tcptrace has been made
* possible over the years through the generous support of NASA GRC,
* the National Science Foundation, and Sun Microsystems.
*
* Tcptrace is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tcptrace is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tcptrace (in the file 'COPYING'); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Author: Shawn Ostermann
* School of Electrical Engineering and Computer Science
* Ohio University
* Athens, OH
* http://www.tcptrace.org/
*/
#include "tcptrace.h"
static char const GCC_UNUSED rcsid[] =
"$Header$";
#ifdef LOAD_MODULE_TRAFFIC
#include "mod_traffic.h"
/* info kept for each (active) port */
struct traffic_info {
/* which port */
u_short port;
/* interval byte counters */
PLINE line_nbytes;
u_long nbytes;
u_long ttlbytes;
/* interval packet counters */
PLINE line_npackets;
u_long npackets;
u_long ttlpackets;
/* active connections */
PLINE line_nactive;
u_long nactive;
u_long ttlactive;
/* idle connections */
PLINE line_nidle;
u_long nidle;
u_long ttlidle;
/* open connections */
PLINE line_nopen;
u_long nopen;
u_long ttlopen;
/* instantaneous open connections */
PLINE line_niopen;
u_long n_i_open;
u_long ttl_i_open;
/* long-duration connections */
PLINE line_nlong;
u_long nlong;
u_long ttllong;
/* pureacks */
PLINE line_pureacks;
u_long npureacks;
u_long ttlpureacks;
/* which color is used for plotting */
char *color;
/* linked list of the one's we're using */
struct traffic_info *next;
};
static struct traffic_info *traffichead = NULL;
#define NUM_PORTS 65536
static struct traffic_info **ports; /* [NUM_PORTS] */
#define EXCLUDE_PORT ((void *)(-1))
#define INCLUDE_PORT (NULL)
/* name of the file that port data is dumped into */
#define PORT_FILENAME "traffic_byport.dat"
#define STATS_FILENAME "traffic_stats.dat"
/* additional info kept per connection */
struct conn_info {
Bool wasactive; /* was this connection active over the interval? */
Bool wasopen; /* was this this connection EVER open? */
Bool isopen; /* is this connection open now? */
Bool islong; /* is this a long-duration connection? */
Bool halfopen; /* for half open conns */
struct traffic_info *pti1; /* pointer to the port info for this one */
struct traffic_info *pti2; /* pointer to the port info for this one */
struct conn_info *next; /* next in the chain */
u_int last_dupacks; /* last value of dupacks I saw */
u_int last_rexmits; /* last value of rexmits I saw */
u_int last_rtts; /* last value of rtt counters I saw */
};
static struct conn_info *connhead = NULL;
/* plotter files that we keep open */
static PLOTTER plotter_bytes;
static PLOTTER plotter_packets;
static PLOTTER plotter_active;
static PLOTTER plotter_idle;
static PLOTTER plotter_open;
static PLOTTER plotter_openclose;
static PLOTTER plotter_i_open;
static PLOTTER plotter_loss;
static PLOTTER plotter_long;
static PLOTTER plotter_rtt;
static PLOTTER plotter_halfopen;
static PLOTTER plotter_pureacks;
static PLOTTER plotter_data;
#define PLOTTER_BYTES_FILENAME "traffic_bytes.xpl"
#define PLOTTER_PACKETS_FILENAME "traffic_packets.xpl"
#define PLOTTER_ACTIVE_FILENAME "traffic_active.xpl"
#define PLOTTER_OPEN_FILENAME "traffic_open.xpl"
#define PLOTTER_OPENCLOSE_FILENAME "traffic_openclose.xpl"
#define PLOTTER_I_OPEN_FILENAME "traffic_i_open.xpl"
#define PLOTTER_LOSS_FILENAME "traffic_loss.xpl"
#define PLOTTER_LONG_FILENAME "traffic_long.xpl"
#define PLOTTER_RTT_FILENAME "traffic_rtt.xpl"
#define PLOTTER_HALFOPEN_FILENAME "traffic_halfopen.xpl"
#define PLOTTER_PUREACKS_FILENAME "traffic_pureacks.xpl"
#define PLOTTER_IDLE_FILENAME "traffic_idle.xpl"
#define PLOTTER_DATA_FILENAME "traffic_data.xpl"
/* argument flags */
static float age_interval = 15.0; /* 15 seconds by default */
static Bool doplot_bytes = FALSE;
static Bool doplot_packets = FALSE;
static Bool doplot_active = FALSE;
static Bool doplot_open = FALSE;
static Bool doplot_openclose = FALSE;
static Bool doplot_i_open = FALSE;
static Bool doplot_loss = FALSE;
static Bool doplot_long = FALSE;
static Bool doplot_rtt = FALSE;
static Bool doplot_halfopen = FALSE;
static Bool doplot_pureacks = FALSE;
static Bool doplot_idle = FALSE;
static Bool doplot_data = FALSE;
static int longconn_duration = 60;
/* local routines */
static struct traffic_info *MakeTrafficRec(u_short port);
static void MakeTrafficLines(struct traffic_info *pti);
static struct conn_info *MakeConnRec(void);
static void AgeTraffic(void);
static struct traffic_info *FindPort(u_short port);
static void IncludePorts(unsigned firstport, unsigned lastport);
static void ExcludePorts(unsigned firstport, unsigned lastport);
static void CheckPortNum(unsigned portnum);
static char *PortName(int port);
static void ParseArgs(char *argstring);
static void DoplotIOpen(int port, Bool fopen);
/* info for opens and closes graphs */
static PLINE line_num_closes;
static PLINE line_num_opens;
static PLINE line_open_conns;
static PLINE line_num_halfopens;
static int num_closes = 0;
static int num_opens = 0;
static int ttl_num_opens = 0;
static int ttl_num_closes = 0;
static int open_conns = 0;
static int num_halfopens = 0;
/* info for total byte counters */
static PLINE line_data_nonrexmit;
static PLINE line_data_all;
static u_llong data_nbytes_nonrexmit;
static u_llong data_nbytes_all;
/* info for the loss events graph */
static PLINE line_dupacks;
static PLINE line_rexmits;
static u_long dupacks;
static u_long ttl_dupacks;
static u_long rexmits;
static u_long ttl_rexmits;
/* info for the RTT graph */
static PLINE line_rtt_avg;
static PLINE line_rtt_min;
static PLINE line_rtt_max;
static float rtt_ttl; /* in msecs */
static float ttl_rtt_ttl; /* in msecs */
static int rtt_min = -1; /* in msecs */
static int rtt_max = -1; /* in msecs */
static int rtt_samples;
static int ttl_rtt_samples;
static u_int rtt_minvalid = 0; /* minimum RTT to consider (ms) */
static u_int rtt_maxvalid = 0xffffffff; /* maximum RTT to consider (ms) */
/* local debugging flag */
static int ldebug = 0;
static void
CheckPortNum(
unsigned portnum)
{
if (portnum >= NUM_PORTS) {
fprintf(stderr,"mod_traffic: Invalid port number '%d'\n", portnum);
traffic_usage();
exit(-1);
}
}
static void
ExcludePorts(
unsigned firstport,
unsigned lastport)
{
CheckPortNum(firstport);
CheckPortNum(lastport);
if (ldebug)
printf("mod_traffic: excluding ports [%d-%d]\n", firstport, lastport);
while (firstport <= lastport)
ports[firstport++] = EXCLUDE_PORT;
}
static void
IncludePorts(
unsigned firstport,
unsigned lastport)
{
CheckPortNum(firstport);
CheckPortNum(lastport);
if (ldebug)
printf("mod_traffic: including ports [%d-%d]\n", firstport, lastport);
while (firstport <= lastport)
ports[firstport++] = INCLUDE_PORT;
}
static int
traffic_init_files(void)
{
static int created = 0;
if (created)
return;
created = 1;
/* open the output files */
if (doplot_packets)
plotter_packets =
new_plotter(NULL,
PLOTTER_PACKETS_FILENAME,
"packets per second over time by port",
"time","packets/second",
NULL);
if (doplot_bytes)
plotter_bytes =
new_plotter(NULL,
PLOTTER_BYTES_FILENAME,
"bytes per second over time by port",
"time","bytes/second",
NULL);
if (doplot_active)
plotter_active =
new_plotter(NULL,
PLOTTER_ACTIVE_FILENAME,
"active connections over time by port",
"time","active connections",
NULL);
if (doplot_idle)
plotter_idle =
new_plotter(NULL,
PLOTTER_IDLE_FILENAME,
"idle connections over time by port",
"time","idle connections",
NULL);
if (doplot_open)
plotter_open =
new_plotter(NULL,
PLOTTER_OPEN_FILENAME,
"open connections over time by port",
"time","open connections",
NULL);
if (doplot_i_open)
plotter_i_open =
new_plotter(NULL,
PLOTTER_I_OPEN_FILENAME,
"open connections over time by port - instantaneous",
"time","number of connections",
NULL);
if (doplot_openclose) {
plotter_openclose =
new_plotter(NULL,
PLOTTER_OPENCLOSE_FILENAME,
"connections opened and closed over time",
"time","number of connections",
NULL);
line_num_opens = new_line(plotter_openclose, "Number Opens", "green");
line_num_closes = new_line(plotter_openclose, "Number Closes", "red");
line_open_conns = new_line(plotter_openclose, "Total Open", "blue");
}
if (doplot_halfopen) {
plotter_halfopen =
new_plotter(NULL,
PLOTTER_HALFOPEN_FILENAME,
"half open connections over time",
"time","number of half open connections",
NULL);
line_num_halfopens = new_line(plotter_halfopen,
"Halfopen Conns", "green");
}
if (doplot_pureacks) {
plotter_pureacks =
new_plotter(NULL,
PLOTTER_PUREACKS_FILENAME,
"pure acks (no data) per second over time",
"time","pureacks/second",
NULL);
}
if (doplot_loss) {
plotter_loss =
new_plotter(NULL,
PLOTTER_LOSS_FILENAME,
"packet loss per second over time",
"time","events/second",
NULL);
line_dupacks = new_line(plotter_loss, "Triple Dupacks", "yellow");
line_rexmits = new_line(plotter_loss, "Retransmits", "blue");
}
if (doplot_rtt) {
plotter_rtt =
new_plotter(NULL,
PLOTTER_RTT_FILENAME,
"RTT over time",
"time","RTT (msecs)",
NULL);
line_rtt_min = new_line(plotter_rtt, "Min RTT", "green");
line_rtt_max = new_line(plotter_rtt, "Max RTT", "red");
line_rtt_avg = new_line(plotter_rtt, "Average RTT", "blue");
}
if (doplot_data) {
plotter_data =
new_plotter(NULL,
PLOTTER_DATA_FILENAME,
"Total Data Sent Over Time",
"time","total bytes",
NULL);
line_data_all = new_line(plotter_data, "All Data", "blue");
line_data_nonrexmit = new_line(plotter_data, "Non-Rexmitted Data", "red");
}
if (doplot_long) {
char title[100];
snprintf(title,sizeof(title),"connections still open after %d seconds\n",
longconn_duration);
plotter_long =
new_plotter(NULL,
PLOTTER_LONG_FILENAME,
title,
"time","number of connections",
NULL);
}
}
/* Mostly as a module example, here's a plug in that records TRAFFIC info */
int
traffic_init(
int argc,
char *argv[])
{
int i;
int enable=0;
char *args = NULL;
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,"traffic",sizeof("traffic")-1) == 0) {
/* I want to be called */
args = argv[i]+(sizeof("-xtraffic")-1);
enable = 1;
printf("mod_traffic: characterizing traffic\n");
argv[i] = NULL;
}
}
}
if (!enable)
return(0); /* don't call me again */
/* init the data storage structure */
ports = MallocZ(NUM_PORTS*sizeof(struct traffic_info *));
ports[0] = MakeTrafficRec(0);
/* parse the encoded args */
ParseArgs(args);
/* we don't want the normal output */
printsuppress = TRUE;
/* create any lines that I want to draw */
MakeTrafficLines(ports[0]);
/* init the graphs and etc... */
AgeTraffic();
return(1); /* TRUE means call traffic_read and traffic_done later */
}
/* return the record for traffic on this port */
static struct traffic_info *
FindPort(
u_short port)
{
struct traffic_info *pti;
/* port "0" means "all", but we don't need to treat it as a special case */
/* see what's there now */
pti = ports[port];
/* see if it's "excluded" */
if ((port != 0) && (pti == EXCLUDE_PORT))
return(NULL);
/* make a new one if there's a NULL there */
if (!pti) {
pti = MakeTrafficRec(port);
/* create any lines that I want to draw */
MakeTrafficLines(pti);
}
return(pti);
}
static struct traffic_info *
MakeTrafficRec(
u_short port)
{
struct traffic_info *pti;
pti = MallocZ(sizeof(struct traffic_info));
if (ldebug>10)
printf("MakeTrafficRec(%d) called\n", (int)port);
/* init */
pti->port = port;
/* chain it in (at head of list) */
pti->next = traffichead;
traffichead = pti;
/* add to lookup array */
ports[port] = pti;
return(pti);
}
static void
MakeTrafficLines(
struct traffic_info *pti)
{
char *portname;
static int nextcolor = 0;
/* map port number to name for printing */
portname = (pti->port==0)?"total":strdup(PortName(pti->port));
/* pick color */
pti->color = ColorNames[nextcolor % NCOLORS];
++nextcolor;
/* create the lines that we sometimes use */
if (doplot_bytes)
pti->line_nbytes = new_line(plotter_bytes, portname, pti->color);
if (doplot_packets)
pti->line_npackets = new_line(plotter_packets, portname, pti->color);
if (doplot_active)
pti->line_nactive = new_line(plotter_active, portname, pti->color);
if (doplot_idle)
pti->line_nidle = new_line(plotter_idle, portname, pti->color);
if (doplot_open)
pti->line_nopen = new_line(plotter_open, portname, pti->color);
if (doplot_long)
pti->line_nlong = new_line(plotter_long, portname, pti->color);
if (doplot_i_open)
pti->line_niopen = new_line(plotter_i_open, portname, pti->color);
if (doplot_pureacks)
pti->line_pureacks = new_line(plotter_pureacks, portname, pti->color);
}
static struct conn_info *
MakeConnRec(void)
{
struct conn_info *pci;
pci = MallocZ(sizeof(struct conn_info));
/* chain it in (at head of list) */
pci->next = connhead;
connhead = pci;
return(pci);
}
void
traffic_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) /* connection info for this one */
{
struct tcphdr *ptcp = (struct tcphdr *) ((char *)pip + 4*IP_HL(pip));
struct traffic_info *pti1 = FindPort(ntohs(ptcp->th_sport));
struct traffic_info *pti2 = FindPort(ntohs(ptcp->th_dport));
u_long bytes = ntohs(pip->ip_len);
static timeval last_time = {0,0};
struct conn_info *pci = mod_data;
int was_rexmit = 0;
/* in case files aren't set up yet */
traffic_init_files();
/* if neither port is interesting, then ignore this one */
if (!pti1 && !pti2) {
return;
}
/* OK, this connection is now active */
pci->wasactive = 1;
/* check to see if it's really "open" (traffic in both directions) */
if (!pci->wasopen) {
if ((ptp->a2b.packets > 0) && (ptp->b2a.packets > 0)) {
/* bidirectional: OK, we'll call it open */
pci->wasopen = 1;
pci->isopen = 1;
++num_opens;
++ttl_num_opens;
++open_conns;
/* instantaneous opens and closes */
if (doplot_i_open) {
DoplotIOpen(ntohs(ptcp->th_dport), TRUE);
DoplotIOpen(ntohs(ptcp->th_sport), TRUE);
DoplotIOpen(0, TRUE);
}
}
}
/* add to port-specific counters */
if (pti1) {
pti1->nbytes += bytes;
pti1->npackets += 1;
}
if (pti2) {
pti2->nbytes += bytes;
pti2->npackets += 1;
}
/* add to GLOBAL counters */
ports[0]->nbytes += bytes;
ports[0]->npackets += 1;
ports[0]->npureacks += 1;
/* see if we're closing it */
if (RESET_SET(ptcp) ||
(FIN_SET(ptcp) && /* find in BOTH directions */
((ptp->a2b.fin_count>0) && (ptp->b2a.fin_count>0)))) {
if (pci->isopen) {
pci->isopen = 0;
++num_closes;
--open_conns;
/* instantaneous opens and closes */
if (doplot_i_open) {
DoplotIOpen(ntohs(ptcp->th_dport), FALSE);
DoplotIOpen(ntohs(ptcp->th_sport), FALSE);
DoplotIOpen(0, FALSE);
}
}
}
/* half open conns */
if (FIN_SET(ptcp)) {
if ((ptp->a2b.fin_count>0) && (ptp->b2a.fin_count>0)) {
if (pci->halfopen) {
/* fully closed now */
--num_halfopens;
pci->halfopen = 0;
}
} else if (!pci->halfopen) {
/* half open now */
++num_halfopens;
pci->halfopen = 1;
}
}
/* check losses */
if (pci->last_dupacks != ptp->a2b.rtt_triple_dupack+
ptp->b2a.rtt_triple_dupack) {
pci->last_dupacks = ptp->a2b.rtt_triple_dupack+
ptp->b2a.rtt_triple_dupack;
++dupacks;
++ttl_dupacks;
}
if (pci->last_rexmits != ptp->a2b.rexmit_pkts+ptp->b2a.rexmit_pkts) {
pci->last_rexmits = ptp->a2b.rexmit_pkts+ptp->b2a.rexmit_pkts;
was_rexmit = 1;
++rexmits;
++ttl_rexmits;
}
/* add to total data counters */
data_nbytes_all += bytes;
if (!was_rexmit)
data_nbytes_nonrexmit += bytes;
/* RTT stats */
if (ACK_SET(ptcp)) {
tcb *ptcb;
int rtt;
/* see which of the 2 TCB's this goes with */
if (ptp->addr_pair.a_port == ntohs(ptcp->th_dport))
ptcb = &ptp->a2b;
else
ptcb = &ptp->b2a;
/* check the rtt counter of the last sample */
rtt = ptcb->rtt_last / 1000.0;
if ((pci->last_rtts != ptcb->rtt_count + ptcb->rtt_amback) &&
(ptcb->rtt_last != 0.0) &&
(rtt > rtt_minvalid) && (rtt <= rtt_maxvalid)) {
/* sample is only valid when one of these counters is higher */
pci->last_rtts = ptcb->rtt_count + ptcb->rtt_amback;
/* keep stats */
rtt_ttl += rtt;
ttl_rtt_ttl += rtt;
++rtt_samples;
++ttl_rtt_samples;
/* also, remember min and max */
if ((rtt_max == -1) || (rtt_max < rtt))
rtt_max = rtt;
if ((rtt_min == -1) || (rtt_min > rtt))
rtt_min = rtt;
if (ldebug > 9)
printf("Rtt: %d, min:%d, max:%d\n",
rtt, rtt_min, rtt_max);
}
}
/* see if this is now "long duration" */
if (!pci->islong) {
int etime_msecs = elapsed(ptp->first_time,current_time);
if (etime_msecs/1000000 > longconn_duration) {
pci->islong = 1;
}
}
/* count "pure acks" (no data) */
if (ACK_SET(ptcp)) {
int tcp_length, tcp_data_length;
tcp_length = getpayloadlength(pip, plast);
tcp_data_length = tcp_length - (4 * TH_OFF(ptcp));
if (tcp_data_length == 0) {
if (pti1) {
++pti1->npureacks;
}
if (pti2) {
++pti2->npureacks;
}
}
}
/* determine elapsed time and age the samples */
if (elapsed(last_time,current_time)/1000000.0 > age_interval) {
AgeTraffic();
last_time = current_time;
}
}
static char *
PortName(
int port)
{
static char buf[20];
if (port == 0)
return("total");
snprintf(buf,sizeof(buf),"%d",port);
return(buf);
}
static void
AgeTraffic(void)
{
struct traffic_info *pti;
struct conn_info *pci;
static timeval last_time = {0,0};
float etime;
int ups; /* units per second */
/* first time doesn't count */
if (ZERO_TIME(&last_time)) {
last_time = current_time;
return;
}
/* check elapsed time */
etime = elapsed(last_time, current_time);
if (ldebug>1)
printf("AgeTraffic called, elapsed time is %.3f seconds\n", etime/1000000);
if (etime == 0.0)
return;
/* roll the open/active/long connections into the port records */
for (pci=connhead; pci; pci=pci->next) {
if (pci->wasactive) {
if (pci->pti1)
++pci->pti1->nactive;
if (pci->pti2)
++pci->pti2->nactive;
pci->wasactive = 0;
++ports[0]->nactive;
}
if (pci->isopen) {
if (pci->pti1)
++pci->pti1->nopen;
if (pci->pti2)
++pci->pti2->nopen;
++ports[0]->nopen;
if (pci->islong) {
if (pci->pti1)
++pci->pti1->nlong;
if (pci->pti2)
++pci->pti2->nlong;
++ports[0]->nlong;
}
if (!pci->wasactive) {
/* open and !active ==> IDLE */
if (pci->pti1)
++pci->pti1->nidle;
if (pci->pti2)
++pci->pti2->nidle;
++ports[0]->nidle;
}
}
}
/* ============================================================ */
/* plot halfopen conns */
if (doplot_halfopen) {
/* draw lines */
extend_line(line_num_halfopens,current_time, num_halfopens);
}
/* ============================================================ */
/* plot connection activity */
/* opens */
if (doplot_openclose) {
/* draw lines */
extend_line(line_num_opens,current_time, num_opens);
extend_line(line_num_closes,current_time, num_closes);
extend_line(line_open_conns,current_time, open_conns);
/* reset interval counters */
// Counting ttl_num_opens instantaneously as and when num_opens is incremented,
// so that ttl_num_opens is printed properly in traffic_stats.dat even
// when the -C(openclose) option is not given.
// Hence commenting off the following line. - Mani, 4 Aug 2003.
// ttl_num_opens += num_opens;
ttl_num_closes += num_closes;
num_opens = 0;
num_closes = 0;
}
/* ============================================================ */
/* report of loss events */
if (doplot_loss) {
/* convert to events/second */
/* sdo bugfix - Wed May 12, 1999 - round UP!! */
dupacks = (dupacks+age_interval-1)/age_interval;
rexmits = (rexmits+age_interval-1)/age_interval;
/* draw lines */
extend_line(line_dupacks,current_time, dupacks);
extend_line(line_rexmits,current_time, rexmits);
/* reset interval counters */
dupacks = 0;
rexmits = 0;
}
/* ============================================================ */
/* report of RTT */
if (doplot_rtt && (rtt_samples > 0)) {
int rtt_avg;
/* convert to average rtt */
rtt_avg = (int)((rtt_ttl/(float)rtt_samples));
/* draw lines */
extend_line(line_rtt_avg, current_time, rtt_avg);
if (rtt_min != -1)
extend_line(line_rtt_min, current_time, rtt_min);
if (rtt_max != -1)
extend_line(line_rtt_max, current_time, rtt_max);
/* reset interval counters */
rtt_ttl = 0;
rtt_samples = 0;
rtt_min = -1;
rtt_max = -1;
}
/* ============================================================ */
/* report of total data */
if (doplot_data) {
extend_line(line_data_all, current_time, data_nbytes_all);
extend_line(line_data_nonrexmit, current_time, data_nbytes_nonrexmit);
}
/* ============================================================ */
/* print them out */
for (pti=traffichead; pti; pti=pti->next) {
if (ldebug>1)
printf(" Aging Port %u bytes: %lu packets: %lu\n",
pti->port, pti->nbytes, pti->npackets);
/* plot bytes */
if (doplot_bytes) {
/* convert to units per second */
ups = (int)((float)pti->nbytes * 1000000.0 / etime);
/* plot it */
extend_line(pti->line_nbytes,current_time, ups);
}
/* plot packets */
if (doplot_packets) {
/* convert to units per second */
ups = (int)((float)pti->npackets * 1000000.0 / etime);
/* plot it */
extend_line(pti->line_npackets,current_time, ups);
}
/* plot active connections */
if (doplot_active) {
/* plot it */
extend_line(pti->line_nactive,current_time, pti->nactive);
}
/* plot idle connections */
if (doplot_idle) {
/* plot it */
extend_line(pti->line_nidle,current_time, pti->nidle);
}
/* plot open connections */
if (doplot_open) {
/* plot it */
extend_line(pti->line_nopen,current_time, pti->nopen);
}
/* plot long-duration */
if (doplot_long) {
extend_line(pti->line_nlong,current_time, pti->nlong);
}
/* plot pureacks */
if (doplot_pureacks) {
/* convert to units per second */
ups = (int)((float)pti->npureacks * 1000000.0 / etime);
extend_line(pti->line_pureacks, current_time, ups);
}
}
/* zero them out */
for (pti=traffichead; pti; pti=pti->next) {
pti->ttlbytes += pti->nbytes;
pti->ttlpackets += pti->npackets;
pti->ttlpureacks += pti->npureacks;
pti->nbytes = 0;
pti->nlong = 0;
pti->npackets = 0;
pti->nactive = 0;
pti->nidle = 0;
pti->nopen = 0;
pti->npureacks = 0;
}
last_time = current_time;
}
void
traffic_done(void)
{
struct traffic_info *pti;
struct conn_info *pci;
double etime = elapsed(first_packet,last_packet);
int etime_secs = etime / 1000000.0;