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