1 /* Edit capture files. We can delete packets, adjust timestamps, or
2 * simply convert from one format to another format.
4 * Originally written by Richard Sharpe.
5 * Improved by Guy Harris.
6 * Further improved by Richard Sharpe.
8 * Copyright 2013, Richard Sharpe <realrichardsharpe[AT]gmail.com>
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 * Just make sure we include the prototype for strptime as well
38 * (needed for glibc 2.2) but make sure we do this only if not
57 #ifdef HAVE_SYS_TIME_H
62 #include <zlib.h> /* to get the libz version number */
67 #ifndef HAVE_GETOPT_LONG
68 #include "wsutil/wsgetopt.h"
72 #include <wsutil/file_util.h>
73 #include <wsutil/unicode-utils.h>
74 #include <process.h> /* getpid */
75 #ifdef HAVE_WINSOCK2_H
81 # include "wsutil/strptime.h"
84 #include <wsutil/privileges.h>
85 #include <wsutil/filesystem.h>
86 #include <wsutil/report_err.h>
87 #include <wsutil/strnatcmp.h>
88 #include <wsutil/md5.h>
89 #include <wsutil/plugins.h>
90 #include <wsutil/crash_info.h>
91 #include <wsutil/ws_version_info.h>
93 #include "version_info.h"
95 #include "ringbuffer.h" /* For RINGBUFFER_MAX_NUM_FILES */
98 * Some globals so we can pass things to various routines
107 * Duplicate frame detection
109 typedef struct _fd_hash_t {
110 md5_byte_t digest[16];
115 #define DEFAULT_DUP_DEPTH 5 /* Used with -d */
116 #define MAX_DUP_DEPTH 1000000 /* the maximum window (and actual size of fd_hash[]) for de-duplication */
118 static fd_hash_t fd_hash[MAX_DUP_DEPTH];
119 static int dup_window = DEFAULT_DUP_DEPTH;
120 static int cur_dup_entry = 0;
122 static int ignored_bytes = 0; /* Used with -I */
124 #define ONE_BILLION 1000000000
126 /* Weights of different errors we can introduce */
127 /* We should probably make these command-line arguments */
128 /* XXX - Should we add a bit-level error? */
129 #define ERR_WT_BIT 5 /* Flip a random bit */
130 #define ERR_WT_BYTE 5 /* Substitute a random byte */
131 #define ERR_WT_ALNUM 5 /* Substitute a random character in [A-Za-z0-9] */
132 #define ERR_WT_FMT 2 /* Substitute "%s" */
133 #define ERR_WT_AA 1 /* Fill the remainder of the buffer with 0xAA */
134 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
136 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
137 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
139 struct time_adjustment {
144 typedef struct _chop_t {
153 #define MAX_SELECTIONS 512
154 static struct select_item selectfrm[MAX_SELECTIONS];
155 static int max_selected = -1;
156 static int keep_em = 0;
157 #ifdef PCAP_NG_DEFAULT
158 static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng */
160 static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcap */
162 static int out_frame_type = -2; /* Leave frame type alone */
163 static int verbose = 0; /* Not so verbose */
164 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
165 static nstime_t relative_time_window = {0, 0}; /* de-dup time window */
166 static double err_prob = 0.0;
167 static time_t starttime = 0;
168 static time_t stoptime = 0;
169 static gboolean check_startstop = FALSE;
170 static gboolean dup_detect = FALSE;
171 static gboolean dup_detect_by_time = FALSE;
173 static int do_strict_time_adjustment = FALSE;
174 static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */
175 static nstime_t previous_time = {0, 0}; /* previous time */
177 static int find_dct2000_real_data(guint8 *buf);
178 static void handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
179 const struct wtap_pkthdr *in_phdr, guint8 **buf,
183 abs_time_to_str_with_sec_resolution(const nstime_t *abs_time)
186 gchar *buf = (gchar *)g_malloc(16);
188 tmp = localtime(&abs_time->secs);
191 g_snprintf(buf, 16, "%d%02d%02d%02d%02d%02d",
206 fileset_get_filename_by_pattern(guint idx, const struct wtap_pkthdr *phdr,
207 gchar *fprefix, gchar *fsuffix)
213 g_snprintf(filenum, sizeof(filenum), "%05u", idx % RINGBUFFER_MAX_NUM_FILES);
214 if (phdr->presence_flags & WTAP_HAS_TS) {
215 timestr = abs_time_to_str_with_sec_resolution(&phdr->ts);
216 abs_str = g_strconcat(fprefix, "_", filenum, "_", timestr, fsuffix, NULL);
219 abs_str = g_strconcat(fprefix, "_", filenum, fsuffix, NULL);
225 fileset_extract_prefix_suffix(const char *fname, gchar **fprefix, gchar **fsuffix)
227 char *pfx, *last_pathsep;
230 save_file = g_strdup(fname);
231 if (save_file == NULL) {
232 fprintf(stderr, "editcap: Out of memory\n");
236 last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
237 pfx = strrchr(save_file,'.');
238 if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
239 /* The pathname has a "." in it, and it's in the last component
240 * of the pathname (because there is either only one component,
241 * i.e. last_pathsep is null as there are no path separators,
242 * or the "." is after the path separator before the last
245 * Treat it as a separator between the rest of the file name and
246 * the file name suffix, and arrange that the names given to the
247 * ring buffer files have the specified suffix, i.e. put the
248 * changing part of the name *before* the suffix. */
250 *fprefix = g_strdup(save_file);
251 pfx[0] = '.'; /* restore capfile_name */
252 *fsuffix = g_strdup(pfx);
254 /* Either there's no "." in the pathname, or it's in a directory
255 * component, so the last component has no suffix. */
256 *fprefix = g_strdup(save_file);
263 /* Add a selection item, a simple parser for now */
265 add_selection(char *sel)
270 if (++max_selected >= MAX_SELECTIONS) {
271 /* Let the user know we stopped selecting */
272 fprintf(stderr, "Out of room for packet selections!\n");
277 fprintf(stderr, "Add_Selected: %s\n", sel);
279 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
281 fprintf(stderr, "Not inclusive ...");
283 selectfrm[max_selected].inclusive = 0;
284 selectfrm[max_selected].first = atoi(sel);
287 fprintf(stderr, " %i\n", selectfrm[max_selected].first);
290 fprintf(stderr, "Inclusive ...");
293 selectfrm[max_selected].inclusive = 1;
294 selectfrm[max_selected].first = atoi(sel);
295 selectfrm[max_selected].second = atoi(next);
298 fprintf(stderr, " %i, %i\n", selectfrm[max_selected].first,
299 selectfrm[max_selected].second);
305 /* Was the packet selected? */
312 for (i = 0; i <= max_selected; i++) {
313 if (selectfrm[i].inclusive) {
314 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
317 if (recno == selectfrm[i].first)
326 set_time_adjustment(char *optarg_str_p)
335 /* skip leading whitespace */
336 while (*optarg_str_p == ' ' || *optarg_str_p == '\t')
339 /* check for a negative adjustment */
340 if (*optarg_str_p == '-') {
341 time_adj.is_negative = 1;
345 /* collect whole number of seconds, if any */
346 if (*optarg_str_p == '.') { /* only fractional (i.e., .5 is ok) */
350 val = strtol(optarg_str_p, &frac, 10);
351 if (frac == NULL || frac == optarg_str_p
352 || val == LONG_MIN || val == LONG_MAX) {
353 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
357 if (val < 0) { /* implies '--' since we caught '-' above */
358 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
363 time_adj.tv.secs = val;
365 /* now collect the partial seconds, if any */
366 if (*frac != '\0') { /* chars left, so get fractional part */
367 val = strtol(&(frac[1]), &end, 10);
368 /* if more than 9 fractional digits truncate to 9 */
369 if ((end - &(frac[1])) > 9) {
370 frac[10] = 't'; /* 't' for truncate */
371 val = strtol(&(frac[1]), &end, 10);
373 if (*frac != '.' || end == NULL || end == frac || val < 0
374 || val > ONE_BILLION || val == LONG_MIN || val == LONG_MAX) {
375 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
380 return; /* no fractional digits */
383 /* adjust fractional portion from fractional to numerator
384 * e.g., in "1.5" from 5 to 500000000 since .5*10^9 = 500000000 */
385 if (frac && end) { /* both are valid */
386 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
387 while(frac_digits < 9) { /* this is frac of 10^9 */
392 time_adj.tv.nsecs = (int)val;
396 set_strict_time_adj(char *optarg_str_p)
405 /* skip leading whitespace */
406 while (*optarg_str_p == ' ' || *optarg_str_p == '\t')
410 * check for a negative adjustment
411 * A negative strict adjustment value is a flag
412 * to adjust all frames by the specifed delta time.
414 if (*optarg_str_p == '-') {
415 strict_time_adj.is_negative = 1;
419 /* collect whole number of seconds, if any */
420 if (*optarg_str_p == '.') { /* only fractional (i.e., .5 is ok) */
424 val = strtol(optarg_str_p, &frac, 10);
425 if (frac == NULL || frac == optarg_str_p
426 || val == LONG_MIN || val == LONG_MAX) {
427 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
431 if (val < 0) { /* implies '--' since we caught '-' above */
432 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
437 strict_time_adj.tv.secs = val;
439 /* now collect the partial seconds, if any */
440 if (*frac != '\0') { /* chars left, so get fractional part */
441 val = strtol(&(frac[1]), &end, 10);
442 /* if more than 9 fractional digits truncate to 9 */
443 if ((end - &(frac[1])) > 9) {
444 frac[10] = 't'; /* 't' for truncate */
445 val = strtol(&(frac[1]), &end, 10);
447 if (*frac != '.' || end == NULL || end == frac || val < 0
448 || val > ONE_BILLION || val == LONG_MIN || val == LONG_MAX) {
449 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
454 return; /* no fractional digits */
457 /* adjust fractional portion from fractional to numerator
458 * e.g., in "1.5" from 5 to 500000000 since .5*10^9 = 500000000 */
459 if (frac && end) { /* both are valid */
460 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
461 while(frac_digits < 9) { /* this is frac of 10^9 */
466 strict_time_adj.tv.nsecs = (int)val;
470 set_rel_time(char *optarg_str_p)
479 /* skip leading whitespace */
480 while (*optarg_str_p == ' ' || *optarg_str_p == '\t')
483 /* ignore negative adjustment */
484 if (*optarg_str_p == '-')
487 /* collect whole number of seconds, if any */
488 if (*optarg_str_p == '.') { /* only fractional (i.e., .5 is ok) */
492 val = strtol(optarg_str_p, &frac, 10);
493 if (frac == NULL || frac == optarg_str_p
494 || val == LONG_MIN || val == LONG_MAX) {
495 fprintf(stderr, "1: editcap: \"%s\" isn't a valid rel time value\n",
499 if (val < 0) { /* implies '--' since we caught '-' above */
500 fprintf(stderr, "2: editcap: \"%s\" isn't a valid rel time value\n",
505 relative_time_window.secs = val;
507 /* now collect the partial seconds, if any */
508 if (*frac != '\0') { /* chars left, so get fractional part */
509 val = strtol(&(frac[1]), &end, 10);
510 /* if more than 9 fractional digits truncate to 9 */
511 if ((end - &(frac[1])) > 9) {
512 frac[10] = 't'; /* 't' for truncate */
513 val = strtol(&(frac[1]), &end, 10);
515 if (*frac != '.' || end == NULL || end == frac || val < 0
516 || val > ONE_BILLION || val == LONG_MIN || val == LONG_MAX) {
517 fprintf(stderr, "3: editcap: \"%s\" isn't a valid rel time value\n",
522 return; /* no fractional digits */
525 /* adjust fractional portion from fractional to numerator
526 * e.g., in "1.5" from 5 to 500000000 since .5*10^9 = 500000000 */
527 if (frac && end) { /* both are valid */
528 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
529 while(frac_digits < 9) { /* this is frac of 10^9 */
534 relative_time_window.nsecs = (int)val;
538 is_duplicate(guint8* fd, guint32 len) {
542 /*Hint to ignore some bytes at the start of the frame for the digest calculation(-I option) */
546 new_fd = &fd[ignored_bytes];
547 new_len = len - (ignored_bytes);
550 if (cur_dup_entry >= dup_window)
553 /* Calculate our digest */
555 md5_append(&ms, new_fd, new_len);
556 md5_finish(&ms, fd_hash[cur_dup_entry].digest);
558 fd_hash[cur_dup_entry].len = len;
560 /* Look for duplicates */
561 for (i = 0; i < dup_window; i++) {
562 if (i == cur_dup_entry)
565 if (fd_hash[i].len == fd_hash[cur_dup_entry].len
566 && memcmp(fd_hash[i].digest, fd_hash[cur_dup_entry].digest, 16) == 0) {
575 is_duplicate_rel_time(guint8* fd, guint32 len, const nstime_t *current) {
579 /*Hint to ignore some bytes at the start of the frame for the digest calculation(-I option) */
583 new_fd = &fd[ignored_bytes];
584 new_len = len - (ignored_bytes);
587 if (cur_dup_entry >= dup_window)
590 /* Calculate our digest */
592 md5_append(&ms, new_fd, new_len);
593 md5_finish(&ms, fd_hash[cur_dup_entry].digest);
595 fd_hash[cur_dup_entry].len = len;
596 fd_hash[cur_dup_entry].time.secs = current->secs;
597 fd_hash[cur_dup_entry].time.nsecs = current->nsecs;
600 * Look for relative time related duplicates.
601 * This is hopefully a reasonably efficient mechanism for
602 * finding duplicates by rel time in the fd_hash[] cache.
603 * We check starting from the most recently added hash
604 * entries and work backwards towards older packets.
605 * This approach allows the dup test to be terminated
606 * when the relative time of a cached entry is found to
607 * be beyond the dup time window.
609 * Of course this assumes that the input trace file is
610 * "well-formed" in the sense that the packet timestamps are
611 * in strict chronologically increasing order (which is NOT
612 * always the case!!).
614 * The fd_hash[] table was deliberately created large (1,000,000).
615 * Looking for time related duplicates in large trace files with
616 * non-fractional dup time window values can potentially take
617 * a long time to complete.
620 for (i = cur_dup_entry - 1;; i--) {
627 if (i == cur_dup_entry) {
629 * We've decremented back to where we started.
635 if (nstime_is_unset(&(fd_hash[i].time))) {
637 * We've decremented to an unused fd_hash[] entry.
643 nstime_delta(&delta, current, &fd_hash[i].time);
645 if (delta.secs < 0 || delta.nsecs < 0) {
647 * A negative delta implies that the current packet
648 * has an absolute timestamp less than the cached packet
649 * that it is being compared to. This is NOT a normal
650 * situation since trace files usually have packets in
651 * chronological order (oldest to newest).
653 * There are several possible ways to deal with this:
654 * 1. 'continue' dup checking with the next cached frame.
655 * 2. 'break' from looking for a duplicate of the current frame.
656 * 3. Take the absolute value of the delta and see if that
657 * falls within the specifed dup time window.
659 * Currently this code does option 1. But it would pretty
660 * easy to add yet-another-editcap-option to select one of
661 * the other behaviors for dealing with out-of-sequence
667 cmp = nstime_cmp(&delta, &relative_time_window);
671 * The delta time indicates that we are now looking at
672 * cached packets beyond the specified dup time window.
676 } else if (fd_hash[i].len == fd_hash[cur_dup_entry].len
677 && memcmp(fd_hash[i].digest, fd_hash[cur_dup_entry].digest, 16) == 0) {
686 print_usage(FILE *output)
688 fprintf(output, "\n");
689 fprintf(output, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
690 fprintf(output, "\n");
691 fprintf(output, "<infile> and <outfile> must both be present.\n");
692 fprintf(output, "A single packet or a range of packets can be selected.\n");
693 fprintf(output, "\n");
694 fprintf(output, "Packet selection:\n");
695 fprintf(output, " -r keep the selected packets; default is to delete them.\n");
696 fprintf(output, " -A <start time> only output packets whose timestamp is after (or equal\n");
697 fprintf(output, " to) the given time (format as YYYY-MM-DD hh:mm:ss).\n");
698 fprintf(output, " -B <stop time> only output packets whose timestamp is before the\n");
699 fprintf(output, " given time (format as YYYY-MM-DD hh:mm:ss).\n");
700 fprintf(output, "\n");
701 fprintf(output, "Duplicate packet removal:\n");
702 fprintf(output, " -d remove packet if duplicate (window == %d).\n", DEFAULT_DUP_DEPTH);
703 fprintf(output, " -D <dup window> remove packet if duplicate; configurable <dup window>\n");
704 fprintf(output, " Valid <dup window> values are 0 to %d.\n", MAX_DUP_DEPTH);
705 fprintf(output, " NOTE: A <dup window> of 0 with -v (verbose option) is\n");
706 fprintf(output, " useful to print MD5 hashes.\n");
707 fprintf(output, " -w <dup time window> remove packet if duplicate packet is found EQUAL TO OR\n");
708 fprintf(output, " LESS THAN <dup time window> prior to current packet.\n");
709 fprintf(output, " A <dup time window> is specified in relative seconds\n");
710 fprintf(output, " (e.g. 0.000001).\n");
711 fprintf(output, "\n");
712 fprintf(output, " -I <bytes to ignore> ignore the specified bytes at the beginning of\n");
713 fprintf(output, " the frame during MD5 hash calculation\n");
714 fprintf(output, " Useful to remove duplicated packets taken on\n");
715 fprintf(output, " several routers(differents mac addresses for \n");
716 fprintf(output, " example)\n");
717 fprintf(output, " e.g. -I 26 in case of Ether/IP/ will ignore \n");
718 fprintf(output, " ether(14) and IP header(20 - 4(src ip) - 4(dst ip)).\n");
719 fprintf(output, "\n");
720 fprintf(output, " NOTE: The use of the 'Duplicate packet removal' options with\n");
721 fprintf(output, " other editcap options except -v may not always work as expected.\n");
722 fprintf(output, " Specifically the -r, -t or -S options will very likely NOT have the\n");
723 fprintf(output, " desired effect if combined with the -d, -D or -w.\n");
724 fprintf(output, "\n");
725 fprintf(output, "Packet manipulation:\n");
726 fprintf(output, " -s <snaplen> truncate each packet to max. <snaplen> bytes of data.\n");
727 fprintf(output, " -C [offset:]<choplen> chop each packet by <choplen> bytes. Positive values\n");
728 fprintf(output, " chop at the packet beginning, negative values at the\n");
729 fprintf(output, " packet end. If an optional offset precedes the length,\n");
730 fprintf(output, " then the bytes chopped will be offset from that value.\n");
731 fprintf(output, " Positive offsets are from the packet beginning,\n");
732 fprintf(output, " negative offsets are from the packet end. You can use\n");
733 fprintf(output, " this option more than once, allowing up to 2 chopping\n");
734 fprintf(output, " regions within a packet provided that at least 1\n");
735 fprintf(output, " choplen is positive and at least 1 is negative.\n");
736 fprintf(output, " -L adjust the frame length when chopping and/or snapping\n");
737 fprintf(output, " -t <time adjustment> adjust the timestamp of each packet;\n");
738 fprintf(output, " <time adjustment> is in relative seconds (e.g. -0.5).\n");
739 fprintf(output, " -S <strict adjustment> adjust timestamp of packets if necessary to insure\n");
740 fprintf(output, " strict chronological increasing order. The <strict\n");
741 fprintf(output, " adjustment> is specified in relative seconds with\n");
742 fprintf(output, " values of 0 or 0.000001 being the most reasonable.\n");
743 fprintf(output, " A negative adjustment value will modify timestamps so\n");
744 fprintf(output, " that each packet's delta time is the absolute value\n");
745 fprintf(output, " of the adjustment specified. A value of -0 will set\n");
746 fprintf(output, " all packets to the timestamp of the first packet.\n");
747 fprintf(output, " -E <error probability> set the probability (between 0.0 and 1.0 incl.) that\n");
748 fprintf(output, " a particular packet byte will be randomly changed.\n");
749 fprintf(output, "\n");
750 fprintf(output, "Output File(s):\n");
751 fprintf(output, " -c <packets per file> split the packet output to different files based on\n");
752 fprintf(output, " uniform packet counts with a maximum of\n");
753 fprintf(output, " <packets per file> each.\n");
754 fprintf(output, " -i <seconds per file> split the packet output to different files based on\n");
755 fprintf(output, " uniform time intervals with a maximum of\n");
756 fprintf(output, " <seconds per file> each.\n");
757 fprintf(output, " -F <capture type> set the output file type; default is pcapng. An empty\n");
758 fprintf(output, " \"-F\" option will list the file types.\n");
759 fprintf(output, " -T <encap type> set the output file encapsulation type; default is the\n");
760 fprintf(output, " same as the input file. An empty \"-T\" option will\n");
761 fprintf(output, " list the encapsulation types.\n");
762 fprintf(output, "\n");
763 fprintf(output, "Miscellaneous:\n");
764 fprintf(output, " -h display this help and exit.\n");
765 fprintf(output, " -v verbose output.\n");
766 fprintf(output, " If -v is used with any of the 'Duplicate Packet\n");
767 fprintf(output, " Removal' options (-d, -D or -w) then Packet lengths\n");
768 fprintf(output, " and MD5 hashes are printed to standard-error.\n");
769 fprintf(output, "\n");
773 const char *sstr; /* The short string */
774 const char *lstr; /* The long string */
778 string_compare(gconstpointer a, gconstpointer b)
780 return strcmp(((const struct string_elem *)a)->sstr,
781 ((const struct string_elem *)b)->sstr);
785 string_nat_compare(gconstpointer a, gconstpointer b)
787 return ws_ascii_strnatcmp(((const struct string_elem *)a)->sstr,
788 ((const struct string_elem *)b)->sstr);
792 string_elem_print(gpointer data, gpointer not_used _U_)
794 fprintf(stderr, " %s - %s\n",
795 ((struct string_elem *)data)->sstr,
796 ((struct string_elem *)data)->lstr);
800 list_capture_types(void) {
802 struct string_elem *captypes;
805 captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
806 fprintf(stderr, "editcap: The available capture file types for the \"-F\" flag are:\n");
807 for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
808 if (wtap_dump_can_open(i)) {
809 captypes[i].sstr = wtap_file_type_subtype_short_string(i);
810 captypes[i].lstr = wtap_file_type_subtype_string(i);
811 list = g_slist_insert_sorted(list, &captypes[i], string_compare);
814 g_slist_foreach(list, string_elem_print, NULL);
820 list_encap_types(void) {
822 struct string_elem *encaps;
825 encaps = (struct string_elem *)g_malloc(sizeof(struct string_elem) * WTAP_NUM_ENCAP_TYPES);
826 fprintf(stderr, "editcap: The available encapsulation types for the \"-T\" flag are:\n");
827 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
828 encaps[i].sstr = wtap_encap_short_string(i);
829 if (encaps[i].sstr != NULL) {
830 encaps[i].lstr = wtap_encap_string(i);
831 list = g_slist_insert_sorted(list, &encaps[i], string_nat_compare);
834 g_slist_foreach(list, string_elem_print, NULL);
841 * Don't report failures to load plugins because most (non-wiretap) plugins
842 * *should* fail to load (because we're not linked against libwireshark and
843 * dissector plugins need libwireshark).
846 failure_message(const char *msg_format _U_, va_list ap _U_)
852 get_editcap_compiled_info(GString *str)
855 g_string_append(str, ", ");
857 g_string_append(str, "with libz ");
859 g_string_append(str, ZLIB_VERSION);
860 #else /* ZLIB_VERSION */
861 g_string_append(str, "(version unknown)");
862 #endif /* ZLIB_VERSION */
863 #else /* HAVE_LIBZ */
864 g_string_append(str, "without libz");
865 #endif /* HAVE_LIBZ */
869 get_editcap_runtime_info(GString *str)
872 #if defined(HAVE_LIBZ) && !defined(_WIN32)
873 g_string_append_printf(str, ", with libz %s", zlibVersion());
878 main(int argc, char *argv[])
880 GString *comp_info_str;
881 GString *runtime_info_str;
886 static const struct option long_options[] = {
887 {(char *)"help", no_argument, NULL, 'h'},
888 {(char *)"version", no_argument, NULL, 'V'},
893 guint32 snaplen = 0; /* No limit */
894 chop_t chop = {0, 0, 0, 0, 0, 0}; /* No chop */
895 gboolean adjlen = FALSE;
896 wtap_dumper *pdh = NULL;
897 unsigned int count = 1;
898 unsigned int duplicate_count = 0;
902 guint32 read_count = 0;
903 int split_packet_count = 0;
904 int written_count = 0;
905 char *filename = NULL;
907 int secs_per_block = 0;
909 nstime_t block_start;
910 gchar *fprefix = NULL;
911 gchar *fsuffix = NULL;
913 const struct wtap_pkthdr *phdr;
914 struct wtap_pkthdr snap_phdr;
915 wtapng_iface_descriptions_t *idb_inf;
916 wtapng_section_t *shb_hdr;
919 char* init_progfile_dir_error;
923 arg_list_utf_16to8(argc, argv);
924 create_app_running_mutex();
927 /* Assemble the compile-time version information string */
928 comp_info_str = g_string_new("Compiled ");
929 get_compiled_version_info(comp_info_str, NULL, get_editcap_compiled_info);
931 /* Assemble the run-time version information string */
932 runtime_info_str = g_string_new("Running ");
933 get_runtime_version_info(runtime_info_str, get_editcap_runtime_info);
935 /* Add it to the information to be reported on a crash. */
936 ws_add_crash_info("Editcap (Wireshark) %s\n"
941 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
944 * Get credential information for later use.
946 init_process_policies();
947 init_open_routines();
950 /* Register wiretap plugins */
951 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
952 g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
953 g_free(init_progfile_dir_error);
955 /* Register all the plugin types we have. */
956 wtap_register_plugin_types(); /* Types known to libwiretap */
958 init_report_err(failure_message,NULL,NULL,NULL);
960 /* Scan for plugins. This does *not* call their registration routines;
961 that's done later. */
964 /* Register all libwiretap plugin modules. */
965 register_all_wiretap_modules();
969 /* Process the options */
970 while ((opt = getopt_long(argc, argv, "A:B:c:C:dD:E:F:hi:I:Lrs:S:t:T:vVw:", long_options, NULL)) != -1) {
976 memset(&starttm,0,sizeof(struct tm));
978 if (!strptime(optarg,"%Y-%m-%d %T", &starttm)) {
979 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
984 check_startstop = TRUE;
985 starttm.tm_isdst = -1;
987 starttime = mktime(&starttm);
995 memset(&stoptm,0,sizeof(struct tm));
997 if (!strptime(optarg,"%Y-%m-%d %T", &stoptm)) {
998 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
1002 check_startstop = TRUE;
1003 stoptm.tm_isdst = -1;
1004 stoptime = mktime(&stoptm);
1009 split_packet_count = (int)strtol(optarg, &p, 10);
1010 if (p == optarg || *p != '\0') {
1011 fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
1015 if (split_packet_count <= 0) {
1016 fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
1017 split_packet_count);
1024 int choplen = 0, chopoff = 0;
1026 switch (sscanf(optarg, "%d:%d", &chopoff, &choplen)) {
1027 case 1: /* only the chop length was specififed */
1032 case 2: /* both an offset and chop length was specified */
1036 fprintf(stderr, "editcap: \"%s\" isn't a valid chop length or offset:length\n",
1043 chop.len_begin += choplen;
1045 chop.off_begin_pos += chopoff;
1047 chop.off_begin_neg += chopoff;
1048 } else if (choplen < 0) {
1049 chop.len_end += choplen;
1051 chop.off_end_pos += chopoff;
1053 chop.off_end_neg += chopoff;
1060 dup_detect_by_time = FALSE;
1061 dup_window = DEFAULT_DUP_DEPTH;
1066 dup_detect_by_time = FALSE;
1067 dup_window = (int)strtol(optarg, &p, 10);
1068 if (p == optarg || *p != '\0') {
1069 fprintf(stderr, "editcap: \"%s\" isn't a valid duplicate window value\n",
1073 if (dup_window < 0 || dup_window > MAX_DUP_DEPTH) {
1074 fprintf(stderr, "editcap: \"%d\" duplicate window value must be between 0 and %d inclusive.\n",
1075 dup_window, MAX_DUP_DEPTH);
1081 err_prob = strtod(optarg, &p);
1082 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
1083 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
1087 srand( (unsigned int) (time(NULL) + getpid()) );
1091 out_file_type_subtype = wtap_short_string_to_file_type_subtype(optarg);
1092 if (out_file_type_subtype < 0) {
1093 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
1095 list_capture_types();
1101 printf("Editcap (Wireshark) %s\n"
1102 "Edit and/or translate the format of capture files.\n"
1103 "See http://www.wireshark.org for more information.\n",
1104 get_ws_vcs_version_info());
1105 print_usage(stdout);
1109 case 'i': /* break capture file based on time interval */
1110 secs_per_block = atoi(optarg);
1111 if (secs_per_block <= 0) {
1112 fprintf(stderr, "editcap: \"%s\" isn't a valid time interval\n\n",
1118 case 'I': /* ignored_bytes at the beginning of the frame for duplications removal */
1119 ignored_bytes = atoi(optarg);
1120 if(ignored_bytes <= 0) {
1121 fprintf(stderr, "editcap: \"%s\" isn't a valid number of bytes to ignore\n", optarg);
1131 keep_em = !keep_em; /* Just invert */
1135 snaplen = (guint32)strtol(optarg, &p, 10);
1136 if (p == optarg || *p != '\0') {
1137 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
1144 set_strict_time_adj(optarg);
1145 do_strict_time_adjustment = TRUE;
1149 set_time_adjustment(optarg);
1153 out_frame_type = wtap_short_string_to_encap(optarg);
1154 if (out_frame_type < 0) {
1155 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
1163 verbose = !verbose; /* Just invert */
1167 show_version("Editcap (Wireshark)", comp_info_str, runtime_info_str);
1168 g_string_free(comp_info_str, TRUE);
1169 g_string_free(runtime_info_str, TRUE);
1175 dup_detect_by_time = TRUE;
1176 dup_window = MAX_DUP_DEPTH;
1177 set_rel_time(optarg);
1180 case '?': /* Bad options if GNU getopt */
1183 list_capture_types();
1189 print_usage(stderr);
1198 fprintf(stderr, "Optind = %i, argc = %i\n", optind, argc);
1201 if ((argc - optind) < 1) {
1202 print_usage(stderr);
1206 if (check_startstop && !stoptime) {
1209 /* XXX: will work until 2035 */
1210 memset(&stoptm,0,sizeof(struct tm));
1211 stoptm.tm_year = 135;
1212 stoptm.tm_mday = 31;
1215 stoptime = mktime(&stoptm);
1218 nstime_set_unset(&block_start);
1220 if (starttime > stoptime) {
1221 fprintf(stderr, "editcap: start time is after the stop time\n");
1225 if (split_packet_count > 0 && secs_per_block > 0) {
1226 fprintf(stderr, "editcap: can't split on both packet count and time interval\n");
1227 fprintf(stderr, "editcap: at the same time\n");
1231 wth = wtap_open_offline(argv[optind], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1234 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
1235 wtap_strerror(err));
1236 if (err_info != NULL) {
1237 fprintf(stderr, "(%s)\n", err_info);
1244 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
1245 wtap_file_type_subtype_string(wtap_file_type_subtype(wth)));
1248 shb_hdr = wtap_file_get_shb_info(wth);
1249 idb_inf = wtap_file_get_idb_info(wth);
1252 * Now, process the rest, if any ... we only write if there is an extra
1253 * argument or so ...
1256 if ((argc - optind) >= 2) {
1257 if (out_frame_type == -2)
1258 out_frame_type = wtap_file_encap(wth);
1260 for (i = optind + 2; i < argc; i++)
1261 if (add_selection(argv[i]) == FALSE)
1264 if (dup_detect || dup_detect_by_time) {
1265 for (i = 0; i < dup_window; i++) {
1266 memset(&fd_hash[i].digest, 0, 16);
1268 nstime_set_unset(&fd_hash[i].time);
1272 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1275 phdr = wtap_phdr(wth);
1277 if (read_count == 1) { /* the first packet */
1278 if (split_packet_count > 0 || secs_per_block > 0) {
1279 if (!fileset_extract_prefix_suffix(argv[optind+1], &fprefix, &fsuffix))
1282 filename = fileset_get_filename_by_pattern(block_cnt++, phdr, fprefix, fsuffix);
1284 filename = g_strdup(argv[optind+1]);
1288 /* If we don't have an application name add Editcap */
1289 if (shb_hdr->shb_user_appl == NULL) {
1290 shb_hdr->shb_user_appl = g_strdup("Editcap " VERSION);
1293 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1294 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
1295 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1298 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1299 filename, wtap_strerror(err));
1304 buf = wtap_buf_ptr(wth);
1307 * Not all packets have time stamps. Only process the time
1308 * stamp if we have one.
1310 if (phdr->presence_flags & WTAP_HAS_TS) {
1311 if (nstime_is_unset(&block_start)) {
1312 block_start.secs = phdr->ts.secs;
1313 block_start.nsecs = phdr->ts.nsecs;
1316 if (secs_per_block > 0) {
1317 while ((phdr->ts.secs - block_start.secs > secs_per_block)
1318 || (phdr->ts.secs - block_start.secs == secs_per_block
1319 && phdr->ts.nsecs >= block_start.nsecs )) { /* time for the next file */
1321 if (!wtap_dump_close(pdh, &err)) {
1322 fprintf(stderr, "editcap: Error writing to %s: %s\n",
1323 filename, wtap_strerror(err));
1326 block_start.secs = block_start.secs + secs_per_block; /* reset for next interval */
1328 filename = fileset_get_filename_by_pattern(block_cnt++, phdr, fprefix, fsuffix);
1332 fprintf(stderr, "Continuing writing in file %s\n", filename);
1334 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1335 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
1336 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1339 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1340 filename, wtap_strerror(err));
1347 if (split_packet_count > 0) {
1348 /* time for the next file? */
1349 if (written_count > 0 && written_count % split_packet_count == 0) {
1350 if (!wtap_dump_close(pdh, &err)) {
1351 fprintf(stderr, "editcap: Error writing to %s: %s\n",
1352 filename, wtap_strerror(err));
1357 filename = fileset_get_filename_by_pattern(block_cnt++, phdr, fprefix, fsuffix);
1361 fprintf(stderr, "Continuing writing in file %s\n", filename);
1363 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1364 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
1365 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1367 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1368 filename, wtap_strerror(err));
1374 if (check_startstop) {
1376 * Is the packet in the selected timeframe?
1377 * If the packet has no time stamp, the answer is "no".
1379 if (phdr->presence_flags & WTAP_HAS_TS)
1380 ts_okay = (phdr->ts.secs >= starttime) && (phdr->ts.secs < stoptime);
1385 * No selected timeframe, so all packets are "in the
1386 * selected timeframe".
1391 if (ts_okay && ((!selected(count) && !keep_em)
1392 || (selected(count) && keep_em))) {
1394 if (verbose && !dup_detect && !dup_detect_by_time)
1395 fprintf(stderr, "Packet: %u\n", count);
1397 /* We simply write it, perhaps after truncating it; we could
1398 * do other things, like modify it. */
1400 phdr = wtap_phdr(wth);
1403 if (phdr->caplen > snaplen) {
1405 snap_phdr.caplen = snaplen;
1408 if (adjlen && phdr->len > snaplen) {
1410 snap_phdr.len = snaplen;
1417 handle_chopping(chop, &snap_phdr, phdr, &buf, adjlen);
1420 if (phdr->presence_flags & WTAP_HAS_TS) {
1421 /* Do we adjust timestamps to ensure strict chronological
1423 if (do_strict_time_adjustment) {
1424 if (previous_time.secs || previous_time.nsecs) {
1425 if (!strict_time_adj.is_negative) {
1429 current.secs = phdr->ts.secs;
1430 current.nsecs = phdr->ts.nsecs;
1432 nstime_delta(&delta, ¤t, &previous_time);
1434 if (delta.secs < 0 || delta.nsecs < 0) {
1436 * A negative delta indicates that the current packet
1437 * has an absolute timestamp less than the previous packet
1438 * that it is being compared to. This is NOT a normal
1439 * situation since trace files usually have packets in
1440 * chronological order (oldest to newest).
1442 /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */
1444 snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.secs;
1445 snap_phdr.ts.nsecs = previous_time.nsecs;
1446 if (snap_phdr.ts.nsecs + strict_time_adj.tv.nsecs > ONE_BILLION) {
1448 snap_phdr.ts.secs++;
1449 snap_phdr.ts.nsecs += strict_time_adj.tv.nsecs - ONE_BILLION;
1451 snap_phdr.ts.nsecs += strict_time_adj.tv.nsecs;
1457 * A negative strict time adjustment is requested.
1458 * Unconditionally set each timestamp to previous
1459 * packet's timestamp plus delta.
1462 snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.secs;
1463 snap_phdr.ts.nsecs = previous_time.nsecs;
1464 if (snap_phdr.ts.nsecs + strict_time_adj.tv.nsecs > ONE_BILLION) {
1466 snap_phdr.ts.secs++;
1467 snap_phdr.ts.nsecs += strict_time_adj.tv.nsecs - ONE_BILLION;
1469 snap_phdr.ts.nsecs += strict_time_adj.tv.nsecs;
1474 previous_time.secs = phdr->ts.secs;
1475 previous_time.nsecs = phdr->ts.nsecs;
1478 /* assume that if the frame's tv_sec is 0, then
1479 * the timestamp isn't supported */
1480 if (phdr->ts.secs > 0 && time_adj.tv.secs != 0) {
1482 if (time_adj.is_negative)
1483 snap_phdr.ts.secs -= time_adj.tv.secs;
1485 snap_phdr.ts.secs += time_adj.tv.secs;
1489 /* assume that if the frame's tv_sec is 0, then
1490 * the timestamp isn't supported */
1491 if (phdr->ts.secs > 0 && time_adj.tv.nsecs != 0) {
1493 if (time_adj.is_negative) { /* subtract */
1494 if (snap_phdr.ts.nsecs < time_adj.tv.nsecs) { /* borrow */
1495 snap_phdr.ts.secs--;
1496 snap_phdr.ts.nsecs += ONE_BILLION;
1498 snap_phdr.ts.nsecs -= time_adj.tv.nsecs;
1500 if (snap_phdr.ts.nsecs + time_adj.tv.nsecs > ONE_BILLION) {
1502 snap_phdr.ts.secs++;
1503 snap_phdr.ts.nsecs += time_adj.tv.nsecs - ONE_BILLION;
1505 snap_phdr.ts.nsecs += time_adj.tv.nsecs;
1512 /* suppress duplicates by packet window */
1514 if (is_duplicate(buf, phdr->caplen)) {
1516 fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
1517 count, phdr->caplen);
1518 for (i = 0; i < 16; i++)
1519 fprintf(stderr, "%02x",
1520 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1521 fprintf(stderr, "\n");
1528 fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
1529 count, phdr->caplen);
1530 for (i = 0; i < 16; i++)
1531 fprintf(stderr, "%02x",
1532 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1533 fprintf(stderr, "\n");
1538 if (phdr->presence_flags & WTAP_HAS_TS) {
1539 /* suppress duplicates by time window */
1540 if (dup_detect_by_time) {
1543 current.secs = phdr->ts.secs;
1544 current.nsecs = phdr->ts.nsecs;
1546 if (is_duplicate_rel_time(buf, phdr->caplen, ¤t)) {
1548 fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
1549 count, phdr->caplen);
1550 for (i = 0; i < 16; i++)
1551 fprintf(stderr, "%02x",
1552 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1553 fprintf(stderr, "\n");
1560 fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
1561 count, phdr->caplen);
1562 for (i = 0; i < 16; i++)
1563 fprintf(stderr, "%02x",
1564 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1565 fprintf(stderr, "\n");
1571 /* Random error mutation */
1572 if (err_prob > 0.0) {
1573 int real_data_start = 0;
1575 /* Protect non-protocol data */
1576 if (wtap_file_type_subtype(wth) == WTAP_FILE_TYPE_SUBTYPE_CATAPULT_DCT2000)
1577 real_data_start = find_dct2000_real_data(buf);
1579 for (i = real_data_start; i < (int) phdr->caplen; i++) {
1580 if (rand() <= err_prob * RAND_MAX) {
1581 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
1583 if (err_type < ERR_WT_BIT) {
1584 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
1585 err_type = ERR_WT_TOTAL;
1587 err_type -= ERR_WT_BYTE;
1590 if (err_type < ERR_WT_BYTE) {
1591 buf[i] = rand() / (RAND_MAX / 255 + 1);
1592 err_type = ERR_WT_TOTAL;
1594 err_type -= ERR_WT_BYTE;
1597 if (err_type < ERR_WT_ALNUM) {
1598 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
1599 err_type = ERR_WT_TOTAL;
1601 err_type -= ERR_WT_ALNUM;
1604 if (err_type < ERR_WT_FMT) {
1605 if ((unsigned int)i < phdr->caplen - 2)
1606 g_strlcpy((char*) &buf[i], "%s", 2);
1607 err_type = ERR_WT_TOTAL;
1609 err_type -= ERR_WT_FMT;
1612 if (err_type < ERR_WT_AA) {
1613 for (j = i; j < (int) phdr->caplen; j++)
1621 if (!wtap_dump(pdh, phdr, buf, &err, &err_info)) {
1623 case WTAP_ERR_UNWRITABLE_ENCAP:
1625 * This is a problem with the particular frame we're
1626 * writing and the file type and subtype we're
1627 * writing; note that, and report the frame number
1628 * and file type/subtype.
1631 "editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n",
1632 read_count, argv[optind],
1633 wtap_file_type_subtype_string(out_file_type_subtype));
1636 case WTAP_ERR_PACKET_TOO_LARGE:
1638 * This is a problem with the particular frame we're
1639 * writing and the file type and subtype we're
1640 * writing; note that, and report the frame number
1641 * and file type/subtype.
1644 "editcap: Frame %u of \"%s\" is too large for a \"%s\" file.\n",
1645 read_count, argv[optind],
1646 wtap_file_type_subtype_string(out_file_type_subtype));
1649 case WTAP_ERR_UNWRITABLE_REC_TYPE:
1651 * This is a problem with the particular record we're
1652 * writing and the file type and subtype we're
1653 * writing; note that, and report the record number
1654 * and file type/subtype.
1657 "editcap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
1658 read_count, argv[optind],
1659 wtap_file_type_subtype_string(out_file_type_subtype));
1662 case WTAP_ERR_UNWRITABLE_REC_DATA:
1664 * This is a problem with the particular record we're
1665 * writing and the file type and subtype we're
1666 * writing; note that, and report the record number
1667 * and file type/subtype.
1670 "editcap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
1671 read_count, argv[optind],
1672 wtap_file_type_subtype_string(out_file_type_subtype),
1673 err_info != NULL ? err_info : "no information supplied");
1678 fprintf(stderr, "editcap: Error writing to %s: %s\n",
1679 filename, wtap_strerror(err));
1693 /* Print a message noting that the read failed somewhere along the
1696 "editcap: An error occurred while reading \"%s\": %s.\n",
1697 argv[optind], wtap_strerror(err));
1698 if (err_info != NULL) {
1699 fprintf(stderr, "(%s)\n", err_info);
1705 /* No valid packages found, open the outfile so we can write an
1708 filename = g_strdup(argv[optind+1]);
1710 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1711 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth),
1712 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1714 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1715 filename, wtap_strerror(err));
1723 if (!wtap_dump_close(pdh, &err)) {
1724 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
1725 wtap_strerror(err));
1733 fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate window of %i packets.\n",
1734 count - 1, plurality(count - 1, "", "s"), duplicate_count,
1735 plurality(duplicate_count, "", "s"), dup_window);
1736 } else if (dup_detect_by_time) {
1737 fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
1738 count - 1, plurality(count - 1, "", "s"), duplicate_count,
1739 plurality(duplicate_count, "", "s"),
1740 (long)relative_time_window.secs,
1741 (long int)relative_time_window.nsecs);
1747 /* Skip meta-information read from file to return offset of real
1750 find_dct2000_real_data(guint8 *buf)
1754 for (n = 0; buf[n] != '\0'; n++); /* Context name */
1756 n++; /* Context port number */
1757 for (; buf[n] != '\0'; n++); /* Timestamp */
1759 for (; buf[n] != '\0'; n++); /* Protocol name */
1761 for (; buf[n] != '\0'; n++); /* Variant number (as string) */
1763 for (; buf[n] != '\0'; n++); /* Outhdr (as string) */
1765 n += 2; /* Direction & encap */
1771 * We support up to 2 chopping regions in a single pass: one specified by the
1772 * positive chop length, and one by the negative chop length.
1775 handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
1776 const struct wtap_pkthdr *in_phdr, guint8 **buf,
1779 /* Only packets can be chopped. */
1780 if (in_phdr->rec_type != REC_TYPE_PACKET)
1783 /* If we're not chopping anything from one side, then the offset for that
1784 * side is meaningless. */
1785 if (chop.len_begin == 0)
1786 chop.off_begin_pos = chop.off_begin_neg = 0;
1787 if (chop.len_end == 0)
1788 chop.off_end_pos = chop.off_end_neg = 0;
1790 if (chop.off_begin_neg < 0) {
1791 chop.off_begin_pos += in_phdr->caplen + chop.off_begin_neg;
1792 chop.off_begin_neg = 0;
1794 if (chop.off_end_pos > 0) {
1795 chop.off_end_neg += chop.off_end_pos - in_phdr->caplen;
1796 chop.off_end_pos = 0;
1799 /* If we've crossed chopping regions, swap them */
1800 if (chop.len_begin && chop.len_end) {
1801 if (chop.off_begin_pos > ((int)in_phdr->caplen + chop.off_end_neg)) {
1802 int tmp_len, tmp_off;
1804 tmp_off = in_phdr->caplen + chop.off_end_neg + chop.len_end;
1805 tmp_len = -chop.len_end;
1807 chop.off_end_neg = chop.len_begin + chop.off_begin_pos - in_phdr->caplen;
1808 chop.len_end = -chop.len_begin;
1810 chop.len_begin = tmp_len;
1811 chop.off_begin_pos = tmp_off;
1815 /* Make sure we don't chop off more than we have available */
1816 if (in_phdr->caplen < (guint32)(chop.off_begin_pos - chop.off_end_neg)) {
1820 if ((guint32)(chop.len_begin - chop.len_end) >
1821 (in_phdr->caplen - (guint32)(chop.off_begin_pos - chop.off_end_neg))) {
1822 chop.len_begin = in_phdr->caplen - (chop.off_begin_pos - chop.off_end_neg);
1826 /* Handle chopping from the beginning. Note that if a beginning offset
1827 * was specified, we need to keep that piece */
1828 if (chop.len_begin > 0) {
1829 *out_phdr = *in_phdr;
1831 if (chop.off_begin_pos > 0) {
1832 memmove(*buf + chop.off_begin_pos,
1833 *buf + chop.off_begin_pos + chop.len_begin,
1834 out_phdr->caplen - chop.len_begin);
1836 *buf += chop.len_begin;
1838 out_phdr->caplen -= chop.len_begin;
1841 if (in_phdr->len > (guint32)chop.len_begin)
1842 out_phdr->len -= chop.len_begin;
1849 /* Handle chopping from the end. Note that if an ending offset was
1850 * specified, we need to keep that piece */
1851 if (chop.len_end < 0) {
1852 *out_phdr = *in_phdr;
1854 if (chop.off_end_neg < 0) {
1855 memmove(*buf + (gint)out_phdr->caplen + (chop.len_end + chop.off_end_neg),
1856 *buf + (gint)out_phdr->caplen + chop.off_end_neg,
1859 out_phdr->caplen += chop.len_end;
1862 if (((signed int) in_phdr->len + chop.len_end) > 0)
1863 out_phdr->len += chop.len_end;
1867 /*in_phdr = out_phdr;*/
1872 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1877 * indent-tabs-mode: nil
1880 * vi: set shiftwidth=4 tabstop=8 expandtab:
1881 * :indentSize=4:tabSize=8:noTabs=true: