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