| 1 | #include <netdb.h> |
|---|
| 2 | #include <inttypes.h> |
|---|
| 3 | #include <lt_inttypes.h> |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | #include "libtrace.h" |
|---|
| 6 | #include "tracereport.h" |
|---|
| 7 | |
|---|
| 8 | static stat_t tcpopt_stat[4][256] = {{{0,0}}}; |
|---|
| 9 | /* Suppressing things seems a little pointless to me */ |
|---|
| 10 | static bool suppress[4] = {true,true,true,true}; |
|---|
| 11 | |
|---|
| 12 | void tcpopt_per_packet(struct libtrace_packet_t *packet) |
|---|
| 13 | { |
|---|
| 14 | struct libtrace_tcp *tcp = trace_get_tcp(packet); |
|---|
| 15 | unsigned char *opt_ptr; |
|---|
| 16 | libtrace_direction_t dir = trace_get_direction(packet); |
|---|
| 17 | int tcp_payload, len; |
|---|
| 18 | unsigned char type, optlen, *data; |
|---|
| 19 | |
|---|
| 20 | if(!tcp) |
|---|
| 21 | return; |
|---|
| 22 | |
|---|
| 23 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
|---|
| 24 | dir = TRACE_DIR_OTHER; |
|---|
| 25 | |
|---|
| 26 | len = tcp->doff * 4 - sizeof(libtrace_tcp_t); |
|---|
| 27 | if(len == 0) |
|---|
| 28 | return; |
|---|
| 29 | |
|---|
| 30 | tcp_payload = trace_get_wire_length(packet) - trace_get_capture_length(packet); |
|---|
| 31 | |
|---|
| 32 | opt_ptr = (unsigned char *)tcp + sizeof (libtrace_tcp_t); |
|---|
| 33 | |
|---|
| 34 | while(trace_get_next_option(&opt_ptr,&len,&type,&optlen,&data)){ |
|---|
| 35 | /* I don't think we need to count NO-OPs */ |
|---|
| 36 | if (type == 1) |
|---|
| 37 | continue; |
|---|
| 38 | tcpopt_stat[dir][type].count++; |
|---|
| 39 | tcpopt_stat[dir][type].bytes+= tcp_payload; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | suppress[dir] = false; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void tcpopt_suppress() |
|---|
| 46 | { |
|---|
| 47 | int i; |
|---|
| 48 | printf("%-20s","Direction:"); |
|---|
| 49 | for(i=0;i<4;i++){ |
|---|
| 50 | if(!suppress[i]){ |
|---|
| 51 | switch(i){ |
|---|
| 52 | case 0: |
|---|
| 53 | printf("\t%24s", "Outbound "); |
|---|
| 54 | break; |
|---|
| 55 | case 1: |
|---|
| 56 | printf("\t%24s", "Inbound "); |
|---|
| 57 | break; |
|---|
| 58 | case 2: |
|---|
| 59 | printf("\t%24s", "Undefined "); |
|---|
| 60 | break; |
|---|
| 61 | default: |
|---|
| 62 | break; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | printf("\n"); |
|---|
| 67 | printf("%-20s","TCP OPTIONS"); |
|---|
| 68 | for(i=0;i<4;i++){ |
|---|
| 69 | if(!suppress[i]){ |
|---|
| 70 | printf("\t%12s\t%12s", "bytes","packets"); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | printf("\n"); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void tcpopt_report(void) |
|---|
| 77 | { |
|---|
| 78 | int i,j; |
|---|
| 79 | printf("# TCP OPTION breakdown:\n"); |
|---|
| 80 | tcpopt_suppress(); |
|---|
| 81 | |
|---|
| 82 | for(i=0;i<256;++i) { |
|---|
| 83 | if (tcpopt_stat[0][i].count==0 && |
|---|
| 84 | tcpopt_stat[1][i].count==0 && tcpopt_stat[2][i].count==0) |
|---|
| 85 | continue; |
|---|
| 86 | |
|---|
| 87 | switch(i) { |
|---|
| 88 | case 1: |
|---|
| 89 | printf("%20s", "NOP: "); |
|---|
| 90 | break; |
|---|
| 91 | case 2: |
|---|
| 92 | printf("%20s", "MSS: "); |
|---|
| 93 | break; |
|---|
| 94 | case 3: |
|---|
| 95 | printf("%20s", "Winscale: "); |
|---|
| 96 | break; |
|---|
| 97 | case 4: |
|---|
| 98 | printf("%20s", "SACK Permitted: "); |
|---|
| 99 | break; |
|---|
| 100 | case 5: |
|---|
| 101 | printf("%20s", "SACK Information: "); |
|---|
| 102 | break; |
|---|
| 103 | case 8: |
|---|
| 104 | printf("%20s", "Timestamp: "); |
|---|
| 105 | break; |
|---|
| 106 | case 19: |
|---|
| 107 | printf("%20s", "MD5: "); |
|---|
| 108 | default: |
|---|
| 109 | printf("%20i:",i); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | for(j=0;j<4;j++){ |
|---|
| 113 | if (tcpopt_stat[j][i].count==0){ |
|---|
| 114 | if(!suppress[j]) |
|---|
| 115 | printf("\t%24s"," "); |
|---|
| 116 | continue; |
|---|
| 117 | } |
|---|
| 118 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
|---|
| 119 | tcpopt_stat[j][i].bytes, |
|---|
| 120 | tcpopt_stat[j][i].count); |
|---|
| 121 | } |
|---|
| 122 | printf("\n"); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|