2 * Routines for capture options setting
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 #include <epan/packet.h>
39 #include <epan/prefs.h>
41 #include "capture_opts.h"
42 #include "ringbuffer.h"
44 #include <wsutil/clopts_common.h>
45 #include <wsutil/cmdarg_err.h>
46 #include <wsutil/file_util.h>
48 #include "caputils/capture_ifinfo.h"
49 #include "caputils/capture-pcap-util.h"
51 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
55 capture_opts_init(capture_options *capture_opts)
57 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
58 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
59 capture_opts->num_selected = 0;
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; /* use interface default */
66 capture_opts->default_options.promisc_mode = TRUE;
67 capture_opts->default_options.if_type = IF_WIRED;
69 capture_opts->default_options.extcap = NULL;
70 capture_opts->default_options.extcap_fifo = NULL;
71 capture_opts->default_options.extcap_args = NULL;
72 capture_opts->default_options.extcap_pid = INVALID_EXTCAP_PID;
74 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
75 capture_opts->default_options.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
77 capture_opts->default_options.monitor_mode = FALSE;
78 #ifdef HAVE_PCAP_REMOTE
79 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
80 capture_opts->default_options.remote_host = NULL;
81 capture_opts->default_options.remote_port = NULL;
82 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
83 capture_opts->default_options.auth_username = NULL;
84 capture_opts->default_options.auth_password = NULL;
85 capture_opts->default_options.datatx_udp = FALSE;
86 capture_opts->default_options.nocap_rpcap = TRUE;
87 capture_opts->default_options.nocap_local = FALSE;
89 #ifdef HAVE_PCAP_SETSAMPLING
90 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
91 capture_opts->default_options.sampling_param = 0;
93 capture_opts->saving_to_file = FALSE;
94 capture_opts->save_file = NULL;
95 capture_opts->group_read_access = FALSE;
96 #ifdef PCAP_NG_DEFAULT
97 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
99 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
101 capture_opts->real_time_mode = TRUE;
102 capture_opts->show_info = TRUE;
103 capture_opts->quit_after_cap = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
104 capture_opts->restart = FALSE;
105 capture_opts->orig_save_file = NULL;
107 capture_opts->multi_files_on = FALSE;
108 capture_opts->has_file_duration = FALSE;
109 capture_opts->file_duration = 60; /* 1 min */
110 capture_opts->has_ring_num_files = FALSE;
111 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
113 capture_opts->has_autostop_files = FALSE;
114 capture_opts->autostop_files = 1;
115 capture_opts->has_autostop_packets = FALSE;
116 capture_opts->autostop_packets = 0;
117 capture_opts->has_autostop_filesize = FALSE;
118 capture_opts->autostop_filesize = 1000; /* 1 MB */
119 capture_opts->has_autostop_duration = FALSE;
120 capture_opts->autostop_duration = 60; /* 1 min */
121 capture_opts->capture_comment = NULL;
123 capture_opts->output_to_pipe = FALSE;
124 capture_opts->capture_child = FALSE;
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 :");
135 for (i = 0; i < capture_opts->ifaces->len; i++) {
136 interface_options interface_opts;
138 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
139 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name ? interface_opts.name : "(unspecified)");
140 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts.descr ? interface_opts.descr : "(unspecified)");
141 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts.console_display_name ? interface_opts.console_display_name : "(unspecified)");
142 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter ? interface_opts.cfilter : "(unspecified)");
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");
147 g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts.extcap ? interface_opts.extcap : "(unspecified)");
148 g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts.extcap_fifo ? interface_opts.extcap_fifo : "(unspecified)");
149 g_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts.extcap_pid);
151 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
152 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
154 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
155 #ifdef HAVE_PCAP_REMOTE
156 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
157 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
158 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
160 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
161 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host ? interface_opts.remote_host : "(unspecified)");
162 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port ? interface_opts.remote_port : "(unspecified)");
164 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
165 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
166 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
168 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
169 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username ? interface_opts.auth_username : "(unspecified)");
170 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
172 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
173 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
174 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
176 #ifdef HAVE_PCAP_SETSAMPLING
177 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
178 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
181 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
182 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
183 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
184 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
185 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
186 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
188 g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
189 g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
191 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
192 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
194 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
195 #ifdef HAVE_PCAP_REMOTE
196 g_log(log_domain, log_level, "Capture source[df] : %s",
197 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
198 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
200 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
201 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
202 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
204 g_log(log_domain, log_level, "Authentication[df] : %s",
205 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
206 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
208 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
209 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
210 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
212 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
213 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
214 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
216 #ifdef HAVE_PCAP_SETSAMPLING
217 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
218 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
220 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
221 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
222 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
223 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
224 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
225 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
226 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
228 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
229 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
230 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
232 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
233 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
234 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
235 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
239 * Given a string of the form "<autostop criterion>:<value>", as might appear
240 * as an argument to a "-a" option, parse it and set the criterion in
241 * question. Return an indication of whether it succeeded or failed
245 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
249 colonp = strchr(autostoparg, ':');
257 * Skip over any white space (there probably won't be any, but
258 * as we allow it in the preferences file, we might as well
261 while (g_ascii_isspace(*p))
265 * Put the colon back, so if our caller uses, in an
266 * error message, the string they passed us, the message
272 if (strcmp(autostoparg,"duration") == 0) {
273 capture_opts->has_autostop_duration = TRUE;
274 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
275 } else if (strcmp(autostoparg,"filesize") == 0) {
276 capture_opts->has_autostop_filesize = TRUE;
277 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
278 } else if (strcmp(autostoparg,"files") == 0) {
279 capture_opts->multi_files_on = TRUE;
280 capture_opts->has_autostop_files = TRUE;
281 capture_opts->autostop_files = get_positive_int(p,"autostop files");
285 *colonp = ':'; /* put the colon back */
290 * Given a string of the form "<ring buffer file>:<duration>", as might appear
291 * as an argument to a "-b" option, parse it and set the arguments in
292 * question. Return an indication of whether it succeeded or failed
296 get_ring_arguments(capture_options *capture_opts, const char *arg)
298 gchar *p = NULL, *colonp;
300 colonp = strchr(arg, ':');
308 * Skip over any white space (there probably won't be any, but
309 * as we allow it in the preferences file, we might as well
312 while (g_ascii_isspace(*p))
316 * Put the colon back, so if our caller uses, in an
317 * error message, the string they passed us, the message
324 if (strcmp(arg,"files") == 0) {
325 capture_opts->has_ring_num_files = TRUE;
326 capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
327 } else if (strcmp(arg,"filesize") == 0) {
328 capture_opts->has_autostop_filesize = TRUE;
329 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
330 } else if (strcmp(arg,"duration") == 0) {
331 capture_opts->has_file_duration = TRUE;
332 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
335 *colonp = ':'; /* put the colon back */
339 #ifdef HAVE_PCAP_SETSAMPLING
341 * Given a string of the form "<sampling type>:<value>", as might appear
342 * as an argument to a "-m" option, parse it and set the arguments in
343 * question. Return an indication of whether it succeeded or failed
347 get_sampling_arguments(capture_options *capture_opts, const char *arg)
349 gchar *p = NULL, *colonp;
351 colonp = strchr(arg, ':');
358 while (g_ascii_isspace(*p))
365 if (strcmp(arg, "count") == 0) {
366 if (capture_opts->ifaces->len > 0) {
367 interface_options interface_opts;
369 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
370 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
371 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
372 interface_opts.sampling_param = get_positive_int(p, "sampling count");
373 g_array_append_val(capture_opts->ifaces, interface_opts);
375 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
376 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
378 } else if (strcmp(arg, "timer") == 0) {
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_TIMER;
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 (g_ascii_isspace(*p))
419 if (capture_opts->ifaces->len > 0) {
420 interface_options interface_opts;
422 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
423 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
424 interface_opts.auth_type = CAPTURE_AUTH_PWD;
425 interface_opts.auth_username = g_strdup(arg);
426 interface_opts.auth_password = g_strdup(p);
427 g_array_append_val(capture_opts->ifaces, interface_opts);
429 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
430 capture_opts->default_options.auth_username = g_strdup(arg);
431 capture_opts->default_options.auth_password = g_strdup(p);
439 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
447 interface_options interface_opts;
450 * If the argument is a number, treat it as an index into the list
451 * of adapters, as printed by "tshark -D".
453 * This should be OK on UNIX systems, as interfaces shouldn't have
454 * names that begin with digits. It can be useful on Windows, where
455 * more than one interface can have the same name.
457 adapter_index = strtol(optarg_str_p, &p, 10);
458 if (p != NULL && *p == '\0') {
459 if (adapter_index < 0) {
460 cmdarg_err("The specified adapter index is a negative number");
463 if (adapter_index > INT_MAX) {
464 cmdarg_err("The specified adapter index is too large (greater than %d)",
468 if (adapter_index == 0) {
469 cmdarg_err("There is no interface with that adapter index");
472 if_list = capture_interface_list(&err, &err_str, NULL);
473 if (if_list == NULL) {
475 cmdarg_err("There are no interfaces on which a capture can be done");
477 cmdarg_err("%s", err_str);
482 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
483 if (if_info == NULL) {
484 cmdarg_err("There is no interface with that adapter index");
487 interface_opts.name = g_strdup(if_info->name);
488 if (if_info->friendly_name != NULL) {
490 * We have a friendly name for the interface, so display that
491 * instead of the interface name/guid.
493 * XXX - on UN*X, the interface name is not quite so ugly,
494 * and might be more familiar to users; display them both?
496 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
498 /* fallback to the interface name */
499 interface_opts.console_display_name = g_strdup(if_info->name);
501 interface_opts.if_type = if_info->type;
503 interface_opts.extcap = g_strdup(if_info->extcap);
505 free_interface_list(if_list);
506 } else if (capture_opts->capture_child) {
507 /* In Wireshark capture child mode, thus proper device name is supplied. */
508 /* No need for trying to match it for friendly names. */
509 interface_opts.name = g_strdup(optarg_str_p);
510 interface_opts.console_display_name = g_strdup(optarg_str_p);
511 interface_opts.if_type = capture_opts->default_options.if_type;
513 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
517 * Retrieve the interface list so that we can search for the
518 * specified option amongst both the interface names and the
519 * friendly names and so that we find the friendly name even
520 * if an interface name was specified.
522 * If we can't get the list, just use the specified option as
523 * the interface name, so that the user can try specifying an
524 * interface explicitly for testing purposes.
526 if_list = capture_interface_list(&err, NULL, NULL);
527 if (if_list != NULL) {
528 /* try and do an exact match (case insensitive) */
533 for (if_entry = g_list_first(if_list); if_entry != NULL;
534 if_entry = g_list_next(if_entry))
536 if_info = (if_info_t *)if_entry->data;
537 /* exact name check */
538 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
539 /* exact match on the interface name, use that for displaying etc */
540 interface_opts.name = g_strdup(if_info->name);
542 if (if_info->friendly_name != NULL) {
544 * If we have a friendly name, use that for the
545 * console display name, as it is the basis for
546 * the auto generated temp filename.
548 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
550 interface_opts.console_display_name = g_strdup(if_info->name);
552 interface_opts.if_type = if_info->type;
554 interface_opts.extcap = g_strdup(if_info->extcap);
560 /* exact friendly name check */
561 if (if_info->friendly_name != NULL &&
562 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
563 /* exact match - use the friendly name for display */
564 interface_opts.name = g_strdup(if_info->name);
565 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
566 interface_opts.if_type = if_info->type;
568 interface_opts.extcap = g_strdup(if_info->extcap);
575 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
577 size_t prefix_length;
579 prefix_length = strlen(optarg_str_p);
580 for (if_entry = g_list_first(if_list); if_entry != NULL;
581 if_entry = g_list_next(if_entry))
583 if_info = (if_info_t *)if_entry->data;
585 if (if_info->friendly_name != NULL &&
586 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
587 /* prefix match - use the friendly name for display */
588 interface_opts.name = g_strdup(if_info->name);
589 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
590 interface_opts.if_type = if_info->type;
592 interface_opts.extcap = g_strdup(if_info->extcap);
601 * We didn't find the interface in the list; just use
602 * the specified name, so that, for example, if an
603 * interface doesn't show up in the list for some
604 * reason, the user can try specifying it explicitly
605 * for testing purposes.
607 interface_opts.name = g_strdup(optarg_str_p);
608 interface_opts.console_display_name = g_strdup(optarg_str_p);
609 interface_opts.if_type = capture_opts->default_options.if_type;
611 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
614 free_interface_list(if_list);
616 interface_opts.name = g_strdup(optarg_str_p);
617 interface_opts.console_display_name = g_strdup(optarg_str_p);
618 interface_opts.if_type = capture_opts->default_options.if_type;
620 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
625 /* We don't set iface_descr here because doing so requires
626 * capture_ui_utils.c which requires epan/prefs.c which is
627 * probably a bit too much dependency for here...
629 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
630 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
631 interface_opts.snaplen = capture_opts->default_options.snaplen;
632 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
633 interface_opts.linktype = capture_opts->default_options.linktype;
634 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
636 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
637 interface_opts.extcap_args = NULL;
638 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
640 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
641 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
643 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
644 #ifdef HAVE_PCAP_REMOTE
645 interface_opts.src_type = capture_opts->default_options.src_type;
646 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
647 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
648 interface_opts.auth_type = capture_opts->default_options.auth_type;
649 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
650 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
651 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
652 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
653 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
655 #ifdef HAVE_PCAP_SETSAMPLING
656 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
657 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
660 g_array_append_val(capture_opts->ifaces, interface_opts);
667 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
672 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
673 if (capture_opts->capture_comment) {
674 cmdarg_err("--capture-comment can be set only once per file");
677 capture_opts->capture_comment = g_strdup(optarg_str_p);
679 case 'a': /* autostop criteria */
680 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
681 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
685 #ifdef HAVE_PCAP_REMOTE
687 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
688 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
693 case 'b': /* Ringbuffer option */
694 capture_opts->multi_files_on = TRUE;
695 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
696 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
700 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
701 case 'B': /* Buffer size */
702 if (capture_opts->ifaces->len > 0) {
703 interface_options interface_opts;
705 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
706 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
707 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
708 g_array_append_val(capture_opts->ifaces, interface_opts);
710 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
714 case 'c': /* Capture n packets */
715 capture_opts->has_autostop_packets = TRUE;
716 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
718 case 'f': /* capture filter */
719 if (capture_opts->ifaces->len > 0) {
720 interface_options interface_opts;
722 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
723 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
724 g_free(interface_opts.cfilter);
725 interface_opts.cfilter = g_strdup(optarg_str_p);
726 g_array_append_val(capture_opts->ifaces, interface_opts);
728 g_free(capture_opts->default_options.cfilter);
729 capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
732 case 'g': /* enable group read access on the capture file(s) */
733 capture_opts->group_read_access = TRUE;
735 case 'H': /* Hide capture info dialog box */
736 capture_opts->show_info = FALSE;
738 case 'i': /* Use interface x */
739 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
744 #ifdef HAVE_PCAP_CREATE
745 case 'I': /* Capture in monitor mode */
746 if (capture_opts->ifaces->len > 0) {
747 interface_options interface_opts;
749 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
750 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
751 interface_opts.monitor_mode = TRUE;
752 g_array_append_val(capture_opts->ifaces, interface_opts);
754 capture_opts->default_options.monitor_mode = TRUE;
758 case 'k': /* Start capture immediately */
759 *start_capture = TRUE;
761 /*case 'l':*/ /* Automatic scrolling in live capture mode */
762 #ifdef HAVE_PCAP_SETSAMPLING
764 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
765 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
770 case 'n': /* Use pcapng format */
771 capture_opts->use_pcapng = TRUE;
773 case 'p': /* Don't capture in promiscuous mode */
774 if (capture_opts->ifaces->len > 0) {
775 interface_options interface_opts;
777 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
778 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
779 interface_opts.promisc_mode = FALSE;
780 g_array_append_val(capture_opts->ifaces, interface_opts);
782 capture_opts->default_options.promisc_mode = FALSE;
785 case 'P': /* Use pcap format */
786 capture_opts->use_pcapng = FALSE;
788 #ifdef HAVE_PCAP_REMOTE
790 if (capture_opts->ifaces->len > 0) {
791 interface_options interface_opts;
793 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
794 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
795 interface_opts.nocap_rpcap = FALSE;
796 g_array_append_val(capture_opts->ifaces, interface_opts);
798 capture_opts->default_options.nocap_rpcap = FALSE;
802 case 's': /* Set the snapshot (capture) length */
803 snaplen = get_natural_int(optarg_str_p, "snapshot length");
805 * Make a snapshot length of 0 equivalent to the maximum packet
806 * length, mirroring what tcpdump does.
809 snaplen = WTAP_MAX_PACKET_SIZE;
810 if (capture_opts->ifaces->len > 0) {
811 interface_options interface_opts;
813 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
814 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
815 interface_opts.has_snaplen = TRUE;
816 interface_opts.snaplen = snaplen;
817 g_array_append_val(capture_opts->ifaces, interface_opts);
819 capture_opts->default_options.snaplen = snaplen;
820 capture_opts->default_options.has_snaplen = TRUE;
823 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
824 capture_opts->real_time_mode = TRUE;
826 #ifdef HAVE_PCAP_REMOTE
828 if (capture_opts->ifaces->len > 0) {
829 interface_options interface_opts;
831 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
832 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
833 interface_opts.datatx_udp = TRUE;
834 g_array_append_val(capture_opts->ifaces, interface_opts);
836 capture_opts->default_options.datatx_udp = TRUE;
840 case 'w': /* Write to capture file x */
841 capture_opts->saving_to_file = TRUE;
842 g_free(capture_opts->save_file);
843 capture_opts->save_file = g_strdup(optarg_str_p);
844 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
846 case 'y': /* Set the pcap data link type */
847 if (capture_opts->ifaces->len > 0) {
848 interface_options interface_opts;
850 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
851 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
852 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
853 if (interface_opts.linktype == -1) {
854 cmdarg_err("The specified data link type \"%s\" isn't valid",
858 g_array_append_val(capture_opts->ifaces, interface_opts);
860 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
861 if (capture_opts->default_options.linktype == -1) {
862 cmdarg_err("The specified data link type \"%s\" isn't valid",
869 /* the caller is responsible to send us only the right opt's */
870 g_assert_not_reached();
877 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
878 gboolean monitor_mode)
881 data_link_info_t *data_link_info;
883 if (caps->can_set_rfmon)
884 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
885 name, monitor_mode ? "" : "not ");
887 printf("Data link types of interface %s (use option -y to set):\n", name);
888 for (lt_entry = caps->data_link_types; lt_entry != NULL;
889 lt_entry = g_list_next(lt_entry)) {
890 data_link_info = (data_link_info_t *)lt_entry->data;
891 printf(" %s", data_link_info->name);
892 if (data_link_info->description != NULL)
893 printf(" (%s)", data_link_info->description);
895 printf(" (not supported)");
900 /* Print an ASCII-formatted list of interfaces. */
902 capture_opts_print_interfaces(GList *if_list)
908 i = 1; /* Interface id number */
909 for (if_entry = g_list_first(if_list); if_entry != NULL;
910 if_entry = g_list_next(if_entry)) {
911 if_info = (if_info_t *)if_entry->data;
912 printf("%d. %s", i++, if_info->name);
914 /* Print the interface friendly name, if it exists;
915 if not fall back to vendor description, if it exists. */
916 if (if_info->friendly_name != NULL){
917 printf(" (%s)", if_info->friendly_name);
919 if (if_info->vendor_description != NULL)
920 printf(" (%s)", if_info->vendor_description);
928 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
931 interface_options interface_opts;
933 if (capture_opts->ifaces->len > 0) {
934 for (i = 0; i < capture_opts->ifaces->len; i++) {
935 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
936 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
937 if (interface_opts.snaplen < 1)
938 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
939 else if (interface_opts.snaplen < snaplen_min)
940 interface_opts.snaplen = snaplen_min;
941 g_array_append_val(capture_opts->ifaces, interface_opts);
944 if (capture_opts->default_options.snaplen < 1)
945 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
946 else if (capture_opts->default_options.snaplen < snaplen_min)
947 capture_opts->default_options.snaplen = snaplen_min;
953 capture_opts_trim_ring_num_files(capture_options *capture_opts)
955 /* Check the value range of the ring_num_files parameter */
956 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
957 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
958 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
959 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
960 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
962 #if RINGBUFFER_MIN_NUM_FILES > 0
963 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
964 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
965 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
970 * If no interface was specified explicitly, pick a default.
973 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
974 const char *capture_device)
978 /* Did the user specify an interface to use? */
979 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
980 /* yes they did, return immediately - nothing further to do here */
984 /* No - is a default specified in the preferences file? */
985 if (capture_device != NULL) {
987 status = capture_opts_add_iface_opt(capture_opts, capture_device);
990 /* No default in preferences file, just pick the first interface from the list of interfaces. */
991 return capture_opts_add_iface_opt(capture_opts, "1");
995 #define S_IFIFO _S_IFIFO
998 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1001 /* copied from filesystem.c */
1003 capture_opts_test_for_fifo(const char *path)
1007 if (ws_stat64(path, &statb) < 0)
1010 if (S_ISFIFO(statb.st_mode))
1017 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1023 if (save_file != NULL) {
1024 /* We're writing to a capture file. */
1025 if (strcmp(save_file, "-") == 0) {
1026 /* Writing to stdout. */
1027 /* XXX - should we check whether it's a pipe? It's arguably
1028 silly to do "-w - >output_file" rather than "-w output_file",
1029 but by not checking we might be violating the Principle Of
1030 Least Astonishment. */
1033 /* not writing to stdout, test for a FIFO (aka named pipe) */
1034 err = capture_opts_test_for_fifo(save_file);
1037 case ENOENT: /* it doesn't exist, so we'll be creating it,
1038 and it won't be a FIFO */
1039 case 0: /* found it, but it's not a FIFO */
1042 case ESPIPE: /* it is a FIFO */
1046 default: /* couldn't stat it */
1047 break; /* ignore: later attempt to open */
1048 /* will generate a nice msg */
1057 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1059 interface_options interface_opts;
1061 interface_opts = g_array_index(capture_opts->ifaces, interface_options, if_index);
1062 /* XXX - check if found? */
1064 g_free(interface_opts.name);
1065 g_free(interface_opts.descr);
1066 if (interface_opts.console_display_name != NULL)
1067 g_free(interface_opts.console_display_name);
1068 g_free(interface_opts.cfilter);
1070 g_free(interface_opts.extcap);
1071 g_free(interface_opts.extcap_fifo);
1072 if (interface_opts.extcap_args)
1073 g_hash_table_unref(interface_opts.extcap_args);
1074 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
1075 g_spawn_close_pid(interface_opts.extcap_pid);
1077 #ifdef HAVE_PCAP_REMOTE
1078 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1079 g_free(interface_opts.remote_host);
1080 g_free(interface_opts.remote_port);
1081 g_free(interface_opts.auth_username);
1082 g_free(interface_opts.auth_password);
1085 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1091 * Add all non-hidden selected interfaces in the "all interfaces" list
1092 * to the list of interfaces for the capture.
1095 collect_ifaces(capture_options *capture_opts)
1099 interface_options interface_opts;
1101 /* Empty out the existing list of interfaces. */
1102 for (i = capture_opts->ifaces->len; i != 0; i--)
1103 capture_opts_del_iface(capture_opts, i-1);
1105 /* Now fill the list up again. */
1106 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1107 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1108 if (!device.hidden && device.selected) {
1109 interface_opts.name = g_strdup(device.name);
1110 interface_opts.descr = g_strdup(device.display_name);
1111 interface_opts.console_display_name = g_strdup(device.name);
1112 interface_opts.linktype = device.active_dlt;
1113 interface_opts.cfilter = g_strdup(device.cfilter);
1114 interface_opts.snaplen = device.snaplen;
1115 interface_opts.has_snaplen = device.has_snaplen;
1116 interface_opts.promisc_mode = device.pmode;
1117 interface_opts.if_type = device.if_info.type;
1119 interface_opts.extcap = g_strdup(device.if_info.extcap);
1120 interface_opts.extcap_fifo = NULL;
1121 interface_opts.extcap_args = device.external_cap_args_settings;
1122 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1123 if (interface_opts.extcap_args)
1124 g_hash_table_ref(interface_opts.extcap_args);
1126 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1127 interface_opts.buffer_size = device.buffer;
1129 #ifdef HAVE_PCAP_CREATE
1130 interface_opts.monitor_mode = device.monitor_mode_enabled;
1132 #ifdef HAVE_PCAP_REMOTE
1133 interface_opts.src_type = CAPTURE_IFREMOTE;
1134 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1135 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1136 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1137 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1138 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1139 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1140 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1141 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1143 #ifdef HAVE_PCAP_SETSAMPLING
1144 interface_opts.sampling_method = device.remote_opts.sampling_method;
1145 interface_opts.sampling_param = device.remote_opts.sampling_param;
1147 g_array_append_val(capture_opts->ifaces, interface_opts);
1155 #endif /* HAVE_LIBPCAP */
1158 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1163 * indent-tabs-mode: nil
1166 * vi: set shiftwidth=4 tabstop=8 expandtab:
1167 * :indentSize=4:tabSize=8:noTabs=true: