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