6 This file is a HOWTO for Wireshark developers interested in writing or working
7 on Wireshark protocol dissectors. It describes expected code patterns and the
8 use of some of the important functions and variables.
10 This file is compiled to give in depth information on Wireshark.
11 It is by no means all inclusive and complete. Please feel free to send
12 remarks and patches to the developer mailing list.
14 If you haven't read README.developer, read that first!
18 Before starting to develop a new dissector, a "running" Wireshark build
19 environment is required - there's no such thing as a standalone "dissector
22 How to setup such an environment is platform dependent; detailed information
23 about these steps can be found in the "Developer's Guide" (available from:
24 http://www.wireshark.org) and in the INSTALL and README files of the sources
27 0.1. Dissector related README files.
29 You'll find additional dissector related information in the following README
32 - README.heuristic - what are heuristic dissectors and how to write them
33 - README.plugins - how to "pluginize" a dissector
34 - README.python - writing a dissector in PYTHON.
35 - README.request_response_tracking - how to track req./resp. times and such
36 - README.wmem - how to obtain "memory leak free" memory
40 James Coe <jammer[AT]cin.net>
41 Gilbert Ramirez <gram[AT]alumni.rice.edu>
42 Jeff Foster <jfoste[AT]woodward.com>
43 Olivier Abad <oabad[AT]cybercable.fr>
44 Laurent Deniel <laurent.deniel[AT]free.fr>
45 Gerald Combs <gerald[AT]wireshark.org>
46 Guy Harris <guy[AT]alum.mit.edu>
47 Ulf Lamping <ulf.lamping[AT]web.de>
49 1. Setting up your protocol dissector code.
51 This section provides skeleton code for a protocol dissector. It also explains
52 the basic functions needed to enter values in the traffic summary columns,
53 add to the protocol tree, and work with registered header fields.
57 Wireshark requires certain things when setting up a protocol dissector.
58 We provide basic skeleton code for a dissector that you can copy to a new file
59 and fill in. Your dissector should follow the naming convention of "packet-"
60 followed by the abbreviated name for the protocol. It is recommended that where
61 possible you keep to the IANA abbreviated name for the protocol, if there is
62 one, or a commonly-used abbreviation for the protocol, if any.
64 The skeleton code lives in the file "packet-PROTOABBREV.c" in the same source
65 directory as this README.
67 If instead of using the skeleton you base your dissector on an existing real
68 dissector, please put a little note in the copyright header indicating which
69 dissector you started with.
71 Usually, you will put your newly created dissector file into the directory
72 epan/dissectors/, just like all the other packet-*.c files already in there.
74 Also, please add your dissector file to the corresponding makefiles,
75 described in section "1.9 Editing Makefile.common and CMakeLists.txt
76 to add your dissector" below.
78 Dissectors that use the dissector registration API to register with a lower
79 level protocol (this is the vast majority) don't need to define a prototype in
80 their .h file. For other dissectors the main dissector routine should have a
81 prototype in a header file whose name is "packet-", followed by the abbreviated
82 name for the protocol, followed by ".h"; any dissector file that calls your
83 dissector should be changed to include that file.
85 You may not need to include all the headers listed in the skeleton, and you may
86 need to include additional headers.
88 The "$Id$" tag in the header comment will be updated by Subversion when the file
91 1.2 Explanation of needed substitutions in code skeleton.
93 In the skeleton sample code the following strings should be substituted with
96 YOUR_NAME Your name, of course. You do want credit, don't you?
97 It's the only payment you will receive....
98 YOUR_EMAIL_ADDRESS Keep those cards and letters coming.
99 PROTONAME The name of the protocol; this is displayed in the
100 top-level protocol tree item for that protocol.
101 PROTOSHORTNAME An abbreviated name for the protocol; this is displayed
102 in the "Preferences" dialog box if your dissector has
103 any preferences, in the dialog box of enabled protocols,
104 and in the dialog box for filter fields when constructing
106 PROTOABBREV A name for the protocol for use in filter expressions;
107 it shall contain only lower-case letters, digits, and hyphens.
108 FIELDNAME The displayed name for the header field.
109 FIELDABBREV The abbreviated name for the header field. (NO SPACES)
110 FIELDTYPE FT_NONE, FT_BOOLEAN, FT_UINT8, FT_UINT16, FT_UINT24,
111 FT_UINT32, FT_UINT64, FT_INT8, FT_INT16, FT_INT24, FT_INT32,
112 FT_INT64, FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME,
113 FT_RELATIVE_TIME, FT_STRING, FT_STRINGZ, FT_EUI64,
114 FT_UINT_STRING, FT_ETHER, FT_BYTES, FT_UINT_BYTES, FT_IPv4,
115 FT_IPv6, FT_IPXNET, FT_FRAMENUM, FT_PROTOCOL, FT_GUID, FT_OID,
117 FIELDDISPLAY --For FT_UINT{8,16,24,32,64} and FT_INT{8,16,24,32,64):
119 BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC,
120 or BASE_CUSTOM, possibly ORed with BASE_RANGE_STRING or
123 --For FT_ABSOLUTE_TIME:
125 ABSOLUTE_TIME_LOCAL, ABSOLUTE_TIME_UTC, or
126 ABSOLUTE_TIME_DOY_UTC
130 if BITMASK is non-zero:
131 Number of bits in the field containing the FT_BOOLEAN
136 --For all other types:
139 FIELDCONVERT VALS(x), RVALS(x), TFS(x), NULL
140 BITMASK Used to mask a field not 8-bit aligned or with a size other
141 than a multiple of 8 bits
142 FIELDDESCR A brief description of the field, or NULL. [Please do not use ""].
143 PARENT_SUBFIELD Lower level protocol field used for lookup, i.e. "tcp.port"
144 ID_VALUE Lower level protocol field value that identifies this protocol
145 For example the TCP or UDP port number
147 If, for example, PROTONAME is "Internet Bogosity Discovery Protocol",
148 PROTOSHORTNAME would be "IBDP", and PROTOABBREV would be "ibdp". Try to
149 conform with IANA names.
151 1.3 The dissector and the data it receives.
156 This is only needed if the dissector doesn't use self-registration to
157 register itself with the lower level dissector, or if the protocol dissector
158 wants/needs to expose code to other subdissectors.
160 The dissector must be declared exactly as follows in the file
161 packet-PROTOABBREV.h:
164 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
167 1.3.2 Extracting data from packets.
169 NOTE: See the file /epan/tvbuff.h for more details.
171 The "tvb" argument to a dissector points to a buffer containing the raw
172 data to be analyzed by the dissector; for example, for a protocol
173 running atop UDP, it contains the UDP payload (but not the UDP header,
174 or any protocol headers above it). A tvbuffer is an opaque data
175 structure, the internal data structures are hidden and the data must be
176 accessed via the tvbuffer accessors.
180 Bit accessors for a maximum of 8-bits, 16-bits 32-bits and 64-bits:
182 guint8 tvb_get_bits8(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits);
183 guint16 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
184 guint32 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
185 guint64 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
187 Single-byte accessor:
189 guint8 tvb_get_guint8(tvbuff_t *tvb, const gint offset);
191 Network-to-host-order accessors for 16-bit integers (guint16), 24-bit
192 integers, 32-bit integers (guint32), 40-bit integers, 48-bit integers,
193 56-bit integers and 64-bit integers (guint64):
195 guint16 tvb_get_ntohs(tvbuff_t *tvb, const gint offset);
196 guint32 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset);
197 guint32 tvb_get_ntohl(tvbuff_t *tvb, const gint offset);
198 guint64 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset);
199 guint64 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset);
200 guint64 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset);
201 guint64 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset);
203 Network-to-host-order accessors for single-precision and
204 double-precision IEEE floating-point numbers:
206 gfloat tvb_get_ntohieee_float(tvbuff_t *tvb, const gint offset);
207 gdouble tvb_get_ntohieee_double(tvbuff_t *tvb, const gint offset);
209 Little-Endian-to-host-order accessors for 16-bit integers (guint16),
210 24-bit integers, 32-bit integers (guint32), 40-bit integers, 48-bit
211 integers, 56-bit integers, and 64-bit integers (guint64):
213 guint16 tvb_get_letohs(tvbuff_t *tvb, const gint offset);
214 guint32 tvb_get_letoh24(tvbuff_t *tvb, const gint offset);
215 guint32 tvb_get_letohl(tvbuff_t *tvb, const gint offset);
216 guint64 tvb_get_letoh40(tvbuff_t *tvb, const gint offset);
217 guint64 tvb_get_letoh48(tvbuff_t *tvb, const gint offset);
218 guint64 tvb_get_letoh56(tvbuff_t *tvb, const gint offset);
219 guint64 tvb_get_letoh64(tvbuff_t *tvb, const gint offset);
221 Little-Endian-to-host-order accessors for single-precision and
222 double-precision IEEE floating-point numbers:
224 gfloat tvb_get_letohieee_float(tvbuff_t *tvb, const gint offset);
225 gdouble tvb_get_letohieee_double(tvbuff_t *tvb, const gint offset);
227 Accessors for IPv4 and IPv6 addresses:
229 guint32 tvb_get_ipv4(tvbuff_t *tvb, const gint offset);
230 void tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr);
232 NOTE: IPv4 addresses are not to be converted to host byte order before
233 being passed to "proto_tree_add_ipv4()". You should use "tvb_get_ipv4()"
234 to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't,
235 for example, try to use "tvb_get_ntohl()", find that it gives you the
236 wrong answer on the PC on which you're doing development, and try
237 "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong
238 answer on big-endian machines.
240 gchar *tvb_ip_to_str(tvbuff_t *tvb, const gint offset)
241 gchar *tvb_ip6_to_str(tvbuff_t *tvb, const gint offset)
243 Returns a null-terminated buffer containing a string with IPv4 or IPv6 Address
244 from the specified tvbuff, starting at the specified offset.
248 void tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid);
249 void tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid);
250 void tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation);
254 guint8 *tvb_get_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length);
255 gchar *tvb_get_unicode_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint length, const guint encoding);
256 guint8 *tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length, const guint encoding);
258 Returns a null-terminated buffer containing data from the specified
259 tvbuff, starting at the specified offset, and containing the specified
260 length worth of characters (the length of the buffer will be length+1,
261 as it includes a null character to terminate the string).
263 tvb_get_string() returns a buffer allocated by g_malloc() if scope is set
264 to NULL (in that case memory must be explicitely freed), or with the
265 allocator lifetime if scope is not NULL.
267 tvb_get_unicode_string() is a unicode (UTF-16) version of above. This
268 is intended for reading UTF-16 unicode strings out of a tvbuff and
269 returning them as a UTF-8 string for use in Wireshark. The offset and
270 returned length pointer are in bytes, not UTF-16 characters.
272 guint8 *tvb_get_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp);
273 guint8 *tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding);
274 const guint8 *tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp);
275 gchar *tvb_get_unicode_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding);
276 gint tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer);
277 gint tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer);
279 Returns a null-terminated buffer containing data from the specified tvbuff,
280 starting at the specified offset, and containing all characters from the
281 tvbuff up to and including a terminating null character in the tvbuff.
282 "*lengthp" will be set to the length of the string, including the terminating
285 tvb_get_stringz() returns a buffer allocated by g_malloc() if scope is set
286 to NULL (in that case memory must be explicitely freed), or with the
287 allocator lifetime if scope is not NULL.
289 tvb_get_const_stringz() returns a pointer to the (const) string in the tvbuff.
290 You do not need to free() this buffer, it will happen automatically once the
291 next packet is dissected. This function is slightly more efficient than the
292 others because it does not allocate memory and copy the string.
294 tvb_get_unicode_stringz() is a unicode (UTF-16) version of above.
295 This is intended for reading UTF-16 unicode strings out of a tvbuff
296 and returning them as a UTF-8 string for use in Wireshark. The offset and
297 returned length pointer are in bytes, not UTF-16 characters.
299 tvb_get_faked_unicode() has been superseded by tvb_get_string(), which
300 properly handles Unicode (UTF-16) strings by converting them to UTF-8.
302 Byte Array Accessors:
304 gchar *tvb_bytes_to_str(tvbuff_t *tvb, gint offset, gint len);
306 Formats a bunch of data from a tvbuff as bytes, returning a pointer
307 to the string with the data formatted as two hex digits for each byte.
308 The string pointed to is stored in an "ep_alloc'd" buffer which will be freed
309 before the next frame is dissected. The formatted string will contain the hex digits
310 for at most the first 16 bytes of the data. If len is greater than 16 bytes, a
311 trailing "..." will be added to the string.
313 gchar *tvb_bytes_to_str_punct(tvbuff_t *tvb, gint offset, gint len, gchar punct);
315 This function is similar to tvb_bytes_to_str(...) except that 'punct' is inserted
316 between the hex representation of each byte.
318 gchar *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first);
320 Given a tvbuff, an offset into the tvbuff, and a length that starts
321 at that offset (which may be -1 for "all the way to the end of the
322 tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
323 the low or high half byte, formatting the digits according to an input digit set,
324 if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
325 A pointer to the packet scope allocated string will be returned.
326 Note: a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
329 guint8* tvb_memcpy(tvbuff_t *tvb, guint8* target, gint offset, gint length);
331 Copies into the specified target the specified length's worth of data
332 from the specified tvbuff, starting at the specified offset.
334 guint8* tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length);
336 Returns a buffer, allocated with "g_malloc()" if scope is NULL, or with the specified pool.
339 /* WARNING! Don't use this function. There is almost always a better way.
340 * It's dangerous because once this pointer is given to the user, there's
341 * no guarantee that the user will honor the 'length' and not overstep the
342 * boundaries of the buffer. Also see the warning in the Portability section.
344 guint8* tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length);
347 1.4 Functions to handle columns in the traffic summary window.
349 The topmost pane of the main window is a list of the packets in the
350 capture, possibly filtered by a display filter.
352 Each line corresponds to a packet, and has one or more columns, as
353 configured by the user.
355 Many of the columns are handled by code outside individual dissectors;
356 most dissectors need only specify the value to put in the "Protocol" and
359 Columns are specified by COL_ values; the COL_ value for the "Protocol"
360 field, typically giving an abbreviated name for the protocol (but not
361 the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the
362 COL_ value for the "Info" field, giving a summary of the contents of the
363 packet for that protocol, is COL_INFO.
365 The value for a column can be specified with one of several functions,
366 all of which take the 'fd' argument to the dissector as their first
367 argument, and the COL_ value for the column as their second argument.
369 1.4.1 The col_set_str function.
371 'col_set_str' takes a string as its third argument, and sets the value
372 for the column to that value. It assumes that the pointer passed to it
373 points to a string constant or a static "const" array, not to a
374 variable, as it doesn't copy the string, it merely saves the pointer
375 value; the argument can itself be a variable, as long as it always
376 points to a string constant or a static "const" array.
378 It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if
379 the dissector will be using 'col_append_str' or 'col_append_fstr" to
380 append more information to the column, the string will have to be copied
381 anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in
384 For example, to set the "Protocol" column
387 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
390 1.4.2 The col_add_str function.
392 'col_add_str' takes a string as its third argument, and sets the value
393 for the column to that value. It takes the same arguments as
394 'col_set_str', but copies the string, so that if the string is, for
395 example, an automatic variable that won't remain in scope when the
396 dissector returns, it's safe to use.
399 1.4.3 The col_add_fstr function.
401 'col_add_fstr' takes a 'printf'-style format string as its third
402 argument, and 'printf'-style arguments corresponding to '%' format
403 items in that string as its subsequent arguments. For example, to set
404 the "Info" field to "<XXX> request, <N> bytes", where "reqtype" is a
405 string containing the type of the request in the packet and "n" is an
406 unsigned integer containing the number of bytes in the request:
408 col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
411 Don't use 'col_add_fstr' with a format argument of just "%s" -
412 'col_add_str', or possibly even 'col_set_str' if the string that matches
413 the "%s" is a static constant string, will do the same job more
417 1.4.4 The col_clear function.
419 If the Info column will be filled with information from the packet, that
420 means that some data will be fetched from the packet before the Info
421 column is filled in. If the packet is so small that the data in
422 question cannot be fetched, the routines to fetch the data will throw an
423 exception (see the comment at the beginning about tvbuffers improving
424 the handling of short packets - the tvbuffers keep track of how much
425 data is in the packet, and throw an exception on an attempt to fetch
426 data past the end of the packet, so that the dissector won't process
427 bogus data), causing the Info column not to be filled in.
429 This means that the Info column will have data for the previous
430 protocol, which would be confusing if, for example, the Protocol column
431 had data for this protocol.
433 Therefore, before a dissector fetches any data whatsoever from the
434 packet (unless it's a heuristic dissector fetching data to determine
435 whether the packet is one that it should dissect, in which case it
436 should check, before fetching the data, whether there's any data to
437 fetch; if there isn't, it should return FALSE), it should set the
438 Protocol column and the Info column.
440 If the Protocol column will ultimately be set to, for example, a value
441 containing a protocol version number, with the version number being a
442 field in the packet, the dissector should, before fetching the version
443 number field or any other field from the packet, set it to a value
444 without a version number, using 'col_set_str', and should later set it
445 to a value with the version number after it's fetched the version
448 If the Info column will ultimately be set to a value containing
449 information from the packet, the dissector should, before fetching any
450 fields from the packet, clear the column using 'col_clear' (which is
451 more efficient than clearing it by calling 'col_set_str' or
452 'col_add_str' with a null string), and should later set it to the real
453 string after it's fetched the data to use when doing that.
456 1.4.5 The col_append_str function.
458 Sometimes the value of a column, especially the "Info" column, can't be
459 conveniently constructed at a single point in the dissection process;
460 for example, it might contain small bits of information from many of the
461 fields in the packet. 'col_append_str' takes, as arguments, the same
462 arguments as 'col_add_str', but the string is appended to the end of the
463 current value for the column, rather than replacing the value for that
464 column. (Note that no blank separates the appended string from the
465 string to which it is appended; if you want a blank there, you must add
466 it yourself as part of the string being appended.)
469 1.4.6 The col_append_fstr function.
471 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to
472 'col_add_str' - it takes, as arguments, the same arguments as
473 'col_add_fstr', but the formatted string is appended to the end of the
474 current value for the column, rather than replacing the value for that
477 1.4.7 The col_append_sep_str and col_append_sep_fstr functions.
479 In specific situations the developer knows that a column's value will be
480 created in a stepwise manner, where the appended values are listed. Both
481 'col_append_sep_str' and 'col_append_sep_fstr' functions will add an item
482 separator between two consecutive items, and will not add the separator at the
483 beginning of the column. The remainder of the work both functions do is
484 identical to what 'col_append_str' and 'col_append_fstr' do.
486 1.4.8 The col_set_fence and col_prepend_fence_fstr functions.
488 Sometimes a dissector may be called multiple times for different PDUs in the
489 same frame (for example in the case of SCTP chunk bundling: several upper
490 layer data packets may be contained in one SCTP packet). If the upper layer
491 dissector calls 'col_set_str()' or 'col_clear()' on the Info column when it
492 begins dissecting each of those PDUs then when the frame is fully dissected
493 the Info column would contain only the string from the last PDU in the frame.
494 The 'col_set_fence' function erects a "fence" in the column that prevents
495 subsequent 'col_...' calls from clearing the data currently in that column.
496 For example, the SCTP dissector calls 'col_set_fence' on the Info column
497 after it has called any subdissectors for that chunk so that subdissectors
498 of any subsequent chunks may only append to the Info column.
499 'col_prepend_fence_fstr' prepends data before a fence (moving it if
500 necessary). It will create a fence at the end of the prepended data if the
501 fence does not already exist.
504 1.4.9 The col_set_time function.
506 The 'col_set_time' function takes an nstime value as its third argument.
507 This nstime value is a relative value and will be added as such to the
508 column. The fourth argument is the filtername holding this value. This
509 way, rightclicking on the column makes it possible to build a filter
510 based on the time-value.
514 nstime_delta(&ts, &pinfo->fd->abs_ts, &tcpd->ts_first);
515 col_set_time(pinfo->cinfo, COL_REL_CONV_TIME, &ts, "tcp.time_relative");
518 1.5 Constructing the protocol tree.
520 The middle pane of the main window, and the topmost pane of a packet
521 popup window, are constructed from the "protocol tree" for a packet.
523 The protocol tree, or proto_tree, is a GNode, the N-way tree structure
524 available within GLIB. Of course the protocol dissectors don't care
525 what a proto_tree really is; they just pass the proto_tree pointer as an
526 argument to the routines which allow them to add items and new branches
529 When a packet is selected in the packet-list pane, or a packet popup
530 window is created, a new logical protocol tree (proto_tree) is created.
531 The pointer to the proto_tree (in this case, 'protocol tree'), is passed
532 to the top-level protocol dissector, and then to all subsequent protocol
533 dissectors for that packet, and then the GUI tree is drawn via
536 The logical proto_tree needs to know detailed information about the protocols
537 and fields about which information will be collected from the dissection
538 routines. By strictly defining (or "typing") the data that can be attached to a
539 proto tree, searching and filtering becomes possible. This means that for
540 every protocol and field (which I also call "header fields", since they are
541 fields in the protocol headers) which might be attached to a tree, some
542 information is needed.
544 Every dissector routine will need to register its protocols and fields
545 with the central protocol routines (in proto.c). At first I thought I
546 might keep all the protocol and field information about all the
547 dissectors in one file, but decentralization seemed like a better idea.
548 That one file would have gotten very large; one small change would have
549 required a re-compilation of the entire file. Also, by allowing
550 registration of protocols and fields at run-time, loadable modules of
551 protocol dissectors (perhaps even user-supplied) is feasible.
553 To do this, each protocol should have a register routine, which will be
554 called when Wireshark starts. The code to call the register routines is
555 generated automatically; to arrange that a protocol's register routine
556 be called at startup:
558 the file containing a dissector's "register" routine must be
559 added to "DISSECTOR_SRC" in "epan/dissectors/Makefile.common"
560 (and in "epan/CMakeLists.txt");
562 the "register" routine must have a name of the form
563 "proto_register_XXX";
565 the "register" routine must take no argument, and return no
568 the "register" routine's name must appear in the source file
569 either at the beginning of the line, or preceded only by "void "
570 at the beginning of the line (that would typically be the
571 definition) - other white space shouldn't cause a problem, e.g.:
573 void proto_register_XXX(void) {
582 proto_register_XXX( void )
589 and so on should work.
591 For every protocol or field that a dissector wants to register, a variable of
592 type int needs to be used to keep track of the protocol. The IDs are
593 needed for establishing parent/child relationships between protocols and
594 fields, as well as associating data with a particular field so that it
595 can be stored in the logical tree and displayed in the GUI protocol
598 Some dissectors will need to create branches within their tree to help
599 organize header fields. These branches should be registered as header
600 fields. Only true protocols should be registered as protocols. This is
601 so that a display filter user interface knows how to distinguish
602 protocols from fields.
604 A protocol is registered with the name of the protocol and its
607 Here is how the frame "protocol" is registered.
611 proto_frame = proto_register_protocol (
613 /* short name */ "Frame",
614 /* abbrev */ "frame" );
616 A header field is also registered with its name and abbreviation, but
617 information about its data type is needed. It helps to look at
618 the header_field_info struct to see what information is expected:
620 struct header_field_info {
633 A string representing the name of the field. This is the name
634 that will appear in the graphical protocol tree. It must be a non-empty
639 A string with an abbreviation of the field. We concatenate the
640 abbreviation of the parent protocol with an abbreviation for the field,
641 using a period as a separator. For example, the "src" field in an IP packet
642 would have "ip.src" as an abbreviation. It is acceptable to have
643 multiple levels of periods if, for example, you have fields in your
644 protocol that are then subdivided into subfields. For example, TRMAC
645 has multiple error fields, so the abbreviations follow this pattern:
646 "trmac.errors.iso", "trmac.errors.noniso", etc.
648 The abbreviation is the identifier used in a display filter. If it is
649 an empty string then the field will not be filterable.
653 The type of value this field holds. The current field types are:
655 FT_NONE No field type. Used for fields that
656 aren't given a value, and that can only
657 be tested for presence or absence; a
658 field that represents a data structure,
659 with a subtree below it containing
660 fields for the members of the structure,
661 or that represents an array with a
662 subtree below it containing fields for
663 the members of the array, might be an
665 FT_PROTOCOL Used for protocols which will be placing
666 themselves as top-level items in the
667 "Packet Details" pane of the UI.
668 FT_BOOLEAN 0 means "false", any other value means
670 FT_FRAMENUM A frame number; if this is used, the "Go
671 To Corresponding Frame" menu item can
673 FT_UINT8 An 8-bit unsigned integer.
674 FT_UINT16 A 16-bit unsigned integer.
675 FT_UINT24 A 24-bit unsigned integer.
676 FT_UINT32 A 32-bit unsigned integer.
677 FT_UINT64 A 64-bit unsigned integer.
678 FT_INT8 An 8-bit signed integer.
679 FT_INT16 A 16-bit signed integer.
680 FT_INT24 A 24-bit signed integer.
681 FT_INT32 A 32-bit signed integer.
682 FT_INT64 A 64-bit signed integer.
683 FT_FLOAT A single-precision floating point number.
684 FT_DOUBLE A double-precision floating point number.
685 FT_ABSOLUTE_TIME An absolute time from some fixed point in time,
686 displayed as the date, followed by the time, as
687 hours, minutes, and seconds with 9 digits after
689 FT_RELATIVE_TIME Seconds (4 bytes) and nanoseconds (4 bytes)
690 of time relative to an arbitrary time.
691 displayed as seconds and 9 digits
692 after the decimal point.
693 FT_STRING A string of characters, not necessarily
694 NULL-terminated, but possibly NULL-padded.
695 This, and the other string-of-characters
696 types, are to be used for text strings,
698 FT_STRINGZ A NULL-terminated string of characters.
699 The string length is normally the length
700 given in the proto_tree_add_item() call.
701 However if the length given in the call
702 is -1, then the length used is that
703 returned by calling tvb_strsize().
704 FT_UINT_STRING A counted string of characters, consisting
705 of a count (represented as an integral value,
706 of width given in the proto_tree_add_item()
707 call) followed immediately by that number of
709 FT_ETHER A six octet string displayed in
710 Ethernet-address format.
711 FT_BYTES A string of bytes with arbitrary values;
712 used for raw binary data.
713 FT_UINT_BYTES A counted string of bytes, consisting
714 of a count (represented as an integral value,
715 of width given in the proto_tree_add_item()
716 call) followed immediately by that number of
717 arbitrary values; used for raw binary data.
718 FT_IPv4 A version 4 IP address (4 bytes) displayed
719 in dotted-quad IP address format (4
720 decimal numbers separated by dots).
721 FT_IPv6 A version 6 IP address (16 bytes) displayed
722 in standard IPv6 address format.
723 FT_IPXNET An IPX address displayed in hex as a 6-byte
724 network number followed by a 6-byte station
726 FT_GUID A Globally Unique Identifier
727 FT_OID An ASN.1 Object Identifier
728 FT_REL_OID An ASN.1 Relative Object Identifier
729 FT_EUI64 A EUI-64 Address
731 Some of these field types are still not handled in the display filter
732 routines, but the most common ones are. The FT_UINT* variables all
733 represent unsigned integers, and the FT_INT* variables all represent
734 signed integers; the number on the end represent how many bits are used
735 to represent the number.
737 Some constraints are imposed on the header fields depending on the type
738 (e.g. FT_BYTES) of the field. Fields of type FT_ABSOLUTE_TIME must use
739 'ABSOLUTE_TIME_{LOCAL,UTC,DOY_UTC}, NULL, 0x0' as values for the
740 'display, 'strings', and 'bitmask' fields, and all other non-integral
741 types (i.e.. types that are _not_ FT_INT* and FT_UINT*) must use
742 'BASE_NONE, NULL, 0x0' as values for the 'display', 'strings', 'bitmask'
743 fields. The reason is simply that the type itself implicitly defines the
744 nature of 'display', 'strings', 'bitmask'.
748 The display field has a couple of overloaded uses. This is unfortunate,
749 but since we're using C as an application programming language, this sometimes
750 makes for cleaner programs. Right now I still think that overloading
751 this variable was okay.
753 For integer fields (FT_UINT* and FT_INT*), this variable represents the
754 base in which you would like the value displayed. The acceptable bases
764 BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal,
765 respectively. BASE_DEC_HEX and BASE_HEX_DEC display value in two bases
766 (the 1st representation followed by the 2nd in parenthesis).
768 BASE_CUSTOM allows one to specify a callback function pointer that will
769 format the value. The function pointer of the same type as defined by
770 custom_fmt_func_t in epan/proto.h, specifically:
772 void func(gchar *, guint32);
774 The first argument is a pointer to a buffer of the ITEM_LABEL_LENGTH size
775 and the second argument is the value to be formatted.
777 For FT_BOOLEAN fields that are also bitfields (i.e., 'bitmask' is non-zero),
778 'display' is used specify a "field-width" (i.e., tell the proto_tree how
779 wide the parent bitfield is). (If the FT_BOOLEAN 'bitmask' is zero, then
780 'display' must be BASE_NONE).
782 For integer fields a "field-width" is not needed since the type of integer itself
783 (FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, etc.) tells the proto_tree how
784 wide the parent bitfield is.
786 For FT_ABSOLUTE_TIME fields, 'display' is used to indicate whether the
787 time is to be displayed as a time in the time zone for the machine on
788 which Wireshark/TShark is running or as UTC and, for UTC, whether the
789 date should be displayed as "{monthname}, {month} {day_of_month},
790 {year}" or as "{year/day_of_year}".
792 Additionally, BASE_NONE is used for 'display' as a NULL-value. That is, for
793 non-integers other than FT_ABSOLUTE_TIME fields, and non-bitfield
794 FT_BOOLEANs, you'll want to use BASE_NONE in the 'display' field. You may
795 not use BASE_NONE for integers.
797 It is possible that in the future we will record the endianness of
798 integers. If so, it is likely that we'll use a bitmask on the display field
799 so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX.
800 But that has not happened yet; note that there are protocols for which
801 no endianness is specified, such as the X11 protocol and the DCE RPC
802 protocol, so it would not be possible to record the endianness of all
808 Some integer fields, of type FT_UINT*, need labels to represent the true
809 value of a field. You could think of those fields as having an
810 enumerated data type, rather than an integral data type.
812 A 'value_string' structure is a way to map values to strings.
814 typedef struct _value_string {
819 For fields of that type, you would declare an array of "value_string"s:
821 static const value_string valstringname[] = {
822 { INTVAL1, "Descriptive String 1" },
823 { INTVAL2, "Descriptive String 2" },
827 (the last entry in the array must have a NULL 'strptr' value, to
828 indicate the end of the array). The 'strings' field would be set to
829 'VALS(valstringname)'.
831 If the field has a numeric rather than an enumerated type, the 'strings'
832 field would be set to NULL.
834 -- Extended value strings
835 You can also use an extended version of the value_string for faster lookups.
836 It requires a value_string array as input.
837 If all of a contiguous range of values from min to max are present in the array
838 in ascending order the value will be used as a direct index into a value_string array.
840 If the values in the array are not contiguous (ie: there are "gaps"), but are
841 in ascending order a binary search will be used.
843 Note: "gaps" in a value_string array can be filled with "empty" entries eg:
844 {value, "Unknown"} so that direct access to the array is is possible.
846 Note: the value_string array values are *unsigned*; IOW: -1 is greater than 0.
848 { -2, -1, 1, 2 }; wrong: linear search will be used (note gap)
849 { 1, 2, -2, -1 }; correct: binary search will be used
852 { -2, -1, 0, 1, 2 }; OK: direct(indexed) access will be used (note no gap)
854 The init macro (see below) will perform a check on the value string the first
855 time it is used to determine which search algorithm fits and fall back to a
856 linear search if the value_string does not meet the criteria above.
858 Use this macro to initialize the extended value_string at compile time:
860 static value_string_ext valstringname_ext = VALUE_STRING_EXT_INIT(valstringname);
862 Extended value strings can be created at run time by calling
863 value_string_ext_new(<ptr to value_string array>,
864 <total number of entries in the value_string_array>, /* include {0, NULL} entry */
865 <value_string_name>);
867 For hf[] array FT_(U)INT* fields that need a 'valstringname_ext' struct, the
868 'strings' field would be set to '&valstringname_ext'. Furthermore, the 'display'
869 field must be ORed with 'BASE_EXT_STRING' (e.g. BASE_DEC|BASE_EXT_STRING).
873 If the field has a numeric type that might logically fit in ranges of values
874 one can use a range_string struct.
876 Thus a 'range_string' structure is a way to map ranges to strings.
878 typedef struct _range_string {
884 For fields of that type, you would declare an array of "range_string"s:
886 static const range_string rvalstringname[] = {
887 { INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" },
888 { INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" },
892 If INTVAL_MIN equals INTVAL_MAX for a given entry the range_string
893 behavior collapses to the one of value_string.
894 For FT_(U)INT* fields that need a 'range_string' struct, the 'strings' field
895 would be set to 'RVALS(rvalstringname)'. Furthermore, 'display' field must be
896 ORed with 'BASE_RANGE_STRING' (e.g. BASE_DEC|BASE_RANGE_STRING).
899 FT_BOOLEANs have a default map of 0 = "False", 1 (or anything else) = "True".
900 Sometimes it is useful to change the labels for boolean values (e.g.,
901 to "Yes"/"No", "Fast"/"Slow", etc.). For these mappings, a struct called
902 true_false_string is used.
904 typedef struct true_false_string {
909 For Boolean fields for which "False" and "True" aren't the desired
910 labels, you would declare a "true_false_string"s:
912 static const true_false_string boolstringname = {
917 Its two fields are pointers to the string representing truth, and the
918 string representing falsehood. For FT_BOOLEAN fields that need a
919 'true_false_string' struct, the 'strings' field would be set to
920 'TFS(&boolstringname)'.
922 If the Boolean field is to be displayed as "False" or "True", the
923 'strings' field would be set to NULL.
925 Wireshark predefines a whole range of ready made "true_false_string"s
926 in tfs.h, included via packet.h.
930 If the field is a bitfield, then the bitmask is the mask which will
931 leave only the bits needed to make the field when ANDed with a value.
932 The proto_tree routines will calculate 'bitshift' automatically
933 from 'bitmask', by finding the rightmost set bit in the bitmask.
934 This shift is applied before applying string mapping functions or
936 If the field is not a bitfield, then bitmask should be set to 0.
940 This is a string giving a proper description of the field. It should be
941 at least one grammatically complete sentence, or NULL in which case the
942 name field is used. (Please do not use "").
943 It is meant to provide a more detailed description of the field than the
944 name alone provides. This information will be used in the man page, and
945 in a future GUI display-filter creation tool. We might also add tooltips
946 to the labels in the GUI protocol tree, in which case the blurb would
947 be used as the tooltip text.
950 1.5.1 Field Registration.
952 Protocol registration is handled by creating an instance of the
953 header_field_info struct (or an array of such structs), and
954 calling the registration function along with the registration ID of
955 the protocol that is the parent of the fields. Here is a complete example:
957 static int proto_eg = -1;
958 static int hf_field_a = -1;
959 static int hf_field_b = -1;
961 static hf_register_info hf[] = {
964 { "Field A", "proto.field_a", FT_UINT8, BASE_HEX, NULL,
965 0xf0, "Field A represents Apples", HFILL }},
968 { "Field B", "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
969 0x0, "Field B represents Bananas", HFILL }}
972 proto_eg = proto_register_protocol("Example Protocol",
974 proto_register_field_array(proto_eg, hf, array_length(hf));
976 Be sure that your array of hf_register_info structs is declared 'static',
977 since the proto_register_field_array() function does not create a copy
978 of the information in the array... it uses that static copy of the
979 information that the compiler created inside your array. Here's the
980 layout of the hf_register_info struct:
982 typedef struct hf_register_info {
983 int *p_id; /* pointer to parent variable */
984 header_field_info hfinfo;
987 Also be sure to use the handy array_length() macro found in packet.h
988 to have the compiler compute the array length for you at compile time.
990 If you don't have any fields to register, do *NOT* create a zero-length
991 "hf" array; not all compilers used to compile Wireshark support them.
992 Just omit the "hf" array, and the "proto_register_field_array()" call,
995 It is OK to have header fields with a different format be registered with
996 the same abbreviation. For instance, the following is valid:
998 static hf_register_info hf[] = {
1000 { &hf_field_8bit, /* 8-bit version of proto.field */
1001 { "Field (8 bit)", "proto.field", FT_UINT8, BASE_DEC, NULL,
1002 0x00, "Field represents FOO", HFILL }},
1004 { &hf_field_32bit, /* 32-bit version of proto.field */
1005 { "Field (32 bit)", "proto.field", FT_UINT32, BASE_DEC, NULL,
1006 0x00, "Field represents FOO", HFILL }}
1009 This way a filter expression can match a header field, irrespective of the
1010 representation of it in the specific protocol context. This is interesting
1011 for protocols with variable-width header fields.
1012 Note that the formats used must all belong to the same list as defined below:
1013 - FT_INT8, FT_INT16, FT_INT24 and FT_INT32
1014 - FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_IPXNET and FT_FRAMENUM
1015 - FT_UINT64 and FT_EUI64
1016 - FT_STRING, FT_STRINGZ and FT_UINT_STRING
1017 - FT_FLOAT and FT_DOUBLE
1018 - FT_BYTES, FT_UINT_BYTES, FT_AX25, FT_ETHER, FT_VINES, FT_OID and FT_REL_OID
1019 - FT_ABSOLUTE_TIME and FT_RELATIVE_TIME
1021 The HFILL macro at the end of the struct will set reasonable default values
1022 for internally used fields.
1024 1.5.2 Adding Items and Values to the Protocol Tree.
1026 A protocol item is added to an existing protocol tree with one of a
1027 handful of proto_XXX_DO_YYY() functions.
1029 Subtrees can be made with the proto_item_add_subtree() function:
1031 item = proto_tree_add_item(....);
1032 new_tree = proto_item_add_subtree(item, tree_type);
1034 This will add a subtree under the item in question; a subtree can be
1035 created under an item made by any of the "proto_tree_add_XXX" functions,
1036 so that the tree can be given an arbitrary depth.
1038 Subtree types are integers, assigned by
1039 "proto_register_subtree_array()". To register subtree types, pass an
1040 array of pointers to "gint" variables to hold the subtree type values to
1041 "proto_register_subtree_array()":
1043 static gint ett_eg = -1;
1044 static gint ett_field_a = -1;
1046 static gint *ett[] = {
1051 proto_register_subtree_array(ett, array_length(ett));
1053 in your "register" routine, just as you register the protocol and the
1054 fields for that protocol.
1056 The ett_ variables identify particular type of subtree so that if you expand
1057 one of them, Wireshark keeps track of that and, when you click on
1058 another packet, it automatically opens all subtrees of that type.
1059 If you close one of them, all subtrees of that type will be closed when
1060 you move to another packet.
1062 There are several functions that the programmer can use to add either
1063 protocol or field labels to the proto_tree:
1066 proto_tree_add_item(tree, id, tvb, start, length, encoding);
1069 proto_tree_add_text(tree, tvb, start, length, format, ...);
1072 proto_tree_add_text_valist(tree, tvb, start, length, format, ap);
1075 proto_tree_add_none_format(tree, id, tvb, start, length, format, ...);
1078 proto_tree_add_protocol_format(tree, id, tvb, start, length,
1082 proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr);
1085 proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr,
1089 proto_tree_add_bytes_format_value(tree, id, tvb, start, length,
1090 start_ptr, format, ...);
1093 proto_tree_add_time(tree, id, tvb, start, length, value_ptr);
1096 proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr,
1100 proto_tree_add_time_format_value(tree, id, tvb, start, length,
1101 value_ptr, format, ...);
1104 proto_tree_add_ipxnet(tree, id, tvb, start, length, value);
1107 proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value,
1111 proto_tree_add_ipxnet_format_value(tree, id, tvb, start, length,
1112 value, format, ...);
1115 proto_tree_add_ipv4(tree, id, tvb, start, length, value);
1118 proto_tree_add_ipv4_format(tree, id, tvb, start, length, value,
1122 proto_tree_add_ipv4_format_value(tree, id, tvb, start, length,
1123 value, format, ...);
1126 proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr);
1129 proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr,
1133 proto_tree_add_ipv6_format_value(tree, id, tvb, start, length,
1134 value_ptr, format, ...);
1137 proto_tree_add_ax25(tree, id, tvb, start, length, value);
1140 proto_tree_add_ether(tree, id, tvb, start, length, value_ptr);
1143 proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr,
1147 proto_tree_add_ether_format_value(tree, id, tvb, start, length,
1148 value_ptr, format, ...);
1151 proto_tree_add_guid(tree, id, tvb, start, length, value_ptr);
1154 proto_tree_add_guid_format(tree, id, tvb, start, length, value_ptr,
1158 proto_tree_add_guid_format_value(tree, id, tvb, start, length,
1159 value_ptr, format, ...);
1162 proto_tree_add_oid(tree, id, tvb, start, length, value_ptr);
1165 proto_tree_add_oid_format(tree, id, tvb, start, length, value_ptr,
1169 proto_tree_add_oid_format_value(tree, id, tvb, start, length,
1170 value_ptr, format, ...);
1173 proto_tree_add_string(tree, id, tvb, start, length, value_ptr);
1176 proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr,
1180 proto_tree_add_string_format_value(tree, id, tvb, start, length,
1181 value_ptr, format, ...);
1184 proto_tree_add_unicode_string(tree, id, tvb, start, length, value);
1187 proto_tree_add_boolean(tree, id, tvb, start, length, value);
1190 proto_tree_add_boolean_format(tree, id, tvb, start, length, value,
1194 proto_tree_add_boolean_format_value(tree, id, tvb, start, length,
1195 value, format, ...);
1198 proto_tree_add_float(tree, id, tvb, start, length, value);
1201 proto_tree_add_float_format(tree, id, tvb, start, length, value,
1205 proto_tree_add_float_format_value(tree, id, tvb, start, length,
1206 value, format, ...);
1209 proto_tree_add_double(tree, id, tvb, start, length, value);
1212 proto_tree_add_double_format(tree, id, tvb, start, length, value,
1216 proto_tree_add_double_format_value(tree, id, tvb, start, length,
1217 value, format, ...);
1220 proto_tree_add_uint(tree, id, tvb, start, length, value);
1223 proto_tree_add_uint_format(tree, id, tvb, start, length, value,
1227 proto_tree_add_uint_format_value(tree, id, tvb, start, length,
1228 value, format, ...);
1231 proto_tree_add_uint64(tree, id, tvb, start, length, value);
1234 proto_tree_add_uint64_format(tree, id, tvb, start, length, value,
1238 proto_tree_add_uint64_format_value(tree, id, tvb, start, length,
1239 value, format, ...);
1242 proto_tree_add_int(tree, id, tvb, start, length, value);
1245 proto_tree_add_int_format(tree, id, tvb, start, length, value,
1249 proto_tree_add_int_format_value(tree, id, tvb, start, length,
1250 value, format, ...);
1253 proto_tree_add_int64(tree, id, tvb, start, length, value);
1256 proto_tree_add_int64_format(tree, id, tvb, start, length, value,
1260 proto_tree_add_int64_format_value(tree, id, tvb, start, length,
1261 value, format, ...);
1264 proto_tree_add_eui64(tree, id, tvb, start, length, value);
1267 proto_tree_add_eui64_format(tree, id, tvb, start, length, value,
1271 proto_tree_add_eui64_format_value(tree, id, tvb, start, length,
1272 value, format, ...);
1275 proto_tree_add_bitmask(tree, tvb, start, header, ett, fields,
1279 proto_tree_add_bitmask_len(tree, tvb, start, len, header, ett, fields,
1283 proto_tree_add_bitmask_text(tree, tvb, offset, len, name, fallback,
1284 ett, fields, encoding, flags);
1287 proto_tree_add_bits_item(tree, id, tvb, bit_offset, no_of_bits,
1291 proto_tree_add_split_bits_item_ret_val(tree, hf_index, tvb, bit_offset,
1292 crumb_spec, return_value);
1295 proto_tree_add_split_bits_crumb(tree, hf_index, tvb, bit_offset,
1296 crumb_spec, crumb_index);
1299 proto_tree_add_bits_ret_val(tree, id, tvb, bit_offset, no_of_bits,
1300 return_value, encoding);
1303 proto_tree_add_uint_bits_format_value(tree, id, tvb, bit_offset,
1304 no_of_bits, value, format, ...);
1307 proto_tree_add_boolean_bits_format_value(tree, id, tvb, bit_offset,
1308 no_of_bits, value, format, ...);
1311 proto_tree_add_int_bits_format_value(tree, id, tvb, bit_offset,
1312 no_of_bits, value, format, ...);
1315 proto_tree_add_float_bits_format_value(tree, id, tvb, bit_offset,
1316 no_of_bits, value, format, ...);
1318 The 'tree' argument is the tree to which the item is to be added. The
1319 'tvb' argument is the tvbuff from which the item's value is being
1320 extracted; the 'start' argument is the offset from the beginning of that
1321 tvbuff of the item being added, and the 'length' argument is the length,
1322 in bytes, of the item, bit_offset is the offset in bits and no_of_bits
1323 is the length in bits.
1325 The length of some items cannot be determined until the item has been
1326 dissected; to add such an item, add it with a length of -1, and, when the
1327 dissection is complete, set the length with 'proto_item_set_len()':
1330 proto_item_set_len(ti, length);
1332 The "ti" argument is the value returned by the call that added the item
1333 to the tree, and the "length" argument is the length of the item.
1335 proto_tree_add_item()
1336 ---------------------
1337 proto_tree_add_item is used when you wish to do no special formatting.
1338 The item added to the GUI tree will contain the name (as passed in the
1339 proto_register_*() function) and a value. The value will be fetched
1340 from the tvbuff by proto_tree_add_item(), based on the type of the field
1341 and the encoding of the value as specified by the "encoding" argument.
1343 For FT_NONE, FT_BYTES, FT_ETHER, FT_IPv6, FT_IPXNET, FT_OID, FT_REL_OID
1344 fields, and 'protocol' fields the encoding is not relevant; the 'encoding'
1345 argument should be ENC_NA (Not Applicable).
1347 For integral, floating-point, Boolean, FT_GUID, and FT_EUI64 fields,
1348 the encoding specifies the byte order of the value; the 'encoding'
1349 argument should be ENC_LITTLE_ENDIAN if the value is little-endian
1350 and ENC_BIG_ENDIAN if it is big-endian.
1352 For FT_IPv4 fields, the encoding also specifies the byte order of the
1353 value. In almost all cases, the encoding is in network byte order,
1354 hence big-endian, but in at least one protocol dissected by Wireshark,
1355 at least one IPv4 address is byte-swapped, so it's in little-endian
1358 For string fields, the encoding specifies the character set used for the
1359 string and the way individual code points in that character set are
1360 encoded. For FT_UINT_STRING fields, the byte order of the count must be
1361 specified; for UCS-2 and UTF-16, the byte order of the encoding must be
1362 specified (for counted UCS-2 and UTF-16 strings, the byte order of the
1363 count and the 16-bit values in the string must be the same). In other
1364 cases, ENC_NA should be used. The character encodings that are
1365 currently supported are:
1367 ENC_ASCII - ASCII (currently treated as UTF-8; in the future,
1368 all bytes with the 8th bit set will be treated as
1372 ENC_UTF_16 - UTF-16 (currently treated as UCS-2; in the future,
1373 surrogate pairs will be handled, and non-valid 16-bit
1374 code points and surrogate pairs will be treated as
1378 Other encodings will be added in the future.
1380 For FT_ABSOLUTE_TIME fields, the encoding specifies the form in which
1381 the time stamp is specified, as well as its byte order. The time stamp
1382 encodings that are currently supported are:
1384 ENC_TIME_TIMESPEC - seconds (4 bytes) and nanoseconds (4 bytes)
1385 of time since January 1, 1970, midnight UTC.
1387 ENC_TIME_NTP - an NTP timestamp, represented as a 64-bit
1388 unsigned fixed-point number, in seconds relative to 0h
1389 on 1 January 1900. The integer part is in the first 32
1390 bits and the fraction part in the last 32 bits.
1392 For other types, there is no support for proto_tree_add_item().
1394 Now that definitions of fields have detailed information about bitfield
1395 fields, you can use proto_tree_add_item() with no extra processing to
1396 add bitfield values to your tree. Here's an example. Take the Format
1397 Identifier (FID) field in the Transmission Header (TH) portion of the SNA
1398 protocol. The FID is the high nibble of the first byte of the TH. The
1399 FID would be registered like this:
1401 name = "Format Identifier"
1402 abbrev = "sna.th.fid"
1405 strings = sna_th_fid_vals
1408 The bitmask contains the value which would leave only the FID if bitwise-ANDed
1409 against the parent field, the first byte of the TH.
1411 The code to add the FID to the tree would be;
1413 proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1,
1416 The definition of the field already has the information about bitmasking
1417 and bitshifting, so it does the work of masking and shifting for us!
1418 This also means that you no longer have to create value_string structs
1419 with the values bitshifted. The value_string for FID looks like this,
1420 even though the FID value is actually contained in the high nibble.
1421 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
1423 /* Format Identifier */
1424 static const value_string sna_th_fid_vals[] = {
1425 { 0x0, "SNA device <--> Non-SNA Device" },
1426 { 0x1, "Subarea Node <--> Subarea Node" },
1427 { 0x2, "Subarea Node <--> PU2" },
1428 { 0x3, "Subarea Node or SNA host <--> Subarea Node" },
1431 { 0xf, "Adjacent Subarea Nodes" },
1435 The final implication of this is that display filters work the way you'd
1436 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
1437 Subarea Nodes. The user does not have to shift the value of the FID to
1438 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
1441 proto_tree_add_protocol_format()
1442 --------------------------------
1443 proto_tree_add_protocol_format is used to add the top-level item for the
1444 protocol when the dissector routine wants complete control over how the
1445 field and value will be represented on the GUI tree. The ID value for
1446 the protocol is passed in as the "id" argument; the rest of the
1447 arguments are a "printf"-style format and any arguments for that format.
1448 The caller must include the name of the protocol in the format; it is
1449 not added automatically as in proto_tree_add_item().
1451 proto_tree_add_none_format()
1452 ----------------------------
1453 proto_tree_add_none_format is used to add an item of type FT_NONE.
1454 The caller must include the name of the field in the format; it is
1455 not added automatically as in proto_tree_add_item().
1457 proto_tree_add_bytes()
1458 proto_tree_add_time()
1459 proto_tree_add_ipxnet()
1460 proto_tree_add_ipv4()
1461 proto_tree_add_ipv6()
1462 proto_tree_add_ether()
1463 proto_tree_add_string()
1464 proto_tree_add_boolean()
1465 proto_tree_add_float()
1466 proto_tree_add_double()
1467 proto_tree_add_uint()
1468 proto_tree_add_uint64()
1469 proto_tree_add_int()
1470 proto_tree_add_int64()
1471 proto_tree_add_guid()
1472 proto_tree_add_oid()
1473 proto_tree_add_eui64()
1474 ------------------------
1475 These routines are used to add items to the protocol tree if either:
1477 the value of the item to be added isn't just extracted from the
1478 packet data, but is computed from data in the packet;
1480 the value was fetched into a variable.
1482 The 'value' argument has the value to be added to the tree.
1484 NOTE: in all cases where the 'value' argument is a pointer, a copy is
1485 made of the object pointed to; if you have dynamically allocated a
1486 buffer for the object, that buffer will not be freed when the protocol
1487 tree is freed - you must free the buffer yourself when you don't need it
1490 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
1493 For proto_tree_add_bytes_format() and proto_tree_add_bytes_format_value(), the
1494 'value_ptr' argument is a pointer to a sequence of bytes or NULL if the bytes
1495 should be taken from the given TVB using the given offset and length.
1497 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
1498 "nstime_t", which is a structure containing the time to be added; it has
1499 'secs' and 'nsecs' members, giving the integral part and the fractional
1500 part of a time in units of seconds, with 'nsecs' being the number of
1501 nanoseconds. For absolute times, "secs" is a UNIX-style seconds since
1502 January 1, 1970, 00:00:00 GMT value.
1504 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
1507 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
1508 address, in network byte order.
1510 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
1511 128-bit IPv6 address.
1513 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
1516 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
1517 text string; this string must be NULL terminated even if the string in the
1518 TVB is not (as may be the case with FT_STRINGs).
1520 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer.
1521 It is masked and shifted as defined by the field info after which zero
1522 means "false", and non-zero means "true".
1524 For proto_tree_add_float(), the 'value' argument is a 'float' in the
1525 host's floating-point format.
1527 For proto_tree_add_double(), the 'value' argument is a 'double' in the
1528 host's floating-point format.
1530 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
1531 integer value, in host byte order. (This routine cannot be used to add
1534 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
1535 integer value, in host byte order.
1537 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
1538 integer value, in host byte order. (This routine cannot be used to add
1541 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
1542 integer value, in host byte order.
1544 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
1547 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
1548 ASN.1 Object Identifier.
1550 For proto_tree_add_eui64(), the 'value' argument is a 64-bit integer
1553 proto_tree_add_bytes_format()
1554 proto_tree_add_time_format()
1555 proto_tree_add_ipxnet_format()
1556 proto_tree_add_ipv4_format()
1557 proto_tree_add_ipv6_format()
1558 proto_tree_add_ether_format()
1559 proto_tree_add_string_format()
1560 proto_tree_add_boolean_format()
1561 proto_tree_add_float_format()
1562 proto_tree_add_double_format()
1563 proto_tree_add_uint_format()
1564 proto_tree_add_uint64_format()
1565 proto_tree_add_int_format()
1566 proto_tree_add_int64_format()
1567 proto_tree_add_guid_format()
1568 proto_tree_add_oid_format()
1569 proto_tree_add_eui64_format()
1570 ----------------------------
1571 These routines are used to add items to the protocol tree when the
1572 dissector routine wants complete control over how the field and value
1573 will be represented on the GUI tree. The argument giving the value is
1574 the same as the corresponding proto_tree_add_XXX() function; the rest of
1575 the arguments are a "printf"-style format and any arguments for that
1576 format. The caller must include the name of the field in the format; it
1577 is not added automatically as in the proto_tree_add_XXX() functions.
1579 proto_tree_add_bytes_format_value()
1580 proto_tree_add_time_format_value()
1581 proto_tree_add_ipxnet_format_value()
1582 proto_tree_add_ipv4_format_value()
1583 proto_tree_add_ipv6_format_value()
1584 proto_tree_add_ether_format_value()
1585 proto_tree_add_string_format_value()
1586 proto_tree_add_boolean_format_value()
1587 proto_tree_add_float_format_value()
1588 proto_tree_add_double_format_value()
1589 proto_tree_add_uint_format_value()
1590 proto_tree_add_uint64_format_value()
1591 proto_tree_add_int_format_value()
1592 proto_tree_add_int64_format_value()
1593 proto_tree_add_guid_format_value()
1594 proto_tree_add_oid_format_value()
1595 proto_tree_add_eui64_format_value()
1596 ------------------------------------
1598 These routines are used to add items to the protocol tree when the
1599 dissector routine wants complete control over how the value will be
1600 represented on the GUI tree. The argument giving the value is the same
1601 as the corresponding proto_tree_add_XXX() function; the rest of the
1602 arguments are a "printf"-style format and any arguments for that format.
1603 With these routines, unlike the proto_tree_add_XXX_format() routines,
1604 the name of the field is added automatically as in the
1605 proto_tree_add_XXX() functions; only the value is added with the format.
1607 proto_tree_add_text()
1608 ---------------------
1609 proto_tree_add_text() is used to add a label to the GUI tree. It will
1610 contain no value, so it is not searchable in the display filter process.
1611 This function was needed in the transition from the old-style proto_tree
1612 to this new-style proto_tree so that Wireshark would still decode all
1613 protocols w/o being able to filter on all protocols and fields.
1614 Otherwise we would have had to cripple Wireshark's functionality while we
1615 converted all the old-style proto_tree calls to the new-style proto_tree
1616 calls. In other words, you should not use this in new code unless you've got
1617 a specific reason (see below).
1619 This can (and should only) be used for items with subtrees, which may not
1620 have values themselves - the items in the subtree are the ones with values.
1621 In other words, if you're using proto_tree_add_text() and not using the
1622 return value to build a new tree, you probably shouldn't be using this
1623 function: you probably should be using proto_tree_add_item() instead.
1625 For a subtree, the label on the subtree might reflect some of the items
1626 in the subtree. This means the label can't be set until at least some
1627 of the items in the subtree have been dissected. To do this, use
1628 'proto_item_set_text()' or 'proto_item_append_text()':
1631 proto_item_set_text(proto_item *ti, ...);
1634 proto_item_append_text(proto_item *ti, ...);
1636 'proto_item_set_text()' takes as an argument the value returned by
1637 'proto_tree_add_text()', a 'printf'-style format string, and a set of
1638 arguments corresponding to '%' format items in that string, and replaces
1639 the text for the item created by 'proto_tree_add_text()' with the result
1640 of applying the arguments to the format string.
1642 'proto_item_append_text()' is similar, but it appends to the text for
1643 the item the result of applying the arguments to the format string.
1645 For example, early in the dissection, one might do:
1647 ti = proto_tree_add_text(tree, tvb, offset, length, <label>);
1651 proto_item_set_text(ti, "%s: %s", type, value);
1653 after the "type" and "value" fields have been extracted and dissected.
1654 <label> would be a label giving what information about the subtree is
1655 available without dissecting any of the data in the subtree.
1657 Note that an exception might be thrown when trying to extract the values of
1658 the items used to set the label, if not all the bytes of the item are
1659 available. Thus, one should create the item with text that is as
1660 meaningful as possible, and set it or append additional information to
1661 it as the values needed to supply that information are extracted.
1663 proto_tree_add_text_valist()
1664 ----------------------------
1665 This is like proto_tree_add_text(), but takes, as the last argument, a
1666 'va_list'; it is used to allow routines that take a printf-like
1667 variable-length list of arguments to add a text item to the protocol
1670 proto_tree_add_bits_item()
1671 --------------------------
1672 Adds a number of bits to the protocol tree which does not have to be byte
1673 aligned. The offset and length is in bits.
1676 ..10 1010 10.. .... "value" (formatted as FT_ indicates).
1678 proto_tree_add_bits_ret_val()
1679 -----------------------------
1680 Works in the same way but also returns the value of the read bits.
1682 proto_tree_add_split_bits_item_ret_val()
1683 -----------------------------------
1684 Similar, but is used for items that are made of 2 or more smaller sets of bits (crumbs)
1685 which are not contiguous, but are concatenated to form the actual value. The size of
1686 the crumbs and the order of assembly are specified in an array of crumb_spec structures.
1688 proto_tree_add_split_bits_crumb()
1689 ---------------------------------
1690 Helper function for the above, to add text for each crumb as it is encountered.
1692 proto_tree_add_bitmask() et al.
1693 -------------------------------
1694 These functions provide easy to use and convenient dissection of many types of common
1695 bitmasks into individual fields.
1697 header is an integer type and must be of type FT_[U]INT{8|16|24|32} and
1698 represents the entire dissectable width of the bitmask.
1700 'header' and 'ett' are the hf fields and ett field respectively to create an
1701 expansion that covers the bytes of the bitmask.
1703 'fields' is a NULL terminated array of pointers to hf fields representing
1704 the individual subfields of the bitmask. These fields must either be integers
1705 (usually of the same byte width as 'header') or of the type FT_BOOLEAN.
1706 Each of the entries in 'fields' will be dissected as an item under the
1707 'header' expansion and also IF the field is a boolean and IF it is set to 1,
1708 then the name of that boolean field will be printed on the 'header' expansion
1709 line. For integer type subfields that have a value_string defined, the
1710 matched string from that value_string will be printed on the expansion line
1713 Example: (from the SCSI dissector)
1714 static int hf_scsi_inq_peripheral = -1;
1715 static int hf_scsi_inq_qualifier = -1;
1716 static int hf_scsi_inq_devtype = -1;
1718 static gint ett_scsi_inq_peripheral = -1;
1720 static const int *peripheral_fields[] = {
1721 &hf_scsi_inq_qualifier,
1722 &hf_scsi_inq_devtype,
1726 /* Qualifier and DeviceType */
1727 proto_tree_add_bitmask(tree, tvb, offset, hf_scsi_inq_peripheral,
1728 ett_scsi_inq_peripheral, peripheral_fields, ENC_BIG_ENDIAN);
1731 { &hf_scsi_inq_peripheral,
1732 {"Peripheral", "scsi.inquiry.peripheral", FT_UINT8, BASE_HEX,
1733 NULL, 0, NULL, HFILL}},
1734 { &hf_scsi_inq_qualifier,
1735 {"Qualifier", "scsi.inquiry.qualifier", FT_UINT8, BASE_HEX,
1736 VALS (scsi_qualifier_val), 0xE0, NULL, HFILL}},
1737 { &hf_scsi_inq_devtype,
1738 {"Device Type", "scsi.inquiry.devtype", FT_UINT8, BASE_HEX,
1739 VALS (scsi_devtype_val), SCSI_DEV_BITS, NULL, HFILL}},
1742 Which provides very pretty dissection of this one byte bitmask.
1744 Peripheral: 0x05, Qualifier: Device type is connected to logical unit, Device Type: CD-ROM
1745 000. .... = Qualifier: Device type is connected to logical unit (0x00)
1746 ...0 0101 = Device Type: CD-ROM (0x05)
1748 The proto_tree_add_bitmask_text() function is an extended version of
1749 the proto_tree_add_bitmask() function. In addition, it allows to:
1750 - Provide a leading text (e.g. "Flags: ") that will appear before
1751 the comma-separated list of field values
1752 - Provide a fallback text (e.g. "None") that will be appended if
1753 no fields warranted a change to the top-level title.
1754 - Using flags, specify which fields will affect the top-level title.
1756 There are the following flags defined:
1758 BMT_NO_APPEND - the title is taken "as-is" from the 'name' argument.
1759 BMT_NO_INT - only boolean flags are added to the title.
1760 BMT_NO_FALSE - boolean flags are only added to the title if they are set.
1761 BMT_NO_TFS - only add flag name to the title, do not use true_false_string
1763 The proto_tree_add_bitmask() behavior can be obtained by providing
1764 both 'name' and 'fallback' arguments as NULL, and a flags of
1765 (BMT_NO_FALSE|BMT_NO_TFS).
1767 The proto_tree_add_bitmask_len() function is intended for protocols where
1768 bitmask length is permitted to vary, so a length is specified explicitly
1769 along with the bitmask value. USB Video "bmControl" and "bControlSize"
1770 fields follow this pattern. The primary intent of this is "forward
1771 compatibility," enabling an interpreter coded for version M of a structure
1772 to comprehend fields in version N of the structure, where N > M and
1773 bControlSize increases from version M to version N.
1775 proto_tree_add_bitmask_len() is an extended version of proto_tree_add_bitmask()
1776 that uses an explicitly specified (rather than inferred) length to control
1777 dissection. Because of this, it may encounter two cases that
1778 proto_tree_add_bitmask() and proto_tree_add_bitmask_text() may not:
1779 - A length that exceeds that of the 'header' and bitmask subfields.
1780 In this case the least-significant bytes of the bitmask are dissected.
1781 An expert warning is generated in this case, because the dissection code
1782 likely needs to be updated for a new revision of the protocol.
1783 - A length that is shorter than that of the 'header' and bitmask subfields.
1784 In this case, subfields whose data is fully present are dissected,
1785 and other subfields are not. No warning is generated in this case,
1786 because the dissection code is likely for a later revision of the protocol
1787 than the packet it was called to interpret.
1790 PROTO_ITEM_SET_GENERATED()
1791 --------------------------
1792 PROTO_ITEM_SET_GENERATED is used to mark fields as not being read from the
1793 captured data directly, but inferred from one or more values.
1795 One of the primary uses of this is the presentation of verification of
1796 checksums. Every IP packet has a checksum line, which can present the result
1797 of the checksum verification, if enabled in the preferences. The result is
1798 presented as a subtree, where the result is enclosed in square brackets
1799 indicating a generated field.
1801 Header checksum: 0x3d42 [correct]
1805 PROTO_ITEM_SET_HIDDEN()
1806 -----------------------
1807 PROTO_ITEM_SET_HIDDEN is used to hide fields, which have already been added
1808 to the tree, from being visible in the displayed tree.
1810 NOTE that creating hidden fields is actually quite a bad idea from a UI design
1811 perspective because the user (someone who did not write nor has ever seen the
1812 code) has no way of knowing that hidden fields are there to be filtered on
1813 thus defeating the whole purpose of putting them there. A Better Way might
1814 be to add the fields (that might otherwise be hidden) to a subtree where they
1815 won't be seen unless the user opens the subtree--but they can be found if the
1818 One use for hidden fields (which would be better implemented using visible
1819 fields in a subtree) follows: The caller may want a value to be
1820 included in a tree so that the packet can be filtered on this field, but
1821 the representation of that field in the tree is not appropriate. An
1822 example is the token-ring routing information field (RIF). The best way
1823 to show the RIF in a GUI is by a sequence of ring and bridge numbers.
1824 Rings are 3-digit hex numbers, and bridges are single hex digits:
1826 RIF: 001-A-013-9-C0F-B-555
1828 In the case of RIF, the programmer should use a field with no value and
1829 use proto_tree_add_none_format() to build the above representation. The
1830 programmer can then add the ring and bridge values, one-by-one, with
1831 proto_tree_add_item() and hide them with PROTO_ITEM_SET_HIDDEN() so that the
1832 user can then filter on or search for a particular ring or bridge. Here's a
1833 skeleton of how the programmer might code this.
1836 rif = create_rif_string(...);
1838 proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
1840 for(i = 0; i < num_rings; i++) {
1843 pi = proto_tree_add_item(tree, hf_tr_rif_ring, ...,
1845 PROTO_ITEM_SET_HIDDEN(pi);
1847 for(i = 0; i < num_rings - 1; i++) {
1850 pi = proto_tree_add_item(tree, hf_tr_rif_bridge, ...,
1852 PROTO_ITEM_SET_HIDDEN(pi);
1855 The logical tree has these items:
1857 hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
1858 hf_tr_rif_ring, hidden, value=0x001
1859 hf_tr_rif_bridge, hidden, value=0xA
1860 hf_tr_rif_ring, hidden, value=0x013
1861 hf_tr_rif_bridge, hidden, value=0x9
1862 hf_tr_rif_ring, hidden, value=0xC0F
1863 hf_tr_rif_bridge, hidden, value=0xB
1864 hf_tr_rif_ring, hidden, value=0x555
1866 GUI or print code will not display the hidden fields, but a display
1867 filter or "packet grep" routine will still see the values. The possible
1868 filter is then possible:
1870 tr.rif_ring eq 0x013
1874 PROTO_ITEM_SET_URL is used to mark fields as containing a URL. This can only
1875 be done with fields of type FT_STRING(Z). If these fields are presented they
1876 are underlined, as could be done in a browser. These fields are sensitive to
1877 clicks as well, launching the configured browser with this URL as parameter.
1879 1.6 Utility routines.
1881 1.6.1 val_to_str, val_to_str_const, try_val_to_str and try_val_to_str_idx
1883 A dissector may need to convert a value to a string, using a
1884 'value_string' structure, by hand, rather than by declaring a field with
1885 an associated 'value_string' structure; this might be used, for example,
1886 to generate a COL_INFO line for a frame.
1888 val_to_str() handles the most common case:
1891 val_to_str(guint32 val, const value_string *vs, const char *fmt)
1893 If the value 'val' is found in the 'value_string' table pointed to by
1894 'vs', 'val_to_str' will return the corresponding string; otherwise, it
1895 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
1896 to generate a string, and will return a pointer to that string.
1897 You can use it in a call to generate a COL_INFO line for a frame such as
1899 col_add_fstr(COL_INFO, ", %s", val_to_str(val, table, "Unknown %d"));
1901 If you don't need to display 'val' in your fmt string, you can use
1902 val_to_str_const() which just takes a string constant instead and returns it
1903 unmodified when 'val' isn't found.
1905 If you need to handle the failure case in some custom way, try_val_to_str()
1906 will return NULL if val isn't found:
1909 try_val_to_str(guint32 val, const value_string *vs)
1911 Note that, you must check whether 'try_val_to_str()' returns NULL, and arrange
1912 that its return value not be dereferenced if it's NULL. 'try_val_to_str_idx()'
1913 behaves similarly, except it also returns an index into the value_string array,
1914 or -1 if 'val' was not found.
1916 The *_ext functions are "extended" versions of those already described. They
1917 should be used for large value-string arrays which contain many entries. They
1918 implement value to string conversions which will do either a direct access or
1919 a binary search of the value string array if possible. See
1920 "Extended Value Strings" under section 1.6 "Constructing the protocol tree" for
1923 See epan/value_string.h for detailed information on the various value_string
1926 1.6.2 rval_to_str, try_rval_to_str and try_rval_to_str_idx
1928 A dissector may need to convert a range of values to a string, using a
1929 'range_string' structure.
1931 Most of the same functions exist as with regular value_strings (see section
1932 1.6.1) except with the names 'rval' instead of 'val'.
1935 1.7 Calling Other Dissectors.
1937 As each dissector completes its portion of the protocol analysis, it
1938 is expected to create a new tvbuff of type TVBUFF_SUBSET which
1939 contains the payload portion of the protocol (that is, the bytes
1940 that are relevant to the next dissector).
1942 The syntax for creating a new TVBUFF_SUBSET is:
1944 next_tvb = tvb_new_subset(tvb, offset, length, reported_length)
1947 tvb is the tvbuff that the dissector has been working on. It
1948 can be a tvbuff of any type.
1950 next_tvb is the new TVBUFF_SUBSET.
1952 offset is the byte offset of 'tvb' at which the new tvbuff
1953 should start. The first byte is the 0th byte.
1955 length is the number of bytes in the new TVBUFF_SUBSET. A length
1956 argument of -1 says to use as many bytes as are available in
1959 reported_length is the number of bytes that the current protocol
1960 says should be in the payload. A reported_length of -1 says that
1961 the protocol doesn't say anything about the size of its payload.
1964 An example from packet-ipx.c -
1967 dissect_ipx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1970 int reported_length, available_length;
1973 /* Make the next tvbuff */
1975 /* IPX does have a length value in the header, so calculate report_length */
1976 Set this to -1 if there isn't any length information in the protocol
1978 reported_length = ipx_length - IPX_HEADER_LEN;
1980 /* Calculate the available data in the packet,
1981 set this to -1 to use all the data in the tv_buffer
1983 available_length = tvb_length(tvb) - IPX_HEADER_LEN;
1985 /* Create the tvbuffer for the next dissector */
1986 next_tvb = tvb_new_subset(tvb, IPX_HEADER_LEN,
1987 MIN(available_length, reported_length),
1990 /* call the next dissector */
1991 dissector_next( next_tvb, pinfo, tree);
1994 1.8 Editing Makefile.common and CMakeLists.txt to add your dissector.
1996 To arrange that your dissector will be built as part of Wireshark, you
1997 must add the name of the source file for your dissector to the
1998 'DISSECTOR_SRC' macro in the 'Makefile.common' file in the 'epan/dissectors'
1999 directory. (Note that this is for modern versions of UNIX, so there
2000 is no 14-character limitation on file names, and for modern versions of
2001 Windows, so there is no 8.3-character limitation on file names.)
2003 If your dissector also has its own header file or files, you must add
2004 them to the 'DISSECTOR_INCLUDES' macro in the 'Makefile.common' file in
2005 the 'epan/dissectors' directory, so that it's included when release source
2006 tarballs are built (otherwise, the source in the release tarballs won't
2009 In addition to the above, you should add your dissector source file name
2010 to the DISSECTOR_SRC section of epan/CMakeLists.txt
2013 1.9 Using the SVN source code tree.
2015 See <http://www.wireshark.org/develop.html>
2018 1.9a Using git with the SVN source code tree.
2020 Install git and the git-svn package.
2021 A probably incomplete list for various OSes of git GUIs can be found at
2022 "http://delicious.com/matthew.mccullough/git+gui".
2023 Run "mkdir git; cd git; git svn clone <svn-url>", e.g. if you are using
2024 the anonymous svn tree, run
2025 "git svn clone http://anonsvn.wireshark.org/wireshark/trunk/"
2027 After that, a typical workflow may look like this (from "man git-svn"):
2029 # Clone a repo (like git clone):
2030 git svn clone http://svn.example.com/project/trunk
2031 # Enter the newly cloned directory:
2033 # You should be on master branch, double-check with ´git branch´
2035 # Do some work and commit locally to git:
2037 # Something is committed to SVN, rebase your local changes against the
2038 # latest changes in SVN:
2040 # Now commit your changes (that were committed previously using git) to SVN
2041 # as well as automatically updating your working HEAD:
2043 # Append svn:ignore settings to the default git exclude file:
2044 git svn show-ignore >> .git/info/exclude
2047 1.10 Submitting code for your new dissector.
2049 - VERIFY that your dissector code does not use prohibited or deprecated APIs
2051 perl <wireshark_root>/tools/checkAPIs.pl <source-filename(s)>
2053 - VERIFY that your dissector code does not contain any header field related
2055 perl <wireshark_root>/tools/checkhf.pl <source-filename(s)>
2057 - VERIFY that your dissector code does not contain any display filter related
2059 perl <wireshark_root>/tools/checkfiltername.pl <source-filename(s)>
2061 - CHECK your dissector with CppCheck (http://cppcheck.sourceforge.net/) using
2062 Wireshark's customized configuration. This is particularly important on
2063 Windows, since Microsoft's compiler warnings are quite thin:
2064 ./tools/cppcheck/cppcheck.sh <source-filename(s)>
2066 - TEST YOUR DISSECTOR BEFORE SUBMITTING IT.
2067 Use fuzz-test.sh and/or randpkt against your dissector. These are
2068 described at <http://wiki.wireshark.org/FuzzTesting>.
2070 - Subscribe to <mailto:wireshark-dev[AT]wireshark.org> by sending an email to
2071 <mailto:wireshark-dev-request[AT]wireshark.org?body="help"> or visiting
2072 <http://www.wireshark.org/lists/>.
2074 - 'svn add' all the files of your new dissector.
2076 - 'svn diff' the workspace and save the result to a file.
2078 - Edit the diff file - remove any changes unrelated to your new dissector,
2079 e.g. changes in config.nmake
2081 - Submit a bug report to the Wireshark bug database, found at
2082 <http://bugs.wireshark.org>, qualified as an enhancement and attach your
2083 diff file there. Set the review request flag to '?' so it will pop up in
2084 the patch review list.
2086 - Create a Wiki page on the protocol at <http://wiki.wireshark.org>.
2087 A template is provided so it is easy to setup in a consistent style.
2088 See: <http://wiki.wireshark.org/HowToEdit>
2089 and <http://wiki.wireshark.org/ProtocolReference>
2091 - If possible, add sample capture files to the sample captures page at
2092 <http://wiki.wireshark.org/SampleCaptures>. These files are used by
2093 the automated build system for fuzz testing.
2095 - If you find that you are contributing a lot to wireshark on an ongoing
2096 basis you can request to become a committer which will allow you to
2097 commit files to subversion directly.
2099 2. Advanced dissector topics.
2103 Some of the advanced features are being worked on constantly. When using them
2104 it is wise to check the relevant header and source files for additional details.
2106 2.2 Following "conversations".
2108 In wireshark a conversation is defined as a series of data packets between two
2109 address:port combinations. A conversation is not sensitive to the direction of
2110 the packet. The same conversation will be returned for a packet bound from
2111 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
2113 2.2.1 Conversation Routines
2115 There are six routines that you will use to work with a conversation:
2116 conversation_new, find_conversation, conversation_add_proto_data,
2117 conversation_get_proto_data, conversation_delete_proto_data,
2118 and conversation_set_dissector.
2121 2.2.1.1 The conversation_init function.
2123 This is an internal routine for the conversation code. As such you
2124 will not have to call this routine. Just be aware that this routine is
2125 called at the start of each capture and before the packets are filtered
2126 with a display filter. The routine will destroy all stored
2127 conversations. This routine does NOT clean up any data pointers that are
2128 passed in the conversation_add_proto_data 'data' variable. You are
2129 responsible for this clean up if you pass a malloc'ed pointer
2132 See item 2.2.1.5 for more information about use of the 'data' pointer.
2135 2.2.1.2 The conversation_new function.
2137 This routine will create a new conversation based upon two address/port
2138 pairs. If you want to associate with the conversation a pointer to a
2139 private data structure you must use the conversation_add_proto_data
2140 function. The ptype variable is used to differentiate between
2141 conversations over different protocols, i.e. TCP and UDP. The options
2142 variable is used to define a conversation that will accept any destination
2143 address and/or port. Set options = 0 if the destination port and address
2144 are know when conversation_new is called. See section 2.4 for more
2145 information on usage of the options parameter.
2147 The conversation_new prototype:
2148 conversation_t *conversation_new(guint32 setup_frame, address *addr1,
2149 address *addr2, port_type ptype, guint32 port1, guint32 port2,
2153 guint32 setup_frame = The lowest numbered frame for this conversation
2154 address* addr1 = first data packet address
2155 address* addr2 = second data packet address
2156 port_type ptype = port type, this is defined in packet.h
2157 guint32 port1 = first data packet port
2158 guint32 port2 = second data packet port
2159 guint options = conversation options, NO_ADDR2 and/or NO_PORT2
2161 setup_frame indicates the first frame for this conversation, and is used to
2162 distinguish multiple conversations with the same addr1/port1 and addr2/port2
2163 pair that occur within the same capture session.
2165 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
2166 are the second address/port pair. A conversation doesn't have source
2167 and destination address/port pairs - packets in a conversation go in
2168 both directions - so "addr1"/"port1" may be the source or destination
2169 address/port pair; "addr2"/"port2" would be the other pair.
2171 If NO_ADDR2 is specified, the conversation is set up so that a
2172 conversation lookup will match only the "addr1" address; if NO_PORT2 is
2173 specified, the conversation is set up so that a conversation lookup will
2174 match only the "port1" port; if both are specified, i.e.
2175 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
2176 match only the "addr1"/"port1" address/port pair. This can be used if a
2177 packet indicates that, later in the capture, a conversation will be
2178 created using certain addresses and ports, in the case where the packet
2179 doesn't specify the addresses and ports of both sides.
2181 2.2.1.3 The find_conversation function.
2183 Call this routine to look up a conversation. If no conversation is found,
2184 the routine will return a NULL value.
2186 The find_conversation prototype:
2188 conversation_t *find_conversation(guint32 frame_num, address *addr_a,
2189 address *addr_b, port_type ptype, guint32 port_a, guint32 port_b,
2193 guint32 frame_num = a frame number to match
2194 address* addr_a = first address
2195 address* addr_b = second address
2196 port_type ptype = port type
2197 guint32 port_a = first data packet port
2198 guint32 port_b = second data packet port
2199 guint options = conversation options, NO_ADDR_B and/or NO_PORT_B
2201 frame_num is a frame number to match. The conversation returned is where
2202 (frame_num >= conversation->setup_frame
2203 && frame_num < conversation->next->setup_frame)
2204 Suppose there are a total of 3 conversations (A, B, and C) that match
2205 addr_a/port_a and addr_b/port_b, where the setup_frame used in
2206 conversation_new() for A, B and C are 10, 50, and 100 respectively. The
2207 frame_num passed in find_conversation is compared to the setup_frame of each
2208 conversation. So if (frame_num >= 10 && frame_num < 50), conversation A is
2209 returned. If (frame_num >= 50 && frame_num < 100), conversation B is returned.
2210 If (frame_num >= 100) conversation C is returned.
2212 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
2213 "port_b" are the second address/port pair. Again, as a conversation
2214 doesn't have source and destination address/port pairs, so
2215 "addr_a"/"port_a" may be the source or destination address/port pair;
2216 "addr_b"/"port_b" would be the other pair. The search will match the
2217 "a" address/port pair against both the "1" and "2" address/port pairs,
2218 and match the "b" address/port pair against both the "2" and "1"
2219 address/port pairs; you don't have to worry about which side the "a" or
2220 "b" pairs correspond to.
2222 If the NO_ADDR_B flag was specified to "find_conversation()", the
2223 "addr_b" address will be treated as matching any "wildcarded" address;
2224 if the NO_PORT_B flag was specified, the "port_b" port will be treated
2225 as matching any "wildcarded" port. If both flags are specified, i.e.
2226 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
2227 any "wildcarded" address and the "port_b" port will be treated as
2228 matching any "wildcarded" port.
2231 2.2.1.4 The find_or_create_conversation function.
2233 This convenience function will create find an existing conversation (by calling
2234 find_conversation()) and, if a conversation does not already exist, create a
2235 new conversation by calling conversation_new().
2237 The find_or_create_conversation prototype:
2239 extern conversation_t *find_or_create_conversation(packet_info *pinfo);
2242 packet_info *pinfo = the packet_info structure
2244 The frame number and the addresses necessary for find_conversation() and
2245 conversation_new() are taken from the pinfo structure (as is commonly done)
2246 and no 'options' are used.
2249 2.2.1.5 The conversation_add_proto_data function.
2251 Once you have created a conversation with conversation_new, you can
2252 associate data with it using this function.
2254 The conversation_add_proto_data prototype:
2256 void conversation_add_proto_data(conversation_t *conv, int proto,
2260 conversation_t *conv = the conversation in question
2261 int proto = registered protocol number
2262 void *data = dissector data structure
2264 "conversation" is the value returned by conversation_new. "proto" is a
2265 unique protocol number created with proto_register_protocol. Protocols
2266 are typically registered in the proto_register_XXXX section of your
2267 dissector. "data" is a pointer to the data you wish to associate with the
2268 conversation. "data" usually points to "wmem_alloc'd" memory; the
2269 memory will be automatically freed each time a new dissection begins
2270 and thus need not be managed (freed) by the dissector.
2271 Using the protocol number allows several dissectors to
2272 associate data with a given conversation.
2275 2.2.1.6 The conversation_get_proto_data function.
2277 After you have located a conversation with find_conversation, you can use
2278 this function to retrieve any data associated with it.
2280 The conversation_get_proto_data prototype:
2282 void *conversation_get_proto_data(conversation_t *conv, int proto);
2285 conversation_t *conv = the conversation in question
2286 int proto = registered protocol number
2288 "conversation" is the conversation created with conversation_new. "proto"
2289 is a unique protocol number created with proto_register_protocol,
2290 typically in the proto_register_XXXX portion of a dissector. The function
2291 returns a pointer to the data requested, or NULL if no data was found.
2294 2.2.1.7 The conversation_delete_proto_data function.
2296 After you are finished with a conversation, you can remove your association
2297 with this function. Please note that ONLY the conversation entry is
2298 removed. If you have allocated any memory for your data (other than with wmem_alloc),
2299 you must free it as well.
2301 The conversation_delete_proto_data prototype:
2303 void conversation_delete_proto_data(conversation_t *conv, int proto);
2306 conversation_t *conv = the conversation in question
2307 int proto = registered protocol number
2309 "conversation" is the conversation created with conversation_new. "proto"
2310 is a unique protocol number created with proto_register_protocol,
2311 typically in the proto_register_XXXX portion of a dissector.
2313 2.2.1.8 The conversation_set_dissector function
2315 This function sets the protocol dissector to be invoked whenever
2316 conversation parameters (addresses, port_types, ports, etc) are matched
2317 during the dissection of a packet.
2319 The conversation_set_dissector prototype:
2321 void conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle);
2324 conversation_t *conv = the conversation in question
2325 const dissector_handle_t handle = the dissector handle.
2328 2.2.2 Using timestamps relative to the conversation
2330 There is a framework to calculate timestamps relative to the start of the
2331 conversation. First of all the timestamp of the first packet that has been
2332 seen in the conversation must be kept in the protocol data to be able
2333 to calculate the timestamp of the current packet relative to the start
2334 of the conversation. The timestamp of the last packet that was seen in the
2335 conversation should also be kept in the protocol data. This way the
2336 delta time between the current packet and the previous packet in the
2337 conversation can be calculated.
2339 So add the following items to the struct that is used for the protocol data:
2344 The ts_prev value should only be set during the first run through the
2345 packets (ie pinfo->fd->flags.visited is false).
2347 Next step is to use the per-packet information (described in section 2.5)
2348 to keep the calculated delta timestamp, as it can only be calculated
2349 on the first run through the packets. This is because a packet can be
2350 selected in random order once the whole file has been read.
2352 After calculating the conversation timestamps, it is time to put them in
2353 the appropriate columns with the function 'col_set_time' (described in
2354 section 1.5.9). There are two columns for conversation timestamps:
2356 COL_REL_CONV_TIME, /* Relative time to beginning of conversation */
2357 COL_DELTA_CONV_TIME,/* Delta time to last frame in conversation */
2359 Last but not least, there MUST be a preference in each dissector that
2360 uses conversation timestamps that makes it possible to enable and
2361 disable the calculation of conversation timestamps. The main argument
2362 for this is that a higher level conversation is able to overwrite
2363 the values of lower level conversations in these two columns. Being
2364 able to actively select which protocols may overwrite the conversation
2365 timestamp columns gives the user the power to control these columns.
2366 (A second reason is that conversation timestamps use the per-packet
2367 data structure which uses additional memory, which should be avoided
2368 if these timestamps are not needed)
2370 Have a look at the differences to packet-tcp.[ch] in SVN 22966 and
2371 SVN 23058 to see the implementation of conversation timestamps for
2375 2.2.3 The example conversation code using wmem_file_scope memory.
2377 For a conversation between two IP addresses and ports you can use this as an
2378 example. This example uses wmem_alloc() with wmem_file_scope() to allocate
2379 memory and stores the data pointer in the conversation 'data' variable.
2381 /************************ Global values ************************/
2383 /* define your structure here */
2388 /* Registered protocol number */
2389 static int my_proto = -1;
2391 /********************* in the dissector routine *********************/
2393 /* the local variables in the dissector */
2395 conversation_t *conversation;
2396 my_entry_t *data_ptr;
2399 /* look up the conversation */
2401 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2402 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2404 /* if conversation found get the data pointer that you stored */
2406 data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2409 /* new conversation create local data structure */
2411 data_ptr = wmem_alloc(wmem_file_scope(), sizeof(my_entry_t));
2413 /*** add your code here to setup the new data structure ***/
2415 /* create the conversation with your data pointer */
2417 conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
2418 pinfo->srcport, pinfo->destport, 0);
2419 conversation_add_proto_data(conversation, my_proto, (void *)data_ptr);
2422 /* at this point the conversation data is ready */
2424 /***************** in the protocol register routine *****************/
2426 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2429 2.2.4 An example conversation code that starts at a specific frame number.
2431 Sometimes a dissector has determined that a new conversation is needed that
2432 starts at a specific frame number, when a capture session encompasses multiple
2433 conversation that reuse the same src/dest ip/port pairs. You can use the
2434 conversation->setup_frame returned by find_conversation with
2435 pinfo->fd->num to determine whether or not there already exists a conversation
2436 that starts at the specific frame number.
2438 /* in the dissector routine */
2440 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2441 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2442 if (conversation == NULL || (conversation->setup_frame != pinfo->fd->num)) {
2443 /* It's not part of any conversation or the returned
2444 * conversation->setup_frame doesn't match the current frame
2447 conversation = conversation_new(pinfo->fd->num, &pinfo->src,
2448 &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
2453 2.2.5 The example conversation code using conversation index field.
2455 Sometimes the conversation isn't enough to define a unique data storage
2456 value for the network traffic. For example if you are storing information
2457 about requests carried in a conversation, the request may have an
2458 identifier that is used to define the request. In this case the
2459 conversation and the identifier are required to find the data storage
2460 pointer. You can use the conversation data structure index value to
2461 uniquely define the conversation.
2463 See packet-afs.c for an example of how to use the conversation index. In
2464 this dissector multiple requests are sent in the same conversation. To store
2465 information for each request the dissector has an internal hash table based
2466 upon the conversation index and values inside the request packets.
2469 /* in the dissector routine */
2471 /* to find a request value, first lookup conversation to get index */
2472 /* then used the conversation index, and request data to find data */
2473 /* in the local hash table */
2475 conversation = find_or_create_conversation(pinfo);
2477 request_key.conversation = conversation->index;
2478 request_key.service = pntohs(&rxh->serviceId);
2479 request_key.callnumber = pntohl(&rxh->callNumber);
2481 request_val = (struct afs_request_val *)g_hash_table_lookup(
2482 afs_request_hash, &request_key);
2484 /* only allocate a new hash element when it's a request */
2486 if (!request_val && !reply)
2488 new_request_key = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_key));
2489 *new_request_key = request_key;
2491 request_val = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_val));
2492 request_val -> opcode = pntohl(&afsh->opcode);
2493 opcode = request_val->opcode;
2495 g_hash_table_insert(afs_request_hash, new_request_key,
2501 2.3 Dynamic conversation dissector registration.
2504 NOTE: This sections assumes that all information is available to
2505 create a complete conversation, source port/address and
2506 destination port/address. If either the destination port or
2507 address is know, see section 2.4 Dynamic server port dissector
2510 For protocols that negotiate a secondary port connection, for example
2511 packet-msproxy.c, a conversation can install a dissector to handle
2512 the secondary protocol dissection. After the conversation is created
2513 for the negotiated ports use the conversation_set_dissector to define
2514 the dissection routine.
2515 Before we create these conversations or assign a dissector to them we should
2516 first check that the conversation does not already exist and if it exists
2517 whether it is registered to our protocol or not.
2518 We should do this because it is uncommon but it does happen that multiple
2519 different protocols can use the same socketpair during different stages of
2520 an application cycle. By keeping track of the frame number a conversation
2521 was started in wireshark can still tell these different protocols apart.
2523 The second argument to conversation_set_dissector is a dissector handle,
2524 which is created with a call to create_dissector_handle or
2527 create_dissector_handle takes as arguments a pointer to the dissector
2528 function and a protocol ID as returned by proto_register_protocol;
2529 register_dissector takes as arguments a string giving a name for the
2530 dissector, a pointer to the dissector function, and a protocol ID.
2532 The protocol ID is the ID for the protocol dissected by the function.
2533 The function will not be called if the protocol has been disabled by the
2534 user; instead, the data for the protocol will be dissected as raw data.
2538 /* the handle for the dynamic dissector *
2539 static dissector_handle_t sub_dissector_handle;
2541 /* prototype for the dynamic dissector */
2542 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo,
2545 /* in the main protocol dissector, where the next dissector is setup */
2547 /* if conversation has a data field, create it and load structure */
2549 /* First check if a conversation already exists for this
2552 conversation = find_conversation(pinfo->fd->num,
2553 &pinfo->src, &pinfo->dst, protocol,
2554 src_port, dst_port, 0);
2556 /* If there is no such conversation, or if there is one but for
2557 someone else's protocol then we just create a new conversation
2558 and assign our protocol to it.
2560 if ( (conversation == NULL) ||
2561 (conversation->dissector_handle != sub_dissector_handle) ) {
2562 new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2563 new_conv_info->data1 = value1;
2565 /* create the conversation for the dynamic port */
2566 conversation = conversation_new(pinfo->fd->num,
2567 &pinfo->src, &pinfo->dst, protocol,
2568 src_port, dst_port, new_conv_info, 0);
2570 /* set the dissector for the new conversation */
2571 conversation_set_dissector(conversation, sub_dissector_handle);
2576 proto_register_PROTOABBREV(void)
2580 sub_dissector_handle = create_dissector_handle(sub_dissector,
2586 2.4 Dynamic server port dissector registration.
2588 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
2589 conversation with only one port and address set, this isn't a
2590 requirement. Either the second port or the second address can be set
2591 when the conversation is created.
2593 For protocols that define a server address and port for a secondary
2594 protocol, a conversation can be used to link a protocol dissector to
2595 the server port and address. The key is to create the new
2596 conversation with the second address and port set to the "accept
2599 Some server applications can use the same port for different protocols during
2600 different stages of a transaction. For example it might initially use SNMP
2601 to perform some discovery and later switch to use TFTP using the same port.
2602 In order to handle this properly we must first check whether such a
2603 conversation already exists or not and if it exists we also check whether the
2604 registered dissector_handle for that conversation is "our" dissector or not.
2605 If not we create a new conversation on top of the previous one and set this new
2606 conversation to use our protocol.
2607 Since wireshark keeps track of the frame number where a conversation started
2608 wireshark will still be able to keep the packets apart even though they do use
2609 the same socketpair.
2610 (See packet-tftp.c and packet-snmp.c for examples of this)
2612 There are two support routines that will allow the second port and/or
2613 address to be set later.
2615 conversation_set_port2( conversation_t *conv, guint32 port);
2616 conversation_set_addr2( conversation_t *conv, address addr);
2618 These routines will change the second address or port for the
2619 conversation. So, the server port conversation will be converted into a
2620 more complete conversation definition. Don't use these routines if you
2621 want to create a conversation between the server and client and retain the
2622 server port definition, you must create a new conversation.
2627 /* the handle for the dynamic dissector *
2628 static dissector_handle_t sub_dissector_handle;
2632 /* in the main protocol dissector, where the next dissector is setup */
2634 /* if conversation has a data field, create it and load structure */
2636 new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2637 new_conv_info->data1 = value1;
2639 /* create the conversation for the dynamic server address and port */
2640 /* NOTE: The second address and port values don't matter because the */
2641 /* NO_ADDR2 and NO_PORT2 options are set. */
2643 /* First check if a conversation already exists for this
2646 conversation = find_conversation(pinfo->fd->num,
2647 &server_src_addr, 0, protocol,
2648 server_src_port, 0, NO_ADDR2 | NO_PORT_B);
2649 /* If there is no such conversation, or if there is one but for
2650 someone else's protocol then we just create a new conversation
2651 and assign our protocol to it.
2653 if ( (conversation == NULL) ||
2654 (conversation->dissector_handle != sub_dissector_handle) ) {
2655 conversation = conversation_new(pinfo->fd->num,
2656 &server_src_addr, 0, protocol,
2657 server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
2659 /* set the dissector for the new conversation */
2660 conversation_set_dissector(conversation, sub_dissector_handle);
2663 2.5 Per-packet information.
2665 Information can be stored for each data packet that is processed by the
2666 dissector. The information is added with the p_add_proto_data function and
2667 retrieved with the p_get_proto_data function. The data pointers passed into
2668 the p_add_proto_data are not managed by the proto_data routines. If you use
2669 malloc or any other dynamic memory allocation scheme, you must release the
2670 data when it isn't required.
2673 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
2675 p_get_proto_data(frame_data *fd, int proto)
2678 fd - The fd pointer in the pinfo structure, pinfo->fd
2679 proto - Protocol id returned by the proto_register_protocol call
2680 during initialization
2681 proto_data - pointer to the dissector data.
2684 2.6 User Preferences.
2686 If the dissector has user options, there is support for adding these preferences
2687 to a configuration dialog.
2689 You must register the module with the preferences routine with -
2691 module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
2693 module_t *prefs_register_protocol_subtree(const char *subtree, int id,
2694 void (*apply_cb)(void));
2697 Where: proto_id - the value returned by "proto_register_protocol()" when
2698 the protocol was registered.
2699 apply_cb - Callback routine that is called when preferences are
2700 applied. It may be NULL, which inhibits the callback.
2701 subtree - grouping preferences tree node name (several protocols can
2702 be grouped under one preferences subtree)
2704 Then you can register the fields that can be configured by the user with these
2707 /* Register a preference with an unsigned integral value. */
2708 void prefs_register_uint_preference(module_t *module, const char *name,
2709 const char *title, const char *description, guint base, guint *var);
2711 /* Register a preference with an Boolean value. */
2712 void prefs_register_bool_preference(module_t *module, const char *name,
2713 const char *title, const char *description, gboolean *var);
2715 /* Register a preference with an enumerated value. */
2716 void prefs_register_enum_preference(module_t *module, const char *name,
2717 const char *title, const char *description, gint *var,
2718 const enum_val_t *enumvals, gboolean radio_buttons)
2720 /* Register a preference with a character-string value. */
2721 void prefs_register_string_preference(module_t *module, const char *name,
2722 const char *title, const char *description, char **var)
2724 /* Register a preference with a file name (string) value.
2725 * File name preferences are basically like string preferences
2726 * except that the GUI gives the user the ability to browse for the
2729 void prefs_register_filename_preference(module_t *module, const char *name,
2730 const char *title, const char *description, char **var)
2732 /* Register a preference with a range of unsigned integers (e.g.,
2735 void prefs_register_range_preference(module_t *module, const char *name,
2736 const char *title, const char *description, range_t *var,
2739 Where: module - Returned by the prefs_register_protocol routine
2740 name - This is appended to the name of the protocol, with a
2741 "." between them, to construct a name that identifies
2742 the field in the preference file; the name itself
2743 should not include the protocol name, as the name in
2744 the preference file will already have it. Make sure that
2745 only lower-case ASCII letters, numbers, underscores and
2746 dots appear in the preference name.
2747 title - Field title in the preferences dialog
2748 description - Comments added to the preference file above the
2749 preference value and shown as tooltip in the GUI, or NULL
2750 var - pointer to the storage location that is updated when the
2751 field is changed in the preference dialog box. Note that
2752 with string preferences the given pointer is overwritten
2753 with a pointer to a new copy of the string during the
2754 preference registration. The passed-in string may be
2755 freed, but you must keep another pointer to the string
2756 in order to free it.
2757 base - Base that the unsigned integer is expected to be in,
2759 enumvals - an array of enum_val_t structures. This must be
2760 NULL-terminated; the members of that structure are:
2762 a short name, to be used with the "-o" flag - it
2763 should not contain spaces or upper-case letters,
2764 so that it's easier to put in a command line;
2766 a description, which is used in the GUI (and
2767 which, for compatibility reasons, is currently
2768 what's written to the preferences file) - it can
2769 contain spaces, capital letters, punctuation,
2772 the numerical value corresponding to that name
2774 radio_buttons - TRUE if the field is to be displayed in the
2775 preferences dialog as a set of radio buttons,
2776 FALSE if it is to be displayed as an option
2778 max_value - The maximum allowed value for a range (0 is the minimum).
2780 An example from packet-beep.c -
2782 proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
2787 /* Register our configuration options for BEEP, particularly our port */
2789 beep_module = prefs_register_protocol(proto_beep, proto_reg_handoff_beep);
2791 prefs_register_uint_preference(beep_module, "tcp.port", "BEEP TCP Port",
2792 "Set the port for BEEP messages (if other"
2793 " than the default of 10288)",
2794 10, &global_beep_tcp_port);
2796 prefs_register_bool_preference(beep_module, "strict_header_terminator",
2797 "BEEP Header Requires CRLF",
2798 "Specifies that BEEP requires CRLF as a "
2799 "terminator, and not just CR or LF",
2800 &global_beep_strict_term);
2802 This will create preferences "beep.tcp.port" and
2803 "beep.strict_header_terminator", the first of which is an unsigned
2804 integer and the second of which is a Boolean.
2806 Note that a warning will pop up if you've saved such preference to the
2807 preference file and you subsequently take the code out. The way to make
2808 a preference obsolete is to register it as such:
2810 /* Register a preference that used to be supported but no longer is. */
2811 void prefs_register_obsolete_preference(module_t *module,
2814 2.7 Reassembly/desegmentation for protocols running atop TCP.
2816 There are two main ways of reassembling a Protocol Data Unit (PDU) which
2817 spans across multiple TCP segments. The first approach is simpler, but
2818 assumes you are running atop of TCP when this occurs (but your dissector
2819 might run atop of UDP, too, for example), and that your PDUs consist of a
2820 fixed amount of data that includes enough information to determine the PDU
2821 length, possibly followed by additional data. The second method is more
2822 generic but requires more code and is less efficient.
2824 2.7.1 Using tcp_dissect_pdus().
2826 For the first method, you register two different dissection methods, one
2827 for the TCP case, and one for the other cases. It is a good idea to
2828 also have a dissect_PROTO_common function which will parse the generic
2829 content that you can find in all PDUs which is called from
2830 dissect_PROTO_tcp when the reassembly is complete and from
2831 dissect_PROTO_udp (or dissect_PROTO_other).
2833 To register the distinct dissector functions, consider the following
2834 example, stolen from packet-dns.c:
2836 dissector_handle_t dns_udp_handle;
2837 dissector_handle_t dns_tcp_handle;
2838 dissector_handle_t mdns_udp_handle;
2840 dns_udp_handle = create_dissector_handle(dissect_dns_udp,
2842 dns_tcp_handle = create_dissector_handle(dissect_dns_tcp,
2844 mdns_udp_handle = create_dissector_handle(dissect_mdns_udp,
2847 dissector_add_uint("udp.port", UDP_PORT_DNS, dns_udp_handle);
2848 dissector_add_uint("tcp.port", TCP_PORT_DNS, dns_tcp_handle);
2849 dissector_add_uint("udp.port", UDP_PORT_MDNS, mdns_udp_handle);
2850 dissector_add_uint("tcp.port", TCP_PORT_MDNS, dns_tcp_handle);
2852 The dissect_dns_udp function does very little work and calls
2853 dissect_dns_common, while dissect_dns_tcp calls tcp_dissect_pdus with a
2854 reference to a callback which will be called with reassembled data:
2857 dissect_dns_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2859 tcp_dissect_pdus(tvb, pinfo, tree, dns_desegment, 2,
2860 get_dns_pdu_len, dissect_dns_tcp_pdu);
2863 (The dissect_dns_tcp_pdu function acts similarly to dissect_dns_udp.)
2864 The arguments to tcp_dissect_pdus are:
2866 the tvbuff pointer, packet_info pointer, and proto_tree pointer
2867 passed to the dissector;
2869 a gboolean flag indicating whether desegmentation is enabled for
2872 the number of bytes of PDU data required to determine the length
2875 a routine that takes as arguments a packet_info pointer, a tvbuff
2876 pointer and an offset value representing the offset into the tvbuff
2877 at which a PDU begins and should return - *without* throwing an
2878 exception (it is guaranteed that the number of bytes specified by the
2879 previous argument to tcp_dissect_pdus is available, but more data
2880 might not be available, so don't refer to any data past that) - the
2881 total length of the PDU, in bytes;
2883 a routine to dissect the pdu that's passed a tvbuff pointer,
2884 packet_info pointer, and proto_tree pointer, with the tvbuff
2885 containing a possibly-reassembled PDU. (The "reported_length"
2886 of the tvbuff will be the length of the PDU).
2888 2.7.2 Modifying the pinfo struct.
2890 The second reassembly mode is preferred when the dissector cannot determine
2891 how many bytes it will need to read in order to determine the size of a PDU.
2892 It may also be useful if your dissector needs to support reassembly from
2893 protocols other than TCP.
2895 Your dissect_PROTO will initially be passed a tvbuff containing the payload of
2896 the first packet. It should dissect as much data as it can, noting that it may
2897 contain more than one complete PDU. If the end of the provided tvbuff coincides
2898 with the end of a PDU then all is well and your dissector can just return as
2899 normal. (If it is a new-style dissector, it should return the number of bytes
2900 successfully processed.)
2902 If the dissector discovers that the end of the tvbuff does /not/ coincide with
2903 the end of a PDU, (ie, there is half of a PDU at the end of the tvbuff), it can
2904 indicate this to the parent dissector, by updating the pinfo struct. The
2905 desegment_offset field is the offset in the tvbuff at which the dissector will
2906 continue processing when next called. The desegment_len field should contain
2907 the estimated number of additional bytes required for completing the PDU. Next
2908 time your dissect_PROTO is called, it will be passed a tvbuff composed of the
2909 end of the data from the previous tvbuff together with desegment_len more bytes.
2911 If the dissector cannot tell how many more bytes it will need, it should set
2912 desegment_len=DESEGMENT_ONE_MORE_SEGMENT; it will then be called again as soon
2913 as any more data becomes available. Dissectors should set the desegment_len to a
2914 reasonable value when possible rather than always setting
2915 DESEGMENT_ONE_MORE_SEGMENT as it will generally be more efficient. Also, you
2916 *must not* set desegment_len=1 in this case, in the hope that you can change
2917 your mind later: once you return a positive value from desegment_len, your PDU
2918 boundary is set in stone.
2920 static hf_register_info hf[] = {
2922 {"C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0,
2928 * Dissect a buffer containing ASCII C strings.
2930 * @param tvb The buffer to dissect.
2931 * @param pinfo Packet Info.
2932 * @param tree The protocol tree.
2934 static void dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
2937 while(offset < tvb_reported_length(tvb)) {
2938 gint available = tvb_reported_length_remaining(tvb, offset);
2939 gint len = tvb_strnlen(tvb, offset, available);
2942 /* we ran out of data: ask for more */
2943 pinfo->desegment_offset = offset;
2944 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
2948 col_set_str(pinfo->cinfo, COL_INFO, "C String");
2950 len += 1; /* Add one for the '\0' */
2953 proto_tree_add_item(tree, hf_cstring, tvb, offset, len,
2956 offset += (guint)len;
2959 /* if we get here, then the end of the tvb coincided with the end of a
2960 string. Happy days. */
2963 This simple dissector will repeatedly return DESEGMENT_ONE_MORE_SEGMENT
2964 requesting more data until the tvbuff contains a complete C string. The C string
2965 will then be added to the protocol tree. Note that there may be more
2966 than one complete C string in the tvbuff, so the dissection is done in a
2971 The ptvcursor API allows a simpler approach to writing dissectors for
2972 simple protocols. The ptvcursor API works best for protocols whose fields
2973 are static and whose format does not depend on the value of other fields.
2974 However, even if only a portion of your protocol is statically defined,
2975 then that portion could make use of ptvcursors.
2977 The ptvcursor API lets you extract data from a tvbuff, and add it to a
2978 protocol tree in one step. It also keeps track of the position in the
2979 tvbuff so that you can extract data again without having to compute any
2980 offsets --- hence the "cursor" name of the API.
2982 The three steps for a simple protocol are:
2983 1. Create a new ptvcursor with ptvcursor_new()
2984 2. Add fields with multiple calls of ptvcursor_add()
2985 3. Delete the ptvcursor with ptvcursor_free()
2987 ptvcursor offers the possibility to add subtrees in the tree as well. It can be
2988 done in very simple steps :
2989 1. Create a new subtree with ptvcursor_push_subtree(). The old subtree is
2990 pushed in a stack and the new subtree will be used by ptvcursor.
2991 2. Add fields with multiple calls of ptvcursor_add(). The fields will be
2992 added in the new subtree created at the previous step.
2993 3. Pop the previous subtree with ptvcursor_pop_subtree(). The previous
2994 subtree is again used by ptvcursor.
2995 Note that at the end of the parsing of a packet you must have popped each
2996 subtree you pushed. If it's not the case, the dissector will generate an error.
2998 To use the ptvcursor API, include the "ptvcursor.h" file. The PGM dissector
2999 is an example of how to use it. You don't need to look at it as a guide;
3000 instead, the API description here should be good enough.
3002 2.8.1 ptvcursor API.
3005 ptvcursor_new(proto_tree* tree, tvbuff_t* tvb, gint offset)
3006 This creates a new ptvcursor_t object for iterating over a tvbuff.
3007 You must call this and use this ptvcursor_t object so you can use the
3011 ptvcursor_add(ptvcursor_t* ptvc, int hf, gint length, const guint encoding)
3012 This will extract 'length' bytes from the tvbuff and place it in
3013 the proto_tree as field 'hf', which is a registered header_field. The
3014 pointer to the proto_item that is created is passed back to you. Internally,
3015 the ptvcursor advances its cursor so the next call to ptvcursor_add
3016 starts where this call finished. The 'encoding' parameter is relevant for
3017 certain type of fields (See above under proto_tree_add_item()).
3020 ptvcursor_add_no_advance(ptvcursor_t* ptvc, int hf, gint length, const guint encoding)
3021 Like ptvcursor_add, but does not advance the internal cursor.
3024 ptvcursor_advance(ptvcursor_t* ptvc, gint length)
3025 Advances the internal cursor without adding anything to the proto_tree.
3028 ptvcursor_free(ptvcursor_t* ptvc)
3029 Frees the memory associated with the ptvcursor. You must call this
3030 after your dissection with the ptvcursor API is completed.
3034 ptvcursor_push_subtree(ptvcursor_t* ptvc, proto_item* it, gint ett_subtree)
3035 Pushes the current subtree in the tree stack of the cursor, creates a new
3036 one and sets this one as the working tree.
3039 ptvcursor_pop_subtree(ptvcursor_t* ptvc);
3040 Pops a subtree in the tree stack of the cursor
3043 ptvcursor_add_with_subtree(ptvcursor_t* ptvc, int hfindex, gint length,
3044 const guint encoding, gint ett_subtree);
3045 Adds an item to the tree and creates a subtree.
3046 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3047 In this case, at the next pop, the item length will be equal to the advancement
3048 of the cursor since the creation of the subtree.
3051 ptvcursor_add_text_with_subtree(ptvcursor_t* ptvc, gint length,
3052 gint ett_subtree, const char* format, ...);
3053 Add a text node to the tree and create a subtree.
3054 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3055 In this case, at the next pop, the item length will be equal to the advancement
3056 of the cursor since the creation of the subtree.
3058 2.8.2 Miscellaneous functions.
3061 ptvcursor_tvbuff(ptvcursor_t* ptvc)
3062 Returns the tvbuff associated with the ptvcursor.
3065 ptvcursor_current_offset(ptvcursor_t* ptvc)
3066 Returns the current offset.
3069 ptvcursor_tree(ptvcursor_t* ptvc)
3070 Returns the proto_tree associated with the ptvcursor.
3073 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree)
3074 Sets a new proto_tree for the ptvcursor.
3077 ptvcursor_set_subtree(ptvcursor_t* ptvc, proto_item* it, gint ett_subtree);
3078 Creates a subtree and adds it to the cursor as the working tree but does
3079 not save the old working tree.
3083 A protocol dissector may be called in 2 different ways - with, or
3084 without a non-null "tree" argument.
3086 If the proto_tree argument is null, Wireshark does not need to use
3087 the protocol tree information from your dissector, and therefore is
3088 passing the dissector a null "tree" argument so that it doesn't
3089 need to do work necessary to build the protocol tree.
3091 In the interest of speed, if "tree" is NULL, avoid building a
3092 protocol tree and adding stuff to it, or even looking at any packet
3093 data needed only if you're building the protocol tree, if possible.
3095 Note, however, that you must fill in column information, create
3096 conversations, reassemble packets, do calls to "expert" functions,
3097 build any other persistent state needed for dissection, and call
3098 subdissectors regardless of whether "tree" is NULL or not.
3100 This might be inconvenient to do without doing most of the
3101 dissection work; the routines for adding items to the protocol tree
3102 can be passed a null protocol tree pointer, in which case they'll
3103 return a null item pointer, and "proto_item_add_subtree()" returns
3104 a null tree pointer if passed a null item pointer, so, if you're
3105 careful not to dereference any null tree or item pointers, you can
3106 accomplish this by doing all the dissection work. This might not
3107 be as efficient as skipping that work if you're not building a
3108 protocol tree, but if the code would have a lot of tests whether
3109 "tree" is null if you skipped that work, you might still be better
3110 off just doing all that work regardless of whether "tree" is null
3113 Note also that there is no guarantee, the first time the dissector is
3114 called, whether "tree" will be null or not; your dissector must work
3115 correctly, building or updating whatever state information is
3116 necessary, in either case.
3119 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3124 * indent-tabs-mode: nil
3127 * vi: set shiftwidth=4 tabstop=8 expandtab:
3128 * :indentSize=4:tabSize=8:noTabs=true: