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