Give the IPX dissector dissector hash tables for the IPX type and socket
[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.11 2000/05/19 23:06:06 gram 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>] <infile>\\\n"); 
174   fprintf(stderr, "                <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, "\n      \t    A range of records can be specified as well\n");
195 }
196
197 int main(int argc, char *argv[])
198
199 {
200   wtap *wth;
201   int i, err;
202   callback_arg args;
203   extern char *optarg;
204   extern int optind;
205   char opt;
206   char *p;
207
208   /* Process the options first */
209
210   while ((opt = getopt(argc, argv, "T:F:rvs:")) != EOF) {
211
212     switch (opt) {
213
214     case 'T':
215       out_frame_type = wtap_short_string_to_encap(optarg);
216       if (out_frame_type < 0) {
217         fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
218             optarg);
219         exit(1);
220       }
221       break;
222       
223     case 'F':
224       out_file_type = wtap_short_string_to_file_type(optarg);
225       if (out_file_type < 0) {
226         fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
227             optarg);
228         exit(1);
229       }
230       break;
231
232     case 'v':
233       verbose = !verbose;  /* Just invert */
234       break;
235
236     case 'r':
237       keep_em = !keep_em;  /* Just invert */
238       break;
239
240     case 's':
241       snaplen = strtol(optarg, &p, 10);
242       if (p == optarg || *p != '\0') {
243         fprintf(stderr, "editcap: \"%s\" is not a valid snapshot length\n",
244             optarg);
245         exit(1);
246       }
247       break;
248
249     case 'h':
250       usage();
251       exit(1);
252       break;
253
254     case '?':              /* Bad options if GNU getopt */
255       usage();
256       exit(1);
257       break;
258
259     }
260
261   }
262
263 #ifdef DEBUG
264   printf("Optind = %i, argc = %i\n", optind, argc);
265 #endif
266
267   if ((argc - optind) < 1) {
268
269     usage();
270     exit(1);
271
272   }
273
274   wth = wtap_open_offline(argv[optind], &err, FALSE);
275
276   if (!wth) {
277
278     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
279         wtap_strerror(err));
280     exit(1);
281
282   }
283
284   if (verbose) {
285
286     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
287             wtap_file_type_string(wtap_file_type(wth)));
288
289   }
290
291   /*
292    * Now, process the rest, if any ... we only write if there is an extra
293    * argument or so ...
294    */
295
296   if ((argc - optind) >= 2) {
297
298     args.filename = argv[optind + 1];
299     if (out_frame_type == -2)
300       out_frame_type = wtap_file_encap(wth);
301
302     args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
303                               out_frame_type, wtap_snapshot_length(wth), &err);
304     if (args.pdh == NULL) {
305
306       fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
307               wtap_strerror(err));
308       exit(1);
309
310     }
311
312     for (i = optind + 2; i < argc; i++)
313       add_selection(argv[i]);
314
315     wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
316
317     if (!wtap_dump_close(args.pdh, &err)) {
318
319       fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
320               wtap_strerror(err));
321       exit(1);
322
323     }
324   }
325
326   exit(0);
327   return 0;  /* Silence compiler warnings */
328 }
329