*
* $Id$
*
- * Written by Scott Renfro <scott@renfro.org> based on
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Mergecap written by Scott Renfro <scott@renfro.org> based on
* editcap by Richard Sharpe and Guy Harris
*
*/
#include <string.h>
#include "wtap.h"
-#ifdef NEED_GETOPT_H
-#include "getopt.h"
+#ifndef HAVE_GETOPT
+#include "wsutil/wsgetopt.h"
#endif
#include "svnversion.h"
#include <fcntl.h>
#endif
+#ifdef _WIN32
+#include <wsutil/unicode-utils.h>
+#endif /* _WIN32 */
+
static int
get_natural_int(const char *string, const char *name)
{
- long number;
+ int number;
char *p;
- number = strtol(string, &p, 10);
+ number = (int) strtol(string, &p, 10);
if (p == string || *p != '\0') {
fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
name, string);
static int
get_positive_int(const char *string, const char *name)
{
- long number;
+ int number;
number = get_natural_int(string, name);
fprintf(stderr, "Mergecap %s"
#ifdef SVNVERSION
- " (" SVNVERSION ")"
+ " (" SVNVERSION " from " SVNPATH ")"
#endif
"\n", VERSION);
fprintf(stderr, "Merge two or more capture files into one.\n");
fprintf(stderr, "See http://www.wireshark.org for more information.\n");
fprintf(stderr, "\n");
- fprintf(stderr, "Usage: mergecap [options] -w <outfile|-> <infile> ...\n");
+ fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
fprintf(stderr, "\n");
fprintf(stderr, "Output:\n");
- fprintf(stderr, " -a files should be concatenated, not merged\n");
- fprintf(stderr, " Default merges based on frame timestamps\n");
- fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data\n");
- fprintf(stderr, " -w <outfile|-> set the output filename to <outfile> or '-' for stdout\n");
- fprintf(stderr, " -F <capture type> set the output file type, default is libpcap\n");
- fprintf(stderr, " an empty \"-F\" option will list the file types\n");
- fprintf(stderr, " -T <encap type> set the output file encapsulation type,\n");
- fprintf(stderr, " default is the same as the first input file\n");
- fprintf(stderr, " an empty \"-T\" option will list the encapsulation types\n");
+ fprintf(stderr, " -a concatenate rather than merge files.\n");
+ fprintf(stderr, " default is to merge based on frame timestamps.\n");
+ fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
+ fprintf(stderr, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
+ fprintf(stderr, " -F <capture type> set the output file type; default is pcapng.\n");
+ fprintf(stderr, " an empty \"-F\" option will list the file types.\n");
+ fprintf(stderr, " -T <encap type> set the output file encapsulation type;\n");
+ fprintf(stderr, " default is the same as the first input file.\n");
+ fprintf(stderr, " an empty \"-T\" option will list the encapsulation types.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Miscellaneous:\n");
- fprintf(stderr, " -h display this help and exit\n");
- fprintf(stderr, " -v verbose output\n");
+ fprintf(stderr, " -h display this help and exit.\n");
+ fprintf(stderr, " -v verbose output.\n");
}
-static void list_capture_types(void) {
- int i;
+struct string_elem {
+ const char *sstr; /* The short string */
+ const char *lstr; /* The long string */
+};
- fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
- for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
- if (wtap_dump_can_open(i))
- fprintf(stderr, " %s - %s\n",
- wtap_file_type_short_string(i), wtap_file_type_string(i));
+static gint
+string_compare(gconstpointer a, gconstpointer b)
+{
+ return strcmp(((const struct string_elem *)a)->sstr,
+ ((const struct string_elem *)b)->sstr);
+}
+
+static void
+string_elem_print(gpointer data, gpointer not_used _U_)
+{
+ fprintf(stderr, " %s - %s\n",
+ ((struct string_elem *)data)->sstr,
+ ((struct string_elem *)data)->lstr);
+}
+
+static void
+list_capture_types(void) {
+ int i;
+ struct string_elem *captypes;
+ GSList *list = NULL;
+
+ captypes = g_malloc(sizeof(struct string_elem) * WTAP_NUM_FILE_TYPES);
+
+ fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
+ for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
+ if (wtap_dump_can_open(i)) {
+ captypes[i].sstr = wtap_file_type_short_string(i);
+ captypes[i].lstr = wtap_file_type_string(i);
+ list = g_slist_insert_sorted(list, &captypes[i], string_compare);
}
+ }
+ g_slist_foreach(list, string_elem_print, NULL);
+ g_slist_free(list);
+ g_free(captypes);
}
-static void list_encap_types(void) {
+static void
+list_encap_types(void) {
int i;
- const char *string;
+ struct string_elem *encaps;
+ GSList *list = NULL;
- fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
+ encaps = g_malloc(sizeof(struct string_elem) * WTAP_NUM_ENCAP_TYPES);
+ fprintf(stderr, "mergecap: The available encapsulation types for the \"-T\" flag are:\n");
for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
- string = wtap_encap_short_string(i);
- if (string != NULL)
- fprintf(stderr, " %s - %s\n",
- string, wtap_encap_string(i));
+ encaps[i].sstr = wtap_encap_short_string(i);
+ if (encaps[i].sstr != NULL) {
+ encaps[i].lstr = wtap_encap_string(i);
+ list = g_slist_insert_sorted(list, &encaps[i], string_compare);
+ }
}
+ g_slist_foreach(list, string_elem_print, NULL);
+ g_slist_free(list);
+ g_free(encaps);
}
int
main(int argc, char *argv[])
{
- extern char *optarg;
- extern int optind;
int opt;
+
gboolean do_append = FALSE;
gboolean verbose = FALSE;
int in_file_count = 0;
guint snaplen = 0;
- int file_type = WTAP_FILE_PCAP; /* default to libpcap format */
+#ifdef PCAP_NG_DEFAULT
+ int file_type = WTAP_FILE_PCAPNG; /* default to pcap format */
+#else
+ int file_type = WTAP_FILE_PCAP; /* default to pcapng format */
+#endif
int frame_type = -2;
int out_fd;
- merge_in_file_t *in_files = NULL;
+ merge_in_file_t *in_files = NULL, *in_file;
int i;
- wtap *wth;
struct wtap_pkthdr *phdr, snap_phdr;
wtap_dumper *pdh;
- int open_err, read_err, write_err, close_err;
+ int open_err, read_err=0, write_err, close_err;
gchar *err_info;
int err_fileno;
char *out_filename = NULL;
gboolean got_read_error = FALSE, got_write_error = FALSE;
int count;
+#ifdef _WIN32
+ arg_list_utf_16to8(argc, argv);
+#endif /* _WIN32 */
+
/* Process the options first */
while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNSUPPORTED_ENCAP:
- case WTAP_ERR_BAD_RECORD:
+ case WTAP_ERR_BAD_FILE:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
if (out_fd == -1) {
fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
- out_filename, strerror(errno));
+ out_filename, g_strerror(errno));
exit(1);
}
}
count = 1;
for (;;) {
if (do_append)
- wth = merge_append_read_packet(in_file_count, in_files, &read_err,
- &err_info);
+ in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
+ &err_info);
else
- wth = merge_read_packet(in_file_count, in_files, &read_err,
- &err_info);
- if (wth == NULL) {
- if (read_err != 0)
- got_read_error = TRUE;
+ in_file = merge_read_packet(in_file_count, in_files, &read_err,
+ &err_info);
+ if (in_file == NULL) {
+ /* EOF */
+ break;
+ }
+
+ if (read_err != 0) {
+ /* I/O error reading from in_file */
+ got_read_error = TRUE;
break;
}
/* We simply write it, perhaps after truncating it; we could do other
* things, like modify it. */
- phdr = wtap_phdr(wth);
+ phdr = wtap_phdr(in_file->wth);
if (snaplen != 0 && phdr->caplen > snaplen) {
snap_phdr = *phdr;
snap_phdr.caplen = snaplen;
phdr = &snap_phdr;
}
- if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
- wtap_buf_ptr(wth), &write_err)) {
+ if (!wtap_dump(pdh, phdr, wtap_pseudoheader(in_file->wth),
+ wtap_buf_ptr(in_file->wth), &write_err)) {
got_write_error = TRUE;
break;
}
case WTAP_ERR_UNSUPPORTED:
case WTAP_ERR_UNSUPPORTED_ENCAP:
- case WTAP_ERR_BAD_RECORD:
+ case WTAP_ERR_BAD_FILE:
fprintf(stderr, "(%s)\n", err_info);
g_free(err_info);
break;
}
if (got_write_error) {
- fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
- wtap_strerror(write_err));
+ switch (write_err) {
+
+ case WTAP_ERR_UNSUPPORTED_ENCAP:
+ /*
+ * This is a problem with the particular frame we're writing;
+ * note that, and give the frame number.
+ */
+ fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
+ in_file->packet_num, in_file->filename);
+ break;
+
+ default:
+ fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
+ wtap_strerror(write_err));
+ break;
+ }
}
g_free(in_files);