2 * Routines for capture options setting
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
40 #include <epan/packet.h>
41 #include <epan/prefs.h>
42 #include "capture_ui_utils.h"
44 #include "capture_opts.h"
45 #include "ringbuffer.h"
46 #include "clopts_common.h"
47 #include "cmdarg_err.h"
49 #include "capture_ifinfo.h"
50 #include "capture-pcap-util.h"
51 #include <wsutil/file_util.h>
53 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
57 capture_opts_init(capture_options *capture_opts)
59 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
60 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
61 capture_opts->num_selected = 0;
62 capture_opts->default_options.name = NULL;
63 capture_opts->default_options.descr = NULL;
64 capture_opts->default_options.cfilter = NULL;
65 capture_opts->default_options.has_snaplen = FALSE;
66 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
67 capture_opts->default_options.linktype = -1;
68 capture_opts->default_options.promisc_mode = TRUE;
69 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
70 capture_opts->default_options.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
72 capture_opts->default_options.monitor_mode = FALSE;
73 #ifdef HAVE_PCAP_REMOTE
74 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
75 capture_opts->default_options.remote_host = NULL;
76 capture_opts->default_options.remote_port = NULL;
77 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
78 capture_opts->default_options.auth_username = NULL;
79 capture_opts->default_options.auth_password = NULL;
80 capture_opts->default_options.datatx_udp = FALSE;
81 capture_opts->default_options.nocap_rpcap = TRUE;
82 capture_opts->default_options.nocap_local = FALSE;
84 #ifdef HAVE_PCAP_SETSAMPLING
85 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
86 capture_opts->default_options.sampling_param = 0;
88 capture_opts->saving_to_file = FALSE;
89 capture_opts->save_file = NULL;
90 capture_opts->group_read_access = FALSE;
91 #ifdef PCAP_NG_DEFAULT
92 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
94 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
96 capture_opts->real_time_mode = TRUE;
97 capture_opts->show_info = TRUE;
98 capture_opts->quit_after_cap = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
99 capture_opts->restart = FALSE;
101 capture_opts->multi_files_on = FALSE;
102 capture_opts->has_file_duration = FALSE;
103 capture_opts->file_duration = 60; /* 1 min */
104 capture_opts->has_ring_num_files = FALSE;
105 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
107 capture_opts->has_autostop_files = FALSE;
108 capture_opts->autostop_files = 1;
109 capture_opts->has_autostop_packets = FALSE;
110 capture_opts->autostop_packets = 0;
111 capture_opts->has_autostop_filesize = FALSE;
112 capture_opts->autostop_filesize = 1024; /* 1 MB */
113 capture_opts->has_autostop_duration = FALSE;
114 capture_opts->autostop_duration = 60; /* 1 min */
116 capture_opts->output_to_pipe = FALSE;
117 capture_opts->capture_child = FALSE;
121 /* log content of capture_opts */
123 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
126 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
128 for (i = 0; i < capture_opts->ifaces->len; i++) {
129 interface_options interface_opts;
131 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
132 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name ? interface_opts.name : "(unspecified)");
133 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts.descr ? interface_opts.descr : "(unspecified)");
134 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts.console_display_name ? interface_opts.console_display_name : "(unspecified)");
135 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter ? interface_opts.cfilter : "(unspecified)");
136 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
137 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
138 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
139 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
140 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
142 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
143 #ifdef HAVE_PCAP_REMOTE
144 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
145 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
146 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
148 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
149 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host ? interface_opts.remote_host : "(unspecified)");
150 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port ? interface_opts.remote_port : "(unspecified)");
152 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
153 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
154 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
156 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
157 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username ? interface_opts.auth_username : "(unspecified)");
158 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
160 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
161 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
162 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
164 #ifdef HAVE_PCAP_SETSAMPLING
165 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
166 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
169 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
170 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
171 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
172 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
173 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
174 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
175 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
176 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
178 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
179 #ifdef HAVE_PCAP_REMOTE
180 g_log(log_domain, log_level, "Capture source[df] : %s",
181 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
182 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
184 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
185 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
186 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
188 g_log(log_domain, log_level, "Authentication[df] : %s",
189 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
190 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
192 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
193 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
194 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
196 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
197 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
198 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
200 #ifdef HAVE_PCAP_SETSAMPLING
201 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
202 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
204 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
205 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
206 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
207 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
208 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
209 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
210 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
212 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
213 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
214 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
216 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
217 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
218 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
219 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
223 * Given a string of the form "<autostop criterion>:<value>", as might appear
224 * as an argument to a "-a" option, parse it and set the criterion in
225 * question. Return an indication of whether it succeeded or failed
229 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
233 colonp = strchr(autostoparg, ':');
241 * Skip over any white space (there probably won't be any, but
242 * as we allow it in the preferences file, we might as well
245 while (isspace((guchar)*p))
249 * Put the colon back, so if our caller uses, in an
250 * error message, the string they passed us, the message
256 if (strcmp(autostoparg,"duration") == 0) {
257 capture_opts->has_autostop_duration = TRUE;
258 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
259 } else if (strcmp(autostoparg,"filesize") == 0) {
260 capture_opts->has_autostop_filesize = TRUE;
261 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
262 } else if (strcmp(autostoparg,"files") == 0) {
263 capture_opts->multi_files_on = TRUE;
264 capture_opts->has_autostop_files = TRUE;
265 capture_opts->autostop_files = get_positive_int(p,"autostop files");
269 *colonp = ':'; /* put the colon back */
274 * Given a string of the form "<ring buffer file>:<duration>", as might appear
275 * as an argument to a "-b" option, parse it and set the arguments in
276 * question. Return an indication of whether it succeeded or failed
280 get_ring_arguments(capture_options *capture_opts, const char *arg)
282 gchar *p = NULL, *colonp;
284 colonp = strchr(arg, ':');
292 * Skip over any white space (there probably won't be any, but
293 * as we allow it in the preferences file, we might as well
296 while (isspace((guchar)*p))
300 * Put the colon back, so if our caller uses, in an
301 * error message, the string they passed us, the message
308 if (strcmp(arg,"files") == 0) {
309 capture_opts->has_ring_num_files = TRUE;
310 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
311 } else if (strcmp(arg,"filesize") == 0) {
312 capture_opts->has_autostop_filesize = TRUE;
313 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
314 } else if (strcmp(arg,"duration") == 0) {
315 capture_opts->has_file_duration = TRUE;
316 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
319 *colonp = ':'; /* put the colon back */
323 #ifdef HAVE_PCAP_SETSAMPLING
325 * Given a string of the form "<sampling type>:<value>", as might appear
326 * as an argument to a "-m" option, parse it and set the arguments in
327 * question. Return an indication of whether it succeeded or failed
331 get_sampling_arguments(capture_options *capture_opts, const char *arg)
333 gchar *p = NULL, *colonp;
335 colonp = strchr(arg, ':');
342 while (isspace((guchar)*p))
349 if (strcmp(arg, "count") == 0) {
350 if (capture_opts->ifaces->len > 0) {
351 interface_options interface_opts;
353 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
354 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
355 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
356 interface_opts.sampling_param = get_positive_int(p, "sampling count");
357 g_array_append_val(capture_opts->ifaces, interface_opts);
359 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
360 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
362 } else if (strcmp(arg, "timer") == 0) {
363 if (capture_opts->ifaces->len > 0) {
364 interface_options interface_opts;
366 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
367 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
368 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
369 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
370 g_array_append_val(capture_opts->ifaces, interface_opts);
372 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
373 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
381 #ifdef HAVE_PCAP_REMOTE
383 * Given a string of the form "<username>:<password>", as might appear
384 * as an argument to a "-A" option, parse it and set the arguments in
385 * question. Return an indication of whether it succeeded or failed
389 get_auth_arguments(capture_options *capture_opts, const char *arg)
391 gchar *p = NULL, *colonp;
393 colonp = strchr(arg, ':');
400 while (isspace((guchar)*p))
403 if (capture_opts->ifaces->len > 0) {
404 interface_options interface_opts;
406 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
407 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
408 interface_opts.auth_type = CAPTURE_AUTH_PWD;
409 interface_opts.auth_username = g_strdup(arg);
410 interface_opts.auth_password = g_strdup(p);
411 g_array_append_val(capture_opts->ifaces, interface_opts);
413 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
414 capture_opts->default_options.auth_username = g_strdup(arg);
415 capture_opts->default_options.auth_password = g_strdup(p);
423 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
431 interface_options interface_opts;
434 * If the argument is a number, treat it as an index into the list
435 * of adapters, as printed by "tshark -D".
437 * This should be OK on UNIX systems, as interfaces shouldn't have
438 * names that begin with digits. It can be useful on Windows, where
439 * more than one interface can have the same name.
441 adapter_index = strtol(optarg_str_p, &p, 10);
442 if (p != NULL && *p == '\0') {
443 if (adapter_index < 0) {
444 cmdarg_err("The specified adapter index is a negative number");
447 if (adapter_index > INT_MAX) {
448 cmdarg_err("The specified adapter index is too large (greater than %d)",
452 if (adapter_index == 0) {
453 cmdarg_err("There is no interface with that adapter index");
456 if_list = capture_interface_list(&err, &err_str, NULL);
457 if (if_list == NULL) {
460 case CANT_GET_INTERFACE_LIST:
462 cmdarg_err("%s", err_str);
466 case NO_INTERFACES_FOUND:
467 cmdarg_err("There are no interfaces on which a capture can be done");
472 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
473 if (if_info == NULL) {
474 cmdarg_err("There is no interface with that adapter index");
477 interface_opts.name = g_strdup(if_info->name);
478 if (if_info->friendly_name != NULL) {
480 * We have a friendly name for the interface, so display that
481 * instead of the interface name/guid.
483 * XXX - on UN*X, the interface name is not quite so ugly,
484 * and might be more familiar to users; display them both?
486 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
488 /* fallback to the interface name */
489 interface_opts.console_display_name = g_strdup(if_info->name);
491 free_interface_list(if_list);
492 } else if (capture_opts->capture_child) {
493 /* In Wireshark capture child mode, thus proper device name is supplied. */
494 /* No need for trying to match it for friendly names. */
495 interface_opts.name = g_strdup(optarg_str_p);
496 interface_opts.console_display_name = g_strdup(optarg_str_p);
499 * Retrieve the interface list so that we can search for the
500 * specified option amongst both the interface names and the
501 * friendly names and so that we find the friendly name even
502 * if an interface name was specified.
504 * If we can't get the list, just use the specified option as
505 * the interface name, so that the user can try specifying an
506 * interface explicitly for testing purposes.
508 if_list = capture_interface_list(&err, NULL, NULL);
509 if (if_list != NULL) {
510 /* try and do an exact match (case insensitive) */
515 for (if_entry = g_list_first(if_list); if_entry != NULL;
516 if_entry = g_list_next(if_entry))
518 if_info = (if_info_t *)if_entry->data;
519 /* exact name check */
520 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
521 /* exact match on the interface name, use that for displaying etc */
522 interface_opts.name = g_strdup(if_info->name);
524 if (if_info->friendly_name != NULL) {
526 * If we have a friendly name, use that for the
527 * console display name, as it is the basis for
528 * the auto generated temp filename.
530 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
532 interface_opts.console_display_name = g_strdup(if_info->name);
538 /* exact friendly name check */
539 if (if_info->friendly_name != NULL &&
540 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
541 /* exact match - use the friendly name for display */
542 interface_opts.name = g_strdup(if_info->name);
543 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
549 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
551 size_t prefix_length;
553 prefix_length = strlen(optarg_str_p);
554 for (if_entry = g_list_first(if_list); if_entry != NULL;
555 if_entry = g_list_next(if_entry))
557 if_info = (if_info_t *)if_entry->data;
559 if (if_info->friendly_name != NULL &&
560 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
561 /* prefix match - use the friendly name for display */
562 interface_opts.name = g_strdup(if_info->name);
563 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
571 * We didn't find the interface in the list; just use
572 * the specified name, so that, for example, if an
573 * interface doesn't show up in the list for some
574 * reason, the user can try specifying it explicitly
575 * for testing purposes.
577 interface_opts.name = g_strdup(optarg_str_p);
578 interface_opts.console_display_name = g_strdup(optarg_str_p);
580 free_interface_list(if_list);
582 interface_opts.name = g_strdup(optarg_str_p);
583 interface_opts.console_display_name = g_strdup(optarg_str_p);
587 /* We don't set iface_descr here because doing so requires
588 * capture_ui_utils.c which requires epan/prefs.c which is
589 * probably a bit too much dependency for here...
591 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
592 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
593 interface_opts.snaplen = capture_opts->default_options.snaplen;
594 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
595 interface_opts.linktype = capture_opts->default_options.linktype;
596 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
597 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
598 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
600 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
601 #ifdef HAVE_PCAP_REMOTE
602 interface_opts.src_type = capture_opts->default_options.src_type;
603 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
604 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
605 interface_opts.auth_type = capture_opts->default_options.auth_type;
606 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
607 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
608 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
609 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
610 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
612 #ifdef HAVE_PCAP_SETSAMPLING
613 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
614 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
617 g_array_append_val(capture_opts->ifaces, interface_opts);
624 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
629 case 'a': /* autostop criteria */
630 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
631 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
635 #ifdef HAVE_PCAP_REMOTE
637 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
638 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
643 case 'b': /* Ringbuffer option */
644 capture_opts->multi_files_on = TRUE;
645 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
646 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
650 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
651 case 'B': /* Buffer size */
652 if (capture_opts->ifaces->len > 0) {
653 interface_options interface_opts;
655 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
656 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
657 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
658 g_array_append_val(capture_opts->ifaces, interface_opts);
660 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
664 case 'c': /* Capture n packets */
665 capture_opts->has_autostop_packets = TRUE;
666 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
668 case 'f': /* capture filter */
669 if (capture_opts->ifaces->len > 0) {
670 interface_options interface_opts;
672 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
673 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
674 g_free(interface_opts.cfilter);
675 interface_opts.cfilter = g_strdup(optarg_str_p);
676 g_array_append_val(capture_opts->ifaces, interface_opts);
678 g_free(capture_opts->default_options.cfilter);
679 capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
682 case 'g': /* enable group read access on the capture file(s) */
683 capture_opts->group_read_access = TRUE;
685 case 'H': /* Hide capture info dialog box */
686 capture_opts->show_info = FALSE;
688 case 'i': /* Use interface x */
689 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
694 #ifdef HAVE_PCAP_CREATE
695 case 'I': /* Capture in monitor mode */
696 if (capture_opts->ifaces->len > 0) {
697 interface_options interface_opts;
699 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
700 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
701 interface_opts.monitor_mode = TRUE;
702 g_array_append_val(capture_opts->ifaces, interface_opts);
704 capture_opts->default_options.monitor_mode = TRUE;
708 case 'k': /* Start capture immediately */
709 *start_capture = TRUE;
711 /*case 'l':*/ /* Automatic scrolling in live capture mode */
712 #ifdef HAVE_PCAP_SETSAMPLING
714 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
715 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
720 case 'n': /* Use pcapng format */
721 capture_opts->use_pcapng = TRUE;
723 case 'p': /* Don't capture in promiscuous mode */
724 if (capture_opts->ifaces->len > 0) {
725 interface_options interface_opts;
727 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
728 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
729 interface_opts.promisc_mode = FALSE;
730 g_array_append_val(capture_opts->ifaces, interface_opts);
732 capture_opts->default_options.promisc_mode = FALSE;
735 case 'P': /* Use pcap format */
736 capture_opts->use_pcapng = FALSE;
738 #ifdef HAVE_PCAP_REMOTE
740 if (capture_opts->ifaces->len > 0) {
741 interface_options interface_opts;
743 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
744 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
745 interface_opts.nocap_rpcap = FALSE;
746 g_array_append_val(capture_opts->ifaces, interface_opts);
748 capture_opts->default_options.nocap_rpcap = FALSE;
752 case 's': /* Set the snapshot (capture) length */
753 snaplen = get_natural_int(optarg_str_p, "snapshot length");
755 * Make a snapshot length of 0 equivalent to the maximum packet
756 * length, mirroring what tcpdump does.
759 snaplen = WTAP_MAX_PACKET_SIZE;
760 if (capture_opts->ifaces->len > 0) {
761 interface_options interface_opts;
763 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
764 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
765 interface_opts.has_snaplen = TRUE;
766 interface_opts.snaplen = snaplen;
767 g_array_append_val(capture_opts->ifaces, interface_opts);
769 capture_opts->default_options.snaplen = snaplen;
770 capture_opts->default_options.has_snaplen = TRUE;
773 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
774 capture_opts->real_time_mode = TRUE;
776 #ifdef HAVE_PCAP_REMOTE
778 if (capture_opts->ifaces->len > 0) {
779 interface_options interface_opts;
781 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
782 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
783 interface_opts.datatx_udp = TRUE;
784 g_array_append_val(capture_opts->ifaces, interface_opts);
786 capture_opts->default_options.datatx_udp = TRUE;
790 case 'w': /* Write to capture file x */
791 capture_opts->saving_to_file = TRUE;
792 g_free(capture_opts->save_file);
793 capture_opts->save_file = g_strdup(optarg_str_p);
794 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
796 case 'y': /* Set the pcap data link type */
797 if (capture_opts->ifaces->len > 0) {
798 interface_options interface_opts;
800 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
801 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
802 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
803 if (interface_opts.linktype == -1) {
804 cmdarg_err("The specified data link type \"%s\" isn't valid",
808 g_array_append_val(capture_opts->ifaces, interface_opts);
810 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
811 if (capture_opts->default_options.linktype == -1) {
812 cmdarg_err("The specified data link type \"%s\" isn't valid",
819 /* the caller is responsible to send us only the right opt's */
820 g_assert_not_reached();
827 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
828 gboolean monitor_mode)
831 data_link_info_t *data_link_info;
833 if (caps->can_set_rfmon)
834 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
835 name, monitor_mode ? "" : "not ");
837 printf("Data link types of interface %s (use option -y to set):\n", name);
838 for (lt_entry = caps->data_link_types; lt_entry != NULL;
839 lt_entry = g_list_next(lt_entry)) {
840 data_link_info = (data_link_info_t *)lt_entry->data;
841 printf(" %s", data_link_info->name);
842 if (data_link_info->description != NULL)
843 printf(" (%s)", data_link_info->description);
845 printf(" (not supported)");
850 /* Print an ASCII-formatted list of interfaces. */
852 capture_opts_print_interfaces(GList *if_list)
858 i = 1; /* Interface id number */
859 for (if_entry = g_list_first(if_list); if_entry != NULL;
860 if_entry = g_list_next(if_entry)) {
861 if_info = (if_info_t *)if_entry->data;
862 printf("%d. %s", i++, if_info->name);
864 /* Print the interface friendly name, if it exists;
865 if not fall back to vendor description, if it exists. */
866 if (if_info->friendly_name != NULL){
867 printf(" (%s)", if_info->friendly_name);
869 if (if_info->vendor_description != NULL)
870 printf(" (%s)", if_info->vendor_description);
878 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
881 interface_options interface_opts;
883 if (capture_opts->ifaces->len > 0) {
884 for (i = 0; i < capture_opts->ifaces->len; i++) {
885 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
886 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
887 if (interface_opts.snaplen < 1)
888 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
889 else if (interface_opts.snaplen < snaplen_min)
890 interface_opts.snaplen = snaplen_min;
891 g_array_append_val(capture_opts->ifaces, interface_opts);
894 if (capture_opts->default_options.snaplen < 1)
895 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
896 else if (capture_opts->default_options.snaplen < snaplen_min)
897 capture_opts->default_options.snaplen = snaplen_min;
903 capture_opts_trim_ring_num_files(capture_options *capture_opts)
905 /* Check the value range of the ring_num_files parameter */
906 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
907 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
908 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
909 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
910 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
912 #if RINGBUFFER_MIN_NUM_FILES > 0
913 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
914 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
915 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
920 * If no interface was specified explicitly, pick a default.
923 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
924 const char *capture_device)
928 /* Did the user specify an interface to use? */
929 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
930 /* yes they did, return immediately - nothing further to do here */
934 /* No - is a default specified in the preferences file? */
935 if (capture_device != NULL) {
937 status = capture_opts_add_iface_opt(capture_opts, capture_device);
940 /* No default in preferences file, just pick the first interface from the list of interfaces. */
941 return capture_opts_add_iface_opt(capture_opts, "1");
945 #define S_IFIFO _S_IFIFO
948 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
951 /* copied from filesystem.c */
953 capture_opts_test_for_fifo(const char *path)
957 if (ws_stat64(path, &statb) < 0)
960 if (S_ISFIFO(statb.st_mode))
967 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
973 if (save_file != NULL) {
974 /* We're writing to a capture file. */
975 if (strcmp(save_file, "-") == 0) {
976 /* Writing to stdout. */
977 /* XXX - should we check whether it's a pipe? It's arguably
978 silly to do "-w - >output_file" rather than "-w output_file",
979 but by not checking we might be violating the Principle Of
980 Least Astonishment. */
983 /* not writing to stdout, test for a FIFO (aka named pipe) */
984 err = capture_opts_test_for_fifo(save_file);
987 case ENOENT: /* it doesn't exist, so we'll be creating it,
988 and it won't be a FIFO */
989 case 0: /* found it, but it's not a FIFO */
992 case ESPIPE: /* it is a FIFO */
996 default: /* couldn't stat it */
997 break; /* ignore: later attempt to open */
998 /* will generate a nice msg */
1007 * Add all non-hidden selected interfaces in the "all interfaces" list
1008 * to the list of interfaces for the capture.
1011 collect_ifaces(capture_options *capture_opts)
1015 interface_options interface_opts;
1017 /* Empty out the existing list of interfaces. */
1018 for (i = capture_opts->ifaces->len; i != 0; i--) {
1019 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i - 1);
1020 g_free(interface_opts.name);
1021 g_free(interface_opts.descr);
1022 if (interface_opts.console_display_name != NULL)
1023 g_free(interface_opts.console_display_name);
1024 g_free(interface_opts.cfilter);
1025 #ifdef HAVE_PCAP_REMOTE
1026 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1027 g_free(interface_opts.remote_host);
1028 g_free(interface_opts.remote_port);
1029 g_free(interface_opts.auth_username);
1030 g_free(interface_opts.auth_password);
1033 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i - 1);
1036 /* Now fill the list up again. */
1037 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1038 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1039 if (!device.hidden && device.selected) {
1040 interface_opts.name = g_strdup(device.name);
1041 interface_opts.descr = g_strdup(device.display_name);
1042 interface_opts.console_display_name = g_strdup(device.name);
1043 interface_opts.linktype = device.active_dlt;
1044 interface_opts.cfilter = g_strdup(device.cfilter);
1045 interface_opts.snaplen = device.snaplen;
1046 interface_opts.has_snaplen = device.has_snaplen;
1047 interface_opts.promisc_mode = device.pmode;
1048 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
1049 interface_opts.buffer_size = device.buffer;
1051 #ifdef HAVE_PCAP_CREATE
1052 interface_opts.monitor_mode = device.monitor_mode_enabled;
1054 #ifdef HAVE_PCAP_REMOTE
1055 interface_opts.src_type = CAPTURE_IFREMOTE;
1056 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1057 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1058 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1059 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1060 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1061 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1062 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1063 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1065 #ifdef HAVE_PCAP_SETSAMPLING
1066 interface_opts.sampling_method = device.remote_opts.sampling_method;
1067 interface_opts.sampling_param = device.remote_opts.sampling_param;
1069 g_array_append_val(capture_opts->ifaces, interface_opts);
1077 #endif /* HAVE_LIBPCAP */