2 * Routines for capture options setting
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #include "capture_opts.h"
37 #include "ringbuffer.h"
39 #include <wsutil/clopts_common.h>
40 #include <wsutil/cmdarg_err.h>
41 #include <wsutil/file_util.h>
43 #include "caputils/capture_ifinfo.h"
44 #include "caputils/capture-pcap-util.h"
46 #include "filter_files.h"
48 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
52 capture_opts_init(capture_options *capture_opts)
54 capture_opts->ifaces = g_array_new(FALSE, FALSE, sizeof(interface_options));
55 capture_opts->all_ifaces = g_array_new(FALSE, FALSE, sizeof(interface_t));
56 capture_opts->num_selected = 0;
57 capture_opts->default_options.name = NULL;
58 capture_opts->default_options.descr = NULL;
59 capture_opts->default_options.cfilter = NULL;
60 capture_opts->default_options.has_snaplen = FALSE;
61 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
62 capture_opts->default_options.linktype = -1; /* use interface default */
63 capture_opts->default_options.promisc_mode = TRUE;
64 capture_opts->default_options.if_type = IF_WIRED;
66 capture_opts->default_options.extcap = NULL;
67 capture_opts->default_options.extcap_fifo = NULL;
68 capture_opts->default_options.extcap_args = NULL;
69 capture_opts->default_options.extcap_userdata = NULL;
70 capture_opts->default_options.extcap_pid = INVALID_EXTCAP_PID;
72 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
73 capture_opts->default_options.buffer_size = DEFAULT_CAPTURE_BUFFER_SIZE;
75 capture_opts->default_options.monitor_mode = FALSE;
76 #ifdef HAVE_PCAP_REMOTE
77 capture_opts->default_options.src_type = CAPTURE_IFLOCAL;
78 capture_opts->default_options.remote_host = NULL;
79 capture_opts->default_options.remote_port = NULL;
80 capture_opts->default_options.auth_type = CAPTURE_AUTH_NULL;
81 capture_opts->default_options.auth_username = NULL;
82 capture_opts->default_options.auth_password = NULL;
83 capture_opts->default_options.datatx_udp = FALSE;
84 capture_opts->default_options.nocap_rpcap = TRUE;
85 capture_opts->default_options.nocap_local = FALSE;
87 #ifdef HAVE_PCAP_SETSAMPLING
88 capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
89 capture_opts->default_options.sampling_param = 0;
91 capture_opts->saving_to_file = FALSE;
92 capture_opts->save_file = NULL;
93 capture_opts->group_read_access = FALSE;
94 #ifdef PCAP_NG_DEFAULT
95 capture_opts->use_pcapng = TRUE; /* Save as pcap-ng by default */
97 capture_opts->use_pcapng = FALSE; /* Save as pcap by default */
99 capture_opts->real_time_mode = TRUE;
100 capture_opts->show_info = TRUE;
101 capture_opts->restart = FALSE;
102 capture_opts->orig_save_file = NULL;
104 capture_opts->multi_files_on = FALSE;
105 capture_opts->has_file_duration = FALSE;
106 capture_opts->file_duration = 60; /* 1 min */
107 capture_opts->has_ring_num_files = FALSE;
108 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
110 capture_opts->has_autostop_files = FALSE;
111 capture_opts->autostop_files = 1;
112 capture_opts->has_autostop_packets = FALSE;
113 capture_opts->autostop_packets = 0;
114 capture_opts->has_autostop_filesize = FALSE;
115 capture_opts->autostop_filesize = 1000; /* 1 MB */
116 capture_opts->has_autostop_duration = FALSE;
117 capture_opts->autostop_duration = 60; /* 1 min */
118 capture_opts->capture_comment = NULL;
120 capture_opts->output_to_pipe = FALSE;
121 capture_opts->capture_child = FALSE;
125 /* log content of capture_opts */
127 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
130 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
132 for (i = 0; i < capture_opts->ifaces->len; i++) {
133 interface_options interface_opts;
135 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
136 g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts.name ? interface_opts.name : "(unspecified)");
137 g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts.descr ? interface_opts.descr : "(unspecified)");
138 g_log(log_domain, log_level, "Console display name[%02d]: %s", i, interface_opts.console_display_name ? interface_opts.console_display_name : "(unspecified)");
139 g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts.cfilter ? interface_opts.cfilter : "(unspecified)");
140 g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
141 g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts.linktype);
142 g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
144 g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts.extcap ? interface_opts.extcap : "(unspecified)");
145 g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts.extcap_fifo ? interface_opts.extcap_fifo : "(unspecified)");
146 g_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts.extcap_pid);
148 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
149 g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts.buffer_size);
151 g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
152 #ifdef HAVE_PCAP_REMOTE
153 g_log(log_domain, log_level, "Capture source[%02d] : %s", i,
154 interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
155 interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
157 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
158 g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts.remote_host ? interface_opts.remote_host : "(unspecified)");
159 g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts.remote_port ? interface_opts.remote_port : "(unspecified)");
161 g_log(log_domain, log_level, "Authentication[%02d] : %s", i,
162 interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
163 interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
165 if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
166 g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts.auth_username ? interface_opts.auth_username : "(unspecified)");
167 g_log(log_domain, log_level, "Auth password[%02d] : <hidden>", i);
169 g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts.datatx_udp);
170 g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts.nocap_rpcap);
171 g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts.nocap_local);
173 #ifdef HAVE_PCAP_SETSAMPLING
174 g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts.sampling_method);
175 g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
178 g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)");
179 g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)");
180 g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)");
181 g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
182 g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype);
183 g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
185 g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
186 g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
188 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
189 g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size);
191 g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
192 #ifdef HAVE_PCAP_REMOTE
193 g_log(log_domain, log_level, "Capture source[df] : %s",
194 capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
195 capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
197 if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
198 g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)");
199 g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)");
201 g_log(log_domain, log_level, "Authentication[df] : %s",
202 capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
203 capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
205 if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
206 g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)");
207 g_log(log_domain, log_level, "Auth password[df] : <hidden>");
209 g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp);
210 g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap);
211 g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local);
213 #ifdef HAVE_PCAP_SETSAMPLING
214 g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
215 g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
217 g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file);
218 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
219 g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access);
220 g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
221 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
222 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
224 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
225 g_log(log_domain, log_level, "FileDuration (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
226 g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
228 g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
229 g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
230 g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
231 g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
235 * Given a string of the form "<autostop criterion>:<value>", as might appear
236 * as an argument to a "-a" option, parse it and set the criterion in
237 * question. Return an indication of whether it succeeded or failed
241 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
245 colonp = strchr(autostoparg, ':');
253 * Skip over any white space (there probably won't be any, but
254 * as we allow it in the preferences file, we might as well
257 while (g_ascii_isspace(*p))
261 * Put the colon back, so if our caller uses, in an
262 * error message, the string they passed us, the message
268 if (strcmp(autostoparg,"duration") == 0) {
269 capture_opts->has_autostop_duration = TRUE;
270 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
271 } else if (strcmp(autostoparg,"filesize") == 0) {
272 capture_opts->has_autostop_filesize = TRUE;
273 capture_opts->autostop_filesize = get_nonzero_guint32(p,"autostop filesize");
274 } else if (strcmp(autostoparg,"files") == 0) {
275 capture_opts->multi_files_on = TRUE;
276 capture_opts->has_autostop_files = TRUE;
277 capture_opts->autostop_files = get_positive_int(p,"autostop files");
281 *colonp = ':'; /* put the colon back */
285 static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
289 char* filter_exp = NULL;
291 colonp = strchr(arg, ':');
296 if (strcmp(arg, "predef") == 0) {
299 filterItem = get_filter_list_first(CFILTER_LIST);
300 while (filterItem != NULL) {
301 filter_def *filterDef;
303 filterDef = (filter_def*)filterItem->data;
304 if (strcmp(val, filterDef->name) == 0) {
305 filter_exp = g_strdup(filterDef->strval);
308 filterItem = filterItem->next;
313 if (filter_exp == NULL) {
314 /* No filter expression found yet; fallback to previous implemention
315 and assume the arg contains a filter expression */
317 *colonp = ':'; /* restore colon */
319 filter_exp = g_strdup(arg);
322 if (capture_opts->ifaces->len > 0) {
323 interface_options interface_opts;
325 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
326 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
327 g_free(interface_opts.cfilter);
328 interface_opts.cfilter = filter_exp;
329 g_array_append_val(capture_opts->ifaces, interface_opts);
333 g_free(capture_opts->default_options.cfilter);
334 capture_opts->default_options.cfilter = filter_exp;
340 * Given a string of the form "<ring buffer file>:<duration>", as might appear
341 * as an argument to a "-b" option, parse it and set the arguments in
342 * question. Return an indication of whether it succeeded or failed
346 get_ring_arguments(capture_options *capture_opts, const char *arg)
348 gchar *p = NULL, *colonp;
350 colonp = strchr(arg, ':');
358 * Skip over any white space (there probably won't be any, but
359 * as we allow it in the preferences file, we might as well
362 while (g_ascii_isspace(*p))
366 * Put the colon back, so if our caller uses, in an
367 * error message, the string they passed us, the message
374 if (strcmp(arg,"files") == 0) {
375 capture_opts->has_ring_num_files = TRUE;
376 capture_opts->ring_num_files = get_nonzero_guint32(p, "number of ring buffer files");
377 } else if (strcmp(arg,"filesize") == 0) {
378 capture_opts->has_autostop_filesize = TRUE;
379 capture_opts->autostop_filesize = get_nonzero_guint32(p, "ring buffer filesize");
380 } else if (strcmp(arg,"duration") == 0) {
381 capture_opts->has_file_duration = TRUE;
382 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
385 *colonp = ':'; /* put the colon back */
389 #ifdef HAVE_PCAP_SETSAMPLING
391 * Given a string of the form "<sampling type>:<value>", as might appear
392 * as an argument to a "-m" option, parse it and set the arguments in
393 * question. Return an indication of whether it succeeded or failed
397 get_sampling_arguments(capture_options *capture_opts, const char *arg)
399 gchar *p = NULL, *colonp;
401 colonp = strchr(arg, ':');
408 while (g_ascii_isspace(*p))
415 if (strcmp(arg, "count") == 0) {
416 if (capture_opts->ifaces->len > 0) {
417 interface_options interface_opts;
419 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
420 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
421 interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
422 interface_opts.sampling_param = get_positive_int(p, "sampling count");
423 g_array_append_val(capture_opts->ifaces, interface_opts);
425 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
426 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
428 } else if (strcmp(arg, "timer") == 0) {
429 if (capture_opts->ifaces->len > 0) {
430 interface_options interface_opts;
432 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
433 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
434 interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
435 interface_opts.sampling_param = get_positive_int(p, "sampling timer");
436 g_array_append_val(capture_opts->ifaces, interface_opts);
438 capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
439 capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
447 #ifdef HAVE_PCAP_REMOTE
449 * Given a string of the form "<username>:<password>", as might appear
450 * as an argument to a "-A" option, parse it and set the arguments in
451 * question. Return an indication of whether it succeeded or failed
455 get_auth_arguments(capture_options *capture_opts, const char *arg)
457 gchar *p = NULL, *colonp;
459 colonp = strchr(arg, ':');
466 while (g_ascii_isspace(*p))
469 if (capture_opts->ifaces->len > 0) {
470 interface_options interface_opts;
472 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
473 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
474 interface_opts.auth_type = CAPTURE_AUTH_PWD;
475 interface_opts.auth_username = g_strdup(arg);
476 interface_opts.auth_password = g_strdup(p);
477 g_array_append_val(capture_opts->ifaces, interface_opts);
479 capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
480 capture_opts->default_options.auth_username = g_strdup(arg);
481 capture_opts->default_options.auth_password = g_strdup(p);
489 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
497 interface_options interface_opts;
500 * If the argument is a number, treat it as an index into the list
501 * of adapters, as printed by "tshark -D".
503 * This should be OK on UNIX systems, as interfaces shouldn't have
504 * names that begin with digits. It can be useful on Windows, where
505 * more than one interface can have the same name.
507 adapter_index = strtol(optarg_str_p, &p, 10);
508 if (p != NULL && *p == '\0') {
509 if (adapter_index < 0) {
510 cmdarg_err("The specified adapter index is a negative number");
513 if (adapter_index > INT_MAX) {
514 cmdarg_err("The specified adapter index is too large (greater than %d)",
518 if (adapter_index == 0) {
519 cmdarg_err("There is no interface with that adapter index");
522 if_list = capture_interface_list(&err, &err_str, NULL);
523 if (if_list == NULL) {
525 cmdarg_err("There are no interfaces on which a capture can be done");
527 cmdarg_err("%s", err_str);
532 if_info = (if_info_t *)g_list_nth_data(if_list, (int)(adapter_index - 1));
533 if (if_info == NULL) {
534 cmdarg_err("There is no interface with that adapter index");
537 interface_opts.name = g_strdup(if_info->name);
538 if (if_info->friendly_name != NULL) {
540 * We have a friendly name for the interface, so display that
541 * instead of the interface name/guid.
543 * XXX - on UN*X, the interface name is not quite so ugly,
544 * and might be more familiar to users; display them both?
546 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
548 /* fallback to the interface name */
549 interface_opts.console_display_name = g_strdup(if_info->name);
551 interface_opts.if_type = if_info->type;
553 interface_opts.extcap = g_strdup(if_info->extcap);
555 free_interface_list(if_list);
556 } else if (capture_opts->capture_child) {
557 /* In Wireshark capture child mode, thus proper device name is supplied. */
558 /* No need for trying to match it for friendly names. */
559 interface_opts.name = g_strdup(optarg_str_p);
560 interface_opts.console_display_name = g_strdup(optarg_str_p);
561 interface_opts.if_type = capture_opts->default_options.if_type;
563 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
567 * Retrieve the interface list so that we can search for the
568 * specified option amongst both the interface names and the
569 * friendly names and so that we find the friendly name even
570 * if an interface name was specified.
572 * If we can't get the list, just use the specified option as
573 * the interface name, so that the user can try specifying an
574 * interface explicitly for testing purposes.
576 if_list = capture_interface_list(&err, NULL, NULL);
577 if (if_list != NULL) {
578 /* try and do an exact match (case insensitive) */
583 for (if_entry = g_list_first(if_list); if_entry != NULL;
584 if_entry = g_list_next(if_entry))
586 if_info = (if_info_t *)if_entry->data;
587 /* exact name check */
588 if (g_ascii_strcasecmp(if_info->name, optarg_str_p) == 0) {
589 /* exact match on the interface name, use that for displaying etc */
590 interface_opts.name = g_strdup(if_info->name);
592 if (if_info->friendly_name != NULL) {
594 * If we have a friendly name, use that for the
595 * console display name, as it is the basis for
596 * the auto generated temp filename.
598 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
600 interface_opts.console_display_name = g_strdup(if_info->name);
602 interface_opts.if_type = if_info->type;
604 interface_opts.extcap = g_strdup(if_info->extcap);
610 /* exact friendly name check */
611 if (if_info->friendly_name != NULL &&
612 g_ascii_strcasecmp(if_info->friendly_name, optarg_str_p) == 0) {
613 /* exact match - use the friendly name for display */
614 interface_opts.name = g_strdup(if_info->name);
615 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
616 interface_opts.if_type = if_info->type;
618 interface_opts.extcap = g_strdup(if_info->extcap);
625 /* didn't find, attempt a case insensitive prefix match of the friendly name*/
627 size_t prefix_length;
629 prefix_length = strlen(optarg_str_p);
630 for (if_entry = g_list_first(if_list); if_entry != NULL;
631 if_entry = g_list_next(if_entry))
633 if_info = (if_info_t *)if_entry->data;
635 if (if_info->friendly_name != NULL &&
636 g_ascii_strncasecmp(if_info->friendly_name, optarg_str_p, prefix_length) == 0) {
637 /* prefix match - use the friendly name for display */
638 interface_opts.name = g_strdup(if_info->name);
639 interface_opts.console_display_name = g_strdup(if_info->friendly_name);
640 interface_opts.if_type = if_info->type;
642 interface_opts.extcap = g_strdup(if_info->extcap);
651 * We didn't find the interface in the list; just use
652 * the specified name, so that, for example, if an
653 * interface doesn't show up in the list for some
654 * reason, the user can try specifying it explicitly
655 * for testing purposes.
657 interface_opts.name = g_strdup(optarg_str_p);
658 interface_opts.console_display_name = g_strdup(optarg_str_p);
659 interface_opts.if_type = capture_opts->default_options.if_type;
661 interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
664 free_interface_list(if_list);
666 interface_opts.name = g_strdup(optarg_str_p);
667 interface_opts.console_display_name = g_strdup(optarg_str_p);
668 interface_opts.if_type = capture_opts->default_options.if_type;
670 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;
686 interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
687 interface_opts.extcap_args = NULL;
688 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
689 interface_opts.extcap_userdata = NULL;
691 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
692 interface_opts.buffer_size = capture_opts->default_options.buffer_size;
694 interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
695 #ifdef HAVE_PCAP_REMOTE
696 interface_opts.src_type = capture_opts->default_options.src_type;
697 interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
698 interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
699 interface_opts.auth_type = capture_opts->default_options.auth_type;
700 interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
701 interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
702 interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
703 interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
704 interface_opts.nocap_local = capture_opts->default_options.nocap_local;
706 #ifdef HAVE_PCAP_SETSAMPLING
707 interface_opts.sampling_method = capture_opts->default_options.sampling_method;
708 interface_opts.sampling_param = capture_opts->default_options.sampling_param;
711 g_array_append_val(capture_opts->ifaces, interface_opts);
718 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
723 case LONGOPT_NUM_CAP_COMMENT: /* capture comment */
724 if (capture_opts->capture_comment) {
725 cmdarg_err("--capture-comment can be set only once per file");
728 capture_opts->capture_comment = g_strdup(optarg_str_p);
730 case 'a': /* autostop criteria */
731 if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
732 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
736 #ifdef HAVE_PCAP_REMOTE
738 if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
739 cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
744 case 'b': /* Ringbuffer option */
745 capture_opts->multi_files_on = TRUE;
746 if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
747 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
751 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
752 case 'B': /* Buffer size */
753 if (capture_opts->ifaces->len > 0) {
754 interface_options interface_opts;
756 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
757 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
758 interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
759 g_array_append_val(capture_opts->ifaces, interface_opts);
761 capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
765 case 'c': /* Capture n packets */
766 capture_opts->has_autostop_packets = TRUE;
767 capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
769 case 'f': /* capture filter */
770 get_filter_arguments(capture_opts, optarg_str_p);
772 case 'g': /* enable group read access on the capture file(s) */
773 capture_opts->group_read_access = TRUE;
775 case 'H': /* Hide capture info dialog box */
776 capture_opts->show_info = FALSE;
778 case 'i': /* Use interface x */
779 status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
784 #ifdef HAVE_PCAP_CREATE
785 case 'I': /* Capture in monitor mode */
786 if (capture_opts->ifaces->len > 0) {
787 interface_options interface_opts;
789 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
790 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
791 interface_opts.monitor_mode = TRUE;
792 g_array_append_val(capture_opts->ifaces, interface_opts);
794 capture_opts->default_options.monitor_mode = TRUE;
798 case 'k': /* Start capture immediately */
799 *start_capture = TRUE;
801 /*case 'l':*/ /* Automatic scrolling in live capture mode */
802 #ifdef HAVE_PCAP_SETSAMPLING
804 if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
805 cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
810 case 'n': /* Use pcapng format */
811 capture_opts->use_pcapng = TRUE;
813 case 'p': /* Don't capture in promiscuous mode */
814 if (capture_opts->ifaces->len > 0) {
815 interface_options interface_opts;
817 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
818 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
819 interface_opts.promisc_mode = FALSE;
820 g_array_append_val(capture_opts->ifaces, interface_opts);
822 capture_opts->default_options.promisc_mode = FALSE;
825 case 'P': /* Use pcap format */
826 capture_opts->use_pcapng = FALSE;
828 #ifdef HAVE_PCAP_REMOTE
830 if (capture_opts->ifaces->len > 0) {
831 interface_options interface_opts;
833 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
834 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
835 interface_opts.nocap_rpcap = FALSE;
836 g_array_append_val(capture_opts->ifaces, interface_opts);
838 capture_opts->default_options.nocap_rpcap = FALSE;
842 case 's': /* Set the snapshot (capture) length */
843 snaplen = get_natural_int(optarg_str_p, "snapshot length");
845 * Make a snapshot length of 0 equivalent to the maximum packet
846 * length, mirroring what tcpdump does.
849 snaplen = WTAP_MAX_PACKET_SIZE;
850 if (capture_opts->ifaces->len > 0) {
851 interface_options interface_opts;
853 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
854 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
855 interface_opts.has_snaplen = TRUE;
856 interface_opts.snaplen = snaplen;
857 g_array_append_val(capture_opts->ifaces, interface_opts);
859 capture_opts->default_options.snaplen = snaplen;
860 capture_opts->default_options.has_snaplen = TRUE;
863 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
864 capture_opts->real_time_mode = TRUE;
866 #ifdef HAVE_PCAP_REMOTE
868 if (capture_opts->ifaces->len > 0) {
869 interface_options interface_opts;
871 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
872 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
873 interface_opts.datatx_udp = TRUE;
874 g_array_append_val(capture_opts->ifaces, interface_opts);
876 capture_opts->default_options.datatx_udp = TRUE;
880 case 'w': /* Write to capture file x */
881 capture_opts->saving_to_file = TRUE;
882 g_free(capture_opts->save_file);
883 capture_opts->save_file = g_strdup(optarg_str_p);
884 status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
886 case 'y': /* Set the pcap data link type */
887 if (capture_opts->ifaces->len > 0) {
888 interface_options interface_opts;
890 interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
891 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
892 interface_opts.linktype = linktype_name_to_val(optarg_str_p);
893 if (interface_opts.linktype == -1) {
894 cmdarg_err("The specified data link type \"%s\" isn't valid",
898 g_array_append_val(capture_opts->ifaces, interface_opts);
900 capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
901 if (capture_opts->default_options.linktype == -1) {
902 cmdarg_err("The specified data link type \"%s\" isn't valid",
909 /* the caller is responsible to send us only the right opt's */
910 g_assert_not_reached();
917 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
918 gboolean monitor_mode)
921 data_link_info_t *data_link_info;
923 if (caps->can_set_rfmon)
924 printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
925 name, monitor_mode ? "" : "not ");
927 printf("Data link types of interface %s (use option -y to set):\n", name);
928 for (lt_entry = caps->data_link_types; lt_entry != NULL;
929 lt_entry = g_list_next(lt_entry)) {
930 data_link_info = (data_link_info_t *)lt_entry->data;
931 printf(" %s", data_link_info->name);
932 if (data_link_info->description != NULL)
933 printf(" (%s)", data_link_info->description);
935 printf(" (not supported)");
940 /* Print an ASCII-formatted list of interfaces. */
942 capture_opts_print_interfaces(GList *if_list)
948 i = 1; /* Interface id number */
949 for (if_entry = g_list_first(if_list); if_entry != NULL;
950 if_entry = g_list_next(if_entry)) {
951 if_info = (if_info_t *)if_entry->data;
952 printf("%d. %s", i++, if_info->name);
954 /* Print the interface friendly name, if it exists;
955 if not fall back to vendor description, if it exists. */
956 if (if_info->friendly_name != NULL){
957 printf(" (%s)", if_info->friendly_name);
959 if (if_info->vendor_description != NULL)
960 printf(" (%s)", if_info->vendor_description);
968 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
971 interface_options interface_opts;
973 if (capture_opts->ifaces->len > 0) {
974 for (i = 0; i < capture_opts->ifaces->len; i++) {
975 interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
976 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
977 if (interface_opts.snaplen < 1)
978 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
979 else if (interface_opts.snaplen < snaplen_min)
980 interface_opts.snaplen = snaplen_min;
981 g_array_append_val(capture_opts->ifaces, interface_opts);
984 if (capture_opts->default_options.snaplen < 1)
985 capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
986 else if (capture_opts->default_options.snaplen < snaplen_min)
987 capture_opts->default_options.snaplen = snaplen_min;
993 capture_opts_trim_ring_num_files(capture_options *capture_opts)
995 /* Check the value range of the ring_num_files parameter */
996 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
997 cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
998 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
999 } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
1000 cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
1002 #if RINGBUFFER_MIN_NUM_FILES > 0
1003 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
1004 cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
1005 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
1010 * If no interface was specified explicitly, pick a default.
1013 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
1014 const char *capture_device)
1018 /* Did the user specify an interface to use? */
1019 if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
1020 /* yes they did, return immediately - nothing further to do here */
1024 /* No - is a default specified in the preferences file? */
1025 if (capture_device != NULL) {
1027 status = capture_opts_add_iface_opt(capture_opts, capture_device);
1030 /* No default in preferences file, just pick the first interface from the list of interfaces. */
1031 return capture_opts_add_iface_opt(capture_opts, "1");
1035 #define S_IFIFO _S_IFIFO
1038 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
1041 /* copied from filesystem.c */
1043 capture_opts_test_for_fifo(const char *path)
1047 if (ws_stat64(path, &statb) < 0)
1050 if (S_ISFIFO(statb.st_mode))
1057 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1063 if (save_file != NULL) {
1064 /* We're writing to a capture file. */
1065 if (strcmp(save_file, "-") == 0) {
1066 /* Writing to stdout. */
1067 /* XXX - should we check whether it's a pipe? It's arguably
1068 silly to do "-w - >output_file" rather than "-w output_file",
1069 but by not checking we might be violating the Principle Of
1070 Least Astonishment. */
1073 /* not writing to stdout, test for a FIFO (aka named pipe) */
1074 err = capture_opts_test_for_fifo(save_file);
1077 case ENOENT: /* it doesn't exist, so we'll be creating it,
1078 and it won't be a FIFO */
1079 case 0: /* found it, but it's not a FIFO */
1082 case ESPIPE: /* it is a FIFO */
1086 default: /* couldn't stat it */
1087 break; /* ignore: later attempt to open */
1088 /* will generate a nice msg */
1097 capture_opts_del_iface(capture_options *capture_opts, guint if_index)
1099 interface_options interface_opts;
1101 interface_opts = g_array_index(capture_opts->ifaces, interface_options, if_index);
1102 /* XXX - check if found? */
1104 g_free(interface_opts.name);
1105 g_free(interface_opts.descr);
1106 if (interface_opts.console_display_name != NULL)
1107 g_free(interface_opts.console_display_name);
1108 g_free(interface_opts.cfilter);
1110 g_free(interface_opts.extcap);
1111 g_free(interface_opts.extcap_fifo);
1112 if (interface_opts.extcap_args)
1113 g_hash_table_unref(interface_opts.extcap_args);
1114 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
1115 g_spawn_close_pid(interface_opts.extcap_pid);
1116 g_free(interface_opts.extcap_userdata);
1118 #ifdef HAVE_PCAP_REMOTE
1119 if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1120 g_free(interface_opts.remote_host);
1121 g_free(interface_opts.remote_port);
1122 g_free(interface_opts.auth_username);
1123 g_free(interface_opts.auth_password);
1126 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
1132 * Add all non-hidden selected interfaces in the "all interfaces" list
1133 * to the list of interfaces for the capture.
1136 collect_ifaces(capture_options *capture_opts)
1140 interface_options interface_opts;
1142 /* Empty out the existing list of interfaces. */
1143 for (i = capture_opts->ifaces->len; i != 0; i--)
1144 capture_opts_del_iface(capture_opts, i-1);
1146 /* Now fill the list up again. */
1147 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1148 device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1149 if (!device.hidden && device.selected) {
1150 interface_opts.name = g_strdup(device.name);
1151 interface_opts.descr = g_strdup(device.display_name);
1152 interface_opts.console_display_name = g_strdup(device.name);
1153 interface_opts.linktype = device.active_dlt;
1154 interface_opts.cfilter = g_strdup(device.cfilter);
1155 interface_opts.snaplen = device.snaplen;
1156 interface_opts.has_snaplen = device.has_snaplen;
1157 interface_opts.promisc_mode = device.pmode;
1158 interface_opts.if_type = device.if_info.type;
1160 interface_opts.extcap = g_strdup(device.if_info.extcap);
1161 interface_opts.extcap_fifo = NULL;
1162 interface_opts.extcap_userdata = NULL;
1163 interface_opts.extcap_args = device.external_cap_args_settings;
1164 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1165 if (interface_opts.extcap_args)
1166 g_hash_table_ref(interface_opts.extcap_args);
1167 interface_opts.extcap_userdata = NULL;
1169 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
1170 interface_opts.buffer_size = device.buffer;
1172 #ifdef HAVE_PCAP_CREATE
1173 interface_opts.monitor_mode = device.monitor_mode_enabled;
1175 #ifdef HAVE_PCAP_REMOTE
1176 interface_opts.src_type = CAPTURE_IFREMOTE;
1177 interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1178 interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1179 interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1180 interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1181 interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1182 interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1183 interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1184 interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1186 #ifdef HAVE_PCAP_SETSAMPLING
1187 interface_opts.sampling_method = device.remote_opts.sampling_method;
1188 interface_opts.sampling_param = device.remote_opts.sampling_param;
1190 g_array_append_val(capture_opts->ifaces, interface_opts);
1198 capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
1200 link_row* e = (link_row*)elem;
1207 capture_opts_free_interface_t_addrs(gpointer elem, gpointer unused _U_)
1213 capture_opts_free_interface_t(interface_t *device)
1215 if (device != NULL) {
1216 g_free(device->name);
1217 g_free(device->display_name);
1218 g_free(device->friendly_name);
1219 g_free(device->addresses);
1220 g_free(device->cfilter);
1221 g_list_foreach(device->links,
1222 capture_opts_free_interface_t_links, NULL);
1223 g_list_free(device->links);
1224 #ifdef HAVE_PCAP_REMOTE
1225 g_free(device->remote_opts.remote_host_opts.remote_host);
1226 g_free(device->remote_opts.remote_host_opts.remote_port);
1227 g_free(device->remote_opts.remote_host_opts.auth_username);
1228 g_free(device->remote_opts.remote_host_opts.auth_password);
1230 g_free(device->if_info.name);
1231 g_free(device->if_info.friendly_name);
1232 g_free(device->if_info.vendor_description);
1233 g_slist_foreach(device->if_info.addrs,
1234 capture_opts_free_interface_t_addrs, NULL);
1235 g_slist_free(device->if_info.addrs);
1237 g_free(device->if_info.extcap);
1242 #endif /* HAVE_LIBPCAP */
1245 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1250 * indent-tabs-mode: nil
1253 * vi: set shiftwidth=4 tabstop=8 expandtab:
1254 * :indentSize=4:tabSize=8:noTabs=true: