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 "ui/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_STANDARD;
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;
71 capture_opts->default_options.extcap_control_in = NULL;
72 capture_opts->default_options.extcap_control_out = NULL;
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->restart = FALSE;
104 capture_opts->orig_save_file = NULL;
106 capture_opts->multi_files_on = FALSE;
107 capture_opts->has_file_duration = FALSE;
108 capture_opts->file_duration = 60; /* 1 min */
109 capture_opts->has_file_interval = FALSE;
110 capture_opts->file_interval = 60; /* 1 min */
111 capture_opts->has_ring_num_files = FALSE;
112 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
114 capture_opts->has_autostop_files = FALSE;
115 capture_opts->autostop_files = 1;
116 capture_opts->has_autostop_packets = FALSE;
117 capture_opts->autostop_packets = 0;
118 capture_opts->has_autostop_filesize = FALSE;
119 capture_opts->autostop_filesize = 1000; /* 1 MB */
120 capture_opts->has_autostop_duration = FALSE;
121 capture_opts->autostop_duration = 60; /* 1 min */
122 capture_opts->capture_comment = NULL;
124 capture_opts->output_to_pipe = FALSE;
125 capture_opts->capture_child = FALSE;
129 capture_opts_cleanup(capture_options *capture_opts)
134 if (capture_opts->ifaces) {
135 g_array_free(capture_opts->ifaces, TRUE);
136 capture_opts->ifaces = NULL;
138 if (capture_opts->all_ifaces) {
139 g_array_free(capture_opts->all_ifaces, TRUE);
140 capture_opts->all_ifaces = NULL;
144 /* log content of capture_opts */
146 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
149 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
151 for (i = 0; i < capture_opts->ifaces->len; i++) {
152 interface_options interface_opts;
154 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
155 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name ? interface_opts.name : "(unspecified)");
156 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts.descr ? interface_opts.descr : "(unspecified)");
157 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts.console_display_name ? interface_opts.console_display_name : "(unspecified)");
158 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter ? interface_opts.cfilter : "(unspecified)");
159 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
160 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
161 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
163 g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts.extcap ? interface_opts.extcap : "(unspecified)");
164 g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts.extcap_fifo ? interface_opts.extcap_fifo : "(unspecified)");
165 g_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts.extcap_pid);
167 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
168 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
170 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
171 #ifdef HAVE_PCAP_REMOTE
172 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
173 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
174 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
176 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
177 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host ? interface_opts.remote_host : "(unspecified)");
178 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port ? interface_opts.remote_port : "(unspecified)");
180 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
181 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
182 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
184 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
185 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username ? interface_opts.auth_username : "(unspecified)");
186 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
188 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
189 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
190 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
192 #ifdef HAVE_PCAP_SETSAMPLING
193 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
194 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
197 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
198 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
199 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
200 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
201 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
202 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
204 g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
205 g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
207 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
208 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
210 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
211 #ifdef HAVE_PCAP_REMOTE
212 g_log(log_domain, log_level, "Capture source[df] : %s",
213 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
214 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
216 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
217 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
218 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
220 g_log(log_domain, log_level, "Authentication[df] : %s",
221 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
222 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
224 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
225 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
226 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
228 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
229 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
230 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
232 #ifdef HAVE_PCAP_SETSAMPLING
233 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
234 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
236 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
237 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
238 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
239 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
240 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
241 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
243 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
244 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
245 g_log(log_domain, log_level, "FileInterval (%u) : %u", capture_opts->has_file_interval, capture_opts->file_interval);
246 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
248 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
249 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
250 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
251 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
255 * Given a string of the form "<autostop criterion>:<value>", as might appear
256 * as an argument to a "-a" option, parse it and set the criterion in
257 * question. Return an indication of whether it succeeded or failed
261 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
265 colonp = strchr(autostoparg, ':');
273 * Skip over any white space (there probably won't be any, but
274 * as we allow it in the preferences file, we might as well
277 while (g_ascii_isspace(*p))
281 * Put the colon back, so if our caller uses, in an
282 * error message, the string they passed us, the message
288 if (strcmp(autostoparg,"duration") == 0) {
289 capture_opts->has_autostop_duration = TRUE;
290 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
291 } else if (strcmp(autostoparg,"filesize") == 0) {
292 capture_opts->has_autostop_filesize = TRUE;
293 capture_opts->autostop_filesize = get_nonzero_guint32(p,"autostop filesize");
294 } else if (strcmp(autostoparg,"files") == 0) {
295 capture_opts->multi_files_on = TRUE;
296 capture_opts->has_autostop_files = TRUE;
297 capture_opts->autostop_files = get_positive_int(p,"autostop files");
301 *colonp = ':'; /* put the colon back */
305 static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
309 char* filter_exp = NULL;
311 colonp = strchr(arg, ':');
316 if (strcmp(arg, "predef") == 0) {
319 filterItem = get_filter_list_first(CFILTER_LIST);
320 while (filterItem != NULL) {
321 filter_def *filterDef;
323 filterDef = (filter_def*)filterItem->data;
324 if (strcmp(val, filterDef->name) == 0) {
325 filter_exp = g_strdup(filterDef->strval);
328 filterItem = filterItem->next;
333 if (filter_exp == NULL) {
334 /* No filter expression found yet; fallback to previous implemention
335 and assume the arg contains a filter expression */
337 *colonp = ':'; /* restore colon */
339 filter_exp = g_strdup(arg);
342 if (capture_opts->ifaces->len > 0) {
343 interface_options interface_opts;
345 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
346 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
347 g_free(interface_opts.cfilter);
348 interface_opts.cfilter = filter_exp;
349 g_array_append_val(capture_opts->ifaces, interface_opts);
353 g_free(capture_opts->default_options.cfilter);
354 capture_opts->default_options.cfilter = filter_exp;
360 * Given a string of the form "<ring buffer file>:<duration>", as might appear
361 * as an argument to a "-b" option, parse it and set the arguments in
362 * question. Return an indication of whether it succeeded or failed
366 get_ring_arguments(capture_options *capture_opts, const char *arg)
368 gchar *p = NULL, *colonp;
370 colonp = strchr(arg, ':');
378 * Skip over any white space (there probably won't be any, but
379 * as we allow it in the preferences file, we might as well
382 while (g_ascii_isspace(*p))
386 * Put the colon back, so if our caller uses, in an
387 * error message, the string they passed us, the message
394 if (strcmp(arg,"files") == 0) {
395 capture_opts->has_ring_num_files = TRUE;
396 capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
397 } else if (strcmp(arg,"filesize") == 0) {
398 capture_opts->has_autostop_filesize = TRUE;
399 capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
400 } else if (strcmp(arg,"duration") == 0) {
401 capture_opts->has_file_duration = TRUE;
402 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
403 } else if (strcmp(arg,"interval") == 0) {
404 capture_opts->has_file_interval = TRUE;
405 capture_opts->file_interval = get_positive_int(p, "ring buffer interval");
408 *colonp = ':'; /* put the colon back */
412 #ifdef HAVE_PCAP_SETSAMPLING
414 * Given a string of the form "<sampling type>:<value>", as might appear
415 * as an argument to a "-m" option, parse it and set the arguments in
416 * question. Return an indication of whether it succeeded or failed
420 get_sampling_arguments(capture_options *capture_opts, const char *arg)
422 gchar *p = NULL, *colonp;
424 colonp = strchr(arg, ':');
431 while (g_ascii_isspace(*p))
438 if (strcmp(arg, "count") == 0) {
439 if (capture_opts->ifaces->len > 0) {
440 interface_options interface_opts;
442 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
443 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
444 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
445 interface_opts.sampling_param = get_positive_int(p, "sampling count");
446 g_array_append_val(capture_opts->ifaces, interface_opts);
448 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
449 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
451 } else if (strcmp(arg, "timer") == 0) {
452 if (capture_opts->ifaces->len > 0) {
453 interface_options interface_opts;
455 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
456 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
457 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
458 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
459 g_array_append_val(capture_opts->ifaces, interface_opts);
461 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
462 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
470 #ifdef HAVE_PCAP_REMOTE
472 * Given a string of the form "<username>:<password>", as might appear
473 * as an argument to a "-A" option, parse it and set the arguments in
474 * question. Return an indication of whether it succeeded or failed
478 get_auth_arguments(capture_options *capture_opts, const char *arg)
480 gchar *p = NULL, *colonp;
482 colonp = strchr(arg, ':');
489 while (g_ascii_isspace(*p))
492 if (capture_opts->ifaces->len > 0) {
493 interface_options interface_opts;
495 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
496 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
497 interface_opts.auth_type = CAPTURE_AUTH_PWD;
498 interface_opts.auth_username = g_strdup(arg);
499 interface_opts.auth_password = g_strdup(p);
500 g_array_append_val(capture_opts->ifaces, interface_opts);
502 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
503 capture_opts->default_options.auth_username = g_strdup(arg);
504 capture_opts->default_options.auth_password = g_strdup(p);
512 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
520 interface_options interface_opts;
523 * If the argument is a number, treat it as an index into the list
524 * of adapters, as printed by "tshark -D".
526 * This should be OK on UNIX systems, as interfaces shouldn't have
527 * names that begin with digits. It can be useful on Windows, where
528 * more than one interface can have the same name.
530 adapter_index = strtol(optarg_str_p, &p, 10);
531 if (p != NULL && *p == '\0') {
532 if (adapter_index < 0) {
533 cmdarg_err("The specified adapter index is a negative number");
536 if (adapter_index > INT_MAX) {
537 cmdarg_err("The specified adapter index is too large (greater than %d)",
541 if (adapter_index == 0) {
542 cmdarg_err("There is no interface with that adapter index");
545 if_list = capture_interface_list(&err, &err_str, NULL);
546 if (if_list == NULL) {
548 cmdarg_err("There are no interfaces on which a capture can be done");
550 cmdarg_err("%s", err_str);
555 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
556 if (if_info == NULL) {
557 cmdarg_err("There is no interface with that adapter index");
560 interface_opts.name = g_strdup(if_info->name);
561 if (if_info->friendly_name != NULL) {
563 * We have a friendly name for the interface, so display that
564 * instead of the interface name/guid.
566 * XXX - on UN*X, the interface name is not quite so ugly,
567 * and might be more familiar to users; display them both?
569 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
571 /* fallback to the interface name */
572 interface_opts.console_display_name = g_strdup(if_info->name);
574 interface_opts.if_type = if_info->type;
576 interface_opts.extcap = g_strdup(if_info->extcap);
578 free_interface_list(if_list);
579 } else if (capture_opts->capture_child) {
580 /* In Wireshark capture child mode, thus proper device name is supplied. */
581 /* No need for trying to match it for friendly names. */
582 interface_opts.name = g_strdup(optarg_str_p);
583 interface_opts.console_display_name = g_strdup(optarg_str_p);
584 interface_opts.if_type = capture_opts->default_options.if_type;
586 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
590 * Retrieve the interface list so that we can search for the
591 * specified option amongst both the interface names and the
592 * friendly names and so that we find the friendly name even
593 * if an interface name was specified.
595 * If we can't get the list, just use the specified option as
596 * the interface name, so that the user can try specifying an
597 * interface explicitly for testing purposes.
599 if_list = capture_interface_list(&err, NULL, NULL);
600 if (if_list != NULL) {
601 /* try and do an exact match (case insensitive) */
606 for (if_entry = g_list_first(if_list); if_entry != NULL;
607 if_entry = g_list_next(if_entry))
609 if_info = (if_info_t *)if_entry->data;
610 /* exact name check */
611 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
612 /* exact match on the interface name, use that for displaying etc */
613 interface_opts.name = g_strdup(if_info->name);
615 if (if_info->friendly_name != NULL) {
617 * If we have a friendly name, use that for the
618 * console display name, as it is the basis for
619 * the auto generated temp filename.
621 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
623 interface_opts.console_display_name = g_strdup(if_info->name);
625 interface_opts.if_type = if_info->type;
627 interface_opts.extcap = g_strdup(if_info->extcap);
633 /* exact friendly name check */
634 if (if_info->friendly_name != NULL &&
635 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
636 /* exact match - use the friendly name for display */
637 interface_opts.name = g_strdup(if_info->name);
638 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
639 interface_opts.if_type = if_info->type;
641 interface_opts.extcap = g_strdup(if_info->extcap);
648 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
650 size_t prefix_length;
652 prefix_length = strlen(optarg_str_p);
653 for (if_entry = g_list_first(if_list); if_entry != NULL;
654 if_entry = g_list_next(if_entry))
656 if_info = (if_info_t *)if_entry->data;
658 if (if_info->friendly_name != NULL &&
659 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
660 /* prefix match - use the friendly name for display */
661 interface_opts.name = g_strdup(if_info->name);
662 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
663 interface_opts.if_type = if_info->type;
665 interface_opts.extcap = g_strdup(if_info->extcap);
674 * We didn't find the interface in the list; just use
675 * the specified name, so that, for example, if an
676 * interface doesn't show up in the list for some
677 * reason, the user can try specifying it explicitly
678 * for testing purposes.
680 interface_opts.name = g_strdup(optarg_str_p);
681 interface_opts.console_display_name = g_strdup(optarg_str_p);
682 interface_opts.if_type = capture_opts->default_options.if_type;
684 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
687 free_interface_list(if_list);
689 interface_opts.name = g_strdup(optarg_str_p);
690 interface_opts.console_display_name = g_strdup(optarg_str_p);
691 interface_opts.if_type = capture_opts->default_options.if_type;
693 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
698 /* We don't set iface_descr here because doing so requires
699 * capture_ui_utils.c which requires epan/prefs.c which is
700 * probably a bit too much dependency for here...
702 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
703 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
704 interface_opts.snaplen = capture_opts->default_options.snaplen;
705 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
706 interface_opts.linktype = capture_opts->default_options.linktype;
707 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
709 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
710 interface_opts.extcap_args = NULL;
711 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
712 interface_opts.extcap_userdata = NULL;
713 interface_opts.extcap_control_in = g_strdup(capture_opts->default_options.extcap_control_in);
714 interface_opts.extcap_control_out = g_strdup(capture_opts->default_options.extcap_control_out);
716 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
717 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
719 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
720 #ifdef HAVE_PCAP_REMOTE
721 interface_opts.src_type = capture_opts->default_options.src_type;
722 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
723 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
724 interface_opts.auth_type = capture_opts->default_options.auth_type;
725 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
726 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
727 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
728 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
729 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
731 #ifdef HAVE_PCAP_SETSAMPLING
732 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
733 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
736 g_array_append_val(capture_opts->ifaces, interface_opts);
743 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
748 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
749 if (capture_opts->capture_comment) {
750 cmdarg_err("--capture-comment can be set only once per file");
753 capture_opts->capture_comment = g_strdup(optarg_str_p);
755 case 'a': /* autostop criteria */
756 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
757 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
761 #ifdef HAVE_PCAP_REMOTE
763 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
764 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
769 case 'b': /* Ringbuffer option */
770 capture_opts->multi_files_on = TRUE;
771 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
772 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
776 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
777 case 'B': /* Buffer size */
778 if (capture_opts->ifaces->len > 0) {
779 interface_options interface_opts;
781 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
782 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
783 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
784 g_array_append_val(capture_opts->ifaces, interface_opts);
786 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
790 case 'c': /* Capture n packets */
791 capture_opts->has_autostop_packets = TRUE;
792 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
794 case 'f': /* capture filter */
795 get_filter_arguments(capture_opts, optarg_str_p);
797 case 'g': /* enable group read access on the capture file(s) */
798 capture_opts->group_read_access = TRUE;
800 case 'H': /* Hide capture info dialog box */
801 capture_opts->show_info = FALSE;
803 case 'i': /* Use interface x */
804 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
809 #ifdef HAVE_PCAP_CREATE
810 case 'I': /* Capture in monitor mode */
811 if (capture_opts->ifaces->len > 0) {
812 interface_options interface_opts;
814 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
815 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
816 interface_opts.monitor_mode = TRUE;
817 g_array_append_val(capture_opts->ifaces, interface_opts);
819 capture_opts->default_options.monitor_mode = TRUE;
823 case 'k': /* Start capture immediately */
824 *start_capture = TRUE;
826 /*case 'l':*/ /* Automatic scrolling in live capture mode */
827 #ifdef HAVE_PCAP_SETSAMPLING
829 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
830 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
835 case 'n': /* Use pcapng format */
836 capture_opts->use_pcapng = TRUE;
838 case 'p': /* Don't capture in promiscuous mode */
839 if (capture_opts->ifaces->len > 0) {
840 interface_options interface_opts;
842 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
843 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
844 interface_opts.promisc_mode = FALSE;
845 g_array_append_val(capture_opts->ifaces, interface_opts);
847 capture_opts->default_options.promisc_mode = FALSE;
850 case 'P': /* Use pcap format */
851 capture_opts->use_pcapng = FALSE;
853 #ifdef HAVE_PCAP_REMOTE
855 if (capture_opts->ifaces->len > 0) {
856 interface_options interface_opts;
858 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
859 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
860 interface_opts.nocap_rpcap = FALSE;
861 g_array_append_val(capture_opts->ifaces, interface_opts);
863 capture_opts->default_options.nocap_rpcap = FALSE;
867 case 's': /* Set the snapshot (capture) length */
868 snaplen = get_natural_int(optarg_str_p, "snapshot length");
870 * Make a snapshot length of 0 equivalent to the maximum packet
871 * length, mirroring what tcpdump does.
874 snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
875 if (capture_opts->ifaces->len > 0) {
876 interface_options interface_opts;
878 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
879 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
880 interface_opts.has_snaplen = TRUE;
881 interface_opts.snaplen = snaplen;
882 g_array_append_val(capture_opts->ifaces, interface_opts);
884 capture_opts->default_options.snaplen = snaplen;
885 capture_opts->default_options.has_snaplen = TRUE;
888 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
889 capture_opts->real_time_mode = TRUE;
891 #ifdef HAVE_PCAP_REMOTE
893 if (capture_opts->ifaces->len > 0) {
894 interface_options interface_opts;
896 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
897 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
898 interface_opts.datatx_udp = TRUE;
899 g_array_append_val(capture_opts->ifaces, interface_opts);
901 capture_opts->default_options.datatx_udp = TRUE;
905 case 'w': /* Write to capture file x */
906 capture_opts->saving_to_file = TRUE;
907 g_free(capture_opts->save_file);
908 capture_opts->save_file = g_strdup(optarg_str_p);
909 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
911 case 'y': /* Set the pcap data link type */
912 if (capture_opts->ifaces->len > 0) {
913 interface_options interface_opts;
915 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
916 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
917 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
918 if (interface_opts.linktype == -1) {
919 cmdarg_err("The specified data link type \"%s\" isn't valid",
923 g_array_append_val(capture_opts->ifaces, interface_opts);
925 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
926 if (capture_opts->default_options.linktype == -1) {
927 cmdarg_err("The specified data link type \"%s\" isn't valid",
934 /* the caller is responsible to send us only the right opt's */
935 g_assert_not_reached();
942 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
943 gboolean monitor_mode)
946 data_link_info_t *data_link_info;
948 if (caps->can_set_rfmon)
949 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
950 name, monitor_mode ? "" : "not ");
952 printf("Data link types of interface %s (use option -y to set):\n", name);
953 for (lt_entry = caps->data_link_types; lt_entry != NULL;
954 lt_entry = g_list_next(lt_entry)) {
955 data_link_info = (data_link_info_t *)lt_entry->data;
956 printf(" %s", data_link_info->name);
957 if (data_link_info->description != NULL)
958 printf(" (%s)", data_link_info->description);
960 printf(" (not supported)");
965 /* Print an ASCII-formatted list of interfaces. */
967 capture_opts_print_interfaces(GList *if_list)
973 i = 1; /* Interface id number */
974 for (if_entry = g_list_first(if_list); if_entry != NULL;
975 if_entry = g_list_next(if_entry)) {
976 if_info = (if_info_t *)if_entry->data;
977 printf("%d. %s", i++, if_info->name);
979 /* Print the interface friendly name, if it exists;
980 if not fall back to vendor description, if it exists. */
981 if (if_info->friendly_name != NULL){
982 printf(" (%s)", if_info->friendly_name);
984 if (if_info->vendor_description != NULL)
985 printf(" (%s)", if_info->vendor_description);
993 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
996 interface_options interface_opts;
998 if (capture_opts->ifaces->len > 0) {
999 for (i = 0; i < capture_opts->ifaces->len; i++) {
1000 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
1001 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
1002 if (interface_opts.snaplen < 1)
1003 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1004 else if (interface_opts.snaplen < snaplen_min)
1005 interface_opts.snaplen = snaplen_min;
1006 g_array_append_val(capture_opts->ifaces, interface_opts);
1009 if (capture_opts->default_options.snaplen < 1)
1010 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1011 else if (capture_opts->default_options.snaplen < snaplen_min)
1012 capture_opts->default_options.snaplen = snaplen_min;
1018 capture_opts_trim_ring_num_files(capture_options *capture_opts)
1020 /* Check the value range of the ring_num_files parameter */
1021 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
1022 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
1023 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
1024 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
1025 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
1027 #if RINGBUFFER_MIN_NUM_FILES > 0
1028 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
1029 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
1030 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
1035 * If no interface was specified explicitly, pick a default.
1038 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
1039 const char *capture_device)
1043 /* Did the user specify an interface to use? */
1044 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
1045 /* yes they did, return immediately - nothing further to do here */
1049 /* No - is a default specified in the preferences file? */
1050 if (capture_device != NULL) {
1052 status = capture_opts_add_iface_opt(capture_opts, capture_device);
1055 /* No default in preferences file, just pick the first interface from the list of interfaces. */
1056 return capture_opts_add_iface_opt(capture_opts, "1");
1060 #define S_IFIFO _S_IFIFO
1063 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1066 /* copied from filesystem.c */
1068 capture_opts_test_for_fifo(const char *path)
1072 if (ws_stat64(path, &statb) < 0)
1075 if (S_ISFIFO(statb.st_mode))
1082 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1088 if (save_file != NULL) {
1089 /* We're writing to a capture file. */
1090 if (strcmp(save_file, "-") == 0) {
1091 /* Writing to stdout. */
1092 /* XXX - should we check whether it's a pipe? It's arguably
1093 silly to do "-w - >output_file" rather than "-w output_file",
1094 but by not checking we might be violating the Principle Of
1095 Least Astonishment. */
1098 /* not writing to stdout, test for a FIFO (aka named pipe) */
1099 err = capture_opts_test_for_fifo(save_file);
1102 case ENOENT: /* it doesn't exist, so we'll be creating it,
1103 and it won't be a FIFO */
1104 case 0: /* found it, but it's not a FIFO */
1107 case ESPIPE: /* it is a FIFO */
1111 default: /* couldn't stat it */
1112 break; /* ignore: later attempt to open */
1113 /* will generate a nice msg */
1122 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1124 interface_options interface_opts;
1126 interface_opts = g_array_index(capture_opts->ifaces, interface_options, if_index);
1127 /* XXX - check if found? */
1129 g_free(interface_opts.name);
1130 g_free(interface_opts.descr);
1131 if (interface_opts.console_display_name != NULL)
1132 g_free(interface_opts.console_display_name);
1133 g_free(interface_opts.cfilter);
1135 g_free(interface_opts.extcap);
1136 g_free(interface_opts.extcap_fifo);
1137 if (interface_opts.extcap_args)
1138 g_hash_table_unref(interface_opts.extcap_args);
1139 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
1140 g_spawn_close_pid(interface_opts.extcap_pid);
1141 g_free(interface_opts.extcap_userdata);
1142 g_free(interface_opts.extcap_control_in);
1143 g_free(interface_opts.extcap_control_out);
1145 #ifdef HAVE_PCAP_REMOTE
1146 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1147 g_free(interface_opts.remote_host);
1148 g_free(interface_opts.remote_port);
1149 g_free(interface_opts.auth_username);
1150 g_free(interface_opts.auth_password);
1153 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1159 * Add all non-hidden selected interfaces in the "all interfaces" list
1160 * to the list of interfaces for the capture.
1163 collect_ifaces(capture_options *capture_opts)
1167 interface_options interface_opts;
1169 /* Empty out the existing list of interfaces. */
1170 for (i = capture_opts->ifaces->len; i != 0; i--)
1171 capture_opts_del_iface(capture_opts, i-1);
1173 /* Now fill the list up again. */
1174 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1175 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1176 if (!device.hidden && device.selected) {
1177 interface_opts.name = g_strdup(device.name);
1178 interface_opts.descr = g_strdup(device.display_name);
1179 interface_opts.console_display_name = g_strdup(device.name);
1180 interface_opts.linktype = device.active_dlt;
1181 interface_opts.cfilter = g_strdup(device.cfilter);
1182 interface_opts.snaplen = device.snaplen;
1183 interface_opts.has_snaplen = device.has_snaplen;
1184 interface_opts.promisc_mode = device.pmode;
1185 interface_opts.if_type = device.if_info.type;
1187 interface_opts.extcap = g_strdup(device.if_info.extcap);
1188 interface_opts.extcap_fifo = NULL;
1189 interface_opts.extcap_userdata = NULL;
1190 interface_opts.extcap_args = device.external_cap_args_settings;
1191 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1192 if (interface_opts.extcap_args)
1193 g_hash_table_ref(interface_opts.extcap_args);
1194 interface_opts.extcap_userdata = NULL;
1195 interface_opts.extcap_control_in = NULL;
1196 interface_opts.extcap_control_out = NULL;
1198 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1199 interface_opts.buffer_size = device.buffer;
1201 #ifdef HAVE_PCAP_CREATE
1202 interface_opts.monitor_mode = device.monitor_mode_enabled;
1204 #ifdef HAVE_PCAP_REMOTE
1205 interface_opts.src_type = CAPTURE_IFREMOTE;
1206 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1207 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1208 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1209 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1210 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1211 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1212 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1213 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1215 #ifdef HAVE_PCAP_SETSAMPLING
1216 interface_opts.sampling_method = device.remote_opts.sampling_method;
1217 interface_opts.sampling_param = device.remote_opts.sampling_param;
1219 g_array_append_val(capture_opts->ifaces, interface_opts);
1227 capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
1229 link_row* e = (link_row*)elem;
1236 capture_opts_free_interface_t_addrs(gpointer elem, gpointer unused _U_)
1242 capture_opts_free_interface_t(interface_t *device)
1244 if (device != NULL) {
1245 g_free(device->name);
1246 g_free(device->display_name);
1247 g_free(device->friendly_name);
1248 g_free(device->addresses);
1249 g_free(device->cfilter);
1250 g_list_foreach(device->links,
1251 capture_opts_free_interface_t_links, NULL);
1252 g_list_free(device->links);
1253 #ifdef HAVE_PCAP_REMOTE
1254 g_free(device->remote_opts.remote_host_opts.remote_host);
1255 g_free(device->remote_opts.remote_host_opts.remote_port);
1256 g_free(device->remote_opts.remote_host_opts.auth_username);
1257 g_free(device->remote_opts.remote_host_opts.auth_password);
1259 g_free(device->if_info.name);
1260 g_free(device->if_info.friendly_name);
1261 g_free(device->if_info.vendor_description);
1262 g_slist_foreach(device->if_info.addrs,
1263 capture_opts_free_interface_t_addrs, NULL);
1264 g_slist_free(device->if_info.addrs);
1266 g_free(device->if_info.extcap);
1271 #endif /* HAVE_LIBPCAP */
1274 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1279 * indent-tabs-mode: nil
1282 * vi: set shiftwidth=4 tabstop=8 expandtab:
1283 * :indentSize=4:tabSize=8:noTabs=true: