| 1 | #include <libtrace.h> |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #ifdef HAVE_INTTYPES_H |
|---|
| 5 | # include <inttypes.h> |
|---|
| 6 | #endif |
|---|
| 7 | #include <stdbool.h> |
|---|
| 8 | #include <getopt.h> |
|---|
| 9 | #include <string.h> |
|---|
| 10 | |
|---|
| 11 | #ifndef UINT64_MAX |
|---|
| 12 | # if __WORDSIZE == 64 |
|---|
| 13 | # define UINT64_MAX 18446744073709551615UL |
|---|
| 14 | # else |
|---|
| 15 | # define UINT64_MAX 18446744073709551615ULL |
|---|
| 16 | # endif |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | char *strdupcat(char *str,char *app) |
|---|
| 20 | { |
|---|
| 21 | str=realloc(str,strlen(str)+strlen(app)+1); |
|---|
| 22 | strcat(str,app); |
|---|
| 23 | return str; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | char *strdupcati(char *str,int i) |
|---|
| 27 | { |
|---|
| 28 | char buffer[64]; |
|---|
| 29 | snprintf(buffer,sizeof(buffer),"%i",i); |
|---|
| 30 | return strdupcat(str,buffer); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | int usage(char *argv) |
|---|
| 34 | { |
|---|
| 35 | printf("Usage: %s inputurl [ -c count ] [ -f bpffilter ] [ -b bytes ]\n\t[ -s starttime ] [ -e endtime ] [ -i interval ] outputurl\n",argv); |
|---|
| 36 | printf("\n"); |
|---|
| 37 | printf("Splits up traces\n"); |
|---|
| 38 | printf("-c count split every count packets\n"); |
|---|
| 39 | printf("-f bpffilter only output packets that match filter\n"); |
|---|
| 40 | printf("-b bytes split every capture bytes\n"); |
|---|
| 41 | printf("-s time start at starttime\n"); |
|---|
| 42 | printf("-e time end at endtime\n"); |
|---|
| 43 | printf("-i seconds create a new trace every <seconds>\n"); |
|---|
| 44 | printf("\n"); |
|---|
| 45 | exit(1); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | int main(int argc, char *argv[]) |
|---|
| 49 | { |
|---|
| 50 | struct libtrace_filter_t *filter=NULL; |
|---|
| 51 | struct libtrace_out_t *output = NULL; |
|---|
| 52 | struct libtrace_t *input; |
|---|
| 53 | uint64_t count=UINT64_MAX; |
|---|
| 54 | uint64_t bytes=UINT64_MAX; |
|---|
| 55 | uint64_t starttime=0; |
|---|
| 56 | uint64_t endtime=UINT64_MAX; |
|---|
| 57 | uint64_t interval=UINT64_MAX; |
|---|
| 58 | double firsttime=0; |
|---|
| 59 | uint64_t pktcount=0; |
|---|
| 60 | uint64_t totbytes=0; |
|---|
| 61 | uint64_t totbyteslast=0; |
|---|
| 62 | |
|---|
| 63 | if (argc<2) { |
|---|
| 64 | usage(argv[0]); |
|---|
| 65 | return 1; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /* Parse command line options */ |
|---|
| 69 | while(1) { |
|---|
| 70 | int option_index; |
|---|
| 71 | struct option long_options[] = { |
|---|
| 72 | { "filter", 1, 0, 'f' }, |
|---|
| 73 | { "count", 1, 0, 'c' }, |
|---|
| 74 | { "bytes", 1, 0, 'b' }, |
|---|
| 75 | { "starttime", 1, 0, 's' }, |
|---|
| 76 | { "endtime", 1, 0, 'e' }, |
|---|
| 77 | { "interval", 1, 0, 'i' }, |
|---|
| 78 | { NULL, 0, 0, 0 }, |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | int c=getopt_long(argc, argv, "f:c:b:s:e:i:", |
|---|
| 82 | long_options, &option_index); |
|---|
| 83 | |
|---|
| 84 | if (c==-1) |
|---|
| 85 | break; |
|---|
| 86 | |
|---|
| 87 | switch (c) { |
|---|
| 88 | case 'f': filter=trace_bpf_setfilter(optarg); |
|---|
| 89 | break; |
|---|
| 90 | case 'c': count=atoi(optarg); |
|---|
| 91 | break; |
|---|
| 92 | case 'b': bytes=atoi(optarg); |
|---|
| 93 | break; |
|---|
| 94 | case 's': starttime=atoi(optarg); /* FIXME: use getdate */ |
|---|
| 95 | break; |
|---|
| 96 | case 'e': endtime=atoi(optarg); |
|---|
| 97 | break; |
|---|
| 98 | case 'i': interval=atoi(optarg); |
|---|
| 99 | break; |
|---|
| 100 | default: |
|---|
| 101 | fprintf(stderr,"Unknown option: %c\n",c); |
|---|
| 102 | usage(argv[0]); |
|---|
| 103 | return 1; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | if (optind+2<argc) { |
|---|
| 107 | fprintf(stderr,"missing inputuri or outputuri\n"); |
|---|
| 108 | usage(argv[0]); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | output=NULL; |
|---|
| 112 | input=trace_create(argv[optind]); |
|---|
| 113 | |
|---|
| 114 | while(1) { |
|---|
| 115 | struct libtrace_packet_t packet; |
|---|
| 116 | if (trace_read_packet(input,&packet)<1) { |
|---|
| 117 | break; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | if (filter && !trace_bpf_filter(filter,&packet)) { |
|---|
| 122 | continue; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if (trace_get_seconds(&packet)<starttime) { |
|---|
| 126 | continue; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | if (trace_get_seconds(&packet)>endtime) { |
|---|
| 130 | break; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (firsttime==0) { |
|---|
| 134 | firsttime=trace_get_seconds(&packet); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | if (output && trace_get_seconds(&packet)>firsttime+interval) { |
|---|
| 138 | trace_output_destroy(output); |
|---|
| 139 | output=NULL; |
|---|
| 140 | firsttime+=interval; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | pktcount++; |
|---|
| 144 | if (output && pktcount%count==0) { |
|---|
| 145 | trace_output_destroy(output); |
|---|
| 146 | output=NULL; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | totbytes+=trace_get_capture_length(&packet); |
|---|
| 150 | if (output && totbytes-totbyteslast>=bytes) { |
|---|
| 151 | trace_output_destroy(output); |
|---|
| 152 | output=NULL; |
|---|
| 153 | totbyteslast=totbytes; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if (!output) { |
|---|
| 157 | char *buffer; |
|---|
| 158 | buffer=strdup(argv[optind+1]); |
|---|
| 159 | if (interval!=UINT64_MAX) { |
|---|
| 160 | buffer=strdupcat(buffer,"-"); |
|---|
| 161 | buffer=strdupcati(buffer,firsttime); |
|---|
| 162 | } |
|---|
| 163 | if (count!=UINT64_MAX) { |
|---|
| 164 | buffer=strdupcat(buffer,"-"); |
|---|
| 165 | buffer=strdupcati(buffer,pktcount); |
|---|
| 166 | } |
|---|
| 167 | if (bytes!=UINT64_MAX) { |
|---|
| 168 | static int filenum=0; |
|---|
| 169 | buffer=strdupcat(buffer,"-"); |
|---|
| 170 | buffer=strdupcati(buffer,++filenum); |
|---|
| 171 | } |
|---|
| 172 | output=trace_output_create(buffer); |
|---|
| 173 | free(buffer); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | trace_write_packet(output,&packet); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | if (!output) |
|---|
| 180 | trace_output_destroy(output); |
|---|
| 181 | |
|---|
| 182 | return 0; |
|---|
| 183 | } |
|---|