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