1 | #include <netdb.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <string.h> |
---|
6 | #include "libtrace.h" |
---|
7 | #include "tracereport.h" |
---|
8 | #include "contain.h" |
---|
9 | |
---|
10 | CMP(cmp_int,int,a-b) |
---|
11 | MAP(int,MAP(int,stat_t)) protocol_tree = MAP_INIT(cmp_int); |
---|
12 | |
---|
13 | |
---|
14 | void port_per_packet(struct libtrace_packet_t *packet) |
---|
15 | { |
---|
16 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
17 | if (!ip) |
---|
18 | return; |
---|
19 | |
---|
20 | int port = trace_get_server_port(ip->ip_p, |
---|
21 | trace_get_source_port(packet), |
---|
22 | trace_get_destination_port(packet))==USE_SOURCE |
---|
23 | ? trace_get_source_port(packet) |
---|
24 | : trace_get_destination_port(packet); |
---|
25 | |
---|
26 | |
---|
27 | if (!MAP_FIND(protocol_tree,ip->ip_p)) { |
---|
28 | MAP_INSERT(protocol_tree,ip->ip_p,MAP_INIT(cmp_int)); |
---|
29 | } |
---|
30 | |
---|
31 | if (!MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)) { |
---|
32 | MAP_INSERT(MAP_FIND(protocol_tree,ip->ip_p)->value,port,{0}); |
---|
33 | } |
---|
34 | |
---|
35 | ++MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)->value.count; |
---|
36 | MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)->value.bytes+=trace_get_wire_length(packet); |
---|
37 | } |
---|
38 | |
---|
39 | static MAP_VISITOR(port_visitor,int,stat_t) |
---|
40 | { |
---|
41 | struct servent *ent = getservbyport(htons(node->key),(char *)userdata); |
---|
42 | if(ent) |
---|
43 | printf("%20s:\t%12" PRIu64 "\t%12" PRIu64 "\n", |
---|
44 | ent->s_name, |
---|
45 | node->value.bytes, |
---|
46 | node->value.count |
---|
47 | ); |
---|
48 | else |
---|
49 | printf("%20i:\t%12" PRIu64 "\t%12" PRIu64 "\n", |
---|
50 | node->key, |
---|
51 | node->value.bytes, |
---|
52 | node->value.count |
---|
53 | ); |
---|
54 | } |
---|
55 | |
---|
56 | static MAP_VISITOR(protocol_visitor,int,MAP(int,stat_t)) |
---|
57 | { |
---|
58 | struct protoent *ent = getprotobynumber(node->key); |
---|
59 | printf("Protocol: %i %s%s%s\n",node->key, |
---|
60 | ent?"(":"",ent?ent->p_name:"",ent?")":""); |
---|
61 | MAP_VISIT(node->value,NULL,port_visitor,NULL,(void*)(ent?ent->p_name:"")); |
---|
62 | } |
---|
63 | |
---|
64 | void port_report(void) |
---|
65 | { |
---|
66 | printf("# Port breakdown:\n"); |
---|
67 | printf("%-20s \t%12s\t%12s\n","Port","Bytes","Packets"); |
---|
68 | setservent(1); |
---|
69 | setprotoent(1); |
---|
70 | MAP_VISIT(protocol_tree,NULL,protocol_visitor,NULL,NULL); |
---|
71 | endprotoent(); |
---|
72 | endservent(); |
---|
73 | } |
---|