f074efaa0d6bbb87106df82b918e2275baad195f
[metze/wireshark/wip.git] / doc / README.dissector
1 $Revision$
2 $Date$
3 $Author$
4 Tabsize: 4
5
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.
9
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.
13
14 If you haven't read README.developer, read that first!
15
16 0. Prerequisites.
17
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
20 build toolkit".
21
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
25 root dir.
26
27 0.1. Dissector related README files.
28
29 You'll find additional dissector related information in the following README
30 files:
31
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
37
38 0.2 Contributors
39
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>
48
49 1. Setting up your protocol dissector code.
50
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.
54
55 1.1 Skeleton code.
56
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.
63
64 The skeleton code lives in the file "packet-PROTOABBREV.c" in the same source
65 directory as this README.
66
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.
70
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.
73
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.
77
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.
84
85 You may not need to include all the headers listed in the skeleton, and you may
86 need to include additional headers.
87
88 The "$Id$" tag in the header comment will be updated by Subversion when the file
89 is checked in.
90
91 1.2 Explanation of needed substitutions in code skeleton.
92
93 In the skeleton sample code the following strings should be substituted with
94 your information.
95
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
105                 a filter expression.
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,
116                 FT_REL_OID
117 FIELDDISPLAY    --For FT_UINT{8,16,24,32,64} and FT_INT{8,16,24,32,64):
118
119                   BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC,
120                   or BASE_CUSTOM, possibly ORed with BASE_RANGE_STRING or
121                   BASE_EXT_STRING
122
123                 --For FT_ABSOLUTE_TIME:
124
125                   ABSOLUTE_TIME_LOCAL, ABSOLUTE_TIME_UTC, or
126                   ABSOLUTE_TIME_DOY_UTC
127
128                 --For FT_BOOLEAN:
129
130                   if BITMASK is non-zero:
131                     Number of bits in the field containing the FT_BOOLEAN
132                     bitfield.
133                   otherwise:
134                     (must be) BASE_NONE
135
136                 --For all other types:
137
138                   BASE_NONE
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
146
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.
150
151 1.3 The dissector and the data it receives.
152
153
154 1.3.1 Header file.
155
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.
159
160 The dissector must be declared exactly as follows in the file
161 packet-PROTOABBREV.h:
162
163 int
164 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
165
166
167 1.3.2 Extracting data from packets.
168
169 NOTE: See the file /epan/tvbuff.h for more details.
170
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.
177
178 The accessors are:
179
180 Bit accessors for a maximum of 8-bits, 16-bits 32-bits and 64-bits:
181
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);
186
187 Single-byte accessor:
188
189 guint8  tvb_get_guint8(tvbuff_t *tvb, const gint offset);
190
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):
194
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);
202
203 Network-to-host-order accessors for single-precision and
204 double-precision IEEE floating-point numbers:
205
206 gfloat tvb_get_ntohieee_float(tvbuff_t *tvb, const gint offset);
207 gdouble tvb_get_ntohieee_double(tvbuff_t *tvb, const gint offset);
208
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):
212
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);
220
221 Little-Endian-to-host-order accessors for single-precision and
222 double-precision IEEE floating-point numbers:
223
224 gfloat tvb_get_letohieee_float(tvbuff_t *tvb, const gint offset);
225 gdouble tvb_get_letohieee_double(tvbuff_t *tvb, const gint offset);
226
227 Accessors for IPv4 and IPv6 addresses:
228
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);
231
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.
239
240 gchar *tvb_ip_to_str(tvbuff_t *tvb, const gint offset)
241 gchar *tvb_ip6_to_str(tvbuff_t *tvb, const gint offset)
242
243 Returns a null-terminated buffer containing a string with IPv4 or IPv6 Address
244 from the specified tvbuff, starting at the specified offset.
245
246 Accessors for GUID:
247
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);
251
252 String accessors:
253
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);
257
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).
262
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.
266
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.
271
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);
278
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
283 null.
284
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.
288
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.
293
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.
298
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.
301
302 Byte Array Accessors:
303
304 gchar *tvb_bytes_to_str(tvbuff_t *tvb, gint offset, gint len);
305
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.
312
313 gchar *tvb_bytes_to_str_punct(tvbuff_t *tvb, gint offset, gint len, gchar punct);
314
315 This function is similar to tvb_bytes_to_str(...) except that 'punct' is inserted
316 between the hex representation of each byte.
317
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);
319
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.
327
328 Copying memory:
329 guint8* tvb_memcpy(tvbuff_t *tvb, guint8* target, gint offset, gint length);
330
331 Copies into the specified target the specified length's worth of data
332 from the specified tvbuff, starting at the specified offset.
333
334 guint8* tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length);
335
336 Returns a buffer, allocated with "g_malloc()" if scope is NULL, or with the specified pool.
337
338 Pointer-retrieval:
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.
343  */
344 guint8* tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length);
345
346
347 1.4 Functions to handle columns in the traffic summary window.
348
349 The topmost pane of the main window is a list of the packets in the
350 capture, possibly filtered by a display filter.
351
352 Each line corresponds to a packet, and has one or more columns, as
353 configured by the user.
354
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
357 "Info" columns.
358
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.
364
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.
368
369 1.4.1 The col_set_str function.
370
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.
377
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
382 that case.
383
384 For example, to set the "Protocol" column
385 to "PROTOABBREV":
386
387     col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
388
389
390 1.4.2 The col_add_str function.
391
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.
397
398
399 1.4.3 The col_add_fstr function.
400
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:
407
408     col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
409                  reqtype, n);
410
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
414 efficiently.
415
416
417 1.4.4 The col_clear function.
418
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.
428
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.
432
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.
439
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
446 number.
447
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.
454
455
456 1.4.5 The col_append_str function.
457
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.)
467
468
469 1.4.6 The col_append_fstr function.
470
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
475 column.
476
477 1.4.7 The col_append_sep_str and col_append_sep_fstr functions.
478
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.
485
486 1.4.8 The col_set_fence and col_prepend_fence_fstr functions.
487
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.
502
503
504 1.4.9 The col_set_time function.
505
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.
511
512 For example:
513
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");
516
517
518 1.5 Constructing the protocol tree.
519
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.
522
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
527 to the tree.
528
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
534 proto_tree_draw().
535
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.
543
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.
552
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:
557
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");
561
562     the "register" routine must have a name of the form
563     "proto_register_XXX";
564
565     the "register" routine must take no argument, and return no
566     value;
567
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.:
572
573 void proto_register_XXX(void) {
574
575     ...
576
577 }
578
579 and
580
581 void
582 proto_register_XXX( void )
583 {
584
585     ...
586
587 }
588
589     and so on should work.
590
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
596 tree.
597
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.
603
604 A protocol is registered with the name of the protocol and its
605 abbreviation.
606
607 Here is how the frame "protocol" is registered.
608
609         int proto_frame;
610
611         proto_frame = proto_register_protocol (
612                 /* name */            "Frame",
613                 /* short name */      "Frame",
614                 /* abbrev */          "frame" );
615
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:
619
620 struct header_field_info {
621     const char      *name;
622     const char      *abbrev;
623     enum ftenum     type;
624     int             display;
625     const void      *strings;
626     guint32         bitmask;
627     const char      *blurb;
628     .....
629 };
630
631 name
632 ----
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
635 string.
636
637 abbrev
638 ------
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.
647
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.
650
651 type
652 ----
653 The type of value this field holds. The current field types are:
654
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
664                 FT_NONE field.
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
669                 "true".
670     FT_FRAMENUM A frame number; if this is used, the "Go
671                 To Corresponding Frame" menu item can
672                 work on that field.
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
688                 the decimal point.
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,
697                 not raw binary data.
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
708                 characters.
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
725                 address.
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
730
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.
736
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'.
745
746 display
747 -------
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.
752
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
755 are:
756
757     BASE_DEC,
758     BASE_HEX,
759     BASE_OCT,
760     BASE_DEC_HEX,
761     BASE_HEX_DEC,
762     BASE_CUSTOM
763
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).
767
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:
771
772     void func(gchar *, guint32);
773
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.
776
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).
781
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.
785
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}".
791
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.
796
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
803 integral fields.
804
805 strings
806 -------
807 -- value_string
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.
811
812 A 'value_string' structure is a way to map values to strings.
813
814     typedef struct _value_string {
815         guint32  value;
816         gchar   *strptr;
817     } value_string;
818
819 For fields of that type, you would declare an array of "value_string"s:
820
821     static const value_string valstringname[] = {
822         { INTVAL1, "Descriptive String 1" },
823         { INTVAL2, "Descriptive String 2" },
824         { 0,       NULL }
825     };
826
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)'.
830
831 If the field has a numeric rather than an enumerated type, the 'strings'
832 field would be set to NULL.
833
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.
839
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.
842
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.
845
846 Note: the value_string array values are *unsigned*; IOW: -1 is greater than 0.
847       So:
848       { -2, -1,  1,  2 }; wrong:   linear search will be used (note gap)
849       {  1,  2, -2, -1 }; correct: binary search will be used
850
851       As a special case:
852       { -2, -1,  0,  1,  2 }; OK: direct(indexed) access will be used (note no gap)
853
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.
857
858 Use this macro to initialize the extended value_string at compile time:
859
860 static value_string_ext valstringname_ext = VALUE_STRING_EXT_INIT(valstringname);
861
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>);
866
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).
870
871
872 -- Ranges
873 If the field has a numeric type that might logically fit in ranges of values
874 one can use a range_string struct.
875
876 Thus a 'range_string' structure is a way to map ranges to strings.
877
878         typedef struct _range_string {
879                 guint32        value_min;
880                 guint32        value_max;
881                 const gchar   *strptr;
882         } range_string;
883
884 For fields of that type, you would declare an array of "range_string"s:
885
886     static const range_string rvalstringname[] = {
887         { INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" },
888         { INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" },
889         { 0,           0,          NULL                   }
890     };
891
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).
897
898 -- Booleans
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.
903
904     typedef struct true_false_string {
905         char    *true_string;
906         char    *false_string;
907     } true_false_string;
908
909 For Boolean fields for which "False" and "True" aren't the desired
910 labels, you would declare a "true_false_string"s:
911
912     static const true_false_string boolstringname = {
913         "String for True",
914         "String for False"
915     };
916
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)'.
921
922 If the Boolean field is to be displayed as "False" or "True", the
923 'strings' field would be set to NULL.
924
925 Wireshark predefines a whole range of ready made "true_false_string"s
926 in tfs.h, included via packet.h.
927
928 bitmask
929 -------
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
935 filtering.
936 If the field is not a bitfield, then bitmask should be set to 0.
937
938 blurb
939 -----
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.
948
949
950 1.5.1 Field Registration.
951
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:
956
957     static int proto_eg = -1;
958     static int hf_field_a = -1;
959     static int hf_field_b = -1;
960
961     static hf_register_info hf[] = {
962
963         { &hf_field_a,
964         { "Field A", "proto.field_a", FT_UINT8, BASE_HEX, NULL,
965             0xf0, "Field A represents Apples", HFILL }},
966
967         { &hf_field_b,
968         { "Field B", "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
969             0x0, "Field B represents Bananas", HFILL }}
970     };
971
972     proto_eg = proto_register_protocol("Example Protocol",
973         "PROTO", "proto");
974     proto_register_field_array(proto_eg, hf, array_length(hf));
975
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:
981
982 typedef struct hf_register_info {
983     int            *p_id; /* pointer to parent variable */
984     header_field_info hfinfo;
985 } hf_register_info;
986
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.
989
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,
993 entirely.
994
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:
997
998     static hf_register_info hf[] = {
999
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 }},
1003
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 }}
1007     };
1008
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
1020
1021 The HFILL macro at the end of the struct will set reasonable default values
1022 for internally used fields.
1023
1024 1.5.2 Adding Items and Values to the Protocol Tree.
1025
1026 A protocol item is added to an existing protocol tree with one of a
1027 handful of proto_XXX_DO_YYY() functions.
1028
1029 Subtrees can be made with the proto_item_add_subtree() function:
1030
1031     item = proto_tree_add_item(....);
1032     new_tree = proto_item_add_subtree(item, tree_type);
1033
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.
1037
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()":
1042
1043     static gint ett_eg = -1;
1044     static gint ett_field_a = -1;
1045
1046     static gint *ett[] = {
1047         &ett_eg,
1048         &ett_field_a
1049     };
1050
1051     proto_register_subtree_array(ett, array_length(ett));
1052
1053 in your "register" routine, just as you register the protocol and the
1054 fields for that protocol.
1055
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.
1061
1062 There are several functions that the programmer can use to add either
1063 protocol or field labels to the proto_tree:
1064
1065     proto_item*
1066     proto_tree_add_item(tree, id, tvb, start, length, encoding);
1067
1068     proto_item*
1069     proto_tree_add_text(tree, tvb, start, length, format, ...);
1070
1071     proto_item*
1072     proto_tree_add_text_valist(tree, tvb, start, length, format, ap);
1073
1074     proto_item*
1075     proto_tree_add_none_format(tree, id, tvb, start, length, format, ...);
1076
1077     proto_item*
1078     proto_tree_add_protocol_format(tree, id, tvb, start, length,
1079         format, ...);
1080
1081     proto_item *
1082     proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr);
1083
1084     proto_item *
1085     proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr,
1086         format, ...);
1087
1088     proto_item *
1089     proto_tree_add_bytes_format_value(tree, id, tvb, start, length,
1090         start_ptr, format, ...);
1091
1092     proto_item *
1093     proto_tree_add_time(tree, id, tvb, start, length, value_ptr);
1094
1095     proto_item *
1096     proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr,
1097         format, ...);
1098
1099     proto_item *
1100     proto_tree_add_time_format_value(tree, id, tvb, start, length,
1101         value_ptr, format, ...);
1102
1103     proto_item *
1104     proto_tree_add_ipxnet(tree, id, tvb, start, length, value);
1105
1106     proto_item *
1107     proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value,
1108         format, ...);
1109
1110     proto_item *
1111     proto_tree_add_ipxnet_format_value(tree, id, tvb, start, length,
1112         value, format, ...);
1113
1114     proto_item *
1115     proto_tree_add_ipv4(tree, id, tvb, start, length, value);
1116
1117     proto_item *
1118     proto_tree_add_ipv4_format(tree, id, tvb, start, length, value,
1119         format, ...);
1120
1121     proto_item *
1122     proto_tree_add_ipv4_format_value(tree, id, tvb, start, length,
1123         value, format, ...);
1124
1125     proto_item *
1126     proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr);
1127
1128     proto_item *
1129     proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr,
1130         format, ...);
1131
1132     proto_item *
1133     proto_tree_add_ipv6_format_value(tree, id, tvb, start, length,
1134         value_ptr, format, ...);
1135
1136     proto_item *
1137     proto_tree_add_ax25(tree, id, tvb, start, length, value);
1138
1139     proto_item *
1140     proto_tree_add_ether(tree, id, tvb, start, length, value_ptr);
1141
1142     proto_item *
1143     proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr,
1144         format, ...);
1145
1146     proto_item *
1147     proto_tree_add_ether_format_value(tree, id, tvb, start, length,
1148         value_ptr, format, ...);
1149
1150     proto_item *
1151     proto_tree_add_guid(tree, id, tvb, start, length, value_ptr);
1152
1153     proto_item *
1154     proto_tree_add_guid_format(tree, id, tvb, start, length, value_ptr,
1155         format, ...);
1156
1157     proto_item *
1158     proto_tree_add_guid_format_value(tree, id, tvb, start, length,
1159         value_ptr, format, ...);
1160
1161     proto_item *
1162     proto_tree_add_oid(tree, id, tvb, start, length, value_ptr);
1163
1164     proto_item *
1165     proto_tree_add_oid_format(tree, id, tvb, start, length, value_ptr,
1166         format, ...);
1167
1168     proto_item *
1169     proto_tree_add_oid_format_value(tree, id, tvb, start, length,
1170         value_ptr, format, ...);
1171
1172     proto_item *
1173     proto_tree_add_string(tree, id, tvb, start, length, value_ptr);
1174
1175     proto_item *
1176     proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr,
1177         format, ...);
1178
1179     proto_item *
1180     proto_tree_add_string_format_value(tree, id, tvb, start, length,
1181         value_ptr, format, ...);
1182
1183     proto_item *
1184     proto_tree_add_unicode_string(tree, id, tvb, start, length, value);
1185
1186     proto_item *
1187     proto_tree_add_boolean(tree, id, tvb, start, length, value);
1188
1189     proto_item *
1190     proto_tree_add_boolean_format(tree, id, tvb, start, length, value,
1191         format, ...);
1192
1193     proto_item *
1194     proto_tree_add_boolean_format_value(tree, id, tvb, start, length,
1195         value, format, ...);
1196
1197     proto_item *
1198     proto_tree_add_float(tree, id, tvb, start, length, value);
1199
1200     proto_item *
1201     proto_tree_add_float_format(tree, id, tvb, start, length, value,
1202         format, ...);
1203
1204     proto_item *
1205     proto_tree_add_float_format_value(tree, id, tvb, start, length,
1206         value, format, ...);
1207
1208     proto_item *
1209     proto_tree_add_double(tree, id, tvb, start, length, value);
1210
1211     proto_item *
1212     proto_tree_add_double_format(tree, id, tvb, start, length, value,
1213         format, ...);
1214
1215     proto_item *
1216     proto_tree_add_double_format_value(tree, id, tvb, start, length,
1217         value, format, ...);
1218
1219     proto_item *
1220     proto_tree_add_uint(tree, id, tvb, start, length, value);
1221
1222     proto_item *
1223     proto_tree_add_uint_format(tree, id, tvb, start, length, value,
1224         format, ...);
1225
1226     proto_item *
1227     proto_tree_add_uint_format_value(tree, id, tvb, start, length,
1228         value, format, ...);
1229
1230     proto_item *
1231     proto_tree_add_uint64(tree, id, tvb, start, length, value);
1232
1233     proto_item *
1234     proto_tree_add_uint64_format(tree, id, tvb, start, length, value,
1235         format, ...);
1236
1237     proto_item *
1238     proto_tree_add_uint64_format_value(tree, id, tvb, start, length,
1239         value, format, ...);
1240
1241     proto_item *
1242     proto_tree_add_int(tree, id, tvb, start, length, value);
1243
1244     proto_item *
1245     proto_tree_add_int_format(tree, id, tvb, start, length, value,
1246         format, ...);
1247
1248     proto_item *
1249     proto_tree_add_int_format_value(tree, id, tvb, start, length,
1250         value, format, ...);
1251
1252     proto_item *
1253     proto_tree_add_int64(tree, id, tvb, start, length, value);
1254
1255     proto_item *
1256     proto_tree_add_int64_format(tree, id, tvb, start, length, value,
1257         format, ...);
1258
1259     proto_item *
1260     proto_tree_add_int64_format_value(tree, id, tvb, start, length,
1261         value, format, ...);
1262
1263     proto_item *
1264     proto_tree_add_eui64(tree, id, tvb, start, length, value);
1265
1266     proto_item *
1267     proto_tree_add_eui64_format(tree, id, tvb, start, length, value,
1268         format, ...);
1269
1270     proto_item *
1271     proto_tree_add_eui64_format_value(tree, id, tvb, start, length,
1272         value, format, ...);
1273
1274     proto_item *
1275     proto_tree_add_bitmask(tree, tvb, start, header, ett, fields,
1276         encoding);
1277
1278     proto_item *
1279     proto_tree_add_bitmask_len(tree, tvb, start, len, header, ett, fields,
1280         encoding);
1281
1282     proto_item *
1283     proto_tree_add_bitmask_text(tree, tvb, offset, len, name, fallback,
1284         ett, fields, encoding, flags);
1285
1286     proto_item*
1287     proto_tree_add_bits_item(tree, id, tvb, bit_offset, no_of_bits,
1288         encoding);
1289
1290     proto_item *
1291     proto_tree_add_split_bits_item_ret_val(tree, hf_index, tvb, bit_offset,
1292         crumb_spec, return_value);
1293
1294     void
1295     proto_tree_add_split_bits_crumb(tree, hf_index, tvb, bit_offset,
1296         crumb_spec, crumb_index);
1297
1298     proto_item *
1299     proto_tree_add_bits_ret_val(tree, id, tvb, bit_offset, no_of_bits,
1300         return_value, encoding);
1301
1302     proto_item *
1303     proto_tree_add_uint_bits_format_value(tree, id, tvb, bit_offset,
1304         no_of_bits, value, format, ...);
1305
1306     proto_item *
1307     proto_tree_add_boolean_bits_format_value(tree, id, tvb, bit_offset,
1308         no_of_bits, value, format, ...);
1309
1310     proto_item *
1311     proto_tree_add_int_bits_format_value(tree, id, tvb, bit_offset,
1312         no_of_bits, value, format, ...);
1313
1314     proto_item *
1315     proto_tree_add_float_bits_format_value(tree, id, tvb, bit_offset,
1316         no_of_bits, value, format, ...);
1317
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.
1324
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()':
1328
1329     void
1330     proto_item_set_len(ti, length);
1331
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.
1334
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.
1342
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).
1346
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.
1351
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
1356 order.
1357
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:
1366
1367     ENC_ASCII - ASCII (currently treated as UTF-8; in the future,
1368         all bytes with the 8th bit set will be treated as
1369         errors)
1370     ENC_UTF_8 - UTF-8
1371     ENC_UCS_2 - UCS-2
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
1375         errors)
1376     ENC_EBCDIC - EBCDIC
1377
1378 Other encodings will be added in the future.
1379
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:
1383
1384     ENC_TIME_TIMESPEC - seconds (4 bytes) and nanoseconds (4 bytes)
1385         of time since January 1, 1970, midnight UTC.
1386
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.
1391
1392 For other types, there is no support for proto_tree_add_item().
1393
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:
1400
1401     name        = "Format Identifier"
1402     abbrev      = "sna.th.fid"
1403     type        = FT_UINT8
1404     display     = BASE_HEX
1405     strings     = sna_th_fid_vals
1406     bitmask     = 0xf0
1407
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.
1410
1411 The code to add the FID to the tree would be;
1412
1413     proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1,
1414         ENC_BIG_ENDIAN);
1415
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.)
1422
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" },
1429     { 0x4, "?" },
1430     { 0x5, "?" },
1431     { 0xf, "Adjacent Subarea Nodes" },
1432     { 0, NULL }
1433 };
1434
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
1439 in the past.
1440
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().
1450
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().
1456
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:
1476
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;
1479
1480     the value was fetched into a variable.
1481
1482 The 'value' argument has the value to be added to the tree.
1483
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
1488 any more.
1489
1490 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
1491 sequence of bytes.
1492
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.
1496
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.
1503
1504 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
1505 network address.
1506
1507 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
1508 address, in network byte order.
1509
1510 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
1511 128-bit IPv6 address.
1512
1513 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
1514 48-bit MAC address.
1515
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).
1519
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".
1523
1524 For proto_tree_add_float(), the 'value' argument is a 'float' in the
1525 host's floating-point format.
1526
1527 For proto_tree_add_double(), the 'value' argument is a 'double' in the
1528 host's floating-point format.
1529
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
1532 64-bit integers.)
1533
1534 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
1535 integer value, in host byte order.
1536
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
1539 64-bit integers.)
1540
1541 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
1542 integer value, in host byte order.
1543
1544 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
1545 e_guid_t structure.
1546
1547 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
1548 ASN.1 Object Identifier.
1549
1550 For proto_tree_add_eui64(), the 'value' argument is a 64-bit integer
1551 value
1552
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.
1578
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 ------------------------------------
1597
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.
1606
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).
1618
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.
1624
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()':
1629
1630     void
1631     proto_item_set_text(proto_item *ti, ...);
1632
1633     void
1634     proto_item_append_text(proto_item *ti, ...);
1635
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.
1641
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.
1644
1645 For example, early in the dissection, one might do:
1646
1647     ti = proto_tree_add_text(tree, tvb, offset, length, <label>);
1648
1649 and later do
1650
1651     proto_item_set_text(ti, "%s: %s", type, value);
1652
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.
1656
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.
1662
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
1668 tree.
1669
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.
1674 Output format:
1675
1676 ..10 1010 10.. .... "value" (formatted as FT_ indicates).
1677
1678 proto_tree_add_bits_ret_val()
1679 -----------------------------
1680 Works in the same way but also returns the value of the read bits.
1681
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.
1687
1688 proto_tree_add_split_bits_crumb()
1689 ---------------------------------
1690 Helper function for the above, to add text for each crumb as it is encountered.
1691
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.
1696
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.
1699
1700 'header' and 'ett' are the hf fields and ett field respectively to create an
1701 expansion that covers the bytes of the bitmask.
1702
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
1711 as well.
1712
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;
1717     ...
1718     static gint ett_scsi_inq_peripheral = -1;
1719     ...
1720     static const int *peripheral_fields[] = {
1721         &hf_scsi_inq_qualifier,
1722         &hf_scsi_inq_devtype,
1723         NULL
1724     };
1725     ...
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);
1729     offset+=1;
1730     ...
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}},
1740     ...
1741
1742 Which provides very pretty dissection of this one byte bitmask.
1743
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)
1747
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.
1755
1756 There are the following flags defined:
1757
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
1762
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).
1766
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.
1774
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.
1788
1789
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.
1794
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.
1800
1801   Header checksum: 0x3d42 [correct]
1802     [Good: True]
1803     [Bad: False]
1804
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.
1809
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
1816 user wants.
1817
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:
1825
1826     RIF: 001-A-013-9-C0F-B-555
1827
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.
1834
1835     char *rif;
1836     rif = create_rif_string(...);
1837
1838     proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
1839
1840     for(i = 0; i < num_rings; i++) {
1841         proto_item *pi;
1842
1843         pi = proto_tree_add_item(tree, hf_tr_rif_ring, ...,
1844             ENC_BIG_ENDIAN);
1845         PROTO_ITEM_SET_HIDDEN(pi);
1846     }
1847     for(i = 0; i < num_rings - 1; i++) {
1848         proto_item *pi;
1849
1850         pi = proto_tree_add_item(tree, hf_tr_rif_bridge, ...,
1851             ENC_BIG_ENDIAN);
1852         PROTO_ITEM_SET_HIDDEN(pi);
1853     }
1854
1855 The logical tree has these items:
1856
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
1865
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:
1869
1870     tr.rif_ring eq 0x013
1871
1872 PROTO_ITEM_SET_URL
1873 ------------------
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.
1878
1879 1.6 Utility routines.
1880
1881 1.6.1 val_to_str, val_to_str_const, try_val_to_str and try_val_to_str_idx
1882
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.
1887
1888 val_to_str() handles the most common case:
1889
1890     gchar*
1891     val_to_str(guint32 val, const value_string *vs, const char *fmt)
1892
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
1898
1899     col_add_fstr(COL_INFO, ", %s", val_to_str(val, table, "Unknown %d"));
1900
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.
1904
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:
1907
1908     gchar*
1909     try_val_to_str(guint32 val, const value_string *vs)
1910
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.
1915
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
1921 more information.
1922
1923 See epan/value_string.h for detailed information on the various value_string
1924 functions.
1925
1926 1.6.2 rval_to_str, try_rval_to_str and try_rval_to_str_idx
1927
1928 A dissector may need to convert a range of values to a string, using a
1929 'range_string' structure.
1930
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'.
1933
1934
1935 1.7 Calling Other Dissectors.
1936
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).
1941
1942 The syntax for creating a new TVBUFF_SUBSET is:
1943
1944 next_tvb = tvb_new_subset(tvb, offset, length, reported_length)
1945
1946 Where:
1947     tvb is the tvbuff that the dissector has been working on. It
1948     can be a tvbuff of any type.
1949
1950     next_tvb is the new TVBUFF_SUBSET.
1951
1952     offset is the byte offset of 'tvb' at which the new tvbuff
1953     should start.  The first byte is the 0th byte.
1954
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
1957     'tvb'.
1958
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.
1962
1963
1964 An example from packet-ipx.c -
1965
1966 void
1967 dissect_ipx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1968 {
1969         tvbuff_t        *next_tvb;
1970         int             reported_length, available_length;
1971
1972
1973         /* Make the next tvbuff */
1974
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
1977 */
1978         reported_length = ipx_length - IPX_HEADER_LEN;
1979
1980 /* Calculate the available data in the packet,
1981    set this to -1 to use all the data in the tv_buffer
1982 */
1983         available_length = tvb_length(tvb) - IPX_HEADER_LEN;
1984
1985 /* Create the tvbuffer for the next dissector */
1986         next_tvb = tvb_new_subset(tvb, IPX_HEADER_LEN,
1987                         MIN(available_length, reported_length),
1988                         reported_length);
1989
1990 /* call the next dissector */
1991     dissector_next( next_tvb, pinfo, tree);
1992
1993
1994 1.8 Editing Makefile.common and CMakeLists.txt to add your dissector.
1995
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.)
2002
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
2007 compile).
2008
2009 In addition to the above, you should add your dissector source file name
2010 to the DISSECTOR_SRC section of epan/CMakeLists.txt
2011
2012
2013 1.9 Using the SVN source code tree.
2014
2015   See <http://www.wireshark.org/develop.html>
2016
2017
2018 1.9a Using git with the SVN source code tree.
2019
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/"
2026
2027   After that, a typical workflow may look like this (from "man git-svn"):
2028
2029   # Clone a repo (like git clone):
2030           git svn clone http://svn.example.com/project/trunk
2031   # Enter the newly cloned directory:
2032           cd trunk
2033   # You should be on master branch, double-check with Â´git branch´
2034           git branch
2035   # Do some work and commit locally to git:
2036           git commit ...
2037   # Something is committed to SVN, rebase your local changes against the
2038   # latest changes in SVN:
2039           git svn rebase
2040   # Now commit your changes (that were committed previously using git) to SVN
2041   # as well as automatically updating your working HEAD:
2042           git svn dcommit
2043   # Append svn:ignore settings to the default git exclude file:
2044           git svn show-ignore >> .git/info/exclude
2045
2046
2047 1.10 Submitting code for your new dissector.
2048
2049   - VERIFY that your dissector code does not use prohibited or deprecated APIs
2050     as follows:
2051     perl <wireshark_root>/tools/checkAPIs.pl <source-filename(s)>
2052
2053   - VERIFY that your dissector code does not contain any header field related
2054     problems:
2055     perl <wireshark_root>/tools/checkhf.pl <source-filename(s)>
2056
2057   - VERIFY that your dissector code does not contain any display filter related
2058     problems:
2059     perl <wireshark_root>/tools/checkfiltername.pl <source-filename(s)>
2060
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)>
2065
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>.
2069
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/>.
2073
2074   - 'svn add' all the files of your new dissector.
2075
2076   - 'svn diff' the workspace and save the result to a file.
2077
2078   - Edit the diff file - remove any changes unrelated to your new dissector,
2079     e.g. changes in config.nmake
2080
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.
2085
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>
2090
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.
2094
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.
2098
2099 2. Advanced dissector topics.
2100
2101 2.1 Introduction.
2102
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.
2105
2106 2.2 Following "conversations".
2107
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.
2112
2113 2.2.1 Conversation Routines
2114
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.
2119
2120
2121 2.2.1.1 The conversation_init function.
2122
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
2130 in this variable.
2131
2132 See item 2.2.1.5 for more information about use of the 'data' pointer.
2133
2134
2135 2.2.1.2 The conversation_new function.
2136
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.
2146
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,
2150         guint options);
2151
2152 Where:
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
2160
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.
2164
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.
2170
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.
2180
2181 2.2.1.3 The find_conversation function.
2182
2183 Call this routine to look up a conversation.  If no conversation is found,
2184 the routine will return a NULL value.
2185
2186 The find_conversation prototype:
2187
2188     conversation_t *find_conversation(guint32 frame_num, address *addr_a,
2189         address *addr_b, port_type ptype, guint32 port_a, guint32 port_b,
2190         guint options);
2191
2192 Where:
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
2200
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.
2211
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.
2221
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.
2229
2230
2231 2.2.1.4 The find_or_create_conversation function.
2232
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().
2236
2237 The find_or_create_conversation prototype:
2238
2239     extern conversation_t *find_or_create_conversation(packet_info *pinfo);
2240
2241 Where:
2242     packet_info *pinfo = the packet_info structure
2243
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.
2247
2248
2249 2.2.1.5 The conversation_add_proto_data function.
2250
2251 Once you have created a conversation with conversation_new, you can
2252 associate data with it using this function.
2253
2254 The conversation_add_proto_data prototype:
2255
2256     void conversation_add_proto_data(conversation_t *conv, int proto,
2257         void *proto_data);
2258
2259 Where:
2260     conversation_t *conv    = the conversation in question
2261     int proto               = registered protocol number
2262     void *data              = dissector data structure
2263
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.
2273
2274
2275 2.2.1.6 The conversation_get_proto_data function.
2276
2277 After you have located a conversation with find_conversation, you can use
2278 this function to retrieve any data associated with it.
2279
2280 The conversation_get_proto_data prototype:
2281
2282     void *conversation_get_proto_data(conversation_t *conv, int proto);
2283
2284 Where:
2285     conversation_t *conv    = the conversation in question
2286     int proto               = registered protocol number
2287
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.
2292
2293
2294 2.2.1.7 The conversation_delete_proto_data function.
2295
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.
2300
2301 The conversation_delete_proto_data prototype:
2302
2303     void conversation_delete_proto_data(conversation_t *conv, int proto);
2304
2305 Where:
2306     conversation_t *conv = the conversation in question
2307     int proto            = registered protocol number
2308
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.
2312
2313 2.2.1.8 The conversation_set_dissector function
2314
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.
2318
2319 The conversation_set_dissector prototype:
2320
2321     void conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle);
2322
2323 Where:
2324     conversation_t *conv = the conversation in question
2325     const dissector_handle_t handle = the dissector handle.
2326
2327
2328 2.2.2 Using timestamps relative to the conversation
2329
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.
2338
2339 So add the following items to the struct that is used for the protocol data:
2340
2341   nstime_t ts_first;
2342   nstime_t ts_prev;
2343
2344 The ts_prev value should only be set during the first run through the
2345 packets (ie pinfo->fd->flags.visited is false).
2346
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.
2351
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:
2355
2356 COL_REL_CONV_TIME,  /* Relative time to beginning of conversation */
2357 COL_DELTA_CONV_TIME,/* Delta time to last frame in conversation */
2358
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)
2369
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
2372 the tcp-dissector.
2373
2374
2375 2.2.3 The example conversation code using wmem_file_scope memory.
2376
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.
2380
2381 /************************ Global values ************************/
2382
2383 /* define your structure here */
2384 typedef struct {
2385
2386 } my_entry_t;
2387
2388 /* Registered protocol number */
2389 static int my_proto = -1;
2390
2391 /********************* in the dissector routine *********************/
2392
2393 /* the local variables in the dissector */
2394
2395 conversation_t *conversation;
2396 my_entry_t *data_ptr;
2397
2398
2399 /* look up the conversation */
2400
2401 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2402         pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2403
2404 /* if conversation found get the data pointer that you stored */
2405 if (conversation)
2406     data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2407 else {
2408
2409     /* new conversation create local data structure */
2410
2411     data_ptr = wmem_alloc(wmem_file_scope(), sizeof(my_entry_t));
2412
2413     /*** add your code here to setup the new data structure ***/
2414
2415     /* create the conversation with your data pointer  */
2416
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);
2420 }
2421
2422 /* at this point the conversation data is ready */
2423
2424 /***************** in the protocol register routine *****************/
2425
2426 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2427
2428
2429 2.2.4 An example conversation code that starts at a specific frame number.
2430
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.
2437
2438 /* in the dissector routine */
2439
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
2445          * create a new one.
2446          */
2447         conversation = conversation_new(pinfo->fd->num, &pinfo->src,
2448             &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
2449             NULL, 0);
2450     }
2451
2452
2453 2.2.5 The example conversation code using conversation index field.
2454
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.
2462
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.
2467
2468
2469     /* in the dissector routine */
2470
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 */
2474
2475     conversation = find_or_create_conversation(pinfo);
2476
2477     request_key.conversation = conversation->index;
2478     request_key.service = pntohs(&rxh->serviceId);
2479     request_key.callnumber = pntohl(&rxh->callNumber);
2480
2481     request_val = (struct afs_request_val *)g_hash_table_lookup(
2482         afs_request_hash, &request_key);
2483
2484     /* only allocate a new hash element when it's a request */
2485     opcode = 0;
2486     if (!request_val && !reply)
2487     {
2488         new_request_key = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_key));
2489         *new_request_key = request_key;
2490
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;
2494
2495         g_hash_table_insert(afs_request_hash, new_request_key,
2496             request_val);
2497     }
2498
2499
2500
2501 2.3 Dynamic conversation dissector registration.
2502
2503
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
2508     registration.
2509
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.
2522
2523 The second argument to conversation_set_dissector is a dissector handle,
2524 which is created with a call to create_dissector_handle or
2525 register_dissector.
2526
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.
2531
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.
2535
2536 An example -
2537
2538 /* the handle for the dynamic dissector *
2539 static dissector_handle_t sub_dissector_handle;
2540
2541 /* prototype for the dynamic dissector */
2542 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo,
2543                 proto_tree *tree);
2544
2545 /* in the main protocol dissector, where the next dissector is setup */
2546
2547 /* if conversation has a data field, create it and load structure */
2548
2549 /* First check if a conversation already exists for this
2550     socketpair
2551 */
2552     conversation = find_conversation(pinfo->fd->num,
2553                 &pinfo->src, &pinfo->dst, protocol,
2554                 src_port, dst_port,  0);
2555
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.
2559 */
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;
2564
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);
2569
2570 /* set the dissector for the new conversation */
2571             conversation_set_dissector(conversation, sub_dissector_handle);
2572     }
2573     ...
2574
2575 void
2576 proto_register_PROTOABBREV(void)
2577 {
2578     ...
2579
2580     sub_dissector_handle = create_dissector_handle(sub_dissector,
2581         proto);
2582
2583     ...
2584 }
2585
2586 2.4 Dynamic server port dissector registration.
2587
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.
2592
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
2597 any" values.
2598
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)
2611
2612 There are two support routines that will allow the second port and/or
2613 address to be set later.
2614
2615 conversation_set_port2( conversation_t *conv, guint32 port);
2616 conversation_set_addr2( conversation_t *conv, address addr);
2617
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.
2623
2624
2625 An example -
2626
2627 /* the handle for the dynamic dissector *
2628 static dissector_handle_t sub_dissector_handle;
2629
2630     ...
2631
2632 /* in the main protocol dissector, where the next dissector is setup */
2633
2634 /* if conversation has a data field, create it and load structure */
2635
2636     new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2637     new_conv_info->data1 = value1;
2638
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.                               */
2642
2643 /* First check if a conversation already exists for this
2644     IP/protocol/port
2645 */
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.
2652 */
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);
2658
2659 /* set the dissector for the new conversation */
2660         conversation_set_dissector(conversation, sub_dissector_handle);
2661     }
2662
2663 2.5 Per-packet information.
2664
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.
2671
2672 void
2673 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
2674 void *
2675 p_get_proto_data(frame_data *fd, int proto)
2676
2677 Where:
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.
2682
2683
2684 2.6 User Preferences.
2685
2686 If the dissector has user options, there is support for adding these preferences
2687 to a configuration dialog.
2688
2689 You must register the module with the preferences routine with -
2690
2691        module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
2692        or
2693        module_t *prefs_register_protocol_subtree(const char *subtree, int id,
2694               void (*apply_cb)(void));
2695
2696
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)
2703
2704 Then you can register the fields that can be configured by the user with these
2705 routines -
2706
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);
2710
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);
2714
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)
2719
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)
2723
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
2727     * file.
2728     */
2729     void prefs_register_filename_preference(module_t *module, const char *name,
2730         const char *title, const char *description, char **var)
2731
2732     /* Register a preference with a range of unsigned integers (e.g.,
2733      * "1-20,30-40").
2734      */
2735     void prefs_register_range_preference(module_t *module, const char *name,
2736         const char *title, const char *description, range_t *var,
2737         guint32 max_value)
2738
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,
2758             see strtoul(3).
2759      enumvals - an array of enum_val_t structures.  This must be
2760             NULL-terminated; the members of that structure are:
2761
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;
2765
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,
2770             etc.;
2771
2772             the numerical value corresponding to that name
2773             and description
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
2777              menu
2778      max_value - The maximum allowed value for a range (0 is the minimum).
2779
2780 An example from packet-beep.c -
2781
2782    proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
2783                        "BEEP", "beep");
2784
2785     ...
2786
2787     /* Register our configuration options for BEEP, particularly our port */
2788
2789     beep_module = prefs_register_protocol(proto_beep, proto_reg_handoff_beep);
2790
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);
2795
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);
2801
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.
2805
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:
2809
2810 /* Register a preference that used to be supported but no longer is. */
2811     void prefs_register_obsolete_preference(module_t *module,
2812         const char *name);
2813
2814 2.7 Reassembly/desegmentation for protocols running atop TCP.
2815
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.
2823
2824 2.7.1 Using tcp_dissect_pdus().
2825
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).
2832
2833 To register the distinct dissector functions, consider the following
2834 example, stolen from packet-dns.c:
2835
2836     dissector_handle_t dns_udp_handle;
2837     dissector_handle_t dns_tcp_handle;
2838     dissector_handle_t mdns_udp_handle;
2839
2840     dns_udp_handle = create_dissector_handle(dissect_dns_udp,
2841         proto_dns);
2842     dns_tcp_handle = create_dissector_handle(dissect_dns_tcp,
2843         proto_dns);
2844     mdns_udp_handle = create_dissector_handle(dissect_mdns_udp,
2845         proto_dns);
2846
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);
2851
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:
2855
2856     static void
2857     dissect_dns_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2858     {
2859         tcp_dissect_pdus(tvb, pinfo, tree, dns_desegment, 2,
2860             get_dns_pdu_len, dissect_dns_tcp_pdu);
2861     }
2862
2863 (The dissect_dns_tcp_pdu function acts similarly to dissect_dns_udp.)
2864 The arguments to tcp_dissect_pdus are:
2865
2866     the tvbuff pointer, packet_info pointer, and proto_tree pointer
2867     passed to the dissector;
2868
2869     a gboolean flag indicating whether desegmentation is enabled for
2870     your protocol;
2871
2872     the number of bytes of PDU data required to determine the length
2873     of the PDU;
2874
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;
2882
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).
2887
2888 2.7.2 Modifying the pinfo struct.
2889
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.
2894
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.)
2901
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.
2910
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.
2919
2920 static hf_register_info hf[] = {
2921     {&hf_cstring,
2922      {"C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0,
2923       NULL, HFILL}
2924      }
2925    };
2926
2927 /**
2928 *   Dissect a buffer containing ASCII C strings.
2929 *
2930 *   @param  tvb     The buffer to dissect.
2931 *   @param  pinfo   Packet Info.
2932 *   @param  tree    The protocol tree.
2933 **/
2934 static void dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
2935 {
2936     guint offset = 0;
2937     while(offset < tvb_reported_length(tvb)) {
2938         gint available = tvb_reported_length_remaining(tvb, offset);
2939         gint len = tvb_strnlen(tvb, offset, available);
2940
2941         if( -1 == len ) {
2942             /* we ran out of data: ask for more */
2943             pinfo->desegment_offset = offset;
2944             pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
2945             return;
2946         }
2947
2948         col_set_str(pinfo->cinfo, COL_INFO, "C String");
2949
2950         len += 1; /* Add one for the '\0' */
2951
2952         if (tree) {
2953             proto_tree_add_item(tree, hf_cstring, tvb, offset, len,
2954                 ENC_ASCII|ENC_NA);
2955         }
2956         offset += (guint)len;
2957     }
2958
2959     /* if we get here, then the end of the tvb coincided with the end of a
2960        string. Happy days. */
2961 }
2962
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
2967 loop.
2968
2969 2.8 ptvcursors.
2970
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.
2976
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.
2981
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()
2986
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.
2997
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.
3001
3002 2.8.1 ptvcursor API.
3003
3004 ptvcursor_t*
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
3008 ptvcursor API.
3009
3010 proto_item*
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()).
3018
3019 proto_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.
3022
3023 void
3024 ptvcursor_advance(ptvcursor_t* ptvc, gint length)
3025     Advances the internal cursor without adding anything to the proto_tree.
3026
3027 void
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.
3031
3032
3033 proto_tree*
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.
3037
3038 void
3039 ptvcursor_pop_subtree(ptvcursor_t* ptvc);
3040     Pops a subtree in the tree stack of the cursor
3041
3042 proto_tree*
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.
3049
3050 proto_tree*
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.
3057
3058 2.8.2 Miscellaneous functions.
3059
3060 tvbuff_t*
3061 ptvcursor_tvbuff(ptvcursor_t* ptvc)
3062     Returns the tvbuff associated with the ptvcursor.
3063
3064 gint
3065 ptvcursor_current_offset(ptvcursor_t* ptvc)
3066     Returns the current offset.
3067
3068 proto_tree*
3069 ptvcursor_tree(ptvcursor_t* ptvc)
3070     Returns the proto_tree associated with the ptvcursor.
3071
3072 void
3073 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree)
3074     Sets a new proto_tree for the ptvcursor.
3075
3076 proto_tree*
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.
3080
3081 2.9 Optimizations
3082
3083 A protocol dissector may be called in 2 different ways - with, or
3084 without a non-null "tree" argument.
3085
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.
3090
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.
3094
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.
3099
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
3111 or not.
3112
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.
3117
3118 /*
3119  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
3120  *
3121  * Local variables:
3122  * c-basic-offset: 4
3123  * tab-width: 8
3124  * indent-tabs-mode: nil
3125  * End:
3126  *
3127  * vi: set shiftwidth=4 tabstop=8 expandtab:
3128  * :indentSize=4:tabSize=8:noTabs=true:
3129  */