| 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 | |
|---|
| 11 | stat_t ports[4][256][65536]={{{{0,0}}}}; |
|---|
| 12 | char protn[256]={0}; |
|---|
| 13 | static bool suppress[4] = {true,true,true,true}; |
|---|
| 14 | |
|---|
| 15 | void port_per_packet(struct libtrace_packet_t *packet) |
|---|
| 16 | { |
|---|
| 17 | uint8_t proto; |
|---|
| 18 | int port; |
|---|
| 19 | int dir = trace_get_direction(packet); |
|---|
| 20 | if(dir < 0 || dir > 1) |
|---|
| 21 | dir = 2; |
|---|
| 22 | if(trace_get_transport(packet,&proto,NULL)==NULL) |
|---|
| 23 | return; |
|---|
| 24 | |
|---|
| 25 | port = trace_get_server_port(proto, |
|---|
| 26 | trace_get_source_port(packet), |
|---|
| 27 | trace_get_destination_port(packet))==USE_SOURCE |
|---|
| 28 | ? trace_get_source_port(packet) |
|---|
| 29 | : trace_get_destination_port(packet); |
|---|
| 30 | |
|---|
| 31 | ports[dir][proto][port].bytes+=trace_get_wire_length(packet); |
|---|
| 32 | ports[dir][proto][port].count++; |
|---|
| 33 | protn[proto]=1; |
|---|
| 34 | suppress[dir] = false; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | void port_suppress() |
|---|
| 38 | { |
|---|
| 39 | int i; |
|---|
| 40 | printf("%-20s","Direction:"); |
|---|
| 41 | for(i=0;i<4;i++){ |
|---|
| 42 | if(!suppress[i]){ |
|---|
| 43 | switch(i){ |
|---|
| 44 | case 0: |
|---|
| 45 | printf("\t%24s", "Outbound "); |
|---|
| 46 | break; |
|---|
| 47 | case 1: |
|---|
| 48 | printf("\t%24s", "Inbound "); |
|---|
| 49 | break; |
|---|
| 50 | case 2: |
|---|
| 51 | printf("\t%24s", "Undefined "); |
|---|
| 52 | break; |
|---|
| 53 | default: |
|---|
| 54 | break; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | printf("\n"); |
|---|
| 59 | printf("%-20s","Port"); |
|---|
| 60 | for(i=0;i<4;i++){ |
|---|
| 61 | if(!suppress[i]){ |
|---|
| 62 | printf("\t%12s\t%12s", "bytes","packets"); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | printf("\n"); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void port_port(int i,char *prot, int j) |
|---|
| 69 | { |
|---|
| 70 | struct servent *ent = getservbyport(htons(j),prot); |
|---|
| 71 | int k; |
|---|
| 72 | |
|---|
| 73 | if(ent){ |
|---|
| 74 | printf("%20s:",ent->s_name); |
|---|
| 75 | for(k=0;k<4;k++){ |
|---|
| 76 | if (ports[k][i][j].count==0){ |
|---|
| 77 | if(!suppress[k]) |
|---|
| 78 | printf("\t%24s"," "); |
|---|
| 79 | continue; |
|---|
| 80 | } |
|---|
| 81 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
|---|
| 82 | ports[k][i][j].bytes, |
|---|
| 83 | ports[k][i][j].count |
|---|
| 84 | ); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | else{ |
|---|
| 88 | printf("%20i:",j); |
|---|
| 89 | for(k=0;k<4;k++){ |
|---|
| 90 | if (ports[k][i][j].count==0){ |
|---|
| 91 | if(!suppress[k]) |
|---|
| 92 | printf("\t%24s"," "); |
|---|
| 93 | continue; |
|---|
| 94 | } |
|---|
| 95 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
|---|
| 96 | ports[k][i][j].bytes, |
|---|
| 97 | ports[k][i][j].count |
|---|
| 98 | ); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | printf("\n"); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void port_protocol(int i) |
|---|
| 105 | { |
|---|
| 106 | int j,k; |
|---|
| 107 | struct protoent *ent = getprotobynumber(i); |
|---|
| 108 | printf("Protocol: %i %s%s%s\n",i, |
|---|
| 109 | ent?"(":"",ent?ent->p_name:"",ent?")":""); |
|---|
| 110 | for(j=0;j<65536;++j) { |
|---|
| 111 | for(k=0;k<4;k++){ |
|---|
| 112 | if (ports[k][i][j].count) { |
|---|
| 113 | port_port(i,ent?ent->p_name:"",j); |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void port_report(void) |
|---|
| 121 | { |
|---|
| 122 | int i; |
|---|
| 123 | printf("# Port breakdown:\n"); |
|---|
| 124 | port_suppress(); |
|---|
| 125 | setservent(1); |
|---|
| 126 | setprotoent(1); |
|---|
| 127 | for(i=0;i<256;++i) { |
|---|
| 128 | if (protn[i]) { |
|---|
| 129 | port_protocol(i); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | endprotoent(); |
|---|
| 133 | endservent(); |
|---|
| 134 | } |
|---|