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