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->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
60 capture_opts->default_options.name = NULL;
61 capture_opts->default_options.descr = NULL;
62 capture_opts->default_options.cfilter = NULL;
63 capture_opts->default_options.has_snaplen = FALSE;
64 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
65 capture_opts->default_options.linktype = -1;
66 capture_opts->default_options.promisc_mode = TRUE;
67 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
68 capture_opts->default_options.buffer_size = 1; /* 1 MB */
70 capture_opts->default_options.monitor_mode = FALSE;
71 #ifdef HAVE_PCAP_REMOTE
72 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
73 capture_opts->default_options.remote_host = NULL;
74 capture_opts->default_options.remote_port = NULL;
75 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
76 capture_opts->default_options.auth_username = NULL;
77 capture_opts->default_options.auth_password = NULL;
78 capture_opts->default_options.datatx_udp = FALSE;
79 capture_opts->default_options.nocap_rpcap = TRUE;
80 capture_opts->default_options.nocap_local = FALSE;
82 #ifdef HAVE_PCAP_SETSAMPLING
83 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
84 capture_opts->default_options.sampling_param = 0;
86 capture_opts->saving_to_file = FALSE;
87 capture_opts->save_file = NULL;
88 capture_opts->group_read_access = FALSE;
89 #ifdef PCAP_NG_DEFAULT
90 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
92 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
94 capture_opts->real_time_mode = TRUE;
95 capture_opts->show_info = TRUE;
96 capture_opts->quit_after_cap = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
97 capture_opts->restart = FALSE;
99 capture_opts->multi_files_on = FALSE;
100 capture_opts->has_file_duration = FALSE;
101 capture_opts->file_duration = 60; /* 1 min */
102 capture_opts->has_ring_num_files = FALSE;
103 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
105 capture_opts->has_autostop_files = FALSE;
106 capture_opts->autostop_files = 1;
107 capture_opts->has_autostop_packets = FALSE;
108 capture_opts->autostop_packets = 0;
109 capture_opts->has_autostop_filesize = FALSE;
110 capture_opts->autostop_filesize = 1024; /* 1 MB */
111 capture_opts->has_autostop_duration = FALSE;
112 capture_opts->autostop_duration = 60; /* 1 min */
115 capture_opts->fork_child = -1; /* invalid process handle */
117 capture_opts->signal_pipe_write_fd = -1;
119 capture_opts->state = CAPTURE_STOPPED;
120 capture_opts->output_to_pipe = FALSE;
122 capture_opts->owner = getuid();
123 capture_opts->group = getgid();
128 /* log content of capture_opts */
130 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
133 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
134 g_log(log_domain, log_level, "CFile : %p", capture_opts->cf);
136 for (i = 0; i < capture_opts->ifaces->len; i++) {
137 interface_options interface_opts;
139 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
140 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name);
141 g_log(log_domain, log_level, "Interface Descr[%02d] : %s", i, interface_opts.descr);
142 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter);
143 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
144 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
145 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
146 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
147 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
149 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
150 #ifdef HAVE_PCAP_REMOTE
151 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
152 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
153 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
155 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
156 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host);
157 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port);
159 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
160 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
161 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
163 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
164 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username);
165 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
167 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
168 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
169 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
171 #ifdef HAVE_PCAP_SETSAMPLING
172 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
173 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
176 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name);
177 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr);
178 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter);
179 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
180 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
181 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
182 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
183 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
185 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
186 #ifdef HAVE_PCAP_REMOTE
187 g_log(log_domain, log_level, "Capture source[df] : %s",
188 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
189 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
191 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
192 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host);
193 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port);
195 g_log(log_domain, log_level, "Authentication[df] : %s",
196 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
197 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
199 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
200 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username);
201 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
203 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
204 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
205 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
207 #ifdef HAVE_PCAP_SETSAMPLING
208 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
209 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
211 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
212 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
213 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
214 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
215 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
216 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
217 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
219 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
220 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
221 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
223 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
224 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
225 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
226 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
228 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
230 g_log(log_domain, log_level, "SignalPipeWrite : %d", capture_opts->signal_pipe_write_fd);
235 * Given a string of the form "<autostop criterion>:<value>", as might appear
236 * as an argument to a "-a" option, parse it and set the criterion in
237 * question. Return an indication of whether it succeeded or failed
241 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
245 colonp = strchr(autostoparg, ':');
253 * Skip over any white space (there probably won't be any, but
254 * as we allow it in the preferences file, we might as well
257 while (isspace((guchar)*p))
261 * Put the colon back, so if our caller uses, in an
262 * error message, the string they passed us, the message
268 if (strcmp(autostoparg,"duration") == 0) {
269 capture_opts->has_autostop_duration = TRUE;
270 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
271 } else if (strcmp(autostoparg,"filesize") == 0) {
272 capture_opts->has_autostop_filesize = TRUE;
273 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
274 } else if (strcmp(autostoparg,"files") == 0) {
275 capture_opts->multi_files_on = TRUE;
276 capture_opts->has_autostop_files = TRUE;
277 capture_opts->autostop_files = get_positive_int(p,"autostop files");
281 *colonp = ':'; /* put the colon back */
286 * Given a string of the form "<ring buffer file>:<duration>", as might appear
287 * as an argument to a "-b" option, parse it and set the arguments in
288 * question. Return an indication of whether it succeeded or failed
292 get_ring_arguments(capture_options *capture_opts, const char *arg)
294 gchar *p = NULL, *colonp;
296 colonp = strchr(arg, ':');
304 * Skip over any white space (there probably won't be any, but
305 * as we allow it in the preferences file, we might as well
308 while (isspace((guchar)*p))
312 * Put the colon back, so if our caller uses, in an
313 * error message, the string they passed us, the message
320 if (strcmp(arg,"files") == 0) {
321 capture_opts->has_ring_num_files = TRUE;
322 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
323 } else if (strcmp(arg,"filesize") == 0) {
324 capture_opts->has_autostop_filesize = TRUE;
325 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
326 } else if (strcmp(arg,"duration") == 0) {
327 capture_opts->has_file_duration = TRUE;
328 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
331 *colonp = ':'; /* put the colon back */
335 #ifdef HAVE_PCAP_SETSAMPLING
337 * Given a string of the form "<sampling type>:<value>", as might appear
338 * as an argument to a "-m" option, parse it and set the arguments in
339 * question. Return an indication of whether it succeeded or failed
343 get_sampling_arguments(capture_options *capture_opts, const char *arg)
345 gchar *p = NULL, *colonp;
347 colonp = strchr(arg, ':');
354 while (isspace((guchar)*p))
361 if (strcmp(arg, "count") == 0) {
362 if (capture_opts->ifaces->len > 0) {
363 interface_options interface_opts;
365 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
366 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
367 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
368 interface_opts.sampling_param = get_positive_int(p, "sampling count");
369 g_array_append_val(capture_opts->ifaces, interface_opts);
371 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
372 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
374 } else if (strcmp(arg, "timer") == 0) {
375 if (capture_opts->ifaces->len > 0) {
376 interface_options interface_opts;
378 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
379 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
380 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
381 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
382 g_array_append_val(capture_opts->ifaces, interface_opts);
384 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
385 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
393 #ifdef HAVE_PCAP_REMOTE
395 * Given a string of the form "<username>:<password>", as might appear
396 * as an argument to a "-A" option, parse it and set the arguments in
397 * question. Return an indication of whether it succeeded or failed
401 get_auth_arguments(capture_options *capture_opts, const char *arg)
403 gchar *p = NULL, *colonp;
405 colonp = strchr(arg, ':');
412 while (isspace((guchar)*p))
415 if (capture_opts->ifaces->len > 0) {
416 interface_options interface_opts;
418 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
419 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
420 interface_opts.auth_type = CAPTURE_AUTH_PWD;
421 interface_opts.auth_username = g_strdup(arg);
422 interface_opts.auth_password = g_strdup(p);
423 g_array_append_val(capture_opts->ifaces, interface_opts);
425 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
426 capture_opts->default_options.auth_username = g_strdup(arg);
427 capture_opts->default_options.auth_password = g_strdup(p);
435 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
443 interface_options interface_opts;
447 * If the argument is a number, treat it as an index into the list
448 * of adapters, as printed by "tshark -D".
450 * This should be OK on UNIX systems, as interfaces shouldn't have
451 * names that begin with digits. It can be useful on Windows, where
452 * more than one interface can have the same name.
454 adapter_index = strtol(optarg_str_p, &p, 10);
455 if (p != NULL && *p == '\0') {
456 if (adapter_index < 0) {
457 cmdarg_err("The specified adapter index is a negative number");
460 if (adapter_index > INT_MAX) {
461 cmdarg_err("The specified adapter index is too large (greater than %d)",
465 if (adapter_index == 0) {
466 cmdarg_err("There is no interface with that adapter index");
469 if_list = capture_interface_list(&err, &err_str);
470 if (if_list == NULL) {
473 case CANT_GET_INTERFACE_LIST:
474 cmdarg_err("%s", err_str);
478 case NO_INTERFACES_FOUND:
479 cmdarg_err("There are no interfaces on which a capture can be done");
484 if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
485 if (if_info == NULL) {
486 cmdarg_err("There is no interface with that adapter index");
489 interface_opts.name = g_strdup(if_info->name);
490 /* We don't set iface_descr here because doing so requires
491 * capture_ui_utils.c which requires epan/prefs.c which is
492 * probably a bit too much dependency for here...
494 free_interface_list(if_list);
496 interface_opts.name = g_strdup(optarg_str_p);
498 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
499 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
500 interface_opts.snaplen = capture_opts->default_options.snaplen;
501 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
502 interface_opts.linktype = capture_opts->default_options.linktype;
503 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
504 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
505 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
507 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
508 #ifdef HAVE_PCAP_REMOTE
509 interface_opts.src_type = capture_opts->default_options.src_type;
510 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
511 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
512 interface_opts.auth_type = capture_opts->default_options.auth_type;
513 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
514 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
515 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
516 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
517 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
519 #ifdef HAVE_PCAP_SETSAMPLING
520 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
521 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
524 g_array_append_val(capture_opts->ifaces, interface_opts);
530 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
535 case 'a': /* autostop criteria */
536 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
537 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
541 #ifdef HAVE_PCAP_REMOTE
543 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
544 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
549 case 'b': /* Ringbuffer option */
550 capture_opts->multi_files_on = TRUE;
551 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
552 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
556 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
557 case 'B': /* Buffer size */
558 if (capture_opts->ifaces->len > 0) {
559 interface_options interface_opts;
561 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
562 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
563 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
564 g_array_append_val(capture_opts->ifaces, interface_opts);
566 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
570 case 'c': /* Capture n packets */
571 capture_opts->has_autostop_packets = TRUE;
572 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
574 case 'f': /* capture filter */
575 if (capture_opts->ifaces->len > 0) {
576 interface_options interface_opts;
578 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
579 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
580 g_free(interface_opts.cfilter);
581 interface_opts.cfilter = g_strdup(optarg_str_p);
582 g_array_append_val(capture_opts->ifaces, interface_opts);
584 g_free(capture_opts->default_options.cfilter);
585 capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
588 case 'H': /* Hide capture info dialog box */
589 capture_opts->show_info = FALSE;
591 case 'i': /* Use interface x */
592 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
597 #ifdef HAVE_PCAP_CREATE
598 case 'I': /* Capture in monitor mode */
599 if (capture_opts->ifaces->len > 0) {
600 interface_options interface_opts;
602 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
603 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
604 interface_opts.monitor_mode = TRUE;
605 g_array_append_val(capture_opts->ifaces, interface_opts);
607 capture_opts->default_options.monitor_mode = TRUE;
611 case 'k': /* Start capture immediately */
612 *start_capture = TRUE;
614 /*case 'l':*/ /* Automatic scrolling in live capture mode */
615 #ifdef HAVE_PCAP_SETSAMPLING
617 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
618 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
623 case 'n': /* Use pcapng format */
624 capture_opts->use_pcapng = TRUE;
626 case 'p': /* Don't capture in promiscuous mode */
627 if (capture_opts->ifaces->len > 0) {
628 interface_options interface_opts;
630 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
631 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
632 interface_opts.promisc_mode = FALSE;
633 g_array_append_val(capture_opts->ifaces, interface_opts);
635 capture_opts->default_options.promisc_mode = FALSE;
638 case 'P': /* Use pcap format */
639 capture_opts->use_pcapng = FALSE;
641 #ifdef HAVE_PCAP_REMOTE
643 if (capture_opts->ifaces->len > 0) {
644 interface_options interface_opts;
646 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
647 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
648 interface_opts.nocap_rpcap = FALSE;
649 g_array_append_val(capture_opts->ifaces, interface_opts);
651 capture_opts->default_options.nocap_rpcap = FALSE;
655 case 's': /* Set the snapshot (capture) length */
656 snaplen = get_natural_int(optarg_str_p, "snapshot length");
658 * Make a snapshot length of 0 equivalent to the maximum packet
659 * length, mirroring what tcpdump does.
662 snaplen = WTAP_MAX_PACKET_SIZE;
663 if (capture_opts->ifaces->len > 0) {
664 interface_options interface_opts;
666 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
667 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
668 interface_opts.has_snaplen = TRUE;
669 interface_opts.snaplen = snaplen;
670 g_array_append_val(capture_opts->ifaces, interface_opts);
672 capture_opts->default_options.snaplen = snaplen;
673 capture_opts->default_options.has_snaplen = TRUE;
676 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
677 capture_opts->real_time_mode = TRUE;
679 #ifdef HAVE_PCAP_REMOTE
681 if (capture_opts->ifaces->len > 0) {
682 interface_options interface_opts;
684 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
685 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
686 interface_opts.datatx_udp = TRUE;
687 g_array_append_val(capture_opts->ifaces, interface_opts);
689 capture_opts->default_options.datatx_udp = TRUE;
693 case 'w': /* Write to capture file x */
694 capture_opts->saving_to_file = TRUE;
695 g_free(capture_opts->save_file);
696 capture_opts->save_file = g_strdup(optarg_str_p);
697 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
699 case 'g': /* enable group read access on the capture file(s) */
700 capture_opts->group_read_access = TRUE;
702 case 'y': /* Set the pcap data link type */
703 if (capture_opts->ifaces->len > 0) {
704 interface_options interface_opts;
706 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
707 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
708 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
709 if (interface_opts.linktype == -1) {
710 cmdarg_err("The specified data link type \"%s\" isn't valid",
714 g_array_append_val(capture_opts->ifaces, interface_opts);
716 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
717 if (capture_opts->default_options.linktype == -1) {
718 cmdarg_err("The specified data link type \"%s\" isn't valid",
725 /* the caller is responsible to send us only the right opt's */
726 g_assert_not_reached();
733 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
734 gboolean monitor_mode)
737 data_link_info_t *data_link_info;
739 if (caps->can_set_rfmon)
740 fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
741 name, monitor_mode ? "" : "not ");
743 fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
744 for (lt_entry = caps->data_link_types; lt_entry != NULL;
745 lt_entry = g_list_next(lt_entry)) {
746 data_link_info = (data_link_info_t *)lt_entry->data;
747 fprintf_stderr(" %s", data_link_info->name);
748 if (data_link_info->description != NULL)
749 fprintf_stderr(" (%s)", data_link_info->description);
751 fprintf_stderr(" (not supported)");
752 fprintf_stderr("\n");
756 /* Print an ASCII-formatted list of interfaces. */
758 capture_opts_print_interfaces(GList *if_list)
764 i = 1; /* Interface id number */
765 for (if_entry = g_list_first(if_list); if_entry != NULL;
766 if_entry = g_list_next(if_entry)) {
767 if_info = (if_info_t *)if_entry->data;
768 fprintf_stderr("%d. %s", i++, if_info->name);
770 /* Print the description if it exists */
771 if (if_info->description != NULL)
772 fprintf_stderr(" (%s)", if_info->description);
773 fprintf_stderr("\n");
778 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
781 interface_options interface_opts;
783 if (capture_opts->ifaces->len > 0) {
784 for (i = 0; i < capture_opts->ifaces->len; i++) {
785 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
786 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
787 if (interface_opts.snaplen < 1)
788 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
789 else if (interface_opts.snaplen < snaplen_min)
790 interface_opts.snaplen = snaplen_min;
791 g_array_append_val(capture_opts->ifaces, interface_opts);
794 if (capture_opts->default_options.snaplen < 1)
795 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
796 else if (capture_opts->default_options.snaplen < snaplen_min)
797 capture_opts->default_options.snaplen = snaplen_min;
802 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
804 /* Check the value range of the ring_num_files parameter */
805 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
806 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
807 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
808 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
809 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
811 #if RINGBUFFER_MIN_NUM_FILES > 0
812 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
813 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
814 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
819 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
825 interface_options interface_opts;
828 /* Did the user specify an interface to use? */
829 if (capture_opts->ifaces->len == 0) {
830 /* No - is a default specified in the preferences file? */
831 if (capture_device != NULL) {
833 interface_opts.name = g_strdup(capture_device);
834 /* We don't set iface_descr here because doing so requires
835 * capture_ui_utils.c which requires epan/prefs.c which is
836 * probably a bit too much dependency for here...
839 /* No - pick the first one from the list of interfaces. */
840 if_list = capture_interface_list(&err, &err_str);
841 if (if_list == NULL) {
844 case CANT_GET_INTERFACE_LIST:
845 cmdarg_err("%s", err_str);
849 case NO_INTERFACES_FOUND:
850 cmdarg_err("There are no interfaces on which a capture can be done");
855 if_info = (if_info_t *)if_list->data; /* first interface */
856 interface_opts.name = g_strdup(if_info->name);
857 /* We don't set iface_descr here because doing so requires
858 * capture_ui_utils.c which requires epan/prefs.c which is
859 * probably a bit too much dependency for here...
861 free_interface_list(if_list);
863 if (capture_opts->default_options.descr) {
864 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
866 interface_opts.descr = NULL;
868 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
869 interface_opts.snaplen = capture_opts->default_options.snaplen;
870 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
871 interface_opts.linktype = capture_opts->default_options.linktype;
872 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
873 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
874 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
876 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
877 #ifdef HAVE_PCAP_REMOTE
878 interface_opts.src_type = capture_opts->default_options.src_type;
879 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
880 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
881 interface_opts.auth_type = capture_opts->default_options.auth_type;
882 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
883 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
884 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
885 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
886 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
888 #ifdef HAVE_PCAP_SETSAMPLING
889 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
890 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
892 g_array_append_val(capture_opts->ifaces, interface_opts);
901 #define S_IFIFO _S_IFIFO
904 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
907 /* copied from filesystem.c */
908 static int capture_opts_test_for_fifo(const char *path)
912 if (ws_stat64(path, &statb) < 0)
915 if (S_ISFIFO(statb.st_mode))
921 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
927 if (save_file != NULL) {
928 /* We're writing to a capture file. */
929 if (strcmp(save_file, "-") == 0) {
930 /* Writing to stdout. */
931 /* XXX - should we check whether it's a pipe? It's arguably
932 silly to do "-w - >output_file" rather than "-w output_file",
933 but by not checking we might be violating the Principle Of
934 Least Astonishment. */
937 /* not writing to stdout, test for a FIFO (aka named pipe) */
938 err = capture_opts_test_for_fifo(save_file);
941 case ENOENT: /* it doesn't exist, so we'll be creating it,
942 and it won't be a FIFO */
943 case 0: /* found it, but it's not a FIFO */
946 case ESPIPE: /* it is a FIFO */
950 default: /* couldn't stat it */
951 break; /* ignore: later attempt to open */
952 /* will generate a nice msg */
960 #endif /* HAVE_LIBPCAP */