2 * Routines for capture options setting
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
24 #include "capture_opts.h"
25 #include "ringbuffer.h"
27 #include <wsutil/clopts_common.h>
28 #include <wsutil/cmdarg_err.h>
29 #include <wsutil/file_util.h>
30 #include <wsutil/ws_pipe.h>
32 #include "caputils/capture_ifinfo.h"
33 #include "caputils/capture-pcap-util.h"
35 #include "ui/filter_files.h"
37 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
41 capture_opts_init(capture_options *capture_opts)
43 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
44 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
45 capture_opts->num_selected = 0;
46 capture_opts->default_options.name = NULL;
47 capture_opts->default_options.descr = NULL;
48 capture_opts->default_options.cfilter = NULL;
49 capture_opts->default_options.has_snaplen = FALSE;
50 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
51 capture_opts->default_options.linktype = -1; /* use interface default */
52 capture_opts->default_options.promisc_mode = TRUE;
53 capture_opts->default_options.if_type = IF_WIRED;
54 capture_opts->default_options.extcap = NULL;
55 capture_opts->default_options.extcap_fifo = NULL;
56 capture_opts->default_options.extcap_args = NULL;
57 capture_opts->default_options.extcap_pipedata = NULL;
58 capture_opts->default_options.extcap_pid = WS_INVALID_PID;
60 capture_opts->default_options.extcap_pipe_h = INVALID_HANDLE_VALUE;
61 capture_opts->default_options.extcap_control_in_h = INVALID_HANDLE_VALUE;
62 capture_opts->default_options.extcap_control_out_h = INVALID_HANDLE_VALUE;
64 capture_opts->default_options.extcap_control_in = NULL;
65 capture_opts->default_options.extcap_control_out = NULL;
66 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
67 capture_opts->default_options.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
69 capture_opts->default_options.monitor_mode = FALSE;
70 #ifdef HAVE_PCAP_REMOTE
71 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
72 capture_opts->default_options.remote_host = NULL;
73 capture_opts->default_options.remote_port = NULL;
74 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
75 capture_opts->default_options.auth_username = NULL;
76 capture_opts->default_options.auth_password = NULL;
77 capture_opts->default_options.datatx_udp = FALSE;
78 capture_opts->default_options.nocap_rpcap = TRUE;
79 capture_opts->default_options.nocap_local = FALSE;
81 #ifdef HAVE_PCAP_SETSAMPLING
82 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
83 capture_opts->default_options.sampling_param = 0;
85 capture_opts->default_options.timestamp_type = NULL;
86 capture_opts->saving_to_file = FALSE;
87 capture_opts->save_file = NULL;
88 capture_opts->group_read_access = FALSE;
89 #ifdef PCAP_NG_DEFAULT
90 capture_opts->use_pcapng = TRUE; /* Save as pcapng by default */
92 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
94 capture_opts->real_time_mode = TRUE;
95 capture_opts->show_info = TRUE;
96 capture_opts->restart = FALSE;
97 capture_opts->orig_save_file = NULL;
99 capture_opts->multi_files_on = FALSE;
100 capture_opts->has_file_duration = FALSE;
101 capture_opts->file_duration = 60; /* 1 min */
102 capture_opts->has_file_interval = FALSE;
103 capture_opts->file_interval = 60; /* 1 min */
104 capture_opts->has_ring_num_files = FALSE;
105 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
107 capture_opts->has_autostop_files = FALSE;
108 capture_opts->autostop_files = 1;
109 capture_opts->has_autostop_packets = FALSE;
110 capture_opts->autostop_packets = 0;
111 capture_opts->has_autostop_filesize = FALSE;
112 capture_opts->autostop_filesize = 1000; /* 1 MB */
113 capture_opts->has_autostop_duration = FALSE;
114 capture_opts->autostop_duration = 60; /* 1 min */
115 capture_opts->capture_comment = NULL;
117 capture_opts->output_to_pipe = FALSE;
118 capture_opts->capture_child = FALSE;
122 capture_opts_cleanup(capture_options *capture_opts)
127 if (capture_opts->ifaces) {
128 while (capture_opts->ifaces->len > 0) {
129 capture_opts_del_iface(capture_opts, 0);
131 g_array_free(capture_opts->ifaces, TRUE);
132 capture_opts->ifaces = NULL;
134 if (capture_opts->all_ifaces) {
135 while (capture_opts->all_ifaces->len > 0) {
136 interface_t *device = &g_array_index(capture_opts->all_ifaces, interface_t, 0);
137 capture_opts_free_interface_t(device);
138 capture_opts->all_ifaces = g_array_remove_index(capture_opts->all_ifaces, 0);
140 g_array_free(capture_opts->all_ifaces, TRUE);
141 capture_opts->all_ifaces = NULL;
143 g_free(capture_opts->save_file);
146 /* log content of capture_opts */
148 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
151 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
153 for (i = 0; i < capture_opts->ifaces->len; i++) {
154 interface_options *interface_opts;
156 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
157 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts->name ? interface_opts->name : "(unspecified)");
158 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts->descr ? interface_opts->descr : "(unspecified)");
159 g_log(log_domain, log_level, "Display name[%02d]: %s", i, interface_opts->display_name ? interface_opts->display_name : "(unspecified)");
160 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts->cfilter ? interface_opts->cfilter : "(unspecified)");
161 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts->has_snaplen, interface_opts->snaplen);
162 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts->linktype);
163 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts->promisc_mode?"TRUE":"FALSE");
164 g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts->extcap ? interface_opts->extcap : "(unspecified)");
165 g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts->extcap_fifo ? interface_opts->extcap_fifo : "(unspecified)");
166 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);
196 g_log(log_domain, log_level, "Timestamp type [%02d] : %s", i, interface_opts->timestamp_type);
198 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
199 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
200 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
201 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
202 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
203 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)");
206 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
207 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
209 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
210 #ifdef HAVE_PCAP_REMOTE
211 g_log(log_domain, log_level, "Capture source[df] : %s",
212 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
213 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
215 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
216 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
217 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
219 g_log(log_domain, log_level, "Authentication[df] : %s",
220 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
221 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
223 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
224 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
225 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
227 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
228 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
229 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
231 #ifdef HAVE_PCAP_SETSAMPLING
232 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
233 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
235 g_log(log_domain, log_level, "Timestamp type [df] : %s", capture_opts->default_options.timestamp_type ? capture_opts->default_options.timestamp_type : "(unspecified)");
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 g_free(interface_opts->cfilter);
347 interface_opts->cfilter = filter_exp;
351 g_free(capture_opts->default_options.cfilter);
352 capture_opts->default_options.cfilter = filter_exp;
358 * Given a string of the form "<ring buffer file>:<duration>", as might appear
359 * as an argument to a "-b" option, parse it and set the arguments in
360 * question. Return an indication of whether it succeeded or failed
364 get_ring_arguments(capture_options *capture_opts, const char *arg)
366 gchar *p = NULL, *colonp;
368 colonp = strchr(arg, ':');
376 * Skip over any white space (there probably won't be any, but
377 * as we allow it in the preferences file, we might as well
380 while (g_ascii_isspace(*p))
384 * Put the colon back, so if our caller uses, in an
385 * error message, the string they passed us, the message
392 if (strcmp(arg,"files") == 0) {
393 capture_opts->has_ring_num_files = TRUE;
394 capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
395 } else if (strcmp(arg,"filesize") == 0) {
396 capture_opts->has_autostop_filesize = TRUE;
397 capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
398 } else if (strcmp(arg,"duration") == 0) {
399 capture_opts->has_file_duration = TRUE;
400 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
401 } else if (strcmp(arg,"interval") == 0) {
402 capture_opts->has_file_interval = TRUE;
403 capture_opts->file_interval = get_positive_int(p, "ring buffer interval");
406 *colonp = ':'; /* put the colon back */
410 #ifdef HAVE_PCAP_SETSAMPLING
412 * Given a string of the form "<sampling type>:<value>", as might appear
413 * as an argument to a "-m" option, parse it and set the arguments in
414 * question. Return an indication of whether it succeeded or failed
418 get_sampling_arguments(capture_options *capture_opts, const char *arg)
420 gchar *p = NULL, *colonp;
422 colonp = strchr(arg, ':');
429 while (g_ascii_isspace(*p))
436 if (strcmp(arg, "count") == 0) {
437 if (capture_opts->ifaces->len > 0) {
438 interface_options *interface_opts;
440 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
441 interface_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
442 interface_opts->sampling_param = get_positive_int(p, "sampling count");
444 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
445 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
447 } else if (strcmp(arg, "timer") == 0) {
448 if (capture_opts->ifaces->len > 0) {
449 interface_options *interface_opts;
451 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
452 interface_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
453 interface_opts->sampling_param = get_positive_int(p, "sampling timer");
455 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
456 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
464 #ifdef HAVE_PCAP_REMOTE
466 * Given a string of the form "<username>:<password>", as might appear
467 * as an argument to a "-A" option, parse it and set the arguments in
468 * question. Return an indication of whether it succeeded or failed
472 get_auth_arguments(capture_options *capture_opts, const char *arg)
474 gchar *p = NULL, *colonp;
476 colonp = strchr(arg, ':');
483 while (g_ascii_isspace(*p))
486 if (capture_opts->ifaces->len > 0) {
487 interface_options *interface_opts;
489 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
490 interface_opts->auth_type = CAPTURE_AUTH_PWD;
491 interface_opts->auth_username = g_strdup(arg);
492 interface_opts->auth_password = g_strdup(p);
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);
505 capture_opts_generate_display_name(const char *friendly_name,
506 const char *name _U_)
509 * Display the friendly name rather than the not-so-friendly
510 * GUID-based interface name.
512 return g_strdup(friendly_name);
516 capture_opts_generate_display_name(const char *friendly_name,
520 * On UN*X, however, users are more used to interface names,
521 * and may find it helpful to see them.
523 return g_strdup_printf("%s: %s", friendly_name, name);
528 fill_in_interface_opts_from_ifinfo(interface_options *interface_opts,
529 const if_info_t *if_info)
531 interface_opts->name = g_strdup(if_info->name);
533 if (if_info->friendly_name != NULL) {
535 * We have a friendly name; remember it as the
538 interface_opts->descr = g_strdup(if_info->friendly_name);
540 * ...and use it in the console display name.
542 interface_opts->display_name = capture_opts_generate_display_name(if_info->friendly_name, if_info->name);
544 /* fallback to the interface name */
545 interface_opts->descr = NULL;
546 interface_opts->display_name = g_strdup(if_info->name);
548 interface_opts->if_type = if_info->type;
549 interface_opts->extcap = g_strdup(if_info->extcap);
553 fill_in_interface_opts_from_ifinfo_by_name(interface_options *interface_opts,
561 size_t prefix_length;
564 if_list = capture_interface_list(&err, NULL, NULL);
565 if (if_list != NULL) {
567 * Try and do an exact match (case insensitive) on the
568 * interface name, the interface description, and the
569 * hardware description.
571 for (if_entry = g_list_first(if_list); if_entry != NULL;
572 if_entry = g_list_next(if_entry))
574 if_info = (if_info_t *)if_entry->data;
577 * Does the specified name match the interface name
578 * with a case-insensitive match?
580 if (g_ascii_strcasecmp(if_info->name, name) == 0) {
589 * Does this interface have a friendly name and, if so,
590 * does the specified name match the friendly name with
591 * a case-insensitive match?
593 if (if_info->friendly_name != NULL &&
594 g_ascii_strcasecmp(if_info->friendly_name, name) == 0) {
605 * We didn't find it; attempt a case-insensitive prefix match
606 * of the friendly name.
608 prefix_length = strlen(name);
609 for (if_entry = g_list_first(if_list); if_entry != NULL;
610 if_entry = g_list_next(if_entry))
612 if_info = (if_info_t *)if_entry->data;
614 if (if_info->friendly_name != NULL &&
615 g_ascii_strncasecmp(if_info->friendly_name, name, prefix_length) == 0) {
617 * We found an interface whose friendly name matches
618 * with a case-insensitive prefix match.
629 * We found an interface that matches.
631 fill_in_interface_opts_from_ifinfo(interface_opts, if_info);
633 free_interface_list(if_list);
638 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
646 interface_options interface_opts;
649 * If the argument is a number, treat it as an index into the list
650 * of adapters, as printed by "tshark -D".
652 * This should be OK on UNIX systems, as interfaces shouldn't have
653 * names that begin with digits. It can be useful on Windows, where
654 * more than one interface can have the same name.
656 adapter_index = strtol(optarg_str_p, &p, 10);
657 if (p != NULL && *p == '\0') {
658 if (adapter_index < 0) {
659 cmdarg_err("The specified adapter index is a negative number");
662 if (adapter_index > INT_MAX) {
663 cmdarg_err("The specified adapter index is too large (greater than %d)",
667 if (adapter_index == 0) {
668 cmdarg_err("There is no interface with that adapter index");
671 if_list = capture_interface_list(&err, &err_str, NULL);
672 if (if_list == NULL) {
674 cmdarg_err("There are no interfaces on which a capture can be done");
676 cmdarg_err("%s", err_str);
681 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
682 if (if_info == NULL) {
683 cmdarg_err("There is no interface with that adapter index");
686 fill_in_interface_opts_from_ifinfo(&interface_opts, if_info);
687 free_interface_list(if_list);
688 } else if (capture_opts->capture_child) {
690 * In Wireshark capture child mode, so the exact interface name
691 * is supplied, and we don't need to look it up.
693 if_info = if_info_get(optarg_str_p);
694 fill_in_interface_opts_from_ifinfo(&interface_opts, if_info);
695 if_info_free(if_info);
698 * Search for that name in the interface list and, if we found
699 * it, fill in fields in the interface_opts structure.
701 if (!fill_in_interface_opts_from_ifinfo_by_name(&interface_opts,
704 * We didn't find the interface in the list; just use
705 * the specified name, so that, for example, if an
706 * interface doesn't show up in the list for some
707 * reason, the user can try specifying it explicitly
708 * for testing purposes.
710 interface_opts.name = g_strdup(optarg_str_p);
711 interface_opts.descr = NULL;
712 interface_opts.display_name = g_strdup(optarg_str_p);
713 interface_opts.if_type = capture_opts->default_options.if_type;
714 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
718 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
719 interface_opts.snaplen = capture_opts->default_options.snaplen;
720 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
721 interface_opts.linktype = capture_opts->default_options.linktype;
722 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
723 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
724 interface_opts.extcap_args = NULL;
725 interface_opts.extcap_pid = WS_INVALID_PID;
726 interface_opts.extcap_pipedata = NULL;
728 interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
729 interface_opts.extcap_control_in_h = INVALID_HANDLE_VALUE;
730 interface_opts.extcap_control_out_h = INVALID_HANDLE_VALUE;
732 interface_opts.extcap_control_in = g_strdup(capture_opts->default_options.extcap_control_in);
733 interface_opts.extcap_control_out = g_strdup(capture_opts->default_options.extcap_control_out);
734 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
735 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
737 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
738 #ifdef HAVE_PCAP_REMOTE
739 interface_opts.src_type = capture_opts->default_options.src_type;
740 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
741 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
742 interface_opts.auth_type = capture_opts->default_options.auth_type;
743 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
744 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
745 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
746 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
747 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
749 #ifdef HAVE_PCAP_SETSAMPLING
750 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
751 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
753 interface_opts.timestamp_type = capture_opts->default_options.timestamp_type;
755 g_array_append_val(capture_opts->ifaces, interface_opts);
762 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
767 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
768 if (capture_opts->capture_comment) {
769 cmdarg_err("--capture-comment can be set only once per file");
772 capture_opts->capture_comment = g_strdup(optarg_str_p);
774 case 'a': /* autostop criteria */
775 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
776 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
780 #ifdef HAVE_PCAP_REMOTE
782 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
783 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
788 case 'b': /* Ringbuffer option */
789 capture_opts->multi_files_on = TRUE;
790 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
791 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
795 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
796 case 'B': /* Buffer size */
797 if (capture_opts->ifaces->len > 0) {
798 interface_options *interface_opts;
800 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
801 interface_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
803 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
807 case 'c': /* Capture n packets */
808 capture_opts->has_autostop_packets = TRUE;
809 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
811 case 'f': /* capture filter */
812 get_filter_arguments(capture_opts, optarg_str_p);
814 case 'g': /* enable group read access on the capture file(s) */
815 capture_opts->group_read_access = TRUE;
817 case 'H': /* Hide capture info dialog box */
818 capture_opts->show_info = FALSE;
820 case LONGOPT_SET_TSTAMP_TYPE: /* Set capture time stamp type */
821 if (capture_opts->ifaces->len > 0) {
822 interface_options *interface_opts;
824 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
825 g_free(interface_opts->timestamp_type);
826 interface_opts->timestamp_type = g_strdup(optarg_str_p);
828 g_free(capture_opts->default_options.timestamp_type);
829 capture_opts->default_options.timestamp_type = g_strdup(optarg_str_p);
832 case 'i': /* Use interface x */
833 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
838 #ifdef HAVE_PCAP_CREATE
839 case 'I': /* Capture in monitor mode */
840 if (capture_opts->ifaces->len > 0) {
841 interface_options *interface_opts;
843 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
844 interface_opts->monitor_mode = TRUE;
846 capture_opts->default_options.monitor_mode = TRUE;
850 case 'k': /* Start capture immediately */
851 *start_capture = TRUE;
853 /*case 'l':*/ /* Automatic scrolling in live capture mode */
854 #ifdef HAVE_PCAP_SETSAMPLING
856 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
857 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
862 case 'n': /* Use pcapng format */
863 capture_opts->use_pcapng = TRUE;
865 case 'p': /* Don't capture in promiscuous mode */
866 if (capture_opts->ifaces->len > 0) {
867 interface_options *interface_opts;
869 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
870 interface_opts->promisc_mode = FALSE;
872 capture_opts->default_options.promisc_mode = FALSE;
875 case 'P': /* Use pcap format */
876 capture_opts->use_pcapng = FALSE;
878 #ifdef HAVE_PCAP_REMOTE
880 if (capture_opts->ifaces->len > 0) {
881 interface_options *interface_opts;
883 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
884 interface_opts->nocap_rpcap = FALSE;
886 capture_opts->default_options.nocap_rpcap = FALSE;
890 case 's': /* Set the snapshot (capture) length */
891 snaplen = get_natural_int(optarg_str_p, "snapshot length");
893 * Make a snapshot length of 0 equivalent to the maximum packet
894 * length, mirroring what tcpdump does.
897 snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
898 if (capture_opts->ifaces->len > 0) {
899 interface_options *interface_opts;
901 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
902 interface_opts->has_snaplen = TRUE;
903 interface_opts->snaplen = snaplen;
905 capture_opts->default_options.snaplen = snaplen;
906 capture_opts->default_options.has_snaplen = TRUE;
909 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
910 capture_opts->real_time_mode = TRUE;
912 #ifdef HAVE_PCAP_REMOTE
914 if (capture_opts->ifaces->len > 0) {
915 interface_options *interface_opts;
917 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
918 interface_opts->datatx_udp = TRUE;
920 capture_opts->default_options.datatx_udp = TRUE;
924 case 'w': /* Write to capture file x */
925 capture_opts->saving_to_file = TRUE;
926 g_free(capture_opts->save_file);
927 capture_opts->save_file = g_strdup(optarg_str_p);
928 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
930 case 'y': /* Set the pcap data link type */
931 if (capture_opts->ifaces->len > 0) {
932 interface_options *interface_opts;
934 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
935 interface_opts->linktype = linktype_name_to_val(optarg_str_p);
936 if (interface_opts->linktype == -1) {
937 cmdarg_err("The specified data link type \"%s\" isn't valid",
942 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
943 if (capture_opts->default_options.linktype == -1) {
944 cmdarg_err("The specified data link type \"%s\" isn't valid",
951 /* the caller is responsible to send us only the right opt's */
952 g_assert_not_reached();
959 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name, int queries)
961 GList *lt_entry, *ts_entry;
963 if (queries & CAPS_QUERY_LINK_TYPES) {
964 if (caps->can_set_rfmon)
965 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
966 name, queries & CAPS_MONITOR_MODE ? "" : "not ");
968 printf("Data link types of interface %s (use option -y to set):\n", name);
969 for (lt_entry = caps->data_link_types; lt_entry != NULL;
970 lt_entry = g_list_next(lt_entry)) {
971 data_link_info_t *data_link_info = (data_link_info_t *)lt_entry->data;
972 printf(" %s", data_link_info->name);
973 if (data_link_info->description != NULL)
974 printf(" (%s)", data_link_info->description);
976 printf(" (not supported)");
981 if (queries & CAPS_QUERY_TIMESTAMP_TYPES) {
982 printf("Timestamp types of the interface (use option --time-stamp-type to set):\n");
983 for (ts_entry = caps->timestamp_types; ts_entry != NULL;
984 ts_entry = g_list_next(ts_entry)) {
985 timestamp_info_t *timestamp = (timestamp_info_t *)ts_entry->data;
986 printf(" %s", timestamp->name);
987 if (timestamp->description != NULL)
988 printf(" (%s)", timestamp->description);
996 /* Print an ASCII-formatted list of interfaces. */
998 capture_opts_print_interfaces(GList *if_list)
1004 i = 1; /* Interface id number */
1005 for (if_entry = g_list_first(if_list); if_entry != NULL;
1006 if_entry = g_list_next(if_entry)) {
1007 if_info = (if_info_t *)if_entry->data;
1008 printf("%d. %s", i++, if_info->name);
1010 /* Print the interface friendly name, if it exists;
1011 if not, fall back to the vendor description, if it exists. */
1012 if (if_info->friendly_name != NULL){
1013 printf(" (%s)", if_info->friendly_name);
1015 if (if_info->vendor_description != NULL)
1016 printf(" (%s)", if_info->vendor_description);
1024 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
1027 interface_options *interface_opts;
1029 if (capture_opts->ifaces->len > 0) {
1030 for (i = 0; i < capture_opts->ifaces->len; i++) {
1031 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, 0);
1032 if (interface_opts->snaplen < 1)
1033 interface_opts->snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1034 else if (interface_opts->snaplen < snaplen_min)
1035 interface_opts->snaplen = snaplen_min;
1038 if (capture_opts->default_options.snaplen < 1)
1039 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1040 else if (capture_opts->default_options.snaplen < snaplen_min)
1041 capture_opts->default_options.snaplen = snaplen_min;
1047 capture_opts_trim_ring_num_files(capture_options *capture_opts)
1049 /* Check the value range of the ring_num_files parameter */
1050 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
1051 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
1052 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
1053 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
1054 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
1056 #if RINGBUFFER_MIN_NUM_FILES > 0
1057 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
1058 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
1059 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
1064 * If no interface was specified explicitly, pick a default.
1067 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
1068 const char *capture_device)
1072 /* Did the user specify an interface to use? */
1073 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
1074 /* yes they did, return immediately - nothing further to do here */
1078 /* No - is a default specified in the preferences file? */
1079 if (capture_device != NULL) {
1081 status = capture_opts_add_iface_opt(capture_opts, capture_device);
1084 /* No default in preferences file, just pick the first interface from the list of interfaces. */
1085 return capture_opts_add_iface_opt(capture_opts, "1");
1089 #define S_IFIFO _S_IFIFO
1092 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1095 /* copied from filesystem.c */
1097 capture_opts_test_for_fifo(const char *path)
1101 if (ws_stat64(path, &statb) < 0)
1104 if (S_ISFIFO(statb.st_mode))
1111 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1117 if (save_file != NULL) {
1118 /* We're writing to a capture file. */
1119 if (strcmp(save_file, "-") == 0) {
1120 /* Writing to stdout. */
1121 /* XXX - should we check whether it's a pipe? It's arguably
1122 silly to do "-w - >output_file" rather than "-w output_file",
1123 but by not checking we might be violating the Principle Of
1124 Least Astonishment. */
1127 /* not writing to stdout, test for a FIFO (aka named pipe) */
1128 err = capture_opts_test_for_fifo(save_file);
1131 case ENOENT: /* it doesn't exist, so we'll be creating it,
1132 and it won't be a FIFO */
1133 case 0: /* found it, but it's not a FIFO */
1136 case ESPIPE: /* it is a FIFO */
1140 default: /* couldn't stat it */
1141 break; /* ignore: later attempt to open */
1142 /* will generate a nice msg */
1151 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1153 interface_options *interface_opts;
1155 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, if_index);
1156 /* XXX - check if found? */
1158 g_free(interface_opts->name);
1159 g_free(interface_opts->descr);
1160 g_free(interface_opts->display_name);
1161 g_free(interface_opts->cfilter);
1162 g_free(interface_opts->timestamp_type);
1163 g_free(interface_opts->extcap);
1164 g_free(interface_opts->extcap_fifo);
1165 if (interface_opts->extcap_args)
1166 g_hash_table_unref(interface_opts->extcap_args);
1167 if (interface_opts->extcap_pid != WS_INVALID_PID)
1168 g_spawn_close_pid(interface_opts->extcap_pid);
1169 g_free(interface_opts->extcap_pipedata);
1170 g_free(interface_opts->extcap_control_in);
1171 g_free(interface_opts->extcap_control_out);
1172 #ifdef HAVE_PCAP_REMOTE
1173 if (interface_opts->src_type == CAPTURE_IFREMOTE) {
1174 g_free(interface_opts->remote_host);
1175 g_free(interface_opts->remote_port);
1176 g_free(interface_opts->auth_username);
1177 g_free(interface_opts->auth_password);
1180 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1186 * Add all non-hidden selected interfaces in the "all interfaces" list
1187 * to the list of interfaces for the capture.
1190 collect_ifaces(capture_options *capture_opts)
1193 interface_t *device;
1194 interface_options interface_opts;
1196 /* Empty out the existing list of interfaces. */
1197 for (i = capture_opts->ifaces->len; i != 0; i--)
1198 capture_opts_del_iface(capture_opts, i-1);
1200 /* Now fill the list up again. */
1201 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1202 device = &g_array_index(capture_opts->all_ifaces, interface_t, i);
1203 if (!device->hidden && device->selected) {
1204 interface_opts.name = g_strdup(device->name);
1205 interface_opts.descr = g_strdup(device->friendly_name);
1206 interface_opts.display_name = g_strdup(device->display_name);
1207 interface_opts.linktype = device->active_dlt;
1208 interface_opts.cfilter = g_strdup(device->cfilter);
1209 interface_opts.timestamp_type = g_strdup(device->timestamp_type);
1210 interface_opts.snaplen = device->snaplen;
1211 interface_opts.has_snaplen = device->has_snaplen;
1212 interface_opts.promisc_mode = device->pmode;
1213 interface_opts.if_type = device->if_info.type;
1214 interface_opts.extcap = g_strdup(device->if_info.extcap);
1215 interface_opts.extcap_fifo = NULL;
1216 interface_opts.extcap_pipedata = NULL;
1217 interface_opts.extcap_args = device->external_cap_args_settings;
1218 interface_opts.extcap_pid = WS_INVALID_PID;
1219 if (interface_opts.extcap_args)
1220 g_hash_table_ref(interface_opts.extcap_args);
1221 interface_opts.extcap_pipedata = NULL;
1223 interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
1224 interface_opts.extcap_control_in_h = INVALID_HANDLE_VALUE;
1225 interface_opts.extcap_control_out_h = INVALID_HANDLE_VALUE;
1227 interface_opts.extcap_control_in = NULL;
1228 interface_opts.extcap_control_out = NULL;
1229 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1230 interface_opts.buffer_size = device->buffer;
1232 #ifdef HAVE_PCAP_CREATE
1233 interface_opts.monitor_mode = device->monitor_mode_enabled;
1235 #ifdef HAVE_PCAP_REMOTE
1236 interface_opts.src_type = CAPTURE_IFREMOTE;
1237 interface_opts.remote_host = g_strdup(device->remote_opts.remote_host_opts.remote_host);
1238 interface_opts.remote_port = g_strdup(device->remote_opts.remote_host_opts.remote_port);
1239 interface_opts.auth_type = device->remote_opts.remote_host_opts.auth_type;
1240 interface_opts.auth_username = g_strdup(device->remote_opts.remote_host_opts.auth_username);
1241 interface_opts.auth_password = g_strdup(device->remote_opts.remote_host_opts.auth_password);
1242 interface_opts.datatx_udp = device->remote_opts.remote_host_opts.datatx_udp;
1243 interface_opts.nocap_rpcap = device->remote_opts.remote_host_opts.nocap_rpcap;
1244 interface_opts.nocap_local = device->remote_opts.remote_host_opts.nocap_local;
1246 #ifdef HAVE_PCAP_SETSAMPLING
1247 interface_opts.sampling_method = device->remote_opts.sampling_method;
1248 interface_opts.sampling_param = device->remote_opts.sampling_param;
1250 g_array_append_val(capture_opts->ifaces, interface_opts);
1258 capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
1260 link_row* e = (link_row*)elem;
1267 capture_opts_free_interface_t(interface_t *device)
1269 if (device != NULL) {
1270 g_free(device->name);
1271 g_free(device->display_name);
1272 g_free(device->friendly_name);
1273 g_free(device->addresses);
1274 g_free(device->cfilter);
1275 g_free(device->timestamp_type);
1276 g_list_foreach(device->links,
1277 capture_opts_free_interface_t_links, NULL);
1278 g_list_free(device->links);
1279 #ifdef HAVE_PCAP_REMOTE
1280 g_free(device->remote_opts.remote_host_opts.remote_host);
1281 g_free(device->remote_opts.remote_host_opts.remote_port);
1282 g_free(device->remote_opts.remote_host_opts.auth_username);
1283 g_free(device->remote_opts.remote_host_opts.auth_password);
1285 g_free(device->if_info.name);
1286 g_free(device->if_info.friendly_name);
1287 g_free(device->if_info.vendor_description);
1288 g_slist_free_full(device->if_info.addrs, g_free);
1289 g_free(device->if_info.extcap);
1293 #endif /* HAVE_LIBPCAP */
1296 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1301 * indent-tabs-mode: nil
1304 * vi: set shiftwidth=4 tabstop=8 expandtab:
1305 * :indentSize=4:tabSize=8:noTabs=true: