foo="\windows\style\pathname"; printf "$foo" isn't going to work too well in the...
[obnox/wireshark/wip.git] / mergecap.c
1 /* Combine two dump files, either by appending or by merging by timestamp
2  *
3  * $Id$
4  *
5  * Written by Scott Renfro <scott@renfro.org> based on
6  * editcap by Richard Sharpe and Guy Harris
7  *
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <glib.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <string.h>
28 #include "wtap.h"
29
30 #ifdef HAVE_GETOPT_H
31 #include <getopt.h>
32 #else
33 #include "wsutil/wsgetopt.h"
34 #endif
35
36 #include "svnversion.h"
37 #include "merge.h"
38 #include "wsutil/file_util.h"
39
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif
43
44 #ifdef _WIN32
45 #include <windows.h>
46 #include <shellapi.h>
47 #endif /* _WIN32 */
48
49 static int
50 get_natural_int(const char *string, const char *name)
51 {
52   int number;
53   char *p;
54
55   number = (int) strtol(string, &p, 10);
56   if (p == string || *p != '\0') {
57     fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
58             name, string);
59     exit(1);
60   }
61   if (number < 0) {
62     fprintf(stderr, "mergecap: The specified %s is a negative number\n",
63             name);
64     exit(1);
65   }
66   if (number > INT_MAX) {
67     fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
68             name, INT_MAX);
69     exit(1);
70   }
71   return number;
72 }
73
74 static int
75 get_positive_int(const char *string, const char *name)
76 {
77   int number;
78
79   number = get_natural_int(string, name);
80
81   if (number == 0) {
82     fprintf(stderr, "mergecap: The specified %s is zero\n",
83             name);
84     exit(1);
85   }
86
87   return number;
88 }
89
90 /*
91  * Show the usage
92  */
93 static void
94 usage(void)
95 {
96
97   fprintf(stderr, "Mergecap %s"
98 #ifdef SVNVERSION
99           " (" SVNVERSION " from " SVNPATH ")"
100 #endif
101           "\n", VERSION);
102   fprintf(stderr, "Merge two or more capture files into one.\n");
103   fprintf(stderr, "See http://www.wireshark.org for more information.\n");
104   fprintf(stderr, "\n");
105   fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> ...\n");
106   fprintf(stderr, "\n");
107   fprintf(stderr, "Output:\n");
108   fprintf(stderr, "  -a                concatenate rather than merge files.\n");
109   fprintf(stderr, "                    default is to merge based on frame timestamps.\n");
110   fprintf(stderr, "  -s <snaplen>      truncate packets to <snaplen> bytes of data.\n");
111   fprintf(stderr, "  -w <outfile>|-    set the output filename to <outfile> or '-' for stdout.\n");
112   fprintf(stderr, "  -F <capture type> set the output file type; default is libpcap.\n");
113   fprintf(stderr, "                    an empty \"-F\" option will list the file types.\n");
114   fprintf(stderr, "  -T <encap type>   set the output file encapsulation type;\n");
115   fprintf(stderr, "                    default is the same as the first input file.\n");
116   fprintf(stderr, "                    an empty \"-T\" option will list the encapsulation types.\n");
117   fprintf(stderr, "\n");
118   fprintf(stderr, "Miscellaneous:\n");
119   fprintf(stderr, "  -h                display this help and exit.\n");
120   fprintf(stderr, "  -v                verbose output.\n");
121 }
122
123 static void list_capture_types(void) {
124     int i;
125
126     fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
127     for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
128       if (wtap_dump_can_open(i))
129         fprintf(stderr, "    %s - %s\n",
130           wtap_file_type_short_string(i), wtap_file_type_string(i));
131     }
132 }
133
134 static void list_encap_types(void) {
135     int i;
136     const char *string;
137
138     fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
139     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
140         string = wtap_encap_short_string(i);
141         if (string != NULL)
142           fprintf(stderr, "    %s - %s\n",
143             string, wtap_encap_string(i));
144     }
145 }
146
147 int
148 main(int argc, char *argv[])
149 {
150   int          opt;
151
152 #ifdef _WIN32
153   LPWSTR              *wc_argv;
154   int                  wc_argc;
155 #endif  /* _WIN32 */
156
157   gboolean     do_append     = FALSE;
158   gboolean     verbose       = FALSE;
159   int          in_file_count = 0;
160   guint        snaplen = 0;
161   int          file_type = WTAP_FILE_PCAP;      /* default to libpcap format */
162   int          frame_type = -2;
163   int          out_fd;
164   merge_in_file_t   *in_files      = NULL;
165   int          i;
166   wtap        *wth;
167   struct wtap_pkthdr *phdr, snap_phdr;
168   wtap_dumper *pdh;
169   int          open_err, read_err, write_err, close_err;
170   gchar       *err_info;
171   int          err_fileno;
172   char        *out_filename = NULL;
173   gboolean     got_read_error = FALSE, got_write_error = FALSE;
174   int          count;
175
176 #ifdef _WIN32
177   /* Convert our arg list to UTF-8. */
178   wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc);
179   if (wc_argv && wc_argc == argc) {
180     for (i = 0; i < argc; i++) {
181       argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
182     }
183   } /* XXX else bail because something is horribly, horribly wrong? */
184 #endif /* _WIN32 */
185
186   /* Process the options first */
187   while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
188
189     switch (opt) {
190     case 'w':
191       out_filename = optarg;
192       break;
193
194     case 'a':
195       do_append = !do_append;
196       break;
197
198     case 'T':
199       frame_type = wtap_short_string_to_encap(optarg);
200       if (frame_type < 0) {
201         fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
202             optarg);
203         list_encap_types();
204         exit(1);
205       }
206       break;
207
208     case 'F':
209       file_type = wtap_short_string_to_file_type(optarg);
210       if (file_type < 0) {
211         fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
212             optarg);
213         list_capture_types();
214         exit(1);
215       }
216       break;
217
218     case 'v':
219       verbose = TRUE;
220       break;
221
222     case 's':
223       snaplen = get_positive_int(optarg, "snapshot length");
224       break;
225
226     case 'h':
227       usage();
228       exit(0);
229       break;
230
231     case '?':              /* Bad options if GNU getopt */
232       switch(optopt) {
233       case'F':
234         list_capture_types();
235         break;
236       case'T':
237         list_encap_types();
238         break;
239       default:
240         usage();
241       }
242       exit(1);
243       break;
244
245     }
246
247   }
248
249   /* check for proper args; at a minimum, must have an output
250    * filename and one input file
251    */
252   in_file_count = argc - optind;
253   if (!out_filename) {
254     fprintf(stderr, "mergecap: an output filename must be set with -w\n");
255     fprintf(stderr, "          run with -h for help\n");
256     return 1;
257   }
258   if (in_file_count < 1) {
259     fprintf(stderr, "mergecap: No input files were specified\n");
260     return 1;
261   }
262
263   /* open the input files */
264   if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
265                            &open_err, &err_info, &err_fileno)) {
266     fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
267         wtap_strerror(open_err));
268     switch (open_err) {
269
270     case WTAP_ERR_UNSUPPORTED:
271     case WTAP_ERR_UNSUPPORTED_ENCAP:
272     case WTAP_ERR_BAD_RECORD:
273       fprintf(stderr, "(%s)\n", err_info);
274       g_free(err_info);
275       break;
276     }
277     return 2;
278   }
279
280   if (verbose) {
281     for (i = 0; i < in_file_count; i++)
282       fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
283               wtap_file_type_string(wtap_file_type(in_files[i].wth)));
284   }
285
286   if (snaplen == 0) {
287     /*
288      * Snapshot length not specified - default to the maximum of the
289      * snapshot lengths of the input files.
290      */
291     snaplen = merge_max_snapshot_length(in_file_count, in_files);
292   }
293
294   /* set the outfile frame type */
295   if (frame_type == -2) {
296     /*
297      * Default to the appropriate frame type for the input files.
298      */
299     frame_type = merge_select_frame_type(in_file_count, in_files);
300     if (verbose) {
301       if (frame_type == WTAP_ENCAP_PER_PACKET) {
302         /*
303          * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
304          */
305         int first_frame_type, this_frame_type;
306
307         first_frame_type = wtap_file_encap(in_files[0].wth);
308         for (i = 1; i < in_file_count; i++) {
309           this_frame_type = wtap_file_encap(in_files[i].wth);
310           if (first_frame_type != this_frame_type) {
311             fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
312             fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
313             fprintf(stderr, "          %s had type %s (%s)\n",
314                     in_files[0].filename,
315                     wtap_encap_string(first_frame_type),
316                     wtap_encap_short_string(first_frame_type));
317             fprintf(stderr, "          %s had type %s (%s)\n",
318                     in_files[i].filename,
319                     wtap_encap_string(this_frame_type),
320                     wtap_encap_short_string(this_frame_type));
321             break;
322           }
323         }
324       }
325       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
326               wtap_encap_string(frame_type),
327               wtap_encap_short_string(frame_type));
328     }
329   }
330
331   /* open the outfile */
332   if (strncmp(out_filename, "-", 2) == 0) {
333     /* use stdout as the outfile */
334     out_fd = 1 /*stdout*/;
335   } else {
336     /* open the outfile */
337     out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
338     if (out_fd == -1) {
339       fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
340               out_filename, strerror(errno));
341       exit(1);
342     }
343   }
344
345   /* prepare the outfile */
346   pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
347   if (pdh == NULL) {
348     merge_close_in_files(in_file_count, in_files);
349     g_free(in_files);
350     fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
351             wtap_strerror(open_err));
352     exit(1);
353   }
354
355   /* do the merge (or append) */
356   count = 1;
357   for (;;) {
358     if (do_append)
359       wth = merge_append_read_packet(in_file_count, in_files, &read_err,
360                                      &err_info);
361     else
362       wth = merge_read_packet(in_file_count, in_files, &read_err,
363                               &err_info);
364     if (wth == NULL) {
365       if (read_err != 0)
366         got_read_error = TRUE;
367       break;
368     }
369
370     if (verbose)
371       fprintf(stderr, "Record: %u\n", count++);
372
373     /* We simply write it, perhaps after truncating it; we could do other
374      * things, like modify it. */
375     phdr = wtap_phdr(wth);
376     if (snaplen != 0 && phdr->caplen > snaplen) {
377       snap_phdr = *phdr;
378       snap_phdr.caplen = snaplen;
379       phdr = &snap_phdr;
380     }
381
382     if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
383          wtap_buf_ptr(wth), &write_err)) {
384       got_write_error = TRUE;
385       break;
386     }
387   }
388
389   merge_close_in_files(in_file_count, in_files);
390   if (!got_read_error && !got_write_error) {
391     if (!wtap_dump_close(pdh, &write_err))
392       got_write_error = TRUE;
393   } else
394     wtap_dump_close(pdh, &close_err);
395
396   if (got_read_error) {
397     /*
398      * Find the file on which we got the error, and report the error.
399      */
400     for (i = 0; i < in_file_count; i++) {
401       if (in_files[i].state == GOT_ERROR) {
402         fprintf(stderr, "mergecap: Error reading %s: %s\n",
403                 in_files[i].filename, wtap_strerror(read_err));
404         switch (read_err) {
405
406         case WTAP_ERR_UNSUPPORTED:
407         case WTAP_ERR_UNSUPPORTED_ENCAP:
408         case WTAP_ERR_BAD_RECORD:
409           fprintf(stderr, "(%s)\n", err_info);
410           g_free(err_info);
411           break;
412         }
413       }
414     }
415   }
416
417   if (got_write_error) {
418     fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
419             wtap_strerror(write_err));
420   }
421
422   g_free(in_files);
423
424   return (!got_read_error && !got_write_error) ? 0 : 2;
425 }