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_bpf_setfilter(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 | |
---|
108 | trace_start(input); |
---|
109 | |
---|
110 | while(1) { |
---|
111 | if (trace_read_packet(input,packet)<1) { |
---|
112 | break; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | if (filter && !trace_bpf_filter(filter,packet)) { |
---|
117 | continue; |
---|
118 | } |
---|
119 | |
---|
120 | if (trace_get_seconds(packet)<starttime) { |
---|
121 | continue; |
---|
122 | } |
---|
123 | |
---|
124 | if (trace_get_seconds(packet)>endtime) { |
---|
125 | break; |
---|
126 | } |
---|
127 | |
---|
128 | if (firsttime==0) { |
---|
129 | firsttime=trace_get_seconds(packet); |
---|
130 | } |
---|
131 | |
---|
132 | if (output && trace_get_seconds(packet)>firsttime+interval) { |
---|
133 | trace_destroy_output(output); |
---|
134 | output=NULL; |
---|
135 | firsttime+=interval; |
---|
136 | } |
---|
137 | |
---|
138 | pktcount++; |
---|
139 | if (output && pktcount%count==0) { |
---|
140 | trace_destroy_output(output); |
---|
141 | output=NULL; |
---|
142 | } |
---|
143 | |
---|
144 | totbytes+=trace_get_capture_length(packet); |
---|
145 | if (output && totbytes-totbyteslast>=bytes) { |
---|
146 | trace_destroy_output(output); |
---|
147 | output=NULL; |
---|
148 | totbyteslast=totbytes; |
---|
149 | } |
---|
150 | |
---|
151 | if (!output) { |
---|
152 | char *buffer; |
---|
153 | buffer=strdup(argv[optind+1]); |
---|
154 | if (interval!=UINT64_MAX) { |
---|
155 | buffer=strdupcat(buffer,"-"); |
---|
156 | buffer=strdupcati(buffer,firsttime); |
---|
157 | } |
---|
158 | if (count!=UINT64_MAX) { |
---|
159 | buffer=strdupcat(buffer,"-"); |
---|
160 | buffer=strdupcati(buffer,pktcount); |
---|
161 | } |
---|
162 | if (bytes!=UINT64_MAX) { |
---|
163 | static int filenum=0; |
---|
164 | buffer=strdupcat(buffer,"-"); |
---|
165 | buffer=strdupcati(buffer,++filenum); |
---|
166 | } |
---|
167 | output=trace_create_output(buffer); |
---|
168 | trace_start_output(output); |
---|
169 | free(buffer); |
---|
170 | } |
---|
171 | |
---|
172 | if (trace_write_packet(output,packet)==-1) { |
---|
173 | trace_perror_output(output,"write_packet"); |
---|
174 | break; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | if (!output) |
---|
179 | trace_destroy_output(output); |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|