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
1377 - FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_IPXNET and FT_FRAMENUM
1378 - FT_INT40, FT_INT48, FT_INT56 and FT_INT64
1379 - FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64 and FT_EUI64
1380 - FT_ABSOLUTE_TIME and FT_RELATIVE_TIME
1381 - FT_STRING, FT_STRINGZ, FT_UINT_STRING, FT_STRINGZPAD, and FT_STRINGZTRUNC
1382 - FT_FLOAT, FT_DOUBLE, FT_IEEE_11073_SFLOAT and FT_IEEE_11073_FLOAT
1383 - FT_BYTES, FT_UINT_BYTES, FT_ETHER, FT_AX25, FT_VINES and FT_FCWWN
1384 - FT_OID, FT_REL_OID and FT_SYSTEM_ID
1386 Any field not in a grouping above should *NOT* be used in duplicate field
1387 abbreviations. The current code does not prevent it, but someday in the future
1390 The HFILL macro at the end of the struct will set reasonable default values
1391 for internally used fields.
1393 1.5.2 Adding Items and Values to the Protocol Tree.
1395 A protocol item is added to an existing protocol tree with one of a
1396 handful of proto_XXX_DO_YYY() functions.
1398 Subtrees can be made with the proto_item_add_subtree() function:
1400 item = proto_tree_add_item(...);
1401 new_tree = proto_item_add_subtree(item, tree_type);
1403 This will add a subtree under the item in question; a subtree can be
1404 created under an item made by any of the "proto_tree_add_XXX" functions,
1405 so that the tree can be given an arbitrary depth.
1407 Subtree types are integers, assigned by
1408 "proto_register_subtree_array()". To register subtree types, pass an
1409 array of pointers to "int" variables to hold the subtree type values to
1410 "proto_register_subtree_array()":
1413 static int ett_field_a;
1415 static int *ett[] = {
1420 proto_register_subtree_array(ett, array_length(ett));
1422 in your "register" routine, just as you register the protocol and the
1423 fields for that protocol.
1425 The ett_ variables identify particular type of subtree so that if you expand
1426 one of them, Wireshark keeps track of that and, when you click on
1427 another packet, it automatically opens all subtrees of that type.
1428 If you close one of them, all subtrees of that type will be closed when
1429 you move to another packet.
1431 There are many functions that the programmer can use to add either
1432 protocol or field labels to the proto_tree, for example:
1435 proto_tree_add_item(tree, id, tvb, start, length, encoding);
1438 proto_tree_add_item_ret_int(tree, id, tvb, start, length, encoding,
1442 proto_tree_add_subtree(tree, tvb, start, length, idx, tree_item,
1446 proto_tree_add_int_format_value(tree, id, tvb, start, length,
1447 value, format, ...);
1450 proto_tree_add_checksum(proto_tree *tree, tvbuff_t *tvb, const unsigned offset,
1451 const int hf_checksum, const int hf_checksum_status,
1452 struct expert_field* bad_checksum_expert, packet_info *pinfo,
1453 uint32_t computed_checksum, const unsigned encoding, const unsigned flags);
1456 proto_tree_add_bitmask(tree, tvb, start, header, ett, fields,
1460 proto_tree_add_bits_item(tree, id, tvb, bit_offset, no_of_bits,
1463 The 'tree' argument is the tree to which the item is to be added. The
1464 'tvb' argument is the tvbuff from which the item's value is being
1465 extracted; the 'start' argument is the offset from the beginning of that
1466 tvbuff of the item being added, and the 'length' argument is the length,
1467 in bytes, of the item, bit_offset is the offset in bits and no_of_bits
1468 is the length in bits.
1470 The length of some items cannot be determined until the item has been
1471 dissected; to add such an item, add it with a length of -1, and, when the
1472 dissection is complete, set the length with 'proto_item_set_len()':
1475 proto_item_set_len(ti, length);
1477 The "ti" argument is the value returned by the call that added the item
1478 to the tree, and the "length" argument is the length of the item.
1480 All available protocol tree functions are declared in epan/proto.h, with
1481 their documentation. The details of these functions and their parameters
1482 are described below.
1484 proto_tree_add_item()
1485 ---------------------
1486 proto_tree_add_item is used when you wish to do no special formatting.
1487 The item added to the GUI tree will contain the name (as passed in the
1488 proto_register_*() function) and a value. The value will be fetched
1489 from the tvbuff by proto_tree_add_item(), based on the type of the field
1490 and the encoding of the value as specified by the "encoding" argument.
1492 For FT_NONE, FT_BYTES, FT_ETHER, FT_IPv6, FT_IPXNET, FT_OID, FT_REL_OID,
1493 FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FCWWN fields, and 'protocol' fields
1494 the encoding is not relevant; the 'encoding' argument should be
1495 ENC_NA (Not Applicable).
1497 For FT_UINT_BYTES fields, the byte order of the count must be specified
1498 as well as the 'encoding' for bytes which should be ENC_NA,
1499 i.e. ENC_LITTLE_ENDIAN|ENC_NA
1501 For integral, floating-point, Boolean, FT_GUID, and FT_EUI64 fields,
1502 the encoding specifies the byte order of the value; the 'encoding'
1503 argument should be ENC_LITTLE_ENDIAN if the value is little-endian
1504 and ENC_BIG_ENDIAN if it is big-endian.
1506 For FT_IPv4 fields, the encoding also specifies the byte order of the
1507 value. In almost all cases, the encoding is in network byte order,
1508 hence big-endian, but in at least one protocol dissected by Wireshark,
1509 at least one IPv4 address is byte-swapped, so it's in little-endian
1512 For string fields, the encoding specifies the character set used for the
1513 string and the way individual code points in that character set are
1514 encoded. For FT_UINT_STRING fields, the byte order of the count must be
1515 specified. For UTF-16, UCS-2, and UCS-4, the byte order of the encoding
1516 must be specified, and optionally ENC_BOM can also be indicated to detect
1517 an initial BYTE ORDER MARK (the specified value is used if the field does
1518 not begin with a BOM.) For counted UTF-16, UCS-2, and UCS-4 strings, the
1519 byte order of the count and the characters in the string must be the same,
1520 unless a BOM overrides the value for the characters. In other cases the
1521 string encoding has no endianness or the endianness is implicitly specified
1522 and nothing should be used. The character encodings that are currently
1525 ENC_ASCII - ASCII (currently treated as UTF-8; in the future,
1526 all bytes with the 8th bit set will be treated as
1528 ENC_UTF_8 - UTF-8-encoded Unicode
1529 ENC_UTF_16 - UTF-16-encoded Unicode, with surrogate pairs
1530 ENC_UCS_2 - UCS-2-encoded subset of Unicode, with no surrogate pairs
1531 and thus no code points above 0xFFFF
1532 ENC_UCS_4 - UCS-4-encoded Unicode (aka UTF-32)
1533 ENC_WINDOWS_1250 - Windows-1250 code page
1534 ENC_WINDOWS_1251 - Windows-1251 code page
1535 ENC_WINDOWS_1252 - Windows-1252 code page
1536 ENC_ISO_646_BASIC - ISO 646 "basic code table"
1537 ENC_ISO_8859_1 - ISO 8859-1
1538 ENC_ISO_8859_2 - ISO 8859-2
1539 ENC_ISO_8859_3 - ISO 8859-3
1540 ENC_ISO_8859_4 - ISO 8859-4
1541 ENC_ISO_8859_5 - ISO 8859-5
1542 ENC_ISO_8859_6 - ISO 8859-6
1543 ENC_ISO_8859_7 - ISO 8859-7
1544 ENC_ISO_8859_8 - ISO 8859-8
1545 ENC_ISO_8859_9 - ISO 8859-9
1546 ENC_ISO_8859_10 - ISO 8859-10
1547 ENC_ISO_8859_11 - ISO 8859-11
1548 ENC_ISO_8859_13 - ISO 8859-13
1549 ENC_ISO_8859_14 - ISO 8859-14
1550 ENC_ISO_8859_15 - ISO 8859-15
1551 ENC_ISO_8859_16 - ISO 8859-16
1552 ENC_3GPP_TS_23_038_7BITS - GSM 7 bits alphabet as described
1554 ENC_3GPP_TS_23_038_7BITS_UNPACKED - GSM 7 bits alphabet where each
1555 7 bit character occupies a distinct octet
1556 ENC_ETSI_TS_102_221_ANNEX_A - Coding scheme for SIM cards with GSM 7 bit
1557 alphabet, UCS-2 characters, or a mixture of the two as described
1558 in ETSI TS 102 221 Annex A
1560 ENC_EBCDIC_CP037 - EBCDIC code page 037
1561 ENC_EBCDIC_CP500 - EBCDIC code page 500
1562 ENC_MAC_ROMAN - MAC ROMAN
1563 ENC_CP437 - DOS code page 437
1564 ENC_CP855 - DOS code page 855
1565 ENC_CP866 - DOS code page 866
1566 ENC_ASCII_7BITS - 7 bits ASCII
1568 ENC_BCD_DIGITS_0_9 - packed BCD (one digit per nibble), digits 0-9
1569 ENC_KEYPAD_ABC_TBCD - keypad-with-a/b/c "telephony packed BCD" = 0-9, *, #, a, b, c
1570 ENC_KEYPAD_BC_TBCD - keypad-with-B/C "telephony packed BCD" = 0-9, B, C, *, #
1571 ENC_GB18030 - GB 18030
1573 ENC_DECT_STANDARD_8BITS - DECT standard 8 bit character set as defined in
1575 ENC_DECT_STANDARD_4BITS_TBCD - DECT standard 4 bit character set "telephony
1576 packet BCD" = 0-9, 0xb = SPACE
1578 Other encodings will be added in the future.
1580 For FT_ABSOLUTE_TIME fields, the encoding specifies the form in which
1581 the time stamp is specified, as well as its byte order. The time stamp
1582 encodings that are currently supported are:
1584 ENC_TIME_SECS_NSECS - 8, 12, or 16 bytes. For 8 bytes, the first 4
1585 bytes are seconds and the next 4 bytes are nanoseconds; for 12
1586 bytes, the first 8 bytes are seconds and the next 4 bytes are
1587 nanoseconds; for 16 bytes, the first 8 bytes are seconds and
1588 the next 8 bytes are nanoseconds. The seconds are seconds
1589 since the UN*X epoch (1970-01-01 00:00:00 UTC). (I.e., a UN*X
1590 struct timespec with a 4-byte or 8-byte time_t or a structure
1591 with an 8-byte time_t and an 8-byte nanoseconds field.)
1593 ENC_TIME_NTP - 8 bytes; the first 4 bytes are seconds since the NTP
1594 epoch (1900-01-01 00:00:00 GMT) and the next 4 bytes are 1/2^32's of
1595 a second since that second. (I.e., a 64-bit count of 1/2^32's of a
1596 second since the NTP epoch, with the upper 32 bits first and the
1597 lower 32 bits second, even when little-endian.)
1599 ENC_TIME_TOD - 8 bytes, as a count of microseconds since the System/3x0
1600 and z/Architecture epoch (1900-01-01 00:00:00 GMT).
1602 ENC_TIME_RTPS - 8 bytes; the first 4 bytes are seconds since the UN*X
1603 epoch and the next 4 bytes are 1/2^32's of a second since that
1604 second. (I.e., it's the offspring of a mating between UN*X time and
1605 NTP time). It's used by the Object Management Group's Real-Time
1606 Publish-Subscribe Wire Protocol for the Data Distribution Service.
1608 ENC_TIME_SECS_USECS - 8 bytes; the first 4 bytes are seconds since the
1609 UN*X epoch and the next 4 bytes are microseconds since that
1610 second. (I.e., a UN*X struct timeval with a 4-byte time_t.)
1612 ENC_TIME_SECS - 4 to 8 bytes, representing a value in seconds since
1615 ENC_TIME_MSECS - 6 to 8 bytes, representing a value in milliseconds
1616 since the UN*X epoch.
1618 ENC_TIME_USECS - 8 bytes, representing a value in microseconds since
1621 ENC_TIME_NSECS - 8 bytes, representing a value in nanoseconds since
1624 ENC_TIME_SECS_NTP - 4 bytes, representing a count of seconds since
1627 ENC_TIME_RFC_3971 - 8 bytes, representing a count of 1/64ths of a
1628 second since the UN*X epoch; see section 5.3.1 "Timestamp Option"
1631 ENC_TIME_MSEC_NTP - 4-8 bytes, representing a count of milliseconds since
1634 ENC_TIME_MIP6 - 8 bytes; the first 48 bits are seconds since the UN*X epoch
1635 and the remaining 16 bits indicate the number of 1/65536's of a second
1638 ENC_TIME_CLASSIC_MAC_OS_SECS - 4-8 bytes, representing a count of seconds
1639 since January 1, 1904, 00:00:00 UTC.
1641 For FT_RELATIVE_TIME fields, the encoding specifies the form in which
1642 the time stamp is specified, as well as its byte order. The time stamp
1643 encodings that are currently supported are:
1645 ENC_TIME_SECS_NSECS - 8, 12, or 16 bytes. For 8 bytes, the first 4
1646 bytes are seconds and the next 4 bytes are nanoseconds; for 12
1647 bytes, the first 8 bytes are seconds and the next 4 bytes are
1648 nanoseconds; for 16 bytes, the first 8 bytes are seconds and
1649 the next 8 bytes are nanoseconds.
1651 ENC_TIME_SECS_USECS - 8 bytes; the first 4 bytes are seconds and the
1652 next 4 bytes are microseconds.
1654 ENC_TIME_SECS - 4 to 8 bytes, representing a value in seconds.
1656 ENC_TIME_MSECS - 6 to 8 bytes, representing a value in milliseconds.
1658 ENC_TIME_USECS - 8 bytes, representing a value in microseconds.
1660 ENC_TIME_NSECS - 8 bytes, representing a value in nanoseconds.
1662 For other types, there is no support for proto_tree_add_item().
1664 Now that definitions of fields have detailed information about bitfield
1665 fields, you can use proto_tree_add_item() with no extra processing to
1666 add bitfield values to your tree. Here's an example. Take the Format
1667 Identifier (FID) field in the Transmission Header (TH) portion of the SNA
1668 protocol. The FID is the high nibble of the first byte of the TH. The
1669 FID would be registered like this:
1671 name = "Format Identifier"
1672 abbrev = "sna.th.fid"
1675 strings = sna_th_fid_vals
1678 The bitmask contains the value which would leave only the FID if bitwise-ANDed
1679 against the parent field, the first byte of the TH.
1681 The code to add the FID to the tree would be;
1683 proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1,
1686 The definition of the field already has the information about bitmasking
1687 and bitshifting, so it does the work of masking and shifting for us!
1688 This also means that you no longer have to create value_string structs
1689 with the values bitshifted. The value_string for FID looks like this,
1690 even though the FID value is actually contained in the high nibble.
1691 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
1693 /* Format Identifier */
1694 static const value_string sna_th_fid_vals[] = {
1695 { 0x0, "SNA device <--> Non-SNA Device" },
1696 { 0x1, "Subarea Node <--> Subarea Node" },
1697 { 0x2, "Subarea Node <--> PU2" },
1698 { 0x3, "Subarea Node or SNA host <--> Subarea Node" },
1701 { 0xf, "Adjacent Subarea Nodes" },
1705 The final implication of this is that display filters work the way you'd
1706 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
1707 Subarea Nodes. The user does not have to shift the value of the FID to
1708 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
1711 proto_tree_add_item_ret_XXX()
1712 ------------------------------
1713 proto_tree_add_item_ret_XXX is used when you want the displayed value returned
1714 for further processing only integer and unsigned integer types up to 32 bits are
1715 supported usage of proper FT_ is checked.
1717 proto_tree_add_XXX_item()
1718 ---------------------
1719 proto_tree_add_XXX_item is used when you wish to do no special formatting,
1720 but also either wish for the retrieved value from the tvbuff to be handed
1721 back (to avoid doing tvb_get_...), and/or wish to have the value be decoded
1722 from the tvbuff in a string-encoded format.
1724 The item added to the GUI tree will contain the name (as passed in the
1725 proto_register_*() function) and a value. The value will be fetched
1726 from the tvbuff, based on the type of the XXX name and the encoding of
1727 the value as specified by the "encoding" argument.
1729 This function retrieves the value even if the passed-in tree param is NULL,
1730 so that it can be used by dissectors at all times to both get the value
1731 and set the tree item to it.
1733 Like other proto_tree_add functions, if there is a tree and the value cannot
1734 be decoded from the tvbuff, then an expert info error is reported. For string
1735 encoding, this means that a failure to decode the hex value from the string
1736 results in an expert info error being added to the tree.
1738 For string-decoding, the passed-in encoding argument needs to specify the
1739 string encoding (e.g., ENC_ASCII, ENC_UTF_8) as well as the format. For
1740 some XXX types, the format is constrained - for example for the encoding format
1741 for proto_tree_add_time_item() can only be one of the ENC_ISO_8601_* ones
1742 or ENC_IMF_DATE_TIME. For proto_tree_add_bytes_item() it can only
1743 be ENC_STR_HEX bit-or'ed with one or more of the ENC_SEP_* separator types.
1745 proto_tree_add_protocol_format()
1746 --------------------------------
1747 proto_tree_add_protocol_format is used to add the top-level item for the
1748 protocol when the dissector routine wants complete control over how the
1749 field and value will be represented on the GUI tree. The ID value for
1750 the protocol is passed in as the "id" argument; the rest of the
1751 arguments are a "printf"-style format and any arguments for that format.
1752 The caller must include the name of the protocol in the format; it is
1753 not added automatically as in proto_tree_add_item().
1755 proto_tree_add_none_format()
1756 ----------------------------
1757 proto_tree_add_none_format is used to add an item of type FT_NONE.
1758 The caller must include the name of the field in the format; it is
1759 not added automatically as in proto_tree_add_item().
1761 proto_tree_add_bytes()
1762 proto_tree_add_time()
1763 proto_tree_add_ipxnet()
1764 proto_tree_add_ipv4()
1765 proto_tree_add_ipv6()
1766 proto_tree_add_ether()
1767 proto_tree_add_string()
1768 proto_tree_add_boolean()
1769 proto_tree_add_float()
1770 proto_tree_add_double()
1771 proto_tree_add_uint()
1772 proto_tree_add_uint64()
1773 proto_tree_add_int()
1774 proto_tree_add_int64()
1775 proto_tree_add_guid()
1776 proto_tree_add_oid()
1777 proto_tree_add_eui64()
1778 ------------------------
1779 These routines are used to add items to the protocol tree if either:
1781 the value of the item to be added isn't just extracted from the
1782 packet data, but is computed from data in the packet;
1784 the value was fetched into a variable.
1786 The 'value' argument has the value to be added to the tree.
1788 NOTE: in all cases where the 'value' argument is a pointer, a copy is
1789 made of the object pointed to; if you have dynamically allocated a
1790 buffer for the object, that buffer will not be freed when the protocol
1791 tree is freed - you must free the buffer yourself when you don't need it
1794 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
1798 proto_tree_add_bytes_with_length() is similar to proto_tree_add_bytes,
1799 except that the length is not derived from the tvb length. Instead,
1800 the displayed data size is controlled by 'ptr_length'.
1802 For proto_tree_add_bytes_format() and proto_tree_add_bytes_format_value(), the
1803 'value_ptr' argument is a pointer to a sequence of bytes or NULL if the bytes
1804 should be taken from the given TVB using the given offset and length.
1806 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
1807 "nstime_t", which is a structure containing the time to be added; it has
1808 'secs' and 'nsecs' members, giving the integral part and the fractional
1809 part of a time in units of seconds, with 'nsecs' being the number of
1810 nanoseconds. For absolute times, "secs" is a UNIX-style seconds since
1811 January 1, 1970, 00:00:00 GMT value.
1813 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
1816 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
1817 address, in network byte order.
1819 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
1820 128-bit IPv6 address.
1822 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
1825 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
1826 text string; this string must be NULL terminated even if the string in the
1827 TVB is not (as may be the case with FT_STRINGs).
1829 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer.
1830 It is masked and shifted as defined by the field info after which zero
1831 means "false", and non-zero means "true".
1833 For proto_tree_add_float(), the 'value' argument is a 'float' in the
1834 host's floating-point format.
1836 For proto_tree_add_double(), the 'value' argument is a 'double' in the
1837 host's floating-point format.
1839 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
1840 integer value, in host byte order. (This routine cannot be used to add
1843 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
1844 integer value, in host byte order.
1846 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
1847 integer value, in host byte order. (This routine cannot be used to add
1850 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
1851 integer value, in host byte order.
1853 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
1856 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
1857 ASN.1 Object Identifier.
1859 For proto_tree_add_eui64(), the 'value' argument is a 64-bit integer
1862 proto_tree_add_bytes_format()
1863 proto_tree_add_time_format()
1864 proto_tree_add_ipxnet_format()
1865 proto_tree_add_ipv4_format()
1866 proto_tree_add_ipv6_format()
1867 proto_tree_add_ether_format()
1868 proto_tree_add_string_format()
1869 proto_tree_add_boolean_format()
1870 proto_tree_add_float_format()
1871 proto_tree_add_double_format()
1872 proto_tree_add_uint_format()
1873 proto_tree_add_uint64_format()
1874 proto_tree_add_int_format()
1875 proto_tree_add_int64_format()
1876 proto_tree_add_guid_format()
1877 proto_tree_add_oid_format()
1878 proto_tree_add_eui64_format()
1879 ----------------------------
1880 These routines are used to add items to the protocol tree when the
1881 dissector routine wants complete control over how the field and value
1882 will be represented on the GUI tree. The argument giving the value is
1883 the same as the corresponding proto_tree_add_XXX() function; the rest of
1884 the arguments are a "printf"-style format and any arguments for that
1885 format. The caller must include the name of the field in the format; it
1886 is not added automatically as in the proto_tree_add_XXX() functions.
1888 proto_tree_add_bytes_format_value()
1889 proto_tree_add_time_format_value()
1890 proto_tree_add_ipxnet_format_value()
1891 proto_tree_add_ipv4_format_value()
1892 proto_tree_add_ipv6_format_value()
1893 proto_tree_add_ether_format_value()
1894 proto_tree_add_string_format_value()
1895 proto_tree_add_boolean_format_value()
1896 proto_tree_add_float_format_value()
1897 proto_tree_add_double_format_value()
1898 proto_tree_add_uint_format_value()
1899 proto_tree_add_uint64_format_value()
1900 proto_tree_add_int_format_value()
1901 proto_tree_add_int64_format_value()
1902 proto_tree_add_guid_format_value()
1903 proto_tree_add_oid_format_value()
1904 proto_tree_add_eui64_format_value()
1905 ------------------------------------
1907 These routines are used to add items to the protocol tree when the
1908 dissector routine wants complete control over how the value will be
1909 represented on the GUI tree. The argument giving the value is the same
1910 as the corresponding proto_tree_add_XXX() function; the rest of the
1911 arguments are a "printf"-style format and any arguments for that format.
1912 With these routines, unlike the proto_tree_add_XXX_format() routines,
1913 the name of the field is added automatically as in the
1914 proto_tree_add_XXX() functions; only the value is added with the format.
1915 One use case for this would be to add a unit of measurement string to
1916 the value of the field, however using BASE_UNIT_STRING in the hf_
1917 definition is now preferred.
1919 proto_tree_add_checksum()
1920 ----------------------------
1921 proto_tree_add_checksum is used to add a checksum field. The hf field
1922 provided must be the correct size of the checksum (FT_UINT, FT_UINT16,
1923 FT_UINT32, etc). Additional parameters are there to provide "status"
1924 and expert info depending on whether the checksum matches the provided
1925 value. The "status" and expert info can be used in cases except
1926 where PROTO_CHECKSUM_NO_FLAGS is used.
1928 proto_tree_add_subtree()
1929 ---------------------
1930 proto_tree_add_subtree() is used to add a label to the GUI tree and create
1931 a subtree for other fields. It will contain no value, so it is not searchable
1932 in the display filter process.
1934 This should only be used for items with subtrees, which may not
1935 have values themselves - the items in the subtree are the ones with values.
1937 For a subtree, the label on the subtree might reflect some of the items
1938 in the subtree. This means the label can't be set until at least some
1939 of the items in the subtree have been dissected. To do this, use
1940 'proto_item_set_text()' or 'proto_item_append_text()':
1943 proto_item_set_text(proto_item *ti, ...);
1946 proto_item_append_text(proto_item *ti, ...);
1948 'proto_item_set_text()' takes as an argument the proto_item value returned by
1949 one of the parameters in 'proto_tree_add_subtree()', a 'printf'-style format
1950 string, and a set of arguments corresponding to '%' format items in that string,
1951 and replaces the text for the item created by 'proto_tree_add_subtree()' with the result
1952 of applying the arguments to the format string.
1954 'proto_item_append_text()' is similar, but it appends to the text for
1955 the item the result of applying the arguments to the format string.
1957 For example, early in the dissection, one might do:
1959 subtree = proto_tree_add_subtree(tree, tvb, offset, length, ett, &ti, <label>);
1963 proto_item_set_text(ti, "%s: %s", type, value);
1965 after the "type" and "value" fields have been extracted and dissected.
1966 <label> would be a label giving what information about the subtree is
1967 available without dissecting any of the data in the subtree.
1969 Note that an exception might be thrown when trying to extract the values of
1970 the items used to set the label, if not all the bytes of the item are
1971 available. Thus, one should create the item with text that is as
1972 meaningful as possible, and set it or append additional information to
1973 it as the values needed to supply that information are extracted.
1975 proto_tree_add_subtree_format()
1976 ----------------------------
1977 This is like proto_tree_add_subtree(), but uses printf-style arguments to
1978 create the label; it is used to allow routines that take a printf-like
1979 variable-length list of arguments to add a text item to the protocol
1982 proto_tree_add_bits_item()
1983 --------------------------
1984 Adds a number of bits to the protocol tree which does not have to be byte
1985 aligned. The offset and length is in bits.
1988 ..10 1010 10.. .... "value" (formatted as FT_ indicates).
1990 proto_tree_add_bits_ret_val()
1991 -----------------------------
1992 Works in the same way but also returns the value of the read bits.
1994 proto_tree_add_split_bits_item_ret_val()
1995 -----------------------------------
1996 Similar, but is used for items that are made of 2 or more smaller sets of bits (crumbs)
1997 which are not contiguous, but are concatenated to form the actual value. The size of
1998 the crumbs and the order of assembly are specified in an array of crumb_spec structures.
2000 proto_tree_add_split_bits_crumb()
2001 ---------------------------------
2002 Helper function for the above, to add text for each crumb as it is encountered.
2004 proto_tree_add_ts_23_038_7bits_item()
2005 -------------------------------------
2006 Adds a string of a given number of characters and encoded according to 3GPP TS 23.038 7 bits
2009 proto_tree_add_bitmask() et al.
2010 -------------------------------
2011 These functions provide easy to use and convenient dissection of many types of common
2012 bitmasks into individual fields.
2014 header is an integer type and must be of type FT_[U]INT{8|16|24|32||40|48|56|64} and
2015 represents the entire dissectable width of the bitmask.
2017 'header' and 'ett' are the hf fields and ett field respectively to create an
2018 expansion that covers the bytes of the bitmask.
2020 'fields' is a NULL terminated array of pointers to hf fields representing
2021 the individual subfields of the bitmask. These fields must either be integers
2022 (usually of the same byte width as 'header') or of the type FT_BOOLEAN.
2023 Each of the entries in 'fields' will be dissected as an item under the
2024 'header' expansion and also IF the field is a boolean and IF it is set to 1,
2025 then the name of that boolean field will be printed on the 'header' expansion
2026 line. For integer type subfields that have a value_string defined, the
2027 matched string from that value_string will be printed on the expansion line
2030 Example: (from the SCSI dissector)
2031 static int hf_scsi_inq_peripheral;
2032 static int hf_scsi_inq_qualifier;
2033 static int hf_scsi_inq_devtype;
2035 static int ett_scsi_inq_peripheral;
2037 static int * const peripheral_fields[] = {
2038 &hf_scsi_inq_qualifier,
2039 &hf_scsi_inq_devtype,
2043 /* Qualifier and DeviceType */
2044 proto_tree_add_bitmask(tree, tvb, offset, hf_scsi_inq_peripheral,
2045 ett_scsi_inq_peripheral, peripheral_fields, ENC_BIG_ENDIAN);
2048 { &hf_scsi_inq_peripheral,
2049 {"Peripheral", "scsi.inquiry.peripheral", FT_UINT8, BASE_HEX,
2050 NULL, 0, NULL, HFILL}
2052 { &hf_scsi_inq_qualifier,
2053 {"Qualifier", "scsi.inquiry.qualifier", FT_UINT8, BASE_HEX,
2054 VALS (scsi_qualifier_val), 0xE0, NULL, HFILL}
2056 { &hf_scsi_inq_devtype,
2057 {"Device Type", "scsi.inquiry.devtype", FT_UINT8, BASE_HEX,
2058 VALS (scsi_devtype_val), SCSI_DEV_BITS, NULL, HFILL}
2062 Which provides very pretty dissection of this one byte bitmask.
2064 Peripheral: 0x05, Qualifier: Device type is connected to logical unit, Device Type: CD-ROM
2065 000. .... = Qualifier: Device type is connected to logical unit (0x00)
2066 ...0 0101 = Device Type: CD-ROM (0x05)
2068 The proto_tree_add_bitmask_text() function is an extended version of
2069 the proto_tree_add_bitmask() function. In addition, it allows to:
2070 - Provide a leading text (e.g. "Flags: ") that will appear before
2071 the comma-separated list of field values
2072 - Provide a fallback text (e.g. "None") that will be appended if
2073 no fields warranted a change to the top-level title.
2074 - Using flags, specify which fields will affect the top-level title.
2076 There are the following flags defined:
2078 BMT_NO_APPEND - the title is taken "as-is" from the 'name' argument.
2079 BMT_NO_INT - only boolean flags are added to the title.
2080 BMT_NO_FALSE - boolean flags are only added to the title if they are set.
2081 BMT_NO_TFS - only add flag name to the title, do not use true_false_string
2083 The proto_tree_add_bitmask_with_flags() function is an extended version
2084 of the proto_tree_add_bitmask() function. It allows using flags to specify
2085 which fields will affect the top-level title. The flags are the
2086 same BMT_NO_* flags as used in the proto_tree_add_bitmask_text() function.
2088 The proto_tree_add_bitmask() behavior can be obtained by providing
2089 both 'name' and 'fallback' arguments as NULL, and a flags of
2090 (BMT_NO_FALSE|BMT_NO_TFS).
2092 The proto_tree_add_bitmask_len() function is intended for protocols where
2093 bitmask length is permitted to vary, so a length is specified explicitly
2094 along with the bitmask value. USB Video "bmControl" and "bControlSize"
2095 fields follow this pattern. The primary intent of this is "forward
2096 compatibility," enabling an interpreter coded for version M of a structure
2097 to comprehend fields in version N of the structure, where N > M and
2098 bControlSize increases from version M to version N.
2100 proto_tree_add_bitmask_len() is an extended version of proto_tree_add_bitmask()
2101 that uses an explicitly specified (rather than inferred) length to control
2102 dissection. Because of this, it may encounter two cases that
2103 proto_tree_add_bitmask() and proto_tree_add_bitmask_text() may not:
2104 - A length that exceeds that of the 'header' and bitmask subfields.
2105 In this case the least-significant bytes of the bitmask are dissected.
2106 An expert warning is generated in this case, because the dissection code
2107 likely needs to be updated for a new revision of the protocol.
2108 - A length that is shorter than that of the 'header' and bitmask subfields.
2109 In this case, subfields whose data is fully present are dissected,
2110 and other subfields are not. No warning is generated in this case,
2111 because the dissection code is likely for a later revision of the protocol
2112 than the packet it was called to interpret.
2115 proto_item_set_generated()
2116 --------------------------
2117 proto_item_set_generated is used to mark fields as not being read from the
2118 captured data directly, but inferred from one or more values.
2120 One of the primary uses of this is the presentation of verification of
2121 checksums. Every IP packet has a checksum line, which can present the result
2122 of the checksum verification, if enabled in the preferences. The result is
2123 presented as a subtree, where the result is enclosed in square brackets
2124 indicating a generated field.
2126 Header checksum: 0x3d42 [correct]
2127 [Checksum Status: Good (1)]
2129 proto_item_set_hidden()
2130 -----------------------
2131 proto_item_set_hidden is used to hide fields, which have already been added
2132 to the tree, from being visible in the displayed tree.
2134 NOTE that creating hidden fields is actually quite a bad idea from a UI design
2135 perspective because the user (someone who did not write nor has ever seen the
2136 code) has no way of knowing that hidden fields are there to be filtered on
2137 thus defeating the whole purpose of putting them there. A Better Way might
2138 be to add the fields (that might otherwise be hidden) to a subtree where they
2139 won't be seen unless the user opens the subtree--but they can be found if the
2142 One use for hidden fields (which would be better implemented using visible
2143 fields in a subtree) follows: The caller may want a value to be
2144 included in a tree so that the packet can be filtered on this field, but
2145 the representation of that field in the tree is not appropriate. An
2146 example is the token-ring routing information field (RIF). The best way
2147 to show the RIF in a GUI is by a sequence of ring and bridge numbers.
2148 Rings are 3-digit hex numbers, and bridges are single hex digits:
2150 RIF: 001-A-013-9-C0F-B-555
2152 In the case of RIF, the programmer should use a field with no value and
2153 use proto_tree_add_none_format() to build the above representation. The
2154 programmer can then add the ring and bridge values, one-by-one, with
2155 proto_tree_add_item() and hide them with proto_item_set_hidden() so that the
2156 user can then filter on or search for a particular ring or bridge. Here's a
2157 skeleton of how the programmer might code this.
2160 rif = create_rif_string(...);
2162 proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
2164 for (i = 0; i < num_rings; i++) {
2167 pi = proto_tree_add_item(tree, hf_tr_rif_ring, ..., ENC_BIG_ENDIAN);
2168 proto_item_set_hidden(pi);
2170 for (i = 0; i < num_rings - 1; i++) {
2173 pi = proto_tree_add_item(tree, hf_tr_rif_bridge, ..., ENC_BIG_ENDIAN);
2174 proto_item_set_hidden(pi);
2177 The logical tree has these items:
2179 hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
2180 hf_tr_rif_ring, hidden, value=0x001
2181 hf_tr_rif_bridge, hidden, value=0xA
2182 hf_tr_rif_ring, hidden, value=0x013
2183 hf_tr_rif_bridge, hidden, value=0x9
2184 hf_tr_rif_ring, hidden, value=0xC0F
2185 hf_tr_rif_bridge, hidden, value=0xB
2186 hf_tr_rif_ring, hidden, value=0x555
2188 GUI or print code will not display the hidden fields, but a display
2189 filter or "packet grep" routine will still see the values. The possible
2190 filter is then possible:
2192 tr.rif_ring eq 0x013
2196 proto_item_set_url is used to mark fields as containing a URL. This can only
2197 be done with fields of type FT_STRING(Z). If these fields are presented they
2198 are underlined, as could be done in a browser. These fields are sensitive to
2199 clicks as well, launching the configured browser with this URL as parameter.
2201 1.6 Utility routines.
2203 1.6.1 val_to_str, val_to_str_const, try_val_to_str and try_val_to_str_idx
2205 A dissector may need to convert a value to a string, using a
2206 'value_string' structure, by hand, rather than by declaring a field with
2207 an associated 'value_string' structure; this might be used, for example,
2208 to generate a COL_INFO line for a frame.
2210 val_to_str() handles the most common case:
2213 val_to_str(uint32_t val, const value_string *vs, const char *fmt)
2215 If the value 'val' is found in the 'value_string' table pointed to by
2216 'vs', 'val_to_str' will return the corresponding string; otherwise, it
2217 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
2218 to generate a string, and will return a pointer to that string.
2219 You can use it in a call to generate a COL_INFO line for a frame such as
2221 col_add_fstr(COL_INFO, ", %s", val_to_str(val, table, "Unknown %d"));
2223 If you don't need to display 'val' in your fmt string, you can use
2224 val_to_str_const() which just takes a string constant instead and returns it
2225 unmodified when 'val' isn't found.
2227 If you need to handle the failure case in some custom way, try_val_to_str()
2228 will return NULL if val isn't found:
2231 try_val_to_str(uint32_t val, const value_string *vs)
2233 Note that, you must check whether 'try_val_to_str()' returns NULL, and arrange
2234 that its return value not be dereferenced if it's NULL. 'try_val_to_str_idx()'
2235 behaves similarly, except it also returns an index into the value_string array,
2236 or -1 if 'val' was not found.
2238 The *_ext functions are "extended" versions of those already described. They
2239 should be used for large value-string arrays which contain many entries. They
2240 implement value to string conversions which will do either a direct access or
2241 a binary search of the value string array if possible. See
2242 "Extended Value Strings" under section 1.6 "Constructing the protocol tree" for
2245 See epan/value_string.h for detailed information on the various value_string
2248 To handle 64-bit values, there are an equivalent set of functions. These are:
2251 val64_to_str(const uint64_t val, const val64_string *vs, const char *fmt)
2254 val64_to_str_const(const uint64_t val, const val64_string *vs, const char *unknown_str);
2257 try_val64_to_str(const uint64_t val, const val64_string *vs);
2260 try_val64_to_str_idx(const uint64_t val, const val64_string *vs, int *idx);
2263 1.6.2 rval_to_str, try_rval_to_str and try_rval_to_str_idx
2265 A dissector may need to convert a range of values to a string, using a
2266 'range_string' structure.
2268 Most of the same functions exist as with regular value_strings (see section
2269 1.6.1) except with the names 'rval' instead of 'val'.
2272 1.7 Calling Other Dissectors.
2274 As each dissector completes its portion of the protocol analysis, it
2275 is expected to create a new tvbuff of type TVBUFF_SUBSET which
2276 contains the payload portion of the protocol (that is, the bytes
2277 that are relevant to the next dissector).
2279 To create a new TVBUFF_SUBSET that begins at a specified offset in a
2280 parent tvbuff, and runs to the end of the parent tvbuff, the routine
2281 tvbuff_new_subset_remaining() is used:
2283 next_tvb = tvb_new_subset_remaining(tvb, offset);
2286 tvb is the tvbuff that the dissector has been working on. It
2287 can be a tvbuff of any type.
2289 next_tvb is the new TVBUFF_SUBSET.
2291 offset is the byte offset of 'tvb' at which the new tvbuff
2292 should start. The first byte is the byte at offset 0.
2294 To create a new TVBUFF_SUBSET that begins at a specified offset in a
2295 parent tvbuff, with a specified number of bytes in the payload, the
2296 routine tvbuff_new_subset_length() is used:
2298 next_tvb = tvb_new_subset_length(tvb, offset, reported_length);
2301 tvb is the tvbuff that the dissector has been working on. It
2302 can be a tvbuff of any type.
2304 next_tvb is the new TVBUFF_SUBSET.
2306 offset is the byte offset of 'tvb' at which the new tvbuff
2307 should start. The first byte is the byte at offset 0.
2309 reported_length is the number of bytes that the current protocol
2310 says should be in the payload.
2312 In the few cases where the number of bytes available in the new subset
2313 must be explicitly specified, rather than being calculated based on the
2314 number of bytes in the payload, the routine tvb_new_subset_length_caplen()
2317 next_tvb = tvb_new_subset_length_caplen(tvb, offset, length, reported_length);
2320 tvb is the tvbuff that the dissector has been working on. It
2321 can be a tvbuff of any type.
2323 next_tvb is the new TVBUFF_SUBSET.
2325 offset is the byte offset of 'tvb' at which the new tvbuff
2326 should start. The first byte is the byte at offset 0.
2328 length is the number of bytes in the new TVBUFF_SUBSET. A length
2329 argument of -1 says to use as many bytes as are available in
2332 reported_length is the number of bytes that the current protocol
2333 says should be in the payload. A reported_length of -1 says that
2334 the protocol doesn't say anything about the size of its payload.
2336 To call a dissector you need to get the handle of the dissector using
2337 find_dissector(), passing it the string name of the dissector. The setting
2338 of the handle is usually done once at startup during the proto_reg_handoff
2339 function within the calling dissector.
2341 1.7.1 Dissector Tables
2343 Another way to call a subdissector is to setup a dissector table. A dissector
2344 table is a list of subdissectors grouped by a common identifier (integer or
2345 string) in a dissector. Subdissectors will register themselves with the dissector
2346 table using their unique identifier using one of the following APIs:
2348 void dissector_add_uint(const char *abbrev, const uint32_t pattern,
2349 dissector_handle_t handle);
2351 void dissector_add_uint_range(const char *abbrev, struct epan_range *range,
2352 dissector_handle_t handle);
2354 void dissector_add_string(const char *name, const char *pattern,
2355 dissector_handle_t handle);
2357 void dissector_add_for_decode_as(const char *name,
2358 dissector_handle_t handle);
2360 dissector_add_for_decode_as doesn't add a unique identifier in the dissector
2361 table, but it lets the user add it from the command line or, in Wireshark,
2362 through the "Decode As" UI.
2364 Then when the dissector hits the common identifier field, it will use one of the
2365 following APIs to invoke the subdissector:
2367 int dissector_try_uint(dissector_table_t sub_dissectors,
2368 const uint32_t uint_val, tvbuff_t *tvb, packet_info *pinfo,
2371 int dissector_try_uint_new(dissector_table_t sub_dissectors,
2372 const uint32_t uint_val, tvbuff_t *tvb, packet_info *pinfo,
2373 proto_tree *tree, const bool add_proto_name, void *data);
2375 int dissector_try_string(dissector_table_t sub_dissectors, const char *string,
2376 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data);
2378 These pass a subset of the remaining packet (typically the rest of the
2379 packet) for the dissector table to determine which subdissector is called.
2380 This allows dissection of a packet to be expanded outside of dissector without
2381 having to modify the dissector directly.
2384 1.8 Editing CMakeLists.txt to add your dissector.
2386 To arrange that your dissector will be built as part of Wireshark, you
2387 must add the name of the source file for your dissector to the DISSECTOR_SRC
2388 section of epan/dissectors/CMakeLists.txt
2391 1.9 Using the git source code tree.
2393 See <https://www.wireshark.org/develop.html>
2396 1.10 Submitting code for your new dissector.
2398 See <https://www.wireshark.org/docs/wsdg_html_chunked/ChSrcContribute.html>
2399 and <https://wiki.wireshark.org/Development/SubmittingPatches>.
2401 - VERIFY that your dissector code does not use prohibited or deprecated APIs
2403 perl <wireshark_root>/tools/checkAPIs.pl <source-filename(s)>
2405 - VERIFY that your dissector code does not contain any header field related
2407 perl <wireshark_root>/tools/checkhf.pl <source-filename(s)>
2409 - VERIFY that your dissector code does not contain any display filter related
2411 perl <wireshark_root>/tools/checkfiltername.pl <source-filename(s)>
2413 - CHECK your dissector with CppCheck (http://cppcheck.sourceforge.net/) using
2414 Wireshark's customized configuration. This is particularly important on
2415 Windows, since Microsoft's compiler warnings are quite thin:
2416 ./tools/cppcheck/cppcheck.sh <source-filename(s)>
2418 - TEST YOUR DISSECTOR BEFORE SUBMITTING IT.
2419 Use fuzz-test.sh and/or randpkt against your dissector. These are
2420 described at <https://wiki.wireshark.org/FuzzTesting>.
2422 - Subscribe to <mailto:wireshark-dev[AT]wireshark.org> by sending an email to
2423 <mailto:wireshark-dev-request[AT]wireshark.org?body="help"> or visiting
2424 <https://lists.wireshark.org/>.
2426 - 'git diff' to verify all your changes look good.
2428 - 'git add' all the files you changed.
2430 - 'git commit' to commit (locally) your changes. First line of commit message
2431 should be a summary of the changes followed by an empty line and a more
2432 verbose description.
2434 - 'git push downstream HEAD' to push the changes to GitLab. (This assumes
2435 that you have a remote named "downstream" that points to a fork of
2436 https://gitlab.com/wireshark/wireshark.)
2438 - Create a Wiki page on the protocol at <https://gitlab.com/wireshark/editor-wiki>.
2439 (You'll need to request access to https://gitlab.com/wireshark/wiki-editors.)
2440 A template is provided so it is easy to setup in a consistent style.
2441 See: <https://wiki.wireshark.org/HowToEdit>
2442 and <https://wiki.wireshark.org/ProtocolReference>
2444 - If possible, add sample capture files to the sample captures page at
2445 <https://wiki.wireshark.org/SampleCaptures>. These
2446 files are used by the automated build system for fuzz testing.
2448 - If you don't think the wiki is the right place for your sample capture,
2449 submit a bug report to the Wireshark issue database, found at
2450 <https://gitlab.com/wireshark/wireshark/-/issues>, qualified as an
2451 enhancement and attach your sample capture there. Normally a new
2452 dissector won't be accepted without a sample capture! If you open a
2453 bug be sure to cross-link your GitLab merge request.
2455 2. Advanced dissector topics.
2459 Some of the advanced features are being worked on constantly. When using them
2460 it is wise to check the relevant header and source files for additional details.
2462 2.2 Following "conversations".
2464 In Wireshark a conversation is defined as a series of data packets between two
2465 address:port combinations. A conversation is not sensitive to the direction of
2466 the packet. The same conversation will be returned for a packet bound from
2467 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
2469 2.2.1 Conversation Routines
2471 There are nine routines that you will use to work with a conversation:
2472 conversation_new, conversation_new_full, find_conversation,
2473 find_conversation_full, find_or_create_conversation,
2474 conversation_add_proto_data, conversation_get_proto_data,
2475 conversation_delete_proto_data, and conversation_set_dissector.
2477 2.2.1.1 The conversation_init function.
2479 This is an internal routine for the conversation code. As such you
2480 will not have to call this routine. Just be aware that this routine is
2481 called at the start of each capture and before the packets are filtered
2482 with a display filter. The routine will destroy all stored
2483 conversations. This routine does NOT clean up any data pointers that are
2484 passed in the conversation_add_proto_data 'data' variable. You are
2485 responsible for this clean up if you pass a malloc'ed pointer
2488 See item 2.2.1.5 for more information about use of the 'data' pointer.
2491 2.2.1.2 The conversation_new function.
2493 This routine will create a new conversation based upon two address/port
2494 pairs. If you want to associate with the conversation a pointer to a
2495 private data structure you must use the conversation_add_proto_data
2496 function. The ctype variable is used to differentiate between
2497 conversations over different protocols, i.e. TCP and UDP. The options
2498 variable is used to define a conversation that will accept any destination
2499 address and/or port. Set options = 0 if the destination port and address
2500 are known when conversation_new is called. See section 2.4 for more
2501 information on usage of the options parameter.
2503 The conversation_new prototype:
2504 conversation_t *conversation_new(uint32_t setup_frame, address *addr1,
2505 address *addr2, conversation_type ctype, uint32_t port1, uint32_t port2,
2509 uint32_t setup_frame = The lowest numbered frame for this conversation
2510 address* addr1 = first data packet address
2511 address* addr2 = second data packet address
2512 conversation_type ctype = conversation type, defined in conversation.h
2513 uint32_t port1 = first data packet port
2514 uint32_t port2 = second data packet port
2515 unsigned options = conversation options, NO_ADDR2 and/or NO_PORT2
2517 setup_frame indicates the first frame for this conversation, and is used to
2518 distinguish multiple conversations with the same addr1/port1 and addr2/port2
2519 pair that occur within the same capture session.
2521 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
2522 are the second address/port pair. A conversation doesn't have source
2523 and destination address/port pairs - packets in a conversation go in
2524 both directions - so "addr1"/"port1" may be the source or destination
2525 address/port pair; "addr2"/"port2" would be the other pair.
2527 If NO_ADDR2 is specified, the conversation is set up so that a
2528 conversation lookup will match only the "addr1" address; if NO_PORT2 is
2529 specified, the conversation is set up so that a conversation lookup will
2530 match only the "port1" port; if both are specified, i.e.
2531 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
2532 match only the "addr1"/"port1" address/port pair. This can be used if a
2533 packet indicates that, later in the capture, a conversation will be
2534 created using certain addresses and ports, in the case where the packet
2535 doesn't specify the addresses and ports of both sides.
2537 2.2.1.3 The conversation_new_full function.
2539 This routine will create a new conversation based upon an arbitrary
2540 lists of elements. Elements can be addresses, strings, unsigned
2541 integers, or unsigned 64-bit integers. Unlike conversation_new, element
2542 lists are matched strictly; wildcards aren't (yet) supported.
2544 The conversation_new_full prototype:
2545 conversation_t *conversation_new_full(const uint32_t setup_frame,
2546 conversation_element_t *elements);
2549 uint32_t setup_frame = The lowest numbered frame for
2551 conversation_element_t *elements = An array of data types and
2552 values which identify this conversation. The array MUST be
2553 terminated with a CE_ENDPOINT element.
2555 2.2.1.4 The find_conversation function.
2557 Call this routine to look up a conversation. If no conversation is found,
2558 the routine will return a NULL value.
2560 The find_conversation prototype:
2562 conversation_t *find_conversation(uint32_t frame_num, address *addr_a,
2563 address *addr_b, conversation_type ctype, uint32_t port_a, uint32_t port_b,
2567 uint32_t frame_num = a frame number to match
2568 address* addr_a = first address
2569 address* addr_b = second address
2570 conversation_type ctype = conversation type
2571 uint32_t port_a = first data packet port
2572 uint32_t port_b = second data packet port
2573 unsigned options = conversation options, NO_ADDR_B and/or NO_PORT_B
2575 frame_num is a frame number to match. The conversation returned is where
2576 (frame_num >= conversation->setup_frame
2577 && frame_num < conversation->next->setup_frame)
2578 Suppose there are a total of 3 conversations (A, B, and C) that match
2579 addr_a/port_a and addr_b/port_b, where the setup_frame used in
2580 conversation_new() for A, B and C are 10, 50, and 100 respectively. The
2581 frame_num passed in find_conversation is compared to the setup_frame of each
2582 conversation. So if (frame_num >= 10 && frame_num < 50), conversation A is
2583 returned. If (frame_num >= 50 && frame_num < 100), conversation B is returned.
2584 If (frame_num >= 100) conversation C is returned.
2586 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
2587 "port_b" are the second address/port pair. Again, as a conversation
2588 doesn't have source and destination address/port pairs, so
2589 "addr_a"/"port_a" may be the source or destination address/port pair;
2590 "addr_b"/"port_b" would be the other pair. The search will match the
2591 "a" address/port pair against both the "1" and "2" address/port pairs,
2592 and match the "b" address/port pair against both the "2" and "1"
2593 address/port pairs; you don't have to worry about which side the "a" or
2594 "b" pairs correspond to.
2596 If the NO_ADDR_B flag was specified to "find_conversation()", the
2597 "addr_b" address will be treated as matching any "wildcarded" address;
2598 if the NO_PORT_B flag was specified, the "port_b" port will be treated
2599 as matching any "wildcarded" port. If both flags are specified, i.e.
2600 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
2601 any "wildcarded" address and the "port_b" port will be treated as
2602 matching any "wildcarded" port.
2604 2.2.1.5 The find_conversation_full function.
2606 Call this routine to look up a conversation based on an element list. If
2607 no conversation is found, the routine will return a NULL value.
2609 The find_conversation_full prototype:
2611 conversation_t *find_conversation_full(uint32_t frame_num,
2612 conversation_element_t *elements);
2615 uint32_t setup_frame = The lowest numbered frame for
2617 conversation_element_t *elements = An array of data types and
2618 values which identify this conversation. The array MUST be
2619 terminated with a CE_ENDPOINT element.
2621 2.2.1.6 The find_conversation_pinfo function.
2623 This convenience function will find an existing conversation (by calling
2624 find_conversation())
2626 The find_conversation_pinfo prototype:
2628 extern conversation_t *find_conversation_pinfo(packet_info *pinfo,
2629 const unsigned options);
2632 packet_info *pinfo = the packet_info structure
2633 const unsigned options = conversation options, NO_ADDR_B and/or NO_PORT_B
2635 The frame number and the addresses necessary for find_conversation() are
2636 taken from the addresses and ports in the pinfo structure,
2637 pinfo->conv_addr_port_endpoints if pinfo->use_conv_addr_port_endpoints is set,
2638 or pinfo->conv_elements if it is set.
2640 2.2.1.7 The find_or_create_conversation function.
2642 This convenience function will find an existing conversation (by calling
2643 find_conversation()) and, if a conversation does not already exist, create a
2644 new conversation by calling conversation_new().
2646 The find_or_create_conversation prototype:
2648 extern conversation_t *find_or_create_conversation(packet_info *pinfo);
2651 packet_info *pinfo = the packet_info structure
2653 The frame number and the addresses necessary for find_conversation() and
2654 conversation_new() are taken from the pinfo structure (as is commonly done)
2655 and no 'options' are used.
2657 2.2.1.8 The conversation_add_proto_data function.
2659 Once you have created a conversation with conversation_new, you can
2660 associate data with it using this function.
2662 The conversation_add_proto_data prototype:
2664 void conversation_add_proto_data(conversation_t *conv, int proto,
2668 conversation_t *conv = the conversation in question
2669 int proto = registered protocol number
2670 void *data = dissector data structure
2672 "conversation" is the value returned by conversation_new. "proto" is a
2673 unique protocol number created with proto_register_protocol. Protocols
2674 are typically registered in the proto_register_XXXX section of your
2675 dissector. "data" is a pointer to the data you wish to associate with the
2676 conversation. "data" usually points to "wmem_alloc'd" memory; the
2677 memory will be automatically freed each time a new dissection begins
2678 and thus need not be managed (freed) by the dissector.
2679 Using the protocol number allows several dissectors to
2680 associate data with a given conversation.
2683 2.2.1.9 The conversation_get_proto_data function.
2685 After you have located a conversation with find_conversation, you can use
2686 this function to retrieve any data associated with it.
2688 The conversation_get_proto_data prototype:
2690 void *conversation_get_proto_data(conversation_t *conv, int proto);
2693 conversation_t *conv = the conversation in question
2694 int proto = registered protocol number
2696 "conversation" is the conversation created with conversation_new. "proto"
2697 is a unique protocol number created with proto_register_protocol,
2698 typically in the proto_register_XXXX portion of a dissector. The function
2699 returns a pointer to the data requested, or NULL if no data was found.
2702 2.2.1.10 The conversation_delete_proto_data function.
2704 After you are finished with a conversation, you can remove your association
2705 with this function. Please note that ONLY the conversation entry is
2706 removed. If you have allocated any memory for your data (other than with wmem_alloc),
2707 you must free it as well.
2709 The conversation_delete_proto_data prototype:
2711 void conversation_delete_proto_data(conversation_t *conv, int proto);
2714 conversation_t *conv = the conversation in question
2715 int proto = registered protocol number
2717 "conversation" is the conversation created with conversation_new. "proto"
2718 is a unique protocol number created with proto_register_protocol,
2719 typically in the proto_register_XXXX portion of a dissector.
2721 2.2.1.11 The conversation_set_dissector function
2723 This function sets the protocol dissector to be invoked whenever
2724 conversation parameters (addresses, port_types, ports, etc) are matched
2725 during the dissection of a packet.
2727 The conversation_set_dissector prototype:
2729 void conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle);
2732 conversation_t *conv = the conversation in question
2733 const dissector_handle_t handle = the dissector handle.
2736 2.2.2 Using timestamps relative to the conversation
2738 There is a framework to calculate timestamps relative to the start of the
2739 conversation. First of all the timestamp of the first packet that has been
2740 seen in the conversation must be kept in the protocol data to be able
2741 to calculate the timestamp of the current packet relative to the start
2742 of the conversation. The timestamp of the last packet that was seen in the
2743 conversation should also be kept in the protocol data. This way the
2744 delta time between the current packet and the previous packet in the
2745 conversation can be calculated.
2747 So add the following items to the struct that is used for the protocol data:
2752 The ts_prev value should only be set during the first run through the
2753 packets (ie PINFO_FD_VISITED(pinfo) is false).
2755 Next step is to use the per-packet information (described in section 2.5)
2756 to keep the calculated delta timestamp, as it can only be calculated
2757 on the first run through the packets. This is because a packet can be
2758 selected in random order once the whole file has been read.
2760 After calculating the conversation timestamps, it is time to put them in
2761 the appropriate columns with the function 'col_set_time' (described in
2762 section 1.5.9). The column used for relative timestamps is:
2764 COL_REL_TIME, /* Delta time to last frame in conversation */
2766 Last but not least, there MUST be a preference in each dissector that
2767 uses conversation timestamps that makes it possible to enable and
2768 disable the calculation of conversation timestamps. The main argument
2769 for this is that a higher level conversation is able to overwrite
2770 the values of lower level conversations in these two columns. Being
2771 able to actively select which protocols may overwrite the conversation
2772 timestamp columns gives the user the power to control these columns.
2773 (A second reason is that conversation timestamps use the per-packet
2774 data structure which uses additional memory, which should be avoided
2775 if these timestamps are not needed)
2777 Have a look at the differences to packet-tcp.[ch] in SVN 22966 and
2778 SVN 23058 to see the implementation of conversation timestamps for
2782 2.2.3 The example conversation code using wmem_file_scope memory.
2784 For a conversation between two IP addresses and ports you can use this as an
2785 example. This example uses wmem_alloc() with wmem_file_scope() to allocate
2786 memory and stores the data pointer in the conversation 'data' variable.
2788 /************************ Global values ************************/
2790 /* define your structure here */
2795 /* Registered protocol number */
2796 static int my_proto;
2798 /********************* in the dissector routine *********************/
2800 /* the local variables in the dissector */
2802 conversation_t *conversation;
2803 my_entry_t *data_ptr;
2806 /* look up the conversation */
2808 conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
2809 conversation_pt_to_conversation_type(pinfo->ptype),
2810 pinfo->srcport, pinfo->destport, 0);
2812 /* if conversation found get the data pointer that you stored */
2814 data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2817 /* new conversation create local data structure */
2819 data_ptr = wmem_alloc(wmem_file_scope(), sizeof(my_entry_t));
2821 /*** add your code here to setup the new data structure ***/
2823 /* create the conversation with your data pointer */
2825 conversation = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst,
2826 conversation_pt_to_conversation_type(pinfo->ptype),
2827 pinfo->srcport, pinfo->destport, 0);
2828 conversation_add_proto_data(conversation, my_proto, (void *)data_ptr);
2831 /* at this point the conversation data is ready */
2833 /***************** in the protocol register routine *****************/
2835 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2838 2.2.4 An example conversation code that starts at a specific frame number.
2840 Sometimes a dissector has determined that a new conversation is needed that
2841 starts at a specific frame number, when a capture session encompasses multiple
2842 conversation that reuse the same src/dest ip/port pairs. You can use the
2843 conversation->setup_frame returned by find_conversation with
2844 pinfo->num to determine whether or not there already exists a conversation
2845 that starts at the specific frame number.
2847 /* in the dissector routine */
2849 conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
2850 conversation_pt_to_conversation_type(pinfo->ptype),
2851 pinfo->srcport, pinfo->destport, 0);
2852 if (conversation == NULL || (conversation->setup_frame != pinfo->num)) {
2853 /* It's not part of any conversation or the returned
2854 * conversation->setup_frame doesn't match the current frame
2857 conversation = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst,
2858 conversation_pt_to_conversation_type(pinfo->ptype),
2859 pinfo->srcport, pinfo->destport, 0);
2863 2.2.5 The example conversation code using conversation index field.
2865 Sometimes the conversation isn't enough to define a unique data storage
2866 value for the network traffic. For example if you are storing information
2867 about requests carried in a conversation, the request may have an
2868 identifier that is used to define the request. In this case the
2869 conversation and the identifier are required to find the data storage
2870 pointer. You can use the conversation data structure index value to
2871 uniquely define the conversation.
2873 See packet-afs.c for an example of how to use the conversation index. In
2874 this dissector multiple requests are sent in the same conversation. To store
2875 information for each request the dissector has an internal hash table based
2876 upon the conversation index and values inside the request packets.
2879 /* in the dissector routine */
2881 /* to find a request value, first lookup conversation to get index */
2882 /* then used the conversation index, and request data to find data */
2883 /* in the local hash table */
2885 conversation = find_or_create_conversation(pinfo);
2887 request_key.conversation = conversation->index;
2888 request_key.service = pntoh16(&rxh->serviceId);
2889 request_key.callnumber = pntoh32(&rxh->callNumber);
2891 request_val = (struct afs_request_val *)g_hash_table_lookup(
2892 afs_request_hash, &request_key);
2894 /* only allocate a new hash element when it's a request */
2896 if (!request_val && !reply) {
2897 new_request_key = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_key));
2898 *new_request_key = request_key;
2900 request_val = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_val));
2901 request_val -> opcode = pntoh32(&afsh->opcode);
2902 opcode = request_val->opcode;
2904 g_hash_table_insert(afs_request_hash, new_request_key, request_val);
2909 2.3 Dynamic conversation dissector registration.
2912 NOTE: This sections assumes that all information is available to
2913 create a complete conversation, source port/address and
2914 destination port/address. If either the destination port or
2915 address is known, see section 2.4 Dynamic server port dissector
2918 For protocols that negotiate a secondary port connection, for example
2919 packet-msproxy.c, a conversation can install a dissector to handle
2920 the secondary protocol dissection. After the conversation is created
2921 for the negotiated ports use the conversation_set_dissector to define
2922 the dissection routine.
2923 Before we create these conversations or assign a dissector to them we should
2924 first check that the conversation does not already exist and if it exists
2925 whether it is registered to our protocol or not.
2926 We should do this because it is uncommon but it does happen that multiple
2927 different protocols can use the same socketpair during different stages of
2928 an application cycle. By keeping track of the frame number a conversation
2929 was started in Wireshark can still tell these different protocols apart.
2931 The second argument to conversation_set_dissector is a dissector handle,
2932 which is created with a call to create_dissector_handle,
2933 register_dissector, or register_dissector_with_description.
2935 register_dissector_with_description takes as arguments a string giving a name
2936 for the dissector, a string with a human-readable summary of the dissector, a
2937 pointer to the dissector function, and a protocol ID as returned by
2938 proto_register_protocol.
2940 register_dissector takes as arguments a string giving a name for the
2941 dissector, a pointer to the dissector function, and a protocol ID
2942 as returned by proto_register_protocol.
2944 create_dissector_handle takes as arguments a pointer to the dissector
2945 function and a protocol ID as returned by proto_register_protocol.
2946 It is recommended to use one of the above two functions instead of this one,
2947 since they allow the dissector to be referenced by name from the command line,
2948 by other dissectors via calls to find_dissector, etc.
2950 The protocol ID is the ID for the protocol dissected by the function.
2951 The function will not be called if the protocol has been disabled by the
2952 user; instead, the data for the protocol will be dissected as raw data.
2956 /* the handle for the dynamic dissector *
2957 static dissector_handle_t sub_dissector_handle;
2959 /* prototype for the dynamic dissector */
2960 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
2962 /* in the main protocol dissector, where the next dissector is setup */
2964 /* if conversation has a data field, create it and load structure */
2966 /* First check if a conversation already exists for this socketpair */
2967 conversation = find_conversation(pinfo->num,
2968 &pinfo->src, &pinfo->dst, conversation_type,
2969 src_port, dst_port, 0);
2971 /* If there is no such conversation, or if there is one but for
2972 someone else's protocol then we just create a new conversation
2973 and assign our protocol to it.
2975 if ((conversation == NULL) ||
2976 (conversation->dissector_handle != sub_dissector_handle)) {
2977 new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2978 new_conv_info->data1 = value1;
2980 /* create the conversation for the dynamic port */
2981 conversation = conversation_new(pinfo->num,
2982 &pinfo->src, &pinfo->dst, protocol,
2983 src_port, dst_port, new_conv_info, 0);
2985 /* set the dissector for the new conversation */
2986 conversation_set_dissector(conversation, sub_dissector_handle);
2991 proto_register_PROTOABBREV(void)
2995 sub_dissector_handle = register_dissector("PROTOABBREV", sub_dissector,
3001 2.4 Dynamic server port dissector registration.
3003 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
3004 conversation with only one port and address set, this isn't a
3005 requirement. Either the second port or the second address can be set
3006 when the conversation is created.
3008 For protocols that define a server address and port for a secondary
3009 protocol, a conversation can be used to link a protocol dissector to
3010 the server port and address. The key is to create the new
3011 conversation with the second address and port set to the "accept
3014 Some server applications can use the same port for different protocols during
3015 different stages of a transaction. For example it might initially use SNMP
3016 to perform some discovery and later switch to use TFTP using the same port.
3017 In order to handle this properly we must first check whether such a
3018 conversation already exists or not and if it exists we also check whether the
3019 registered dissector_handle for that conversation is "our" dissector or not.
3020 If not we create a new conversation on top of the previous one and set this new
3021 conversation to use our protocol.
3022 Since Wireshark keeps track of the frame number where a conversation started
3023 wireshark will still be able to keep the packets apart even though they do use
3024 the same socketpair.
3025 (See packet-tftp.c and packet-snmp.c for examples of this)
3027 There are two support routines that will allow the second port and/or
3028 address to be set later.
3030 conversation_set_port2(conversation_t *conv, uint32_t port);
3031 conversation_set_addr2(conversation_t *conv, address addr);
3033 These routines will change the second address or port for the
3034 conversation. So, the server port conversation will be converted into a
3035 more complete conversation definition. Don't use these routines if you
3036 want to create a conversation between the server and client and retain the
3037 server port definition, you must create a new conversation.
3042 /* the handle for the dynamic dissector *
3043 static dissector_handle_t sub_dissector_handle;
3047 /* in the main protocol dissector, where the next dissector is setup */
3049 /* if conversation has a data field, create it and load structure */
3051 new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
3052 new_conv_info->data1 = value1;
3054 /* create the conversation for the dynamic server address and port */
3055 /* NOTE: The second address and port values don't matter because the */
3056 /* NO_ADDR2 and NO_PORT2 options are set. */
3058 /* First check if a conversation already exists for this
3061 conversation = find_conversation(pinfo->num,
3062 &server_src_addr, 0, protocol,
3063 server_src_port, 0, NO_ADDR2 | NO_PORT_B);
3065 /* If there is no such conversation, or if there is one but for
3066 someone else's protocol then we just create a new conversation
3067 and assign our protocol to it.
3069 if ((conversation == NULL) ||
3070 (conversation->dissector_handle != sub_dissector_handle)) {
3071 conversation = conversation_new(pinfo->num,
3072 &server_src_addr, 0, conversation_type,
3073 server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
3075 /* set the dissector for the new conversation */
3076 conversation_set_dissector(conversation, sub_dissector_handle);
3079 2.5 Per-packet information.
3081 Information can be stored for each data packet that is processed by the
3082 dissector. The information is added with the p_add_proto_data function and
3083 retrieved with the p_get_proto_data function. The data pointers passed into
3084 the p_add_proto_data are not managed by the proto_data routines, however the
3085 data pointer memory scope must match that of the scope parameter.
3086 The two most common use cases for p_add_proto_data/p_get_proto_data are for
3087 persistent data about the packet for the lifetime of the capture (file scope)
3088 and to exchange data between dissectors across a single packet (packet scope).
3089 It is also used to provide packet data for Decode As dialog (packet scope).
3091 These functions are declared in <epan/proto_data.h>.
3094 p_add_proto_data(wmem_allocator_t *scope, packet_info *pinfo, int proto, uint32_t key, void *proto_data)
3096 p_get_proto_data(wmem_allocator_t *scope, packet_info *pinfo, int proto, uint32_t key)
3099 scope - Lifetime of the data to be stored, typically wmem_file_scope()
3100 or pinfo->pool (packet scope). Must match scope of data
3102 pinfo - The packet info pointer.
3103 proto - Protocol id returned by the proto_register_protocol call
3104 during initialization
3105 key - key associated with 'proto_data'
3106 proto_data - pointer to the dissector data.
3109 2.6 User Preferences.
3111 If the dissector has user options, there is support for adding these preferences
3112 to a configuration dialog.
3114 You must register the module with the preferences routine with -
3116 module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
3118 module_t *prefs_register_protocol_subtree(const char *subtree, int id,
3119 void (*apply_cb)(void));
3122 Where: proto_id - the value returned by "proto_register_protocol()" when
3123 the protocol was registered.
3124 apply_cb - Callback routine that is called when preferences are
3125 applied. It may be NULL, which inhibits the callback.
3126 subtree - grouping preferences tree node name (several protocols can
3127 be grouped under one preferences subtree)
3129 Then you can register the fields that can be configured by the user with these
3132 /* Register a preference with an unsigned integral value. */
3133 void prefs_register_uint_preference(module_t *module, const char *name,
3134 const char *title, const char *description, unsigned base, unsigned *var);
3136 /* Register a preference with an Boolean value. */
3137 void prefs_register_bool_preference(module_t *module, const char *name,
3138 const char *title, const char *description, bool *var);
3140 /* Register a preference with an enumerated value. */
3141 void prefs_register_enum_preference(module_t *module, const char *name,
3142 const char *title, const char *description, int *var,
3143 const enum_val_t *enumvals, bool radio_buttons)
3145 /* Register a preference with a character-string value. */
3146 void prefs_register_string_preference(module_t *module, const char *name,
3147 const char *title, const char *description, char **var)
3149 /* Register a preference with a password (a character-string) value. */
3150 /* The value is hold during runtime, only in memory. It is never written to disk */
3151 void prefs_register_password_preference(module_t *module, const char *name,
3152 const char *title, const char *description, char **var)
3154 /* Register a preference with a file name (string) value.
3155 * File name preferences are basically like string preferences
3156 * except that the GUI gives the user the ability to browse for the
3157 * file. Set for_writing true to show a Save dialog instead of normal Open.
3159 void prefs_register_filename_preference(module_t *module, const char *name,
3160 const char *title, const char *description, char **var,
3163 /* Register a preference with a range of unsigned integers (e.g.,
3166 void prefs_register_range_preference(module_t *module, const char *name,
3167 const char *title, const char *description, range_t *var,
3170 Where: module - Returned by the prefs_register_protocol routine
3171 name - This is appended to the name of the protocol, with a
3172 "." between them, to construct a name that identifies
3173 the field in the preference file; the name itself
3174 should not include the protocol name, as the name in
3175 the preference file will already have it. Make sure that
3176 only lower-case ASCII letters, numbers, underscores and
3177 dots appear in the preference name.
3178 title - Field title in the preferences dialog
3179 description - Comments added to the preference file above the
3180 preference value and shown as tooltip in the GUI, or NULL
3181 var - pointer to the storage location that is updated when the
3182 field is changed in the preference dialog box. Note that
3183 with string preferences the given pointer is overwritten
3184 with a pointer to a new copy of the string during the
3185 preference registration. The passed-in string may be
3186 freed, but you must keep another pointer to the string
3187 in order to free it.
3188 base - Base that the unsigned integer is expected to be in,
3190 enumvals - an array of enum_val_t structures. This must be
3191 NULL-terminated; the members of that structure are:
3193 a short name, to be used with the "-o" flag - it
3194 should not contain spaces or upper-case letters,
3195 so that it's easier to put in a command line;
3197 a description, which is used in the GUI (and
3198 which, for compatibility reasons, is currently
3199 what's written to the preferences file) - it can
3200 contain spaces, capital letters, punctuation,
3203 the numerical value corresponding to that name
3205 radio_buttons - true if the field is to be displayed in the
3206 preferences dialog as a set of radio buttons,
3207 false if it is to be displayed as an option
3209 max_value - The maximum allowed value for a range (0 is the minimum).
3211 These functions are declared in <epan/prefs.h>.
3213 An example from packet-rtpproxy.c -
3215 proto_rtpproxy = proto_register_protocol ( "Sippy RTPproxy Protocol", "RTPproxy", "rtpproxy");
3219 rtpproxy_module = prefs_register_protocol(proto_rtpproxy, proto_reg_handoff_rtpproxy);
3221 prefs_register_bool_preference(rtpproxy_module, "establish_conversation",
3222 "Establish Media Conversation",
3223 "Specifies that RTP/RTCP/T.38/MSRP/etc streams are decoded based "
3224 "upon port numbers found in RTPproxy answers",
3225 &rtpproxy_establish_conversation);
3227 prefs_register_uint_preference(rtpproxy_module, "reply.timeout",
3228 "RTPproxy reply timeout", /* Title */
3229 "Maximum timeout value in waiting for reply from RTPProxy (in milliseconds).", /* Descr */
3233 This will create preferences "rtpproxy.establish_conversation" and
3234 "rtpproxy.reply.timeout", the first of which is an Boolean and the
3235 second of which is a unsigned integer.
3237 Note that a warning will pop up if you've saved such preference to the
3238 preference file and you subsequently take the code out. The way to make
3239 a preference obsolete is to register it as such:
3241 /* Register a preference that used to be supported but no longer is. */
3242 void prefs_register_obsolete_preference(module_t *module,
3245 2.7 Reassembly/desegmentation for protocols running atop TCP.
3247 There are two main ways of reassembling a Protocol Data Unit (PDU) which
3248 spans across multiple TCP segments. The first approach is simpler, but
3249 assumes you are running atop of TCP when this occurs (but your dissector
3250 might run atop of UDP, too, for example), and that your PDUs consist of a
3251 fixed amount of data that includes enough information to determine the PDU
3252 length, possibly followed by additional data. The second method is more
3253 generic but requires more code and is less efficient.
3255 2.7.1 Using tcp_dissect_pdus().
3257 For the first method, you register two different dissection methods, one
3258 for the TCP case, and one for the other cases. It is a good idea to
3259 also have a dissect_PROTO_common function which will parse the generic
3260 content that you can find in all PDUs which is called from
3261 dissect_PROTO_tcp when the reassembly is complete and from
3262 dissect_PROTO_udp (or dissect_PROTO_other).
3264 To register the distinct dissector functions, consider the following
3265 example, stolen from packet-hartip.c:
3267 #include "packet-tcp.h"
3269 dissector_handle_t hartip_tcp_handle;
3270 dissector_handle_t hartip_udp_handle;
3272 hartip_udp_handle = register_dissector_with_description("hart_ip", "HART-IP over UDP", dissect_hartip_udp, proto_hartip);
3273 hartip_tcp_handle = register_dissector_with_description("hart_ip.tcp", "HART-IP over TCP", dissect_hartip_tcp, proto_hartip);
3275 dissector_add_uint_with_preference("udp.port", HARTIP_PORT, hartip_udp_handle);
3276 dissector_add_uint_with_preference("tcp.port", HARTIP_PORT, hartip_tcp_handle);
3278 The dissect_hartip_udp function does very little work and calls
3279 dissect_hartip_common, while dissect_hartip_tcp calls tcp_dissect_pdus with a
3280 reference to a callback which will be called with reassembled data:
3283 dissect_hartip_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
3286 if (!tvb_bytes_exist(tvb, 0, HARTIP_HEADER_LENGTH))
3289 tcp_dissect_pdus(tvb, pinfo, tree, hartip_desegment, HARTIP_HEADER_LENGTH,
3290 get_dissect_hartip_len, dissect_hartip_pdu, data);
3291 return tvb_reported_length(tvb);
3294 (The dissect_hartip_pdu function acts similarly to dissect_hartip_udp.)
3295 The arguments to tcp_dissect_pdus are:
3297 the tvbuff pointer, packet_info pointer, and proto_tree pointer
3298 passed to the dissector;
3300 a bool flag indicating whether desegmentation is enabled for
3303 the number of bytes of PDU data required to determine the length
3306 a routine that takes as arguments a packet_info pointer, a tvbuff
3307 pointer and an offset value representing the offset into the tvbuff
3308 at which a PDU begins, and a void pointer for user data, and should
3309 return the total length of the PDU in bytes (or 0 if more bytes are
3310 needed to determine the message length).
3311 The routine must not throw exceptions (it is guaranteed that the
3312 number of bytes specified by the previous argument to
3313 tcp_dissect_pdus is available, but more data might not be available,
3314 so don't refer to any data past that);
3316 a new_dissector_t routine to dissect the pdu that's passed a tvbuff
3317 pointer, packet_info pointer, proto_tree pointer and a void pointer for
3318 user data, with the tvbuff containing a possibly-reassembled PDU. (The
3319 "reported_length" of the tvbuff will be the length of the PDU);
3321 a void pointer to user data that is passed to the length-determining
3322 routine, and the dissector routine referenced in the previous parameter.
3324 2.7.2 Modifying the pinfo struct.
3326 The second reassembly mode is preferred when the dissector cannot determine
3327 how many bytes it will need to read in order to determine the size of a PDU.
3328 It may also be useful if your dissector needs to support reassembly from
3329 protocols other than TCP.
3331 Your dissect_PROTO will initially be passed a tvbuff containing the payload of
3332 the first packet. It should dissect as much data as it can, noting that it may
3333 contain more than one complete PDU. If the end of the provided tvbuff coincides
3334 with the end of a PDU then all is well and your dissector can just return as
3335 normal. (If it is a new-style dissector, it should return the number of bytes
3336 successfully processed.)
3338 If the dissector discovers that the end of the tvbuff does /not/ coincide with
3339 the end of a PDU, (ie, there is half of a PDU at the end of the tvbuff), it can
3340 indicate this to the parent dissector, by updating the pinfo struct. The
3341 desegment_offset field is the offset in the tvbuff at which the dissector will
3342 continue processing when next called. The desegment_len field should contain
3343 the estimated number of additional bytes required for completing the PDU. Next
3344 time your dissect_PROTO is called, it will be passed a tvbuff composed of the
3345 end of the data from the previous tvbuff together with desegment_len more bytes.
3347 If the dissector cannot tell how many more bytes it will need, it should set
3348 desegment_len=DESEGMENT_ONE_MORE_SEGMENT; it will then be called again as soon
3349 as any more data becomes available. Dissectors should set the desegment_len to a
3350 reasonable value when possible rather than always setting
3351 DESEGMENT_ONE_MORE_SEGMENT as it will generally be more efficient. Also, you
3352 *must not* set desegment_len=1 in this case, in the hope that you can change
3353 your mind later: once you return a positive value from desegment_len, your PDU
3354 boundary is set in stone.
3356 static hf_register_info hf[] = {
3358 { "C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
3363 * Dissect a buffer containing ASCII C strings.
3365 * @param tvb The buffer to dissect.
3366 * @param pinfo Packet Info.
3367 * @param tree The protocol tree.
3368 * @param data Optional data parameter given by parent dissector.
3370 static int dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void *data _U_)
3372 unsigned offset = 0;
3374 while (offset < tvb_reported_length(tvb)) {
3375 int available = tvb_reported_length_remaining(tvb, offset);
3376 int len = tvb_strnlen(tvb, offset, available);
3379 /* we ran out of data: ask for more */
3380 pinfo->desegment_offset = offset;
3381 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
3382 return (offset + available);
3385 col_set_str(pinfo->cinfo, COL_INFO, "C String");
3387 len += 1; /* Add one for the '\0' */
3389 proto_tree_add_item(tree, hf_cstring, tvb, offset, len, ENC_ASCII);
3390 offset += (unsigned)len;
3393 /* if we get here, then the end of the tvb coincided with the end of a
3394 string. Happy days. */
3395 return tvb_captured_length(tvb);
3398 This simple dissector will repeatedly return DESEGMENT_ONE_MORE_SEGMENT
3399 requesting more data until the tvbuff contains a complete C string. The C string
3400 will then be added to the protocol tree. Note that there may be more
3401 than one complete C string in the tvbuff, so the dissection is done in a
3404 2.8 Using udp_dissect_pdus().
3406 As noted in section 2.7.1, TCP has an API to dissect its PDU that can handle
3407 a PDU spread across multiple packets or multiple PDUs spread across a single
3408 packet. This section describes a similar mechanism for UDP, but is only
3409 applicable for one or more PDUs in a single packet. If a protocol runs on top
3410 of TCP as well as UDP, a common PDU dissection function can be created for both.
3412 To register the distinct dissector functions, consider the following
3413 example using UDP and TCP dissection, stolen from packet-dnp.c:
3415 #include "packet-tcp.h"
3416 #include "packet-udp.h"
3418 dissector_handle_t dnp3_tcp_handle;
3419 dissector_handle_t dnp3_udp_handle;
3421 dnp3_tcp_handle = register_dissector("dnp3.tcp", dissect_dnp3_tcp, proto_dnp3);
3422 dnp3_udp_handle = register_dissector("dnp3.udp", dissect_dnp3_udp, proto_dnp3);
3424 dissector_add_uint_with_preference("tcp.port", TCP_PORT_DNP, dnp3_tcp_handle);
3425 dissector_add_uint_with_preference("udp.port", UDP_PORT_DNP, dnp3_udp_handle);
3427 Both dissect_dnp3_tcp and dissect_dnp3_udp call tcp_dissect_pdus and
3428 udp_dissect_pdus respectively, with a reference to the same callbacks which
3429 are called to handle PDU data.
3432 dissect_dnp3_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
3434 return udp_dissect_pdus(tvb, pinfo, tree, DNP_HDR_LEN, dnp3_udp_check_header,
3435 get_dnp3_message_len, dissect_dnp3_message, data);
3439 dissect_dnp3_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
3441 if (!check_dnp3_header(tvb, false)) {
3445 tcp_dissect_pdus(tvb, pinfo, tree, true, DNP_HDR_LEN,
3446 get_dnp3_message_len, dissect_dnp3_message, data);
3448 return tvb_captured_length(tvb);
3451 (udp_dissect_pdus has an option of a heuristic check function within it while
3452 tcp_dissect_pdus does not, so it's done outside)
3454 The arguments to udp_dissect_pdus are:
3456 the tvbuff pointer, packet_info pointer, and proto_tree pointer
3457 passed to the dissector;
3459 the number of bytes of PDU data required to determine the length
3462 an optional routine (passing NULL is okay) that takes as arguments a
3463 packet_info pointer, a tvbuff pointer and an offset value representing the
3464 offset into the tvbuff at which a PDU begins, and a void pointer for user
3465 data, and should return true if the packet belongs to the dissector.
3466 The routine must not throw exceptions (it is guaranteed that the
3467 number of bytes specified by the previous argument to
3468 udp_dissect_pdus is available, but more data might not be available,
3469 so don't refer to any data past that);
3471 a routine that takes as arguments a packet_info pointer, a tvbuff
3472 pointer and an offset value representing the offset into the tvbuff
3473 at which a PDU begins, and a void pointer for user data, and should
3474 return the total length of the PDU in bytes. If return value is 0,
3475 it's treated the same as a failed heuristic.
3476 The routine must not throw exceptions (it is guaranteed that the
3477 number of bytes specified by the previous argument to
3478 tcp_dissect_pdus is available, but more data might not be available,
3479 so don't refer to any data past that);
3481 a new_dissector_t routine to dissect the pdu that's passed a tvbuff
3482 pointer, packet_info pointer, proto_tree pointer and a void pointer for
3483 user data, with the tvbuff containing a possibly-reassembled PDU. (The
3484 "reported_length" of the tvbuff will be the length of the PDU);
3486 a void pointer to user data that is passed to the length-determining
3487 routine, and the dissector routine referenced in the previous parameter.
3489 2.9 PINOs (Protocols in name only)
3491 For the typical dissector there is a 1-1 relationship between it and its
3492 protocol. However, there are times when a protocol needs multiple "names"
3493 because it has multiple dissection functions going into the same dissector
3494 table. The multiple names removes confusion when picking dissection through
3495 Decode As functionality.
3497 Once the "main" protocol name has been created through proto_register_protocol,
3498 additional "pinos" can be created with proto_register_protocol_in_name_only.
3499 These pinos have all of the naming conventions of a protocol, but are stored
3500 separately as to remove confusion from real protocols. "pinos" the main
3501 protocol's properties for things like enable/disable. i.e. If the "main"
3502 protocol has been disabled, all of its pinos will be disabled as well.
3503 Pinos should not have any fields registered with them or heuristic tables
3504 associated with them.
3506 Another use case for pinos is when a protocol contains a TLV design and it
3507 wants to create a dissector table to handle dissection of the "V". Dissector
3508 tables require a "protocol", but the dissection functions for that table
3509 typically aren't a protocol. In this case proto_register_protocol_in_name_only
3510 creates the necessary placeholder for the dissector table. In addition, because
3511 a dissector table exists, "V"s of the TLVs can be dissected outside of the
3512 original dissector file.
3514 2.10 Creating Decode As functionality.
3516 While the Decode As functionality is available through the GUI, the underlying
3517 functionality is controlled by dissectors themselves. To create Decode As
3518 functionality for a dissector, two things are required:
3519 1. A dissector table
3520 2. A series of structures to assist the GUI in how to present the dissector
3523 Consider the following example using IP dissection, stolen from packet-ip.c:
3525 static build_valid_func ip_da_build_value[1] = {ip_value};
3526 static decode_as_value_t ip_da_values = {ip_prompt, 1, ip_da_build_value};
3527 static decode_as_t ip_da = {"ip", "ip.proto", 1, 0, &ip_da_values, NULL, NULL,
3528 decode_as_default_populate_list, decode_as_default_reset, decode_as_default_change, NULL};
3530 ip_dissector_table = register_dissector_table("ip.proto", "IP protocol", ip_proto, FT_UINT8, BASE_DEC);
3532 register_decode_as(&ip_da);
3534 ip_da_build_value contains all of the function pointers (typically just 1) that
3535 can be used to retrieve the value(s) that go into the dissector table. This is
3536 usually data saved by the dissector during packet dissector with an API like
3537 p_add_proto_data and retrieved in the "value" function with p_get_proto_data.
3539 ip_da_values contains all of the function pointers (typically just 1) that
3540 provide the text explaining the name and use of the value field that will
3541 be passed to the dissector table to change the dissection output.
3543 ip_da pulls everything together including the dissector (protocol) name, the
3544 "layer type" of the dissector, the dissector table name, the function pointer
3545 values as well as handlers for populating, applying and resetting the changes
3546 to the dissector table through Decode As GUI functionality. For dissector
3547 tables that are an integer or string type, the provided "default" handling
3548 functions shown in the example should suffice.
3550 All entries into a dissector table that use Decode As must have a unique
3551 protocol ID. If a protocol wants multiple entries into a dissector table,
3552 a pino should be used (see section 2.9)
3556 The ptvcursor API allows a simpler approach to writing dissectors for
3557 simple protocols. The ptvcursor API works best for protocols whose fields
3558 are static and whose format does not depend on the value of other fields.
3559 However, even if only a portion of your protocol is statically defined,
3560 then that portion could make use of ptvcursors.
3562 The ptvcursor API lets you extract data from a tvbuff, and add it to a
3563 protocol tree in one step. It also keeps track of the position in the
3564 tvbuff so that you can extract data again without having to compute any
3565 offsets --- hence the "cursor" name of the API.
3567 The three steps for a simple protocol are:
3568 1. Create a new ptvcursor with ptvcursor_new()
3569 2. Add fields with multiple calls of ptvcursor_add()
3570 3. Delete the ptvcursor with ptvcursor_free()
3572 ptvcursor offers the possibility to add subtrees in the tree as well. It can be
3573 done in very simple steps :
3574 1. Create a new subtree with ptvcursor_push_subtree(). The old subtree is
3575 pushed in a stack and the new subtree will be used by ptvcursor.
3576 2. Add fields with multiple calls of ptvcursor_add(). The fields will be
3577 added in the new subtree created at the previous step.
3578 3. Pop the previous subtree with ptvcursor_pop_subtree(). The previous
3579 subtree is again used by ptvcursor.
3580 Note that at the end of the parsing of a packet you must have popped each
3581 subtree you pushed. If it's not the case, the dissector will generate an error.
3583 To use the ptvcursor API, include the "ptvcursor.h" file. The PGM dissector
3584 is an example of how to use it. You don't need to look at it as a guide;
3585 instead, the API description here should be good enough.
3587 2.11.1 ptvcursor API.
3590 ptvcursor_new(proto_tree* tree, tvbuff_t* tvb, int offset)
3591 This creates a new ptvcursor_t object for iterating over a tvbuff.
3592 You must call this and use this ptvcursor_t object so you can use the
3596 ptvcursor_add(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding)
3597 This will extract 'length' bytes from the tvbuff and place it in
3598 the proto_tree as field 'hf', which is a registered header_field. The
3599 pointer to the proto_item that is created is passed back to you. Internally,
3600 the ptvcursor advances its cursor so the next call to ptvcursor_add
3601 starts where this call finished. The 'encoding' parameter is relevant for
3602 certain type of fields (See above under proto_tree_add_item()).
3605 ptvcursor_add_ret_uint(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, uint32_t *retval);
3606 Like ptvcursor_add, but returns uint value retrieved
3609 ptvcursor_add_ret_int(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, int32_t *retval);
3610 Like ptvcursor_add, but returns int value retrieved
3613 ptvcursor_add_ret_string(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, wmem_allocator_t *scope, const uint8_t **retval);
3614 Like ptvcursor_add, but returns string retrieved
3617 ptvcursor_add_ret_boolean(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding, bool *retval);
3618 Like ptvcursor_add, but returns boolean value retrieved
3621 ptvcursor_add_no_advance(ptvcursor_t* ptvc, int hf, int length, const unsigned encoding)
3622 Like ptvcursor_add, but does not advance the internal cursor.
3625 ptvcursor_advance(ptvcursor_t* ptvc, int length)
3626 Advances the internal cursor without adding anything to the proto_tree.
3629 ptvcursor_free(ptvcursor_t* ptvc)
3630 Frees the memory associated with the ptvcursor. You must call this
3631 after your dissection with the ptvcursor API is completed.
3635 ptvcursor_push_subtree(ptvcursor_t* ptvc, proto_item* it, int ett_subtree)
3636 Pushes the current subtree in the tree stack of the cursor, creates a new
3637 one and sets this one as the working tree.
3640 ptvcursor_pop_subtree(ptvcursor_t* ptvc);
3641 Pops a subtree in the tree stack of the cursor
3644 ptvcursor_add_with_subtree(ptvcursor_t* ptvc, int hfindex, int length,
3645 const unsigned encoding, int ett_subtree);
3646 Adds an item to the tree and creates a subtree.
3647 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3648 In this case, at the next pop, the item length will be equal to the advancement
3649 of the cursor since the creation of the subtree.
3652 ptvcursor_add_text_with_subtree(ptvcursor_t* ptvc, int length,
3653 int ett_subtree, const char *format, ...);
3654 Add a text node to the tree and create a subtree.
3655 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3656 In this case, at the next pop, the item length will be equal to the advancement
3657 of the cursor since the creation of the subtree.
3659 2.11.2 Miscellaneous functions.
3662 ptvcursor_tvbuff(ptvcursor_t* ptvc)
3663 Returns the tvbuff associated with the ptvcursor.
3666 ptvcursor_current_offset(ptvcursor_t* ptvc)
3667 Returns the current offset.
3670 ptvcursor_tree(ptvcursor_t* ptvc)
3671 Returns the proto_tree associated with the ptvcursor.
3674 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree)
3675 Sets a new proto_tree for the ptvcursor.
3678 ptvcursor_set_subtree(ptvcursor_t* ptvc, proto_item* it, int ett_subtree);
3679 Creates a subtree and adds it to the cursor as the working tree but does
3680 not save the old working tree.
3684 2.12.1 Null Protocol Trees
3686 A protocol dissector may be called in 2 different ways - with, or
3687 without a non-null "tree" argument.
3689 If the proto_tree argument is null, Wireshark does not need to use
3690 the protocol tree information from your dissector, and therefore is
3691 passing the dissector a null "tree" argument so that it doesn't
3692 need to do work necessary to build the protocol tree.
3694 In the interest of speed, if "tree" is NULL, one can avoid building a
3695 protocol tree and adding stuff to it, or even looking at any packet
3696 data needed only if you're building the protocol tree, if possible.
3697 In particular, string operations and complex calculations that produce
3698 values which will be added as text to the tree and used nowhere else
3699 should be omitted when the tree is NULL.
3701 Note, however, that you must fill in column information, create
3702 conversations, reassemble packets, do calls to "expert" functions,
3703 build any other persistent state needed for dissection, and call
3704 subdissectors regardless of whether "tree" is NULL or not. Note
3705 also that there is no guarantee, the first time the dissector is
3706 called, whether "tree" will be null or not; your dissector must work
3707 correctly, building or updating whatever state information is
3708 necessary, in either case.
3710 Doing this properly generally means structuring the dissection so that
3711 the functions for adding items to the protocol tree are interleaved with
3712 the functions that must be called even when the tree is NULL. This
3713 allows dissection to occur without backtracking to an earlier offset,
3714 which ensures that in the case of invalid or truncated data that aborts
3715 dissection as much information is added to the protocol tree as possible.
3717 This can be inconvenient. Fortunately, the routines for adding items to
3718 the protocol tree can be passed a null protocol tree pointer, in which
3719 case they'll return a null item pointer, and "proto_item_add_subtree()"
3720 returns a null tree pointer if passed a null item pointer. Functions
3721 like "proto_tree_add_item_ret_XXX()" still return the requested value
3722 if there is a null protocol tree, but skip the rest of the work.
3724 So, if you call the API routines and are careful not to dereference any
3725 null tree or item pointers directly, you can ignore whether or not "tree"
3726 is null and do the dissection normally and still get some optimization
3727 benefits. This almost certainly results in easier to read code and lowers
3728 the chance of making subtle errors, and is thus recommended for initial
3729 implementations. It might not be as efficient as skipping that work if
3730 you're not building a protocol tree, but if the code would have a lot of
3731 tests whether "tree" is null to skip that work, you might still be better
3732 off just doing all that work regardless.
3734 2.12.2 Faked Protocol Items
3736 If the "tree" parameter is not NULL, Wireshark needs to use protocol tree
3737 information from your dissector. However, it may not need to use protocol
3738 tree information from *all* fields, because the full set of tree details
3739 will not be displayed or printed (the tree is not "visible"). Wireshark
3740 may be dissecting the packet in order to see if a particular filter matches,
3741 or to determine the value of custom columns that depend on field values,
3742 or to tap values from certain fields (on packets that only match a filter).
3744 In such cases, Wireshark will keep track of which fields are referenced,
3745 and when the API routines are called avoid actually creating a field_info
3746 struct and extracting data from the frame, instead returning a "faked"
3747 protocol item. Passing this faked protocol item to other API routines
3748 like proto_item_set_len() or proto_item_append_text() will have no effect.
3749 API routines that return a value, such as proto_tree_add_item_ret_string(),
3750 will still extract data from the string as necessary to return. Thus the
3751 typical dissector can ignore whether items are faked or not and simply
3752 benefit from the optimization. Dissectors that examine the protocol tree
3753 and protocol items and their field_info structs after creation directly
3754 may have unexpected results; this is best avoided in favor of using the
3758 * Editor modelines - https://www.wireshark.org/tools/modelines.html
3763 * indent-tabs-mode: nil
3766 * vi: set shiftwidth=4 tabstop=4 expandtab:
3767 * :indentSize=4:tabSize=4:noTabs=true: