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] = {{NULL}}; |
---|
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 | if (!ports[dir][proto]) |
---|
34 | ports[dir][proto]=calloc(65536,sizeof(stat_t)); |
---|
35 | ports[dir][proto][port].bytes+=trace_get_wire_length(packet); |
---|
36 | ports[dir][proto][port].count++; |
---|
37 | protn[proto]=1; |
---|
38 | suppress[dir] = false; |
---|
39 | } |
---|
40 | |
---|
41 | void port_suppress() |
---|
42 | { |
---|
43 | int i; |
---|
44 | printf("%-20s","Direction:"); |
---|
45 | for(i=0;i<3;i++){ |
---|
46 | if(!suppress[i]){ |
---|
47 | switch(i){ |
---|
48 | case 0: |
---|
49 | printf("\t%24s", "Outbound "); |
---|
50 | break; |
---|
51 | case 1: |
---|
52 | printf("\t%24s", "Inbound "); |
---|
53 | break; |
---|
54 | case 2: |
---|
55 | printf("\t%24s", "Undefined "); |
---|
56 | break; |
---|
57 | default: |
---|
58 | break; |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | printf("\n"); |
---|
63 | printf("%-20s","Port"); |
---|
64 | for(i=0;i<3;i++){ |
---|
65 | if(!suppress[i]){ |
---|
66 | printf("\t%12s\t%12s", "bytes","packets"); |
---|
67 | } |
---|
68 | } |
---|
69 | printf("\n"); |
---|
70 | } |
---|
71 | |
---|
72 | void port_port(int i,char *prot, int j) |
---|
73 | { |
---|
74 | struct servent *ent = getservbyport(htons(j),prot); |
---|
75 | int k; |
---|
76 | |
---|
77 | if(ent){ |
---|
78 | printf("%20s:",ent->s_name); |
---|
79 | for(k=0;k<3;k++){ |
---|
80 | if (!ports[k][i] || ports[k][i][j].count==0){ |
---|
81 | if(!suppress[k]) |
---|
82 | printf("\t%24s"," "); |
---|
83 | continue; |
---|
84 | } |
---|
85 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
86 | ports[k][i][j].bytes, |
---|
87 | ports[k][i][j].count |
---|
88 | ); |
---|
89 | } |
---|
90 | } |
---|
91 | else{ |
---|
92 | printf("%20i:",j); |
---|
93 | for(k=0;k<3;k++){ |
---|
94 | if (!ports[k][i] || ports[k][i][j].count==0){ |
---|
95 | if(!suppress[k]) |
---|
96 | printf("\t%24s"," "); |
---|
97 | continue; |
---|
98 | } |
---|
99 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
100 | ports[k][i][j].bytes, |
---|
101 | ports[k][i][j].count |
---|
102 | ); |
---|
103 | } |
---|
104 | } |
---|
105 | printf("\n"); |
---|
106 | } |
---|
107 | |
---|
108 | void port_protocol(int i) |
---|
109 | { |
---|
110 | int j,k; |
---|
111 | struct protoent *ent = getprotobynumber(i); |
---|
112 | printf("Protocol: %i %s%s%s\n",i, |
---|
113 | ent?"(":"",ent?ent->p_name:"",ent?")":""); |
---|
114 | for(j=0;j<65536;++j) { |
---|
115 | for(k=0;k<3;k++){ |
---|
116 | if (ports[k][i] && ports[k][i][j].count) { |
---|
117 | port_port(i,ent?ent->p_name:"",j); |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | void port_report(void) |
---|
125 | { |
---|
126 | int i; |
---|
127 | printf("# Port breakdown:\n"); |
---|
128 | port_suppress(); |
---|
129 | setservent(1); |
---|
130 | setprotoent(1); |
---|
131 | for(i=0;i<256;++i) { |
---|
132 | if (protn[i]) { |
---|
133 | port_protocol(i); |
---|
134 | free(ports[0][i]); |
---|
135 | free(ports[1][i]); |
---|
136 | free(ports[2][i]); |
---|
137 | } |
---|
138 | } |
---|
139 | endprotoent(); |
---|
140 | endservent(); |
---|
141 | } |
---|