source: tools/tracereport/port_report.c @ 548da5c

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 548da5c was c3e6e47, checked in by Perry Lorier <perry@…>, 16 years ago

Update to support libtrace3, also fix (minor) warnings where possible!

  • Property mode set to 100644
File size: 1.9 KB
Line 
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
11CMP(cmp_int,int,a-b)
12MAP(int,MAP(int,stat_t)) protocol_tree = MAP_INIT(cmp_int);
13
14
15void port_per_packet(struct libtrace_packet_t *packet)
16{
17        struct libtrace_ip *ip = trace_get_ip(packet);
18        int port;
19        if (!ip)
20                return;
21
22        port = trace_get_server_port(ip->ip_p,
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
29        if (!MAP_FIND(protocol_tree,ip->ip_p)) {
30                MAP_INSERT(protocol_tree,ip->ip_p,MAP_INIT(cmp_int));
31        }
32
33        if (!MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)) {
34                MAP_INSERT(MAP_FIND(protocol_tree,ip->ip_p)->value,port,{0});
35        }
36
37        ++MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)->value.count;
38        MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)->value.bytes+=trace_get_wire_length(packet);
39}
40
41static MAP_VISITOR(port_visitor,int,stat_t)
42{
43        struct servent *ent = getservbyport(htons(node->key),(char *)userdata);
44        if(ent)
45                printf("%20s:\t%12" PRIu64 "\t%12" PRIu64 "\n",
46                                ent->s_name,
47                                node->value.bytes,
48                                node->value.count
49                      );
50        else
51                printf("%20i:\t%12" PRIu64 "\t%12" PRIu64 "\n",
52                                node->key,
53                                node->value.bytes,
54                                node->value.count
55                      );
56}
57
58static MAP_VISITOR(protocol_visitor,int,MAP(int,stat_t))
59{
60        struct protoent *ent = getprotobynumber(node->key);
61        printf("Protocol: %i %s%s%s\n",node->key,
62                        ent?"(":"",ent?ent->p_name:"",ent?")":"");
63        MAP_VISIT(node->value,NULL,port_visitor,NULL,(void*)(ent?ent->p_name:""));
64}
65
66void port_report(void)
67{
68        printf("# Port breakdown:\n");
69        printf("%-20s \t%12s\t%12s\n","Port","Bytes","Packets");
70        setservent(1);
71        setprotoent(1);
72        MAP_VISIT(protocol_tree,NULL,protocol_visitor,NULL,NULL);
73        endprotoent();
74        endservent();
75}
Note: See TracBrowser for help on using the repository browser.