2 * Routines for capture options setting
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #include <epan/packet.h>
45 #include "ringbuffer.h"
49 capture_opts_init(capture_options *capture_opts, void *cfile)
51 capture_opts->cf = cfile;
52 capture_opts->cfilter = g_strdup(""); /* No capture filter string specified */
53 capture_opts->iface = NULL; /* Default is "pick the first interface" */
55 capture_opts->buffer_size = 1; /* 1 MB */
57 capture_opts->has_snaplen = FALSE;
58 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
59 infinite, in effect */
60 capture_opts->promisc_mode = TRUE; /* promiscuous mode is the default */
61 capture_opts->linktype = -1; /* the default linktype */
62 capture_opts->capture_child = FALSE;
63 capture_opts->save_file = NULL;
64 capture_opts->save_file_fd = -1;
65 capture_opts->sync_mode = TRUE;
66 capture_opts->show_info = TRUE;
67 capture_opts->quit_after_cap = FALSE;
69 capture_opts->multi_files_on = FALSE;
70 capture_opts->has_file_duration = FALSE;
71 capture_opts->file_duration = 60; /* 1 min */
72 capture_opts->has_ring_num_files = FALSE;
73 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
75 capture_opts->has_autostop_files = FALSE;
76 capture_opts->autostop_files = 1;
77 capture_opts->has_autostop_packets = FALSE;
78 capture_opts->autostop_packets = 0;
79 capture_opts->has_autostop_filesize = FALSE;
80 capture_opts->autostop_filesize = 1024 * 1024; /* 1 MB */
81 capture_opts->has_autostop_duration = FALSE;
82 capture_opts->autostop_duration = 60; /* 1 min */
85 capture_opts->fork_child = -1; /* invalid process handle */
89 get_natural_int(const char *appname, const char *string, const char *name)
94 number = strtol(string, &p, 10);
95 if (p == string || *p != '\0') {
96 fprintf(stderr, "%s: The specified %s \"%s\" isn't a decimal number\n",
97 appname, name, string);
101 fprintf(stderr, "%s: The specified %s \"%s\" is a negative number\n",
102 appname, name, string);
105 if (number > INT_MAX) {
106 fprintf(stderr, "%s: The specified %s \"%s\" is too large (greater than %d)\n",
107 appname, name, string, INT_MAX);
115 get_positive_int(const char *appname, const char *string, const char *name)
119 number = get_natural_int(appname, string, name);
122 fprintf(stderr, "%s: The specified %s is zero\n",
132 * Given a string of the form "<autostop criterion>:<value>", as might appear
133 * as an argument to a "-a" option, parse it and set the criterion in
134 * question. Return an indication of whether it succeeded or failed
138 set_autostop_criterion(capture_options *capture_opts, const char *appname, const char *autostoparg)
142 colonp = strchr(autostoparg, ':');
150 * Skip over any white space (there probably won't be any, but
151 * as we allow it in the preferences file, we might as well
154 while (isspace((guchar)*p))
158 * Put the colon back, so if our caller uses, in an
159 * error message, the string they passed us, the message
165 if (strcmp(autostoparg,"duration") == 0) {
166 capture_opts->has_autostop_duration = TRUE;
167 capture_opts->autostop_duration = get_positive_int(appname, p,"autostop duration");
168 } else if (strcmp(autostoparg,"filesize") == 0) {
169 capture_opts->has_autostop_filesize = TRUE;
170 capture_opts->autostop_filesize = get_positive_int(appname, p,"autostop filesize");
171 } else if (strcmp(autostoparg,"files") == 0) {
172 capture_opts->multi_files_on = TRUE;
173 capture_opts->has_autostop_files = TRUE;
174 capture_opts->autostop_files = get_positive_int(appname, p,"autostop files");
178 *colonp = ':'; /* put the colon back */
183 * Given a string of the form "<ring buffer file>:<duration>", as might appear
184 * as an argument to a "-b" option, parse it and set the arguments in
185 * question. Return an indication of whether it succeeded or failed
189 get_ring_arguments(capture_options *capture_opts, const char *appname, const char *arg)
191 gchar *p = NULL, *colonp;
193 colonp = strchr(arg, ':');
195 if (colonp != NULL) {
200 capture_opts->ring_num_files =
201 get_natural_int(appname, arg, "number of ring buffer files");
207 * Skip over any white space (there probably won't be any, but
208 * as we allow it in the preferences file, we might as well
211 while (isspace((guchar)*p))
215 * Put the colon back, so if our caller uses, in an
216 * error message, the string they passed us, the message
223 capture_opts->has_file_duration = TRUE;
224 capture_opts->file_duration = get_positive_int(appname, p,
225 "ring buffer duration");
227 *colonp = ':'; /* put the colon back */
233 capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt, const char *optarg, gboolean *start_capture)
240 case 'a': /* autostop criteria */
241 if (set_autostop_criterion(capture_opts, appname, optarg) == FALSE) {
242 fprintf(stderr, "%s: Invalid or unknown -a flag \"%s\"\n", appname, optarg);
246 case 'b': /* Ringbuffer option */
247 capture_opts->multi_files_on = TRUE;
248 capture_opts->has_ring_num_files = TRUE;
249 if (get_ring_arguments(capture_opts, appname, optarg) == FALSE) {
250 fprintf(stderr, "%s: Invalid or unknown -b arg \"%s\"\n", appname, optarg);
254 case 'c': /* Capture xxx packets */
255 capture_opts->has_autostop_packets = TRUE;
256 capture_opts->autostop_packets = get_positive_int(appname, optarg, "packet count");
258 case 'f': /* capture filter */
259 if (capture_opts->cfilter)
260 g_free(capture_opts->cfilter);
261 capture_opts->cfilter = g_strdup(optarg);
263 case 'H': /* Hide capture info dialog box */
264 capture_opts->show_info = FALSE;
266 case 'i': /* Use interface xxx */
267 capture_opts->iface = g_strdup(optarg);
269 case 'k': /* Start capture immediately */
270 *start_capture = TRUE;
272 /*case 'l':*/ /* Automatic scrolling in live capture mode */
273 case 'p': /* Don't capture in promiscuous mode */
274 capture_opts->promisc_mode = FALSE;
276 case 'Q': /* Quit after capture (just capture to file) */
277 capture_opts->quit_after_cap = TRUE;
278 *start_capture = TRUE; /*** -Q implies -k !! ***/
280 case 's': /* Set the snapshot (capture) length */
281 capture_opts->has_snaplen = TRUE;
282 capture_opts->snaplen = get_positive_int(appname, optarg, "snapshot length");
284 case 'S': /* "Sync" mode: used for following file ala tail -f */
285 capture_opts->sync_mode = TRUE;
287 case 'w': /* Write to capture file xxx */
288 capture_opts->save_file = g_strdup(optarg);
290 case 'W': /* Write to capture file FD xxx */
291 capture_opts->save_file_fd = atoi(optarg);
293 case 'y': /* Set the pcap data link type */
294 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
295 capture_opts->linktype = pcap_datalink_name_to_val(optarg);
296 if (capture_opts->linktype == -1) {
297 fprintf(stderr, "%s: The specified data link type \"%s\" isn't valid\n",
301 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
302 /* XXX - just treat it as a number */
303 capture_opts->linktype = get_natural_int(appname, optarg, "data link type");
304 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
307 /* Hidden option supporting Sync mode */
308 case 'Z': /* Write to pipe FD XXX */
309 /* associate stdout with pipe */
311 if (dup2(i, 1) < 0) {
312 fprintf(stderr, "%s: Unable to dup pipe handle\n", appname);
318 /* the caller is responsible to send us only the right opt's */
319 g_assert_not_reached();
323 #endif /* HAVE_LIBPCAP */