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>
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);
146 g_log(log_domain, log_level, "Interface Descr[%02d] : %s", i, interface_opts.descr);
147 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter);
148 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
149 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
150 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
151 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
152 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
154 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
155 #ifdef HAVE_PCAP_REMOTE
156 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
157 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
158 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
160 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
161 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host);
162 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port);
164 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
165 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
166 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
168 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
169 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username);
170 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
172 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
173 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
174 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
176 #ifdef HAVE_PCAP_SETSAMPLING
177 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
178 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
181 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name);
182 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr);
183 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter);
184 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
185 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
186 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
187 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
188 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
190 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
191 #ifdef HAVE_PCAP_REMOTE
192 g_log(log_domain, log_level, "Capture source[df] : %s",
193 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
194 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
196 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
197 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host);
198 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port);
200 g_log(log_domain, log_level, "Authentication[df] : %s",
201 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
202 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
204 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
205 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username);
206 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
208 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
209 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
210 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
212 #ifdef HAVE_PCAP_SETSAMPLING
213 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
214 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
216 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
217 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
218 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
219 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
220 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
221 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
222 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
224 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
225 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
226 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
228 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
229 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
230 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
231 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
233 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
235 g_log(log_domain, log_level, "SignalPipeWrite : %d", capture_opts->signal_pipe_write_fd);
240 * Given a string of the form "<autostop criterion>:<value>", as might appear
241 * as an argument to a "-a" option, parse it and set the criterion in
242 * question. Return an indication of whether it succeeded or failed
246 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
250 colonp = strchr(autostoparg, ':');
258 * Skip over any white space (there probably won't be any, but
259 * as we allow it in the preferences file, we might as well
262 while (isspace((guchar)*p))
266 * Put the colon back, so if our caller uses, in an
267 * error message, the string they passed us, the message
273 if (strcmp(autostoparg,"duration") == 0) {
274 capture_opts->has_autostop_duration = TRUE;
275 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
276 } else if (strcmp(autostoparg,"filesize") == 0) {
277 capture_opts->has_autostop_filesize = TRUE;
278 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
279 } else if (strcmp(autostoparg,"files") == 0) {
280 capture_opts->multi_files_on = TRUE;
281 capture_opts->has_autostop_files = TRUE;
282 capture_opts->autostop_files = get_positive_int(p,"autostop files");
286 *colonp = ':'; /* put the colon back */
291 * Given a string of the form "<ring buffer file>:<duration>", as might appear
292 * as an argument to a "-b" option, parse it and set the arguments in
293 * question. Return an indication of whether it succeeded or failed
297 get_ring_arguments(capture_options *capture_opts, const char *arg)
299 gchar *p = NULL, *colonp;
301 colonp = strchr(arg, ':');
309 * Skip over any white space (there probably won't be any, but
310 * as we allow it in the preferences file, we might as well
313 while (isspace((guchar)*p))
317 * Put the colon back, so if our caller uses, in an
318 * error message, the string they passed us, the message
325 if (strcmp(arg,"files") == 0) {
326 capture_opts->has_ring_num_files = TRUE;
327 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
328 } else if (strcmp(arg,"filesize") == 0) {
329 capture_opts->has_autostop_filesize = TRUE;
330 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
331 } else if (strcmp(arg,"duration") == 0) {
332 capture_opts->has_file_duration = TRUE;
333 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
336 *colonp = ':'; /* put the colon back */
340 #ifdef HAVE_PCAP_SETSAMPLING
342 * Given a string of the form "<sampling type>:<value>", as might appear
343 * as an argument to a "-m" option, parse it and set the arguments in
344 * question. Return an indication of whether it succeeded or failed
348 get_sampling_arguments(capture_options *capture_opts, const char *arg)
350 gchar *p = NULL, *colonp;
352 colonp = strchr(arg, ':');
359 while (isspace((guchar)*p))
366 if (strcmp(arg, "count") == 0) {
367 if (capture_opts->ifaces->len > 0) {
368 interface_options interface_opts;
370 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
371 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
372 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
373 interface_opts.sampling_param = get_positive_int(p, "sampling count");
374 g_array_append_val(capture_opts->ifaces, interface_opts);
376 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
377 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
379 } else if (strcmp(arg, "timer") == 0) {
380 if (capture_opts->ifaces->len > 0) {
381 interface_options interface_opts;
383 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
384 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
385 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
386 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
387 g_array_append_val(capture_opts->ifaces, interface_opts);
389 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
390 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
398 #ifdef HAVE_PCAP_REMOTE
400 * Given a string of the form "<username>:<password>", as might appear
401 * as an argument to a "-A" option, parse it and set the arguments in
402 * question. Return an indication of whether it succeeded or failed
406 get_auth_arguments(capture_options *capture_opts, const char *arg)
408 gchar *p = NULL, *colonp;
410 colonp = strchr(arg, ':');
417 while (isspace((guchar)*p))
420 if (capture_opts->ifaces->len > 0) {
421 interface_options interface_opts;
423 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
424 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
425 interface_opts.auth_type = CAPTURE_AUTH_PWD;
426 interface_opts.auth_username = g_strdup(arg);
427 interface_opts.auth_password = g_strdup(p);
428 g_array_append_val(capture_opts->ifaces, interface_opts);
430 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
431 capture_opts->default_options.auth_username = g_strdup(arg);
432 capture_opts->default_options.auth_password = g_strdup(p);
440 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
448 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 /* We don't set iface_descr here because doing so requires
497 * capture_ui_utils.c which requires epan/prefs.c which is
498 * probably a bit too much dependency for here...
500 free_interface_list(if_list);
502 interface_opts.name = g_strdup(optarg_str_p);
504 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
505 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
506 interface_opts.snaplen = capture_opts->default_options.snaplen;
507 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
508 interface_opts.linktype = capture_opts->default_options.linktype;
509 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
510 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
511 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
513 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
514 #ifdef HAVE_PCAP_REMOTE
515 interface_opts.src_type = capture_opts->default_options.src_type;
516 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
517 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
518 interface_opts.auth_type = capture_opts->default_options.auth_type;
519 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
520 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
521 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
522 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
523 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
525 #ifdef HAVE_PCAP_SETSAMPLING
526 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
527 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
530 g_array_append_val(capture_opts->ifaces, interface_opts);
537 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
542 case 'a': /* autostop criteria */
543 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
544 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
548 #ifdef HAVE_PCAP_REMOTE
550 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
551 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
556 case 'b': /* Ringbuffer option */
557 capture_opts->multi_files_on = TRUE;
558 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
559 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
563 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
564 case 'B': /* Buffer size */
565 if (capture_opts->ifaces->len > 0) {
566 interface_options interface_opts;
568 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
569 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
570 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
571 g_array_append_val(capture_opts->ifaces, interface_opts);
573 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
577 case 'c': /* Capture n packets */
578 capture_opts->has_autostop_packets = TRUE;
579 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
581 case 'f': /* capture filter */
582 if (capture_opts->ifaces->len > 0) {
583 interface_options interface_opts;
585 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
586 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
587 g_free(interface_opts.cfilter);
588 interface_opts.cfilter = g_strdup(optarg_str_p);
589 g_array_append_val(capture_opts->ifaces, interface_opts);
591 g_free(capture_opts->default_options.cfilter);
592 capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
595 case 'H': /* Hide capture info dialog box */
596 capture_opts->show_info = FALSE;
598 case 'i': /* Use interface x */
599 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
604 #ifdef HAVE_PCAP_CREATE
605 case 'I': /* Capture in monitor mode */
606 if (capture_opts->ifaces->len > 0) {
607 interface_options interface_opts;
609 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
610 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
611 interface_opts.monitor_mode = TRUE;
612 g_array_append_val(capture_opts->ifaces, interface_opts);
614 capture_opts->default_options.monitor_mode = TRUE;
618 case 'k': /* Start capture immediately */
619 *start_capture = TRUE;
621 /*case 'l':*/ /* Automatic scrolling in live capture mode */
622 #ifdef HAVE_PCAP_SETSAMPLING
624 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
625 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
630 case 'n': /* Use pcapng format */
631 capture_opts->use_pcapng = TRUE;
633 case 'p': /* Don't capture in promiscuous mode */
634 if (capture_opts->ifaces->len > 0) {
635 interface_options interface_opts;
637 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
638 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
639 interface_opts.promisc_mode = FALSE;
640 g_array_append_val(capture_opts->ifaces, interface_opts);
642 capture_opts->default_options.promisc_mode = FALSE;
645 case 'P': /* Use pcap format */
646 capture_opts->use_pcapng = FALSE;
648 #ifdef HAVE_PCAP_REMOTE
650 if (capture_opts->ifaces->len > 0) {
651 interface_options interface_opts;
653 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
654 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
655 interface_opts.nocap_rpcap = FALSE;
656 g_array_append_val(capture_opts->ifaces, interface_opts);
658 capture_opts->default_options.nocap_rpcap = FALSE;
662 case 's': /* Set the snapshot (capture) length */
663 snaplen = get_natural_int(optarg_str_p, "snapshot length");
665 * Make a snapshot length of 0 equivalent to the maximum packet
666 * length, mirroring what tcpdump does.
669 snaplen = WTAP_MAX_PACKET_SIZE;
670 if (capture_opts->ifaces->len > 0) {
671 interface_options interface_opts;
673 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
674 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
675 interface_opts.has_snaplen = TRUE;
676 interface_opts.snaplen = snaplen;
677 g_array_append_val(capture_opts->ifaces, interface_opts);
679 capture_opts->default_options.snaplen = snaplen;
680 capture_opts->default_options.has_snaplen = TRUE;
683 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
684 capture_opts->real_time_mode = TRUE;
686 #ifdef HAVE_PCAP_REMOTE
688 if (capture_opts->ifaces->len > 0) {
689 interface_options interface_opts;
691 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
692 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
693 interface_opts.datatx_udp = TRUE;
694 g_array_append_val(capture_opts->ifaces, interface_opts);
696 capture_opts->default_options.datatx_udp = TRUE;
700 case 'w': /* Write to capture file x */
701 capture_opts->saving_to_file = TRUE;
702 g_free(capture_opts->save_file);
703 capture_opts->save_file = g_strdup(optarg_str_p);
704 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
706 case 'g': /* enable group read access on the capture file(s) */
707 capture_opts->group_read_access = TRUE;
709 case 'y': /* Set the pcap data link type */
710 if (capture_opts->ifaces->len > 0) {
711 interface_options interface_opts;
713 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
714 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
715 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
716 if (interface_opts.linktype == -1) {
717 cmdarg_err("The specified data link type \"%s\" isn't valid",
721 g_array_append_val(capture_opts->ifaces, interface_opts);
723 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
724 if (capture_opts->default_options.linktype == -1) {
725 cmdarg_err("The specified data link type \"%s\" isn't valid",
732 /* the caller is responsible to send us only the right opt's */
733 g_assert_not_reached();
740 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
741 gboolean monitor_mode)
744 data_link_info_t *data_link_info;
746 if (caps->can_set_rfmon)
747 fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
748 name, monitor_mode ? "" : "not ");
750 fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
751 for (lt_entry = caps->data_link_types; lt_entry != NULL;
752 lt_entry = g_list_next(lt_entry)) {
753 data_link_info = (data_link_info_t *)lt_entry->data;
754 fprintf_stderr(" %s", data_link_info->name);
755 if (data_link_info->description != NULL)
756 fprintf_stderr(" (%s)", data_link_info->description);
758 fprintf_stderr(" (not supported)");
759 fprintf_stderr("\n");
763 /* Print an ASCII-formatted list of interfaces. */
765 capture_opts_print_interfaces(GList *if_list)
771 i = 1; /* Interface id number */
772 for (if_entry = g_list_first(if_list); if_entry != NULL;
773 if_entry = g_list_next(if_entry)) {
774 if_info = (if_info_t *)if_entry->data;
775 fprintf_stderr("%d. %s", i++, if_info->name);
777 /* Print the description if it exists */
778 if (if_info->description != NULL)
779 fprintf_stderr(" (%s)", if_info->description);
780 fprintf_stderr("\n");
785 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
788 interface_options interface_opts;
790 if (capture_opts->ifaces->len > 0) {
791 for (i = 0; i < capture_opts->ifaces->len; i++) {
792 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
793 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
794 if (interface_opts.snaplen < 1)
795 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
796 else if (interface_opts.snaplen < snaplen_min)
797 interface_opts.snaplen = snaplen_min;
798 g_array_append_val(capture_opts->ifaces, interface_opts);
801 if (capture_opts->default_options.snaplen < 1)
802 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
803 else if (capture_opts->default_options.snaplen < snaplen_min)
804 capture_opts->default_options.snaplen = snaplen_min;
809 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
811 /* Check the value range of the ring_num_files parameter */
812 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
813 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
814 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
815 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
816 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
818 #if RINGBUFFER_MIN_NUM_FILES > 0
819 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
820 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
821 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
826 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
832 interface_options interface_opts;
835 /* Did the user specify an interface to use? */
836 if (capture_opts->num_selected == 0 && capture_opts->ifaces->len == 0) {
837 /* No - is a default specified in the preferences file? */
838 if (capture_device != NULL) {
840 interface_opts.name = g_strdup(capture_device);
841 /* We don't set iface_descr here because doing so requires
842 * capture_ui_utils.c which requires epan/prefs.c which is
843 * probably a bit too much dependency for here...
846 /* No - pick the first one from the list of interfaces. */
847 if_list = capture_interface_list(&err, &err_str);
848 if (if_list == NULL) {
851 case CANT_GET_INTERFACE_LIST:
853 cmdarg_err("%s", err_str);
857 case NO_INTERFACES_FOUND:
858 cmdarg_err("There are no interfaces on which a capture can be done");
863 if_info = (if_info_t *)if_list->data; /* first interface */
864 interface_opts.name = g_strdup(if_info->name);
865 /* We don't set iface_descr here because doing so requires
866 * capture_ui_utils.c which requires epan/prefs.c which is
867 * probably a bit too much dependency for here...
869 free_interface_list(if_list);
871 if (capture_opts->default_options.descr) {
872 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
874 interface_opts.descr = NULL;
876 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
877 interface_opts.snaplen = capture_opts->default_options.snaplen;
878 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
879 interface_opts.linktype = capture_opts->default_options.linktype;
880 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
881 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
882 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
884 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
885 #ifdef HAVE_PCAP_REMOTE
886 interface_opts.src_type = capture_opts->default_options.src_type;
887 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
888 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
889 interface_opts.auth_type = capture_opts->default_options.auth_type;
890 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
891 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
892 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
893 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
894 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
896 #ifdef HAVE_PCAP_SETSAMPLING
897 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
898 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
900 g_array_append_val(capture_opts->ifaces, interface_opts);
909 #define S_IFIFO _S_IFIFO
912 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
915 /* copied from filesystem.c */
916 static int capture_opts_test_for_fifo(const char *path)
920 if (ws_stat64(path, &statb) < 0)
923 if (S_ISFIFO(statb.st_mode))
929 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
935 if (save_file != NULL) {
936 /* We're writing to a capture file. */
937 if (strcmp(save_file, "-") == 0) {
938 /* Writing to stdout. */
939 /* XXX - should we check whether it's a pipe? It's arguably
940 silly to do "-w - >output_file" rather than "-w output_file",
941 but by not checking we might be violating the Principle Of
942 Least Astonishment. */
945 /* not writing to stdout, test for a FIFO (aka named pipe) */
946 err = capture_opts_test_for_fifo(save_file);
949 case ENOENT: /* it doesn't exist, so we'll be creating it,
950 and it won't be a FIFO */
951 case 0: /* found it, but it's not a FIFO */
954 case ESPIPE: /* it is a FIFO */
958 default: /* couldn't stat it */
959 break; /* ignore: later attempt to open */
960 /* will generate a nice msg */
969 collect_ifaces(capture_options *capture_opts)
973 interface_options interface_opts;
974 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
975 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
976 if (!device.hidden && device.selected) {
977 interface_opts.name = g_strdup(device.name);
978 interface_opts.descr = g_strdup(device.display_name);
979 interface_opts.linktype = device.active_dlt;
980 interface_opts.cfilter = g_strdup(device.cfilter);
981 interface_opts.snaplen = device.snaplen;
982 interface_opts.has_snaplen = device.has_snaplen;
983 interface_opts.promisc_mode = device.pmode;
984 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
985 interface_opts.buffer_size = device.buffer;
987 #ifdef HAVE_PCAP_CREATE
988 interface_opts.monitor_mode = device.monitor_mode_enabled;
991 #ifdef HAVE_PCAP_REMOTE
992 interface_opts.src_type = CAPTURE_IFREMOTE;
993 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
994 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
995 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
996 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
997 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
998 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
999 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1000 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1002 #ifdef HAVE_PCAP_SETSAMPLING
1003 interface_opts.sampling_method = device.remote_opts.sampling_method;
1004 interface_opts.sampling_param = device.remote_opts.sampling_param;
1007 g_array_append_val(capture_opts->ifaces, interface_opts);
1015 #endif /* HAVE_LIBPCAP */