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.
21 * Just make sure we include the prototype for strptime as well
22 * (needed for glibc 2.2) but make sure we do this only if not
39 #ifdef HAVE_SYS_TIME_H
50 #include <process.h> /* getpid */
53 #ifdef NEED_STRPTIME_H
54 # include "strptime.h"
57 #include "epan/crypt/crypt-md5.h"
58 #include "epan/plugins.h"
59 #include "epan/report_err.h"
60 #include "epan/filesystem.h"
62 #include "svnversion.h"
65 * Some globals so we can pass things to various routines
77 * Duplicate frame detection
79 typedef struct _fd_hash_t {
80 md5_byte_t digest[16];
85 fd_hash_t fd_hash[DUP_DEPTH];
88 #define ONE_MILLION 1000000
90 /* Weights of different errors we can introduce */
91 /* We should probably make these command-line arguments */
92 /* XXX - Should we add a bit-level error? */
93 #define ERR_WT_BIT 5 /* Flip a random bit */
94 #define ERR_WT_BYTE 5 /* Substitute a random byte */
95 #define ERR_WT_ALNUM 5 /* Substitute a random character in [A-Za-z0-9] */
96 #define ERR_WT_FMT 2 /* Substitute "%s" */
97 #define ERR_WT_AA 1 /* Fill the remainder of the buffer with 0xAA */
98 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
100 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
101 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
104 struct time_adjustment {
109 #define MAX_SELECTIONS 512
110 static struct select_item selectfrm[MAX_SELECTIONS];
111 static int max_selected = -1;
112 static int keep_em = 0;
113 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
114 static int out_frame_type = -2; /* Leave frame type alone */
115 static int verbose = 0; /* Not so verbose */
116 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
117 static double err_prob = 0.0;
118 static time_t starttime = 0;
119 static time_t stoptime = 0;
120 static gboolean check_startstop = FALSE;
121 static gboolean dup_detect = FALSE;
123 /* Add a selection item, a simple parser for now */
125 add_selection(char *sel)
130 if (++max_selected >= MAX_SELECTIONS) {
131 /* Let the user know we stopped selecting */
132 printf("Out of room for packet selections!\n");
136 printf("Add_Selected: %s\n", sel);
138 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
140 printf("Not inclusive ...");
142 selectfrm[max_selected].inclusive = 0;
143 selectfrm[max_selected].first = atoi(sel);
145 printf(" %i\n", selectfrm[max_selected].first);
150 printf("Inclusive ...");
153 selectfrm[max_selected].inclusive = 1;
154 selectfrm[max_selected].first = atoi(sel);
155 selectfrm[max_selected].second = atoi(next);
157 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
164 /* Was the packet selected? */
171 for (i = 0; i<= max_selected; i++) {
173 if (selectfrm[i].inclusive) {
174 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
178 if (recno == selectfrm[i].first)
187 /* is the packet in the selected timeframe */
188 static gboolean check_timestamp(wtap *wth) {
190 struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
191 if (!((i++)%250)) printf("== %d starttime=%lu stoptime=%lu ts=%lu",i,starttime,stoptime,pkthdr->ts.secs);
192 return ( (time_t) pkthdr->ts.secs >= starttime ) && ( (time_t) pkthdr->ts.secs <= stoptime );
196 set_time_adjustment(char *optarg)
205 /* skip leading whitespace */
206 while (*optarg == ' ' || *optarg == '\t') {
210 /* check for a negative adjustment */
211 if (*optarg == '-') {
212 time_adj.is_negative = 1;
216 /* collect whole number of seconds, if any */
217 if (*optarg == '.') { /* only fractional (i.e., .5 is ok) */
221 val = strtol(optarg, &frac, 10);
222 if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
223 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
227 if (val < 0) { /* implies '--' since we caught '-' above */
228 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
233 time_adj.tv.tv_sec = val;
235 /* now collect the partial seconds, if any */
236 if (*frac != '\0') { /* chars left, so get fractional part */
237 val = strtol(&(frac[1]), &end, 10);
238 if (*frac != '.' || end == NULL || end == frac
239 || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
240 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
246 return; /* no fractional digits */
249 /* adjust fractional portion from fractional to numerator
250 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
251 if (frac && end) { /* both are valid */
252 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
253 while(frac_digits < 6) { /* this is frac of 10^6 */
258 time_adj.tv.tv_usec = val;
262 is_duplicate(guint8* fd, guint32 len) {
267 if (cur_dup >= DUP_DEPTH)
270 /* Calculate our digest */
272 md5_append(&ms, fd, len);
273 md5_finish(&ms, fd_hash[cur_dup].digest);
275 fd_hash[cur_dup].len = len;
277 /* Look for duplicates */
278 for (i = 0; i < DUP_DEPTH; i++) {
282 if (fd_hash[i].len == fd_hash[cur_dup].len &&
283 memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
291 static void usage(void)
293 fprintf(stderr, "Editcap %s"
298 fprintf(stderr, "Edit and/or translate the format of capture files.\n");
299 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
300 fprintf(stderr, "\n");
301 fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
302 fprintf(stderr, "\n");
303 fprintf(stderr, "A single packet or a range of packets can be selected.\n");
304 fprintf(stderr, "\n");
305 fprintf(stderr, "Packets:\n");
306 fprintf(stderr, " -C <choplen> chop each packet at the end by <choplen> bytes\n");
307 fprintf(stderr, " -d remove duplicate packets\n");
308 fprintf(stderr, " -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
309 fprintf(stderr, " that a particular packet byte will be randomly changed\n");
310 fprintf(stderr, " -r keep the selected packets, default is to delete them\n");
311 fprintf(stderr, " -s <snaplen> truncate packets to max. <snaplen> bytes of data\n");
312 fprintf(stderr, " -t <time adjustment> adjust the timestamp of selected packets,\n");
313 fprintf(stderr, " <time adjustment> is in relative seconds (e.g. -0.5)\n");
314 fprintf(stderr, " -A <start time> don't output packets whose timestamp is before the\n");
315 fprintf(stderr, " given time (format as YYYY-MM-DD hh:mm:ss)\n");
316 fprintf(stderr, " -B <stop time> don't output packets whose timestamp is after the\n");
317 fprintf(stderr, " given time (format as YYYY-MM-DD hh:mm:ss)\n");
318 fprintf(stderr, "\n");
319 fprintf(stderr, "Output File(s):\n");
320 fprintf(stderr, " -c <packets per file> split the packet output to different files,\n");
321 fprintf(stderr, " with a maximum of <packets per file> each\n");
322 fprintf(stderr, " -F <capture type> set the output file type, default is libpcap\n");
323 fprintf(stderr, " an empty \"-F\" option will list the file types\n");
324 fprintf(stderr, " -T <encap type> set the output file encapsulation type,\n");
325 fprintf(stderr, " default is the same as the input file\n");
326 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types\n");
327 fprintf(stderr, "\n");
328 fprintf(stderr, "Miscellaneous:\n");
329 fprintf(stderr, " -h display this help and exit\n");
330 fprintf(stderr, " -v verbose output\n");
331 fprintf(stderr, "\n");
334 static void list_capture_types(void) {
337 fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
338 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
339 if (wtap_dump_can_open(i))
340 fprintf(stderr, " %s - %s\n",
341 wtap_file_type_short_string(i), wtap_file_type_string(i));
345 static void list_encap_types(void) {
349 fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
350 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
351 string = wtap_encap_short_string(i);
353 fprintf(stderr, " %s - %s\n",
354 string, wtap_encap_string(i));
359 failure_message(const char *msg_format, va_list ap)
361 fprintf(stderr, "editcap: ");
362 vfprintf(stderr, msg_format, ap);
363 fprintf(stderr, "\n");
366 int main(int argc, char *argv[])
376 unsigned int snaplen = 0; /* No limit */
377 unsigned int choplen = 0; /* No chop */
381 struct wtap_pkthdr snap_phdr;
382 const struct wtap_pkthdr *phdr;
385 int split_packet_count = 0;
386 int written_count = 0;
389 char* init_progfile_dir_error;
392 /* Register wiretap plugins */
393 if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
394 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
395 g_free(init_progfile_dir_error);
397 init_report_err(failure_message,NULL,NULL);
402 /* Process the options */
403 while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
408 err_prob = strtod(optarg, &p);
409 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
410 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
414 srand( (unsigned int) (time(NULL) + getpid()) );
418 out_file_type = wtap_short_string_to_file_type(optarg);
419 if (out_file_type < 0) {
420 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
422 list_capture_types();
428 split_packet_count = strtol(optarg, &p, 10);
429 if (p == optarg || *p != '\0') {
430 fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
434 if (split_packet_count <= 0) {
435 fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
442 choplen = strtol(optarg, &p, 10);
443 if (p == optarg || *p != '\0') {
444 fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
452 for (i = 0; i < DUP_DEPTH; i++) {
453 memset(&fd_hash[i].digest, 0, 16);
458 case '?': /* Bad options if GNU getopt */
461 list_capture_types();
478 keep_em = !keep_em; /* Just invert */
482 snaplen = strtol(optarg, &p, 10);
483 if (p == optarg || *p != '\0') {
484 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
491 set_time_adjustment(optarg);
495 out_frame_type = wtap_short_string_to_encap(optarg);
496 if (out_frame_type < 0) {
497 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
505 verbose = !verbose; /* Just invert */
512 memset(&starttm,0,sizeof(struct tm));
514 if(!strptime(optarg,"%F %T",&starttm)) {
515 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
520 check_startstop = TRUE;
521 starttm.tm_isdst = -1;
523 starttime = mktime(&starttm);
524 printf("=START=> given='%s' stoptime=%lu\n",optarg,starttime);
531 memset(&stoptm,0,sizeof(struct tm));
533 if(!strptime(optarg,"%F %T",&stoptm)) {
534 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
538 check_startstop = TRUE;
539 stoptm.tm_isdst = -1;
540 stoptime = mktime(&stoptm);
541 printf("=STOP=> given='%s' stoptime=%lu\n",optarg,stoptime);
549 printf("Optind = %i, argc = %i\n", optind, argc);
552 if ((argc - optind) < 1) {
559 if (check_startstop && !stoptime) {
561 /* XXX: will work until 2035 */
562 memset(&stoptm,0,sizeof(struct tm));
563 stoptm.tm_year = 135;
567 stoptime = mktime(&stoptm);
568 printf("=STOP=NEVER=> stoptime=%lu\n",stoptime);
571 if (starttime > stoptime) {
572 fprintf(stderr, "editcap: start time is after the stop time\n");
575 printf("==> stoptime=%lu stoptime=%lu\n",starttime,stoptime);
577 wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
580 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
584 case WTAP_ERR_UNSUPPORTED:
585 case WTAP_ERR_UNSUPPORTED_ENCAP:
586 case WTAP_ERR_BAD_RECORD:
587 fprintf(stderr, "(%s)\n", err_info);
597 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
598 wtap_file_type_string(wtap_file_type(wth)));
603 * Now, process the rest, if any ... we only write if there is an extra
607 if ((argc - optind) >= 2) {
609 if (out_frame_type == -2)
610 out_frame_type = wtap_file_encap(wth);
612 if (split_packet_count > 0) {
613 filename = (char *) malloc(strlen(argv[optind+1]) + 20);
617 sprintf(filename, "%s-%05d", argv[optind+1], 0);
619 filename = argv[optind+1];
622 pdh = wtap_dump_open(filename, out_file_type,
623 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
626 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
632 for (i = optind + 2; i < argc; i++)
633 if (add_selection(argv[i]) == FALSE)
636 while (wtap_read(wth, &err, &err_info, &data_offset)) {
638 if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
639 if (!wtap_dump_close(pdh, &err)) {
641 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
646 sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
649 fprintf(stderr, "Continuing writing in file %s\n", filename);
652 pdh = wtap_dump_open(filename, out_file_type,
653 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
656 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
663 check_ts = check_timestamp(wth);
665 if ( ((check_startstop && check_ts) || (!check_startstop && !check_ts)) && ((!selected(count) && !keep_em) ||
666 (selected(count) && keep_em)) ) {
669 printf("Packet: %u\n", count);
671 /* We simply write it, perhaps after truncating it; we could do other
672 things, like modify it. */
674 phdr = wtap_phdr(wth);
676 if (choplen != 0 && phdr->caplen > choplen) {
678 snap_phdr.caplen -= choplen;
682 if (snaplen != 0 && phdr->caplen > snaplen) {
684 snap_phdr.caplen = snaplen;
688 /* assume that if the frame's tv_sec is 0, then
689 * the timestamp isn't supported */
690 if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
692 if (time_adj.is_negative)
693 snap_phdr.ts.secs -= time_adj.tv.tv_sec;
695 snap_phdr.ts.secs += time_adj.tv.tv_sec;
699 /* assume that if the frame's tv_sec is 0, then
700 * the timestamp isn't supported */
701 if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
703 if (time_adj.is_negative) { /* subtract */
704 if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
706 snap_phdr.ts.nsecs += ONE_MILLION * 1000;
708 snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
710 if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
713 snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
715 snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
722 buf = wtap_buf_ptr(wth);
723 if (is_duplicate(buf, phdr->caplen)) {
725 printf("Skipping duplicate: %u\n", count);
731 if (err_prob > 0.0) {
732 buf = wtap_buf_ptr(wth);
733 for (i = 0; i < (int) phdr->caplen; i++) {
734 if (rand() <= err_prob * RAND_MAX) {
735 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
737 if (err_type < ERR_WT_BIT) {
738 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
739 err_type = ERR_WT_TOTAL;
741 err_type -= ERR_WT_BYTE;
744 if (err_type < ERR_WT_BYTE) {
745 buf[i] = rand() / (RAND_MAX / 255 + 1);
746 err_type = ERR_WT_TOTAL;
748 err_type -= ERR_WT_BYTE;
751 if (err_type < ERR_WT_ALNUM) {
752 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
753 err_type = ERR_WT_TOTAL;
755 err_type -= ERR_WT_ALNUM;
758 if (err_type < ERR_WT_FMT) {
759 if ((unsigned int)i < phdr->caplen - 2)
760 strcpy((char*) &buf[i], "%s");
761 err_type = ERR_WT_TOTAL;
763 err_type -= ERR_WT_FMT;
766 if (err_type < ERR_WT_AA) {
767 for (j = i; j < (int) phdr->caplen; j++) {
776 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
779 fprintf(stderr, "editcap: Error writing to %s: %s\n",
780 filename, wtap_strerror(err));
794 /* Print a message noting that the read failed somewhere along the line. */
796 "editcap: An error occurred while reading \"%s\": %s.\n",
797 argv[optind], wtap_strerror(err));
800 case WTAP_ERR_UNSUPPORTED:
801 case WTAP_ERR_UNSUPPORTED_ENCAP:
802 case WTAP_ERR_BAD_RECORD:
803 fprintf(stderr, "(%s)\n", err_info);
808 if (!wtap_dump_close(pdh, &err)) {
810 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,