1 /* Edit capture files. We can delete records, or simply convert from one
2 * format to another format.
4 * $Id: editcap.c,v 1.7 2000/04/12 21:51:27 gram Exp $
6 * Originally written by Richard Sharpe.
7 * Improved by Guy Harris.
8 * Further improved by Richard Sharpe.
23 #ifdef HAVE_SYS_TIME_H
39 * Some globals so we can pass things to various routines
49 struct select_item selectfrm[100];
50 int max_selected = -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 */
57 /* Add a selection item, a simple parser for now */
59 void add_selection(char *sel)
64 if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
67 printf("Add_Selected: %s\n", sel);
69 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
71 printf("Not inclusive ...");
74 selectfrm[max_selected].inclusive = 0;
75 selectfrm[max_selected].first = atoi(sel);
77 printf(" %i\n", selectfrm[max_selected].first);
82 printf("Inclusive ...");
86 selectfrm[max_selected].inclusive = 1;
87 selectfrm[max_selected].first = atoi(sel);
88 selectfrm[max_selected].second = atoi(next);
90 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
97 /* Was the record selected? */
99 int selected(int recno)
103 for (i = 0; i<= max_selected; i++) {
105 if (selectfrm[i].inclusive) {
106 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
110 if (recno == selectfrm[i].first)
119 /* An argument to the callback routine */
127 *The callback routine that is called for each frame in the input file
131 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
134 callback_arg *argp = (callback_arg *)user;
137 if ((!selected(count) && !keep_em) ||
138 (selected(count) && keep_em)) {
141 printf("Record: %u\n", count);
143 /* We simply write it, we could do other things, like modify it */
145 if (!wtap_dump(argp->pdh, phdr, buf, &err)) {
147 fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
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);
174 fprintf(stderr, " \t %s - %s\n",
175 string, wtap_encap_string(i));
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));
184 fprintf(stderr, " \t default is libpcap\n");
185 fprintf(stderr, "\n \t A range of records can be specified as well\n");
188 int main(int argc, char *argv[])
195 extern int optind, opterr, optopt;
198 /* Process the options first */
200 while ((opt = getopt(argc, argv, "T:F:rv")) != EOF) {
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",
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",
223 verbose = !verbose; /* Just invert */
227 keep_em = !keep_em; /* Just invert */
235 case '?': /* Bad options if GNU getopt */
245 printf("Optind = %i, argc = %i\n", optind, argc);
248 if ((argc - optind) < 1) {
255 wth = wtap_open_offline(argv[optind], &err);
259 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
267 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
268 wtap_file_type_string(wtap_file_type(wth)));
273 * Now, process the rest, if any ... we only write if there is an extra
277 if ((argc - optind) >= 2) {
279 args.filename = argv[optind + 1];
280 if (out_frame_type == -2)
281 out_frame_type = wtap_file_encap(wth);
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) {
287 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
293 for (i = optind + 2; i < argc; i++)
294 add_selection(argv[i]);
296 wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
298 if (!wtap_dump_close(args.pdh, &err)) {
300 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
308 return 0; /* Silence compiler warnings */