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