Support multiple Scheduling Requests in one PDU.
[obnox/wireshark/wip.git] / capture_opts.c
1 /* capture_opts.c
2  * Routines for capture options setting
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <string.h>
32 #include <ctype.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <glib.h>
39
40 #include <epan/packet.h>
41
42 #include "capture_opts.h"
43 #include "ringbuffer.h"
44 #include "clopts_common.h"
45 #include "console_io.h"
46 #include "cmdarg_err.h"
47
48 #include "capture_ifinfo.h"
49 #include "capture-pcap-util.h"
50 #include <wsutil/file_util.h>
51
52 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
53
54
55 void
56 capture_opts_init(capture_options *capture_opts, void *cf)
57 {
58   capture_opts->cf                      = cf;
59   capture_opts->cfilter                 = g_strdup("");     /* No capture filter string specified */
60   capture_opts->iface                   = NULL;             /* Default is "pick the first interface" */
61   capture_opts->iface_descr             = NULL;
62 #ifdef HAVE_PCAP_REMOTE
63   capture_opts->src_type                = CAPTURE_IFLOCAL;
64   capture_opts->remote_host             = NULL;
65   capture_opts->remote_port             = NULL;
66   capture_opts->auth_type               = CAPTURE_AUTH_NULL;
67   capture_opts->auth_username           = NULL;
68   capture_opts->auth_password           = NULL;
69   capture_opts->datatx_udp              = FALSE;
70   capture_opts->nocap_rpcap             = TRUE;
71   capture_opts->nocap_local             = FALSE;
72 #endif
73 #ifdef HAVE_PCAP_SETSAMPLING
74   capture_opts->sampling_method         = CAPTURE_SAMP_NONE;
75   capture_opts->sampling_param          = 0;
76 #endif
77 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
78   capture_opts->buffer_size             = 1;                /* 1 MB */
79 #endif
80   capture_opts->has_snaplen             = FALSE;
81   capture_opts->snaplen                 = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
82                                                                    infinite, in effect */
83   capture_opts->promisc_mode            = TRUE;             /* promiscuous mode is the default */
84   capture_opts->monitor_mode            = FALSE;
85   capture_opts->linktype                = -1;               /* the default linktype */
86   capture_opts->saving_to_file          = FALSE;
87   capture_opts->save_file               = NULL;
88   capture_opts->group_read_access       = FALSE;
89   capture_opts->use_pcapng              = FALSE;            /* the default is pcap */
90   capture_opts->real_time_mode          = TRUE;
91   capture_opts->show_info               = TRUE;
92   capture_opts->quit_after_cap          = FALSE;
93   capture_opts->restart                 = FALSE;
94
95   capture_opts->multi_files_on          = FALSE;
96   capture_opts->has_file_duration       = FALSE;
97   capture_opts->file_duration           = 60;               /* 1 min */
98   capture_opts->has_ring_num_files      = FALSE;
99   capture_opts->ring_num_files          = RINGBUFFER_MIN_NUM_FILES;
100
101   capture_opts->has_autostop_files      = FALSE;
102   capture_opts->autostop_files          = 1;
103   capture_opts->has_autostop_packets    = FALSE;
104   capture_opts->autostop_packets        = 0;
105   capture_opts->has_autostop_filesize   = FALSE;
106   capture_opts->autostop_filesize       = 1024;             /* 1 MB */
107   capture_opts->has_autostop_duration   = FALSE;
108   capture_opts->autostop_duration       = 60;               /* 1 min */
109
110
111   capture_opts->fork_child              = -1;               /* invalid process handle */
112 #ifdef _WIN32
113   capture_opts->signal_pipe_write_fd    = -1;
114 #endif
115   capture_opts->state                   = CAPTURE_STOPPED;
116   capture_opts->output_to_pipe          = FALSE;
117 #ifndef _WIN32
118   capture_opts->owner                   = getuid();
119   capture_opts->group                   = getgid();
120 #endif
121 }
122
123
124 /* log content of capture_opts */
125 void
126 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
127     g_log(log_domain, log_level, "CAPTURE OPTIONS    :");
128     g_log(log_domain, log_level, "CFile              : 0x%p", capture_opts->cf);
129     g_log(log_domain, log_level, "Filter             : %s", capture_opts->cfilter);
130     g_log(log_domain, log_level, "Interface          : %s", capture_opts->iface);
131     /* iface_descr may not been filled in and some C Libraries hate a null ptr for %s */
132     g_log(log_domain, log_level, "Interface Descr    : %s",
133         capture_opts->iface_descr ? capture_opts->iface_descr : "<null>");
134 #ifdef HAVE_PCAP_REMOTE
135     g_log(log_domain, log_level, "Capture source     : %s",
136         capture_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" :
137         capture_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" :
138         "Unknown");
139     if (capture_opts->src_type == CAPTURE_IFREMOTE) {
140         g_log(log_domain, log_level, "Remote host        : %s", capture_opts->remote_host);
141         g_log(log_domain, log_level, "Remote port        : %s", capture_opts->remote_port);
142     }
143     g_log(log_domain, log_level, "Authentication     : %s",
144         capture_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" :
145         capture_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
146         "Unknown");
147     if (capture_opts->auth_type == CAPTURE_AUTH_PWD) {
148         g_log(log_domain, log_level, "Auth username      : %s", capture_opts->auth_password);
149         g_log(log_domain, log_level, "Auth password      : <hidden>");
150     }
151     g_log(log_domain, log_level, "UDP data transfer  : %u", capture_opts->datatx_udp);
152     g_log(log_domain, log_level, "No capture RPCAP   : %u", capture_opts->nocap_rpcap);
153     g_log(log_domain, log_level, "No capture local   : %u", capture_opts->nocap_local);
154 #endif
155 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
156     g_log(log_domain, log_level, "BufferSize         : %u (MB)", capture_opts->buffer_size);
157 #endif
158     g_log(log_domain, log_level, "SnapLen         (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
159     g_log(log_domain, log_level, "Promisc            : %u", capture_opts->promisc_mode);
160     g_log(log_domain, log_level, "LinkType           : %d", capture_opts->linktype);
161     g_log(log_domain, log_level, "SavingToFile       : %u", capture_opts->saving_to_file);
162     g_log(log_domain, log_level, "SaveFile           : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
163     g_log(log_domain, log_level, "GroupReadAccess    : %u", capture_opts->group_read_access);
164     g_log(log_domain, log_level, "Fileformat         : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
165     g_log(log_domain, log_level, "RealTimeMode       : %u", capture_opts->real_time_mode);
166     g_log(log_domain, log_level, "ShowInfo           : %u", capture_opts->show_info);
167     g_log(log_domain, log_level, "QuitAfterCap       : %u", capture_opts->quit_after_cap);
168
169     g_log(log_domain, log_level, "MultiFilesOn       : %u", capture_opts->multi_files_on);
170     g_log(log_domain, log_level, "FileDuration    (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
171     g_log(log_domain, log_level, "RingNumFiles    (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
172
173     g_log(log_domain, log_level, "AutostopFiles   (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
174     g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
175     g_log(log_domain, log_level, "AutostopFilesize(%u): %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
176     g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
177
178     g_log(log_domain, log_level, "ForkChild          : %d", capture_opts->fork_child);
179 #ifdef _WIN32
180     g_log(log_domain, log_level, "SignalPipeWrite    : %d", capture_opts->signal_pipe_write_fd);
181 #endif
182 }
183
184 /*
185  * Given a string of the form "<autostop criterion>:<value>", as might appear
186  * as an argument to a "-a" option, parse it and set the criterion in
187  * question.  Return an indication of whether it succeeded or failed
188  * in some fashion.
189  */
190 static gboolean
191 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
192 {
193   gchar *p, *colonp;
194
195   colonp = strchr(autostoparg, ':');
196   if (colonp == NULL)
197     return FALSE;
198
199   p = colonp;
200   *p++ = '\0';
201
202   /*
203    * Skip over any white space (there probably won't be any, but
204    * as we allow it in the preferences file, we might as well
205    * allow it here).
206    */
207   while (isspace((guchar)*p))
208     p++;
209   if (*p == '\0') {
210     /*
211      * Put the colon back, so if our caller uses, in an
212      * error message, the string they passed us, the message
213      * looks correct.
214      */
215     *colonp = ':';
216     return FALSE;
217   }
218   if (strcmp(autostoparg,"duration") == 0) {
219     capture_opts->has_autostop_duration = TRUE;
220     capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
221   } else if (strcmp(autostoparg,"filesize") == 0) {
222     capture_opts->has_autostop_filesize = TRUE;
223     capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
224   } else if (strcmp(autostoparg,"files") == 0) {
225     capture_opts->multi_files_on = TRUE;
226     capture_opts->has_autostop_files = TRUE;
227     capture_opts->autostop_files = get_positive_int(p,"autostop files");
228   } else {
229     return FALSE;
230   }
231   *colonp = ':'; /* put the colon back */
232   return TRUE;
233 }
234
235 /*
236  * Given a string of the form "<ring buffer file>:<duration>", as might appear
237  * as an argument to a "-b" option, parse it and set the arguments in
238  * question.  Return an indication of whether it succeeded or failed
239  * in some fashion.
240  */
241 static gboolean
242 get_ring_arguments(capture_options *capture_opts, const char *arg)
243 {
244   gchar *p = NULL, *colonp;
245
246   colonp = strchr(arg, ':');
247   if (colonp == NULL)
248     return FALSE;
249
250   p = colonp;
251   *p++ = '\0';
252
253   /*
254    * Skip over any white space (there probably won't be any, but
255    * as we allow it in the preferences file, we might as well
256    * allow it here).
257    */
258   while (isspace((guchar)*p))
259     p++;
260   if (*p == '\0') {
261     /*
262      * Put the colon back, so if our caller uses, in an
263      * error message, the string they passed us, the message
264      * looks correct.
265      */
266     *colonp = ':';
267     return FALSE;
268   }
269
270   if (strcmp(arg,"files") == 0) {
271     capture_opts->has_ring_num_files = TRUE;
272     capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
273   } else if (strcmp(arg,"filesize") == 0) {
274     capture_opts->has_autostop_filesize = TRUE;
275     capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
276   } else if (strcmp(arg,"duration") == 0) {
277     capture_opts->has_file_duration = TRUE;
278     capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
279   }
280
281   *colonp = ':';    /* put the colon back */
282   return TRUE;
283 }
284
285 #ifdef HAVE_PCAP_SETSAMPLING
286 /*
287  * Given a string of the form "<sampling type>:<value>", as might appear
288  * as an argument to a "-m" option, parse it and set the arguments in
289  * question.  Return an indication of whether it succeeded or failed
290  * in some fashion.
291  */
292 static gboolean
293 get_sampling_arguments(capture_options *capture_opts, const char *arg)
294 {
295     gchar *p = NULL, *colonp;
296
297     colonp = strchr(arg, ':');
298     if (colonp == NULL)
299         return FALSE;
300
301     p = colonp;
302     *p++ = '\0';
303
304     while (isspace((guchar)*p))
305         p++;
306     if (*p == '\0') {
307         *colonp = ':';
308         return FALSE;
309     }
310
311     if (strcmp(arg, "count") == 0) {
312         capture_opts->sampling_method = CAPTURE_SAMP_BY_COUNT;
313         capture_opts->sampling_param = get_positive_int(p, "sampling count");
314     } else if (strcmp(arg, "timer") == 0) {
315         capture_opts->sampling_method = CAPTURE_SAMP_BY_TIMER;
316         capture_opts->sampling_param = get_positive_int(p, "sampling timer");
317     }
318     *colonp = ':';
319     return TRUE;
320 }
321 #endif
322
323 #ifdef HAVE_PCAP_REMOTE
324 /*
325  * Given a string of the form "<username>:<password>", as might appear
326  * as an argument to a "-A" option, parse it and set the arguments in
327  * question.  Return an indication of whether it succeeded or failed
328  * in some fashion.
329  */
330 static gboolean
331 get_auth_arguments(capture_options *capture_opts, const char *arg)
332 {
333     gchar *p = NULL, *colonp;
334
335     colonp = strchr(arg, ':');
336     if (colonp == NULL)
337         return FALSE;
338
339     p = colonp;
340     *p++ = '\0';
341
342     while (isspace((guchar)*p))
343         p++;
344
345     capture_opts->auth_type = CAPTURE_AUTH_PWD;
346     capture_opts->auth_username = g_strdup(arg);
347     capture_opts->auth_password = g_strdup(p);
348     *colonp = ':';
349     return TRUE;
350 }
351 #endif
352
353 static int
354 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
355 {
356     long        adapter_index;
357     char        *p;
358     GList       *if_list;
359     if_info_t   *if_info;
360     int         err;
361     gchar       *err_str;
362
363
364     /*
365      * If the argument is a number, treat it as an index into the list
366      * of adapters, as printed by "tshark -D".
367      *
368      * This should be OK on UNIX systems, as interfaces shouldn't have
369      * names that begin with digits.  It can be useful on Windows, where
370      * more than one interface can have the same name.
371      */
372     adapter_index = strtol(optarg_str_p, &p, 10);
373     if (p != NULL && *p == '\0') {
374       if (adapter_index < 0) {
375         cmdarg_err("The specified adapter index is a negative number");
376         return 1;
377       }
378       if (adapter_index > INT_MAX) {
379         cmdarg_err("The specified adapter index is too large (greater than %d)",
380             INT_MAX);
381         return 1;
382       }
383       if (adapter_index == 0) {
384         cmdarg_err("There is no interface with that adapter index");
385         return 1;
386       }
387       if_list = capture_interface_list(&err, &err_str);
388       if (if_list == NULL) {
389         switch (err) {
390
391         case CANT_GET_INTERFACE_LIST:
392             cmdarg_err("%s", err_str);
393             g_free(err_str);
394             break;
395
396         case NO_INTERFACES_FOUND:
397             cmdarg_err("There are no interfaces on which a capture can be done");
398             break;
399         }
400         return 2;
401       }
402       if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
403       if (if_info == NULL) {
404         cmdarg_err("There is no interface with that adapter index");
405         return 1;
406       }
407       capture_opts->iface = g_strdup(if_info->name);
408       /*  We don't set iface_descr here because doing so requires
409        *  capture_ui_utils.c which requires epan/prefs.c which is
410        *  probably a bit too much dependency for here...
411        */
412       free_interface_list(if_list);
413     } else {
414       capture_opts->iface = g_strdup(optarg_str_p);
415     }
416
417     return 0;
418 }
419
420 int
421 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
422 {
423     int status;
424
425     switch(opt) {
426     case 'a':        /* autostop criteria */
427         if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
428           cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
429           return 1;
430         }
431         break;
432 #ifdef HAVE_PCAP_REMOTE
433     case 'A':
434         if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
435             cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
436             return 1;
437         }
438         break;
439 #endif
440     case 'b':        /* Ringbuffer option */
441         capture_opts->multi_files_on = TRUE;
442         if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
443           cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
444           return 1;
445         }
446         break;
447 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
448     case 'B':        /* Buffer size */
449         capture_opts->buffer_size = get_positive_int(optarg_str_p, "buffer size");
450         break;
451 #endif
452     case 'c':        /* Capture n packets */
453         capture_opts->has_autostop_packets = TRUE;
454         capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
455         break;
456     case 'f':        /* capture filter */
457         if (capture_opts->has_cfilter) {
458             cmdarg_err("More than one -f argument specified");
459             return 1;
460         }
461         capture_opts->has_cfilter = TRUE;
462         g_free(capture_opts->cfilter);
463         capture_opts->cfilter = g_strdup(optarg_str_p);
464         break;
465     case 'H':        /* Hide capture info dialog box */
466         capture_opts->show_info = FALSE;
467         break;
468     case 'i':        /* Use interface x */
469         status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
470         if (status != 0) {
471             return status;
472         }
473         break;
474 #ifdef HAVE_PCAP_CREATE
475     case 'I':        /* Capture in monitor mode */
476         capture_opts->monitor_mode = TRUE;
477         break;
478 #endif
479     case 'k':        /* Start capture immediately */
480         *start_capture = TRUE;
481         break;
482     /*case 'l':*/    /* Automatic scrolling in live capture mode */
483 #ifdef HAVE_PCAP_SETSAMPLING
484     case 'm':
485         if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
486             cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
487             return 1;
488         }
489         break;
490 #endif
491     case 'n':        /* Use pcapng format */
492         capture_opts->use_pcapng = TRUE;
493         break;
494     case 'p':        /* Don't capture in promiscuous mode */
495         capture_opts->promisc_mode = FALSE;
496         break;
497     case 'Q':        /* Quit after capture (just capture to file) */
498         capture_opts->quit_after_cap  = TRUE;
499         *start_capture   = TRUE;  /*** -Q implies -k !! ***/
500         break;
501 #ifdef HAVE_PCAP_REMOTE
502     case 'r':
503         capture_opts->nocap_rpcap = FALSE;
504         break;
505 #endif
506     case 's':        /* Set the snapshot (capture) length */
507         capture_opts->has_snaplen = TRUE;
508         capture_opts->snaplen = get_natural_int(optarg_str_p, "snapshot length");
509         /*
510          * Make a snapshot length of 0 equivalent to the maximum packet
511          * length, mirroring what tcpdump does.
512          */
513         if (capture_opts->snaplen == 0)
514           capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
515         break;
516     case 'S':        /* "Real-Time" mode: used for following file ala tail -f */
517         capture_opts->real_time_mode = TRUE;
518         break;
519 #ifdef HAVE_PCAP_REMOTE
520     case 'u':
521         capture_opts->datatx_udp = TRUE;
522         break;
523 #endif
524     case 'w':        /* Write to capture file x */
525         capture_opts->saving_to_file = TRUE;
526         g_free(capture_opts->save_file);
527 #if defined _WIN32 && GLIB_CHECK_VERSION(2,6,0)
528         /* since GLib 2.6, we need to convert filenames to utf8 for Win32 */
529         capture_opts->save_file = g_locale_to_utf8(optarg_str_p, -1, NULL, NULL, NULL);
530 #else
531         capture_opts->save_file = g_strdup(optarg_str_p);
532 #endif
533         status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
534         return status;
535     case 'g':        /* enable group read access on the capture file(s) */
536         capture_opts->group_read_access = TRUE;
537         break;
538     case 'y':        /* Set the pcap data link type */
539         capture_opts->linktype = linktype_name_to_val(optarg_str_p);
540         if (capture_opts->linktype == -1) {
541           cmdarg_err("The specified data link type \"%s\" isn't valid",
542               optarg_str_p);
543           return 1;
544         }
545         break;
546     default:
547         /* the caller is responsible to send us only the right opt's */
548         g_assert_not_reached();
549     }
550
551     return 0;
552 }
553
554 void
555 capture_opts_print_if_capabilities(if_capabilities_t *caps,
556                                    gboolean monitor_mode)
557 {
558     GList *lt_entry;
559     data_link_info_t *data_link_info;
560
561     if (caps->can_set_rfmon)
562         fprintf_stderr("Data link types when %sin monitor mode (use option -y to set):\n",
563                        monitor_mode ? "" : "not ");
564     else
565         fprintf_stderr("Data link types (use option -y to set):\n");
566     for (lt_entry = caps->data_link_types; lt_entry != NULL;
567          lt_entry = g_list_next(lt_entry)) {
568         data_link_info = (data_link_info_t *)lt_entry->data;
569         fprintf_stderr("  %s", data_link_info->name);
570         if (data_link_info->description != NULL)
571             fprintf_stderr(" (%s)", data_link_info->description);
572         else
573             fprintf_stderr(" (not supported)");
574         fprintf_stderr("\n");
575     }
576 }
577
578 /* Print an ASCII-formatted list of interfaces. */
579 void
580 capture_opts_print_interfaces(GList *if_list)
581 {
582     int         i;
583     GList       *if_entry;
584     if_info_t   *if_info;
585
586     i = 1;  /* Interface id number */
587     for (if_entry = g_list_first(if_list); if_entry != NULL;
588          if_entry = g_list_next(if_entry)) {
589         if_info = (if_info_t *)if_entry->data;
590         fprintf_stderr("%d. %s", i++, if_info->name);
591
592         /* Print the description if it exists */
593         if (if_info->description != NULL)
594             fprintf_stderr(" (%s)", if_info->description);
595         fprintf_stderr("\n");
596     }
597 }
598
599
600 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
601 {
602   if (capture_opts->snaplen < 1)
603     capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
604   else if (capture_opts->snaplen < snaplen_min)
605     capture_opts->snaplen = snaplen_min;
606 }
607
608
609 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
610 {
611   /* Check the value range of the ring_num_files parameter */
612   if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
613     cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
614     capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
615   } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
616     cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
617   }
618 #if RINGBUFFER_MIN_NUM_FILES > 0
619   else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
620     cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
621     capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
622 #endif
623 }
624
625
626 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
627 {
628     GList       *if_list;
629     if_info_t   *if_info;
630     int         err;
631     gchar       *err_str;
632
633
634     /* Did the user specify an interface to use? */
635     if (capture_opts->iface == NULL) {
636       /* No - is a default specified in the preferences file? */
637       if (capture_device != NULL) {
638           /* Yes - use it. */
639           capture_opts->iface = g_strdup(capture_device);
640           /*  We don't set iface_descr here because doing so requires
641            *  capture_ui_utils.c which requires epan/prefs.c which is
642            *  probably a bit too much dependency for here...
643            */
644       } else {
645         /* No - pick the first one from the list of interfaces. */
646         if_list = capture_interface_list(&err, &err_str);
647         if (if_list == NULL) {
648           switch (err) {
649
650           case CANT_GET_INTERFACE_LIST:
651               cmdarg_err("%s", err_str);
652               g_free(err_str);
653               break;
654
655           case NO_INTERFACES_FOUND:
656               cmdarg_err("There are no interfaces on which a capture can be done");
657               break;
658           }
659           return FALSE;
660         }
661         if_info = (if_info_t *)if_list->data;   /* first interface */
662         capture_opts->iface = g_strdup(if_info->name);
663         /*  We don't set iface_descr here because doing so requires
664          *  capture_ui_utils.c which requires epan/prefs.c which is
665          *  probably a bit too much dependency for here...
666          */
667         free_interface_list(if_list);
668       }
669     }
670
671     return TRUE;
672 }
673
674
675
676 #ifndef S_IFIFO
677 #define S_IFIFO _S_IFIFO
678 #endif
679 #ifndef S_ISFIFO
680 #define S_ISFIFO(mode)  (((mode) & S_IFMT) == S_IFIFO)
681 #endif
682
683 /* copied from filesystem.c */
684 static int capture_opts_test_for_fifo(const char *path)
685 {
686   ws_statb64 statb;
687
688   if (ws_stat64(path, &statb) < 0)
689     return errno;
690
691   if (S_ISFIFO(statb.st_mode))
692     return ESPIPE;
693   else
694     return 0;
695 }
696
697 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
698 {
699   int err;
700
701   *is_pipe = FALSE;
702
703   if (save_file != NULL) {
704     /* We're writing to a capture file. */
705     if (strcmp(save_file, "-") == 0) {
706       /* Writing to stdout. */
707       /* XXX - should we check whether it's a pipe?  It's arguably
708          silly to do "-w - >output_file" rather than "-w output_file",
709          but by not checking we might be violating the Principle Of
710          Least Astonishment. */
711       *is_pipe = TRUE;
712     } else {
713       /* not writing to stdout, test for a FIFO (aka named pipe) */
714       err = capture_opts_test_for_fifo(save_file);
715       switch (err) {
716
717       case ENOENT:      /* it doesn't exist, so we'll be creating it,
718                            and it won't be a FIFO */
719       case 0:           /* found it, but it's not a FIFO */
720         break;
721
722       case ESPIPE:      /* it is a FIFO */
723         *is_pipe = TRUE;
724         break;
725
726       default:          /* couldn't stat it              */
727         break;          /* ignore: later attempt to open */
728                         /*  will generate a nice msg     */
729       }
730     }
731   }
732
733   return 0;
734 }
735
736 #endif /* HAVE_LIBPCAP */