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 | |
---|
42 | void port_port(int i,char *prot, int j, FILE *out) |
---|
43 | { |
---|
44 | struct servent *ent = getservbyport(htons(j),prot); |
---|
45 | int k; |
---|
46 | |
---|
47 | if(ent){ |
---|
48 | fprintf(out,"%16s:",ent->s_name); |
---|
49 | } |
---|
50 | else{ |
---|
51 | fprintf(out,"%16i:",j); |
---|
52 | } |
---|
53 | |
---|
54 | for (k = 0; k < 3; k++) { |
---|
55 | if (!ports[k][i]) |
---|
56 | continue; |
---|
57 | if (k != 0) { |
---|
58 | fprintf(out, "%16s", " "); |
---|
59 | } |
---|
60 | switch (k) { |
---|
61 | case 0: |
---|
62 | fprintf(out, "\t%10s", "Outbound"); |
---|
63 | break; |
---|
64 | case 1: |
---|
65 | fprintf(out, "\t%10s", "Inbound"); |
---|
66 | break; |
---|
67 | case 2: |
---|
68 | fprintf(out, "\t%10s", "Unknown"); |
---|
69 | break; |
---|
70 | } |
---|
71 | fprintf(out, "\t%16" PRIu64 " %16" PRIu64 "\n", |
---|
72 | ports[k][i][j].bytes, |
---|
73 | ports[k][i][j].count); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void port_protocol(int i, FILE *out) |
---|
78 | { |
---|
79 | int j,k; |
---|
80 | struct protoent *ent = getprotobynumber(i); |
---|
81 | fprintf(out, "Protocol: %i %s%s%s\n",i, |
---|
82 | ent?"(":"",ent?ent->p_name:"",ent?")":""); |
---|
83 | for(j=0;j<65536;++j) { |
---|
84 | for(k=0;k<3;k++){ |
---|
85 | if (ports[k][i] && ports[k][i][j].count) { |
---|
86 | port_port(i,ent?ent->p_name:"",j, out); |
---|
87 | break; |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void port_report(void) |
---|
94 | { |
---|
95 | int i; |
---|
96 | FILE *out = fopen("ports.rpt", "w"); |
---|
97 | if (!out) { |
---|
98 | perror("fopen"); |
---|
99 | return; |
---|
100 | } |
---|
101 | fprintf(out, "%-16s\t%10s\t%16s %16s\n", |
---|
102 | "PORT", |
---|
103 | "DIRECTION", |
---|
104 | "BYTES", |
---|
105 | "PACKETS"); |
---|
106 | |
---|
107 | setservent(1); |
---|
108 | setprotoent(1); |
---|
109 | for(i=0;i<256;++i) { |
---|
110 | if (protn[i]) { |
---|
111 | port_protocol(i, out); |
---|
112 | free(ports[0][i]); |
---|
113 | free(ports[1][i]); |
---|
114 | free(ports[2][i]); |
---|
115 | } |
---|
116 | } |
---|
117 | endprotoent(); |
---|
118 | endservent(); |
---|
119 | fclose(out); |
---|
120 | } |
---|