| 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_create_output(argv[optind]); |
|---|
| 53 | if (trace_is_err_output(output)) { |
|---|
| 54 | trace_perror_output(output,"trace_create"); |
|---|
| 55 | return 1; |
|---|
| 56 | } |
|---|
| 57 | if (trace_start_output(output)==-1) { |
|---|
| 58 | trace_perror_output(output,"trace_start"); |
|---|
| 59 | return 1; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | input=calloc((argc-optind),sizeof(struct libtrace_t *)); |
|---|
| 63 | packet=calloc((argc-optind),sizeof(struct libtrace_packet_t *)); |
|---|
| 64 | live=calloc((argc-optind),sizeof(bool)); |
|---|
| 65 | for(i=0;i<argc-optind;++i) { |
|---|
| 66 | struct libtrace_t *f; |
|---|
| 67 | struct libtrace_packet_t *p; |
|---|
| 68 | f=trace_create(argv[i+optind]); |
|---|
| 69 | if (trace_is_err(f)) { |
|---|
| 70 | trace_perror(f,"trace_create"); |
|---|
| 71 | return 1; |
|---|
| 72 | } |
|---|
| 73 | if (trace_start(f)==-1) { |
|---|
| 74 | trace_perror(f,"trace_start"); |
|---|
| 75 | return 1; |
|---|
| 76 | } |
|---|
| 77 | p=trace_create_packet(); |
|---|
| 78 | input[i]=f; |
|---|
| 79 | packet[i]=p; |
|---|
| 80 | trace_read_packet(f,p); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | while(1) { |
|---|
| 84 | uint64_t oldest_ts=0; |
|---|
| 85 | int oldest=-1; |
|---|
| 86 | for(i=0;i<argc-2;++i) { |
|---|
| 87 | if (!live[i] && input[i]) { |
|---|
| 88 | int ret=trace_read_packet(input[i],packet[i]); |
|---|
| 89 | if (ret<0) { |
|---|
| 90 | /* Error */ |
|---|
| 91 | perror(argv[i+2]); |
|---|
| 92 | trace_destroy(input[i]); |
|---|
| 93 | input[i]=NULL; |
|---|
| 94 | } |
|---|
| 95 | else if (ret==0) { |
|---|
| 96 | /* EOF */ |
|---|
| 97 | trace_destroy(input[i]); |
|---|
| 98 | input[i]=NULL; |
|---|
| 99 | } |
|---|
| 100 | else |
|---|
| 101 | live[i]=true; |
|---|
| 102 | } |
|---|
| 103 | if (live[i] && |
|---|
| 104 | (oldest==-1 || |
|---|
| 105 | oldest_ts<trace_get_erf_timestamp(packet[i]))) { |
|---|
| 106 | oldest=i; |
|---|
| 107 | oldest_ts=trace_get_erf_timestamp(packet[i]); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | /* We have run out of packets! */ |
|---|
| 111 | if (oldest==-1) { |
|---|
| 112 | break; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if (set_interface) |
|---|
| 116 | trace_set_direction(packet[oldest],oldest); |
|---|
| 117 | trace_write_packet(output,packet[oldest]); |
|---|
| 118 | live[oldest]=false; |
|---|
| 119 | |
|---|
| 120 | } |
|---|
| 121 | trace_destroy_output(output); |
|---|
| 122 | |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|