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;
145 /* log content of capture_opts */
147 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
150 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
152 for (i = 0; i < capture_opts->ifaces->len; i++) {
153 interface_options *interface_opts;
155 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
156 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts->name ? interface_opts->name : "(unspecified)");
157 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts->descr ? interface_opts->descr : "(unspecified)");
158 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts->console_display_name ? interface_opts->console_display_name : "(unspecified)");
159 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts->cfilter ? interface_opts->cfilter : "(unspecified)");
160 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts->has_snaplen, interface_opts->snaplen);
161 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts->linktype);
162 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);
166 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
167 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts->buffer_size);
169 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts->monitor_mode?"TRUE":"FALSE");
170 #ifdef HAVE_PCAP_REMOTE
171 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
172 interface_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" :
173 interface_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" :
175 if (interface_opts->src_type == CAPTURE_IFREMOTE) {
176 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts->remote_host ? interface_opts->remote_host : "(unspecified)");
177 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts->remote_port ? interface_opts->remote_port : "(unspecified)");
179 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
180 interface_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" :
181 interface_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
183 if (interface_opts->auth_type == CAPTURE_AUTH_PWD) {
184 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts->auth_username ? interface_opts->auth_username : "(unspecified)");
185 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
187 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts->datatx_udp);
188 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts->nocap_rpcap);
189 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts->nocap_local);
191 #ifdef HAVE_PCAP_SETSAMPLING
192 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts->sampling_method);
193 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts->sampling_param);
195 g_log(log_domain, log_level, "Timestamp type [%02d] : %s", i, interface_opts->timestamp_type);
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");
203 g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
204 g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
205 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
206 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
208 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
209 #ifdef HAVE_PCAP_REMOTE
210 g_log(log_domain, log_level, "Capture source[df] : %s",
211 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
212 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
214 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
215 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
216 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
218 g_log(log_domain, log_level, "Authentication[df] : %s",
219 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
220 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
222 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
223 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
224 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
226 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
227 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
228 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
230 #ifdef HAVE_PCAP_SETSAMPLING
231 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
232 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
234 g_log(log_domain, log_level, "Timestamp type [df] : %s", capture_opts->default_options.timestamp_type ? capture_opts->default_options.timestamp_type : "(unspecified)");
235 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
236 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
237 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
238 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
239 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
240 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
242 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
243 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
244 g_log(log_domain, log_level, "FileInterval (%u) : %u", capture_opts->has_file_interval, capture_opts->file_interval);
245 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
247 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
248 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
249 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
250 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
254 * Given a string of the form "<autostop criterion>:<value>", as might appear
255 * as an argument to a "-a" option, parse it and set the criterion in
256 * question. Return an indication of whether it succeeded or failed
260 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
264 colonp = strchr(autostoparg, ':');
272 * Skip over any white space (there probably won't be any, but
273 * as we allow it in the preferences file, we might as well
276 while (g_ascii_isspace(*p))
280 * Put the colon back, so if our caller uses, in an
281 * error message, the string they passed us, the message
287 if (strcmp(autostoparg,"duration") == 0) {
288 capture_opts->has_autostop_duration = TRUE;
289 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
290 } else if (strcmp(autostoparg,"filesize") == 0) {
291 capture_opts->has_autostop_filesize = TRUE;
292 capture_opts->autostop_filesize = get_nonzero_guint32(p,"autostop filesize");
293 } else if (strcmp(autostoparg,"files") == 0) {
294 capture_opts->multi_files_on = TRUE;
295 capture_opts->has_autostop_files = TRUE;
296 capture_opts->autostop_files = get_positive_int(p,"autostop files");
300 *colonp = ':'; /* put the colon back */
304 static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
308 char* filter_exp = NULL;
310 colonp = strchr(arg, ':');
315 if (strcmp(arg, "predef") == 0) {
318 filterItem = get_filter_list_first(CFILTER_LIST);
319 while (filterItem != NULL) {
320 filter_def *filterDef;
322 filterDef = (filter_def*)filterItem->data;
323 if (strcmp(val, filterDef->name) == 0) {
324 filter_exp = g_strdup(filterDef->strval);
327 filterItem = filterItem->next;
332 if (filter_exp == NULL) {
333 /* No filter expression found yet; fallback to previous implemention
334 and assume the arg contains a filter expression */
336 *colonp = ':'; /* restore colon */
338 filter_exp = g_strdup(arg);
341 if (capture_opts->ifaces->len > 0) {
342 interface_options *interface_opts;
344 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
345 g_free(interface_opts->cfilter);
346 interface_opts->cfilter = filter_exp;
350 g_free(capture_opts->default_options.cfilter);
351 capture_opts->default_options.cfilter = filter_exp;
357 * Given a string of the form "<ring buffer file>:<duration>", as might appear
358 * as an argument to a "-b" option, parse it and set the arguments in
359 * question. Return an indication of whether it succeeded or failed
363 get_ring_arguments(capture_options *capture_opts, const char *arg)
365 gchar *p = NULL, *colonp;
367 colonp = strchr(arg, ':');
375 * Skip over any white space (there probably won't be any, but
376 * as we allow it in the preferences file, we might as well
379 while (g_ascii_isspace(*p))
383 * Put the colon back, so if our caller uses, in an
384 * error message, the string they passed us, the message
391 if (strcmp(arg,"files") == 0) {
392 capture_opts->has_ring_num_files = TRUE;
393 capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
394 } else if (strcmp(arg,"filesize") == 0) {
395 capture_opts->has_autostop_filesize = TRUE;
396 capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
397 } else if (strcmp(arg,"duration") == 0) {
398 capture_opts->has_file_duration = TRUE;
399 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
400 } else if (strcmp(arg,"interval") == 0) {
401 capture_opts->has_file_interval = TRUE;
402 capture_opts->file_interval = get_positive_int(p, "ring buffer interval");
405 *colonp = ':'; /* put the colon back */
409 #ifdef HAVE_PCAP_SETSAMPLING
411 * Given a string of the form "<sampling type>:<value>", as might appear
412 * as an argument to a "-m" option, parse it and set the arguments in
413 * question. Return an indication of whether it succeeded or failed
417 get_sampling_arguments(capture_options *capture_opts, const char *arg)
419 gchar *p = NULL, *colonp;
421 colonp = strchr(arg, ':');
428 while (g_ascii_isspace(*p))
435 if (strcmp(arg, "count") == 0) {
436 if (capture_opts->ifaces->len > 0) {
437 interface_options *interface_opts;
439 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
440 interface_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
441 interface_opts->sampling_param = get_positive_int(p, "sampling count");
443 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
444 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
446 } else if (strcmp(arg, "timer") == 0) {
447 if (capture_opts->ifaces->len > 0) {
448 interface_options *interface_opts;
450 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
451 interface_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
452 interface_opts->sampling_param = get_positive_int(p, "sampling timer");
454 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
455 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
463 #ifdef HAVE_PCAP_REMOTE
465 * Given a string of the form "<username>:<password>", as might appear
466 * as an argument to a "-A" option, parse it and set the arguments in
467 * question. Return an indication of whether it succeeded or failed
471 get_auth_arguments(capture_options *capture_opts, const char *arg)
473 gchar *p = NULL, *colonp;
475 colonp = strchr(arg, ':');
482 while (g_ascii_isspace(*p))
485 if (capture_opts->ifaces->len > 0) {
486 interface_options *interface_opts;
488 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
489 interface_opts->auth_type = CAPTURE_AUTH_PWD;
490 interface_opts->auth_username = g_strdup(arg);
491 interface_opts->auth_password = g_strdup(p);
493 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
494 capture_opts->default_options.auth_username = g_strdup(arg);
495 capture_opts->default_options.auth_password = g_strdup(p);
503 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
511 interface_options interface_opts;
514 * If the argument is a number, treat it as an index into the list
515 * of adapters, as printed by "tshark -D".
517 * This should be OK on UNIX systems, as interfaces shouldn't have
518 * names that begin with digits. It can be useful on Windows, where
519 * more than one interface can have the same name.
521 adapter_index = strtol(optarg_str_p, &p, 10);
522 if (p != NULL && *p == '\0') {
523 if (adapter_index < 0) {
524 cmdarg_err("The specified adapter index is a negative number");
527 if (adapter_index > INT_MAX) {
528 cmdarg_err("The specified adapter index is too large (greater than %d)",
532 if (adapter_index == 0) {
533 cmdarg_err("There is no interface with that adapter index");
536 if_list = capture_interface_list(&err, &err_str, NULL);
537 if (if_list == NULL) {
539 cmdarg_err("There are no interfaces on which a capture can be done");
541 cmdarg_err("%s", err_str);
546 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
547 if (if_info == NULL) {
548 cmdarg_err("There is no interface with that adapter index");
551 interface_opts.name = g_strdup(if_info->name);
552 if (if_info->friendly_name != NULL) {
554 * We have a friendly name for the interface, so display that
555 * instead of the interface name/guid.
557 * XXX - on UN*X, the interface name is not quite so ugly,
558 * and might be more familiar to users; display them both?
560 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
562 /* fallback to the interface name */
563 interface_opts.console_display_name = g_strdup(if_info->name);
565 interface_opts.if_type = if_info->type;
566 interface_opts.extcap = g_strdup(if_info->extcap);
567 free_interface_list(if_list);
568 } else if (capture_opts->capture_child) {
569 /* In Wireshark capture child mode, thus proper device name is supplied. */
570 /* No need for trying to match it for friendly names. */
571 interface_opts.name = g_strdup(optarg_str_p);
572 interface_opts.console_display_name = g_strdup(optarg_str_p);
573 interface_opts.if_type = capture_opts->default_options.if_type;
574 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
577 * Retrieve the interface list so that we can search for the
578 * specified option amongst both the interface names and the
579 * friendly names and so that we find the friendly name even
580 * if an interface name was specified.
582 * If we can't get the list, just use the specified option as
583 * the interface name, so that the user can try specifying an
584 * interface explicitly for testing purposes.
586 if_list = capture_interface_list(&err, NULL, NULL);
587 if (if_list != NULL) {
588 /* try and do an exact match (case insensitive) */
593 for (if_entry = g_list_first(if_list); if_entry != NULL;
594 if_entry = g_list_next(if_entry))
596 if_info = (if_info_t *)if_entry->data;
597 /* exact name check */
598 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
599 /* exact match on the interface name, use that for displaying etc */
600 interface_opts.name = g_strdup(if_info->name);
602 if (if_info->friendly_name != NULL) {
604 * If we have a friendly name, use that for the
605 * console display name, as it is the basis for
606 * the auto generated temp filename.
608 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
610 interface_opts.console_display_name = g_strdup(if_info->name);
612 interface_opts.if_type = if_info->type;
613 interface_opts.extcap = g_strdup(if_info->extcap);
618 /* exact friendly name check */
619 if (if_info->friendly_name != NULL &&
620 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
621 /* exact match - use the friendly name for display */
622 interface_opts.name = g_strdup(if_info->name);
623 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
624 interface_opts.if_type = if_info->type;
625 interface_opts.extcap = g_strdup(if_info->extcap);
631 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
633 size_t prefix_length;
635 prefix_length = strlen(optarg_str_p);
636 for (if_entry = g_list_first(if_list); if_entry != NULL;
637 if_entry = g_list_next(if_entry))
639 if_info = (if_info_t *)if_entry->data;
641 if (if_info->friendly_name != NULL &&
642 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
643 /* prefix match - use the friendly name for display */
644 interface_opts.name = g_strdup(if_info->name);
645 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
646 interface_opts.if_type = if_info->type;
647 interface_opts.extcap = g_strdup(if_info->extcap);
655 * We didn't find the interface in the list; just use
656 * the specified name, so that, for example, if an
657 * interface doesn't show up in the list for some
658 * reason, the user can try specifying it explicitly
659 * for testing purposes.
661 interface_opts.name = g_strdup(optarg_str_p);
662 interface_opts.console_display_name = g_strdup(optarg_str_p);
663 interface_opts.if_type = capture_opts->default_options.if_type;
664 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
666 free_interface_list(if_list);
668 interface_opts.name = g_strdup(optarg_str_p);
669 interface_opts.console_display_name = g_strdup(optarg_str_p);
670 interface_opts.if_type = capture_opts->default_options.if_type;
671 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
675 /* We don't set iface_descr here because doing so requires
676 * capture_ui_utils.c which requires epan/prefs.c which is
677 * probably a bit too much dependency for here...
679 interface_opts.descr = g_strdup(capture_opts->default_options.descr);
680 interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
681 interface_opts.snaplen = capture_opts->default_options.snaplen;
682 interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
683 interface_opts.linktype = capture_opts->default_options.linktype;
684 interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
685 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
686 interface_opts.extcap_args = NULL;
687 interface_opts.extcap_pid = WS_INVALID_PID;
688 interface_opts.extcap_pipedata = NULL;
690 interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
691 interface_opts.extcap_control_in_h = INVALID_HANDLE_VALUE;
692 interface_opts.extcap_control_out_h = INVALID_HANDLE_VALUE;
694 interface_opts.extcap_control_in = g_strdup(capture_opts->default_options.extcap_control_in);
695 interface_opts.extcap_control_out = g_strdup(capture_opts->default_options.extcap_control_out);
696 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
697 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
699 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
700 #ifdef HAVE_PCAP_REMOTE
701 interface_opts.src_type = capture_opts->default_options.src_type;
702 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
703 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
704 interface_opts.auth_type = capture_opts->default_options.auth_type;
705 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
706 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
707 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
708 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
709 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
711 #ifdef HAVE_PCAP_SETSAMPLING
712 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
713 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
715 interface_opts.timestamp_type = capture_opts->default_options.timestamp_type;
717 g_array_append_val(capture_opts->ifaces, interface_opts);
724 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
729 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
730 if (capture_opts->capture_comment) {
731 cmdarg_err("--capture-comment can be set only once per file");
734 capture_opts->capture_comment = g_strdup(optarg_str_p);
736 case 'a': /* autostop criteria */
737 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
738 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
742 #ifdef HAVE_PCAP_REMOTE
744 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
745 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
750 case 'b': /* Ringbuffer option */
751 capture_opts->multi_files_on = TRUE;
752 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
753 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
757 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
758 case 'B': /* Buffer size */
759 if (capture_opts->ifaces->len > 0) {
760 interface_options *interface_opts;
762 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
763 interface_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
765 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
769 case 'c': /* Capture n packets */
770 capture_opts->has_autostop_packets = TRUE;
771 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
773 case 'f': /* capture filter */
774 get_filter_arguments(capture_opts, optarg_str_p);
776 case 'g': /* enable group read access on the capture file(s) */
777 capture_opts->group_read_access = TRUE;
779 case 'H': /* Hide capture info dialog box */
780 capture_opts->show_info = FALSE;
782 case LONGOPT_SET_TSTAMP_TYPE: /* Set capture time stamp type */
783 if (capture_opts->ifaces->len > 0) {
784 interface_options *interface_opts;
786 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
787 g_free(interface_opts->timestamp_type);
788 interface_opts->timestamp_type = g_strdup(optarg_str_p);
790 g_free(capture_opts->default_options.timestamp_type);
791 capture_opts->default_options.timestamp_type = g_strdup(optarg_str_p);
794 case 'i': /* Use interface x */
795 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
800 #ifdef HAVE_PCAP_CREATE
801 case 'I': /* Capture in monitor mode */
802 if (capture_opts->ifaces->len > 0) {
803 interface_options *interface_opts;
805 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
806 interface_opts->monitor_mode = TRUE;
808 capture_opts->default_options.monitor_mode = TRUE;
812 case 'k': /* Start capture immediately */
813 *start_capture = TRUE;
815 /*case 'l':*/ /* Automatic scrolling in live capture mode */
816 #ifdef HAVE_PCAP_SETSAMPLING
818 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
819 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
824 case 'n': /* Use pcapng format */
825 capture_opts->use_pcapng = TRUE;
827 case 'p': /* Don't capture in promiscuous mode */
828 if (capture_opts->ifaces->len > 0) {
829 interface_options *interface_opts;
831 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
832 interface_opts->promisc_mode = FALSE;
834 capture_opts->default_options.promisc_mode = FALSE;
837 case 'P': /* Use pcap format */
838 capture_opts->use_pcapng = FALSE;
840 #ifdef HAVE_PCAP_REMOTE
842 if (capture_opts->ifaces->len > 0) {
843 interface_options *interface_opts;
845 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
846 interface_opts->nocap_rpcap = FALSE;
848 capture_opts->default_options.nocap_rpcap = FALSE;
852 case 's': /* Set the snapshot (capture) length */
853 snaplen = get_natural_int(optarg_str_p, "snapshot length");
855 * Make a snapshot length of 0 equivalent to the maximum packet
856 * length, mirroring what tcpdump does.
859 snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
860 if (capture_opts->ifaces->len > 0) {
861 interface_options *interface_opts;
863 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
864 interface_opts->has_snaplen = TRUE;
865 interface_opts->snaplen = snaplen;
867 capture_opts->default_options.snaplen = snaplen;
868 capture_opts->default_options.has_snaplen = TRUE;
871 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
872 capture_opts->real_time_mode = TRUE;
874 #ifdef HAVE_PCAP_REMOTE
876 if (capture_opts->ifaces->len > 0) {
877 interface_options *interface_opts;
879 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
880 interface_opts->datatx_udp = TRUE;
882 capture_opts->default_options.datatx_udp = TRUE;
886 case 'w': /* Write to capture file x */
887 capture_opts->saving_to_file = TRUE;
888 g_free(capture_opts->save_file);
889 capture_opts->save_file = g_strdup(optarg_str_p);
890 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
892 case 'y': /* Set the pcap data link type */
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 interface_opts->linktype = linktype_name_to_val(optarg_str_p);
898 if (interface_opts->linktype == -1) {
899 cmdarg_err("The specified data link type \"%s\" isn't valid",
904 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
905 if (capture_opts->default_options.linktype == -1) {
906 cmdarg_err("The specified data link type \"%s\" isn't valid",
913 /* the caller is responsible to send us only the right opt's */
914 g_assert_not_reached();
921 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name, int queries)
923 GList *lt_entry, *ts_entry;
925 if (queries & CAPS_QUERY_LINK_TYPES) {
926 if (caps->can_set_rfmon)
927 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
928 name, queries & CAPS_MONITOR_MODE ? "" : "not ");
930 printf("Data link types of interface %s (use option -y to set):\n", name);
931 for (lt_entry = caps->data_link_types; lt_entry != NULL;
932 lt_entry = g_list_next(lt_entry)) {
933 data_link_info_t *data_link_info = (data_link_info_t *)lt_entry->data;
934 printf(" %s", data_link_info->name);
935 if (data_link_info->description != NULL)
936 printf(" (%s)", data_link_info->description);
938 printf(" (not supported)");
943 if (queries & CAPS_QUERY_TIMESTAMP_TYPES) {
944 printf("Timestamp types of the interface (use option --time-stamp-type to set):\n");
945 for (ts_entry = caps->timestamp_types; ts_entry != NULL;
946 ts_entry = g_list_next(ts_entry)) {
947 timestamp_info_t *timestamp = (timestamp_info_t *)ts_entry->data;
948 printf(" %s", timestamp->name);
949 if (timestamp->description != NULL)
950 printf(" (%s)", timestamp->description);
958 /* Print an ASCII-formatted list of interfaces. */
960 capture_opts_print_interfaces(GList *if_list)
966 i = 1; /* Interface id number */
967 for (if_entry = g_list_first(if_list); if_entry != NULL;
968 if_entry = g_list_next(if_entry)) {
969 if_info = (if_info_t *)if_entry->data;
970 printf("%d. %s", i++, if_info->name);
972 /* Print the interface friendly name, if it exists;
973 if not fall back to vendor description, if it exists. */
974 if (if_info->friendly_name != NULL){
975 printf(" (%s)", if_info->friendly_name);
977 if (if_info->vendor_description != NULL)
978 printf(" (%s)", if_info->vendor_description);
986 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
989 interface_options *interface_opts;
991 if (capture_opts->ifaces->len > 0) {
992 for (i = 0; i < capture_opts->ifaces->len; i++) {
993 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, 0);
994 if (interface_opts->snaplen < 1)
995 interface_opts->snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
996 else if (interface_opts->snaplen < snaplen_min)
997 interface_opts->snaplen = snaplen_min;
1000 if (capture_opts->default_options.snaplen < 1)
1001 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
1002 else if (capture_opts->default_options.snaplen < snaplen_min)
1003 capture_opts->default_options.snaplen = snaplen_min;
1009 capture_opts_trim_ring_num_files(capture_options *capture_opts)
1011 /* Check the value range of the ring_num_files parameter */
1012 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
1013 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
1014 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
1015 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
1016 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
1018 #if RINGBUFFER_MIN_NUM_FILES > 0
1019 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
1020 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
1021 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
1026 * If no interface was specified explicitly, pick a default.
1029 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
1030 const char *capture_device)
1034 /* Did the user specify an interface to use? */
1035 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
1036 /* yes they did, return immediately - nothing further to do here */
1040 /* No - is a default specified in the preferences file? */
1041 if (capture_device != NULL) {
1043 status = capture_opts_add_iface_opt(capture_opts, capture_device);
1046 /* No default in preferences file, just pick the first interface from the list of interfaces. */
1047 return capture_opts_add_iface_opt(capture_opts, "1");
1051 #define S_IFIFO _S_IFIFO
1054 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1057 /* copied from filesystem.c */
1059 capture_opts_test_for_fifo(const char *path)
1063 if (ws_stat64(path, &statb) < 0)
1066 if (S_ISFIFO(statb.st_mode))
1073 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1079 if (save_file != NULL) {
1080 /* We're writing to a capture file. */
1081 if (strcmp(save_file, "-") == 0) {
1082 /* Writing to stdout. */
1083 /* XXX - should we check whether it's a pipe? It's arguably
1084 silly to do "-w - >output_file" rather than "-w output_file",
1085 but by not checking we might be violating the Principle Of
1086 Least Astonishment. */
1089 /* not writing to stdout, test for a FIFO (aka named pipe) */
1090 err = capture_opts_test_for_fifo(save_file);
1093 case ENOENT: /* it doesn't exist, so we'll be creating it,
1094 and it won't be a FIFO */
1095 case 0: /* found it, but it's not a FIFO */
1098 case ESPIPE: /* it is a FIFO */
1102 default: /* couldn't stat it */
1103 break; /* ignore: later attempt to open */
1104 /* will generate a nice msg */
1113 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1115 interface_options *interface_opts;
1117 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, if_index);
1118 /* XXX - check if found? */
1120 g_free(interface_opts->name);
1121 g_free(interface_opts->descr);
1122 g_free(interface_opts->console_display_name);
1123 g_free(interface_opts->cfilter);
1124 g_free(interface_opts->timestamp_type);
1125 g_free(interface_opts->extcap);
1126 g_free(interface_opts->extcap_fifo);
1127 if (interface_opts->extcap_args)
1128 g_hash_table_unref(interface_opts->extcap_args);
1129 if (interface_opts->extcap_pid != WS_INVALID_PID)
1130 g_spawn_close_pid(interface_opts->extcap_pid);
1131 g_free(interface_opts->extcap_pipedata);
1132 g_free(interface_opts->extcap_control_in);
1133 g_free(interface_opts->extcap_control_out);
1134 #ifdef HAVE_PCAP_REMOTE
1135 if (interface_opts->src_type == CAPTURE_IFREMOTE) {
1136 g_free(interface_opts->remote_host);
1137 g_free(interface_opts->remote_port);
1138 g_free(interface_opts->auth_username);
1139 g_free(interface_opts->auth_password);
1142 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1148 * Add all non-hidden selected interfaces in the "all interfaces" list
1149 * to the list of interfaces for the capture.
1152 collect_ifaces(capture_options *capture_opts)
1155 interface_t *device;
1156 interface_options interface_opts;
1158 /* Empty out the existing list of interfaces. */
1159 for (i = capture_opts->ifaces->len; i != 0; i--)
1160 capture_opts_del_iface(capture_opts, i-1);
1162 /* Now fill the list up again. */
1163 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1164 device = &g_array_index(capture_opts->all_ifaces, interface_t, i);
1165 if (!device->hidden && device->selected) {
1166 interface_opts.name = g_strdup(device->name);
1167 interface_opts.descr = g_strdup(device->display_name);
1168 interface_opts.console_display_name = g_strdup(device->name);
1169 interface_opts.linktype = device->active_dlt;
1170 interface_opts.cfilter = g_strdup(device->cfilter);
1171 interface_opts.timestamp_type = g_strdup(device->timestamp_type);
1172 interface_opts.snaplen = device->snaplen;
1173 interface_opts.has_snaplen = device->has_snaplen;
1174 interface_opts.promisc_mode = device->pmode;
1175 interface_opts.if_type = device->if_info.type;
1176 interface_opts.extcap = g_strdup(device->if_info.extcap);
1177 interface_opts.extcap_fifo = NULL;
1178 interface_opts.extcap_pipedata = NULL;
1179 interface_opts.extcap_args = device->external_cap_args_settings;
1180 interface_opts.extcap_pid = WS_INVALID_PID;
1181 if (interface_opts.extcap_args)
1182 g_hash_table_ref(interface_opts.extcap_args);
1183 interface_opts.extcap_pipedata = NULL;
1185 interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
1186 interface_opts.extcap_control_in_h = INVALID_HANDLE_VALUE;
1187 interface_opts.extcap_control_out_h = INVALID_HANDLE_VALUE;
1189 interface_opts.extcap_control_in = NULL;
1190 interface_opts.extcap_control_out = NULL;
1191 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1192 interface_opts.buffer_size = device->buffer;
1194 #ifdef HAVE_PCAP_CREATE
1195 interface_opts.monitor_mode = device->monitor_mode_enabled;
1197 #ifdef HAVE_PCAP_REMOTE
1198 interface_opts.src_type = CAPTURE_IFREMOTE;
1199 interface_opts.remote_host = g_strdup(device->remote_opts.remote_host_opts.remote_host);
1200 interface_opts.remote_port = g_strdup(device->remote_opts.remote_host_opts.remote_port);
1201 interface_opts.auth_type = device->remote_opts.remote_host_opts.auth_type;
1202 interface_opts.auth_username = g_strdup(device->remote_opts.remote_host_opts.auth_username);
1203 interface_opts.auth_password = g_strdup(device->remote_opts.remote_host_opts.auth_password);
1204 interface_opts.datatx_udp = device->remote_opts.remote_host_opts.datatx_udp;
1205 interface_opts.nocap_rpcap = device->remote_opts.remote_host_opts.nocap_rpcap;
1206 interface_opts.nocap_local = device->remote_opts.remote_host_opts.nocap_local;
1208 #ifdef HAVE_PCAP_SETSAMPLING
1209 interface_opts.sampling_method = device->remote_opts.sampling_method;
1210 interface_opts.sampling_param = device->remote_opts.sampling_param;
1212 g_array_append_val(capture_opts->ifaces, interface_opts);
1220 capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
1222 link_row* e = (link_row*)elem;
1229 capture_opts_free_interface_t(interface_t *device)
1231 if (device != NULL) {
1232 g_free(device->name);
1233 g_free(device->display_name);
1234 g_free(device->friendly_name);
1235 g_free(device->addresses);
1236 g_free(device->cfilter);
1237 g_free(device->timestamp_type);
1238 g_list_foreach(device->links,
1239 capture_opts_free_interface_t_links, NULL);
1240 g_list_free(device->links);
1241 #ifdef HAVE_PCAP_REMOTE
1242 g_free(device->remote_opts.remote_host_opts.remote_host);
1243 g_free(device->remote_opts.remote_host_opts.remote_port);
1244 g_free(device->remote_opts.remote_host_opts.auth_username);
1245 g_free(device->remote_opts.remote_host_opts.auth_password);
1247 g_free(device->if_info.name);
1248 g_free(device->if_info.friendly_name);
1249 g_free(device->if_info.vendor_description);
1250 g_slist_free_full(device->if_info.addrs, g_free);
1251 g_free(device->if_info.extcap);
1255 #endif /* HAVE_LIBPCAP */
1258 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1263 * indent-tabs-mode: nil
1266 * vi: set shiftwidth=4 tabstop=8 expandtab:
1267 * :indentSize=4:tabSize=8:noTabs=true: