Drop privileges very early on.
[metze/wireshark/wip.git] / capture_opts.h
1 /* capture_opts.h
2  * Capture options (all parameters needed to do the actual capture)
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23
24 /** @file
25  *
26  *  Capture options (all parameters needed to do the actual capture)
27  *
28  */
29
30 #ifndef __CAPTURE_OPTS_H__
31 #define __CAPTURE_OPTS_H__
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>     /* for gid_t */
35 #endif
36
37 #include <capchild/capture_ifinfo.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif /* __cplusplus */
42
43 /*
44  * Long options.
45  * We do not currently have long options corresponding to all short
46  * options; we should probably pick appropriate option names for them.
47  *
48  * For long options with no corresponding short options, we define values
49  * outside the range of ASCII graphic characters, make that the last
50  * component of the entry for the long option, and have a case for that
51  * option in the switch statement.
52  *
53  * NOTE:
54  * for tshark, we're using a leading - in the optstring to prevent getopt()
55  * from permuting the argv[] entries, in this case, unknown argv[] entries
56  * will be returned as parameters to a dummy-option 1.
57  * In short: we must not use 1 here, which is another reason to use
58  * values outside the range of ASCII graphic characters.
59  */
60 #define LONGOPT_NUM_CAP_COMMENT 128
61
62 /*
63  * Non-capture long-only options should start here, to avoid collision
64  * with capture options.
65  */
66 #define MIN_NON_CAPTURE_LONGOPT 129
67
68 /*
69  * Options for capturing common to all capturing programs.
70  */
71 #ifdef HAVE_PCAP_REMOTE
72 #define OPTSTRING_A "A:"
73 #else
74 #define OPTSTRING_A ""
75 #endif
76
77 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
78 #define LONGOPT_BUFFER_SIZE \
79         {(char *)"buffer-size", required_argument, NULL, 'B'},
80 #define OPTSTRING_B "B:"
81 #else
82 #define LONGOPT_BUFFER_SIZE
83 #define OPTSTRING_B ""
84 #endif
85
86 #ifdef HAVE_PCAP_CREATE
87 #define LONGOPT_MONITOR_MODE {(char *)"monitor-mode", no_argument, NULL, 'I'},
88 #define OPTSTRING_I "I"
89 #else
90 #define LONGOPT_MONITOR_MODE
91 #define OPTSTRING_I ""
92 #endif
93
94 #define LONGOPT_CAPTURE_COMMON \
95         {(char *)"capture-comment", required_argument, NULL, LONGOPT_NUM_CAP_COMMENT}, \
96         {(char *)"autostop", required_argument, NULL, 'a'}, \
97         {(char *)"ring-buffer", required_argument, NULL, 'b'}, \
98         LONGOPT_BUFFER_SIZE \
99         {(char *)"list-interfaces", no_argument, NULL, 'D'}, \
100         {(char *)"interface", required_argument, NULL, 'i'}, \
101         LONGOPT_MONITOR_MODE \
102         {(char *)"list-data-link-types", no_argument, NULL, 'L'}, \
103         {(char *)"no-promiscuous-mode", no_argument, NULL, 'p'}, \
104         {(char *)"snapshot-length", required_argument, NULL, 's'}, \
105         {(char *)"linktype", required_argument, NULL, 'y'},
106
107 #define OPTSTRING_CAPTURE_COMMON \
108          "a:" OPTSTRING_A "b:" OPTSTRING_B "c:Df:i:" OPTSTRING_I "Lps:y:"
109
110 #ifdef HAVE_PCAP_REMOTE
111 /* Type of capture source */
112 typedef enum {
113     CAPTURE_IFLOCAL,        /**< Local network interface */
114     CAPTURE_IFREMOTE        /**< Remote network interface */
115 } capture_source;
116
117 /* Type of RPCAPD Authentication */
118 typedef enum {
119     CAPTURE_AUTH_NULL,      /**< No authentication */
120     CAPTURE_AUTH_PWD        /**< User/password authentication */
121 } capture_auth;
122 #endif
123 #ifdef HAVE_PCAP_SETSAMPLING
124 /**
125  * Method of packet sampling (dropping some captured packets),
126  * may require additional integer parameter, marked here as N
127  */
128 typedef enum {
129     CAPTURE_SAMP_NONE,      /**< No sampling - capture all packets */
130     CAPTURE_SAMP_BY_COUNT,  /**< Counter-based sampling -
131                                  capture 1 packet from every N */
132     CAPTURE_SAMP_BY_TIMER   /**< Timer-based sampling -
133                                  capture no more than 1 packet
134                                  in N milliseconds */
135 } capture_sampling;
136 #endif
137
138 #ifdef HAVE_PCAP_REMOTE
139 struct remote_host_info {
140     gchar *remote_host;          /**< Host name or network address for remote capturing */
141     gchar *remote_port;          /**< TCP port of remote RPCAP server */
142     gint auth_type;              /**< Authentication type */
143     gchar *auth_username;        /**< Remote authentication parameters */
144     gchar *auth_password;        /**< Remote authentication parameters */
145     gboolean datatx_udp;
146     gboolean nocap_rpcap;
147     gboolean nocap_local;
148 };
149
150 typedef struct remote_options_tag {
151     capture_source src_type;
152     struct remote_host_info remote_host_opts;
153 #ifdef HAVE_PCAP_SETSAMPLING
154     capture_sampling sampling_method;
155     int sampling_param;
156 #endif
157 } remote_options;
158 #endif /* HAVE_PCAP_REMOTE */
159
160 typedef struct interface_tag {
161     gchar *name;
162     gchar *display_name;
163     gchar *friendly_name;
164     guint type;
165     gchar *addresses;
166     gint no_addresses;
167     gchar *cfilter;
168     GList *links;
169     gint active_dlt;
170     gboolean pmode;
171     gboolean has_snaplen;
172     guint snaplen;
173     gboolean local;
174 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
175     gint buffer;
176 #endif
177 #ifdef HAVE_PCAP_CREATE
178     gboolean monitor_mode_enabled;
179     gboolean monitor_mode_supported;
180 #endif
181 #ifdef HAVE_PCAP_REMOTE
182     remote_options remote_opts;
183 #endif
184     guint32     last_packets;
185     guint32     packet_diff;
186     if_info_t   if_info;
187     gboolean    selected;
188     gboolean    hidden;
189     gboolean    locked;
190 } interface_t;
191
192 typedef struct link_row_tag {
193     gchar *name;
194     gint dlt;
195 } link_row;
196
197 typedef struct interface_options_tag {
198     gchar *name; /* the name of the interface provided to winpcap/libpcap to specify the interface */
199     gchar *descr;
200     gchar *console_display_name; /* the name displayed in the console, also the basis for autonamed pcap filenames */
201     gchar *cfilter;
202     gboolean has_snaplen;
203     int snaplen;
204     int linktype;
205     gboolean promisc_mode;
206     interface_type if_type;
207 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
208     int buffer_size;
209 #endif
210     gboolean monitor_mode;
211 #ifdef HAVE_PCAP_REMOTE
212     capture_source src_type;
213     gchar *remote_host;
214     gchar *remote_port;
215     capture_auth auth_type;
216     gchar *auth_username;
217     gchar *auth_password;
218     gboolean datatx_udp;
219     gboolean nocap_rpcap;
220     gboolean nocap_local;
221 #endif
222 #ifdef HAVE_PCAP_SETSAMPLING
223     capture_sampling sampling_method;
224     int sampling_param;
225 #endif
226 } interface_options;
227
228 /** Capture options coming from user interface */
229 typedef struct capture_options_tag {
230     /* general */
231     GArray   *ifaces;               /**< array of interfaces.
232                                          Currently only used by dumpcap. */
233     GArray   *all_ifaces;
234     guint    num_selected;
235
236     /*
237      * Options to be applied to all interfaces.
238      *
239      * Some of these can be set from the GUI, others can't; setting
240      * the link-layer header type, for example, doesn't necessarily
241      * make sense, as different interfaces may support different sets
242      * of link-layer header types.
243      *
244      * Some that can't be set from the GUI can be set from the command
245      * line, by specifying them before any interface is specified.
246      * This includes the link-layer header type, so if somebody asks
247      * for a link-layer header type that an interface on which they're
248      * capturing doesn't support, we should report an error and fail
249      * to capture.
250      *
251      * These can be overridden per-interface.
252      */
253     interface_options default_options;
254
255     gboolean saving_to_file;        /**< TRUE if capture is writing to a file */
256     gchar    *save_file;            /**< the capture file name */
257     gboolean group_read_access;     /**< TRUE is group read permission needs to be set */
258     gboolean use_pcapng;            /**< TRUE if file format is pcapng */
259
260     /* GUI related */
261     gboolean real_time_mode;        /**< Update list of packets in real time */
262     gboolean show_info;             /**< show the info dialog */
263     gboolean quit_after_cap;        /**< Makes a "capture only mode". Implies -k */
264     gboolean restart;               /**< restart after closing is done */
265     gchar    *orig_save_file;       /**< the original capture file name (saved for a restart) */
266
267     /* multiple files (and ringbuffer) */
268     gboolean multi_files_on;        /**< TRUE if ring buffer in use */
269
270     gboolean has_file_duration;     /**< TRUE if ring duration specified */
271     gint32 file_duration;           /**< Switch file after n seconds */
272     gboolean has_ring_num_files;    /**< TRUE if ring num_files specified */
273     guint32 ring_num_files;         /**< Number of multiple buffer files */
274
275     /* autostop conditions */
276     gboolean has_autostop_files;    /**< TRUE if maximum number of capture files
277                                          are specified */
278     gint32 autostop_files;          /**< Maximum number of capture files */
279
280     gboolean has_autostop_packets;  /**< TRUE if maximum packet count is
281                                          specified */
282     int autostop_packets;           /**< Maximum packet count */
283     gboolean has_autostop_filesize; /**< TRUE if maximum capture file size
284                                          is specified */
285     guint32 autostop_filesize;      /**< Maximum capture file size */
286     gboolean has_autostop_duration; /**< TRUE if maximum capture duration
287                                          is specified */
288     gint32 autostop_duration;       /**< Maximum capture duration */
289
290     gchar *capture_comment;         /** capture comment to write to the
291                                         output file */
292
293     /* internally used (don't touch from outside) */
294     gboolean output_to_pipe;        /**< save_file is a pipe (named or stdout) */
295     gboolean capture_child;         /**< hidden option: Wireshark child mode */
296 } capture_options;
297
298 /* initialize the capture_options with some reasonable values */
299 extern void
300 capture_opts_init(capture_options *capture_opts);
301
302 /* set a command line option value */
303 extern int
304 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture);
305
306 /* log content of capture_opts */
307 extern void
308 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts);
309
310 /* print interface capabilities, including link layer types */
311 extern void
312 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name,
313                                    gboolean monitor_mode);
314
315 /* print list of interfaces */
316 extern void
317 capture_opts_print_interfaces(GList *if_list);
318
319 /* trim the snaplen entry */
320 extern void
321 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min);
322
323 /* trim the ring_num_files entry */
324 extern void
325 capture_opts_trim_ring_num_files(capture_options *capture_opts);
326
327 /* pick default interface if none was specified */
328 extern int
329 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
330                                         const char *capture_device);
331
332 extern void
333 collect_ifaces(capture_options *capture_opts);
334
335 /* Default capture buffer size in Mbytes. */
336 #define DEFAULT_CAPTURE_BUFFER_SIZE 2
337
338 #ifdef __cplusplus
339 }
340 #endif /* __cplusplus */
341
342 #endif /* capture_opts.h */