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