1 /* Edit capture files. We can delete records, or simply convert from one
2 * format to another format.
4 * $Id: editcap.c,v 1.4 1999/12/12 21:04:29 sharpe Exp $
6 * Originally written by Richard Sharpe.
7 * Improved by Guy Harris.
18 * Some globals so we can pass things to various routines
21 int selectfrm[100], max_selected = -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 */
28 /* Was the record selected? */
30 int selected(int recno)
34 for (i = 0; i<= max_selected; i++) {
36 if (recno == selectfrm[i]) return 1;
44 /* An argument to the callback routine */
52 *The callback routine that is called for each frame in the input file
56 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
59 callback_arg *argp = (callback_arg *)user;
62 if ((!selected(count) && !keep_em) ||
63 (selected(count) && keep_em)) {
66 printf("Record: %u\n", count);
68 /* We simply write it, we could do other things, like modify it */
70 if (!wtap_dump(argp->pdh, phdr, buf, &err)) {
72 fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
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);
99 fprintf(stderr, " \t %s - %s\n",
100 string, wtap_encap_string(i));
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));
109 fprintf(stderr, " \t default is libpcap\n");
112 int main(int argc, char *argv[])
119 extern int optind, opterr, optopt;
122 /* Process the options first */
124 while ((opt = getopt(argc, argv, "T:F:rv")) != EOF) {
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",
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",
147 verbose = !verbose; /* Just invert */
151 keep_em = !keep_em; /* Just invert */
159 case '?': /* Bad options if GNU getopt */
169 printf("Optind = %i, argc = %i\n", optind, argc);
172 if ((argc - optind) < 1) {
179 wth = wtap_open_offline(argv[optind], &err);
183 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
191 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
192 wtap_file_type_string(wtap_file_type(wth)));
197 * Now, process the rest, if any ... we only write if there is an extra
201 if ((argc - optind) >= 2) {
203 args.filename = argv[optind + 1];
204 if (out_frame_type == -2)
205 out_frame_type = wtap_file_encap(wth);
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) {
211 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
217 for (i = optind + 2; i < argc; i++)
218 selectfrm[++max_selected] = atoi(argv[i]);
220 wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
222 if (!wtap_dump_close(args.pdh, &err)) {
224 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
232 return 0; /* Silence compiler warnings */