source: tools/tracereport/port_report.c @ 49ce177

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

Added:

  • error reports
  • flow reports
  • per port reports
  • direction report
  • moved everything to use the container library
  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[d3ff1fb]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"
[49ce177]8#include "contain.h"
[d3ff1fb]9
[49ce177]10CMP(cmp_int,int,a-b)
11MAP(int,MAP(int,stat_t)) protocol_tree = MAP_INIT(cmp_int);
[d3ff1fb]12
13
14void 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
[49ce177]27        if (!MAP_FIND(protocol_tree,ip->ip_p)) {
28                MAP_INSERT(protocol_tree,ip->ip_p,MAP_INIT(cmp_int));
[d3ff1fb]29        }
[49ce177]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});
[d3ff1fb]33        }
[49ce177]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);
[d3ff1fb]37}
38
[49ce177]39static MAP_VISITOR(port_visitor,int,stat_t)
[d3ff1fb]40{
[49ce177]41        struct servent *ent = getservbyport(htons(node->key),(char *)userdata);
[d3ff1fb]42        if(ent)
43                printf("%20s:\t%12" PRIu64 "\t%12" PRIu64 "\n",
44                                ent->s_name,
[49ce177]45                                node->value.bytes,
46                                node->value.count
[d3ff1fb]47                      );
48        else
49                printf("%20i:\t%12" PRIu64 "\t%12" PRIu64 "\n",
[49ce177]50                                node->key,
51                                node->value.bytes,
52                                node->value.count
[d3ff1fb]53                      );
54}
55
[49ce177]56static MAP_VISITOR(protocol_visitor,int,MAP(int,stat_t))
[d3ff1fb]57{
[49ce177]58        struct protoent *ent = getprotobynumber(node->key);
59        printf("Protocol: %i %s%s%s\n",node->key,
[d3ff1fb]60                        ent?"(":"",ent?ent->p_name:"",ent?")":"");
[49ce177]61        MAP_VISIT(node->value,NULL,port_visitor,NULL,(void*)(ent?ent->p_name:""));
[d3ff1fb]62}
63
64void 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);
[49ce177]70        MAP_VISIT(protocol_tree,NULL,protocol_visitor,NULL,NULL);
[d3ff1fb]71        endprotoent();
72        endservent();
73}
Note: See TracBrowser for help on using the repository browser.