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.
38 #include <epan/packet.h>
41 #include "capture_opts.h"
42 #include "ringbuffer.h"
43 #include "clopts_common.h"
44 #include "cmdarg_err.h"
47 capture_opts_init(capture_options *capture_opts, void *cfile)
49 capture_opts->cf = cfile;
50 capture_opts->cfilter = g_strdup(""); /* No capture filter string specified */
51 capture_opts->iface = NULL; /* Default is "pick the first interface" */
53 capture_opts->buffer_size = 1; /* 1 MB */
55 capture_opts->has_snaplen = FALSE;
56 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
57 infinite, in effect */
58 capture_opts->promisc_mode = TRUE; /* promiscuous mode is the default */
59 capture_opts->linktype = -1; /* the default linktype */
60 capture_opts->save_file = NULL;
61 capture_opts->real_time_mode = TRUE;
62 capture_opts->show_info = TRUE;
63 capture_opts->quit_after_cap = FALSE;
64 capture_opts->restart = FALSE;
66 capture_opts->multi_files_on = FALSE;
67 capture_opts->has_file_duration = FALSE;
68 capture_opts->file_duration = 60; /* 1 min */
69 capture_opts->has_ring_num_files = FALSE;
70 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
72 capture_opts->has_autostop_files = FALSE;
73 capture_opts->autostop_files = 1;
74 capture_opts->has_autostop_packets = FALSE;
75 capture_opts->autostop_packets = 0;
76 capture_opts->has_autostop_filesize = FALSE;
77 capture_opts->autostop_filesize = 1024; /* 1 MB */
78 capture_opts->has_autostop_duration = FALSE;
79 capture_opts->autostop_duration = 60; /* 1 min */
82 capture_opts->fork_child = -1; /* invalid process handle */
84 capture_opts->signal_pipe_fd = -1;
86 capture_opts->state = CAPTURE_STOPPED;
90 /* log content of capture_opts */
92 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
93 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
94 g_log(log_domain, log_level, "CFile : 0x%p", capture_opts->cf);
95 g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
96 g_log(log_domain, log_level, "Interface : %s", capture_opts->iface);
98 g_log(log_domain, log_level, "BufferSize : %u (MB)", capture_opts->buffer_size);
100 g_log(log_domain, log_level, "SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
101 g_log(log_domain, log_level, "Promisc : %u", capture_opts->promisc_mode);
102 g_log(log_domain, log_level, "LinkType : %d", capture_opts->linktype);
103 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
104 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
105 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
106 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
108 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
109 g_log(log_domain, log_level, "FileDuration (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
110 g_log(log_domain, log_level, "RingNumFiles (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
112 g_log(log_domain, log_level, "AutostopFiles (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
113 g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
114 g_log(log_domain, log_level, "AutostopFilesize(%u): %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
115 g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
117 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
119 g_log(log_domain, log_level, "SignalPipeFd : %d", capture_opts->signal_pipe_fd);
124 * Given a string of the form "<autostop criterion>:<value>", as might appear
125 * as an argument to a "-a" option, parse it and set the criterion in
126 * question. Return an indication of whether it succeeded or failed
130 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
134 colonp = strchr(autostoparg, ':');
142 * Skip over any white space (there probably won't be any, but
143 * as we allow it in the preferences file, we might as well
146 while (isspace((guchar)*p))
150 * Put the colon back, so if our caller uses, in an
151 * error message, the string they passed us, the message
157 if (strcmp(autostoparg,"duration") == 0) {
158 capture_opts->has_autostop_duration = TRUE;
159 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
160 } else if (strcmp(autostoparg,"filesize") == 0) {
161 capture_opts->has_autostop_filesize = TRUE;
162 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
163 } else if (strcmp(autostoparg,"files") == 0) {
164 capture_opts->multi_files_on = TRUE;
165 capture_opts->has_autostop_files = TRUE;
166 capture_opts->autostop_files = get_positive_int(p,"autostop files");
170 *colonp = ':'; /* put the colon back */
175 * Given a string of the form "<ring buffer file>:<duration>", as might appear
176 * as an argument to a "-b" option, parse it and set the arguments in
177 * question. Return an indication of whether it succeeded or failed
181 get_ring_arguments(capture_options *capture_opts, const char *arg)
183 gchar *p = NULL, *colonp;
185 colonp = strchr(arg, ':');
193 * Skip over any white space (there probably won't be any, but
194 * as we allow it in the preferences file, we might as well
197 while (isspace((guchar)*p))
201 * Put the colon back, so if our caller uses, in an
202 * error message, the string they passed us, the message
209 if (strcmp(arg,"files") == 0) {
210 capture_opts->has_ring_num_files = TRUE;
211 capture_opts->ring_num_files = get_natural_int(p, "number of ring buffer files");
212 } else if (strcmp(arg,"filesize") == 0) {
213 capture_opts->has_autostop_filesize = TRUE;
214 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
215 } else if (strcmp(arg,"duration") == 0) {
216 capture_opts->has_file_duration = TRUE;
217 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
220 *colonp = ':'; /* put the colon back */
227 * Given a string of the form "<pipe name>:<file descriptor>", as might appear
228 * as an argument to a "-Z" option, parse it and set the arguments in
229 * question. Return an indication of whether it succeeded or failed
233 get_pipe_arguments(capture_options *capture_opts, const char *arg)
235 gchar *p = NULL, *colonp;
239 colonp = strchr(arg, ':');
247 * Skip over any white space (there probably won't be any, but
248 * as we allow it in the preferences file, we might as well
251 while (isspace((guchar)*p))
255 * Put the colon back, so if our caller uses, in an
256 * error message, the string they passed us, the message
263 if (strcmp(arg,"sync") == 0) {
264 /* associate stdout with sync pipe */
265 pipe_fd = get_natural_int(p, "sync pipe file descriptor");
266 if (dup2(pipe_fd, 1) < 0) {
267 cmdarg_err("Unable to dup sync pipe handle");
270 } else if (strcmp(arg,"signal") == 0) {
271 /* associate stdin with signal pipe */
272 pipe_fd = get_natural_int(p, "signal pipe file descriptor");
273 if (dup2(pipe_fd, 0) < 0) {
274 cmdarg_err("Unable to dup signal pipe handle");
279 *colonp = ':'; /* put the colon back */
286 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture)
289 case 'a': /* autostop criteria */
290 if (set_autostop_criterion(capture_opts, optarg) == FALSE) {
291 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg);
295 case 'b': /* Ringbuffer option */
296 capture_opts->multi_files_on = TRUE;
297 if (get_ring_arguments(capture_opts, optarg) == FALSE) {
298 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg);
303 case 'B': /* Buffer size */
304 capture_opts->buffer_size = get_positive_int(optarg, "buffer size");
307 case 'c': /* Capture xxx packets */
308 capture_opts->has_autostop_packets = TRUE;
309 capture_opts->autostop_packets = get_positive_int(optarg, "packet count");
311 case 'f': /* capture filter */
312 if (capture_opts->cfilter)
313 g_free(capture_opts->cfilter);
314 capture_opts->cfilter = g_strdup(optarg);
316 case 'H': /* Hide capture info dialog box */
317 capture_opts->show_info = FALSE;
319 case 'i': /* Use interface xxx */
320 capture_opts->iface = g_strdup(optarg);
322 case 'k': /* Start capture immediately */
323 *start_capture = TRUE;
325 /*case 'l':*/ /* Automatic scrolling in live capture mode */
326 case 'p': /* Don't capture in promiscuous mode */
327 capture_opts->promisc_mode = FALSE;
329 case 'Q': /* Quit after capture (just capture to file) */
330 capture_opts->quit_after_cap = TRUE;
331 *start_capture = TRUE; /*** -Q implies -k !! ***/
333 case 's': /* Set the snapshot (capture) length */
334 capture_opts->has_snaplen = TRUE;
335 capture_opts->snaplen = get_positive_int(optarg, "snapshot length");
337 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
338 capture_opts->real_time_mode = TRUE;
340 case 'w': /* Write to capture file xxx */
341 capture_opts->save_file = g_strdup(optarg);
343 case 'y': /* Set the pcap data link type */
344 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
345 capture_opts->linktype = pcap_datalink_name_to_val(optarg);
346 if (capture_opts->linktype == -1) {
347 cmdarg_err("The specified data link type \"%s\" isn't valid",
351 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
352 /* XXX - just treat it as a number */
353 capture_opts->linktype = get_natural_int(optarg, "data link type");
354 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
357 /* Hidden option supporting Sync mode */
358 case 'Z': /* Write to pipe FD XXX */
359 if (get_pipe_arguments(capture_opts, optarg) == FALSE) {
360 cmdarg_err("Invalid or unknown -Z flag \"%s\"", optarg);
366 /* the caller is responsible to send us only the right opt's */
367 g_assert_not_reached();
372 void capture_opts_trim(capture_options *capture_opts, int snaplen_min)
374 if (capture_opts->snaplen < 1)
375 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
376 else if (capture_opts->snaplen < snaplen_min)
377 capture_opts->snaplen = snaplen_min;
379 /* Check the value range of the ring_num_files parameter */
380 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES)
381 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
382 #if RINGBUFFER_MIN_NUM_FILES > 0
383 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
384 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
388 #endif /* HAVE_LIBPCAP */