Tvbuffify the BOOTP/DHCP dissector.
[obnox/wireshark/wip.git] / editcap.c
1 /* Edit capture files.  We can delete records, or simply convert from one 
2  * format to another format.
3  *
4  * $Id: editcap.c,v 1.13 2000/12/03 21:11:05 guy 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 #ifdef HAVE_WINSOCK_H
28 #include <winsock.h>
29 #endif
30
31 #include <string.h>
32 #include "wtap.h"
33
34 #ifdef NEED_GETOPT_H
35 #include "getopt.h"
36 #endif
37
38 /*
39  * Some globals so we can pass things to various routines
40  */
41
42 struct select_item {
43
44   int inclusive;
45   int first, second;
46
47 } select_item;
48
49 struct select_item selectfrm[100];
50 int max_selected = -1;
51 static int count = 1;
52 static int keep_em = 0;
53 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
54 static int out_frame_type = -2;              /* Leave frame type alone */
55 static int verbose = 0;                      /* Not so verbose         */
56 static int snaplen = 0;                      /* No limit               */
57
58 /* Add a selection item, a simple parser for now */
59
60 void add_selection(char *sel) 
61 {
62   char *locn;
63   char *next;
64
65   if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
66     return;
67
68   printf("Add_Selected: %s\n", sel);
69
70   if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
71
72     printf("Not inclusive ...");
73
74     max_selected++;
75     selectfrm[max_selected].inclusive = 0;
76     selectfrm[max_selected].first = atoi(sel);
77
78     printf(" %i\n", selectfrm[max_selected].first);
79
80   }
81   else {
82
83     printf("Inclusive ...");
84
85     next = locn + 1;
86     max_selected++;
87     selectfrm[max_selected].inclusive = 1;
88     selectfrm[max_selected].first = atoi(sel);
89     selectfrm[max_selected].second = atoi(next);
90
91     printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
92
93   }
94
95
96 }
97
98 /* Was the record selected? */
99
100 int selected(int recno)
101 {
102   int i = 0;
103
104   for (i = 0; i<= max_selected; i++) {
105
106     if (selectfrm[i].inclusive) {
107       if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
108         return 1;
109     }
110     else {
111       if (recno == selectfrm[i].first)
112         return 1;
113     }
114   }
115
116   return 0;
117
118 }
119
120 /* An argument to the callback routine */
121
122 typedef struct {
123         char    *filename;
124         wtap_dumper *pdh;
125 } callback_arg;
126
127 /*
128  *The callback routine that is called for each frame in the input file
129  */
130
131 static void
132 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
133     union wtap_pseudo_header *pseudo_header, const u_char *buf) 
134 {
135   callback_arg *argp = (callback_arg *)user;
136   int err;
137   struct wtap_pkthdr snap_phdr;
138
139   if ((!selected(count) && !keep_em) ||
140       (selected(count) && keep_em)) {
141
142     if (verbose)
143       printf("Record: %u\n", count);
144
145     /* We simply write it, perhaps after truncating it; we could do other
146        things, like modify it. */
147
148     if (snaplen != 0 && phdr->caplen > snaplen) {
149       snap_phdr = *phdr;
150       snap_phdr.caplen = snaplen;
151       phdr = &snap_phdr;
152     }
153
154     if (!wtap_dump(argp->pdh, phdr, pseudo_header, buf, &err)) {
155
156       fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
157         wtap_strerror(err));
158       exit(1);
159
160     }
161
162   }
163
164   count++;
165
166 }
167
168 void usage()
169 {
170   int i;
171   const char *string;
172
173   fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>]\n");
174   fprintf(stderr, "               [-s <snaplen>] <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
175   fprintf(stderr, "  where\t-r specifies that the records specified should be kept, not deleted, \n");
176   fprintf(stderr, "                           default is to delete\n");
177   fprintf(stderr, "       \t-v specifies verbose operation, default is silent\n");
178   fprintf(stderr, "       \t-h produces this help listing.\n");
179   fprintf(stderr, "       \t-T <encap type> specifies the encapsulation type to use:\n");
180   for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
181       string = wtap_encap_short_string(i);
182       if (string != NULL)
183         fprintf(stderr, "       \t    %s - %s\n",
184           string, wtap_encap_string(i));
185   }
186   fprintf(stderr, "       \t    default is the same as the input file\n");
187   fprintf(stderr, "       \t-F <capture type> specifies the capture file type to write:\n");
188   for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
189     if (wtap_dump_can_open(i))
190       fprintf(stderr, "       \t    %s - %s\n",
191         wtap_file_type_short_string(i), wtap_file_type_string(i));
192   }
193   fprintf(stderr, "       \t    default is libpcap\n");
194   fprintf(stderr, "       \t-s <snaplen> specifies that packets should be truncated to\n");
195   fprintf(stderr, "       \t   <snaplen> bytes of data\n");
196   fprintf(stderr, "\n      \t    A range of records can be specified as well\n");
197 }
198
199 int main(int argc, char *argv[])
200
201 {
202   wtap *wth;
203   int i, err;
204   callback_arg args;
205   extern char *optarg;
206   extern int optind;
207   char opt;
208   char *p;
209
210   /* Process the options first */
211
212   while ((opt = getopt(argc, argv, "T:F:rvs:h")) != EOF) {
213
214     switch (opt) {
215
216     case 'T':
217       out_frame_type = wtap_short_string_to_encap(optarg);
218       if (out_frame_type < 0) {
219         fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
220             optarg);
221         exit(1);
222       }
223       break;
224       
225     case 'F':
226       out_file_type = wtap_short_string_to_file_type(optarg);
227       if (out_file_type < 0) {
228         fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
229             optarg);
230         exit(1);
231       }
232       break;
233
234     case 'v':
235       verbose = !verbose;  /* Just invert */
236       break;
237
238     case 'r':
239       keep_em = !keep_em;  /* Just invert */
240       break;
241
242     case 's':
243       snaplen = strtol(optarg, &p, 10);
244       if (p == optarg || *p != '\0') {
245         fprintf(stderr, "editcap: \"%s\" is not a valid snapshot length\n",
246             optarg);
247         exit(1);
248       }
249       break;
250
251     case 'h':
252       usage();
253       exit(1);
254       break;
255
256     case '?':              /* Bad options if GNU getopt */
257       usage();
258       exit(1);
259       break;
260
261     }
262
263   }
264
265 #ifdef DEBUG
266   printf("Optind = %i, argc = %i\n", optind, argc);
267 #endif
268
269   if ((argc - optind) < 1) {
270
271     usage();
272     exit(1);
273
274   }
275
276   wth = wtap_open_offline(argv[optind], &err, FALSE);
277
278   if (!wth) {
279
280     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
281         wtap_strerror(err));
282     exit(1);
283
284   }
285
286   if (verbose) {
287
288     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
289             wtap_file_type_string(wtap_file_type(wth)));
290
291   }
292
293   /*
294    * Now, process the rest, if any ... we only write if there is an extra
295    * argument or so ...
296    */
297
298   if ((argc - optind) >= 2) {
299
300     args.filename = argv[optind + 1];
301     if (out_frame_type == -2)
302       out_frame_type = wtap_file_encap(wth);
303
304     args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
305                               out_frame_type, wtap_snapshot_length(wth), &err);
306     if (args.pdh == NULL) {
307
308       fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
309               wtap_strerror(err));
310       exit(1);
311
312     }
313
314     for (i = optind + 2; i < argc; i++)
315       add_selection(argv[i]);
316
317     wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
318
319     if (!wtap_dump_close(args.pdh, &err)) {
320
321       fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
322               wtap_strerror(err));
323       exit(1);
324
325     }
326   }
327
328   exit(0);
329   return 0;  /* Silence compiler warnings */
330 }
331