1 | #include <netdb.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <lt_inttypes.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <string.h> |
---|
7 | #include "libtrace.h" |
---|
8 | #include "tracereport.h" |
---|
9 | #include "contain.h" |
---|
10 | |
---|
11 | stat_t ports[3][256][65536]={{{{0,0}}}}; |
---|
12 | char protn[256]={0}; |
---|
13 | static bool suppress[3] = {true,true,true}; |
---|
14 | |
---|
15 | void port_per_packet(struct libtrace_packet_t *packet) |
---|
16 | { |
---|
17 | uint8_t proto; |
---|
18 | int port; |
---|
19 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
20 | |
---|
21 | if(trace_get_transport(packet,&proto,NULL)==NULL) |
---|
22 | return; |
---|
23 | |
---|
24 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
25 | dir = TRACE_DIR_OTHER; |
---|
26 | |
---|
27 | port = trace_get_server_port(proto, |
---|
28 | trace_get_source_port(packet), |
---|
29 | trace_get_destination_port(packet))==USE_SOURCE |
---|
30 | ? trace_get_source_port(packet) |
---|
31 | : trace_get_destination_port(packet); |
---|
32 | |
---|
33 | ports[dir][proto][port].bytes+=trace_get_wire_length(packet); |
---|
34 | ports[dir][proto][port].count++; |
---|
35 | protn[proto]=1; |
---|
36 | suppress[dir] = false; |
---|
37 | } |
---|
38 | |
---|
39 | void port_suppress() |
---|
40 | { |
---|
41 | int i; |
---|
42 | printf("%-20s","Direction:"); |
---|
43 | for(i=0;i<3;i++){ |
---|
44 | if(!suppress[i]){ |
---|
45 | switch(i){ |
---|
46 | case 0: |
---|
47 | printf("\t%24s", "Outbound "); |
---|
48 | break; |
---|
49 | case 1: |
---|
50 | printf("\t%24s", "Inbound "); |
---|
51 | break; |
---|
52 | case 2: |
---|
53 | printf("\t%24s", "Undefined "); |
---|
54 | break; |
---|
55 | default: |
---|
56 | break; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | printf("\n"); |
---|
61 | printf("%-20s","Port"); |
---|
62 | for(i=0;i<3;i++){ |
---|
63 | if(!suppress[i]){ |
---|
64 | printf("\t%12s\t%12s", "bytes","packets"); |
---|
65 | } |
---|
66 | } |
---|
67 | printf("\n"); |
---|
68 | } |
---|
69 | |
---|
70 | void port_port(int i,char *prot, int j) |
---|
71 | { |
---|
72 | struct servent *ent = getservbyport(htons(j),prot); |
---|
73 | int k; |
---|
74 | |
---|
75 | if(ent){ |
---|
76 | printf("%20s:",ent->s_name); |
---|
77 | for(k=0;k<3;k++){ |
---|
78 | if (ports[k][i][j].count==0){ |
---|
79 | if(!suppress[k]) |
---|
80 | printf("\t%24s"," "); |
---|
81 | continue; |
---|
82 | } |
---|
83 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
84 | ports[k][i][j].bytes, |
---|
85 | ports[k][i][j].count |
---|
86 | ); |
---|
87 | } |
---|
88 | } |
---|
89 | else{ |
---|
90 | printf("%20i:",j); |
---|
91 | for(k=0;k<3;k++){ |
---|
92 | if (ports[k][i][j].count==0){ |
---|
93 | if(!suppress[k]) |
---|
94 | printf("\t%24s"," "); |
---|
95 | continue; |
---|
96 | } |
---|
97 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
98 | ports[k][i][j].bytes, |
---|
99 | ports[k][i][j].count |
---|
100 | ); |
---|
101 | } |
---|
102 | } |
---|
103 | printf("\n"); |
---|
104 | } |
---|
105 | |
---|
106 | void port_protocol(int i) |
---|
107 | { |
---|
108 | int j,k; |
---|
109 | struct protoent *ent = getprotobynumber(i); |
---|
110 | printf("Protocol: %i %s%s%s\n",i, |
---|
111 | ent?"(":"",ent?ent->p_name:"",ent?")":""); |
---|
112 | for(j=0;j<65536;++j) { |
---|
113 | for(k=0;k<3;k++){ |
---|
114 | if (ports[k][i][j].count) { |
---|
115 | port_port(i,ent?ent->p_name:"",j); |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void port_report(void) |
---|
123 | { |
---|
124 | int i; |
---|
125 | printf("# Port breakdown:\n"); |
---|
126 | port_suppress(); |
---|
127 | setservent(1); |
---|
128 | setprotoent(1); |
---|
129 | for(i=0;i<256;++i) { |
---|
130 | if (protn[i]) { |
---|
131 | port_protocol(i); |
---|
132 | } |
---|
133 | } |
---|
134 | endprotoent(); |
---|
135 | endservent(); |
---|
136 | } |
---|