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