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