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.
36 #include "capture_opts.h"
37 #include "ringbuffer.h"
39 #include <wsutil/clopts_common.h>
40 #include <wsutil/cmdarg_err.h>
41 #include <wsutil/file_util.h>
43 #include "caputils/capture_ifinfo.h"
44 #include "caputils/capture-pcap-util.h"
46 #include "filter_files.h"
48 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
52 capture_opts_init(capture_options *capture_opts)
54 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
55 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
56 capture_opts->num_selected = 0;
57 capture_opts->default_options.name = NULL;
58 capture_opts->default_options.descr = NULL;
59 capture_opts->default_options.cfilter = NULL;
60 capture_opts->default_options.has_snaplen = FALSE;
61 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
62 capture_opts->default_options.linktype = -1; /* use interface default */
63 capture_opts->default_options.promisc_mode = TRUE;
64 capture_opts->default_options.if_type = IF_WIRED;
66 capture_opts->default_options.extcap = NULL;
67 capture_opts->default_options.extcap_fifo = NULL;
68 capture_opts->default_options.extcap_args = NULL;
69 capture_opts->default_options.extcap_userdata = NULL;
70 capture_opts->default_options.extcap_pid = INVALID_EXTCAP_PID;
72 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
73 capture_opts->default_options.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
75 capture_opts->default_options.monitor_mode = FALSE;
76 #ifdef HAVE_PCAP_REMOTE
77 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
78 capture_opts->default_options.remote_host = NULL;
79 capture_opts->default_options.remote_port = NULL;
80 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
81 capture_opts->default_options.auth_username = NULL;
82 capture_opts->default_options.auth_password = NULL;
83 capture_opts->default_options.datatx_udp = FALSE;
84 capture_opts->default_options.nocap_rpcap = TRUE;
85 capture_opts->default_options.nocap_local = FALSE;
87 #ifdef HAVE_PCAP_SETSAMPLING
88 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
89 capture_opts->default_options.sampling_param = 0;
91 capture_opts->saving_to_file = FALSE;
92 capture_opts->save_file = NULL;
93 capture_opts->group_read_access = FALSE;
94 #ifdef PCAP_NG_DEFAULT
95 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
97 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
99 capture_opts->real_time_mode = TRUE;
100 capture_opts->show_info = TRUE;
101 capture_opts->restart = FALSE;
102 capture_opts->orig_save_file = NULL;
104 capture_opts->multi_files_on = FALSE;
105 capture_opts->has_file_duration = FALSE;
106 capture_opts->file_duration = 60; /* 1 min */
107 capture_opts->has_ring_num_files = FALSE;
108 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
110 capture_opts->has_autostop_files = FALSE;
111 capture_opts->autostop_files = 1;
112 capture_opts->has_autostop_packets = FALSE;
113 capture_opts->autostop_packets = 0;
114 capture_opts->has_autostop_filesize = FALSE;
115 capture_opts->autostop_filesize = 1000; /* 1 MB */
116 capture_opts->has_autostop_duration = FALSE;
117 capture_opts->autostop_duration = 60; /* 1 min */
118 capture_opts->capture_comment = NULL;
120 capture_opts->output_to_pipe = FALSE;
121 capture_opts->capture_child = FALSE;
125 capture_opts_cleanup(capture_options *capture_opts)
130 if (capture_opts->ifaces) {
131 g_array_free(capture_opts->ifaces, TRUE);
132 capture_opts->ifaces = NULL;
134 if (capture_opts->all_ifaces) {
135 g_array_free(capture_opts->all_ifaces, TRUE);
136 capture_opts->all_ifaces = NULL;
140 /* log content of capture_opts */
142 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
145 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
147 for (i = 0; i < capture_opts->ifaces->len; i++) {
148 interface_options interface_opts;
150 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
151 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name ? interface_opts.name : "(unspecified)");
152 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts.descr ? interface_opts.descr : "(unspecified)");
153 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts.console_display_name ? interface_opts.console_display_name : "(unspecified)");
154 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter ? interface_opts.cfilter : "(unspecified)");
155 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, 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, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
159 g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts.extcap ? interface_opts.extcap : "(unspecified)");
160 g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts.extcap_fifo ? interface_opts.extcap_fifo : "(unspecified)");
161 g_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts.extcap_pid);
163 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
164 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
166 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
167 #ifdef HAVE_PCAP_REMOTE
168 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
169 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
170 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
172 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
173 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host ? interface_opts.remote_host : "(unspecified)");
174 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port ? interface_opts.remote_port : "(unspecified)");
176 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
177 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
178 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
180 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
181 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username ? interface_opts.auth_username : "(unspecified)");
182 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
184 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
185 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
186 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
188 #ifdef HAVE_PCAP_SETSAMPLING
189 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
190 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
193 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
194 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
195 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
196 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
197 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
198 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
200 g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
201 g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
203 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
204 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
206 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
207 #ifdef HAVE_PCAP_REMOTE
208 g_log(log_domain, log_level, "Capture source[df] : %s",
209 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
210 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
212 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
213 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
214 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
216 g_log(log_domain, log_level, "Authentication[df] : %s",
217 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
218 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
220 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
221 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
222 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
224 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
225 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
226 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
228 #ifdef HAVE_PCAP_SETSAMPLING
229 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
230 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
232 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
233 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
234 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
235 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
236 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
237 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
239 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
240 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
241 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
243 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
244 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
245 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
246 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
250 * Given a string of the form "<autostop criterion>:<value>", as might appear
251 * as an argument to a "-a" option, parse it and set the criterion in
252 * question. Return an indication of whether it succeeded or failed
256 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
260 colonp = strchr(autostoparg, ':');
268 * Skip over any white space (there probably won't be any, but
269 * as we allow it in the preferences file, we might as well
272 while (g_ascii_isspace(*p))
276 * Put the colon back, so if our caller uses, in an
277 * error message, the string they passed us, the message
283 if (strcmp(autostoparg,"duration") == 0) {
284 capture_opts->has_autostop_duration = TRUE;
285 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
286 } else if (strcmp(autostoparg,"filesize") == 0) {
287 capture_opts->has_autostop_filesize = TRUE;
288 capture_opts->autostop_filesize = get_nonzero_guint32(p,"autostop filesize");
289 } else if (strcmp(autostoparg,"files") == 0) {
290 capture_opts->multi_files_on = TRUE;
291 capture_opts->has_autostop_files = TRUE;
292 capture_opts->autostop_files = get_positive_int(p,"autostop files");
296 *colonp = ':'; /* put the colon back */
300 static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
304 char* filter_exp = NULL;
306 colonp = strchr(arg, ':');
311 if (strcmp(arg, "predef") == 0) {
314 filterItem = get_filter_list_first(CFILTER_LIST);
315 while (filterItem != NULL) {
316 filter_def *filterDef;
318 filterDef = (filter_def*)filterItem->data;
319 if (strcmp(val, filterDef->name) == 0) {
320 filter_exp = g_strdup(filterDef->strval);
323 filterItem = filterItem->next;
328 if (filter_exp == NULL) {
329 /* No filter expression found yet; fallback to previous implemention
330 and assume the arg contains a filter expression */
332 *colonp = ':'; /* restore colon */
334 filter_exp = g_strdup(arg);
337 if (capture_opts->ifaces->len > 0) {
338 interface_options interface_opts;
340 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
341 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
342 g_free(interface_opts.cfilter);
343 interface_opts.cfilter = filter_exp;
344 g_array_append_val(capture_opts->ifaces, interface_opts);
348 g_free(capture_opts->default_options.cfilter);
349 capture_opts->default_options.cfilter = filter_exp;
355 * Given a string of the form "<ring buffer file>:<duration>", as might appear
356 * as an argument to a "-b" option, parse it and set the arguments in
357 * question. Return an indication of whether it succeeded or failed
361 get_ring_arguments(capture_options *capture_opts, const char *arg)
363 gchar *p = NULL, *colonp;
365 colonp = strchr(arg, ':');
373 * Skip over any white space (there probably won't be any, but
374 * as we allow it in the preferences file, we might as well
377 while (g_ascii_isspace(*p))
381 * Put the colon back, so if our caller uses, in an
382 * error message, the string they passed us, the message
389 if (strcmp(arg,"files") == 0) {
390 capture_opts->has_ring_num_files = TRUE;
391 capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
392 } else if (strcmp(arg,"filesize") == 0) {
393 capture_opts->has_autostop_filesize = TRUE;
394 capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
395 } else if (strcmp(arg,"duration") == 0) {
396 capture_opts->has_file_duration = TRUE;
397 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
400 *colonp = ':'; /* put the colon back */
404 #ifdef HAVE_PCAP_SETSAMPLING
406 * Given a string of the form "<sampling type>:<value>", as might appear
407 * as an argument to a "-m" option, parse it and set the arguments in
408 * question. Return an indication of whether it succeeded or failed
412 get_sampling_arguments(capture_options *capture_opts, const char *arg)
414 gchar *p = NULL, *colonp;
416 colonp = strchr(arg, ':');
423 while (g_ascii_isspace(*p))
430 if (strcmp(arg, "count") == 0) {
431 if (capture_opts->ifaces->len > 0) {
432 interface_options interface_opts;
434 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
435 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
436 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
437 interface_opts.sampling_param = get_positive_int(p, "sampling count");
438 g_array_append_val(capture_opts->ifaces, interface_opts);
440 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
441 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
443 } else if (strcmp(arg, "timer") == 0) {
444 if (capture_opts->ifaces->len > 0) {
445 interface_options interface_opts;
447 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
448 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
449 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
450 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
451 g_array_append_val(capture_opts->ifaces, interface_opts);
453 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
454 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
462 #ifdef HAVE_PCAP_REMOTE
464 * Given a string of the form "<username>:<password>", as might appear
465 * as an argument to a "-A" option, parse it and set the arguments in
466 * question. Return an indication of whether it succeeded or failed
470 get_auth_arguments(capture_options *capture_opts, const char *arg)
472 gchar *p = NULL, *colonp;
474 colonp = strchr(arg, ':');
481 while (g_ascii_isspace(*p))
484 if (capture_opts->ifaces->len > 0) {
485 interface_options interface_opts;
487 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
488 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
489 interface_opts.auth_type = CAPTURE_AUTH_PWD;
490 interface_opts.auth_username = g_strdup(arg);
491 interface_opts.auth_password = g_strdup(p);
492 g_array_append_val(capture_opts->ifaces, interface_opts);
494 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
495 capture_opts->default_options.auth_username = g_strdup(arg);
496 capture_opts->default_options.auth_password = g_strdup(p);
504 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
512 interface_options interface_opts;
515 * If the argument is a number, treat it as an index into the list
516 * of adapters, as printed by "tshark -D".
518 * This should be OK on UNIX systems, as interfaces shouldn't have
519 * names that begin with digits. It can be useful on Windows, where
520 * more than one interface can have the same name.
522 adapter_index = strtol(optarg_str_p, &p, 10);
523 if (p != NULL && *p == '\0') {
524 if (adapter_index < 0) {
525 cmdarg_err("The specified adapter index is a negative number");
528 if (adapter_index > INT_MAX) {
529 cmdarg_err("The specified adapter index is too large (greater than %d)",
533 if (adapter_index == 0) {
534 cmdarg_err("There is no interface with that adapter index");
537 if_list = capture_interface_list(&err, &err_str, NULL);
538 if (if_list == NULL) {
540 cmdarg_err("There are no interfaces on which a capture can be done");
542 cmdarg_err("%s", err_str);
547 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
548 if (if_info == NULL) {
549 cmdarg_err("There is no interface with that adapter index");
552 interface_opts.name = g_strdup(if_info->name);
553 if (if_info->friendly_name != NULL) {
555 * We have a friendly name for the interface, so display that
556 * instead of the interface name/guid.
558 * XXX - on UN*X, the interface name is not quite so ugly,
559 * and might be more familiar to users; display them both?
561 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
563 /* fallback to the interface name */
564 interface_opts.console_display_name = g_strdup(if_info->name);
566 interface_opts.if_type = if_info->type;
568 interface_opts.extcap = g_strdup(if_info->extcap);
570 free_interface_list(if_list);
571 } else if (capture_opts->capture_child) {
572 /* In Wireshark capture child mode, thus proper device name is supplied. */
573 /* No need for trying to match it for friendly names. */
574 interface_opts.name = g_strdup(optarg_str_p);
575 interface_opts.console_display_name = g_strdup(optarg_str_p);
576 interface_opts.if_type = capture_opts->default_options.if_type;
578 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
582 * Retrieve the interface list so that we can search for the
583 * specified option amongst both the interface names and the
584 * friendly names and so that we find the friendly name even
585 * if an interface name was specified.
587 * If we can't get the list, just use the specified option as
588 * the interface name, so that the user can try specifying an
589 * interface explicitly for testing purposes.
591 if_list = capture_interface_list(&err, NULL, NULL);
592 if (if_list != NULL) {
593 /* try and do an exact match (case insensitive) */
598 for (if_entry = g_list_first(if_list); if_entry != NULL;
599 if_entry = g_list_next(if_entry))
601 if_info = (if_info_t *)if_entry->data;
602 /* exact name check */
603 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
604 /* exact match on the interface name, use that for displaying etc */
605 interface_opts.name = g_strdup(if_info->name);
607 if (if_info->friendly_name != NULL) {
609 * If we have a friendly name, use that for the
610 * console display name, as it is the basis for
611 * the auto generated temp filename.
613 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
615 interface_opts.console_display_name = g_strdup(if_info->name);
617 interface_opts.if_type = if_info->type;
619 interface_opts.extcap = g_strdup(if_info->extcap);
625 /* exact friendly name check */
626 if (if_info->friendly_name != NULL &&
627 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
628 /* exact match - use the friendly name for display */
629 interface_opts.name = g_strdup(if_info->name);
630 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
631 interface_opts.if_type = if_info->type;
633 interface_opts.extcap = g_strdup(if_info->extcap);
640 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
642 size_t prefix_length;
644 prefix_length = strlen(optarg_str_p);
645 for (if_entry = g_list_first(if_list); if_entry != NULL;
646 if_entry = g_list_next(if_entry))
648 if_info = (if_info_t *)if_entry->data;
650 if (if_info->friendly_name != NULL &&
651 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
652 /* prefix match - use the friendly name for display */
653 interface_opts.name = g_strdup(if_info->name);
654 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
655 interface_opts.if_type = if_info->type;
657 interface_opts.extcap = g_strdup(if_info->extcap);
666 * We didn't find the interface in the list; just use
667 * the specified name, so that, for example, if an
668 * interface doesn't show up in the list for some
669 * reason, the user can try specifying it explicitly
670 * for testing purposes.
672 interface_opts.name = g_strdup(optarg_str_p);
673 interface_opts.console_display_name = g_strdup(optarg_str_p);
674 interface_opts.if_type = capture_opts->default_options.if_type;
676 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
679 free_interface_list(if_list);
681 interface_opts.name = g_strdup(optarg_str_p);
682 interface_opts.console_display_name = g_strdup(optarg_str_p);
683 interface_opts.if_type = capture_opts->default_options.if_type;
685 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
690 /* We don't set iface_descr here because doing so requires
691 * capture_ui_utils.c which requires epan/prefs.c which is
692 * probably a bit too much dependency for here...
694 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
695 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
696 interface_opts.snaplen = capture_opts->default_options.snaplen;
697 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
698 interface_opts.linktype = capture_opts->default_options.linktype;
699 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
701 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
702 interface_opts.extcap_args = NULL;
703 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
704 interface_opts.extcap_userdata = NULL;
706 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
707 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
709 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
710 #ifdef HAVE_PCAP_REMOTE
711 interface_opts.src_type = capture_opts->default_options.src_type;
712 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
713 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
714 interface_opts.auth_type = capture_opts->default_options.auth_type;
715 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
716 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
717 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
718 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
719 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
721 #ifdef HAVE_PCAP_SETSAMPLING
722 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
723 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
726 g_array_append_val(capture_opts->ifaces, interface_opts);
733 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
738 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
739 if (capture_opts->capture_comment) {
740 cmdarg_err("--capture-comment can be set only once per file");
743 capture_opts->capture_comment = g_strdup(optarg_str_p);
745 case 'a': /* autostop criteria */
746 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
747 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
751 #ifdef HAVE_PCAP_REMOTE
753 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
754 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
759 case 'b': /* Ringbuffer option */
760 capture_opts->multi_files_on = TRUE;
761 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
762 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
766 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
767 case 'B': /* Buffer size */
768 if (capture_opts->ifaces->len > 0) {
769 interface_options interface_opts;
771 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
772 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
773 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
774 g_array_append_val(capture_opts->ifaces, interface_opts);
776 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
780 case 'c': /* Capture n packets */
781 capture_opts->has_autostop_packets = TRUE;
782 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
784 case 'f': /* capture filter */
785 get_filter_arguments(capture_opts, optarg_str_p);
787 case 'g': /* enable group read access on the capture file(s) */
788 capture_opts->group_read_access = TRUE;
790 case 'H': /* Hide capture info dialog box */
791 capture_opts->show_info = FALSE;
793 case 'i': /* Use interface x */
794 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
799 #ifdef HAVE_PCAP_CREATE
800 case 'I': /* Capture in monitor mode */
801 if (capture_opts->ifaces->len > 0) {
802 interface_options interface_opts;
804 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
805 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
806 interface_opts.monitor_mode = TRUE;
807 g_array_append_val(capture_opts->ifaces, interface_opts);
809 capture_opts->default_options.monitor_mode = TRUE;
813 case 'k': /* Start capture immediately */
814 *start_capture = TRUE;
816 /*case 'l':*/ /* Automatic scrolling in live capture mode */
817 #ifdef HAVE_PCAP_SETSAMPLING
819 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
820 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
825 case 'n': /* Use pcapng format */
826 capture_opts->use_pcapng = TRUE;
828 case 'p': /* Don't capture in promiscuous mode */
829 if (capture_opts->ifaces->len > 0) {
830 interface_options interface_opts;
832 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
833 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
834 interface_opts.promisc_mode = FALSE;
835 g_array_append_val(capture_opts->ifaces, interface_opts);
837 capture_opts->default_options.promisc_mode = FALSE;
840 case 'P': /* Use pcap format */
841 capture_opts->use_pcapng = FALSE;
843 #ifdef HAVE_PCAP_REMOTE
845 if (capture_opts->ifaces->len > 0) {
846 interface_options interface_opts;
848 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
849 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
850 interface_opts.nocap_rpcap = FALSE;
851 g_array_append_val(capture_opts->ifaces, interface_opts);
853 capture_opts->default_options.nocap_rpcap = FALSE;
857 case 's': /* Set the snapshot (capture) length */
858 snaplen = get_natural_int(optarg_str_p, "snapshot length");
860 * Make a snapshot length of 0 equivalent to the maximum packet
861 * length, mirroring what tcpdump does.
864 snaplen = WTAP_MAX_PACKET_SIZE;
865 if (capture_opts->ifaces->len > 0) {
866 interface_options interface_opts;
868 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
869 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
870 interface_opts.has_snaplen = TRUE;
871 interface_opts.snaplen = snaplen;
872 g_array_append_val(capture_opts->ifaces, interface_opts);
874 capture_opts->default_options.snaplen = snaplen;
875 capture_opts->default_options.has_snaplen = TRUE;
878 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
879 capture_opts->real_time_mode = TRUE;
881 #ifdef HAVE_PCAP_REMOTE
883 if (capture_opts->ifaces->len > 0) {
884 interface_options interface_opts;
886 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
887 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
888 interface_opts.datatx_udp = TRUE;
889 g_array_append_val(capture_opts->ifaces, interface_opts);
891 capture_opts->default_options.datatx_udp = TRUE;
895 case 'w': /* Write to capture file x */
896 capture_opts->saving_to_file = TRUE;
897 g_free(capture_opts->save_file);
898 capture_opts->save_file = g_strdup(optarg_str_p);
899 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
901 case 'y': /* Set the pcap data link type */
902 if (capture_opts->ifaces->len > 0) {
903 interface_options interface_opts;
905 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
906 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
907 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
908 if (interface_opts.linktype == -1) {
909 cmdarg_err("The specified data link type \"%s\" isn't valid",
913 g_array_append_val(capture_opts->ifaces, interface_opts);
915 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
916 if (capture_opts->default_options.linktype == -1) {
917 cmdarg_err("The specified data link type \"%s\" isn't valid",
924 /* the caller is responsible to send us only the right opt's */
925 g_assert_not_reached();
932 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
933 gboolean monitor_mode)
936 data_link_info_t *data_link_info;
938 if (caps->can_set_rfmon)
939 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
940 name, monitor_mode ? "" : "not ");
942 printf("Data link types of interface %s (use option -y to set):\n", name);
943 for (lt_entry = caps->data_link_types; lt_entry != NULL;
944 lt_entry = g_list_next(lt_entry)) {
945 data_link_info = (data_link_info_t *)lt_entry->data;
946 printf(" %s", data_link_info->name);
947 if (data_link_info->description != NULL)
948 printf(" (%s)", data_link_info->description);
950 printf(" (not supported)");
955 /* Print an ASCII-formatted list of interfaces. */
957 capture_opts_print_interfaces(GList *if_list)
963 i = 1; /* Interface id number */
964 for (if_entry = g_list_first(if_list); if_entry != NULL;
965 if_entry = g_list_next(if_entry)) {
966 if_info = (if_info_t *)if_entry->data;
967 printf("%d. %s", i++, if_info->name);
969 /* Print the interface friendly name, if it exists;
970 if not fall back to vendor description, if it exists. */
971 if (if_info->friendly_name != NULL){
972 printf(" (%s)", if_info->friendly_name);
974 if (if_info->vendor_description != NULL)
975 printf(" (%s)", if_info->vendor_description);
983 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
986 interface_options interface_opts;
988 if (capture_opts->ifaces->len > 0) {
989 for (i = 0; i < capture_opts->ifaces->len; i++) {
990 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
991 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
992 if (interface_opts.snaplen < 1)
993 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
994 else if (interface_opts.snaplen < snaplen_min)
995 interface_opts.snaplen = snaplen_min;
996 g_array_append_val(capture_opts->ifaces, interface_opts);
999 if (capture_opts->default_options.snaplen < 1)
1000 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
1001 else if (capture_opts->default_options.snaplen < snaplen_min)
1002 capture_opts->default_options.snaplen = snaplen_min;
1008 capture_opts_trim_ring_num_files(capture_options *capture_opts)
1010 /* Check the value range of the ring_num_files parameter */
1011 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
1012 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
1013 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
1014 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
1015 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
1017 #if RINGBUFFER_MIN_NUM_FILES > 0
1018 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
1019 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
1020 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
1025 * If no interface was specified explicitly, pick a default.
1028 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
1029 const char *capture_device)
1033 /* Did the user specify an interface to use? */
1034 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
1035 /* yes they did, return immediately - nothing further to do here */
1039 /* No - is a default specified in the preferences file? */
1040 if (capture_device != NULL) {
1042 status = capture_opts_add_iface_opt(capture_opts, capture_device);
1045 /* No default in preferences file, just pick the first interface from the list of interfaces. */
1046 return capture_opts_add_iface_opt(capture_opts, "1");
1050 #define S_IFIFO _S_IFIFO
1053 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1056 /* copied from filesystem.c */
1058 capture_opts_test_for_fifo(const char *path)
1062 if (ws_stat64(path, &statb) < 0)
1065 if (S_ISFIFO(statb.st_mode))
1072 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1078 if (save_file != NULL) {
1079 /* We're writing to a capture file. */
1080 if (strcmp(save_file, "-") == 0) {
1081 /* Writing to stdout. */
1082 /* XXX - should we check whether it's a pipe? It's arguably
1083 silly to do "-w - >output_file" rather than "-w output_file",
1084 but by not checking we might be violating the Principle Of
1085 Least Astonishment. */
1088 /* not writing to stdout, test for a FIFO (aka named pipe) */
1089 err = capture_opts_test_for_fifo(save_file);
1092 case ENOENT: /* it doesn't exist, so we'll be creating it,
1093 and it won't be a FIFO */
1094 case 0: /* found it, but it's not a FIFO */
1097 case ESPIPE: /* it is a FIFO */
1101 default: /* couldn't stat it */
1102 break; /* ignore: later attempt to open */
1103 /* will generate a nice msg */
1112 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1114 interface_options interface_opts;
1116 interface_opts = g_array_index(capture_opts->ifaces, interface_options, if_index);
1117 /* XXX - check if found? */
1119 g_free(interface_opts.name);
1120 g_free(interface_opts.descr);
1121 if (interface_opts.console_display_name != NULL)
1122 g_free(interface_opts.console_display_name);
1123 g_free(interface_opts.cfilter);
1125 g_free(interface_opts.extcap);
1126 g_free(interface_opts.extcap_fifo);
1127 if (interface_opts.extcap_args)
1128 g_hash_table_unref(interface_opts.extcap_args);
1129 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
1130 g_spawn_close_pid(interface_opts.extcap_pid);
1131 g_free(interface_opts.extcap_userdata);
1133 #ifdef HAVE_PCAP_REMOTE
1134 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1135 g_free(interface_opts.remote_host);
1136 g_free(interface_opts.remote_port);
1137 g_free(interface_opts.auth_username);
1138 g_free(interface_opts.auth_password);
1141 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1147 * Add all non-hidden selected interfaces in the "all interfaces" list
1148 * to the list of interfaces for the capture.
1151 collect_ifaces(capture_options *capture_opts)
1155 interface_options interface_opts;
1157 /* Empty out the existing list of interfaces. */
1158 for (i = capture_opts->ifaces->len; i != 0; i--)
1159 capture_opts_del_iface(capture_opts, i-1);
1161 /* Now fill the list up again. */
1162 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1163 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1164 if (!device.hidden && device.selected) {
1165 interface_opts.name = g_strdup(device.name);
1166 interface_opts.descr = g_strdup(device.display_name);
1167 interface_opts.console_display_name = g_strdup(device.name);
1168 interface_opts.linktype = device.active_dlt;
1169 interface_opts.cfilter = g_strdup(device.cfilter);
1170 interface_opts.snaplen = device.snaplen;
1171 interface_opts.has_snaplen = device.has_snaplen;
1172 interface_opts.promisc_mode = device.pmode;
1173 interface_opts.if_type = device.if_info.type;
1175 interface_opts.extcap = g_strdup(device.if_info.extcap);
1176 interface_opts.extcap_fifo = NULL;
1177 interface_opts.extcap_userdata = NULL;
1178 interface_opts.extcap_args = device.external_cap_args_settings;
1179 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1180 if (interface_opts.extcap_args)
1181 g_hash_table_ref(interface_opts.extcap_args);
1182 interface_opts.extcap_userdata = NULL;
1184 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1185 interface_opts.buffer_size = device.buffer;
1187 #ifdef HAVE_PCAP_CREATE
1188 interface_opts.monitor_mode = device.monitor_mode_enabled;
1190 #ifdef HAVE_PCAP_REMOTE
1191 interface_opts.src_type = CAPTURE_IFREMOTE;
1192 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1193 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1194 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1195 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1196 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1197 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1198 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1199 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1201 #ifdef HAVE_PCAP_SETSAMPLING
1202 interface_opts.sampling_method = device.remote_opts.sampling_method;
1203 interface_opts.sampling_param = device.remote_opts.sampling_param;
1205 g_array_append_val(capture_opts->ifaces, interface_opts);
1213 capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
1215 link_row* e = (link_row*)elem;
1222 capture_opts_free_interface_t_addrs(gpointer elem, gpointer unused _U_)
1228 capture_opts_free_interface_t(interface_t *device)
1230 if (device != NULL) {
1231 g_free(device->name);
1232 g_free(device->display_name);
1233 g_free(device->friendly_name);
1234 g_free(device->addresses);
1235 g_free(device->cfilter);
1236 g_list_foreach(device->links,
1237 capture_opts_free_interface_t_links, NULL);
1238 g_list_free(device->links);
1239 #ifdef HAVE_PCAP_REMOTE
1240 g_free(device->remote_opts.remote_host_opts.remote_host);
1241 g_free(device->remote_opts.remote_host_opts.remote_port);
1242 g_free(device->remote_opts.remote_host_opts.auth_username);
1243 g_free(device->remote_opts.remote_host_opts.auth_password);
1245 g_free(device->if_info.name);
1246 g_free(device->if_info.friendly_name);
1247 g_free(device->if_info.vendor_description);
1248 g_slist_foreach(device->if_info.addrs,
1249 capture_opts_free_interface_t_addrs, NULL);
1250 g_slist_free(device->if_info.addrs);
1252 g_free(device->if_info.extcap);
1257 #endif /* HAVE_LIBPCAP */
1260 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1265 * indent-tabs-mode: nil
1268 * vi: set shiftwidth=4 tabstop=8 expandtab:
1269 * :indentSize=4:tabSize=8:noTabs=true: