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 ] 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 | int i=0; |
---|
25 | |
---|
26 | while (1) { |
---|
27 | int option_index; |
---|
28 | struct option long_options[] = { |
---|
29 | { "set-interface", 0, 0, 'i' }, |
---|
30 | { NULL, 0, 0, 0 }, |
---|
31 | }; |
---|
32 | |
---|
33 | int c=getopt_long(argc, argv, "i:", |
---|
34 | long_options, &option_index); |
---|
35 | |
---|
36 | if (c==-1) |
---|
37 | break; |
---|
38 | |
---|
39 | switch (c) { |
---|
40 | case 'i': set_interface=true; break; |
---|
41 | default: |
---|
42 | fprintf(stderr,"unknown option: %c\n",c); |
---|
43 | usage(argv[0]); |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | } |
---|
48 | |
---|
49 | if (optind+2<argc) |
---|
50 | usage(argv[0]); |
---|
51 | |
---|
52 | output=trace_output_create(argv[optind]); |
---|
53 | if (!output) { |
---|
54 | fprintf(stderr,"Unable to open output file %s\n",argv[optind]); |
---|
55 | return 1; |
---|
56 | } |
---|
57 | |
---|
58 | input=calloc((argc-optind),sizeof(struct libtrace_t *)); |
---|
59 | packet=calloc((argc-optind),sizeof(struct libtrace_packet_t)); |
---|
60 | live=calloc((argc-optind),sizeof(bool)); |
---|
61 | for(i=0;i<argc-optind;++i) { |
---|
62 | struct libtrace_t *f; |
---|
63 | struct libtrace_packet_t p; |
---|
64 | f=trace_create(argv[i+optind]); |
---|
65 | input[i]=f; |
---|
66 | if (!input[i]) { |
---|
67 | fprintf(stderr,"Could not read %s\n",argv[i+optind]); |
---|
68 | return 1; |
---|
69 | } |
---|
70 | trace_read_packet(f,&p); |
---|
71 | } |
---|
72 | |
---|
73 | while(1) { |
---|
74 | uint64_t oldest_ts=0; |
---|
75 | int oldest=-1; |
---|
76 | for(i=0;i<argc-2;++i) { |
---|
77 | if (!live[i] && input[i]) { |
---|
78 | int ret=trace_read_packet(input[i],&packet[i]); |
---|
79 | if (ret<0) { |
---|
80 | /* Error */ |
---|
81 | perror(argv[i+2]); |
---|
82 | trace_destroy(input[i]); |
---|
83 | input[i]=NULL; |
---|
84 | } |
---|
85 | else if (ret==0) { |
---|
86 | /* EOF */ |
---|
87 | trace_destroy(input[i]); |
---|
88 | input[i]=NULL; |
---|
89 | } |
---|
90 | else |
---|
91 | live[i]=true; |
---|
92 | } |
---|
93 | if (live[i] && |
---|
94 | (oldest==-1 || |
---|
95 | oldest_ts<trace_get_erf_timestamp(&packet[i]))) { |
---|
96 | oldest=i; |
---|
97 | oldest_ts=trace_get_erf_timestamp(&packet[i]); |
---|
98 | } |
---|
99 | } |
---|
100 | /* We have run out of packets! */ |
---|
101 | if (oldest==-1) { |
---|
102 | break; |
---|
103 | } |
---|
104 | |
---|
105 | if (set_interface) |
---|
106 | trace_set_direction(&packet[oldest],oldest); |
---|
107 | trace_write_packet(output,&packet[oldest]); |
---|
108 | live[oldest]=false; |
---|
109 | |
---|
110 | } |
---|
111 | trace_output_destroy(output); |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|