Second try. This time pipes and stdin are supported.
[jlayton/wireshark.git] / capture_opts.c
1 /* capture_opts.c
2  * Routines for capture options setting
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <string.h>
32 #include <ctype.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <glib.h>
39
40 #include <epan/packet.h>
41 #include <epan/prefs.h>
42 #include "ui/simple_dialog.h"
43 #include "capture_ui_utils.h"
44
45 #include "capture_opts.h"
46 #include "ringbuffer.h"
47 #include "clopts_common.h"
48 #include "console_io.h"
49 #include "cmdarg_err.h"
50
51 #include "capture_ifinfo.h"
52 #include "capture-pcap-util.h"
53 #include <wsutil/file_util.h>
54
55 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
56
57
58 void
59 capture_opts_init(capture_options *capture_opts, void *cf)
60 {
61   capture_opts->cf                              = cf;
62   capture_opts->ifaces                          = g_array_new(FALSE, FALSE, sizeof(interface_options));
63   capture_opts->all_ifaces                      = g_array_new(FALSE, FALSE, sizeof(interface_t));
64   capture_opts->num_selected                    = 0;
65   capture_opts->default_options.name            = NULL;
66   capture_opts->default_options.descr           = NULL;
67   capture_opts->default_options.cfilter         = NULL;
68   capture_opts->default_options.has_snaplen     = FALSE;
69   capture_opts->default_options.snaplen         = WTAP_MAX_PACKET_SIZE;
70   capture_opts->default_options.linktype        = -1;
71   capture_opts->default_options.promisc_mode    = TRUE;
72 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
73   capture_opts->default_options.buffer_size     = 1;                /* 1 MB */
74 #endif
75   capture_opts->default_options.monitor_mode    = FALSE;
76 #ifdef HAVE_PCAP_REMOTE
77   capture_opts->default_options.src_type        = CAPTURE_IFLOCAL;
78   capture_opts->default_options.remote_host     = NULL;
79   capture_opts->default_options.remote_port     = NULL;
80   capture_opts->default_options.auth_type       = CAPTURE_AUTH_NULL;
81   capture_opts->default_options.auth_username   = NULL;
82   capture_opts->default_options.auth_password   = NULL;
83   capture_opts->default_options.datatx_udp      = FALSE;
84   capture_opts->default_options.nocap_rpcap     = TRUE;
85   capture_opts->default_options.nocap_local     = FALSE;
86 #endif
87 #ifdef HAVE_PCAP_SETSAMPLING
88   capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
89   capture_opts->default_options.sampling_param  = 0;
90 #endif
91   capture_opts->saving_to_file                  = FALSE;
92   capture_opts->save_file                       = NULL;
93   capture_opts->group_read_access               = FALSE;
94 #ifdef PCAP_NG_DEFAULT
95   capture_opts->use_pcapng                      = TRUE;             /* Save as pcap-ng by default */
96 #else
97   capture_opts->use_pcapng                      = FALSE;            /* Save as pcap by default */
98 #endif
99   capture_opts->real_time_mode                  = TRUE;
100   capture_opts->show_info                       = TRUE;
101   capture_opts->quit_after_cap                  = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
102   capture_opts->restart                         = FALSE;
103
104   capture_opts->multi_files_on                  = FALSE;
105   capture_opts->has_file_duration               = FALSE;
106   capture_opts->file_duration                   = 60;               /* 1 min */
107   capture_opts->has_ring_num_files              = FALSE;
108   capture_opts->ring_num_files                  = RINGBUFFER_MIN_NUM_FILES;
109
110   capture_opts->has_autostop_files              = FALSE;
111   capture_opts->autostop_files                  = 1;
112   capture_opts->has_autostop_packets            = FALSE;
113   capture_opts->autostop_packets                = 0;
114   capture_opts->has_autostop_filesize           = FALSE;
115   capture_opts->autostop_filesize               = 1024;             /* 1 MB */
116   capture_opts->has_autostop_duration           = FALSE;
117   capture_opts->autostop_duration               = 60;               /* 1 min */
118
119
120   capture_opts->fork_child                      = -1;               /* invalid process handle */
121 #ifdef _WIN32
122   capture_opts->signal_pipe_write_fd            = -1;
123 #endif
124   capture_opts->state                           = CAPTURE_STOPPED;
125   capture_opts->output_to_pipe                  = FALSE;
126 #ifndef _WIN32
127   capture_opts->owner                           = getuid();
128   capture_opts->group                           = getgid();
129 #endif
130 }
131
132
133 /* log content of capture_opts */
134 void
135 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
136     guint i;
137
138     g_log(log_domain, log_level, "CAPTURE OPTIONS     :");
139     g_log(log_domain, log_level, "CFile               : %p", capture_opts->cf);
140
141     for (i = 0; i < capture_opts->ifaces->len; i++) {
142         interface_options interface_opts;
143
144         interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
145         g_log(log_domain, log_level, "Interface name[%02d]  : %s", i, interface_opts.name);
146         g_log(log_domain, log_level, "Interface Descr[%02d] : %s", i, interface_opts.descr);
147         g_log(log_domain, log_level, "Capture filter[%02d]  : %s", i, interface_opts.cfilter);
148         g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
149         g_log(log_domain, log_level, "Link Type[%02d]       : %d", i, interface_opts.linktype);
150         g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
151 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
152         g_log(log_domain, log_level, "Buffer size[%02d]     : %d (MB)", i, interface_opts.buffer_size);
153 #endif
154         g_log(log_domain, log_level, "Monitor Mode[%02d]    : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
155 #ifdef HAVE_PCAP_REMOTE
156         g_log(log_domain, log_level, "Capture source[%02d]  : %s", i,
157             interface_opts.src_type == CAPTURE_IFLOCAL ? "Local interface" :
158             interface_opts.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
159             "Unknown");
160         if (interface_opts.src_type == CAPTURE_IFREMOTE) {
161             g_log(log_domain, log_level, "Remote host[%02d]     : %s", i, interface_opts.remote_host);
162             g_log(log_domain, log_level, "Remote port[%02d]     : %s", i, interface_opts.remote_port);
163         }
164         g_log(log_domain, log_level, "Authentication[%02d]  : %s", i,
165             interface_opts.auth_type == CAPTURE_AUTH_NULL ? "Null" :
166             interface_opts.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
167             "Unknown");
168         if (interface_opts.auth_type == CAPTURE_AUTH_PWD) {
169             g_log(log_domain, log_level, "Auth username[%02d]   : %s", i, interface_opts.auth_username);
170             g_log(log_domain, log_level, "Auth password[%02d]   : <hidden>", i);
171         }
172         g_log(log_domain, log_level, "UDP data tfer[%02d]   : %u", i, interface_opts.datatx_udp);
173         g_log(log_domain, log_level, "No cap. RPCAP[%02d]   : %u", i, interface_opts.nocap_rpcap);
174         g_log(log_domain, log_level, "No cap. local[%02d]   : %u", i, interface_opts.nocap_local);
175 #endif
176 #ifdef HAVE_PCAP_SETSAMPLING
177         g_log(log_domain, log_level, "Sampling meth.[%02d]  : %d", i, interface_opts.sampling_method);
178         g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts.sampling_param);
179 #endif
180     }
181     g_log(log_domain, log_level, "Interface name[df]  : %s", capture_opts->default_options.name);
182     g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr);
183     g_log(log_domain, log_level, "Capture filter[df]  : %s", capture_opts->default_options.cfilter);
184     g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
185     g_log(log_domain, log_level, "Link Type[df]       : %d", capture_opts->default_options.linktype);
186     g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
187 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
188     g_log(log_domain, log_level, "Buffer size[df]     : %d (MB)", capture_opts->default_options.buffer_size);
189 #endif
190     g_log(log_domain, log_level, "Monitor Mode[df]    : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
191 #ifdef HAVE_PCAP_REMOTE
192     g_log(log_domain, log_level, "Capture source[df]  : %s",
193         capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" :
194         capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" :
195         "Unknown");
196     if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) {
197         g_log(log_domain, log_level, "Remote host[df]     : %s", capture_opts->default_options.remote_host);
198         g_log(log_domain, log_level, "Remote port[df]     : %s", capture_opts->default_options.remote_port);
199     }
200     g_log(log_domain, log_level, "Authentication[df]  : %s",
201         capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" :
202         capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" :
203         "Unknown");
204     if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) {
205         g_log(log_domain, log_level, "Auth username[df]   : %s", capture_opts->default_options.auth_username);
206         g_log(log_domain, log_level, "Auth password[df]   : <hidden>");
207     }
208     g_log(log_domain, log_level, "UDP data tfer[df]   : %u", capture_opts->default_options.datatx_udp);
209     g_log(log_domain, log_level, "No cap. RPCAP[df]   : %u", capture_opts->default_options.nocap_rpcap);
210     g_log(log_domain, log_level, "No cap. local[df]   : %u", capture_opts->default_options.nocap_local);
211 #endif
212 #ifdef HAVE_PCAP_SETSAMPLING
213     g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method);
214     g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param);
215 #endif
216     g_log(log_domain, log_level, "SavingToFile        : %u", capture_opts->saving_to_file);
217     g_log(log_domain, log_level, "SaveFile            : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
218     g_log(log_domain, log_level, "GroupReadAccess     : %u", capture_opts->group_read_access);
219     g_log(log_domain, log_level, "Fileformat          : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP");
220     g_log(log_domain, log_level, "RealTimeMode        : %u", capture_opts->real_time_mode);
221     g_log(log_domain, log_level, "ShowInfo            : %u", capture_opts->show_info);
222     g_log(log_domain, log_level, "QuitAfterCap        : %u", capture_opts->quit_after_cap);
223
224     g_log(log_domain, log_level, "MultiFilesOn        : %u", capture_opts->multi_files_on);
225     g_log(log_domain, log_level, "FileDuration    (%u) : %u", capture_opts->has_file_duration, capture_opts->file_duration);
226     g_log(log_domain, log_level, "RingNumFiles    (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
227
228     g_log(log_domain, log_level, "AutostopFiles   (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
229     g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
230     g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
231     g_log(log_domain, log_level, "AutostopDuration(%u) : %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
232
233     g_log(log_domain, log_level, "ForkChild           : %d", capture_opts->fork_child);
234 #ifdef _WIN32
235     g_log(log_domain, log_level, "SignalPipeWrite     : %d", capture_opts->signal_pipe_write_fd);
236 #endif
237 }
238
239 /*
240  * Given a string of the form "<autostop criterion>:<value>", as might appear
241  * as an argument to a "-a" option, parse it and set the criterion in
242  * question.  Return an indication of whether it succeeded or failed
243  * in some fashion.
244  */
245 static gboolean
246 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
247 {
248   gchar *p, *colonp;
249
250   colonp = strchr(autostoparg, ':');
251   if (colonp == NULL)
252     return FALSE;
253
254   p = colonp;
255   *p++ = '\0';
256
257   /*
258    * Skip over any white space (there probably won't be any, but
259    * as we allow it in the preferences file, we might as well
260    * allow it here).
261    */
262   while (isspace((guchar)*p))
263     p++;
264   if (*p == '\0') {
265     /*
266      * Put the colon back, so if our caller uses, in an
267      * error message, the string they passed us, the message
268      * looks correct.
269      */
270     *colonp = ':';
271     return FALSE;
272   }
273   if (strcmp(autostoparg,"duration") == 0) {
274     capture_opts->has_autostop_duration = TRUE;
275     capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
276   } else if (strcmp(autostoparg,"filesize") == 0) {
277     capture_opts->has_autostop_filesize = TRUE;
278     capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
279   } else if (strcmp(autostoparg,"files") == 0) {
280     capture_opts->multi_files_on = TRUE;
281     capture_opts->has_autostop_files = TRUE;
282     capture_opts->autostop_files = get_positive_int(p,"autostop files");
283   } else {
284     return FALSE;
285   }
286   *colonp = ':'; /* put the colon back */
287   return TRUE;
288 }
289
290 /*
291  * Given a string of the form "<ring buffer file>:<duration>", as might appear
292  * as an argument to a "-b" option, parse it and set the arguments in
293  * question.  Return an indication of whether it succeeded or failed
294  * in some fashion.
295  */
296 static gboolean
297 get_ring_arguments(capture_options *capture_opts, const char *arg)
298 {
299   gchar *p = NULL, *colonp;
300
301   colonp = strchr(arg, ':');
302   if (colonp == NULL)
303     return FALSE;
304
305   p = colonp;
306   *p++ = '\0';
307
308   /*
309    * Skip over any white space (there probably won't be any, but
310    * as we allow it in the preferences file, we might as well
311    * allow it here).
312    */
313   while (isspace((guchar)*p))
314     p++;
315   if (*p == '\0') {
316     /*
317      * Put the colon back, so if our caller uses, in an
318      * error message, the string they passed us, the message
319      * looks correct.
320      */
321     *colonp = ':';
322     return FALSE;
323   }
324
325   if (strcmp(arg,"files") == 0) {
326     capture_opts->has_ring_num_files = TRUE;
327     capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
328   } else if (strcmp(arg,"filesize") == 0) {
329     capture_opts->has_autostop_filesize = TRUE;
330     capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
331   } else if (strcmp(arg,"duration") == 0) {
332     capture_opts->has_file_duration = TRUE;
333     capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
334   }
335
336   *colonp = ':';    /* put the colon back */
337   return TRUE;
338 }
339
340 #ifdef HAVE_PCAP_SETSAMPLING
341 /*
342  * Given a string of the form "<sampling type>:<value>", as might appear
343  * as an argument to a "-m" 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_sampling_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     if (*p == '\0') {
362         *colonp = ':';
363         return FALSE;
364     }
365
366     if (strcmp(arg, "count") == 0) {
367         if (capture_opts->ifaces->len > 0) {
368             interface_options interface_opts;
369
370             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
371             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
372             interface_opts.sampling_method = CAPTURE_SAMP_BY_COUNT;
373             interface_opts.sampling_param = get_positive_int(p, "sampling count");
374             g_array_append_val(capture_opts->ifaces, interface_opts);
375         } else {
376             capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_COUNT;
377             capture_opts->default_options.sampling_param = get_positive_int(p, "sampling count");
378         }
379     } else if (strcmp(arg, "timer") == 0) {
380         if (capture_opts->ifaces->len > 0) {
381             interface_options interface_opts;
382
383             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
384             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
385             interface_opts.sampling_method = CAPTURE_SAMP_BY_TIMER;
386             interface_opts.sampling_param = get_positive_int(p, "sampling timer");
387             g_array_append_val(capture_opts->ifaces, interface_opts);
388         } else {
389             capture_opts->default_options.sampling_method = CAPTURE_SAMP_BY_TIMER;
390             capture_opts->default_options.sampling_param = get_positive_int(p, "sampling timer");
391         }
392     }
393     *colonp = ':';
394     return TRUE;
395 }
396 #endif
397
398 #ifdef HAVE_PCAP_REMOTE
399 /*
400  * Given a string of the form "<username>:<password>", as might appear
401  * as an argument to a "-A" option, parse it and set the arguments in
402  * question.  Return an indication of whether it succeeded or failed
403  * in some fashion.
404  */
405 static gboolean
406 get_auth_arguments(capture_options *capture_opts, const char *arg)
407 {
408     gchar *p = NULL, *colonp;
409
410     colonp = strchr(arg, ':');
411     if (colonp == NULL)
412         return FALSE;
413
414     p = colonp;
415     *p++ = '\0';
416
417     while (isspace((guchar)*p))
418         p++;
419
420     if (capture_opts->ifaces->len > 0) {
421         interface_options interface_opts;
422
423         interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
424         capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
425         interface_opts.auth_type = CAPTURE_AUTH_PWD;
426         interface_opts.auth_username = g_strdup(arg);
427         interface_opts.auth_password = g_strdup(p);
428         g_array_append_val(capture_opts->ifaces, interface_opts);
429     } else {
430         capture_opts->default_options.auth_type = CAPTURE_AUTH_PWD;
431         capture_opts->default_options.auth_username = g_strdup(arg);
432         capture_opts->default_options.auth_password = g_strdup(p);
433     }
434     *colonp = ':';
435     return TRUE;
436 }
437 #endif
438
439 static int
440 capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str_p)
441 {
442     long        adapter_index;
443     char        *p;
444     GList       *if_list;
445     if_info_t   *if_info;
446     int         err;
447     gchar       *err_str;
448     interface_options interface_opts;
449
450
451     /*
452      * If the argument is a number, treat it as an index into the list
453      * of adapters, as printed by "tshark -D".
454      *
455      * This should be OK on UNIX systems, as interfaces shouldn't have
456      * names that begin with digits.  It can be useful on Windows, where
457      * more than one interface can have the same name.
458      */
459     adapter_index = strtol(optarg_str_p, &p, 10);
460     if (p != NULL && *p == '\0') {
461         if (adapter_index < 0) {
462             cmdarg_err("The specified adapter index is a negative number");
463             return 1;
464         }
465         if (adapter_index > INT_MAX) {
466             cmdarg_err("The specified adapter index is too large (greater than %d)",
467                        INT_MAX);
468             return 1;
469         }
470         if (adapter_index == 0) {
471             cmdarg_err("There is no interface with that adapter index");
472             return 1;
473         }
474         if_list = capture_interface_list(&err, &err_str);
475         if (if_list == NULL) {
476             switch (err) {
477
478             case CANT_GET_INTERFACE_LIST:
479             case DONT_HAVE_PCAP:
480                 cmdarg_err("%s", err_str);
481                 g_free(err_str);
482                 break;
483
484             case NO_INTERFACES_FOUND:
485                 cmdarg_err("There are no interfaces on which a capture can be done");
486                 break;
487             }
488             return 2;
489         }
490         if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
491         if (if_info == NULL) {
492             cmdarg_err("There is no interface with that adapter index");
493             return 1;
494         }
495         interface_opts.name = g_strdup(if_info->name);
496         /*  We don't set iface_descr here because doing so requires
497          *  capture_ui_utils.c which requires epan/prefs.c which is
498          *  probably a bit too much dependency for here...
499          */
500         free_interface_list(if_list);
501     } else {
502         interface_opts.name = g_strdup(optarg_str_p);
503     }
504     interface_opts.descr = g_strdup(capture_opts->default_options.descr);
505     interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
506     interface_opts.snaplen = capture_opts->default_options.snaplen;
507     interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
508     interface_opts.linktype = capture_opts->default_options.linktype;
509     interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
510 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
511     interface_opts.buffer_size = capture_opts->default_options.buffer_size;
512 #endif
513     interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
514 #ifdef HAVE_PCAP_REMOTE
515     interface_opts.src_type = capture_opts->default_options.src_type;
516     interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
517     interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
518     interface_opts.auth_type = capture_opts->default_options.auth_type;
519     interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
520     interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
521     interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
522     interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
523     interface_opts.nocap_local = capture_opts->default_options.nocap_local;
524 #endif
525 #ifdef HAVE_PCAP_SETSAMPLING
526     interface_opts.sampling_method = capture_opts->default_options.sampling_method;
527     interface_opts.sampling_param  = capture_opts->default_options.sampling_param;
528 #endif
529
530     g_array_append_val(capture_opts->ifaces, interface_opts);
531
532     return 0;
533 }
534
535 int
536 capture_opts_select_iface(capture_options *capture_opts, const char *optarg_str_p)
537 {
538     long        adapter_index;
539     char        *p;
540     GList       *if_list;
541     if_info_t   *if_info;
542     int         err;
543     guint       i;
544     gchar       *err_str, *name = NULL;
545     interface_t device;
546     gboolean    found = FALSE;
547     interface_options interface_opts;
548
549     /*
550      * If the argument is a number, treat it as an index into the list
551      * of adapters, as printed by "tshark -D".
552      *
553      * This should be OK on UNIX systems, as interfaces shouldn't have
554      * names that begin with digits.  It can be useful on Windows, where
555      * more than one interface can have the same name.
556      */
557     adapter_index = strtol(optarg_str_p, &p, 10);
558     if (p != NULL && *p == '\0') {
559         if (adapter_index < 0) {
560             cmdarg_err("The specified adapter index is a negative number");
561             return 1;
562         }
563         if (adapter_index > INT_MAX) {
564             cmdarg_err("The specified adapter index is too large (greater than %d)",
565                        INT_MAX);
566             return 1;
567         }
568         if (adapter_index == 0) {
569             cmdarg_err("There is no interface with that adapter index");
570             return 1;
571         }
572         if_list = capture_interface_list(&err, &err_str);
573         if (if_list == NULL) {
574             switch (err) {
575
576             case CANT_GET_INTERFACE_LIST:
577                 cmdarg_err("%s", err_str);
578                 g_free(err_str);
579                 break;
580
581             case NO_INTERFACES_FOUND:
582                 cmdarg_err("There are no interfaces on which a capture can be done");
583                 break;
584             }
585             return 2;
586         }
587         if_info = (if_info_t *)g_list_nth_data(if_list, adapter_index - 1);
588         if (if_info == NULL) {
589             cmdarg_err("There is no interface with that adapter index");
590             return 1;
591         }
592         name = g_strdup(if_info->name);
593         /*  We don't set iface_descr here because doing so requires
594          *  capture_ui_utils.c which requires epan/prefs.c which is
595          *  probably a bit too much dependency for here...
596          */
597         free_interface_list(if_list);
598     } else {
599         name = g_strdup(optarg_str_p);
600     }
601     if (capture_opts->all_ifaces->len > 0) {
602         for(i = 0; i < capture_opts->all_ifaces->len; i++) {
603             device = g_array_index(capture_opts->all_ifaces, interface_t, i);
604             if (strcmp(device.name, name) == 0) {
605                 if (device.hidden) {
606                     cmdarg_err("Interface %s is hidden. You can't capture on hidden interfaces.", name);
607                     return 1;
608                 }
609                 device.selected = TRUE;
610                 capture_opts->num_selected++;
611                 capture_opts->all_ifaces = g_array_remove_index(capture_opts->all_ifaces, i);
612                 g_array_insert_val(capture_opts->all_ifaces, i, device);
613                 found = TRUE;
614                 break;
615             }
616         } 
617         if (!found) {
618             device.name         = g_strdup(name);
619             device.display_name = g_strdup_printf("%s", device.name);
620             device.hidden       = FALSE;
621             device.selected     = TRUE;
622             device.type         = IF_PIPE;
623             device.pmode        = capture_opts->default_options.promisc_mode;
624             device.has_snaplen  = capture_opts->default_options.has_snaplen;
625             device.snaplen      = capture_opts->default_options.snaplen;
626             device.cfilter      = g_strdup(capture_opts->default_options.cfilter);
627             device.addresses    = NULL;
628             device.no_addresses = 0;
629             device.last_packets = 0;
630             device.links        = NULL;
631             device.active_dlt   = -1;
632             device.local        = TRUE;
633             device.locked       = FALSE;
634 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
635             device.buffer = 1;
636             device.monitor_mode_enabled   = FALSE;
637             device.monitor_mode_supported = FALSE;
638 #endif
639             g_array_append_val(capture_opts->all_ifaces, device);
640             capture_opts->num_selected++;
641         }
642     } else {
643         interface_opts.name = g_strdup(optarg_str_p);
644         interface_opts.descr = g_strdup(capture_opts->default_options.descr);
645         interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
646         interface_opts.snaplen = capture_opts->default_options.snaplen;
647         interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
648         interface_opts.linktype = capture_opts->default_options.linktype;
649         interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
650 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
651         interface_opts.buffer_size = capture_opts->default_options.buffer_size;
652 #endif
653         interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
654 #ifdef HAVE_PCAP_REMOTE
655         interface_opts.src_type = capture_opts->default_options.src_type;
656         interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
657         interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
658         interface_opts.auth_type = capture_opts->default_options.auth_type;
659         interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
660         interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
661         interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
662         interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
663         interface_opts.nocap_local = capture_opts->default_options.nocap_local;
664 #endif
665 #ifdef HAVE_PCAP_SETSAMPLING
666         interface_opts.sampling_method = capture_opts->default_options.sampling_method;
667         interface_opts.sampling_param  = capture_opts->default_options.sampling_param;
668 #endif
669         g_array_append_val(capture_opts->ifaces, interface_opts);
670
671         device.name         = g_strdup(name);
672         device.display_name = g_strdup_printf("%s", device.name);
673         device.hidden       = FALSE;
674         device.selected     = TRUE;
675         device.type         = IF_PIPE;
676         device.pmode        = capture_opts->default_options.promisc_mode;
677         device.has_snaplen  = capture_opts->default_options.has_snaplen;
678         device.snaplen      = capture_opts->default_options.snaplen;
679         device.cfilter      = g_strdup(capture_opts->default_options.cfilter);
680         device.addresses    = NULL;
681         device.no_addresses = 0;
682         device.last_packets = 0;
683         device.links        = NULL;
684         device.active_dlt   = -1;
685         device.local        = TRUE;
686         device.locked       = FALSE;
687 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
688             device.buffer = 1;
689         device.monitor_mode_enabled   = FALSE;
690         device.monitor_mode_supported = FALSE;
691 #endif
692         g_array_append_val(capture_opts->all_ifaces, device);
693         capture_opts->num_selected++;
694     }
695     return 0;
696 }
697
698 int
699 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_str_p, gboolean *start_capture)
700 {
701     int status, snaplen;
702
703     switch(opt) {
704     case 'a':        /* autostop criteria */
705         if (set_autostop_criterion(capture_opts, optarg_str_p) == FALSE) {
706             cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg_str_p);
707             return 1;
708         }
709         break;
710 #ifdef HAVE_PCAP_REMOTE
711     case 'A':
712         if (get_auth_arguments(capture_opts, optarg_str_p) == FALSE) {
713             cmdarg_err("Invalid or unknown -A arg \"%s\"", optarg_str_p);
714             return 1;
715         }
716         break;
717 #endif
718     case 'b':        /* Ringbuffer option */
719         capture_opts->multi_files_on = TRUE;
720         if (get_ring_arguments(capture_opts, optarg_str_p) == FALSE) {
721             cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg_str_p);
722             return 1;
723         }
724         break;
725 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
726     case 'B':        /* Buffer size */
727         if (capture_opts->ifaces->len > 0) {
728             interface_options interface_opts;
729
730             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
731             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
732             interface_opts.buffer_size = get_positive_int(optarg_str_p, "buffer size");
733             g_array_append_val(capture_opts->ifaces, interface_opts);
734         } else {
735             capture_opts->default_options.buffer_size = get_positive_int(optarg_str_p, "buffer size");
736         }
737         break;
738 #endif
739     case 'c':        /* Capture n packets */
740         capture_opts->has_autostop_packets = TRUE;
741         capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
742         break;
743     case 'f':        /* capture filter */
744         if (capture_opts->ifaces->len > 0) {
745             interface_options interface_opts;
746
747             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
748             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
749             g_free(interface_opts.cfilter);
750             interface_opts.cfilter = g_strdup(optarg_str_p);
751             g_array_append_val(capture_opts->ifaces, interface_opts);
752         } else {
753             g_free(capture_opts->default_options.cfilter);
754             capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
755         }
756         break;
757     case 'H':        /* Hide capture info dialog box */
758         capture_opts->show_info = FALSE;
759         break;
760     case 'i':        /* Use interface x */
761         status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
762         if (status != 0) {
763             return status;
764         }
765         break;
766 #ifdef HAVE_PCAP_CREATE
767     case 'I':        /* Capture in monitor mode */
768         if (capture_opts->ifaces->len > 0) {
769             interface_options interface_opts;
770
771             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
772             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
773             interface_opts.monitor_mode = TRUE;
774             g_array_append_val(capture_opts->ifaces, interface_opts);
775         } else {
776             capture_opts->default_options.monitor_mode = TRUE;
777         }
778         break;
779 #endif
780     case 'k':        /* Start capture immediately */
781         *start_capture = TRUE;
782         break;
783     /*case 'l':*/    /* Automatic scrolling in live capture mode */
784 #ifdef HAVE_PCAP_SETSAMPLING
785     case 'm':
786         if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
787             cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
788             return 1;
789         }
790         break;
791 #endif
792     case 'n':        /* Use pcapng format */
793         capture_opts->use_pcapng = TRUE;
794         break;
795     case 'p':        /* Don't capture in promiscuous mode */
796         if (capture_opts->ifaces->len > 0) {
797             interface_options interface_opts;
798
799             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
800             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
801             interface_opts.promisc_mode = FALSE;
802             g_array_append_val(capture_opts->ifaces, interface_opts);
803         } else {
804             capture_opts->default_options.promisc_mode = FALSE;
805         }
806         break;
807     case 'P':        /* Use pcap format */
808         capture_opts->use_pcapng = FALSE;
809         break;
810 #ifdef HAVE_PCAP_REMOTE
811     case 'r':
812         if (capture_opts->ifaces->len > 0) {
813             interface_options interface_opts;
814
815             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
816             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
817             interface_opts.nocap_rpcap = FALSE;
818             g_array_append_val(capture_opts->ifaces, interface_opts);
819         } else {
820             capture_opts->default_options.nocap_rpcap = FALSE;
821         }
822         break;
823 #endif
824     case 's':        /* Set the snapshot (capture) length */
825         snaplen = get_natural_int(optarg_str_p, "snapshot length");
826         /*
827          * Make a snapshot length of 0 equivalent to the maximum packet
828          * length, mirroring what tcpdump does.
829          */
830         if (snaplen == 0)
831             snaplen = WTAP_MAX_PACKET_SIZE;
832         if (capture_opts->ifaces->len > 0) {
833             interface_options interface_opts;
834
835             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
836             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
837             interface_opts.has_snaplen = TRUE;
838             interface_opts.snaplen = snaplen;
839             g_array_append_val(capture_opts->ifaces, interface_opts);
840         } else {
841             capture_opts->default_options.snaplen = snaplen;
842             capture_opts->default_options.has_snaplen = TRUE;
843         }
844         break;
845     case 'S':        /* "Real-Time" mode: used for following file ala tail -f */
846         capture_opts->real_time_mode = TRUE;
847         break;
848 #ifdef HAVE_PCAP_REMOTE
849     case 'u':
850         if (capture_opts->ifaces->len > 0) {
851             interface_options interface_opts;
852
853             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
854             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
855             interface_opts.datatx_udp = TRUE;
856             g_array_append_val(capture_opts->ifaces, interface_opts);
857         } else {
858             capture_opts->default_options.datatx_udp = TRUE;
859         }
860         break;
861 #endif
862     case 'w':        /* Write to capture file x */
863         capture_opts->saving_to_file = TRUE;
864         g_free(capture_opts->save_file);
865         capture_opts->save_file = g_strdup(optarg_str_p);
866         status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
867         return status;
868     case 'g':        /* enable group read access on the capture file(s) */
869         capture_opts->group_read_access = TRUE;
870         break;
871     case 'y':        /* Set the pcap data link type */
872         if (capture_opts->ifaces->len > 0) {
873             interface_options interface_opts;
874
875             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
876             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
877             interface_opts.linktype = linktype_name_to_val(optarg_str_p);
878             if (interface_opts.linktype == -1) {
879                 cmdarg_err("The specified data link type \"%s\" isn't valid",
880                            optarg_str_p);
881                 return 1;
882             }
883             g_array_append_val(capture_opts->ifaces, interface_opts);
884         } else {
885             capture_opts->default_options.linktype = linktype_name_to_val(optarg_str_p);
886             if (capture_opts->default_options.linktype == -1) {
887                 cmdarg_err("The specified data link type \"%s\" isn't valid",
888                            optarg_str_p);
889                 return 1;
890             }
891         }
892         break;
893     default:
894         /* the caller is responsible to send us only the right opt's */
895         g_assert_not_reached();
896     }
897
898     return 0;
899 }
900
901 void
902 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
903                                    gboolean monitor_mode)
904 {
905     GList *lt_entry;
906     data_link_info_t *data_link_info;
907
908     if (caps->can_set_rfmon)
909         fprintf_stderr("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
910                        name, monitor_mode ? "" : "not ");
911     else
912         fprintf_stderr("Data link types of interface %s (use option -y to set):\n", name);
913     for (lt_entry = caps->data_link_types; lt_entry != NULL;
914          lt_entry = g_list_next(lt_entry)) {
915         data_link_info = (data_link_info_t *)lt_entry->data;
916         fprintf_stderr("  %s", data_link_info->name);
917         if (data_link_info->description != NULL)
918             fprintf_stderr(" (%s)", data_link_info->description);
919         else
920             fprintf_stderr(" (not supported)");
921         fprintf_stderr("\n");
922     }
923 }
924
925 /* Print an ASCII-formatted list of interfaces. */
926 void
927 capture_opts_print_interfaces(GList *if_list)
928 {
929     int         i;
930     GList       *if_entry;
931     if_info_t   *if_info;
932
933     i = 1;  /* Interface id number */
934     for (if_entry = g_list_first(if_list); if_entry != NULL;
935          if_entry = g_list_next(if_entry)) {
936         if_info = (if_info_t *)if_entry->data;
937         fprintf_stderr("%d. %s", i++, if_info->name);
938
939         /* Print the description if it exists */
940         if (if_info->description != NULL)
941             fprintf_stderr(" (%s)", if_info->description);
942         fprintf_stderr("\n");
943     }
944 }
945
946
947 void capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
948 {
949     guint i;
950     interface_options interface_opts;
951
952     if (capture_opts->ifaces->len > 0) {
953         for (i = 0; i < capture_opts->ifaces->len; i++) {
954             interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
955             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
956             if (interface_opts.snaplen < 1)
957                 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
958             else if (interface_opts.snaplen < snaplen_min)
959                 interface_opts.snaplen = snaplen_min;
960             g_array_append_val(capture_opts->ifaces, interface_opts);
961         }
962     } else {
963         if (capture_opts->default_options.snaplen < 1)
964             capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
965         else if (capture_opts->default_options.snaplen < snaplen_min)
966             capture_opts->default_options.snaplen = snaplen_min;
967     }
968 }
969
970
971 void capture_opts_trim_ring_num_files(capture_options *capture_opts)
972 {
973     /* Check the value range of the ring_num_files parameter */
974     if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
975         cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
976         capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
977     } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
978         cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
979     }
980 #if RINGBUFFER_MIN_NUM_FILES > 0
981     else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
982         cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
983         capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
984 #endif
985 }
986
987
988 gboolean capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
989 {
990     GList       *if_list;
991     if_info_t   *if_info;
992     int         err;
993     gchar       *err_str;
994     interface_options interface_opts;
995
996
997     /* Did the user specify an interface to use? */
998     if (capture_opts->num_selected == 0 && capture_opts->ifaces->len == 0) {
999         /* No - is a default specified in the preferences file? */
1000         if (capture_device != NULL) {
1001             /* Yes - use it. */
1002             interface_opts.name = g_strdup(capture_device);
1003             /*  We don't set iface_descr here because doing so requires
1004              *  capture_ui_utils.c which requires epan/prefs.c which is
1005              *  probably a bit too much dependency for here...
1006              */
1007         } else {
1008             /* No - pick the first one from the list of interfaces. */
1009             if_list = capture_interface_list(&err, &err_str);
1010             if (if_list == NULL) {
1011                 switch (err) {
1012
1013                 case CANT_GET_INTERFACE_LIST:
1014                 case DONT_HAVE_PCAP:
1015                     cmdarg_err("%s", err_str);
1016                     g_free(err_str);
1017                     break;
1018
1019                 case NO_INTERFACES_FOUND:
1020                     cmdarg_err("There are no interfaces on which a capture can be done");
1021                     break;
1022                 }
1023                 return FALSE;
1024             }
1025             if_info = (if_info_t *)if_list->data;       /* first interface */
1026             interface_opts.name = g_strdup(if_info->name);
1027             /*  We don't set iface_descr here because doing so requires
1028              *  capture_ui_utils.c which requires epan/prefs.c which is
1029              *  probably a bit too much dependency for here...
1030              */
1031             free_interface_list(if_list);
1032         }
1033         if (capture_opts->default_options.descr) {
1034             interface_opts.descr = g_strdup(capture_opts->default_options.descr);
1035         } else {
1036             interface_opts.descr = NULL;
1037         }
1038         interface_opts.cfilter = g_strdup(capture_opts->default_options.cfilter);
1039         interface_opts.snaplen = capture_opts->default_options.snaplen;
1040         interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
1041         interface_opts.linktype = capture_opts->default_options.linktype;
1042         interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
1043 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
1044         interface_opts.buffer_size = capture_opts->default_options.buffer_size;
1045 #endif
1046         interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
1047 #ifdef HAVE_PCAP_REMOTE
1048         interface_opts.src_type = capture_opts->default_options.src_type;
1049         interface_opts.remote_host = g_strdup(capture_opts->default_options.remote_host);
1050         interface_opts.remote_port = g_strdup(capture_opts->default_options.remote_port);
1051         interface_opts.auth_type = capture_opts->default_options.auth_type;
1052         interface_opts.auth_username = g_strdup(capture_opts->default_options.auth_username);
1053         interface_opts.auth_password = g_strdup(capture_opts->default_options.auth_password);
1054         interface_opts.datatx_udp = capture_opts->default_options.datatx_udp;
1055         interface_opts.nocap_rpcap = capture_opts->default_options.nocap_rpcap;
1056         interface_opts.nocap_local = capture_opts->default_options.nocap_local;
1057 #endif
1058 #ifdef HAVE_PCAP_SETSAMPLING
1059         interface_opts.sampling_method = capture_opts->default_options.sampling_method;
1060         interface_opts.sampling_param  = capture_opts->default_options.sampling_param;
1061 #endif
1062         g_array_append_val(capture_opts->ifaces, interface_opts);
1063     }
1064
1065     return TRUE;
1066 }
1067
1068
1069
1070 #ifndef S_IFIFO
1071 #define S_IFIFO _S_IFIFO
1072 #endif
1073 #ifndef S_ISFIFO
1074 #define S_ISFIFO(mode)  (((mode) & S_IFMT) == S_IFIFO)
1075 #endif
1076
1077 /* copied from filesystem.c */
1078 static int capture_opts_test_for_fifo(const char *path)
1079 {
1080   ws_statb64 statb;
1081
1082   if (ws_stat64(path, &statb) < 0)
1083     return errno;
1084
1085   if (S_ISFIFO(statb.st_mode))
1086     return ESPIPE;
1087   else
1088     return 0;
1089 }
1090
1091 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
1092 {
1093   int err;
1094
1095   *is_pipe = FALSE;
1096
1097   if (save_file != NULL) {
1098     /* We're writing to a capture file. */
1099     if (strcmp(save_file, "-") == 0) {
1100       /* Writing to stdout. */
1101       /* XXX - should we check whether it's a pipe?  It's arguably
1102          silly to do "-w - >output_file" rather than "-w output_file",
1103          but by not checking we might be violating the Principle Of
1104          Least Astonishment. */
1105       *is_pipe = TRUE;
1106     } else {
1107       /* not writing to stdout, test for a FIFO (aka named pipe) */
1108       err = capture_opts_test_for_fifo(save_file);
1109       switch (err) {
1110
1111       case ENOENT:      /* it doesn't exist, so we'll be creating it,
1112                            and it won't be a FIFO */
1113       case 0:           /* found it, but it's not a FIFO */
1114         break;
1115
1116       case ESPIPE:      /* it is a FIFO */
1117         *is_pipe = TRUE;
1118         break;
1119
1120       default:          /* couldn't stat it              */
1121         break;          /* ignore: later attempt to open */
1122                         /*  will generate a nice msg     */
1123       }
1124     }
1125   }
1126
1127   return 0;
1128 }
1129
1130 void
1131 collect_ifaces(capture_options *capture_opts)
1132 {
1133   guint i;
1134   interface_t device;
1135   interface_options interface_opts;
1136   for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1137     device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1138     if (!device.hidden && device.selected) {
1139       interface_opts.name = g_strdup(device.name);
1140       interface_opts.descr = g_strdup(device.display_name);
1141       interface_opts.monitor_mode = device.monitor_mode_enabled;
1142       interface_opts.linktype = device.active_dlt;
1143       interface_opts.cfilter = g_strdup(device.cfilter);
1144       interface_opts.snaplen = device.snaplen;
1145       interface_opts.has_snaplen = device.has_snaplen;
1146       interface_opts.promisc_mode = device.pmode;
1147 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
1148       interface_opts.buffer_size =  device.buffer;
1149 #endif
1150       if (!device.local) {
1151 #ifdef HAVE_PCAP_REMOTE 
1152         interface_opts.src_type = CAPTURE_IFREMOTE;
1153         interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1154         interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1155         interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1156         interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1157         interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1158         interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1159         interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1160         interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1161 #endif
1162 #ifdef HAVE_PCAP_SETSAMPLING
1163         interface_opts.sampling_method = device.remote_opts.sampling_method;
1164         interface_opts.sampling_param  = device.remote_opts.sampling_param;
1165 #endif
1166       }
1167       g_array_append_val(capture_opts->ifaces, interface_opts);
1168     } else {
1169       continue;
1170     }
1171   }
1172 }
1173
1174
1175 #endif /* HAVE_LIBPCAP */