1 | #include <netdb.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <lt_inttypes.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include "libtrace.h" |
---|
6 | #include "tracereport.h" |
---|
7 | #include "report.h" |
---|
8 | |
---|
9 | static stat_t prot_stat[4][256] = {{{0,0}}} ; |
---|
10 | static bool suppress[4] = {true,true,true,true}; |
---|
11 | |
---|
12 | void protocol_per_packet(struct libtrace_packet_t *packet) |
---|
13 | { |
---|
14 | uint8_t proto; |
---|
15 | int dir = trace_get_direction(packet); |
---|
16 | if(dir < 0 || dir > 1) |
---|
17 | dir = 2; |
---|
18 | if (trace_get_transport(packet,&proto,NULL)==NULL) |
---|
19 | return; |
---|
20 | |
---|
21 | prot_stat[dir][proto].count++; |
---|
22 | prot_stat[dir][proto].bytes+=trace_get_wire_length(packet); |
---|
23 | suppress[dir] = false; |
---|
24 | } |
---|
25 | |
---|
26 | void protocol_suppress() |
---|
27 | { |
---|
28 | int i; |
---|
29 | printf("%-20s","Direction:"); |
---|
30 | for(i=0;i<4;i++){ |
---|
31 | if(!suppress[i]){ |
---|
32 | switch(i){ |
---|
33 | case 0: |
---|
34 | printf("\t%24s", "Outbound "); |
---|
35 | break; |
---|
36 | case 1: |
---|
37 | printf("\t%24s", "Inbound "); |
---|
38 | break; |
---|
39 | case 2: |
---|
40 | printf("\t%24s", "Undefined "); |
---|
41 | break; |
---|
42 | default: |
---|
43 | break; |
---|
44 | } |
---|
45 | } |
---|
46 | } |
---|
47 | printf("\n"); |
---|
48 | printf("%-20s","Protocol"); |
---|
49 | for(i=0;i<4;i++){ |
---|
50 | if(!suppress[i]){ |
---|
51 | printf("\t%12s\t%12s", "bytes","packets"); |
---|
52 | } |
---|
53 | } |
---|
54 | printf("\n"); |
---|
55 | } |
---|
56 | |
---|
57 | void protocol_report(void) |
---|
58 | { |
---|
59 | int i,j; |
---|
60 | printf("# Protocol breakdown:\n"); |
---|
61 | protocol_suppress(); |
---|
62 | setprotoent(1); |
---|
63 | for(i=0;i<256;++i) { |
---|
64 | struct protoent *prot; |
---|
65 | if (prot_stat[0][i].count==0 && |
---|
66 | prot_stat[1][i].count==0 && prot_stat[2][i].count==0) |
---|
67 | continue; |
---|
68 | prot = getprotobynumber(i); |
---|
69 | if (prot) { |
---|
70 | printf("%20s",prot->p_name); |
---|
71 | for(j=0;j<4;j++){ |
---|
72 | if (prot_stat[j][i].count==0){ |
---|
73 | if(!suppress[j]) |
---|
74 | printf("\t%24s"," "); |
---|
75 | continue; |
---|
76 | } |
---|
77 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
78 | prot_stat[j][i].bytes, |
---|
79 | prot_stat[j][i].count); |
---|
80 | } |
---|
81 | } |
---|
82 | else { |
---|
83 | printf("%20i:",i); |
---|
84 | for(j=0;j<4;j++){ |
---|
85 | if (prot_stat[j][i].count==0){ |
---|
86 | if(!suppress[j]) |
---|
87 | printf("\t%24s"," "); |
---|
88 | continue; |
---|
89 | } |
---|
90 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
91 | prot_stat[j][i].bytes, |
---|
92 | prot_stat[j][i].count); |
---|
93 | } |
---|
94 | } |
---|
95 | printf("\n"); |
---|
96 | } |
---|
97 | setprotoent(0); |
---|
98 | } |
---|