Add sample strace output to illustrate the timeout problem.
[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.4 1999/12/12 21:04:29 sharpe Exp $
5  *
6  * Originally written by Richard Sharpe.
7  * Improved by Guy Harris.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <glib.h>
13 #include <unistd.h>
14 #include <sys/time.h>
15 #include "wtap.h"
16
17 /*
18  * Some globals so we can pass things to various routines
19  */
20
21 int selectfrm[100], max_selected = -1;
22 static int count = 1;
23 static int keep_em = 0;
24 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
25 static int out_frame_type = -2;              /* Leave frame type alone */
26 static int verbose = 0;                      /* Not so verbose         */
27
28 /* Was the record selected? */
29
30 int selected(int recno)
31 {
32   int i = 0;
33
34   for (i = 0; i<= max_selected; i++) {
35
36     if (recno == selectfrm[i]) return 1;
37
38   }
39
40   return 0;
41
42 }
43
44 /* An argument to the callback routine */
45
46 typedef struct {
47         char    *filename;
48         wtap_dumper *pdh;
49 } callback_arg;
50
51 /*
52  *The callback routine that is called for each frame in the input file
53  */
54
55 static void
56 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
57     const u_char *buf) 
58 {
59   callback_arg *argp = (callback_arg *)user;
60   int err;
61
62   if ((!selected(count) && !keep_em) ||
63       (selected(count) && keep_em)) {
64
65     if (verbose)
66       printf("Record: %u\n", count);
67
68     /* We simply write it, we could do other things, like modify it */
69
70     if (!wtap_dump(argp->pdh, phdr, buf, &err)) {
71
72       fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
73         wtap_strerror(err));
74       exit(1);
75
76     }
77
78   }
79
80   count++;
81
82 }
83
84 void usage()
85 {
86   int i;
87   const char *string;
88
89   fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>] <infile>\\\n"); 
90   fprintf(stderr, "                <outfile> [ <record#> ... ]\n");
91   fprintf(stderr, "  where\t-r specifies that the records specified should be kept, not deleted, \n");
92   fprintf(stderr, "                           default is to delete\n");
93   fprintf(stderr, "       \t-v specifies verbose operation, default is silent\n");
94   fprintf(stderr, "       \t-h produces this help listing.\n");
95   fprintf(stderr, "       \t-T <encap type> specifies the encapsulation type to use:\n");
96   for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
97       string = wtap_encap_short_string(i);
98       if (string != NULL)
99         fprintf(stderr, "       \t    %s - %s\n",
100           string, wtap_encap_string(i));
101   }
102   fprintf(stderr, "       \t    default is the same as the input file\n");
103   fprintf(stderr, "       \t-F <capture type> specifies the capture file type to write:\n");
104   for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
105     if (wtap_dump_can_open(i))
106       fprintf(stderr, "       \t    %s - %s\n",
107         wtap_file_type_short_string(i), wtap_file_type_string(i));
108   }
109   fprintf(stderr, "       \t    default is libpcap\n");
110 }
111
112 int main(int argc, char *argv[])
113
114 {
115   wtap *wth;
116   int i, err;
117   callback_arg args;
118   extern char *optarg;
119   extern int optind, opterr, optopt;
120   char opt;
121
122   /* Process the options first */
123
124   while ((opt = getopt(argc, argv, "T:F:rv")) != EOF) {
125
126     switch (opt) {
127
128     case 'T':
129       out_frame_type = wtap_short_string_to_encap(optarg);
130       if (out_frame_type < 0) {
131         fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
132             optarg);
133         exit(1);
134       }
135       break;
136       
137     case 'F':
138       out_file_type = wtap_short_string_to_file_type(optarg);
139       if (out_file_type < 0) {
140         fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
141             optarg);
142         exit(1);
143       }
144       break;
145
146     case 'v':
147       verbose = !verbose;  /* Just invert */
148       break;
149
150     case 'r':
151       keep_em = !keep_em;  /* Just invert */
152       break;
153
154     case 'h':
155       usage();
156       exit(1);
157       break;
158
159     case '?':              /* Bad options if GNU getopt */
160       usage();
161       exit(1);
162       break;
163
164     }
165
166   }
167
168 #ifdef DEBUG
169   printf("Optind = %i, argc = %i\n", optind, argc);
170 #endif
171
172   if ((argc - optind) < 1) {
173
174     usage();
175     exit(1);
176
177   }
178
179   wth = wtap_open_offline(argv[optind], &err);
180
181   if (!wth) {
182
183     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
184         wtap_strerror(err));
185     exit(1);
186
187   }
188
189   if (verbose) {
190
191     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
192             wtap_file_type_string(wtap_file_type(wth)));
193
194   }
195
196   /*
197    * Now, process the rest, if any ... we only write if there is an extra
198    * argument or so ...
199    */
200
201   if ((argc - optind) >= 2) {
202
203     args.filename = argv[optind + 1];
204     if (out_frame_type == -2)
205       out_frame_type = wtap_file_encap(wth);
206
207     args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
208                               out_frame_type, wtap_snapshot_length(wth), &err);
209     if (args.pdh == NULL) {
210
211       fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
212               wtap_strerror(err));
213       exit(1);
214
215     }
216
217     for (i = optind + 2; i < argc; i++)
218       selectfrm[++max_selected] = atoi(argv[i]);
219
220     wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
221
222     if (!wtap_dump_close(args.pdh, &err)) {
223
224       fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
225               wtap_strerror(err));
226       exit(1);
227
228     }
229   }
230
231   exit(0);
232   return 0;  /* Silence compiler warnings */
233 }
234