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+
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>
31 #include "caputils/capture_ifinfo.h"
32 #include "caputils/capture-pcap-util.h"
34 #include "ui/filter_files.h"
36 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
40 capture_opts_init(capture_options *capture_opts)
42 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
43 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
44 capture_opts->num_selected = 0;
45 capture_opts->default_options.name = NULL;
46 capture_opts->default_options.descr = NULL;
47 capture_opts->default_options.cfilter = NULL;
48 capture_opts->default_options.has_snaplen = FALSE;
49 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
50 capture_opts->default_options.linktype = -1; /* use interface default */
51 capture_opts->default_options.promisc_mode = TRUE;
52 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_userdata = NULL;
58 capture_opts->default_options.extcap_pid = INVALID_EXTCAP_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;
67 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
68 capture_opts->default_options.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
70 capture_opts->default_options.monitor_mode = FALSE;
71 #ifdef HAVE_PCAP_REMOTE
72 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
73 capture_opts->default_options.remote_host = NULL;
74 capture_opts->default_options.remote_port = NULL;
75 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
76 capture_opts->default_options.auth_username = NULL;
77 capture_opts->default_options.auth_password = NULL;
78 capture_opts->default_options.datatx_udp = FALSE;
79 capture_opts->default_options.nocap_rpcap = TRUE;
80 capture_opts->default_options.nocap_local = FALSE;
82 #ifdef HAVE_PCAP_SETSAMPLING
83 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
84 capture_opts->default_options.sampling_param = 0;
86 capture_opts->default_options.timestamp_type = NULL;
87 capture_opts->saving_to_file = FALSE;
88 capture_opts->save_file = NULL;
89 capture_opts->group_read_access = FALSE;
90 #ifdef PCAP_NG_DEFAULT
91 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
93 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
95 capture_opts->real_time_mode = TRUE;
96 capture_opts->show_info = TRUE;
97 capture_opts->restart = FALSE;
98 capture_opts->orig_save_file = NULL;
100 capture_opts->multi_files_on = FALSE;
101 capture_opts->has_file_duration = FALSE;
102 capture_opts->file_duration = 60; /* 1 min */
103 capture_opts->has_file_interval = FALSE;
104 capture_opts->file_interval = 60; /* 1 min */
105 capture_opts->has_ring_num_files = FALSE;
106 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
108 capture_opts->has_autostop_files = FALSE;
109 capture_opts->autostop_files = 1;
110 capture_opts->has_autostop_packets = FALSE;
111 capture_opts->autostop_packets = 0;
112 capture_opts->has_autostop_filesize = FALSE;
113 capture_opts->autostop_filesize = 1000; /* 1 MB */
114 capture_opts->has_autostop_duration = FALSE;
115 capture_opts->autostop_duration = 60; /* 1 min */
116 capture_opts->capture_comment = NULL;
118 capture_opts->output_to_pipe = FALSE;
119 capture_opts->capture_child = FALSE;
123 capture_opts_cleanup(capture_options *capture_opts)
128 if (capture_opts->ifaces) {
129 while (capture_opts->ifaces->len > 0) {
130 capture_opts_del_iface(capture_opts, 0);
132 g_array_free(capture_opts->ifaces, TRUE);
133 capture_opts->ifaces = NULL;
135 if (capture_opts->all_ifaces) {
136 while (capture_opts->all_ifaces->len > 0) {
137 interface_t *device = &g_array_index(capture_opts->all_ifaces, interface_t, 0);
138 capture_opts_free_interface_t(device);
139 capture_opts->all_ifaces = g_array_remove_index(capture_opts->all_ifaces, 0);
141 g_array_free(capture_opts->all_ifaces, TRUE);
142 capture_opts->all_ifaces = NULL;
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, "Console display name[%02d]: %s", i, interface_opts->console_display_name ? interface_opts->console_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");
165 g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts->extcap ? interface_opts->extcap : "(unspecified)");
166 g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts->extcap_fifo ? interface_opts->extcap_fifo : "(unspecified)");
167 g_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts->extcap_pid);
169 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
170 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts->buffer_size);
172 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts->monitor_mode?"TRUE":"FALSE");
173 #ifdef HAVE_PCAP_REMOTE
174 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
175 interface_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" :
176 interface_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" :
178 if (interface_opts->src_type == CAPTURE_IFREMOTE) {
179 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts->remote_host ? interface_opts->remote_host : "(unspecified)");
180 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts->remote_port ? interface_opts->remote_port : "(unspecified)");
182 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
183 interface_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" :
184 interface_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
186 if (interface_opts->auth_type == CAPTURE_AUTH_PWD) {
187 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts->auth_username ? interface_opts->auth_username : "(unspecified)");
188 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
190 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts->datatx_udp);
191 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts->nocap_rpcap);
192 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts->nocap_local);
194 #ifdef HAVE_PCAP_SETSAMPLING
195 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts->sampling_method);
196 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts->sampling_param);
198 g_log(log_domain, log_level, "Timestamp type [%02d] : %s", i, interface_opts->timestamp_type);
200 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
201 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
202 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
203 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
204 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
205 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
207 g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
208 g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
210 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
211 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
213 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
214 #ifdef HAVE_PCAP_REMOTE
215 g_log(log_domain, log_level, "Capture source[df] : %s",
216 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
217 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
219 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
220 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
221 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
223 g_log(log_domain, log_level, "Authentication[df] : %s",
224 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
225 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
227 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
228 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
229 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
231 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
232 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
233 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
235 #ifdef HAVE_PCAP_SETSAMPLING
236 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
237 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
239 g_log(log_domain, log_level, "Timestamp type [df] : %s", capture_opts->default_options.timestamp_type ? capture_opts->default_options.timestamp_type : "(unspecified)");
240 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
241 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
242 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
243 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
244 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
245 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
247 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
248 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
249 g_log(log_domain, log_level, "FileInterval (%u) : %u", capture_opts->has_file_interval, capture_opts->file_interval);
250 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
252 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
253 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
254 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
255 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
259 * Given a string of the form "<autostop criterion>:<value>", as might appear
260 * as an argument to a "-a" option, parse it and set the criterion in
261 * question. Return an indication of whether it succeeded or failed
265 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
269 colonp = strchr(autostoparg, ':');
277 * Skip over any white space (there probably won't be any, but
278 * as we allow it in the preferences file, we might as well
281 while (g_ascii_isspace(*p))
285 * Put the colon back, so if our caller uses, in an
286 * error message, the string they passed us, the message
292 if (strcmp(autostoparg,"duration") == 0) {
293 capture_opts->has_autostop_duration = TRUE;
294 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
295 } else if (strcmp(autostoparg,"filesize") == 0) {
296 capture_opts->has_autostop_filesize = TRUE;
297 capture_opts->autostop_filesize = get_nonzero_guint32(p,"autostop filesize");
298 } else if (strcmp(autostoparg,"files") == 0) {
299 capture_opts->multi_files_on = TRUE;
300 capture_opts->has_autostop_files = TRUE;
301 capture_opts->autostop_files = get_positive_int(p,"autostop files");
305 *colonp = ':'; /* put the colon back */
309 static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
313 char* filter_exp = NULL;
315 colonp = strchr(arg, ':');
320 if (strcmp(arg, "predef") == 0) {
323 filterItem = get_filter_list_first(CFILTER_LIST);
324 while (filterItem != NULL) {
325 filter_def *filterDef;
327 filterDef = (filter_def*)filterItem->data;
328 if (strcmp(val, filterDef->name) == 0) {
329 filter_exp = g_strdup(filterDef->strval);
332 filterItem = filterItem->next;
337 if (filter_exp == NULL) {
338 /* No filter expression found yet; fallback to previous implemention
339 and assume the arg contains a filter expression */
341 *colonp = ':'; /* restore colon */
343 filter_exp = g_strdup(arg);
346 if (capture_opts->ifaces->len > 0) {
347 interface_options *interface_opts;
349 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
350 g_free(interface_opts->cfilter);
351 interface_opts->cfilter = filter_exp;
355 g_free(capture_opts->default_options.cfilter);
356 capture_opts->default_options.cfilter = filter_exp;
362 * Given a string of the form "<ring buffer file>:<duration>", as might appear
363 * as an argument to a "-b" option, parse it and set the arguments in
364 * question. Return an indication of whether it succeeded or failed
368 get_ring_arguments(capture_options *capture_opts, const char *arg)
370 gchar *p = NULL, *colonp;
372 colonp = strchr(arg, ':');
380 * Skip over any white space (there probably won't be any, but
381 * as we allow it in the preferences file, we might as well
384 while (g_ascii_isspace(*p))
388 * Put the colon back, so if our caller uses, in an
389 * error message, the string they passed us, the message
396 if (strcmp(arg,"files") == 0) {
397 capture_opts->has_ring_num_files = TRUE;
398 capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
399 } else if (strcmp(arg,"filesize") == 0) {
400 capture_opts->has_autostop_filesize = TRUE;
401 capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
402 } else if (strcmp(arg,"duration") == 0) {
403 capture_opts->has_file_duration = TRUE;
404 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
405 } else if (strcmp(arg,"interval") == 0) {
406 capture_opts->has_file_interval = TRUE;
407 capture_opts->file_interval = get_positive_int(p, "ring buffer interval");
410 *colonp = ':'; /* put the colon back */
414 #ifdef HAVE_PCAP_SETSAMPLING
416 * Given a string of the form "<sampling type>:<value>", as might appear
417 * as an argument to a "-m" option, parse it and set the arguments in
418 * question. Return an indication of whether it succeeded or failed
422 get_sampling_arguments(capture_options *capture_opts, const char *arg)
424 gchar *p = NULL, *colonp;
426 colonp = strchr(arg, ':');
433 while (g_ascii_isspace(*p))
440 if (strcmp(arg, "count") == 0) {
441 if (capture_opts->ifaces->len > 0) {
442 interface_options *interface_opts;
444 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
445 interface_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
446 interface_opts->sampling_param = get_positive_int(p, "sampling count");
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 interface_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
457 interface_opts->sampling_param = get_positive_int(p, "sampling timer");
459 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
460 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
468 #ifdef HAVE_PCAP_REMOTE
470 * Given a string of the form "<username>:<password>", as might appear
471 * as an argument to a "-A" option, parse it and set the arguments in
472 * question. Return an indication of whether it succeeded or failed
476 get_auth_arguments(capture_options *capture_opts, const char *arg)
478 gchar *p = NULL, *colonp;
480 colonp = strchr(arg, ':');
487 while (g_ascii_isspace(*p))
490 if (capture_opts->ifaces->len > 0) {
491 interface_options *interface_opts;
493 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
494 interface_opts->auth_type = CAPTURE_AUTH_PWD;
495 interface_opts->auth_username = g_strdup(arg);
496 interface_opts->auth_password = g_strdup(p);
498 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
499 capture_opts->default_options.auth_username = g_strdup(arg);
500 capture_opts->default_options.auth_password = g_strdup(p);
508 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
516 interface_options interface_opts;
519 * If the argument is a number, treat it as an index into the list
520 * of adapters, as printed by "tshark -D".
522 * This should be OK on UNIX systems, as interfaces shouldn't have
523 * names that begin with digits. It can be useful on Windows, where
524 * more than one interface can have the same name.
526 adapter_index = strtol(optarg_str_p, &p, 10);
527 if (p != NULL && *p == '\0') {
528 if (adapter_index < 0) {
529 cmdarg_err("The specified adapter index is a negative number");
532 if (adapter_index > INT_MAX) {
533 cmdarg_err("The specified adapter index is too large (greater than %d)",
537 if (adapter_index == 0) {
538 cmdarg_err("There is no interface with that adapter index");
541 if_list = capture_interface_list(&err, &err_str, NULL);
542 if (if_list == NULL) {
544 cmdarg_err("There are no interfaces on which a capture can be done");
546 cmdarg_err("%s", err_str);
551 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
552 if (if_info == NULL) {
553 cmdarg_err("There is no interface with that adapter index");
556 interface_opts.name = g_strdup(if_info->name);
557 if (if_info->friendly_name != NULL) {
559 * We have a friendly name for the interface, so display that
560 * instead of the interface name/guid.
562 * XXX - on UN*X, the interface name is not quite so ugly,
563 * and might be more familiar to users; display them both?
565 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
567 /* fallback to the interface name */
568 interface_opts.console_display_name = g_strdup(if_info->name);
570 interface_opts.if_type = if_info->type;
572 interface_opts.extcap = g_strdup(if_info->extcap);
574 free_interface_list(if_list);
575 } else if (capture_opts->capture_child) {
576 /* In Wireshark capture child mode, thus proper device name is supplied. */
577 /* No need for trying to match it for friendly names. */
578 interface_opts.name = g_strdup(optarg_str_p);
579 interface_opts.console_display_name = g_strdup(optarg_str_p);
580 interface_opts.if_type = capture_opts->default_options.if_type;
582 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
586 * Retrieve the interface list so that we can search for the
587 * specified option amongst both the interface names and the
588 * friendly names and so that we find the friendly name even
589 * if an interface name was specified.
591 * If we can't get the list, just use the specified option as
592 * the interface name, so that the user can try specifying an
593 * interface explicitly for testing purposes.
595 if_list = capture_interface_list(&err, NULL, NULL);
596 if (if_list != NULL) {
597 /* try and do an exact match (case insensitive) */
602 for (if_entry = g_list_first(if_list); if_entry != NULL;
603 if_entry = g_list_next(if_entry))
605 if_info = (if_info_t *)if_entry->data;
606 /* exact name check */
607 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
608 /* exact match on the interface name, use that for displaying etc */
609 interface_opts.name = g_strdup(if_info->name);
611 if (if_info->friendly_name != NULL) {
613 * If we have a friendly name, use that for the
614 * console display name, as it is the basis for
615 * the auto generated temp filename.
617 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
619 interface_opts.console_display_name = g_strdup(if_info->name);
621 interface_opts.if_type = if_info->type;
623 interface_opts.extcap = g_strdup(if_info->extcap);
629 /* exact friendly name check */
630 if (if_info->friendly_name != NULL &&
631 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
632 /* exact match - use the friendly name for display */
633 interface_opts.name = g_strdup(if_info->name);
634 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
635 interface_opts.if_type = if_info->type;
637 interface_opts.extcap = g_strdup(if_info->extcap);
644 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
646 size_t prefix_length;
648 prefix_length = strlen(optarg_str_p);
649 for (if_entry = g_list_first(if_list); if_entry != NULL;
650 if_entry = g_list_next(if_entry))
652 if_info = (if_info_t *)if_entry->data;
654 if (if_info->friendly_name != NULL &&
655 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
656 /* prefix match - use the friendly name for display */
657 interface_opts.name = g_strdup(if_info->name);
658 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
659 interface_opts.if_type = if_info->type;
661 interface_opts.extcap = g_strdup(if_info->extcap);
670 * We didn't find the interface in the list; just use
671 * the specified name, so that, for example, if an
672 * interface doesn't show up in the list for some
673 * reason, the user can try specifying it explicitly
674 * for testing purposes.
676 interface_opts.name = g_strdup(optarg_str_p);
677 interface_opts.console_display_name = g_strdup(optarg_str_p);
678 interface_opts.if_type = capture_opts->default_options.if_type;
680 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
683 free_interface_list(if_list);
685 interface_opts.name = g_strdup(optarg_str_p);
686 interface_opts.console_display_name = g_strdup(optarg_str_p);
687 interface_opts.if_type = capture_opts->default_options.if_type;
689 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
694 /* We don't set iface_descr here because doing so requires
695 * capture_ui_utils.c which requires epan/prefs.c which is
696 * probably a bit too much dependency for here...
698 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
699 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
700 interface_opts.snaplen = capture_opts->default_options.snaplen;
701 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
702 interface_opts.linktype = capture_opts->default_options.linktype;
703 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
705 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
706 interface_opts.extcap_args = NULL;
707 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
708 interface_opts.extcap_userdata = NULL;
710 interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
711 interface_opts.extcap_control_in_h = INVALID_HANDLE_VALUE;
712 interface_opts.extcap_control_out_h = INVALID_HANDLE_VALUE;
714 interface_opts.extcap_control_in = g_strdup(capture_opts->default_options.extcap_control_in);
715 interface_opts.extcap_control_out = g_strdup(capture_opts->default_options.extcap_control_out);
717 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
718 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
720 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
721 #ifdef HAVE_PCAP_REMOTE
722 interface_opts.src_type = capture_opts->default_options.src_type;
723 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
724 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
725 interface_opts.auth_type = capture_opts->default_options.auth_type;
726 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
727 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
728 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
729 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
730 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
732 #ifdef HAVE_PCAP_SETSAMPLING
733 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
734 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
736 interface_opts.timestamp_type = capture_opts->default_options.timestamp_type;
738 g_array_append_val(capture_opts->ifaces, interface_opts);
745 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
750 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
751 if (capture_opts->capture_comment) {
752 cmdarg_err("--capture-comment can be set only once per file");
755 capture_opts->capture_comment = g_strdup(optarg_str_p);
757 case 'a': /* autostop criteria */
758 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
759 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
763 #ifdef HAVE_PCAP_REMOTE
765 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
766 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
771 case 'b': /* Ringbuffer option */
772 capture_opts->multi_files_on = TRUE;
773 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
774 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
778 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
779 case 'B': /* Buffer size */
780 if (capture_opts->ifaces->len > 0) {
781 interface_options *interface_opts;
783 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
784 interface_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
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 LONGOPT_SET_TSTAMP_TYPE: /* Set capture time stamp type */
804 if (capture_opts->ifaces->len > 0) {
805 interface_options *interface_opts;
807 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
808 g_free(interface_opts->timestamp_type);
809 interface_opts->timestamp_type = g_strdup(optarg_str_p);
811 g_free(capture_opts->default_options.timestamp_type);
812 capture_opts->default_options.timestamp_type = g_strdup(optarg_str_p);
815 case 'i': /* Use interface x */
816 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
821 #ifdef HAVE_PCAP_CREATE
822 case 'I': /* Capture in monitor mode */
823 if (capture_opts->ifaces->len > 0) {
824 interface_options *interface_opts;
826 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
827 interface_opts->monitor_mode = TRUE;
829 capture_opts->default_options.monitor_mode = TRUE;
833 case 'k': /* Start capture immediately */
834 *start_capture = TRUE;
836 /*case 'l':*/ /* Automatic scrolling in live capture mode */
837 #ifdef HAVE_PCAP_SETSAMPLING
839 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
840 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
845 case 'n': /* Use pcapng format */
846 capture_opts->use_pcapng = TRUE;
848 case 'p': /* Don't capture in promiscuous mode */
849 if (capture_opts->ifaces->len > 0) {
850 interface_options *interface_opts;
852 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
853 interface_opts->promisc_mode = FALSE;
855 capture_opts->default_options.promisc_mode = FALSE;
858 case 'P': /* Use pcap format */
859 capture_opts->use_pcapng = FALSE;
861 #ifdef HAVE_PCAP_REMOTE
863 if (capture_opts->ifaces->len > 0) {
864 interface_options *interface_opts;
866 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
867 interface_opts->nocap_rpcap = FALSE;
869 capture_opts->default_options.nocap_rpcap = FALSE;
873 case 's': /* Set the snapshot (capture) length */
874 snaplen = get_natural_int(optarg_str_p, "snapshot length");
876 * Make a snapshot length of 0 equivalent to the maximum packet
877 * length, mirroring what tcpdump does.
880 snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
881 if (capture_opts->ifaces->len > 0) {
882 interface_options *interface_opts;
884 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
885 interface_opts->has_snaplen = TRUE;
886 interface_opts->snaplen = snaplen;
888 capture_opts->default_options.snaplen = snaplen;
889 capture_opts->default_options.has_snaplen = TRUE;
892 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
893 capture_opts->real_time_mode = TRUE;
895 #ifdef HAVE_PCAP_REMOTE
897 if (capture_opts->ifaces->len > 0) {
898 interface_options *interface_opts;
900 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
901 interface_opts->datatx_udp = TRUE;
903 capture_opts->default_options.datatx_udp = TRUE;
907 case 'w': /* Write to capture file x */
908 capture_opts->saving_to_file = TRUE;
909 g_free(capture_opts->save_file);
910 capture_opts->save_file = g_strdup(optarg_str_p);
911 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
913 case 'y': /* Set the pcap data link type */
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->linktype = linktype_name_to_val(optarg_str_p);
919 if (interface_opts->linktype == -1) {
920 cmdarg_err("The specified data link type \"%s\" isn't valid",
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, int queries)
944 GList *lt_entry, *ts_entry;
946 if (queries & CAPS_QUERY_LINK_TYPES) {
947 if (caps->can_set_rfmon)
948 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
949 name, queries & CAPS_MONITOR_MODE ? "" : "not ");
951 printf("Data link types of interface %s (use option -y to set):\n", name);
952 for (lt_entry = caps->data_link_types; lt_entry != NULL;
953 lt_entry = g_list_next(lt_entry)) {
954 data_link_info_t *data_link_info = (data_link_info_t *)lt_entry->data;
955 printf(" %s", data_link_info->name);
956 if (data_link_info->description != NULL)
957 printf(" (%s)", data_link_info->description);
959 printf(" (not supported)");
964 if (queries & CAPS_QUERY_TIMESTAMP_TYPES) {
965 printf("Timestamp types of the interface (use option --time-stamp-type to set):\n");
966 for (ts_entry = caps->timestamp_types; ts_entry != NULL;
967 ts_entry = g_list_next(ts_entry)) {
968 timestamp_info_t *timestamp = (timestamp_info_t *)ts_entry->data;
969 printf(" %s", timestamp->name);
970 if (timestamp->description != NULL)
971 printf(" (%s)", timestamp->description);
979 /* Print an ASCII-formatted list of interfaces. */
981 capture_opts_print_interfaces(GList *if_list)
987 i = 1; /* Interface id number */
988 for (if_entry = g_list_first(if_list); if_entry != NULL;
989 if_entry = g_list_next(if_entry)) {
990 if_info = (if_info_t *)if_entry->data;
991 printf("%d. %s", i++, if_info->name);
993 /* Print the interface friendly name, if it exists;
994 if not fall back to vendor description, if it exists. */
995 if (if_info->friendly_name != NULL){
996 printf(" (%s)", if_info->friendly_name);
998 if (if_info->vendor_description != NULL)
999 printf(" (%s)", if_info->vendor_description);
1007 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
1010 interface_options *interface_opts;
1012 if (capture_opts->ifaces->len > 0) {
1013 for (i = 0; i < capture_opts->ifaces->len; i++) {
1014 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, 0);
1015 if (interface_opts->snaplen < 1)
1016 interface_opts->snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1017 else if (interface_opts->snaplen < snaplen_min)
1018 interface_opts->snaplen = snaplen_min;
1021 if (capture_opts->default_options.snaplen < 1)
1022 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1023 else if (capture_opts->default_options.snaplen < snaplen_min)
1024 capture_opts->default_options.snaplen = snaplen_min;
1030 capture_opts_trim_ring_num_files(capture_options *capture_opts)
1032 /* Check the value range of the ring_num_files parameter */
1033 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
1034 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
1035 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
1036 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
1037 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
1039 #if RINGBUFFER_MIN_NUM_FILES > 0
1040 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
1041 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
1042 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
1047 * If no interface was specified explicitly, pick a default.
1050 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
1051 const char *capture_device)
1055 /* Did the user specify an interface to use? */
1056 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
1057 /* yes they did, return immediately - nothing further to do here */
1061 /* No - is a default specified in the preferences file? */
1062 if (capture_device != NULL) {
1064 status = capture_opts_add_iface_opt(capture_opts, capture_device);
1067 /* No default in preferences file, just pick the first interface from the list of interfaces. */
1068 return capture_opts_add_iface_opt(capture_opts, "1");
1072 #define S_IFIFO _S_IFIFO
1075 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1078 /* copied from filesystem.c */
1080 capture_opts_test_for_fifo(const char *path)
1084 if (ws_stat64(path, &statb) < 0)
1087 if (S_ISFIFO(statb.st_mode))
1094 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1100 if (save_file != NULL) {
1101 /* We're writing to a capture file. */
1102 if (strcmp(save_file, "-") == 0) {
1103 /* Writing to stdout. */
1104 /* XXX - should we check whether it's a pipe? It's arguably
1105 silly to do "-w - >output_file" rather than "-w output_file",
1106 but by not checking we might be violating the Principle Of
1107 Least Astonishment. */
1110 /* not writing to stdout, test for a FIFO (aka named pipe) */
1111 err = capture_opts_test_for_fifo(save_file);
1114 case ENOENT: /* it doesn't exist, so we'll be creating it,
1115 and it won't be a FIFO */
1116 case 0: /* found it, but it's not a FIFO */
1119 case ESPIPE: /* it is a FIFO */
1123 default: /* couldn't stat it */
1124 break; /* ignore: later attempt to open */
1125 /* will generate a nice msg */
1134 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1136 interface_options *interface_opts;
1138 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, if_index);
1139 /* XXX - check if found? */
1141 g_free(interface_opts->name);
1142 g_free(interface_opts->descr);
1143 g_free(interface_opts->console_display_name);
1144 g_free(interface_opts->cfilter);
1145 g_free(interface_opts->timestamp_type);
1147 g_free(interface_opts->extcap);
1148 g_free(interface_opts->extcap_fifo);
1149 if (interface_opts->extcap_args)
1150 g_hash_table_unref(interface_opts->extcap_args);
1151 if (interface_opts->extcap_pid != INVALID_EXTCAP_PID)
1152 g_spawn_close_pid(interface_opts->extcap_pid);
1153 g_free(interface_opts->extcap_userdata);
1154 g_free(interface_opts->extcap_control_in);
1155 g_free(interface_opts->extcap_control_out);
1157 #ifdef HAVE_PCAP_REMOTE
1158 if (interface_opts->src_type == CAPTURE_IFREMOTE) {
1159 g_free(interface_opts->remote_host);
1160 g_free(interface_opts->remote_port);
1161 g_free(interface_opts->auth_username);
1162 g_free(interface_opts->auth_password);
1165 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1171 * Add all non-hidden selected interfaces in the "all interfaces" list
1172 * to the list of interfaces for the capture.
1175 collect_ifaces(capture_options *capture_opts)
1178 interface_t *device;
1179 interface_options interface_opts;
1181 /* Empty out the existing list of interfaces. */
1182 for (i = capture_opts->ifaces->len; i != 0; i--)
1183 capture_opts_del_iface(capture_opts, i-1);
1185 /* Now fill the list up again. */
1186 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1187 device = &g_array_index(capture_opts->all_ifaces, interface_t, i);
1188 if (!device->hidden && device->selected) {
1189 interface_opts.name = g_strdup(device->name);
1190 interface_opts.descr = g_strdup(device->display_name);
1191 interface_opts.console_display_name = g_strdup(device->name);
1192 interface_opts.linktype = device->active_dlt;
1193 interface_opts.cfilter = g_strdup(device->cfilter);
1194 interface_opts.timestamp_type = g_strdup(device->timestamp_type);
1195 interface_opts.snaplen = device->snaplen;
1196 interface_opts.has_snaplen = device->has_snaplen;
1197 interface_opts.promisc_mode = device->pmode;
1198 interface_opts.if_type = device->if_info.type;
1200 interface_opts.extcap = g_strdup(device->if_info.extcap);
1201 interface_opts.extcap_fifo = NULL;
1202 interface_opts.extcap_userdata = NULL;
1203 interface_opts.extcap_args = device->external_cap_args_settings;
1204 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1205 if (interface_opts.extcap_args)
1206 g_hash_table_ref(interface_opts.extcap_args);
1207 interface_opts.extcap_userdata = NULL;
1209 interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
1210 interface_opts.extcap_control_in_h = INVALID_HANDLE_VALUE;
1211 interface_opts.extcap_control_out_h = INVALID_HANDLE_VALUE;
1213 interface_opts.extcap_control_in = NULL;
1214 interface_opts.extcap_control_out = NULL;
1216 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1217 interface_opts.buffer_size = device->buffer;
1219 #ifdef HAVE_PCAP_CREATE
1220 interface_opts.monitor_mode = device->monitor_mode_enabled;
1222 #ifdef HAVE_PCAP_REMOTE
1223 interface_opts.src_type = CAPTURE_IFREMOTE;
1224 interface_opts.remote_host = g_strdup(device->remote_opts.remote_host_opts.remote_host);
1225 interface_opts.remote_port = g_strdup(device->remote_opts.remote_host_opts.remote_port);
1226 interface_opts.auth_type = device->remote_opts.remote_host_opts.auth_type;
1227 interface_opts.auth_username = g_strdup(device->remote_opts.remote_host_opts.auth_username);
1228 interface_opts.auth_password = g_strdup(device->remote_opts.remote_host_opts.auth_password);
1229 interface_opts.datatx_udp = device->remote_opts.remote_host_opts.datatx_udp;
1230 interface_opts.nocap_rpcap = device->remote_opts.remote_host_opts.nocap_rpcap;
1231 interface_opts.nocap_local = device->remote_opts.remote_host_opts.nocap_local;
1233 #ifdef HAVE_PCAP_SETSAMPLING
1234 interface_opts.sampling_method = device->remote_opts.sampling_method;
1235 interface_opts.sampling_param = device->remote_opts.sampling_param;
1237 g_array_append_val(capture_opts->ifaces, interface_opts);
1245 capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
1247 link_row* e = (link_row*)elem;
1254 capture_opts_free_interface_t_addrs(gpointer elem, gpointer unused _U_)
1260 capture_opts_free_interface_t(interface_t *device)
1262 if (device != NULL) {
1263 g_free(device->name);
1264 g_free(device->display_name);
1265 g_free(device->friendly_name);
1266 g_free(device->addresses);
1267 g_free(device->cfilter);
1268 g_free(device->timestamp_type);
1269 g_list_foreach(device->links,
1270 capture_opts_free_interface_t_links, NULL);
1271 g_list_free(device->links);
1272 #ifdef HAVE_PCAP_REMOTE
1273 g_free(device->remote_opts.remote_host_opts.remote_host);
1274 g_free(device->remote_opts.remote_host_opts.remote_port);
1275 g_free(device->remote_opts.remote_host_opts.auth_username);
1276 g_free(device->remote_opts.remote_host_opts.auth_password);
1278 g_free(device->if_info.name);
1279 g_free(device->if_info.friendly_name);
1280 g_free(device->if_info.vendor_description);
1281 g_slist_foreach(device->if_info.addrs,
1282 capture_opts_free_interface_t_addrs, NULL);
1283 g_slist_free(device->if_info.addrs);
1285 g_free(device->if_info.extcap);
1290 #endif /* HAVE_LIBPCAP */
1293 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1298 * indent-tabs-mode: nil
1301 * vi: set shiftwidth=4 tabstop=8 expandtab:
1302 * :indentSize=4:tabSize=8:noTabs=true: