source: tools/tracereport/port_report.c @ 5843707

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 5843707 was e3b0188, checked in by Daniel Lawson <dlawson@…>, 16 years ago

a lot of changes for cross-compilation support

  • header ordering
  • os-specific headers
  • an lt_inttypes.h which defines PRIu64 etc for platforms that don't have them
  • removed daglegacy.h, merged with dagformat.h
  • 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        if (!ip)
19                return;
20
21        int port = trace_get_server_port(ip->ip_p,
22                        trace_get_source_port(packet),
23                        trace_get_destination_port(packet))==USE_SOURCE
24                ? trace_get_source_port(packet)
25                : trace_get_destination_port(packet);
26
27
28        if (!MAP_FIND(protocol_tree,ip->ip_p)) {
29                MAP_INSERT(protocol_tree,ip->ip_p,MAP_INIT(cmp_int));
30        }
31
32        if (!MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)) {
33                MAP_INSERT(MAP_FIND(protocol_tree,ip->ip_p)->value,port,{0});
34        }
35
36        ++MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)->value.count;
37        MAP_FIND(MAP_FIND(protocol_tree,ip->ip_p)->value,port)->value.bytes+=trace_get_wire_length(packet);
38}
39
40static MAP_VISITOR(port_visitor,int,stat_t)
41{
42        struct servent *ent = getservbyport(htons(node->key),(char *)userdata);
43        if(ent)
44                printf("%20s:\t%12" PRIu64 "\t%12" PRIu64 "\n",
45                                ent->s_name,
46                                node->value.bytes,
47                                node->value.count
48                      );
49        else
50                printf("%20i:\t%12" PRIu64 "\t%12" PRIu64 "\n",
51                                node->key,
52                                node->value.bytes,
53                                node->value.count
54                      );
55}
56
57static MAP_VISITOR(protocol_visitor,int,MAP(int,stat_t))
58{
59        struct protoent *ent = getprotobynumber(node->key);
60        printf("Protocol: %i %s%s%s\n",node->key,
61                        ent?"(":"",ent?ent->p_name:"",ent?")":"");
62        MAP_VISIT(node->value,NULL,port_visitor,NULL,(void*)(ent?ent->p_name:""));
63}
64
65void port_report(void)
66{
67        printf("# Port breakdown:\n");
68        printf("%-20s \t%12s\t%12s\n","Port","Bytes","Packets");
69        setservent(1);
70        setprotoent(1);
71        MAP_VISIT(protocol_tree,NULL,protocol_visitor,NULL,NULL);
72        endprotoent();
73        endservent();
74}
Note: See TracBrowser for help on using the repository browser.