1 /* Edit capture files. We can delete records, 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.
24 #ifdef HAVE_SYS_TIME_H
36 #include <process.h> /* getpid */
40 * Some globals so we can pass things to various routines
50 #define ONE_MILLION 1000000
52 /* Weights of different errors we can introduce */
53 /* We should probably make these command-line arguments */
54 /* XXX - Should we add a bit-level error? */
55 #define ERR_WT_BIT 5 /* Flip a random bit */
56 #define ERR_WT_BYTE 5 /* Substitute a random byte */
57 #define ERR_WT_ALNUM 5 /* Substitute a random character in [A-Za-z0-9] */
58 #define ERR_WT_FMT 2 /* Substitute "%s" */
59 #define ERR_WT_AA 1 /* Fill the remainder of the buffer with 0xAA */
60 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
62 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
63 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
66 struct time_adjustment {
71 static struct select_item selectfrm[100];
72 static int max_selected = -1;
73 static int keep_em = 0;
74 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
75 static int out_frame_type = -2; /* Leave frame type alone */
76 static int verbose = 0; /* Not so verbose */
77 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
78 static double err_prob = 0.0;
80 /* Add a selection item, a simple parser for now */
82 static void add_selection(char *sel)
87 if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
90 printf("Add_Selected: %s\n", sel);
92 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
94 printf("Not inclusive ...");
97 selectfrm[max_selected].inclusive = 0;
98 selectfrm[max_selected].first = atoi(sel);
100 printf(" %i\n", selectfrm[max_selected].first);
105 printf("Inclusive ...");
109 selectfrm[max_selected].inclusive = 1;
110 selectfrm[max_selected].first = atoi(sel);
111 selectfrm[max_selected].second = atoi(next);
113 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
120 /* Was the record selected? */
122 static int selected(int recno)
126 for (i = 0; i<= max_selected; i++) {
128 if (selectfrm[i].inclusive) {
129 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
133 if (recno == selectfrm[i].first)
143 set_time_adjustment(char *optarg)
152 /* skip leading whitespace */
153 while (*optarg == ' ' || *optarg == '\t') {
157 /* check for a negative adjustment */
158 if (*optarg == '-') {
159 time_adj.is_negative = 1;
163 /* collect whole number of seconds, if any */
164 if (*optarg == '.') { /* only fractional (i.e., .5 is ok) */
168 val = strtol(optarg, &frac, 10);
169 if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
170 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
174 if (val < 0) { /* implies '--' since we caught '-' above */
175 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
180 time_adj.tv.tv_sec = val;
182 /* now collect the partial seconds, if any */
183 if (*frac != '\0') { /* chars left, so get fractional part */
184 val = strtol(&(frac[1]), &end, 10);
185 if (*frac != '.' || end == NULL || end == frac
186 || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
187 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
193 return; /* no fractional digits */
196 /* adjust fractional portion from fractional to numerator
197 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
198 if (frac && end) { /* both are valid */
199 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
200 while(frac_digits < 6) { /* this is frac of 10^6 */
205 time_adj.tv.tv_usec = val;
208 static void usage(void)
213 fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-E <probability>]\n");
214 fprintf(stderr, " [-F <capture type>] [-s <snaplen>] [-t <time adjustment>]\n");
215 fprintf(stderr, " <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
216 fprintf(stderr, " where\n");
217 fprintf(stderr, " \t-E <probability> specifies the probability (between 0 and 1)\n");
218 fprintf(stderr, " \t that a particular byte will will have an error.\n");
219 fprintf(stderr, " \t-F <capture type> specifies the capture file type to write:\n");
220 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
221 if (wtap_dump_can_open(i))
222 fprintf(stderr, " \t %s - %s\n",
223 wtap_file_type_short_string(i), wtap_file_type_string(i));
225 fprintf(stderr, " \t default is libpcap\n");
226 fprintf(stderr, " \t-h produces this help listing.\n");
227 fprintf(stderr, " \t-r specifies that the records specified should be kept, not deleted, \n");
228 fprintf(stderr, " default is to delete\n");
229 fprintf(stderr, " \t-s <snaplen> specifies that packets should be truncated to\n");
230 fprintf(stderr, " \t <snaplen> bytes of data\n");
231 fprintf(stderr, " \t-t <time adjustment> specifies the time adjustment\n");
232 fprintf(stderr, " \t to be applied to selected packets\n");
233 fprintf(stderr, " \t-T <encap type> specifies the encapsulation type to use:\n");
234 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
235 string = wtap_encap_short_string(i);
237 fprintf(stderr, " \t %s - %s\n",
238 string, wtap_encap_string(i));
240 fprintf(stderr, " \t default is the same as the input file\n");
241 fprintf(stderr, " \t-v specifies verbose operation, default is silent\n");
242 fprintf(stderr, "\n \t A range of records can be specified as well\n");
245 int main(int argc, char *argv[])
255 unsigned int snaplen = 0; /* No limit */
259 struct wtap_pkthdr snap_phdr;
260 const struct wtap_pkthdr *phdr;
264 /* Process the options first */
266 while ((opt = getopt(argc, argv, "E:F:hrs:t:T:v")) !=-1) {
271 err_prob = strtod(optarg, &p);
272 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
273 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
277 srand(time(NULL) + getpid());
281 out_file_type = wtap_short_string_to_file_type(optarg);
282 if (out_file_type < 0) {
283 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n",
290 case '?': /* Bad options if GNU getopt */
296 keep_em = !keep_em; /* Just invert */
300 snaplen = strtol(optarg, &p, 10);
301 if (p == optarg || *p != '\0') {
302 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
309 set_time_adjustment(optarg);
313 out_frame_type = wtap_short_string_to_encap(optarg);
314 if (out_frame_type < 0) {
315 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n",
322 verbose = !verbose; /* Just invert */
330 printf("Optind = %i, argc = %i\n", optind, argc);
333 if ((argc - optind) < 1) {
340 wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
343 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
347 case WTAP_ERR_UNSUPPORTED:
348 case WTAP_ERR_UNSUPPORTED_ENCAP:
349 case WTAP_ERR_BAD_RECORD:
350 fprintf(stderr, "(%s)\n", err_info);
360 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
361 wtap_file_type_string(wtap_file_type(wth)));
366 * Now, process the rest, if any ... we only write if there is an extra
370 if ((argc - optind) >= 2) {
372 if (out_frame_type == -2)
373 out_frame_type = wtap_file_encap(wth);
375 pdh = wtap_dump_open(argv[optind + 1], out_file_type,
376 out_frame_type, wtap_snapshot_length(wth), &err);
379 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
385 for (i = optind + 2; i < argc; i++)
386 add_selection(argv[i]);
388 while (wtap_read(wth, &err, &err_info, &data_offset)) {
390 if ((!selected(count) && !keep_em) ||
391 (selected(count) && keep_em)) {
394 printf("Record: %u\n", count);
396 /* We simply write it, perhaps after truncating it; we could do other
397 things, like modify it. */
399 phdr = wtap_phdr(wth);
401 if (snaplen != 0 && phdr->caplen > snaplen) {
403 snap_phdr.caplen = snaplen;
407 /* assume that if the frame's tv_sec is 0, then
408 * the timestamp isn't supported */
409 if (phdr->ts.tv_sec > 0 && time_adj.tv.tv_sec != 0) {
411 if (time_adj.is_negative)
412 snap_phdr.ts.tv_sec -= time_adj.tv.tv_sec;
414 snap_phdr.ts.tv_sec += time_adj.tv.tv_sec;
418 /* assume that if the frame's tv_sec is 0, then
419 * the timestamp isn't supported */
420 if (phdr->ts.tv_sec > 0 && time_adj.tv.tv_usec != 0) {
422 if (time_adj.is_negative) { /* subtract */
423 if (snap_phdr.ts.tv_usec < time_adj.tv.tv_usec) { /* borrow */
424 snap_phdr.ts.tv_sec--;
425 snap_phdr.ts.tv_usec += ONE_MILLION;
427 snap_phdr.ts.tv_usec -= time_adj.tv.tv_usec;
429 if (snap_phdr.ts.tv_usec + time_adj.tv.tv_usec > ONE_MILLION) {
431 snap_phdr.ts.tv_sec++;
432 snap_phdr.ts.tv_usec += time_adj.tv.tv_usec - ONE_MILLION;
434 snap_phdr.ts.tv_usec += time_adj.tv.tv_usec;
440 if (err_prob > 0.0) {
441 buf = wtap_buf_ptr(wth);
442 for (i = 0; i < (int) phdr->caplen; i++) {
443 if (rand() <= err_prob * RAND_MAX) {
444 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
446 if (err_type < ERR_WT_BIT) {
447 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
448 err_type = ERR_WT_TOTAL;
450 err_type -= ERR_WT_BYTE;
453 if (err_type < ERR_WT_BYTE) {
454 buf[i] = rand() / (RAND_MAX / 255 + 1);
455 err_type = ERR_WT_TOTAL;
457 err_type -= ERR_WT_BYTE;
460 if (err_type < ERR_WT_ALNUM) {
461 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
462 err_type = ERR_WT_TOTAL;
464 err_type -= ERR_WT_ALNUM;
467 if (err_type < ERR_WT_FMT) {
468 if ((unsigned int)i < phdr->caplen - 2)
469 strcpy(&buf[i], "%s");
470 err_type = ERR_WT_TOTAL;
472 err_type -= ERR_WT_FMT;
475 if (err_type < ERR_WT_AA) {
476 for (j = i; j < (int) phdr->caplen; j++) {
485 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
488 fprintf(stderr, "editcap: Error writing to %s: %s\n",
489 argv[optind + 1], wtap_strerror(err));
501 /* Print a message noting that the read failed somewhere along the line. */
503 "editcap: An error occurred while reading \"%s\": %s.\n",
504 argv[optind], wtap_strerror(err));
507 case WTAP_ERR_UNSUPPORTED:
508 case WTAP_ERR_UNSUPPORTED_ENCAP:
509 case WTAP_ERR_BAD_RECORD:
510 fprintf(stderr, "(%s)\n", err_info);
515 if (!wtap_dump_close(pdh, &err)) {
517 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[optind + 1],