Changeset 814


Ignore:
Timestamp:
05/23/06 13:23:49 (7 years ago)
Author:
perry
Message:

Simplify the code, and move to being IPv{4,6} independant

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/tracereport/port_report.c

    r642 r814  
    99#include "contain.h" 
    1010 
    11 CMP(cmp_int,int,a-b) 
    12 MAP(int,MAP(int,stat_t)) protocol_tree = MAP_INIT(cmp_int); 
    13  
     11stat_t ports[256][65536]={{{0,0}}}; 
     12char protn[256]={0}; 
    1413 
    1514void port_per_packet(struct libtrace_packet_t *packet) 
    1615{ 
    17         struct libtrace_ip *ip = trace_get_ip(packet); 
     16        uint8_t proto; 
    1817        int port; 
    19         if (!ip) 
     18 
     19        if(trace_get_transport(packet,&proto,NULL)==NULL)  
    2020                return; 
    2121 
    22         port = trace_get_server_port(ip->ip_p, 
     22        port = trace_get_server_port(proto, 
    2323                        trace_get_source_port(packet), 
    2424                        trace_get_destination_port(packet))==USE_SOURCE 
     
    2626                : trace_get_destination_port(packet); 
    2727 
    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); 
     28        ports[proto][port].bytes+=trace_get_wire_length(packet); 
     29        ports[proto][port].count++; 
     30        protn[proto]=1; 
    3931} 
    4032 
    41 static MAP_VISITOR(port_visitor,int,stat_t) 
     33void port_port(int i,char *prot, int j) 
    4234{ 
    43         struct servent *ent = getservbyport(htons(node->key),(char *)userdata); 
     35        struct servent *ent = getservbyport(htons(j),prot); 
    4436        if(ent) 
    4537                printf("%20s:\t%12" PRIu64 "\t%12" PRIu64 "\n", 
    4638                                ent->s_name, 
    47                                 node->value.bytes, 
    48                                 node->value.count 
     39                                ports[i][j].bytes, 
     40                                ports[i][j].count 
    4941                      ); 
    5042        else 
    5143                printf("%20i:\t%12" PRIu64 "\t%12" PRIu64 "\n", 
    52                                 node->key, 
    53                                 node->value.bytes, 
    54                                 node->value.count 
     44                                j, 
     45                                ports[i][j].bytes, 
     46                                ports[i][j].count 
    5547                      ); 
    5648} 
    5749 
    58 static MAP_VISITOR(protocol_visitor,int,MAP(int,stat_t)) 
     50void port_protocol(int i) 
    5951{ 
    60         struct protoent *ent = getprotobynumber(node->key); 
    61         printf("Protocol: %i %s%s%s\n",node->key, 
     52        int j; 
     53        struct protoent *ent = getprotobynumber(i); 
     54        printf("Protocol: %i %s%s%s\n",i, 
    6255                        ent?"(":"",ent?ent->p_name:"",ent?")":""); 
    63         MAP_VISIT(node->value,NULL,port_visitor,NULL,(void*)(ent?ent->p_name:"")); 
     56        for(j=0;j<65536;++j) { 
     57                if (ports[i][j].count) { 
     58                        port_port(i,ent?ent->p_name:"",j); 
     59                } 
     60        } 
    6461} 
    6562 
    6663void port_report(void) 
    6764{ 
     65        int i; 
    6866        printf("# Port breakdown:\n"); 
    6967        printf("%-20s \t%12s\t%12s\n","Port","Bytes","Packets"); 
    7068        setservent(1); 
    7169        setprotoent(1); 
    72         MAP_VISIT(protocol_tree,NULL,protocol_visitor,NULL,NULL); 
     70        for(i=0;i<256;++i) { 
     71                if (protn[i]) { 
     72                        port_protocol(i); 
     73                } 
     74        } 
    7375        endprotoent(); 
    7476        endservent(); 
Note: See TracChangeset for help on using the changeset viewer.