Add wmem_map_steal
[metze/wireshark/wip.git] / doc / README.dissector
1 Tabsize: 4
2
3 This file is a HOWTO for Wireshark developers interested in writing or working
4 on Wireshark protocol dissectors. It describes expected code patterns and the
5 use of some of the important functions and variables.
6
7 This file is compiled to give in depth information on Wireshark.
8 It is by no means all inclusive and complete. Please feel free to send
9 remarks and patches to the developer mailing list.
10
11 If you haven't read README.developer, read that first!
12
13 0. Prerequisites.
14
15 Before starting to develop a new dissector, a "running" Wireshark build
16 environment is required - there's no such thing as a standalone "dissector
17 build toolkit".
18
19 How to setup such an environment is platform dependent; detailed information
20 about these steps can be found in the "Developer's Guide" (available from:
21 https://www.wireshark.org) and in the INSTALL and README files of the sources
22 root dir.
23
24 0.1. Dissector related README files.
25
26 You'll find additional dissector related information in the following README
27 files:
28
29 - README.heuristic      - what are heuristic dissectors and how to write them
30 - README.plugins        - how to "pluginize" a dissector
31 - README.request_response_tracking - how to track req./resp. times and such
32 - README.wmem           - how to obtain "memory leak free" memory
33
34 0.2 Contributors
35
36 James Coe <jammer[AT]cin.net>
37 Gilbert Ramirez <gram[AT]alumni.rice.edu>
38 Jeff Foster <jfoste[AT]woodward.com>
39 Olivier Abad <oabad[AT]cybercable.fr>
40 Laurent Deniel <laurent.deniel[AT]free.fr>
41 Gerald Combs <gerald[AT]wireshark.org>
42 Guy Harris <guy[AT]alum.mit.edu>
43 Ulf Lamping <ulf.lamping[AT]web.de>
44 Barbu Paul - Gheorghe <barbu.paul.gheorghe[AT]gmail.com>
45
46 1. Setting up your protocol dissector code.
47
48 This section provides skeleton code for a protocol dissector. It also explains
49 the basic functions needed to enter values in the traffic summary columns,
50 add to the protocol tree, and work with registered header fields.
51
52 1.1 Skeleton code.
53
54 Wireshark requires certain things when setting up a protocol dissector.
55 We provide basic skeleton code for a dissector that you can copy to a new file
56 and fill in.  Your dissector should follow the naming convention of "packet-"
57 followed by the abbreviated name for the protocol. It is recommended that where
58 possible you keep to the IANA abbreviated name for the protocol, if there is
59 one, or a commonly-used abbreviation for the protocol, if any.
60
61 The skeleton code lives in the file "packet-PROTOABBREV.c" in the same source
62 directory as this README.
63
64 If instead of using the skeleton you base your dissector on an existing real
65 dissector, please put a little note in the copyright header indicating which
66 dissector you started with.
67
68 Usually, you will put your newly created dissector file into the directory
69 epan/dissectors/, just like all the other packet-*.c files already in there.
70
71 Also, please add your dissector file to the corresponding makefiles,
72 described in section "1.8 Editing Makefile.am and CMakeLists.txt
73 to add your dissector" below.
74
75 Dissectors that use the dissector registration API to register with a lower
76 level protocol (this is the vast majority) don't need to define a prototype in
77 their .h file. For other dissectors the main dissector routine should have a
78 prototype in a header file whose name is "packet-", followed by the abbreviated
79 name for the protocol, followed by ".h"; any dissector file that calls your
80 dissector should be changed to include that file.
81
82 You may not need to include all the headers listed in the skeleton, and you may
83 need to include additional headers.
84
85 1.2 Explanation of needed substitutions in code skeleton.
86
87 In the skeleton sample code the following strings should be substituted with
88 your information.
89
90 YOUR_NAME       Your name, of course.  You do want credit, don't you?
91                 It's the only payment you will receive....
92 YOUR_EMAIL_ADDRESS  Keep those cards and letters coming.
93 PROTONAME       The name of the protocol; this is displayed in the
94                 top-level protocol tree item for that protocol.
95 PROTOSHORTNAME  An abbreviated name for the protocol; this is displayed
96                 in the "Preferences" dialog box if your dissector has
97                 any preferences, in the dialog box of enabled protocols,
98                 and in the dialog box for filter fields when constructing
99                 a filter expression.
100 PROTOABBREV     A name for the protocol for use in filter expressions;
101                 it may contain only lower-case letters, digits, and hyphens,
102                 underscores, and periods.
103 FIELDNAME       The displayed name for the header field.
104 FIELDABBREV     The abbreviated name for the header field; it may contain
105                 only letters, digits, hyphens, underscores, and periods.
106 FIELDTYPE       FT_NONE, FT_BOOLEAN, FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24,
107                 FT_UINT32, FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64,
108                 FT_INT8, FT_INT16, FT_INT24, FT_INT32, FT_INT40, FT_INT48,
109                 FT_INT56, FT_INT64, FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME,
110                 FT_RELATIVE_TIME, FT_STRING, FT_STRINGZ, FT_EUI64,
111                 FT_UINT_STRING, FT_ETHER, FT_BYTES, FT_UINT_BYTES, FT_IPv4,
112                 FT_IPv6, FT_IPXNET, FT_FRAMENUM, FT_PROTOCOL, FT_GUID, FT_OID,
113                 FT_REL_OID, FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FC, FT_FCWWN
114 FIELDDISPLAY    --For FT_UINT{8,16,24,32,40,48,56,64} and
115                        FT_INT{8,16,24,32,40,48,56,64):
116
117                   BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC,
118                   BASE_CUSTOM, or BASE_NONE, possibly ORed with
119                   BASE_RANGE_STRING, BASE_EXT_STRING, BASE_VAL64_STRING,
120                   BASE_ALLOW_ZERO, BASE_UNIT_STRING or BASE_NO_DISPLAY_VALUE
121
122                   BASE_NONE may be used with a non-NULL FIELDCONVERT when the
123                   numeric value of the field itself is not of significance to
124                   the user (for example, the number is a generated field).
125                   When this is the case the numeric value is not shown to the
126                   user in the protocol decode nor is it used when preparing
127                   filters for the field in question.
128
129                   BASE_NO_DISPLAY_VALUE will just display the field name with
130                   no value.  It is intended for byte arrays (FT_BYTES) or
131                   header fields above a subtree.  The value will still be
132                   filterable, just not displayed.
133
134                 --For FT_UINT16:
135
136                   BASE_PT_UDP, BASE_PT_TCP, BASE_PT_DCCP or BASE_PT_SCTP
137
138                 --For FT_CHAR:
139                   BASE_HEX, BASE_OCT, BASE_CUSTOM, or BASE_NONE, possibly
140                   ORed with BASE_RANGE_STRING, BASE_EXT_STRING or
141                   BASE_VAL64_STRING.
142
143                   BASE_NONE can be used in the same way as with FT_UINT8.
144
145                 --For FT_ABSOLUTE_TIME:
146
147                   ABSOLUTE_TIME_LOCAL, ABSOLUTE_TIME_UTC, or
148                   ABSOLUTE_TIME_DOY_UTC
149
150                 --For FT_BOOLEAN:
151
152                   if BITMASK is non-zero:
153                     Number of bits in the field containing the FT_BOOLEAN
154                     bitfield.
155                   otherwise:
156                     (must be) BASE_NONE
157
158                 --For FT_STRING, FT_STRINGZ and FT_UINT_STRING:
159
160                   STR_ASCII or STR_UNICODE
161
162                 --For FT_BYTES:
163
164                   SEP_DOT, SEP_DASH, SEP_COLON, or SEP_SPACE to provide
165                   a separator between bytes.
166                   BASE_NONE has no separator between bytes
167                   BASE_ALLOW_ZERO displays <none> instead of <MISSING> for zero sized byte array
168
169                 --For FT_IPv4:
170
171                   BASE_NETMASK - Used for IPv4 address that should never
172                                  attempted to be resolved (like netmasks)
173                   otherwise:
174                     (must be) BASE_NONE
175
176                 --For all other types:
177
178                   BASE_NONE
179 FIELDCONVERT    VALS(x), VALS64(x), RVALS(x), TFS(x), CF_FUNC(x), NULL
180 BITMASK         Used to mask a field not 8-bit aligned or with a size other
181                 than a multiple of 8 bits
182 FIELDDESCR      A brief description of the field, or NULL. [Please do not use ""].
183
184 If, for example, PROTONAME is "Internet Bogosity Discovery Protocol",
185 PROTOSHORTNAME would be "IBDP", and PROTOABBREV would be "ibdp".  Try to
186 conform with IANA names.
187
188 1.3 The dissector and the data it receives.
189
190
191 1.3.1 Header file.
192
193 This is only needed if the dissector doesn't use self-registration to
194 register itself with the lower level dissector, or if the protocol dissector
195 wants/needs to expose code to other subdissectors.
196
197 The dissector must be declared exactly as follows in the file
198 packet-PROTOABBREV.h:
199
200 int
201 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
202
203
204 1.3.2 Extracting data from packets.
205
206 NOTE: See the file /epan/tvbuff.h for more details.
207
208 The "tvb" argument to a dissector points to a buffer containing the raw
209 data to be analyzed by the dissector; for example, for a protocol
210 running atop UDP, it contains the UDP payload (but not the UDP header,
211 or any protocol headers above it).  A tvbuffer is an opaque data
212 structure, the internal data structures are hidden and the data must be
213 accessed via the tvbuffer accessors.
214
215 The accessors are:
216
217 Bit accessors for a maximum of 8-bits, 16-bits 32-bits and 64-bits:
218
219 guint8 tvb_get_bits8(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits);
220 guint16 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
221 guint32 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
222 guint64 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
223
224 Single-byte accessor:
225
226 guint8  tvb_get_guint8(tvbuff_t *tvb, const gint offset);
227
228 Network-to-host-order accessors for 16-bit integers (guint16), 24-bit
229 integers, 32-bit integers (guint32), 40-bit integers, 48-bit integers,
230 56-bit integers and 64-bit integers (guint64):
231
232 guint16 tvb_get_ntohs(tvbuff_t *tvb, const gint offset);
233 guint32 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset);
234 guint32 tvb_get_ntohl(tvbuff_t *tvb, const gint offset);
235 guint64 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset);
236 guint64 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset);
237 guint64 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset);
238 guint64 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset);
239
240 Network-to-host-order accessors for single-precision and
241 double-precision IEEE floating-point numbers:
242
243 gfloat tvb_get_ntohieee_float(tvbuff_t *tvb, const gint offset);
244 gdouble tvb_get_ntohieee_double(tvbuff_t *tvb, const gint offset);
245
246 Little-Endian-to-host-order accessors for 16-bit integers (guint16),
247 24-bit integers, 32-bit integers (guint32), 40-bit integers, 48-bit
248 integers, 56-bit integers, and 64-bit integers (guint64):
249
250 guint16 tvb_get_letohs(tvbuff_t *tvb, const gint offset);
251 guint32 tvb_get_letoh24(tvbuff_t *tvb, const gint offset);
252 guint32 tvb_get_letohl(tvbuff_t *tvb, const gint offset);
253 guint64 tvb_get_letoh40(tvbuff_t *tvb, const gint offset);
254 guint64 tvb_get_letoh48(tvbuff_t *tvb, const gint offset);
255 guint64 tvb_get_letoh56(tvbuff_t *tvb, const gint offset);
256 guint64 tvb_get_letoh64(tvbuff_t *tvb, const gint offset);
257
258 Little-Endian-to-host-order accessors for single-precision and
259 double-precision IEEE floating-point numbers:
260
261 gfloat tvb_get_letohieee_float(tvbuff_t *tvb, const gint offset);
262 gdouble tvb_get_letohieee_double(tvbuff_t *tvb, const gint offset);
263
264 Encoding-to_host-order accessors for 16-bit integers (guint16),
265 24-bit integers, 32-bit integers (guint32), 40-bit integers, 48-bit
266 integers, 56-bit integers, and 64-bit integers (guint64):
267
268 guint16 tvb_get_guint16(tvbuff_t *tvb, const gint offset, const guint encoding);
269 guint32 tvb_get_guint24(tvbuff_t *tvb, const gint offset, const guint encoding);
270 guint32 tvb_get_guint32(tvbuff_t *tvb, const gint offset, const guint encoding);
271 guint64 tvb_get_guint40(tvbuff_t *tvb, const gint offset, const guint encoding);
272 gint64 tvb_get_gint40(tvbuff_t *tvb, const gint offset, const guint encoding);
273 guint64 tvb_get_guint48(tvbuff_t *tvb, const gint offset, const guint encoding);
274 gint64 tvb_get_gint48(tvbuff_t *tvb, const gint offset, const guint encoding);
275 guint64 tvb_get_guint56(tvbuff_t *tvb, const gint offset, const guint encoding);
276 gint64 tvb_get_gint56(tvbuff_t *tvb, const gint offset, const guint encoding);
277 guint64 tvb_get_guint64(tvbuff_t *tvb, const gint offset, const guint encoding);
278
279 "encoding" should be ENC_BIG_ENDIAN for Network-to-host-order and ENC_LITTLE_ENDIAN
280 for Little-Endian-to-host-order.
281
282 Endian-to-host-order accessors for single-precision and
283 double-precision IEEE floating-point numbers:
284
285 gfloat tvb_get_ieee_float(tvbuff_t *tvb, const gint offset, const guint encoding);
286 gdouble tvb_get_ieee_double(tvbuff_t *tvb, const gint offset, const guint encoding);
287
288 Just as above, "encoding" should be ENC_BIG_ENDIAN for Network-to-host-order and
289 ENC_LITTLE_ENDIAN for Little-Endian-to-host-order.
290
291 Accessors for IPv4 and IPv6 addresses:
292
293 guint32 tvb_get_ipv4(tvbuff_t *tvb, const gint offset);
294 void tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr);
295
296 NOTE: IPv4 addresses are not to be converted to host byte order before
297 being passed to "proto_tree_add_ipv4()".  You should use "tvb_get_ipv4()"
298 to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't,
299 for example, try to use "tvb_get_ntohl()", find that it gives you the
300 wrong answer on the PC on which you're doing development, and try
301 "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong
302 answer on big-endian machines.
303
304 gchar *tvb_ip_to_str(tvbuff_t *tvb, const gint offset)
305 gchar *tvb_ip6_to_str(tvbuff_t *tvb, const gint offset)
306
307 Returns a null-terminated buffer containing a string with IPv4 or IPv6 Address
308 from the specified tvbuff, starting at the specified offset.
309
310 Accessors for GUID:
311
312 void tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid);
313 void tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid);
314 void tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation);
315
316 String accessors:
317
318 guint8 *tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length, const guint encoding);
319
320 Returns a null-terminated buffer allocated from the specified scope, containing
321 data from the specified tvbuff, starting at the specified offset, and containing
322 the specified length worth of characters. Reads data in the specified encoding
323 and produces UTF-8 in the buffer. See below for a list of input encoding values.
324
325 The buffer is allocated in the given wmem scope (see README.wmem for more
326 information).
327
328 guint8 *tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding);
329
330 Returns a null-terminated buffer allocated from the specified scope,
331 containing data from the specified tvbuff, starting at the specified
332 offset, and containing all characters from the tvbuff up to and
333 including a terminating null character in the tvbuff.  Reads data in the
334 specified encoding and produces UTF-8 in the buffer.  See below for a
335 list of input encoding values.  "*lengthp" will be set to the length of
336 the string, including the terminating null.
337
338 The buffer is allocated in the given wmem scope (see README.wmem for more
339 information).
340
341 const guint8 *tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp);
342
343 Returns a null-terminated const buffer containing data from the
344 specified tvbuff, starting at the specified offset, and containing all
345 bytes from the tvbuff up to and including a terminating null character
346 in the tvbuff.  "*lengthp" will be set to the length of the string,
347 including the terminating null.
348
349 You do not need to free() this buffer; it will happen automatically once
350 the next packet is dissected.  This function is slightly more efficient
351 than the others because it does not allocate memory and copy the string,
352 but it does not do any mapping to UTF-8 or checks for valid octet
353 sequences.
354
355 gint tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer);
356 gint tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer);
357
358 Returns a null-terminated buffer containing data from the specified tvbuff,
359 starting at the specified offset, and containing all characters from the
360 tvbuff up to and including a terminating null character in the tvbuff.
361 "*lengthp" will be set to the length of the string, including the terminating
362 null.
363
364 gchar *tvb_get_ts_23_038_7bits_string(wmem_allocator_t *scope,
365     tvbuff_t *tvb, const gint bit_offset, gint no_of_chars);
366
367 tvb_get_ts_23_038_7bits_string() returns a string of a given number of
368 characters and encoded according to 3GPP TS 23.038 7 bits alphabet.
369
370 The buffer is allocated in the given wmem scope (see README.wmem for more
371 information).
372
373 Byte Array Accessors:
374
375 gchar *tvb_bytes_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint len);
376
377 Formats a bunch of data from a tvbuff as bytes, returning a pointer
378 to the string with the data formatted as two hex digits for each byte.
379 The string pointed to is stored in an "wmem_alloc'd" buffer which will be freed
380 depending on its scope (typically wmem_packet_scope which is freed after the frame).
381 The formatted string will contain the hex digits for at most the first 16 bytes of
382 the data. If len is greater than 16 bytes, a trailing "..." will be added to the string.
383
384 gchar *tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint len, gchar punct);
385
386 This function is similar to tvb_bytes_to_str(...) except that 'punct' is inserted
387 between the hex representation of each byte.
388
389 GByteArray *tvb_get_string_bytes(tvbuff_t *tvb, const gint offset, const gint length,
390                                  const guint encoding, GByteArray* bytes, gint *endoff)
391
392 Given a tvbuff, an offset into the tvbuff, and a length that starts
393 at that offset (which may be -1 for "all the way to the end of the
394 tvbuff"), fetch the hex-decoded byte values of the tvbuff into the
395 passed-in 'bytes' array, based on the passed-in encoding. In other
396 words, convert from a hex-ascii string in tvbuff, into the supplied
397 GByteArray.
398
399 gchar *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first);
400
401 Given a tvbuff, an offset into the tvbuff, and a length that starts
402 at that offset (which may be -1 for "all the way to the end of the
403 tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
404 the low or high half byte, formatting the digits according to an input digit set,
405 if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
406 A pointer to the packet scope allocated string will be returned.
407 Note: a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
408
409 Copying memory:
410 guint8* tvb_memcpy(tvbuff_t *tvb, guint8* target, gint offset, gint length);
411
412 Copies into the specified target the specified length's worth of data
413 from the specified tvbuff, starting at the specified offset.
414
415 guint8* tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length);
416
417 Returns a buffer containing a copy of the given TVB bytes. The buffer is
418 allocated in the given wmem scope (see README.wmem for more information).
419
420 Pointer-retrieval:
421 /* WARNING! Don't use this function.  There is almost always a better way.
422  * It's dangerous because once this pointer is given to the user, there's
423  * no guarantee that the user will honor the 'length' and not overstep the
424  * boundaries of the buffer.  Also see the warning in the Portability section.
425  */
426 guint8* tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length);
427
428 Length query:
429 Get amount of captured data in the buffer (which is *NOT* necessarily the
430 length of the packet). You probably want tvb_reported_length instead:
431
432     guint tvb_captured_length(const tvbuff_t *tvb);
433
434 Get reported length of buffer:
435
436     guint tvb_reported_length(const tvbuff_t *tvb);
437
438
439 1.4 Functions to handle columns in the traffic summary window.
440
441 The topmost pane of the main window is a list of the packets in the
442 capture, possibly filtered by a display filter.
443
444 Each line corresponds to a packet, and has one or more columns, as
445 configured by the user.
446
447 Many of the columns are handled by code outside individual dissectors;
448 most dissectors need only specify the value to put in the "Protocol" and
449 "Info" columns.
450
451 Columns are specified by COL_ values; the COL_ value for the "Protocol"
452 field, typically giving an abbreviated name for the protocol (but not
453 the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the
454 COL_ value for the "Info" field, giving a summary of the contents of the
455 packet for that protocol, is COL_INFO.
456
457 The value for a column can be specified with one of several functions,
458 all of which take the 'fd' argument to the dissector as their first
459 argument, and the COL_ value for the column as their second argument.
460
461 1.4.1 The col_set_str function.
462
463 'col_set_str' takes a string as its third argument, and sets the value
464 for the column to that value.  It assumes that the pointer passed to it
465 points to a string constant or a static "const" array, not to a
466 variable, as it doesn't copy the string, it merely saves the pointer
467 value; the argument can itself be a variable, as long as it always
468 points to a string constant or a static "const" array.
469
470 It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if
471 the dissector will be using 'col_append_str' or 'col_append_fstr" to
472 append more information to the column, the string will have to be copied
473 anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in
474 that case.
475
476 For example, to set the "Protocol" column
477 to "PROTOABBREV":
478
479     col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
480
481
482 1.4.2 The col_add_str function.
483
484 'col_add_str' takes a string as its third argument, and sets the value
485 for the column to that value.  It takes the same arguments as
486 'col_set_str', but copies the string, so that if the string is, for
487 example, an automatic variable that won't remain in scope when the
488 dissector returns, it's safe to use.
489
490
491 1.4.3 The col_add_fstr function.
492
493 'col_add_fstr' takes a 'printf'-style format string as its third
494 argument, and 'printf'-style arguments corresponding to '%' format
495 items in that string as its subsequent arguments.  For example, to set
496 the "Info" field to "<XXX> request, <N> bytes", where "reqtype" is a
497 string containing the type of the request in the packet and "n" is an
498 unsigned integer containing the number of bytes in the request:
499
500     col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
501                  reqtype, n);
502
503 Don't use 'col_add_fstr' with a format argument of just "%s" -
504 'col_add_str', or possibly even 'col_set_str' if the string that matches
505 the "%s" is a static constant string, will do the same job more
506 efficiently.
507
508
509 1.4.4 The col_clear function.
510
511 If the Info column will be filled with information from the packet, that
512 means that some data will be fetched from the packet before the Info
513 column is filled in.  If the packet is so small that the data in
514 question cannot be fetched, the routines to fetch the data will throw an
515 exception (see the comment at the beginning about tvbuffers improving
516 the handling of short packets - the tvbuffers keep track of how much
517 data is in the packet, and throw an exception on an attempt to fetch
518 data past the end of the packet, so that the dissector won't process
519 bogus data), causing the Info column not to be filled in.
520
521 This means that the Info column will have data for the previous
522 protocol, which would be confusing if, for example, the Protocol column
523 had data for this protocol.
524
525 Therefore, before a dissector fetches any data whatsoever from the
526 packet (unless it's a heuristic dissector fetching data to determine
527 whether the packet is one that it should dissect, in which case it
528 should check, before fetching the data, whether there's any data to
529 fetch; if there isn't, it should return FALSE), it should set the
530 Protocol column and the Info column.
531
532 If the Protocol column will ultimately be set to, for example, a value
533 containing a protocol version number, with the version number being a
534 field in the packet, the dissector should, before fetching the version
535 number field or any other field from the packet, set it to a value
536 without a version number, using 'col_set_str', and should later set it
537 to a value with the version number after it's fetched the version
538 number.
539
540 If the Info column will ultimately be set to a value containing
541 information from the packet, the dissector should, before fetching any
542 fields from the packet, clear the column using 'col_clear' (which is
543 more efficient than clearing it by calling 'col_set_str' or
544 'col_add_str' with a null string), and should later set it to the real
545 string after it's fetched the data to use when doing that.
546
547
548 1.4.5 The col_append_str function.
549
550 Sometimes the value of a column, especially the "Info" column, can't be
551 conveniently constructed at a single point in the dissection process;
552 for example, it might contain small bits of information from many of the
553 fields in the packet.  'col_append_str' takes, as arguments, the same
554 arguments as 'col_add_str', but the string is appended to the end of the
555 current value for the column, rather than replacing the value for that
556 column.  (Note that no blank separates the appended string from the
557 string to which it is appended; if you want a blank there, you must add
558 it yourself as part of the string being appended.)
559
560
561 1.4.6 The col_append_fstr function.
562
563 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to
564 'col_add_str' - it takes, as arguments, the same arguments as
565 'col_add_fstr', but the formatted string is appended to the end of the
566 current value for the column, rather than replacing the value for that
567 column.
568
569 1.4.7 The col_append_sep_str and col_append_sep_fstr functions.
570
571 In specific situations the developer knows that a column's value will be
572 created in a stepwise manner, where the appended values are listed. Both
573 'col_append_sep_str' and 'col_append_sep_fstr' functions will add an item
574 separator between two consecutive items, and will not add the separator at the
575 beginning of the column. The remainder of the work both functions do is
576 identical to what 'col_append_str' and 'col_append_fstr' do.
577
578 1.4.8 The col_set_fence and col_prepend_fence_fstr functions.
579
580 Sometimes a dissector may be called multiple times for different PDUs in the
581 same frame (for example in the case of SCTP chunk bundling: several upper
582 layer data packets may be contained in one SCTP packet).  If the upper layer
583 dissector calls 'col_set_str()' or 'col_clear()' on the Info column when it
584 begins dissecting each of those PDUs then when the frame is fully dissected
585 the Info column would contain only the string from the last PDU in the frame.
586 The 'col_set_fence' function erects a "fence" in the column that prevents
587 subsequent 'col_...' calls from clearing the data currently in that column.
588 For example, the SCTP dissector calls 'col_set_fence' on the Info column
589 after it has called any subdissectors for that chunk so that subdissectors
590 of any subsequent chunks may only append to the Info column.
591 'col_prepend_fence_fstr' prepends data before a fence (moving it if
592 necessary).  It will create a fence at the end of the prepended data if the
593 fence does not already exist.
594
595
596 1.4.9 The col_set_time function.
597
598 The 'col_set_time' function takes an nstime value as its third argument.
599 This nstime value is a relative value and will be added as such to the
600 column. The fourth argument is the filtername holding this value. This
601 way, rightclicking on the column makes it possible to build a filter
602 based on the time-value.
603
604 For example:
605
606     col_set_time(pinfo->cinfo, COL_REL_TIME, &ts, "s4607.ploc.time");
607
608
609 1.5 Constructing the protocol tree.
610
611 The middle pane of the main window, and the topmost pane of a packet
612 popup window, are constructed from the "protocol tree" for a packet.
613
614 The protocol tree, or proto_tree, is a GNode, the N-way tree structure
615 available within GLIB. Of course the protocol dissectors don't care
616 what a proto_tree really is; they just pass the proto_tree pointer as an
617 argument to the routines which allow them to add items and new branches
618 to the tree.
619
620 When a packet is selected in the packet-list pane, or a packet popup
621 window is created, a new logical protocol tree (proto_tree) is created.
622 The pointer to the proto_tree (in this case, 'protocol tree'), is passed
623 to the top-level protocol dissector, and then to all subsequent protocol
624 dissectors for that packet, and then the GUI tree is drawn via
625 proto_tree_draw().
626
627 The logical proto_tree needs to know detailed information about the protocols
628 and fields about which information will be collected from the dissection
629 routines. By strictly defining (or "typing") the data that can be attached to a
630 proto tree, searching and filtering becomes possible.  This means that for
631 every protocol and field (which I also call "header fields", since they are
632 fields in the protocol headers) which might be attached to a tree, some
633 information is needed.
634
635 Every dissector routine will need to register its protocols and fields
636 with the central protocol routines (in proto.c). At first I thought I
637 might keep all the protocol and field information about all the
638 dissectors in one file, but decentralization seemed like a better idea.
639 That one file would have gotten very large; one small change would have
640 required a re-compilation of the entire file. Also, by allowing
641 registration of protocols and fields at run-time, loadable modules of
642 protocol dissectors (perhaps even user-supplied) is feasible.
643
644 To do this, each protocol should have a register routine, which will be
645 called when Wireshark starts.  The code to call the register routines is
646 generated automatically; to arrange that a protocol's register routine
647 be called at startup:
648
649     the file containing a dissector's "register" routine must be
650     added to "DISSECTOR_SRC" in "epan/dissectors/Makefile.am"
651     (and in "epan/dissectors/CMakeLists.txt");
652
653     the "register" routine must have a name of the form
654     "proto_register_XXX";
655
656     the "register" routine must take no argument, and return no
657     value;
658
659     the "register" routine's name must appear in the source file
660     either at the beginning of the line, or preceded only by "void "
661     at the beginning of the line (that would typically be the
662     definition) - other white space shouldn't cause a problem, e.g.:
663
664 void proto_register_XXX(void) {
665
666     ...
667
668 }
669
670 and
671
672 void
673 proto_register_XXX( void )
674 {
675
676     ...
677
678 }
679
680     and so on should work.
681
682 For every protocol or field that a dissector wants to register, a variable of
683 type int needs to be used to keep track of the protocol. The IDs are
684 needed for establishing parent/child relationships between protocols and
685 fields, as well as associating data with a particular field so that it
686 can be stored in the logical tree and displayed in the GUI protocol
687 tree.
688
689 Some dissectors will need to create branches within their tree to help
690 organize header fields. These branches should be registered as header
691 fields. Only true protocols should be registered as protocols. This is
692 so that a display filter user interface knows how to distinguish
693 protocols from fields.
694
695 A protocol is registered with the name of the protocol and its
696 abbreviation.
697
698 Here is how the frame "protocol" is registered.
699
700         int proto_frame;
701
702         proto_frame = proto_register_protocol (
703                 /* name */            "Frame",
704                 /* short name */      "Frame",
705                 /* abbrev */          "frame" );
706
707 A header field is also registered with its name and abbreviation, but
708 information about its data type is needed. It helps to look at
709 the header_field_info struct to see what information is expected:
710
711 struct header_field_info {
712     const char      *name;
713     const char      *abbrev;
714     enum ftenum     type;
715     int             display;
716     const void      *strings;
717     guint64         bitmask;
718     const char      *blurb;
719     .....
720 };
721
722 name (FIELDNAME)
723 ----------------
724 A string representing the name of the field. This is the name
725 that will appear in the graphical protocol tree.  It must be a non-empty
726 string.
727
728 abbrev (FIELDABBREV)
729 --------------------
730 A string with an abbreviation of the field.  The abbreviation should start
731 with the abbreviation of the parent protocol followed by a period as a
732 separator.  For example, the "src" field in an IP packet would have "ip.src"
733 as an abbreviation. It is acceptable to have multiple levels of periods if,
734 for example, you have fields in your protocol that are then subdivided into
735 subfields. For example, TRMAC has multiple error fields, so the abbreviations
736 follow this pattern: "trmac.errors.iso", "trmac.errors.noniso", etc.
737
738 The abbreviation is the identifier used in a display filter.  As such it
739 cannot be an empty string.
740
741 type (FIELDTYPE)
742 ----------------
743 The type of value this field holds. The current field types are:
744
745     FT_NONE     No field type. Used for fields that
746                 aren't given a value, and that can only
747                 be tested for presence or absence; a
748                 field that represents a data structure,
749                 with a subtree below it containing
750                 fields for the members of the structure,
751                 or that represents an array with a
752                 subtree below it containing fields for
753                 the members of the array, might be an
754                 FT_NONE field.
755     FT_PROTOCOL Used for protocols which will be placing
756                 themselves as top-level items in the
757                 "Packet Details" pane of the UI.
758     FT_BOOLEAN  0 means "false", any other value means
759                 "true".
760     FT_FRAMENUM A frame number; if this is used, the "Go
761                 To Corresponding Frame" menu item can
762                 work on that field.
763     FT_CHAR     An 8-bit ASCII character.  It's treated similarly to an
764                 FT_UINT8, but is displayed as a C-style character
765                 constant.
766     FT_UINT8    An 8-bit unsigned integer.
767     FT_UINT16   A 16-bit unsigned integer.
768     FT_UINT24   A 24-bit unsigned integer.
769     FT_UINT32   A 32-bit unsigned integer.
770     FT_UINT40   A 40-bit unsigned integer.
771     FT_UINT48   A 48-bit unsigned integer.
772     FT_UINT56   A 56-bit unsigned integer.
773     FT_UINT64   A 64-bit unsigned integer.
774     FT_INT8     An 8-bit signed integer.
775     FT_INT16    A 16-bit signed integer.
776     FT_INT24    A 24-bit signed integer.
777     FT_INT32    A 32-bit signed integer.
778     FT_INT40    A 40-bit signed integer.
779     FT_INT48    A 48-bit signed integer.
780     FT_INT56    A 56-bit signed integer.
781     FT_INT64    A 64-bit signed integer.
782     FT_FLOAT    A single-precision floating point number.
783     FT_DOUBLE   A double-precision floating point number.
784     FT_ABSOLUTE_TIME    An absolute time from some fixed point in time,
785                 displayed as the date, followed by the time, as
786                 hours, minutes, and seconds with 9 digits after
787                 the decimal point.
788     FT_RELATIVE_TIME    Seconds (4 bytes) and nanoseconds (4 bytes)
789                 of time relative to an arbitrary time.
790                 displayed as seconds and 9 digits
791                 after the decimal point.
792     FT_STRING   A string of characters, not necessarily
793                 NULL-terminated, but possibly NULL-padded.
794                 This, and the other string-of-characters
795                 types, are to be used for text strings,
796                 not raw binary data.
797     FT_STRINGZ  A NULL-terminated string of characters.
798                 The string length is normally the length
799                 given in the proto_tree_add_item() call.
800                 However if the length given in the call
801                 is -1, then the length used is that
802                 returned by calling tvb_strsize().
803     FT_UINT_STRING  A counted string of characters, consisting
804                 of a count (represented as an integral value,
805                 of width given in the proto_tree_add_item()
806                 call) followed immediately by that number of
807                 characters.
808     FT_ETHER    A six octet string displayed in
809                 Ethernet-address format.
810     FT_BYTES    A string of bytes with arbitrary values;
811                 used for raw binary data.
812     FT_UINT_BYTES   A counted string of bytes, consisting
813                 of a count (represented as an integral value,
814                 of width given in the proto_tree_add_item()
815                 call) followed immediately by that number of
816                 arbitrary values; used for raw binary data.
817     FT_IPv4     A version 4 IP address (4 bytes) displayed
818                 in dotted-quad IP address format (4
819                 decimal numbers separated by dots).
820     FT_IPv6     A version 6 IP address (16 bytes) displayed
821                 in standard IPv6 address format.
822     FT_IPXNET   An IPX address displayed in hex as a 6-byte
823                 network number followed by a 6-byte station
824                 address.
825     FT_GUID     A Globally Unique Identifier
826     FT_OID      An ASN.1 Object Identifier
827     FT_REL_OID  An ASN.1 Relative Object Identifier
828     FT_EUI64    A EUI-64 Address
829     FT_AX25     A AX-25 Address
830     FT_VINES    A Vines Address
831     FT_SYSTEM_ID  An OSI System-ID
832     FT_FCWWN    A Fibre Channel WWN Address
833
834 Some of these field types are still not handled in the display filter
835 routines, but the most common ones are. The FT_UINT* variables all
836 represent unsigned integers, and the FT_INT* variables all represent
837 signed integers; the number on the end represent how many bits are used
838 to represent the number.
839
840 Some constraints are imposed on the header fields depending on the type
841 (e.g.  FT_BYTES) of the field.  Fields of type FT_ABSOLUTE_TIME must use
842 'ABSOLUTE_TIME_{LOCAL,UTC,DOY_UTC}, NULL, 0x0' as values for the
843 'display, 'strings', and 'bitmask' fields, and all other non-integral
844 types (i.e.. types that are _not_ FT_INT* and FT_UINT*) must use
845 'BASE_NONE, NULL, 0x0' as values for the 'display', 'strings', 'bitmask'
846 fields.  The reason is simply that the type itself implicitly defines the
847 nature of 'display', 'strings', 'bitmask'.
848
849 display (FIELDDISPLAY)
850 ----------------------
851 The display field has a couple of overloaded uses. This is unfortunate,
852 but since we're using C as an application programming language, this sometimes
853 makes for cleaner programs. Right now I still think that overloading
854 this variable was okay.
855
856 For integer fields (FT_UINT* and FT_INT*), this variable represents the
857 base in which you would like the value displayed.  The acceptable bases
858 are:
859
860     BASE_DEC,
861     BASE_HEX,
862     BASE_OCT,
863     BASE_DEC_HEX,
864     BASE_HEX_DEC,
865     BASE_CUSTOM
866
867 BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal,
868 respectively. BASE_DEC_HEX and BASE_HEX_DEC display value in two bases
869 (the 1st representation followed by the 2nd in parenthesis).
870
871 BASE_CUSTOM allows one to specify a callback function pointer that will
872 format the value.
873
874 For 32-bit and smaller values, custom_fmt_func_t can be used to declare
875 the callback function pointer. Specifically, this is defined as:
876
877     void func(gchar *, guint32);
878
879 For values larger than 32-bits, custom_fmt_func_64_t can be used to declare
880 the callback function pointer. Specifically, this is defined as:
881
882     void func(gchar *, guint64);
883
884 The first argument is a pointer to a buffer of the ITEM_LABEL_LENGTH size
885 and the second argument is the value to be formatted.
886
887 Both custom_fmt_func_t and custom_fmt_func_64_t are defined in epan/proto.h.
888
889 For FT_UINT16 'display' can be used to select a transport layer protocol using one
890 of BASE_PT_UDP, BASE_PT_TCP, BASE_PT_DCCP or BASE_PT_SCTP. If transport name
891 resolution is enabled the port field label is displayed in decimal and as a well-known
892 service name (if one is available).
893
894 For FT_BOOLEAN fields that are also bitfields (i.e., 'bitmask' is non-zero),
895 'display' is used specify a "field-width" (i.e., tell the proto_tree how
896 wide the parent bitfield is). (If the FT_BOOLEAN 'bitmask' is zero, then
897 'display' must be BASE_NONE).
898
899 For integer fields a "field-width" is not needed since the type of
900 integer itself (FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_UINT40,
901 FT_UINT48, FT_UINT56, FT_UINT64, etc) tells the proto_tree how wide the
902 parent bitfield is.  The same is true of FT_CHAR, as it's an 8-bit
903 character.
904
905 For FT_ABSOLUTE_TIME fields, 'display' is used to indicate whether the
906 time is to be displayed as a time in the time zone for the machine on
907 which Wireshark/TShark is running or as UTC and, for UTC, whether the
908 date should be displayed as "{monthname} {day_of_month}, {year}" or as
909 "{year/day_of_year}".
910
911 Additionally, BASE_NONE is used for 'display' as a NULL-value. That is, for
912 non-integers other than FT_ABSOLUTE_TIME fields, and non-bitfield
913 FT_BOOLEANs, you'll want to use BASE_NONE in the 'display' field.  You may
914 not use BASE_NONE for integers.
915
916 It is possible that in the future we will record the endianness of
917 integers. If so, it is likely that we'll use a bitmask on the display field
918 so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX.
919 But that has not happened yet; note that there are protocols for which
920 no endianness is specified, such as the X11 protocol and the DCE RPC
921 protocol, so it would not be possible to record the endianness of all
922 integral fields.
923
924 strings (FIELDCONVERT)
925 ----------------------
926 -- value_string
927 Some integer fields, of type FT_UINT*, need labels to represent the true
928 value of a field.  You could think of those fields as having an
929 enumerated data type, rather than an integral data type.
930
931 A 'value_string' structure is a way to map values to strings.
932
933     typedef struct _value_string {
934         guint32  value;
935         gchar   *strptr;
936     } value_string;
937
938 For fields of that type, you would declare an array of "value_string"s:
939
940     static const value_string valstringname[] = {
941         { INTVAL1, "Descriptive String 1" },
942         { INTVAL2, "Descriptive String 2" },
943         { 0,       NULL }
944     };
945
946 (the last entry in the array must have a NULL 'strptr' value, to
947 indicate the end of the array).  The 'strings' field would be set to
948 'VALS(valstringname)'.
949
950 If the field has a numeric rather than an enumerated type, the 'strings'
951 field would be set to NULL.
952
953 -- Extended value strings
954 You can also use an extended version of the value_string for faster lookups.
955 It requires a value_string array as input.
956 If all of a contiguous range of values from min to max are present in the array
957 in ascending order the value will be used as a direct index into a value_string array.
958
959 If the values in the array are not contiguous (ie: there are "gaps"), but are
960 in ascending order a binary search will be used.
961
962 Note: "gaps" in a value_string array can be filled with "empty" entries eg:
963 {value, "Unknown"} so that direct access to the array is is possible.
964
965 Note: the value_string array values are *unsigned*; IOW: -1 is greater than 0.
966       So:
967       { -2, -1,  1,  2 }; wrong:   linear search will be used (note gap)
968       {  1,  2, -2, -1 }; correct: binary search will be used
969
970       As a special case:
971       { -2, -1,  0,  1,  2 }; OK: direct(indexed) access will be used (note no gap)
972
973 The init macro (see below) will perform a check on the value string the first
974 time it is used to determine which search algorithm fits and fall back to a
975 linear search if the value_string does not meet the criteria above.
976
977 Use this macro to initialize the extended value_string at compile time:
978
979 static value_string_ext valstringname_ext = VALUE_STRING_EXT_INIT(valstringname);
980
981 Extended value strings can be created at run time by calling
982    value_string_ext_new(<ptr to value_string array>,
983                         <total number of entries in the value_string_array>, /* include {0, NULL} entry */
984                         <value_string_name>);
985
986 For hf[] array FT_(U)INT* fields that need a 'valstringname_ext' struct, the
987 'strings' field would be set to '&valstringname_ext'. Furthermore, the 'display'
988 field must be ORed with 'BASE_EXT_STRING' (e.g. BASE_DEC|BASE_EXT_STRING).
989
990 -- val64_string
991
992 val64_strings are like value_strings, except that the integer type
993 used is a guint64 (instead of guint32). Instead of using the VALS()
994 macro for the 'strings' field in the header_field_info struct array,
995 'VALS64()' is used.
996
997 -- Unit string
998 Some integer fields, of type FT_UINT* and float fiels, of type FT_FLOAT
999 or FT_DOUBLE, need units of measurement to help convey the field value.
1000
1001 A 'unit_name_string' structure is a way to add a unit suffix to a field.
1002
1003     typedef struct unit_name_string {
1004         char *singular;     /* name to use for 1 unit */
1005         char *plural;       /* name to use for < 1 or > 1 units */
1006     } unit_name_string;
1007
1008 For fields with that unit name, you would declare a "unit_name_string":
1009
1010     static const unit_name_string unitname[] =
1011         { "single item name" , "multiple item name" };
1012
1013 (the second entry can be NULL if there is no plural form of the unit name.
1014 This is typically the case when abbreviations are used instead of full words.)
1015
1016 There are several "common" unit name structures already defined in
1017 epan/unit_strings.h.  Dissector authors may choose to add the unit name
1018 structure there rather than locally in a dissector.
1019
1020 For hf[] array FT_(U)INT*, FT_FlOAT and FT_DOUBLE fields that need a
1021 'unit_name_string' struct, the 'strings' field would be set to
1022 '&units_second_seconds'. Furthermore, the 'display' field must be ORed
1023 with 'BASE_UNIT_STRING' (e.g. BASE_DEC|BASE_UNIT_STRING).
1024
1025 -- Ranges
1026 If the field has a numeric type that might logically fit in ranges of values
1027 one can use a range_string struct.
1028
1029 Thus a 'range_string' structure is a way to map ranges to strings.
1030
1031         typedef struct _range_string {
1032                 guint32        value_min;
1033                 guint32        value_max;
1034                 const gchar   *strptr;
1035         } range_string;
1036
1037 For fields of that type, you would declare an array of "range_string"s:
1038
1039     static const range_string rvalstringname[] = {
1040         { INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" },
1041         { INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" },
1042         { 0,           0,          NULL                   }
1043     };
1044
1045 If INTVAL_MIN equals INTVAL_MAX for a given entry the range_string
1046 behavior collapses to the one of value_string.
1047 For FT_(U)INT* fields that need a 'range_string' struct, the 'strings' field
1048 would be set to 'RVALS(rvalstringname)'. Furthermore, 'display' field must be
1049 ORed with 'BASE_RANGE_STRING' (e.g. BASE_DEC|BASE_RANGE_STRING).
1050
1051 -- Booleans
1052 FT_BOOLEANs have a default map of 0 = "False", 1 (or anything else) = "True".
1053 Sometimes it is useful to change the labels for boolean values (e.g.,
1054 to "Yes"/"No", "Fast"/"Slow", etc.).  For these mappings, a struct called
1055 true_false_string is used.
1056
1057     typedef struct true_false_string {
1058         char    *true_string;
1059         char    *false_string;
1060     } true_false_string;
1061
1062 For Boolean fields for which "False" and "True" aren't the desired
1063 labels, you would declare a "true_false_string"s:
1064
1065     static const true_false_string boolstringname = {
1066         "String for True",
1067         "String for False"
1068     };
1069
1070 Its two fields are pointers to the string representing truth, and the
1071 string representing falsehood.  For FT_BOOLEAN fields that need a
1072 'true_false_string' struct, the 'strings' field would be set to
1073 'TFS(&boolstringname)'.
1074
1075 If the Boolean field is to be displayed as "False" or "True", the
1076 'strings' field would be set to NULL.
1077
1078 Wireshark predefines a whole range of ready made "true_false_string"s
1079 in tfs.h, included via packet.h.
1080
1081 -- Custom
1082 Custom fields (BASE_CUSTOM) should use CF_FUNC(&custom_format_func) for the
1083 'strings' field.
1084
1085 bitmask (BITMASK)
1086 -----------------
1087 If the field is a bitfield, then the bitmask is the mask which will
1088 leave only the bits needed to make the field when ANDed with a value.
1089 The proto_tree routines will calculate 'bitshift' automatically
1090 from 'bitmask', by finding the rightmost set bit in the bitmask.
1091 This shift is applied before applying string mapping functions or
1092 filtering.
1093
1094 If the field is not a bitfield, then bitmask should be set to 0.
1095
1096 blurb (FIELDDESCR)
1097 ------------------
1098 This is a string giving a proper description of the field.  It should be
1099 at least one grammatically complete sentence, or NULL in which case the
1100 name field is used. (Please do not use "").
1101
1102 It is meant to provide a more detailed description of the field than the
1103 name alone provides. This information will be used in the man page, and
1104 in a future GUI display-filter creation tool. We might also add tooltips
1105 to the labels in the GUI protocol tree, in which case the blurb would
1106 be used as the tooltip text.
1107
1108
1109 1.5.1 Field Registration.
1110
1111 Protocol registration is handled by creating an instance of the
1112 header_field_info struct (or an array of such structs), and
1113 calling the registration function along with the registration ID of
1114 the protocol that is the parent of the fields. Here is a complete example:
1115
1116     static int proto_eg = -1;
1117     static int hf_field_a = -1;
1118     static int hf_field_b = -1;
1119
1120     static hf_register_info hf[] = {
1121
1122         { &hf_field_a,
1123         { "Field A", "proto.field_a", FT_UINT8, BASE_HEX, NULL,
1124             0xf0, "Field A represents Apples", HFILL }},
1125
1126         { &hf_field_b,
1127         { "Field B", "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
1128             0x0, "Field B represents Bananas", HFILL }}
1129     };
1130
1131     proto_eg = proto_register_protocol("Example Protocol",
1132         "PROTO", "proto");
1133     proto_register_field_array(proto_eg, hf, array_length(hf));
1134
1135 Be sure that your array of hf_register_info structs is declared 'static',
1136 since the proto_register_field_array() function does not create a copy
1137 of the information in the array... it uses that static copy of the
1138 information that the compiler created inside your array. Here's the
1139 layout of the hf_register_info struct:
1140
1141 typedef struct hf_register_info {
1142     int            *p_id; /* pointer to parent variable */
1143     header_field_info hfinfo;
1144 } hf_register_info;
1145
1146 Also be sure to use the handy array_length() macro found in packet.h
1147 to have the compiler compute the array length for you at compile time.
1148
1149 If you don't have any fields to register, do *NOT* create a zero-length
1150 "hf" array; not all compilers used to compile Wireshark support them.
1151 Just omit the "hf" array, and the "proto_register_field_array()" call,
1152 entirely.
1153
1154 It is OK to have header fields with a different format be registered with
1155 the same abbreviation. For instance, the following is valid:
1156
1157     static hf_register_info hf[] = {
1158
1159         { &hf_field_8bit, /* 8-bit version of proto.field */
1160         { "Field (8 bit)", "proto.field", FT_UINT8, BASE_DEC, NULL,
1161             0x00, "Field represents FOO", HFILL }},
1162
1163         { &hf_field_32bit, /* 32-bit version of proto.field */
1164         { "Field (32 bit)", "proto.field", FT_UINT32, BASE_DEC, NULL,
1165             0x00, "Field represents FOO", HFILL }}
1166     };
1167
1168 This way a filter expression can match a header field, irrespective of the
1169 representation of it in the specific protocol context. This is interesting
1170 for protocols with variable-width header fields.
1171
1172 Note that the formats used must all belong to the same group as defined below:
1173 - FT_INT8, FT_INT16, FT_INT24 and FT_INT32
1174 - FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_IPXNET and FT_FRAMENUM
1175 - FT_INT40, FT_INT48, FT_INT56 and FT_INT64
1176 - FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64 and FT_EUI64
1177 - FT_ABSOLUTE_TIME and FT_RELATIVE_TIME
1178 - FT_STRING, FT_STRINGZ, FT_UINT_STRING and FT_STRINGZPAD
1179 - FT_FLOAT and FT_DOUBLE
1180 - FT_BYTES, FT_UINT_BYTES, FT_ETHER, FT_AX25, FT_VINES and FT_FCWWN
1181 - FT_OID, FT_REL_OID and FT_SYSTEM_ID
1182
1183 Any field not in a grouping above should *NOT* be used in duplicate field
1184 abbreviations. The current code does not prevent it, but someday in the future
1185 it might.
1186
1187 The HFILL macro at the end of the struct will set reasonable default values
1188 for internally used fields.
1189
1190 1.5.2 Adding Items and Values to the Protocol Tree.
1191
1192 A protocol item is added to an existing protocol tree with one of a
1193 handful of proto_XXX_DO_YYY() functions.
1194
1195 Subtrees can be made with the proto_item_add_subtree() function:
1196
1197     item = proto_tree_add_item(....);
1198     new_tree = proto_item_add_subtree(item, tree_type);
1199
1200 This will add a subtree under the item in question; a subtree can be
1201 created under an item made by any of the "proto_tree_add_XXX" functions,
1202 so that the tree can be given an arbitrary depth.
1203
1204 Subtree types are integers, assigned by
1205 "proto_register_subtree_array()".  To register subtree types, pass an
1206 array of pointers to "gint" variables to hold the subtree type values to
1207 "proto_register_subtree_array()":
1208
1209     static gint ett_eg = -1;
1210     static gint ett_field_a = -1;
1211
1212     static gint *ett[] = {
1213         &ett_eg,
1214         &ett_field_a
1215     };
1216
1217     proto_register_subtree_array(ett, array_length(ett));
1218
1219 in your "register" routine, just as you register the protocol and the
1220 fields for that protocol.
1221
1222 The ett_ variables identify particular type of subtree so that if you expand
1223 one of them, Wireshark keeps track of that and, when you click on
1224 another packet, it automatically opens all subtrees of that type.
1225 If you close one of them, all subtrees of that type will be closed when
1226 you move to another packet.
1227
1228 There are several functions that the programmer can use to add either
1229 protocol or field labels to the proto_tree:
1230
1231     proto_item*
1232     proto_tree_add_item(tree, id, tvb, start, length, encoding);
1233
1234     proto_item*
1235     proto_tree_add_item_ret_int(tree, id, tvb, start, length, encoding,
1236         *retval);
1237
1238     proto_item*
1239     proto_tree_add_item_ret_uint(tree, id, tvb, start, length, encoding,
1240         *retval);
1241
1242     proto_item*
1243     proto_tree_add_subtree(tree, tvb, start, length, idx, tree_item,
1244         text);
1245
1246     proto_item*
1247     proto_tree_add_subtree_format(tree, tvb, start, length, idx, tree_item,
1248         format, ...);
1249
1250     proto_item*
1251     proto_tree_add_none_format(tree, id, tvb, start, length, format, ...);
1252
1253     proto_item*
1254     proto_tree_add_protocol_format(tree, id, tvb, start, length,
1255         format, ...);
1256
1257     proto_item *
1258     proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr);
1259
1260     proto_item *
1261     proto_tree_add_bytes_item(tree, id, tvb, start, length, encoding,
1262         retval, endoff, err);
1263
1264     proto_item *
1265     proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr,
1266         format, ...);
1267
1268     proto_item *
1269     proto_tree_add_bytes_format_value(tree, id, tvb, start, length,
1270         start_ptr, format, ...);
1271
1272     proto_item *
1273     proto_tree_add_bytes_with_length(tree, id, tvb, start, tvb_length, start_ptr, ptr_length);
1274
1275     proto_item *
1276     proto_tree_add_time(tree, id, tvb, start, length, value_ptr);
1277
1278     proto_item *
1279     proto_tree_add_time_item(tree, id, tvb, start, length, encoding,
1280         retval, endoff, err);
1281
1282     proto_item *
1283     proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr,
1284         format, ...);
1285
1286     proto_item *
1287     proto_tree_add_time_format_value(tree, id, tvb, start, length,
1288         value_ptr, format, ...);
1289
1290     proto_item *
1291     proto_tree_add_ipxnet(tree, id, tvb, start, length, value);
1292
1293     proto_item *
1294     proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value,
1295         format, ...);
1296
1297     proto_item *
1298     proto_tree_add_ipxnet_format_value(tree, id, tvb, start, length,
1299         value, format, ...);
1300
1301     proto_item *
1302     proto_tree_add_ipv4(tree, id, tvb, start, length, value);
1303
1304     proto_item *
1305     proto_tree_add_ipv4_format(tree, id, tvb, start, length, value,
1306         format, ...);
1307
1308     proto_item *
1309     proto_tree_add_ipv4_format_value(tree, id, tvb, start, length,
1310         value, format, ...);
1311
1312     proto_item *
1313     proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr);
1314
1315     proto_item *
1316     proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr,
1317         format, ...);
1318
1319     proto_item *
1320     proto_tree_add_ipv6_format_value(tree, id, tvb, start, length,
1321         value_ptr, format, ...);
1322
1323     proto_item *
1324     proto_tree_add_ether(tree, id, tvb, start, length, value_ptr);
1325
1326     proto_item *
1327     proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr,
1328         format, ...);
1329
1330     proto_item *
1331     proto_tree_add_ether_format_value(tree, id, tvb, start, length,
1332         value_ptr, format, ...);
1333
1334     proto_item *
1335     proto_tree_add_guid(tree, id, tvb, start, length, value_ptr);
1336
1337     proto_item *
1338     proto_tree_add_guid_format(tree, id, tvb, start, length, value_ptr,
1339         format, ...);
1340
1341     proto_item *
1342     proto_tree_add_guid_format_value(tree, id, tvb, start, length,
1343         value_ptr, format, ...);
1344
1345     proto_item *
1346     proto_tree_add_oid(tree, id, tvb, start, length, value_ptr);
1347
1348     proto_item *
1349     proto_tree_add_oid_format(tree, id, tvb, start, length, value_ptr,
1350         format, ...);
1351
1352     proto_item *
1353     proto_tree_add_oid_format_value(tree, id, tvb, start, length,
1354         value_ptr, format, ...);
1355
1356     proto_item *
1357     proto_tree_add_string(tree, id, tvb, start, length, value_ptr);
1358
1359     proto_item *
1360     proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr,
1361         format, ...);
1362
1363     proto_item *
1364     proto_tree_add_string_format_value(tree, id, tvb, start, length,
1365         value_ptr, format, ...);
1366
1367     proto_item *
1368     proto_tree_add_boolean(tree, id, tvb, start, length, value);
1369
1370     proto_item *
1371     proto_tree_add_boolean_format(tree, id, tvb, start, length, value,
1372         format, ...);
1373
1374     proto_item *
1375     proto_tree_add_boolean_format_value(tree, id, tvb, start, length,
1376         value, format, ...);
1377
1378     proto_item *
1379     proto_tree_add_float(tree, id, tvb, start, length, value);
1380
1381     proto_item *
1382     proto_tree_add_float_format(tree, id, tvb, start, length, value,
1383         format, ...);
1384
1385     proto_item *
1386     proto_tree_add_float_format_value(tree, id, tvb, start, length,
1387         value, format, ...);
1388
1389     proto_item *
1390     proto_tree_add_double(tree, id, tvb, start, length, value);
1391
1392     proto_item *
1393     proto_tree_add_double_format(tree, id, tvb, start, length, value,
1394         format, ...);
1395
1396     proto_item *
1397     proto_tree_add_double_format_value(tree, id, tvb, start, length,
1398         value, format, ...);
1399
1400     proto_item *
1401     proto_tree_add_uint(tree, id, tvb, start, length, value);
1402
1403     proto_item *
1404     proto_tree_add_uint_format(tree, id, tvb, start, length, value,
1405         format, ...);
1406
1407     proto_item *
1408     proto_tree_add_uint_format_value(tree, id, tvb, start, length,
1409         value, format, ...);
1410
1411     proto_item *
1412     proto_tree_add_uint64(tree, id, tvb, start, length, value);
1413
1414     proto_item *
1415     proto_tree_add_uint64_format(tree, id, tvb, start, length, value,
1416         format, ...);
1417
1418     proto_item *
1419     proto_tree_add_uint64_format_value(tree, id, tvb, start, length,
1420         value, format, ...);
1421
1422     proto_item *
1423     proto_tree_add_int(tree, id, tvb, start, length, value);
1424
1425     proto_item *
1426     proto_tree_add_int_format(tree, id, tvb, start, length, value,
1427         format, ...);
1428
1429     proto_item *
1430     proto_tree_add_int_format_value(tree, id, tvb, start, length,
1431         value, format, ...);
1432
1433     proto_item *
1434     proto_tree_add_int64(tree, id, tvb, start, length, value);
1435
1436     proto_item *
1437     proto_tree_add_int64_format(tree, id, tvb, start, length, value,
1438         format, ...);
1439
1440     proto_item *
1441     proto_tree_add_int64_format_value(tree, id, tvb, start, length,
1442         value, format, ...);
1443
1444     proto_item *
1445     proto_tree_add_eui64(tree, id, tvb, start, length, value);
1446
1447     proto_item *
1448     proto_tree_add_eui64_format(tree, id, tvb, start, length, value,
1449         format, ...);
1450
1451     proto_item *
1452     proto_tree_add_eui64_format_value(tree, id, tvb, start, length,
1453         value, format, ...);
1454
1455     proto_item *
1456     proto_tree_add_checksum(proto_tree *tree, tvbuff_t *tvb, const guint offset,
1457         const int hf_checksum, const int hf_checksum_status, struct expert_field* bad_checksum_expert,
1458         packet_info *pinfo, guint32 computed_checksum, const guint encoding, const guint flags);
1459
1460     proto_item *
1461     proto_tree_add_bitmask(tree, tvb, start, header, ett, fields,
1462         encoding);
1463
1464     proto_item *
1465     proto_tree_add_bitmask_len(tree, tvb, start, len, header, ett, fields,
1466         encoding);
1467
1468     proto_item *
1469     proto_tree_add_bitmask_text(tree, tvb, offset, len, name, fallback,
1470         ett, fields, encoding, flags);
1471
1472     proto_item *
1473     proto_tree_add_bitmask_with_flags(tree, tvb, offset, hf_hdr, ett,
1474         fields, encoding, flags);
1475
1476     proto_item*
1477     proto_tree_add_bits_item(tree, id, tvb, bit_offset, no_of_bits,
1478         encoding);
1479
1480     proto_item *
1481     proto_tree_add_split_bits_item_ret_val(tree, hf_index, tvb, bit_offset,
1482         crumb_spec, return_value);
1483
1484     void
1485     proto_tree_add_split_bits_crumb(tree, hf_index, tvb, bit_offset,
1486         crumb_spec, crumb_index);
1487
1488     proto_item *
1489     proto_tree_add_bits_ret_val(tree, id, tvb, bit_offset, no_of_bits,
1490         return_value, encoding);
1491
1492     proto_item *
1493     proto_tree_add_uint_bits_format_value(tree, id, tvb, bit_offset,
1494         no_of_bits, value, format, ...);
1495
1496     proto_item *
1497     proto_tree_add_boolean_bits_format_value(tree, id, tvb, bit_offset,
1498         no_of_bits, value, format, ...);
1499
1500     proto_item *
1501     proto_tree_add_int_bits_format_value(tree, id, tvb, bit_offset,
1502         no_of_bits, value, format, ...);
1503
1504     proto_item *
1505     proto_tree_add_float_bits_format_value(tree, id, tvb, bit_offset,
1506         no_of_bits, value, format, ...);
1507
1508     proto_item *
1509     proto_tree_add_ts_23_038_7bits_item(tree, hf_index, tvb,
1510         bit_offset, no_of_chars);
1511
1512 The 'tree' argument is the tree to which the item is to be added.  The
1513 'tvb' argument is the tvbuff from which the item's value is being
1514 extracted; the 'start' argument is the offset from the beginning of that
1515 tvbuff of the item being added, and the 'length' argument is the length,
1516 in bytes, of the item, bit_offset is the offset in bits and no_of_bits
1517 is the length in bits.
1518
1519 The length of some items cannot be determined until the item has been
1520 dissected; to add such an item, add it with a length of -1, and, when the
1521 dissection is complete, set the length with 'proto_item_set_len()':
1522
1523     void
1524     proto_item_set_len(ti, length);
1525
1526 The "ti" argument is the value returned by the call that added the item
1527 to the tree, and the "length" argument is the length of the item.
1528
1529 proto_tree_add_item()
1530 ---------------------
1531 proto_tree_add_item is used when you wish to do no special formatting.
1532 The item added to the GUI tree will contain the name (as passed in the
1533 proto_register_*() function) and a value.  The value will be fetched
1534 from the tvbuff by proto_tree_add_item(), based on the type of the field
1535 and the encoding of the value as specified by the "encoding" argument.
1536
1537 For FT_NONE, FT_BYTES, FT_ETHER, FT_IPv6, FT_IPXNET, FT_OID, FT_REL_OID,
1538 FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FCWWN fields, and 'protocol' fields
1539 the encoding is not relevant; the 'encoding' argument should be
1540 ENC_NA (Not Applicable).
1541
1542 For integral, floating-point, Boolean, FT_GUID, and FT_EUI64 fields,
1543 the encoding specifies the byte order of the value; the 'encoding'
1544 argument should be ENC_LITTLE_ENDIAN if the value is little-endian
1545 and ENC_BIG_ENDIAN if it is big-endian.
1546
1547 For FT_IPv4 fields, the encoding also specifies the byte order of the
1548 value.  In almost all cases, the encoding is in network byte order,
1549 hence big-endian, but in at least one protocol dissected by Wireshark,
1550 at least one IPv4 address is byte-swapped, so it's in little-endian
1551 order.
1552
1553 For string fields, the encoding specifies the character set used for the
1554 string and the way individual code points in that character set are
1555 encoded.  For FT_UINT_STRING fields, the byte order of the count must be
1556 specified; for UCS-2 and UTF-16, the byte order of the encoding must be
1557 specified (for counted UCS-2 and UTF-16 strings, the byte order of the
1558 count and the 16-bit values in the string must be the same).  In other
1559 cases, ENC_NA should be used.  The character encodings that are
1560 currently supported are:
1561
1562     ENC_ASCII - ASCII (currently treated as UTF-8; in the future,
1563         all bytes with the 8th bit set will be treated as
1564         errors)
1565     ENC_UTF_8 - UTF-8-encoded Unicode
1566     ENC_UTF_16 - UTF-16-encoded Unicode, with surrogate pairs
1567     ENC_UCS_2 - UCS-2-encoded subset of Unicode, with no surrogate pairs
1568         and thus no code points above 0xFFFF
1569     ENC_UCS_4 - UCS-4-encoded Unicode
1570     ENC_WINDOWS_1250 - Windows-1250 code page
1571     ENC_ISO_8859_1 - ISO 8859-1
1572     ENC_ISO_8859_2 - ISO 8859-2
1573     ENC_ISO_8859_3 - ISO 8859-3
1574     ENC_ISO_8859_4 - ISO 8859-4
1575     ENC_ISO_8859_5 - ISO 8859-5
1576     ENC_ISO_8859_6 - ISO 8859-6
1577     ENC_ISO_8859_7 - ISO 8859-7
1578     ENC_ISO_8859_8 - ISO 8859-8
1579     ENC_ISO_8859_9 - ISO 8859-9
1580     ENC_ISO_8859_10 - ISO 8859-10
1581     ENC_ISO_8859_11 - ISO 8859-11
1582     ENC_ISO_8859_13 - ISO 8859-13
1583     ENC_ISO_8859_14 - ISO 8859-14
1584     ENC_ISO_8859_15 - ISO 8859-15
1585     ENC_ISO_8859_16 - ISO 8859-16
1586     ENC_WINDOWS_1250 - Windows-1250
1587     ENC_3GPP_TS_23_038_7BITS - GSM 7 bits alphabet as described
1588         in 3GPP TS 23.038
1589     ENC_EBCDIC - EBCDIC
1590     ENC_MAC_ROMAN - MAC ROMAN
1591     ENC_CP437 - DOS CP437
1592     ENC_ASCII_7BITS - 7 bits ASCII
1593     ENC_T61 - ITU T.61
1594
1595 Other encodings will be added in the future.
1596
1597 For FT_ABSOLUTE_TIME fields, the encoding specifies the form in which
1598 the time stamp is specified, as well as its byte order.  The time stamp
1599 encodings that are currently supported are:
1600
1601     ENC_TIME_TIMESPEC - seconds (4 bytes) and nanoseconds (4 bytes)
1602         of time since January 1, 1970, midnight UTC.
1603         (If the field length is 4, then nanosecs is defaulted to 0).
1604
1605     ENC_TIME_NTP - an NTP timestamp, represented as a 64-bit
1606         unsigned fixed-point number, in seconds relative to 0h
1607         on 1 January 1900.  The integer part is in the first 32
1608         bits and the fraction part in the last 32 bits.
1609
1610 For other types, there is no support for proto_tree_add_item().
1611
1612 Now that definitions of fields have detailed information about bitfield
1613 fields, you can use proto_tree_add_item() with no extra processing to
1614 add bitfield values to your tree.  Here's an example.  Take the Format
1615 Identifier (FID) field in the Transmission Header (TH) portion of the SNA
1616 protocol.  The FID is the high nibble of the first byte of the TH.  The
1617 FID would be registered like this:
1618
1619     name        = "Format Identifier"
1620     abbrev      = "sna.th.fid"
1621     type        = FT_UINT8
1622     display     = BASE_HEX
1623     strings     = sna_th_fid_vals
1624     bitmask     = 0xf0
1625
1626 The bitmask contains the value which would leave only the FID if bitwise-ANDed
1627 against the parent field, the first byte of the TH.
1628
1629 The code to add the FID to the tree would be;
1630
1631     proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1,
1632         ENC_BIG_ENDIAN);
1633
1634 The definition of the field already has the information about bitmasking
1635 and bitshifting, so it does the work of masking and shifting for us!
1636 This also means that you no longer have to create value_string structs
1637 with the values bitshifted.  The value_string for FID looks like this,
1638 even though the FID value is actually contained in the high nibble.
1639 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
1640
1641 /* Format Identifier */
1642 static const value_string sna_th_fid_vals[] = {
1643     { 0x0, "SNA device <--> Non-SNA Device" },
1644     { 0x1, "Subarea Node <--> Subarea Node" },
1645     { 0x2, "Subarea Node <--> PU2" },
1646     { 0x3, "Subarea Node or SNA host <--> Subarea Node" },
1647     { 0x4, "?" },
1648     { 0x5, "?" },
1649     { 0xf, "Adjacent Subarea Nodes" },
1650     { 0, NULL }
1651 };
1652
1653 The final implication of this is that display filters work the way you'd
1654 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
1655 Subarea Nodes. The user does not have to shift the value of the FID to
1656 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
1657 in the past.
1658
1659 proto_tree_add_item_ret_XXX()
1660 ------------------------------
1661 proto_tree_add_item_ret_XXX is used when you want the displayed value returned
1662 for futher processing only integer and unsigned integer types up to 32 bits are
1663 supported usage of proper FT_ is checked.
1664
1665 proto_tree_add_XXX_item()
1666 ---------------------
1667 proto_tree_add_XXX_item is used when you wish to do no special formatting,
1668 but also either wish for the retrieved value from the tvbuff to be handed
1669 back (to avoid doing tvb_get_...), and/or wish to have the value be decoded
1670 from the tvbuff in a string-encoded format.
1671
1672 The item added to the GUI tree will contain the name (as passed in the
1673 proto_register_*() function) and a value.  The value will be fetched
1674 from the tvbuff, based on the type of the XXX name and the encoding of
1675 the value as specified by the "encoding" argument.
1676
1677 This function retrieves the value even if the passed-in tree param is NULL,
1678 so that it can be used by dissectors at all times to both get the value
1679 and set the tree item to it.
1680
1681 Like other proto_tree_add functions, if there is a tree and the value cannot
1682 be decoded from the tvbuff, then an expert info error is reported. For string
1683 encoding, this means that a failure to decode the hex value from the string
1684 results in an expert info error being added to the tree.
1685
1686 For string-decoding, the passed-in encoding argument needs to specify the
1687 string encoding (e.g., ENC_ASCII, ENC_UTF_8) as well as the format.  For
1688 some XXX types, the format is constrained - for example for the encoding format
1689 for proto_tree_add_time_item() can only be one of the ENC_ISO_8601_* ones
1690 or ENC_RFC_822 or ENC_RFC_1123. For proto_tree_add_bytes_item() it can only
1691 be ENC_STR_HEX bit-or'ed with one or more of the ENC_SEP_* separator types.
1692
1693 proto_tree_add_protocol_format()
1694 --------------------------------
1695 proto_tree_add_protocol_format is used to add the top-level item for the
1696 protocol when the dissector routine wants complete control over how the
1697 field and value will be represented on the GUI tree.  The ID value for
1698 the protocol is passed in as the "id" argument; the rest of the
1699 arguments are a "printf"-style format and any arguments for that format.
1700 The caller must include the name of the protocol in the format; it is
1701 not added automatically as in proto_tree_add_item().
1702
1703 proto_tree_add_none_format()
1704 ----------------------------
1705 proto_tree_add_none_format is used to add an item of type FT_NONE.
1706 The caller must include the name of the field in the format; it is
1707 not added automatically as in proto_tree_add_item().
1708
1709 proto_tree_add_bytes()
1710 proto_tree_add_time()
1711 proto_tree_add_ipxnet()
1712 proto_tree_add_ipv4()
1713 proto_tree_add_ipv6()
1714 proto_tree_add_ether()
1715 proto_tree_add_string()
1716 proto_tree_add_boolean()
1717 proto_tree_add_float()
1718 proto_tree_add_double()
1719 proto_tree_add_uint()
1720 proto_tree_add_uint64()
1721 proto_tree_add_int()
1722 proto_tree_add_int64()
1723 proto_tree_add_guid()
1724 proto_tree_add_oid()
1725 proto_tree_add_eui64()
1726 ------------------------
1727 These routines are used to add items to the protocol tree if either:
1728
1729     the value of the item to be added isn't just extracted from the
1730     packet data, but is computed from data in the packet;
1731
1732     the value was fetched into a variable.
1733
1734 The 'value' argument has the value to be added to the tree.
1735
1736 NOTE: in all cases where the 'value' argument is a pointer, a copy is
1737 made of the object pointed to; if you have dynamically allocated a
1738 buffer for the object, that buffer will not be freed when the protocol
1739 tree is freed - you must free the buffer yourself when you don't need it
1740 any more.
1741
1742 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
1743 sequence of bytes.
1744
1745
1746 proto_tree_add_bytes_with_length() is similar to proto_tree_add_bytes,
1747 except that the length is not derived from the tvb length. Instead,
1748 the displayed data size is controlled by 'ptr_length'.
1749
1750 For proto_tree_add_bytes_format() and proto_tree_add_bytes_format_value(), the
1751 'value_ptr' argument is a pointer to a sequence of bytes or NULL if the bytes
1752 should be taken from the given TVB using the given offset and length.
1753
1754 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
1755 "nstime_t", which is a structure containing the time to be added; it has
1756 'secs' and 'nsecs' members, giving the integral part and the fractional
1757 part of a time in units of seconds, with 'nsecs' being the number of
1758 nanoseconds.  For absolute times, "secs" is a UNIX-style seconds since
1759 January 1, 1970, 00:00:00 GMT value.
1760
1761 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
1762 network address.
1763
1764 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
1765 address, in network byte order.
1766
1767 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
1768 128-bit IPv6 address.
1769
1770 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
1771 48-bit MAC address.
1772
1773 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
1774 text string; this string must be NULL terminated even if the string in the
1775 TVB is not (as may be the case with FT_STRINGs).
1776
1777 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer.
1778 It is masked and shifted as defined by the field info after which zero
1779 means "false", and non-zero means "true".
1780
1781 For proto_tree_add_float(), the 'value' argument is a 'float' in the
1782 host's floating-point format.
1783
1784 For proto_tree_add_double(), the 'value' argument is a 'double' in the
1785 host's floating-point format.
1786
1787 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
1788 integer value, in host byte order.  (This routine cannot be used to add
1789 64-bit integers.)
1790
1791 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
1792 integer value, in host byte order.
1793
1794 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
1795 integer value, in host byte order.  (This routine cannot be used to add
1796 64-bit integers.)
1797
1798 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
1799 integer value, in host byte order.
1800
1801 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
1802 e_guid_t structure.
1803
1804 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
1805 ASN.1 Object Identifier.
1806
1807 For proto_tree_add_eui64(), the 'value' argument is a 64-bit integer
1808 value
1809
1810 proto_tree_add_bytes_format()
1811 proto_tree_add_time_format()
1812 proto_tree_add_ipxnet_format()
1813 proto_tree_add_ipv4_format()
1814 proto_tree_add_ipv6_format()
1815 proto_tree_add_ether_format()
1816 proto_tree_add_string_format()
1817 proto_tree_add_boolean_format()
1818 proto_tree_add_float_format()
1819 proto_tree_add_double_format()
1820 proto_tree_add_uint_format()
1821 proto_tree_add_uint64_format()
1822 proto_tree_add_int_format()
1823 proto_tree_add_int64_format()
1824 proto_tree_add_guid_format()
1825 proto_tree_add_oid_format()
1826 proto_tree_add_eui64_format()
1827 ----------------------------
1828 These routines are used to add items to the protocol tree when the
1829 dissector routine wants complete control over how the field and value
1830 will be represented on the GUI tree.  The argument giving the value is
1831 the same as the corresponding proto_tree_add_XXX() function; the rest of
1832 the arguments are a "printf"-style format and any arguments for that
1833 format.  The caller must include the name of the field in the format; it
1834 is not added automatically as in the proto_tree_add_XXX() functions.
1835
1836 proto_tree_add_bytes_format_value()
1837 proto_tree_add_time_format_value()
1838 proto_tree_add_ipxnet_format_value()
1839 proto_tree_add_ipv4_format_value()
1840 proto_tree_add_ipv6_format_value()
1841 proto_tree_add_ether_format_value()
1842 proto_tree_add_string_format_value()
1843 proto_tree_add_boolean_format_value()
1844 proto_tree_add_float_format_value()
1845 proto_tree_add_double_format_value()
1846 proto_tree_add_uint_format_value()
1847 proto_tree_add_uint64_format_value()
1848 proto_tree_add_int_format_value()
1849 proto_tree_add_int64_format_value()
1850 proto_tree_add_guid_format_value()
1851 proto_tree_add_oid_format_value()
1852 proto_tree_add_eui64_format_value()
1853 ------------------------------------
1854
1855 These routines are used to add items to the protocol tree when the
1856 dissector routine wants complete control over how the value will be
1857 represented on the GUI tree.  The argument giving the value is the same
1858 as the corresponding proto_tree_add_XXX() function; the rest of the
1859 arguments are a "printf"-style format and any arguments for that format.
1860 With these routines, unlike the proto_tree_add_XXX_format() routines,
1861 the name of the field is added automatically as in the
1862 proto_tree_add_XXX() functions; only the value is added with the format.
1863 One use case for this would be to add a unit of measurement string to
1864 the value of the field, however using BASE_UNIT_STRING in the hf_
1865 definition is now preferred.
1866
1867 proto_tree_add_checksum()
1868 ----------------------------
1869 proto_tree_add_checksum is used to add a checksum field. The hf field
1870 provided must be the correct size of the checksum (FT_UINT, FT_UINT16,
1871 FT_UINT32, etc).  Additional parameters are there to provide "status"
1872 and expert info depending on whether the checksum matches the provided
1873 value.  The "status" and expert info can be used in cases except
1874 where PROTO_CHECKSUM_NO_FLAGS is used.
1875
1876 proto_tree_add_subtree()
1877 ---------------------
1878 proto_tree_add_subtree() is used to add a label to the GUI tree and create
1879 a subtree for other fields.  It will contain no value, so it is not searchable
1880 in the display filter process.
1881
1882 This should only be used for items with subtrees, which may not
1883 have values themselves - the items in the subtree are the ones with values.
1884
1885 For a subtree, the label on the subtree might reflect some of the items
1886 in the subtree.  This means the label can't be set until at least some
1887 of the items in the subtree have been dissected.  To do this, use
1888 'proto_item_set_text()' or 'proto_item_append_text()':
1889
1890     void
1891     proto_item_set_text(proto_item *ti, ...);
1892
1893     void
1894     proto_item_append_text(proto_item *ti, ...);
1895
1896 'proto_item_set_text()' takes as an argument the proto_item value returned by
1897 one of the parameters in 'proto_tree_add_subtree()', a 'printf'-style format
1898 string, and a set of arguments corresponding to '%' format items in that string,
1899 and replaces the text for the item created by 'proto_tree_add_subtree()' with the result
1900 of applying the arguments to the format string.
1901
1902 'proto_item_append_text()' is similar, but it appends to the text for
1903 the item the result of applying the arguments to the format string.
1904
1905 For example, early in the dissection, one might do:
1906
1907     subtree = proto_tree_add_subtree(tree, tvb, offset, length, ett, &ti, <label>);
1908
1909 and later do
1910
1911     proto_item_set_text(ti, "%s: %s", type, value);
1912
1913 after the "type" and "value" fields have been extracted and dissected.
1914 <label> would be a label giving what information about the subtree is
1915 available without dissecting any of the data in the subtree.
1916
1917 Note that an exception might be thrown when trying to extract the values of
1918 the items used to set the label, if not all the bytes of the item are
1919 available.  Thus, one should create the item with text that is as
1920 meaningful as possible, and set it or append additional information to
1921 it as the values needed to supply that information are extracted.
1922
1923 proto_tree_add_subtree_format()
1924 ----------------------------
1925 This is like proto_tree_add_subtree(), but uses printf-style arguments to
1926 create the label; it is used to allow routines that take a printf-like
1927 variable-length list of arguments to add a text item to the protocol
1928 tree.
1929
1930 proto_tree_add_bits_item()
1931 --------------------------
1932 Adds a number of bits to the protocol tree which does not have to be byte
1933 aligned. The offset and length is in bits.
1934 Output format:
1935
1936 ..10 1010 10.. .... "value" (formatted as FT_ indicates).
1937
1938 proto_tree_add_bits_ret_val()
1939 -----------------------------
1940 Works in the same way but also returns the value of the read bits.
1941
1942 proto_tree_add_split_bits_item_ret_val()
1943 -----------------------------------
1944 Similar, but is used for items that are made of 2 or more smaller sets of bits (crumbs)
1945 which are not contiguous, but are concatenated to form the actual value.  The size of
1946 the crumbs and the order of assembly are specified in an array of crumb_spec structures.
1947
1948 proto_tree_add_split_bits_crumb()
1949 ---------------------------------
1950 Helper function for the above, to add text for each crumb as it is encountered.
1951
1952 proto_tree_add_ts_23_038_7bits_item()
1953 -------------------------------------
1954 Adds a string of a given number of characters and encoded according to 3GPP TS 23.038 7 bits
1955 alphabet.
1956
1957 proto_tree_add_bitmask() et al.
1958 -------------------------------
1959 These functions provide easy to use and convenient dissection of many types of common
1960 bitmasks into individual fields.
1961
1962 header is an integer type and must be of type FT_[U]INT{8|16|24|32||40|48|56|64} and
1963 represents the entire dissectable width of the bitmask.
1964
1965 'header' and 'ett' are the hf fields and ett field respectively to create an
1966 expansion that covers the bytes of the bitmask.
1967
1968 'fields' is a NULL terminated array of pointers to hf fields representing
1969 the individual subfields of the bitmask. These fields must either be integers
1970 (usually of the same byte width as 'header') or of the type FT_BOOLEAN.
1971 Each of the entries in 'fields' will be dissected as an item under the
1972 'header' expansion and also IF the field is a boolean and IF it is set to 1,
1973 then the name of that boolean field will be printed on the 'header' expansion
1974 line.  For integer type subfields that have a value_string defined, the
1975 matched string from that value_string will be printed on the expansion line
1976 as well.
1977
1978 Example: (from the SCSI dissector)
1979     static int hf_scsi_inq_peripheral        = -1;
1980     static int hf_scsi_inq_qualifier         = -1;
1981     static int hf_scsi_inq_devtype           = -1;
1982     ...
1983     static gint ett_scsi_inq_peripheral = -1;
1984     ...
1985     static const int *peripheral_fields[] = {
1986         &hf_scsi_inq_qualifier,
1987         &hf_scsi_inq_devtype,
1988         NULL
1989     };
1990     ...
1991     /* Qualifier and DeviceType */
1992     proto_tree_add_bitmask(tree, tvb, offset, hf_scsi_inq_peripheral,
1993         ett_scsi_inq_peripheral, peripheral_fields, ENC_BIG_ENDIAN);
1994     offset+=1;
1995     ...
1996         { &hf_scsi_inq_peripheral,
1997           {"Peripheral", "scsi.inquiry.peripheral", FT_UINT8, BASE_HEX,
1998            NULL, 0, NULL, HFILL}},
1999         { &hf_scsi_inq_qualifier,
2000           {"Qualifier", "scsi.inquiry.qualifier", FT_UINT8, BASE_HEX,
2001            VALS (scsi_qualifier_val), 0xE0, NULL, HFILL}},
2002         { &hf_scsi_inq_devtype,
2003           {"Device Type", "scsi.inquiry.devtype", FT_UINT8, BASE_HEX,
2004            VALS (scsi_devtype_val), SCSI_DEV_BITS, NULL, HFILL}},
2005     ...
2006
2007 Which provides very pretty dissection of this one byte bitmask.
2008
2009     Peripheral: 0x05, Qualifier: Device type is connected to logical unit, Device Type: CD-ROM
2010         000. .... = Qualifier: Device type is connected to logical unit (0x00)
2011         ...0 0101 = Device Type: CD-ROM (0x05)
2012
2013 The proto_tree_add_bitmask_text() function is an extended version of
2014 the proto_tree_add_bitmask() function. In addition, it allows to:
2015 - Provide a leading text (e.g. "Flags: ") that will appear before
2016   the comma-separated list of field values
2017 - Provide a fallback text (e.g. "None") that will be appended if
2018   no fields warranted a change to the top-level title.
2019 - Using flags, specify which fields will affect the top-level title.
2020
2021 There are the following flags defined:
2022
2023   BMT_NO_APPEND - the title is taken "as-is" from the 'name' argument.
2024   BMT_NO_INT - only boolean flags are added to the title.
2025   BMT_NO_FALSE - boolean flags are only added to the title if they are set.
2026   BMT_NO_TFS - only add flag name to the title, do not use true_false_string
2027
2028 The proto_tree_add_bitmask_with_flags() function is an extended version
2029 of the proto_tree_add_bitmask() function. It allows using flags to specify
2030 which fields will affect the top-level title. The flags are the
2031 same BMT_NO_* flags as used in the proto_tree_add_bitmask_text() function.
2032
2033 The proto_tree_add_bitmask() behavior can be obtained by providing
2034 both 'name' and 'fallback' arguments as NULL, and a flags of
2035 (BMT_NO_FALSE|BMT_NO_TFS).
2036
2037 The proto_tree_add_bitmask_len() function is intended for protocols where
2038 bitmask length is permitted to vary, so a length is specified explicitly
2039 along with the bitmask value. USB Video "bmControl" and "bControlSize"
2040 fields follow this pattern. The primary intent of this is "forward
2041 compatibility," enabling an interpreter coded for version M of a structure
2042 to comprehend fields in version N of the structure, where N > M and
2043 bControlSize increases from version M to version N.
2044
2045 proto_tree_add_bitmask_len() is an extended version of proto_tree_add_bitmask()
2046 that uses an explicitly specified (rather than inferred) length to control
2047 dissection. Because of this, it may encounter two cases that
2048 proto_tree_add_bitmask() and proto_tree_add_bitmask_text() may not:
2049 - A length that exceeds that of the 'header' and bitmask subfields.
2050   In this case the least-significant bytes of the bitmask are dissected.
2051   An expert warning is generated in this case, because the dissection code
2052   likely needs to be updated for a new revision of the protocol.
2053 - A length that is shorter than that of the 'header' and bitmask subfields.
2054   In this case, subfields whose data is fully present are dissected,
2055   and other subfields are not. No warning is generated in this case,
2056   because the dissection code is likely for a later revision of the protocol
2057   than the packet it was called to interpret.
2058
2059
2060 PROTO_ITEM_SET_GENERATED()
2061 --------------------------
2062 PROTO_ITEM_SET_GENERATED is used to mark fields as not being read from the
2063 captured data directly, but inferred from one or more values.
2064
2065 One of the primary uses of this is the presentation of verification of
2066 checksums. Every IP packet has a checksum line, which can present the result
2067 of the checksum verification, if enabled in the preferences. The result is
2068 presented as a subtree, where the result is enclosed in square brackets
2069 indicating a generated field.
2070
2071   Header checksum: 0x3d42 [correct]
2072   [Checksum Status: Good (1)]
2073
2074 PROTO_ITEM_SET_HIDDEN()
2075 -----------------------
2076 PROTO_ITEM_SET_HIDDEN is used to hide fields, which have already been added
2077 to the tree, from being visible in the displayed tree.
2078
2079 NOTE that creating hidden fields is actually quite a bad idea from a UI design
2080 perspective because the user (someone who did not write nor has ever seen the
2081 code) has no way of knowing that hidden fields are there to be filtered on
2082 thus defeating the whole purpose of putting them there.  A Better Way might
2083 be to add the fields (that might otherwise be hidden) to a subtree where they
2084 won't be seen unless the user opens the subtree--but they can be found if the
2085 user wants.
2086
2087 One use for hidden fields (which would be better implemented using visible
2088 fields in a subtree) follows: The caller may want a value to be
2089 included in a tree so that the packet can be filtered on this field, but
2090 the representation of that field in the tree is not appropriate.  An
2091 example is the token-ring routing information field (RIF).  The best way
2092 to show the RIF in a GUI is by a sequence of ring and bridge numbers.
2093 Rings are 3-digit hex numbers, and bridges are single hex digits:
2094
2095     RIF: 001-A-013-9-C0F-B-555
2096
2097 In the case of RIF, the programmer should use a field with no value and
2098 use proto_tree_add_none_format() to build the above representation. The
2099 programmer can then add the ring and bridge values, one-by-one, with
2100 proto_tree_add_item() and hide them with PROTO_ITEM_SET_HIDDEN() so that the
2101 user can then filter on or search for a particular ring or bridge. Here's a
2102 skeleton of how the programmer might code this.
2103
2104     char *rif;
2105     rif = create_rif_string(...);
2106
2107     proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
2108
2109     for(i = 0; i < num_rings; i++) {
2110         proto_item *pi;
2111
2112         pi = proto_tree_add_item(tree, hf_tr_rif_ring, ...,
2113             ENC_BIG_ENDIAN);
2114         PROTO_ITEM_SET_HIDDEN(pi);
2115     }
2116     for(i = 0; i < num_rings - 1; i++) {
2117         proto_item *pi;
2118
2119         pi = proto_tree_add_item(tree, hf_tr_rif_bridge, ...,
2120             ENC_BIG_ENDIAN);
2121         PROTO_ITEM_SET_HIDDEN(pi);
2122     }
2123
2124 The logical tree has these items:
2125
2126     hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
2127     hf_tr_rif_ring,  hidden, value=0x001
2128     hf_tr_rif_bridge, hidden, value=0xA
2129     hf_tr_rif_ring,  hidden, value=0x013
2130     hf_tr_rif_bridge, hidden, value=0x9
2131     hf_tr_rif_ring,  hidden, value=0xC0F
2132     hf_tr_rif_bridge, hidden, value=0xB
2133     hf_tr_rif_ring,  hidden, value=0x555
2134
2135 GUI or print code will not display the hidden fields, but a display
2136 filter or "packet grep" routine will still see the values. The possible
2137 filter is then possible:
2138
2139     tr.rif_ring eq 0x013
2140
2141 PROTO_ITEM_SET_URL
2142 ------------------
2143 PROTO_ITEM_SET_URL is used to mark fields as containing a URL. This can only
2144 be done with fields of type FT_STRING(Z). If these fields are presented they
2145 are underlined, as could be done in a browser. These fields are sensitive to
2146 clicks as well, launching the configured browser with this URL as parameter.
2147
2148 1.6 Utility routines.
2149
2150 1.6.1 val_to_str, val_to_str_const, try_val_to_str and try_val_to_str_idx
2151
2152 A dissector may need to convert a value to a string, using a
2153 'value_string' structure, by hand, rather than by declaring a field with
2154 an associated 'value_string' structure; this might be used, for example,
2155 to generate a COL_INFO line for a frame.
2156
2157 val_to_str() handles the most common case:
2158
2159     const gchar*
2160     val_to_str(guint32 val, const value_string *vs, const char *fmt)
2161
2162 If the value 'val' is found in the 'value_string' table pointed to by
2163 'vs', 'val_to_str' will return the corresponding string; otherwise, it
2164 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
2165 to generate a string, and will return a pointer to that string.
2166 You can use it in a call to generate a COL_INFO line for a frame such as
2167
2168     col_add_fstr(COL_INFO, ", %s", val_to_str(val, table, "Unknown %d"));
2169
2170 If you don't need to display 'val' in your fmt string, you can use
2171 val_to_str_const() which just takes a string constant instead and returns it
2172 unmodified when 'val' isn't found.
2173
2174 If you need to handle the failure case in some custom way, try_val_to_str()
2175 will return NULL if val isn't found:
2176
2177     const gchar*
2178     try_val_to_str(guint32 val, const value_string *vs)
2179
2180 Note that, you must check whether 'try_val_to_str()' returns NULL, and arrange
2181 that its return value not be dereferenced if it's NULL. 'try_val_to_str_idx()'
2182 behaves similarly, except it also returns an index into the value_string array,
2183 or -1 if 'val' was not found.
2184
2185 The *_ext functions are "extended" versions of those already described. They
2186 should be used for large value-string arrays which contain many entries. They
2187 implement value to string conversions which will do either a direct access or
2188 a binary search of the value string array if possible. See
2189 "Extended Value Strings" under section 1.6 "Constructing the protocol tree" for
2190 more information.
2191
2192 See epan/value_string.h for detailed information on the various value_string
2193 functions.
2194
2195 To handle 64-bit values, there are an equivalent set of functions. These are:
2196
2197     const gchar *
2198     val64_to_str(const guint64 val, const val64_string *vs, const char *fmt)
2199
2200     const gchar *
2201     val64_to_str_const(const guint64 val, const val64_string *vs, const char *unknown_str);
2202
2203     const gchar *
2204     try_val64_to_str(const guint64 val, const val64_string *vs);
2205
2206     const gchar *
2207     try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx);
2208
2209
2210 1.6.2 rval_to_str, try_rval_to_str and try_rval_to_str_idx
2211
2212 A dissector may need to convert a range of values to a string, using a
2213 'range_string' structure.
2214
2215 Most of the same functions exist as with regular value_strings (see section
2216 1.6.1) except with the names 'rval' instead of 'val'.
2217
2218
2219 1.7 Calling Other Dissectors.
2220
2221 As each dissector completes its portion of the protocol analysis, it
2222 is expected to create a new tvbuff of type TVBUFF_SUBSET which
2223 contains the payload portion of the protocol (that is, the bytes
2224 that are relevant to the next dissector).
2225
2226 The syntax for creating a new TVBUFF_SUBSET is:
2227
2228 next_tvb = tvb_new_subset_length_caplen(tvb, offset, length, reported_length)
2229
2230 or, in the common case where it should just run to the end of the packet,
2231
2232 next_tvb = tvb_new_subset_remaining(tvb, offset)
2233
2234 Where:
2235     tvb is the tvbuff that the dissector has been working on. It
2236     can be a tvbuff of any type.
2237
2238     next_tvb is the new TVBUFF_SUBSET.
2239
2240     offset is the byte offset of 'tvb' at which the new tvbuff
2241     should start.  The first byte is the 0th byte.
2242
2243     length is the number of bytes in the new TVBUFF_SUBSET. A length
2244     argument of -1 says to use as many bytes as are available in
2245     'tvb'.
2246
2247     reported_length is the number of bytes that the current protocol
2248     says should be in the payload. A reported_length of -1 says that
2249     the protocol doesn't say anything about the size of its payload.
2250
2251 To call a dissector you need to get the handle of the dissector using
2252 find_dissector(), passing it the string name of the dissector.  The setting
2253 of the handle is usually done once at startup during the proto_reg_handoff
2254 function within the calling dissector.
2255
2256 1.7.1 Dissector Tables
2257
2258 Another way to call a subdissector is to setup a dissector table. A dissector
2259 table is a list of subdissectors grouped by a common identifier (integer or
2260 string) in a dissector.  Subdissectors will register themselves with the dissector
2261 table using their unique identifier using one of the following APIs:
2262
2263     void dissector_add_uint(const char *abbrev, const guint32 pattern,
2264                             dissector_handle_t handle);
2265
2266     void dissector_add_uint_range(const char *abbrev, struct epan_range *range,
2267                             dissector_handle_t handle);
2268
2269     void dissector_add_string(const char *name, const gchar *pattern,
2270                             dissector_handle_t handle);
2271
2272     void dissector_add_for_decode_as(const char *name,
2273                             dissector_handle_t handle);
2274
2275     dissector_add_for_decode_as doesn't add a unique identifier in the dissector
2276     table, but it lets the user add it from the command line or, in Wireshark,
2277     through the "Decode As" UI.
2278
2279 Then when the dissector hits the common identifier field, it will useone of the
2280 following APIs to invoke the subdissector:
2281
2282     int dissector_try_uint(dissector_table_t sub_dissectors,
2283         const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo,
2284         proto_tree *tree);
2285
2286     int dissector_try_uint_new(dissector_table_t sub_dissectors,
2287         const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo,
2288         proto_tree *tree, const gboolean add_proto_name, void *data);
2289
2290     int dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
2291         tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data);
2292
2293 These pass a subset of the remaining packet (typically the rest of the
2294 packet) for the dissector table to determine which subdissector is called.
2295 This allows dissection of a packet to be expanded outside of dissector without
2296 having to modify the dissector directly.
2297
2298
2299 1.8 Editing Makefile.am and CMakeLists.txt to add your dissector.
2300
2301 To arrange that your dissector will be built as part of Wireshark, you
2302 must add the name of the source file for your dissector to the
2303 'DISSECTOR_SRC' macro in the 'Makefile.am' file in the 'epan/dissectors'
2304 directory.  (Note that this is for modern versions of UNIX, so there
2305 is no 14-character limitation on file names, and for modern versions of
2306 Windows, so there is no 8.3-character limitation on file names.)
2307
2308 If your dissector also has its own header file or files, you must add
2309 them to the 'DISSECTOR_INCLUDES' macro in the 'Makefile.am' file in
2310 the 'epan/dissectors' directory, so that it's included when release source
2311 tarballs are built (otherwise, the source in the release tarballs won't
2312 compile).
2313
2314 In addition to the above, you should add your dissector source file name
2315 to the DISSECTOR_SRC section of epan/dissectors/CMakeLists.txt
2316
2317
2318 1.9 Using the git source code tree.
2319
2320   See <https://www.wireshark.org/develop.html>
2321
2322
2323 1.10 Submitting code for your new dissector.
2324
2325   - VERIFY that your dissector code does not use prohibited or deprecated APIs
2326     as follows:
2327     perl <wireshark_root>/tools/checkAPIs.pl <source-filename(s)>
2328
2329   - VERIFY that your dissector code does not contain any header field related
2330     problems:
2331     perl <wireshark_root>/tools/checkhf.pl <source-filename(s)>
2332
2333   - VERIFY that your dissector code does not contain any display filter related
2334     problems:
2335     perl <wireshark_root>/tools/checkfiltername.pl <source-filename(s)>
2336
2337   - CHECK your dissector with CppCheck (http://cppcheck.sourceforge.net/) using
2338     Wireshark's customized configuration. This is particularly important on
2339     Windows, since Microsoft's compiler warnings are quite thin:
2340     ./tools/cppcheck/cppcheck.sh <source-filename(s)>
2341
2342   - TEST YOUR DISSECTOR BEFORE SUBMITTING IT.
2343     Use fuzz-test.sh and/or randpkt against your dissector.  These are
2344     described at <https://wiki.wireshark.org/FuzzTesting>.
2345
2346   - Subscribe to <mailto:wireshark-dev[AT]wireshark.org> by sending an email to
2347     <mailto:wireshark-dev-request[AT]wireshark.org?body="help"> or visiting
2348     <https://www.wireshark.org/lists/>.
2349
2350   - 'git diff' to verify all your changes look good.
2351
2352   - 'git add' all the files you changed.
2353
2354   - 'git commit' to commit (locally) your changes. First line of commit message
2355     should be a summary of the changes followed by an empty line and a more
2356     verbose description.
2357
2358   - 'git push origin HEAD:refs/for/master' to push the changes to Gerrit.  (If
2359     you previously ran 'git config --add remote.origin.push HEAD:refs/for/master'
2360     then only 'git push' is needed.)
2361
2362   - Create a Wiki page on the protocol at <https://wiki.wireshark.org>.
2363     A template is provided so it is easy to setup in a consistent style.
2364       See: <https://wiki.wireshark.org/HowToEdit>
2365       and  <https://wiki.wireshark.org/ProtocolReference>
2366
2367   - If possible, add sample capture files to the sample captures page at
2368     <https://wiki.wireshark.org/SampleCaptures>.  These files are used by
2369     the automated build system for fuzz testing.
2370
2371   - If you don't think the wiki is the right place for your sample capture,
2372     submit a bug report to the Wireshark bug database, found at
2373     <https://bugs.wireshark.org>, qualified as an enhancement and attach your
2374     sample capture there.  Normally a new dissector won't be accepted without
2375     a sample capture!  If you open a bug be sure to cross-link your Gerrit
2376     change and bug.
2377
2378 2. Advanced dissector topics.
2379
2380 2.1 Introduction.
2381
2382 Some of the advanced features are being worked on constantly. When using them
2383 it is wise to check the relevant header and source files for additional details.
2384
2385 2.2 Following "conversations".
2386
2387 In wireshark a conversation is defined as a series of data packets between two
2388 address:port combinations.  A conversation is not sensitive to the direction of
2389 the packet.  The same conversation will be returned for a packet bound from
2390 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
2391
2392 2.2.1 Conversation Routines
2393
2394 There are seven routines that you will use to work with a conversation:
2395 conversation_new, find_conversation, find_or_create_conversation,
2396 conversation_add_proto_data, conversation_get_proto_data,
2397 conversation_delete_proto_data, and conversation_set_dissector.
2398
2399
2400 2.2.1.1 The conversation_init function.
2401
2402 This is an internal routine for the conversation code.  As such you
2403 will not have to call this routine.  Just be aware that this routine is
2404 called at the start of each capture and before the packets are filtered
2405 with a display filter.  The routine will destroy all stored
2406 conversations.  This routine does NOT clean up any data pointers that are
2407 passed in the conversation_add_proto_data 'data' variable.  You are
2408 responsible for this clean up if you pass a malloc'ed pointer
2409 in this variable.
2410
2411 See item 2.2.1.5 for more information about use of the 'data' pointer.
2412
2413
2414 2.2.1.2 The conversation_new function.
2415
2416 This routine will create a new conversation based upon two address/port
2417 pairs.  If you want to associate with the conversation a pointer to a
2418 private data structure you must use the conversation_add_proto_data
2419 function.  The ptype variable is used to differentiate between
2420 conversations over different protocols, i.e. TCP and UDP.  The options
2421 variable is used to define a conversation that will accept any destination
2422 address and/or port.  Set options = 0 if the destination port and address
2423 are know when conversation_new is called.  See section 2.4 for more
2424 information on usage of the options parameter.
2425
2426 The conversation_new prototype:
2427     conversation_t *conversation_new(guint32 setup_frame, address *addr1,
2428         address *addr2, port_type ptype, guint32 port1, guint32 port2,
2429         guint options);
2430
2431 Where:
2432     guint32 setup_frame = The lowest numbered frame for this conversation
2433     address* addr1      = first data packet address
2434     address* addr2      = second data packet address
2435     port_type ptype     = port type, this is defined in packet.h
2436     guint32 port1       = first data packet port
2437     guint32 port2       = second data packet port
2438     guint options       = conversation options, NO_ADDR2 and/or NO_PORT2
2439
2440 setup_frame indicates the first frame for this conversation, and is used to
2441 distinguish multiple conversations with the same addr1/port1 and addr2/port2
2442 pair that occur within the same capture session.
2443
2444 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
2445 are the second address/port pair.  A conversation doesn't have source
2446 and destination address/port pairs - packets in a conversation go in
2447 both directions - so "addr1"/"port1" may be the source or destination
2448 address/port pair; "addr2"/"port2" would be the other pair.
2449
2450 If NO_ADDR2 is specified, the conversation is set up so that a
2451 conversation lookup will match only the "addr1" address; if NO_PORT2 is
2452 specified, the conversation is set up so that a conversation lookup will
2453 match only the "port1" port; if both are specified, i.e.
2454 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
2455 match only the "addr1"/"port1" address/port pair.  This can be used if a
2456 packet indicates that, later in the capture, a conversation will be
2457 created using certain addresses and ports, in the case where the packet
2458 doesn't specify the addresses and ports of both sides.
2459
2460 2.2.1.3 The find_conversation function.
2461
2462 Call this routine to look up a conversation.  If no conversation is found,
2463 the routine will return a NULL value.
2464
2465 The find_conversation prototype:
2466
2467     conversation_t *find_conversation(guint32 frame_num, address *addr_a,
2468         address *addr_b, port_type ptype, guint32 port_a, guint32 port_b,
2469         guint options);
2470
2471 Where:
2472     guint32 frame_num = a frame number to match
2473     address* addr_a = first address
2474     address* addr_b = second address
2475     port_type ptype = port type
2476     guint32 port_a = first data packet port
2477     guint32 port_b = second data packet port
2478     guint options = conversation options, NO_ADDR_B and/or NO_PORT_B
2479
2480 frame_num is a frame number to match. The conversation returned is where
2481     (frame_num >= conversation->setup_frame
2482     && frame_num < conversation->next->setup_frame)
2483 Suppose there are a total of 3 conversations (A, B, and C) that match
2484 addr_a/port_a and addr_b/port_b, where the setup_frame used in
2485 conversation_new() for A, B and C are 10, 50, and 100 respectively. The
2486 frame_num passed in find_conversation is compared to the setup_frame of each
2487 conversation. So if (frame_num >= 10 && frame_num < 50), conversation A is
2488 returned. If (frame_num >= 50 && frame_num < 100), conversation B is returned.
2489 If (frame_num >= 100) conversation C is returned.
2490
2491 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
2492 "port_b" are the second address/port pair.  Again, as a conversation
2493 doesn't have source and destination address/port pairs, so
2494 "addr_a"/"port_a" may be the source or destination address/port pair;
2495 "addr_b"/"port_b" would be the other pair.  The search will match the
2496 "a" address/port pair against both the "1" and "2" address/port pairs,
2497 and match the "b" address/port pair against both the "2" and "1"
2498 address/port pairs; you don't have to worry about which side the "a" or
2499 "b" pairs correspond to.
2500
2501 If the NO_ADDR_B flag was specified to "find_conversation()", the
2502 "addr_b" address will be treated as matching any "wildcarded" address;
2503 if the NO_PORT_B flag was specified, the "port_b" port will be treated
2504 as matching any "wildcarded" port.  If both flags are specified, i.e.
2505 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
2506 any "wildcarded" address and the "port_b" port will be treated as
2507 matching any "wildcarded" port.
2508
2509
2510 2.2.1.4 The find_or_create_conversation function.
2511
2512 This convenience function will create find an existing conversation (by calling
2513 find_conversation()) and, if a conversation does not already exist, create a
2514 new conversation by calling conversation_new().
2515
2516 The find_or_create_conversation prototype:
2517
2518     extern conversation_t *find_or_create_conversation(packet_info *pinfo);
2519
2520 Where:
2521     packet_info *pinfo = the packet_info structure
2522
2523 The frame number and the addresses necessary for find_conversation() and
2524 conversation_new() are taken from the pinfo structure (as is commonly done)
2525 and no 'options' are used.
2526
2527
2528 2.2.1.5 The conversation_add_proto_data function.
2529
2530 Once you have created a conversation with conversation_new, you can
2531 associate data with it using this function.
2532
2533 The conversation_add_proto_data prototype:
2534
2535     void conversation_add_proto_data(conversation_t *conv, int proto,
2536         void *proto_data);
2537
2538 Where:
2539     conversation_t *conv    = the conversation in question
2540     int proto               = registered protocol number
2541     void *data              = dissector data structure
2542
2543 "conversation" is the value returned by conversation_new.  "proto" is a
2544 unique protocol number created with proto_register_protocol.  Protocols
2545 are typically registered in the proto_register_XXXX section of your
2546 dissector.  "data" is a pointer to the data you wish to associate with the
2547 conversation.  "data" usually points to "wmem_alloc'd" memory; the
2548 memory will be automatically freed each time a new dissection begins
2549 and thus need not be managed (freed) by the dissector.
2550 Using the protocol number allows several dissectors to
2551 associate data with a given conversation.
2552
2553
2554 2.2.1.6 The conversation_get_proto_data function.
2555
2556 After you have located a conversation with find_conversation, you can use
2557 this function to retrieve any data associated with it.
2558
2559 The conversation_get_proto_data prototype:
2560
2561     void *conversation_get_proto_data(conversation_t *conv, int proto);
2562
2563 Where:
2564     conversation_t *conv    = the conversation in question
2565     int proto               = registered protocol number
2566
2567 "conversation" is the conversation created with conversation_new.  "proto"
2568 is a unique protocol number created with proto_register_protocol,
2569 typically in the proto_register_XXXX portion of a dissector.  The function
2570 returns a pointer to the data requested, or NULL if no data was found.
2571
2572
2573 2.2.1.7 The conversation_delete_proto_data function.
2574
2575 After you are finished with a conversation, you can remove your association
2576 with this function.  Please note that ONLY the conversation entry is
2577 removed.  If you have allocated any memory for your data (other than with wmem_alloc),
2578  you must free it as well.
2579
2580 The conversation_delete_proto_data prototype:
2581
2582     void conversation_delete_proto_data(conversation_t *conv, int proto);
2583
2584 Where:
2585     conversation_t *conv = the conversation in question
2586     int proto            = registered protocol number
2587
2588 "conversation" is the conversation created with conversation_new.  "proto"
2589 is a unique protocol number created with proto_register_protocol,
2590 typically in the proto_register_XXXX portion of a dissector.
2591
2592 2.2.1.8 The conversation_set_dissector function
2593
2594 This function sets the protocol dissector to be invoked whenever
2595 conversation parameters (addresses, port_types, ports, etc) are matched
2596 during the dissection of a packet.
2597
2598 The conversation_set_dissector prototype:
2599
2600     void conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle);
2601
2602 Where:
2603     conversation_t *conv = the conversation in question
2604     const dissector_handle_t handle = the dissector handle.
2605
2606
2607 2.2.2 Using timestamps relative to the conversation
2608
2609 There is a framework to calculate timestamps relative to the start of the
2610 conversation. First of all the timestamp of the first packet that has been
2611 seen in the conversation must be kept in the protocol data to be able
2612 to calculate the timestamp of the current packet relative to the start
2613 of the conversation. The timestamp of the last packet that was seen in the
2614 conversation should also be kept in the protocol data. This way the
2615 delta time between the current packet and the previous packet in the
2616 conversation can be calculated.
2617
2618 So add the following items to the struct that is used for the protocol data:
2619
2620   nstime_t ts_first;
2621   nstime_t ts_prev;
2622
2623 The ts_prev value should only be set during the first run through the
2624 packets (ie PINFO_FD_VISITED(pinfo) is false).
2625
2626 Next step is to use the per-packet information (described in section 2.5)
2627 to keep the calculated delta timestamp, as it can only be calculated
2628 on the first run through the packets. This is because a packet can be
2629 selected in random order once the whole file has been read.
2630
2631 After calculating the conversation timestamps, it is time to put them in
2632 the appropriate columns with the function 'col_set_time' (described in
2633 section 1.5.9). The column used for relative timestamps is:
2634
2635 COL_REL_TIME, /* Delta time to last frame in conversation */
2636
2637 Last but not least, there MUST be a preference in each dissector that
2638 uses conversation timestamps that makes it possible to enable and
2639 disable the calculation of conversation timestamps. The main argument
2640 for this is that a higher level conversation is able to overwrite
2641 the values of lower level conversations in these two columns. Being
2642 able to actively select which protocols may overwrite the conversation
2643 timestamp columns gives the user the power to control these columns.
2644 (A second reason is that conversation timestamps use the per-packet
2645 data structure which uses additional memory, which should be avoided
2646 if these timestamps are not needed)
2647
2648 Have a look at the differences to packet-tcp.[ch] in SVN 22966 and
2649 SVN 23058 to see the implementation of conversation timestamps for
2650 the tcp-dissector.
2651
2652
2653 2.2.3 The example conversation code using wmem_file_scope memory.
2654
2655 For a conversation between two IP addresses and ports you can use this as an
2656 example.  This example uses wmem_alloc() with wmem_file_scope() to allocate
2657 memory and stores the data pointer in the conversation 'data' variable.
2658
2659 /************************ Global values ************************/
2660
2661 /* define your structure here */
2662 typedef struct {
2663
2664 } my_entry_t;
2665
2666 /* Registered protocol number */
2667 static int my_proto = -1;
2668
2669 /********************* in the dissector routine *********************/
2670
2671 /* the local variables in the dissector */
2672
2673 conversation_t *conversation;
2674 my_entry_t *data_ptr;
2675
2676
2677 /* look up the conversation */
2678
2679 conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
2680         pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2681
2682 /* if conversation found get the data pointer that you stored */
2683 if (conversation)
2684     data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2685 else {
2686
2687     /* new conversation create local data structure */
2688
2689     data_ptr = wmem_alloc(wmem_file_scope(), sizeof(my_entry_t));
2690
2691     /*** add your code here to setup the new data structure ***/
2692
2693     /* create the conversation with your data pointer  */
2694
2695     conversation = conversation_new(pinfo->num,  &pinfo->src, &pinfo->dst, pinfo->ptype,
2696         pinfo->srcport, pinfo->destport, 0);
2697     conversation_add_proto_data(conversation, my_proto, (void *)data_ptr);
2698 }
2699
2700 /* at this point the conversation data is ready */
2701
2702 /***************** in the protocol register routine *****************/
2703
2704 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2705
2706
2707 2.2.4 An example conversation code that starts at a specific frame number.
2708
2709 Sometimes a dissector has determined that a new conversation is needed that
2710 starts at a specific frame number, when a capture session encompasses multiple
2711 conversation that reuse the same src/dest ip/port pairs. You can use the
2712 conversation->setup_frame returned by find_conversation with
2713 pinfo->num to determine whether or not there already exists a conversation
2714 that starts at the specific frame number.
2715
2716 /* in the dissector routine */
2717
2718     conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
2719         pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2720     if (conversation == NULL || (conversation->setup_frame != pinfo->num)) {
2721         /* It's not part of any conversation or the returned
2722          * conversation->setup_frame doesn't match the current frame
2723          * create a new one.
2724          */
2725         conversation = conversation_new(pinfo->num, &pinfo->src,
2726             &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
2727             NULL, 0);
2728     }
2729
2730
2731 2.2.5 The example conversation code using conversation index field.
2732
2733 Sometimes the conversation isn't enough to define a unique data storage
2734 value for the network traffic.  For example if you are storing information
2735 about requests carried in a conversation, the request may have an
2736 identifier that is used to  define the request. In this case the
2737 conversation and the identifier are required to find the data storage
2738 pointer.  You can use the conversation data structure index value to
2739 uniquely define the conversation.
2740
2741 See packet-afs.c for an example of how to use the conversation index.  In
2742 this dissector multiple requests are sent in the same conversation.  To store
2743 information for each request the dissector has an internal hash table based
2744 upon the conversation index and values inside the request packets.
2745
2746
2747     /* in the dissector routine */
2748
2749     /* to find a request value, first lookup conversation to get index */
2750     /* then used the conversation index, and request data to find data */
2751     /* in the local hash table */
2752
2753     conversation = find_or_create_conversation(pinfo);
2754
2755     request_key.conversation = conversation->index;
2756     request_key.service = pntoh16(&rxh->serviceId);
2757     request_key.callnumber = pntoh32(&rxh->callNumber);
2758
2759     request_val = (struct afs_request_val *)g_hash_table_lookup(
2760         afs_request_hash, &request_key);
2761
2762     /* only allocate a new hash element when it's a request */
2763     opcode = 0;
2764     if (!request_val && !reply)
2765     {
2766         new_request_key = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_key));
2767         *new_request_key = request_key;
2768
2769         request_val = wmem_alloc(wmem_file_scope(), sizeof(struct afs_request_val));
2770         request_val -> opcode = pntoh32(&afsh->opcode);
2771         opcode = request_val->opcode;
2772
2773         g_hash_table_insert(afs_request_hash, new_request_key,
2774             request_val);
2775     }
2776
2777
2778
2779 2.3 Dynamic conversation dissector registration.
2780
2781
2782 NOTE:   This sections assumes that all information is available to
2783     create a complete conversation, source port/address and
2784     destination port/address.  If either the destination port or
2785     address is known, see section 2.4 Dynamic server port dissector
2786     registration.
2787
2788 For protocols that negotiate a secondary port connection, for example
2789 packet-msproxy.c, a conversation can install a dissector to handle
2790 the secondary protocol dissection.  After the conversation is created
2791 for the negotiated ports use the conversation_set_dissector to define
2792 the dissection routine.
2793 Before we create these conversations or assign a dissector to them we should
2794 first check that the conversation does not already exist and if it exists
2795 whether it is registered to our protocol or not.
2796 We should do this because it is uncommon but it does happen that multiple
2797 different protocols can use the same socketpair during different stages of
2798 an application cycle. By keeping track of the frame number a conversation
2799 was started in wireshark can still tell these different protocols apart.
2800
2801 The second argument to conversation_set_dissector is a dissector handle,
2802 which is created with a call to create_dissector_handle or
2803 register_dissector.
2804
2805 create_dissector_handle takes as arguments a pointer to the dissector
2806 function and a protocol ID as returned by proto_register_protocol;
2807 register_dissector takes as arguments a string giving a name for the
2808 dissector, a pointer to the dissector function, and a protocol ID.
2809
2810 The protocol ID is the ID for the protocol dissected by the function.
2811 The function will not be called if the protocol has been disabled by the
2812 user; instead, the data for the protocol will be dissected as raw data.
2813
2814 An example -
2815
2816 /* the handle for the dynamic dissector *
2817 static dissector_handle_t sub_dissector_handle;
2818
2819 /* prototype for the dynamic dissector */
2820 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo,
2821                 proto_tree *tree);
2822
2823 /* in the main protocol dissector, where the next dissector is setup */
2824
2825 /* if conversation has a data field, create it and load structure */
2826
2827 /* First check if a conversation already exists for this
2828     socketpair
2829 */
2830     conversation = find_conversation(pinfo->num,
2831                 &pinfo->src, &pinfo->dst, protocol,
2832                 src_port, dst_port,  0);
2833
2834 /* If there is no such conversation, or if there is one but for
2835    someone else's protocol then we just create a new conversation
2836    and assign our protocol to it.
2837 */
2838     if ( (conversation == NULL) ||
2839          (conversation->dissector_handle != sub_dissector_handle) ) {
2840         new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2841         new_conv_info->data1 = value1;
2842
2843 /* create the conversation for the dynamic port */
2844             conversation = conversation_new(pinfo->num,
2845             &pinfo->src, &pinfo->dst, protocol,
2846                 src_port, dst_port, new_conv_info, 0);
2847
2848 /* set the dissector for the new conversation */
2849             conversation_set_dissector(conversation, sub_dissector_handle);
2850     }
2851     ...
2852
2853 void
2854 proto_register_PROTOABBREV(void)
2855 {
2856     ...
2857
2858     sub_dissector_handle = create_dissector_handle(sub_dissector,
2859         proto);
2860
2861     ...
2862 }
2863
2864 2.4 Dynamic server port dissector registration.
2865
2866 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
2867 conversation with only one port and address set, this isn't a
2868 requirement.  Either the second port or the second address can be set
2869 when the conversation is created.
2870
2871 For protocols that define a server address and port for a secondary
2872 protocol, a conversation can be used to link a protocol dissector to
2873 the server port and address.  The key is to create the new
2874 conversation with the second address and port set to the "accept
2875 any" values.
2876
2877 Some server applications can use the same port for different protocols during
2878 different stages of a transaction. For example it might initially use SNMP
2879 to perform some discovery and later switch to use TFTP using the same port.
2880 In order to handle this properly we must first check whether such a
2881 conversation already exists or not and if it exists we also check whether the
2882 registered dissector_handle for that conversation is "our" dissector or not.
2883 If not we create a new conversation on top of the previous one and set this new
2884 conversation to use our protocol.
2885 Since wireshark keeps track of the frame number where a conversation started
2886 wireshark will still be able to keep the packets apart even though they do use
2887 the same socketpair.
2888         (See packet-tftp.c and packet-snmp.c for examples of this)
2889
2890 There are two support routines that will allow the second port and/or
2891 address to be set later.
2892
2893 conversation_set_port2( conversation_t *conv, guint32 port);
2894 conversation_set_addr2( conversation_t *conv, address addr);
2895
2896 These routines will change the second address or port for the
2897 conversation.  So, the server port conversation will be converted into a
2898 more complete conversation definition.  Don't use these routines if you
2899 want to create a conversation between the server and client and retain the
2900 server port definition, you must create a new conversation.
2901
2902
2903 An example -
2904
2905 /* the handle for the dynamic dissector *
2906 static dissector_handle_t sub_dissector_handle;
2907
2908     ...
2909
2910 /* in the main protocol dissector, where the next dissector is setup */
2911
2912 /* if conversation has a data field, create it and load structure */
2913
2914     new_conv_info = wmem_alloc(wmem_file_scope(), sizeof(struct _new_conv_info));
2915     new_conv_info->data1 = value1;
2916
2917 /* create the conversation for the dynamic server address and port      */
2918 /* NOTE: The second address and port values don't matter because the    */
2919 /* NO_ADDR2 and NO_PORT2 options are set.                               */
2920
2921 /* First check if a conversation already exists for this
2922     IP/protocol/port
2923 */
2924     conversation = find_conversation(pinfo->num,
2925                 &server_src_addr, 0, protocol,
2926                 server_src_port, 0, NO_ADDR2 | NO_PORT_B);
2927 /* If there is no such conversation, or if there is one but for
2928    someone else's protocol then we just create a new conversation
2929    and assign our protocol to it.
2930 */
2931     if ( (conversation == NULL) ||
2932          (conversation->dissector_handle != sub_dissector_handle) ) {
2933         conversation = conversation_new(pinfo->num,
2934         &server_src_addr, 0, protocol,
2935         server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
2936
2937 /* set the dissector for the new conversation */
2938         conversation_set_dissector(conversation, sub_dissector_handle);
2939     }
2940
2941 2.5 Per-packet information.
2942
2943 Information can be stored for each data packet that is processed by the
2944 dissector.  The information is added with the p_add_proto_data function and
2945 retrieved with the p_get_proto_data function.  The data pointers passed into
2946 the p_add_proto_data are not managed by the proto_data routines, however the
2947 data pointer memory scope must match that of the scope parameter.
2948 The two most common use cases for p_add_proto_data/p_get_proto_data are for
2949 persistent data about the packet for the lifetime of the capture (file scope)
2950 and to exchange data between dissectors across a single packet (packet scope).
2951 It is also used to provide packet data for Decode As dialog (packet scope).
2952
2953 These functions are delcared in <epan/proto_data.h>.
2954
2955 void
2956 p_add_proto_data(wmem_allocator_t *scope, packet_info *pinfo, int proto, guint32 key, void *proto_data)
2957 void *
2958 p_get_proto_data(wmem_allocator_t *scope, packet_info *pinfo, int proto, guint32 key)
2959
2960 Where:
2961     scope      - Lifetime of the data to be stored, typically wmem_file_scope()
2962                  or pinfo->pool (packet scope).  Must match scope of data
2963                  allocated.
2964     pinfo      - The packet info pointer.
2965     proto      - Protocol id returned by the proto_register_protocol call
2966                  during initialization
2967     key        - key associated with 'proto_data'
2968     proto_data - pointer to the dissector data.
2969
2970
2971 2.6 User Preferences.
2972
2973 If the dissector has user options, there is support for adding these preferences
2974 to a configuration dialog.
2975
2976 You must register the module with the preferences routine with -
2977
2978        module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
2979        or
2980        module_t *prefs_register_protocol_subtree(const char *subtree, int id,
2981               void (*apply_cb)(void));
2982
2983
2984 Where: proto_id   - the value returned by "proto_register_protocol()" when
2985                     the protocol was registered.
2986        apply_cb   - Callback routine that is called when preferences are
2987                     applied. It may be NULL, which inhibits the callback.
2988        subtree    - grouping preferences tree node name (several protocols can
2989                     be grouped under one preferences subtree)
2990
2991 Then you can register the fields that can be configured by the user with these
2992 routines -
2993
2994     /* Register a preference with an unsigned integral value. */
2995     void prefs_register_uint_preference(module_t *module, const char *name,
2996         const char *title, const char *description, guint base, guint *var);
2997
2998     /* Register a preference with an Boolean value. */
2999     void prefs_register_bool_preference(module_t *module, const char *name,
3000         const char *title, const char *description, gboolean *var);
3001
3002     /* Register a preference with an enumerated value. */
3003     void prefs_register_enum_preference(module_t *module, const char *name,
3004         const char *title, const char *description, gint *var,
3005         const enum_val_t *enumvals, gboolean radio_buttons)
3006
3007     /* Register a preference with a character-string value. */
3008     void prefs_register_string_preference(module_t *module, const char *name,
3009         const char *title, const char *description, char **var)
3010
3011     /* Register a preference with a file name (string) value.
3012     * File name preferences are basically like string preferences
3013     * except that the GUI gives the user the ability to browse for the
3014     * file.
3015     */
3016     void prefs_register_filename_preference(module_t *module, const char *name,
3017         const char *title, const char *description, char **var)
3018
3019     /* Register a preference with a range of unsigned integers (e.g.,
3020      * "1-20,30-40").
3021      */
3022     void prefs_register_range_preference(module_t *module, const char *name,
3023         const char *title, const char *description, range_t *var,
3024         guint32 max_value)
3025
3026 Where: module - Returned by the prefs_register_protocol routine
3027      name     - This is appended to the name of the protocol, with a
3028             "." between them, to construct a name that identifies
3029             the field in the preference file; the name itself
3030             should not include the protocol name, as the name in
3031             the preference file will already have it. Make sure that
3032             only lower-case ASCII letters, numbers, underscores and
3033             dots appear in the preference name.
3034      title    - Field title in the preferences dialog
3035      description - Comments added to the preference file above the
3036                preference value and shown as tooltip in the GUI, or NULL
3037      var      - pointer to the storage location that is updated when the
3038             field is changed in the preference dialog box.  Note that
3039             with string preferences the given pointer is overwritten
3040             with a pointer to a new copy of the string during the
3041             preference registration.  The passed-in string may be
3042             freed, but you must keep another pointer to the string
3043             in order to free it.
3044      base      - Base that the unsigned integer is expected to be in,
3045             see strtoul(3).
3046      enumvals - an array of enum_val_t structures.  This must be
3047             NULL-terminated; the members of that structure are:
3048
3049             a short name, to be used with the "-o" flag - it
3050             should not contain spaces or upper-case letters,
3051             so that it's easier to put in a command line;
3052
3053             a description, which is used in the GUI (and
3054             which, for compatibility reasons, is currently
3055             what's written to the preferences file) - it can
3056             contain spaces, capital letters, punctuation,
3057             etc.;
3058
3059             the numerical value corresponding to that name
3060             and description
3061      radio_buttons - TRUE if the field is to be displayed in the
3062              preferences dialog as a set of radio buttons,
3063              FALSE if it is to be displayed as an option
3064              menu
3065      max_value - The maximum allowed value for a range (0 is the minimum).
3066
3067 These functions are declared in <epan/prefs.h>.
3068
3069 An example from packet-rtpproxy.c -
3070
3071     proto_rtpproxy = proto_register_protocol ( "Sippy RTPproxy Protocol", "RTPproxy", "rtpproxy");
3072
3073     ...
3074
3075     rtpproxy_module = prefs_register_protocol(proto_rtpproxy, proto_reg_handoff_rtpproxy);
3076
3077     prefs_register_bool_preference(rtpproxy_module, "establish_conversation",
3078                                  "Establish Media Conversation",
3079                                  "Specifies that RTP/RTCP/T.38/MSRP/etc streams are decoded based "
3080                                  "upon port numbers found in RTPproxy answers",
3081                                  &rtpproxy_establish_conversation);
3082
3083     prefs_register_uint_preference(rtpproxy_module, "reply.timeout",
3084                                  "RTPproxy reply timeout", /* Title */
3085                                  "Maximum timeout value in waiting for reply from RTPProxy (in milliseconds).", /* Descr */
3086                                  10,
3087                                  &rtpproxy_timeout);
3088
3089 This will create preferences "rtpproxy.establish_conversation" and
3090 "rtpproxy.reply.timeout", the first of which is an Boolean and the
3091 second of which is a unsigned integer.
3092
3093 Note that a warning will pop up if you've saved such preference to the
3094 preference file and you subsequently take the code out. The way to make
3095 a preference obsolete is to register it as such:
3096
3097 /* Register a preference that used to be supported but no longer is. */
3098     void prefs_register_obsolete_preference(module_t *module,
3099         const char *name);
3100
3101 2.7 Reassembly/desegmentation for protocols running atop TCP.
3102
3103 There are two main ways of reassembling a Protocol Data Unit (PDU) which
3104 spans across multiple TCP segments.  The first approach is simpler, but
3105 assumes you are running atop of TCP when this occurs (but your dissector
3106 might run atop of UDP, too, for example), and that your PDUs consist of a
3107 fixed amount of data that includes enough information to determine the PDU
3108 length, possibly followed by additional data.  The second method is more
3109 generic but requires more code and is less efficient.
3110
3111 2.7.1 Using tcp_dissect_pdus().
3112
3113 For the first method, you register two different dissection methods, one
3114 for the TCP case, and one for the other cases.  It is a good idea to
3115 also have a dissect_PROTO_common function which will parse the generic
3116 content that you can find in all PDUs which is called from
3117 dissect_PROTO_tcp when the reassembly is complete and from
3118 dissect_PROTO_udp (or dissect_PROTO_other).
3119
3120 To register the distinct dissector functions, consider the following
3121 example, stolen from packet-dns.c:
3122
3123     #include "packet-tcp.h"
3124
3125     dissector_handle_t hartip_tcp_handle;
3126     dissector_handle_t hartip_udp_handle;
3127
3128     hartip_tcp_handle = create_dissector_handle(dissect_hartip_tcp, proto_hartip);
3129     hartip_udp_handle = create_dissector_handle(dissect_hartip_udp, proto_hartip);
3130
3131     dissector_add_uint("udp.port", HARTIP_PORT, hartip_udp_handle);
3132     dissector_add_uint_with_preference("tcp.port", HARTIP_PORT, hartip_tcp_handle);
3133
3134 The dissect_hartip_udp function does very little work and calls
3135 dissect_hartip_common, while dissect_hartip_tcp calls tcp_dissect_pdus with a
3136 reference to a callback which will be called with reassembled data:
3137
3138     static int
3139     dissect_hartip_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
3140                    void *data)
3141     {
3142         if (!tvb_bytes_exist(tvb, 0, HARTIP_HEADER_LENGTH))
3143             return 0;
3144
3145         tcp_dissect_pdus(tvb, pinfo, tree, hartip_desegment, HARTIP_HEADER_LENGTH,
3146                    get_dissect_hartip_len, dissect_hartip_pdu, data);
3147         return tvb_reported_length(tvb);
3148     }
3149
3150 (The dissect_hartip_pdu function acts similarly to dissect_hartip_udp.)
3151 The arguments to tcp_dissect_pdus are:
3152
3153     the tvbuff pointer, packet_info pointer, and proto_tree pointer
3154     passed to the dissector;
3155
3156     a gboolean flag indicating whether desegmentation is enabled for
3157     your protocol;
3158
3159     the number of bytes of PDU data required to determine the length
3160     of the PDU;
3161
3162     a routine that takes as arguments a packet_info pointer, a tvbuff
3163     pointer and an offset value representing the offset into the tvbuff
3164     at which a PDU begins, and a void pointer for user data, and should
3165     return the total length of the PDU in bytes (or 0 if more bytes are
3166     needed to determine the message length).
3167     The routine must not throw exceptions (it is guaranteed that the
3168     number of bytes specified by the previous argument to
3169     tcp_dissect_pdus is available, but more data might not be available,
3170     so don't refer to any data past that);
3171
3172     a new_dissector_t routine to dissect the pdu that's passed a tvbuff
3173     pointer, packet_info pointer, proto_tree pointer and a void pointer for
3174     user data, with the tvbuff containing a possibly-reassembled PDU. (The
3175     "reported_length" of the tvbuff will be the length of the PDU);
3176
3177     a void pointer to user data that is passed to the length-determining
3178     routine, and the dissector routine referenced in the previous parameter.
3179
3180 2.7.2 Modifying the pinfo struct.
3181
3182 The second reassembly mode is preferred when the dissector cannot determine
3183 how many bytes it will need to read in order to determine the size of a PDU.
3184 It may also be useful if your dissector needs to support reassembly from
3185 protocols other than TCP.
3186
3187 Your dissect_PROTO will initially be passed a tvbuff containing the payload of
3188 the first packet. It should dissect as much data as it can, noting that it may
3189 contain more than one complete PDU. If the end of the provided tvbuff coincides
3190 with the end of a PDU then all is well and your dissector can just return as
3191 normal. (If it is a new-style dissector, it should return the number of bytes
3192 successfully processed.)
3193
3194 If the dissector discovers that the end of the tvbuff does /not/ coincide with
3195 the end of a PDU, (ie, there is half of a PDU at the end of the tvbuff), it can
3196 indicate this to the parent dissector, by updating the pinfo struct. The
3197 desegment_offset field is the offset in the tvbuff at which the dissector will
3198 continue processing when next called.  The desegment_len field should contain
3199 the estimated number of additional bytes required for completing the PDU.  Next
3200 time your dissect_PROTO is called, it will be passed a tvbuff composed of the
3201 end of the data from the previous tvbuff together with desegment_len more bytes.
3202
3203 If the dissector cannot tell how many more bytes it will need, it should set
3204 desegment_len=DESEGMENT_ONE_MORE_SEGMENT; it will then be called again as soon
3205 as any more data becomes available. Dissectors should set the desegment_len to a
3206 reasonable value when possible rather than always setting
3207 DESEGMENT_ONE_MORE_SEGMENT as it will generally be more efficient. Also, you
3208 *must not* set desegment_len=1 in this case, in the hope that you can change
3209 your mind later: once you return a positive value from desegment_len, your PDU
3210 boundary is set in stone.
3211
3212 static hf_register_info hf[] = {
3213     {&hf_cstring,
3214      {"C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0,
3215       NULL, HFILL}
3216      }
3217    };
3218
3219 /**
3220 *   Dissect a buffer containing ASCII C strings.
3221 *
3222 *   @param  tvb     The buffer to dissect.
3223 *   @param  pinfo   Packet Info.
3224 *   @param  tree    The protocol tree.
3225 *   @param  data    Optional data parameter given by parent dissector.
3226 **/
3227 static int dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void *data _U_)
3228 {
3229     guint offset = 0;
3230     while(offset < tvb_reported_length(tvb)) {
3231         gint available = tvb_reported_length_remaining(tvb, offset);
3232         gint len = tvb_strnlen(tvb, offset, available);
3233
3234         if( -1 == len ) {
3235             /* we ran out of data: ask for more */
3236             pinfo->desegment_offset = offset;
3237             pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
3238             return (offset + available);
3239         }
3240
3241         col_set_str(pinfo->cinfo, COL_INFO, "C String");
3242
3243         len += 1; /* Add one for the '\0' */
3244
3245         if (tree) {
3246             proto_tree_add_item(tree, hf_cstring, tvb, offset, len,
3247                 ENC_ASCII|ENC_NA);
3248         }
3249         offset += (guint)len;
3250     }
3251
3252     /* if we get here, then the end of the tvb coincided with the end of a
3253        string. Happy days. */
3254     return tvb_captured_length(tvb);
3255 }
3256
3257 This simple dissector will repeatedly return DESEGMENT_ONE_MORE_SEGMENT
3258 requesting more data until the tvbuff contains a complete C string. The C string
3259 will then be added to the protocol tree. Note that there may be more
3260 than one complete C string in the tvbuff, so the dissection is done in a
3261 loop.
3262
3263 2.8 Using udp_dissect_pdus().
3264
3265 As noted in section 2.7.1, TCP has an API to dissect its PDU that can handle
3266 a PDU spread across multiple packets or multiple PDUs spread across a single
3267 packet.  This section describes a similar mechanism for UDP, but is only
3268 applicable for one or more PDUs in a single packet. If a protocol runs on top
3269 of TCP as well as UDP, a common PDU dissection function can be created for both.
3270
3271 To register the distinct dissector functions, consider the following
3272 example using UDP and TCP dissection, stolen from packet-dnp.c:
3273
3274     #include "packet-tcp.h"
3275     #include "packet-udp.h"
3276
3277     dissector_handle_t dnp3_tcp_handle;
3278     dissector_handle_t dnp3_udp_handle;
3279
3280     dnp3_tcp_handle = create_dissector_handle(dissect_dnp3_tcp, proto_dnp3);
3281     dnp3_udp_handle = create_dissector_handle(dissect_dnp3_udp, proto_dnp3);
3282
3283     dissector_add_uint("tcp.port", TCP_PORT_DNP, dnp3_tcp_handle);
3284     dissector_add_uint("udp.port", UDP_PORT_DNP, dnp3_udp_handle);
3285
3286 Both dissect_dnp3_tcp and dissect_dnp3_udp call tcp_dissect_pdus and
3287 udp_dissect_pdus respectively, with a reference to the same callbacks which
3288 are called to handle PDU data.
3289
3290     static int
3291     dissect_dnp3_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
3292     {
3293         return udp_dissect_pdus(tvb, pinfo, tree, DNP_HDR_LEN, dnp3_udp_check_header,
3294                    get_dnp3_message_len, dissect_dnp3_message, data);
3295     }
3296
3297     static int
3298     dissect_dnp3_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
3299     {
3300         if (!check_dnp3_header(tvb, FALSE)) {
3301             return 0;
3302         }
3303
3304         tcp_dissect_pdus(tvb, pinfo, tree, TRUE, DNP_HDR_LEN,
3305                    get_dnp3_message_len, dissect_dnp3_message, data);
3306
3307         return tvb_captured_length(tvb);
3308     }
3309
3310 (udp_dissect_pdus has an option of a heuristic check function within it while
3311 tcp_dissect_pdus does not, so it's done outside)
3312
3313 The arguments to udp_dissect_pdus are:
3314
3315     the tvbuff pointer, packet_info pointer, and proto_tree pointer
3316     passed to the dissector;
3317
3318     the number of bytes of PDU data required to determine the length
3319     of the PDU;
3320
3321     an optional routine (passing NULL is okay) that takes as arguments a
3322     packet_info pointer, a tvbuff pointer and an offset value representing the
3323     offset into the tvbuff at which a PDU begins, and a void pointer for user
3324     data, and should return TRUE if the packet belongs to the dissector.
3325     The routine must not throw exceptions (it is guaranteed that the
3326     number of bytes specified by the previous argument to
3327     udp_dissect_pdus is available, but more data might not be available,
3328     so don't refer to any data past that);
3329
3330     a routine that takes as arguments a packet_info pointer, a tvbuff
3331     pointer and an offset value representing the offset into the tvbuff
3332     at which a PDU begins, and a void pointer for user data, and should
3333     return the total length of the PDU in bytes. If return value is 0,
3334     it's treated the same as a failed heuristic.
3335     The routine must not throw exceptions (it is guaranteed that the
3336     number of bytes specified by the previous argument to
3337     tcp_dissect_pdus is available, but more data might not be available,
3338     so don't refer to any data past that);
3339
3340     a new_dissector_t routine to dissect the pdu that's passed a tvbuff
3341     pointer, packet_info pointer, proto_tree pointer and a void pointer for
3342     user data, with the tvbuff containing a possibly-reassembled PDU. (The
3343     "reported_length" of the tvbuff will be the length of the PDU);
3344
3345     a void pointer to user data that is passed to the length-determining
3346     routine, and the dissector routine referenced in the previous parameter.
3347
3348 2.9 PINOs (Protocols in name only)
3349
3350 For the typical dissector there is a 1-1 relationship between it and it's
3351 protocol.  However, there are times when a protocol needs multiple "names"
3352 because it has multiple dissection functions going into the same dissector
3353 table. The muliple names removes confusion when picking dissection through
3354 Decode As functionality.
3355
3356 Once the "main" protocol name has been created through proto_register_protocol,
3357 additional "pinos" can be created with proto_register_protocol_in_name_only.
3358 These pinos have all of the naming conventions of a protocol, but are stored
3359 separately as to remove confusion from real protocols.  "pinos" the main
3360 protocol's properties for things like enable/disable.  i.e.  If the "main"
3361 protocol has been disabled, all of its pinos will be disabled as well.
3362 Pinos should not have any fields registered with them or heuristic tables
3363 associated with them.
3364
3365 Another use case for pinos is when a protocol contains a TLV design and it
3366 wants to create a dissector table to handle dissection of the "V". Dissector
3367 tables require a "protocol", but the dissection functions for that table
3368 typically aren't a protocol.  In this case proto_register_protocol_in_name_only
3369 creates the necessary placeholder for the dissector table.  In addition, because
3370 a dissector table exists, "V"s of the TLVs can be dissected outside of the
3371 original dissector file.
3372
3373 2.10 Creating Decode As functionality.
3374
3375 While the Decode As functionality is available through the GUI, the underlying
3376 functionality is controlled by dissectors themselves. To create Decode As
3377 functionality for a dissector, two things are required:
3378     1. A dissector table
3379     2. A series of structures to assist the GUI in how to present the dissector
3380        table data.
3381
3382 Consider the following example using IP dissection, stolen from packet-ip.c:
3383
3384     static build_valid_func ip_da_build_value[1] = {ip_value};
3385     static decode_as_value_t ip_da_values = {ip_prompt, 1, ip_da_build_value};
3386     static decode_as_t ip_da = {"ip", "Network", "ip.proto", 1, 0, &ip_da_values, NULL, NULL,
3387                               decode_as_default_populate_list, decode_as_default_reset, decode_as_default_change, NULL};
3388     ...
3389     ip_dissector_table = register_dissector_table("ip.proto", "IP protocol", ip_proto, FT_UINT8, BASE_DEC);
3390     ...
3391     register_decode_as(&ip_da);
3392
3393 ip_da_build_value contains all of the function pointers (typically just 1) that
3394 can be used to retrieve the value(s) that go into the dissector table.  This is
3395 usually data saved by the dissector during packet dissector with an API like
3396 p_add_proto_data and retrieved in the "value" function with p_get_proto_data.
3397
3398 ip_da_values contains all of the function pointers (typically just 1) that
3399 provide the text explaining the name and use of the value field that will
3400 be passed to the dissector table to change the dissection output.
3401
3402 ip_da pulls everything together including the dissector (protocol) name, the
3403 "layer type" of the dissector, the dissector table name, the function pointer
3404 values as well as handlers for populating, applying and reseting the changes
3405 to the dissector table through Decode As GUI functionality.  For dissector
3406 tables that are an integer or string type, the provided "default" handling
3407 functions shown in the example should suffice.
3408
3409 All entries into a dissector table that use Decode As must have a unique
3410 protocol ID. If a protocol wants multiple entries into a dissector table,
3411 a pino should be used (see section 2.9)
3412
3413 2.11 ptvcursors.
3414
3415 The ptvcursor API allows a simpler approach to writing dissectors for
3416 simple protocols. The ptvcursor API works best for protocols whose fields
3417 are static and whose format does not depend on the value of other fields.
3418 However, even if only a portion of your protocol is statically defined,
3419 then that portion could make use of ptvcursors.
3420
3421 The ptvcursor API lets you extract data from a tvbuff, and add it to a
3422 protocol tree in one step. It also keeps track of the position in the
3423 tvbuff so that you can extract data again without having to compute any
3424 offsets --- hence the "cursor" name of the API.
3425
3426 The three steps for a simple protocol are:
3427     1. Create a new ptvcursor with ptvcursor_new()
3428     2. Add fields with multiple calls of ptvcursor_add()
3429     3. Delete the ptvcursor with ptvcursor_free()
3430
3431 ptvcursor offers the possibility to add subtrees in the tree as well. It can be
3432 done in very simple steps :
3433     1. Create a new subtree with ptvcursor_push_subtree(). The old subtree is
3434        pushed in a stack and the new subtree will be used by ptvcursor.
3435     2. Add fields with multiple calls of ptvcursor_add(). The fields will be
3436        added in the new subtree created at the previous step.
3437     3. Pop the previous subtree with ptvcursor_pop_subtree(). The previous
3438        subtree is again used by ptvcursor.
3439 Note that at the end of the parsing of a packet you must have popped each
3440 subtree you pushed. If it's not the case, the dissector will generate an error.
3441
3442 To use the ptvcursor API, include the "ptvcursor.h" file. The PGM dissector
3443 is an example of how to use it. You don't need to look at it as a guide;
3444 instead, the API description here should be good enough.
3445
3446 2.11.1 ptvcursor API.
3447
3448 ptvcursor_t*
3449 ptvcursor_new(proto_tree* tree, tvbuff_t* tvb, gint offset)
3450     This creates a new ptvcursor_t object for iterating over a tvbuff.
3451 You must call this and use this ptvcursor_t object so you can use the
3452 ptvcursor API.
3453
3454 proto_item*
3455 ptvcursor_add(ptvcursor_t* ptvc, int hf, gint length, const guint encoding)
3456     This will extract 'length' bytes from the tvbuff and place it in
3457 the proto_tree as field 'hf', which is a registered header_field. The
3458 pointer to the proto_item that is created is passed back to you. Internally,
3459 the ptvcursor advances its cursor so the next call to ptvcursor_add
3460 starts where this call finished. The 'encoding' parameter is relevant for
3461 certain type of fields (See above under proto_tree_add_item()).
3462
3463 proto_item*
3464 ptvcursor_add_no_advance(ptvcursor_t* ptvc, int hf, gint length, const guint encoding)
3465     Like ptvcursor_add, but does not advance the internal cursor.
3466
3467 void
3468 ptvcursor_advance(ptvcursor_t* ptvc, gint length)
3469     Advances the internal cursor without adding anything to the proto_tree.
3470
3471 void
3472 ptvcursor_free(ptvcursor_t* ptvc)
3473     Frees the memory associated with the ptvcursor. You must call this
3474 after your dissection with the ptvcursor API is completed.
3475
3476
3477 proto_tree*
3478 ptvcursor_push_subtree(ptvcursor_t* ptvc, proto_item* it, gint ett_subtree)
3479     Pushes the current subtree in the tree stack of the cursor, creates a new
3480 one and sets this one as the working tree.
3481
3482 void
3483 ptvcursor_pop_subtree(ptvcursor_t* ptvc);
3484     Pops a subtree in the tree stack of the cursor
3485
3486 proto_tree*
3487 ptvcursor_add_with_subtree(ptvcursor_t* ptvc, int hfindex, gint length,
3488                             const guint encoding, gint ett_subtree);
3489     Adds an item to the tree and creates a subtree.
3490 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3491 In this case, at the next pop, the item length will be equal to the advancement
3492 of the cursor since the creation of the subtree.
3493
3494 proto_tree*
3495 ptvcursor_add_text_with_subtree(ptvcursor_t* ptvc, gint length,
3496                                 gint ett_subtree, const char* format, ...);
3497     Add a text node to the tree and create a subtree.
3498 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3499 In this case, at the next pop, the item length will be equal to the advancement
3500 of the cursor since the creation of the subtree.
3501
3502 2.11.2 Miscellaneous functions.
3503
3504 tvbuff_t*
3505 ptvcursor_tvbuff(ptvcursor_t* ptvc)
3506     Returns the tvbuff associated with the ptvcursor.
3507
3508 gint
3509 ptvcursor_current_offset(ptvcursor_t* ptvc)
3510     Returns the current offset.
3511
3512 proto_tree*
3513 ptvcursor_tree(ptvcursor_t* ptvc)
3514     Returns the proto_tree associated with the ptvcursor.
3515
3516 void
3517 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree)
3518     Sets a new proto_tree for the ptvcursor.
3519
3520 proto_tree*
3521 ptvcursor_set_subtree(ptvcursor_t* ptvc, proto_item* it, gint ett_subtree);
3522     Creates a subtree and adds it to the cursor as the working tree but does
3523 not save the old working tree.
3524
3525 2.12 Optimizations
3526
3527 A protocol dissector may be called in 2 different ways - with, or
3528 without a non-null "tree" argument.
3529
3530 If the proto_tree argument is null, Wireshark does not need to use
3531 the protocol tree information from your dissector, and therefore is
3532 passing the dissector a null "tree" argument so that it doesn't
3533 need to do work necessary to build the protocol tree.
3534
3535 In the interest of speed, if "tree" is NULL, avoid building a
3536 protocol tree and adding stuff to it, or even looking at any packet
3537 data needed only if you're building the protocol tree, if possible.
3538
3539 Note, however, that you must fill in column information, create
3540 conversations, reassemble packets, do calls to "expert" functions,
3541 build any other persistent state needed for dissection, and call
3542 subdissectors regardless of whether "tree" is NULL or not.
3543
3544 This might be inconvenient to do without doing most of the
3545 dissection work; the routines for adding items to the protocol tree
3546 can be passed a null protocol tree pointer, in which case they'll
3547 return a null item pointer, and "proto_item_add_subtree()" returns
3548 a null tree pointer if passed a null item pointer, so, if you're
3549 careful not to dereference any null tree or item pointers, you can
3550 accomplish this by doing all the dissection work.  This might not
3551 be as efficient as skipping that work if you're not building a
3552 protocol tree, but if the code would have a lot of tests whether
3553 "tree" is null if you skipped that work, you might still be better
3554 off just doing all that work regardless of whether "tree" is null
3555 or not.
3556
3557 Note also that there is no guarantee, the first time the dissector is
3558 called, whether "tree" will be null or not; your dissector must work
3559 correctly, building or updating whatever state information is
3560 necessary, in either case.
3561
3562 /*
3563  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
3564  *
3565  * Local variables:
3566  * c-basic-offset: 4
3567  * tab-width: 8
3568  * indent-tabs-mode: nil
3569  * End:
3570  *
3571  * vi: set shiftwidth=4 tabstop=8 expandtab:
3572  * :indentSize=4:tabSize=8:noTabs=true:
3573  */