1 /* Edit capture files. We can delete records, or simply convert from one
2 * format to another format.
4 * $Id: editcap.c,v 1.13 2000/12/03 21:11:05 guy 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 */
56 static int snaplen = 0; /* No limit */
58 /* Add a selection item, a simple parser for now */
60 void add_selection(char *sel)
65 if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
68 printf("Add_Selected: %s\n", sel);
70 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
72 printf("Not inclusive ...");
75 selectfrm[max_selected].inclusive = 0;
76 selectfrm[max_selected].first = atoi(sel);
78 printf(" %i\n", selectfrm[max_selected].first);
83 printf("Inclusive ...");
87 selectfrm[max_selected].inclusive = 1;
88 selectfrm[max_selected].first = atoi(sel);
89 selectfrm[max_selected].second = atoi(next);
91 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
98 /* Was the record selected? */
100 int selected(int recno)
104 for (i = 0; i<= max_selected; i++) {
106 if (selectfrm[i].inclusive) {
107 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
111 if (recno == selectfrm[i].first)
120 /* An argument to the callback routine */
128 *The callback routine that is called for each frame in the input file
132 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
133 union wtap_pseudo_header *pseudo_header, const u_char *buf)
135 callback_arg *argp = (callback_arg *)user;
137 struct wtap_pkthdr snap_phdr;
139 if ((!selected(count) && !keep_em) ||
140 (selected(count) && keep_em)) {
143 printf("Record: %u\n", count);
145 /* We simply write it, perhaps after truncating it; we could do other
146 things, like modify it. */
148 if (snaplen != 0 && phdr->caplen > snaplen) {
150 snap_phdr.caplen = snaplen;
154 if (!wtap_dump(argp->pdh, phdr, pseudo_header, buf, &err)) {
156 fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
173 fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>]\n");
174 fprintf(stderr, " [-s <snaplen>] <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
175 fprintf(stderr, " where\t-r specifies that the records specified should be kept, not deleted, \n");
176 fprintf(stderr, " default is to delete\n");
177 fprintf(stderr, " \t-v specifies verbose operation, default is silent\n");
178 fprintf(stderr, " \t-h produces this help listing.\n");
179 fprintf(stderr, " \t-T <encap type> specifies the encapsulation type to use:\n");
180 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
181 string = wtap_encap_short_string(i);
183 fprintf(stderr, " \t %s - %s\n",
184 string, wtap_encap_string(i));
186 fprintf(stderr, " \t default is the same as the input file\n");
187 fprintf(stderr, " \t-F <capture type> specifies the capture file type to write:\n");
188 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
189 if (wtap_dump_can_open(i))
190 fprintf(stderr, " \t %s - %s\n",
191 wtap_file_type_short_string(i), wtap_file_type_string(i));
193 fprintf(stderr, " \t default is libpcap\n");
194 fprintf(stderr, " \t-s <snaplen> specifies that packets should be truncated to\n");
195 fprintf(stderr, " \t <snaplen> bytes of data\n");
196 fprintf(stderr, "\n \t A range of records can be specified as well\n");
199 int main(int argc, char *argv[])
210 /* Process the options first */
212 while ((opt = getopt(argc, argv, "T:F:rvs:h")) != EOF) {
217 out_frame_type = wtap_short_string_to_encap(optarg);
218 if (out_frame_type < 0) {
219 fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
226 out_file_type = wtap_short_string_to_file_type(optarg);
227 if (out_file_type < 0) {
228 fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
235 verbose = !verbose; /* Just invert */
239 keep_em = !keep_em; /* Just invert */
243 snaplen = strtol(optarg, &p, 10);
244 if (p == optarg || *p != '\0') {
245 fprintf(stderr, "editcap: \"%s\" is not a valid snapshot length\n",
256 case '?': /* Bad options if GNU getopt */
266 printf("Optind = %i, argc = %i\n", optind, argc);
269 if ((argc - optind) < 1) {
276 wth = wtap_open_offline(argv[optind], &err, FALSE);
280 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
288 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
289 wtap_file_type_string(wtap_file_type(wth)));
294 * Now, process the rest, if any ... we only write if there is an extra
298 if ((argc - optind) >= 2) {
300 args.filename = argv[optind + 1];
301 if (out_frame_type == -2)
302 out_frame_type = wtap_file_encap(wth);
304 args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
305 out_frame_type, wtap_snapshot_length(wth), &err);
306 if (args.pdh == NULL) {
308 fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
314 for (i = optind + 2; i < argc; i++)
315 add_selection(argv[i]);
317 wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
319 if (!wtap_dump_close(args.pdh, &err)) {
321 fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
329 return 0; /* Silence compiler warnings */