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