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