From Jesper Peterson:
[obnox/wireshark/wip.git] / editcap.c
1 /* Edit capture files.  We can delete records, adjust timestamps, or
2  * simply convert from one format to another format.
3  *
4  * $Id: editcap.c,v 1.26 2002/08/28 21:00:06 jmayer Exp $
5  *
6  * Originally written by Richard Sharpe.
7  * Improved by Guy Harris.
8  * Further improved by Richard Sharpe.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <glib.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <string.h>
28 #include "wtap.h"
29
30 #ifdef NEED_GETOPT_H
31 #include "getopt.h"
32 #endif
33
34 /*
35  * Some globals so we can pass things to various routines
36  */
37
38 struct select_item {
39
40   int inclusive;
41   int first, second;
42
43 };
44
45 #define ONE_MILLION 1000000
46
47 struct time_adjustment {
48   struct timeval tv;
49   int is_negative;
50 };
51
52 static struct select_item selectfrm[100];
53 static int max_selected = -1;
54 static int count = 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 */
61
62 /* Add a selection item, a simple parser for now */
63
64 static void add_selection(char *sel)
65 {
66   char *locn;
67   char *next;
68
69   if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
70     return;
71
72   printf("Add_Selected: %s\n", sel);
73
74   if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
75
76     printf("Not inclusive ...");
77
78     max_selected++;
79     selectfrm[max_selected].inclusive = 0;
80     selectfrm[max_selected].first = atoi(sel);
81
82     printf(" %i\n", selectfrm[max_selected].first);
83
84   }
85   else {
86
87     printf("Inclusive ...");
88
89     next = locn + 1;
90     max_selected++;
91     selectfrm[max_selected].inclusive = 1;
92     selectfrm[max_selected].first = atoi(sel);
93     selectfrm[max_selected].second = atoi(next);
94
95     printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
96
97   }
98
99
100 }
101
102 /* Was the record selected? */
103
104 static int selected(int recno)
105 {
106   int i = 0;
107
108   for (i = 0; i<= max_selected; i++) {
109
110     if (selectfrm[i].inclusive) {
111       if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
112         return 1;
113     }
114     else {
115       if (recno == selectfrm[i].first)
116         return 1;
117     }
118   }
119
120   return 0;
121
122 }
123
124 /* An argument to the callback routine */
125
126 typedef struct {
127         char    *filename;
128         wtap_dumper *pdh;
129 } callback_arg;
130
131 /*
132  *The callback routine that is called for each frame in the input file
133  */
134
135 static void
136 edit_callback(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
137     union wtap_pseudo_header *pseudo_header, const guchar *buf)
138 {
139   callback_arg *argp = (callback_arg *)user;
140   int err;
141   struct wtap_pkthdr snap_phdr;
142
143   if ((!selected(count) && !keep_em) ||
144       (selected(count) && keep_em)) {
145
146     if (verbose)
147       printf("Record: %u\n", count);
148
149     /* We simply write it, perhaps after truncating it; we could do other
150        things, like modify it. */
151
152     if (snaplen != 0 && phdr->caplen > snaplen) {
153       snap_phdr = *phdr;
154       snap_phdr.caplen = snaplen;
155       phdr = &snap_phdr;
156     }
157
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) {
161       snap_phdr = *phdr;
162       if (time_adj.is_negative)
163         snap_phdr.ts.tv_sec -= time_adj.tv.tv_sec;
164       else
165         snap_phdr.ts.tv_sec += time_adj.tv.tv_sec;
166       phdr = &snap_phdr;
167     }
168
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) {
172       snap_phdr = *phdr;
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;
177         }
178         snap_phdr.ts.tv_usec -= time_adj.tv.tv_usec;
179       } else {                  /* add */
180         if (snap_phdr.ts.tv_usec + time_adj.tv.tv_usec > ONE_MILLION) {
181           /* carry */
182           snap_phdr.ts.tv_sec++;
183           snap_phdr.ts.tv_usec += time_adj.tv.tv_usec - ONE_MILLION;
184         } else {
185           snap_phdr.ts.tv_usec += time_adj.tv.tv_usec;
186         }
187       }
188       phdr = &snap_phdr;
189     }
190
191     if (!wtap_dump(argp->pdh, phdr, pseudo_header, buf, &err)) {
192
193       fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
194         wtap_strerror(err));
195       exit(1);
196
197     }
198
199   }
200
201   count++;
202
203 }
204
205 static void
206 set_time_adjustment(char *optarg)
207 {
208   char *frac, *end;
209   long val;
210   int frac_digits;
211
212   if (!optarg)
213     return;
214
215   /* skip leading whitespace */
216   while (*optarg == ' ' || *optarg == '\t') {
217       optarg++;
218   }
219
220   /* check for a negative adjustment */
221   if (*optarg == '-') {
222       time_adj.is_negative = 1;
223       optarg++;
224   }
225
226   /* collect whole number of seconds, if any */
227   if (*optarg == '.') {         /* only fractional (i.e., .5 is ok) */
228       val  = 0;
229       frac = optarg;
230   } else {
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 time adjustment\n",
234                   optarg);
235           exit(1);
236       }
237       if (val < 0) {            /* implies '--' since we caught '-' above  */
238           fprintf(stderr, "editcap: \"%s\" is not a valid time adjustment\n",
239                   optarg);
240           exit(1);
241       }
242   }
243   time_adj.tv.tv_sec = val;
244
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",
251               optarg);
252       exit(1);
253     }
254   }
255   else {
256     return;                     /* no fractional digits */
257   }
258
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 */
264       val *= 10;
265       frac_digits++;
266     }
267   }
268   time_adj.tv.tv_usec = val;
269 }
270
271 static void usage()
272 {
273   int i;
274   const char *string;
275
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);
286       if (string != NULL)
287         fprintf(stderr, "       \t    %s - %s\n",
288           string, wtap_encap_string(i));
289   }
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));
296   }
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");
303 }
304
305 int main(int argc, char *argv[])
306
307 {
308   wtap *wth;
309   int i, err;
310   callback_arg args;
311   extern char *optarg;
312   extern int optind;
313   int opt;
314   char *p;
315   int snapshot_length;
316
317   /* Process the options first */
318
319   while ((opt = getopt(argc, argv, "T:F:rvs:t:h")) !=-1) {
320
321     switch (opt) {
322
323     case 'T':
324       out_frame_type = wtap_short_string_to_encap(optarg);
325       if (out_frame_type < 0) {
326         fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
327             optarg);
328         exit(1);
329       }
330       break;
331
332     case 'F':
333       out_file_type = wtap_short_string_to_file_type(optarg);
334       if (out_file_type < 0) {
335         fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
336             optarg);
337         exit(1);
338       }
339       break;
340
341     case 'v':
342       verbose = !verbose;  /* Just invert */
343       break;
344
345     case 'r':
346       keep_em = !keep_em;  /* Just invert */
347       break;
348
349     case 's':
350       snaplen = strtol(optarg, &p, 10);
351       if (p == optarg || *p != '\0') {
352         fprintf(stderr, "editcap: \"%s\" is not a valid snapshot length\n",
353             optarg);
354         exit(1);
355       }
356       break;
357
358     case 't':
359       set_time_adjustment(optarg);
360       break;
361
362     case 'h':
363       usage();
364       exit(1);
365       break;
366
367     case '?':              /* Bad options if GNU getopt */
368       usage();
369       exit(1);
370       break;
371
372     }
373
374   }
375
376 #ifdef DEBUG
377   printf("Optind = %i, argc = %i\n", optind, argc);
378 #endif
379
380   if ((argc - optind) < 1) {
381
382     usage();
383     exit(1);
384
385   }
386
387   wth = wtap_open_offline(argv[optind], &err, FALSE);
388
389   if (!wth) {
390
391     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
392         wtap_strerror(err));
393     exit(1);
394
395   }
396
397   if (verbose) {
398
399     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
400             wtap_file_type_string(wtap_file_type(wth)));
401
402   }
403
404   /*
405    * Now, process the rest, if any ... we only write if there is an extra
406    * argument or so ...
407    */
408
409   if ((argc - optind) >= 2) {
410
411     args.filename = argv[optind + 1];
412     if (out_frame_type == -2)
413       out_frame_type = wtap_file_encap(wth);
414
415     snapshot_length = wtap_snapshot_length(wth);
416     if (snapshot_length == 0) {
417       /* Snapshot length of input file not known. */
418       snapshot_length = WTAP_MAX_PACKET_SIZE;
419     }
420     args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
421                               out_frame_type, wtap_snapshot_length(wth), &err);
422     if (args.pdh == NULL) {
423
424       fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
425               wtap_strerror(err));
426       exit(1);
427
428     }
429
430     for (i = optind + 2; i < argc; i++)
431       add_selection(argv[i]);
432
433     wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
434
435     if (!wtap_dump_close(args.pdh, &err)) {
436
437       fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
438               wtap_strerror(err));
439       exit(1);
440
441     }
442   }
443
444   return 0;
445 }
446