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 */
189 check_timestamp(wtap *wth)
192 struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
195 printf("== %d starttime=%lu stoptime=%lu ts=%lu\n",i,
196 (unsigned long)starttime,
197 (unsigned long)stoptime,
198 (unsigned long)pkthdr->ts.secs);
199 return ( pkthdr->ts.secs >= starttime ) && ( pkthdr->ts.secs <= stoptime );
203 set_time_adjustment(char *optarg)
212 /* skip leading whitespace */
213 while (*optarg == ' ' || *optarg == '\t') {
217 /* check for a negative adjustment */
218 if (*optarg == '-') {
219 time_adj.is_negative = 1;
223 /* collect whole number of seconds, if any */
224 if (*optarg == '.') { /* only fractional (i.e., .5 is ok) */
228 val = strtol(optarg, &frac, 10);
229 if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
230 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
234 if (val < 0) { /* implies '--' since we caught '-' above */
235 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
240 time_adj.tv.tv_sec = val;
242 /* now collect the partial seconds, if any */
243 if (*frac != '\0') { /* chars left, so get fractional part */
244 val = strtol(&(frac[1]), &end, 10);
245 if (*frac != '.' || end == NULL || end == frac
246 || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
247 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
253 return; /* no fractional digits */
256 /* adjust fractional portion from fractional to numerator
257 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
258 if (frac && end) { /* both are valid */
259 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
260 while(frac_digits < 6) { /* this is frac of 10^6 */
265 time_adj.tv.tv_usec = val;
269 is_duplicate(guint8* fd, guint32 len) {
274 if (cur_dup >= DUP_DEPTH)
277 /* Calculate our digest */
279 md5_append(&ms, fd, len);
280 md5_finish(&ms, fd_hash[cur_dup].digest);
282 fd_hash[cur_dup].len = len;
284 /* Look for duplicates */
285 for (i = 0; i < DUP_DEPTH; i++) {
289 if (fd_hash[i].len == fd_hash[cur_dup].len &&
290 memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
298 static void usage(void)
300 fprintf(stderr, "Editcap %s"
305 fprintf(stderr, "Edit and/or translate the format of capture files.\n");
306 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
307 fprintf(stderr, "\n");
308 fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
309 fprintf(stderr, "\n");
310 fprintf(stderr, "A single packet or a range of packets can be selected.\n");
311 fprintf(stderr, "\n");
312 fprintf(stderr, "Packet selection:\n");
313 fprintf(stderr, " -r keep the selected packets, default is to delete them\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, " -d remove duplicate packets\n");
319 fprintf(stderr, "\n");
320 fprintf(stderr, "Packet manipulation:\n");
321 fprintf(stderr, " -s <snaplen> truncate each packet to max. <snaplen> bytes of data\n");
322 fprintf(stderr, " -C <choplen> chop each packet at the end by <choplen> bytes\n");
323 fprintf(stderr, " -t <time adjustment> adjust the timestamp of each packet,\n");
324 fprintf(stderr, " <time adjustment> is in relative seconds (e.g. -0.5)\n");
325 fprintf(stderr, " -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
326 fprintf(stderr, " that a particular packet byte will be randomly changed\n");
327 fprintf(stderr, "\n");
328 fprintf(stderr, "Output File(s):\n");
329 fprintf(stderr, " -c <packets per file> split the packet output to different files,\n");
330 fprintf(stderr, " with a maximum of <packets per file> each\n");
331 fprintf(stderr, " -F <capture type> set the output file type, default is libpcap\n");
332 fprintf(stderr, " an empty \"-F\" option will list the file types\n");
333 fprintf(stderr, " -T <encap type> set the output file encapsulation type,\n");
334 fprintf(stderr, " default is the same as the input file\n");
335 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types\n");
336 fprintf(stderr, "\n");
337 fprintf(stderr, "Miscellaneous:\n");
338 fprintf(stderr, " -h display this help and exit\n");
339 fprintf(stderr, " -v verbose output\n");
340 fprintf(stderr, "\n");
343 static void list_capture_types(void) {
346 fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
347 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
348 if (wtap_dump_can_open(i))
349 fprintf(stderr, " %s - %s\n",
350 wtap_file_type_short_string(i), wtap_file_type_string(i));
354 static void list_encap_types(void) {
358 fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
359 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
360 string = wtap_encap_short_string(i);
362 fprintf(stderr, " %s - %s\n",
363 string, wtap_encap_string(i));
368 failure_message(const char *msg_format, va_list ap)
370 fprintf(stderr, "editcap: ");
371 vfprintf(stderr, msg_format, ap);
372 fprintf(stderr, "\n");
375 int main(int argc, char *argv[])
385 unsigned int snaplen = 0; /* No limit */
386 unsigned int choplen = 0; /* No chop */
390 struct wtap_pkthdr snap_phdr;
391 const struct wtap_pkthdr *phdr;
394 int split_packet_count = 0;
395 int written_count = 0;
399 char* init_progfile_dir_error;
401 /* Register wiretap plugins */
402 if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
403 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
404 g_free(init_progfile_dir_error);
406 init_report_err(failure_message,NULL,NULL);
411 /* Process the options */
412 while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
417 err_prob = strtod(optarg, &p);
418 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
419 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
423 srand( (unsigned int) (time(NULL) + getpid()) );
427 out_file_type = wtap_short_string_to_file_type(optarg);
428 if (out_file_type < 0) {
429 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
431 list_capture_types();
437 split_packet_count = strtol(optarg, &p, 10);
438 if (p == optarg || *p != '\0') {
439 fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
443 if (split_packet_count <= 0) {
444 fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
451 choplen = strtol(optarg, &p, 10);
452 if (p == optarg || *p != '\0') {
453 fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
461 for (i = 0; i < DUP_DEPTH; i++) {
462 memset(&fd_hash[i].digest, 0, 16);
467 case '?': /* Bad options if GNU getopt */
470 list_capture_types();
487 keep_em = !keep_em; /* Just invert */
491 snaplen = strtol(optarg, &p, 10);
492 if (p == optarg || *p != '\0') {
493 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
500 set_time_adjustment(optarg);
504 out_frame_type = wtap_short_string_to_encap(optarg);
505 if (out_frame_type < 0) {
506 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
514 verbose = !verbose; /* Just invert */
521 memset(&starttm,0,sizeof(struct tm));
523 if(!strptime(optarg,"%Y-%m-%d %T",&starttm)) {
524 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
528 check_startstop = TRUE;
529 starttm.tm_isdst = -1;
531 starttime = mktime(&starttm);
532 printf("=START=> given='%s' stoptime=%lu\n",optarg,(unsigned long)starttime);
540 memset(&stoptm,0,sizeof(struct tm));
542 if(!strptime(optarg,"%Y-%m-%d %T",&stoptm)) {
543 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
546 check_startstop = TRUE;
547 stoptm.tm_isdst = -1;
548 stoptime = mktime(&stoptm);
549 printf("=STOP=> given='%s' stoptime=%lu\n",optarg,(unsigned long)stoptime);
557 printf("Optind = %i, argc = %i\n", optind, argc);
560 if ((argc - optind) < 1) {
567 if (check_startstop && !stoptime) {
569 /* XXX: will work until 2035 */
570 memset(&stoptm,0,sizeof(struct tm));
571 stoptm.tm_year = 135;
575 stoptime = mktime(&stoptm);
576 printf("=STOP=NEVER=> stoptime=%lu\n",(unsigned long)stoptime);
579 if (starttime > stoptime) {
580 fprintf(stderr, "editcap: start time is after the stop time\n");
583 printf("==> stoptime=%lu stoptime=%lu\n",(unsigned long)starttime,(unsigned long)stoptime);
585 wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
588 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
592 case WTAP_ERR_UNSUPPORTED:
593 case WTAP_ERR_UNSUPPORTED_ENCAP:
594 case WTAP_ERR_BAD_RECORD:
595 fprintf(stderr, "(%s)\n", err_info);
605 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
606 wtap_file_type_string(wtap_file_type(wth)));
611 * Now, process the rest, if any ... we only write if there is an extra
615 if ((argc - optind) >= 2) {
617 if (out_frame_type == -2)
618 out_frame_type = wtap_file_encap(wth);
620 if (split_packet_count > 0) {
621 filename = (char *) malloc(strlen(argv[optind+1]) + 20);
625 sprintf(filename, "%s-%05d", argv[optind+1], 0);
627 filename = argv[optind+1];
630 pdh = wtap_dump_open(filename, out_file_type,
631 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
634 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
640 for (i = optind + 2; i < argc; i++)
641 if (add_selection(argv[i]) == FALSE)
644 while (wtap_read(wth, &err, &err_info, &data_offset)) {
646 if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
647 if (!wtap_dump_close(pdh, &err)) {
649 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
654 sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
657 fprintf(stderr, "Continuing writing in file %s\n", filename);
660 pdh = wtap_dump_open(filename, out_file_type,
661 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
664 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
671 check_ts = check_timestamp(wth);
673 if ( ((check_startstop && check_ts) || (!check_startstop && !check_ts)) && ((!selected(count) && !keep_em) ||
674 (selected(count) && keep_em)) ) {
677 printf("Packet: %u\n", count);
679 /* We simply write it, perhaps after truncating it; we could do other
680 things, like modify it. */
682 phdr = wtap_phdr(wth);
684 if (choplen != 0 && phdr->caplen > choplen) {
686 snap_phdr.caplen -= choplen;
690 if (snaplen != 0 && phdr->caplen > snaplen) {
692 snap_phdr.caplen = snaplen;
696 /* assume that if the frame's tv_sec is 0, then
697 * the timestamp isn't supported */
698 if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
700 if (time_adj.is_negative)
701 snap_phdr.ts.secs -= time_adj.tv.tv_sec;
703 snap_phdr.ts.secs += time_adj.tv.tv_sec;
707 /* assume that if the frame's tv_sec is 0, then
708 * the timestamp isn't supported */
709 if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
711 if (time_adj.is_negative) { /* subtract */
712 if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
714 snap_phdr.ts.nsecs += ONE_MILLION * 1000;
716 snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
718 if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
721 snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
723 snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
730 buf = wtap_buf_ptr(wth);
731 if (is_duplicate(buf, phdr->caplen)) {
733 printf("Skipping duplicate: %u\n", count);
739 if (err_prob > 0.0) {
740 buf = wtap_buf_ptr(wth);
741 for (i = 0; i < (int) phdr->caplen; i++) {
742 if (rand() <= err_prob * RAND_MAX) {
743 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
745 if (err_type < ERR_WT_BIT) {
746 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
747 err_type = ERR_WT_TOTAL;
749 err_type -= ERR_WT_BYTE;
752 if (err_type < ERR_WT_BYTE) {
753 buf[i] = rand() / (RAND_MAX / 255 + 1);
754 err_type = ERR_WT_TOTAL;
756 err_type -= ERR_WT_BYTE;
759 if (err_type < ERR_WT_ALNUM) {
760 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
761 err_type = ERR_WT_TOTAL;
763 err_type -= ERR_WT_ALNUM;
766 if (err_type < ERR_WT_FMT) {
767 if ((unsigned int)i < phdr->caplen - 2)
768 strcpy((char*) &buf[i], "%s");
769 err_type = ERR_WT_TOTAL;
771 err_type -= ERR_WT_FMT;
774 if (err_type < ERR_WT_AA) {
775 for (j = i; j < (int) phdr->caplen; j++) {
784 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
787 fprintf(stderr, "editcap: Error writing to %s: %s\n",
788 filename, wtap_strerror(err));
802 /* Print a message noting that the read failed somewhere along the line. */
804 "editcap: An error occurred while reading \"%s\": %s.\n",
805 argv[optind], wtap_strerror(err));
808 case WTAP_ERR_UNSUPPORTED:
809 case WTAP_ERR_UNSUPPORTED_ENCAP:
810 case WTAP_ERR_BAD_RECORD:
811 fprintf(stderr, "(%s)\n", err_info);
816 if (!wtap_dump_close(pdh, &err)) {
818 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,