1 /* Combine two dump files, either by appending or by merging by timestamp
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
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.
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.
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.
23 * Mergecap written by Scott Renfro <scott@renfro.org> based on
24 * editcap by Richard Sharpe and Guy Harris
39 #ifdef HAVE_SYS_TIME_H
47 #include "wsutil/wsgetopt.h"
50 #include "svnversion.h"
52 #include "wsutil/file_util.h"
59 #include <wsutil/unicode-utils.h>
63 get_natural_int(const char *string, const char *name)
68 number = strtol(string, &p, 10);
69 if (p == string || *p != '\0') {
70 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
75 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
79 if (number > INT_MAX) {
80 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
88 get_positive_int(const char *string, const char *name)
92 number = get_natural_int(string, name);
95 fprintf(stderr, "mergecap: The specified %s is zero\n",
110 fprintf(stderr, "Mergecap %s"
112 " (" SVNVERSION " from " SVNPATH ")"
115 fprintf(stderr, "Merge two or more capture files into one.\n");
116 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
117 fprintf(stderr, "\n");
118 fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
119 fprintf(stderr, "\n");
120 fprintf(stderr, "Output:\n");
121 fprintf(stderr, " -a concatenate rather than merge files.\n");
122 fprintf(stderr, " default is to merge based on frame timestamps.\n");
123 fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
124 fprintf(stderr, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
125 fprintf(stderr, " -F <capture type> set the output file type; default is pcapng.\n");
126 fprintf(stderr, " an empty \"-F\" option will list the file types.\n");
127 fprintf(stderr, " -T <encap type> set the output file encapsulation type;\n");
128 fprintf(stderr, " default is the same as the first input file.\n");
129 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types.\n");
130 fprintf(stderr, "\n");
131 fprintf(stderr, "Miscellaneous:\n");
132 fprintf(stderr, " -h display this help and exit.\n");
133 fprintf(stderr, " -v verbose output.\n");
137 const char *sstr; /* The short string */
138 const char *lstr; /* The long string */
142 string_compare(gconstpointer a, gconstpointer b)
144 return strcmp(((const struct string_elem *)a)->sstr,
145 ((const struct string_elem *)b)->sstr);
149 string_elem_print(gpointer data, gpointer not_used _U_)
151 fprintf(stderr, " %s - %s\n",
152 ((struct string_elem *)data)->sstr,
153 ((struct string_elem *)data)->lstr);
157 list_capture_types(void) {
159 struct string_elem *captypes;
162 captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES);
164 fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
165 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
166 if (wtap_dump_can_open(i)) {
167 captypes[i].sstr = wtap_file_type_short_string(i);
168 captypes[i].lstr = wtap_file_type_string(i);
169 list = g_slist_insert_sorted(list, &captypes[i], string_compare);
172 g_slist_foreach(list, string_elem_print, NULL);
178 list_encap_types(void) {
180 struct string_elem *encaps;
183 encaps = g_new(struct string_elem,WTAP_NUM_ENCAP_TYPES);
184 fprintf(stderr, "mergecap: The available encapsulation types for the \"-T\" flag are:\n");
185 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
186 encaps[i].sstr = wtap_encap_short_string(i);
187 if (encaps[i].sstr != NULL) {
188 encaps[i].lstr = wtap_encap_string(i);
189 list = g_slist_insert_sorted(list, &encaps[i], string_compare);
192 g_slist_foreach(list, string_elem_print, NULL);
198 main(int argc, char *argv[])
202 gboolean do_append = FALSE;
203 gboolean verbose = FALSE;
204 int in_file_count = 0;
206 #ifdef PCAP_NG_DEFAULT
207 int file_type = WTAP_FILE_PCAPNG; /* default to pcap format */
209 int file_type = WTAP_FILE_PCAP; /* default to pcapng format */
213 merge_in_file_t *in_files = NULL, *in_file;
215 struct wtap_pkthdr *phdr, snap_phdr;
217 int open_err, read_err=0, write_err, close_err;
220 char *out_filename = NULL;
221 gboolean got_read_error = FALSE, got_write_error = FALSE;
225 arg_list_utf_16to8(argc, argv);
226 create_app_running_mutex();
229 /* Process the options first */
230 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
234 out_filename = optarg;
238 do_append = !do_append;
242 frame_type = wtap_short_string_to_encap(optarg);
243 if (frame_type < 0) {
244 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
252 file_type = wtap_short_string_to_file_type(optarg);
254 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
256 list_capture_types();
266 snaplen = get_positive_int(optarg, "snapshot length");
274 case '?': /* Bad options if GNU getopt */
277 list_capture_types();
292 /* check for proper args; at a minimum, must have an output
293 * filename and one input file
295 in_file_count = argc - optind;
297 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
298 fprintf(stderr, " run with -h for help\n");
301 if (in_file_count < 1) {
302 fprintf(stderr, "mergecap: No input files were specified\n");
306 /* open the input files */
307 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
308 &open_err, &err_info, &err_fileno)) {
309 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
310 wtap_strerror(open_err));
313 case WTAP_ERR_UNSUPPORTED:
314 case WTAP_ERR_UNSUPPORTED_ENCAP:
315 case WTAP_ERR_BAD_FILE:
316 fprintf(stderr, "(%s)\n", err_info);
324 for (i = 0; i < in_file_count; i++)
325 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
326 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
331 * Snapshot length not specified - default to the maximum of the
332 * snapshot lengths of the input files.
334 snaplen = merge_max_snapshot_length(in_file_count, in_files);
337 /* set the outfile frame type */
338 if (frame_type == -2) {
340 * Default to the appropriate frame type for the input files.
342 frame_type = merge_select_frame_type(in_file_count, in_files);
344 if (frame_type == WTAP_ENCAP_PER_PACKET) {
346 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
348 int first_frame_type, this_frame_type;
350 first_frame_type = wtap_file_encap(in_files[0].wth);
351 for (i = 1; i < in_file_count; i++) {
352 this_frame_type = wtap_file_encap(in_files[i].wth);
353 if (first_frame_type != this_frame_type) {
354 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
355 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
356 fprintf(stderr, " %s had type %s (%s)\n",
357 in_files[0].filename,
358 wtap_encap_string(first_frame_type),
359 wtap_encap_short_string(first_frame_type));
360 fprintf(stderr, " %s had type %s (%s)\n",
361 in_files[i].filename,
362 wtap_encap_string(this_frame_type),
363 wtap_encap_short_string(this_frame_type));
368 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
369 wtap_encap_string(frame_type),
370 wtap_encap_short_string(frame_type));
374 /* open the outfile */
375 if (strncmp(out_filename, "-", 2) == 0) {
376 /* use stdout as the outfile */
377 out_fd = 1 /*stdout*/;
379 /* open the outfile */
380 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
382 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
383 out_filename, g_strerror(errno));
388 /* prepare the outfile */
389 if(file_type == WTAP_FILE_PCAPNG ){
390 wtapng_section_t *shb_hdr;
391 GString *comment_gstr;
393 shb_hdr = g_new(wtapng_section_t,1);
394 comment_gstr = g_string_new("File created by merging: \n");
396 for (i = 0; i < in_file_count; i++) {
397 g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename);
399 shb_hdr->section_length = -1;
401 shb_hdr->opt_comment = comment_gstr->str; /* NULL if not available */
402 shb_hdr->shb_hardware = NULL; /* NULL if not available, UTF-8 string containing the description of the hardware used to create this section. */
403 shb_hdr->shb_os = NULL; /* NULL if not available, UTF-8 string containing the name of the operating system used to create this section. */
404 shb_hdr->shb_user_appl = "mergecap"; /* NULL if not available, UTF-8 string containing the name of the application used to create this section. */
406 pdh = wtap_dump_fdopen_ng(out_fd, file_type, frame_type, snaplen,
407 FALSE /* compressed */, shb_hdr, NULL /* wtapng_iface_descriptions_t *idb_inf */, &open_err);
408 g_string_free(comment_gstr, TRUE);
410 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
413 merge_close_in_files(in_file_count, in_files);
415 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
416 wtap_strerror(open_err));
420 /* do the merge (or append) */
424 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
427 in_file = merge_read_packet(in_file_count, in_files, &read_err,
429 if (in_file == NULL) {
435 /* I/O error reading from in_file */
436 got_read_error = TRUE;
441 fprintf(stderr, "Record: %u\n", count++);
443 /* We simply write it, perhaps after truncating it; we could do other
444 * things, like modify it. */
445 phdr = wtap_phdr(in_file->wth);
446 if (snaplen != 0 && phdr->caplen > snaplen) {
448 snap_phdr.caplen = snaplen;
452 if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err)) {
453 got_write_error = TRUE;
458 merge_close_in_files(in_file_count, in_files);
459 if (!got_read_error && !got_write_error) {
460 if (!wtap_dump_close(pdh, &write_err))
461 got_write_error = TRUE;
463 wtap_dump_close(pdh, &close_err);
465 if (got_read_error) {
467 * Find the file on which we got the error, and report the error.
469 for (i = 0; i < in_file_count; i++) {
470 if (in_files[i].state == GOT_ERROR) {
471 fprintf(stderr, "mergecap: Error reading %s: %s\n",
472 in_files[i].filename, wtap_strerror(read_err));
475 case WTAP_ERR_UNSUPPORTED:
476 case WTAP_ERR_UNSUPPORTED_ENCAP:
477 case WTAP_ERR_BAD_FILE:
478 fprintf(stderr, "(%s)\n", err_info);
486 if (got_write_error) {
489 case WTAP_ERR_UNSUPPORTED_ENCAP:
491 * This is a problem with the particular frame we're writing;
492 * note that, and give the frame number.
494 fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
495 in_file->packet_num, in_file->filename);
499 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
500 wtap_strerror(write_err));
507 return (!got_read_error && !got_write_error) ? 0 : 2;