Fix for bug 2612:
[obnox/wireshark/wip.git] / doc / README.developer
1 $Revision$
2 $Date$
3 $Author$
4
5 This file is a HOWTO for Wireshark developers. It describes how to start coding
6 a Wireshark protocol dissector and the use of some of the important functions
7 and variables.
8
9 This file is compiled to give in depth information on Wireshark.
10 It is by no means all inclusive and complete. Please feel free to send
11 remarks and patches to the developer mailing list.
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 http://www.wireshark.org) and in the INSTALL and README files of the sources
22 root dir.
23
24 0.1. General README files.
25
26 You'll find additional information in the following README files:
27
28 - README.capture - the capture engine internals
29 - README.design - Wireshark software design - incomplete
30 - README.developer - this file
31 - README.display_filter - Display Filter Engine
32 - README.idl2wrs - CORBA IDL converter
33 - README.packaging - how to distribute a software package containing WS
34 - README.regression - regression testing of WS and TS
35 - README.stats_tree - a tree statistics counting specific packets
36 - README.tapping - "tap" a dissector to get protocol specific events
37 - README.xml-output - how to work with the PDML exported output
38 - wiretap/README.developer - how to add additional capture file types to
39   Wiretap
40
41 0.2. Dissector related README files.
42
43 You'll find additional dissector related information in the following README
44 files:
45
46 - README.binarytrees - fast access to large data collections
47 - README.malloc - how to obtain "memory leak free" memory
48 - README.plugins - how to "pluginize" a dissector
49 - README.request_response_tracking - how to track req./resp. times and such
50
51 0.3 Contributors
52
53 James Coe <jammer[AT]cin.net>
54 Gilbert Ramirez <gram[AT]alumni.rice.edu>
55 Jeff Foster <jfoste[AT]woodward.com>
56 Olivier Abad <oabad[AT]cybercable.fr>
57 Laurent Deniel <laurent.deniel[AT]free.fr>
58 Gerald Combs <gerald[AT]wireshark.org>
59 Guy Harris <guy[AT]alum.mit.edu>
60 Ulf Lamping <ulf.lamping[AT]web.de>
61
62 1. Setting up your protocol dissector code.
63
64 This section provides skeleton code for a protocol dissector. It also explains
65 the basic functions needed to enter values in the traffic summary columns,
66 add to the protocol tree, and work with registered header fields.
67
68 1.1 Code style.
69
70 1.1.1 Portability.
71
72 Wireshark runs on many platforms, and can be compiled with a number of
73 different compilers; here are some rules for writing code that will work
74 on multiple platforms.
75
76 Don't use C++-style comments (comments beginning with "//" and running
77 to the end of the line); Wireshark's dissectors are written in C, and
78 thus run through C rather than C++ compilers, and not all C compilers
79 support C++-style comments (GCC does, but IBM's C compiler for AIX, for
80 example, doesn't do so by default).
81
82 Don't initialize variables in their declaration with non-constant
83 values. Not all compilers support this. E.g. don't use
84         guint32 i = somearray[2];
85 use
86         guint32 i;
87         i = somearray[2];
88 instead.
89
90 Don't use zero-length arrays; not all compilers support them.  If an
91 array would have no members, just leave it out.
92
93 Don't declare variables in the middle of executable code; not all C
94 compilers support that.  Variables should be declared outside a
95 function, or at the beginning of a function or compound statement.
96
97 Don't use anonymous unions; not all compilers support it. 
98 Example:
99 typedef struct foo {
100   guint32 foo;
101   union {
102     guint32 foo_l;
103     guint16 foo_s;
104   } u;  /* have a name here */
105 } foo_t;
106
107 Don't use "uchar", "u_char", "ushort", "u_short", "uint", "u_int",
108 "ulong", "u_long" or "boolean"; they aren't defined on all platforms.
109 If you want an 8-bit unsigned quantity, use "guint8"; if you want an
110 8-bit character value with the 8th bit not interpreted as a sign bit,
111 use "guchar"; if you want a 16-bit unsigned quantity, use "guint16";
112 if you want a 32-bit unsigned quantity, use "guint32"; and if you want
113 an "int-sized" unsigned quantity, use "guint"; if you want a boolean,
114 use "gboolean".  Use "%d", "%u", "%x", and "%o" to print those types;
115 don't use "%ld", "%lu", "%lx", or "%lo", as longs are 64 bits long on
116 many platforms, but "guint32" is 32 bits long.
117
118 Don't use "long" to mean "signed 32-bit integer", and don't use
119 "unsigned long" to mean "unsigned 32-bit integer"; "long"s are 64 bits
120 long on many platforms.  Use "gint32" for signed 32-bit integers and use
121 "guint32" for unsigned 32-bit integers.
122
123 Don't use "long" to mean "signed 64-bit integer" and don't use "unsigned
124 long" to mean "unsigned 64-bit integer"; "long"s are 32 bits long on
125 many other platforms.  Don't use "long long" or "unsigned long long",
126 either, as not all platforms support them; use "gint64" or "guint64",
127 which will be defined as the appropriate types for 64-bit signed and
128 unsigned integers.
129
130 When printing or displaying the values of 64-bit integral data types,
131 don't use "%lld", "%llu", "%llx", or "%llo" - not all platforms
132 support "%ll" for printing 64-bit integral data types.  Instead, for
133 GLib routines, and routines that use them, such as all the routines in
134 Wireshark that take format arguments, use G_GINT64_MODIFIER, for example:
135
136     proto_tree_add_text(tree, tvb, offset, 8,
137                         "Sequence Number: %" G_GINT64_MODIFIER "u",
138                         sequence_number);
139
140 When using standard C routines, such as printf and scanf, use
141 PRId64, PRIu64, PRIx64, PRIX64, and PRIo64, for example:
142
143    printf("Sequence Number: %" PRIu64 "\n", sequence_number);
144
145 When specifying an integral constant that doesn't fit in 32 bits, don't
146 use "LL" at the end of the constant - not all compilers use "LL" for
147 that.  Instead, put the constant in a call to the "G_GINT64_CONSTANT()"
148 macro, e.g.
149
150         G_GINT64_CONSTANT(11644473600U)
151
152 rather than
153
154         11644473600ULL
155
156 Don't use a label without a statement following it.  For example,
157 something such as
158
159         if (...) {
160
161                 ...
162
163         done:
164         }
165
166 will not work with all compilers - you have to do
167
168         if (...) {
169
170                 ...
171
172         done:
173                 ;
174         }
175
176 with some statement, even if it's a null statement, after the label.
177
178 Don't use "bzero()", "bcopy()", or "bcmp()"; instead, use the ANSI C
179 routines
180
181         "memset()" (with zero as the second argument, so that it sets
182         all the bytes to zero);
183
184         "memcpy()" or "memmove()" (note that the first and second
185         arguments to "memcpy()" are in the reverse order to the
186         arguments to "bcopy()"; note also that "bcopy()" is typically
187         guaranteed to work on overlapping memory regions, while
188         "memcpy()" isn't, so if you may be copying from one region to a
189         region that overlaps it, use "memmove()", not "memcpy()" - but
190         "memcpy()" might be faster as a result of not guaranteeing
191         correct operation on overlapping memory regions);
192
193         and "memcmp()" (note that "memcmp()" returns 0, 1, or -1, doing
194         an ordered comparison, rather than just returning 0 for "equal"
195         and 1 for "not equal", as "bcmp()" does).
196
197 Not all platforms necessarily have "bzero()"/"bcopy()"/"bcmp()", and
198 those that do might not declare them in the header file on which they're
199 declared on your platform.
200
201 Don't use "index()" or "rindex()"; instead, use the ANSI C equivalents,
202 "strchr()" and "strrchr()".  Not all platforms necessarily have
203 "index()" or "rindex()", and those that do might not declare them in the
204 header file on which they're declared on your platform.
205
206 Don't fetch data from packets by getting a pointer to data in the packet
207 with "tvb_get_ptr()", casting that pointer to a pointer to a structure,
208 and dereferencing that pointer.  That pointer won't necessarily be aligned
209 on the proper boundary, which can cause crashes on some platforms (even
210 if it doesn't crash on an x86-based PC); furthermore, the data in a
211 packet is not necessarily in the byte order of the machine on which
212 Wireshark is running.  Use the tvbuff routines to extract individual
213 items from the packet, or use "proto_tree_add_item()" and let it extract
214 the items for you.
215
216 Don't use structures that overlay packet data, or into which you copy
217 packet data; the C programming language does not guarantee any
218 particular alignment of fields within a structure, and even the
219 extensions that try to guarantee that are compiler-specific and not
220 necessarily supported by all compilers used to build Wireshark.  Using
221 bitfields in those structures is even worse; the order of bitfields
222 is not guaranteed.
223
224 Don't use "ntohs()", "ntohl()", "htons()", or "htonl()"; the header
225 files required to define or declare them differ between platforms, and
226 you might be able to get away with not including the appropriate header
227 file on your platform but that might not work on other platforms.
228 Instead, use "g_ntohs()", "g_ntohl()", "g_htons()", and "g_htonl()";
229 those are declared by <glib.h>, and you'll need to include that anyway,
230 as Wireshark header files that all dissectors must include use stuff from
231 <glib.h>.
232
233 Don't fetch a little-endian value using "tvb_get_ntohs() or
234 "tvb_get_ntohl()" and then using "g_ntohs()", "g_htons()", "g_ntohl()",
235 or "g_htonl()" on the resulting value - the g_ routines in question
236 convert between network byte order (big-endian) and *host* byte order,
237 not *little-endian* byte order; not all machines on which Wireshark runs
238 are little-endian, even though PCs are.  Fetch those values using
239 "tvb_get_letohs()" and "tvb_get_letohl()".
240
241 Don't put a comma after the last element of an enum - some compilers may
242 either warn about it (producing extra noise) or refuse to accept it.
243
244 Don't include <unistd.h> without protecting it with
245
246         #ifdef HAVE_UNISTD_H
247
248                 ...
249
250         #endif
251
252 and, if you're including it to get routines such as "open()", "close()",
253 "read()", and "write()" declared, also include <io.h> if present:
254
255         #ifdef HAVE_IO_H
256         #include <io.h>
257         #endif
258
259 in order to declare the Windows C library routines "_open()",
260 "_close()", "_read()", and "_write()".  Your file must include <glib.h>
261 - which many of the Wireshark header files include, so you might not have
262 to include it explicitly - in order to get "open()", "close()",
263 "read()", "write()", etc. mapped to "_open()", "_close()", "_read()",
264 "_write()", etc..
265
266 Do not use "open()", "rename()", "mkdir()", "stat()", "unlink()", "remove()",
267 "fopen()", "freopen()" directly.  Instead use "ws_open()", "ws_rename()",
268 "ws_mkdir()", "ws_stat()", "ws_unlink()", "ws_remove()", "ws_fopen()",
269 "ws_freopen()": these wrapper functions change the path and file name from
270 UTF8 to UTF16 on Windows allowing the functions to work correctly when the
271 path or file name contain non-ASCII characters.
272
273 When opening a file with "ws_fopen()", "ws_freopen()", or "ws_fdopen()", if
274 the file contains ASCII text, use "r", "w", "a", and so on as the open mode
275 - but if it contains binary data, use "rb", "wb", and so on.  On
276 Windows, if a file is opened in a text mode, writing a byte with the
277 value of octal 12 (newline) to the file causes two bytes, one with the
278 value octal 15 (carriage return) and one with the value octal 12, to be
279 written to the file, and causes bytes with the value octal 15 to be
280 discarded when reading the file (to translate between C's UNIX-style
281 lines that end with newline and Windows' DEC-style lines that end with
282 carriage return/line feed).
283
284 In addition, that also means that when opening or creating a binary
285 file, you must use "ws_open()" (with O_CREAT and possibly O_TRUNC if the
286 file is to be created if it doesn't exist), and OR in the O_BINARY flag.
287 That flag is not present on most, if not all, UNIX systems, so you must
288 also do
289
290         #ifndef O_BINARY
291         #define O_BINARY        0
292         #endif
293
294 to properly define it for UNIX (it's not necessary on UNIX).
295
296 Don't use forward declarations of static arrays without a specified size
297 in a fashion such as this:
298
299         static const value_string foo_vals[];
300
301                 ...
302
303         static const value_string foo_vals[] = {
304                 { 0,            "Red" },
305                 { 1,            "Green" },
306                 { 2,            "Blue" },
307                 { 0,            NULL }
308         };
309
310 as some compilers will reject the first of those statements.  Instead,
311 initialize the array at the point at which it's first declared, so that
312 the size is known.
313
314 Don't put a comma after the last tuple of an initializer of an array.
315
316 For #define names and enum member names, prefix the names with a tag so
317 as to avoid collisions with other names - this might be more of an issue
318 on Windows, as it appears to #define names such as DELETE and
319 OPTIONAL.
320
321 Don't use the "numbered argument" feature that many UNIX printf's
322 implement, e.g.:
323
324         g_snprintf(add_string, 30, " - (%1$d) (0x%1$04x)", value);
325
326 as not all UNIX printf's implement it, and Windows printf doesn't appear
327 to implement it.  Use something like
328
329         g_snprintf(add_string, 30, " - (%d) (0x%04x)", value, value);
330
331 instead.
332
333 Don't use "variadic macros", such as
334
335         #define DBG(format, args...)    fprintf(stderr, format, ## args)
336
337 as not all C compilers support them.  Use macros that take a fixed
338 number of arguments, such as
339
340         #define DBG0(format)            fprintf(stderr, format)
341         #define DBG1(format, arg1)      fprintf(stderr, format, arg1)
342         #define DBG2(format, arg1, arg2) fprintf(stderr, format, arg1, arg2)
343
344                 ...
345
346 or something such as
347
348         #define DBG(args)               printf args
349
350 Don't use
351
352         case N ... M:
353
354 as that's not supported by all compilers.
355
356 snprintf() -> g_snprintf()
357 snprintf() is not available on all platforms, so it's a good idea to use the
358 g_snprintf() function declared by <glib.h> instead.
359
360 tmpnam() -> mkstemp()
361 tmpnam is insecure and should not be used any more. Wireshark brings its
362 own mkstemp implementation for use on platforms that lack mkstemp.
363 Note: mkstemp does not accept NULL as a parameter.
364
365 The pointer returned by a call to "tvb_get_ptr()" is not guaranteed to be
366 aligned on any particular byte boundary; this means that you cannot
367 safely cast it to any data type other than a pointer to "char",
368 "unsigned char", "guint8", or other one-byte data types.  You cannot,
369 for example, safely cast it to a pointer to a structure, and then access
370 the structure members directly; on some systems, unaligned accesses to
371 integral data types larger than 1 byte, and floating-point data types,
372 cause a trap, which will, at best, result in the OS slowly performing an
373 unaligned access for you, and will, on at least some platforms, cause
374 the program to be terminated.
375
376 Wireshark supports platforms with GLib 2.4[.x]/GTK+ 2.4[.x] or newer. 
377 If a Glib/GTK+ mechanism is available only in Glib/GTK+ versions 
378 newer than 2.4/2.4 then use "#if GTK_CHECK_VERSION(...)" to conditionally
379 compile code using that mechanism. 
380
381 When different code must be used on UN*X and Win32, use a #if or #ifdef
382 that tests _WIN32, not WIN32.  Try to write code portably whenever
383 possible, however; note that there are some routines in Wireshark with
384 platform-dependent implementations and platform-independent APIs, such
385 as the routines in epan/filesystem.c, allowing the code that calls it to
386 be written portably without #ifdefs.
387
388 1.1.2 String handling
389
390 Do not use functions such as strcat() or strcpy().
391 A lot of work has been done to remove the existing calls to these functions and
392 we do not want any new callers of these functions.
393
394 Instead use g_snprintf() since that function will if used correctly prevent
395 buffer overflows for large strings.
396
397 When using a buffer to create a string, do not use a buffer stored on the stack.
398 I.e. do not use a buffer declared as
399    char buffer[1024];
400 instead allocate a buffer dynamically using the emem routines (see
401 README.malloc) such as
402    char *buffer=NULL;
403    ...
404    #define MAX_BUFFER 1024
405    buffer=ep_alloc(MAX_BUFFER);
406    buffer[0]=0;
407    ...
408    g_snprintf(buffer, MAX_BUFFER, ...
409
410 This avoids the stack from being corrupted in case there is a bug in your code
411 that accidentally writes beyond the end of the buffer.
412
413
414 If you write a routine that will create and return a pointer to a filled in
415 string and if that buffer will not be further processed or appended to after
416 the routine returns (except being added to the proto tree),
417 do not preallocate the buffer to fill in and pass as a parameter instead
418 pass a pointer to a pointer to the function and return a pointer to an
419 emem allocated buffer that will be automatically freed. (see README.malloc)
420
421 I.e. do not write code such as
422   static void
423   foo_to_str(char *string, ... ){
424      <fill in string>
425   }
426   ...
427      char buffer[1024];
428      ...
429      foo_to_str(buffer, ...
430      proto_tree_add_text(... buffer ...
431
432 instead write the code as
433   static void
434   foo_to_str(char **buffer, ...
435     #define MAX_BUFFER x
436     *buffer=ep_alloc(MAX_BUFFER);
437     <fill in *buffer>
438   }
439   ...
440     char *buffer;
441     ...
442     foo_to_str(&buffer, ...
443     proto_tree_add_text(... *buffer ...
444
445 Use ep_ allocated buffers. They are very fast and nice. These buffers are all
446 automatically free()d when the dissection of the current packet ends so you
447 don't have to worry about free()ing them explicitly in order to not leak memory.
448 Please read README.malloc.
449
450 1.1.3 Robustness.
451
452 Wireshark is not guaranteed to read only network traces that contain correctly-
453 formed packets. Wireshark is commonly used to track down networking
454 problems, and the problems might be due to a buggy protocol implementation
455 sending out bad packets.
456
457 Therefore, protocol dissectors not only have to be able to handle
458 correctly-formed packets without, for example, crashing or looping
459 infinitely, they also have to be able to handle *incorrectly*-formed
460 packets without crashing or looping infinitely.
461
462 Here are some suggestions for making dissectors more robust in the face
463 of incorrectly-formed packets:
464
465 Do *NOT* use "g_assert()" or "g_assert_not_reached()" in dissectors.
466 *NO* value in a packet's data should be considered "wrong" in the sense
467 that it's a problem with the dissector if found; if it cannot do
468 anything else with a particular value from a packet's data, the
469 dissector should put into the protocol tree an indication that the
470 value is invalid, and should return. You can use the DISSECTOR_ASSERT
471 macro for that purpose.
472
473 If you are allocating a chunk of memory to contain data from a packet,
474 or to contain information derived from data in a packet, and the size of
475 the chunk of memory is derived from a size field in the packet, make
476 sure all the data is present in the packet before allocating the buffer.
477 Doing so means that
478
479         1) Wireshark won't leak that chunk of memory if an attempt to
480            fetch data not present in the packet throws an exception
481
482 and
483
484         2) it won't crash trying to allocate an absurdly-large chunk of
485            memory if the size field has a bogus large value.
486
487 If you're fetching into such a chunk of memory a string from the buffer,
488 and the string has a specified size, you can use "tvb_get_*_string()",
489 which will check whether the entire string is present before allocating
490 a buffer for the string, and will also put a trailing '\0' at the end of
491 the buffer.
492
493 If you're fetching into such a chunk of memory a 2-byte Unicode string
494 from the buffer, and the string has a specified size, you can use
495 "tvb_get_ephemeral_faked_unicode()", which will check whether the entire
496 string is present before allocating a buffer for the string, and will also
497 put a trailing '\0' at the end of the buffer.  The resulting string will be
498 a sequence of single-byte characters; the only Unicode characters that
499 will be handled correctly are those in the ASCII range.  (Wireshark's
500 ability to handle non-ASCII strings is limited; it needs to be
501 improved.)
502
503 If you're fetching into such a chunk of memory a sequence of bytes from
504 the buffer, and the sequence has a specified size, you can use
505 "tvb_memdup()", which will check whether the entire sequence is present
506 before allocating a buffer for it.
507
508 Otherwise, you can check whether the data is present by using
509 "tvb_ensure_bytes_exist()" or by getting a pointer to the data by using
510 "tvb_get_ptr()", although note that there might be problems with using
511 the pointer from "tvb_get_ptr()" (see the item on this in the
512 Portability section above, and the next item below).
513
514 Note also that you should only fetch string data into a fixed-length
515 buffer if the code ensures that no more bytes than will fit into the
516 buffer are fetched ("the protocol ensures" isn't good enough, as
517 protocol specifications can't ensure only packets that conform to the
518 specification will be transmitted or that only packets for the protocol
519 in question will be interpreted as packets for that protocol by
520 Wireshark).  If there's no maximum length of string data to be fetched,
521 routines such as "tvb_get_*_string()" are safer, as they allocate a buffer
522 large enough to hold the string.  (Note that some variants of this call
523 require you to free the string once you're finished with it.)
524
525 If you have gotten a pointer using "tvb_get_ptr()", you must make sure
526 that you do not refer to any data past the length passed as the last
527 argument to "tvb_get_ptr()"; while the various "tvb_get" routines
528 perform bounds checking and throw an exception if you refer to data not
529 available in the tvbuff, direct references through a pointer gotten from
530 "tvb_get_ptr()" do not do any bounds checking.
531
532 If you have a loop that dissects a sequence of items, each of which has
533 a length field, with the offset in the tvbuff advanced by the length of
534 the item, then, if the length field is the total length of the item, and
535 thus can be zero, you *MUST* check for a zero-length item and abort the
536 loop if you see one.  Otherwise, a zero-length item could cause the
537 dissector to loop infinitely.  You should also check that the offset,
538 after having the length added to it, is greater than the offset before
539 the length was added to it, if the length field is greater than 24 bits
540 long, so that, if the length value is *very* large and adding it to the
541 offset causes an overflow, that overflow is detected.
542
543 If you are fetching a length field from the buffer, corresponding to the
544 length of a portion of the packet, and subtracting from that length a
545 value corresponding to the length of, for example, a header in the
546 packet portion in question, *ALWAYS* check that the value of the length
547 field is greater than or equal to the length you're subtracting from it,
548 and report an error in the packet and stop dissecting the packet if it's
549 less than the length you're subtracting from it.  Otherwise, the
550 resulting length value will be negative, which will either cause errors
551 in the dissector or routines called by the dissector, or, if the value
552 is interpreted as an unsigned integer, will cause the value to be
553 interpreted as a very large positive value.
554
555 Any tvbuff offset that is added to as processing is done on a packet
556 should be stored in a 32-bit variable, such as an "int"; if you store it
557 in an 8-bit or 16-bit variable, you run the risk of the variable
558 overflowing.
559
560 sprintf() -> g_snprintf()
561 Prevent yourself from using the sprintf() function, as it does not test the
562 length of the given output buffer and might be writing into unintended memory
563 areas. This function is one of the main causes of security problems like buffer
564 exploits and many other bugs that are very hard to find. It's much better to
565 use the g_snprintf() function declared by <glib.h> instead.
566
567 You should test your dissector against incorrectly-formed packets.  This
568 can be done using the randpkt and editcap utilities that come with the
569 Wireshark distribution.  Testing using randpkt can be done by generating
570 output at the same layer as your protocol, and forcing Wireshark/TShark
571 to decode it as your protocol, e.g. if your protocol sits on top of UDP:
572
573     randpkt -c 50000 -t dns randpkt.pcap
574     tshark -nVr randpkt.pcap -d udp.port==53,<myproto>
575
576 Testing using editcap can be done using preexisting capture files and the
577 "-E" flag, which introduces errors in a capture file.  E.g.:
578
579     editcap -E 0.03 infile.pcap outfile.pcap
580     tshark -nVr outfile.pcap
581
582 The script fuzz-test.sh is available to help automate these tests.
583
584 1.1.4 Name convention.
585
586 Wireshark uses the underscore_convention rather than the InterCapConvention for
587 function names, so new code should probably use underscores rather than
588 intercaps for functions and variable names. This is especially important if you
589 are writing code that will be called from outside your code.  We are just
590 trying to keep things consistent for other developers.
591
592 1.1.5 White space convention.
593
594 Avoid using tab expansions different from 8 column widths, as not all
595 text editors in use by the developers support this. For a detailed
596 discussion of tabs, spaces, and indentation, see
597
598     http://www.jwz.org/doc/tabs-vs-spaces.html
599
600 When creating a new file, you are free to choose an indentation logic.
601 Most of the files in Wireshark tend to use 2-space or 4-space
602 indentation. You are encouraged to write a short comment on the
603 indentation logic at the beginning of this new file, especially if
604 you're using non-mod-8 tabs.  The tabs-vs-spaces document above provides
605 examples of Emacs and vi modelines for this purpose.
606
607 When editing an existing file, try following the existing indentation
608 logic and even if it very tempting, never ever use a restyler/reindenter
609 utility on an existing file.  If you run across wildly varying
610 indentation styles within the same file, it might be helpful to send a
611 note to wireshark-dev for guidance.
612
613 1.1.6 Compiler warnings
614
615 You should write code that is free of compiler warnings. Such warnings will
616 often indicate questionable code and sometimes even real bugs, so it's best
617 to avoid warnings at all.
618
619 The compiler flags in the Makefiles are set to "treat warnings as errors",
620 so your code won't even compile when warnings occur.
621
622 1.2 Skeleton code.
623
624 Wireshark requires certain things when setting up a protocol dissector.
625 Below is skeleton code for a dissector that you can copy to a file and
626 fill in.  Your dissector should follow the naming convention of packet-
627 followed by the abbreviated name for the protocol.  It is recommended
628 that where possible you keep to the IANA abbreviated name for the
629 protocol, if there is one, or a commonly-used abbreviation for the
630 protocol, if any.
631
632 Usually, you will put your newly created dissector file into the directory
633 epan/dissectors, just like all the other packet-....c files already in there.
634
635 Also, please add your dissector file to the corresponding makefile,
636 described in section "1.9 Editing Makefile.common to add your dissector" below.
637
638 Dissectors that use the dissector registration to register with a lower level
639 dissector don't need to define a prototype in the .h file. For other
640 dissectors the main dissector routine should have a prototype in a header
641 file whose name is "packet-", followed by the abbreviated name for the
642 protocol, followed by ".h"; any dissector file that calls your dissector
643 should be changed to include that file.
644
645 You may not need to include all the headers listed in the skeleton
646 below, and you may need to include additional headers.  For example, the
647 code inside
648
649         #ifdef HAVE_LIBPCRE
650
651                 ...
652
653         #endif
654
655 is needed only if you are using a function from libpcre, e.g. the
656 "pcre_compile()" function.
657
658 The "$Id$" in the comment will be updated by Subversion when the file is
659 checked in.
660
661 When creating a new file, it is fine to just write "$Id$" as Subversion will
662 automatically fill in the identifier at the time the file will be added to the
663 SVN repository (committed).
664
665 ------------------------------------Cut here------------------------------------
666 /* packet-PROTOABBREV.c
667  * Routines for PROTONAME dissection
668  * Copyright 200x, YOUR_NAME <YOUR_EMAIL_ADDRESS>
669  *
670  * $Id$
671  *
672  * Wireshark - Network traffic analyzer
673  * By Gerald Combs <gerald@wireshark.org>
674  * Copyright 1998 Gerald Combs
675  *
676  * Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
677  * is a dissector file; if you just copied this from README.developer,
678  * don't bother with the "Copied from" - you don't even need to put
679  * in a "Copied from" if you copied an existing dissector, especially
680  * if the bulk of the code in the new dissector is your code)
681  *
682  * This program is free software; you can redistribute it and/or
683  * modify it under the terms of the GNU General Public License
684  * as published by the Free Software Foundation; either version 2
685  * of the License, or (at your option) any later version.
686  *
687  * This program is distributed in the hope that it will be useful,
688  * but WITHOUT ANY WARRANTY; without even the implied warranty of
689  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
690  * GNU General Public License for more details.
691  *
692  * You should have received a copy of the GNU General Public License
693  * along with this program; if not, write to the Free Software
694  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
695  */
696
697 #ifdef HAVE_CONFIG_H
698 # include "config.h"
699 #endif
700
701 #include <stdio.h>
702 #include <stdlib.h>
703 #include <string.h>
704
705 #include <glib.h>
706
707 #include <epan/packet.h>
708 #include <epan/prefs.h>
709
710 /* IF PROTO exposes code to other dissectors, then it must be exported
711    in a header file. If not, a header file is not needed at all. */
712 #include "packet-PROTOABBREV.h"
713
714 /* Forward declaration we need below */
715 void proto_reg_handoff_PROTOABBREV(void);
716
717 /* Initialize the protocol and registered fields */
718 static int proto_PROTOABBREV = -1;
719 static int hf_PROTOABBREV_FIELDABBREV = -1;
720
721 /* Global sample preference ("controls" display of numbers) */
722 static gboolean gPREF_HEX = FALSE;
723
724 /* Initialize the subtree pointers */
725 static gint ett_PROTOABBREV = -1;
726
727 /* Code to actually dissect the packets */
728 static int
729 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
730 {
731
732 /* Set up structures needed to add the protocol subtree and manage it */
733         proto_item *ti;
734         proto_tree *PROTOABBREV_tree;
735
736 /*  First, if at all possible, do some heuristics to check if the packet cannot
737  *  possibly belong to your protocol.  This is especially important for
738  *  protocols directly on top of TCP or UDP where port collisions are
739  *  common place (e.g., even though your protocol uses a well known port,
740  *  someone else may set up, for example, a web server on that port which,
741  *  if someone analyzed that web server's traffic in Wireshark, would result
742  *  in Wireshark handing an HTTP packet to your dissector).  For example:
743  */
744         /* Check that there's enough data */
745         if (tvb_length(tvb) < /* your protocol's smallest packet size */)
746                 return 0;
747
748         /* Get some values from the packet header, probably using tvb_get_*() */
749         if ( /* these values are not possible in PROTONAME */ )
750                 /*  This packet does not appear to belong to PROTONAME.
751                  *  Return 0 to give another dissector a chance to dissect it.
752                  */
753                 return 0;
754
755 /* Make entries in Protocol column and Info column on summary display */
756         if (check_col(pinfo->cinfo, COL_PROTOCOL))
757                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
758
759 /* This field shows up as the "Info" column in the display; you should use
760    it, if possible, to summarize what's in the packet, so that a user looking
761    at the list of packets can tell what type of packet it is. See section 1.5
762    for more information.
763
764    Before changing the contents of a column you should make sure the column is
765    active by calling "check_col(pinfo->cinfo, COL_*)". If it is not active
766    don't bother setting it.
767
768    If you are setting the column to a constant string, use "col_set_str()",
769    as it's more efficient than the other "col_set_XXX()" calls.
770
771    If you're setting it to a string you've constructed, or will be
772    appending to the column later, use "col_add_str()".
773
774    "col_add_fstr()" can be used instead of "col_add_str()"; it takes
775    "printf()"-like arguments.  Don't use "col_add_fstr()" with a format
776    string of "%s" - just use "col_add_str()" or "col_set_str()", as it's
777    more efficient than "col_add_fstr()".
778
779    If you will be fetching any data from the packet before filling in
780    the Info column, clear that column first, in case the calls to fetch
781    data from the packet throw an exception because they're fetching data
782    past the end of the packet, so that the Info column doesn't have data
783    left over from the previous dissector; do
784
785         if (check_col(pinfo->cinfo, COL_INFO))
786                 col_clear(pinfo->cinfo, COL_INFO);
787
788    */
789
790         if (check_col(pinfo->cinfo, COL_INFO))
791                 col_set_str(pinfo->cinfo, COL_INFO, "XXX Request");
792
793 /* A protocol dissector can be called in 2 different ways:
794
795         (a) Operational dissection
796
797                 In this mode, Wireshark is only interested in the way protocols
798                 interact, protocol conversations are created, packets are
799                 reassembled and handed over to higher-level protocol dissectors.
800                 In this mode Wireshark does not build a so-called "protocol
801                 tree".
802
803         (b) Detailed dissection
804
805                 In this mode, Wireshark is also interested in all details of
806                 a given protocol, so a "protocol tree" is created.
807
808    Wireshark distinguishes between the 2 modes with the proto_tree pointer:
809         (a) <=> tree == NULL
810         (b) <=> tree != NULL
811
812    In the interest of speed, if "tree" is NULL, avoid building a
813    protocol tree and adding stuff to it, or even looking at any packet
814    data needed only if you're building the protocol tree, if possible.
815
816    Note, however, that you must fill in column information, create
817    conversations, reassemble packets, build any other persistent state
818    needed for dissection, and call subdissectors regardless of whether
819    "tree" is NULL or not.  This might be inconvenient to do without
820    doing most of the dissection work; the routines for adding items to
821    the protocol tree can be passed a null protocol tree pointer, in
822    which case they'll return a null item pointer, and
823    "proto_item_add_subtree()" returns a null tree pointer if passed a
824    null item pointer, so, if you're careful not to dereference any null
825    tree or item pointers, you can accomplish this by doing all the
826    dissection work.  This might not be as efficient as skipping that
827    work if you're not building a protocol tree, but if the code would
828    have a lot of tests whether "tree" is null if you skipped that work,
829    you might still be better off just doing all that work regardless of
830    whether "tree" is null or not. */
831         if (tree) {
832
833 /* NOTE: The offset and length values in the call to
834    "proto_tree_add_item()" define what data bytes to highlight in the hex
835    display window when the line in the protocol tree display
836    corresponding to that item is selected.
837
838    Supplying a length of -1 is the way to highlight all data from the
839    offset to the end of the packet. */
840
841 /* create display subtree for the protocol */
842                 ti = proto_tree_add_item(tree, proto_PROTOABBREV, tvb, 0, -1, FALSE);
843
844                 PROTOABBREV_tree = proto_item_add_subtree(ti, ett_PROTOABBREV);
845
846 /* add an item to the subtree, see section 1.6 for more information */
847                 proto_tree_add_item(PROTOABBREV_tree,
848                     hf_PROTOABBREV_FIELDABBREV, tvb, offset, len, FALSE);
849
850
851 /* Continue adding tree items to process the packet here */
852
853
854         }
855
856 /* If this protocol has a sub-dissector call it here, see section 1.8 */
857
858 /* Return the amount of data this dissector was able to dissect */
859         return tvb_length(tvb);
860 }
861
862
863 /* Register the protocol with Wireshark */
864
865 /* this format is require because a script is used to build the C function
866    that calls all the protocol registration.
867 */
868
869 void
870 proto_register_PROTOABBREV(void)
871 {
872         module_t *PROTOABBREV_module;
873
874 /* Setup list of header fields  See Section 1.6.1 for details*/
875         static hf_register_info hf[] = {
876                 { &hf_PROTOABBREV_FIELDABBREV,
877                         { "FIELDNAME",           "PROTOABBREV.FIELDABBREV",
878                         FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK,
879                         "FIELDDESCR", HFILL }
880                 }
881         };
882
883 /* Setup protocol subtree array */
884         static gint *ett[] = {
885                 &ett_PROTOABBREV
886         };
887
888 /* Register the protocol name and description */
889         proto_PROTOABBREV = proto_register_protocol("PROTONAME",
890             "PROTOSHORTNAME", "PROTOABBREV");
891
892 /* Required function calls to register the header fields and subtrees used */
893         proto_register_field_array(proto_PROTOABBREV, hf, array_length(hf));
894         proto_register_subtree_array(ett, array_length(ett));
895
896 /* Register preferences module (See Section 2.6 for more on preferences) */
897         PROTOABBREV_module = prefs_register_protocol(proto_PROTOABBREV,
898             proto_reg_handoff_PROTOABBREV);
899
900 /* Register a sample preference */
901         prefs_register_bool_preference(PROTOABBREV_module, "show_hex",
902              "Display numbers in Hex",
903              "Enable to display numerical values in hexadecimal.",
904              &gPREF_HEX);
905 }
906
907
908 /* If this dissector uses sub-dissector registration add a registration routine.
909    This exact format is required because a script is used to find these
910    routines and create the code that calls these routines.
911
912    This function is also called by preferences whenever "Apply" is pressed
913    (see prefs_register_protocol above) so it should accommodate being called
914    more than once.
915 */
916 void
917 proto_reg_handoff_PROTOABBREV(void)
918 {
919         static gboolean inited = FALSE;
920
921         if (!inited) {
922
923             dissector_handle_t PROTOABBREV_handle;
924
925 /*  Use new_create_dissector_handle() to indicate that dissect_PROTOABBREV()
926  *  returns the number of bytes it dissected (or 0 if it thinks the packet
927  *  does not belong to PROTONAME).
928  */
929             PROTOABBREV_handle = new_create_dissector_handle(dissect_PROTOABBREV,
930                 proto_PROTOABBREV);
931             dissector_add("PARENT_SUBFIELD", ID_VALUE, PROTOABBREV_handle);
932
933             inited = TRUE;
934         }
935
936         /*
937           If you perform registration functions which are dependent upon
938           prefs the you should de-register everything which was associated
939           with the previous settings and re-register using the new prefs
940           settings here. In general this means you need to keep track of what
941           value the preference had at the time you registered using a local
942           static in this function. ie.
943
944           static int currentPort = -1;
945
946           if (currentPort != -1) {
947               dissector_delete("tcp.port", currentPort, PROTOABBREV_handle);
948           }
949
950           currentPort = gPortPref;
951
952           dissector_add("tcp.port", currentPort, PROTOABBREV_handle);
953
954         */
955 }
956
957 ------------------------------------Cut here------------------------------------
958
959 1.3 Explanation of needed substitutions in code skeleton.
960
961 In the above code block the following strings should be substituted with
962 your information.
963
964 YOUR_NAME       Your name, of course.  You do want credit, don't you?
965                 It's the only payment you will receive....
966 YOUR_EMAIL_ADDRESS      Keep those cards and letters coming.
967 WHATEVER_FILE_YOU_USED  Add this line if you are using another file as a
968                 starting point.
969 PROTONAME       The name of the protocol; this is displayed in the
970                 top-level protocol tree item for that protocol.
971 PROTOSHORTNAME  An abbreviated name for the protocol; this is displayed
972                 in the "Preferences" dialog box if your dissector has
973                 any preferences, in the dialog box of enabled protocols,
974                 and in the dialog box for filter fields when constructing
975                 a filter expression.
976 PROTOABBREV     A name for the protocol for use in filter expressions;
977                 it shall contain only lower-case letters, digits, and
978                 hyphens.
979 FIELDNAME       The displayed name for the header field.
980 FIELDABBREV     The abbreviated name for the header field. (NO SPACES)
981 FIELDTYPE       FT_NONE, FT_BOOLEAN, FT_UINT8, FT_UINT16, FT_UINT24,
982                 FT_UINT32, FT_UINT64, FT_INT8, FT_INT16, FT_INT24, FT_INT32,
983                 FT_INT64, FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME,
984                 FT_RELATIVE_TIME, FT_STRING, FT_STRINGZ, FT_EBCDIC,
985                 FT_UINT_STRING, FT_ETHER, FT_BYTES, FT_IPv4, FT_IPv6, FT_IPXNET,
986                 FT_FRAMENUM, FT_PROTOCOL, FT_GUID, FT_OID
987 FIELDBASE       BASE_NONE, BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX,
988                 BASE_HEX_DEC, BASE_RANGE_STRING
989 FIELDCONVERT    VALS(x), RVALS(x), TFS(x), NULL
990 BITMASK         Usually 0x0 unless using the TFS(x) field conversion.
991 FIELDDESCR      A brief description of the field, or NULL.
992 PARENT_SUBFIELD Lower level protocol field used for lookup, i.e. "tcp.port"
993 ID_VALUE        Lower level protocol field value that identifies this protocol
994                 For example the TCP or UDP port number
995
996 If, for example, PROTONAME is "Internet Bogosity Discovery Protocol",
997 PROTOSHORTNAME would be "IBDP", and PROTOABBREV would be "ibdp".  Try to
998 conform with IANA names.
999
1000 1.4 The dissector and the data it receives.
1001
1002
1003 1.4.1 Header file.
1004
1005 This is only needed if the dissector doesn't use self-registration to
1006 register itself with the lower level dissector, or if the protocol dissector
1007 wants/needs to expose code to other subdissectors.
1008
1009 The dissector must be declared exactly as follows in the file
1010 packet-PROTOABBREV.h:
1011
1012 int
1013 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
1014
1015
1016 1.4.2 Extracting data from packets.
1017
1018 NOTE: See the file /epan/tvbuff.h for more details.
1019
1020 The "tvb" argument to a dissector points to a buffer containing the raw
1021 data to be analyzed by the dissector; for example, for a protocol
1022 running atop UDP, it contains the UDP payload (but not the UDP header,
1023 or any protocol headers above it).  A tvbuffer is an opaque data
1024 structure, the internal data structures are hidden and the data must be
1025 accessed via the tvbuffer accessors.
1026
1027 The accessors are:
1028
1029 Single-byte accessor:
1030
1031 guint8  tvb_get_guint8(tvbuff_t*, gint offset);
1032
1033 Network-to-host-order accessors for 16-bit integers (guint16), 24-bit
1034 integers, 32-bit integers (guint32), and 64-bit integers (guint64):
1035
1036 guint16 tvb_get_ntohs(tvbuff_t*, gint offset);
1037 guint32 tvb_get_ntoh24(tvbuff_t*, gint offset);
1038 guint32 tvb_get_ntohl(tvbuff_t*, gint offset);
1039 guint64 tvb_get_ntoh64(tvbuff_t*, gint offset);
1040
1041 Network-to-host-order accessors for single-precision and
1042 double-precision IEEE floating-point numbers:
1043
1044 gfloat tvb_get_ntohieee_float(tvbuff_t*, gint offset);
1045 gdouble tvb_get_ntohieee_double(tvbuff_t*, gint offset);
1046
1047 Little-Endian-to-host-order accessors for 16-bit integers (guint16),
1048 24-bit integers, 32-bit integers (guint32), and 64-bit integers
1049 (guint64):
1050
1051 guint16 tvb_get_letohs(tvbuff_t*, gint offset);
1052 guint32 tvb_get_letoh24(tvbuff_t*, gint offset);
1053 guint32 tvb_get_letohl(tvbuff_t*, gint offset);
1054 guint64 tvb_get_letoh64(tvbuff_t*, gint offset);
1055
1056 Little-Endian-to-host-order accessors for single-precision and
1057 double-precision IEEE floating-point numbers:
1058
1059 gfloat tvb_get_letohieee_float(tvbuff_t*, gint offset);
1060 gdouble tvb_get_letohieee_double(tvbuff_t*, gint offset);
1061
1062 Accessors for IPv4 and IPv6 addresses:
1063
1064 guint32 tvb_get_ipv4(tvbuff_t*, gint offset);
1065 void tvb_get_ipv6(tvbuff_t*, gint offset, struct e_in6_addr *addr);
1066
1067 NOTE: IPv4 addresses are not to be converted to host byte order before
1068 being passed to "proto_tree_add_ipv4()".  You should use "tvb_get_ipv4()"
1069 to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't,
1070 for example, try to use "tvb_get_ntohl()", find that it gives you the
1071 wrong answer on the PC on which you're doing development, and try
1072 "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong
1073 answer on big-endian machines.
1074
1075 Accessors for GUID:
1076
1077 void tvb_get_ntohguid(tvbuff_t *, gint offset, e_guid_t *guid);
1078 void tvb_get_letohguid(tvbuff_t *, gint offset, e_guid_t *guid);
1079
1080 String accessors:
1081
1082 guint8 *tvb_get_string(tvbuff_t*, gint offset, gint length);
1083 guint8 *tvb_get_ephemeral_string(tvbuff_t*, gint offset, gint length);
1084
1085 Returns a null-terminated buffer containing data from the specified
1086 tvbuff, starting at the specified offset, and containing the specified
1087 length worth of characters (the length of the buffer will be length+1,
1088 as it includes a null character to terminate the string).
1089
1090 tvb_get_string() returns a buffer allocated by g_malloc() so you must
1091 g_free() it when you are finished with the string. Failure to g_free() this
1092 buffer will lead to memory leaks.
1093 tvb_get_ephemeral_string() returns a buffer allocated from a special heap
1094 with a lifetime until the next packet is dissected. You do not need to
1095 free() this buffer, it will happen automatically once the next packet is
1096 dissected.
1097
1098
1099 guint8 *tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp);
1100 guint8 *tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp);
1101
1102 Returns a null-terminated buffer, allocated with "g_malloc()",
1103 containing data from the specified tvbuff, starting at the
1104 specified offset, and containing all characters from the tvbuff up to
1105 and including a terminating null character in the tvbuff.  "*lengthp"
1106 will be set to the length of the string, including the terminating null.
1107
1108 tvb_get_stringz() returns a buffer allocated by g_malloc() so you must
1109 g_free() it when you are finished with the string. Failure to g_free() this
1110 buffer will lead to memory leaks.
1111 tvb_get_ephemeral_stringz() returns a buffer allocated from a special heap
1112 with a lifetime until the next packet is dissected. You do not need to
1113 free() this buffer, it will happen automatically once the next packet is
1114 dissected.
1115
1116
1117 guint8 *tvb_fake_unicode(tvbuff_t*, gint offset, gint length);
1118 guint8 *tvb_get_ephemeral_faked_unicode(tvbuff_t*, gint offset, gint length);
1119
1120 Converts a 2-byte unicode string to an ASCII string.
1121 Returns a null-terminated buffer containing data from the specified
1122 tvbuff, starting at the specified offset, and containing the specified
1123 length worth of characters (the length of the buffer will be length+1,
1124 as it includes a null character to terminate the string).
1125
1126 tvb_fake_unicode() returns a buffer allocated by g_malloc() so you must
1127 g_free() it when you are finished with the string. Failure to g_free() this
1128 buffer will lead to memory leaks.
1129 tvb_get_ephemeral_faked_unicode() returns a buffer allocated from a special
1130 heap with a lifetime until the next packet is dissected. You do not need to
1131 free() this buffer, it will happen automatically once the next packet is
1132 dissected.
1133
1134
1135 Copying memory:
1136 guint8* tvb_memcpy(tvbuff_t*, guint8* target, gint offset, gint length);
1137
1138 Copies into the specified target the specified length's worth of data
1139 from the specified tvbuff, starting at the specified offset.
1140
1141 guint8* tvb_memdup(tvbuff_t*, gint offset, gint length);
1142 guint8* ep_tvb_memdup(tvbuff_t*, gint offset, gint length);
1143
1144 Returns a buffer, allocated with "g_malloc()", containing the specified
1145 length's worth of data from the specified tvbuff, starting at the
1146 specified offset. The ephemeral variant is freed automatically after the
1147 packet is dissected.
1148
1149 Pointer-retrieval:
1150 /* WARNING! This function is possibly expensive, temporarily allocating
1151  * another copy of the packet data. Furthermore, it's dangerous because once
1152  * this pointer is given to the user, there's no guarantee that the user will
1153  * honor the 'length' and not overstep the boundaries of the buffer.
1154  */
1155 guint8* tvb_get_ptr(tvbuff_t*, gint offset, gint length);
1156
1157 The reason that tvb_get_ptr() might have to allocate a copy of its data
1158 only occurs with TVBUFF_COMPOSITES, data that spans multiple tvbuffers.
1159 If the user requests a pointer to a range of bytes that span the member
1160 tvbuffs that make up the TVBUFF_COMPOSITE, the data will have to be
1161 copied to another memory region to assure that all the bytes are
1162 contiguous.
1163
1164
1165
1166 1.5 Functions to handle columns in the traffic summary window.
1167
1168 The topmost pane of the main window is a list of the packets in the
1169 capture, possibly filtered by a display filter.
1170
1171 Each line corresponds to a packet, and has one or more columns, as
1172 configured by the user.
1173
1174 Many of the columns are handled by code outside individual dissectors;
1175 most dissectors need only specify the value to put in the "Protocol" and
1176 "Info" columns.
1177
1178 Columns are specified by COL_ values; the COL_ value for the "Protocol"
1179 field, typically giving an abbreviated name for the protocol (but not
1180 the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the
1181 COL_ value for the "Info" field, giving a summary of the contents of the
1182 packet for that protocol, is COL_INFO.
1183
1184 A value for a column should only be added if the user specified that it
1185 be displayed; to check whether a given column is to be displayed, call
1186 'check_col' with the COL_ value for that field as an argument - it will
1187 return TRUE if the column is to be displayed and FALSE if it is not to
1188 be displayed.
1189
1190 The value for a column can be specified with one of several functions,
1191 all of which take the 'fd' argument to the dissector as their first
1192 argument, and the COL_ value for the column as their second argument.
1193
1194 1.5.1 The col_set_str function.
1195
1196 'col_set_str' takes a string as its third argument, and sets the value
1197 for the column to that value.  It assumes that the pointer passed to it
1198 points to a string constant or a static "const" array, not to a
1199 variable, as it doesn't copy the string, it merely saves the pointer
1200 value; the argument can itself be a variable, as long as it always
1201 points to a string constant or a static "const" array.
1202
1203 It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if
1204 the dissector will be using 'col_append_str' or 'col_append_fstr" to
1205 append more information to the column, the string will have to be copied
1206 anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in
1207 that case.
1208
1209 For example, to set the "Protocol" column
1210 to "PROTOABBREV":
1211
1212         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1213                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
1214
1215
1216 1.5.2 The col_add_str function.
1217
1218 'col_add_str' takes a string as its third argument, and sets the value
1219 for the column to that value.  It takes the same arguments as
1220 'col_set_str', but copies the string, so that if the string is, for
1221 example, an automatic variable that won't remain in scope when the
1222 dissector returns, it's safe to use.
1223
1224
1225 1.5.3 The col_add_fstr function.
1226
1227 'col_add_fstr' takes a 'printf'-style format string as its third
1228 argument, and 'printf'-style arguments corresponding to '%' format
1229 items in that string as its subsequent arguments.  For example, to set
1230 the "Info" field to "<XXX> request, <N> bytes", where "reqtype" is a
1231 string containing the type of the request in the packet and "n" is an
1232 unsigned integer containing the number of bytes in the request:
1233
1234         if (check_col(pinfo->cinfo, COL_INFO))
1235                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
1236                     reqtype, n);
1237
1238 Don't use 'col_add_fstr' with a format argument of just "%s" -
1239 'col_add_str', or possibly even 'col_set_str' if the string that matches
1240 the "%s" is a static constant string, will do the same job more
1241 efficiently.
1242
1243
1244 1.5.4 The col_clear function.
1245
1246 If the Info column will be filled with information from the packet, that
1247 means that some data will be fetched from the packet before the Info
1248 column is filled in.  If the packet is so small that the data in
1249 question cannot be fetched, the routines to fetch the data will throw an
1250 exception (see the comment at the beginning about tvbuffers improving
1251 the handling of short packets - the tvbuffers keep track of how much
1252 data is in the packet, and throw an exception on an attempt to fetch
1253 data past the end of the packet, so that the dissector won't process
1254 bogus data), causing the Info column not to be filled in.
1255
1256 This means that the Info column will have data for the previous
1257 protocol, which would be confusing if, for example, the Protocol column
1258 had data for this protocol.
1259
1260 Therefore, before a dissector fetches any data whatsoever from the
1261 packet (unless it's a heuristic dissector fetching data to determine
1262 whether the packet is one that it should dissect, in which case it
1263 should check, before fetching the data, whether there's any data to
1264 fetch; if there isn't, it should return FALSE), it should set the
1265 Protocol column and the Info column.
1266
1267 If the Protocol column will ultimately be set to, for example, a value
1268 containing a protocol version number, with the version number being a
1269 field in the packet, the dissector should, before fetching the version
1270 number field or any other field from the packet, set it to a value
1271 without a version number, using 'col_set_str', and should later set it
1272 to a value with the version number after it's fetched the version
1273 number.
1274
1275 If the Info column will ultimately be set to a value containing
1276 information from the packet, the dissector should, before fetching any
1277 fields from the packet, clear the column using 'col_clear' (which is
1278 more efficient than clearing it by calling 'col_set_str' or
1279 'col_add_str' with a null string), and should later set it to the real
1280 string after it's fetched the data to use when doing that.
1281
1282
1283 1.5.5 The col_append_str function.
1284
1285 Sometimes the value of a column, especially the "Info" column, can't be
1286 conveniently constructed at a single point in the dissection process;
1287 for example, it might contain small bits of information from many of the
1288 fields in the packet.  'col_append_str' takes, as arguments, the same
1289 arguments as 'col_add_str', but the string is appended to the end of the
1290 current value for the column, rather than replacing the value for that
1291 column.  (Note that no blank separates the appended string from the
1292 string to which it is appended; if you want a blank there, you must add
1293 it yourself as part of the string being appended.)
1294
1295
1296 1.5.6 The col_append_fstr function.
1297
1298 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to
1299 'col_add_str' - it takes, as arguments, the same arguments as
1300 'col_add_fstr', but the formatted string is appended to the end of the
1301 current value for the column, rather than replacing the value for that
1302 column.
1303
1304 1.5.7 The col_append_sep_str and col_append_sep_fstr functions.
1305
1306 In specific situations the developer knows that a column's value will be
1307 created in a stepwise manner, where the appended values are listed. Both
1308 'col_append_sep_str' and 'col_append_sep_fstr' functions will add an item
1309 separator between two consecutive items, and will not add the separator at the
1310 beginning of the column. The remainder of the work both functions do is
1311 identical to what 'col_append_str' and 'col_append_fstr' do.
1312
1313 1.5.8 The col_set_fence and col_prepend_fence_fstr functions.
1314
1315 Sometimes a dissector may be called multiple times for different PDUs in the
1316 same frame (for example in the case of SCTP chunk bundling: several upper
1317 layer data packets may be contained in one SCTP packet).  If the upper layer
1318 dissector calls 'col_set_str()' or 'col_clear()' on the Info column when it
1319 begins dissecting each of those PDUs then when the frame is fully dissected
1320 the Info column would contain only the string from the last PDU in the frame.
1321 The 'col_set_fence' function erects a "fence" in the column that prevents
1322 subsequent 'col_...' calls from clearing the data currently in that column.
1323 For example, the SCTP dissector calls 'col_set_fence' on the Info column
1324 after it has called any subdissectors for that chunk so that subdissectors
1325 of any subsequent chunks may only append to the Info column.
1326 'col_prepend_fence_fstr' prepends data before a fence (moving it if
1327 necessary).  It will create a fence at the end of the prended data if the
1328 fence does not already exist.
1329
1330
1331 1.5.9 The col_set_time function.
1332
1333 The 'col_set_time' function takes an nstime value as its third argument.
1334 This nstime value is a relative value and will be added as such to the
1335 column. The fourth argument is the filtername holding this value. This
1336 way, rightclicking on the column makes it possible to build a filter
1337 based on the time-value.
1338
1339 For example:
1340
1341 if (check_col(pinfo->cinfo, COL_REL_CONV_TIME)) {
1342   nstime_delta(&ts, &pinfo->fd->abs_ts, &tcpd->ts_first);
1343   col_set_time(pinfo->cinfo, COL_REL_CONV_TIME, &ts, "tcp.time_relative");
1344 }
1345
1346
1347 1.6 Constructing the protocol tree.
1348
1349 The middle pane of the main window, and the topmost pane of a packet
1350 popup window, are constructed from the "protocol tree" for a packet.
1351
1352 The protocol tree, or proto_tree, is a GNode, the N-way tree structure
1353 available within GLIB. Of course the protocol dissectors don't care
1354 what a proto_tree really is; they just pass the proto_tree pointer as an
1355 argument to the routines which allow them to add items and new branches
1356 to the tree.
1357
1358 When a packet is selected in the packet-list pane, or a packet popup
1359 window is created, a new logical protocol tree (proto_tree) is created.
1360 The pointer to the proto_tree (in this case, 'protocol tree'), is passed
1361 to the top-level protocol dissector, and then to all subsequent protocol
1362 dissectors for that packet, and then the GUI tree is drawn via
1363 proto_tree_draw().
1364
1365 The logical proto_tree needs to know detailed information about the protocols
1366 and fields about which information will be collected from the dissection
1367 routines. By strictly defining (or "typing") the data that can be attached to a
1368 proto tree, searching and filtering becomes possible.  This means that for
1369 every protocol and field (which I also call "header fields", since they are
1370 fields in the protocol headers) which might be attached to a tree, some
1371 information is needed.
1372
1373 Every dissector routine will need to register its protocols and fields
1374 with the central protocol routines (in proto.c). At first I thought I
1375 might keep all the protocol and field information about all the
1376 dissectors in one file, but decentralization seemed like a better idea.
1377 That one file would have gotten very large; one small change would have
1378 required a re-compilation of the entire file. Also, by allowing
1379 registration of protocols and fields at run-time, loadable modules of
1380 protocol dissectors (perhaps even user-supplied) is feasible.
1381
1382 To do this, each protocol should have a register routine, which will be
1383 called when Wireshark starts.  The code to call the register routines is
1384 generated automatically; to arrange that a protocol's register routine
1385 be called at startup:
1386
1387         the file containing a dissector's "register" routine must be
1388         added to "DISSECTOR_SRC" in "epan/dissectors/Makefile.common";
1389
1390         the "register" routine must have a name of the form
1391         "proto_register_XXX";
1392
1393         the "register" routine must take no argument, and return no
1394         value;
1395
1396         the "register" routine's name must appear in the source file
1397         either at the beginning of the line, or preceded only by "void "
1398         at the beginning of the line (that would typically be the
1399         definition) - other white space shouldn't cause a problem, e.g.:
1400
1401 void proto_register_XXX(void) {
1402
1403         ...
1404
1405 }
1406
1407 and
1408
1409 void
1410 proto_register_XXX( void )
1411 {
1412
1413         ...
1414
1415 }
1416
1417         and so on should work.
1418
1419 For every protocol or field that a dissector wants to register, a variable of
1420 type int needs to be used to keep track of the protocol. The IDs are
1421 needed for establishing parent/child relationships between protocols and
1422 fields, as well as associating data with a particular field so that it
1423 can be stored in the logical tree and displayed in the GUI protocol
1424 tree.
1425
1426 Some dissectors will need to create branches within their tree to help
1427 organize header fields. These branches should be registered as header
1428 fields. Only true protocols should be registered as protocols. This is
1429 so that a display filter user interface knows how to distinguish
1430 protocols from fields.
1431
1432 A protocol is registered with the name of the protocol and its
1433 abbreviation.
1434
1435 Here is how the frame "protocol" is registered.
1436
1437         int proto_frame;
1438
1439         proto_frame = proto_register_protocol (
1440                 /* name */            "Frame",
1441                 /* short name */      "Frame",
1442                 /* abbrev */          "frame" );
1443
1444 A header field is also registered with its name and abbreviation, but
1445 information about its data type is needed. It helps to look at
1446 the header_field_info struct to see what information is expected:
1447
1448 struct header_field_info {
1449         const char                      *name;
1450         const char                      *abbrev;
1451         enum ftenum                     type;
1452         int                             display;
1453         const void                      *strings;
1454         guint32                         bitmask;
1455         const char                      *blurb;
1456         .....
1457 };
1458
1459 name
1460 ----
1461 A string representing the name of the field. This is the name
1462 that will appear in the graphical protocol tree.  It must be a non-empty
1463 string.
1464
1465 abbrev
1466 ------
1467 A string with an abbreviation of the field. We concatenate the
1468 abbreviation of the parent protocol with an abbreviation for the field,
1469 using a period as a separator. For example, the "src" field in an IP packet
1470 would have "ip.src" as an abbreviation. It is acceptable to have
1471 multiple levels of periods if, for example, you have fields in your
1472 protocol that are then subdivided into subfields. For example, TRMAC
1473 has multiple error fields, so the abbreviations follow this pattern:
1474 "trmac.errors.iso", "trmac.errors.noniso", etc.
1475
1476 The abbreviation is the identifier used in a display filter.  If it is
1477 an empty string then the field will not be filterable.
1478
1479 type
1480 ----
1481 The type of value this field holds. The current field types are:
1482
1483         FT_NONE                 No field type. Used for fields that
1484                                 aren't given a value, and that can only
1485                                 be tested for presence or absence; a
1486                                 field that represents a data structure,
1487                                 with a subtree below it containing
1488                                 fields for the members of the structure,
1489                                 or that represents an array with a
1490                                 subtree below it containing fields for
1491                                 the members of the array, might be an
1492                                 FT_NONE field.
1493         FT_PROTOCOL             Used for protocols which will be placing
1494                                 themselves as top-level items in the
1495                                 "Packet Details" pane of the UI.
1496         FT_BOOLEAN              0 means "false", any other value means
1497                                 "true".
1498         FT_FRAMENUM             A frame number; if this is used, the "Go
1499                                 To Corresponding Frame" menu item can
1500                                 work on that field.
1501         FT_UINT8                An 8-bit unsigned integer.
1502         FT_UINT16               A 16-bit unsigned integer.
1503         FT_UINT24               A 24-bit unsigned integer.
1504         FT_UINT32               A 32-bit unsigned integer.
1505         FT_UINT64               A 64-bit unsigned integer.
1506         FT_INT8                 An 8-bit signed integer.
1507         FT_INT16                A 16-bit signed integer.
1508         FT_INT24                A 24-bit signed integer.
1509         FT_INT32                A 32-bit signed integer.
1510         FT_INT64                A 64-bit signed integer.
1511         FT_FLOAT                A single-precision floating point number.
1512         FT_DOUBLE               A double-precision floating point number.
1513         FT_ABSOLUTE_TIME        Seconds (4 bytes) and nanoseconds (4 bytes)
1514                                 of time displayed as month name, month day,
1515                                 year, hours, minutes, and seconds with 9
1516                                 digits after the decimal point.
1517         FT_RELATIVE_TIME        Seconds (4 bytes) and nanoseconds (4 bytes)
1518                                 of time displayed as seconds and 9 digits
1519                                 after the decimal point.
1520         FT_STRING               A string of characters, not necessarily
1521                                 NUL-terminated, but possibly NUL-padded.
1522                                 This, and the other string-of-characters
1523                                 types, are to be used for text strings,
1524                                 not raw binary data.
1525         FT_STRINGZ              A NUL-terminated string of characters.
1526         FT_EBCDIC               A string of characters, not necessarily
1527                                 NUL-terminated, but possibly NUL-padded.
1528                                 The data from the packet is converted from
1529                                 EBCDIC to ASCII before displaying to the user.
1530         FT_UINT_STRING          A counted string of characters, consisting
1531                                 of a count (represented as an integral value,
1532                                 of width given in the proto_tree_add_item()
1533                                 call) followed immediately by that number of
1534                                 characters.
1535         FT_ETHER                A six octet string displayed in
1536                                 Ethernet-address format.
1537         FT_BYTES                A string of bytes with arbitrary values;
1538                                 used for raw binary data.
1539         FT_IPv4                 A version 4 IP address (4 bytes) displayed
1540                                 in dotted-quad IP address format (4
1541                                 decimal numbers separated by dots).
1542         FT_IPv6                 A version 6 IP address (16 bytes) displayed
1543                                 in standard IPv6 address format.
1544         FT_IPXNET               An IPX address displayed in hex as a 6-byte
1545                                 network number followed by a 6-byte station
1546                                 address.
1547         FT_GUID                 A Globally Unique Identifier
1548         FT_OID                  An ASN.1 Object Identifier
1549
1550 Some of these field types are still not handled in the display filter
1551 routines, but the most common ones are. The FT_UINT* variables all
1552 represent unsigned integers, and the FT_INT* variables all represent
1553 signed integers; the number on the end represent how many bits are used
1554 to represent the number.
1555
1556 display
1557 -------
1558 The display field has a couple of overloaded uses. This is unfortunate,
1559 but since we're using C as an application programming language, this sometimes
1560 makes for cleaner programs. Right now I still think that overloading
1561 this variable was okay.
1562
1563 For integer fields (FT_UINT* and FT_INT*), this variable represents the
1564 base in which you would like the value displayed.  The acceptable bases
1565 are:
1566
1567         BASE_DEC,
1568         BASE_HEX,
1569         BASE_OCT,
1570         BASE_DEC_HEX,
1571         BASE_HEX_DEC
1572
1573 BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal,
1574 respectively. BASE_DEC_HEX and BASE_HEX_DEC display value in two bases
1575 (the 1st representation followed by the 2nd in parenthesis).
1576
1577 For FT_BOOLEAN fields that are also bitfields, 'display' is used to tell
1578 the proto_tree how wide the parent bitfield is.  With integers this is
1579 not needed since the type of integer itself (FT_UINT8, FT_UINT16,
1580 FT_UINT24, FT_UINT32, etc.) tells the proto_tree how wide the parent
1581 bitfield is.
1582
1583 Additionally, BASE_NONE is used for 'display' as a NULL-value. That is,
1584 for non-integers and non-bitfield FT_BOOLEANs, you'll want to use BASE_NONE
1585 in the 'display' field.  You may not use BASE_NONE for integers.
1586
1587 It is possible that in the future we will record the endianness of
1588 integers. If so, it is likely that we'll use a bitmask on the display field
1589 so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX.
1590 But that has not happened yet.
1591
1592 strings
1593 -------
1594 Some integer fields, of type FT_UINT*, need labels to represent the true
1595 value of a field.  You could think of those fields as having an
1596 enumerated data type, rather than an integral data type.
1597
1598 A 'value_string' structure is a way to map values to strings.
1599
1600         typedef struct _value_string {
1601                 guint32  value;
1602                 gchar   *strptr;
1603         } value_string;
1604
1605 For fields of that type, you would declare an array of "value_string"s:
1606
1607         static const value_string valstringname[] = {
1608                 { INTVAL1, "Descriptive String 1" },
1609                 { INTVAL2, "Descriptive String 2" },
1610                 { 0,       NULL }
1611         };
1612
1613 (the last entry in the array must have a NULL 'strptr' value, to
1614 indicate the end of the array).  The 'strings' field would be set to
1615 'VALS(valstringname)'.
1616
1617 If the field has a numeric rather than an enumerated type, the 'strings'
1618 field would be set to NULL.
1619
1620 If the field has a numeric type that might logically fit in ranges of values
1621 one can use a range_string struct.
1622
1623 Thus a 'range_string' structure is a way to map ranges to strings.
1624
1625         typedef struct _range_string {
1626                 guint32        value_min;
1627                 guint32        value_max;
1628                 const gchar   *strptr;
1629         } range_string;
1630
1631 For fields of that type, you would declare an array of "range_string"s:
1632
1633         static const range_string rvalstringname[] = {
1634                 { INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" },
1635                 { INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" },
1636                 { 0,           0,          NULL                   }
1637         };
1638
1639 If INTVAL_MIN equals INTVAL_MAX for a given entry the range_string
1640 behavior collapses to the one of value_string.
1641 For FT_(U)INT* fields that need a 'range_string' struct, the 'strings' field
1642 would be set to 'RVALS(rvalstringname)'. Furthermore, 'display' field must be
1643 ORed with 'BASE_RANGE_STRING' (e.g. BASE_DEC|BASE_RANGE_STRING).
1644
1645 FT_BOOLEANs have a default map of 0 = "False", 1 (or anything else) = "True".
1646 Sometimes it is useful to change the labels for boolean values (e.g.,
1647 to "Yes"/"No", "Fast"/"Slow", etc.).  For these mappings, a struct called
1648 true_false_string is used.
1649
1650         typedef struct true_false_string {
1651                 char    *true_string;
1652                 char    *false_string;
1653         } true_false_string;
1654
1655 For Boolean fields for which "False" and "True" aren't the desired
1656 labels, you would declare a "true_false_string"s:
1657
1658         static const true_false_string boolstringname = {
1659                 "String for True",
1660                 "String for False"
1661         };
1662
1663 Its two fields are pointers to the string representing truth, and the
1664 string representing falsehood.  For FT_BOOLEAN fields that need a
1665 'true_false_string' struct, the 'strings' field would be set to
1666 'TFS(&boolstringname)'.
1667
1668 If the Boolean field is to be displayed as "False" or "True", the
1669 'strings' field would be set to NULL.
1670
1671 Wireshark predefines a whole range of ready made "true_false_string"s
1672 in tfs.h, included via packet.h.
1673
1674 bitmask
1675 -------
1676 If the field is a bitfield, then the bitmask is the mask which will
1677 leave only the bits needed to make the field when ANDed with a value.
1678 The proto_tree routines will calculate 'bitshift' automatically
1679 from 'bitmask', by finding the rightmost set bit in the bitmask.
1680 This shift is applied before applying string mapping functions or 
1681 filtering.
1682 If the field is not a bitfield, then bitmask should be set to 0.
1683
1684 blurb
1685 -----
1686 This is a string giving a proper description of the field.  It should be
1687 at least one grammatically complete sentence, or NULL in which case the
1688 name field is used.
1689 It is meant to provide a more detailed description of the field than the
1690 name alone provides. This information will be used in the man page, and
1691 in a future GUI display-filter creation tool. We might also add tooltips
1692 to the labels in the GUI protocol tree, in which case the blurb would
1693 be used as the tooltip text.
1694
1695
1696 1.6.1 Field Registration.
1697
1698 Protocol registration is handled by creating an instance of the
1699 header_field_info struct (or an array of such structs), and
1700 calling the registration function along with the registration ID of
1701 the protocol that is the parent of the fields. Here is a complete example:
1702
1703         static int proto_eg = -1;
1704         static int hf_field_a = -1;
1705         static int hf_field_b = -1;
1706
1707         static hf_register_info hf[] = {
1708
1709                 { &hf_field_a,
1710                 { "Field A",    "proto.field_a", FT_UINT8, BASE_HEX, NULL,
1711                         0xf0, "Field A represents Apples", HFILL }},
1712
1713                 { &hf_field_b,
1714                 { "Field B",    "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
1715                         0x0, "Field B represents Bananas", HFILL }}
1716         };
1717
1718         proto_eg = proto_register_protocol("Example Protocol",
1719             "PROTO", "proto");
1720         proto_register_field_array(proto_eg, hf, array_length(hf));
1721
1722 Be sure that your array of hf_register_info structs is declared 'static',
1723 since the proto_register_field_array() function does not create a copy
1724 of the information in the array... it uses that static copy of the
1725 information that the compiler created inside your array. Here's the
1726 layout of the hf_register_info struct:
1727
1728 typedef struct hf_register_info {
1729         int                     *p_id;  /* pointer to parent variable */
1730         header_field_info       hfinfo;
1731 } hf_register_info;
1732
1733 Also be sure to use the handy array_length() macro found in packet.h
1734 to have the compiler compute the array length for you at compile time.
1735
1736 If you don't have any fields to register, do *NOT* create a zero-length
1737 "hf" array; not all compilers used to compile Wireshark support them.
1738 Just omit the "hf" array, and the "proto_register_field_array()" call,
1739 entirely.
1740
1741 It is OK to have header fields with a different format be registered with
1742 the same abbreviation. For instance, the following is valid:
1743
1744         static hf_register_info hf[] = {
1745
1746                 { &hf_field_8bit, /* 8-bit version of proto.field */
1747                 { "Field (8 bit)", "proto.field", FT_UINT8, BASE_DEC, NULL,
1748                         0x00, "Field represents FOO", HFILL }},
1749
1750                 { &hf_field_32bit, /* 32-bit version of proto.field */
1751                 { "Field (32 bit)", "proto.field", FT_UINT32, BASE_DEC, NULL,
1752                         0x00, "Field represents FOO", HFILL }}
1753         };
1754
1755 This way a filter expression can match a header field, irrespective of the
1756 representation of it in the specific protocol context. This is interesting
1757 for protocols with variable-width header fields.
1758
1759 The HFILL macro at the end of the struct will set reasonable default values
1760 for internally used fields.
1761
1762 1.6.2 Adding Items and Values to the Protocol Tree.
1763
1764 A protocol item is added to an existing protocol tree with one of a
1765 handful of proto_XXX_DO_YYY() functions.
1766
1767 Remember that it only makes sense to add items to a protocol tree if its
1768 proto_tree pointer is not null. Should you add an item to a NULL tree, then
1769 the proto_XXX_DO_YYY() function will immediately return. The cost of this
1770 function call can be avoided by checking for the tree pointer.
1771
1772 Subtrees can be made with the proto_item_add_subtree() function:
1773
1774         item = proto_tree_add_item(....);
1775         new_tree = proto_item_add_subtree(item, tree_type);
1776
1777 This will add a subtree under the item in question; a subtree can be
1778 created under an item made by any of the "proto_tree_add_XXX" functions,
1779 so that the tree can be given an arbitrary depth.
1780
1781 Subtree types are integers, assigned by
1782 "proto_register_subtree_array()".  To register subtree types, pass an
1783 array of pointers to "gint" variables to hold the subtree type values to
1784 "proto_register_subtree_array()":
1785
1786         static gint ett_eg = -1;
1787         static gint ett_field_a = -1;
1788
1789         static gint *ett[] = {
1790                 &ett_eg,
1791                 &ett_field_a
1792         };
1793
1794         proto_register_subtree_array(ett, array_length(ett));
1795
1796 in your "register" routine, just as you register the protocol and the
1797 fields for that protocol.
1798
1799 There are several functions that the programmer can use to add either
1800 protocol or field labels to the proto_tree:
1801
1802         proto_item*
1803         proto_tree_add_item(tree, id, tvb, start, length, little_endian);
1804
1805         proto_item*
1806         proto_tree_add_item_hidden(tree, id, tvb, start, length, little_endian);
1807
1808         proto_item*
1809         proto_tree_add_none_format(tree, id, tvb, start, length, format, ...);
1810
1811         proto_item*
1812         proto_tree_add_protocol_format(tree, id, tvb, start, length,
1813             format, ...);
1814
1815         proto_item *
1816         proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr);
1817
1818         proto_item *
1819         proto_tree_add_bytes_hidden(tree, id, tvb, start, length, start_ptr);
1820
1821         proto_item *
1822         proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr,
1823             format, ...);
1824
1825         proto_item *
1826         proto_tree_add_bytes_format_value(tree, id, tvb, start, length,
1827             start_ptr, format, ...);
1828
1829         proto_item *
1830         proto_tree_add_time(tree, id, tvb, start, length, value_ptr);
1831
1832         proto_item *
1833         proto_tree_add_time_hidden(tree, id, tvb, start, length, value_ptr);
1834
1835         proto_item *
1836         proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr,
1837             format, ...);
1838
1839         proto_item *
1840         proto_tree_add_time_format_value(tree, id, tvb, start, length,
1841             value_ptr, format, ...);
1842
1843         proto_item *
1844         proto_tree_add_ipxnet(tree, id, tvb, start, length, value);
1845
1846         proto_item *
1847         proto_tree_add_ipxnet_hidden(tree, id, tvb, start, length, value);
1848
1849         proto_item *
1850         proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value,
1851             format, ...);
1852
1853         proto_item *
1854         proto_tree_add_ipxnet_format_value(tree, id, tvb, start, length,
1855             value, format, ...);
1856
1857         proto_item *
1858         proto_tree_add_ipv4(tree, id, tvb, start, length, value);
1859
1860         proto_item *
1861         proto_tree_add_ipv4_hidden(tree, id, tvb, start, length, value);
1862
1863         proto_item *
1864         proto_tree_add_ipv4_format(tree, id, tvb, start, length, value,
1865             format, ...);
1866
1867         proto_item *
1868         proto_tree_add_ipv4_format_value(tree, id, tvb, start, length,
1869             value, format, ...);
1870
1871         proto_item *
1872         proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr);
1873
1874         proto_item *
1875         proto_tree_add_ipv6_hidden(tree, id, tvb, start, length, value_ptr);
1876
1877         proto_item *
1878         proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr,
1879             format, ...);
1880
1881         proto_item *
1882         proto_tree_add_ipv6_format_value(tree, id, tvb, start, length,
1883             value_ptr, format, ...);
1884
1885         proto_item *
1886         proto_tree_add_ether(tree, id, tvb, start, length, value_ptr);
1887
1888         proto_item *
1889         proto_tree_add_ether_hidden(tree, id, tvb, start, length, value_ptr);
1890
1891         proto_item *
1892         proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr,
1893             format, ...);
1894
1895         proto_item *
1896         proto_tree_add_ether_format_value(tree, id, tvb, start, length,
1897             value_ptr, format, ...);
1898
1899         proto_item *
1900         proto_tree_add_string(tree, id, tvb, start, length, value_ptr);
1901
1902         proto_item *
1903         proto_tree_add_string_hidden(tree, id, tvb, start, length, value_ptr);
1904
1905         proto_item *
1906         proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr,
1907             format, ...);
1908
1909         proto_item *
1910         proto_tree_add_string_format_value(tree, id, tvb, start, length,
1911             value_ptr, format, ...);
1912
1913         proto_item *
1914         proto_tree_add_boolean(tree, id, tvb, start, length, value);
1915
1916         proto_item *
1917         proto_tree_add_boolean_hidden(tree, id, tvb, start, length, value);
1918
1919         proto_item *
1920         proto_tree_add_boolean_format(tree, id, tvb, start, length, value,
1921             format, ...);
1922
1923         proto_item *
1924         proto_tree_add_boolean_format_value(tree, id, tvb, start, length,
1925             value, format, ...);
1926
1927         proto_item *
1928         proto_tree_add_float(tree, id, tvb, start, length, value);
1929
1930         proto_item *
1931         proto_tree_add_float_hidden(tree, id, tvb, start, length, value);
1932
1933         proto_item *
1934         proto_tree_add_float_format(tree, id, tvb, start, length, value,
1935             format, ...);
1936
1937         proto_item *
1938         proto_tree_add_float_format_value(tree, id, tvb, start, length,
1939             value, format, ...);
1940
1941         proto_item *
1942         proto_tree_add_double(tree, id, tvb, start, length, value);
1943
1944         proto_item *
1945         proto_tree_add_double_hidden(tree, id, tvb, start, length, value);
1946
1947         proto_item *
1948         proto_tree_add_double_format(tree, id, tvb, start, length, value,
1949             format, ...);
1950
1951         proto_item *
1952         proto_tree_add_double_format_value(tree, id, tvb, start, length,
1953             value, format, ...);
1954
1955         proto_item *
1956         proto_tree_add_uint(tree, id, tvb, start, length, value);
1957
1958         proto_item *
1959         proto_tree_add_uint_hidden(tree, id, tvb, start, length, value);
1960
1961         proto_item *
1962         proto_tree_add_uint_format(tree, id, tvb, start, length, value,
1963             format, ...);
1964
1965         proto_item *
1966         proto_tree_add_uint_format_value(tree, id, tvb, start, length,
1967             value, format, ...);
1968
1969         proto_item *
1970         proto_tree_add_uint64(tree, id, tvb, start, length, value);
1971
1972         proto_item *
1973         proto_tree_add_uint64_format(tree, id, tvb, start, length, value,
1974             format, ...);
1975
1976         proto_item *
1977         proto_tree_add_uint64_format_value(tree, id, tvb, start, length,
1978             value, format, ...);
1979
1980         proto_item *
1981         proto_tree_add_int(tree, id, tvb, start, length, value);
1982
1983         proto_item *
1984         proto_tree_add_int_hidden(tree, id, tvb, start, length, value);
1985
1986         proto_item *
1987         proto_tree_add_int_format(tree, id, tvb, start, length, value,
1988             format, ...);
1989
1990         proto_item *
1991         proto_tree_add_int_format_value(tree, id, tvb, start, length,
1992             value, format, ...);
1993
1994         proto_item *
1995         proto_tree_add_int64(tree, id, tvb, start, length, value);
1996
1997         proto_item *
1998         proto_tree_add_int64_format(tree, id, tvb, start, length, value,
1999             format, ...);
2000
2001         proto_item *
2002         proto_tree_add_int64_format_value(tree, id, tvb, start, length,
2003             value, format, ...);
2004
2005         proto_item*
2006         proto_tree_add_text(tree, tvb, start, length, format, ...);
2007
2008         proto_item*
2009         proto_tree_add_text_valist(tree, tvb, start, length, format, ap);
2010
2011         proto_item *
2012         proto_tree_add_guid(tree, id, tvb, start, length, value_ptr);
2013
2014         proto_item *
2015         proto_tree_add_guid_hidden(tree, id, tvb, start, length, value_ptr);
2016
2017         proto_item *
2018         proto_tree_add_guid_format(tree, id, tvb, start, length, value_ptr,
2019             format, ...);
2020
2021         proto_item *
2022         proto_tree_add_guid_format_value(tree, id, tvb, start, length,
2023             value_ptr, format, ...);
2024
2025         proto_item *
2026         proto_tree_add_oid(tree, id, tvb, start, length, value_ptr);
2027
2028         proto_item *
2029         proto_tree_add_oid_hidden(tree, id, tvb, start, length, value_ptr);
2030
2031         proto_item *
2032         proto_tree_add_oid_format(tree, id, tvb, start, length, value_ptr,
2033             format, ...);
2034
2035         proto_item *
2036         proto_tree_add_oid_format_value(tree, id, tvb, start, length,
2037             value_ptr, format, ...);
2038
2039         proto_item *
2040         proto_tree_add_bitmask(tree, tvb, start, header, ett, **fields,
2041             little_endian);
2042
2043 The 'tree' argument is the tree to which the item is to be added.  The
2044 'tvb' argument is the tvbuff from which the item's value is being
2045 extracted; the 'start' argument is the offset from the beginning of that
2046 tvbuff of the item being added, and the 'length' argument is the length,
2047 in bytes, of the item.
2048
2049 The length of some items cannot be determined until the item has been
2050 dissected; to add such an item, add it with a length of -1, and, when the
2051 dissection is complete, set the length with 'proto_item_set_len()':
2052
2053         void
2054         proto_item_set_len(ti, length);
2055
2056 The "ti" argument is the value returned by the call that added the item
2057 to the tree, and the "length" argument is the length of the item.
2058
2059 proto_tree_add_item()
2060 ---------------------
2061 proto_tree_add_item is used when you wish to do no special formatting.
2062 The item added to the GUI tree will contain the name (as passed in the
2063 proto_register_*() function) and a value.  The value will be fetched
2064 from the tvbuff by proto_tree_add_item(), based on the type of the field
2065 and, for integral and Boolean fields, the byte order of the value; the
2066 byte order is specified by the 'little_endian' argument, which is TRUE
2067 if the value is little-endian and FALSE if it is big-endian.
2068
2069 Now that definitions of fields have detailed information about bitfield
2070 fields, you can use proto_tree_add_item() with no extra processing to
2071 add bitfield values to your tree.  Here's an example.  Take the Format
2072 Identifier (FID) field in the Transmission Header (TH) portion of the SNA
2073 protocol.  The FID is the high nibble of the first byte of the TH.  The
2074 FID would be registered like this:
2075
2076         name            = "Format Identifier"
2077         abbrev          = "sna.th.fid"
2078         type            = FT_UINT8
2079         display         = BASE_HEX
2080         strings         = sna_th_fid_vals
2081         bitmask         = 0xf0
2082
2083 The bitmask contains the value which would leave only the FID if bitwise-ANDed
2084 against the parent field, the first byte of the TH.
2085
2086 The code to add the FID to the tree would be;
2087
2088         proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, TRUE);
2089
2090 The definition of the field already has the information about bitmasking
2091 and bitshifting, so it does the work of masking and shifting for us!
2092 This also means that you no longer have to create value_string structs
2093 with the values bitshifted.  The value_string for FID looks like this,
2094 even though the FID value is actually contained in the high nibble.
2095 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
2096
2097 /* Format Identifier */
2098 static const value_string sna_th_fid_vals[] = {
2099         { 0x0,  "SNA device <--> Non-SNA Device" },
2100         { 0x1,  "Subarea Node <--> Subarea Node" },
2101         { 0x2,  "Subarea Node <--> PU2" },
2102         { 0x3,  "Subarea Node or SNA host <--> Subarea Node" },
2103         { 0x4,  "?" },
2104         { 0x5,  "?" },
2105         { 0xf,  "Adjaced Subarea Nodes" },
2106         { 0,    NULL }
2107 };
2108
2109 The final implication of this is that display filters work the way you'd
2110 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
2111 Subarea Nodes. The user does not have to shift the value of the FID to
2112 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
2113 in the past.
2114
2115 proto_tree_add_item_hidden()
2116 ----------------------------
2117 proto_tree_add_item_hidden is used to add fields and values to a tree,
2118 but not show them on a GUI tree.
2119
2120 NOTE that creating hidden fields is actually quite a bad idea from a UI design
2121 perspective because the user (someone who did not write nor has ever seen the
2122 code) has no way of knowing that hidden fields are there to be filtered on
2123 thus defeating the whole purpose of putting them there.  A Better Way might
2124 be to add the fields (that might otherwise be hidden) to a subtree where they
2125 won't be seen unless the user opens the subtree--but they can be found if the
2126 user wants.
2127
2128 NOTE, too, that all of the proto_tree_add_*_hidden() APIs are deprecated:
2129 instead of using them, add the item using proto_tree_add_item() and then
2130 make it hidden using PROTO_ITEM_SET_HIDDEN().
2131
2132 One use for hidden fields (which would be better implemented using visible
2133 fields in a subtree) follows: The caller may want a value to be
2134 included in a tree so that the packet can be filtered on this field, but
2135 the representation of that field in the tree is not appropriate.  An
2136 example is the token-ring routing information field (RIF).  The best way
2137 to show the RIF in a GUI is by a sequence of ring and bridge numbers.
2138 Rings are 3-digit hex numbers, and bridges are single hex digits:
2139
2140         RIF: 001-A-013-9-C0F-B-555
2141
2142 In the case of RIF, the programmer should use a field with no value and
2143 use proto_tree_add_none_format() to build the above representation. The
2144 programmer can then add the ring and bridge values, one-by-one, with
2145 proto_tree_add_item_hidden() so that the user can then filter on or
2146 search for a particular ring or bridge. Here's a skeleton of how the
2147 programmer might code this.
2148
2149         char *rif;
2150         rif = create_rif_string(...);
2151
2152         proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
2153
2154         for(i = 0; i < num_rings; i++) {
2155                 proto_tree_add_item_hidden(tree, hf_tr_rif_ring, ..., FALSE);
2156         }
2157         for(i = 0; i < num_rings - 1; i++) {
2158                 proto_tree_add_item_hidden(tree, hf_tr_rif_bridge, ..., FALSE);
2159         }
2160
2161 The logical tree has these items:
2162
2163         hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
2164         hf_tr_rif_ring,  hidden, value=0x001
2165         hf_tr_rif_bridge, hidden, value=0xA
2166         hf_tr_rif_ring,  hidden, value=0x013
2167         hf_tr_rif_bridge, hidden, value=0x9
2168         hf_tr_rif_ring,  hidden, value=0xC0F
2169         hf_tr_rif_bridge, hidden, value=0xB
2170         hf_tr_rif_ring,  hidden, value=0x555
2171
2172 GUI or print code will not display the hidden fields, but a display
2173 filter or "packet grep" routine will still see the values. The possible
2174 filter is then possible:
2175
2176         tr.rif_ring eq 0x013
2177
2178 proto_tree_add_protocol_format()
2179 --------------------------------
2180 proto_tree_add_protocol_format is used to add the top-level item for the
2181 protocol when the dissector routine wants complete control over how the
2182 field and value will be represented on the GUI tree.  The ID value for
2183 the protocol is passed in as the "id" argument; the rest of the
2184 arguments are a "printf"-style format and any arguments for that format.
2185 The caller must include the name of the protocol in the format; it is
2186 not added automatically as in proto_tree_add_item().
2187
2188 proto_tree_add_none_format()
2189 ----------------------------
2190 proto_tree_add_none_format is used to add an item of type FT_NONE.
2191 The caller must include the name of the field in the format; it is
2192 not added automatically as in proto_tree_add_item().
2193
2194 proto_tree_add_bytes()
2195 proto_tree_add_time()
2196 proto_tree_add_ipxnet()
2197 proto_tree_add_ipv4()
2198 proto_tree_add_ipv6()
2199 proto_tree_add_ether()
2200 proto_tree_add_string()
2201 proto_tree_add_boolean()
2202 proto_tree_add_float()
2203 proto_tree_add_double()
2204 proto_tree_add_uint()
2205 proto_tree_add_uint64()
2206 proto_tree_add_int()
2207 proto_tree_add_int64()
2208 proto_tree_add_guid()
2209 proto_tree_add_oid()
2210 ------------------------
2211 These routines are used to add items to the protocol tree if either:
2212
2213         the value of the item to be added isn't just extracted from the
2214         packet data, but is computed from data in the packet;
2215
2216         the value was fetched into a variable.
2217
2218 The 'value' argument has the value to be added to the tree.
2219
2220 NOTE: in all cases where the 'value' argument is a pointer, a copy is
2221 made of the object pointed to; if you have dynamically allocated a
2222 buffer for the object, that buffer will not be freed when the protocol
2223 tree is freed - you must free the buffer yourself when you don't need it
2224 any more.
2225
2226 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
2227 sequence of bytes.
2228
2229 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
2230 "nstime_t", which is a structure containing the time to be added; it has
2231 'secs' and 'nsecs' members, giving the integral part and the fractional
2232 part of a time in units of seconds, with 'nsecs' being the number of
2233 nanoseconds.  For absolute times, "secs" is a UNIX-style seconds since
2234 January 1, 1970, 00:00:00 GMT value.
2235
2236 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
2237 network address.
2238
2239 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
2240 address, in network byte order.
2241
2242 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
2243 128-bit IPv6 address.
2244
2245 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
2246 48-bit MAC address.
2247
2248 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
2249 text string.
2250
2251 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer.
2252 It is masked and shifted as defined by the field info after which zero
2253 means "false", and non-zero means "true".
2254
2255 For proto_tree_add_float(), the 'value' argument is a 'float' in the
2256 host's floating-point format.
2257
2258 For proto_tree_add_double(), the 'value' argument is a 'double' in the
2259 host's floating-point format.
2260
2261 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
2262 integer value, in host byte order.  (This routine cannot be used to add
2263 64-bit integers.)
2264
2265 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
2266 integer value, in host byte order.
2267
2268 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
2269 integer value, in host byte order.  (This routine cannot be used to add
2270 64-bit integers.)
2271
2272 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
2273 integer value, in host byte order.
2274
2275 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
2276 e_guid_t structure.
2277
2278 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
2279 ASN.1 Object Identifier.
2280
2281 proto_tree_add_bytes_hidden()
2282 proto_tree_add_time_hidden()
2283 proto_tree_add_ipxnet_hidden()
2284 proto_tree_add_ipv4_hidden()
2285 proto_tree_add_ipv6_hidden()
2286 proto_tree_add_ether_hidden()
2287 proto_tree_add_string_hidden()
2288 proto_tree_add_boolean_hidden()
2289 proto_tree_add_float_hidden()
2290 proto_tree_add_double_hidden()
2291 proto_tree_add_uint_hidden()
2292 proto_tree_add_int_hidden()
2293 proto_tree_add_guid_hidden()
2294 proto_tree_add_oid_hidden()
2295 ----------------------------
2296 These routines add fields and values to a tree, but don't show them in the GUI
2297 tree.  They are used for the same reason that proto_tree_add_item_hidden() is
2298 used (and they should not be used for the same reasons).
2299
2300 proto_tree_add_bytes_format()
2301 proto_tree_add_time_format()
2302 proto_tree_add_ipxnet_format()
2303 proto_tree_add_ipv4_format()
2304 proto_tree_add_ipv6_format()
2305 proto_tree_add_ether_format()
2306 proto_tree_add_string_format()
2307 proto_tree_add_boolean_format()
2308 proto_tree_add_float_format()
2309 proto_tree_add_double_format()
2310 proto_tree_add_uint_format()
2311 proto_tree_add_uint64_format()
2312 proto_tree_add_int_format()
2313 proto_tree_add_int64_format()
2314 proto_tree_add_guid_format()
2315 proto_tree_add_oid_format()
2316 ----------------------------
2317 These routines are used to add items to the protocol tree when the
2318 dissector routine wants complete control over how the field and value
2319 will be represented on the GUI tree.  The argument giving the value is
2320 the same as the corresponding proto_tree_add_XXX() function; the rest of
2321 the arguments are a "printf"-style format and any arguments for that
2322 format.  The caller must include the name of the field in the format; it
2323 is not added automatically as in the proto_tree_add_XXX() functions.
2324
2325 proto_tree_add_bytes_format_value()
2326 proto_tree_add_time_format_value()
2327 proto_tree_add_ipxnet_format_value()
2328 proto_tree_add_ipv4_format_value()
2329 proto_tree_add_ipv6_format_value()
2330 proto_tree_add_ether_format_value()
2331 proto_tree_add_string_format_value()
2332 proto_tree_add_boolean_format_value()
2333 proto_tree_add_float_format_value()
2334 proto_tree_add_double_format_value()
2335 proto_tree_add_uint_format_value()
2336 proto_tree_add_uint64_format_value()
2337 proto_tree_add_int_format_value()
2338 proto_tree_add_int64_format_value()
2339 proto_tree_add_guid_format_value()
2340 proto_tree_add_oid_format_value()
2341 ------------------------------------
2342
2343 These routines are used to add items to the protocol tree when the
2344 dissector routine wants complete control over how the value will be
2345 represented on the GUI tree.  The argument giving the value is the same
2346 as the corresponding proto_tree_add_XXX() function; the rest of the
2347 arguments are a "printf"-style format and any arguments for that format.
2348 With these routines, unlike the proto_tree_add_XXX_format() routines,
2349 the name of the field is added automatically as in the
2350 proto_tree_add_XXX() functions; only the value is added with the format.
2351
2352 proto_tree_add_text()
2353 ---------------------
2354 proto_tree_add_text() is used to add a label to the GUI tree.  It will
2355 contain no value, so it is not searchable in the display filter process.
2356 This function was needed in the transition from the old-style proto_tree
2357 to this new-style proto_tree so that Wireshark would still decode all
2358 protocols w/o being able to filter on all protocols and fields.
2359 Otherwise we would have had to cripple Wireshark's functionality while we
2360 converted all the old-style proto_tree calls to the new-style proto_tree
2361 calls.  In other words, you should not use this in new code unless you've got
2362 a specific reason (see below).
2363
2364 This can also be used for items with subtrees, which may not have values
2365 themselves - the items in the subtree are the ones with values.
2366
2367 For a subtree, the label on the subtree might reflect some of the items
2368 in the subtree.  This means the label can't be set until at least some
2369 of the items in the subtree have been dissected.  To do this, use
2370 'proto_item_set_text()' or 'proto_item_append_text()':
2371
2372         void
2373         proto_item_set_text(proto_item *ti, ...);
2374
2375         void
2376         proto_item_append_text(proto_item *ti, ...);
2377
2378 'proto_item_set_text()' takes as an argument the value returned by
2379 'proto_tree_add_text()', a 'printf'-style format string, and a set of
2380 arguments corresponding to '%' format items in that string, and replaces
2381 the text for the item created by 'proto_tree_add_text()' with the result
2382 of applying the arguments to the format string.
2383
2384 'proto_item_append_text()' is similar, but it appends to the text for
2385 the item the result of applying the arguments to the format string.
2386
2387 For example, early in the dissection, one might do:
2388
2389         ti = proto_tree_add_text(tree, tvb, offset, length, <label>);
2390
2391 and later do
2392
2393         proto_item_set_text(ti, "%s: %s", type, value);
2394
2395 after the "type" and "value" fields have been extracted and dissected.
2396 <label> would be a label giving what information about the subtree is
2397 available without dissecting any of the data in the subtree.
2398
2399 Note that an exception might be thrown when trying to extract the values of
2400 the items used to set the label, if not all the bytes of the item are
2401 available.  Thus, one should create the item with text that is as
2402 meaningful as possible, and set it or append additional information to
2403 it as the values needed to supply that information are extracted.
2404
2405 proto_tree_add_text_valist()
2406 ----------------------------
2407 This is like proto_tree_add_text(), but takes, as the last argument, a
2408 'va_list'; it is used to allow routines that take a printf-like
2409 variable-length list of arguments to add a text item to the protocol
2410 tree.
2411
2412 proto_tree_add_bitmask()
2413 ------------------------
2414 This function provides an easy to use and convenient helper function
2415 to manage many types of common bitmasks that occur in protocols.
2416
2417 This function will dissect a 1/2/3/4 byte large bitmask into its individual
2418 fields.
2419 header is an integer type and must be of type FT_[U]INT{8|16|24|32} and
2420 represents the entire width of the bitmask.
2421
2422 'header' and 'ett' are the hf fields and ett field respectively to create an
2423 expansion that covers the 1-4 bytes of the bitmask.
2424
2425 '**fields' is a NULL terminated a array of pointers to hf fields representing
2426 the individual subfields of the bitmask. These fields must either be integers
2427 of the same byte width as 'header' or of the type FT_BOOLEAN.
2428 Each of the entries in '**fields' will be dissected as an item under the
2429 'header' expansion and also IF the field is a boolean and IF it is set to 1,
2430 then the name of that boolean field will be printed on the 'header' expansion
2431 line.  For integer type subfields that have a value_string defined, the
2432 matched string from that value_string will be printed on the expansion line as well.
2433
2434 Example: (from the scsi dissector)
2435         static int hf_scsi_inq_peripheral        = -1;
2436         static int hf_scsi_inq_qualifier         = -1;
2437         static gint ett_scsi_inq_peripheral = -1;
2438         ...
2439         static const int *peripheal_fields[] = {
2440                 &hf_scsi_inq_qualifier,
2441                 &hf_scsi_inq_devtype,
2442                 NULL
2443         };
2444         ...
2445         /* Qualifier and DeviceType */
2446         proto_tree_add_bitmask(tree, tvb, offset, hf_scsi_inq_peripheral, ett_scsi_inq_peripheral, peripheal_fields, FALSE);
2447         offset+=1;
2448         ...
2449         { &hf_scsi_inq_peripheral,
2450           {"Peripheral", "scsi.inquiry.preipheral", FT_UINT8, BASE_HEX,
2451            NULL, 0, NULL, HFILL}},
2452         { &hf_scsi_inq_qualifier,
2453           {"Qualifier", "scsi.inquiry.qualifier", FT_UINT8, BASE_HEX,
2454            VALS (scsi_qualifier_val), 0xE0, NULL, HFILL}},
2455         ...
2456
2457 Which provides very pretty dissection of this one byte bitmask.
2458
2459
2460 1.7 Utility routines.
2461
2462 1.7.1 match_strval and val_to_str.
2463
2464 A dissector may need to convert a value to a string, using a
2465 'value_string' structure, by hand, rather than by declaring a field with
2466 an associated 'value_string' structure; this might be used, for example,
2467 to generate a COL_INFO line for a frame.
2468
2469 'match_strval()' will do that:
2470
2471         gchar*
2472         match_strval(guint32 val, const value_string *vs)
2473
2474 It will look up the value 'val' in the 'value_string' table pointed to
2475 by 'vs', and return either the corresponding string, or NULL if the
2476 value could not be found in the table.  Note that, unless 'val' is
2477 guaranteed to be a value in the 'value_string' table ("guaranteed" as in
2478 "the code has already checked that it's one of those values" or "the
2479 table handles all possible values of the size of 'val'", not "the
2480 protocol spec says it has to be" - protocol specs do not prevent invalid
2481 packets from being put onto a network or into a purported packet capture
2482 file), you must check whether 'match_strval()' returns NULL, and arrange
2483 that its return value not be dereferenced if it's NULL. 'val_to_str()'
2484 can be used to generate a string for values not found in the table:
2485
2486         gchar*
2487         val_to_str(guint32 val, const value_string *vs, const char *fmt)
2488
2489 If the value 'val' is found in the 'value_string' table pointed to by
2490 'vs', 'val_to_str' will return the corresponding string; otherwise, it
2491 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
2492 to generate a string, and will return a pointer to that string.
2493 You can use it in a call to generate a COL_INFO line for a frame such as
2494
2495         col_add_fstr(COL_INFO, ", %s", val_to_str(val, table, "Unknown %d"));
2496
2497 1.7.2 match_strrval and rval_to_str.
2498
2499 A dissector may need to convert a range of values to a string, using a
2500 'range_string' structure.
2501
2502 'match_strrval()' will do that:
2503
2504         gchar*
2505         match_strrval(guint32 val, const range_string *rs)
2506
2507 It will look up the value 'val' in the 'range_string' table pointed to
2508 by 'rs', and return either the corresponding string, or NULL if the
2509 value could not be found in the table. Please note that its base
2510 behavior is inherited from match_strval().
2511
2512 'rval_to_str()' can be used to generate a string for values not found in
2513 the table:
2514
2515         gchar*
2516         rval_to_str(guint32 val, const range_string *rs, const char *fmt)
2517
2518 If the value 'val' is found in the 'range_string' table pointed to by
2519 'rs', 'rval_to_str' will return the corresponding string; otherwise, it
2520 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
2521 to generate a string, and will return a pointer to that string. Please
2522 note that its base behavior is inherited from match_strval().
2523
2524 1.8 Calling Other Dissectors.
2525
2526 As each dissector completes its portion of the protocol analysis, it
2527 is expected to create a new tvbuff of type TVBUFF_SUBSET which
2528 contains the payload portion of the protocol (that is, the bytes
2529 that are relevant to the next dissector).
2530
2531 The syntax for creating a new TVBUFF_SUBSET is:
2532
2533 next_tvb = tvb_new_subset(tvb, offset, length, reported_length)
2534
2535 Where:
2536         tvb is the tvbuff that the dissector has been working on. It
2537         can be a tvbuff of any type.
2538
2539         next_tvb is the new TVBUFF_SUBSET.
2540
2541         offset is the byte offset of 'tvb' at which the new tvbuff
2542         should start.  The first byte is the 0th byte.
2543
2544         length is the number of bytes in the new TVBUFF_SUBSET. A length
2545         argument of -1 says to use as many bytes as are available in
2546         'tvb'.
2547
2548         reported_length is the number of bytes that the current protocol
2549         says should be in the payload. A reported_length of -1 says that
2550         the protocol doesn't say anything about the size of its payload.
2551
2552
2553 An example from packet-ipx.c -
2554
2555 void
2556 dissect_ipx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2557 {
2558         tvbuff_t        *next_tvb;
2559         int             reported_length, available_length;
2560
2561
2562         /* Make the next tvbuff */
2563
2564 /* IPX does have a length value in the header, so calculate report_length */
2565    Set this to -1 if there isn't any length information in the protocol
2566 */
2567         reported_length = ipx_length - IPX_HEADER_LEN;
2568
2569 /* Calculate the available data in the packet,
2570    set this to -1 to use all the data in the tv_buffer
2571 */
2572         available_length = tvb_length(tvb) - IPX_HEADER_LEN;
2573
2574 /* Create the tvbuffer for the next dissector */
2575         next_tvb = tvb_new_subset(tvb, IPX_HEADER_LEN,
2576                         MIN(available_length, reported_length),
2577                         reported_length);
2578
2579 /* call the next dissector */
2580         dissector_next( next_tvb, pinfo, tree);
2581
2582
2583 1.9 Editing Makefile.common to add your dissector.
2584
2585 To arrange that your dissector will be built as part of Wireshark, you
2586 must add the name of the source file for your dissector to the
2587 'DISSECTOR_SRC' macro in the 'Makefile.common' file in the 'epan/dissectors'
2588 directory.  (Note that this is for modern versions of UNIX, so there
2589 is no 14-character limitation on file names, and for modern versions of
2590 Windows, so there is no 8.3-character limitation on file names.)
2591
2592 If your dissector also has its own header file or files, you must add
2593 them to the 'DISSECTOR_INCLUDES' macro in the 'Makefile.common' file in
2594 the 'epan/dissectors' directory, so that it's included when release source
2595 tarballs are built (otherwise, the source in the release tarballs won't
2596 compile).
2597
2598 1.10 Using the SVN source code tree.
2599
2600   See <http://www.wireshark.org/develop.html>
2601
2602 1.11 Submitting code for your new dissector.
2603
2604   - VERIFY that your dissector code does not use prohibited or deprecated APIs
2605     as follows:
2606     perl <wireshark_root>/tools/checkAPIs.pl <source-filename(s)>
2607
2608   - TEST YOUR DISSECTOR BEFORE SUBMITTING IT.
2609     Use fuzz-test.sh and/or randpkt against your dissector.  These are
2610     described at <http://wiki.wireshark.org/FuzzTesting>.
2611
2612   - Subscribe to <mailto:wireshark-dev[AT]wireshark.org> by sending an email to
2613     <mailto:wireshark-dev-request[AT]wireshark.org?body="help"> or visiting
2614     <http://www.wireshark.org/lists/>.
2615
2616   - 'svn add' all the files of your new dissector.
2617
2618   - 'svn diff' the workspace and save the result to a file.
2619
2620   - Edit the diff file - remove any changes unrelated to your new dissector,
2621     e.g. changes in config.nmake
2622
2623   - Submit a bug report to the Wireshark bug database, found at
2624     <http://bugs.wireshark.org>, qualified as an enhancement and attach your
2625     diff file there. Set the review request flag to '?' so it will pop up in
2626     the patch review list.
2627
2628   - Create a Wiki page on the protocol at <http://wiki.wireshark.org>.
2629     A template is provided so it is easy to setup in a consistent style.
2630
2631   - If possible, add sample capture files to the sample captures page at
2632     <http://wiki.wireshark.org/SampleCaptures>.  These files are used by
2633     the automated build system for fuzz testing.
2634
2635   - If you find that you are contributing a lot to wireshark on an ongoing
2636     basis you can request to become a committer which will allow you to
2637     commit files to subversion directly.
2638
2639 2. Advanced dissector topics.
2640
2641 2.1 Introduction.
2642
2643 Some of the advanced features are being worked on constantly. When using them
2644 it is wise to check the relevant header and source files for additional details.
2645
2646 2.2 Following "conversations".
2647
2648 In wireshark a conversation is defined as a series of data packets between two
2649 address:port combinations.  A conversation is not sensitive to the direction of
2650 the packet.  The same conversation will be returned for a packet bound from
2651 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
2652
2653 There are five routines that you will use to work with a conversation:
2654 conversation_new, find_conversation, conversation_add_proto_data,
2655 conversation_get_proto_data, and conversation_delete_proto_data.
2656
2657
2658 2.2.1 The conversation_init function.
2659
2660 This is an internal routine for the conversation code.  As such you
2661 will not have to call this routine.  Just be aware that this routine is
2662 called at the start of each capture and before the packets are filtered
2663 with a display filter.  The routine will destroy all stored
2664 conversations.  This routine does NOT clean up any data pointers that are
2665 passed in the conversation_new 'data' variable.  You are responsible for
2666 this clean up if you pass a malloc'ed pointer in this variable.
2667
2668 See item 2.2.8 for more information about the 'data' pointer.
2669
2670
2671 2.2.2 The conversation_new function.
2672
2673 This routine will create a new conversation based upon two address/port
2674 pairs.  If you want to associate with the conversation a pointer to a
2675 private data structure you must use the conversation_add_proto_data
2676 function.  The ptype variable is used to differentiate between
2677 conversations over different protocols, i.e. TCP and UDP.  The options
2678 variable is used to define a conversation that will accept any destination
2679 address and/or port.  Set options = 0 if the destination port and address
2680 are know when conversation_new is called.  See section 2.4 for more
2681 information on usage of the options parameter.
2682
2683 The conversation_new prototype:
2684         conversation_t *conversation_new(guint32 setup_frame, address *addr1,
2685             address *addr2, port_type ptype, guint32 port1, guint32 port2,
2686             guint options);
2687
2688 Where:
2689         guint32 setup_frame = The lowest numbered frame for this conversation
2690         address* addr1      = first data packet address
2691         address* addr2      = second data packet address
2692         port_type ptype     = port type, this is defined in packet.h
2693         guint32 port1       = first data packet port
2694         guint32 port2       = second data packet port
2695         guint options       = conversation options, NO_ADDR2 and/or NO_PORT2
2696
2697 setup_frame indicates the first frame for this conversation, and is used to
2698 distinguish multiple conversations with the same addr1/port1 and addr2/port2
2699 pair that occur within the same capture session.
2700
2701 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
2702 are the second address/port pair.  A conversation doesn't have source
2703 and destination address/port pairs - packets in a conversation go in
2704 both directions - so "addr1"/"port1" may be the source or destination
2705 address/port pair; "addr2"/"port2" would be the other pair.
2706
2707 If NO_ADDR2 is specified, the conversation is set up so that a
2708 conversation lookup will match only the "addr1" address; if NO_PORT2 is
2709 specified, the conversation is set up so that a conversation lookup will
2710 match only the "port1" port; if both are specified, i.e.
2711 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
2712 match only the "addr1"/"port1" address/port pair.  This can be used if a
2713 packet indicates that, later in the capture, a conversation will be
2714 created using certain addresses and ports, in the case where the packet
2715 doesn't specify the addresses and ports of both sides.
2716
2717 2.2.3 The find_conversation function.
2718
2719 Call this routine to look up a conversation.  If no conversation is found,
2720 the routine will return a NULL value.
2721
2722 The find_conversation prototype:
2723
2724         conversation_t *find_conversation(guint32 frame_num, address *addr_a,
2725             address *addr_b, port_type ptype, guint32 port_a, guint32 port_b,
2726             guint options);
2727
2728 Where:
2729         guint32 frame_num = a frame number to match
2730         address* addr_a = first address
2731         address* addr_b = second address
2732         port_type ptype = port type
2733         guint32 port_a  = first data packet port
2734         guint32 port_b  = second data packet port
2735         guint options   = conversation options, NO_ADDR_B and/or NO_PORT_B
2736
2737 frame_num is a frame number to match. The conversation returned is where
2738         (frame_num >= conversation->setup_frame
2739         && frame_num < conversation->next->setup_frame)
2740 Suppose there are a total of 3 conversations (A, B, and C) that match
2741 addr_a/port_a and addr_b/port_b, where the setup_frame used in
2742 conversation_new() for A, B and C are 10, 50, and 100 respectively. The
2743 frame_num passed in find_conversation is compared to the setup_frame of each
2744 conversation. So if (frame_num >= 10 && frame_num < 50), conversation A is
2745 returned. If (frame_num >= 50 && frame_num < 100), conversation B is returned.
2746 If (frame_num >= 100) conversation C is returned.
2747
2748 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
2749 "port_b" are the second address/port pair.  Again, as a conversation
2750 doesn't have source and destination address/port pairs, so
2751 "addr_a"/"port_a" may be the source or destination address/port pair;
2752 "addr_b"/"port_b" would be the other pair.  The search will match the
2753 "a" address/port pair against both the "1" and "2" address/port pairs,
2754 and match the "b" address/port pair against both the "2" and "1"
2755 address/port pairs; you don't have to worry about which side the "a" or
2756 "b" pairs correspond to.
2757
2758 If the NO_ADDR_B flag was specified to "find_conversation()", the
2759 "addr_b" address will be treated as matching any "wildcarded" address;
2760 if the NO_PORT_B flag was specified, the "port_b" port will be treated
2761 as matching any "wildcarded" port.  If both flags are specified, i.e.
2762 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
2763 any "wildcarded" address and the "port_b" port will be treated as
2764 matching any "wildcarded" port.
2765
2766
2767 2.2.4 The conversation_add_proto_data function.
2768
2769 Once you have created a conversation with conversation_new, you can
2770 associate data with it using this function.
2771
2772 The conversation_add_proto_data prototype:
2773
2774         void conversation_add_proto_data(conversation_t *conv, int proto,
2775             void *proto_data);
2776
2777 Where:
2778         conversation_t *conv = the conversation in question
2779         int proto            = registered protocol number
2780         void *data           = dissector data structure
2781
2782 "conversation" is the value returned by conversation_new.  "proto" is a
2783 unique protocol number created with proto_register_protocol.  Protocols
2784 are typically registered in the proto_register_XXXX section of your
2785 dissector.  "data" is a pointer to the data you wish to associate with the
2786 conversation.  Using the protocol number allows several dissectors to
2787 associate data with a given conversation.
2788
2789
2790 2.2.5 The conversation_get_proto_data function.
2791
2792 After you have located a conversation with find_conversation, you can use
2793 this function to retrieve any data associated with it.
2794
2795 The conversation_get_proto_data prototype:
2796
2797         void *conversation_get_proto_data(conversation_t *conv, int proto);
2798
2799 Where:
2800         conversation_t *conv = the conversation in question
2801         int proto            = registered protocol number
2802
2803 "conversation" is the conversation created with conversation_new.  "proto"
2804 is a unique protocol number created with proto_register_protocol,
2805 typically in the proto_register_XXXX portion of a dissector.  The function
2806 returns a pointer to the data requested, or NULL if no data was found.
2807
2808
2809 2.2.6 The conversation_delete_proto_data function.
2810
2811 After you are finished with a conversation, you can remove your association
2812 with this function.  Please note that ONLY the conversation entry is
2813 removed.  If you have allocated any memory for your data, you must free it
2814 as well.
2815
2816 The conversation_delete_proto_data prototype:
2817
2818         void conversation_delete_proto_data(conversation_t *conv, int proto);
2819
2820 Where:
2821         conversation_t *conv = the conversation in question
2822         int proto            = registered protocol number
2823
2824 "conversation" is the conversation created with conversation_new.  "proto"
2825 is a unique protocol number created with proto_register_protocol,
2826 typically in the proto_register_XXXX portion of a dissector.
2827
2828
2829 2.2.7 Using timestamps relative to the conversation
2830
2831 There is a framework to calculate timestamps relative to the start of the
2832 conversation. First of all the timestamp of the first packet that has been
2833 seen in the conversation must be kept in the protocol data to be able
2834 to calculate the timestamp of the current packet relative to the start
2835 of the conversation. The timestamp of the last packet that was seen in the
2836 conversation should also be kept in the protocol data. This way the
2837 delta time between the current packet and the previous packet in the
2838 conversation can be calculated.
2839
2840 So add the following items to the struct that is used for the protocol data:
2841
2842   nstime_t      ts_first;
2843   nstime_t      ts_prev;
2844
2845 The ts_prev value should only be set during the first run through the
2846 packets (ie pinfo->fd->flags.visited is false).
2847
2848 Next step is to use the per packet information (described in section 2.5)
2849 to keep the calculated delta timestamp, as it can only be calculated
2850 on the first run through the packets. This is because a packet can be
2851 selected in random order once the whole file has been read.
2852
2853 After calculating the conversation timestamps, it is time to put them in
2854 the appropriate columns with the function 'col_set_time' (described in
2855 section 1.5.9). There are two columns for conversation timestamps:
2856
2857 COL_REL_CONV_TIME,  /* Relative time to beginning of conversation */
2858 COL_DELTA_CONV_TIME,/* Delta time to last frame in conversation */
2859
2860 Last but not least, there MUST be a preference in each dissector that
2861 uses conversation timestamps that makes it possible to enable and
2862 disable the calculation of conversation timestamps. The main argument
2863 for this is that a higher level conversation is able to overwrite
2864 the values of lowel level conversations in these two columns. Being
2865 able to actively select which protocols may overwrite the conversation
2866 timestamp columns gives the user the power to control these columns.
2867 (A second reason is that conversation timestamps use the per packet
2868 data structure which uses additional memory, which should be avoided
2869 if these timestamps are not needed)
2870
2871 Have a look at the differences to packet-tcp.[ch] in SVN 22966 and
2872 SVN 23058 to see the implementation of conversation timestamps for
2873 the tcp-dissector.
2874
2875
2876 2.2.8 The example conversation code with GMemChunk's.
2877
2878 For a conversation between two IP addresses and ports you can use this as an
2879 example.  This example uses the GMemChunk to allocate memory and stores the data
2880 pointer in the conversation 'data' variable.
2881
2882 NOTE: Remember to register the init routine (my_dissector_init) in the
2883 protocol_register routine.
2884
2885
2886 /************************ Global values ************************/
2887
2888 /* the number of entries in the memory chunk array */
2889 #define my_init_count 10
2890
2891 /* define your structure here */
2892 typedef struct {
2893
2894 } my_entry_t;
2895
2896 /* the GMemChunk base structure */
2897 static GMemChunk *my_vals = NULL;
2898
2899 /* Registered protocol number */
2900 static int my_proto = -1;
2901
2902
2903 /********************* in the dissector routine *********************/
2904
2905 /* the local variables in the dissector */
2906
2907 conversation_t *conversation;
2908 my_entry_t *data_ptr;
2909
2910
2911 /* look up the conversation */
2912
2913 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2914         pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2915
2916 /* if conversation found get the data pointer that you stored */
2917 if (conversation)
2918     data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2919 else {
2920
2921     /* new conversation create local data structure */
2922
2923     data_ptr = g_mem_chunk_alloc(my_vals);
2924
2925     /*** add your code here to setup the new data structure ***/
2926
2927     /* create the conversation with your data pointer  */
2928
2929     conversation = conversation_new(pinfo->fd->num,  &pinfo->src, &pinfo->dst, pinfo->ptype,
2930             pinfo->srcport, pinfo->destport, 0);
2931     conversation_add_proto_data(conversation, my_proto, (void *)data_ptr);
2932 }
2933
2934 /* at this point the conversation data is ready */
2935
2936
2937 /******************* in the dissector init routine *******************/
2938
2939 #define my_init_count 20
2940
2941 static void
2942 my_dissector_init(void)
2943 {
2944
2945     /* destroy memory chunks if needed */
2946
2947     if (my_vals)
2948         g_mem_chunk_destroy(my_vals);
2949
2950     /* now create memory chunks */
2951
2952     my_vals = g_mem_chunk_new("my_proto_vals",
2953             sizeof(my_entry_t),
2954             my_init_count * sizeof(my_entry_t),
2955             G_ALLOC_AND_FREE);
2956 }
2957
2958 /***************** in the protocol register routine *****************/
2959
2960 /* register re-init routine */
2961
2962 register_init_routine(&my_dissector_init);
2963
2964 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2965
2966
2967 2.2.9 An example conversation code that starts at a specific frame number.
2968
2969 Sometimes a dissector has determined that a new conversation is needed that
2970 starts at a specific frame number, when a capture session encompasses multiple
2971 conversation that reuse the same src/dest ip/port pairs. You can use the
2972 conversation->setup_frame returned by find_conversation with
2973 pinfo->fd->num to determine whether or not there already exists a conversation
2974 that starts at the specific frame number.
2975
2976 /* in the dissector routine */
2977
2978         conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2979             pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2980         if (conversation == NULL || (conversation->setup_frame != pinfo->fd->num)) {
2981                 /* It's not part of any conversation or the returned
2982                  * conversation->setup_frame doesn't match the current frame
2983                  * create a new one.
2984                  */
2985                 conversation = conversation_new(pinfo->fd->num, &pinfo->src,
2986                     &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
2987                     NULL, 0);
2988         }
2989
2990
2991 2.2.10 The example conversation code using conversation index field.
2992
2993 Sometimes the conversation isn't enough to define a unique data storage
2994 value for the network traffic.  For example if you are storing information
2995 about requests carried in a conversation, the request may have an
2996 identifier that is used to  define the request. In this case the
2997 conversation and the identifier are required to find the data storage
2998 pointer.  You can use the conversation data structure index value to
2999 uniquely define the conversation.
3000
3001 See packet-afs.c for an example of how to use the conversation index.  In
3002 this dissector multiple requests are sent in the same conversation.  To store
3003 information for each request the dissector has an internal hash table based
3004 upon the conversation index and values inside the request packets.
3005
3006
3007         /* in the dissector routine */
3008
3009         /* to find a request value, first lookup conversation to get index */
3010         /* then used the conversation index, and request data to find data */
3011         /* in the local hash table */
3012
3013         conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
3014             pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
3015         if (conversation == NULL) {
3016                 /* It's not part of any conversation - create a new one. */
3017                 conversation = conversation_new(pinfo->fd->num, &pinfo->src,
3018                     &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
3019                     NULL, 0);
3020         }
3021
3022         request_key.conversation = conversation->index;
3023         request_key.service = pntohs(&rxh->serviceId);
3024         request_key.callnumber = pntohl(&rxh->callNumber);
3025
3026         request_val = (struct afs_request_val *)g_hash_table_lookup(
3027                 afs_request_hash, &request_key);
3028
3029         /* only allocate a new hash element when it's a request */
3030         opcode = 0;
3031         if (!request_val && !reply)
3032         {
3033                 new_request_key = g_mem_chunk_alloc(afs_request_keys);
3034                 *new_request_key = request_key;
3035
3036                 request_val = g_mem_chunk_alloc(afs_request_vals);
3037                 request_val -> opcode = pntohl(&afsh->opcode);
3038                 opcode = request_val->opcode;
3039
3040                 g_hash_table_insert(afs_request_hash, new_request_key,
3041                         request_val);
3042         }
3043
3044
3045
3046 2.3 Dynamic conversation dissector registration.
3047
3048
3049 NOTE:   This sections assumes that all information is available to
3050         create a complete conversation, source port/address and
3051         destination port/address.  If either the destination port or
3052         address is know, see section 2.4 Dynamic server port dissector
3053         registration.
3054
3055 For protocols that negotiate a secondary port connection, for example
3056 packet-msproxy.c, a conversation can install a dissector to handle
3057 the secondary protocol dissection.  After the conversation is created
3058 for the negotiated ports use the conversation_set_dissector to define
3059 the dissection routine.
3060 Before we create these conversations or assign a dissector to them we should
3061 first check that the conversation does not already exist and if it exists
3062 whether it is registered to our protocol or not.
3063 We should do this because it is uncommon but it does happen that multiple
3064 different protocols can use the same socketpair during different stages of
3065 an application cycle. By keeping track of the frame number a conversation
3066 was started in wireshark can still tell these different protocols apart.
3067
3068 The second argument to conversation_set_dissector is a dissector handle,
3069 which is created with a call to create_dissector_handle or
3070 register_dissector.
3071
3072 create_dissector_handle takes as arguments a pointer to the dissector
3073 function and a protocol ID as returned by proto_register_protocol;
3074 register_dissector takes as arguments a string giving a name for the
3075 dissector, a pointer to the dissector function, and a protocol ID.
3076
3077 The protocol ID is the ID for the protocol dissected by the function.
3078 The function will not be called if the protocol has been disabled by the
3079 user; instead, the data for the protocol will be dissected as raw data.
3080
3081 An example -
3082
3083 /* the handle for the dynamic dissector *
3084 static dissector_handle_t sub_dissector_handle;
3085
3086 /* prototype for the dynamic dissector */
3087 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo,
3088                 proto_tree *tree);
3089
3090 /* in the main protocol dissector, where the next dissector is setup */
3091
3092 /* if conversation has a data field, create it and load structure */
3093
3094 /* First check if a conversation already exists for this
3095         socketpair
3096 */
3097         conversation = find_conversation(pinfo->fd->num,
3098                                 &pinfo->src, &pinfo->dst, protocol,
3099                                 src_port, dst_port, new_conv_info, 0);
3100
3101 /* If there is no such conversation, or if there is one but for
3102    someone else's protocol then we just create a new conversation
3103    and assign our protocol to it.
3104 */
3105         if ( (conversation == NULL) ||
3106              (conversation->dissector_handle != sub_dissector_handle) ) {
3107             new_conv_info = g_mem_chunk_alloc(new_conv_vals);
3108             new_conv_info->data1 = value1;
3109
3110 /* create the conversation for the dynamic port */
3111             conversation = conversation_new(pinfo->fd->num,
3112                 &pinfo->src, &pinfo->dst, protocol,
3113                 src_port, dst_port, new_conv_info, 0);
3114
3115 /* set the dissector for the new conversation */
3116             conversation_set_dissector(conversation, sub_dissector_handle);
3117         }
3118                 ...
3119
3120 void
3121 proto_register_PROTOABBREV(void)
3122 {
3123         ...
3124
3125         sub_dissector_handle = create_dissector_handle(sub_dissector,
3126             proto);
3127
3128         ...
3129 }
3130
3131 2.4 Dynamic server port dissector registration.
3132
3133 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
3134 conversation with only one port and address set, this isn't a
3135 requirement.  Either the second port or the second address can be set
3136 when the conversation is created.
3137
3138 For protocols that define a server address and port for a secondary
3139 protocol, a conversation can be used to link a protocol dissector to
3140 the server port and address.  The key is to create the new
3141 conversation with the second address and port set to the "accept
3142 any" values.
3143
3144 Some server applications can use the same port for different protocols during
3145 different stages of a transaction. For example it might initially use SNMP
3146 to perform some discovery and later switch to use TFTP using the same port.
3147 In order to handle this properly we must first check whether such a
3148 conversation already exists or not and if it exists we also check whether the
3149 registered dissector_handle for that conversation is "our" dissector or not.
3150 If not we create a new conversation on top of the previous one and set this new
3151 conversation to use our protocol.
3152 Since wireshark keeps track of the frame number where a conversation started
3153 wireshark will still be able to keep the packets apart even though they do use
3154 the same socketpair.
3155                 (See packet-tftp.c and packet-snmp.c for examples of this)
3156
3157 There are two support routines that will allow the second port and/or
3158 address to be set later.
3159
3160 conversation_set_port2( conversation_t *conv, guint32 port);
3161 conversation_set_addr2( conversation_t *conv, address addr);
3162
3163 These routines will change the second address or port for the
3164 conversation.  So, the server port conversation will be converted into a
3165 more complete conversation definition.  Don't use these routines if you
3166 want to create a conversation between the server and client and retain the
3167 server port definition, you must create a new conversation.
3168
3169
3170 An example -
3171
3172 /* the handle for the dynamic dissector *
3173 static dissector_handle_t sub_dissector_handle;
3174
3175         ...
3176
3177 /* in the main protocol dissector, where the next dissector is setup */
3178
3179 /* if conversation has a data field, create it and load structure */
3180
3181         new_conv_info = g_mem_chunk_alloc(new_conv_vals);
3182         new_conv_info->data1 = value1;
3183
3184 /* create the conversation for the dynamic server address and port      */
3185 /* NOTE: The second address and port values don't matter because the    */
3186 /* NO_ADDR2 and NO_PORT2 options are set.                               */
3187
3188 /* First check if a conversation already exists for this
3189         IP/protocol/port
3190 */
3191         conversation = find_conversation(pinfo->fd->num,
3192                                 &server_src_addr, 0, protocol,
3193                                 server_src_port, 0, NO_ADDR2 | NO_PORT_B);
3194 /* If there is no such conversation, or if there is one but for
3195    someone else's protocol then we just create a new conversation
3196    and assign our protocol to it.
3197 */
3198         if ( (conversation == NULL) ||
3199              (conversation->dissector_handle != sub_dissector_handle) ) {
3200             conversation = conversation_new(pinfo->fd->num,
3201             &server_src_addr, 0, protocol,
3202             server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
3203
3204 /* set the dissector for the new conversation */
3205             conversation_set_dissector(conversation, sub_dissector_handle);
3206         }
3207
3208 2.5 Per packet information.
3209
3210 Information can be stored for each data packet that is processed by the
3211 dissector.  The information is added with the p_add_proto_data function and
3212 retrieved with the p_get_proto_data function.  The data pointers passed into
3213 the p_add_proto_data are not managed by the proto_data routines. If you use
3214 malloc or any other dynamic memory allocation scheme, you must release the
3215 data when it isn't required.
3216
3217 void
3218 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
3219 void *
3220 p_get_proto_data(frame_data *fd, int proto)
3221
3222 Where:
3223         fd         - The fd pointer in the pinfo structure, pinfo->fd
3224         proto      - Protocol id returned by the proto_register_protocol call
3225                      during initialization
3226         proto_data - pointer to the dissector data.
3227
3228
3229 2.6 User Preferences.
3230
3231 If the dissector has user options, there is support for adding these preferences
3232 to a configuration dialog.
3233
3234 You must register the module with the preferences routine with -
3235
3236 module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
3237
3238 Where: proto_id   - the value returned by "proto_register_protocol()" when
3239                     the protocol was registered
3240        apply_cb   - Callback routine that is call when preferences are applied
3241
3242
3243 Then you can register the fields that can be configured by the user with these
3244 routines -
3245
3246         /* Register a preference with an unsigned integral value. */
3247         void prefs_register_uint_preference(module_t *module, const char *name,
3248             const char *title, const char *description, guint base, guint *var);
3249
3250         /* Register a preference with an Boolean value. */
3251         void prefs_register_bool_preference(module_t *module, const char *name,
3252             const char *title, const char *description, gboolean *var);
3253
3254         /* Register a preference with an enumerated value. */
3255         void prefs_register_enum_preference(module_t *module, const char *name,
3256             const char *title, const char *description, gint *var,
3257             const enum_val_t *enumvals, gboolean radio_buttons)
3258
3259         /* Register a preference with a character-string value. */
3260         void prefs_register_string_preference(module_t *module, const char *name,
3261             const char *title, const char *description, char **var)
3262
3263         /* Register a preference with a range of unsigned integers (e.g.,
3264          * "1-20,30-40").
3265          */
3266         void prefs_register_range_preference(module_t *module, const char *name,
3267             const char *title, const char *description, range_t *var,
3268             guint32 max_value)
3269
3270 Where: module - Returned by the prefs_register_protocol routine
3271          name     - This is appended to the name of the protocol, with a
3272                     "." between them, to construct a name that identifies
3273                     the field in the preference file; the name itself
3274                     should not include the protocol name, as the name in
3275                     the preference file will already have it
3276          title    - Field title in the preferences dialog
3277          description - Comments added to the preference file above the
3278                        preference value
3279          var      - pointer to the storage location that is updated when the
3280                     field is changed in the preference dialog box
3281          base     - Base that the unsigned integer is expected to be in,
3282                     see strtoul(3).
3283          enumvals - an array of enum_val_t structures.  This must be
3284                     NULL-terminated; the members of that structure are:
3285
3286                         a short name, to be used with the "-o" flag - it
3287                         should not contain spaces or upper-case letters,
3288                         so that it's easier to put in a command line;
3289
3290                         a description, which is used in the GUI (and
3291                         which, for compatibility reasons, is currently
3292                         what's written to the preferences file) - it can
3293                         contain spaces, capital letters, punctuation,
3294                         etc.;
3295
3296                         the numerical value corresponding to that name
3297                         and description
3298          radio_buttons - TRUE if the field is to be displayed in the
3299                          preferences dialog as a set of radio buttons,
3300                          FALSE if it is to be displayed as an option
3301                          menu
3302          max_value - The maximum allowed value for a range (0 is the minimum).
3303
3304 An example from packet-beep.c -
3305
3306   proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
3307                                        "BEEP", "beep");
3308
3309         ...
3310
3311   /* Register our configuration options for BEEP, particularly our port */
3312
3313   beep_module = prefs_register_protocol(proto_beep, proto_reg_handoff_beep);
3314
3315   prefs_register_uint_preference(beep_module, "tcp.port", "BEEP TCP Port",
3316                                  "Set the port for BEEP messages (if other"
3317                                  " than the default of 10288)",
3318                                  10, &global_beep_tcp_port);
3319
3320   prefs_register_bool_preference(beep_module, "strict_header_terminator",
3321                                  "BEEP Header Requires CRLF",
3322                                  "Specifies that BEEP requires CRLF as a "
3323                                  "terminator, and not just CR or LF",
3324                                  &global_beep_strict_term);
3325
3326 This will create preferences "beep.tcp.port" and
3327 "beep.strict_header_terminator", the first of which is an unsigned
3328 integer and the second of which is a Boolean.
3329
3330 Note that a warning will pop up if you've saved such preference to the
3331 preference file and you subsequently take the code out. The way to make
3332 a preference obsolete is to register it as such:
3333
3334 /* Register a preference that used to be supported but no longer is. */
3335         void prefs_register_obsolete_preference(module_t *module,
3336             const char *name);
3337
3338 2.7 Reassembly/desegmentation for protocols running atop TCP.
3339
3340 There are two main ways of reassembling a Protocol Data Unit (PDU) which
3341 spans across multiple TCP segments.  The first approach is simpler, but
3342 assumes you are running atop of TCP when this occurs (but your dissector
3343 might run atop of UDP, too, for example), and that your PDUs consist of a
3344 fixed amount of data that includes enough information to determine the PDU
3345 length, possibly followed by additional data.  The second method is more
3346 generic but requires more code and is less efficient.
3347
3348 2.7.1 Using tcp_dissect_pdus().
3349
3350 For the first method, you register two different dissection methods, one
3351 for the TCP case, and one for the other cases.  It is a good idea to
3352 also have a dissect_PROTO_common function which will parse the generic
3353 content that you can find in all PDUs which is called from
3354 dissect_PROTO_tcp when the reassembly is complete and from
3355 dissect_PROTO_udp (or dissect_PROTO_other).
3356
3357 To register the distinct dissector functions, consider the following
3358 example, stolen from packet-dns.c:
3359
3360         dissector_handle_t dns_udp_handle;
3361         dissector_handle_t dns_tcp_handle;
3362         dissector_handle_t mdns_udp_handle;
3363
3364         dns_udp_handle = create_dissector_handle(dissect_dns_udp,
3365             proto_dns);
3366         dns_tcp_handle = create_dissector_handle(dissect_dns_tcp,
3367             proto_dns);
3368         mdns_udp_handle = create_dissector_handle(dissect_mdns_udp,
3369             proto_dns);
3370
3371         dissector_add("udp.port", UDP_PORT_DNS, dns_udp_handle);
3372         dissector_add("tcp.port", TCP_PORT_DNS, dns_tcp_handle);
3373         dissector_add("udp.port", UDP_PORT_MDNS, mdns_udp_handle);
3374         dissector_add("tcp.port", TCP_PORT_MDNS, dns_tcp_handle);
3375
3376 The dissect_dns_udp function does very little work and calls
3377 dissect_dns_common, while dissect_dns_tcp calls tcp_dissect_pdus with a
3378 reference to a callback which will be called with reassembled data:
3379
3380         static void
3381         dissect_dns_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3382         {
3383                 tcp_dissect_pdus(tvb, pinfo, tree, dns_desegment, 2,
3384                     get_dns_pdu_len, dissect_dns_tcp_pdu);
3385         }
3386
3387 (The dissect_dns_tcp_pdu function acts similarly to dissect_dns_udp.)
3388 The arguments to tcp_dissect_pdus are:
3389
3390         the tvbuff pointer, packet_info pointer, and proto_tree pointer
3391         passed to the dissector;
3392
3393         a gboolean flag indicating whether desegmentation is enabled for
3394         your protocol;
3395
3396         the number of bytes of PDU data required to determine the length
3397         of the PDU;
3398
3399         a routine that takes as arguments a packet_info pointer, a tvbuff
3400         pointer and an offset value representing the offset into the tvbuff
3401         at which a PDU begins and should return - *without* throwing an
3402         exception (it is guaranteed that the number of bytes specified by the
3403         previous argument to tcp_dissect_pdus is available, but more data
3404         might not be available, so don't refer to any data past that) - the
3405         total length of the PDU, in bytes;
3406
3407         a routine that's passed a tvbuff pointer, packet_info pointer,
3408         and proto_tree pointer, with the tvbuff containing a
3409         possibly-reassembled PDU, and that should dissect that PDU.
3410
3411 2.7.2 Modifying the pinfo struct.
3412
3413 The second reassembly mode is preferred when the dissector cannot determine
3414 how many bytes it will need to read in order to determine the size of a PDU.
3415 It may also be useful if your dissector needs to support reassembly from
3416 protocols other than TCP.
3417
3418 Your dissect_PROTO will initially be passed a tvbuff containing the payload of
3419 the first packet. It should dissect as much data as it can, noting that it may
3420 contain more than one complete PDU. If the end of the provided tvbuff coincides
3421 with the end of a PDU then all is well and your dissector can just return as
3422 normal. (If it is a new-style dissector, it should return the number of bytes
3423 successfully processed.)
3424
3425 If the dissector discovers that the end of the tvbuff does /not/ coincide with
3426 the end of a PDU, (ie, there is half of a PDU at the end of the tvbuff), it can
3427 indicate this to the parent dissector, by updating the pinfo struct. The
3428 desegment_offset field is the offset in the tvbuff at which the dissector will
3429 continue processing when next called.  The desegment_len field should contain
3430 the estimated number of additional bytes required for completing the PDU.  Next
3431 time your dissect_PROTO is called, it will be passed a tvbuff composed of the
3432 end of the data from the previous tvbuff together with desegment_len more bytes.
3433
3434 If the dissector cannot tell how many more bytes it will need, it should set
3435 desegment_len=DESEGMENT_ONE_MORE_SEGMENT; it will then be called again as soon
3436 as any more data becomes available. Dissectors should set the desegment_len to a
3437 reasonable value when possible rather than always setting
3438 DESEGMENT_ONE_MORE_SEGMENT as it will generally be more efficient. Also, you
3439 *must not* set desegment_len=1 in this case, in the hope that you can change
3440 your mind later: once you return a positive value from desegment_len, your PDU
3441 boundary is set in stone.
3442
3443 static hf_register_info hf[] = {
3444     {&hf_cstring,
3445      {"C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0,
3446       "C String", HFILL}
3447      }
3448    };
3449
3450 /**
3451 *   Dissect a buffer containing C strings.
3452 *
3453 *   @param  tvb     The buffer to dissect.
3454 *   @param  pinfo   Packet Info.
3455 *   @param  tree    The protocol tree.
3456 **/
3457 static void dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
3458 {
3459     guint offset = 0;
3460     while(offset < tvb_reported_length(tvb)) {
3461         gint available = tvb_reported_length_remaining(tvb, offset);
3462         gint len = tvb_strnlen(tvb, offset, available);
3463
3464         if( -1 == len ) {
3465             /* we ran out of data: ask for more */
3466             pinfo->desegment_offset = offset;
3467             pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
3468             return;
3469         }
3470
3471         if (check_col(pinfo->cinfo, COL_INFO)) {
3472             col_set_str(pinfo->cinfo, COL_INFO, "C String");
3473         }
3474
3475         len += 1; /* Add one for the '\0' */
3476
3477         if (tree) {
3478             proto_tree_add_item(tree, hf_cstring, tvb, offset, len, FALSE);
3479         }
3480         offset += (guint)len;
3481     }
3482
3483     /* if we get here, then the end of the tvb coincided with the end of a
3484        string. Happy days. */
3485 }
3486
3487 This simple dissector will repeatedly return DESEGMENT_ONE_MORE_SEGMENT
3488 requesting more data until the tvbuff contains a complete C string. The C string
3489 will then be added to the protocol tree. Note that there may be more
3490 than one complete C string in the tvbuff, so the dissection is done in a
3491 loop.
3492
3493 2.8 ptvcursors.
3494
3495 The ptvcursor API allows a simpler approach to writing dissectors for
3496 simple protocols. The ptvcursor API works best for protocols whose fields
3497 are static and whose format does not depend on the value of other fields.
3498 However, even if only a portion of your protocol is statically defined,
3499 then that portion could make use of ptvcursors.
3500
3501 The ptvcursor API lets you extract data from a tvbuff, and add it to a
3502 protocol tree in one step. It also keeps track of the position in the
3503 tvbuff so that you can extract data again without having to compute any
3504 offsets --- hence the "cursor" name of the API.
3505
3506 The three steps for a simple protocol are:
3507     1. Create a new ptvcursor with ptvcursor_new()
3508     2. Add fields with multiple calls of ptvcursor_add()
3509     3. Delete the ptvcursor with ptvcursor_free()
3510
3511 ptvcursor offers the possibility to add subtrees in the tree as well. It can be
3512 done in very simple steps :
3513     1. Create a new subtree with ptvcursor_push_subtree(). The old subtree is
3514        pushed in a stack and the new subtree will be used by ptvcursor.
3515     2. Add fields with multiple calls of ptvcursor_add(). The fields will be
3516        added in the new subtree created at the previous step.
3517     3. Pop the previous subtree with ptvcursor_pop_subtree(). The previous
3518        subtree is again used by ptvcursor.
3519 Note that at the end of the parsing of a packet you must have popped each
3520 subtree you pushed. If it's not the case, the dissector will generate an error.
3521
3522 To use the ptvcursor API, include the "ptvcursor.h" file. The PGM dissector
3523 is an example of how to use it. You don't need to look at it as a guide;
3524 instead, the API description here should be good enough.
3525
3526 2.8.1 ptvcursor API.
3527
3528 ptvcursor_t*
3529 ptvcursor_new(proto_tree* tree, tvbuff_t* tvb, gint offset)
3530     This creates a new ptvcursor_t object for iterating over a tvbuff.
3531 You must call this and use this ptvcursor_t object so you can use the
3532 ptvcursor API.
3533
3534 proto_item*
3535 ptvcursor_add(ptvcursor_t* ptvc, int hf, gint length, gboolean endianness)
3536     This will extract 'length' bytes from the tvbuff and place it in
3537 the proto_tree as field 'hf', which is a registered header_field. The
3538 pointer to the proto_item that is created is passed back to you. Internally,
3539 the ptvcursor advances its cursor so the next call to ptvcursor_add
3540 starts where this call finished. The 'endianness' parameter matters for
3541 FT_UINT* and FT_INT* fields.
3542
3543 proto_item*
3544 ptvcursor_add_no_advance(ptvcursor_t* ptvc, int hf, gint length, gboolean endianness)
3545     Like ptvcursor_add, but does not advance the internal cursor.
3546
3547 void
3548 ptvcursor_advance(ptvcursor_t* ptvc, gint length)
3549     Advances the internal cursor without adding anything to the proto_tree.
3550
3551 void
3552 ptvcursor_free(ptvcursor_t* ptvc)
3553     Frees the memory associated with the ptvcursor. You must call this
3554 after your dissection with the ptvcursor API is completed.
3555
3556
3557 proto_tree*
3558 ptvcursor_push_subtree(ptvcursor_t* ptvc, proto_item* it, gint ett_subtree)
3559     Pushes the current subtree in the tree stack of the cursor, creates a new
3560 one and sets this one as the working tree.
3561
3562 void
3563 ptvcursor_pop_subtree(ptvcursor_t* ptvc);
3564     Pops a subtree in the tree stack of the cursor
3565
3566 proto_tree*
3567 ptvcursor_add_with_subtree(ptvcursor_t* ptvc, int hfindex, gint length,
3568                             gboolean little_endian, gint ett_subtree);
3569     Adds an item to the tree and creates a subtree.
3570 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3571 In this case, at the next pop, the item length will be equal to the advancement
3572 of the cursor since the creation of the subtree.
3573
3574 proto_tree*
3575 ptvcursor_add_text_with_subtree(ptvcursor_t* ptvc, gint length,
3576                                 gint ett_subtree, const char* format, ...);
3577     Add a text node to the tree and create a subtree.
3578 If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
3579 In this case, at the next pop, the item length will be equal to the advancement
3580 of the cursor since the creation of the subtree.
3581
3582 2.8.2 Miscellaneous functions.
3583
3584 tvbuff_t*
3585 ptvcursor_tvbuff(ptvcursor_t* ptvc)
3586     Returns the tvbuff associated with the ptvcursor.
3587
3588 gint
3589 ptvcursor_current_offset(ptvcursor_t* ptvc)
3590     Returns the current offset.
3591
3592 proto_tree*
3593 ptvcursor_tree(ptvcursor_t* ptvc)
3594     Returns the proto_tree associated with the ptvcursor.
3595
3596 void
3597 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree)
3598     Sets a new proto_tree for the ptvcursor.
3599
3600 proto_tree*
3601 ptvcursor_set_subtree(ptvcursor_t* ptvc, proto_item* it, gint ett_subtree);
3602     Creates a subtree and adds it to the cursor as the working tree but does
3603 not save the old working tree.