MSWSP: WIP: dissect_CPMSetBindings()
[wireshark-wip.git] / mergecap.c
blob1a575424543884dec7a2e33c4c635df0c4a2d062
1 /* Combine dump files, either by appending or by merging by timestamp
3 * $Id$
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
28 #include "config.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <glib.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #ifdef HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
43 #include <string.h>
44 #include "wtap.h"
46 #ifndef HAVE_GETOPT
47 #include <wsutil/wsgetopt.h>
48 #endif
50 #include <wsutil/strnatcmp.h>
51 #include <wsutil/file_util.h>
53 #include <wiretap/merge.h>
55 #include "svnversion.h"
57 #ifdef HAVE_FCNTL_H
58 #include <fcntl.h>
59 #endif
61 #ifdef _WIN32
62 #include <wsutil/unicode-utils.h>
63 #endif /* _WIN32 */
65 static int
66 get_natural_int(const char *string, const char *name)
68 long number;
69 char *p;
71 number = strtol(string, &p, 10);
72 if (p == string || *p != '\0') {
73 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
74 name, string);
75 exit(1);
77 if (number < 0) {
78 fprintf(stderr, "mergecap: The specified %s is a negative number\n", name);
79 exit(1);
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);
86 return (int)number;
89 static int
90 get_positive_int(const char *string, const char *name)
92 int number;
94 number = get_natural_int(string, name);
96 if (number == 0) {
97 fprintf(stderr, "mergecap: The specified %s is zero\n", name);
98 exit(1);
101 return number;
105 * Show the usage
107 static void
108 usage(void)
110 fprintf(stderr, "Mergecap %s"
111 #ifdef SVNVERSION
112 " (" SVNVERSION " from " SVNPATH ")"
113 #endif
114 "\n", VERSION);
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");
136 struct string_elem {
137 const char *sstr; /* The short string */
138 const char *lstr; /* The long string */
141 static gint
142 string_compare(gconstpointer a, gconstpointer b)
144 return strcmp(((const struct string_elem *)a)->sstr,
145 ((const struct string_elem *)b)->sstr);
148 static gint
149 string_nat_compare(gconstpointer a, gconstpointer b)
151 return strnatcmp(((const struct string_elem *)a)->sstr,
152 ((const struct string_elem *)b)->sstr);
155 static void
156 string_elem_print(gpointer data, gpointer not_used _U_)
158 fprintf(stderr, " %s - %s\n", ((struct string_elem *)data)->sstr,
159 ((struct string_elem *)data)->lstr);
162 static void
163 list_capture_types(void) {
164 int i;
165 struct string_elem *captypes;
166 GSList *list = NULL;
168 captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
170 fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
171 for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
172 if (wtap_dump_can_open(i)) {
173 captypes[i].sstr = wtap_file_type_subtype_short_string(i);
174 captypes[i].lstr = wtap_file_type_subtype_string(i);
175 list = g_slist_insert_sorted(list, &captypes[i], string_compare);
178 g_slist_foreach(list, string_elem_print, NULL);
179 g_slist_free(list);
180 g_free(captypes);
183 static void
184 list_encap_types(void) {
185 int i;
186 struct string_elem *encaps;
187 GSList *list = NULL;
189 encaps = g_new(struct string_elem,WTAP_NUM_ENCAP_TYPES);
190 fprintf(stderr, "mergecap: The available encapsulation types for the \"-T\" flag are:\n");
191 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
192 encaps[i].sstr = wtap_encap_short_string(i);
193 if (encaps[i].sstr != NULL) {
194 encaps[i].lstr = wtap_encap_string(i);
195 list = g_slist_insert_sorted(list, &encaps[i], string_nat_compare);
198 g_slist_foreach(list, string_elem_print, NULL);
199 g_slist_free(list);
200 g_free(encaps);
204 main(int argc, char *argv[])
206 int opt;
207 gboolean do_append = FALSE;
208 gboolean verbose = FALSE;
209 int in_file_count = 0;
210 guint snaplen = 0;
211 #ifdef PCAP_NG_DEFAULT
212 int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */
213 #else
214 int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */
215 #endif
216 int frame_type = -2;
217 int out_fd;
218 merge_in_file_t *in_files = NULL, *in_file;
219 int i;
220 struct wtap_pkthdr *phdr, snap_phdr;
221 wtap_dumper *pdh;
222 int open_err, read_err = 0, write_err, close_err;
223 gchar *err_info;
224 int err_fileno;
225 char *out_filename = NULL;
226 gboolean got_read_error = FALSE, got_write_error = FALSE;
227 int count;
229 #ifdef _WIN32
230 arg_list_utf_16to8(argc, argv);
231 create_app_running_mutex();
232 #endif /* _WIN32 */
234 /* Process the options first */
235 while ((opt = getopt(argc, argv, "aF:hs:T:vw:")) != -1) {
237 switch (opt) {
238 case 'a':
239 do_append = !do_append;
240 break;
242 case 'F':
243 file_type = wtap_short_string_to_file_type_subtype(optarg);
244 if (file_type < 0) {
245 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
246 optarg);
247 list_capture_types();
248 exit(1);
250 break;
252 case 'h':
253 usage();
254 exit(0);
255 break;
257 case 's':
258 snaplen = get_positive_int(optarg, "snapshot length");
259 break;
261 case 'T':
262 frame_type = wtap_short_string_to_encap(optarg);
263 if (frame_type < 0) {
264 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
265 optarg);
266 list_encap_types();
267 exit(1);
269 break;
271 case 'v':
272 verbose = TRUE;
273 break;
275 case 'w':
276 out_filename = optarg;
277 break;
279 case '?': /* Bad options if GNU getopt */
280 switch(optopt) {
281 case'F':
282 list_capture_types();
283 break;
284 case'T':
285 list_encap_types();
286 break;
287 default:
288 usage();
290 exit(1);
291 break;
295 /* check for proper args; at a minimum, must have an output
296 * filename and one input file
298 in_file_count = argc - optind;
299 if (!out_filename) {
300 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
301 fprintf(stderr, " run with -h for help\n");
302 return 1;
304 if (in_file_count < 1) {
305 fprintf(stderr, "mergecap: No input files were specified\n");
306 return 1;
309 /* open the input files */
310 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
311 &open_err, &err_info, &err_fileno)) {
312 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
313 wtap_strerror(open_err));
314 switch (open_err) {
316 case WTAP_ERR_UNSUPPORTED:
317 case WTAP_ERR_UNSUPPORTED_ENCAP:
318 case WTAP_ERR_BAD_FILE:
319 fprintf(stderr, "(%s)\n", err_info);
320 g_free(err_info);
321 break;
323 return 2;
326 if (verbose) {
327 for (i = 0; i < in_file_count; i++)
328 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
329 wtap_file_type_subtype_string(wtap_file_type_subtype(in_files[i].wth)));
332 if (snaplen == 0) {
334 * Snapshot length not specified - default to the maximum of the
335 * snapshot lengths of the input files.
337 snaplen = merge_max_snapshot_length(in_file_count, in_files);
340 /* set the outfile frame type */
341 if (frame_type == -2) {
343 * Default to the appropriate frame type for the input files.
345 frame_type = merge_select_frame_type(in_file_count, in_files);
346 if (verbose) {
347 if (frame_type == WTAP_ENCAP_PER_PACKET) {
349 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
351 int first_frame_type, this_frame_type;
353 first_frame_type = wtap_file_encap(in_files[0].wth);
354 for (i = 1; i < in_file_count; i++) {
355 this_frame_type = wtap_file_encap(in_files[i].wth);
356 if (first_frame_type != this_frame_type) {
357 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
358 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
359 fprintf(stderr, " %s had type %s (%s)\n",
360 in_files[0].filename,
361 wtap_encap_string(first_frame_type),
362 wtap_encap_short_string(first_frame_type));
363 fprintf(stderr, " %s had type %s (%s)\n",
364 in_files[i].filename,
365 wtap_encap_string(this_frame_type),
366 wtap_encap_short_string(this_frame_type));
367 break;
371 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
372 wtap_encap_string(frame_type),
373 wtap_encap_short_string(frame_type));
377 /* open the outfile */
378 if (strncmp(out_filename, "-", 2) == 0) {
379 /* use stdout as the outfile */
380 out_fd = 1 /*stdout*/;
381 } else {
382 /* open the outfile */
383 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
384 if (out_fd == -1) {
385 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
386 out_filename, g_strerror(errno));
387 exit(1);
391 /* prepare the outfile */
392 if(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG ){
393 wtapng_section_t *shb_hdr;
394 GString *comment_gstr;
396 shb_hdr = g_new(wtapng_section_t,1);
397 comment_gstr = g_string_new("File created by merging: \n");
399 for (i = 0; i < in_file_count; i++) {
400 g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename);
402 shb_hdr->section_length = -1;
403 /* options */
404 shb_hdr->opt_comment = comment_gstr->str; /* NULL if not available */
405 shb_hdr->shb_hardware = NULL; /* NULL if not available, UTF-8 string containing the description of the hardware used to create this section. */
406 shb_hdr->shb_os = NULL; /* NULL if not available, UTF-8 string containing the name of the operating system used to create this section. */
407 shb_hdr->shb_user_appl = "mergecap"; /* NULL if not available, UTF-8 string containing the name of the application used to create this section. */
409 pdh = wtap_dump_fdopen_ng(out_fd, file_type, frame_type, snaplen,
410 FALSE /* compressed */, shb_hdr, NULL /* wtapng_iface_descriptions_t *idb_inf */, &open_err);
411 g_string_free(comment_gstr, TRUE);
412 } else {
413 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
415 if (pdh == NULL) {
416 merge_close_in_files(in_file_count, in_files);
417 g_free(in_files);
418 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
419 wtap_strerror(open_err));
420 exit(1);
423 /* do the merge (or append) */
424 count = 1;
425 for (;;) {
426 if (do_append)
427 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
428 &err_info);
429 else
430 in_file = merge_read_packet(in_file_count, in_files, &read_err,
431 &err_info);
432 if (in_file == NULL) {
433 /* EOF */
434 break;
437 if (read_err != 0) {
438 /* I/O error reading from in_file */
439 got_read_error = TRUE;
440 break;
443 if (verbose)
444 fprintf(stderr, "Record: %u\n", count++);
446 /* We simply write it, perhaps after truncating it; we could do other
447 * things, like modify it. */
448 phdr = wtap_phdr(in_file->wth);
449 if (snaplen != 0 && phdr->caplen > snaplen) {
450 snap_phdr = *phdr;
451 snap_phdr.caplen = snaplen;
452 phdr = &snap_phdr;
455 if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err)) {
456 got_write_error = TRUE;
457 break;
461 merge_close_in_files(in_file_count, in_files);
462 if (!got_read_error && !got_write_error) {
463 if (!wtap_dump_close(pdh, &write_err))
464 got_write_error = TRUE;
465 } else
466 wtap_dump_close(pdh, &close_err);
468 if (got_read_error) {
470 * Find the file on which we got the error, and report the error.
472 for (i = 0; i < in_file_count; i++) {
473 if (in_files[i].state == GOT_ERROR) {
474 fprintf(stderr, "mergecap: Error reading %s: %s\n",
475 in_files[i].filename, wtap_strerror(read_err));
476 switch (read_err) {
478 case WTAP_ERR_UNSUPPORTED:
479 case WTAP_ERR_UNSUPPORTED_ENCAP:
480 case WTAP_ERR_BAD_FILE:
481 fprintf(stderr, "(%s)\n", err_info);
482 g_free(err_info);
483 break;
489 if (got_write_error) {
490 switch (write_err) {
492 case WTAP_ERR_UNSUPPORTED_ENCAP:
494 * This is a problem with the particular frame we're writing;
495 * note that, and give the frame number.
497 fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
498 in_file->packet_num, in_file->filename);
499 break;
501 default:
502 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
503 wtap_strerror(write_err));
504 break;
508 g_free(in_files);
510 return (!got_read_error && !got_write_error) ? 0 : 2;
514 * Editor modelines - http://www.wireshark.org/tools/modelines.html
516 * Local variables:
517 * c-basic-offset: 2
518 * tab-width: 2
519 * indent-tabs-mode: nil
520 * End:
522 * vi: set shiftwidth=2 tabstop=2 expandtab:
523 * :indentSize=2:tabSize=2:noTabs=true: