1 This file is a HOWTO for Wireshark developers interested in writing or working
2 on Wireshark protocol dissectors. It describes expected code patterns and the
3 use of some of the important functions and variables.
5 This file is compiled to give in depth information on Wireshark.
6 It is by no means all inclusive and complete. Please feel free to discuss on
7 the developer mailing list or upload merge requests to gitlab.
8 If you haven't read README.developer, read that first!
12 Before starting to develop a new dissector, a "running" Wireshark build
13 environment is required - there's no such thing as a standalone "dissector
16 How to setup such an environment is platform dependent; detailed
17 information about these steps can be found in the "Developer's Guide"
18 (available from: https://www.wireshark.org) and in the INSTALL and
19 README.md files of the sources root dir.
21 0.1. Dissector related README files.
23 You'll find additional dissector related information in the following README
26 - doc/README.heuristic - what are heuristic dissectors and how to write them
27 - doc/README.plugins - how to "pluginize" a dissector
28 - doc/README.request_response_tracking - how to track req./resp. times and such
29 - doc/README.wmem - how to obtain "memory leak free" memory
33 James Coe <jammer[AT]cin.net>
34 Gilbert Ramirez <gram[AT]alumni.rice.edu>
35 Jeff Foster <jfoste[AT]woodward.com>
36 Olivier Abad <oabad[AT]cybercable.fr>
37 Laurent Deniel <laurent.deniel[AT]free.fr>
38 Gerald Combs <gerald[AT]wireshark.org>
39 Guy Harris <guy[AT]alum.mit.edu>
40 Ulf Lamping <ulf.lamping[AT]web.de>
41 Barbu Paul - Gheorghe <barbu.paul.gheorghe[AT]gmail.com>
43 1. Setting up your protocol dissector code.
45 This section provides skeleton code for a protocol dissector. It also explains
46 the basic functions needed to enter values in the traffic summary columns,
47 add to the protocol tree, and work with registered header fields.
51 Wireshark requires certain things when setting up a protocol dissector.
52 We provide basic skeleton code for a dissector that you can copy to a new file
53 and fill in. Your dissector should follow the naming convention of "packet-"
54 followed by the abbreviated name for the protocol. It is recommended that where
55 possible you keep to the IANA abbreviated name for the protocol, if there is
56 one, or a commonly-used abbreviation for the protocol, if any.
58 The skeleton code lives in the file "packet-PROTOABBREV.c" in the same source
59 directory as this README.
61 If instead of using the skeleton you base your dissector on an existing real
62 dissector, please put a little note in the copyright header indicating which
63 dissector you started with.
65 Usually, you will put your newly created dissector file into the directory
66 epan/dissectors/, just like all the other packet-*.c files already in there.
68 Also, please add your dissector file to the corresponding makefiles,
69 described in section "1.8 Editing CMakeLists.txt to add your dissector" below.
71 Dissectors that use the dissector registration API to register with a lower
72 level protocol (this is the vast majority) don't need to define a prototype in
73 their .h file. For other dissectors the main dissector routine should have a
74 prototype in a header file whose name is "packet-", followed by the abbreviated
75 name for the protocol, followed by ".h"; any dissector file that calls your
76 dissector should be changed to include that file.
78 You may not need to include all the headers listed in the skeleton, and you may
79 need to include additional headers.
81 1.2 Explanation of needed substitutions in code skeleton.
83 In the skeleton sample code the following strings should be substituted with
86 YOUR_NAME Your name, of course. You do want credit, don't you?
87 It's the only payment you will receive...
88 YOUR_EMAIL_ADDRESS Keep those cards and letters coming.
89 PROTONAME The name of the protocol; this is displayed in the
90 top-level protocol tree item for that protocol.
91 PROTOSHORTNAME An abbreviated name for the protocol; this is displayed
92 in the "Preferences" dialog box if your dissector has
93 any preferences, in the dialog box of enabled protocols,
94 and in the dialog box for filter fields when constructing
96 PROTOFILTERNAME A name for the protocol for use in filter expressions;
97 it may contain only letters, digits, hyphens, underscores and
98 periods. Names should use lower case only. (Support for
99 upper/mixed case may be removed in the future.)
100 PROTOABBREV An abbreviation for the protocol; this is used in code and
101 must be a valid C identifier. Additionally it should follow
102 any applicable C style guidelines. It is usually the same as
103 PROTOFILTERNAME with all lower-case letters and
104 non-alphanumerics replaced with underscores.
105 LICENSE The license this dissector is under. Please use a SPDX License
107 YEARS The years the above license is valid for.
108 FIELDNAME The displayed name for the header field.
109 FIELDFILTERNAME A name for the header field for use in filter expressions;
110 it may contain only letters, digits, hyphens, underscores and
111 periods. It must start with PROTOFILTERNAME followed by a dot.
112 Names should use lower case only. (Support for upper/mixed case
113 may be removed in the future.)
114 FIELDABBREV An abbreviation for the header field; this is used in code and
115 must be a valid C identifier. Additionally it should follow
116 any applicable C style guidelines. It is usually the same as
117 FIELDFILTERNAME with all lower-case letters and
118 non-alphanumerics replaced with underscores.
119 FIELDTYPE FT_NONE, FT_BOOLEAN, FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24,
120 FT_UINT32, FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64,
121 FT_INT8, FT_INT16, FT_INT24, FT_INT32, FT_INT40, FT_INT48,
122 FT_INT56, FT_INT64, FT_IEEE_11073_SFLOAT, FT_IEEE_11073_FLOAT,
123 FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME, FT_RELATIVE_TIME,
124 FT_STRING, FT_STRINGZ, FT_STRINGZPAD, FT_STRINGZTRUNC,
125 FT_UINT_STRING, FT_ETHER, FT_BYTES, FT_UINT_BYTES, FT_IPv4,
126 FT_IPv6, FT_IPXNET, FT_FRAMENUM, FT_PROTOCOL, FT_EUI64, FT_GUID,
127 FT_OID, FT_REL_OID, FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FCWWN
128 FIELDDISPLAY --For FT_UINT{8,16,24,32,40,48,56,64} and
129 FT_INT{8,16,24,32,40,48,56,64):
131 BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC,
132 BASE_CUSTOM, or BASE_NONE, possibly ORed with
133 BASE_RANGE_STRING, BASE_EXT_STRING, BASE_VAL64_STRING,
134 BASE_ALLOW_ZERO, BASE_UNIT_STRING, BASE_SPECIAL_VALS,
135 BASE_NO_DISPLAY_VALUE, BASE_SHOW_ASCII_PRINTABLE, or
136 BASE_SHOW_UTF_8_PRINTABLE
138 BASE_NONE may be used with a non-NULL FIELDCONVERT when the
139 numeric value of the field itself is not of significance to
140 the user (for example, the number is a generated field).
141 When this is the case the numeric value is not shown to the
142 user in the protocol decode nor is it used when preparing
143 filters for the field in question.
145 BASE_NO_DISPLAY_VALUE will just display the field name with
146 no value. It is intended for byte arrays (FT_BYTES or
147 FT_UINT_BYTES) or header fields above a subtree. The
148 value will still be filterable, just not displayed.
152 BASE_PT_UDP, BASE_PT_TCP, BASE_PT_DCCP or BASE_PT_SCTP
159 BASE_HEX, BASE_OCT, BASE_CUSTOM, or BASE_NONE, possibly
160 ORed with BASE_RANGE_STRING, BASE_EXT_STRING or
163 BASE_NONE can be used in the same way as with FT_UINT8.
165 --For FT_FLOAT, FT_DOUBLE:
166 BASE_NONE, BASE_DEC, BASE_HEX, BASE_EXP or BASE_CUSTOM.
168 BASE_NONE uses BASE_DEC or BASE_EXP, similarly to the
169 %g double format for the printf() function.
171 --For FT_ABSOLUTE_TIME:
173 ABSOLUTE_TIME_LOCAL, ABSOLUTE_TIME_UTC, ABSOLUTE_TIME_DOY_UTC,
174 ABSOLUTE_TIME_NTP_UTC, or ABSOLUTE_TIME_UNIX
178 if BITMASK is non-zero:
179 Number of bits in the field containing the FT_BOOLEAN
184 --For FT_STRING, FT_STRINGZ, FT_STRINGZPAD, FT_STRINGZTRUNC,
187 BASE_STR_WSP or BASE_NONE
189 --For FT_BYTES and FT_UINT_BYTES:
191 SEP_DOT, SEP_DASH, SEP_COLON, or SEP_SPACE to provide
192 a separator between bytes; BASE_NONE has no separator
193 between bytes. These can be ORed with BASE_ALLOW_ZERO,
194 BASE_SHOW_ASCII_PRINTABLE, or BASE_SHOW_UTF_8_PRINTABLE.
196 BASE_ALLOW_ZERO displays <none> instead of <MISSING>
197 for a zero-sized byte array.
198 BASE_SHOW_ASCII_PRINTABLE will check whether the
199 field's value consists entirely of printable ASCII
200 characters and, if so, will display the field's value
201 as a string, in quotes. The value will still be
202 filterable as a byte value.
203 BASE_SHOW_UTF_8_PRINTABLE will check whether the
204 field's value is valid UTF-8 consisting entirely of
205 printable characters and, if so, will display the field's
206 value as a string, in quotes. The value will still be
207 filterable as a byte value.
211 BASE_NETMASK - Used for IPv4 address that should never
212 attempted to be resolved (like netmasks)
216 --For all other types:
219 FIELDCONVERT VALS(x), VALS64(x), RVALS(x), TFS(x), CF_FUNC(x), NULL
220 BITMASK Used to mask a field not 8-bit aligned or with a size other
221 than a multiple of 8 bits
222 FIELDDESCR A brief description of the field, or NULL. [Please do not use ""].
224 If, for example, PROTONAME is "Internet Bogosity Discovery Protocol",
225 PROTOSHORTNAME would be "IBDP", and PROTOFILTERNAME would be "ibdp". Try to
226 conform with IANA names.
228 1.2.1 Automatic substitution in code skeleton
230 Instead of manual substitutions in the code skeleton, a tool to automate it can
231 be found under the tools directory. The script is called tools/generate-dissector.py
232 and takes all the needed options to generate a compilable dissector. Look at the
233 above fields to know how to set them. Some assumptions have been made in the
234 generation to shorten the list of required options. The script patches the
235 CMakeLists.txt file adding the new dissector in the proper list, alphabetically
238 1.3 The dissector and the data it receives.
243 This is only needed if the dissector doesn't use self-registration to
244 register itself with the lower level dissector, or if the protocol dissector
245 wants/needs to expose code to other subdissectors.
247 The dissector must be declared exactly as follows in the file
248 packet-PROTOABBREV.h:
251 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
254 1.3.2 Extracting data from packets.
256 NOTE: See the file /epan/tvbuff.h for more details.
258 The "tvb" argument to a dissector points to a buffer containing the raw
259 data to be analyzed by the dissector; for example, for a protocol
260 running atop UDP, it contains the UDP payload (but not the UDP header,
261 or any protocol headers above it). A tvbuffer is an opaque data
262 structure, the internal data structures are hidden and the data must be
263 accessed via the tvbuffer accessors.
267 Bit accessors for a maximum of 8-bits, 16-bits 32-bits and 64-bits:
269 uint8_t tvb_get_bits8(tvbuff_t *tvb, int bit_offset, const int no_of_bits);
270 uint16_t tvb_get_bits16(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits, const unsigned encoding);
271 uint32_t tvb_get_bits32(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits, const unsigned encoding);
272 uint64_t tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits, const unsigned encoding);
274 Single-byte accessors for 8-bit unsigned integers (uint8_t) and 8-bit
275 signed integers (int8_t):
277 uint8_t tvb_get_uint8(tvbuff_t *tvb, const int offset);
278 int8_t tvb_get_int8(tvbuff_t *tvb, const int offset);
280 These functions were previously named tvb_get_guint8 and tvb_get_gint8. You
281 can still use the old names but they have been deprecated.
283 Network-to-host-order accessors:
285 16-bit unsigned (uint16_t) and signed (int16_t) integers:
287 uint16_t tvb_get_ntohs(tvbuff_t *tvb, const int offset);
288 int16_t tvb_get_ntohis(tvbuff_t *tvb, const int offset);
290 24-bit unsigned and signed integers:
292 uint32_t tvb_get_ntoh24(tvbuff_t *tvb, const int offset);
293 int32_t tvb_get_ntohi24(tvbuff_t *tvb, const int offset);
295 32-bit unsigned (uint32_t) and signed (int32_t) integers:
297 uint32_t tvb_get_ntohl(tvbuff_t *tvb, const int offset);
298 int32_t tvb_get_ntohil(tvbuff_t *tvb, const int offset);
300 40-bit unsigned and signed integers:
302 uint64_t tvb_get_ntoh40(tvbuff_t *tvb, const int offset);
303 int64_t tvb_get_ntohi40(tvbuff_t *tvb, const int offset);
305 48-bit unsigned and signed integers:
307 uint64_t tvb_get_ntoh48(tvbuff_t *tvb, const int offset);
308 int64_t tvb_get_ntohi48(tvbuff_t *tvb, const int offset);
310 56-bit unsigned and signed integers:
312 uint64_t tvb_get_ntoh56(tvbuff_t *tvb, const int offset);
313 int64_t tvb_get_ntohi56(tvbuff_t *tvb, const int offset);
315 64-bit unsigned (uint64_t) and signed (int64_t) integers:
317 uint64_t tvb_get_ntoh64(tvbuff_t *tvb, const int offset);
318 int64_t tvb_get_ntohi64(tvbuff_t *tvb, const int offset);
320 Single-precision and double-precision IEEE floating-point numbers:
322 float tvb_get_ntohieee_float(tvbuff_t *tvb, const int offset);
323 double tvb_get_ntohieee_double(tvbuff_t *tvb, const int offset);
325 Little-Endian-to-host-order accessors:
327 16-bit unsigned (uint16_t) and signed (int16_t) integers:
329 uint16_t tvb_get_letohs(tvbuff_t *tvb, const int offset);
330 int16_t tvb_get_letohis(tvbuff_t *tvb, const int offset);
332 24-bit unsigned and signed integers:
334 uint32_t tvb_get_letoh24(tvbuff_t *tvb, const int offset);
335 int32_t tvb_get_letohi24(tvbuff_t *tvb, const int offset);
337 32-bit unsigned (uint32_t) and signed (int32_t) integers:
339 uint32_t tvb_get_letohl(tvbuff_t *tvb, const int offset);
340 int32_t tvb_get_letohil(tvbuff_t *tvb, const int offset);
342 40-bit unsigned and signed integers:
344 uint64_t tvb_get_letoh40(tvbuff_t *tvb, const int offset);
345 int64_t tvb_get_letohi40(tvbuff_t *tvb, const int offset);
347 48-bit unsigned and signed integers:
349 uint64_t tvb_get_letoh48(tvbuff_t *tvb, const int offset);
350 int64_t tvb_get_letohi48(tvbuff_t *tvb, const int offset);
352 56-bit unsigned and signed integers:
354 uint64_t tvb_get_letoh56(tvbuff_t *tvb, const int offset);
355 int64_t tvb_get_letohi56(tvbuff_t *tvb, const int offset);
357 64-bit unsigned (uint64_t) and signed (int64_t) integers:
359 uint64_t tvb_get_letoh64(tvbuff_t *tvb, const int offset);
360 int64_t tvb_get_letohi64(tvbuff_t *tvb, const int offset);
362 NOTE: Although each of the integer accessors above return types with
363 specific sizes, the returned values are subject to C's integer promotion
364 rules. It's often safer and more useful to use int or unsigned for 32-bit
365 and smaller types, and int64_t or uint64_t for 40-bit and larger types.
366 Just because a value occupied 16 bits on the wire or over the air
367 doesn't mean it will within Wireshark.
369 Single-precision and double-precision IEEE floating-point numbers:
371 float tvb_get_letohieee_float(tvbuff_t *tvb, const int offset);
372 double tvb_get_letohieee_double(tvbuff_t *tvb, const int offset);
374 Encoding-to_host-order accessors:
376 16-bit unsigned (uint16_t) and signed (int16_t) integers:
378 uint16_t tvb_get_uint16(tvbuff_t *tvb, const int offset, const unsigned encoding);
379 int16_t tvb_get_int16(tvbuff_t *tvb, const int offset, const unsigned encoding);
381 24-bit unsigned and signed integers:
383 uint32_t tvb_get_uint24(tvbuff_t *tvb, const int offset, const unsigned encoding);
384 int32_t tvb_get_int24(tvbuff_t *tvb, const int offset, const unsigned encoding);
386 32-bit unsigned (uint32_t) and signed (int32_t) integers:
388 uint32_t tvb_get_uint32(tvbuff_t *tvb, const int offset, const unsigned encoding);
389 int32_t tvb_get_int32(tvbuff_t *tvb, const int offset, const unsigned encoding);
391 40-bit unsigned and signed integers:
393 uint64_t tvb_get_uint40(tvbuff_t *tvb, const int offset, const unsigned encoding);
394 int64_t tvb_get_int40(tvbuff_t *tvb, const int offset, const unsigned encoding);
396 48-bit unsigned and signed integers:
398 uint64_t tvb_get_uint48(tvbuff_t *tvb, const int offset, const unsigned encoding);
399 int64_t tvb_get_int48(tvbuff_t *tvb, const int offset, const unsigned encoding);
401 56-bit unsigned and signed integers:
403 uint64_t tvb_get_uint56(tvbuff_t *tvb, const int offset, const unsigned encoding);
404 int64_t tvb_get_int56(tvbuff_t *tvb, const int offset, const unsigned encoding);
406 64-bit unsigned (uint64_t) and signed (int64_t) integers:
408 uint64_t tvb_get_uint64(tvbuff_t *tvb, const int offset, const unsigned encoding);
409 int64_t tvb_get_int64(tvbuff_t *tvb, const int offset, const unsigned encoding);
411 These functions were previously named tvb_get_guintXX and tvb_get_gintXX. You
412 can still use the old names but they have been deprecated.
414 Single-precision and double-precision IEEE floating-point numbers:
416 float tvb_get_ieee_float(tvbuff_t *tvb, const int offset, const unsigned encoding);
417 double tvb_get_ieee_double(tvbuff_t *tvb, const int offset, const unsigned encoding);
419 "encoding" should be ENC_BIG_ENDIAN for Network-to-host-order,
420 ENC_LITTLE_ENDIAN for Little-Endian-to-host-order, or ENC_HOST_ENDIAN
423 Accessors for IPv4 and IPv6 addresses:
425 uint32_t tvb_get_ipv4(tvbuff_t *tvb, const int offset);
426 void tvb_get_ipv6(tvbuff_t *tvb, const int offset, ws_in6_addr *addr);
428 NOTE: IPv4 addresses are not to be converted to host byte order before
429 being passed to "proto_tree_add_ipv4()". You should use "tvb_get_ipv4()"
430 to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't,
431 for example, try to use "tvb_get_ntohl()", find that it gives you the
432 wrong answer on the PC on which you're doing development, and try
433 "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong
434 answer on big-endian machines.
436 char *tvb_ip_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset)
437 char *tvb_ip6_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset)
439 Returns a null-terminated buffer containing a string with IPv4 or IPv6 Address
440 from the specified tvbuff, starting at the specified offset.
444 void tvb_get_ntohguid(tvbuff_t *tvb, const int offset, e_guid_t *guid);
445 void tvb_get_letohguid(tvbuff_t *tvb, const int offset, e_guid_t *guid);
446 void tvb_get_guid(tvbuff_t *tvb, const int offset, e_guid_t *guid, const unsigned encoding);
450 uint8_t *tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, const int length, const unsigned encoding);
452 Returns a null-terminated buffer allocated from the specified scope, containing
453 data from the specified tvbuff, starting at the specified offset, and containing
454 the specified length worth of characters. Reads data in the specified encoding
455 and produces UTF-8 in the buffer. See below for a list of input encoding values.
457 The buffer is allocated in the given wmem scope (see README.wmem for more
460 uint8_t *tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int *lengthp, const unsigned encoding);
462 Returns a null-terminated buffer allocated from the specified scope,
463 containing data from the specified tvbuff, starting at the specified
464 offset, and containing all characters from the tvbuff up to and
465 including a terminating null character in the tvbuff. Reads data in the
466 specified encoding and produces UTF-8 in the buffer. See below for a
467 list of input encoding values. "*lengthp" will be set to the length of
468 the string, including the terminating null.
470 The buffer is allocated in the given wmem scope (see README.wmem for more
473 int tvb_get_nstringz(tvbuff_t *tvb, const int offset, const unsigned bufsize, uint8_t* buffer);
474 int tvb_get_nstringz0(tvbuff_t *tvb, const int offset, const unsigned bufsize, uint8_t* buffer);
476 Copies bufsize bytes, including the terminating NULL, to buffer. If a NULL
477 terminator is found before reaching bufsize, only the bytes up to and including
478 the NULL are copied. Returns the number of bytes copied (not including
479 terminating NULL), or -1 if the string was truncated in the buffer due to
480 not having reached the terminating NULL. In this case, the resulting
481 buffer is not NULL-terminated.
482 tvb_get_nstringz0() works like tvb_get_nstringz(), but never returns -1 since
483 the string is guaranteed to have a terminating NULL. If the string was truncated
484 when copied into buffer, a NULL is placed at the end of buffer to terminate it.
486 char *tvb_get_ts_23_038_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
487 const int bit_offset, int no_of_chars);
489 tvb_get_ts_23_038_7bits_string() returns a string of a given number of
490 characters and encoded according to 3GPP TS 23.038 7 bits alphabet.
492 The buffer is allocated in the given wmem scope (see README.wmem for more
495 Byte Array Accessors:
497 char *tvb_bytes_to_str(wmem_allocator_t *scope, tvbuff_t *tvb,
498 const int offset, const int len);
500 Formats a bunch of data from a tvbuff as bytes, returning a pointer
501 to the string with the data formatted as two hex digits for each byte.
502 The string pointed to is stored in an "wmem_alloc'd" buffer which will be freed
503 depending on its scope (typically pinfo->pool which is freed after the frame).
504 The formatted string will contain the hex digits for at most the first 16 bytes of
505 the data. If len is greater than 16 bytes, a trailing "..." will be added to the string.
507 char *tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb,
508 const int offset, const int len, const char punct);
510 This function is similar to tvb_bytes_to_str(...) except that 'punct' is inserted
511 between the hex representation of each byte.
513 GByteArray *tvb_get_string_bytes(tvbuff_t *tvb, const int offset, const int length,
514 const unsigned encoding, GByteArray* bytes, int *endoff)
516 Given a tvbuff, an offset into the tvbuff, and a length that starts
517 at that offset (which may be -1 for "all the way to the end of the
518 tvbuff"), fetch the hex-decoded byte values of the tvbuff into the
519 passed-in 'bytes' array, based on the passed-in encoding. In other
520 words, convert from a hex-ascii string in tvbuff, into the supplied
523 char *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const int offset,
524 const int len, dgt_set_t *dgt, bool skip_first);
526 Given a tvbuff, an offset into the tvbuff, and a length that starts
527 at that offset (which may be -1 for "all the way to the end of the
528 tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
529 the low or high half byte, formatting the digits according to an input digit set,
530 if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
531 A pointer to the packet scope allocated string will be returned.
532 Note: a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
535 void *tvb_memcpy(tvbuff_t *tvb, void *target, const int offset, size_t length);
537 Copies into the specified target the specified length's worth of data
538 from the specified tvbuff, starting at the specified offset.
540 void *tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, size_t length);
542 Returns a buffer containing a copy of the given TVB bytes. The buffer is
543 allocated in the given wmem scope (see README.wmem for more information).
546 /* WARNING! Don't use this function. There is almost always a better way.
547 * It's dangerous because once this pointer is given to the user, there's
548 * no guarantee that the user will honor the 'length' and not overstep the
549 * boundaries of the buffer. Also see the warning in the Portability section.
551 const uint8_t *tvb_get_ptr(tvbuff_t *tvb, const int offset, const int length);
554 Get amount of captured data in the buffer (which is *NOT* necessarily the
555 length of the packet). You probably want tvb_reported_length instead:
557 unsigned tvb_captured_length(const tvbuff_t *tvb);
559 Get reported length of buffer:
561 unsigned tvb_reported_length(const tvbuff_t *tvb);
564 1.4 Functions to handle columns in the traffic summary window.
566 The topmost pane of the main window is a list of the packets in the
567 capture, possibly filtered by a display filter.
569 Each line corresponds to a packet, and has one or more columns, as
570 configured by the user.
572 Many of the columns are handled by code outside individual dissectors;
573 most dissectors need only specify the value to put in the "Protocol" and
576 Columns are specified by COL_ values; the COL_ value for the "Protocol"
577 field, typically giving an abbreviated name for the protocol (but not
578 the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the
579 COL_ value for the "Info" field, giving a summary of the contents of the
580 packet for that protocol, is COL_INFO.
582 The value for a column can be specified with one of several functions,
583 all of which take the 'fd' argument to the dissector as their first
584 argument, and the COL_ value for the column as their second argument.
586 1.4.1 The col_set_str function.
588 'col_set_str' takes a string as its third argument, and sets the value
589 for the column to that value. It assumes that the pointer passed to it
590 points to a string constant or a static "const" array, not to a
591 variable, as it doesn't copy the string, it merely saves the pointer
592 value; the argument can itself be a variable, as long as it always
593 points to a string constant or a static "const" array.
595 It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if
596 the dissector will be using 'col_append_str' or 'col_append_fstr" to
597 append more information to the column, the string will have to be copied
598 anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in
601 For example, to set the "Protocol" column
602 to "PROTOFILTERNAME":
604 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOFILTERNAME");
607 1.4.2 The col_add_str function.
609 'col_add_str' takes a string as its third argument, and sets the value
610 for the column to that value. It takes the same arguments as
611 'col_set_str', but copies the string, so that if the string is, for
612 example, an automatic variable that won't remain in scope when the
613 dissector returns, it's safe to use.
616 1.4.3 The col_add_fstr function.
618 'col_add_fstr' takes a 'printf'-style format string as its third
619 argument, and 'printf'-style arguments corresponding to '%' format
620 items in that string as its subsequent arguments. For example, to set
621 the "Info" field to "<XXX> request, <N> bytes", where "reqtype" is a
622 string containing the type of the request in the packet and "n" is an
623 unsigned integer containing the number of bytes in the request:
625 col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
628 Don't use 'col_add_fstr' with a format argument of just "%s" -
629 'col_add_str', or possibly even 'col_set_str' if the string that matches
630 the "%s" is a static constant string, will do the same job more
634 1.4.4 The col_clear function.
636 If the Info column will be filled with information from the packet, that
637 means that some data will be fetched from the packet before the Info
638 column is filled in. If the packet is so small that the data in
639 question cannot be fetched, the routines to fetch the data will throw an
640 exception (see the comment at the beginning about tvbuffers improving
641 the handling of short packets - the tvbuffers keep track of how much
642 data is in the packet, and throw an exception on an attempt to fetch
643 data past the end of the packet, so that the dissector won't process
644 bogus data), causing the Info column not to be filled in.
646 This means that the Info column will have data for the previous
647 protocol, which would be confusing if, for example, the Protocol column
648 had data for this protocol.
650 Therefore, before a dissector fetches any data whatsoever from the
651 packet (unless it's a heuristic dissector fetching data to determine
652 whether the packet is one that it should dissect, in which case it
653 should check, before fetching the data, whether there's any data to
654 fetch; if there isn't, it should return false), it should set the
655 Protocol column and the Info column.
657 If the Protocol column will ultimately be set to, for example, a value
658 containing a protocol version number, with the version number being a
659 field in the packet, the dissector should, before fetching the version
660 number field or any other field from the packet, set it to a value
661 without a version number, using 'col_set_str', and should later set it
662 to a value with the version number after it's fetched the version
665 If the Info column will ultimately be set to a value containing
666 information from the packet, the dissector should, before fetching any
667 fields from the packet, clear the column using 'col_clear' (which is
668 more efficient than clearing it by calling 'col_set_str' or
669 'col_add_str' with a null string), and should later set it to the real
670 string after it's fetched the data to use when doing that.
673 1.4.5 The col_append_str function.
675 Sometimes the value of a column, especially the "Info" column, can't be
676 conveniently constructed at a single point in the dissection process;
677 for example, it might contain small bits of information from many of the
678 fields in the packet. 'col_append_str' takes, as arguments, the same
679 arguments as 'col_add_str', but the string is appended to the end of the
680 current value for the column, rather than replacing the value for that
681 column. (Note that no blank separates the appended string from the
682 string to which it is appended; if you want a blank there, you must add
683 it yourself as part of the string being appended.)
686 1.4.6 The col_append_fstr function.
688 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to
689 'col_add_str' - it takes, as arguments, the same arguments as
690 'col_add_fstr', but the formatted string is appended to the end of the
691 current value for the column, rather than replacing the value for that
694 1.4.7 The col_append_sep_str and col_append_sep_fstr functions.
696 In specific situations the developer knows that a column's value will be
697 created in a stepwise manner, where the appended values are listed. Both
698 'col_append_sep_str' and 'col_append_sep_fstr' functions will add an item
699 separator between two consecutive items, and will not add the separator at the
700 beginning of the column. The remainder of the work both functions do is
701 identical to what 'col_append_str' and 'col_append_fstr' do.
703 1.4.8 The col_set_fence and col_prepend_fence_fstr functions.
705 Sometimes a dissector may be called multiple times for different PDUs in the
706 same frame (for example in the case of SCTP chunk bundling: several upper
707 layer data packets may be contained in one SCTP packet). If the upper layer
708 dissector calls 'col_set_str()' or 'col_clear()' on the Info column when it
709 begins dissecting each of those PDUs then when the frame is fully dissected
710 the Info column would contain only the string from the last PDU in the frame.
711 The 'col_set_fence' function erects a "fence" in the column that prevents
712 subsequent 'col_...' calls from clearing the data currently in that column.
713 For example, the SCTP dissector calls 'col_set_fence' on the Info column
714 after it has called any subdissectors for that chunk so that subdissectors
715 of any subsequent chunks may only append to the Info column.
716 'col_prepend_fence_fstr' prepends data before a fence (moving it if
717 necessary). It will create a fence at the end of the prepended data if the
718 fence does not already exist.
721 1.4.9 The col_set_time function.
723 The 'col_set_time' function takes an nstime value as its third argument.
724 This nstime value is a relative value and will be added as such to the
725 column. The fourth argument is the filtername holding this value. This
726 way, rightclicking on the column makes it possible to build a filter
727 based on the time-value.
731 col_set_time(pinfo->cinfo, COL_REL_TIME, &ts, "s4607.ploc.time");
734 1.5 Constructing the protocol tree.
736 The middle pane of the main window, and the topmost pane of a packet
737 popup window, are constructed from the "protocol tree" for a packet.
739 The protocol tree, or proto_tree, is an N-way tree structure of proto_nodes.
740 Of course the protocol dissectors don't care what a proto_tree really is;
741 they just pass the proto_tree pointer as an argument to the routines which
742 allow them to add items and new branches to the tree. (Some protocol dissectors
743 do access struct members directly instead of calling routines; this is
746 When a packet is selected in the packet-list pane, or a packet popup
747 window is created, a new logical protocol tree (proto_tree) is created.
748 The pointer to the proto_tree (in this case, 'protocol tree'), is passed
749 to the top-level protocol dissector, and then to all subsequent protocol
750 dissectors for that packet, and then the GUI tree is drawn via
753 The logical proto_tree needs to know detailed information about the protocols
754 and fields about which information will be collected from the dissection
755 routines. By strictly defining (or "typing") the data that can be attached to a
756 proto tree, searching and filtering becomes possible. This means that for
757 every protocol and field (which I also call "header fields", since they are
758 fields in the protocol headers) which might be attached to a tree, some
759 information is needed.
761 Every dissector routine will need to register its protocols and fields
762 with the central protocol routines (in proto.c). At first I thought I
763 might keep all the protocol and field information about all the
764 dissectors in one file, but decentralization seemed like a better idea.
765 That one file would have gotten very large; one small change would have
766 required a re-compilation of the entire file. Also, by allowing
767 registration of protocols and fields at run-time, loadable modules of
768 protocol dissectors (perhaps even user-supplied) is feasible.
770 To do this, each protocol should have a register routine, which will be
771 called when Wireshark starts. The code to call the register routines is
772 generated automatically; to arrange that a protocol's register routine
773 be called at startup:
775 the file containing a dissector's "register" routine must be
776 added to "DISSECTOR_SRC" in "epan/dissectors/CMakeLists.txt";
778 the "register" routine must have a name of the form
779 "proto_register_XXX";
781 the "register" routine must take no argument, and return no
784 the "register" routine's name must appear in the source file
785 either at the beginning of the line, or preceded only by "void "
786 at the beginning of the line (that would typically be the
787 definition) - other white space shouldn't cause a problem, e.g.:
789 void proto_register_XXX(void) {
798 proto_register_XXX( void )
805 and so on should work.
807 For every protocol or field that a dissector wants to register, a variable of
808 type int needs to be used to keep track of the protocol. The IDs are
809 needed for establishing parent/child relationships between protocols and
810 fields, as well as associating data with a particular field so that it
811 can be stored in the logical tree and displayed in the GUI protocol
814 Some dissectors will need to create branches within their tree to help
815 organize header fields. These branches should be registered as header
816 fields. Only true protocols should be registered as protocols. This is
817 so that a display filter user interface knows how to distinguish
818 protocols from fields.
820 A protocol is registered with the name of the protocol and its
823 Here is how the frame "protocol" is registered.
827 proto_frame = proto_register_protocol (
829 /* short name */ "Frame",
830 /* abbrev */ "frame" );
832 A header field is also registered with its name and abbreviation, but
833 information about its data type is needed. It helps to look at
834 the header_field_info struct to see what information is expected:
836 struct header_field_info {
849 A string representing the name of the field. This is the name
850 that will appear in the graphical protocol tree. It must be a non-empty
853 abbrev (FIELDFILTERNAME)
855 A string with a filter name for the field. The name should start
856 with the filter name of the parent protocol followed by a period as a
857 separator. For example, the "src" field in an IP packet would have "ip.src"
858 as a filter name. It is acceptable to have multiple levels of periods if,
859 for example, you have fields in your protocol that are then subdivided into
860 subfields. For example, TRMAC has multiple error fields, so the names
861 follow this pattern: "trmac.errors.iso", "trmac.errors.noniso", etc.
862 It must be a non-empty string.
866 The type of value this field holds. The current field types are:
868 FT_NONE No field type. Used for fields that
869 aren't given a value, and that can only
870 be tested for presence or absence; a
871 field that represents a data structure,
872 with a subtree below it containing
873 fields for the members of the structure,
874 or that represents an array with a
875 subtree below it containing fields for
876 the members of the array, might be an
878 FT_PROTOCOL Used for protocols which will be placing
879 themselves as top-level items in the
880 "Packet Details" pane of the UI.
881 FT_BOOLEAN 0 means "false", any other value means
883 FT_FRAMENUM A frame number; if this is used, the "Go
884 To Corresponding Frame" menu item can
886 FT_CHAR An 8-bit ASCII character. It's treated similarly to an
887 FT_UINT8, but is displayed as a C-style character
889 FT_UINT8 An 8-bit unsigned integer.
890 FT_UINT16 A 16-bit unsigned integer.
891 FT_UINT24 A 24-bit unsigned integer.
892 FT_UINT32 A 32-bit unsigned integer.
893 FT_UINT40 A 40-bit unsigned integer.
894 FT_UINT48 A 48-bit unsigned integer.
895 FT_UINT56 A 56-bit unsigned integer.
896 FT_UINT64 A 64-bit unsigned integer.
897 FT_INT8 An 8-bit signed integer.
898 FT_INT16 A 16-bit signed integer.
899 FT_INT24 A 24-bit signed integer.
900 FT_INT32 A 32-bit signed integer.
901 FT_INT40 A 40-bit signed integer.
902 FT_INT48 A 48-bit signed integer.
903 FT_INT56 A 56-bit signed integer.
904 FT_INT64 A 64-bit signed integer.
905 FT_IEEE_11073_SFLOAT A 16-bit floating point number, consisting
906 of an 4-bit exponent and 12-bit mantissa.
907 FT_IEEE_11073_FLOAT A 32-bit floating point number, consisting
908 of an 8-bit exponent and 24-bit mantissa.
909 FT_FLOAT A single-precision floating point number.
910 FT_DOUBLE A double-precision floating point number.
911 FT_ABSOLUTE_TIME An absolute time from some fixed point in time,
912 displayed as the date, followed by the time, as
913 hours, minutes, and seconds with 9 digits after
915 FT_RELATIVE_TIME Seconds (4 bytes) and nanoseconds (4 bytes)
916 of time relative to an arbitrary time.
917 displayed as seconds and 9 digits
918 after the decimal point.
919 FT_STRING A string of characters, not necessarily
920 NULL-terminated, but possibly NULL-padded.
921 This, and the other string-of-characters
922 types, are to be used for text strings,
924 FT_STRINGZ A NULL-terminated string of characters.
925 The string length is normally the length
926 given in the proto_tree_add_item() call.
927 However if the length given in the call
928 is -1, then the length used is that
929 returned by calling tvb_strsize().
930 This should only be used if the string,
931 in the packet, is always terminated with
932 a NULL character, either because the length
933 isn't otherwise specified or because a
934 character count *and* a NULL terminator are
936 FT_STRINGZPAD A NULL-padded string of characters.
937 The length is given in the proto_tree_add_item()
938 call, but may be larger than the length of
939 the string, with extra bytes being NULL padding.
940 This is typically used for fixed-length fields
941 that contain a string value that might be shorter
942 than the fixed length.
943 FT_STRINGZTRUNC A NULL-truncated string of characters.
944 The length is given in the proto_tree_add_item()
945 call, but may be larger than the length of
946 the string, with a NULL character after the last
947 character of the string, and the remaining bytes
948 being padding with unspecified contents. This is
949 typically used for fixed-length fields that contain
950 a string value that might be shorter than the fixed
952 FT_UINT_STRING A counted string of characters, consisting
953 of a count (represented as an integral value,
954 of width given in the proto_tree_add_item()
955 call) followed immediately by that number of
957 FT_ETHER A six octet string displayed in
958 Ethernet-address format.
959 FT_BYTES A string of bytes with arbitrary values;
960 used for raw binary data.
961 FT_UINT_BYTES A counted string of bytes, consisting
962 of a count (represented as an integral value,
963 of width given in the proto_tree_add_item()
964 call) followed immediately by that number of
965 arbitrary values; used for raw binary data.
966 FT_IPv4 A version 4 IP address (4 bytes) displayed
967 in dotted-quad IP address format (4
968 decimal numbers separated by dots).
969 FT_IPv6 A version 6 IP address (16 bytes) displayed
970 in standard IPv6 address format.
971 FT_IPXNET An IPX address displayed in hex as a 6-byte
972 network number followed by a 6-byte station
974 FT_GUID A Globally Unique Identifier
975 FT_OID An ASN.1 Object Identifier
976 FT_REL_OID An ASN.1 Relative Object Identifier
977 FT_EUI64 A EUI-64 Address
978 FT_AX25 A AX-25 Address
979 FT_VINES A Vines Address
980 FT_SYSTEM_ID An OSI System-ID
981 FT_FCWWN A Fibre Channel WWN Address
983 Some of these field types are still not handled in the display filter
984 routines, but the most common ones are. The FT_UINT* variables all
985 represent unsigned integers, and the FT_INT* variables all represent
986 signed integers; the number on the end represent how many bits are used
987 to represent the number.
989 Some constraints are imposed on the header fields depending on the type
990 (e.g. FT_BYTES) of the field. Fields of type FT_ABSOLUTE_TIME must use
991 'ABSOLUTE_TIME_{LOCAL,UTC,DOY_UTC}, NULL, 0x0' as values for the
992 'display, 'strings', and 'bitmask' fields, and all other non-integral
993 types (i.e.. types that are _not_ FT_INT* and FT_UINT*) must use
994 'BASE_NONE, NULL, 0x0' as values for the 'display', 'strings', 'bitmask'
995 fields. The reason is simply that the type itself implicitly defines the
996 nature of 'display', 'strings', 'bitmask'.
998 display (FIELDDISPLAY)
999 ----------------------
1000 The display field has a couple of overloaded uses. This is unfortunate,
1001 but since we're using C as an application programming language, this sometimes
1002 makes for cleaner programs. Right now I still think that overloading
1003 this variable was okay.
1005 For integer fields (FT_UINT* and FT_INT*), this variable represents the
1006 base in which you would like the value displayed. The acceptable bases
1016 BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal,
1017 respectively. BASE_DEC_HEX and BASE_HEX_DEC display value in two bases
1018 (the 1st representation followed by the 2nd in parenthesis).
1020 BASE_CUSTOM allows one to specify a callback function pointer that will
1023 For 32-bit and smaller values, custom_fmt_func_t can be used to declare
1024 the callback function pointer. Specifically, this is defined as:
1026 void func(char *, uint32_t);
1028 For values larger than 32-bits, custom_fmt_func_64_t can be used to declare
1029 the callback function pointer. Specifically, this is defined as:
1031 void func(char *, uint64_t);
1033 The first argument is a pointer to a buffer of the ITEM_LABEL_LENGTH size
1034 and the second argument is the value to be formatted.
1036 Both custom_fmt_func_t and custom_fmt_func_64_t are defined in epan/proto.h.
1038 For FT_UINT16 'display' can be used to select a transport layer protocol using one
1039 of BASE_PT_UDP, BASE_PT_TCP, BASE_PT_DCCP or BASE_PT_SCTP. If transport name
1040 resolution is enabled the port field label is displayed in decimal and as a well-known
1041 service name (if one is available).
1043 For FT_BOOLEAN fields that are also bitfields (i.e., 'bitmask' is non-zero),
1044 'display' is used specify a "field-width" (i.e., tell the proto_tree how
1045 wide the parent bitfield is). (If the FT_BOOLEAN 'bitmask' is zero, then
1046 'display' must be BASE_NONE).
1048 For integer fields a "field-width" is not needed since the type of
1049 integer itself (FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_UINT40,
1050 FT_UINT48, FT_UINT56, FT_UINT64, etc) tells the proto_tree how wide the
1051 parent bitfield is. The same is true of FT_CHAR, as it's an 8-bit
1054 For FT_ABSOLUTE_TIME fields, 'display' is used to indicate whether the
1055 time is to be displayed as a time in the time zone for the machine on
1056 which Wireshark/TShark is running or as UTC and, for UTC, whether the
1057 date should be displayed as "{monthname} {day_of_month}, {year}" or as
1058 "{year/day_of_year}".
1060 Additionally, BASE_NONE is used for 'display' as a NULL-value. That is, for
1061 non-integers other than FT_ABSOLUTE_TIME fields, and non-bitfield
1062 FT_BOOLEANs, you'll want to use BASE_NONE in the 'display' field. You may
1063 not use BASE_NONE for integers.
1065 It is possible that in the future we will record the endianness of
1066 integers. If so, it is likely that we'll use a bitmask on the display field
1067 so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX.
1068 But that has not happened yet; note that there are protocols for which
1069 no endianness is specified, such as the X11 protocol and the DCE RPC
1070 protocol, so it would not be possible to record the endianness of all
1073 strings (FIELDCONVERT)
1074 ----------------------
1076 Some integer fields, of type FT_UINT*, need labels to represent the true
1077 value of a field. You could think of those fields as having an
1078 enumerated data type, rather than an integral data type.
1080 A 'value_string' structure is a way to map values to strings.
1082 typedef struct _value_string {
1087 For fields of that type, you would declare an array of "value_string"s:
1089 static const value_string valstringname[] = {
1090 { INTVAL1, "Descriptive String 1" },
1091 { INTVAL2, "Descriptive String 2" },
1095 (the last entry in the array must have a NULL 'strptr' value, to
1096 indicate the end of the array). The 'strings' field would be set to
1097 'VALS(valstringname)'.
1099 If the field has a numeric rather than an enumerated type, the 'strings'
1100 field would be set to NULL.
1102 If BASE_SPECIAL_VALS is also applied to the display bitmask, then if the
1103 numeric value of a field doesn't match any values in the value_string
1104 then just the numeric value is displayed (i.e. no "Unknown"). This is
1105 intended for use when the value_string only gives special names for
1106 certain field values and values not in the value_string are expected.
1108 -- Extended value strings
1109 You can also use an extended version of the value_string for faster lookups.
1110 It requires a value_string array as input.
1111 If all of a contiguous range of values from min to max are present in the array
1112 in ascending order the value will be used as a direct index into a value_string array.
1114 If the values in the array are not contiguous (ie: there are "gaps"), but are
1115 in ascending order a binary search will be used.
1117 Note: "gaps" in a value_string array can be filled with "empty" entries eg:
1118 {value, "Unknown"} so that direct access to the array is possible.
1120 Note: the value_string array values are *unsigned*; IOW: -1 is greater than 0.
1122 { -2, -1, 1, 2 }; wrong: linear search will be used (note gap)
1123 { 1, 2, -2, -1 }; correct: binary search will be used
1126 { -2, -1, 0, 1, 2 }; OK: direct(indexed) access will be used (note no gap)
1128 The init macro (see below) will perform a check on the value string the first
1129 time it is used to determine which search algorithm fits and fall back to a
1130 linear search if the value_string does not meet the criteria above.
1132 Use this macro to initialize the extended value_string at compile time:
1134 static value_string_ext valstringname_ext = VALUE_STRING_EXT_INIT(valstringname);
1136 Extended value strings can be created at run time by calling
1137 value_string_ext_new(<ptr to value_string array>,
1138 <total number of entries in the value_string_array>, /* include {0, NULL} entry */
1139 <value_string_name>);
1141 For hf[] array FT_(U)INT* fields that need a 'valstringname_ext' struct, the
1142 'strings' field would be set to '&valstringname_ext'. Furthermore, the 'display'
1143 field must be ORed with 'BASE_EXT_STRING' (e.g. BASE_DEC|BASE_EXT_STRING).
1147 val64_strings are like value_strings, except that the integer type
1148 used is a uint64_t (instead of uint32_t). Instead of using the VALS()
1149 macro for the 'strings' field in the header_field_info struct array,
1152 BASE_SPECIAL_VALS can also be used for val64_string.
1156 val64_string_ext is like value_string_ext, except that the integer type
1157 used is a uint64_t (instead of uint32_t).
1159 Use this macro to initialize the extended val64_string at compile time:
1161 static val64_string_ext val64stringname_ext = VAL64_STRING_EXT_INIT(val64stringname);
1163 Extended val64 strings can be created at run time by calling
1164 val64_string_ext_new(<ptr to val64_string array>,
1165 <total number of entries in the val64_string_array>, /* include {0, NULL} entry */
1166 <val64_string_name>);
1168 For hf[] array FT_(U)INT* fields that need a 'val64stringname_ext' struct, the
1169 'strings' field would be set to '&val64stringname_ext'. Furthermore, the 'display'
1170 field must be ORed with both 'BASE_EXT_STRING' and 'BASE_VAL64_STRING'
1171 (e.g. BASE_DEC|BASE_EXT_STRING|BASE_VAL64_STRING).
1174 Some integer fields, of type FT_UINT* and float fields, of type FT_FLOAT
1175 or FT_DOUBLE, need units of measurement to help convey the field value.
1177 A 'unit_name_string' structure is a way to add a unit suffix to a field.
1179 typedef struct unit_name_string {
1180 char *singular; /* name to use for 1 unit */
1181 char *plural; /* name to use for < 1 or > 1 units */
1184 For fields with that unit name, you would declare a "unit_name_string":
1186 static const unit_name_string unitname[] =
1187 { "single item name" , "multiple item name" };
1189 (the second entry can be NULL if there is no plural form of the unit name.
1190 This is typically the case when abbreviations are used instead of full words.)
1192 For hf[] array FT_(U)INT*, FT_FLOAT and FT_DOUBLE fields that need a
1193 'unit_name_string' struct, the 'strings' field would be set to
1194 '&unitname'. Furthermore, the 'display' field must be ORed
1195 with 'BASE_UNIT_STRING' (e.g. BASE_DEC|BASE_UNIT_STRING).
1197 There are several "common" unit name structures already defined in
1198 epan/unit_strings.h, e.g. 'units_second_seconds'. Dissector authors may choose
1199 to add the unit name structure there rather than locally in a dissector.
1202 If the field has a numeric type that might logically fit in ranges of values
1203 one can use a range_string struct.
1205 Thus a 'range_string' structure is a way to map ranges to strings.
1207 typedef struct _range_string {
1213 For fields of that type, you would declare an array of "range_string"s:
1215 static const range_string rvalstringname[] = {
1216 { INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" },
1217 { INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" },
1221 If INTVAL_MIN equals INTVAL_MAX for a given entry the range_string
1222 behavior collapses to the one of value_string. Note that each range_string
1223 within the array is tested in order, so any 'catch-all' entries need to come
1224 after specific individual entries.
1226 For FT_(U)INT* fields that need a 'range_string' struct, the 'strings' field
1227 would be set to 'RVALS(rvalstringname)'. Furthermore, 'display' field must be
1228 ORed with 'BASE_RANGE_STRING' (e.g. BASE_DEC|BASE_RANGE_STRING).
1231 FT_BOOLEANs have a default map of 0 = "False", 1 (or anything else) = "True".
1232 Sometimes it is useful to change the labels for boolean values (e.g.,
1233 to "Yes"/"No", "Fast"/"Slow", etc.). For these mappings, a struct called
1234 true_false_string is used.
1236 typedef struct true_false_string {
1239 } true_false_string;
1241 For Boolean fields for which "False" and "True" aren't the desired
1242 labels, you would declare a "true_false_string"s:
1244 static const true_false_string boolstringname = {
1249 Its two fields are pointers to the string representing truth, and the
1250 string representing falsehood. For FT_BOOLEAN fields that need a
1251 'true_false_string' struct, the 'strings' field would be set to
1252 'TFS(&boolstringname)'.
1254 If the Boolean field is to be displayed as "False" or "True", the
1255 'strings' field would be set to NULL.
1257 Wireshark predefines a whole range of ready made "true_false_string"s
1258 in tfs.h, included via packet.h.
1261 Custom fields (BASE_CUSTOM) should use CF_FUNC(&custom_format_func) for the
1265 FT_FRAMENUMs can use the 'strings' field to indicate their purpose by
1266 setting the field to 'FRAMENUM_TYPE(x)', where x is one of the values of
1267 the ft_framenum_type enum:
1271 FT_FRAMENUM_RESPONSE
1274 FT_FRAMENUM_RETRANS_PREV
1275 FT_FRAMENUM_RETRANS_NEXT
1277 The packet list uses the value to determine the related packet symbol to draw.
1278 Note that 'strings' field NULL is equal to FRAMENUM_TYPE(FT_FRAMENUM_NONE).
1280 -- Note to plugin authors
1281 Data cannot get exported from DLLs. For this reason plugin authors cannot use
1282 existing fieldconvert strings (e.g. from existing dissectors or those from
1283 epan/unit_strings.h). Plugins must define value_strings, unit_name_strings,
1284 range_strings and true_false_strings locally.
1288 If the field is a bitfield, then the bitmask is the mask which will
1289 leave only the bits needed to make the field when ANDed with a value.
1290 The proto_tree routines will calculate 'bitshift' automatically
1291 from 'bitmask', by finding the rightmost set bit in the bitmask.
1292 This shift is applied before applying string mapping functions or
1295 If the field is not a bitfield, then bitmask should be set to 0.
1299 This is a string giving a proper description of the field. It should be
1300 at least one grammatically complete sentence, or NULL in which case the
1301 name field is used. (Please do not use "").
1303 It is meant to provide a more detailed description of the field than the
1304 name alone provides. This information will be used in the man page, and
1305 in a future GUI display-filter creation tool. We might also add tooltips
1306 to the labels in the GUI protocol tree, in which case the blurb would
1307 be used as the tooltip text.
1310 1.5.1 Field Registration.
1312 Protocol registration is handled by creating an instance of the
1313 header_field_info struct (or an array of such structs), and
1314 calling the registration function along with the registration ID of
1315 the protocol that is the parent of the fields. Here is a complete example:
1317 static int proto_eg;
1318 static int hf_field_a;
1319 static int hf_field_b;
1321 static hf_register_info hf[] = {
1323 { "Field A", "proto.field_a", FT_UINT8, BASE_HEX, NULL,
1324 0xf0, "Field A represents Apples", HFILL }
1328 { "Field B", "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
1329 0x0, "Field B represents Bananas", HFILL }
1333 proto_eg = proto_register_protocol("Example Protocol",
1335 proto_register_field_array(proto_eg, hf, array_length(hf));
1337 Be sure that your array of hf_register_info structs is declared 'static',
1338 since the proto_register_field_array() function does not create a copy
1339 of the information in the array... it uses that static copy of the
1340 information that the compiler created inside your array. Here's the
1341 layout of the hf_register_info struct:
1343 typedef struct hf_register_info {
1344 int *p_id; /* pointer to parent variable */
1345 header_field_info hfinfo;
1348 Also be sure to use the handy array_length() macro found in packet.h
1349 to have the compiler compute the array length for you at compile time.
1351 If you don't have any fields to register, do *NOT* create a zero-length
1352 "hf" array; not all compilers used to compile Wireshark support them.
1353 Just omit the "hf" array, and the "proto_register_field_array()" call,
1356 It is OK to have header fields with a different format be registered with
1357 the same abbreviation. For instance, the following is valid:
1359 static hf_register_info hf[] = {
1360 { &hf_field_8bit, /* 8-bit version of proto.field */
1361 { "Field (8 bit)", "proto.field", FT_UINT8, BASE_DEC, NULL,
1362 0x00, "Field represents FOO", HFILL }
1365 { &hf_field_32bit, /* 32-bit version of proto.field */
1366 { "Field (32 bit)", "proto.field", FT_UINT32, BASE_DEC, NULL,
1367 0x00, "Field represents FOO", HFILL }
1371 This way a filter expression can match a header field, irrespective of the
1372 representation of it in the specific protocol context. This is interesting
1373 for protocols with variable-width header fields.
1375 Note that the formats used must all belong to the same group as defined below:
1376 - FT_INT8, FT_INT16, FT_INT24 and FT_INT32, FT_CHAR, FT_UINT8, FT_UINT16,
1377 FT_UINT24, FT_UINT32, FT_IPXNET, FT_FRAMENUM, FT_INT40, FT_INT48, FT_INT56,
1378 FT_INT64, FT_UINT40, FT_UINT48, FT_UINT56 and FT_UINT64
1379 - FT_ABSOLUTE_TIME and FT_RELATIVE_TIME
1380 - FT_STRING, FT_STRINGZ, FT_UINT_STRING, FT_STRINGZPAD, FT_STRINGZTRUNC and FT_AX25
1381 - FT_FLOAT, FT_DOUBLE, FT_IEEE_11073_SFLOAT and FT_IEEE_11073_FLOAT
1382 - FT_BYTES, FT_UINT_BYTES, FT_ETHER, FT_VINES, FT_FCWWN and FT_EUI64
1383 - FT_OID, FT_REL_OID and FT_SYSTEM_ID
1385 Any field not in a grouping above should *NOT* be used in duplicate field
1386 abbreviations. The current code does not prevent it, but someday in the future
1389 The HFILL macro at the end of the struct will set reasonable default values
1390 for internally used fields.
1392 1.5.2 Adding Items and Values to the Protocol Tree.
1394 A protocol item is added to an existing protocol tree with one of a
1395 handful of proto_XXX_DO_YYY() functions.
1397 Subtrees can be made with the proto_item_add_subtree() function:
1399 item = proto_tree_add_item(...);
1400 new_tree = proto_item_add_subtree(item, tree_type);
1402 This will add a subtree under the item in question; a subtree can be
1403 created under an item made by any of the "proto_tree_add_XXX" functions,
1404 so that the tree can be given an arbitrary depth.
1406 Subtree types are integers, assigned by
1407 "proto_register_subtree_array()". To register subtree types, pass an
1408 array of pointers to "int" variables to hold the subtree type values to
1409 "proto_register_subtree_array()":
1412 static int ett_field_a;
1414 static int *ett[] = {
1419 proto_register_subtree_array(ett, array_length(ett));
1421 in your "register" routine, just as you register the protocol and the
1422 fields for that protocol.
1424 The ett_ variables identify particular type of subtree so that if you expand
1425 one of them, Wireshark keeps track of that and, when you click on
1426 another packet, it automatically opens all subtrees of that type.
1427 If you close one of them, all subtrees of that type will be closed when
1428 you move to another packet.
1430 There are many functions that the programmer can use to add either
1431 protocol or field labels to the proto_tree, for example:
1434 proto_tree_add_item(tree, id, tvb, start, length, encoding);
1437 proto_tree_add_item_ret_int(tree, id, tvb, start, length, encoding,
1441 proto_tree_add_subtree(tree, tvb, start, length, idx, tree_item,
1445 proto_tree_add_int_format_value(tree, id, tvb, start, length,
1446 value, format, ...);
1449 proto_tree_add_checksum(proto_tree *tree, tvbuff_t *tvb, const unsigned offset,
1450 const int hf_checksum, const int hf_checksum_status,
1451 struct expert_field* bad_checksum_expert, packet_info *pinfo,
1452 uint32_t computed_checksum, const unsigned encoding, const unsigned flags);
1455 proto_tree_add_bitmask(tree, tvb, start, header, ett, fields,
1459 proto_tree_add_bits_item(tree, id, tvb, bit_offset, no_of_bits,
1462 The 'tree' argument is the tree to which the item is to be added. The
1463 'tvb' argument is the tvbuff from which the item's value is being
1464 extracted; the 'start' argument is the offset from the beginning of that
1465 tvbuff of the item being added, and the 'length' argument is the length,
1466 in bytes, of the item, bit_offset is the offset in bits and no_of_bits
1467 is the length in bits.
1469 The length of some items cannot be determined until the item has been
1470 dissected; to add such an item, add it with a length of -1, and, when the
1471 dissection is complete, set the length with 'proto_item_set_len()':
1474 proto_item_set_len(ti, length);
1476 The "ti" argument is the value returned by the call that added the item
1477 to the tree, and the "length" argument is the length of the item.
1479 All available protocol tree functions are declared in epan/proto.h, with
1480 their documentation. The details of these functions and their parameters
1481 are described below.
1483 proto_tree_add_item()
1484 ---------------------
1485 proto_tree_add_item is used when you wish to do no special formatting.
1486 The item added to the GUI tree will contain the name (as passed in the
1487 proto_register_*() function) and a value. The value will be fetched
1488 from the tvbuff by proto_tree_add_item(), based on the type of the field
1489 and the encoding of the value as specified by the "encoding" argument.
1491 For FT_NONE, FT_BYTES, FT_ETHER, FT_IPv6, FT_IPXNET, FT_OID, FT_REL_OID,
1492 FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FCWWN fields, and 'protocol' fields
1493 the encoding is not relevant; the 'encoding' argument should be
1494 ENC_NA (Not Applicable).
1496 For FT_UINT_BYTES fields, the byte order of the count must be specified
1497 as well as the 'encoding' for bytes which should be ENC_NA,
1498 i.e. ENC_LITTLE_ENDIAN|ENC_NA
1500 For integral, floating-point, Boolean, FT_GUID, and FT_EUI64 fields,
1501 the encoding specifies the byte order of the value; the 'encoding'
1502 argument should be ENC_LITTLE_ENDIAN if the value is little-endian
1503 and ENC_BIG_ENDIAN if it is big-endian.
1505 For FT_IPv4 fields, the encoding also specifies the byte order of the
1506 value. In almost all cases, the encoding is in network byte order,
1507 hence big-endian, but in at least one protocol dissected by Wireshark,
1508 at least one IPv4 address is byte-swapped, so it's in little-endian
1511 For string fields, the encoding specifies the character set used for the
1512 string and the way individual code points in that character set are
1513 encoded. For FT_UINT_STRING fields, the byte order of the count must be
1514 specified. For UTF-16, UCS-2, and UCS-4, the byte order of the encoding
1515 must be specified, and optionally ENC_BOM can also be indicated to detect
1516 an initial BYTE ORDER MARK (the specified value is used if the field does
1517 not begin with a BOM.) For counted UTF-16, UCS-2, and UCS-4 strings, the
1518 byte order of the count and the characters in the string must be the same,
1519 unless a BOM overrides the value for the characters. In other cases the
1520 string encoding has no endianness or the endianness is implicitly specified
1521 and nothing should be used. The character encodings that are currently
1524 ENC_ASCII - ASCII (currently treated as UTF-8; in the future,
1525 all bytes with the 8th bit set will be treated as
1527 ENC_UTF_8 - UTF-8-encoded Unicode
1528 ENC_UTF_16 - UTF-16-encoded Unicode, with surrogate pairs
1529 ENC_UCS_2 - UCS-2-encoded subset of Unicode, with no surrogate pairs
1530 and thus no code points above 0xFFFF
1531 ENC_UCS_4 - UCS-4-encoded Unicode (aka UTF-32)
1532 ENC_WINDOWS_1250 - Windows-1250 code page
1533 ENC_WINDOWS_1251 - Windows-1251 code page
1534 ENC_WINDOWS_1252 - Windows-1252 code page
1535 ENC_ISO_646_BASIC - ISO 646 "basic code table"
1536 ENC_ISO_8859_1 - ISO 8859-1
1537 ENC_ISO_8859_2 - ISO 8859-2
1538 ENC_ISO_8859_3 - ISO 8859-3
1539 ENC_ISO_8859_4 - ISO 8859-4
1540 ENC_ISO_8859_5 - ISO 8859-5
1541 ENC_ISO_8859_6 - ISO 8859-6
1542 ENC_ISO_8859_7 - ISO 8859-7
1543 ENC_ISO_8859_8 - ISO 8859-8
1544 ENC_ISO_8859_9 - ISO 8859-9
1545 ENC_ISO_8859_10 - ISO 8859-10
1546 ENC_ISO_8859_11 - ISO 8859-11
1547 ENC_ISO_8859_13 - ISO 8859-13
1548 ENC_ISO_8859_14 - ISO 8859-14
1549 ENC_ISO_8859_15 - ISO 8859-15
1550 ENC_ISO_8859_16 - ISO 8859-16
1551 ENC_3GPP_TS_23_038_7BITS - GSM 7 bits alphabet as described
1553 ENC_3GPP_TS_23_038_7BITS_UNPACKED - GSM 7 bits alphabet where each
1554 7 bit character occupies a distinct octet
1555 ENC_ETSI_TS_102_221_ANNEX_A - Coding scheme for SIM cards with GSM 7 bit
1556 alphabet, UCS-2 characters, or a mixture of the two as described
1557 in ETSI TS 102 221 Annex A
1559 ENC_EBCDIC_CP037 - EBCDIC code page 037
1560 ENC_EBCDIC_CP500 - EBCDIC code page 500
1561 ENC_MAC_ROMAN - MAC ROMAN
1562 ENC_CP437 - DOS code page 437
1563 ENC_CP855 - DOS code page 855
1564 ENC_CP866 - DOS code page 866
1565 ENC_ASCII_7BITS - 7 bits ASCII
1567 ENC_BCD_DIGITS_0_9 - packed BCD (one digit per nibble), digits 0-9
1568 ENC_KEYPAD_ABC_TBCD - keypad-with-a/b/c "telephony packed BCD" = 0-9, *, #, a, b, c
1569 ENC_KEYPAD_BC_TBCD - keypad-with-B/C "telephony packed BCD" = 0-9, B, C, *, #
1570 ENC_GB18030 - GB 18030
1572 ENC_DECT_STANDARD_8BITS - DECT standard 8 bit character set as defined in
1574 ENC_DECT_STANDARD_4BITS_TBCD - DECT standard 4 bit character set "telephony
1575 packet BCD" = 0-9, 0xb = SPACE
1577 Other encodings will be added in the future.
1579 For FT_ABSOLUTE_TIME fields, the encoding specifies the form in which
1580 the time stamp is specified, as well as its byte order. The time stamp
1581 encodings that are currently supported are:
1583 ENC_TIME_SECS_NSECS - 8, 12, or 16 bytes. For 8 bytes, the first 4
1584 bytes are seconds and the next 4 bytes are nanoseconds; for 12
1585 bytes, the first 8 bytes are seconds and the next 4 bytes are
1586 nanoseconds; for 16 bytes, the first 8 bytes are seconds and
1587 the next 8 bytes are nanoseconds. The seconds are seconds
1588 since the UN*X epoch (1970-01-01 00:00:00 UTC). (I.e., a UN*X
1589 struct timespec with a 4-byte or 8-byte time_t or a structure
1590 with an 8-byte time_t and an 8-byte nanoseconds field.)
1592 ENC_TIME_NTP - 8 bytes; the first 4 bytes are seconds since the NTP
1593 epoch (1900-01-01 00:00:00 GMT) and the next 4 bytes are 1/2^32's of
1594 a second since that second. (I.e., a 64-bit count of 1/2^32's of a
1595 second since the NTP epoch, with the upper 32 bits first and the
1596 lower 32 bits second, even when little-endian.)
1598 ENC_TIME_TOD - 8 bytes, as a count of microseconds since the System/3x0
1599 and z/Architecture epoch (1900-01-01 00:00:00 GMT).
1601 ENC_TIME_RTPS - 8 bytes; the first 4 bytes are seconds since the UN*X
1602 epoch and the next 4 bytes are 1/2^32's of a second since that
1603 second. (I.e., it's the offspring of a mating between UN*X time and
1604 NTP time). It's used by the Object Management Group's Real-Time
1605 Publish-Subscribe Wire Protocol for the Data Distribution Service.
1607 ENC_TIME_SECS_USECS - 8 bytes; the first 4 bytes are seconds since the
1608 UN*X epoch and the next 4 bytes are microseconds since that
1609 second. (I.e., a UN*X struct timeval with a 4-byte time_t.)
1611 ENC_TIME_SECS - 4 to 8 bytes, representing a value in seconds since
1614 ENC_TIME_MSECS - 6 to 8 bytes, representing a value in milliseconds
1615 since the UN*X epoch.
1617 ENC_TIME_USECS - 8 bytes, representing a value in microseconds since
1620 ENC_TIME_NSECS - 8 bytes, representing a value in nanoseconds since
1623 ENC_TIME_SECS_NTP - 4 bytes, representing a count of seconds since
1626 ENC_TIME_RFC_3971 - 8 bytes, representing a count of 1/64ths of a
1627 second since the UN*X epoch; see section 5.3.1 "Timestamp Option"
1630 ENC_TIME_MSEC_NTP - 4-8 bytes, representing a count of milliseconds since
1633 ENC_TIME_MIP6 - 8 bytes; the first 48 bits are seconds since the UN*X epoch
1634 and the remaining 16 bits indicate the number of 1/65536's of a second
1637 ENC_TIME_CLASSIC_MAC_OS_SECS - 4-8 bytes, representing a count of seconds
1638 since January 1, 1904, 00:00:00 UTC.
1640 For FT_RELATIVE_TIME fields, the encoding specifies the form in which
1641 the time stamp is specified, as well as its byte order. The time stamp
1642 encodings that are currently supported are:
1644 ENC_TIME_SECS_NSECS - 8, 12, or 16 bytes. For 8 bytes, the first 4
1645 bytes are seconds and the next 4 bytes are nanoseconds; for 12
1646 bytes, the first 8 bytes are seconds and the next 4 bytes are
1647 nanoseconds; for 16 bytes, the first 8 bytes are seconds and
1648 the next 8 bytes are nanoseconds.
1650 ENC_TIME_SECS_USECS - 8 bytes; the first 4 bytes are seconds and the
1651 next 4 bytes are microseconds.
1653 ENC_TIME_SECS - 4 to 8 bytes, representing a value in seconds.
1655 ENC_TIME_MSECS - 6 to 8 bytes, representing a value in milliseconds.
1657 ENC_TIME_USECS - 8 bytes, representing a value in microseconds.
1659 ENC_TIME_NSECS - 8 bytes, representing a value in nanoseconds.
1661 For other types, there is no support for proto_tree_add_item().
1663 Now that definitions of fields have detailed information about bitfield
1664 fields, you can use proto_tree_add_item() with no extra processing to
1665 add bitfield values to your tree. Here's an example. Take the Format
1666 Identifier (FID) field in the Transmission Header (TH) portion of the SNA
1667 protocol. The FID is the high nibble of the first byte of the TH. The
1668 FID would be registered like this:
1670 name = "Format Identifier"
1671 abbrev = "sna.th.fid"
1674 strings = sna_th_fid_vals
1677 The bitmask contains the value which would leave only the FID if bitwise-ANDed
1678 against the parent field, the first byte of the TH.
1680 The code to add the FID to the tree would be;
1682 proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1,
1685 The definition of the field already has the information about bitmasking
1686 and bitshifting, so it does the work of masking and shifting for us!
1687 This also means that you no longer have to create value_string structs
1688 with the values bitshifted. The value_string for FID looks like this,
1689 even though the FID value is actually contained in the high nibble.
1690 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
1692 /* Format Identifier */
1693 static const value_string sna_th_fid_vals[] = {
1694 { 0x0, "SNA device <--> Non-SNA Device" },
1695 { 0x1, "Subarea Node <--> Subarea Node" },
1696 { 0x2, "Subarea Node <--> PU2" },
1697 { 0x3, "Subarea Node or SNA host <--> Subarea Node" },
1700 { 0xf, "Adjacent Subarea Nodes" },
1704 The final implication of this is that display filters work the way you'd
1705 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
1706 Subarea Nodes. The user does not have to shift the value of the FID to
1707 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
1710 proto_tree_add_item_ret_XXX()
1711 ------------------------------
1712 proto_tree_add_item_ret_XXX is used when you want the displayed value returned
1713 for further processing only integer and unsigned integer types up to 32 bits are
1714 supported usage of proper FT_ is checked.
1716 proto_tree_add_XXX_item()
1717 ---------------------
1718 proto_tree_add_XXX_item is used when you wish to do no special formatting,
1719 but also either wish for the retrieved value from the tvbuff to be handed
1720 back (to avoid doing tvb_get_...), and/or wish to have the value be decoded
1721 from the tvbuff in a string-encoded format.
1723 The item added to the GUI tree will contain the name (as passed in the
1724 proto_register_*() function) and a value. The value will be fetched
1725 from the tvbuff, based on the type of the XXX name and the encoding of
1726 the value as specified by the "encoding" argument.
1728 This function retrieves the value even if the passed-in tree param is NULL,
1729 so that it can be used by dissectors at all times to both get the value
1730 and set the tree item to it.
1732 Like other proto_tree_add functions, if there is a tree and the value cannot
1733 be decoded from the tvbuff, then an expert info error is reported. For string
1734 encoding, this means that a failure to decode the hex value from the string
1735 results in an expert info error being added to the tree.
1737 For string-decoding, the passed-in encoding argument needs to specify the
1738 string encoding (e.g., ENC_ASCII, ENC_UTF_8) as well as the format. For
1739 some XXX types, the format is constrained - for example for the encoding format
1740 for proto_tree_add_time_item() can only be one of the ENC_ISO_8601_* ones
1741 or ENC_IMF_DATE_TIME. For proto_tree_add_bytes_item() if ENC_STR_HEX is used
1742 it must be bit-or'ed with one or more of the ENC_SEP_* separator types.
1744 proto_tree_add_protocol_format()
1745 --------------------------------
1746 proto_tree_add_protocol_format is used to add the top-level item for the
1747 protocol when the dissector routine wants complete control over how the
1748 field and value will be represented on the GUI tree. The ID value for
1749 the protocol is passed in as the "id" argument; the rest of the
1750 arguments are a "printf"-style format and any arguments for that format.
1751 The caller must include the name of the protocol in the format; it is
1752 not added automatically as in proto_tree_add_item().
1754 proto_tree_add_none_format()
1755 ----------------------------
1756 proto_tree_add_none_format is used to add an item of type FT_NONE.
1757 The caller must include the name of the field in the format; it is
1758 not added automatically as in proto_tree_add_item().
1760 proto_tree_add_bytes()
1761 proto_tree_add_time()
1762 proto_tree_add_ipxnet()
1763 proto_tree_add_ipv4()
1764 proto_tree_add_ipv6()
1765 proto_tree_add_ether()
1766 proto_tree_add_string()
1767 proto_tree_add_boolean()
1768 proto_tree_add_float()
1769 proto_tree_add_double()
1770 proto_tree_add_uint()
1771 proto_tree_add_uint64()
1772 proto_tree_add_int()
1773 proto_tree_add_int64()
1774 proto_tree_add_guid()
1775 proto_tree_add_oid()
1776 proto_tree_add_eui64()
1777 ------------------------
1778 These routines are used to add items to the protocol tree if either:
1780 the value of the item to be added isn't just extracted from the
1781 packet data, but is computed from data in the packet;
1783 the value was fetched into a variable.
1785 The 'value' argument has the value to be added to the tree.
1787 NOTE: in all cases where the 'value' argument is a pointer, a copy is
1788 made of the object pointed to; if you have dynamically allocated a
1789 buffer for the object, that buffer will not be freed when the protocol
1790 tree is freed - you must free the buffer yourself when you don't need it
1793 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
1797 proto_tree_add_bytes_with_length() is similar to proto_tree_add_bytes,
1798 except that the length is not derived from the tvb length. Instead,
1799 the displayed data size is controlled by 'ptr_length'.
1801 For proto_tree_add_bytes_format() and proto_tree_add_bytes_format_value(), the
1802 'value_ptr' argument is a pointer to a sequence of bytes or NULL if the bytes
1803 should be taken from the given TVB using the given offset and length.
1805 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
1806 "nstime_t", which is a structure containing the time to be added; it has
1807 'secs' and 'nsecs' members, giving the integral part and the fractional
1808 part of a time in units of seconds, with 'nsecs' being the number of
1809 nanoseconds. For absolute times, "secs" is a UNIX-style seconds since
1810 January 1, 1970, 00:00:00 GMT value.
1812 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
1815 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
1816 address, in network byte order.
1818 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
1819 128-bit IPv6 address.
1821 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
1824 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
1825 text string; this string must be NULL terminated even if the string in the
1826 TVB is not (as may be the case with FT_STRINGs).
1828 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer.
1829 It is masked and shifted as defined by the field info after which zero
1830 means "false", and non-zero means "true".
1832 For proto_tree_add_float(), the 'value' argument is a 'float' in the
1833 host's floating-point format.
1835 For proto_tree_add_double(), the 'value' argument is a 'double' in the
1836 host's floating-point format.
1838 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
1839 integer value, in host byte order. (This routine cannot be used to add
1842 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
1843 integer value, in host byte order.
1845 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
1846 integer value, in host byte order. (This routine cannot be used to add
1849 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
1850 integer value, in host byte order.
1852 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
1855 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
1856 ASN.1 Object Identifier.
1858 For proto_tree_add_eui64(), the 'value' argument is a 64-bit unsigned
1859 integer value, in host byte order.
1861 proto_tree_add_bytes_format()
1862 proto_tree_add_time_format()
1863 proto_tree_add_ipxnet_format()
1864 proto_tree_add_ipv4_format()
1865 proto_tree_add_ipv6_format()
1866 proto_tree_add_ether_format()
1867 proto_tree_add_string_format()
1868 proto_tree_add_boolean_format()
1869 proto_tree_add_float_format()
1870 proto_tree_add_double_format()
1871 proto_tree_add_uint_format()
1872 proto_tree_add_uint64_format()
1873 proto_tree_add_int_format()
1874 proto_tree_add_int64_format()
1875 proto_tree_add_guid_format()
1876 proto_tree_add_oid_format()
1877 proto_tree_add_eui64_format()
1878 ----------------------------
1879 These routines are used to add items to the protocol tree when the
1880 dissector routine wants complete control over how the field and value
1881 will be represented on the GUI tree. The argument giving the value is
1882 the same as the corresponding proto_tree_add_XXX() function; the rest of
1883 the arguments are a "printf"-style format and any arguments for that
1884 format. The caller must include the name of the field in the format; it
1885 is not added automatically as in the proto_tree_add_XXX() functions.
1887 proto_tree_add_bytes_format_value()
1888 proto_tree_add_time_format_value()
1889 proto_tree_add_ipxnet_format_value()
1890 proto_tree_add_ipv4_format_value()
1891 proto_tree_add_ipv6_format_value()
1892 proto_tree_add_ether_format_value()
1893 proto_tree_add_string_format_value()
1894 proto_tree_add_boolean_format_value()
1895 proto_tree_add_float_format_value()
1896 proto_tree_add_double_format_value()
1897 proto_tree_add_uint_format_value()
1898 proto_tree_add_uint64_format_value()
1899 proto_tree_add_int_format_value()
1900 proto_tree_add_int64_format_value()
1901 proto_tree_add_guid_format_value()
1902 proto_tree_add_oid_format_value()
1903 proto_tree_add_eui64_format_value()
1904 ------------------------------------
1906 These routines are used to add items to the protocol tree when the
1907 dissector routine wants complete control over how the value will be
1908 represented on the GUI tree. The argument giving the value is the same
1909 as the corresponding proto_tree_add_XXX() function; the rest of the
1910 arguments are a "printf"-style format and any arguments for that format.
1911 With these routines, unlike the proto_tree_add_XXX_format() routines,
1912 the name of the field is added automatically as in the
1913 proto_tree_add_XXX() functions; only the value is added with the format.
1914 One use case for this would be to add a unit of measurement string to
1915 the value of the field, however using BASE_UNIT_STRING in the hf_
1916 definition is now preferred.
1918 proto_tree_add_checksum()
1919 ----------------------------
1920 proto_tree_add_checksum is used to add a checksum field. The hf field
1921 provided must be the correct size of the checksum (FT_UINT, FT_UINT16,
1922 FT_UINT32, etc). Additional parameters are there to provide "status"
1923 and expert info depending on whether the checksum matches the provided
1924 value. The "status" and expert info can be used in cases except
1925 where PROTO_CHECKSUM_NO_FLAGS is used.
1927 proto_tree_add_subtree()
1928 ---------------------
1929 proto_tree_add_subtree() is used to add a label to the GUI tree and create
1930 a subtree for other fields. It will contain no value, so it is not searchable
1931 in the display filter process.
1933 This should only be used for items with subtrees, which may not
1934 have values themselves - the items in the subtree are the ones with values.
1936 For a subtree, the label on the subtree might reflect some of the items
1937 in the subtree. This means the label can't be set until at least some
1938 of the items in the subtree have been dissected. To do this, use
1939 'proto_item_set_text()' or 'proto_item_append_text()':
1942 proto_item_set_text(proto_item *ti, ...);
1945 proto_item_append_text(proto_item *ti, ...);
1947 'proto_item_set_text()' takes as an argument the proto_item value returned by
1948 one of the parameters in 'proto_tree_add_subtree()', a 'printf'-style format
1949 string, and a set of arguments corresponding to '%' format items in that string,
1950 and replaces the text for the item created by 'proto_tree_add_subtree()' with the result
1951 of applying the arguments to the format string.
1953 'proto_item_append_text()' is similar, but it appends to the text for
1954 the item the result of applying the arguments to the format string.
1956 For example, early in the dissection, one might do:
1958 subtree = proto_tree_add_subtree(tree, tvb, offset, length, ett, &ti, <label>);
1962 proto_item_set_text(ti, "%s: %s", type, value);
1964 after the "type" and "value" fields have been extracted and dissected.
1965 <label> would be a label giving what information about the subtree is
1966 available without dissecting any of the data in the subtree.
1968 Note that an exception might be thrown when trying to extract the values of
1969 the items used to set the label, if not all the bytes of the item are
1970 available. Thus, one should create the item with text that is as
1971 meaningful as possible, and set it or append additional information to
1972 it as the values needed to supply that information are extracted.
1974 proto_tree_add_subtree_format()
1975 ----------------------------
1976 This is like proto_tree_add_subtree(), but uses printf-style arguments to
1977 create the label; it is used to allow routines that take a printf-like
1978 variable-length list of arguments to add a text item to the protocol
1981 proto_tree_add_bits_item()
1982 --------------------------
1983 Adds a number of bits to the protocol tree which does not have to be byte
1984 aligned. The offset and length is in bits.
1987 ..10 1010 10.. .... "value" (formatted as FT_ indicates).
1989 proto_tree_add_bits_ret_val()
1990 -----------------------------
1991 Works in the same way but also returns the value of the read bits.
1993 proto_tree_add_split_bits_item_ret_val()
1994 -----------------------------------
1995 Similar, but is used for items that are made of 2 or more smaller sets of bits (crumbs)
1996 which are not contiguous, but are concatenated to form the actual value. The size of
1997 the crumbs and the order of assembly are specified in an array of crumb_spec structures.
1999 proto_tree_add_split_bits_crumb()
2000 ---------------------------------
2001 Helper function for the above, to add text for each crumb as it is encountered.
2003 proto_tree_add_ts_23_038_7bits_item()
2004 -------------------------------------
2005 Adds a string of a given number of characters and encoded according to 3GPP TS 23.038 7 bits
2008 proto_tree_add_bitmask() et al.
2009 -------------------------------
2010 These functions provide easy to use and convenient dissection of many types of common
2011 bitmasks into individual fields.
2013 header is an integer type and must be of type FT_[U]INT{8|16|24|32||40|48|56|64} and
2014 represents the entire dissectable width of the bitmask.
2016 'header' and 'ett' are the hf fields and ett field respectively to create an
2017 expansion that covers the bytes of the bitmask.
2019 'fields' is a NULL terminated array of pointers to hf fields representing
2020 the individual subfields of the bitmask. These fields must either be integers
2021 (usually of the same byte width as 'header') or of the type FT_BOOLEAN.
2022 Each of the entries in 'fields' will be dissected as an item under the
2023 'header' expansion and also IF the field is a boolean and IF it is set to 1,
2024 then the name of that boolean field will be printed on the 'header' expansion
2025 line. For integer type subfields that have a value_string defined, the
2026 matched string from that value_string will be printed on the expansion line
2029 Example: (from the SCSI dissector)
2030 static int hf_scsi_inq_peripheral;
2031 static int hf_scsi_inq_qualifier;
2032 static int hf_scsi_inq_devtype;
2034 static int ett_scsi_inq_peripheral;
2036 static int * const peripheral_fields[] = {
2037 &hf_scsi_inq_qualifier,
2038 &hf_scsi_inq_devtype,
2042 /* Qualifier and DeviceType */
2043 proto_tree_add_bitmask(tree, tvb, offset, hf_scsi_inq_peripheral,
2044 ett_scsi_inq_peripheral, peripheral_fields, ENC_BIG_ENDIAN);
2047 { &hf_scsi_inq_peripheral,
2048 {"Peripheral", "scsi.inquiry.peripheral", FT_UINT8, BASE_HEX,
2049 NULL, 0, NULL, HFILL}
2051 { &hf_scsi_inq_qualifier,
2052 {"Qualifier", "scsi.inquiry.qualifier", FT_UINT8, BASE_HEX,
2053 VALS (scsi_qualifier_val), 0xE0, NULL, HFILL}
2055 { &hf_scsi_inq_devtype,
2056 {"Device Type", "scsi.inquiry.devtype", FT_UINT8, BASE_HEX,
2057 VALS (scsi_devtype_val), SCSI_DEV_BITS, NULL, HFILL}
2061 Which provides very pretty dissection of this one byte bitmask.
2063 Peripheral: 0x05, Qualifier: Device type is connected to logical unit, Device Type: CD-ROM
2064 000. .... = Qualifier: Device type is connected to logical unit (0x00)
2065 ...0 0101 = Device Type: CD-ROM (0x05)
2067 The proto_tree_add_bitmask_text() function is an extended version of
2068 the proto_tree_add_bitmask() function. In addition, it allows to:
2069 - Provide a leading text (e.g. "Flags: ") that will appear before
2070 the comma-separated list of field values
2071 - Provide a fallback text (e.g. "None") that will be appended if
2072 no fields warranted a change to the top-level title.
2073 - Using flags, specify which fields will affect the top-level title.
2075 There are the following flags defined:
2077 BMT_NO_APPEND - the title is taken "as-is" from the 'name' argument.
2078 BMT_NO_INT - only boolean flags are added to the title.
2079 BMT_NO_FALSE - boolean flags are only added to the title if they are set.
2080 BMT_NO_TFS - only add flag name to the title, do not use true_false_string
2082 The proto_tree_add_bitmask_with_flags() function is an extended version
2083 of the proto_tree_add_bitmask() function. It allows using flags to specify
2084 which fields will affect the top-level title. The flags are the
2085 same BMT_NO_* flags as used in the proto_tree_add_bitmask_text() function.
2087 The proto_tree_add_bitmask() behavior can be obtained by providing
2088 both 'name' and 'fallback' arguments as NULL, and a flags of
2089 (BMT_NO_FALSE|BMT_NO_TFS).
2091 The proto_tree_add_bitmask_len() function is intended for protocols where
2092 bitmask length is permitted to vary, so a length is specified explicitly
2093 along with the bitmask value. USB Video "bmControl" and "bControlSize"
2094 fields follow this pattern. The primary intent of this is "forward
2095 compatibility," enabling an interpreter coded for version M of a structure
2096 to comprehend fields in version N of the structure, where N > M and
2097 bControlSize increases from version M to version N.
2099 proto_tree_add_bitmask_len() is an extended version of proto_tree_add_bitmask()
2100 that uses an explicitly specified (rather than inferred) length to control
2101 dissection. Because of this, it may encounter two cases that
2102 proto_tree_add_bitmask() and proto_tree_add_bitmask_text() may not:
2103 - A length that exceeds that of the 'header' and bitmask subfields.
2104 In this case the least-significant bytes of the bitmask are dissected.
2105 An expert warning is generated in this case, because the dissection code
2106 likely needs to be updated for a new revision of the protocol.
2107 - A length that is shorter than that of the 'header' and bitmask subfields.
2108 In this case, subfields whose data is fully present are dissected,
2109 and other subfields are not. No warning is generated in this case,
2110 because the dissection code is likely for a later revision of the protocol
2111 than the packet it was called to interpret.
2114 proto_item_set_generated()
2115 --------------------------
2116 proto_item_set_generated is used to mark fields as not being read from the
2117 captured data directly, but inferred from one or more values.
2119 One of the primary uses of this is the presentation of verification of
2120 checksums. Every IP packet has a checksum line, which can present the result
2121 of the checksum verification, if enabled in the preferences. The result is
2122 presented as a subtree, where the result is enclosed in square brackets
2123 indicating a generated field.
2125 Header checksum: 0x3d42 [correct]
2126 [Checksum Status: Good (1)]
2128 proto_item_set_hidden()
2129 -----------------------
2130 proto_item_set_hidden is used to hide fields, which have already been added
2131 to the tree, from being visible in the displayed tree.
2133 NOTE that creating hidden fields is actually quite a bad idea from a UI design
2134 perspective because the user (someone who did not write nor has ever seen the
2135 code) has no way of knowing that hidden fields are there to be filtered on
2136 thus defeating the whole purpose of putting them there. A Better Way might
2137 be to add the fields (that might otherwise be hidden) to a subtree where they
2138 won't be seen unless the user opens the subtree--but they can be found if the
2141 One use for hidden fields (which would be better implemented using visible
2142 fields in a subtree) follows: The caller may want a value to be
2143 included in a tree so that the packet can be filtered on this field, but
2144 the representation of that field in the tree is not appropriate. An
2145 example is the token-ring routing information field (RIF). The best way
2146 to show the RIF in a GUI is by a sequence of ring and bridge numbers.
2147 Rings are 3-digit hex numbers, and bridges are single hex digits:
2149 RIF: 001-A-013-9-C0F-B-555
2151 In the case of RIF, the programmer should use a field with no value and
2152 use proto_tree_add_none_format() to build the above representation. The
2153 programmer can then add the ring and bridge values, one-by-one, with
2154 proto_tree_add_item() and hide them with proto_item_set_hidden() so that the
2155 user can then filter on or search for a particular ring or bridge. Here's a
2156 skeleton of how the programmer might code this.
2159 rif = create_rif_string(...);
2161 proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
2163 for (i = 0; i < num_rings; i++) {
2166 pi = proto_tree_add_item(tree, hf_tr_rif_ring, ..., ENC_BIG_ENDIAN);
2167 proto_item_set_hidden(pi);
2169 for (i = 0; i < num_rings - 1; i++) {
2172 pi = proto_tree_add_item(tree, hf_tr_rif_bridge, ..., ENC_BIG_ENDIAN);
2173 proto_item_set_hidden(pi);
2176 The logical tree has these items:
2178 hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
2179 hf_tr_rif_ring, hidden, value=0x001
2180 hf_tr_rif_bridge, hidden, value=0xA
2181 hf_tr_rif_ring, hidden, value=0x013
2182 hf_tr_rif_bridge, hidden, value=0x9
2183 hf_tr_rif_ring, hidden, value=0xC0F
2184 hf_tr_rif_bridge, hidden, value=0xB
2185 hf_tr_rif_ring, hidden, value=0x555
2187 GUI or print code will not display the hidden fields, but a display
2188 filter or "packet grep" routine will still see the values. The possible
2189 filter is then possible:
2191 tr.rif_ring eq 0x013
2195 proto_item_set_url is used to mark fields as containing a URL. This can only
2196 be done with fields of type FT_STRING(Z). If these fields are presented they
2197 are underlined, as could be done in a browser. These fields are sensitive to
2198 clicks as well, launching the configured browser with this URL as parameter.
2200 1.6 Utility routines.
2202 1.6.1 val_to_str, val_to_str_const, try_val_to_str and try_val_to_str_idx
2204 A dissector may need to convert a value to a string, using a
2205 'value_string' structure, by hand, rather than by declaring a field with
2206 an associated 'value_string' structure; this might be used, for example,
2207 to generate a COL_INFO line for a frame.
2209 val_to_str() handles the most common case:
2212 val_to_str(uint32_t val, const value_string *vs, const char *fmt)
2214 If the value 'val' is found in the 'value_string' table pointed to by
2215 'vs', 'val_to_str' will return the corresponding string; otherwise, it
2216 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
2217 to generate a string, and will return a pointer to that string.
2218 You can use it in a call to generate a COL_INFO line for a frame such as
2220 col_add_fstr(COL_INFO, ", %s", val_to_str(val, table, "Unknown %d"));
2222 If you don't need to display 'val' in your fmt string, you can use
2223 val_to_str_const() which just takes a string constant instead and returns it
2224 unmodified when 'val' isn't found.
2226 If you need to handle the failure case in some custom way, try_val_to_str()
2227 will return NULL if val isn't found:
2230 try_val_to_str(uint32_t val, const value_string *vs)
2232 Note that, you must check whether 'try_val_to_str()' returns NULL, and arrange
2233 that its return value not be dereferenced if it's NULL. 'try_val_to_str_idx()'
2234 behaves similarly, except it also returns an index into the value_string array,
2235 or -1 if 'val' was not found.
2237 The *_ext functions are "extended" versions of those already described. They
2238 should be used for large value-string arrays which contain many entries. They
2239 implement value to string conversions which will do either a direct access or
2240 a binary search of the value string array if possible. See
2241 "Extended Value Strings" under section 1.6 "Constructing the protocol tree" for
2244 See epan/value_string.h for detailed information on the various value_string
2247 To handle 64-bit values, there are an equivalent set of functions. These are:
2250 val64_to_str(const uint64_t val, const val64_string *vs, const char *fmt)
2253 val64_to_str_const(const uint64_t val, const val64_string *vs, const char *unknown_str);
2256 try_val64_to_str(const uint64_t val, const val64_string *vs);
2259 try_val64_to_str_idx(const uint64_t val, const val64_string *vs, int *idx);
2262 1.6.2 rval_to_str, try_rval_to_str and try_rval_to_str_idx
2264 A dissector may need to convert a range of values to a string, using a
2265 'range_string' structure.
2267 Most of the same functions exist as with regular value_strings (see section
2268 1.6.1) except with the names 'rval' instead of 'val'.
2271 1.7 Calling Other Dissectors.
2273 As each dissector completes its portion of the protocol analysis, it
2274 is expected to create a new tvbuff of type TVBUFF_SUBSET which
2275 contains the payload portion of the protocol (that is, the bytes
2276 that are relevant to the next dissector).
2278 To create a new TVBUFF_SUBSET that begins at a specified offset in a
2279 parent tvbuff, and runs to the end of the parent tvbuff, the routine
2280 tvbuff_new_subset_remaining() is used:
2282 next_tvb = tvb_new_subset_remaining(tvb, offset);
2285 tvb is the tvbuff that the dissector has been working on. It
2286 can be a tvbuff of any type.
2288 next_tvb is the new TVBUFF_SUBSET.
2290 offset is the byte offset of 'tvb' at which the new tvbuff
2291 should start. The first byte is the byte at offset 0.
2293 To create a new TVBUFF_SUBSET that begins at a specified offset in a
2294 parent tvbuff, with a specified number of bytes in the payload, the
2295 routine tvbuff_new_subset_length() is used:
2297 next_tvb = tvb_new_subset_length(tvb, offset, reported_length);
2300 tvb is the tvbuff that the dissector has been working on. It
2301 can be a tvbuff of any type.
2303 next_tvb is the new TVBUFF_SUBSET.
2305 offset is the byte offset of 'tvb' at which the new tvbuff
2306 should start. The first byte is the byte at offset 0.
2308 reported_length is the number of bytes that the current protocol
2309 says should be in the payload.
2311 In the few cases where the number of bytes available in the new subset
2312 must be explicitly specified, rather than being calculated based on the
2313 number of bytes in the payload, the routine tvb_new_subset_length_caplen()
2316 next_tvb = tvb_new_subset_length_caplen(tvb, offset, length, reported_length);
2319 tvb is the tvbuff that the dissector has been working on. It
2320 can be a tvbuff of any type.
2322 next_tvb is the new TVBUFF_SUBSET.
2324 offset is the byte offset of 'tvb' at which the new tvbuff
2325 should start. The first byte is the byte at offset 0.
2327 length is the number of bytes in the new TVBUFF_SUBSET. A length
2328 argument of -1 says to use as many bytes as are available in
2331 reported_length is the number of bytes that the current protocol
2332 says should be in the payload. A reported_length of -1 says that
2333 the protocol doesn't say anything about the size of its payload.
2335 To call a dissector you need to get the handle of the dissector using
2336 find_dissector(), passing it the string name of the dissector. The setting
2337 of the handle is usually done once at startup during the proto_reg_handoff
2338 function within the calling dissector.
2340 1.7.1 Dissector Tables
2342 Another way to call a subdissector is to setup a dissector table. A dissector
2343 table is a list of subdissectors grouped by a common identifier (integer or
2344 string) in a dissector. Subdissectors will register themselves with the dissector
2345 table using their unique identifier using one of the following APIs:
2347 void dissector_add_uint(const char *abbrev, const uint32_t pattern,
2348 dissector_handle_t handle);
2350 void dissector_add_uint_range(const char *abbrev, struct epan_range *range,
2351 dissector_handle_t handle);
2353 void dissector_add_string(const char *name, const char *pattern,
2354 dissector_handle_t handle);
2356 void dissector_add_for_decode_as(const char *name,
2357 dissector_handle_t handle);
2359 dissector_add_for_decode_as doesn't add a unique identifier in the dissector
2360 table, but it lets the user add it from the command line or, in Wireshark,
2361 through the "Decode As" UI.
2363 Then when the dissector hits the common identifier field, it will use one of the
2364 following APIs to invoke the subdissector:
2366 int dissector_try_uint(dissector_table_t sub_dissectors,
2367 const uint32_t uint_val, tvbuff_t *tvb, packet_info *pinfo,
2370 int dissector_try_uint_with_data(dissector_table_t sub_dissectors,
2371 const uint32_t uint_val, tvbuff_t *tvb, packet_info *pinfo,
2372 proto_tree *tree, const bool add_proto_name, void *data);
2374 int dissector_try_string_with_data(dissector_table_t sub_dissectors, const char *string,
2375 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, true, void *data);
2377 These pass a subset of the remaining packet (typically the rest of the
2378 packet) for the dissector table to determine which subdissector is called.
2379 This allows dissection of a packet to be expanded outside of dissector without
2380 having to modify the dissector directly.
2383 1.8 Editing CMakeLists.txt to add your dissector.
2385 To arrange that your dissector will be built as part of Wireshark, you
2386 must add the name of the source file for your dissector to the DISSECTOR_SRC
2387 section of epan/dissectors/CMakeLists.txt
2390 1.9 Using the git source code tree.
2392 See <https://www.wireshark.org/develop.html>
2395 1.10 Submitting code for your new dissector.
2397 See <https://www.wireshark.org/docs/wsdg_html_chunked/ChSrcContribute.html>
2398 and <https://wiki.wireshark.org/Development/SubmittingPatches>.
2400 - VERIFY that your dissector code does not use prohibited or deprecated APIs
2402 perl <wireshark_root>/tools/checkAPIs.pl <source-filename(s)>
2404 - VERIFY that your dissector code does not contain any header field related
2406 perl <wireshark_root>/tools/checkhf.pl <source-filename(s)>
2408 - VERIFY that your dissector code does not contain any display filter related
2410 perl <wireshark_root>/tools/checkfiltername.pl <source-filename(s)>
2412 - CHECK your dissector with CppCheck (http://cppcheck.sourceforge.net/) using
2413 Wireshark's customized configuration. This is particularly important on
2414 Windows, since Microsoft's compiler warnings are quite thin:
2415 ./tools/cppcheck/cppcheck.sh <source-filename(s)>
2417 - TEST YOUR DISSECTOR BEFORE SUBMITTING IT.
2418 Use fuzz-test.sh and/or randpkt against your dissector. These are
2419 described at <https://wiki.wireshark.org/FuzzTesting>.
2421 - Subscribe to <mailto:wireshark-dev[AT]wireshark.org> by sending an email to
2422 <mailto:wireshark-dev-request[AT]wireshark.org?body="help"> or visiting
2423 <https://lists.wireshark.org/>.
2425 - 'git diff' to verify all your changes look good.
2427 - 'git add' all the files you changed.
2429 - 'git commit' to commit (locally) your changes. First line of commit message
2430 should be a summary of the changes followed by an empty line and a more
2431 verbose description.
2433 - 'git push downstream HEAD' to push the changes to GitLab. (This assumes
2434 that you have a remote named "downstream" that points to a fork of
2435 https://gitlab.com/wireshark/wireshark.)
2437 - Create a Wiki page on the protocol at <https://gitlab.com/wireshark/editor-wiki>.
2438 (You'll need to request access to https://gitlab.com/wireshark/wiki-editors.)
2439 A template is provided so it is easy to setup in a consistent style.
2440 See: <https://wiki.wireshark.org/HowToEdit>
2441 and <https://wiki.wireshark.org/ProtocolReference>
2443 - If possible, add sample capture files to the sample captures page at
2444 <https://wiki.wireshark.org/SampleCaptures>. These
2445 files are used by the automated build system for fuzz testing.
2447 - If you don't think the wiki is the right place for your sample capture,
2448 submit a bug report to the Wireshark issue database, found at
2449 <https://gitlab.com/wireshark/wireshark/-/issues>, qualified as an
2450 enhancement and attach your sample capture there. Normally a new
2451 dissector won't be accepted without a sample capture! If you open a
2452 bug be sure to cross-link your GitLab merge request.
2454 2. Advanced dissector topics.
2458 Some of the advanced features are being worked on constantly. When using them
2459 it is wise to check the relevant header and source files for additional details.
2461 2.2 Following "conversations".
2463 In Wireshark a conversation is defined as a series of data packets between two
2464 address:port combinations. A conversation is not sensitive to the direction of
2465 the packet. The same conversation will be returned for a packet bound from
2466 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
2468 2.2.1 Conversation Routines
2470 There are nine routines that you will use to work with a conversation:
2471 conversation_new, conversation_new_full, find_conversation,
2472 find_conversation_full, find_or_create_conversation,
2473 conversation_add_proto_data, conversation_get_proto_data,
2474 conversation_delete_proto_data, and conversation_set_dissector.
2476 2.2.1.1 The conversation_init function.
2478 This is an internal routine for the conversation code. As such you
2479 will not have to call this routine. Just be aware that this routine is
2480 called at the start of each capture and before the packets are filtered
2481 with a display filter. The routine will destroy all stored
2482 conversations. This routine does NOT clean up any data pointers that are
2483 passed in the conversation_add_proto_data 'data' variable. You are
2484 responsible for this clean up if you pass a malloc'ed pointer
2487 See item 2.2.1.5 for more information about use of the 'data' pointer.
2490 2.2.1.2 The conversation_new function.
2492 This routine will create a new conversation based upon two address/port
2493 pairs. If you want to associate with the conversation a pointer to a
2494 private data structure you must use the conversation_add_proto_data
2495 function. The ctype variable is used to differentiate between
2496 conversations over different protocols, i.e. TCP and UDP. The options
2497 variable is used to define a conversation that will accept any destination
2498 address and/or port. Set options = 0 if the destination port and address
2499 are known when conversation_new is called. See section 2.4 for more
2500 information on usage of the options parameter.
2502 The conversation_new prototype:
2503 conversation_t *conversation_new(uint32_t setup_frame, address *addr1,
2504 address *addr2, conversation_type ctype, uint32_t port1, uint32_t port2,
2508 uint32_t setup_frame = The lowest numbered frame for this conversation
2509 address* addr1 = first data packet address
2510 address* addr2 = second data packet address
2511 conversation_type ctype = conversation type, defined in conversation.h
2512 uint32_t port1 = first data packet port
2513 uint32_t port2 = second data packet port
2514 unsigned options = conversation options, NO_ADDR2 and/or NO_PORT2
2516 setup_frame indicates the first frame for this conversation, and is used to
2517 distinguish multiple conversations with the same addr1/port1 and addr2/port2
2518 pair that occur within the same capture session.
2520 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
2521 are the second address/port pair. A conversation doesn't have source
2522 and destination address/port pairs - packets in a conversation go in
2523 both directions - so "addr1"/"port1" may be the source or destination
2524 address/port pair; "addr2"/"port2" would be the other pair.
2526 If NO_ADDR2 is specified, the conversation is set up so that a
2527 conversation lookup will match only the "addr1" address; if NO_PORT2 is
2528 specified, the conversation is set up so that a conversation lookup will
2529 match only the "port1" port; if both are specified, i.e.
2530 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
2531 match only the "addr1"/"port1" address/port pair. This can be used if a
2532 packet indicates that, later in the capture, a conversation will be
2533 created using certain addresses and ports, in the case where the packet
2534 doesn't specify the addresses and ports of both sides.
2536 2.2.1.3 The conversation_new_full function.
2538 This routine will create a new conversation based upon an arbitrary
2539 lists of elements. Elements can be addresses, strings, unsigned
2540 integers, or unsigned 64-bit integers. Unlike conversation_new, element
2541 lists are matched strictly; wildcards aren't (yet) supported.
2543 The conversation_new_full prototype:
2544 conversation_t *conversation_new_full(const uint32_t setup_frame,
2545 conversation_element_t *elements);
2548 uint32_t setup_frame = The lowest numbered frame for
2550 conversation_element_t *elements = An array of data types and
2551 values which identify this conversation. The array MUST be
2552 terminated with a CE_ENDPOINT element.
2554 2.2.1.4 The find_conversation function.
2556 Call this routine to look up a conversation. If no conversation is found,
2557 the routine will return a NULL value.
2559 The find_conversation prototype:
2561 conversation_t *find_conversation(uint32_t frame_num, address *addr_a,
2562 address *addr_b, conversation_type ctype, uint32_t port_a, uint32_t port_b,
2566 uint32_t frame_num = a frame number to match
2567 address* addr_a = first address
2568 address* addr_b = second address
2569 conversation_type ctype = conversation type
2570 uint32_t port_a = first data packet port
2571 uint32_t port_b = second data packet port
2572 unsigned options = conversation options, NO_ADDR_B and/or NO_PORT_B
2574 frame_num is a frame number to match. The conversation returned is where
2575 (frame_num >= conversation->setup_frame
2576 && frame_num < conversation->next->setup_frame)
2577 Suppose there are a total of 3 conversations (A, B, and C) that match
2578 addr_a/port_a and addr_b/port_b, where the setup_frame used in
2579 conversation_new() for A, B and C are 10, 50, and 100 respectively. The
2580 frame_num passed in find_conversation is compared to the setup_frame of each
2581 conversation. So if (frame_num >= 10 && frame_num < 50), conversation A is
2582 returned. If (frame_num >= 50 && frame_num < 100), conversation B is returned.
2583 If (frame_num >= 100) conversation C is returned.
2585 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
2586 "port_b" are the second address/port pair. Again, as a conversation
2587 doesn't have source and destination address/port pairs, so
2588 "addr_a"/"port_a" may be the source or destination address/port pair;
2589 "addr_b"/"port_b" would be the other pair. The search will match the
2590 "a" address/port pair against both the "1" and "2" address/port pairs,
2591 and match the "b" address/port pair against both the "2" and "1"
2592 address/port pairs; you don't have to worry about which side the "a" or
2593 "b" pairs correspond to.
2595 If the NO_ADDR_B flag was specified to "find_conversation()", the
2596 "addr_b" address will be treated as matching any "wildcarded" address;
2597 if the NO_PORT_B flag was specified, the "port_b" port will be treated
2598 as matching any "wildcarded" port. If both flags are specified, i.e.
2599 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
2600 any "wildcarded" address and the "port_b" port will be treated as
2601 matching any "wildcarded" port.
2603 2.2.1.5 The find_conversation_full function.
2605 Call this routine to look up a conversation based on an element list. If
2606 no conversation is found, the routine will return a NULL value.
2608 The find_conversation_full prototype:
2610 conversation_t *find_conversation_full(uint32_t frame_num,
2611 conversation_element_t *elements);
2614 uint32_t setup_frame = The lowest numbered frame for
2616 conversation_element_t *elements = An array of data types and
2617 values which identify this conversation. The array MUST be
2618 terminated with a CE_ENDPOINT element.
2620 2.2.1.6 The find_conversation_pinfo function.
2622 This convenience function will find an existing conversation (by calling
2623 find_conversation())
2625 The find_conversation_pinfo prototype:
2627 extern conversation_t *find_conversation_pinfo(packet_info *pinfo,
2628 const unsigned options);
2631 packet_info *pinfo = the packet_info structure
2632 const unsigned options = conversation options, NO_ADDR_B and/or NO_PORT_B
2634 The frame number and the addresses necessary for find_conversation() are
2635 taken from the addresses and ports in the pinfo structure,
2636 pinfo->conv_addr_port_endpoints if pinfo->use_conv_addr_port_endpoints is set,
2637 or pinfo->conv_elements if it is set.
2639 2.2.1.7 The find_or_create_conversation function.
2641 This convenience function will find an existing conversation (by calling
2642 find_conversation()) and, if a conversation does not already exist, create a
2643 new conversation by calling conversation_new().
2645 The find_or_create_conversation prototype:
2647 extern conversation_t *find_or_create_conversation(packet_info *pinfo);
2650 packet_info *pinfo = the packet_info structure
2652 The frame number and the addresses necessary for find_conversation() and
2653 conversation_new() are taken from the pinfo structure (as is commonly done)
2654 and no 'options' are used.
2656 2.2.1.8 The conversation_add_proto_data function.
2658 Once you have created a conversation with conversation_new, you can
2659 associate data with it using this function.
2661 The conversation_add_proto_data prototype:
2663 void conversation_add_proto_data(conversation_t *conv, int proto,
2667 conversation_t *conv = the conversation in question
2668 int proto = registered protocol number
2669 void *data = dissector data structure
2671 "conversation" is the value returned by conversation_new. "proto" is a
2672 unique protocol number created with proto_register_protocol. Protocols
2673 are typically registered in the proto_register_XXXX section of your
2674 dissector. "data" is a pointer to the data you wish to associate with the
2675 conversation. "data" usually points to "wmem_alloc'd" memory; the
2676 memory will be automatically freed each time a new dissection begins
2677 and thus need not be managed (freed) by the dissector.
2678 Using the protocol number allows several dissectors to
2679 associate data with a given conversation.
2682 2.2.1.9 The conversation_get_proto_data function.
2684 After you have located a conversation with find_conversation, you can use
2685 this function to retrieve any data associated with it.
2687 The conversation_get_proto_data prototype:
2689 void *conversation_get_proto_data(conversation_t *conv, int proto);
2692 conversation_t *conv = the conversation in question
2693 int proto = registered protocol number
2695 "conversation" is the conversation created with conversation_new. "proto"
2696 is a unique protocol number created with proto_register_protocol,
2697 typically in the proto_register_XXXX portion of a dissector. The function
2698 returns a pointer to the data requested, or NULL if no data was found.
2701 2.2.1.10 The conversation_delete_proto_data function.
2703 After you are finished with a conversation, you can remove your association
2704 with this function. Please note that ONLY the conversation entry is
2705 removed. If you have allocated any memory for your data (other than with wmem_alloc),
2706 you must free it as well.
2708 The conversation_delete_proto_data prototype:
2710 void conversation_delete_proto_data(conversation_t *conv, int proto);
2713 conversation_t *conv = the conversation in question
2714 int proto = registered protocol number
2716 "conversation" is the conversation created with conversation_new. "proto"
2717 is a unique protocol number created with proto_register_protocol,
2718 typically in the proto_register_XXXX portion of a dissector.
2720 2.2.1.11 The conversation_set_dissector function
2722 This function sets the protocol dissector to be invoked whenever
2723 conversation parameters (addresses, port_types, ports, etc) are matched
2724 during the dissection of a packet.
2726 The conversation_set_dissector prototype:
2728 void conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle);
2731 conversation_t *conv = the conversation in question
2732 const dissector_handle_t handle = the dissector handle.
2735 2.2.2 Using timestamps relative to the conversation
2737 There is a framework to calculate timestamps relative to the start of the
2738 conversation. First of all the timestamp of the first packet that has been
2739 seen in the conversation must be kept in the protocol data to be able
2740 to calculate the timestamp of the current packet relative to the start
2741 of the conversation. The timestamp of the last packet that was seen in the
2742 conversation should also be kept in the protocol data. This way the
2743 delta time between the current packet and the previous packet in the
2744 conversation can be calculated.
2746 So add the following items to the struct that is used for the protocol data:
2751 The ts_prev value should only be set during the first run through the
2752 packets (ie PINFO_FD_VISITED(pinfo) is false).
2754 Next step is to use the per-packet information (described in section 2.5)
2755 to keep the calculated delta timestamp, as it can only be calculated
2756 on the first run through the packets. This is because a packet can be
2757 selected in random order once the whole file has been read.
2759 After calculating the conversation timestamps, it is time to put them in
2760 the appropriate columns with the function 'col_set_time' (described in
2761 section 1.5.9). The column used for relative timestamps is:
2763 COL_REL_TIME, /* Delta time to last frame in conversation */
2765 Last but not least, there MUST be a preference in each dissector that
2766 uses conversation timestamps that makes it possible to enable and
2767 disable the calculation of conversation timestamps. The main argument
2768 for this is that a higher level conversation is able to overwrite
2769 the values of lower level conversations in these two columns. Being
2770 able to actively select which protocols may overwrite the conversation
2771 timestamp columns gives the user the power to control these columns.
2772 (A second reason is that conversation timestamps use the per-packet
2773 data structure which uses additional memory, which should be avoided
2774 if these timestamps are not needed)
2776 Have a look at the differences to packet-tcp.[ch] in SVN 22966 and
2777 SVN 23058 to see the implementation of conversation timestamps for
2781 2.2.3 The example conversation code using wmem_file_scope memory.
2783 For a conversation between two IP addresses and ports you can use this as an
2784 example. This example uses wmem_alloc() with wmem_file_scope() to allocate
2785 memory and stores the data pointer in the conversation 'data' variable.
2787 /************************ Global values ************************/
2789 /* define your structure here */
2794 /* Registered protocol number */
2795 static int my_proto;
2797 /********************* in the dissector routine *********************/
2799 /* the local variables in the dissector */
2801 conversation_t *conversation;
2802 my_entry_t *data_ptr;
2805 /* look up the conversation */
2807 conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
2808 conversation_pt_to_conversation_type(pinfo->ptype),
2809 pinfo->srcport, pinfo->destport, 0);
2811 /* if conversation found get the data pointer that you stored */
2813 data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2816 /* new conversation create local data structure */
2818 data_ptr = wmem_alloc(wmem_file_scope(), sizeof(my_entry_t));
2820 /*** add your code here to setup the new data structure ***/
2822 /* create the conversation with your data pointer */
2824 conversation = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst,
2825 conversation_pt_to_conversation_type(pinfo->ptype),
2826 pinfo->srcport, pinfo->destport, 0);
2827 conversation_add_proto_data(conversation, my_proto, (void *)data_ptr);
2830 /* at this point the conversation data is ready */
2832 /***************** in the protocol register routine *****************/
2834 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2837 2.2.4 An example conversation code that starts at a specific frame number.
2839 Sometimes a dissector has determined that a new conversation is needed that
2840 starts at a specific frame number, when a capture session encompasses multiple
2841 conversation that reuse the same src/dest ip/port pairs. You can use the
2842 conversation->setup_frame returned by find_conversation with
2843 pinfo->num to determine whether or not there already exists a conversation
2844 that starts at the specific frame number.
2846 /* in the dissector routine */
2848 conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
2849 conversation_pt_to_conversation_type(pinfo->ptype),
2850 pinfo->srcport, pinfo->destport, 0);
2851 if (conversation == NULL || (conversation->setup_frame != pinfo->num)) {
2852 /* It's not part of any conversation or the returned
2853 * conversation->setup_frame doesn't match the current frame
2856 conversation = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst,
2857 conversation_pt_to_conversation_type(pinfo->ptype),
2858 pinfo->srcport, pinfo->destport, 0);
2862 2.2.5 The example conversation code using conversation index field.
2864 Sometimes the conversation isn't enough to define a unique data storage
2865 value for the network traffic. For example if you are storing information
2866 about requests carried in a conversation, the request may have an
2867 identifier that is used to define the request. In this case the
2868 conversation and the identifier are required to find the data storage
2869 pointer. You can use the conversation data structure index value to
2870 uniquely define the conversation.
2872 See packet-afs.c for an example of how to use the conversation index. In
2873 this dissector multiple requests are sent in the same conversation. To store
2874 information for each request the dissector has an internal hash table based
2875 upon the conversation index and values inside the request packets.
2878 /* in the dissector routine */
2880 /* to find a request value, first lookup conversation to get index */
2881 /* then used the conversation index, and request data to find data */
2882 /* in the local hash table */
2884 conversation = find_or_create_conversation(pinfo);
2886 request_key.conversation = conversation->index;
2887 request_key.service = pntoh16(&rxh->serviceId);
2888 request_key.callnumber = pntoh32(&rxh->callNumber);
2890 request_val = (struct afs_request_val *)g_hash_table_lookup(
2891 afs_request_hash, &request_key);
2893 /* only allocate a new hash element when it's a request */
2895 if (!request_val && !reply) {
2896 new_request_key = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_key));
2897 *new_request_key = request_key;
2899 request_val = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_val));
2900 request_val -> opcode = pntoh32(&afsh->opcode);
2901 opcode = request_val->opcode;
2903 g_hash_table_insert(afs_request_hash, new_request_key, request_val);
2908 2.3 Dynamic conversation dissector registration.
2911 NOTE: This sections assumes that all information is available to
2912 create a complete conversation, source port/address and
2913 destination port/address. If either the destination port or
2914 address is known, see section 2.4 Dynamic server port dissector
2917 For protocols that negotiate a secondary port connection, for example
2918 packet-msproxy.c, a conversation can install a dissector to handle
2919 the secondary protocol dissection. After the conversation is created
2920 for the negotiated ports use the conversation_set_dissector to define
2921 the dissection routine.
2922 Before we create these conversations or assign a dissector to them we should
2923 first check that the conversation does not already exist and if it exists
2924 whether it is registered to our protocol or not.
2925 We should do this because it is uncommon but it does happen that multiple
2926 different protocols can use the same socketpair during different stages of
2927 an application cycle. By keeping track of the frame number a conversation
2928 was started in Wireshark can still tell these different protocols apart.
2930 The second argument to conversation_set_dissector is a dissector handle,
2931 which is created with a call to create_dissector_handle,
2932 register_dissector, or register_dissector_with_description.
2934 register_dissector_with_description takes as arguments a string giving a name
2935 for the dissector, a string with a human-readable summary of the dissector, a
2936 pointer to the dissector function, and a protocol ID as returned by
2937 proto_register_protocol.
2939 register_dissector takes as arguments a string giving a name for the
2940 dissector, a pointer to the dissector function, and a protocol ID
2941 as returned by proto_register_protocol.
2943 create_dissector_handle takes as arguments a pointer to the dissector
2944 function and a protocol ID as returned by proto_register_protocol.
2945 It is recommended to use one of the above two functions instead of this one,
2946 since they allow the dissector to be referenced by name from the command line,
2947 by other dissectors via calls to find_dissector, etc.
2949 The protocol ID is the ID for the protocol dissected by the function.
2950 The function will not be called if the protocol has been disabled by the
2951 user; instead, the data for the protocol will be dissected as raw data.
2955 /* the handle for the dynamic dissector *
2956 static dissector_handle_t sub_dissector_handle;
2958 /* prototype for the dynamic dissector */
2959 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
2961 /* in the main protocol dissector, where the next dissector is setup */
2963 /* if conversation has a data field, create it and load structure */
2965 /* First check if a conversation already exists for this socketpair */
2966 conversation = find_conversation(pinfo->num,
2967 &pinfo->src, &pinfo->dst, conversation_type,
2968 src_port, dst_port, 0);
2970 /* If there is no such conversation, or if there is one but for
2971 someone else's protocol then we just create a new conversation
2972 and assign our protocol to it.
2974 if ((conversation == NULL) ||
2975 (conversation->dissector_handle != sub_dissector_handle)) {
2976 new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2977 new_conv_info->data1 = value1;
2979 /* create the conversation for the dynamic port */
2980 conversation = conversation_new(pinfo->num,
2981 &pinfo->src, &pinfo->dst, protocol,
2982 src_port, dst_port, new_conv_info, 0);
2984 /* set the dissector for the new conversation */
2985 conversation_set_dissector(conversation, sub_dissector_handle);
2990 proto_register_PROTOABBREV(void)
2994 sub_dissector_handle = register_dissector("PROTOABBREV", sub_dissector,
3000 2.4 Dynamic server port dissector registration.
3002 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
3003 conversation with only one port and address set, this isn't a
3004 requirement. Either the second port or the second address can be set
3005 when the conversation is created.
3007 For protocols that define a server address and port for a secondary
3008 protocol, a conversation can be used to link a protocol dissector to
3009 the server port and address. The key is to create the new
3010 conversation with the second address and port set to the "accept
3013 Some server applications can use the same port for different protocols during
3014 different stages of a transaction. For example it might initially use SNMP
3015 to perform some discovery and later switch to use TFTP using the same port.
3016 In order to handle this properly we must first check whether such a
3017 conversation already exists or not and if it exists we also check whether the
3018 registered dissector_handle for that conversation is "our" dissector or not.
3019 If not we create a new conversation on top of the previous one and set this new
3020 conversation to use our protocol.
3021 Since Wireshark keeps track of the frame number where a conversation started
3022 wireshark will still be able to keep the packets apart even though they do use
3023 the same socketpair.
3024 (See packet-tftp.c and packet-snmp.c for examples of this)
3026 There are two support routines that will allow the second port and/or
3027 address to be set later.
3029 conversation_set_port2(conversation_t *conv, uint32_t port);
3030 conversation_set_addr2(conversation_t *conv, address addr);
3032 These routines will change the second address or port for the
3033 conversation. So, the server port conversation will be converted into a
3034 more complete conversation definition. Don't use these routines if you
3035 want to create a conversation between the server and client and retain the
3036 server port definition, you must create a new conversation.
3041 /* the handle for the dynamic dissector *
3042 static dissector_handle_t sub_dissector_handle;
3046 /* in the main protocol dissector, where the next dissector is setup */
3048 /* if conversation has a data field, create it and load structure */
3050 new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
3051 new_conv_info->data1 = value1;
3053 /* create the conversation for the dynamic server address and port */
3054 /* NOTE: The second address and port values don't matter because the */
3055 /* NO_ADDR2 and NO_PORT2 options are set. */
3057 /* First check if a conversation already exists for this
3060 conversation = find_conversation(pinfo->num,
3061 &server_src_addr, 0, protocol,
3062 server_src_port, 0, NO_ADDR2 | NO_PORT_B);
3064 /* If there is no such conversation, or if there is one but for
3065 someone else's protocol then we just create a new conversation
3066 and assign our protocol to it.
3068 if ((conversation == NULL) ||
3069 (conversation->dissector_handle != sub_dissector_handle)) {
3070 conversation = conversation_new(pinfo->num,
3071 &server_src_addr, 0, conversation_type,
3072 server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
3074 /* set the dissector for the new conversation */
3075 conversation_set_dissector(conversation, sub_dissector_handle);
3078 2.5 Per-packet information.
3080 Information can be stored for each data packet that is processed by the
3081 dissector. The information is added with the p_add_proto_data function and
3082 retrieved with the p_get_proto_data function. The data pointers passed into
3083 the p_add_proto_data are not managed by the proto_data routines, however the
3084 data pointer memory scope must match that of the scope parameter.
3085 The two most common use cases for p_add_proto_data/p_get_proto_data are for
3086 persistent data about the packet for the lifetime of the capture (file scope)
3087 and to exchange data between dissectors across a single packet (packet scope).
3088 It is also used to provide packet data for Decode As dialog (packet scope).
3090 These functions are declared in <epan/proto_data.h>.
3093 p_add_proto_data(wmem_allocator_t *scope, packet_info *pinfo, int proto, uint32_t key, void *proto_data)
3095 p_get_proto_data(wmem_allocator_t *scope, packet_info *pinfo, int proto, uint32_t key)
3098 scope - Lifetime of the data to be stored, typically wmem_file_scope()
3099 or pinfo->pool (packet scope). Must match scope of data
3101 pinfo - The packet info pointer.
3102 proto - Protocol id returned by the proto_register_protocol call
3103 during initialization
3104 key - key associated with 'proto_data'
3105 proto_data - pointer to the dissector data.
3108 2.6 User Preferences.
3110 If the dissector has user options, there is support for adding these preferences
3111 to a configuration dialog.
3113 You must register the module with the preferences routine with -
3115 module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
3117 module_t *prefs_register_protocol_subtree(const char *subtree, int id,
3118 void (*apply_cb)(void));
3121 Where: proto_id - the value returned by "proto_register_protocol()" when
3122 the protocol was registered.
3123 apply_cb - Callback routine that is called when preferences are
3124 applied. It may be NULL, which inhibits the callback.
3125 subtree - grouping preferences tree node name (several protocols can
3126 be grouped under one preferences subtree)
3128 Then you can register the fields that can be configured by the user with these
3131 /* Register a preference with an unsigned integral value. */
3132 void prefs_register_uint_preference(module_t *module, const char *name,
3133 const char *title, const char *description, unsigned base, unsigned *var);
3135 /* Register a preference with an Boolean value. */
3136 void prefs_register_bool_preference(module_t *module, const char *name,
3137 const char *title, const char *description, bool *var);
3139 /* Register a preference with an enumerated value. */
3140 void prefs_register_enum_preference(module_t *module, const char *name,
3141 const char *title, const char *description, int *var,
3142 const enum_val_t *enumvals, bool radio_buttons)
3144 /* Register a preference with a character-string value. */
3145 void prefs_register_string_preference(module_t *module, const char *name,
3146 const char *title, const char *description, char **var)
3148 /* Register a preference with a password (a character-string) value. */
3149 /* The value is hold during runtime, only in memory. It is never written to disk */
3150 void prefs_register_password_preference(module_t *module, const char *name,
3151 const char *title, const char *description, char **var)
3153 /* Register a preference with a file name (string) value.
3154 * File name preferences are basically like string preferences
3155 * except that the GUI gives the user the ability to browse for the
3156 * file. Set for_writing true to show a Save dialog instead of normal Open.
3158 void prefs_register_filename_preference(module_t *module, const char *name,
3159 const char *title, const char *description, char **var,
3162 /* Register a preference with a range of unsigned integers (e.g.,
3165 void prefs_register_range_preference(module_t *module, const char *name,
3166 const char *title, const char *description, range_t *var,
3169 Where: module - Returned by the prefs_register_protocol routine
3170 name - This is appended to the name of the protocol, with a
3171 "." between them, to construct a name that identifies
3172 the field in the preference file; the name itself
3173 should not include the protocol name, as the name in
3174 the preference file will already have it. Make sure that
3175 only lower-case ASCII letters, numbers, underscores and
3176 dots appear in the preference name.
3177 title - Field title in the preferences dialog
3178 description - Comments added to the preference file above the
3179 preference value and shown as tooltip in the GUI, or NULL
3180 var - pointer to the storage location that is updated when the
3181 field is changed in the preference dialog box. Note that
3182 with string preferences the given pointer is overwritten
3183 with a pointer to a new copy of the string during the
3184 preference registration. The passed-in string may be
3185 freed, but you must keep another pointer to the string
3186 in order to free it.
3187 base - Base that the unsigned integer is expected to be in,
3189 enumvals - an array of enum_val_t structures. This must be
3190 NULL-terminated; the members of that structure are:
3192 a short name, to be used with the "-o" flag - it
3193 should not contain spaces or upper-case letters,
3194 so that it's easier to put in a command line;
3196 a description, which is used in the GUI (and
3197 which, for compatibility reasons, is currently
3198 what's written to the preferences file) - it can
3199 contain spaces, capital letters, punctuation,
3202 the numerical value corresponding to that name
3204 radio_buttons - true if the field is to be displayed in the
3205 preferences dialog as a set of radio buttons,
3206 false if it is to be displayed as an option
3208 max_value - The maximum allowed value for a range (0 is the minimum).
3210 These functions are declared in <epan/prefs.h>.
3212 An example from packet-rtpproxy.c -
3214 proto_rtpproxy = proto_register_protocol ( "Sippy RTPproxy Protocol", "RTPproxy", "rtpproxy");
3218 rtpproxy_module = prefs_register_protocol(proto_rtpproxy, proto_reg_handoff_rtpproxy);
3220 prefs_register_bool_preference(rtpproxy_module, "establish_conversation",
3221 "Establish Media Conversation",
3222 "Specifies that RTP/RTCP/T.38/MSRP/etc streams are decoded based "
3223 "upon port numbers found in RTPproxy answers",
3224 &rtpproxy_establish_conversation);
3226 prefs_register_uint_preference(rtpproxy_module, "reply.timeout",
3227 "RTPproxy reply timeout", /* Title */
3228 "Maximum timeout value in waiting for reply from RTPProxy (in milliseconds).", /* Descr */
3232 This will create preferences "rtpproxy.establish_conversation" and
3233 "rtpproxy.reply.timeout", the first of which is an Boolean and the
3234 second of which is a unsigned integer.
3236 Note that a warning will pop up if you've saved such preference to the
3237 preference file and you subsequently take the code out. The way to make
3238 a preference obsolete is to register it as such:
3240 /* Register a preference that used to be supported but no longer is. */
3241 void prefs_register_obsolete_preference(module_t *module,
3244 2.7 Reassembly/desegmentation for protocols running atop TCP.
3246 There are two main ways of reassembling a Protocol Data Unit (PDU) which
3247 spans across multiple TCP segments. The first approach is simpler, but
3248 assumes you are running atop of TCP when this occurs (but your dissector
3249 might run atop of UDP, too, for example), and that your PDUs consist of a
3250 fixed amount of data that includes enough information to determine the PDU
3251 length, possibly followed by additional data. The second method is more
3252 generic but requires more code and is less efficient.
3254 2.7.1 Using tcp_dissect_pdus().
3256 For the first method, you register two different dissection methods, one
3257 for the TCP case, and one for the other cases. It is a good idea to
3258 also have a dissect_PROTO_common function which will parse the generic
3259 content that you can find in all PDUs which is called from
3260 dissect_PROTO_tcp when the reassembly is complete and from
3261 dissect_PROTO_udp (or dissect_PROTO_other).
3263 To register the distinct dissector functions, consider the following
3264 example, stolen from packet-hartip.c:
3266 #include "packet-tcp.h"
3268 dissector_handle_t hartip_tcp_handle;
3269 dissector_handle_t hartip_udp_handle;
3271 hartip_udp_handle = register_dissector_with_description("hart_ip", "HART-IP over UDP", dissect_hartip_udp, proto_hartip);
3272 hartip_tcp_handle = register_dissector_with_description("hart_ip.tcp", "HART-IP over TCP", dissect_hartip_tcp, proto_hartip);
3274 dissector_add_uint_with_preference("udp.port", HARTIP_PORT, hartip_udp_handle);
3275 dissector_add_uint_with_preference("tcp.port", HARTIP_PORT, hartip_tcp_handle);
3277 The dissect_hartip_udp function does very little work and calls
3278 dissect_hartip_common, while dissect_hartip_tcp calls tcp_dissect_pdus with a
3279 reference to a callback which will be called with reassembled data:
3282 dissect_hartip_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
3285 if (!tvb_bytes_exist(tvb, 0, HARTIP_HEADER_LENGTH))
3288 tcp_dissect_pdus(tvb, pinfo, tree, hartip_desegment, HARTIP_HEADER_LENGTH,
3289 get_dissect_hartip_len, dissect_hartip_pdu, data);
3290 return tvb_reported_length(tvb);
3293 (The dissect_hartip_pdu function acts similarly to dissect_hartip_udp.)
3294 The arguments to tcp_dissect_pdus are:
3296 the tvbuff pointer, packet_info pointer, and proto_tree pointer
3297 passed to the dissector;
3299 a bool flag indicating whether desegmentation is enabled for
3302 the number of bytes of PDU data required to determine the length
3305 a routine that takes as arguments a packet_info pointer, a tvbuff
3306 pointer and an offset value representing the offset into the tvbuff
3307 at which a PDU begins, and a void pointer for user data, and should
3308 return the total length of the PDU in bytes (or 0 if more bytes are
3309 needed to determine the message length).
3310 The routine must not throw exceptions (it is guaranteed that the
3311 number of bytes specified by the previous argument to
3312 tcp_dissect_pdus is available, but more data might not be available,
3313 so don't refer to any data past that);
3315 a new_dissector_t routine to dissect the pdu that's passed a tvbuff
3316 pointer, packet_info pointer, proto_tree pointer and a void pointer for
3317 user data, with the tvbuff containing a possibly-reassembled PDU. (The
3318 "reported_length" of the tvbuff will be the length of the PDU);
3320 a void pointer to user data that is passed to the length-determining
3321 routine, and the dissector routine referenced in the previous parameter.
3323 2.7.2 Modifying the pinfo struct.
3325 The second reassembly mode is preferred when the dissector cannot determine
3326 how many bytes it will need to read in order to determine the size of a PDU.
3327 It may also be useful if your dissector needs to support reassembly from
3328 protocols other than TCP.
3330 Your dissect_PROTO will initially be passed a tvbuff containing the payload of
3331 the first packet. It should dissect as much data as it can, noting that it may
3332 contain more than one complete PDU. If the end of the provided tvbuff coincides
3333 with the end of a PDU then all is well and your dissector can just return as
3334 normal. (If it is a new-style dissector, it should return the number of bytes
3335 successfully processed.)
3337 If the dissector discovers that the end of the tvbuff does /not/ coincide with
3338 the end of a PDU, (ie, there is half of a PDU at the end of the tvbuff), it can
3339 indicate this to the parent dissector, by updating the pinfo struct. The
3340 desegment_offset field is the offset in the tvbuff at which the dissector will
3341 continue processing when next called. The desegment_len field should contain
3342 the estimated number of additional bytes required for completing the PDU. Next
3343 time your dissect_PROTO is called, it will be passed a tvbuff composed of the
3344 end of the data from the previous tvbuff together with desegment_len more bytes.
3346 If the dissector cannot tell how many more bytes it will need, it should set
3347 desegment_len=DESEGMENT_ONE_MORE_SEGMENT; it will then be called again as soon
3348 as any more data becomes available. Dissectors should set the desegment_len to a
3349 reasonable value when possible rather than always setting
3350 DESEGMENT_ONE_MORE_SEGMENT as it will generally be more efficient. Also, you
3351 *must not* set desegment_len=1 in this case, in the hope that you can change
3352 your mind later: once you return a positive value from desegment_len, your PDU
3353 boundary is set in stone.
3355 static hf_register_info hf[] = {
3357 { "C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
3362 * Dissect a buffer containing ASCII C strings.
3364 * @param tvb The buffer to dissect.
3365 * @param pinfo Packet Info.
3366 * @param tree The protocol tree.
3367 * @param data Optional data parameter given by parent dissector.
3369 static int dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void *data _U_)
3371 unsigned offset = 0;
3373 while (offset < tvb_reported_length(tvb)) {
3374 int available = tvb_reported_length_remaining(tvb, offset);
3375 int len = tvb_strnlen(tvb, offset, available);
3378 /* we ran out of data: ask for more */
3379 pinfo->desegment_offset = offset;
3380 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
3381 return (offset + available);
3384 col_set_str(pinfo->cinfo, COL_INFO, "C String");
3386 len += 1; /* Add one for the '\0' */
3388 proto_tree_add_item(tree, hf_cstring, tvb, offset, len, ENC_ASCII);
3389 offset += (unsigned)len;
3392 /* if we get here, then the end of the tvb coincided with the end of a
3393 string. Happy days. */
3394 return tvb_captured_length(tvb);
3397 This simple dissector will repeatedly return DESEGMENT_ONE_MORE_SEGMENT
3398 requesting more data until the tvbuff contains a complete C string. The C string
3399 will then be added to the protocol tree. Note that there may be more
3400 than one complete C string in the tvbuff, so the dissection is done in a
3403 2.8 Using udp_dissect_pdus().
3405 As noted in section 2.7.1, TCP has an API to dissect its PDU that can handle
3406 a PDU spread across multiple packets or multiple PDUs spread across a single
3407 packet. This section describes a similar mechanism for UDP, but is only
3408 applicable for one or more PDUs in a single packet. If a protocol runs on top
3409 of TCP as well as UDP, a common PDU dissection function can be created for both.
3411 To register the distinct dissector functions, consider the following
3412 example using UDP and TCP dissection, stolen from packet-dnp.c:
3414 #include "packet-tcp.h"
3415 #include "packet-udp.h"
3417 dissector_handle_t dnp3_tcp_handle;
3418 dissector_handle_t dnp3_udp_handle;
3420 dnp3_tcp_handle = register_dissector("dnp3.tcp", dissect_dnp3_tcp, proto_dnp3);
3421 dnp3_udp_handle = register_dissector("dnp3.udp", dissect_dnp3_udp, proto_dnp3);
3423 dissector_add_uint_with_preference("tcp.port", TCP_PORT_DNP, dnp3_tcp_handle);
3424 dissector_add_uint_with_preference("udp.port", UDP_PORT_DNP, dnp3_udp_handle);
3426 Both dissect_dnp3_tcp and dissect_dnp3_udp call tcp_dissect_pdus and
3427 udp_dissect_pdus respectively, with a reference to the same callbacks which
3428 are called to handle PDU data.
3431 dissect_dnp3_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
3433 return udp_dissect_pdus(tvb, pinfo, tree, DNP_HDR_LEN, dnp3_udp_check_header,
3434 get_dnp3_message_len, dissect_dnp3_message, data);
3438 dissect_dnp3_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
3440 if (!check_dnp3_header(tvb, false)) {
3444 tcp_dissect_pdus(tvb, pinfo, tree, true, DNP_HDR_LEN,
3445 get_dnp3_message_len, dissect_dnp3_message, data);
3447 return tvb_captured_length(tvb);
3450 (udp_dissect_pdus has an option of a heuristic check function within it while
3451 tcp_dissect_pdus does not, so it's done outside)
3453 The arguments to udp_dissect_pdus are:
3455 the tvbuff pointer, packet_info pointer, and proto_tree pointer
3456 passed to the dissector;
3458 the number of bytes of PDU data required to determine the length
3461 an optional routine (passing NULL is okay) that takes as arguments a
3462 packet_info pointer, a tvbuff pointer and an offset value representing the
3463 offset into the tvbuff at which a PDU begins, and a void pointer for user
3464 data, and should return true if the packet belongs to the dissector.
3465 The routine must not throw exceptions (it is guaranteed that the
3466 number of bytes specified by the previous argument to
3467 udp_dissect_pdus is available, but more data might not be available,
3468 so don't refer to any data past that);
3470 a routine that takes as arguments a packet_info pointer, a tvbuff
3471 pointer and an offset value representing the offset into the tvbuff
3472 at which a PDU begins, and a void pointer for user data, and should
3473 return the total length of the PDU in bytes. If return value is 0,
3474 it's treated the same as a failed heuristic.
3475 The routine must not throw exceptions (it is guaranteed that the
3476 number of bytes specified by the previous argument to
3477 tcp_dissect_pdus is available, but more data might not be available,
3478 so don't refer to any data past that);
3480 a new_dissector_t routine to dissect the pdu that's passed a tvbuff
3481 pointer, packet_info pointer, proto_tree pointer and a void pointer for
3482 user data, with the tvbuff containing a possibly-reassembled PDU. (The
3483 "reported_length" of the tvbuff will be the length of the PDU);
3485 a void pointer to user data that is passed to the length-determining
3486 routine, and the dissector routine referenced in the previous parameter.
3488 2.9 PINOs (Protocols in name only)
3490 For the typical dissector there is a 1-1 relationship between it and its
3491 protocol. However, there are times when a protocol needs multiple "names"
3492 because it has multiple dissection functions going into the same dissector
3493 table. The multiple names removes confusion when picking dissection through
3494 Decode As functionality.
3496 Once the "main" protocol name has been created through proto_register_protocol,
3497 additional "pinos" can be created with proto_register_protocol_in_name_only.
3498 These pinos have all of the naming conventions of a protocol, but are stored
3499 separately as to remove confusion from real protocols. "pinos" the main
3500 protocol's properties for things like enable/disable. i.e. If the "main"
3501 protocol has been disabled, all of its pinos will be disabled as well.
3502 Pinos should not have any fields registered with them or heuristic tables
3503 associated with them.
3505 Another use case for pinos is when a protocol contains a TLV design and it
3506 wants to create a dissector table to handle dissection of the "V". Dissector
3507 tables require a "protocol", but the dissection functions for that table
3508 typically aren't a protocol. In this case proto_register_protocol_in_name_only
3509 creates the necessary placeholder for the dissector table. In addition, because
3510 a dissector table exists, "V"s of the TLVs can be dissected outside of the
3511 original dissector file.
3513 2.10 Creating Decode As functionality.
3515 While the Decode As functionality is available through the GUI, the underlying
3516 functionality is controlled by dissectors themselves. To create Decode As
3517 functionality for a dissector, two things are required:
3518 1. A dissector table
3519 2. A series of structures to assist the GUI in how to present the dissector
3522 Consider the following example using IP dissection, stolen from packet-ip.c:
3524 static build_valid_func ip_da_build_value[1] = {ip_value};
3525 static decode_as_value_t ip_da_values = {ip_prompt, 1, ip_da_build_value};
3526 static decode_as_t ip_da = {"ip", "ip.proto", 1, 0, &ip_da_values, NULL, NULL,
3527 decode_as_default_populate_list, decode_as_default_reset, decode_as_default_change, NULL};
3529 ip_dissector_table = register_dissector_table("ip.proto", "IP protocol", ip_proto, FT_UINT8, BASE_DEC);
3531 register_decode_as(&ip_da);
3533 ip_da_build_value contains all of the function pointers (typically just 1) that
3534 can be used to retrieve the value(s) that go into the dissector table. This is
3535 usually data saved by the dissector during packet dissector with an API like
3536 p_add_proto_data and retrieved in the "value" function with p_get_proto_data.
3538 ip_da_values contains all of the function pointers (typically just 1) that
3539 provide the text explaining the name and use of the value field that will
3540 be passed to the dissector table to change the dissection output.
3542 ip_da pulls everything together including the dissector (protocol) name, the
3543 "layer type" of the dissector, the dissector table name, the function pointer
3544 values as well as handlers for populating, applying and resetting the changes
3545 to the dissector table through Decode As GUI functionality. For dissector
3546 tables that are an integer or string type, the provided "default" handling
3547 functions shown in the example should suffice.
3549 All entries into a dissector table that use Decode As must have a unique
3550 protocol ID. If a protocol wants multiple entries into a dissector table,
3551 a pino should be used (see section 2.9)
3555 The ptvcursor API allows a simpler approach to writing dissectors for
3556 simple protocols. The ptvcursor API works best for protocols whose fields
3557 are static and whose format does not depend on the value of other fields.
3558 However, even if only a portion of your protocol is statically defined,
3559 then that portion could make use of ptvcursors.
3561 The ptvcursor API lets you extract data from a tvbuff, and add it to a
3562 protocol tree in one step. It also keeps track of the position in the
3563 tvbuff so that you can extract data again without having to compute any
3564 offsets --- hence the "cursor" name of the API.
3566 The three steps for a simple protocol are:
3567 1. Create a new ptvcursor with ptvcursor_new()
3568 2. Add fields with multiple calls of ptvcursor_add()
3569 3. Delete the ptvcursor with ptvcursor_free()
3571 ptvcursor offers the possibility to add subtrees in the tree as well. It can be
3572 done in very simple steps :
3573 1. Create a new subtree with ptvcursor_push_subtree(). The old subtree is
3574 pushed in a stack and the new subtree will be used by ptvcursor.
3575 2. Add fields with multiple calls of ptvcursor_add(). The fields will be
3576 added in the new subtree created at the previous step.
3577 3. Pop the previous subtree with ptvcursor_pop_subtree(). The previous
3578 subtree is again used by ptvcursor.
3579 Note that at the end of the parsing of a packet you must have popped each
3580 subtree you pushed. If it's not the case, the dissector will generate an error.
3582 To use the ptvcursor API, include the "ptvcursor.h" file. The PGM dissector
3583 is an example of how to use it. You don't need to look at it as a guide;
3584 instead, the API description here should be good enough.
3586 2.11.1 ptvcursor API.
3589 ptvcursor_new(proto_tree* tree, tvbuff_t* tvb, int offset)
3590 This creates a new ptvcursor_t object for iterating over a tvbuff.
3591 You must call this and use this ptvcursor_t object so you can use the
3595 ptvcursor_add(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding)
3596 This will extract 'length' bytes from the tvbuff and place it in
3597 the proto_tree as field 'hf', which is a registered header_field. The
3598 pointer to the proto_item that is created is passed back to you. Internally,
3599 the ptvcursor advances its cursor so the next call to ptvcursor_add
3600 starts where this call finished. The 'encoding' parameter is relevant for
3601 certain type of fields (See above under proto_tree_add_item()).
3604 ptvcursor_add_ret_uint(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, uint32_t *retval);
3605 Like ptvcursor_add, but returns uint value retrieved
3608 ptvcursor_add_ret_int(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, int32_t *retval);
3609 Like ptvcursor_add, but returns int value retrieved
3612 ptvcursor_add_ret_string(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, wmem_allocator_t *scope, const uint8_t **retval);
3613 Like ptvcursor_add, but returns string retrieved
3616 ptvcursor_add_ret_boolean(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, bool *retval);
3617 Like ptvcursor_add, but returns boolean value retrieved
3620 ptvcursor_add_no_advance(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding)
3621 Like ptvcursor_add, but does not advance the internal cursor.
3624 ptvcursor_advance(ptvcursor_t* ptvc, int length)
3625 Advances the internal cursor without adding anything to the proto_tree.
3628 ptvcursor_free(ptvcursor_t* ptvc)
3629 Frees the memory associated with the ptvcursor. You must call this
3630 after your dissection with the ptvcursor API is completed.
3634 ptvcursor_push_subtree(ptvcursor_t* ptvc, proto_item* it, int ett_subtree)
3635 Pushes the current subtree in the tree stack of the cursor, creates a new
3636 one and sets this one as the working tree.
3639 ptvcursor_pop_subtree(ptvcursor_t* ptvc);
3640 Pops a subtree in the tree stack of the cursor
3643 ptvcursor_add_with_subtree(ptvcursor_t* ptvc, int hfindex, int length,
3644 const unsigned encoding, int ett_subtree);
3645 Adds an item to the tree and creates a subtree.
3646 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3647 In this case, at the next pop, the item length will be equal to the advancement
3648 of the cursor since the creation of the subtree.
3651 ptvcursor_add_text_with_subtree(ptvcursor_t* ptvc, int length,
3652 int ett_subtree, const char *format, ...);
3653 Add a text node to the tree and create a subtree.
3654 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3655 In this case, at the next pop, the item length will be equal to the advancement
3656 of the cursor since the creation of the subtree.
3658 2.11.2 Miscellaneous functions.
3661 ptvcursor_tvbuff(ptvcursor_t* ptvc)
3662 Returns the tvbuff associated with the ptvcursor.
3665 ptvcursor_current_offset(ptvcursor_t* ptvc)
3666 Returns the current offset.
3669 ptvcursor_tree(ptvcursor_t* ptvc)
3670 Returns the proto_tree associated with the ptvcursor.
3673 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree)
3674 Sets a new proto_tree for the ptvcursor.
3677 ptvcursor_set_subtree(ptvcursor_t* ptvc, proto_item* it, int ett_subtree);
3678 Creates a subtree and adds it to the cursor as the working tree but does
3679 not save the old working tree.
3683 2.12.1 Null Protocol Trees
3685 A protocol dissector may be called in 2 different ways - with, or
3686 without a non-null "tree" argument.
3688 If the proto_tree argument is null, Wireshark does not need to use
3689 the protocol tree information from your dissector, and therefore is
3690 passing the dissector a null "tree" argument so that it doesn't
3691 need to do work necessary to build the protocol tree.
3693 In the interest of speed, if "tree" is NULL, one can avoid building a
3694 protocol tree and adding stuff to it, or even looking at any packet
3695 data needed only if you're building the protocol tree, if possible.
3696 In particular, string operations and complex calculations that produce
3697 values which will be added as text to the tree and used nowhere else
3698 should be omitted when the tree is NULL.
3700 Note, however, that you must fill in column information, create
3701 conversations, reassemble packets, do calls to "expert" functions,
3702 build any other persistent state needed for dissection, and call
3703 subdissectors regardless of whether "tree" is NULL or not. Note
3704 also that there is no guarantee, the first time the dissector is
3705 called, whether "tree" will be null or not; your dissector must work
3706 correctly, building or updating whatever state information is
3707 necessary, in either case.
3709 Doing this properly generally means structuring the dissection so that
3710 the functions for adding items to the protocol tree are interleaved with
3711 the functions that must be called even when the tree is NULL. This
3712 allows dissection to occur without backtracking to an earlier offset,
3713 which ensures that in the case of invalid or truncated data that aborts
3714 dissection as much information is added to the protocol tree as possible.
3716 This can be inconvenient. Fortunately, the routines for adding items to
3717 the protocol tree can be passed a null protocol tree pointer, in which
3718 case they'll return a null item pointer, and "proto_item_add_subtree()"
3719 returns a null tree pointer if passed a null item pointer. Functions
3720 like "proto_tree_add_item_ret_XXX()" still return the requested value
3721 if there is a null protocol tree, but skip the rest of the work.
3723 So, if you call the API routines and are careful not to dereference any
3724 null tree or item pointers directly, you can ignore whether or not "tree"
3725 is null and do the dissection normally and still get some optimization
3726 benefits. This almost certainly results in easier to read code and lowers
3727 the chance of making subtle errors, and is thus recommended for initial
3728 implementations. It might not be as efficient as skipping that work if
3729 you're not building a protocol tree, but if the code would have a lot of
3730 tests whether "tree" is null to skip that work, you might still be better
3731 off just doing all that work regardless.
3733 2.12.2 Faked Protocol Items
3735 If the "tree" parameter is not NULL, Wireshark needs to use protocol tree
3736 information from your dissector. However, it may not need to use protocol
3737 tree information from *all* fields, because the full set of tree details
3738 will not be displayed or printed (the tree is not "visible"). Wireshark
3739 may be dissecting the packet in order to see if a particular filter matches,
3740 or to determine the value of custom columns that depend on field values,
3741 or to tap values from certain fields (on packets that only match a filter).
3743 In such cases, Wireshark will keep track of which fields are referenced,
3744 and when the API routines are called avoid actually creating a field_info
3745 struct and extracting data from the frame, instead returning a "faked"
3746 protocol item. Passing this faked protocol item to other API routines
3747 like proto_item_set_len() or proto_item_append_text() will have no effect.
3748 API routines that return a value, such as proto_tree_add_item_ret_string(),
3749 will still extract data from the string as necessary to return. Thus the
3750 typical dissector can ignore whether items are faked or not and simply
3751 benefit from the optimization. Dissectors that examine the protocol tree
3752 and protocol items and their field_info structs after creation directly
3753 may have unexpected results; this is best avoided in favor of using the
3757 * Editor modelines - https://www.wireshark.org/tools/modelines.html
3762 * indent-tabs-mode: nil
3765 * vi: set shiftwidth=4 tabstop=4 expandtab:
3766 * :indentSize=4:tabSize=4:noTabs=true: