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 = NULL;
64 capture_opts->default_options.descr = NULL;
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_SETSAMPLING
74 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
75 capture_opts->default_options.sampling_param = 0;
77 #ifdef HAVE_PCAP_REMOTE
78 capture_opts->src_type = CAPTURE_IFLOCAL;
79 capture_opts->remote_host = NULL;
80 capture_opts->remote_port = NULL;
81 capture_opts->auth_type = CAPTURE_AUTH_NULL;
82 capture_opts->auth_username = NULL;
83 capture_opts->auth_password = NULL;
84 capture_opts->datatx_udp = FALSE;
85 capture_opts->nocap_rpcap = TRUE;
86 capture_opts->nocap_local = FALSE;
88 #ifdef HAVE_PCAP_SETSAMPLING
89 capture_opts->sampling_method = CAPTURE_SAMP_NONE;
90 capture_opts->sampling_param = 0;
92 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
93 capture_opts->buffer_size = 1; /* 1 MB */
95 capture_opts->has_snaplen = FALSE;
96 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
97 infinite, in effect */
98 capture_opts->promisc_mode = TRUE; /* promiscuous mode is the default */
99 capture_opts->monitor_mode = FALSE;
100 capture_opts->linktype = -1; /* the default linktype */
101 capture_opts->saving_to_file = FALSE;
102 capture_opts->save_file = NULL;
103 capture_opts->group_read_access = FALSE;
104 capture_opts->use_pcapng = FALSE; /* the default is pcap */
105 capture_opts->real_time_mode = TRUE;
106 capture_opts->show_info = TRUE;
107 capture_opts->quit_after_cap = FALSE;
108 capture_opts->restart = FALSE;
110 capture_opts->multi_files_on = FALSE;
111 capture_opts->has_file_duration = FALSE;
112 capture_opts->file_duration = 60; /* 1 min */
113 capture_opts->has_ring_num_files = FALSE;
114 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
116 capture_opts->has_autostop_files = FALSE;
117 capture_opts->autostop_files = 1;
118 capture_opts->has_autostop_packets = FALSE;
119 capture_opts->autostop_packets = 0;
120 capture_opts->has_autostop_filesize = FALSE;
121 capture_opts->autostop_filesize = 1024; /* 1 MB */
122 capture_opts->has_autostop_duration = FALSE;
123 capture_opts->autostop_duration = 60; /* 1 min */
126 capture_opts->fork_child = -1; /* invalid process handle */
128 capture_opts->signal_pipe_write_fd = -1;
130 capture_opts->state = CAPTURE_STOPPED;
131 capture_opts->output_to_pipe = FALSE;
133 capture_opts->owner = getuid();
134 capture_opts->group = getgid();
139 /* log content of capture_opts */
141 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
144 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
145 g_log(log_domain, log_level, "CFile : %p", capture_opts->cf);
146 g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
148 for (i = 0; i < capture_opts->ifaces->len; i++) {
149 interface_options interface_opts;
151 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
152 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name);
153 g_log(log_domain, log_level, "Interface Descr[%02d]: %s", i, interface_opts.descr);
154 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter);
155 g_log(log_domain, log_level, "Snap length[%02d] : %d", i, interface_opts.snaplen);
156 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
157 g_log(log_domain, log_level, "Promiscous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
158 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
159 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
161 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
162 #ifdef HAVE_PCAP_SETSAMPLING
163 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
164 g_log(log_domain, log_level, "Sampling param.[%02d]: %d", i, interface_opts.sampling_param);
167 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name);
168 g_log(log_domain, log_level, "Interface Descr[df]: %s", capture_opts->default_options.descr);
169 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter);
170 g_log(log_domain, log_level, "Snap length[df] : %d", capture_opts->default_options.snaplen);
171 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
172 g_log(log_domain, log_level, "Promiscous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
173 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
174 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
176 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
177 #ifdef HAVE_PCAP_SETSAMPLING
178 g_log(log_domain, log_level, "Sampling meth. [df]: %d", capture_opts->default_options.sampling_method);
179 g_log(log_domain, log_level, "Sampling param.[df]: %d", capture_opts->default_options.sampling_param);
182 #ifdef HAVE_PCAP_REMOTE
183 g_log(log_domain, log_level, "Capture source : %s",
184 capture_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" :
185 capture_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" :
187 if (capture_opts->src_type == CAPTURE_IFREMOTE) {
188 g_log(log_domain, log_level, "Remote host : %s", capture_opts->remote_host);
189 g_log(log_domain, log_level, "Remote port : %s", capture_opts->remote_port);
191 g_log(log_domain, log_level, "Authentication : %s",
192 capture_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" :
193 capture_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
195 if (capture_opts->auth_type == CAPTURE_AUTH_PWD) {
196 g_log(log_domain, log_level, "Auth username : %s", capture_opts->auth_password);
197 g_log(log_domain, log_level, "Auth password : <hidden>");
199 g_log(log_domain, log_level, "UDP data transfer : %u", capture_opts->datatx_udp);
200 g_log(log_domain, log_level, "No capture RPCAP : %u", capture_opts->nocap_rpcap);
201 g_log(log_domain, log_level, "No capture local : %u", capture_opts->nocap_local);
203 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
204 g_log(log_domain, log_level, "BufferSize : %u (MB)", capture_opts->buffer_size);
206 g_log(log_domain, log_level, "Interface Name : %s", capture_opts->iface);
207 g_log(log_domain, log_level, "Interface Descr. : %s", capture_opts->iface_descr);
208 g_log(log_domain, log_level, "SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
209 g_log(log_domain, log_level, "Promisc : %u", capture_opts->promisc_mode);
210 g_log(log_domain, log_level, "LinkType : %d", capture_opts->linktype);
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 capture_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
363 capture_opts->sampling_param = get_positive_int(p, "sampling count");
364 if (capture_opts->ifaces->len > 0) {
365 interface_options interface_opts;
367 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
368 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
369 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
370 interface_opts.sampling_param = get_positive_int(p, "sampling count");
371 g_array_append_val(capture_opts->ifaces, interface_opts);
373 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
374 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
376 } else if (strcmp(arg, "timer") == 0) {
377 capture_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
378 capture_opts->sampling_param = get_positive_int(p, "sampling timer");
379 if (capture_opts->ifaces->len > 0) {
380 interface_options interface_opts;
382 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
383 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
384 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
385 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
386 g_array_append_val(capture_opts->ifaces, interface_opts);
388 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
389 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
397 #ifdef HAVE_PCAP_REMOTE
399 * Given a string of the form "<username>:<password>", as might appear
400 * as an argument to a "-A" option, parse it and set the arguments in
401 * question. Return an indication of whether it succeeded or failed
405 get_auth_arguments(capture_options *capture_opts, const char *arg)
407 gchar *p = NULL, *colonp;
409 colonp = strchr(arg, ':');
416 while (isspace((guchar)*p))
419 capture_opts->auth_type = CAPTURE_AUTH_PWD;
420 capture_opts->auth_username = g_strdup(arg);
421 capture_opts->auth_password = g_strdup(p);
428 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
436 interface_options interface_opts;
440 * If the argument is a number, treat it as an index into the list
441 * of adapters, as printed by "tshark -D".
443 * This should be OK on UNIX systems, as interfaces shouldn't have
444 * names that begin with digits. It can be useful on Windows, where
445 * more than one interface can have the same name.
447 adapter_index = strtol(optarg_str_p, &p, 10);
448 if (p != NULL && *p == '\0') {
449 if (adapter_index < 0) {
450 cmdarg_err("The specified adapter index is a negative number");
453 if (adapter_index > INT_MAX) {
454 cmdarg_err("The specified adapter index is too large (greater than %d)",
458 if (adapter_index == 0) {
459 cmdarg_err("There is no interface with that adapter index");
462 if_list = capture_interface_list(&err, &err_str);
463 if (if_list == NULL) {
466 case CANT_GET_INTERFACE_LIST:
467 cmdarg_err("%s", err_str);
471 case NO_INTERFACES_FOUND:
472 cmdarg_err("There are no interfaces on which a capture can be done");
477 if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
478 if (if_info == NULL) {
479 cmdarg_err("There is no interface with that adapter index");
482 capture_opts->iface = g_strdup(if_info->name);
483 interface_opts.name = g_strdup(if_info->name);
484 /* We don't set iface_descr here because doing so requires
485 * capture_ui_utils.c which requires epan/prefs.c which is
486 * probably a bit too much dependency for here...
488 free_interface_list(if_list);
490 capture_opts->iface = g_strdup(optarg_str_p);
491 interface_opts.name = g_strdup(optarg_str_p);
493 if (capture_opts->default_options.descr) {
494 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
496 interface_opts.descr = NULL;
498 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
499 interface_opts.snaplen = capture_opts->default_options.snaplen;
500 interface_opts.linktype = capture_opts->default_options.linktype;
501 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
502 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
503 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
505 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
506 #ifdef HAVE_PCAP_SETSAMPLING
507 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
508 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
511 g_array_append_val(capture_opts->ifaces, interface_opts);
517 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
522 case 'a': /* autostop criteria */
523 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
524 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
528 #ifdef HAVE_PCAP_REMOTE
530 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
531 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
536 case 'b': /* Ringbuffer option */
537 capture_opts->multi_files_on = TRUE;
538 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
539 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
543 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
544 case 'B': /* Buffer size */
545 capture_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
546 if (capture_opts->ifaces->len > 0) {
547 interface_options interface_opts;
549 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
550 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
551 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
552 g_array_append_val(capture_opts->ifaces, interface_opts);
554 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
558 case 'c': /* Capture n packets */
559 capture_opts->has_autostop_packets = TRUE;
560 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
562 case 'f': /* capture filter */
563 if ((!capture_opts->use_pcapng) && (capture_opts->has_cfilter)) {
564 cmdarg_err("More than one -f argument specified");
567 capture_opts->has_cfilter = TRUE;
568 g_free(capture_opts->cfilter);
569 capture_opts->cfilter = g_strdup(optarg_str_p);
570 if (capture_opts->ifaces->len > 0) {
571 interface_options interface_opts;
573 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
574 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
575 g_free(interface_opts.cfilter);
576 interface_opts.cfilter = g_strdup(capture_opts->cfilter);
577 g_array_append_val(capture_opts->ifaces, interface_opts);
579 g_free(capture_opts->default_options.cfilter);
580 capture_opts->default_options.cfilter = g_strdup(capture_opts->cfilter);
583 case 'H': /* Hide capture info dialog box */
584 capture_opts->show_info = FALSE;
586 case 'i': /* Use interface x */
587 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
592 #ifdef HAVE_PCAP_CREATE
593 case 'I': /* Capture in monitor mode */
594 capture_opts->monitor_mode = TRUE;
595 if (capture_opts->ifaces->len > 0) {
596 interface_options interface_opts;
598 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
599 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
600 interface_opts.monitor_mode = TRUE;
601 g_array_append_val(capture_opts->ifaces, interface_opts);
603 capture_opts->default_options.monitor_mode = TRUE;
607 case 'k': /* Start capture immediately */
608 *start_capture = TRUE;
610 /*case 'l':*/ /* Automatic scrolling in live capture mode */
611 #ifdef HAVE_PCAP_SETSAMPLING
613 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
614 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
619 case 'n': /* Use pcapng format */
620 capture_opts->use_pcapng = TRUE;
622 case 'p': /* Don't capture in promiscuous mode */
623 capture_opts->promisc_mode = FALSE;
624 if (capture_opts->ifaces->len > 0) {
625 interface_options interface_opts;
627 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
628 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
629 interface_opts.promisc_mode = FALSE;
630 g_array_append_val(capture_opts->ifaces, interface_opts);
632 capture_opts->default_options.promisc_mode = FALSE;
635 case 'Q': /* Quit after capture (just capture to file) */
636 capture_opts->quit_after_cap = TRUE;
637 *start_capture = TRUE; /*** -Q implies -k !! ***/
639 #ifdef HAVE_PCAP_REMOTE
641 capture_opts->nocap_rpcap = FALSE;
644 case 's': /* Set the snapshot (capture) length */
645 capture_opts->has_snaplen = TRUE;
646 capture_opts->snaplen = get_natural_int(optarg_str_p, "snapshot length");
648 * Make a snapshot length of 0 equivalent to the maximum packet
649 * length, mirroring what tcpdump does.
651 if (capture_opts->snaplen == 0)
652 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
653 if (capture_opts->ifaces->len > 0) {
654 interface_options interface_opts;
656 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
657 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
658 interface_opts.snaplen = capture_opts->snaplen;
659 g_array_append_val(capture_opts->ifaces, interface_opts);
661 capture_opts->default_options.snaplen = capture_opts->snaplen;
664 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
665 capture_opts->real_time_mode = TRUE;
667 #ifdef HAVE_PCAP_REMOTE
669 capture_opts->datatx_udp = TRUE;
672 case 'w': /* Write to capture file x */
673 capture_opts->saving_to_file = TRUE;
674 g_free(capture_opts->save_file);
675 #if defined _WIN32 && GLIB_CHECK_VERSION(2,6,0)
676 /* since GLib 2.6, we need to convert filenames to utf8 for Win32 */
677 capture_opts->save_file = g_locale_to_utf8(optarg_str_p, -1, NULL, NULL, NULL);
679 capture_opts->save_file = g_strdup(optarg_str_p);
681 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
683 case 'g': /* enable group read access on the capture file(s) */
684 capture_opts->group_read_access = TRUE;
686 case 'y': /* Set the pcap data link type */
687 capture_opts->linktype = linktype_name_to_val(optarg_str_p);
688 if (capture_opts->linktype == -1) {
689 cmdarg_err("The specified data link type \"%s\" isn't valid",
693 if (capture_opts->ifaces->len > 0) {
694 interface_options interface_opts;
696 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
697 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
698 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
699 g_array_append_val(capture_opts->ifaces, interface_opts);
701 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
705 /* the caller is responsible to send us only the right opt's */
706 g_assert_not_reached();
713 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
714 gboolean monitor_mode)
717 data_link_info_t *data_link_info;
719 if (caps->can_set_rfmon)
720 fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
721 name, monitor_mode ? "" : "not ");
723 fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
724 for (lt_entry = caps->data_link_types; lt_entry != NULL;
725 lt_entry = g_list_next(lt_entry)) {
726 data_link_info = (data_link_info_t *)lt_entry->data;
727 fprintf_stderr(" %s", data_link_info->name);
728 if (data_link_info->description != NULL)
729 fprintf_stderr(" (%s)", data_link_info->description);
731 fprintf_stderr(" (not supported)");
732 fprintf_stderr("\n");
736 /* Print an ASCII-formatted list of interfaces. */
738 capture_opts_print_interfaces(GList *if_list)
744 i = 1; /* Interface id number */
745 for (if_entry = g_list_first(if_list); if_entry != NULL;
746 if_entry = g_list_next(if_entry)) {
747 if_info = (if_info_t *)if_entry->data;
748 fprintf_stderr("%d. %s", i++, if_info->name);
750 /* Print the description if it exists */
751 if (if_info->description != NULL)
752 fprintf_stderr(" (%s)", if_info->description);
753 fprintf_stderr("\n");
758 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
761 interface_options interface_opts;
763 if (capture_opts->snaplen < 1)
764 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
765 else if (capture_opts->snaplen < snaplen_min)
766 capture_opts->snaplen = snaplen_min;
768 for (i = 0; i < capture_opts->ifaces->len; i++) {
769 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
770 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
771 if (interface_opts.snaplen < 1)
772 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
773 else if (interface_opts.snaplen < snaplen_min)
774 interface_opts.snaplen = snaplen_min;
775 g_array_append_val(capture_opts->ifaces, interface_opts);
780 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
782 /* Check the value range of the ring_num_files parameter */
783 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
784 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
785 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
786 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
787 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
789 #if RINGBUFFER_MIN_NUM_FILES > 0
790 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
791 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
792 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
797 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
803 interface_options interface_opts;
806 /* Did the user specify an interface to use? */
807 if (capture_opts->ifaces->len == 0) {
808 /* No - is a default specified in the preferences file? */
809 if (capture_device != NULL) {
811 capture_opts->iface = g_strdup(capture_device);
812 interface_opts.name = g_strdup(capture_device);
813 /* We don't set iface_descr here because doing so requires
814 * capture_ui_utils.c which requires epan/prefs.c which is
815 * probably a bit too much dependency for here...
818 /* No - pick the first one from the list of interfaces. */
819 if_list = capture_interface_list(&err, &err_str);
820 if (if_list == NULL) {
823 case CANT_GET_INTERFACE_LIST:
824 cmdarg_err("%s", err_str);
828 case NO_INTERFACES_FOUND:
829 cmdarg_err("There are no interfaces on which a capture can be done");
834 if_info = (if_info_t *)if_list->data; /* first interface */
835 capture_opts->iface = g_strdup(if_info->name);
836 interface_opts.name = g_strdup(if_info->name);
837 /* We don't set iface_descr here because doing so requires
838 * capture_ui_utils.c which requires epan/prefs.c which is
839 * probably a bit too much dependency for here...
841 free_interface_list(if_list);
843 if (capture_opts->default_options.descr) {
844 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
846 interface_opts.descr = NULL;
848 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
849 interface_opts.snaplen = capture_opts->default_options.snaplen;
850 interface_opts.linktype = capture_opts->default_options.linktype;
851 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
852 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
853 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
855 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
856 #ifdef HAVE_PCAP_SETSAMPLING
857 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
858 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
860 g_array_append_val(capture_opts->ifaces, interface_opts);
869 #define S_IFIFO _S_IFIFO
872 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
875 /* copied from filesystem.c */
876 static int capture_opts_test_for_fifo(const char *path)
880 if (ws_stat64(path, &statb) < 0)
883 if (S_ISFIFO(statb.st_mode))
889 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
895 if (save_file != NULL) {
896 /* We're writing to a capture file. */
897 if (strcmp(save_file, "-") == 0) {
898 /* Writing to stdout. */
899 /* XXX - should we check whether it's a pipe? It's arguably
900 silly to do "-w - >output_file" rather than "-w output_file",
901 but by not checking we might be violating the Principle Of
902 Least Astonishment. */
905 /* not writing to stdout, test for a FIFO (aka named pipe) */
906 err = capture_opts_test_for_fifo(save_file);
909 case ENOENT: /* it doesn't exist, so we'll be creating it,
910 and it won't be a FIFO */
911 case 0: /* found it, but it's not a FIFO */
914 case ESPIPE: /* it is a FIFO */
918 default: /* couldn't stat it */
919 break; /* ignore: later attempt to open */
920 /* will generate a nice msg */
928 #endif /* HAVE_LIBPCAP */