replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / wiretap / wtap_opttypes.h
1 /* wtap_opttypes.h
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 2001 Gerald Combs
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #ifndef WTAP_OPT_TYPES_H
11 #define WTAP_OPT_TYPES_H
12
13 #include "ws_symbol_export.h"
14
15 #include <wsutil/inet_ipv6.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif /* __cplusplus */
20
21 /*
22  * We use the pcapng option codes for option type values.
23  */
24
25 /* Options for all blocks */
26 #define OPT_EOFOPT           0     /**< Appears in pcapng files, but not in blocks. */
27 #define OPT_COMMENT          1     /**< A UTF-8 string containing a human-readable comment. */
28
29 /* Section Header block (SHB) */
30 #define OPT_SHB_HARDWARE     2     /**< A UTF-8 string containing the description of the
31                                      *     hardware used to create this section.
32                                      */
33 #define OPT_SHB_OS           3     /**< A UTF-8 string containing the
34                                      *     name of the operating system used to create this section.
35                                      */
36 #define OPT_SHB_USERAPPL     4     /**< A UTF-8 string containing the
37                                      *     name of the application used to create this section.
38                                      */
39
40 /* Interface Description block (IDB) */
41 #define OPT_IDB_NAME         2     /**< A UTF-8 string containing the name
42                                      *     of the device used to capture data.
43                                      *     "eth0" / "\Device\NPF_{AD1CE675-96D0-47C5-ADD0-2504B9126B68}"
44                                      */
45 #define OPT_IDB_DESCR        3     /**< A UTF-8 string containing the description
46                                      *     of the device used to capture data.
47                                      *     "Broadcom NetXtreme" / "First Ethernet Interface"
48                                      */
49 #define OPT_IDB_IP4ADDR      4     /**< XXX: if_IPv4addr Interface network address and netmask.
50                                      *     This option can be repeated multiple times within the same Interface Description Block
51                                      *     when multiple IPv4 addresses are assigned to the interface.
52                                      *     192 168 1 1 255 255 255 0
53                                      */
54 #define OPT_IDB_IP6ADDR      5     /* XXX: if_IPv6addr Interface network address and prefix length (stored in the last byte).
55                                      *     This option can be repeated multiple times within the same Interface
56                                      *     Description Block when multiple IPv6 addresses are assigned to the interface.
57                                      *     2001:0db8:85a3:08d3:1319:8a2e:0370:7344/64 is written (in hex) as
58                                      *     "20 01 0d b8 85 a3 08 d3 13 19 8a 2e 03 70 73 44 40"*/
59 #define OPT_IDB_MACADDR      6     /* XXX: if_MACaddr  Interface Hardware MAC address (48 bits).                             */
60 #define OPT_IDB_EUIADDR      7     /* XXX: if_EUIaddr  Interface Hardware EUI address (64 bits)                              */
61 #define OPT_IDB_SPEED        8     /**< Interface speed (in bps). 100000000 for 100Mbps
62                                      */
63 #define OPT_IDB_TSRESOL      9     /**< Resolution of timestamps. If the Most Significant Bit is equal to zero,
64                                      *     the remaining bits indicates the resolution of the timestamp as as a
65                                      *     negative power of 10 (e.g. 6 means microsecond resolution, timestamps
66                                      *     are the number of microseconds since 1/1/1970). If the Most Significant Bit
67                                      *     is equal to one, the remaining bits indicates the resolution has a
68                                      *     negative power of 2 (e.g. 10 means 1/1024 of second).
69                                      *     If this option is not present, a resolution of 10^-6 is assumed
70                                      *     (i.e. timestamps have the same resolution of the standard 'libpcap' timestamps).
71                                      */
72 #define OPT_IDB_TZONE        10    /* XXX: if_tzone    Time zone for GMT support (TODO: specify better). */
73 #define OPT_IDB_FILTER       11    /**< The filter (e.g. "capture only TCP traffic") used to capture traffic.
74                                      *     The first byte of the Option Data keeps a code of the filter used
75                                      *     (e.g. if this is a libpcap string, or BPF bytecode, and more).
76                                      *     More details about this format will be presented in Appendix XXX (TODO).
77                                      *     (TODO: better use different options for different fields?
78                                      *     e.g. if_filter_pcap, if_filter_bpf, ...) 00 "tcp port 23 and host 10.0.0.5"
79                                      */
80 #define OPT_IDB_OS           12    /**< A UTF-8 string containing the name of the operating system of the
81                                      *     machine in which this interface is installed.
82                                      *     This can be different from the same information that can be
83                                      *     contained by the Section Header Block
84                                      *     (Section 3.1 (Section Header Block (mandatory))) because
85                                      *     the capture can have been done on a remote machine.
86                                      *     "Windows XP SP2" / "openSUSE 10.2"
87                                      */
88 #define OPT_IDB_FCSLEN       13    /**< An integer value that specified the length of the
89                                      *     Frame Check Sequence (in bits) for this interface.
90                                      *     For link layers whose FCS length can change during time,
91                                      *     the Packet Block Flags Word can be used (see Appendix A (Packet Block Flags Word))
92                                      */
93 #define OPT_IDB_TSOFFSET     14    /**< XXX: A 64 bits integer value that specifies an offset (in seconds)
94                                      *     that must be added to the timestamp of each packet to obtain
95                                      *     the absolute timestamp of a packet. If the option is missing,
96                                      *     the timestamps stored in the packet must be considered absolute
97                                      *     timestamps. The time zone of the offset can be specified with the
98                                      *     option if_tzone. TODO: won't a if_tsoffset_low for fractional
99                                      *     second offsets be useful for highly syncronized capture systems?
100                                      */
101
102 #define OPT_NS_DNSNAME       2
103 #define OPT_NS_DNSIP4ADDR    3
104 #define OPT_NS_DNSIP6ADDR    4
105
106 #define OPT_ISB_STARTTIME    2
107 #define OPT_ISB_ENDTIME      3
108 #define OPT_ISB_IFRECV       4
109 #define OPT_ISB_IFDROP       5
110 #define OPT_ISB_FILTERACCEPT 6
111 #define OPT_ISB_OSDROP       7
112 #define OPT_ISB_USRDELIV     8
113
114 struct wtap_block;
115 typedef struct wtap_block *wtap_block_t;
116
117 /*
118  * Currently supported blocks; these are not the pcapng block type values
119  * for them, they're identifiers used internally.
120  */
121 typedef enum {
122     WTAP_BLOCK_NG_SECTION = 0,
123     WTAP_BLOCK_IF_DESCR,
124     WTAP_BLOCK_NG_NRB,
125     WTAP_BLOCK_IF_STATS,
126     WTAP_BLOCK_END_OF_LIST
127 } wtap_block_type_t;
128
129 /* Currently supported option types */
130 typedef enum {
131     WTAP_OPTTYPE_UINT8,
132     WTAP_OPTTYPE_UINT64,
133     WTAP_OPTTYPE_STRING,
134     WTAP_OPTTYPE_IPv4,
135     WTAP_OPTTYPE_IPv6,
136     WTAP_OPTTYPE_CUSTOM
137 } wtap_opttype_e;
138
139 typedef enum {
140     WTAP_OPTTYPE_SUCCESS = 0,
141     WTAP_OPTTYPE_NO_SUCH_OPTION = -1,
142     WTAP_OPTTYPE_NOT_FOUND = -2,
143     WTAP_OPTTYPE_TYPE_MISMATCH = -3,
144     WTAP_OPTTYPE_NUMBER_MISMATCH = -4,
145     WTAP_OPTTYPE_ALREADY_EXISTS = -5
146 } wtap_opttype_return_val;
147
148 struct wtap_opttype_custom
149 {
150     void* data;
151     guint size;
152 };
153
154 /*
155  * Structure describing a value of an option.
156  */
157 typedef union {
158     guint8 uint8val;
159     guint64 uint64val;
160     guint32 ipv4val;    /* network byte order */
161     ws_in6_addr ipv6val;
162     char *stringval;
163     struct wtap_opttype_custom customval;
164 } wtap_optval_t;
165
166 /*
167  * Structure describing an option in a block.
168  */
169 typedef struct {
170     guint option_id;     /**< option code for the option */
171     wtap_optval_t value; /**< value */
172 } wtap_option_t;
173
174 struct wtap_dumper;
175
176 typedef void (*wtap_block_create_func)(wtap_block_t block);
177 typedef void (*wtap_mand_free_func)(wtap_block_t block);
178 typedef void (*wtap_mand_copy_func)(wtap_block_t dest_block, wtap_block_t src_block);
179
180 /** Initialize block types.
181  *
182  * This is currently just a placeholder as nothing needs to be
183  * initialized yet.  Should handle "registration" when code is
184  * refactored to do so.
185  */
186 WS_DLL_PUBLIC void wtap_opttypes_initialize(void);
187
188 /** Create a block by type
189  *
190  * Return a newly allocated block with default options provided
191  *
192  * @param[in] block_type Block type to be created
193  * @return Newly allocated block
194  */
195 WS_DLL_PUBLIC wtap_block_t wtap_block_create(wtap_block_type_t block_type);
196
197 /** Free a block
198  *
199  * Needs to be called to clean up any allocated block
200  *
201  * @param[in] block Block to be freed
202  */
203 WS_DLL_PUBLIC void wtap_block_free(wtap_block_t block);
204
205 /** Free an array of blocks
206  *
207  * Needs to be called to clean up blocks allocated
208  * through GArray (for multiple blocks of same type)
209  * Includes freeing the GArray
210  *
211  * @param[in] block_array Array of blocks to be freed
212  */
213 WS_DLL_PUBLIC void wtap_block_array_free(GArray* block_array);
214
215 /** Provide mandatory data of a block
216  *
217  * @param[in] block Block from which to retrieve mandatory data
218  * @return Block mandatory data.  Structure varies based on block type
219  */
220 WS_DLL_PUBLIC void* wtap_block_get_mandatory_data(wtap_block_t block);
221
222 /** Add UINT8 option value to a block
223  *
224  * @param[in] block Block to which to add the option
225  * @param[in] option_id Identifier value for option
226  * @param[in] value Value of option
227  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
228  * error code otherwise
229  */
230 WS_DLL_PUBLIC wtap_opttype_return_val
231 wtap_block_add_uint8_option(wtap_block_t block, guint option_id, guint8 value);
232
233 /** Set UINT8 option value in a block
234  *
235  * @param[in] block Block in which to set the option value
236  * @param[in] option_id Identifier value for option
237  * @param[in] value New value of option
238  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
239  * error code otherwise
240  */
241 WS_DLL_PUBLIC wtap_opttype_return_val
242 wtap_block_set_uint8_option_value(wtap_block_t block, guint option_id, guint8 value);
243
244 /** Get UINT8 option value from a block
245  *
246  * @param[in] block Block from which to get the option value
247  * @param[in] option_id Identifier value for option
248  * @param[out] value Returned value of option
249  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
250  * error code otherwise
251  */
252 WS_DLL_PUBLIC wtap_opttype_return_val
253 wtap_block_get_uint8_option_value(wtap_block_t block, guint option_id, guint8* value) G_GNUC_WARN_UNUSED_RESULT;
254
255 /** Add UINT64 option value to a block
256  *
257  * @param[in] block Block to which to add the option
258  * @param[in] option_id Identifier value for option
259  * @param[in] value Value of option
260  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
261  * error code otherwise
262  */
263 WS_DLL_PUBLIC wtap_opttype_return_val
264 wtap_block_add_uint64_option(wtap_block_t block, guint option_id, guint64 value);
265
266 /** Set UINT64 option value in a block
267  *
268  * @param[in] block Block in which to set the option value
269  * @param[in] option_id Identifier value for option
270  * @param[in] value New value of option
271  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
272  * error code otherwise
273  */
274 WS_DLL_PUBLIC wtap_opttype_return_val
275 wtap_block_set_uint64_option_value(wtap_block_t block, guint option_id, guint64 value);
276
277 /** Get UINT64 option value from a block
278  *
279  * @param[in] block Block from which to get the option value
280  * @param[in] option_id Identifier value for option
281  * @param[out] value Returned value of option
282  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
283  * error code otherwise
284  */
285 WS_DLL_PUBLIC wtap_opttype_return_val
286 wtap_block_get_uint64_option_value(wtap_block_t block, guint option_id, guint64* value) G_GNUC_WARN_UNUSED_RESULT;
287
288 /** Add IPv4 address option value to a block
289  *
290  * @param[in] block Block to which to add the option
291  * @param[in] option_id Identifier value for option
292  * @param[in] value Value of option
293  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
294  * error code otherwise
295  */
296 WS_DLL_PUBLIC wtap_opttype_return_val
297 wtap_block_add_ipv4_option(wtap_block_t block, guint option_id, guint32 value);
298
299 /** Set IPv4 option value in a block
300  *
301  * @param[in] block Block in which to set the option value
302  * @param[in] option_id Identifier value for option
303  * @param[in] value New value of option
304  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
305  * error code otherwise
306  */
307 WS_DLL_PUBLIC wtap_opttype_return_val
308 wtap_block_set_ipv4_option_value(wtap_block_t block, guint option_id, guint32 value);
309
310 /** Get IPv4 option value from a block
311  *
312  * @param[in] block Block from which to get the option value
313  * @param[in] option_id Identifier value for option
314  * @param[out] value Returned value of option
315  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
316  * error code otherwise
317  */
318 WS_DLL_PUBLIC wtap_opttype_return_val
319 wtap_block_get_ipv4_option_value(wtap_block_t block, guint option_id, guint32* value) G_GNUC_WARN_UNUSED_RESULT;
320
321 /** Add IPv6 address option value to a block
322  *
323  * @param[in] block Block to which to add the option
324  * @param[in] option_id Identifier value for option
325  * @param[in] value Value of option
326  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
327  * error code otherwise
328  */
329 WS_DLL_PUBLIC wtap_opttype_return_val
330 wtap_block_add_ipv6_option(wtap_block_t block, guint option_id, ws_in6_addr *value);
331
332 /** Set IPv6 option value in a block
333  *
334  * @param[in] block Block in which to set the option value
335  * @param[in] option_id Identifier value for option
336  * @param[in] value New value of option
337  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
338  * error code otherwise
339  */
340 WS_DLL_PUBLIC wtap_opttype_return_val
341 wtap_block_set_ipv6_option_value(wtap_block_t block, guint option_id, ws_in6_addr *value);
342
343 /** Get IPv6 option value from a block
344  *
345  * @param[in] block Block from which to get the option value
346  * @param[in] option_id Identifier value for option
347  * @param[out] value Returned value of option
348  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
349  * error code otherwise
350  */
351 WS_DLL_PUBLIC wtap_opttype_return_val
352 wtap_block_get_ipv6_option_value(wtap_block_t block, guint option_id, ws_in6_addr* value) G_GNUC_WARN_UNUSED_RESULT;
353
354 /** Add a string option to a block
355  *
356  * @param[in] block Block to which to add the option
357  * @param[in] option_id Identifier value for option
358  * @param[in] value Value of option
359  * @param[in] value_length Maximum length of string to copy.
360  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
361  * error code otherwise
362  */
363 WS_DLL_PUBLIC wtap_opttype_return_val
364 wtap_block_add_string_option(wtap_block_t block, guint option_id, const char *value, gsize value_length);
365
366 /** Add a string option to a block witha printf-formatted string as its value
367  *
368  * @param[in] block Block to which to add the option
369  * @param[in] option_id Identifier value for option
370  * @param[in] format printf-like format string
371  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
372  * error code otherwise
373  */
374 WS_DLL_PUBLIC wtap_opttype_return_val
375 wtap_block_add_string_option_format(wtap_block_t block, guint option_id, const char *format, ...)
376                                     G_GNUC_PRINTF(3,4);
377
378 /** Set string option value in a block
379  *
380  * @param[in] block Block in which to set the option value
381  * @param[in] option_id Identifier value for option
382  * @param[in] value New value of option
383  * @param[in] value_length Maximum length of string to copy.
384  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
385  * error code otherwise
386  */
387 WS_DLL_PUBLIC wtap_opttype_return_val
388 wtap_block_set_string_option_value(wtap_block_t block, guint option_id, const char* value, gsize value_length);
389
390 /** Set string option value for nth instance of a particular option in a block
391  *
392  * @param[in] block Block in which to set the option value
393  * @param[in] option_id Identifier value for option
394  * @param[in] idx Instance number of option with that ID
395  * @param[in] value New value of option
396  * @param[in] value_length Maximum length of string to copy.
397  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
398  * error code otherwise
399  */
400 WS_DLL_PUBLIC wtap_opttype_return_val
401 wtap_block_set_nth_string_option_value(wtap_block_t block, guint option_id, guint idx, const char* value, gsize value_length);
402
403 /** Set string option value in a block to a printf-formatted string
404  *
405  * @param[in] block Block in which to set the option value
406  * @param[in] option_id Identifier value for option
407  * @param[in] format printf-like format string
408  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
409  * error code otherwise
410  */
411 WS_DLL_PUBLIC wtap_opttype_return_val
412 wtap_block_set_string_option_value_format(wtap_block_t block, guint option_id, const char *format, ...)
413                                           G_GNUC_PRINTF(3,4);
414
415 /** Get string option value from a block
416  *
417  * @param[in] block Block from which to get the option value
418  * @param[in] option_id Identifier value for option
419  * @param[out] value Returned value of option
420  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
421  * error code otherwise
422  */
423 WS_DLL_PUBLIC wtap_opttype_return_val
424 wtap_block_get_string_option_value(wtap_block_t block, guint option_id, char** value) G_GNUC_WARN_UNUSED_RESULT;
425
426 /** Get string option value for nth instance of a particular option in a block
427  *
428  * @param[in] block Block from which to get the option value
429  * @param[in] option_id Identifier value for option
430  * @param[in] idx Instance number of option with that ID
431  * @param[out] value Returned value of option
432  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
433  * error code otherwise
434  */
435 WS_DLL_PUBLIC wtap_opttype_return_val
436 wtap_block_get_nth_string_option_value(wtap_block_t block, guint option_id, guint idx, char** value) G_GNUC_WARN_UNUSED_RESULT;
437
438 /** Add a "custom" option value to a block
439  *
440  * @param[in] block Block to which to add the option
441  * @param[in] option_id Identifier value for option
442  * @param[in] value Value of option
443  * @param[in] value_size Size of value
444  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
445  * error code otherwise
446  */
447 WS_DLL_PUBLIC wtap_opttype_return_val
448 wtap_block_add_custom_option(wtap_block_t block, guint option_id, void* value, size_t value_size);
449
450 /** Set a "custom" option value in a block
451  *
452  * @param[in] block Block in which to set the option value
453  * @param[in] option_id Identifier value for option
454  * @param[in] value New value of option
455  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
456  * error code otherwise
457  */
458 WS_DLL_PUBLIC wtap_opttype_return_val
459 wtap_block_set_custom_option_value(wtap_block_t block, guint option_id, void* value);
460
461 /** Get a "custom" option value from a block
462  *
463  * @param[in] block Block from which to get the option value
464  * @param[in] option_id Identifier value for option
465  * @param[out] value Returned value of option
466  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
467  * error code otherwise
468  */
469 WS_DLL_PUBLIC wtap_opttype_return_val
470 wtap_block_get_custom_option_value(wtap_block_t block, guint option_id, void** value) G_GNUC_WARN_UNUSED_RESULT;
471
472 /** Remove an option from a block
473  *
474  * @param[in] block Block from which to remove the option
475  * @param[in] option_id Identifier value for option
476  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
477  * error code otherwise
478  */
479 WS_DLL_PUBLIC wtap_opttype_return_val
480 wtap_block_remove_option(wtap_block_t block, guint option_id);
481
482 /** Remove the nth instance of an option from a block
483  *
484  * @param[in] block Block from which to remove the option instance
485  * @param[in] option_id Identifier value for option
486  * @param[in] idx Instance number of option with that ID
487  * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
488  * error code otherwise
489  */
490 WS_DLL_PUBLIC wtap_opttype_return_val
491 wtap_block_remove_nth_option_instance(wtap_block_t block, guint option_id,
492                                       guint idx);
493
494 /** Copy a block to another.
495  *
496  * Any options that are in the destination but not the source are not removed.
497  * Options that are just in source will be added to destination
498  *
499  * @param[in] dest_block Block to be copied to
500  * @param[in] src_block Block to be copied from
501  */
502 WS_DLL_PUBLIC void wtap_block_copy(wtap_block_t dest_block, wtap_block_t src_block);
503
504
505 typedef void (*wtap_block_foreach_func)(wtap_block_t block, guint option_id, wtap_opttype_e option_type, wtap_optval_t *option, void *user_data);
506 WS_DLL_PUBLIC void wtap_block_foreach_option(wtap_block_t block, wtap_block_foreach_func func, void* user_data);
507
508 WS_DLL_PUBLIC int wtap_opttype_register_custom_block_type(const char* name, const char* description, wtap_block_create_func create,
509                                                 wtap_mand_free_func free_mand, wtap_mand_copy_func copy_mand);
510
511 /** Cleanup the internal structures
512  */
513 WS_DLL_PUBLIC void wtap_opttypes_cleanup(void);
514
515 #ifdef __cplusplus
516 }
517 #endif /* __cplusplus */
518
519 #endif /* WTAP_OPT_TYPES_H */
520