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