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