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