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"
46 #include "clopts_common.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->save_file = NULL;
63 capture_opts->sync_mode = TRUE;
64 capture_opts->show_info = TRUE;
65 capture_opts->quit_after_cap = FALSE;
67 capture_opts->multi_files_on = FALSE;
68 capture_opts->has_file_duration = FALSE;
69 capture_opts->file_duration = 60; /* 1 min */
70 capture_opts->has_ring_num_files = FALSE;
71 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
73 capture_opts->has_autostop_files = FALSE;
74 capture_opts->autostop_files = 1;
75 capture_opts->has_autostop_packets = FALSE;
76 capture_opts->autostop_packets = 0;
77 capture_opts->has_autostop_filesize = FALSE;
78 capture_opts->autostop_filesize = 1024 * 1024; /* 1 MB */
79 capture_opts->has_autostop_duration = FALSE;
80 capture_opts->autostop_duration = 60; /* 1 min */
83 capture_opts->fork_child = -1; /* invalid process handle */
87 /* debug only: print content of capture_opts to console */
89 capture_opts_info(capture_options *capture_opts) {
90 g_warning("CAPTURE OPTIONS :");
91 g_warning("File : %p", capture_opts->cf);
92 g_warning("Filter : %s", capture_opts->cfilter);
93 g_warning("Interface : %s", capture_opts->iface);
95 g_warning("BufferSize : %u (MB)", capture_opts->buffer_size);
97 g_warning("SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
98 g_warning("Promisc : %u", capture_opts->promisc_mode);
99 g_warning("LinkType : %d", capture_opts->linktype);
100 g_warning("SaveFile : %s", capture_opts->save_file);
101 g_warning("SyncMode : %u", capture_opts->sync_mode);
102 g_warning("ShowInfo : %u", capture_opts->show_info);
103 g_warning("QuitAfterCap : %u", capture_opts->quit_after_cap);
105 g_warning("MultiFilesOn : %u", capture_opts->multi_files_on);
106 g_warning("FileDuration (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
107 g_warning("RingNumFiles (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
109 g_warning("AutostopFiles (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
110 g_warning("AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
111 g_warning("AutostopFilesize(%u): %u", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
112 g_warning("AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
114 g_warning("ForkChild : %d", capture_opts->fork_child);
118 * Given a string of the form "<autostop criterion>:<value>", as might appear
119 * as an argument to a "-a" option, parse it and set the criterion in
120 * question. Return an indication of whether it succeeded or failed
124 set_autostop_criterion(capture_options *capture_opts, const char *appname, const char *autostoparg)
128 colonp = strchr(autostoparg, ':');
136 * Skip over any white space (there probably won't be any, but
137 * as we allow it in the preferences file, we might as well
140 while (isspace((guchar)*p))
144 * Put the colon back, so if our caller uses, in an
145 * error message, the string they passed us, the message
151 if (strcmp(autostoparg,"duration") == 0) {
152 capture_opts->has_autostop_duration = TRUE;
153 capture_opts->autostop_duration = get_positive_int(appname, p,"autostop duration");
154 } else if (strcmp(autostoparg,"filesize") == 0) {
155 capture_opts->has_autostop_filesize = TRUE;
156 capture_opts->autostop_filesize = get_positive_int(appname, p,"autostop filesize");
157 } else if (strcmp(autostoparg,"files") == 0) {
158 capture_opts->multi_files_on = TRUE;
159 capture_opts->has_autostop_files = TRUE;
160 capture_opts->autostop_files = get_positive_int(appname, p,"autostop files");
164 *colonp = ':'; /* put the colon back */
169 * Given a string of the form "<ring buffer file>:<duration>", as might appear
170 * as an argument to a "-b" option, parse it and set the arguments in
171 * question. Return an indication of whether it succeeded or failed
175 get_ring_arguments(capture_options *capture_opts, const char *appname, const char *arg)
177 gchar *p = NULL, *colonp;
179 colonp = strchr(arg, ':');
187 * Skip over any white space (there probably won't be any, but
188 * as we allow it in the preferences file, we might as well
191 while (isspace((guchar)*p))
195 * Put the colon back, so if our caller uses, in an
196 * error message, the string they passed us, the message
203 if (strcmp(arg,"files") == 0) {
204 capture_opts->has_ring_num_files = TRUE;
205 capture_opts->ring_num_files = get_natural_int(appname, p, "number of ring buffer files");
206 } else if (strcmp(arg,"filesize") == 0) {
207 capture_opts->has_autostop_filesize = TRUE;
208 capture_opts->autostop_filesize = get_positive_int(appname, p, "ring buffer filesize");
209 } else if (strcmp(arg,"duration") == 0) {
210 capture_opts->has_file_duration = TRUE;
211 capture_opts->file_duration = get_positive_int(appname, p, "ring buffer duration");
214 *colonp = ':'; /* put the colon back */
220 capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt, const char *optarg, gboolean *start_capture)
227 case 'a': /* autostop criteria */
228 if (set_autostop_criterion(capture_opts, appname, optarg) == FALSE) {
229 fprintf(stderr, "%s: Invalid or unknown -a flag \"%s\"\n", appname, optarg);
233 case 'b': /* Ringbuffer option */
234 capture_opts->multi_files_on = TRUE;
235 if (get_ring_arguments(capture_opts, appname, optarg) == FALSE) {
236 fprintf(stderr, "%s: Invalid or unknown -b arg \"%s\"\n", appname, optarg);
240 case 'c': /* Capture xxx packets */
241 capture_opts->has_autostop_packets = TRUE;
242 capture_opts->autostop_packets = get_positive_int(appname, optarg, "packet count");
244 case 'f': /* capture filter */
245 if (capture_opts->cfilter)
246 g_free(capture_opts->cfilter);
247 capture_opts->cfilter = g_strdup(optarg);
249 case 'H': /* Hide capture info dialog box */
250 capture_opts->show_info = FALSE;
252 case 'i': /* Use interface xxx */
253 capture_opts->iface = g_strdup(optarg);
255 case 'k': /* Start capture immediately */
256 *start_capture = TRUE;
258 /*case 'l':*/ /* Automatic scrolling in live capture mode */
259 case 'p': /* Don't capture in promiscuous mode */
260 capture_opts->promisc_mode = FALSE;
262 case 'Q': /* Quit after capture (just capture to file) */
263 capture_opts->quit_after_cap = TRUE;
264 *start_capture = TRUE; /*** -Q implies -k !! ***/
266 case 's': /* Set the snapshot (capture) length */
267 capture_opts->has_snaplen = TRUE;
268 capture_opts->snaplen = get_positive_int(appname, optarg, "snapshot length");
270 case 'S': /* "Sync" mode: used for following file ala tail -f */
271 capture_opts->sync_mode = TRUE;
273 case 'w': /* Write to capture file xxx */
274 capture_opts->save_file = g_strdup(optarg);
276 case 'y': /* Set the pcap data link type */
277 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
278 capture_opts->linktype = pcap_datalink_name_to_val(optarg);
279 if (capture_opts->linktype == -1) {
280 fprintf(stderr, "%s: The specified data link type \"%s\" isn't valid\n",
284 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
285 /* XXX - just treat it as a number */
286 capture_opts->linktype = get_natural_int(appname, optarg, "data link type");
287 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
290 /* Hidden option supporting Sync mode */
291 case 'Z': /* Write to pipe FD XXX */
292 /* associate stdout with pipe */
294 if (dup2(i, 1) < 0) {
295 fprintf(stderr, "%s: Unable to dup pipe handle\n", appname);
301 /* the caller is responsible to send us only the right opt's */
302 g_assert_not_reached();
306 #endif /* HAVE_LIBPCAP */