3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 2001 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #ifndef WTAP_OPT_TYPES_H
11 #define WTAP_OPT_TYPES_H
13 #include "ws_symbol_export.h"
15 #include <wsutil/inet_ipv6.h>
19 #endif /* __cplusplus */
22 * We use the pcapng option codes for option type values.
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. */
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.
33 #define OPT_SHB_OS 3 /**< A UTF-8 string containing the
34 * name of the operating system used to create this section.
36 #define OPT_SHB_USERAPPL 4 /**< A UTF-8 string containing the
37 * name of the application used to create this section.
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}"
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"
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
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
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).
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"
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"
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))
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?
102 #define OPT_NS_DNSNAME 2
103 #define OPT_NS_DNSIP4ADDR 3
104 #define OPT_NS_DNSIP6ADDR 4
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
115 typedef struct wtap_block *wtap_block_t;
118 * Currently supported blocks; these are not the pcapng block type values
119 * for them, they're identifiers used internally.
122 WTAP_BLOCK_NG_SECTION = 0,
126 WTAP_BLOCK_END_OF_LIST
129 /* Currently supported option types */
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;
148 struct wtap_opttype_custom
155 * Structure describing a value of an option.
160 guint32 ipv4val; /* network byte order */
163 struct wtap_opttype_custom customval;
167 * Structure describing an option in a block.
170 guint option_id; /**< option code for the option */
171 wtap_optval_t value; /**< value */
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);
180 /** Initialize block types.
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.
186 WS_DLL_PUBLIC void wtap_opttypes_initialize(void);
188 /** Create a block by type
190 * Return a newly allocated block with default options provided
192 * @param[in] block_type Block type to be created
193 * @return Newly allocated block
195 WS_DLL_PUBLIC wtap_block_t wtap_block_create(wtap_block_type_t block_type);
199 * Needs to be called to clean up any allocated block
201 * @param[in] block Block to be freed
203 WS_DLL_PUBLIC void wtap_block_free(wtap_block_t block);
205 /** Free an array of blocks
207 * Needs to be called to clean up blocks allocated
208 * through GArray (for multiple blocks of same type)
209 * Includes freeing the GArray
211 * @param[in] block_array Array of blocks to be freed
213 WS_DLL_PUBLIC void wtap_block_array_free(GArray* block_array);
215 /** Provide mandatory data of a block
217 * @param[in] block Block from which to retrieve mandatory data
218 * @return Block mandatory data. Structure varies based on block type
220 WS_DLL_PUBLIC void* wtap_block_get_mandatory_data(wtap_block_t block);
222 /** Add UINT8 option value to a block
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
230 WS_DLL_PUBLIC wtap_opttype_return_val
231 wtap_block_add_uint8_option(wtap_block_t block, guint option_id, guint8 value);
233 /** Set UINT8 option value in a block
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
241 WS_DLL_PUBLIC wtap_opttype_return_val
242 wtap_block_set_uint8_option_value(wtap_block_t block, guint option_id, guint8 value);
244 /** Get UINT8 option value from a block
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
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;
255 /** Add UINT64 option value to a block
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
263 WS_DLL_PUBLIC wtap_opttype_return_val
264 wtap_block_add_uint64_option(wtap_block_t block, guint option_id, guint64 value);
266 /** Set UINT64 option value in a block
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
274 WS_DLL_PUBLIC wtap_opttype_return_val
275 wtap_block_set_uint64_option_value(wtap_block_t block, guint option_id, guint64 value);
277 /** Get UINT64 option value from a block
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
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;
288 /** Add IPv4 address option value to a block
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
296 WS_DLL_PUBLIC wtap_opttype_return_val
297 wtap_block_add_ipv4_option(wtap_block_t block, guint option_id, guint32 value);
299 /** Set IPv4 option value in a block
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
307 WS_DLL_PUBLIC wtap_opttype_return_val
308 wtap_block_set_ipv4_option_value(wtap_block_t block, guint option_id, guint32 value);
310 /** Get IPv4 option value from a block
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
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;
321 /** Add IPv6 address option value to a block
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
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);
332 /** Set IPv6 option value in a block
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
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);
343 /** Get IPv6 option value from a block
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
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;
354 /** Add a string option to a block
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
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);
366 /** Add a string option to a block witha printf-formatted string as its value
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
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, ...)
378 /** Set string option value in a block
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
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);
390 /** Set string option value for nth instance of a particular option in a block
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
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);
403 /** Set string option value in a block to a printf-formatted string
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
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, ...)
415 /** Get string option value from a block
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
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;
426 /** Get string option value for nth instance of a particular option in a block
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
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;
438 /** Add a "custom" option value to a block
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
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);
450 /** Set a "custom" option value in a block
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
458 WS_DLL_PUBLIC wtap_opttype_return_val
459 wtap_block_set_custom_option_value(wtap_block_t block, guint option_id, void* value);
461 /** Get a "custom" option value from a block
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
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;
472 /** Remove an option from a block
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
479 WS_DLL_PUBLIC wtap_opttype_return_val
480 wtap_block_remove_option(wtap_block_t block, guint option_id);
482 /** Remove the nth instance of an option from a block
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
490 WS_DLL_PUBLIC wtap_opttype_return_val
491 wtap_block_remove_nth_option_instance(wtap_block_t block, guint option_id,
494 /** Copy a block to another.
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
499 * @param[in] dest_block Block to be copied to
500 * @param[in] src_block Block to be copied from
502 WS_DLL_PUBLIC void wtap_block_copy(wtap_block_t dest_block, wtap_block_t src_block);
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);
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);
511 /** Cleanup the internal structures
513 WS_DLL_PUBLIC void wtap_opttypes_cleanup(void);
517 #endif /* __cplusplus */
519 #endif /* WTAP_OPT_TYPES_H */