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