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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include <epan/packet.h>
42 #include "capture_opts.h"
43 #include "ringbuffer.h"
44 #include "clopts_common.h"
45 #include "console_io.h"
46 #include "cmdarg_err.h"
48 #include "capture_ifinfo.h"
49 #include "capture-pcap-util.h"
50 #include <wsutil/file_util.h>
52 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
56 capture_opts_init(capture_options *capture_opts, void *cf)
58 capture_opts->cf = cf;
59 capture_opts->cfilter = g_strdup(""); /* No capture filter string specified */
60 capture_opts->iface = NULL; /* Default is "pick the first interface" */
61 capture_opts->iface_descr = NULL;
62 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
63 capture_opts->default_options.name = g_strdup("");
64 capture_opts->default_options.descr = g_strdup("");
65 capture_opts->default_options.cfilter = g_strdup("");
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 = 1; /* 1 MB */
72 capture_opts->default_options.monitor_mode = FALSE;
73 #ifdef HAVE_PCAP_REMOTE
74 capture_opts->src_type = CAPTURE_IFLOCAL;
75 capture_opts->remote_host = NULL;
76 capture_opts->remote_port = NULL;
77 capture_opts->auth_type = CAPTURE_AUTH_NULL;
78 capture_opts->auth_username = NULL;
79 capture_opts->auth_password = NULL;
80 capture_opts->datatx_udp = FALSE;
81 capture_opts->nocap_rpcap = TRUE;
82 capture_opts->nocap_local = FALSE;
84 #ifdef HAVE_PCAP_SETSAMPLING
85 capture_opts->sampling_method = CAPTURE_SAMP_NONE;
86 capture_opts->sampling_param = 0;
88 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
89 capture_opts->buffer_size = 1; /* 1 MB */
91 capture_opts->has_snaplen = FALSE;
92 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
93 infinite, in effect */
94 capture_opts->promisc_mode = TRUE; /* promiscuous mode is the default */
95 capture_opts->monitor_mode = FALSE;
96 capture_opts->linktype = -1; /* the default linktype */
97 capture_opts->saving_to_file = FALSE;
98 capture_opts->save_file = NULL;
99 capture_opts->group_read_access = FALSE;
100 capture_opts->use_pcapng = FALSE; /* the default is pcap */
101 capture_opts->real_time_mode = TRUE;
102 capture_opts->show_info = TRUE;
103 capture_opts->quit_after_cap = FALSE;
104 capture_opts->restart = FALSE;
106 capture_opts->multi_files_on = FALSE;
107 capture_opts->has_file_duration = FALSE;
108 capture_opts->file_duration = 60; /* 1 min */
109 capture_opts->has_ring_num_files = FALSE;
110 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
112 capture_opts->has_autostop_files = FALSE;
113 capture_opts->autostop_files = 1;
114 capture_opts->has_autostop_packets = FALSE;
115 capture_opts->autostop_packets = 0;
116 capture_opts->has_autostop_filesize = FALSE;
117 capture_opts->autostop_filesize = 1024; /* 1 MB */
118 capture_opts->has_autostop_duration = FALSE;
119 capture_opts->autostop_duration = 60; /* 1 min */
122 capture_opts->fork_child = -1; /* invalid process handle */
124 capture_opts->signal_pipe_write_fd = -1;
126 capture_opts->state = CAPTURE_STOPPED;
127 capture_opts->output_to_pipe = FALSE;
129 capture_opts->owner = getuid();
130 capture_opts->group = getgid();
135 /* log content of capture_opts */
137 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
140 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
141 g_log(log_domain, log_level, "CFile : %p", capture_opts->cf);
142 g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
144 for (i = 0; i < capture_opts->ifaces->len; i++) {
145 interface_options options;
147 options = g_array_index(capture_opts->ifaces, interface_options, i);
148 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, options.name);
149 g_log(log_domain, log_level, "Interface Descr[%02d]: %s", i, options.descr);
150 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, options.cfilter);
151 g_log(log_domain, log_level, "Snap length[%02d] : %d", i, options.snaplen);
152 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, options.linktype);
153 g_log(log_domain, log_level, "Promiscous Mode[%02d]: %s", i, options.promisc_mode?"TRUE":"FALSE");
154 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
155 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, options.buffer_size);
157 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, options.monitor_mode?"TRUE":"FALSE");
159 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name);
160 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter);
161 g_log(log_domain, log_level, "Snap length[df] : %d", capture_opts->default_options.snaplen);
162 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
163 g_log(log_domain, log_level, "Promiscous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
165 #ifdef HAVE_PCAP_REMOTE
166 g_log(log_domain, log_level, "Capture source : %s",
167 capture_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" :
168 capture_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" :
170 if (capture_opts->src_type == CAPTURE_IFREMOTE) {
171 g_log(log_domain, log_level, "Remote host : %s", capture_opts->remote_host);
172 g_log(log_domain, log_level, "Remote port : %s", capture_opts->remote_port);
174 g_log(log_domain, log_level, "Authentication : %s",
175 capture_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" :
176 capture_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
178 if (capture_opts->auth_type == CAPTURE_AUTH_PWD) {
179 g_log(log_domain, log_level, "Auth username : %s", capture_opts->auth_password);
180 g_log(log_domain, log_level, "Auth password : <hidden>");
182 g_log(log_domain, log_level, "UDP data transfer : %u", capture_opts->datatx_udp);
183 g_log(log_domain, log_level, "No capture RPCAP : %u", capture_opts->nocap_rpcap);
184 g_log(log_domain, log_level, "No capture local : %u", capture_opts->nocap_local);
186 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
187 g_log(log_domain, log_level, "BufferSize : %u (MB)", capture_opts->buffer_size);
189 g_log(log_domain, log_level, "SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
190 g_log(log_domain, log_level, "Promisc : %u", capture_opts->promisc_mode);
191 g_log(log_domain, log_level, "LinkType : %d", capture_opts->linktype);
192 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
193 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
194 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
195 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
196 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
197 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
198 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
200 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
201 g_log(log_domain, log_level, "FileDuration (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
202 g_log(log_domain, log_level, "RingNumFiles (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
204 g_log(log_domain, log_level, "AutostopFiles (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
205 g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
206 g_log(log_domain, log_level, "AutostopFilesize(%u): %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
207 g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
209 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
211 g_log(log_domain, log_level, "SignalPipeWrite : %d", capture_opts->signal_pipe_write_fd);
216 * Given a string of the form "<autostop criterion>:<value>", as might appear
217 * as an argument to a "-a" option, parse it and set the criterion in
218 * question. Return an indication of whether it succeeded or failed
222 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
226 colonp = strchr(autostoparg, ':');
234 * Skip over any white space (there probably won't be any, but
235 * as we allow it in the preferences file, we might as well
238 while (isspace((guchar)*p))
242 * Put the colon back, so if our caller uses, in an
243 * error message, the string they passed us, the message
249 if (strcmp(autostoparg,"duration") == 0) {
250 capture_opts->has_autostop_duration = TRUE;
251 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
252 } else if (strcmp(autostoparg,"filesize") == 0) {
253 capture_opts->has_autostop_filesize = TRUE;
254 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
255 } else if (strcmp(autostoparg,"files") == 0) {
256 capture_opts->multi_files_on = TRUE;
257 capture_opts->has_autostop_files = TRUE;
258 capture_opts->autostop_files = get_positive_int(p,"autostop files");
262 *colonp = ':'; /* put the colon back */
267 * Given a string of the form "<ring buffer file>:<duration>", as might appear
268 * as an argument to a "-b" option, parse it and set the arguments in
269 * question. Return an indication of whether it succeeded or failed
273 get_ring_arguments(capture_options *capture_opts, const char *arg)
275 gchar *p = NULL, *colonp;
277 colonp = strchr(arg, ':');
285 * Skip over any white space (there probably won't be any, but
286 * as we allow it in the preferences file, we might as well
289 while (isspace((guchar)*p))
293 * Put the colon back, so if our caller uses, in an
294 * error message, the string they passed us, the message
301 if (strcmp(arg,"files") == 0) {
302 capture_opts->has_ring_num_files = TRUE;
303 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
304 } else if (strcmp(arg,"filesize") == 0) {
305 capture_opts->has_autostop_filesize = TRUE;
306 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
307 } else if (strcmp(arg,"duration") == 0) {
308 capture_opts->has_file_duration = TRUE;
309 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
312 *colonp = ':'; /* put the colon back */
316 #ifdef HAVE_PCAP_SETSAMPLING
318 * Given a string of the form "<sampling type>:<value>", as might appear
319 * as an argument to a "-m" option, parse it and set the arguments in
320 * question. Return an indication of whether it succeeded or failed
324 get_sampling_arguments(capture_options *capture_opts, const char *arg)
326 gchar *p = NULL, *colonp;
328 colonp = strchr(arg, ':');
335 while (isspace((guchar)*p))
342 if (strcmp(arg, "count") == 0) {
343 capture_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
344 capture_opts->sampling_param = get_positive_int(p, "sampling count");
345 } else if (strcmp(arg, "timer") == 0) {
346 capture_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
347 capture_opts->sampling_param = get_positive_int(p, "sampling timer");
354 #ifdef HAVE_PCAP_REMOTE
356 * Given a string of the form "<username>:<password>", as might appear
357 * as an argument to a "-A" option, parse it and set the arguments in
358 * question. Return an indication of whether it succeeded or failed
362 get_auth_arguments(capture_options *capture_opts, const char *arg)
364 gchar *p = NULL, *colonp;
366 colonp = strchr(arg, ':');
373 while (isspace((guchar)*p))
376 capture_opts->auth_type = CAPTURE_AUTH_PWD;
377 capture_opts->auth_username = g_strdup(arg);
378 capture_opts->auth_password = g_strdup(p);
385 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
393 interface_options options;
397 * If the argument is a number, treat it as an index into the list
398 * of adapters, as printed by "tshark -D".
400 * This should be OK on UNIX systems, as interfaces shouldn't have
401 * names that begin with digits. It can be useful on Windows, where
402 * more than one interface can have the same name.
404 adapter_index = strtol(optarg_str_p, &p, 10);
405 if (p != NULL && *p == '\0') {
406 if (adapter_index < 0) {
407 cmdarg_err("The specified adapter index is a negative number");
410 if (adapter_index > INT_MAX) {
411 cmdarg_err("The specified adapter index is too large (greater than %d)",
415 if (adapter_index == 0) {
416 cmdarg_err("There is no interface with that adapter index");
419 if_list = capture_interface_list(&err, &err_str);
420 if (if_list == NULL) {
423 case CANT_GET_INTERFACE_LIST:
424 cmdarg_err("%s", err_str);
428 case NO_INTERFACES_FOUND:
429 cmdarg_err("There are no interfaces on which a capture can be done");
434 if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
435 if (if_info == NULL) {
436 cmdarg_err("There is no interface with that adapter index");
439 capture_opts->iface = g_strdup(if_info->name);
440 options.name = g_strdup(if_info->name);
441 /* We don't set iface_descr here because doing so requires
442 * capture_ui_utils.c which requires epan/prefs.c which is
443 * probably a bit too much dependency for here...
445 free_interface_list(if_list);
447 capture_opts->iface = g_strdup(optarg_str_p);
448 options.name = g_strdup(optarg_str_p);
450 options.descr = g_strdup(capture_opts->default_options.descr);
451 options.cfilter = g_strdup(capture_opts->default_options.cfilter);
452 options.snaplen = capture_opts->default_options.snaplen;
453 options.linktype = capture_opts->default_options.linktype;
454 options.promisc_mode = capture_opts->default_options.promisc_mode;
455 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
456 options.buffer_size = capture_opts->default_options.buffer_size;
458 options.monitor_mode = capture_opts->default_options.monitor_mode;
460 g_array_append_val(capture_opts->ifaces, options);
466 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
471 case 'a': /* autostop criteria */
472 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
473 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
477 #ifdef HAVE_PCAP_REMOTE
479 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
480 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
485 case 'b': /* Ringbuffer option */
486 capture_opts->multi_files_on = TRUE;
487 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
488 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
492 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
493 case 'B': /* Buffer size */
494 capture_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
495 if (capture_opts->ifaces->len > 0) {
496 interface_options options;
498 options = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
499 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
500 options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
501 g_array_append_val(capture_opts->ifaces, options);
503 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
507 case 'c': /* Capture n packets */
508 capture_opts->has_autostop_packets = TRUE;
509 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
511 case 'f': /* capture filter */
512 if ((!capture_opts->use_pcapng) && (capture_opts->has_cfilter)) {
513 cmdarg_err("More than one -f argument specified");
516 capture_opts->has_cfilter = TRUE;
517 g_free(capture_opts->cfilter);
518 capture_opts->cfilter = g_strdup(optarg_str_p);
519 if (capture_opts->ifaces->len > 0) {
520 interface_options options;
522 options = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
523 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
524 g_free(options.cfilter);
525 options.cfilter = g_strdup(capture_opts->cfilter);
526 g_array_append_val(capture_opts->ifaces, options);
528 g_free(capture_opts->default_options.cfilter);
529 capture_opts->default_options.cfilter = g_strdup(capture_opts->cfilter);
532 case 'H': /* Hide capture info dialog box */
533 capture_opts->show_info = FALSE;
535 case 'i': /* Use interface x */
536 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
541 #ifdef HAVE_PCAP_CREATE
542 case 'I': /* Capture in monitor mode */
543 capture_opts->monitor_mode = TRUE;
544 if (capture_opts->ifaces->len > 0) {
545 interface_options options;
547 options = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
548 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
549 options.monitor_mode = TRUE;
550 g_array_append_val(capture_opts->ifaces, options);
552 capture_opts->default_options.monitor_mode = TRUE;
556 case 'k': /* Start capture immediately */
557 *start_capture = TRUE;
559 /*case 'l':*/ /* Automatic scrolling in live capture mode */
560 #ifdef HAVE_PCAP_SETSAMPLING
562 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
563 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
568 case 'n': /* Use pcapng format */
569 capture_opts->use_pcapng = TRUE;
571 case 'p': /* Don't capture in promiscuous mode */
572 capture_opts->promisc_mode = FALSE;
573 if (capture_opts->ifaces->len > 0) {
574 interface_options options;
576 options = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
577 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
578 options.promisc_mode = FALSE;
579 g_array_append_val(capture_opts->ifaces, options);
581 capture_opts->default_options.promisc_mode = FALSE;
584 case 'Q': /* Quit after capture (just capture to file) */
585 capture_opts->quit_after_cap = TRUE;
586 *start_capture = TRUE; /*** -Q implies -k !! ***/
588 #ifdef HAVE_PCAP_REMOTE
590 capture_opts->nocap_rpcap = FALSE;
593 case 's': /* Set the snapshot (capture) length */
594 capture_opts->has_snaplen = TRUE;
595 capture_opts->snaplen = get_natural_int(optarg_str_p, "snapshot length");
597 * Make a snapshot length of 0 equivalent to the maximum packet
598 * length, mirroring what tcpdump does.
600 if (capture_opts->snaplen == 0)
601 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
602 if (capture_opts->ifaces->len > 0) {
603 interface_options options;
605 options = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
606 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
607 options.snaplen = capture_opts->snaplen;
608 g_array_append_val(capture_opts->ifaces, options);
610 capture_opts->default_options.snaplen = capture_opts->snaplen;
613 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
614 capture_opts->real_time_mode = TRUE;
616 #ifdef HAVE_PCAP_REMOTE
618 capture_opts->datatx_udp = TRUE;
621 case 'w': /* Write to capture file x */
622 capture_opts->saving_to_file = TRUE;
623 g_free(capture_opts->save_file);
624 #if defined _WIN32 && GLIB_CHECK_VERSION(2,6,0)
625 /* since GLib 2.6, we need to convert filenames to utf8 for Win32 */
626 capture_opts->save_file = g_locale_to_utf8(optarg_str_p, -1, NULL, NULL, NULL);
628 capture_opts->save_file = g_strdup(optarg_str_p);
630 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
632 case 'g': /* enable group read access on the capture file(s) */
633 capture_opts->group_read_access = TRUE;
635 case 'y': /* Set the pcap data link type */
636 capture_opts->linktype = linktype_name_to_val(optarg_str_p);
637 if (capture_opts->linktype == -1) {
638 cmdarg_err("The specified data link type \"%s\" isn't valid",
642 if (capture_opts->ifaces->len > 0) {
643 interface_options options;
645 options = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
646 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
647 options.linktype = linktype_name_to_val(optarg_str_p);
648 g_array_append_val(capture_opts->ifaces, options);
650 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
654 /* the caller is responsible to send us only the right opt's */
655 g_assert_not_reached();
662 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
663 gboolean monitor_mode)
666 data_link_info_t *data_link_info;
668 if (caps->can_set_rfmon)
669 fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
670 name, monitor_mode ? "" : "not ");
672 fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
673 for (lt_entry = caps->data_link_types; lt_entry != NULL;
674 lt_entry = g_list_next(lt_entry)) {
675 data_link_info = (data_link_info_t *)lt_entry->data;
676 fprintf_stderr(" %s", data_link_info->name);
677 if (data_link_info->description != NULL)
678 fprintf_stderr(" (%s)", data_link_info->description);
680 fprintf_stderr(" (not supported)");
681 fprintf_stderr("\n");
685 /* Print an ASCII-formatted list of interfaces. */
687 capture_opts_print_interfaces(GList *if_list)
693 i = 1; /* Interface id number */
694 for (if_entry = g_list_first(if_list); if_entry != NULL;
695 if_entry = g_list_next(if_entry)) {
696 if_info = (if_info_t *)if_entry->data;
697 fprintf_stderr("%d. %s", i++, if_info->name);
699 /* Print the description if it exists */
700 if (if_info->description != NULL)
701 fprintf_stderr(" (%s)", if_info->description);
702 fprintf_stderr("\n");
707 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
710 interface_options options;
712 if (capture_opts->snaplen < 1)
713 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
714 else if (capture_opts->snaplen < snaplen_min)
715 capture_opts->snaplen = snaplen_min;
717 for (i = 0; i < capture_opts->ifaces->len; i++) {
718 options = g_array_index(capture_opts->ifaces, interface_options, 0);
719 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
720 if (options.snaplen < 1)
721 options.snaplen = WTAP_MAX_PACKET_SIZE;
722 else if (options.snaplen < snaplen_min)
723 options.snaplen = snaplen_min;
724 g_array_append_val(capture_opts->ifaces, options);
729 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
731 /* Check the value range of the ring_num_files parameter */
732 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
733 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
734 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
735 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
736 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
738 #if RINGBUFFER_MIN_NUM_FILES > 0
739 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
740 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
741 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
746 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
752 interface_options options;
755 /* Did the user specify an interface to use? */
756 if (capture_opts->ifaces->len == 0) {
757 /* No - is a default specified in the preferences file? */
758 if (capture_device != NULL) {
760 capture_opts->iface = g_strdup(capture_device);
761 options.name = g_strdup(capture_device);
762 /* We don't set iface_descr here because doing so requires
763 * capture_ui_utils.c which requires epan/prefs.c which is
764 * probably a bit too much dependency for here...
767 /* No - pick the first one from the list of interfaces. */
768 if_list = capture_interface_list(&err, &err_str);
769 if (if_list == NULL) {
772 case CANT_GET_INTERFACE_LIST:
773 cmdarg_err("%s", err_str);
777 case NO_INTERFACES_FOUND:
778 cmdarg_err("There are no interfaces on which a capture can be done");
783 if_info = (if_info_t *)if_list->data; /* first interface */
784 capture_opts->iface = g_strdup(if_info->name);
785 options.name = g_strdup(if_info->name);
786 /* We don't set iface_descr here because doing so requires
787 * capture_ui_utils.c which requires epan/prefs.c which is
788 * probably a bit too much dependency for here...
790 free_interface_list(if_list);
792 options.cfilter = g_strdup(capture_opts->default_options.cfilter);
793 options.snaplen = capture_opts->default_options.snaplen;
794 options.linktype = capture_opts->default_options.linktype;
795 options.promisc_mode = capture_opts->default_options.promisc_mode;
796 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
797 options.buffer_size = capture_opts->default_options.buffer_size;
799 options.monitor_mode = capture_opts->default_options.monitor_mode;
800 g_array_append_val(capture_opts->ifaces, options);
809 #define S_IFIFO _S_IFIFO
812 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
815 /* copied from filesystem.c */
816 static int capture_opts_test_for_fifo(const char *path)
820 if (ws_stat64(path, &statb) < 0)
823 if (S_ISFIFO(statb.st_mode))
829 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
835 if (save_file != NULL) {
836 /* We're writing to a capture file. */
837 if (strcmp(save_file, "-") == 0) {
838 /* Writing to stdout. */
839 /* XXX - should we check whether it's a pipe? It's arguably
840 silly to do "-w - >output_file" rather than "-w output_file",
841 but by not checking we might be violating the Principle Of
842 Least Astonishment. */
845 /* not writing to stdout, test for a FIFO (aka named pipe) */
846 err = capture_opts_test_for_fifo(save_file);
849 case ENOENT: /* it doesn't exist, so we'll be creating it,
850 and it won't be a FIFO */
851 case 0: /* found it, but it's not a FIFO */
854 case ESPIPE: /* it is a FIFO */
858 default: /* couldn't stat it */
859 break; /* ignore: later attempt to open */
860 /* will generate a nice msg */
868 #endif /* HAVE_LIBPCAP */