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[256][65536]={{{0,0}}}; |
---|
12 | char protn[256]={0}; |
---|
13 | |
---|
14 | void port_per_packet(struct libtrace_packet_t *packet) |
---|
15 | { |
---|
16 | uint8_t proto; |
---|
17 | int port; |
---|
18 | |
---|
19 | if(trace_get_transport(packet,&proto,NULL)==NULL) |
---|
20 | return; |
---|
21 | |
---|
22 | port = trace_get_server_port(proto, |
---|
23 | trace_get_source_port(packet), |
---|
24 | trace_get_destination_port(packet))==USE_SOURCE |
---|
25 | ? trace_get_source_port(packet) |
---|
26 | : trace_get_destination_port(packet); |
---|
27 | |
---|
28 | ports[proto][port].bytes+=trace_get_wire_length(packet); |
---|
29 | ports[proto][port].count++; |
---|
30 | protn[proto]=1; |
---|
31 | } |
---|
32 | |
---|
33 | void port_port(int i,char *prot, int j) |
---|
34 | { |
---|
35 | struct servent *ent = getservbyport(htons(j),prot); |
---|
36 | if(ent) |
---|
37 | printf("%20s:\t%12" PRIu64 "\t%12" PRIu64 "\n", |
---|
38 | ent->s_name, |
---|
39 | ports[i][j].bytes, |
---|
40 | ports[i][j].count |
---|
41 | ); |
---|
42 | else |
---|
43 | printf("%20i:\t%12" PRIu64 "\t%12" PRIu64 "\n", |
---|
44 | j, |
---|
45 | ports[i][j].bytes, |
---|
46 | ports[i][j].count |
---|
47 | ); |
---|
48 | } |
---|
49 | |
---|
50 | void port_protocol(int i) |
---|
51 | { |
---|
52 | int j; |
---|
53 | struct protoent *ent = getprotobynumber(i); |
---|
54 | printf("Protocol: %i %s%s%s\n",i, |
---|
55 | ent?"(":"",ent?ent->p_name:"",ent?")":""); |
---|
56 | for(j=0;j<65536;++j) { |
---|
57 | if (ports[i][j].count) { |
---|
58 | port_port(i,ent?ent->p_name:"",j); |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | void port_report(void) |
---|
64 | { |
---|
65 | int i; |
---|
66 | printf("# Port breakdown:\n"); |
---|
67 | printf("%-20s \t%12s\t%12s\n","Port","Bytes","Packets"); |
---|
68 | setservent(1); |
---|
69 | setprotoent(1); |
---|
70 | for(i=0;i<256;++i) { |
---|
71 | if (protn[i]) { |
---|
72 | port_protocol(i); |
---|
73 | } |
---|
74 | } |
---|
75 | endprotoent(); |
---|
76 | endservent(); |
---|
77 | } |
---|