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