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