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