1 /* Edit capture files. We can delete records, or simply convert from one
2 * format to another format.
4 * $Id: editcap.c,v 1.3 1999/12/05 01:27:14 guy Exp $
6 * Originally written by Richard Sharpe.
7 * Improved by Guy Harris.
17 * Some globals so we can pass things to various routines
20 int selectfrm[100], max_selected = -1;
22 static int keep_em = 0;
23 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
24 static int out_frame_type = -2; /* Leave frame type alone */
26 /* Was the record selected? */
28 int selected(int recno)
32 for (i = 0; i<= max_selected; i++) {
34 if (recno == selectfrm[i]) return 1;
42 /* An argument to the callback routine */
50 *The callback routine that is called for each frame in the input file
54 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
57 callback_arg *argp = (callback_arg *)user;
60 if ((!selected(count) && !keep_em) ||
61 (selected(count) && keep_em)) {
63 printf("Record: %u\n", count);
65 /* We simply write it, we could do other things, like modify it */
67 if (!wtap_dump(argp->pdh, phdr, buf, &err)) {
69 fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
86 fprintf(stderr, "Usage: editcap [-r] [-T <encap type>] [-F <capture type>] <infile> <outfile>\\\n");
87 fprintf(stderr, " [ <record#> ... ]\n");
88 fprintf(stderr, " where\t-r specifies that the records specified should be kept, not deleted, \n");
89 fprintf(stderr, " default is to delete\n");
90 fprintf(stderr, " \t-T <encap type> specifies the encapsulation type to use:\n");
91 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
92 string = wtap_encap_short_string(i);
94 fprintf(stderr, " \t %s - %s\n",
95 string, wtap_encap_string(i));
97 fprintf(stderr, " \t default is the same as the input file\n");
98 fprintf(stderr, " \t-F <capture type> specifies the capture file type to write:\n");
99 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
100 if (wtap_dump_can_open(i))
101 fprintf(stderr, " \t %s - %s\n",
102 wtap_file_type_short_string(i), wtap_file_type_string(i));
104 fprintf(stderr, " \t default is libpcap\n");
107 int main(int argc, char *argv[])
111 int read_bytes, pcnt = 0, i, err;
114 extern int optind, opterr, optopt;
117 /* Process the options first */
119 while ((opt = getopt(argc, argv, "T:F:r")) != EOF) {
124 out_frame_type = wtap_short_string_to_encap(optarg);
125 if (out_frame_type < 0) {
126 fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
133 out_file_type = wtap_short_string_to_file_type(optarg);
134 if (out_file_type < 0) {
135 fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
142 keep_em = !keep_em; /* Just invert */
145 case '?': /* Bad options if GNU getopt */
154 printf("Optind = %i, argc = %i\n", optind, argc);
156 if ((argc - optind) < 2) {
163 wth = wtap_open_offline(argv[optind], &err);
167 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
173 args.filename = argv[optind + 1];
174 if (out_frame_type == -2)
175 out_frame_type = wtap_file_encap(wth);
177 args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
178 out_frame_type, wtap_snapshot_length(wth), &err);
179 if (args.pdh == NULL) {
181 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
187 for (i = optind + 2; i < argc; i++)
188 selectfrm[++max_selected] = atoi(argv[i]);
190 wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
192 if (!wtap_dump_close(args.pdh, &err)) {
194 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],