1 /* Edit capture files. We can delete records, or simply convert from one
2 * format to another format.
4 * $Id: editcap.c,v 1.6 2000/01/17 20:21:40 guy Exp $
6 * Originally written by Richard Sharpe.
7 * Improved by Guy Harris.
8 * Further improved by Richard Sharpe.
20 * Some globals so we can pass things to various routines
30 struct select_item selectfrm[100];
31 int max_selected = -1;
33 static int keep_em = 0;
34 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
35 static int out_frame_type = -2; /* Leave frame type alone */
36 static int verbose = 0; /* Not so verbose */
38 /* Add a selection item, a simple parser for now */
40 void add_selection(char *sel)
45 if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
48 printf("Add_Selected: %s\n", sel);
50 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
52 printf("Not inclusive ...");
55 selectfrm[max_selected].inclusive = 0;
56 selectfrm[max_selected].first = atoi(sel);
58 printf(" %i\n", selectfrm[max_selected].first);
63 printf("Inclusive ...");
67 selectfrm[max_selected].inclusive = 1;
68 selectfrm[max_selected].first = atoi(sel);
69 selectfrm[max_selected].second = atoi(next);
71 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
78 /* Was the record selected? */
80 int selected(int recno)
84 for (i = 0; i<= max_selected; i++) {
86 if (selectfrm[i].inclusive) {
87 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
91 if (recno == selectfrm[i].first)
100 /* An argument to the callback routine */
108 *The callback routine that is called for each frame in the input file
112 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
115 callback_arg *argp = (callback_arg *)user;
118 if ((!selected(count) && !keep_em) ||
119 (selected(count) && keep_em)) {
122 printf("Record: %u\n", count);
124 /* We simply write it, we could do other things, like modify it */
126 if (!wtap_dump(argp->pdh, phdr, buf, &err)) {
128 fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
145 fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>] <infile>\\\n");
146 fprintf(stderr, " <outfile> [ <record#>[-<record#>] ... ]\n");
147 fprintf(stderr, " where\t-r specifies that the records specified should be kept, not deleted, \n");
148 fprintf(stderr, " default is to delete\n");
149 fprintf(stderr, " \t-v specifies verbose operation, default is silent\n");
150 fprintf(stderr, " \t-h produces this help listing.\n");
151 fprintf(stderr, " \t-T <encap type> specifies the encapsulation type to use:\n");
152 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
153 string = wtap_encap_short_string(i);
155 fprintf(stderr, " \t %s - %s\n",
156 string, wtap_encap_string(i));
158 fprintf(stderr, " \t default is the same as the input file\n");
159 fprintf(stderr, " \t-F <capture type> specifies the capture file type to write:\n");
160 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
161 if (wtap_dump_can_open(i))
162 fprintf(stderr, " \t %s - %s\n",
163 wtap_file_type_short_string(i), wtap_file_type_string(i));
165 fprintf(stderr, " \t default is libpcap\n");
166 fprintf(stderr, "\n \t A range of records can be specified as well\n");
169 int main(int argc, char *argv[])
176 extern int optind, opterr, optopt;
179 /* Process the options first */
181 while ((opt = getopt(argc, argv, "T:F:rv")) != EOF) {
186 out_frame_type = wtap_short_string_to_encap(optarg);
187 if (out_frame_type < 0) {
188 fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
195 out_file_type = wtap_short_string_to_file_type(optarg);
196 if (out_file_type < 0) {
197 fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
204 verbose = !verbose; /* Just invert */
208 keep_em = !keep_em; /* Just invert */
216 case '?': /* Bad options if GNU getopt */
226 printf("Optind = %i, argc = %i\n", optind, argc);
229 if ((argc - optind) < 1) {
236 wth = wtap_open_offline(argv[optind], &err);
240 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
248 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
249 wtap_file_type_string(wtap_file_type(wth)));
254 * Now, process the rest, if any ... we only write if there is an extra
258 if ((argc - optind) >= 2) {
260 args.filename = argv[optind + 1];
261 if (out_frame_type == -2)
262 out_frame_type = wtap_file_encap(wth);
264 args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
265 out_frame_type, wtap_snapshot_length(wth), &err);
266 if (args.pdh == NULL) {
268 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
274 for (i = optind + 2; i < argc; i++)
275 add_selection(argv[i]);
277 wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
279 if (!wtap_dump_close(args.pdh, &err)) {
281 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
289 return 0; /* Silence compiler warnings */