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