99a9cf26f2d4c4151e29766d0d0910de735cfd20
[metze/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;
161   int          i;
162   wtap        *wth;
163   struct wtap_pkthdr *phdr, snap_phdr;
164   wtap_dumper *pdh;
165   int          open_err, read_err, write_err, close_err;
166   gchar       *err_info;
167   int          err_fileno;
168   char        *out_filename = NULL;
169   gboolean     got_read_error = FALSE, got_write_error = FALSE;
170   int          count;
171
172 #ifdef _WIN32
173   arg_list_utf_16to8(argc, argv);
174 #endif /* _WIN32 */
175
176   /* Process the options first */
177   while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
178
179     switch (opt) {
180     case 'w':
181       out_filename = optarg;
182       break;
183
184     case 'a':
185       do_append = !do_append;
186       break;
187
188     case 'T':
189       frame_type = wtap_short_string_to_encap(optarg);
190       if (frame_type < 0) {
191         fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
192             optarg);
193         list_encap_types();
194         exit(1);
195       }
196       break;
197
198     case 'F':
199       file_type = wtap_short_string_to_file_type(optarg);
200       if (file_type < 0) {
201         fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
202             optarg);
203         list_capture_types();
204         exit(1);
205       }
206       break;
207
208     case 'v':
209       verbose = TRUE;
210       break;
211
212     case 's':
213       snaplen = get_positive_int(optarg, "snapshot length");
214       break;
215
216     case 'h':
217       usage();
218       exit(0);
219       break;
220
221     case '?':              /* Bad options if GNU getopt */
222       switch(optopt) {
223       case'F':
224         list_capture_types();
225         break;
226       case'T':
227         list_encap_types();
228         break;
229       default:
230         usage();
231       }
232       exit(1);
233       break;
234
235     }
236
237   }
238
239   /* check for proper args; at a minimum, must have an output
240    * filename and one input file
241    */
242   in_file_count = argc - optind;
243   if (!out_filename) {
244     fprintf(stderr, "mergecap: an output filename must be set with -w\n");
245     fprintf(stderr, "          run with -h for help\n");
246     return 1;
247   }
248   if (in_file_count < 1) {
249     fprintf(stderr, "mergecap: No input files were specified\n");
250     return 1;
251   }
252
253   /* open the input files */
254   if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
255                            &open_err, &err_info, &err_fileno)) {
256     fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
257         wtap_strerror(open_err));
258     switch (open_err) {
259
260     case WTAP_ERR_UNSUPPORTED:
261     case WTAP_ERR_UNSUPPORTED_ENCAP:
262     case WTAP_ERR_BAD_RECORD:
263       fprintf(stderr, "(%s)\n", err_info);
264       g_free(err_info);
265       break;
266     }
267     return 2;
268   }
269
270   if (verbose) {
271     for (i = 0; i < in_file_count; i++)
272       fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
273               wtap_file_type_string(wtap_file_type(in_files[i].wth)));
274   }
275
276   if (snaplen == 0) {
277     /*
278      * Snapshot length not specified - default to the maximum of the
279      * snapshot lengths of the input files.
280      */
281     snaplen = merge_max_snapshot_length(in_file_count, in_files);
282   }
283
284   /* set the outfile frame type */
285   if (frame_type == -2) {
286     /*
287      * Default to the appropriate frame type for the input files.
288      */
289     frame_type = merge_select_frame_type(in_file_count, in_files);
290     if (verbose) {
291       if (frame_type == WTAP_ENCAP_PER_PACKET) {
292         /*
293          * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
294          */
295         int first_frame_type, this_frame_type;
296
297         first_frame_type = wtap_file_encap(in_files[0].wth);
298         for (i = 1; i < in_file_count; i++) {
299           this_frame_type = wtap_file_encap(in_files[i].wth);
300           if (first_frame_type != this_frame_type) {
301             fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
302             fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
303             fprintf(stderr, "          %s had type %s (%s)\n",
304                     in_files[0].filename,
305                     wtap_encap_string(first_frame_type),
306                     wtap_encap_short_string(first_frame_type));
307             fprintf(stderr, "          %s had type %s (%s)\n",
308                     in_files[i].filename,
309                     wtap_encap_string(this_frame_type),
310                     wtap_encap_short_string(this_frame_type));
311             break;
312           }
313         }
314       }
315       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
316               wtap_encap_string(frame_type),
317               wtap_encap_short_string(frame_type));
318     }
319   }
320
321   /* open the outfile */
322   if (strncmp(out_filename, "-", 2) == 0) {
323     /* use stdout as the outfile */
324     out_fd = 1 /*stdout*/;
325   } else {
326     /* open the outfile */
327     out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
328     if (out_fd == -1) {
329       fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
330               out_filename, g_strerror(errno));
331       exit(1);
332     }
333   }
334
335   /* prepare the outfile */
336   pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
337   if (pdh == NULL) {
338     merge_close_in_files(in_file_count, in_files);
339     g_free(in_files);
340     fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
341             wtap_strerror(open_err));
342     exit(1);
343   }
344
345   /* do the merge (or append) */
346   count = 1;
347   for (;;) {
348     if (do_append)
349       wth = merge_append_read_packet(in_file_count, in_files, &read_err,
350                                      &err_info);
351     else
352       wth = merge_read_packet(in_file_count, in_files, &read_err,
353                               &err_info);
354     if (wth == NULL) {
355       if (read_err != 0)
356         got_read_error = TRUE;
357       break;
358     }
359
360     if (verbose)
361       fprintf(stderr, "Record: %u\n", count++);
362
363     /* We simply write it, perhaps after truncating it; we could do other
364      * things, like modify it. */
365     phdr = wtap_phdr(wth);
366     if (snaplen != 0 && phdr->caplen > snaplen) {
367       snap_phdr = *phdr;
368       snap_phdr.caplen = snaplen;
369       phdr = &snap_phdr;
370     }
371
372     if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
373          wtap_buf_ptr(wth), &write_err)) {
374       got_write_error = TRUE;
375       break;
376     }
377   }
378
379   merge_close_in_files(in_file_count, in_files);
380   if (!got_read_error && !got_write_error) {
381     if (!wtap_dump_close(pdh, &write_err))
382       got_write_error = TRUE;
383   } else
384     wtap_dump_close(pdh, &close_err);
385
386   if (got_read_error) {
387     /*
388      * Find the file on which we got the error, and report the error.
389      */
390     for (i = 0; i < in_file_count; i++) {
391       if (in_files[i].state == GOT_ERROR) {
392         fprintf(stderr, "mergecap: Error reading %s: %s\n",
393                 in_files[i].filename, wtap_strerror(read_err));
394         switch (read_err) {
395
396         case WTAP_ERR_UNSUPPORTED:
397         case WTAP_ERR_UNSUPPORTED_ENCAP:
398         case WTAP_ERR_BAD_RECORD:
399           fprintf(stderr, "(%s)\n", err_info);
400           g_free(err_info);
401           break;
402         }
403       }
404     }
405   }
406
407   if (got_write_error) {
408     fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
409             wtap_strerror(write_err));
410   }
411
412   g_free(in_files);
413
414   return (!got_read_error && !got_write_error) ? 0 : 2;
415 }