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 = NULL; /* 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 = NULL;
66 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
67 capture_opts->default_options.linktype = -1;
68 capture_opts->default_options.promisc_mode = TRUE;
69 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
70 capture_opts->default_options.buffer_size = 1; /* 1 MB */
72 capture_opts->default_options.monitor_mode = FALSE;
73 #ifdef HAVE_PCAP_REMOTE
74 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
75 capture_opts->default_options.remote_host = NULL;
76 capture_opts->default_options.remote_port = NULL;
77 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
78 capture_opts->default_options.auth_username = NULL;
79 capture_opts->default_options.auth_password = NULL;
80 capture_opts->default_options.datatx_udp = FALSE;
81 capture_opts->default_options.nocap_rpcap = TRUE;
82 capture_opts->default_options.nocap_local = FALSE;
84 #ifdef HAVE_PCAP_SETSAMPLING
85 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
86 capture_opts->default_options.sampling_param = 0;
88 #ifdef HAVE_PCAP_REMOTE
89 capture_opts->src_type = CAPTURE_IFLOCAL;
90 capture_opts->remote_host = NULL;
91 capture_opts->remote_port = NULL;
92 capture_opts->auth_type = CAPTURE_AUTH_NULL;
93 capture_opts->auth_username = NULL;
94 capture_opts->auth_password = NULL;
95 capture_opts->datatx_udp = FALSE;
96 capture_opts->nocap_rpcap = TRUE;
97 capture_opts->nocap_local = FALSE;
99 #ifdef HAVE_PCAP_SETSAMPLING
100 capture_opts->sampling_method = CAPTURE_SAMP_NONE;
101 capture_opts->sampling_param = 0;
103 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
104 capture_opts->buffer_size = 1; /* 1 MB */
106 capture_opts->has_snaplen = FALSE;
107 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
108 infinite, in effect */
109 capture_opts->promisc_mode = TRUE; /* promiscuous mode is the default */
110 capture_opts->monitor_mode = FALSE;
111 capture_opts->linktype = -1; /* the default linktype */
112 capture_opts->saving_to_file = FALSE;
113 capture_opts->save_file = NULL;
114 capture_opts->group_read_access = FALSE;
115 capture_opts->use_pcapng = FALSE; /* the default is pcap */
116 capture_opts->real_time_mode = TRUE;
117 capture_opts->show_info = TRUE;
118 capture_opts->quit_after_cap = FALSE;
119 capture_opts->restart = FALSE;
121 capture_opts->multi_files_on = FALSE;
122 capture_opts->has_file_duration = FALSE;
123 capture_opts->file_duration = 60; /* 1 min */
124 capture_opts->has_ring_num_files = FALSE;
125 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
127 capture_opts->has_autostop_files = FALSE;
128 capture_opts->autostop_files = 1;
129 capture_opts->has_autostop_packets = FALSE;
130 capture_opts->autostop_packets = 0;
131 capture_opts->has_autostop_filesize = FALSE;
132 capture_opts->autostop_filesize = 1024; /* 1 MB */
133 capture_opts->has_autostop_duration = FALSE;
134 capture_opts->autostop_duration = 60; /* 1 min */
137 capture_opts->fork_child = -1; /* invalid process handle */
139 capture_opts->signal_pipe_write_fd = -1;
141 capture_opts->state = CAPTURE_STOPPED;
142 capture_opts->output_to_pipe = FALSE;
144 capture_opts->owner = getuid();
145 capture_opts->group = getgid();
150 /* log content of capture_opts */
152 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
155 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
156 g_log(log_domain, log_level, "CFile : %p", capture_opts->cf);
157 g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
159 for (i = 0; i < capture_opts->ifaces->len; i++) {
160 interface_options interface_opts;
162 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
163 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name);
164 g_log(log_domain, log_level, "Interface Descr[%02d]: %s", i, interface_opts.descr);
165 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter);
166 g_log(log_domain, log_level, "Snap length[%02d] : %d", i, interface_opts.snaplen);
167 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
168 g_log(log_domain, log_level, "Promiscous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
169 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
170 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
172 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
173 #ifdef HAVE_PCAP_REMOTE
174 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
175 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
176 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
178 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
179 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host);
180 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port);
182 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
183 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
184 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
186 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
187 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username);
188 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
190 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
191 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
192 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
194 #ifdef HAVE_PCAP_SETSAMPLING
195 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
196 g_log(log_domain, log_level, "Sampling param.[%02d]: %d", i, interface_opts.sampling_param);
199 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name);
200 g_log(log_domain, log_level, "Interface Descr[df]: %s", capture_opts->default_options.descr);
201 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter);
202 g_log(log_domain, log_level, "Snap length[df] : %d", capture_opts->default_options.snaplen);
203 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
204 g_log(log_domain, log_level, "Promiscous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
205 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
206 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
208 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
209 #ifdef HAVE_PCAP_REMOTE
210 g_log(log_domain, log_level, "Capture source[df] : %s",
211 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
212 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
214 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
215 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host);
216 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port);
218 g_log(log_domain, log_level, "Authentication[df] : %s",
219 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
220 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
222 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
223 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username);
224 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
226 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
227 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
228 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
230 #ifdef HAVE_PCAP_SETSAMPLING
231 g_log(log_domain, log_level, "Sampling meth. [df]: %d", capture_opts->default_options.sampling_method);
232 g_log(log_domain, log_level, "Sampling param.[df]: %d", capture_opts->default_options.sampling_param);
235 #ifdef HAVE_PCAP_REMOTE
236 g_log(log_domain, log_level, "Capture source : %s",
237 capture_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" :
238 capture_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" :
240 if (capture_opts->src_type == CAPTURE_IFREMOTE) {
241 g_log(log_domain, log_level, "Remote host : %s", capture_opts->remote_host);
242 g_log(log_domain, log_level, "Remote port : %s", capture_opts->remote_port);
244 g_log(log_domain, log_level, "Authentication : %s",
245 capture_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" :
246 capture_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
248 if (capture_opts->auth_type == CAPTURE_AUTH_PWD) {
249 g_log(log_domain, log_level, "Auth username : %s", capture_opts->auth_username);
250 g_log(log_domain, log_level, "Auth password : <hidden>");
252 g_log(log_domain, log_level, "UDP data transfer : %u", capture_opts->datatx_udp);
253 g_log(log_domain, log_level, "No capture RPCAP : %u", capture_opts->nocap_rpcap);
254 g_log(log_domain, log_level, "No capture local : %u", capture_opts->nocap_local);
256 #ifdef HAVE_PCAP_SETSAMPLING
257 g_log(log_domain, log_level, "Sampling meth. : %d", capture_opts->sampling_method);
258 g_log(log_domain, log_level, "Sampling param. : %d", capture_opts->sampling_param);
260 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
261 g_log(log_domain, log_level, "BufferSize : %u (MB)", capture_opts->buffer_size);
263 g_log(log_domain, log_level, "Interface Name : %s", capture_opts->iface);
264 g_log(log_domain, log_level, "Interface Descr. : %s", capture_opts->iface_descr);
265 g_log(log_domain, log_level, "SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
266 g_log(log_domain, log_level, "Promisc : %u", capture_opts->promisc_mode);
267 g_log(log_domain, log_level, "LinkType : %d", capture_opts->linktype);
268 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
269 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
270 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
271 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
272 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
273 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
274 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
276 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
277 g_log(log_domain, log_level, "FileDuration (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
278 g_log(log_domain, log_level, "RingNumFiles (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
280 g_log(log_domain, log_level, "AutostopFiles (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
281 g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
282 g_log(log_domain, log_level, "AutostopFilesize(%u): %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
283 g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
285 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
287 g_log(log_domain, log_level, "SignalPipeWrite : %d", capture_opts->signal_pipe_write_fd);
292 * Given a string of the form "<autostop criterion>:<value>", as might appear
293 * as an argument to a "-a" option, parse it and set the criterion in
294 * question. Return an indication of whether it succeeded or failed
298 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
302 colonp = strchr(autostoparg, ':');
310 * Skip over any white space (there probably won't be any, but
311 * as we allow it in the preferences file, we might as well
314 while (isspace((guchar)*p))
318 * Put the colon back, so if our caller uses, in an
319 * error message, the string they passed us, the message
325 if (strcmp(autostoparg,"duration") == 0) {
326 capture_opts->has_autostop_duration = TRUE;
327 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
328 } else if (strcmp(autostoparg,"filesize") == 0) {
329 capture_opts->has_autostop_filesize = TRUE;
330 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
331 } else if (strcmp(autostoparg,"files") == 0) {
332 capture_opts->multi_files_on = TRUE;
333 capture_opts->has_autostop_files = TRUE;
334 capture_opts->autostop_files = get_positive_int(p,"autostop files");
338 *colonp = ':'; /* put the colon back */
343 * Given a string of the form "<ring buffer file>:<duration>", as might appear
344 * as an argument to a "-b" option, parse it and set the arguments in
345 * question. Return an indication of whether it succeeded or failed
349 get_ring_arguments(capture_options *capture_opts, const char *arg)
351 gchar *p = NULL, *colonp;
353 colonp = strchr(arg, ':');
361 * Skip over any white space (there probably won't be any, but
362 * as we allow it in the preferences file, we might as well
365 while (isspace((guchar)*p))
369 * Put the colon back, so if our caller uses, in an
370 * error message, the string they passed us, the message
377 if (strcmp(arg,"files") == 0) {
378 capture_opts->has_ring_num_files = TRUE;
379 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
380 } else if (strcmp(arg,"filesize") == 0) {
381 capture_opts->has_autostop_filesize = TRUE;
382 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
383 } else if (strcmp(arg,"duration") == 0) {
384 capture_opts->has_file_duration = TRUE;
385 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
388 *colonp = ':'; /* put the colon back */
392 #ifdef HAVE_PCAP_SETSAMPLING
394 * Given a string of the form "<sampling type>:<value>", as might appear
395 * as an argument to a "-m" option, parse it and set the arguments in
396 * question. Return an indication of whether it succeeded or failed
400 get_sampling_arguments(capture_options *capture_opts, const char *arg)
402 gchar *p = NULL, *colonp;
404 colonp = strchr(arg, ':');
411 while (isspace((guchar)*p))
418 if (strcmp(arg, "count") == 0) {
419 capture_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
420 capture_opts->sampling_param = get_positive_int(p, "sampling count");
421 if (capture_opts->ifaces->len > 0) {
422 interface_options interface_opts;
424 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
425 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
426 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
427 interface_opts.sampling_param = get_positive_int(p, "sampling count");
428 g_array_append_val(capture_opts->ifaces, interface_opts);
430 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
431 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
433 } else if (strcmp(arg, "timer") == 0) {
434 capture_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
435 capture_opts->sampling_param = get_positive_int(p, "sampling timer");
436 if (capture_opts->ifaces->len > 0) {
437 interface_options interface_opts;
439 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
440 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
441 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
442 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
443 g_array_append_val(capture_opts->ifaces, interface_opts);
445 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
446 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
454 #ifdef HAVE_PCAP_REMOTE
456 * Given a string of the form "<username>:<password>", as might appear
457 * as an argument to a "-A" option, parse it and set the arguments in
458 * question. Return an indication of whether it succeeded or failed
462 get_auth_arguments(capture_options *capture_opts, const char *arg)
464 gchar *p = NULL, *colonp;
466 colonp = strchr(arg, ':');
473 while (isspace((guchar)*p))
476 capture_opts->auth_type = CAPTURE_AUTH_PWD;
477 capture_opts->auth_username = g_strdup(arg);
478 capture_opts->auth_password = g_strdup(p);
479 if (capture_opts->ifaces->len > 0) {
480 interface_options interface_opts;
482 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
483 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
484 interface_opts.auth_type = CAPTURE_AUTH_PWD;
485 interface_opts.auth_username = g_strdup(arg);
486 interface_opts.auth_password = g_strdup(p);
487 g_array_append_val(capture_opts->ifaces, interface_opts);
489 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
490 capture_opts->default_options.auth_username = g_strdup(arg);
491 capture_opts->default_options.auth_password = g_strdup(p);
499 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
507 interface_options interface_opts;
511 * If the argument is a number, treat it as an index into the list
512 * of adapters, as printed by "tshark -D".
514 * This should be OK on UNIX systems, as interfaces shouldn't have
515 * names that begin with digits. It can be useful on Windows, where
516 * more than one interface can have the same name.
518 adapter_index = strtol(optarg_str_p, &p, 10);
519 if (p != NULL && *p == '\0') {
520 if (adapter_index < 0) {
521 cmdarg_err("The specified adapter index is a negative number");
524 if (adapter_index > INT_MAX) {
525 cmdarg_err("The specified adapter index is too large (greater than %d)",
529 if (adapter_index == 0) {
530 cmdarg_err("There is no interface with that adapter index");
533 if_list = capture_interface_list(&err, &err_str);
534 if (if_list == NULL) {
537 case CANT_GET_INTERFACE_LIST:
538 cmdarg_err("%s", err_str);
542 case NO_INTERFACES_FOUND:
543 cmdarg_err("There are no interfaces on which a capture can be done");
548 if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
549 if (if_info == NULL) {
550 cmdarg_err("There is no interface with that adapter index");
553 capture_opts->iface = g_strdup(if_info->name);
554 interface_opts.name = g_strdup(if_info->name);
555 /* We don't set iface_descr here because doing so requires
556 * capture_ui_utils.c which requires epan/prefs.c which is
557 * probably a bit too much dependency for here...
559 free_interface_list(if_list);
561 capture_opts->iface = g_strdup(optarg_str_p);
562 interface_opts.name = g_strdup(optarg_str_p);
564 if (capture_opts->default_options.descr) {
565 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
567 interface_opts.descr = NULL;
569 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
570 interface_opts.snaplen = capture_opts->default_options.snaplen;
571 interface_opts.linktype = capture_opts->default_options.linktype;
572 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
573 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
574 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
576 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
577 #ifdef HAVE_PCAP_REMOTE
578 interface_opts.src_type = capture_opts->default_options.src_type;
579 if (capture_opts->default_options.remote_host) {
580 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
582 interface_opts.remote_host = NULL;
584 if (capture_opts->default_options.remote_port) {
585 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
587 interface_opts.remote_port = NULL;
589 interface_opts.auth_type = capture_opts->default_options.auth_type;
590 if (capture_opts->default_options.auth_username) {
591 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
593 interface_opts.auth_username = NULL;
595 if (capture_opts->default_options.auth_password) {
596 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
598 interface_opts.auth_password = NULL;
600 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
601 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
602 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
604 #ifdef HAVE_PCAP_SETSAMPLING
605 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
606 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
609 g_array_append_val(capture_opts->ifaces, interface_opts);
615 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
620 case 'a': /* autostop criteria */
621 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
622 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
626 #ifdef HAVE_PCAP_REMOTE
628 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
629 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
634 case 'b': /* Ringbuffer option */
635 capture_opts->multi_files_on = TRUE;
636 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
637 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
641 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
642 case 'B': /* Buffer size */
643 capture_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
644 if (capture_opts->ifaces->len > 0) {
645 interface_options interface_opts;
647 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
648 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
649 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
650 g_array_append_val(capture_opts->ifaces, interface_opts);
652 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
656 case 'c': /* Capture n packets */
657 capture_opts->has_autostop_packets = TRUE;
658 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
660 case 'f': /* capture filter */
661 g_free(capture_opts->cfilter);
662 capture_opts->cfilter = g_strdup(optarg_str_p);
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 g_free(interface_opts.cfilter);
669 interface_opts.cfilter = g_strdup(capture_opts->cfilter);
670 g_array_append_val(capture_opts->ifaces, interface_opts);
672 g_free(capture_opts->default_options.cfilter);
673 capture_opts->default_options.cfilter = g_strdup(capture_opts->cfilter);
676 case 'H': /* Hide capture info dialog box */
677 capture_opts->show_info = FALSE;
679 case 'i': /* Use interface x */
680 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
685 #ifdef HAVE_PCAP_CREATE
686 case 'I': /* Capture in monitor mode */
687 capture_opts->monitor_mode = TRUE;
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.monitor_mode = TRUE;
694 g_array_append_val(capture_opts->ifaces, interface_opts);
696 capture_opts->default_options.monitor_mode = TRUE;
700 case 'k': /* Start capture immediately */
701 *start_capture = TRUE;
703 /*case 'l':*/ /* Automatic scrolling in live capture mode */
704 #ifdef HAVE_PCAP_SETSAMPLING
706 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
707 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
712 case 'n': /* Use pcapng format */
713 capture_opts->use_pcapng = TRUE;
715 case 'p': /* Don't capture in promiscuous mode */
716 capture_opts->promisc_mode = FALSE;
717 if (capture_opts->ifaces->len > 0) {
718 interface_options interface_opts;
720 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
721 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
722 interface_opts.promisc_mode = FALSE;
723 g_array_append_val(capture_opts->ifaces, interface_opts);
725 capture_opts->default_options.promisc_mode = FALSE;
728 case 'Q': /* Quit after capture (just capture to file) */
729 capture_opts->quit_after_cap = TRUE;
730 *start_capture = TRUE; /*** -Q implies -k !! ***/
732 #ifdef HAVE_PCAP_REMOTE
734 capture_opts->nocap_rpcap = FALSE;
735 if (capture_opts->ifaces->len > 0) {
736 interface_options interface_opts;
738 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
739 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
740 interface_opts.nocap_rpcap = FALSE;
741 g_array_append_val(capture_opts->ifaces, interface_opts);
743 capture_opts->default_options.nocap_rpcap = FALSE;
747 case 's': /* Set the snapshot (capture) length */
748 capture_opts->has_snaplen = TRUE;
749 capture_opts->snaplen = get_natural_int(optarg_str_p, "snapshot length");
751 * Make a snapshot length of 0 equivalent to the maximum packet
752 * length, mirroring what tcpdump does.
754 if (capture_opts->snaplen == 0)
755 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
756 if (capture_opts->ifaces->len > 0) {
757 interface_options interface_opts;
759 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
760 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
761 interface_opts.snaplen = capture_opts->snaplen;
762 g_array_append_val(capture_opts->ifaces, interface_opts);
764 capture_opts->default_options.snaplen = capture_opts->snaplen;
767 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
768 capture_opts->real_time_mode = TRUE;
770 #ifdef HAVE_PCAP_REMOTE
772 capture_opts->datatx_udp = TRUE;
773 if (capture_opts->ifaces->len > 0) {
774 interface_options interface_opts;
776 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
777 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
778 interface_opts.datatx_udp = TRUE;
779 g_array_append_val(capture_opts->ifaces, interface_opts);
781 capture_opts->default_options.datatx_udp = TRUE;
785 case 'w': /* Write to capture file x */
786 capture_opts->saving_to_file = TRUE;
787 g_free(capture_opts->save_file);
788 #if defined _WIN32 && GLIB_CHECK_VERSION(2,6,0)
789 /* since GLib 2.6, we need to convert filenames to utf8 for Win32 */
790 capture_opts->save_file = g_locale_to_utf8(optarg_str_p, -1, NULL, NULL, NULL);
792 capture_opts->save_file = g_strdup(optarg_str_p);
794 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
796 case 'g': /* enable group read access on the capture file(s) */
797 capture_opts->group_read_access = TRUE;
799 case 'y': /* Set the pcap data link type */
800 capture_opts->linktype = linktype_name_to_val(optarg_str_p);
801 if (capture_opts->linktype == -1) {
802 cmdarg_err("The specified data link type \"%s\" isn't valid",
806 if (capture_opts->ifaces->len > 0) {
807 interface_options interface_opts;
809 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
810 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
811 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
812 g_array_append_val(capture_opts->ifaces, interface_opts);
814 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
818 /* the caller is responsible to send us only the right opt's */
819 g_assert_not_reached();
826 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
827 gboolean monitor_mode)
830 data_link_info_t *data_link_info;
832 if (caps->can_set_rfmon)
833 fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
834 name, monitor_mode ? "" : "not ");
836 fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
837 for (lt_entry = caps->data_link_types; lt_entry != NULL;
838 lt_entry = g_list_next(lt_entry)) {
839 data_link_info = (data_link_info_t *)lt_entry->data;
840 fprintf_stderr(" %s", data_link_info->name);
841 if (data_link_info->description != NULL)
842 fprintf_stderr(" (%s)", data_link_info->description);
844 fprintf_stderr(" (not supported)");
845 fprintf_stderr("\n");
849 /* Print an ASCII-formatted list of interfaces. */
851 capture_opts_print_interfaces(GList *if_list)
857 i = 1; /* Interface id number */
858 for (if_entry = g_list_first(if_list); if_entry != NULL;
859 if_entry = g_list_next(if_entry)) {
860 if_info = (if_info_t *)if_entry->data;
861 fprintf_stderr("%d. %s", i++, if_info->name);
863 /* Print the description if it exists */
864 if (if_info->description != NULL)
865 fprintf_stderr(" (%s)", if_info->description);
866 fprintf_stderr("\n");
871 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
874 interface_options interface_opts;
876 if (capture_opts->snaplen < 1)
877 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
878 else if (capture_opts->snaplen < snaplen_min)
879 capture_opts->snaplen = snaplen_min;
881 for (i = 0; i < capture_opts->ifaces->len; i++) {
882 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
883 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
884 if (interface_opts.snaplen < 1)
885 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
886 else if (interface_opts.snaplen < snaplen_min)
887 interface_opts.snaplen = snaplen_min;
888 g_array_append_val(capture_opts->ifaces, interface_opts);
893 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
895 /* Check the value range of the ring_num_files parameter */
896 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
897 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
898 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
899 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
900 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
902 #if RINGBUFFER_MIN_NUM_FILES > 0
903 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
904 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
905 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
910 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
916 interface_options interface_opts;
919 /* Did the user specify an interface to use? */
920 if (capture_opts->ifaces->len == 0) {
921 /* No - is a default specified in the preferences file? */
922 if (capture_device != NULL) {
924 capture_opts->iface = g_strdup(capture_device);
925 interface_opts.name = g_strdup(capture_device);
926 /* We don't set iface_descr here because doing so requires
927 * capture_ui_utils.c which requires epan/prefs.c which is
928 * probably a bit too much dependency for here...
931 /* No - pick the first one from the list of interfaces. */
932 if_list = capture_interface_list(&err, &err_str);
933 if (if_list == NULL) {
936 case CANT_GET_INTERFACE_LIST:
937 cmdarg_err("%s", err_str);
941 case NO_INTERFACES_FOUND:
942 cmdarg_err("There are no interfaces on which a capture can be done");
947 if_info = (if_info_t *)if_list->data; /* first interface */
948 capture_opts->iface = g_strdup(if_info->name);
949 interface_opts.name = g_strdup(if_info->name);
950 /* We don't set iface_descr here because doing so requires
951 * capture_ui_utils.c which requires epan/prefs.c which is
952 * probably a bit too much dependency for here...
954 free_interface_list(if_list);
956 if (capture_opts->default_options.descr) {
957 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
959 interface_opts.descr = NULL;
961 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
962 interface_opts.snaplen = capture_opts->default_options.snaplen;
963 interface_opts.linktype = capture_opts->default_options.linktype;
964 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
965 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
966 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
968 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
969 #ifdef HAVE_PCAP_REMOTE
970 interface_opts.src_type = capture_opts->default_options.src_type;
971 if (capture_opts->default_options.remote_host) {
972 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
974 interface_opts.remote_host = NULL;
976 if (capture_opts->default_options.remote_port) {
977 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
979 interface_opts.remote_port = NULL;
981 interface_opts.auth_type = capture_opts->default_options.auth_type;
982 if (capture_opts->default_options.auth_username) {
983 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
985 interface_opts.auth_username = NULL;
987 if (capture_opts->default_options.auth_password) {
988 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
990 interface_opts.auth_password = NULL;
992 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
993 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
994 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
996 #ifdef HAVE_PCAP_SETSAMPLING
997 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
998 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
1000 g_array_append_val(capture_opts->ifaces, interface_opts);
1009 #define S_IFIFO _S_IFIFO
1012 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1015 /* copied from filesystem.c */
1016 static int capture_opts_test_for_fifo(const char *path)
1020 if (ws_stat64(path, &statb) < 0)
1023 if (S_ISFIFO(statb.st_mode))
1029 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1035 if (save_file != NULL) {
1036 /* We're writing to a capture file. */
1037 if (strcmp(save_file, "-") == 0) {
1038 /* Writing to stdout. */
1039 /* XXX - should we check whether it's a pipe? It's arguably
1040 silly to do "-w - >output_file" rather than "-w output_file",
1041 but by not checking we might be violating the Principle Of
1042 Least Astonishment. */
1045 /* not writing to stdout, test for a FIFO (aka named pipe) */
1046 err = capture_opts_test_for_fifo(save_file);
1049 case ENOENT: /* it doesn't exist, so we'll be creating it,
1050 and it won't be a FIFO */
1051 case 0: /* found it, but it's not a FIFO */
1054 case ESPIPE: /* it is a FIFO */
1058 default: /* couldn't stat it */
1059 break; /* ignore: later attempt to open */
1060 /* will generate a nice msg */
1068 #endif /* HAVE_LIBPCAP */