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->real_time_mode = TRUE;
64 capture_opts->show_info = TRUE;
65 capture_opts->quit_after_cap = FALSE;
66 capture_opts->restart = FALSE;
68 capture_opts->multi_files_on = FALSE;
69 capture_opts->has_file_duration = FALSE;
70 capture_opts->file_duration = 60; /* 1 min */
71 capture_opts->has_ring_num_files = FALSE;
72 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
74 capture_opts->has_autostop_files = FALSE;
75 capture_opts->autostop_files = 1;
76 capture_opts->has_autostop_packets = FALSE;
77 capture_opts->autostop_packets = 0;
78 capture_opts->has_autostop_filesize = FALSE;
79 capture_opts->autostop_filesize = 1024 * 1024; /* 1 MB */
80 capture_opts->has_autostop_duration = FALSE;
81 capture_opts->autostop_duration = 60; /* 1 min */
84 capture_opts->fork_child = -1; /* invalid process handle */
86 capture_opts->signal_pipe_fd = -1;
88 capture_opts->state = CAPTURE_STOPPED;
92 /* log content of capture_opts */
94 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
95 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
96 g_log(log_domain, log_level, "CFile : 0x%p", capture_opts->cf);
97 g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
98 g_log(log_domain, log_level, "Interface : %s", capture_opts->iface);
100 g_log(log_domain, log_level, "BufferSize : %u (MB)", capture_opts->buffer_size);
102 g_log(log_domain, log_level, "SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
103 g_log(log_domain, log_level, "Promisc : %u", capture_opts->promisc_mode);
104 g_log(log_domain, log_level, "LinkType : %d", capture_opts->linktype);
105 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
106 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
107 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
108 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
110 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
111 g_log(log_domain, log_level, "FileDuration (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
112 g_log(log_domain, log_level, "RingNumFiles (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
114 g_log(log_domain, log_level, "AutostopFiles (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
115 g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
116 g_log(log_domain, log_level, "AutostopFilesize(%u): %u", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
117 g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
119 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
121 g_log(log_domain, log_level, "SignalPipeFd : %d", capture_opts->signal_pipe_fd);
126 * Given a string of the form "<autostop criterion>:<value>", as might appear
127 * as an argument to a "-a" option, parse it and set the criterion in
128 * question. Return an indication of whether it succeeded or failed
132 set_autostop_criterion(capture_options *capture_opts, const char *appname, const char *autostoparg)
136 colonp = strchr(autostoparg, ':');
144 * Skip over any white space (there probably won't be any, but
145 * as we allow it in the preferences file, we might as well
148 while (isspace((guchar)*p))
152 * Put the colon back, so if our caller uses, in an
153 * error message, the string they passed us, the message
159 if (strcmp(autostoparg,"duration") == 0) {
160 capture_opts->has_autostop_duration = TRUE;
161 capture_opts->autostop_duration = get_positive_int(appname, p,"autostop duration");
162 } else if (strcmp(autostoparg,"filesize") == 0) {
163 capture_opts->has_autostop_filesize = TRUE;
164 capture_opts->autostop_filesize = get_positive_int(appname, p,"autostop filesize");
165 } else if (strcmp(autostoparg,"files") == 0) {
166 capture_opts->multi_files_on = TRUE;
167 capture_opts->has_autostop_files = TRUE;
168 capture_opts->autostop_files = get_positive_int(appname, p,"autostop files");
172 *colonp = ':'; /* put the colon back */
177 * Given a string of the form "<ring buffer file>:<duration>", as might appear
178 * as an argument to a "-b" option, parse it and set the arguments in
179 * question. Return an indication of whether it succeeded or failed
183 get_ring_arguments(capture_options *capture_opts, const char *appname, const char *arg)
185 gchar *p = NULL, *colonp;
187 colonp = strchr(arg, ':');
195 * Skip over any white space (there probably won't be any, but
196 * as we allow it in the preferences file, we might as well
199 while (isspace((guchar)*p))
203 * Put the colon back, so if our caller uses, in an
204 * error message, the string they passed us, the message
211 if (strcmp(arg,"files") == 0) {
212 capture_opts->has_ring_num_files = TRUE;
213 capture_opts->ring_num_files = get_natural_int(appname, p, "number of ring buffer files");
214 } else if (strcmp(arg,"filesize") == 0) {
215 capture_opts->has_autostop_filesize = TRUE;
216 capture_opts->autostop_filesize = get_positive_int(appname, p, "ring buffer filesize");
217 } else if (strcmp(arg,"duration") == 0) {
218 capture_opts->has_file_duration = TRUE;
219 capture_opts->file_duration = get_positive_int(appname, p, "ring buffer duration");
222 *colonp = ':'; /* put the colon back */
229 * Given a string of the form "<pipe name>:<file descriptor>", as might appear
230 * as an argument to a "-Z" option, parse it and set the arguments in
231 * question. Return an indication of whether it succeeded or failed
235 get_pipe_arguments(capture_options *capture_opts, const char *appname, const char *arg)
237 gchar *p = NULL, *colonp;
241 colonp = strchr(arg, ':');
249 * Skip over any white space (there probably won't be any, but
250 * as we allow it in the preferences file, we might as well
253 while (isspace((guchar)*p))
257 * Put the colon back, so if our caller uses, in an
258 * error message, the string they passed us, the message
265 if (strcmp(arg,"sync") == 0) {
266 /* associate stdout with sync pipe */
267 pipe_fd = get_natural_int(appname, p, "sync pipe file descriptor");
268 if (dup2(pipe_fd, 1) < 0) {
269 fprintf(stderr, "%s: Unable to dup sync pipe handle\n", appname);
272 } else if (strcmp(arg,"signal") == 0) {
273 /* associate stdin with signal pipe */
274 pipe_fd = get_natural_int(appname, p, "signal pipe file descriptor");
275 if (dup2(pipe_fd, 0) < 0) {
276 fprintf(stderr, "%s: Unable to dup signal pipe handle\n", appname);
281 *colonp = ':'; /* put the colon back */
288 capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt, const char *optarg, gboolean *start_capture)
291 case 'a': /* autostop criteria */
292 if (set_autostop_criterion(capture_opts, appname, optarg) == FALSE) {
293 fprintf(stderr, "%s: Invalid or unknown -a flag \"%s\"\n", appname, optarg);
297 case 'b': /* Ringbuffer option */
298 capture_opts->multi_files_on = TRUE;
299 if (get_ring_arguments(capture_opts, appname, optarg) == FALSE) {
300 fprintf(stderr, "%s: Invalid or unknown -b arg \"%s\"\n", appname, optarg);
305 case 'B': /* Buffer size */
306 capture_opts->buffer_size = get_positive_int(appname, optarg, "buffer size");
309 case 'c': /* Capture xxx packets */
310 capture_opts->has_autostop_packets = TRUE;
311 capture_opts->autostop_packets = get_positive_int(appname, optarg, "packet count");
313 case 'f': /* capture filter */
314 if (capture_opts->cfilter)
315 g_free(capture_opts->cfilter);
316 capture_opts->cfilter = g_strdup(optarg);
318 case 'H': /* Hide capture info dialog box */
319 capture_opts->show_info = FALSE;
321 case 'i': /* Use interface xxx */
322 capture_opts->iface = g_strdup(optarg);
324 case 'k': /* Start capture immediately */
325 *start_capture = TRUE;
327 /*case 'l':*/ /* Automatic scrolling in live capture mode */
328 case 'p': /* Don't capture in promiscuous mode */
329 capture_opts->promisc_mode = FALSE;
331 case 'Q': /* Quit after capture (just capture to file) */
332 capture_opts->quit_after_cap = TRUE;
333 *start_capture = TRUE; /*** -Q implies -k !! ***/
335 case 's': /* Set the snapshot (capture) length */
336 capture_opts->has_snaplen = TRUE;
337 capture_opts->snaplen = get_positive_int(appname, optarg, "snapshot length");
339 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
340 capture_opts->real_time_mode = TRUE;
342 case 'w': /* Write to capture file xxx */
343 capture_opts->save_file = g_strdup(optarg);
345 case 'y': /* Set the pcap data link type */
346 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
347 capture_opts->linktype = pcap_datalink_name_to_val(optarg);
348 if (capture_opts->linktype == -1) {
349 fprintf(stderr, "%s: The specified data link type \"%s\" isn't valid\n",
353 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
354 /* XXX - just treat it as a number */
355 capture_opts->linktype = get_natural_int(appname, optarg, "data link type");
356 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
359 /* Hidden option supporting Sync mode */
360 case 'Z': /* Write to pipe FD XXX */
361 if (get_pipe_arguments(capture_opts, appname, optarg) == FALSE) {
362 fprintf(stderr, "%s: Invalid or unknown -Z flag \"%s\"\n", appname, optarg);
368 /* the caller is responsible to send us only the right opt's */
369 g_assert_not_reached();
373 #endif /* HAVE_LIBPCAP */