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 "ui/simple_dialog.h"
43 #include "capture_ui_utils.h"
45 #include "capture_opts.h"
46 #include "ringbuffer.h"
47 #include "clopts_common.h"
48 #include "console_io.h"
49 #include "cmdarg_err.h"
51 #include "capture_ifinfo.h"
52 #include "capture-pcap-util.h"
53 #include <wsutil/file_util.h>
55 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
59 capture_opts_init(capture_options *capture_opts, void *cf)
61 capture_opts->cf = cf;
62 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
63 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
64 capture_opts->num_selected = 0;
65 capture_opts->default_options.name = NULL;
66 capture_opts->default_options.descr = NULL;
67 capture_opts->default_options.cfilter = NULL;
68 capture_opts->default_options.has_snaplen = FALSE;
69 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
70 capture_opts->default_options.linktype = -1;
71 capture_opts->default_options.promisc_mode = TRUE;
72 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
73 capture_opts->default_options.buffer_size = 1; /* 1 MB */
75 capture_opts->default_options.monitor_mode = FALSE;
76 #ifdef HAVE_PCAP_REMOTE
77 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
78 capture_opts->default_options.remote_host = NULL;
79 capture_opts->default_options.remote_port = NULL;
80 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
81 capture_opts->default_options.auth_username = NULL;
82 capture_opts->default_options.auth_password = NULL;
83 capture_opts->default_options.datatx_udp = FALSE;
84 capture_opts->default_options.nocap_rpcap = TRUE;
85 capture_opts->default_options.nocap_local = FALSE;
87 #ifdef HAVE_PCAP_SETSAMPLING
88 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
89 capture_opts->default_options.sampling_param = 0;
91 capture_opts->saving_to_file = FALSE;
92 capture_opts->save_file = NULL;
93 capture_opts->group_read_access = FALSE;
94 #ifdef PCAP_NG_DEFAULT
95 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
97 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
99 capture_opts->real_time_mode = TRUE;
100 capture_opts->show_info = TRUE;
101 capture_opts->quit_after_cap = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
102 capture_opts->restart = FALSE;
104 capture_opts->multi_files_on = FALSE;
105 capture_opts->has_file_duration = FALSE;
106 capture_opts->file_duration = 60; /* 1 min */
107 capture_opts->has_ring_num_files = FALSE;
108 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
110 capture_opts->has_autostop_files = FALSE;
111 capture_opts->autostop_files = 1;
112 capture_opts->has_autostop_packets = FALSE;
113 capture_opts->autostop_packets = 0;
114 capture_opts->has_autostop_filesize = FALSE;
115 capture_opts->autostop_filesize = 1024; /* 1 MB */
116 capture_opts->has_autostop_duration = FALSE;
117 capture_opts->autostop_duration = 60; /* 1 min */
120 capture_opts->fork_child = -1; /* invalid process handle */
122 capture_opts->signal_pipe_write_fd = -1;
124 capture_opts->state = CAPTURE_STOPPED;
125 capture_opts->output_to_pipe = FALSE;
127 capture_opts->owner = getuid();
128 capture_opts->group = getgid();
133 /* log content of capture_opts */
135 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
138 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
139 g_log(log_domain, log_level, "CFile : %p", capture_opts->cf);
141 for (i = 0; i < capture_opts->ifaces->len; i++) {
142 interface_options interface_opts;
144 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
145 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name ? interface_opts.name : "(unspecified)");
146 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts.descr ? interface_opts.descr : "(unspecified)");
147 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts.console_display_name ? interface_opts.console_display_name : "(unspecified)");
148 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter ? interface_opts.cfilter : "(unspecified)");
149 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
150 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
151 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
152 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
153 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
155 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
156 #ifdef HAVE_PCAP_REMOTE
157 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
158 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
159 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
161 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
162 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host ? interface_opts.remote_host : "(unspecified)");
163 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port ? interface_opts.remote_port : "(unspecified)");
165 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
166 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
167 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
169 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
170 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username ? interface_opts.auth_username : "(unspecified)");
171 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
173 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
174 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
175 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
177 #ifdef HAVE_PCAP_SETSAMPLING
178 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
179 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
182 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
183 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
184 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
185 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
186 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
187 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
188 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
189 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
191 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
192 #ifdef HAVE_PCAP_REMOTE
193 g_log(log_domain, log_level, "Capture source[df] : %s",
194 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
195 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
197 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
198 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
199 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
201 g_log(log_domain, log_level, "Authentication[df] : %s",
202 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
203 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
205 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
206 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
207 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
209 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
210 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
211 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
213 #ifdef HAVE_PCAP_SETSAMPLING
214 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
215 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
217 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
218 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
219 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
220 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
221 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
222 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
223 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
225 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
226 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
227 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
229 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
230 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
231 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
232 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
234 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
236 g_log(log_domain, log_level, "SignalPipeWrite : %d", capture_opts->signal_pipe_write_fd);
241 * Given a string of the form "<autostop criterion>:<value>", as might appear
242 * as an argument to a "-a" option, parse it and set the criterion in
243 * question. Return an indication of whether it succeeded or failed
247 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
251 colonp = strchr(autostoparg, ':');
259 * Skip over any white space (there probably won't be any, but
260 * as we allow it in the preferences file, we might as well
263 while (isspace((guchar)*p))
267 * Put the colon back, so if our caller uses, in an
268 * error message, the string they passed us, the message
274 if (strcmp(autostoparg,"duration") == 0) {
275 capture_opts->has_autostop_duration = TRUE;
276 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
277 } else if (strcmp(autostoparg,"filesize") == 0) {
278 capture_opts->has_autostop_filesize = TRUE;
279 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
280 } else if (strcmp(autostoparg,"files") == 0) {
281 capture_opts->multi_files_on = TRUE;
282 capture_opts->has_autostop_files = TRUE;
283 capture_opts->autostop_files = get_positive_int(p,"autostop files");
287 *colonp = ':'; /* put the colon back */
292 * Given a string of the form "<ring buffer file>:<duration>", as might appear
293 * as an argument to a "-b" option, parse it and set the arguments in
294 * question. Return an indication of whether it succeeded or failed
298 get_ring_arguments(capture_options *capture_opts, const char *arg)
300 gchar *p = NULL, *colonp;
302 colonp = strchr(arg, ':');
310 * Skip over any white space (there probably won't be any, but
311 * as we allow it in the preferences file, we might as well
314 while (isspace((guchar)*p))
318 * Put the colon back, so if our caller uses, in an
319 * error message, the string they passed us, the message
326 if (strcmp(arg,"files") == 0) {
327 capture_opts->has_ring_num_files = TRUE;
328 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
329 } else if (strcmp(arg,"filesize") == 0) {
330 capture_opts->has_autostop_filesize = TRUE;
331 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
332 } else if (strcmp(arg,"duration") == 0) {
333 capture_opts->has_file_duration = TRUE;
334 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
337 *colonp = ':'; /* put the colon back */
341 #ifdef HAVE_PCAP_SETSAMPLING
343 * Given a string of the form "<sampling type>:<value>", as might appear
344 * as an argument to a "-m" option, parse it and set the arguments in
345 * question. Return an indication of whether it succeeded or failed
349 get_sampling_arguments(capture_options *capture_opts, const char *arg)
351 gchar *p = NULL, *colonp;
353 colonp = strchr(arg, ':');
360 while (isspace((guchar)*p))
367 if (strcmp(arg, "count") == 0) {
368 if (capture_opts->ifaces->len > 0) {
369 interface_options interface_opts;
371 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
372 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
373 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
374 interface_opts.sampling_param = get_positive_int(p, "sampling count");
375 g_array_append_val(capture_opts->ifaces, interface_opts);
377 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
378 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
380 } else if (strcmp(arg, "timer") == 0) {
381 if (capture_opts->ifaces->len > 0) {
382 interface_options interface_opts;
384 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
385 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
386 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
387 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
388 g_array_append_val(capture_opts->ifaces, interface_opts);
390 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
391 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
399 #ifdef HAVE_PCAP_REMOTE
401 * Given a string of the form "<username>:<password>", as might appear
402 * as an argument to a "-A" option, parse it and set the arguments in
403 * question. Return an indication of whether it succeeded or failed
407 get_auth_arguments(capture_options *capture_opts, const char *arg)
409 gchar *p = NULL, *colonp;
411 colonp = strchr(arg, ':');
418 while (isspace((guchar)*p))
421 if (capture_opts->ifaces->len > 0) {
422 interface_options interface_opts;
424 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
425 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
426 interface_opts.auth_type = CAPTURE_AUTH_PWD;
427 interface_opts.auth_username = g_strdup(arg);
428 interface_opts.auth_password = g_strdup(p);
429 g_array_append_val(capture_opts->ifaces, interface_opts);
431 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
432 capture_opts->default_options.auth_username = g_strdup(arg);
433 capture_opts->default_options.auth_password = g_strdup(p);
441 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
449 interface_options interface_opts;
452 * If the argument is a number, treat it as an index into the list
453 * of adapters, as printed by "tshark -D".
455 * This should be OK on UNIX systems, as interfaces shouldn't have
456 * names that begin with digits. It can be useful on Windows, where
457 * more than one interface can have the same name.
459 adapter_index = strtol(optarg_str_p, &p, 10);
460 if (p != NULL && *p == '\0') {
461 if (adapter_index < 0) {
462 cmdarg_err("The specified adapter index is a negative number");
465 if (adapter_index > INT_MAX) {
466 cmdarg_err("The specified adapter index is too large (greater than %d)",
470 if (adapter_index == 0) {
471 cmdarg_err("There is no interface with that adapter index");
474 if_list = capture_interface_list(&err, &err_str);
475 if (if_list == NULL) {
478 case CANT_GET_INTERFACE_LIST:
480 cmdarg_err("%s", err_str);
484 case NO_INTERFACES_FOUND:
485 cmdarg_err("There are no interfaces on which a capture can be done");
490 if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
491 if (if_info == NULL) {
492 cmdarg_err("There is no interface with that adapter index");
495 interface_opts.name = g_strdup(if_info->name);
496 if (if_info->friendly_name != NULL) {
498 * We have a friendly name for the interface, so display that
499 * instead of the interface name/guid.
501 * XXX - on UN*X, the interface name is not quite so ugly,
502 * and might be more familiar to users; display them both?
504 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
506 /* fallback to the interface name */
507 interface_opts.console_display_name = g_strdup(if_info->name);
509 free_interface_list(if_list);
512 * Retrieve the interface list so that we can search for the
513 * specified option amongst both the interface names and the
514 * friendly names and so that we find the friendly name even
515 * if an interface name was specified.
517 * If we can't get the list, just use the specified option as
518 * the interface name, so that the user can try specifying an
519 * interface explicitly for testing purposes.
521 if_list = capture_interface_list(&err, &err_str);
522 if (if_list != NULL) {
523 /* try and do an exact match (case insensitive) */
528 for (if_entry = g_list_first(if_list); if_entry != NULL;
529 if_entry = g_list_next(if_entry))
531 if_info = (if_info_t *)if_entry->data;
532 /* exact name check */
533 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
534 /* exact match on the interface name, use that for displaying etc */
535 interface_opts.name = g_strdup(if_info->name);
537 if (if_info->friendly_name != NULL) {
539 * If we have a friendly name, use that for the
540 * console display name, as it is the basis for
541 * the auto generated temp filename.
543 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
545 interface_opts.console_display_name = g_strdup(if_info->name);
551 /* exact friendly name check */
552 if (if_info->friendly_name != NULL &&
553 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
554 /* exact match - use the friendly name for display */
555 interface_opts.name = g_strdup(if_info->name);
556 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
562 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
564 size_t prefix_length;
566 prefix_length = strlen(optarg_str_p);
567 for (if_entry = g_list_first(if_list); if_entry != NULL;
568 if_entry = g_list_next(if_entry))
570 if_info = (if_info_t *)if_entry->data;
572 if (if_info->friendly_name != NULL &&
573 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
574 /* prefix match - use the friendly name for display */
575 interface_opts.name = g_strdup(if_info->name);
576 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
584 * We didn't find the interface in the list; just use
585 * the specified name, so that, for example, if an
586 * interface doesn't show up in the list for some
587 * reason, the user can try specifying it explicitly
588 * for testing purposes.
590 interface_opts.name = g_strdup(optarg_str_p);
591 interface_opts.console_display_name = g_strdup(optarg_str_p);
593 free_interface_list(if_list);
595 interface_opts.name = g_strdup(optarg_str_p);
596 interface_opts.console_display_name = g_strdup(optarg_str_p);
600 /* We don't set iface_descr here because doing so requires
601 * capture_ui_utils.c which requires epan/prefs.c which is
602 * probably a bit too much dependency for here...
604 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
605 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
606 interface_opts.snaplen = capture_opts->default_options.snaplen;
607 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
608 interface_opts.linktype = capture_opts->default_options.linktype;
609 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
610 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
611 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
613 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
614 #ifdef HAVE_PCAP_REMOTE
615 interface_opts.src_type = capture_opts->default_options.src_type;
616 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
617 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
618 interface_opts.auth_type = capture_opts->default_options.auth_type;
619 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
620 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
621 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
622 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
623 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
625 #ifdef HAVE_PCAP_SETSAMPLING
626 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
627 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
630 g_array_append_val(capture_opts->ifaces, interface_opts);
637 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
642 case 'a': /* autostop criteria */
643 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
644 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
648 #ifdef HAVE_PCAP_REMOTE
650 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
651 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
656 case 'b': /* Ringbuffer option */
657 capture_opts->multi_files_on = TRUE;
658 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
659 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
663 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
664 case 'B': /* Buffer size */
665 if (capture_opts->ifaces->len > 0) {
666 interface_options interface_opts;
668 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
669 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
670 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
671 g_array_append_val(capture_opts->ifaces, interface_opts);
673 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
677 case 'c': /* Capture n packets */
678 capture_opts->has_autostop_packets = TRUE;
679 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
681 case 'f': /* capture filter */
682 if (capture_opts->ifaces->len > 0) {
683 interface_options interface_opts;
685 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
686 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
687 g_free(interface_opts.cfilter);
688 interface_opts.cfilter = g_strdup(optarg_str_p);
689 g_array_append_val(capture_opts->ifaces, interface_opts);
691 g_free(capture_opts->default_options.cfilter);
692 capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
695 case 'H': /* Hide capture info dialog box */
696 capture_opts->show_info = FALSE;
698 case 'i': /* Use interface x */
699 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
704 #ifdef HAVE_PCAP_CREATE
705 case 'I': /* Capture in monitor mode */
706 if (capture_opts->ifaces->len > 0) {
707 interface_options interface_opts;
709 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
710 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
711 interface_opts.monitor_mode = TRUE;
712 g_array_append_val(capture_opts->ifaces, interface_opts);
714 capture_opts->default_options.monitor_mode = TRUE;
718 case 'k': /* Start capture immediately */
719 *start_capture = TRUE;
721 /*case 'l':*/ /* Automatic scrolling in live capture mode */
722 #ifdef HAVE_PCAP_SETSAMPLING
724 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
725 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
730 case 'n': /* Use pcapng format */
731 capture_opts->use_pcapng = TRUE;
733 case 'p': /* Don't capture in promiscuous mode */
734 if (capture_opts->ifaces->len > 0) {
735 interface_options interface_opts;
737 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
738 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
739 interface_opts.promisc_mode = FALSE;
740 g_array_append_val(capture_opts->ifaces, interface_opts);
742 capture_opts->default_options.promisc_mode = FALSE;
745 case 'P': /* Use pcap format */
746 capture_opts->use_pcapng = FALSE;
748 #ifdef HAVE_PCAP_REMOTE
750 if (capture_opts->ifaces->len > 0) {
751 interface_options interface_opts;
753 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
754 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
755 interface_opts.nocap_rpcap = FALSE;
756 g_array_append_val(capture_opts->ifaces, interface_opts);
758 capture_opts->default_options.nocap_rpcap = FALSE;
762 case 's': /* Set the snapshot (capture) length */
763 snaplen = get_natural_int(optarg_str_p, "snapshot length");
765 * Make a snapshot length of 0 equivalent to the maximum packet
766 * length, mirroring what tcpdump does.
769 snaplen = WTAP_MAX_PACKET_SIZE;
770 if (capture_opts->ifaces->len > 0) {
771 interface_options interface_opts;
773 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
774 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
775 interface_opts.has_snaplen = TRUE;
776 interface_opts.snaplen = snaplen;
777 g_array_append_val(capture_opts->ifaces, interface_opts);
779 capture_opts->default_options.snaplen = snaplen;
780 capture_opts->default_options.has_snaplen = TRUE;
783 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
784 capture_opts->real_time_mode = TRUE;
786 #ifdef HAVE_PCAP_REMOTE
788 if (capture_opts->ifaces->len > 0) {
789 interface_options interface_opts;
791 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
792 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
793 interface_opts.datatx_udp = TRUE;
794 g_array_append_val(capture_opts->ifaces, interface_opts);
796 capture_opts->default_options.datatx_udp = TRUE;
800 case 'w': /* Write to capture file x */
801 capture_opts->saving_to_file = TRUE;
802 g_free(capture_opts->save_file);
803 capture_opts->save_file = g_strdup(optarg_str_p);
804 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
806 case 'g': /* enable group read access on the capture file(s) */
807 capture_opts->group_read_access = TRUE;
809 case 'y': /* Set the pcap data link type */
810 if (capture_opts->ifaces->len > 0) {
811 interface_options interface_opts;
813 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
814 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
815 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
816 if (interface_opts.linktype == -1) {
817 cmdarg_err("The specified data link type \"%s\" isn't valid",
821 g_array_append_val(capture_opts->ifaces, interface_opts);
823 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
824 if (capture_opts->default_options.linktype == -1) {
825 cmdarg_err("The specified data link type \"%s\" isn't valid",
832 /* the caller is responsible to send us only the right opt's */
833 g_assert_not_reached();
840 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
841 gboolean monitor_mode)
844 data_link_info_t *data_link_info;
846 if (caps->can_set_rfmon)
847 fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
848 name, monitor_mode ? "" : "not ");
850 fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
851 for (lt_entry = caps->data_link_types; lt_entry != NULL;
852 lt_entry = g_list_next(lt_entry)) {
853 data_link_info = (data_link_info_t *)lt_entry->data;
854 fprintf_stderr(" %s", data_link_info->name);
855 if (data_link_info->description != NULL)
856 fprintf_stderr(" (%s)", data_link_info->description);
858 fprintf_stderr(" (not supported)");
859 fprintf_stderr("\n");
863 /* Print an ASCII-formatted list of interfaces. */
865 capture_opts_print_interfaces(GList *if_list)
871 i = 1; /* Interface id number */
872 for (if_entry = g_list_first(if_list); if_entry != NULL;
873 if_entry = g_list_next(if_entry)) {
874 if_info = (if_info_t *)if_entry->data;
875 fprintf_stderr("%d. %s", i++, if_info->name);
877 /* print the interface friendly name if known, if not fall back to vendor description */
878 if (if_info->friendly_name != NULL){
879 fprintf_stderr(" (%s)", if_info->friendly_name);
881 /* Print the description if it exists */
882 if (if_info->description != NULL)
883 fprintf_stderr(" (%s)", if_info->description);
885 fprintf_stderr("\n");
890 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
893 interface_options interface_opts;
895 if (capture_opts->ifaces->len > 0) {
896 for (i = 0; i < capture_opts->ifaces->len; i++) {
897 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
898 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
899 if (interface_opts.snaplen < 1)
900 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
901 else if (interface_opts.snaplen < snaplen_min)
902 interface_opts.snaplen = snaplen_min;
903 g_array_append_val(capture_opts->ifaces, interface_opts);
906 if (capture_opts->default_options.snaplen < 1)
907 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
908 else if (capture_opts->default_options.snaplen < snaplen_min)
909 capture_opts->default_options.snaplen = snaplen_min;
914 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
916 /* Check the value range of the ring_num_files parameter */
917 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
918 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
919 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
920 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
921 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
923 #if RINGBUFFER_MIN_NUM_FILES > 0
924 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
925 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
926 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
932 capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
936 /* Did the user specify an interface to use? */
937 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
938 /* yes they did, return immediately - nothing further to do here */
942 /* No - is a default specified in the preferences file? */
943 if (capture_device != NULL) {
945 status = capture_opts_add_iface_opt(capture_opts, capture_device);
948 /* No default in preferences file, just pick the first interface from the list of interfaces. */
949 return capture_opts_add_iface_opt(capture_opts, "1");
953 #define S_IFIFO _S_IFIFO
956 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
959 /* copied from filesystem.c */
960 static int capture_opts_test_for_fifo(const char *path)
964 if (ws_stat64(path, &statb) < 0)
967 if (S_ISFIFO(statb.st_mode))
973 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
979 if (save_file != NULL) {
980 /* We're writing to a capture file. */
981 if (strcmp(save_file, "-") == 0) {
982 /* Writing to stdout. */
983 /* XXX - should we check whether it's a pipe? It's arguably
984 silly to do "-w - >output_file" rather than "-w output_file",
985 but by not checking we might be violating the Principle Of
986 Least Astonishment. */
989 /* not writing to stdout, test for a FIFO (aka named pipe) */
990 err = capture_opts_test_for_fifo(save_file);
993 case ENOENT: /* it doesn't exist, so we'll be creating it,
994 and it won't be a FIFO */
995 case 0: /* found it, but it's not a FIFO */
998 case ESPIPE: /* it is a FIFO */
1002 default: /* couldn't stat it */
1003 break; /* ignore: later attempt to open */
1004 /* will generate a nice msg */
1013 * Add all non-hidden selected interfaces in the "all interfaces" list
1014 * to the list of interfaces for the capture.
1017 collect_ifaces(capture_options *capture_opts)
1021 interface_options interface_opts;
1023 /* Empty out the existing list of interfaces. */
1024 for (i = capture_opts->ifaces->len; i != 0; i--) {
1025 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i - 1);
1026 g_free(interface_opts.name);
1027 g_free(interface_opts.descr);
1028 if (interface_opts.console_display_name != NULL)
1029 g_free(interface_opts.console_display_name);
1030 g_free(interface_opts.cfilter);
1031 #ifdef HAVE_PCAP_REMOTE
1032 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1033 g_free(interface_opts.remote_host);
1034 g_free(interface_opts.remote_port);
1035 g_free(interface_opts.auth_username);
1036 g_free(interface_opts.auth_password);
1039 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i - 1);
1042 /* Now fill the list up again. */
1043 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1044 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1045 if (!device.hidden && device.selected) {
1046 interface_opts.name = g_strdup(device.name);
1047 interface_opts.descr = g_strdup(device.display_name);
1048 interface_opts.console_display_name = g_strdup(device.name);
1049 interface_opts.linktype = device.active_dlt;
1050 interface_opts.cfilter = g_strdup(device.cfilter);
1051 interface_opts.snaplen = device.snaplen;
1052 interface_opts.has_snaplen = device.has_snaplen;
1053 interface_opts.promisc_mode = device.pmode;
1054 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
1055 interface_opts.buffer_size = device.buffer;
1057 #ifdef HAVE_PCAP_CREATE
1058 interface_opts.monitor_mode = device.monitor_mode_enabled;
1060 #ifdef HAVE_PCAP_REMOTE
1061 interface_opts.src_type = CAPTURE_IFREMOTE;
1062 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1063 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1064 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1065 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1066 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1067 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1068 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1069 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1071 #ifdef HAVE_PCAP_SETSAMPLING
1072 interface_opts.sampling_method = device.remote_opts.sampling_method;
1073 interface_opts.sampling_param = device.remote_opts.sampling_param;
1075 g_array_append_val(capture_opts->ifaces, interface_opts);
1083 #endif /* HAVE_LIBPCAP */