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