Send the output of the -D and -L options to the standard output rather
[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 "cmdarg_err.h"
49
50 #include "capture_ifinfo.h"
51 #include "capture-pcap-util.h"
52 #include <wsutil/file_util.h>
53
54 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
55
56
57 void
58 capture_opts_init(capture_options *capture_opts, void *cf)
59 {
60   capture_opts->cf                              = cf;
61   capture_opts->ifaces                          = g_array_new(FALSE, FALSE, sizeof(interface_options));
62   capture_opts->all_ifaces                      = g_array_new(FALSE, FALSE, sizeof(interface_t));
63   capture_opts->num_selected                    = 0;
64   capture_opts->default_options.name            = NULL;
65   capture_opts->default_options.descr           = NULL;
66   capture_opts->default_options.cfilter         = NULL;
67   capture_opts->default_options.has_snaplen     = FALSE;
68   capture_opts->default_options.snaplen         = WTAP_MAX_PACKET_SIZE;
69   capture_opts->default_options.linktype        = -1;
70   capture_opts->default_options.promisc_mode    = TRUE;
71 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
72   capture_opts->default_options.buffer_size     = DEFAULT_CAPTURE_BUFFER_SIZE;
73 #endif
74   capture_opts->default_options.monitor_mode    = FALSE;
75 #ifdef HAVE_PCAP_REMOTE
76   capture_opts->default_options.src_type        = CAPTURE_IFLOCAL;
77   capture_opts->default_options.remote_host     = NULL;
78   capture_opts->default_options.remote_port     = NULL;
79   capture_opts->default_options.auth_type       = CAPTURE_AUTH_NULL;
80   capture_opts->default_options.auth_username   = NULL;
81   capture_opts->default_options.auth_password   = NULL;
82   capture_opts->default_options.datatx_udp      = FALSE;
83   capture_opts->default_options.nocap_rpcap     = TRUE;
84   capture_opts->default_options.nocap_local     = FALSE;
85 #endif
86 #ifdef HAVE_PCAP_SETSAMPLING
87   capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
88   capture_opts->default_options.sampling_param  = 0;
89 #endif
90   capture_opts->saving_to_file                  = FALSE;
91   capture_opts->save_file                       = NULL;
92   capture_opts->group_read_access               = FALSE;
93 #ifdef PCAP_NG_DEFAULT
94   capture_opts->use_pcapng                      = TRUE;             /* Save as pcap-ng by default */
95 #else
96   capture_opts->use_pcapng                      = FALSE;            /* Save as pcap by default */
97 #endif
98   capture_opts->real_time_mode                  = TRUE;
99   capture_opts->show_info                       = TRUE;
100   capture_opts->quit_after_cap                  = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
101   capture_opts->restart                         = FALSE;
102
103   capture_opts->multi_files_on                  = FALSE;
104   capture_opts->has_file_duration               = FALSE;
105   capture_opts->file_duration                   = 60;               /* 1 min */
106   capture_opts->has_ring_num_files              = FALSE;
107   capture_opts->ring_num_files                  = RINGBUFFER_MIN_NUM_FILES;
108
109   capture_opts->has_autostop_files              = FALSE;
110   capture_opts->autostop_files                  = 1;
111   capture_opts->has_autostop_packets            = FALSE;
112   capture_opts->autostop_packets                = 0;
113   capture_opts->has_autostop_filesize           = FALSE;
114   capture_opts->autostop_filesize               = 1024;             /* 1 MB */
115   capture_opts->has_autostop_duration           = FALSE;
116   capture_opts->autostop_duration               = 60;               /* 1 min */
117
118
119   capture_opts->fork_child                      = -1;               /* invalid process handle */
120 #ifdef _WIN32
121   capture_opts->signal_pipe_write_fd            = -1;
122 #endif
123   capture_opts->state                           = CAPTURE_STOPPED;
124   capture_opts->output_to_pipe                  = FALSE;
125 #ifndef _WIN32
126   capture_opts->owner                           = getuid();
127   capture_opts->group                           = getgid();
128 #endif
129   capture_opts->session_started                 = FALSE;
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, (int)(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 'g':        /* enable group read access on the capture file(s) */
696         capture_opts->group_read_access = TRUE;
697         break;
698     case 'H':        /* Hide capture info dialog box */
699         capture_opts->show_info = FALSE;
700         break;
701     case 'i':        /* Use interface x */
702         status = capture_opts_add_iface_opt(capture_opts, optarg_str_p);
703         if (status != 0) {
704             return status;
705         }
706         break;
707 #ifdef HAVE_PCAP_CREATE
708     case 'I':        /* Capture in monitor mode */
709         if (capture_opts->ifaces->len > 0) {
710             interface_options interface_opts;
711
712             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
713             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
714             interface_opts.monitor_mode = TRUE;
715             g_array_append_val(capture_opts->ifaces, interface_opts);
716         } else {
717             capture_opts->default_options.monitor_mode = TRUE;
718         }
719         break;
720 #endif
721     case 'k':        /* Start capture immediately */
722         *start_capture = TRUE;
723         break;
724     /*case 'l':*/    /* Automatic scrolling in live capture mode */
725 #ifdef HAVE_PCAP_SETSAMPLING
726     case 'm':
727         if (get_sampling_arguments(capture_opts, optarg_str_p) == FALSE) {
728             cmdarg_err("Invalid or unknown -m arg \"%s\"", optarg_str_p);
729             return 1;
730         }
731         break;
732 #endif
733     case 'n':        /* Use pcapng format */
734         capture_opts->use_pcapng = TRUE;
735         break;
736     case 'p':        /* Don't capture in promiscuous mode */
737         if (capture_opts->ifaces->len > 0) {
738             interface_options interface_opts;
739
740             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
741             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
742             interface_opts.promisc_mode = FALSE;
743             g_array_append_val(capture_opts->ifaces, interface_opts);
744         } else {
745             capture_opts->default_options.promisc_mode = FALSE;
746         }
747         break;
748     case 'P':        /* Use pcap format */
749         capture_opts->use_pcapng = FALSE;
750         break;
751 #ifdef HAVE_PCAP_REMOTE
752     case 'r':
753         if (capture_opts->ifaces->len > 0) {
754             interface_options interface_opts;
755
756             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
757             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
758             interface_opts.nocap_rpcap = FALSE;
759             g_array_append_val(capture_opts->ifaces, interface_opts);
760         } else {
761             capture_opts->default_options.nocap_rpcap = FALSE;
762         }
763         break;
764 #endif
765     case 's':        /* Set the snapshot (capture) length */
766         snaplen = get_natural_int(optarg_str_p, "snapshot length");
767         /*
768          * Make a snapshot length of 0 equivalent to the maximum packet
769          * length, mirroring what tcpdump does.
770          */
771         if (snaplen == 0)
772             snaplen = WTAP_MAX_PACKET_SIZE;
773         if (capture_opts->ifaces->len > 0) {
774             interface_options interface_opts;
775
776             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
777             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
778             interface_opts.has_snaplen = TRUE;
779             interface_opts.snaplen = snaplen;
780             g_array_append_val(capture_opts->ifaces, interface_opts);
781         } else {
782             capture_opts->default_options.snaplen = snaplen;
783             capture_opts->default_options.has_snaplen = TRUE;
784         }
785         break;
786     case 'S':        /* "Real-Time" mode: used for following file ala tail -f */
787         capture_opts->real_time_mode = TRUE;
788         break;
789 #ifdef HAVE_PCAP_REMOTE
790     case 'u':
791         if (capture_opts->ifaces->len > 0) {
792             interface_options interface_opts;
793
794             interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
795             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
796             interface_opts.datatx_udp = TRUE;
797             g_array_append_val(capture_opts->ifaces, interface_opts);
798         } else {
799             capture_opts->default_options.datatx_udp = TRUE;
800         }
801         break;
802 #endif
803     case 'w':        /* Write to capture file x */
804         capture_opts->saving_to_file = TRUE;
805         g_free(capture_opts->save_file);
806         capture_opts->save_file = g_strdup(optarg_str_p);
807         status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
808         return status;
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         printf("Data link types of interface %s when %sin monitor mode (use option -y to set):\n",
848                name, monitor_mode ? "" : "not ");
849     else
850         printf("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         printf("  %s", data_link_info->name);
855         if (data_link_info->description != NULL)
856             printf(" (%s)", data_link_info->description);
857         else
858             printf(" (not supported)");
859         printf("\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         printf("%d. %s", i++, if_info->name);
876
877         /* Print the interface friendly name, if it exists;
878           if not fall back to vendor description, if it exists. */
879         if (if_info->friendly_name != NULL){
880             printf(" (%s)", if_info->friendly_name);
881         } else {
882             if (if_info->vendor_description != NULL)
883                 printf(" (%s)", if_info->vendor_description);
884         }
885         printf("\n");
886     }
887 }
888
889
890 void
891 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min)
892 {
893     guint i;
894     interface_options interface_opts;
895
896     if (capture_opts->ifaces->len > 0) {
897         for (i = 0; i < capture_opts->ifaces->len; i++) {
898             interface_opts = g_array_index(capture_opts->ifaces, interface_options, 0);
899             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, 0);
900             if (interface_opts.snaplen < 1)
901                 interface_opts.snaplen = WTAP_MAX_PACKET_SIZE;
902             else if (interface_opts.snaplen < snaplen_min)
903                 interface_opts.snaplen = snaplen_min;
904             g_array_append_val(capture_opts->ifaces, interface_opts);
905         }
906     } else {
907         if (capture_opts->default_options.snaplen < 1)
908             capture_opts->default_options.snaplen = WTAP_MAX_PACKET_SIZE;
909         else if (capture_opts->default_options.snaplen < snaplen_min)
910             capture_opts->default_options.snaplen = snaplen_min;
911     }
912 }
913
914
915 void
916 capture_opts_trim_ring_num_files(capture_options *capture_opts)
917 {
918     /* Check the value range of the ring_num_files parameter */
919     if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES) {
920         cmdarg_err("Too many ring buffer files (%u). Reducing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MAX_NUM_FILES);
921         capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
922     } else if (capture_opts->ring_num_files > RINGBUFFER_WARN_NUM_FILES) {
923         cmdarg_err("%u is a lot of ring buffer files.\n", capture_opts->ring_num_files);
924     }
925 #if RINGBUFFER_MIN_NUM_FILES > 0
926     else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
927         cmdarg_err("Too few ring buffer files (%u). Increasing to %u.\n", capture_opts->ring_num_files, RINGBUFFER_MIN_NUM_FILES);
928         capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
929 #endif
930 }
931
932
933 int
934 capture_opts_trim_iface(capture_options *capture_opts, const char *capture_device)
935 {
936     int status;
937
938     /* Did the user specify an interface to use? */
939     if (capture_opts->num_selected != 0 || capture_opts->ifaces->len != 0) {
940         /* yes they did, return immediately - nothing further to do here */
941         return 0;
942     }
943
944     /* No - is a default specified in the preferences file? */
945     if (capture_device != NULL) {
946         /* Yes - use it. */
947         status = capture_opts_add_iface_opt(capture_opts, capture_device);
948         return status;
949     }
950     /* No default in preferences file, just pick the first interface from the list of interfaces. */
951     return capture_opts_add_iface_opt(capture_opts, "1");
952 }
953
954 #ifndef S_IFIFO
955 #define S_IFIFO _S_IFIFO
956 #endif
957 #ifndef S_ISFIFO
958 #define S_ISFIFO(mode)  (((mode) & S_IFMT) == S_IFIFO)
959 #endif
960
961 /* copied from filesystem.c */
962 static int
963 capture_opts_test_for_fifo(const char *path)
964 {
965   ws_statb64 statb;
966
967   if (ws_stat64(path, &statb) < 0)
968     return errno;
969
970   if (S_ISFIFO(statb.st_mode))
971     return ESPIPE;
972   else
973     return 0;
974 }
975
976 static gboolean
977 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
978 {
979   int err;
980
981   *is_pipe = FALSE;
982
983   if (save_file != NULL) {
984     /* We're writing to a capture file. */
985     if (strcmp(save_file, "-") == 0) {
986       /* Writing to stdout. */
987       /* XXX - should we check whether it's a pipe?  It's arguably
988          silly to do "-w - >output_file" rather than "-w output_file",
989          but by not checking we might be violating the Principle Of
990          Least Astonishment. */
991       *is_pipe = TRUE;
992     } else {
993       /* not writing to stdout, test for a FIFO (aka named pipe) */
994       err = capture_opts_test_for_fifo(save_file);
995       switch (err) {
996
997       case ENOENT:      /* it doesn't exist, so we'll be creating it,
998                            and it won't be a FIFO */
999       case 0:           /* found it, but it's not a FIFO */
1000         break;
1001
1002       case ESPIPE:      /* it is a FIFO */
1003         *is_pipe = TRUE;
1004         break;
1005
1006       default:          /* couldn't stat it              */
1007         break;          /* ignore: later attempt to open */
1008                         /*  will generate a nice msg     */
1009       }
1010     }
1011   }
1012
1013   return 0;
1014 }
1015
1016 /*
1017  * Add all non-hidden selected interfaces in the "all interfaces" list
1018  * to the list of interfaces for the capture.
1019  */
1020 void
1021 collect_ifaces(capture_options *capture_opts)
1022 {
1023   guint i;
1024   interface_t device;
1025   interface_options interface_opts;
1026
1027   /* Empty out the existing list of interfaces. */
1028   for (i = capture_opts->ifaces->len; i != 0; i--) {
1029     interface_opts = g_array_index(capture_opts->ifaces, interface_options, i - 1);
1030     g_free(interface_opts.name);
1031     g_free(interface_opts.descr);
1032     if (interface_opts.console_display_name != NULL)
1033         g_free(interface_opts.console_display_name);
1034     g_free(interface_opts.cfilter);
1035 #ifdef HAVE_PCAP_REMOTE
1036     if (interface_opts.src_type == CAPTURE_IFREMOTE) {
1037       g_free(interface_opts.remote_host);
1038       g_free(interface_opts.remote_port);
1039       g_free(interface_opts.auth_username);
1040       g_free(interface_opts.auth_password);
1041     }
1042 #endif
1043     capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i - 1);
1044   }
1045
1046   /* Now fill the list up again. */
1047   for (i = 0; i < capture_opts->all_ifaces->len; i++) {
1048     device = g_array_index(capture_opts->all_ifaces, interface_t, i);
1049     if (!device.hidden && device.selected) {
1050       interface_opts.name = g_strdup(device.name);
1051       interface_opts.descr = g_strdup(device.display_name);
1052       interface_opts.console_display_name = g_strdup(device.name);
1053       interface_opts.linktype = device.active_dlt;
1054       interface_opts.cfilter = g_strdup(device.cfilter);
1055       interface_opts.snaplen = device.snaplen;
1056       interface_opts.has_snaplen = device.has_snaplen;
1057       interface_opts.promisc_mode = device.pmode;
1058 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
1059       interface_opts.buffer_size =  device.buffer;
1060 #endif
1061 #ifdef HAVE_PCAP_CREATE
1062       interface_opts.monitor_mode = device.monitor_mode_enabled;
1063 #endif
1064 #ifdef HAVE_PCAP_REMOTE
1065       interface_opts.src_type = CAPTURE_IFREMOTE;
1066       interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
1067       interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
1068       interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
1069       interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
1070       interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
1071       interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
1072       interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
1073       interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
1074 #endif
1075 #ifdef HAVE_PCAP_SETSAMPLING
1076       interface_opts.sampling_method = device.remote_opts.sampling_method;
1077       interface_opts.sampling_param  = device.remote_opts.sampling_param;
1078 #endif
1079       g_array_append_val(capture_opts->ifaces, interface_opts);
1080     } else {
1081       continue;
1082     }
1083   }
1084 }
1085
1086
1087 #endif /* HAVE_LIBPCAP */