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.
23 #ifdef HAVE_SYS_TIME_H
35 * Some globals so we can pass things to various routines
45 #define ONE_MILLION 1000000
47 struct time_adjustment {
52 static struct select_item selectfrm[100];
53 static int max_selected = -1;
55 static int keep_em = 0;
56 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
57 static int out_frame_type = -2; /* Leave frame type alone */
58 static int verbose = 0; /* Not so verbose */
59 static unsigned int snaplen = 0; /* No limit */
60 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
62 /* Add a selection item, a simple parser for now */
64 static void add_selection(char *sel)
69 if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
72 printf("Add_Selected: %s\n", sel);
74 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
76 printf("Not inclusive ...");
79 selectfrm[max_selected].inclusive = 0;
80 selectfrm[max_selected].first = atoi(sel);
82 printf(" %i\n", selectfrm[max_selected].first);
87 printf("Inclusive ...");
91 selectfrm[max_selected].inclusive = 1;
92 selectfrm[max_selected].first = atoi(sel);
93 selectfrm[max_selected].second = atoi(next);
95 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
102 /* Was the record selected? */
104 static int selected(int recno)
108 for (i = 0; i<= max_selected; i++) {
110 if (selectfrm[i].inclusive) {
111 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
115 if (recno == selectfrm[i].first)
124 /* An argument to the callback routine */
132 *The callback routine that is called for each frame in the input file
136 edit_callback(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
137 union wtap_pseudo_header *pseudo_header, const guchar *buf)
139 callback_arg *argp = (callback_arg *)user;
141 struct wtap_pkthdr snap_phdr;
143 if ((!selected(count) && !keep_em) ||
144 (selected(count) && keep_em)) {
147 printf("Record: %u\n", count);
149 /* We simply write it, perhaps after truncating it; we could do other
150 things, like modify it. */
152 if (snaplen != 0 && phdr->caplen > snaplen) {
154 snap_phdr.caplen = snaplen;
158 /* assume that if the frame's tv_sec is 0, then
159 * the timestamp isn't supported */
160 if (phdr->ts.tv_sec > 0 && time_adj.tv.tv_sec != 0) {
162 if (time_adj.is_negative)
163 snap_phdr.ts.tv_sec -= time_adj.tv.tv_sec;
165 snap_phdr.ts.tv_sec += time_adj.tv.tv_sec;
169 /* assume that if the frame's tv_sec is 0, then
170 * the timestamp isn't supported */
171 if (phdr->ts.tv_sec > 0 && time_adj.tv.tv_usec != 0) {
173 if (time_adj.is_negative) { /* subtract */
174 if (snap_phdr.ts.tv_usec < time_adj.tv.tv_usec) { /* borrow */
175 snap_phdr.ts.tv_sec--;
176 snap_phdr.ts.tv_usec += ONE_MILLION;
178 snap_phdr.ts.tv_usec -= time_adj.tv.tv_usec;
180 if (snap_phdr.ts.tv_usec + time_adj.tv.tv_usec > ONE_MILLION) {
182 snap_phdr.ts.tv_sec++;
183 snap_phdr.ts.tv_usec += time_adj.tv.tv_usec - ONE_MILLION;
185 snap_phdr.ts.tv_usec += time_adj.tv.tv_usec;
191 if (!wtap_dump(argp->pdh, phdr, pseudo_header, buf, &err)) {
193 fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
206 set_time_adjustment(char *optarg)
215 /* skip leading whitespace */
216 while (*optarg == ' ' || *optarg == '\t') {
220 /* check for a negative adjustment */
221 if (*optarg == '-') {
222 time_adj.is_negative = 1;
226 /* collect whole number of seconds, if any */
227 if (*optarg == '.') { /* only fractional (i.e., .5 is ok) */
231 val = strtol(optarg, &frac, 10);
232 if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
233 fprintf(stderr, "editcap: \"%s\" is not a valid ime adjustment\n",
237 if (val < 0) { /* implies '--' since we caught '-' above */
238 fprintf(stderr, "editcap: \"%s\" is not a valid time adjustment\n",
243 time_adj.tv.tv_sec = val;
245 /* now collect the partial seconds, if any */
246 if (*frac != '\0') { /* chars left, so get fractional part */
247 val = strtol(&(frac[1]), &end, 10);
248 if (*frac != '.' || end == NULL || end == frac
249 || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
250 fprintf(stderr, "editcap: \"%s\" is not a valid time adjustment\n",
256 return; /* no fractional digits */
259 /* adjust fractional portion from fractional to numerator
260 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
261 if (frac && end) { /* both are valid */
262 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
263 while(frac_digits < 6) { /* this is frac of 10^6 */
268 time_adj.tv.tv_usec = val;
271 static void usage(void)
276 fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>]\n");
277 fprintf(stderr, " [-s <snaplen>] [-t <time adjustment>]\n");
278 fprintf(stderr, " <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
279 fprintf(stderr, " where\t-r specifies that the records specified should be kept, not deleted, \n");
280 fprintf(stderr, " default is to delete\n");
281 fprintf(stderr, " \t-v specifies verbose operation, default is silent\n");
282 fprintf(stderr, " \t-h produces this help listing.\n");
283 fprintf(stderr, " \t-T <encap type> specifies the encapsulation type to use:\n");
284 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
285 string = wtap_encap_short_string(i);
287 fprintf(stderr, " \t %s - %s\n",
288 string, wtap_encap_string(i));
290 fprintf(stderr, " \t default is the same as the input file\n");
291 fprintf(stderr, " \t-F <capture type> specifies the capture file type to write:\n");
292 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
293 if (wtap_dump_can_open(i))
294 fprintf(stderr, " \t %s - %s\n",
295 wtap_file_type_short_string(i), wtap_file_type_string(i));
297 fprintf(stderr, " \t default is libpcap\n");
298 fprintf(stderr, " \t-s <snaplen> specifies that packets should be truncated to\n");
299 fprintf(stderr, " \t <snaplen> bytes of data\n");
300 fprintf(stderr, " \t-t <time adjustment> specifies the time adjustment\n");
301 fprintf(stderr, " \t to be applied to selected packets\n");
302 fprintf(stderr, "\n \t A range of records can be specified as well\n");
305 int main(int argc, char *argv[])
318 /* Process the options first */
320 while ((opt = getopt(argc, argv, "T:F:rvs:t:h")) !=-1) {
325 out_frame_type = wtap_short_string_to_encap(optarg);
326 if (out_frame_type < 0) {
327 fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
334 out_file_type = wtap_short_string_to_file_type(optarg);
335 if (out_file_type < 0) {
336 fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
343 verbose = !verbose; /* Just invert */
347 keep_em = !keep_em; /* Just invert */
351 snaplen = strtol(optarg, &p, 10);
352 if (p == optarg || *p != '\0') {
353 fprintf(stderr, "editcap: \"%s\" is not a valid snapshot length\n",
360 set_time_adjustment(optarg);
368 case '?': /* Bad options if GNU getopt */
378 printf("Optind = %i, argc = %i\n", optind, argc);
381 if ((argc - optind) < 1) {
388 wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
391 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
395 case WTAP_ERR_UNSUPPORTED:
396 case WTAP_ERR_UNSUPPORTED_ENCAP:
397 case WTAP_ERR_BAD_RECORD:
398 fprintf(stderr, "(%s)\n", err_info);
408 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
409 wtap_file_type_string(wtap_file_type(wth)));
414 * Now, process the rest, if any ... we only write if there is an extra
418 if ((argc - optind) >= 2) {
420 args.filename = argv[optind + 1];
421 if (out_frame_type == -2)
422 out_frame_type = wtap_file_encap(wth);
424 snapshot_length = wtap_snapshot_length(wth);
425 if (snapshot_length == 0) {
426 /* Snapshot length of input file not known. */
427 snapshot_length = WTAP_MAX_PACKET_SIZE;
429 args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
430 out_frame_type, wtap_snapshot_length(wth), &err);
431 if (args.pdh == NULL) {
433 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
439 for (i = optind + 2; i < argc; i++)
440 add_selection(argv[i]);
442 if (!wtap_loop(wth, 0, edit_callback, (char *)&args, &err, &err_info)) {
443 /* Print a message noting that the read failed somewhere along the line. */
445 "editcap: An error occurred while reading \"%s\": %s.\n",
446 argv[optind], wtap_strerror(err));
449 case WTAP_ERR_UNSUPPORTED:
450 case WTAP_ERR_UNSUPPORTED_ENCAP:
451 case WTAP_ERR_BAD_RECORD:
452 fprintf(stderr, "(%s)\n", err_info);
457 if (!wtap_dump_close(args.pdh, &err)) {
459 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],