2 * Routines for capture options setting
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include <epan/packet.h>
41 #include "capture_opts.h"
42 #include "ringbuffer.h"
43 #include "clopts_common.h"
44 #include "cmdarg_err.h"
46 #include "capture-pcap-util.h"
47 #include "capture_ui_utils.h"
52 capture_opts_init(capture_options *capture_opts, void *cfile)
54 capture_opts->cf = cfile;
55 capture_opts->cfilter = g_strdup(""); /* No capture filter string specified */
56 capture_opts->iface = NULL; /* Default is "pick the first interface" */
58 capture_opts->buffer_size = 1; /* 1 MB */
60 capture_opts->has_snaplen = FALSE;
61 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
62 infinite, in effect */
63 capture_opts->promisc_mode = TRUE; /* promiscuous mode is the default */
64 capture_opts->linktype = -1; /* the default linktype */
65 capture_opts->save_file = NULL;
66 capture_opts->real_time_mode = TRUE;
67 capture_opts->show_info = TRUE;
68 capture_opts->quit_after_cap = FALSE;
69 capture_opts->restart = FALSE;
71 capture_opts->multi_files_on = FALSE;
72 capture_opts->has_file_duration = FALSE;
73 capture_opts->file_duration = 60; /* 1 min */
74 capture_opts->has_ring_num_files = FALSE;
75 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
77 capture_opts->has_autostop_files = FALSE;
78 capture_opts->autostop_files = 1;
79 capture_opts->has_autostop_packets = FALSE;
80 capture_opts->autostop_packets = 0;
81 capture_opts->has_autostop_filesize = FALSE;
82 capture_opts->autostop_filesize = 1024; /* 1 MB */
83 capture_opts->has_autostop_duration = FALSE;
84 capture_opts->autostop_duration = 60; /* 1 min */
87 capture_opts->fork_child = -1; /* invalid process handle */
89 capture_opts->signal_pipe_fd = -1;
91 capture_opts->state = CAPTURE_STOPPED;
95 /* log content of capture_opts */
97 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
98 g_log(log_domain, log_level, "CAPTURE OPTIONS :");
99 g_log(log_domain, log_level, "CFile : 0x%p", capture_opts->cf);
100 g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
101 g_log(log_domain, log_level, "Interface : %s", capture_opts->iface);
103 g_log(log_domain, log_level, "BufferSize : %u (MB)", capture_opts->buffer_size);
105 g_log(log_domain, log_level, "SnapLen (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
106 g_log(log_domain, log_level, "Promisc : %u", capture_opts->promisc_mode);
107 g_log(log_domain, log_level, "LinkType : %d", capture_opts->linktype);
108 g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
109 g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode);
110 g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info);
111 g_log(log_domain, log_level, "QuitAfterCap : %u", capture_opts->quit_after_cap);
113 g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on);
114 g_log(log_domain, log_level, "FileDuration (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
115 g_log(log_domain, log_level, "RingNumFiles (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
117 g_log(log_domain, log_level, "AutostopFiles (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
118 g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
119 g_log(log_domain, log_level, "AutostopFilesize(%u): %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
120 g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
122 g_log(log_domain, log_level, "ForkChild : %d", capture_opts->fork_child);
124 g_log(log_domain, log_level, "SignalPipeFd : %d", capture_opts->signal_pipe_fd);
129 * Given a string of the form "<autostop criterion>:<value>", as might appear
130 * as an argument to a "-a" option, parse it and set the criterion in
131 * question. Return an indication of whether it succeeded or failed
135 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
139 colonp = strchr(autostoparg, ':');
147 * Skip over any white space (there probably won't be any, but
148 * as we allow it in the preferences file, we might as well
151 while (isspace((guchar)*p))
155 * Put the colon back, so if our caller uses, in an
156 * error message, the string they passed us, the message
162 if (strcmp(autostoparg,"duration") == 0) {
163 capture_opts->has_autostop_duration = TRUE;
164 capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
165 } else if (strcmp(autostoparg,"filesize") == 0) {
166 capture_opts->has_autostop_filesize = TRUE;
167 capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
168 } else if (strcmp(autostoparg,"files") == 0) {
169 capture_opts->multi_files_on = TRUE;
170 capture_opts->has_autostop_files = TRUE;
171 capture_opts->autostop_files = get_positive_int(p,"autostop files");
175 *colonp = ':'; /* put the colon back */
180 * Given a string of the form "<ring buffer file>:<duration>", as might appear
181 * as an argument to a "-b" option, parse it and set the arguments in
182 * question. Return an indication of whether it succeeded or failed
186 get_ring_arguments(capture_options *capture_opts, const char *arg)
188 gchar *p = NULL, *colonp;
190 colonp = strchr(arg, ':');
198 * Skip over any white space (there probably won't be any, but
199 * as we allow it in the preferences file, we might as well
202 while (isspace((guchar)*p))
206 * Put the colon back, so if our caller uses, in an
207 * error message, the string they passed us, the message
214 if (strcmp(arg,"files") == 0) {
215 capture_opts->has_ring_num_files = TRUE;
216 capture_opts->ring_num_files = get_natural_int(p, "number of ring buffer files");
217 } else if (strcmp(arg,"filesize") == 0) {
218 capture_opts->has_autostop_filesize = TRUE;
219 capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
220 } else if (strcmp(arg,"duration") == 0) {
221 capture_opts->has_file_duration = TRUE;
222 capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
225 *colonp = ':'; /* put the colon back */
232 * Given a string of the form "<pipe name>:<file descriptor>", as might appear
233 * as an argument to a "-Z" option, parse it and set the arguments in
234 * question. Return an indication of whether it succeeded or failed
238 get_pipe_arguments(capture_options *capture_opts, const char *arg)
240 gchar *p = NULL, *colonp;
244 colonp = strchr(arg, ':');
252 * Skip over any white space (there probably won't be any, but
253 * as we allow it in the preferences file, we might as well
256 while (isspace((guchar)*p))
260 * Put the colon back, so if our caller uses, in an
261 * error message, the string they passed us, the message
268 if (strcmp(arg,"sync") == 0) {
269 /* associate stdout with sync pipe */
270 pipe_fd = get_natural_int(p, "sync pipe file descriptor");
271 if (dup2(pipe_fd, 1) < 0) {
272 cmdarg_err("Unable to dup sync pipe handle");
275 } else if (strcmp(arg,"signal") == 0) {
276 /* associate stdin with signal pipe */
277 pipe_fd = get_natural_int(p, "signal pipe file descriptor");
278 if (dup2(pipe_fd, 0) < 0) {
279 cmdarg_err("Unable to dup signal pipe handle");
282 capture_opts->signal_pipe_fd = pipe_fd;
285 *colonp = ':'; /* put the colon back */
292 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg)
299 gchar err_str[PCAP_ERRBUF_SIZE];
300 gchar *cant_get_if_list_errstr;
304 * If the argument is a number, treat it as an index into the list
305 * of adapters, as printed by "tethereal -D".
307 * This should be OK on UNIX systems, as interfaces shouldn't have
308 * names that begin with digits. It can be useful on Windows, where
309 * more than one interface can have the same name.
311 adapter_index = strtol(optarg, &p, 10);
312 if (p != NULL && *p == '\0') {
313 if (adapter_index < 0) {
314 cmdarg_err("The specified adapter index is a negative number");
317 if (adapter_index > INT_MAX) {
318 cmdarg_err("The specified adapter index is too large (greater than %d)",
322 if (adapter_index == 0) {
323 cmdarg_err("there is no interface with that adapter index");
326 if_list = get_interface_list(&err, err_str);
327 if (if_list == NULL) {
330 case CANT_GET_INTERFACE_LIST:
331 cant_get_if_list_errstr =
332 cant_get_if_list_error_message(err_str);
333 cmdarg_err("%s", cant_get_if_list_errstr);
334 g_free(cant_get_if_list_errstr);
337 case NO_INTERFACES_FOUND:
338 cmdarg_err("There are no interfaces on which a capture can be done");
343 if_info = g_list_nth_data(if_list, adapter_index - 1);
344 if (if_info == NULL) {
345 cmdarg_err("there is no interface with that adapter index");
348 capture_opts->iface = g_strdup(if_info->name);
349 free_interface_list(if_list);
351 capture_opts->iface = g_strdup(optarg);
356 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture)
359 case 'a': /* autostop criteria */
360 if (set_autostop_criterion(capture_opts, optarg) == FALSE) {
361 cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg);
365 case 'b': /* Ringbuffer option */
366 capture_opts->multi_files_on = TRUE;
367 if (get_ring_arguments(capture_opts, optarg) == FALSE) {
368 cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg);
373 case 'B': /* Buffer size */
374 capture_opts->buffer_size = get_positive_int(optarg, "buffer size");
377 case 'c': /* Capture xxx packets */
378 capture_opts->has_autostop_packets = TRUE;
379 capture_opts->autostop_packets = get_positive_int(optarg, "packet count");
381 case 'f': /* capture filter */
382 if (capture_opts->cfilter)
383 g_free(capture_opts->cfilter);
384 capture_opts->cfilter = g_strdup(optarg);
386 case 'H': /* Hide capture info dialog box */
387 capture_opts->show_info = FALSE;
389 case 'i': /* Use interface xxx */
390 capture_opts_add_iface_opt(capture_opts, optarg);
392 case 'k': /* Start capture immediately */
393 *start_capture = TRUE;
395 /*case 'l':*/ /* Automatic scrolling in live capture mode */
396 case 'p': /* Don't capture in promiscuous mode */
397 capture_opts->promisc_mode = FALSE;
399 case 'Q': /* Quit after capture (just capture to file) */
400 capture_opts->quit_after_cap = TRUE;
401 *start_capture = TRUE; /*** -Q implies -k !! ***/
403 case 's': /* Set the snapshot (capture) length */
404 capture_opts->has_snaplen = TRUE;
405 capture_opts->snaplen = get_positive_int(optarg, "snapshot length");
407 case 'S': /* "Real-Time" mode: used for following file ala tail -f */
408 capture_opts->real_time_mode = TRUE;
410 case 'w': /* Write to capture file xxx */
411 capture_opts->save_file = g_strdup(optarg);
413 case 'y': /* Set the pcap data link type */
414 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
415 capture_opts->linktype = pcap_datalink_name_to_val(optarg);
416 if (capture_opts->linktype == -1) {
417 cmdarg_err("The specified data link type \"%s\" isn't valid",
421 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
422 /* XXX - just treat it as a number */
423 capture_opts->linktype = get_natural_int(optarg, "data link type");
424 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
427 /* Hidden option supporting Sync mode */
428 case 'Z': /* Write to pipe FD XXX */
429 if (get_pipe_arguments(capture_opts, optarg) == FALSE) {
430 cmdarg_err("Invalid or unknown -Z flag \"%s\"", optarg);
436 /* the caller is responsible to send us only the right opt's */
437 g_assert_not_reached();
442 void capture_opts_list_link_layer_types(capture_options *capture_opts)
444 gchar err_str[PCAP_ERRBUF_SIZE];
445 GList *lt_list, *lt_entry;
446 data_link_info_t *data_link_info;
448 /* Get the list of link-layer types for the capture device. */
449 lt_list = get_pcap_linktype_list(capture_opts->iface, err_str);
450 if (lt_list == NULL) {
451 if (err_str[0] != '\0') {
452 cmdarg_err("The list of data link types for the capture device could not be obtained (%s)."
453 "Please check to make sure you have sufficient permissions, and that\n"
454 "you have the proper interface or pipe specified.\n", err_str);
456 cmdarg_err("The capture device has no data link types.");
459 cmdarg_err_cont("Data link types (use option -y to set):");
460 for (lt_entry = lt_list; lt_entry != NULL;
461 lt_entry = g_list_next(lt_entry)) {
462 data_link_info = lt_entry->data;
463 cmdarg_err_cont(" %s", data_link_info->name);
464 if (data_link_info->description != NULL)
465 cmdarg_err_cont(" (%s)", data_link_info->description);
467 cmdarg_err_cont(" (not supported)");
470 free_pcap_linktype_list(lt_list);
474 void capture_opts_list_interfaces()
480 gchar err_str[PCAP_ERRBUF_SIZE];
481 gchar *cant_get_if_list_errstr;
485 if_list = get_interface_list(&err, err_str);
486 if (if_list == NULL) {
488 case CANT_GET_INTERFACE_LIST:
489 cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
490 cmdarg_err("%s", cant_get_if_list_errstr);
491 g_free(cant_get_if_list_errstr);
494 case NO_INTERFACES_FOUND:
495 cmdarg_err("There are no interfaces on which a capture can be done");
501 i = 1; /* Interface id number */
502 for (if_entry = g_list_first(if_list); if_entry != NULL;
503 if_entry = g_list_next(if_entry)) {
504 if_info = if_entry->data;
505 printf("%d. %s", i++, if_info->name);
506 if (if_info->description != NULL)
507 printf(" (%s)", if_info->description);
510 free_interface_list(if_list);
514 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
516 if (capture_opts->snaplen < 1)
517 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
518 else if (capture_opts->snaplen < snaplen_min)
519 capture_opts->snaplen = snaplen_min;
523 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
525 /* Check the value range of the ring_num_files parameter */
526 if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES)
527 capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
528 #if RINGBUFFER_MIN_NUM_FILES > 0
529 else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
530 capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
535 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
540 gchar err_str[PCAP_ERRBUF_SIZE];
541 gchar *cant_get_if_list_errstr;
544 /* Did the user specify an interface to use? */
545 if (capture_opts->iface == NULL) {
546 /* No - is a default specified in the preferences file? */
547 if (capture_device != NULL) {
549 capture_opts->iface = g_strdup(capture_device);
551 /* No - pick the first one from the list of interfaces. */
552 if_list = get_interface_list(&err, err_str);
553 if (if_list == NULL) {
556 case CANT_GET_INTERFACE_LIST:
557 cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
558 cmdarg_err("%s", cant_get_if_list_errstr);
559 g_free(cant_get_if_list_errstr);
562 case NO_INTERFACES_FOUND:
563 cmdarg_err("There are no interfaces on which a capture can be done");
568 if_info = if_list->data; /* first interface */
569 capture_opts->iface = g_strdup(if_info->name);
570 free_interface_list(if_list);
578 #endif /* HAVE_LIBPCAP */