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