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