#endif
#include <wsutil/privileges.h>
+#include <wsutil/filesystem.h>
#include <wsutil/report_err.h>
#include <wsutil/strnatcmp.h>
#include <wsutil/md5.h>
-
-/*
- * The symbols declared in the below are exported from libwireshark,
- * but we don't want to link whole libwireshark to editcap.
- * We link the object directly instead and this needs a little trick
- * with the WS_BUILD_DLL #define.
- */
-#define WS_BUILD_DLL
-#define RESET_SYMBOL_EXPORT /* wsutil/wsgetopt.h set export behavior above. */
-#include "epan/plugins.h"
-#include "epan/filesystem.h"
-#undef WS_BUILD_DLL
-#define RESET_SYMBOL_EXPORT
+#include <wsutil/plugins.h>
#include "svnversion.h"
*/
typedef struct _fd_hash_t {
md5_byte_t digest[16];
- guint32 len;
- nstime_t time;
+ guint32 len;
+ nstime_t time;
} fd_hash_t;
-#define DEFAULT_DUP_DEPTH 5 /* Used with -d */
-#define MAX_DUP_DEPTH 1000000 /* the maximum window (and actual size of fd_hash[]) for de-duplication */
+#define DEFAULT_DUP_DEPTH 5 /* Used with -d */
+#define MAX_DUP_DEPTH 1000000 /* the maximum window (and actual size of fd_hash[]) for de-duplication */
static fd_hash_t fd_hash[MAX_DUP_DEPTH];
-static int dup_window = DEFAULT_DUP_DEPTH;
-static int cur_dup_entry = 0;
+static int dup_window = DEFAULT_DUP_DEPTH;
+static int cur_dup_entry = 0;
-#define ONE_MILLION 1000000
+#define ONE_MILLION 1000000
#define ONE_BILLION 1000000000
/* Weights of different errors we can introduce */
int off_end_neg;
} chop_t;
-#define MAX_SELECTIONS 512
-static struct select_item selectfrm[MAX_SELECTIONS];
-static int max_selected = -1;
-static int keep_em = 0;
+#define MAX_SELECTIONS 512
+static struct select_item selectfrm[MAX_SELECTIONS];
+static int max_selected = -1;
+static int keep_em = 0;
#ifdef PCAP_NG_DEFAULT
-static int out_file_type = WTAP_FILE_PCAPNG; /* default to pcapng */
+static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng */
#else
-static int out_file_type = WTAP_FILE_PCAP; /* default to pcap */
+static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcap */
#endif
-static int out_frame_type = -2; /* Leave frame type alone */
-static int verbose = 0; /* Not so verbose */
-static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
-static nstime_t relative_time_window = {0, 0}; /* de-dup time window */
-static double err_prob = 0.0;
-static time_t starttime = 0;
-static time_t stoptime = 0;
-static gboolean check_startstop = FALSE;
-static gboolean dup_detect = FALSE;
-static gboolean dup_detect_by_time = FALSE;
-
-static int do_strict_time_adjustment = FALSE;
-static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */
-static nstime_t previous_time = {0, 0}; /* previous time */
+static int out_frame_type = -2; /* Leave frame type alone */
+static int verbose = 0; /* Not so verbose */
+static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
+static nstime_t relative_time_window = {0, 0}; /* de-dup time window */
+static double err_prob = 0.0;
+static time_t starttime = 0;
+static time_t stoptime = 0;
+static gboolean check_startstop = FALSE;
+static gboolean dup_detect = FALSE;
+static gboolean dup_detect_by_time = FALSE;
+
+static int do_strict_time_adjustment = FALSE;
+static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */
+static nstime_t previous_time = {0, 0}; /* previous time */
static int find_dct2000_real_data(guint8 *buf);
static void handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
- const struct wtap_pkthdr *in_phdr, guint8 *buf,
+ const struct wtap_pkthdr *in_phdr, guint8 **buf,
gboolean adjlen);
static gchar *
-abs_time_to_str_with_sec_resolution(const struct wtap_nstime *abs_time)
+abs_time_to_str_with_sec_resolution(const nstime_t *abs_time)
{
struct tm *tmp;
- gchar *buf = (gchar *)g_malloc(16);
-
-#if (defined _WIN32) && (_MSC_VER < 1500)
- /* calling localtime() on MSVC 2005 with huge values causes it to crash */
- /* XXX - find the exact value that still does work */
- /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
- if (abs_time->secs > 2000000000)
- tmp = NULL;
- else
-#endif
- tmp = localtime(&abs_time->secs);
+ gchar *buf = (gchar *)g_malloc(16);
+
+ tmp = localtime(&abs_time->secs);
if (tmp) {
g_snprintf(buf, 16, "%d%02d%02d%02d%02d%02d",
return buf;
}
-static gchar*
-fileset_get_filename_by_pattern(guint idx, const struct wtap_nstime *time_val,
+static gchar *
+fileset_get_filename_by_pattern(guint idx, const nstime_t *time_val,
gchar *fprefix, gchar *fsuffix)
{
- gchar filenum[5+1];
+ gchar filenum[5+1];
gchar *timestr;
gchar *abs_str;
if (++max_selected >= MAX_SELECTIONS) {
/* Let the user know we stopped selecting */
- printf("Out of room for packet selections!\n");
+ fprintf(stderr, "Out of room for packet selections!\n");
return(FALSE);
}
- printf("Add_Selected: %s\n", sel);
+ if (verbose)
+ fprintf(stderr, "Add_Selected: %s\n", sel);
if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
- printf("Not inclusive ...");
+ if (verbose)
+ fprintf(stderr, "Not inclusive ...");
selectfrm[max_selected].inclusive = 0;
selectfrm[max_selected].first = atoi(sel);
- printf(" %i\n", selectfrm[max_selected].first);
+ if (verbose)
+ fprintf(stderr, " %i\n", selectfrm[max_selected].first);
} else {
- printf("Inclusive ...");
+ if (verbose)
+ fprintf(stderr, "Inclusive ...");
next = locn + 1;
selectfrm[max_selected].inclusive = 1;
selectfrm[max_selected].first = atoi(sel);
selectfrm[max_selected].second = atoi(next);
- printf(" %i, %i\n", selectfrm[max_selected].first,
- selectfrm[max_selected].second);
+ if (verbose)
+ fprintf(stderr, " %i, %i\n", selectfrm[max_selected].first,
+ selectfrm[max_selected].second);
}
return(TRUE);
static gboolean
check_timestamp(wtap *wth)
{
- struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
+ struct wtap_pkthdr *pkthdr = wtap_phdr(wth);
return (pkthdr->ts.secs >= starttime) && (pkthdr->ts.secs < stoptime);
}
static void
set_time_adjustment(char *optarg_str_p)
{
- char *frac, *end;
- long val;
- size_t frac_digits;
+ char *frac, *end;
+ long val;
+ size_t frac_digits;
if (!optarg_str_p)
return;
static void
set_strict_time_adj(char *optarg_str_p)
{
- char *frac, *end;
- long val;
- size_t frac_digits;
+ char *frac, *end;
+ long val;
+ size_t frac_digits;
if (!optarg_str_p)
return;
static void
set_rel_time(char *optarg_str_p)
{
- char *frac, *end;
- long val;
- size_t frac_digits;
+ char *frac, *end;
+ long val;
+ size_t frac_digits;
if (!optarg_str_p)
return;
* in strict chronologically increasing order (which is NOT
* always the case!!).
*
- * The fd_hash[] table was deliberatly created large (1,000,000).
+ * The fd_hash[] table was deliberately created large (1,000,000).
* Looking for time related duplicates in large trace files with
* non-fractional dup time window values can potentially take
* a long time to complete.
fprintf(output, " -v verbose output.\n");
fprintf(output, " If -v is used with any of the 'Duplicate Packet\n");
fprintf(output, " Removal' options (-d, -D or -w) then Packet lengths\n");
- fprintf(output, " and MD5 hashes are printed to standard-out.\n");
+ fprintf(output, " and MD5 hashes are printed to standard-error.\n");
fprintf(output, "\n");
}
struct string_elem *captypes;
GSList *list = NULL;
- captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES);
+ captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
fprintf(stderr, "editcap: The available capture file types for the \"-F\" flag are:\n");
- for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
+ for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
if (wtap_dump_can_open(i)) {
- captypes[i].sstr = wtap_file_type_short_string(i);
- captypes[i].lstr = wtap_file_type_string(i);
+ captypes[i].sstr = wtap_file_type_subtype_short_string(i);
+ captypes[i].lstr = wtap_file_type_subtype_string(i);
list = g_slist_insert_sorted(list, &captypes[i], string_compare);
}
}
int
main(int argc, char *argv[])
{
- wtap *wth;
- int i, j, err;
- gchar *err_info;
- int opt;
-
- char *p;
- guint32 snaplen = 0; /* No limit */
- chop_t chop = {0, 0, 0, 0, 0, 0}; /* No chop */
- gboolean adjlen = FALSE;
- wtap_dumper *pdh = NULL;
- unsigned int count = 1;
- unsigned int duplicate_count = 0;
- gint64 data_offset;
- struct wtap_pkthdr snap_phdr;
- const struct wtap_pkthdr *phdr;
- int err_type;
- wtapng_section_t *shb_hdr;
+ wtap *wth;
+ int i, j, err;
+ gchar *err_info;
+ int opt;
+
+ char *p;
+ guint32 snaplen = 0; /* No limit */
+ chop_t chop = {0, 0, 0, 0, 0, 0}; /* No chop */
+ gboolean adjlen = FALSE;
+ wtap_dumper *pdh = NULL;
+ unsigned int count = 1;
+ unsigned int duplicate_count = 0;
+ gint64 data_offset;
+ int err_type;
+ guint8 *buf;
+ guint32 read_count = 0;
+ int split_packet_count = 0;
+ int written_count = 0;
+ char *filename = NULL;
+ gboolean ts_okay = TRUE;
+ int secs_per_block = 0;
+ int block_cnt = 0;
+ nstime_t block_start;
+ gchar *fprefix = NULL;
+ gchar *fsuffix = NULL;
+
+ const struct wtap_pkthdr *phdr;
+ struct wtap_pkthdr snap_phdr;
wtapng_iface_descriptions_t *idb_inf;
- guint8 *buf;
- guint32 read_count = 0;
- int split_packet_count = 0;
- int written_count = 0;
- char *filename = NULL;
- gboolean ts_okay = TRUE;
- int secs_per_block = 0;
- int block_cnt = 0;
- nstime_t block_start;
- gchar *fprefix = NULL;
- gchar *fsuffix = NULL;
- char appname[100];
+ wtapng_section_t *shb_hdr;
#ifdef HAVE_PLUGINS
char* init_progfile_dir_error;
g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
+ /* Register all the plugin types we have. */
+ wtap_register_plugin_types(); /* Types known to libwiretap */
+
init_report_err(failure_message,NULL,NULL,NULL);
- init_plugins();
+
+ /* Scan for plugins. This does *not* call their registration routines;
+ that's done later. */
+ scan_plugins();
+
+ /* Register all libwiretap plugin modules. */
+ register_all_wiretap_modules();
}
#endif
break;
case 'F':
- out_file_type = wtap_short_string_to_file_type(optarg);
- if (out_file_type < 0) {
+ out_file_type_subtype = wtap_short_string_to_file_type_subtype(optarg);
+ if (out_file_type_subtype < 0) {
fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
optarg);
list_capture_types();
case 'h':
usage(FALSE);
- exit(1);
+ exit(0);
break;
case 'i': /* break capture file based on time interval */
}
#ifdef DEBUG
- printf("Optind = %i, argc = %i\n", optind, argc);
+ fprintf(stderr, "Optind = %i, argc = %i\n", optind, argc);
#endif
if ((argc - optind) < 1) {
if (verbose) {
fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
- wtap_file_type_string(wtap_file_type(wth)));
+ wtap_file_type_subtype_string(wtap_file_type_subtype(wth)));
}
shb_hdr = wtap_file_get_shb_info(wth);
/* If we don't have an application name add Editcap */
if (shb_hdr->shb_user_appl == NULL) {
- g_snprintf(appname, sizeof(appname), "Editcap " VERSION);
- shb_hdr->shb_user_appl = appname;
+ shb_hdr->shb_user_appl = "Editcap " VERSION;
}
- pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+ pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
FALSE /* compressed */, shb_hdr, idb_inf, &err);
if (verbose)
fprintf(stderr, "Continuing writing in file %s\n", filename);
- pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+ pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
FALSE /* compressed */, shb_hdr, idb_inf, &err);
if (verbose)
fprintf(stderr, "Continuing writing in file %s\n", filename);
- pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+ pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
FALSE /* compressed */, shb_hdr, idb_inf, &err);
if (pdh == NULL) {
|| (selected(count) && keep_em))) {
if (verbose && !dup_detect && !dup_detect_by_time)
- printf("Packet: %u\n", count);
+ fprintf(stderr, "Packet: %u\n", count);
/* We simply write it, perhaps after truncating it; we could
* do other things, like modify it. */
/* CHOP */
snap_phdr = *phdr;
- handle_chopping(chop, &snap_phdr, phdr, buf, adjlen);
+ handle_chopping(chop, &snap_phdr, phdr, &buf, adjlen);
phdr = &snap_phdr;
/* Do we adjust timestamps to ensure strict chronological
* situation since trace files usually have packets in
* chronological order (oldest to newest).
*/
- /* printf("++out of order, need to adjust this packet!\n"); */
+ /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */
snap_phdr = *phdr;
snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.tv_sec;
snap_phdr.ts.nsecs = previous_time.nsecs;
if (dup_detect) {
if (is_duplicate(buf, phdr->caplen)) {
if (verbose) {
- fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ",
+ fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
count, phdr->caplen);
for (i = 0; i < 16; i++)
- fprintf(stdout, "%02x",
+ fprintf(stderr, "%02x",
(unsigned char)fd_hash[cur_dup_entry].digest[i]);
- fprintf(stdout, "\n");
+ fprintf(stderr, "\n");
}
duplicate_count++;
count++;
continue;
} else {
if (verbose) {
- fprintf(stdout, "Packet: %u, Len: %u, MD5 Hash: ",
+ fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
count, phdr->caplen);
for (i = 0; i < 16; i++)
- fprintf(stdout, "%02x",
+ fprintf(stderr, "%02x",
(unsigned char)fd_hash[cur_dup_entry].digest[i]);
- fprintf(stdout, "\n");
+ fprintf(stderr, "\n");
}
}
}
if (dup_detect_by_time) {
nstime_t current;
- current.secs = phdr->ts.secs;
+ current.secs = phdr->ts.secs;
current.nsecs = phdr->ts.nsecs;
if (is_duplicate_rel_time(buf, phdr->caplen, ¤t)) {
if (verbose) {
- fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ",
+ fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
count, phdr->caplen);
for (i = 0; i < 16; i++)
- fprintf(stdout, "%02x",
+ fprintf(stderr, "%02x",
(unsigned char)fd_hash[cur_dup_entry].digest[i]);
- fprintf(stdout, "\n");
+ fprintf(stderr, "\n");
}
duplicate_count++;
count++;
continue;
} else {
if (verbose) {
- fprintf(stdout, "Packet: %u, Len: %u, MD5 Hash: ",
+ fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
count, phdr->caplen);
for (i = 0; i < 16; i++)
- fprintf(stdout, "%02x",
+ fprintf(stderr, "%02x",
(unsigned char)fd_hash[cur_dup_entry].digest[i]);
- fprintf(stdout, "\n");
+ fprintf(stderr, "\n");
}
}
}
int real_data_start = 0;
/* Protect non-protocol data */
- if (wtap_file_type(wth) == WTAP_FILE_CATAPULT_DCT2000)
+ if (wtap_file_type_subtype(wth) == WTAP_FILE_TYPE_SUBTYPE_CATAPULT_DCT2000)
real_data_start = find_dct2000_real_data(buf);
for (i = real_data_start; i < (int) phdr->caplen; i++) {
case WTAP_ERR_UNSUPPORTED_ENCAP:
/*
* This is a problem with the particular frame we're
- * writing; note that, and give the frame number.
+ * writing and the file type and subtype we're
+ * writing; note that, and report the frame number
+ * and file type/subtype.
+ */
+ fprintf(stderr,
+ "editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file\n.",
+ read_count, argv[optind],
+ wtap_file_type_subtype_string(out_file_type_subtype));
+ break;
+
+ case WTAP_ERR_PACKET_TOO_LARGE:
+ /*
+ * This is a problem with the particular frame we're
+ * writing and the file type and subtype we're
+ * writing; note that, and report the frame number
+ * and file type/subtype.
*/
fprintf(stderr,
- "editcap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
- read_count, argv[optind]);
+ "editcap: Frame %u of \"%s\" is too large for a \"%s\" file\n.",
+ read_count, argv[optind],
+ wtap_file_type_subtype_string(out_file_type_subtype));
break;
default:
g_free (filename);
filename = g_strdup(argv[optind+1]);
- pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+ pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth),
FALSE /* compressed */, shb_hdr, idb_inf, &err);
if (pdh == NULL) {
}
if (dup_detect) {
- fprintf(stdout, "%u packet%s seen, %u packet%s skipped with duplicate window of %u packets.\n",
+ fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate window of %i packets.\n",
count - 1, plurality(count - 1, "", "s"), duplicate_count,
plurality(duplicate_count, "", "s"), dup_window);
} else if (dup_detect_by_time) {
- fprintf(stdout, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
+ fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
count - 1, plurality(count - 1, "", "s"), duplicate_count,
plurality(duplicate_count, "", "s"),
(long)relative_time_window.secs,
}
/*
- * We support up to 2 chopping regions in a single pas, one specified by the
+ * We support up to 2 chopping regions in a single pass: one specified by the
* positive chop length, and one by the negative chop length.
*/
static void
handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
- const struct wtap_pkthdr *in_phdr, guint8 *buf,
+ const struct wtap_pkthdr *in_phdr, guint8 **buf,
gboolean adjlen)
{
/* If we're not chopping anything from one side, then the offset for that
*out_phdr = *in_phdr;
if (chop.off_begin_pos > 0) {
- memmove(&buf[chop.off_begin_pos],
- &buf[chop.off_begin_pos + chop.len_begin],
+ memmove(*buf + chop.off_begin_pos,
+ *buf + chop.off_begin_pos + chop.len_begin,
out_phdr->caplen - chop.len_begin);
} else {
- buf += chop.len_begin;
+ *buf += chop.len_begin;
}
out_phdr->caplen -= chop.len_begin;
*out_phdr = *in_phdr;
if (chop.off_end_neg < 0) {
- memmove(&buf[(gint)out_phdr->caplen + (chop.len_end + chop.off_end_neg)],
- &buf[(gint)out_phdr->caplen + chop.off_end_neg],
+ memmove(*buf + (gint)out_phdr->caplen + (chop.len_end + chop.off_end_neg),
+ *buf + (gint)out_phdr->caplen + chop.off_end_neg,
-chop.off_end_neg);
}
out_phdr->caplen += chop.len_end;
*
* Local variables:
* c-basic-offset: 4
- * tab-width: 4
+ * tab-width: 8
* indent-tabs-mode: nil
* End:
*
- * vi: set shiftwidth=4 tabstop=4 expandtab:
- * :indentSize=4:tabSize=4:noTabs=true:
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
*/