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