| 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 *argv0) |
|---|
| 27 | { |
|---|
| 28 | printf("Usage:\n" |
|---|
| 29 | "%s flags inputuri outputuri\n" |
|---|
| 30 | "-f --filter=bpf only output packets that match filter\n" |
|---|
| 31 | "-c --count=n split every n packets\n" |
|---|
| 32 | "-b --bytes=n Split every n bytes received\n" |
|---|
| 33 | "-i --interval=n Split every n seconds\n" |
|---|
| 34 | "-s --starttime=time Start at time\n" |
|---|
| 35 | "-e --endtime=time End at time\n" |
|---|
| 36 | "-H --libtrace-help Print libtrace runtime documentation\n" |
|---|
| 37 | ,argv0); |
|---|
| 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 | { "libtrace-help", 0, 0, 'H' }, |
|---|
| 73 | { NULL, 0, 0, 0 }, |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | int c=getopt_long(argc, argv, "f:c:b:s:e:i:H", |
|---|
| 77 | long_options, &option_index); |
|---|
| 78 | |
|---|
| 79 | if (c==-1) |
|---|
| 80 | break; |
|---|
| 81 | |
|---|
| 82 | switch (c) { |
|---|
| 83 | case 'f': filter=trace_create_filter(optarg); |
|---|
| 84 | break; |
|---|
| 85 | case 'c': count=atoi(optarg); |
|---|
| 86 | break; |
|---|
| 87 | case 'b': bytes=atoi(optarg); |
|---|
| 88 | break; |
|---|
| 89 | case 's': starttime=atoi(optarg); /* FIXME: use getdate */ |
|---|
| 90 | break; |
|---|
| 91 | case 'e': endtime=atoi(optarg); |
|---|
| 92 | break; |
|---|
| 93 | case 'i': interval=atoi(optarg); |
|---|
| 94 | break; |
|---|
| 95 | case 'H': |
|---|
| 96 | trace_help(); |
|---|
| 97 | exit(1); |
|---|
| 98 | break; |
|---|
| 99 | default: |
|---|
| 100 | fprintf(stderr,"Unknown option: %c\n",c); |
|---|
| 101 | usage(argv[0]); |
|---|
| 102 | return 1; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | if (optind+2<argc) { |
|---|
| 106 | fprintf(stderr,"missing inputuri or outputuri\n"); |
|---|
| 107 | usage(argv[0]); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | output=NULL; |
|---|
| 111 | input=trace_create(argv[optind]); |
|---|
| 112 | if (trace_is_err(input)) { |
|---|
| 113 | trace_perror(input,""); |
|---|
| 114 | return 1; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | if (trace_start(input)==-1) { |
|---|
| 118 | trace_perror(input,""); |
|---|
| 119 | return 1; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | while(1) { |
|---|
| 123 | if (trace_read_packet(input,packet)<1) { |
|---|
| 124 | break; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | if (filter && !trace_apply_filter(filter,packet)) { |
|---|
| 129 | continue; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if (trace_get_seconds(packet)<starttime) { |
|---|
| 133 | continue; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | if (trace_get_seconds(packet)>endtime) { |
|---|
| 137 | break; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | if (firsttime==0) { |
|---|
| 141 | firsttime=trace_get_seconds(packet); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | if (output && trace_get_seconds(packet)>firsttime+interval) { |
|---|
| 145 | trace_destroy_output(output); |
|---|
| 146 | output=NULL; |
|---|
| 147 | firsttime+=interval; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | if (output && pktcount%count==0) { |
|---|
| 151 | trace_destroy_output(output); |
|---|
| 152 | output=NULL; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | pktcount++; |
|---|
| 156 | totbytes+=trace_get_capture_length(packet); |
|---|
| 157 | if (output && totbytes-totbyteslast>=bytes) { |
|---|
| 158 | trace_destroy_output(output); |
|---|
| 159 | output=NULL; |
|---|
| 160 | totbyteslast=totbytes; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | if (!output) { |
|---|
| 164 | char *buffer; |
|---|
| 165 | buffer=strdup(argv[optind+1]); |
|---|
| 166 | if (interval!=UINT64_MAX) { |
|---|
| 167 | buffer=strdupcat(buffer,"-"); |
|---|
| 168 | buffer=strdupcati(buffer,firsttime); |
|---|
| 169 | } |
|---|
| 170 | if (count!=UINT64_MAX) { |
|---|
| 171 | buffer=strdupcat(buffer,"-"); |
|---|
| 172 | buffer=strdupcati(buffer,pktcount); |
|---|
| 173 | } |
|---|
| 174 | if (bytes!=UINT64_MAX) { |
|---|
| 175 | static int filenum=0; |
|---|
| 176 | buffer=strdupcat(buffer,"-"); |
|---|
| 177 | buffer=strdupcati(buffer,++filenum); |
|---|
| 178 | } |
|---|
| 179 | output=trace_create_output(buffer); |
|---|
| 180 | trace_start_output(output); |
|---|
| 181 | free(buffer); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /* Some traces we have are padded (usually with 0x00), so |
|---|
| 185 | * lets sort that out now and truncate them properly |
|---|
| 186 | */ |
|---|
| 187 | |
|---|
| 188 | if (trace_get_capture_length(packet) |
|---|
| 189 | > trace_get_wire_length(packet)) { |
|---|
| 190 | trace_set_capture_length(packet,trace_get_wire_length(packet)); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | if (trace_write_packet(output,packet)==-1) { |
|---|
| 194 | trace_perror_output(output,"write_packet"); |
|---|
| 195 | break; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | if (!output) |
|---|
| 200 | trace_destroy_output(output); |
|---|
| 201 | |
|---|
| 202 | trace_destroy_packet(packet); |
|---|
| 203 | |
|---|
| 204 | return 0; |
|---|
| 205 | } |
|---|