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 | |
---|
8 | void usage(char *argv0) |
---|
9 | { |
---|
10 | fprintf(stderr,"Usage:\n" |
---|
11 | "%s flags outputuri traceuri [traceuri...]\n" |
---|
12 | "-i --set-interface Each trace is allocated an interface. Default leaves this flag as read from the original traces, if appropriate\n" |
---|
13 | "-u --unique-packets Discard duplicate packets\n" |
---|
14 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
15 | ,argv0); |
---|
16 | exit(1); |
---|
17 | } |
---|
18 | |
---|
19 | int main(int argc, char *argv[]) |
---|
20 | { |
---|
21 | |
---|
22 | struct libtrace_out_t *output; |
---|
23 | struct libtrace_t **input; |
---|
24 | struct libtrace_packet_t **packet; |
---|
25 | bool *live; |
---|
26 | bool set_interface=false; |
---|
27 | bool unique_packets=false; |
---|
28 | int i=0; |
---|
29 | uint64_t last_ts=0; |
---|
30 | |
---|
31 | while (1) { |
---|
32 | int option_index; |
---|
33 | struct option long_options[] = { |
---|
34 | { "set-interface", 0, 0, 'i' }, |
---|
35 | { "unique-packets", 0, 0, 'u' }, |
---|
36 | { "libtrace-help", 0, 0, 'H' }, |
---|
37 | { NULL, 0, 0, 0 }, |
---|
38 | }; |
---|
39 | |
---|
40 | int c=getopt_long(argc, argv, "iuH", |
---|
41 | long_options, &option_index); |
---|
42 | |
---|
43 | if (c==-1) |
---|
44 | break; |
---|
45 | |
---|
46 | switch (c) { |
---|
47 | case 'i': set_interface=true; break; |
---|
48 | case 'u': unique_packets=true; break; |
---|
49 | case 'H': |
---|
50 | trace_help(); |
---|
51 | exit(1); |
---|
52 | break; |
---|
53 | default: |
---|
54 | fprintf(stderr,"unknown option: %c\n",c); |
---|
55 | usage(argv[0]); |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | if (optind+2>argc) |
---|
62 | usage(argv[0]); |
---|
63 | |
---|
64 | output=trace_create_output(argv[optind++]); |
---|
65 | if (trace_is_err_output(output)) { |
---|
66 | trace_perror_output(output,"trace_create_output"); |
---|
67 | return 1; |
---|
68 | } |
---|
69 | if (trace_start_output(output)==-1) { |
---|
70 | trace_perror_output(output,"trace_start_output"); |
---|
71 | return 1; |
---|
72 | } |
---|
73 | |
---|
74 | input=calloc((argc-optind),sizeof(struct libtrace_t *)); |
---|
75 | packet=calloc((argc-optind),sizeof(struct libtrace_packet_t *)); |
---|
76 | live=calloc((argc-optind),sizeof(bool)); |
---|
77 | for(i=0;i<argc-optind;++i) { |
---|
78 | libtrace_t *f; |
---|
79 | libtrace_packet_t *p; |
---|
80 | f=trace_create(argv[i+optind]); |
---|
81 | if (trace_is_err(f)) { |
---|
82 | trace_perror(f,"trace_create"); |
---|
83 | return 1; |
---|
84 | } |
---|
85 | if (trace_start(f)==-1) { |
---|
86 | trace_perror(f,"trace_start"); |
---|
87 | return 1; |
---|
88 | } |
---|
89 | p=trace_create_packet(); |
---|
90 | input[i]=f; |
---|
91 | packet[i]=p; |
---|
92 | if (trace_read_packet(f,packet[i])>0) |
---|
93 | live[i]=true; |
---|
94 | } |
---|
95 | |
---|
96 | while(1) { |
---|
97 | uint64_t oldest_ts=0; |
---|
98 | int oldest=-1; |
---|
99 | for(i=0;i<argc-optind;++i) { |
---|
100 | if (!live[i] && input[i]) { |
---|
101 | int ret=trace_read_packet(input[i],packet[i]); |
---|
102 | if (ret<0) { |
---|
103 | /* Error */ |
---|
104 | perror(argv[i+2]); |
---|
105 | trace_destroy(input[i]); |
---|
106 | input[i]=NULL; |
---|
107 | } |
---|
108 | else if (ret==0) { |
---|
109 | /* EOF */ |
---|
110 | trace_destroy(input[i]); |
---|
111 | input[i]=NULL; |
---|
112 | } |
---|
113 | else |
---|
114 | live[i]=true; |
---|
115 | } |
---|
116 | if (live[i] && |
---|
117 | (oldest==-1 || |
---|
118 | oldest_ts>trace_get_erf_timestamp(packet[i]))) { |
---|
119 | oldest=i; |
---|
120 | oldest_ts=trace_get_erf_timestamp(packet[i]); |
---|
121 | } |
---|
122 | } |
---|
123 | /* We have run out of packets! */ |
---|
124 | if (oldest==-1) { |
---|
125 | break; |
---|
126 | } |
---|
127 | |
---|
128 | live[oldest]=false; |
---|
129 | |
---|
130 | if (set_interface) |
---|
131 | trace_set_direction(packet[oldest],oldest); |
---|
132 | |
---|
133 | if (unique_packets && oldest_ts == last_ts) |
---|
134 | continue; |
---|
135 | |
---|
136 | if (trace_write_packet(output,packet[oldest]) < 0) { |
---|
137 | trace_perror_output(output, "trace_write_packet"); |
---|
138 | break; |
---|
139 | } |
---|
140 | |
---|
141 | last_ts=oldest_ts; |
---|
142 | |
---|
143 | } |
---|
144 | trace_destroy_output(output); |
---|
145 | |
---|
146 | return 0; |
---|
147 | } |
---|