1 /* Combine two dump files, either by appending or by merging by timestamp
5 * Written by Scott Renfro <scott@renfro.org> based on
6 * editcap by Richard Sharpe and Guy Harris
23 #ifdef HAVE_SYS_TIME_H
34 #include "svnversion.h"
45 /* Win32 needs the O_BINARY flag for open() */
51 get_natural_int(const char *string, const char *name)
56 number = strtol(string, &p, 10);
57 if (p == string || *p != '\0') {
58 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
63 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
67 if (number > INT_MAX) {
68 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
76 get_positive_int(const char *string, const char *name)
80 number = get_natural_int(string, name);
83 fprintf(stderr, "mergecap: The specified %s is zero\n",
100 printf("Usage: mergecap [-hva] [-s <snaplen>] [-T <encap type>]\n");
101 printf(" [-F <capture type>] -w <outfile> <infile> [...]\n\n");
102 printf(" where\t-h produces this help listing.\n");
103 printf(" \t-v verbose operation, default is silent\n");
104 printf(" \t-a files should be concatenated, not merged\n");
105 printf(" \t Default merges based on frame timestamps\n");
106 printf(" \t-s <snaplen>: truncate packets to <snaplen> bytes of data\n");
107 printf(" \t-w <outfile>: sets output filename to <outfile>\n");
108 printf(" \t-T <encap type> encapsulation type to use:\n");
109 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
110 string = wtap_encap_short_string(i);
112 printf(" \t %s - %s\n",
113 string, wtap_encap_string(i));
115 printf(" \t default is the same as the first input file\n");
116 printf(" \t-F <capture type> capture file type to write:\n");
117 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
118 if (wtap_dump_can_open(i))
119 printf(" \t %s - %s\n",
120 wtap_file_type_short_string(i), wtap_file_type_string(i));
122 printf(" \t default is libpcap\n");
126 main(int argc, char *argv[])
131 gboolean do_append = FALSE;
132 gboolean verbose = FALSE;
133 int in_file_count = 0;
135 int file_type = WTAP_FILE_PCAP; /* default to libpcap format */
138 merge_in_file_t *in_files = NULL;
141 struct wtap_pkthdr *phdr, snap_phdr;
143 int open_err, read_err, write_err, close_err;
146 char *out_filename = NULL;
147 gboolean got_read_error = FALSE, got_write_error = FALSE;
150 /* Process the options first */
151 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
155 out_filename = optarg;
159 do_append = !do_append;
163 frame_type = wtap_short_string_to_encap(optarg);
164 if (frame_type < 0) {
165 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
172 file_type = wtap_short_string_to_file_type(optarg);
174 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
185 snaplen = get_positive_int(optarg, "snapshot length");
189 printf("mergecap version %s"
198 case '?': /* Bad options if GNU getopt */
207 /* check for proper args; at a minimum, must have an output
208 * filename and one input file
210 in_file_count = argc - optind;
212 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
213 fprintf(stderr, " run with -h for help\n");
216 if (in_file_count < 1) {
217 fprintf(stderr, "mergecap: No input files were specified\n");
221 /* open the input files */
222 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
223 &open_err, &err_info, &err_fileno)) {
224 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
225 wtap_strerror(open_err));
228 case WTAP_ERR_UNSUPPORTED:
229 case WTAP_ERR_UNSUPPORTED_ENCAP:
230 case WTAP_ERR_BAD_RECORD:
231 fprintf(stderr, "(%s)\n", err_info);
239 for (i = 0; i < in_file_count; i++)
240 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
241 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
246 * Snapshot length not specified - default to the maximum of the
247 * snapshot lengths of the input files.
249 snaplen = merge_max_snapshot_length(in_file_count, in_files);
252 /* set the outfile frame type */
253 if (frame_type == -2) {
255 * Default to the appropriate frame type for the input files.
257 frame_type = merge_select_frame_type(in_file_count, in_files);
259 if (frame_type == WTAP_ENCAP_PER_PACKET) {
261 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
263 int first_frame_type, this_frame_type;
265 first_frame_type = wtap_file_encap(in_files[0].wth);
266 for (i = 1; i < in_file_count; i++) {
267 this_frame_type = wtap_file_encap(in_files[i].wth);
268 if (first_frame_type != this_frame_type) {
269 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
270 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
271 fprintf(stderr, " %s had type %s (%s)\n",
272 in_files[0].filename,
273 wtap_encap_string(first_frame_type),
274 wtap_encap_short_string(first_frame_type));
275 fprintf(stderr, " %s had type %s (%s)\n",
276 in_files[i].filename,
277 wtap_encap_string(this_frame_type),
278 wtap_encap_short_string(this_frame_type));
283 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
284 wtap_encap_string(frame_type),
285 wtap_encap_short_string(frame_type));
289 /* open the outfile */
290 if (strncmp(out_filename, "-", 2) == 0) {
291 /* use stdout as the outfile */
292 out_fd = 1 /*stdout*/;
294 /* open the outfile */
295 out_fd = open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
297 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
298 out_filename, strerror(errno));
303 /* prepare the outfile */
304 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, &open_err);
306 merge_close_in_files(in_file_count, in_files);
308 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
309 wtap_strerror(open_err));
313 /* do the merge (or append) */
317 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
320 wth = merge_read_packet(in_file_count, in_files, &read_err,
324 got_read_error = TRUE;
329 fprintf(stderr, "Record: %u\n", count++);
331 /* We simply write it, perhaps after truncating it; we could do other
332 * things, like modify it. */
333 phdr = wtap_phdr(wth);
334 if (snaplen != 0 && phdr->caplen > snaplen) {
336 snap_phdr.caplen = snaplen;
340 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
341 wtap_buf_ptr(wth), &write_err)) {
342 got_write_error = TRUE;
347 merge_close_in_files(in_file_count, in_files);
348 if (!got_read_error && !got_write_error) {
349 if (!wtap_dump_close(pdh, &write_err))
350 got_write_error = TRUE;
352 wtap_dump_close(pdh, &close_err);
354 if (got_read_error) {
356 * Find the file on which we got the error, and report the error.
358 for (i = 0; i < in_file_count; i++) {
359 if (in_files[i].state == GOT_ERROR) {
360 fprintf(stderr, "mergecap: Error reading %s: %s\n",
361 in_files[i].filename, wtap_strerror(read_err));
364 case WTAP_ERR_UNSUPPORTED:
365 case WTAP_ERR_UNSUPPORTED_ENCAP:
366 case WTAP_ERR_BAD_RECORD:
367 fprintf(stderr, "(%s)\n", err_info);
375 if (got_write_error) {
376 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
377 wtap_strerror(write_err));
382 return (!got_read_error && !got_write_error) ? 0 : 2;