1 /* Edit capture files. We can delete packets, adjust timestamps, or
2 * simply convert from one format to another format.
6 * Originally written by Richard Sharpe.
7 * Improved by Guy Harris.
8 * Further improved by Richard Sharpe.
20 * Just make sure we include the prototype for strptime as well
21 * (needed for glibc 2.2) but make sure we do this only if not
38 #ifdef HAVE_SYS_TIME_H
49 #include <process.h> /* getpid */
52 #ifdef NEED_STRPTIME_H
53 # include "strptime.h"
56 #include "epan/crypt/crypt-md5.h"
58 #include "svnversion.h"
61 * Some globals so we can pass things to various routines
73 * Duplicate frame detection
75 typedef struct _fd_hash_t {
76 md5_byte_t digest[16];
81 fd_hash_t fd_hash[DUP_DEPTH];
84 #define ONE_MILLION 1000000
86 /* Weights of different errors we can introduce */
87 /* We should probably make these command-line arguments */
88 /* XXX - Should we add a bit-level error? */
89 #define ERR_WT_BIT 5 /* Flip a random bit */
90 #define ERR_WT_BYTE 5 /* Substitute a random byte */
91 #define ERR_WT_ALNUM 5 /* Substitute a random character in [A-Za-z0-9] */
92 #define ERR_WT_FMT 2 /* Substitute "%s" */
93 #define ERR_WT_AA 1 /* Fill the remainder of the buffer with 0xAA */
94 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
96 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
97 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
100 struct time_adjustment {
105 static struct select_item selectfrm[100];
106 static int max_selected = -1;
107 static int keep_em = 0;
108 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
109 static int out_frame_type = -2; /* Leave frame type alone */
110 static int verbose = 0; /* Not so verbose */
111 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
112 static double err_prob = 0.0;
113 static time_t starttime = 0;
114 static time_t stoptime = 0;
115 static gboolean check_startstop = FALSE;
116 static gboolean dup_detect = FALSE;
118 /* Add a selection item, a simple parser for now */
120 static void add_selection(char *sel)
125 if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
128 printf("Add_Selected: %s\n", sel);
130 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
132 printf("Not inclusive ...");
135 selectfrm[max_selected].inclusive = 0;
136 selectfrm[max_selected].first = atoi(sel);
138 printf(" %i\n", selectfrm[max_selected].first);
143 printf("Inclusive ...");
147 selectfrm[max_selected].inclusive = 1;
148 selectfrm[max_selected].first = atoi(sel);
149 selectfrm[max_selected].second = atoi(next);
151 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
158 /* Was the packet selected? */
160 static int selected(int recno)
164 for (i = 0; i<= max_selected; i++) {
166 if (selectfrm[i].inclusive) {
167 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
171 if (recno == selectfrm[i].first)
180 /* is the packet in the selected timeframe */
181 static gboolean check_timestamp(wtap *wth) {
182 struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
183 return ( (time_t) pkthdr->ts.secs >= starttime ) && ( (time_t) pkthdr->ts.secs <= stoptime );
187 set_time_adjustment(char *optarg)
196 /* skip leading whitespace */
197 while (*optarg == ' ' || *optarg == '\t') {
201 /* check for a negative adjustment */
202 if (*optarg == '-') {
203 time_adj.is_negative = 1;
207 /* collect whole number of seconds, if any */
208 if (*optarg == '.') { /* only fractional (i.e., .5 is ok) */
212 val = strtol(optarg, &frac, 10);
213 if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
214 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
218 if (val < 0) { /* implies '--' since we caught '-' above */
219 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
224 time_adj.tv.tv_sec = val;
226 /* now collect the partial seconds, if any */
227 if (*frac != '\0') { /* chars left, so get fractional part */
228 val = strtol(&(frac[1]), &end, 10);
229 if (*frac != '.' || end == NULL || end == frac
230 || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
231 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
237 return; /* no fractional digits */
240 /* adjust fractional portion from fractional to numerator
241 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
242 if (frac && end) { /* both are valid */
243 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
244 while(frac_digits < 6) { /* this is frac of 10^6 */
249 time_adj.tv.tv_usec = val;
253 is_duplicate(guint8* fd, guint32 len) {
258 if (cur_dup >= DUP_DEPTH)
261 /* Calculate our digest */
263 md5_append(&ms, fd, len);
264 md5_finish(&ms, fd_hash[cur_dup].digest);
266 fd_hash[cur_dup].len = len;
268 /* Look for duplicates */
269 for (i = 0; i < DUP_DEPTH; i++) {
273 if (fd_hash[i].len == fd_hash[cur_dup].len &&
274 memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
282 static void usage(void)
284 fprintf(stderr, "Editcap %s"
289 fprintf(stderr, "Edit and/or translate the format of capture files.\n");
290 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
291 fprintf(stderr, "\n");
292 fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
293 fprintf(stderr, "\n");
294 fprintf(stderr, "A single packet or a range of packets can be selected.\n");
295 fprintf(stderr, "\n");
296 fprintf(stderr, "Packets:\n");
297 fprintf(stderr, " -C <choplen> chop each packet at the end by <choplen> bytes\n");
298 fprintf(stderr, " -d remove duplicate packets\n");
299 fprintf(stderr, " -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
300 fprintf(stderr, " that a particular packet byte will be randomly changed\n");
301 fprintf(stderr, " -r keep the selected packets, default is to delete them\n");
302 fprintf(stderr, " -s <snaplen> truncate packets to max. <snaplen> bytes of data\n");
303 fprintf(stderr, " -t <time adjustment> adjust the timestamp of selected packets,\n");
304 fprintf(stderr, " <time adjustment> is in relative seconds (e.g. -0.5)\n");
305 fprintf(stderr, " -A <start time> don't output packets whose timestamp is before the\n");
306 fprintf(stderr, " given time (format as YYYY-MM-DD hh:mm:ss)\n");
307 fprintf(stderr, " -B <stop time> don't output packets whose timestamp is after the\n");
308 fprintf(stderr, " given time (format as YYYY-MM-DD hh:mm:ss)\n");
309 fprintf(stderr, "\n");
310 fprintf(stderr, "Output File(s):\n");
311 fprintf(stderr, " -c <packets per file> split the packet output to different files,\n");
312 fprintf(stderr, " with a maximum of <packets per file> each\n");
313 fprintf(stderr, " -F <capture type> set the output file type, default is libpcap\n");
314 fprintf(stderr, " an empty \"-F\" option will list the file types\n");
315 fprintf(stderr, " -T <encap type> set the output file encapsulation type,\n");
316 fprintf(stderr, " default is the same as the input file\n");
317 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types\n");
318 fprintf(stderr, "\n");
319 fprintf(stderr, "Miscellaneous:\n");
320 fprintf(stderr, " -h display this help and exit\n");
321 fprintf(stderr, " -v verbose output\n");
322 fprintf(stderr, "\n");
325 static void list_capture_types(void) {
328 fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
329 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
330 if (wtap_dump_can_open(i))
331 fprintf(stderr, " %s - %s\n",
332 wtap_file_type_short_string(i), wtap_file_type_string(i));
336 static void list_encap_types(void) {
340 fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
341 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
342 string = wtap_encap_short_string(i);
344 fprintf(stderr, " %s - %s\n",
345 string, wtap_encap_string(i));
349 int main(int argc, char *argv[])
359 unsigned int snaplen = 0; /* No limit */
360 unsigned int choplen = 0; /* No chop */
364 struct wtap_pkthdr snap_phdr;
365 const struct wtap_pkthdr *phdr;
368 int split_packet_count = 0;
369 int written_count = 0;
372 /* Process the options first */
374 while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
379 err_prob = strtod(optarg, &p);
380 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
381 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
385 srand( (unsigned int) (time(NULL) + getpid()) );
389 out_file_type = wtap_short_string_to_file_type(optarg);
390 if (out_file_type < 0) {
391 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
393 list_capture_types();
399 split_packet_count = strtol(optarg, &p, 10);
400 if (p == optarg || *p != '\0') {
401 fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
405 if (split_packet_count <= 0) {
406 fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
413 choplen = strtol(optarg, &p, 10);
414 if (p == optarg || *p != '\0') {
415 fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
423 for (i = 0; i < DUP_DEPTH; i++) {
424 memset(&fd_hash[i].digest, 0, 16);
429 case '?': /* Bad options if GNU getopt */
432 list_capture_types();
449 keep_em = !keep_em; /* Just invert */
453 snaplen = strtol(optarg, &p, 10);
454 if (p == optarg || *p != '\0') {
455 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
462 set_time_adjustment(optarg);
466 out_frame_type = wtap_short_string_to_encap(optarg);
467 if (out_frame_type < 0) {
468 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
476 verbose = !verbose; /* Just invert */
483 memset(&starttm,0,sizeof(struct tm));
485 if(!strptime(optarg,"%F %T",&starttm)) {
486 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
491 check_startstop = TRUE;
492 starttm.tm_isdst = -1;
494 starttime = mktime(&starttm);
501 memset(&stoptm,0,sizeof(struct tm));
503 if(!strptime(optarg,"%F %T",&stoptm)) {
504 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
508 check_startstop = TRUE;
509 stoptm.tm_isdst = -1;
510 stoptime = mktime(&stoptm);
518 printf("Optind = %i, argc = %i\n", optind, argc);
521 if ((argc - optind) < 1) {
528 if (check_startstop && !stoptime) {
530 /* XXX: will work until 2035 */
531 memset(&stoptm,0,sizeof(struct tm));
532 stoptm.tm_year = 135;
536 stoptime = mktime(&stoptm);
539 if (starttime > stoptime) {
540 fprintf(stderr, "editcap: start time is after the stop time\n");
544 wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
547 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
551 case WTAP_ERR_UNSUPPORTED:
552 case WTAP_ERR_UNSUPPORTED_ENCAP:
553 case WTAP_ERR_BAD_RECORD:
554 fprintf(stderr, "(%s)\n", err_info);
564 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
565 wtap_file_type_string(wtap_file_type(wth)));
570 * Now, process the rest, if any ... we only write if there is an extra
574 if ((argc - optind) >= 2) {
576 if (out_frame_type == -2)
577 out_frame_type = wtap_file_encap(wth);
579 if (split_packet_count > 0) {
580 filename = (char *) malloc(strlen(argv[optind+1]) + 20);
584 sprintf(filename, "%s-%05d", argv[optind+1], 0);
586 filename = argv[optind+1];
589 pdh = wtap_dump_open(filename, out_file_type,
590 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
593 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
599 for (i = optind + 2; i < argc; i++)
600 add_selection(argv[i]);
602 while (wtap_read(wth, &err, &err_info, &data_offset)) {
604 if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
605 if (!wtap_dump_close(pdh, &err)) {
607 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
612 sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
615 fprintf(stderr, "Continuing writing in file %s\n", filename);
618 pdh = wtap_dump_open(filename, out_file_type,
619 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
622 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
629 if ( ((check_startstop && check_timestamp(wth)) || (!check_startstop && !check_timestamp(wth))) && ((!selected(count) && !keep_em) ||
630 (selected(count) && keep_em)) ) {
633 printf("Packet: %u\n", count);
635 /* We simply write it, perhaps after truncating it; we could do other
636 things, like modify it. */
638 phdr = wtap_phdr(wth);
640 if (choplen != 0 && phdr->caplen > choplen) {
642 snap_phdr.caplen -= choplen;
646 if (snaplen != 0 && phdr->caplen > snaplen) {
648 snap_phdr.caplen = snaplen;
652 /* assume that if the frame's tv_sec is 0, then
653 * the timestamp isn't supported */
654 if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
656 if (time_adj.is_negative)
657 snap_phdr.ts.secs -= time_adj.tv.tv_sec;
659 snap_phdr.ts.secs += time_adj.tv.tv_sec;
663 /* assume that if the frame's tv_sec is 0, then
664 * the timestamp isn't supported */
665 if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
667 if (time_adj.is_negative) { /* subtract */
668 if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
670 snap_phdr.ts.nsecs += ONE_MILLION * 1000;
672 snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
674 if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
677 snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
679 snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
686 buf = wtap_buf_ptr(wth);
687 if (is_duplicate(buf, phdr->caplen)) {
689 printf("Skipping duplicate: %u\n", count);
695 if (err_prob > 0.0) {
696 buf = wtap_buf_ptr(wth);
697 for (i = 0; i < (int) phdr->caplen; i++) {
698 if (rand() <= err_prob * RAND_MAX) {
699 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
701 if (err_type < ERR_WT_BIT) {
702 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
703 err_type = ERR_WT_TOTAL;
705 err_type -= ERR_WT_BYTE;
708 if (err_type < ERR_WT_BYTE) {
709 buf[i] = rand() / (RAND_MAX / 255 + 1);
710 err_type = ERR_WT_TOTAL;
712 err_type -= ERR_WT_BYTE;
715 if (err_type < ERR_WT_ALNUM) {
716 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
717 err_type = ERR_WT_TOTAL;
719 err_type -= ERR_WT_ALNUM;
722 if (err_type < ERR_WT_FMT) {
723 if ((unsigned int)i < phdr->caplen - 2)
724 strcpy((char*) &buf[i], "%s");
725 err_type = ERR_WT_TOTAL;
727 err_type -= ERR_WT_FMT;
730 if (err_type < ERR_WT_AA) {
731 for (j = i; j < (int) phdr->caplen; j++) {
740 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
743 fprintf(stderr, "editcap: Error writing to %s: %s\n",
744 filename, wtap_strerror(err));
758 /* Print a message noting that the read failed somewhere along the line. */
760 "editcap: An error occurred while reading \"%s\": %s.\n",
761 argv[optind], wtap_strerror(err));
764 case WTAP_ERR_UNSUPPORTED:
765 case WTAP_ERR_UNSUPPORTED_ENCAP:
766 case WTAP_ERR_BAD_RECORD:
767 fprintf(stderr, "(%s)\n", err_info);
772 if (!wtap_dump_close(pdh, &err)) {
774 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,