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