epan: use SPDX indentifiers.
[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  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11
12 /** @file
13  *
14  *  Capture options (all parameters needed to do the actual capture)
15  *
16  */
17
18 #ifndef __CAPTURE_OPTS_H__
19 #define __CAPTURE_OPTS_H__
20
21 #ifdef HAVE_SYS_TYPES_H
22 # include <sys/types.h>     /* for gid_t */
23 #endif
24
25 #include <caputils/capture_ifinfo.h>
26
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34
35 /*
36  * Long options.
37  * We do not currently have long options corresponding to all short
38  * options; we should probably pick appropriate option names for them.
39  *
40  * For long options with no corresponding short options, we define values
41  * outside the range of ASCII graphic characters, make that the last
42  * component of the entry for the long option, and have a case for that
43  * option in the switch statement.
44  *
45  * We also pick values < 4096, so as to leave values >= 4096 for
46  * other long options.
47  *
48  * NOTE:
49  * for tshark, we're using a leading - in the optstring to prevent getopt()
50  * from permuting the argv[] entries, in this case, unknown argv[] entries
51  * will be returned as parameters to a dummy-option 1.
52  * In short: we must not use 1 here, which is another reason to use
53  * values outside the range of ASCII graphic characters.
54  */
55 #define LONGOPT_NUM_CAP_COMMENT   128
56 #define LONGOPT_LIST_TSTAMP_TYPES 129
57 #define LONGOPT_SET_TSTAMP_TYPE   130
58
59 /*
60  * Options for capturing common to all capturing programs.
61  */
62 #ifdef HAVE_PCAP_REMOTE
63 #define OPTSTRING_A "A:"
64 #else
65 #define OPTSTRING_A ""
66 #endif
67
68 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
69 #define LONGOPT_BUFFER_SIZE \
70     {"buffer-size", required_argument, NULL, 'B'},
71 #define OPTSTRING_B "B:"
72 #else
73 #define LONGOPT_BUFFER_SIZE
74 #define OPTSTRING_B ""
75 #endif
76
77 #ifdef HAVE_PCAP_CREATE
78 #define LONGOPT_MONITOR_MODE {"monitor-mode", no_argument, NULL, 'I'},
79 #define OPTSTRING_I "I"
80 #else
81 #define LONGOPT_MONITOR_MODE
82 #define OPTSTRING_I ""
83 #endif
84
85 #define LONGOPT_CAPTURE_COMMON \
86     {"capture-comment",       required_argument, NULL, LONGOPT_NUM_CAP_COMMENT}, \
87     {"autostop",              required_argument, NULL, 'a'}, \
88     {"ring-buffer",           required_argument, NULL, 'b'}, \
89     LONGOPT_BUFFER_SIZE \
90     {"list-interfaces",       no_argument,       NULL, 'D'}, \
91     {"interface",             required_argument, NULL, 'i'}, \
92     LONGOPT_MONITOR_MODE \
93     {"list-data-link-types",  no_argument,       NULL, 'L'}, \
94     {"no-promiscuous-mode",   no_argument,       NULL, 'p'}, \
95     {"snapshot-length",       required_argument, NULL, 's'}, \
96     {"linktype",              required_argument, NULL, 'y'}, \
97     {"list-time-stamp-types", no_argument,       NULL, LONGOPT_LIST_TSTAMP_TYPES}, \
98     {"time-stamp-type",       required_argument, NULL, LONGOPT_SET_TSTAMP_TYPE},
99
100
101 #define OPTSTRING_CAPTURE_COMMON \
102     "a:" OPTSTRING_A "b:" OPTSTRING_B "c:Df:i:" OPTSTRING_I "Lps:y:"
103
104 #ifdef HAVE_PCAP_REMOTE
105 /* Type of capture source */
106 typedef enum {
107     CAPTURE_IFLOCAL,        /**< Local network interface */
108     CAPTURE_IFREMOTE        /**< Remote network interface */
109 } capture_source;
110
111 /* Type of RPCAPD Authentication */
112 typedef enum {
113     CAPTURE_AUTH_NULL,      /**< No authentication */
114     CAPTURE_AUTH_PWD        /**< User/password authentication */
115 } capture_auth;
116 #endif
117 #ifdef HAVE_PCAP_SETSAMPLING
118 /**
119  * Method of packet sampling (dropping some captured packets),
120  * may require additional integer parameter, marked here as N
121  */
122 typedef enum {
123     CAPTURE_SAMP_NONE,      /**< No sampling - capture all packets */
124     CAPTURE_SAMP_BY_COUNT,  /**< Counter-based sampling -
125                                  capture 1 packet from every N */
126     CAPTURE_SAMP_BY_TIMER   /**< Timer-based sampling -
127                                  capture no more than 1 packet
128                                  in N milliseconds */
129 } capture_sampling;
130 #endif
131
132 #ifdef HAVE_PCAP_REMOTE
133 struct remote_host_info {
134     gchar        *remote_host;      /**< Host name or network address for remote capturing */
135     gchar        *remote_port;      /**< TCP port of remote RPCAP server */
136     capture_auth  auth_type;        /**< Authentication type */
137     gchar        *auth_username;    /**< Remote authentication parameters */
138     gchar        *auth_password;    /**< Remote authentication parameters */
139     gboolean      datatx_udp;
140     gboolean      nocap_rpcap;
141     gboolean      nocap_local;
142 };
143
144 struct remote_host {
145     gchar        *r_host;           /**< Host name or network address for remote capturing */
146     gchar        *remote_port;      /**< TCP port of remote RPCAP server */
147     capture_auth  auth_type;        /**< Authentication type */
148     gchar        *auth_username;    /**< Remote authentication parameters */
149     gchar        *auth_password;    /**< Remote authentication parameters */
150 };
151
152 typedef struct remote_options_tag {
153     capture_source src_type;
154     struct remote_host_info remote_host_opts;
155 #ifdef HAVE_PCAP_SETSAMPLING
156     capture_sampling sampling_method;
157     int sampling_param;
158 #endif
159 } remote_options;
160 #endif /* HAVE_PCAP_REMOTE */
161
162 typedef struct interface_tag {
163     gchar          *name;
164     gchar          *display_name;
165     gchar          *friendly_name;
166     guint           type;
167     gchar          *addresses;
168     gint            no_addresses;
169     gchar          *cfilter;
170     GList          *links;
171     gint            active_dlt;
172     gboolean        pmode;
173     gboolean        has_snaplen;
174     int             snaplen;
175     gboolean        local;
176 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
177     gint            buffer;
178 #endif
179 #ifdef HAVE_PCAP_CREATE
180     gboolean        monitor_mode_enabled;
181     gboolean        monitor_mode_supported;
182 #endif
183 #ifdef HAVE_PCAP_REMOTE
184     remote_options  remote_opts;
185 #endif
186     guint32         last_packets;
187     guint32         packet_diff;
188     if_info_t       if_info;
189     gboolean        selected;
190     gboolean        hidden;
191     /* External capture cached data */
192     GHashTable     *external_cap_args_settings;
193     gchar          *timestamp_type;
194 } interface_t;
195
196 typedef struct link_row_tag {
197     gchar *name;
198     gint dlt;
199 } link_row;
200
201 #ifdef _WIN32
202 #define INVALID_EXTCAP_PID INVALID_HANDLE_VALUE
203 #else
204 #define INVALID_EXTCAP_PID (GPid)-1
205 #endif
206
207 typedef struct interface_options_tag {
208     gchar            *name;                 /* the name of the interface provided to winpcap/libpcap to specify the interface */
209     gchar            *descr;
210     gchar            *console_display_name; /* the name displayed in the console, also the basis for autonamed pcap filenames */
211     gchar            *cfilter;
212     gboolean          has_snaplen;
213     int               snaplen;
214     int               linktype;
215     gboolean          promisc_mode;
216     interface_type    if_type;
217     gchar            *extcap;
218     gchar            *extcap_fifo;
219     GHashTable       *extcap_args;
220     GPid              extcap_pid;           /* pid of running process or INVALID_EXTCAP_PID */
221     gpointer          extcap_userdata;
222     guint             extcap_child_watch;
223 #ifdef _WIN32
224     HANDLE            extcap_pipe_h;
225     HANDLE            extcap_control_in_h;
226     HANDLE            extcap_control_out_h;
227 #endif
228     gchar            *extcap_control_in;
229     gchar            *extcap_control_out;
230 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
231     int               buffer_size;
232 #endif
233     gboolean          monitor_mode;
234 #ifdef HAVE_PCAP_REMOTE
235     capture_source    src_type;
236     gchar            *remote_host;
237     gchar            *remote_port;
238     capture_auth      auth_type;
239     gchar            *auth_username;
240     gchar            *auth_password;
241     gboolean          datatx_udp;
242     gboolean          nocap_rpcap;
243     gboolean          nocap_local;
244 #endif
245 #ifdef HAVE_PCAP_SETSAMPLING
246     capture_sampling  sampling_method;
247     int               sampling_param;
248 #endif
249     gchar            *timestamp_type;       /* requested timestamp as string */
250     int               timestamp_type_id;    /* Timestamp type to pass to pcap_set_tstamp_type.
251                                                only valid if timestamp_type != NULL */
252 } interface_options;
253
254 /** Capture options coming from user interface */
255 typedef struct capture_options_tag {
256     /* general */
257     GArray            *ifaces;                /**< the interfaces to use for the
258                                                    next capture, entries are of
259                                                    type interface_options */
260     GArray            *all_ifaces;            /**< all interfaces, entries are
261                                                    of type interface_t */
262     int                ifaces_err;            /**< if all_ifaces is null, the error
263                                                    when it was fetched, if any */
264     gchar             *ifaces_err_info;       /**< error string for that error */
265     guint              num_selected;
266
267     /*
268      * Options to be applied to all interfaces.
269      *
270      * Some of these can be set from the GUI, others can't; setting
271      * the link-layer header type, for example, doesn't necessarily
272      * make sense, as different interfaces may support different sets
273      * of link-layer header types.
274      *
275      * Some that can't be set from the GUI can be set from the command
276      * line, by specifying them before any interface is specified.
277      * This includes the link-layer header type, so if somebody asks
278      * for a link-layer header type that an interface on which they're
279      * capturing doesn't support, we should report an error and fail
280      * to capture.
281      *
282      * These can be overridden per-interface.
283      */
284     interface_options  default_options;
285
286     gboolean           saving_to_file;        /**< TRUE if capture is writing to a file */
287     gchar             *save_file;             /**< the capture file name */
288     gboolean           group_read_access;     /**< TRUE is group read permission needs to be set */
289     gboolean           use_pcapng;            /**< TRUE if file format is pcapng */
290
291     /* GUI related */
292     gboolean           real_time_mode;        /**< Update list of packets in real time */
293     gboolean           show_info;             /**< show the info dialog. GTK+ only. */
294     gboolean           restart;               /**< restart after closing is done */
295     gchar             *orig_save_file;        /**< the original capture file name (saved for a restart) */
296
297     /* multiple files (and ringbuffer) */
298     gboolean           multi_files_on;        /**< TRUE if ring buffer in use */
299
300     gboolean           has_file_duration;     /**< TRUE if ring duration specified */
301     gint32             file_duration;         /**< Switch file after n seconds */
302     gboolean           has_file_interval;     /**< TRUE if ring interval specified */
303     gint32             file_interval;         /**< Create time intervals of n seconds */
304     gboolean           has_ring_num_files;    /**< TRUE if ring num_files specified */
305     guint32            ring_num_files;        /**< Number of multiple buffer files */
306
307     /* autostop conditions */
308     gboolean           has_autostop_files;    /**< TRUE if maximum number of capture files
309                                                    are specified */
310     gint32             autostop_files;        /**< Maximum number of capture files */
311
312     gboolean           has_autostop_packets;  /**< TRUE if maximum packet count is
313                                                    specified */
314     int                autostop_packets;      /**< Maximum packet count */
315     gboolean           has_autostop_filesize; /**< TRUE if maximum capture file size
316                                                    is specified */
317     guint32            autostop_filesize;     /**< Maximum capture file size */
318     gboolean           has_autostop_duration; /**< TRUE if maximum capture duration
319                                                    is specified */
320     gint32             autostop_duration;     /**< Maximum capture duration */
321
322     gchar             *capture_comment;       /** capture comment to write to the
323                                                   output file */
324
325     /* internally used (don't touch from outside) */
326     gboolean           output_to_pipe;        /**< save_file is a pipe (named or stdout) */
327     gboolean           capture_child;         /**< hidden option: Wireshark child mode */
328 } capture_options;
329
330 /* initialize the capture_options with some reasonable values */
331 extern void
332 capture_opts_init(capture_options *capture_opts);
333
334 /* clean internal structures */
335 extern void
336 capture_opts_cleanup(capture_options *capture_opts);
337
338 /* set a command line option value */
339 extern int
340 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture);
341
342 /* log content of capture_opts */
343 extern void
344 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts);
345
346 enum caps_query {
347     CAPS_MONITOR_MODE          = 0x1,
348     CAPS_QUERY_LINK_TYPES      = 0x2,
349     CAPS_QUERY_TIMESTAMP_TYPES = 0x4
350 };
351
352 /* print interface capabilities, including link layer types */
353 extern void
354 capture_opts_print_if_capabilities(if_capabilities_t *caps, char *name, int queries);
355
356 /* print list of interfaces */
357 extern void
358 capture_opts_print_interfaces(GList *if_list);
359
360 /* trim the snaplen entry */
361 extern void
362 capture_opts_trim_snaplen(capture_options *capture_opts, int snaplen_min);
363
364 /* trim the ring_num_files entry */
365 extern void
366 capture_opts_trim_ring_num_files(capture_options *capture_opts);
367
368 /* pick default interface if none was specified */
369 extern int
370 capture_opts_default_iface_if_necessary(capture_options *capture_opts,
371                                         const char *capture_device);
372
373 extern void
374 capture_opts_del_iface(capture_options *capture_opts, guint if_index);
375
376 extern void
377 collect_ifaces(capture_options *capture_opts);
378
379 extern void
380 capture_opts_free_interface_t(interface_t *device);
381
382 /* Default capture buffer size in Mbytes. */
383 #define DEFAULT_CAPTURE_BUFFER_SIZE 2
384
385 #ifdef __cplusplus
386 }
387 #endif /* __cplusplus */
388
389 #endif /* capture_opts.h */
390
391 /*
392  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
393  *
394  * Local variables:
395  * c-basic-offset: 4
396  * tab-width: 8
397  * indent-tabs-mode: nil
398  * End:
399  *
400  * vi: set shiftwidth=4 tabstop=8 expandtab:
401  * :indentSize=4:tabSize=8:noTabs=true:
402  */