[dca0810] | 1 | #include <stdio.h> |
---|
[043d632] | 2 | #include <assert.h> |
---|
[dca0810] | 3 | #include "libtrace.h" |
---|
| 4 | #include <map> |
---|
| 5 | |
---|
| 6 | struct libtrace_packet_t packet; |
---|
| 7 | int psize; |
---|
| 8 | |
---|
| 9 | /** parse an option |
---|
| 10 | * @param ptr the pointer to the current option |
---|
| 11 | * @param plen the length of the remaining buffer |
---|
| 12 | * @param type the type of the option |
---|
| 13 | * @param optlen the length of the option |
---|
| 14 | * @param data the data of the option |
---|
| 15 | * |
---|
| 16 | * @returns bool true if there is another option (and the fields are filled in) |
---|
| 17 | */ |
---|
| 18 | int get_next_option(unsigned char **ptr,int *len, |
---|
| 19 | unsigned char *type, |
---|
| 20 | unsigned char *optlen, |
---|
| 21 | unsigned char **data) |
---|
| 22 | { |
---|
| 23 | if (*len<=0) { |
---|
| 24 | // printf("Missing End of Options\n"); |
---|
| 25 | return 0; |
---|
| 26 | } |
---|
| 27 | *type=**ptr; |
---|
| 28 | switch(*type) { |
---|
| 29 | case 0: /* End of options */ |
---|
| 30 | // printf("End of option\n"); |
---|
| 31 | return 0; |
---|
| 32 | case 1: /* Pad */ |
---|
| 33 | (*ptr)++; |
---|
| 34 | (*len)--; |
---|
| 35 | return 1; |
---|
| 36 | default: |
---|
| 37 | case 6: // ECHO (obsolete) |
---|
| 38 | case 7: // ECHO Reply (obsolete) |
---|
| 39 | case 9: // Partial ordering |
---|
| 40 | case 10: // Partial ordering service profile |
---|
| 41 | case 11: // CC |
---|
| 42 | case 13: // CC.ECHO |
---|
| 43 | case 14: // Alternative checksum request |
---|
| 44 | case 15: // Alternative checksum data |
---|
| 45 | case 16: // Skeeter |
---|
| 46 | case 17: // Bubba |
---|
| 47 | case 18: // Trailer checksum |
---|
| 48 | case 19: // Md5 signature |
---|
| 49 | case 20: // SCPS capability |
---|
| 50 | case 21: // Selective NACK |
---|
| 51 | case 22: // Record boundary |
---|
| 52 | case 23: // Corruption experienced |
---|
| 53 | case 24: // SNAP |
---|
| 54 | case 25: // Unassigned |
---|
| 55 | case 26: // TCP Compression filter |
---|
| 56 | printf("Unknown option type (%i)\n",*type); |
---|
| 57 | case 2: // MSS |
---|
| 58 | case 3: // WS |
---|
| 59 | case 4: // SACK permitted |
---|
| 60 | case 5: // SACK |
---|
| 61 | case 8: // Timestamp |
---|
| 62 | case 12: // CC.new |
---|
| 63 | *optlen = *(*ptr+1); |
---|
| 64 | if (*optlen<2) { |
---|
| 65 | printf("Optlen <2?! %i\n",*optlen); |
---|
| 66 | return 0; // I have no idea wtf is going on |
---|
| 67 | // with these packets |
---|
| 68 | } |
---|
| 69 | (*len)-=(unsigned int)*optlen; |
---|
| 70 | (*data)=(*ptr+2); |
---|
| 71 | (*ptr)+=*optlen; |
---|
| 72 | if (*len<0) { |
---|
| 73 | printf("Option longer than option area (%i > %i)\n",*optlen,*len+*optlen); |
---|
| 74 | return 0; |
---|
| 75 | } |
---|
| 76 | return 1; |
---|
| 77 | } |
---|
| 78 | assert(0); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | int main(int argc, char *argv[]) |
---|
| 82 | { |
---|
| 83 | struct libtrace_t *trace; |
---|
| 84 | double last = 0; |
---|
| 85 | |
---|
| 86 | trace = trace_create(argv[1]); |
---|
| 87 | |
---|
| 88 | for (;;) { |
---|
| 89 | int psize; |
---|
| 90 | |
---|
| 91 | if ((psize = trace_read_packet(trace, &packet)) <= 0) { |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | struct libtrace_ip *ipptr = trace_get_ip(&packet); |
---|
| 96 | |
---|
| 97 | if (!ipptr) |
---|
| 98 | continue; |
---|
| 99 | |
---|
| 100 | struct libtrace_tcp *tcpptr = trace_get_tcp(&packet); |
---|
| 101 | |
---|
| 102 | if (!tcpptr) |
---|
| 103 | continue; |
---|
| 104 | |
---|
| 105 | int plen = (tcpptr->doff*4-sizeof *tcpptr); |
---|
| 106 | int plen2; |
---|
| 107 | |
---|
| 108 | // The len of the section that isn't the tcp header |
---|
| 109 | plen2 = ((char*)tcpptr - (char*)packet.buffer); |
---|
| 110 | // Minus the length of the tcp header |
---|
| 111 | plen2 += sizeof *tcpptr; |
---|
| 112 | // Now, how much do we have left? |
---|
| 113 | plen2 = psize - plen2; |
---|
| 114 | |
---|
| 115 | if (plen != plen2) { |
---|
| 116 | printf("Snapped wrong (%i != %i)\n",plen,plen2); |
---|
| 117 | printf(" %i != %i\n",psize-plen,psize-plen2); |
---|
| 118 | printf("iph %i tcp->doff %i\n",ipptr->ip_hl,tcpptr->doff); |
---|
| 119 | printf("psize = %i totlen=%i\n",psize,htons(ipptr->ip_len)); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | double now=trace_get_seconds(&packet); |
---|
| 124 | |
---|
| 125 | if (now-last>60) { |
---|
| 126 | printf("."); |
---|
| 127 | fflush(stdout); |
---|
| 128 | last=now; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return 0; |
---|
| 134 | } |
---|