5 This file is a HOWTO for Wireshark developers. It describes how to start coding
6 a Wireshark protocol dissector and the use some of the important functions and
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.
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
19 How to setup such an environment is platform dependant, 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
24 0.1. General README files.
26 You'll find additional information in the following README files:
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
41 0.2. Dissector related README files.
43 You'll find additional dissector related information in the following README
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
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>
62 1. Setting up your protocol dissector code.
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.
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.
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).
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];
90 Don't use zero-length arrays; not all compilers support them. If an
91 array would have no members, just leave it out.
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.
97 Don't use "inline"; not all compilers support it. If you want to have a
98 function be an inline function if the compiler supports it, use
99 G_INLINE_FUNC, which is declared by <glib.h>. This may not work with
100 functions declared in header files; if it doesn't work, don't declare
101 the function in a header file, even if this requires that you not make
102 it inline on any platform.
104 Don't use "uchar", "u_char", "ushort", "u_short", "uint", "u_int",
105 "ulong", "u_long" or "boolean"; they aren't defined on all platforms.
106 If you want an 8-bit unsigned quantity, use "guint8"; if you want an
107 8-bit character value with the 8th bit not interpreted as a sign bit,
108 use "guchar"; if you want a 16-bit unsigned quantity, use "guint16";
109 if you want a 32-bit unsigned quantity, use "guint32"; and if you want
110 an "int-sized" unsigned quantity, use "guint"; if you want a boolean,
111 use "gboolean". Use "%d", "%u", "%x", and "%o" to print those types;
112 don't use "%ld", "%lu", "%lx", or "%lo", as longs are 64 bits long on
113 many platforms, but "guint32" is 32 bits long.
115 Don't use "long" to mean "signed 32-bit integer", and don't use
116 "unsigned long" to mean "unsigned 32-bit integer"; "long"s are 64 bits
117 long on many platforms. Use "gint32" for signed 32-bit integers and use
118 "guint32" for unsigned 32-bit integers.
120 Don't use "long" to mean "signed 64-bit integer" and don't use "unsigned
121 long" to mean "unsigned 64-bit integer"; "long"s are 32 bits long on
122 other many platforms. Don't use "long long" or "unsigned long long",
123 either, as not all platforms support them; use "gint64" or "guint64",
124 which will be defined as the appropriate types for 64-bit signed and
127 When printing or displaying the values of 64-bit integral data types,
128 don't assume use "%lld", "%llu", "%llx", or "%llo" - not all platforms
129 support "%ll" for printing 64-bit integral data types. Instead, use
130 PRId64, PRIu64, PRIx64, and PRIo64, for example
132 proto_tree_add_text(tree, tvb, offset, 8,
133 "Sequence Number: %" PRIu64, sequence_number);
135 When specifying an integral constant that doesn't fit in 32 bits, don't
136 use "LL" at the end of the constant - not all compilers use "LL" for
137 that. Instead, put the constant in a call to the "G_GINT64_CONSTANT()"
140 G_GINT64_CONSTANT(11644473600U)
146 Don't use a label without a statement following it. For example,
156 will not work with all compilers - you have to do
166 with some statement, even if it's a null statement, after the label.
168 Don't use "bzero()", "bcopy()", or "bcmp()"; instead, use the ANSI C
171 "memset()" (with zero as the second argument, so that it sets
172 all the bytes to zero);
174 "memcpy()" or "memmove()" (note that the first and second
175 arguments to "memcpy()" are in the reverse order to the
176 arguments to "bcopy()"; note also that "bcopy()" is typically
177 guaranteed to work on overlapping memory regions, while
178 "memcpy()" isn't, so if you may be copying from one region to a
179 region that overlaps it, use "memmove()", not "memcpy()" - but
180 "memcpy()" might be faster as a result of not guaranteeing
181 correct operation on overlapping memory regions);
183 and "memcmp()" (note that "memcmp()" returns 0, 1, or -1, doing
184 an ordered comparison, rather than just returning 0 for "equal"
185 and 1 for "not equal", as "bcmp()" does).
187 Not all platforms necessarily have "bzero()"/"bcopy()"/"bcmp()", and
188 those that do might not declare them in the header file on which they're
189 declared on your platform.
191 Don't use "index()" or "rindex()"; instead, use the ANSI C equivalents,
192 "strchr()" and "strrchr()". Not all platforms necessarily have
193 "index()" or "rindex()", and those that do might not declare them in the
194 header file on which they're declared on your platform.
196 Don't fetch data from packets by getting a pointer to data in the packet
197 with "tvb_get_ptr()", casting that pointer to a pointer to a structure,
198 and dereferencing that pointer. That point won't necessarily be aligned
199 on the proper boundary, which can cause crashes on some platforms (even
200 if it doesn't crash on an x86-based PC); furthermore, the data in a
201 packet is not necessarily in the byte order of the machine on which
202 Wireshark is running. Use the tvbuff routines to extract individual
203 items from the packet, or use "proto_tree_add_item()" and let it extract
206 Don't use structures that overlay packet data, or into which you copy
207 packet data; the C programming language does not guarantee any
208 particular alignment of fields within a structure, and even the
209 extensions that try to guarantee that are compiler-specific and not
210 necessarily supported by all compilers used to build Wireshark. Using
211 bitfields in those structures are even worse; the order of bitfields
214 Don't use "ntohs()", "ntohl()", "htons()", or "htonl()"; the header
215 files required to define or declare them differ between platforms, and
216 you might be able to get away with not including the appropriate header
217 file on your platform but that might not work on other platforms.
218 Instead, use "g_ntohs()", "g_ntohl()", "g_htons()", and "g_htonl()";
219 those are declared by <glib.h>, and you'll need to include that anyway,
220 as Wireshark header files that all dissectors must include use stuff from
223 Don't fetch a little-endian value using "tvb_get_ntohs() or
224 "tvb_get_ntohl()" and then using "g_ntohs()", "g_htons()", "g_ntohl()",
225 or "g_htonl()" on the resulting value - the g_ routines in question
226 convert between network byte order (big-endian) and *host* byte order,
227 not *little-endian* byte order; not all machines on which Wireshark runs
228 are little-endian, even though PCs are. Fetch those values using
229 "tvb_get_letohs()" and "tvb_get_letohl()".
231 Don't put a comma after the last element of an enum - some compilers may
232 either warn about it (producing extra noise) or refuse to accept it.
234 Don't include <unistd.h> without protecting it with
242 and, if you're including it to get routines such as "open()", "close()",
243 "read()", and "write()" declared, also include <io.h> if present:
249 in order to declare the Windows C library routines "_open()",
250 "_close()", "_read()", and "_write()". Your file must include <glib.h>
251 - which many of the Wireshark header files include, so you might not have
252 to include it explicitly - in order to get "open()", "close()",
253 "read()", "write()", etc. mapped to "_open()", "_close()", "_read()",
256 When opening a file with "fopen()", "freopen()", or "fdopen()", if the
257 file contains ASCII text, use "r", "w", "a", and so on as the open mode
258 - but if it contains binary data, use "rb", "wb", and so on. On
259 Windows, if a file is opened in a text mode, writing a byte with the
260 value of octal 12 (newline) to the file causes two bytes, one with the
261 value octal 15 (carriage return) and one with the value octal 12, to be
262 written to the file, and causes bytes with the value octal 15 to be
263 discarded when reading the file (to translate between C's UNIX-style
264 lines that end with newline and Windows' DEC-style lines that end with
265 carriage return/line feed).
267 In addition, that also means that when opening or creating a binary
268 file, you must use "open()" (with O_CREAT and possibly O_TRUNC if the
269 file is to be created if it doesn't exist), and OR in the O_BINARY flag.
270 That flag is not present on most, if not all, UNIX systems, so you must
277 to properly define it for UNIX (it's not necessary on UNIX).
279 Don't use forward declarations of static arrays without a specified size
280 in a fashion such as this:
282 static const value_string foo_vals[];
286 static const value_string foo_vals[] = {
293 as some compilers will reject the first of those statements. Instead,
294 initialize the array at the point at which it's first declared, so that
297 Don't put a comma after the last tuple of an initializer of an array.
299 For #define names and enum member names, prefix the names with a tag so
300 as to avoid collisions with other names - this might be more of an issue
301 on Windows, as it appears to #define names such as DELETE and
304 Don't use the "numbered argument" feature that many UNIX printf's
307 g_snprintf(add_string, 30, " - (%1$d) (0x%1$04x)", value);
309 as not all UNIX printf's implement it, and Windows printf doesn't appear
310 to implement it. Use something like
312 g_snprintf(add_string, 30, " - (%d) (0x%04x)", value, value);
316 Don't use "variadic macros", such as
318 #define DBG(format, args...) fprintf(stderr, format, ## args)
320 as not all C compilers support them. Use macros that take a fixed
321 number of arguments, such as
323 #define DBG0(format) fprintf(stderr, format)
324 #define DBG1(format, arg1) fprintf(stderr, format, arg1)
325 #define DBG2(format, arg1, arg2) fprintf(stderr, format, arg1, arg2)
331 #define DBG(args) printf args
333 snprintf() -> g_snprintf()
334 snprintf() is not available on all platforms, so it's a good idea to use the
335 g_snprintf() function declared by <glib.h> instead.
337 tmpnam() -> mkstemp()
338 tmpnam is insecure and should not be used any more. Wireshark brings its
339 own mkstemp implementation for use on platforms that lack mkstemp.
340 Note: mkstemp does not accept NULL as a parameter.
342 The pointer returned by a call to "tvb_get_ptr()" is not guaranteed to be
343 aligned on any particular byte boundary; this means that you cannot
344 safely cast it to any data type other than a pointer to "char",
345 "unsigned char", "guint8", or other one-byte data types. You cannot,
346 for example, safely cast it to a pointer to a structure, and then access
347 the structure members directly; on some systems, unaligned accesses to
348 integral data types larger than 1 byte, and floating-point data types,
349 cause a trap, which will, at best, result in the OS slowly performing an
350 unaligned access for you, and will, on at least some platforms, cause
351 the program to be terminated.
353 Wireshark supports both platforms with GLib 1.2[.x]/GTK+ 1.2[.x] and GLib
354 2.x/GTK+ 1.3[.x] and 2.x. If at all possible, either use only
355 mechanisms that are present in GLib 1.2[.x] and GTK+ 1.2[.x], use #if's
356 to conditionally use older or newer mechanisms depending on the platform
357 on which Wireshark is being built, or, if the code in GLib or GTK+ that
358 implements that mechanism will build with GLib 1.2[.x]/GTK+ 1.2[.x],
359 conditionally include that code as part of the Wireshark source and use
360 the included version with GLib 1.2[.x] or GTK+ 1.2[.x]. In particular,
361 if the GLib 2.x or GTK+ 2.x mechanism indicates that a routine is
362 deprecated and shouldn't be used in new code, and that it was renamed in
363 GLib 2.x or GTK+ 2.x and the new name should be used, disregard that and
364 use the old name - it'll still work with GLib 2.x or GTK+ 2.x, but will
365 also work with GLib 1.2[.x] and GTK+ 1.2[.x].
367 When different code must be used on UN*X and Win32, use a #if or #ifdef
368 that tests _WIN32, not WIN32. Try to write code portably whenever
369 possible, however; note that there are some routines in Wireshark with
370 platform-dependent implementations and platform-independent APIs, such
371 as the routines in epan/filesystem.c, allowing the code that calls it to
372 be written portably without #ifdefs.
374 1.1.2 String handling
376 Do not use functions such as strcat() or strcpy().
377 A lot of work has been done to remove the existing calls to these functions and
378 we do not want any new callers of these functions.
380 Instead use g_snprintf() since that function will if used correctly prevent
381 buffer overflows for large strings.
383 When using a buffer to create a string, do not use a buffer stored on the stack.
384 I.e. do not use a buffer declared as
386 instead allocate a buffer dynamically using the emem routines (see
387 README.malloc) such as
390 #define MAX_BUFFER 1024
391 buffer=ep_alloc(MAX_BUFFER);
394 g_snprintf(buffer, MAX_BUFFER, ...
396 This avoids the stack from being corrupted in case there is a bug in your code
397 that accidentally writes beyond the end of the buffer.
400 If you write a routine that will create and return a pointer to a filled in
401 string and if that buffer will not be further processed or appended to after
402 the routine returns (except being added to the proto tree),
403 do not preallocate the buffer to fill in and pass as a parameter instead
404 pass a pointer to a pointer to the function and return a pointer to an
405 emem allocated buffer that will be automatically freed. (see README.malloc)
407 I.e. do not write code such as
409 foo_to_str(char *string, ... ){
415 foo_to_str(buffer, ...
416 proto_tree_add_text(... buffer ...
418 instead write the code as
420 foo_to_str(char **buffer, ...
428 foo_to_str(&buffer, ...
429 proto_tree_add_text(... *buffer ...
431 Use ep_ allocated buffers. They are very fast and nice. These buffers are all
432 automatically free()d when the dissection of the current packet ends so you
433 don't have to worry about free()ing them explicitly in order to not leak memory.
434 Please read README.malloc.
439 Wireshark is not guaranteed to read only network traces that contain correctly-
440 formed packets. Wireshark is commonly used is to track down networking
441 problems, and the problems might be due to a buggy protocol implementation
442 sending out bad packets.
444 Therefore, protocol dissectors not only have to be able to handle
445 correctly-formed packets without, for example, crashing or looping
446 infinitely, they also have to be able to handle *incorrectly*-formed
447 packets without crashing or looping infinitely.
449 Here are some suggestions for making dissectors more robust in the face
450 of incorrectly-formed packets:
452 Do *NOT* use "g_assert()" or "g_assert_not_reached()" in dissectors.
453 *NO* value in a packet's data should be considered "wrong" in the sense
454 that it's a problem with the dissector if found; if it cannot do
455 anything else with a particular value from a packet's data, the
456 dissector should put into the protocol tree an indication that the
457 value is invalid, and should return.
459 If you are allocating a chunk of memory to contain data from a packet,
460 or to contain information derived from data in a packet, and the size of
461 the chunk of memory is derived from a size field in the packet, make
462 sure all the data is present in the packet before allocating the buffer.
465 1) Wireshark won't leak that chunk of memory if an attempt to
466 fetch data not present in the packet throws an exception
470 2) it won't crash trying to allocate an absurdly-large chunk of
471 memory if the size field has a bogus large value.
473 If you're fetching into such a chunk of memory a string from the buffer,
474 and the string has a specified size, you can use "tvb_get_*_string()",
475 which will check whether the entire string is present before allocating
476 a buffer for the string, and will also put a trailing '\0' at the end of
479 If you're fetching into such a chunk of memory a 2-byte Unicode string
480 from the buffer, and the string has a specified size, you can use
481 "tvb_get_ephemeral_faked_unicode()", which will check whether the entire
482 string is present before allocating a buffer for the string, and will also
483 put a trailing '\0' at the end of the buffer. The resulting string will be
484 a sequence of single-byte characters; the only Unicode characters that
485 will be handled correctly are those in the ASCII range. (Wireshark's
486 ability to handle non-ASCII strings is limited; it needs to be
489 If you're fetching into such a chunk of memory a sequence of bytes from
490 the buffer, and the sequence has a specified size, you can use
491 "tvb_memdup()", which will check whether the entire sequence is present
492 before allocating a buffer for it.
494 Otherwise, you can check whether the data is present by using
495 "tvb_ensure_bytes_exist()" or by getting a pointer to the data by using
496 "tvb_get_ptr()", although note that there might be problems with using
497 the pointer from "tvb_get_ptr()" (see the item on this in the
498 Portability section above, and the next item below).
500 Note also that you should only fetch string data into a fixed-length
501 buffer if the code ensures that no more bytes than will fit into the
502 buffer are fetched ("the protocol ensures" isn't good enough, as
503 protocol specifications can't ensure only packets that conform to the
504 specification will be transmitted or that only packets for the protocol
505 in question will be interpreted as packets for that protocol by
506 Wireshark). If there's no maximum length of string data to be fetched,
507 routines such as "tvb_get_*_string()" are safer, as they allocate a buffer
508 large enough to hold the string. (Note that some variants of this call
509 require you to free the string once you're finished with it.)
511 If you have gotten a pointer using "tvb_get_ptr()", you must make sure
512 that you do not refer to any data past the length passed as the last
513 argument to "tvb_get_ptr()"; while the various "tvb_get" routines
514 perform bounds checking and throw an exception if you refer to data not
515 available in the tvbuff, direct references through a pointer gotten from
516 "tvb_get_ptr()" do not do any bounds checking.
518 If you have a loop that dissects a sequence of items, each of which has
519 a length field, with the offset in the tvbuff advanced by the length of
520 the item, then, if the length field is the total length of the item, and
521 thus can be zero, you *MUST* check for a zero-length item and abort the
522 loop if you see one. Otherwise, a zero-length item could cause the
523 dissector to loop infinitely. You should also check that the offset,
524 after having the length added to it, is greater than the offset before
525 the length was added to it, if the length field is greater than 24 bits
526 long, so that, if the length value is *very* large and adding it to the
527 offset causes an overflow, that overflow is detected.
529 If you are fetching a length field from the buffer, corresponding to the
530 length of a portion of the packet, and subtracting from that length a
531 value corresponding to the length of, for example, a header in the
532 packet portion in question, *ALWAYS* check that the value of the length
533 field is greater than or equal to the length you're subtracting from it,
534 and report an error in the packet and stop dissecting the packet if it's
535 less than the length you're subtracting from it. Otherwise, the
536 resulting length value will be negative, which will either cause errors
537 in the dissector or routines called by the dissector, or, if the value
538 is interpreted as an unsigned integer, will cause the value to be
539 interpreted as a very large positive value.
541 Any tvbuff offset that is added to as processing is done on a packet
542 should be stored in a 32-bit variable, such as an "int"; if you store it
543 in an 8-bit or 16-bit variable, you run the risk of the variable
546 sprintf() -> g_snprintf()
547 Prevent yourself from using the sprintf() function, as it does not test the
548 length of the given output buffer and might be writing into memory areas not
549 intended for. This function is one of the main causes of security problems
550 like buffer exploits and many other bugs that are very hard to find. It's
551 much better to use the g_snprintf() function declared by <glib.h> instead.
553 You should test your dissector against incorrectly-formed packets. This
554 can be done using the randpkt and editcap utilities that come with the
555 Wireshark distribution. Testing using randpkt can be done by generating
556 output at the same layer as your protocol, and forcing Wireshark/TShark
557 to decode it as your protocol, e.g. if your protocol sits on top of UDP:
559 randpkt -c 50000 -t dns randpkt.pcap
560 tshark -nVr randpkt.pcap -d udp.port==53,<myproto>
562 Testing using editcap can be done using preexisting capture files and the
563 "-E" flag, which introduces errors in a capture file. E.g.:
565 editcap -E 0.03 infile.pcap outfile.pcap
566 tshark -nVr outfile.pcap
568 The script fuzz-test.sh is available to help automate these tests.
570 1.1.4 Name convention.
572 Wireshark uses the underscore_convention rather than the InterCapConvention for
573 function names, so new code should probably use underscores rather than
574 intercaps for functions and variable names. This is especially important if you
575 are writing code that will be called from outside your code. We are just
576 trying to keep things consistent for other users.
578 1.1.5 White space convention.
580 Avoid using tab expansions different from 8 column widths, as not all
581 text editors in use by the developers support this. For a detailed
582 discussion of tabs, spaces, and indentation, see
584 http://www.jwz.org/doc/tabs-vs-spaces.html
586 When creating a new file, you are free to choose an indentation logic.
587 Most of the files in Wireshark tend to use 2-space or 4-space
588 indentation. You are encouraged to write a short comment on the
589 indentation logic at the beginning of this new file, especially if
590 you're using non-mod-8 tabs. The tabs-vs-spaces document above provides
591 examples of Emacs and vi modelines for this purpose.
593 When editing an existing file, try following the existing indentation
594 logic and even if it very tempting, never ever use a restyler/reindenter
595 utility on an existing file. If you run across wildly varying
596 indentation styles within the same file, it might be helpful to send a
597 note to wireshark-dev for guidance.
601 Wireshark requires certain things when setting up a protocol dissector.
602 Below is skeleton code for a dissector that you can copy to a file and
603 fill in. Your dissector should follow the naming convention of packet-
604 followed by the abbreviated name for the protocol. It is recommended
605 that where possible you keep to the IANA abbreviated name for the
606 protocol, if there is one, or a commonly-used abbreviation for the
609 Usually, you will put your newly created dissector file into the directory
610 epan/dissectors, just like all the other packet-....c files already in there.
612 Also, please add your dissector file to the corresponding makefile,
613 described in section "1.9 Editing Makefile.common to add your dissector" below.
615 Dissectors that use the dissector registration to register with a lower level
616 dissector don't need to define a prototype in the .h file. For other
617 dissectors the main dissector routine should have a prototype in a header
618 file whose name is "packet-", followed by the abbreviated name for the
619 protocol, followed by ".h"; any dissector file that calls your dissector
620 should be changed to include that file.
622 You may not need to include all the headers listed in the skeleton
623 below, and you may need to include additional headers. For example, the
632 is needed only if you are using a function from libpcre, e.g. the
633 "pcre_compile()" function.
635 The "$Id$" in the comment will be updated by Subversion when the file is
638 When creating a new file, it is fine to just write "$Id$" as Subversion will
639 automatically fill in the identifier at the time the file will be added to the
640 SVN repository (committed).
642 ------------------------------------Cut here------------------------------------
643 /* packet-PROTOABBREV.c
644 * Routines for PROTONAME dissection
645 * Copyright 200x, YOUR_NAME <YOUR_EMAIL_ADDRESS>
649 * Wireshark - Network traffic analyzer
650 * By Gerald Combs <gerald@wireshark.org>
651 * Copyright 1998 Gerald Combs
653 * Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
654 * is a dissector file; if you just copied this from README.developer,
655 * don't bother with the "Copied from" - you don't even need to put
656 * in a "Copied from" if you copied an existing dissector, especially
657 * if the bulk of the code in the new dissector is your code)
659 * This program is free software; you can redistribute it and/or
660 * modify it under the terms of the GNU General Public License
661 * as published by the Free Software Foundation; either version 2
662 * of the License, or (at your option) any later version.
664 * This program is distributed in the hope that it will be useful,
665 * but WITHOUT ANY WARRANTY; without even the implied warranty of
666 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
667 * GNU General Public License for more details.
669 * You should have received a copy of the GNU General Public License
670 * along with this program; if not, write to the Free Software
671 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
684 #include <epan/packet.h>
685 #include <epan/prefs.h>
687 /* IF PROTO exposes code to other dissectors, then it must be exported
688 in a header file. If not, a header file is not needed at all. */
689 #include "packet-PROTOABBREV.h"
691 /* Forward declaration we need below */
692 void proto_reg_handoff_PROTOABBREV(void);
694 /* Initialize the protocol and registered fields */
695 static int proto_PROTOABBREV = -1;
696 static int hf_PROTOABBREV_FIELDABBREV = -1;
698 /* Global sample preference ("controls" display of numbers) */
699 static gboolean gPREF_HEX = FALSE;
701 /* Initialize the subtree pointers */
702 static gint ett_PROTOABBREV = -1;
704 /* Code to actually dissect the packets */
706 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
709 /* Set up structures needed to add the protocol subtree and manage it */
711 proto_tree *PROTOABBREV_tree;
713 /* First, if at all possible, do some heuristics to check if the packet cannot
714 * possibly belong to your protocol. This is especially important for
715 * protocols directly on top of TCP or UDP where port collisions are
716 * common place (e.g., even though your protocol uses a well known port,
717 * someone else may set up, for example, a web server on that port which,
718 * if someone analyzed that web server's traffic in Wireshark, would result
719 * in Wireshark handing an HTTP packet to your dissector). For example:
722 /* Get some values from the packet header, probably using tvb_get_*() */
723 if ( /* these values are not possible in PROTONAME */ )
724 /* This packet does not appear to belong to PROTONAME.
725 * Return 0 to give another dissector a chance to dissect it.
729 /* Make entries in Protocol column and Info column on summary display */
730 if (check_col(pinfo->cinfo, COL_PROTOCOL))
731 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
733 /* This field shows up as the "Info" column in the display; you should use
734 it, if possible, to summarize what's in the packet, so that a user looking
735 at the list of packets can tell what type of packet it is. See section 1.5
736 for more information.
738 Before changing the contents of a column you should make sure the column is
739 active by calling "check_col(pinfo->cinfo, COL_*)". If it is not active
740 don't bother setting it.
742 If you are setting the column to a constant string, use "col_set_str()",
743 as it's more efficient than the other "col_set_XXX()" calls.
745 If you're setting it to a string you've constructed, or will be
746 appending to the column later, use "col_add_str()".
748 "col_add_fstr()" can be used instead of "col_add_str()"; it takes
749 "printf()"-like arguments. Don't use "col_add_fstr()" with a format
750 string of "%s" - just use "col_add_str()" or "col_set_str()", as it's
751 more efficient than "col_add_fstr()".
753 If you will be fetching any data from the packet before filling in
754 the Info column, clear that column first, in case the calls to fetch
755 data from the packet throw an exception because they're fetching data
756 past the end of the packet, so that the Info column doesn't have data
757 left over from the previous dissector; do
759 if (check_col(pinfo->cinfo, COL_INFO))
760 col_clear(pinfo->cinfo, COL_INFO);
764 if (check_col(pinfo->cinfo, COL_INFO))
765 col_set_str(pinfo->cinfo, COL_INFO, "XXX Request");
767 /* A protocol dissector can be called in 2 different ways:
769 (a) Operational dissection
771 In this mode, Wireshark is only interested in the way protocols
772 interact, protocol conversations are created, packets are
773 reassembled and handed over to higher-level protocol dissectors.
774 In this mode Wireshark does not build a so-called "protocol
777 (b) Detailed dissection
779 In this mode, Wireshark is also interested in all details of
780 a given protocol, so a "protocol tree" is created.
782 Wireshark distinguishes between the 2 modes with the proto_tree pointer:
786 In the interest of speed, if "tree" is NULL, avoid building a
787 protocol tree and adding stuff to it, or even looking at any packet
788 data needed only if you're building the protocol tree, if possible.
790 Note, however, that you must fill in column information, create
791 conversations, reassemble packets, build any other persistent state
792 needed for dissection, and call subdissectors regardless of whether
793 "tree" is NULL or not. This might be inconvenient to do without
794 doing most of the dissection work; the routines for adding items to
795 the protocol tree can be passed a null protocol tree pointer, in
796 which case they'll return a null item pointer, and
797 "proto_item_add_subtree()" returns a null tree pointer if passed a
798 null item pointer, so, if you're careful not to dereference any null
799 tree or item pointers, you can accomplish this by doing all the
800 dissection work. This might not be as efficient as skipping that
801 work if you're not building a protocol tree, but if the code would
802 have a lot of tests whether "tree" is null if you skipped that work,
803 you might still be better off just doing all that work regardless of
804 whether "tree" is null or not. */
807 /* NOTE: The offset and length values in the call to
808 "proto_tree_add_item()" define what data bytes to highlight in the hex
809 display window when the line in the protocol tree display
810 corresponding to that item is selected.
812 Supplying a length of -1 is the way to highlight all data from the
813 offset to the end of the packet. */
815 /* create display subtree for the protocol */
816 ti = proto_tree_add_item(tree, proto_PROTOABBREV, tvb, 0, -1, FALSE);
818 PROTOABBREV_tree = proto_item_add_subtree(ti, ett_PROTOABBREV);
820 /* add an item to the subtree, see section 1.6 for more information */
821 proto_tree_add_item(PROTOABBREV_tree,
822 hf_PROTOABBREV_FIELDABBREV, tvb, offset, len, FALSE)
825 /* Continue adding tree items to process the packet here */
830 /* If this protocol has a sub-dissector call it here, see section 1.8 */
832 /* Return the amount of data this dissector was able to dissect */
833 return tvb_length(tvb);
837 /* Register the protocol with Wireshark */
839 /* this format is require because a script is used to build the C function
840 that calls all the protocol registration.
844 proto_register_PROTOABBREV(void)
846 module_t *PROTOABBREV_module;
848 /* Setup list of header fields See Section 1.6.1 for details*/
849 static hf_register_info hf[] = {
850 { &hf_PROTOABBREV_FIELDABBREV,
851 { "FIELDNAME", "PROTOABBREV.FIELDABBREV",
852 FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK,
853 "FIELDDESCR", HFILL }
857 /* Setup protocol subtree array */
858 static gint *ett[] = {
862 /* Register the protocol name and description */
863 proto_PROTOABBREV = proto_register_protocol("PROTONAME",
864 "PROTOSHORTNAME", "PROTOABBREV");
866 /* Required function calls to register the header fields and subtrees used */
867 proto_register_field_array(proto_PROTOABBREV, hf, array_length(hf));
868 proto_register_subtree_array(ett, array_length(ett));
870 /* Register preferences module (See Section 2.6 for more on preferences) */
871 PROTOABBREV_module = prefs_register_protocol(proto_PROTOABBREV,
872 proto_reg_handoff_PROTOABBREV);
874 /* Register a sample preference */
875 prefs_register_bool_preference(PROTOABBREV_module, "showHex",
876 "Display numbers in Hex",
877 "Enable to display numerical values in hexadecimal.",
882 /* If this dissector uses sub-dissector registration add a registration routine.
883 This exact format is required because a script is used to find these
884 routines and create the code that calls these routines.
886 This function is also called by preferences whenever "Apply" is pressed
887 (see prefs_register_protocol above) so it should accommodate being called
891 proto_reg_handoff_PROTOABBREV(void)
893 static gboolean inited = FALSE;
897 dissector_handle_t PROTOABBREV_handle;
899 /* Use new_create_dissector_handle() to indicate that dissect_PROTOABBREV()
900 * returns the number of bytes it dissected (or 0 if it thinks the packet
901 * does not belong to PROTONAME).
903 PROTOABBREV_handle = new_create_dissector_handle(dissect_PROTOABBREV,
905 dissector_add("PARENT_SUBFIELD", ID_VALUE, PROTOABBREV_handle);
911 If you perform registration functions which are dependant upon
912 prefs the you should de-register everything which was associated
913 with the previous settings and re-register using the new prefs
914 settings here. In general this means you need to keep track of what
915 value the preference had at the time you registered using a local
916 static in this function. ie.
918 static int currentPort = -1;
920 if (currentPort != -1) {
921 dissector_delete("tcp.port", currentPort, PROTOABBREV_handle);
924 currentPort = gPortPref;
926 dissector_add("tcp.port", currentPort, PROTOABBREV_handle);
931 ------------------------------------Cut here------------------------------------
933 1.3 Explanation of needed substitutions in code skeleton.
935 In the above code block the following strings should be substituted with
938 YOUR_NAME Your name, of course. You do want credit, don't you?
939 It's the only payment you will receive....
940 YOUR_EMAIL_ADDRESS Keep those cards and letters coming.
941 WHATEVER_FILE_YOU_USED Add this line if you are using another file as a
943 PROTONAME The name of the protocol; this is displayed in the
944 top-level protocol tree item for that protocol.
945 PROTOSHORTNAME An abbreviated name for the protocol; this is displayed
946 in the "Preferences" dialog box if your dissector has
947 any preferences, in the dialog box of enabled protocols,
948 and in the dialog box for filter fields when constructing
950 PROTOABBREV A name for the protocol for use in filter expressions;
951 it shall contain only lower-case letters, digits, and
953 FIELDNAME The displayed name for the header field.
954 FIELDABBREV The abbreviated name for the header field. (NO SPACES)
955 FIELDTYPE FT_NONE, FT_BOOLEAN, FT_UINT8, FT_UINT16, FT_UINT24,
956 FT_UINT32, FT_UINT64, FT_INT8, FT_INT16, FT_INT24, FT_INT32,
957 FT_INT64, FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME,
958 FT_RELATIVE_TIME, FT_STRING, FT_STRINGZ, FT_UINT_STRING,
959 FT_ETHER, FT_BYTES, FT_IPv4, FT_IPv6, FT_IPXNET,
960 FT_FRAMENUM, FT_PROTOCOL, FT_GUID, FT_OID
961 FIELDBASE BASE_NONE, BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC
962 FIELDCONVERT VALS(x), TFS(x), NULL
963 BITMASK Usually 0x0 unless using the TFS(x) field conversion.
964 FIELDDESCR A brief description of the field.
965 PARENT_SUBFIELD Lower level protocol field used for lookup, i.e. "tcp.port"
966 ID_VALUE Lower level protocol field value that identifies this protocol
967 For example the TCP or UDP port number
969 If, for example, PROTONAME is "Internet Bogosity Discovery Protocol",
970 PROTOSHORTNAME would be "IBDP", and PROTOABBREV would be "ibdp". Try to
971 conform with IANA names.
973 1.4 The dissector and the data it receives.
978 This is only needed if the dissector doesn't use self-registration to
979 register itself with the lower level dissector, or if the protocol dissector
980 wants/needs to expose code to other subdissectors.
982 The dissector must declared as exactly as follows in the file
983 packet-PROTOABBREV.h:
986 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
989 1.4.2 Extracting data from packets.
991 NOTE: See the file /epan/tvbuff.h for more details.
993 The "tvb" argument to a dissector points to a buffer containing the raw
994 data to be analyzed by the dissector; for example, for a protocol
995 running atop UDP, it contains the UDP payload (but not the UDP header,
996 or any protocol headers above it). A tvbuffer is a opaque data
997 structure, the internal data structures are hidden and the data must be
998 access via the tvbuffer accessors.
1002 Single-byte accessor:
1004 guint8 tvb_get_guint8(tvbuff_t*, gint offset);
1006 Network-to-host-order accessors for 16-bit integers (guint16), 24-bit
1007 integers, 32-bit integers (guint32), and 64-bit integers (guint64):
1009 guint16 tvb_get_ntohs(tvbuff_t*, gint offset);
1010 guint32 tvb_get_ntoh24(tvbuff_t*, gint offset);
1011 guint32 tvb_get_ntohl(tvbuff_t*, gint offset);
1012 guint64 tvb_get_ntoh64(tvbuff_t*, gint offset);
1014 Network-to-host-order accessors for single-precision and
1015 double-precision IEEE floating-point numbers:
1017 gfloat tvb_get_ntohieee_float(tvbuff_t*, gint offset);
1018 gdouble tvb_get_ntohieee_double(tvbuff_t*, gint offset);
1020 Little-Endian-to-host-order accessors for 16-bit integers (guint16),
1021 24-bit integers, 32-bit integers (guint32), and 64-bit integers
1024 guint16 tvb_get_letohs(tvbuff_t*, gint offset);
1025 guint32 tvb_get_letoh24(tvbuff_t*, gint offset);
1026 guint32 tvb_get_letohl(tvbuff_t*, gint offset);
1027 guint64 tvb_get_letoh64(tvbuff_t*, gint offset);
1029 Little-Endian-to-host-order accessors for single-precision and
1030 double-precision IEEE floating-point numbers:
1032 gfloat tvb_get_letohieee_float(tvbuff_t*, gint offset);
1033 gdouble tvb_get_letohieee_double(tvbuff_t*, gint offset);
1035 Accessors for IPv4 and IPv6 addresses:
1037 guint32 tvb_get_ipv4(tvbuff_t*, gint offset);
1038 void tvb_get_ipv6(tvbuff_t*, gint offset, struct e_in6_addr *addr);
1040 NOTE: IPv4 addresses are not to be converted to host byte order before
1041 being passed to "proto_tree_add_ipv4()". You should use "tvb_get_ipv4()"
1042 to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't,
1043 for example, try to use "tvb_get_ntohl()", find that it gives you the
1044 wrong answer on the PC on which you're doing development, and try
1045 "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong
1046 answer on big-endian machines.
1050 void tvb_get_ntohguid(tvbuff_t *, gint offset, e_guid_t *guid);
1051 void tvb_get_letohguid(tvbuff_t *, gint offset, e_guid_t *guid);
1055 guint8 *tvb_get_string(tvbuff_t*, gint offset, gint length);
1056 guint8 *tvb_get_ephemeral_string(tvbuff_t*, gint offset, gint length);
1058 Returns a null-terminated buffer containing data from the specified
1059 tvbuff, starting at the specified offset, and containing the specified
1060 length worth of characters (the length of the buffer will be length+1,
1061 as it includes a null character to terminate the string).
1063 tvb_get_string() returns a buffer allocated by g_malloc() so you must
1064 g_free() it when you are finished with the string. Failure to g_free() this
1065 buffer will lead to memory leaks.
1066 tvb_get_ephemeral_string() returns a buffer allocated from a special heap
1067 with a lifetime until the next packet is dissected. You do not need to
1068 free() this buffer, it will happen automatically once the next packet is
1072 guint8 *tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp);
1073 guint8 *tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp);
1075 Returns a null-terminated buffer, allocated with "g_malloc()",
1076 containing data from the specified tvbuff, starting with at the
1077 specified offset, and containing all characters from the tvbuff up to
1078 and including a terminating null character in the tvbuff. "*lengthp"
1079 will be set to the length of the string, including the terminating null.
1081 tvb_get_stringz() returns a buffer allocated by g_malloc() so you must
1082 g_free() it when you are finished with the string. Failure to g_free() this
1083 buffer will lead to memory leaks.
1084 tvb_get_ephemeral_stringz() returns a buffer allocated from a special heap
1085 with a lifetime until the next packet is dissected. You do not need to
1086 free() this buffer, it will happen automatically once the next packet is
1090 guint8 *tvb_fake_unicode(tvbuff_t*, gint offset, gint length);
1091 guint8 *tvb_get_ephemeral_faked_unicode(tvbuff_t*, gint offset, gint length);
1093 Converts a 2-byte unicode string to an ASCII string.
1094 Returns a null-terminated buffer containing data from the specified
1095 tvbuff, starting at the specified offset, and containing the specified
1096 length worth of characters (the length of the buffer will be length+1,
1097 as it includes a null character to terminate the string).
1099 tvb_fake_unicode() returns a buffer allocated by g_malloc() so you must
1100 g_free() it when you are finished with the string. Failure to g_free() this
1101 buffer will lead to memory leaks.
1102 tvb_get_ephemeral_faked_unicode() returns a buffer allocated from a special
1103 heap with a lifetime until the next packet is dissected. You do not need to
1104 free() this buffer, it will happen automatically once the next packet is
1109 guint8* tvb_memcpy(tvbuff_t*, guint8* target, gint offset, gint length);
1111 Copies into the specified target the specified length's worth of data
1112 from the specified tvbuff, starting at the specified offset.
1114 guint8* tvb_memdup(tvbuff_t*, gint offset, gint length);
1115 guint8* ep_tvb_memdup(tvbuff_t*, gint offset, gint length);
1117 Returns a buffer, allocated with "g_malloc()", containing the specified
1118 length's worth of data from the specified tvbuff, starting at the
1119 specified offset. The ephemeral variant is freed automatically after the
1120 packet is dissected.
1123 /* WARNING! This function is possibly expensive, temporarily allocating
1124 * another copy of the packet data. Furthermore, it's dangerous because once
1125 * this pointer is given to the user, there's no guarantee that the user will
1126 * honor the 'length' and not overstep the boundaries of the buffer.
1128 guint8* tvb_get_ptr(tvbuff_t*, gint offset, gint length);
1130 The reason that tvb_get_ptr() might have to allocate a copy of its data
1131 only occurs with TVBUFF_COMPOSITES, data that spans multiple tvbuffers.
1132 If the user request a pointer to a range of bytes that spans the member
1133 tvbuffs that make up the TVBUFF_COMPOSITE, the data will have to be
1134 copied to another memory region to assure that all the bytes are
1139 1.5 Functions to handle columns in the traffic summary window.
1141 The topmost pane of the main window is a list of the packets in the
1142 capture, possibly filtered by a display filter.
1144 Each line corresponds to a packet, and has one or more columns, as
1145 configured by the user.
1147 Many of the columns are handled by code outside individual dissectors;
1148 most dissectors need only specify the value to put in the "Protocol" and
1151 Columns are specified by COL_ values; the COL_ value for the "Protocol"
1152 field, typically giving an abbreviated name for the protocol (but not
1153 the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the
1154 COL_ value for the "Info" field, giving a summary of the contents of the
1155 packet for that protocol, is COL_INFO.
1157 A value for a column should only be added if the user specified that it
1158 be displayed; to check whether a given column is to be displayed, call
1159 'check_col' with the COL_ value for that field as an argument - it will
1160 return TRUE if the column is to be displayed and FALSE if it is not to
1163 The value for a column can be specified with one of several functions,
1164 all of which take the 'fd' argument to the dissector as their first
1165 argument, and the COL_ value for the column as their second argument.
1167 1.5.1 The col_set_str function.
1169 'col_set_str' takes a string as its third argument, and sets the value
1170 for the column to that value. It assumes that the pointer passed to it
1171 points to a string constant or a static "const" array, not to a
1172 variable, as it doesn't copy the string, it merely saves the pointer
1173 value; the argument can itself be a variable, as long as it always
1174 points to a string constant or a static "const" array.
1176 It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if
1177 the dissector will be using 'col_append_str' or 'col_append_fstr" to
1178 append more information to the column, the string will have to be copied
1179 anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in
1182 For example, to set the "Protocol" column
1185 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1186 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
1189 1.5.2 The col_add_str function.
1191 'col_add_str' takes a string as its third argument, and sets the value
1192 for the column to that value. It takes the same arguments as
1193 'col_set_str', but copies the string, so that if the string is, for
1194 example, an automatic variable that won't remain in scope when the
1195 dissector returns, it's safe to use.
1198 1.5.3 The col_add_fstr function.
1200 'col_add_fstr' takes a 'printf'-style format string as its third
1201 argument, and 'printf'-style arguments corresponding to '%' format
1202 items in that string as its subsequent arguments. For example, to set
1203 the "Info" field to "<XXX> request, <N> bytes", where "reqtype" is a
1204 string containing the type of the request in the packet and "n" is an
1205 unsigned integer containing the number of bytes in the request:
1207 if (check_col(pinfo->cinfo, COL_INFO))
1208 col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
1211 Don't use 'col_add_fstr' with a format argument of just "%s" -
1212 'col_add_str', or possibly even 'col_set_str' if the string that matches
1213 the "%s" is a static constant string, will do the same job more
1217 1.5.4 The col_clear function.
1219 If the Info column will be filled with information from the packet, that
1220 means that some data will be fetched from the packet before the Info
1221 column is filled in. If the packet is so small that the data in
1222 question cannot be fetched, the routines to fetch the data will throw an
1223 exception (see the comment at the beginning about tvbuffers improving
1224 the handling of short packets - the tvbuffers keep track of how much
1225 data is in the packet, and throw an exception on an attempt to fetch
1226 data past the end of the packet, so that the dissector won't process
1227 bogus data), causing the Info column not to be filled in.
1229 This means that the Info column will have data for the previous
1230 protocol, which would be confusing if, for example, the Protocol column
1231 had data for this protocol.
1233 Therefore, before a dissector fetches any data whatsoever from the
1234 packet (unless it's a heuristic dissector fetching data to determine
1235 whether the packet is one that it should dissect, in which case it
1236 should check, before fetching the data, whether there's any data to
1237 fetch; if there isn't, it should return FALSE), it should set the
1238 Protocol column and the Info column.
1240 If the Protocol column will ultimately be set to, for example, a value
1241 containing a protocol version number, with the version number being a
1242 field in the packet, the dissector should, before fetching the version
1243 number field or any other field from the packet, set it to a value
1244 without a version number, using 'col_set_str', and should later set it
1245 to a value with the version number after it's fetched the version
1248 If the Info column will ultimately be set to a value containing
1249 information from the packet, the dissector should, before fetching any
1250 fields from the packet, clear the column using 'col_clear' (which is
1251 more efficient than clearing it by calling 'col_set_str' or
1252 'col_add_str' with a null string), and should later set it to the real
1253 string after it's fetched the data to use when doing that.
1256 1.5.5 The col_append_str function.
1258 Sometimes the value of a column, especially the "Info" column, can't be
1259 conveniently constructed at a single point in the dissection process;
1260 for example, it might contain small bits of information from many of the
1261 fields in the packet. 'col_append_str' takes, as arguments, the same
1262 arguments as 'col_add_str', but the string is appended to the end of the
1263 current value for the column, rather than replacing the value for that
1264 column. (Note that no blank separates the appended string from the
1265 string to which it is appended; if you want a blank there, you must add
1266 it yourself as part of the string being appended.)
1269 1.5.6 The col_append_fstr function.
1271 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to
1272 'col_add_str' - it takes, as arguments, the same arguments as
1273 'col_add_fstr', but the formatted string is appended to the end of the
1274 current value for the column, rather than replacing the value for that
1277 1.5.7 The col_append_sep_str and col_append_sep_fstr functions.
1279 In specific situations the developer knows that a column's value will be
1280 created in a stepwise manner, where the appended values are listed. Both
1281 'col_append_sep_str' and 'col_append_sep_fstr' functions will add an item
1282 separator between two consecutive items, and will not add the separator at the
1283 beginning of the column. The remainder of the work both functions do is
1284 identical to what 'col_append_str' and 'col_append_fstr' do.
1286 1.6 Constructing the protocol tree.
1288 The middle pane of the main window, and the topmost pane of a packet
1289 popup window, are constructed from the "protocol tree" for a packet.
1291 The protocol tree, or proto_tree, is a GNode, the N-way tree structure
1292 available within GLIB. Of course the protocol dissectors don't care
1293 what a proto_tree really is; they just pass the proto_tree pointer as an
1294 argument to the routines which allow them to add items and new branches
1297 When a packet is selected in the packet-list pane, or a packet popup
1298 window is created, a new logical protocol tree (proto_tree) is created.
1299 The pointer to the proto_tree (in this case, 'protocol tree'), is passed
1300 to the top-level protocol dissector, and then to all subsequent protocol
1301 dissectors for that packet, and then the GUI tree is drawn via
1304 The logical proto_tree needs to know detailed information about the
1305 protocols and fields about which information will be collected from the
1306 dissection routines. By strictly defining (or "typing") the data that can
1307 be attached to a proto tree, searching and filtering becomes possible.
1308 This means that the for every protocol and field (which I also call
1309 "header fields", since they are fields in the protocol headers) which
1310 might be attached to a tree, some information is needed.
1312 Every dissector routine will need to register its protocols and fields
1313 with the central protocol routines (in proto.c). At first I thought I
1314 might keep all the protocol and field information about all the
1315 dissectors in one file, but decentralization seemed like a better idea.
1316 That one file would have gotten very large; one small change would have
1317 required a re-compilation of the entire file. Also, by allowing
1318 registration of protocols and fields at run-time, loadable modules of
1319 protocol dissectors (perhaps even user-supplied) is feasible.
1321 To do this, each protocol should have a register routine, which will be
1322 called when Wireshark starts. The code to call the register routines is
1323 generated automatically; to arrange that a protocol's register routine
1324 be called at startup:
1326 the file containing a dissector's "register" routine must be
1327 added to "DISSECTOR_SRC" in "epan/dissectors/Makefile.common";
1329 the "register" routine must have a name of the form
1330 "proto_register_XXX";
1332 the "register" routine must take no argument, and return no
1335 the "register" routine's name must appear in the source file
1336 either at the beginning of the line, or preceded only by "void "
1337 at the beginning of the line (that would typically be the
1338 definition) - other white space shouldn't cause a problem, e.g.:
1340 void proto_register_XXX(void) {
1349 proto_register_XXX( void )
1356 and so on should work.
1358 For every protocol or field that a dissector wants to register, a variable of
1359 type int needs to be used to keep track of the protocol. The IDs are
1360 needed for establishing parent/child relationships between protocols and
1361 fields, as well as associating data with a particular field so that it
1362 can be stored in the logical tree and displayed in the GUI protocol
1365 Some dissectors will need to create branches within their tree to help
1366 organize header fields. These branches should be registered as header
1367 fields. Only true protocols should be registered as protocols. This is
1368 so that a display filter user interface knows how to distinguish
1369 protocols from fields.
1371 A protocol is registered with the name of the protocol and its
1374 Here is how the frame "protocol" is registered.
1378 proto_frame = proto_register_protocol (
1380 /* short name */ "Frame",
1381 /* abbrev */ "frame" );
1383 A header field is also registered with its name and abbreviation, but
1384 information about the its data type is needed. It helps to look at
1385 the header_field_info struct to see what information is expected:
1387 struct header_field_info {
1396 int id; /* calculated */
1398 int bitshift; /* calculated */
1403 A string representing the name of the field. This is the name
1404 that will appear in the graphical protocol tree.
1408 A string with an abbreviation of the field. We concatenate the
1409 abbreviation of the parent protocol with an abbreviation for the field,
1410 using a period as a separator. For example, the "src" field in an IP packet
1411 would have "ip.src" as an abbreviation. It is acceptable to have
1412 multiple levels of periods if, for example, you have fields in your
1413 protocol that are then subdivided into subfields. For example, TRMAC
1414 has multiple error fields, so the abbreviations follow this pattern:
1415 "trmac.errors.iso", "trmac.errors.noniso", etc.
1417 The abbreviation is the identifier used in a display filter.
1421 The type of value this field holds. The current field types are:
1423 FT_NONE No field type. Used for fields that
1424 aren't given a value, and that can only
1425 be tested for presence or absence; a
1426 field that represents a data structure,
1427 with a subtree below it containing
1428 fields for the members of the structure,
1429 or that represents an array with a
1430 subtree below it containing fields for
1431 the members of the array, might be an
1433 FT_PROTOCOL Used for protocols which will be placing
1434 themselves as top-level items in the
1435 "Packet Details" pane of the UI.
1436 FT_BOOLEAN 0 means "false", any other value means
1438 FT_FRAMENUM A frame number; if this is used, the "Go
1439 To Corresponding Frame" menu item can
1441 FT_UINT8 An 8-bit unsigned integer.
1442 FT_UINT16 A 16-bit unsigned integer.
1443 FT_UINT24 A 24-bit unsigned integer.
1444 FT_UINT32 A 32-bit unsigned integer.
1445 FT_UINT64 A 64-bit unsigned integer.
1446 FT_INT8 An 8-bit signed integer.
1447 FT_INT16 A 16-bit signed integer.
1448 FT_INT24 A 24-bit signed integer.
1449 FT_INT32 A 32-bit signed integer.
1450 FT_INT64 A 64-bit signed integer.
1451 FT_FLOAT A single-precision floating point number.
1452 FT_DOUBLE A double-precision floating point number.
1453 FT_ABSOLUTE_TIME Seconds (4 bytes) and nanoseconds (4 bytes)
1454 of time displayed as month name, month day,
1455 year, hours, minutes, and seconds with 9
1456 digits after the decimal point.
1457 FT_RELATIVE_TIME Seconds (4 bytes) and nanoseconds (4 bytes)
1458 of time displayed as seconds and 9 digits
1459 after the decimal point.
1460 FT_STRING A string of characters, not necessarily
1461 NUL-terminated, but possibly NUL-padded.
1462 This, and the other string-of-characters
1463 types, are to be used for text strings,
1464 not raw binary data.
1465 FT_STRINGZ A NUL-terminated string of characters.
1466 FT_UINT_STRING A counted string of characters, consisting
1467 of a count (represented as an integral
1468 value) followed immediately by the
1469 specified number of characters.
1470 FT_ETHER A six octet string displayed in
1471 Ethernet-address format.
1472 FT_BYTES A string of bytes with arbitrary values;
1473 used for raw binary data.
1474 FT_IPv4 A version 4 IP address (4 bytes) displayed
1475 in dotted-quad IP address format (4
1476 decimal numbers separated by dots).
1477 FT_IPv6 A version 6 IP address (16 bytes) displayed
1478 in standard IPv6 address format.
1479 FT_IPXNET An IPX address displayed in hex as a 6-byte
1480 network number followed by a 6-byte station
1482 FT_GUID A Globally Unique Identifier
1483 FT_OID An ASN.1 Object Identifier
1485 Some of these field types are still not handled in the display filter
1486 routines, but the most common ones are. The FT_UINT* variables all
1487 represent unsigned integers, and the FT_INT* variables all represent
1488 signed integers; the number on the end represent how many bits are used
1489 to represent the number.
1493 The display field has a couple of overloaded uses. This is unfortunate,
1494 but since we're C as an application programming language, this sometimes
1495 makes for cleaner programs. Right now I still think that overloading
1496 this variable was okay.
1498 For integer fields (FT_UINT* and FT_INT*), this variable represents the
1499 base in which you would like the value displayed. The acceptable bases
1508 BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal,
1509 respectively. BASE_DEC_HEX and BASE_HEX_DEC display value in two bases
1510 (the 1st representation followed by the 2nd in parenthesis)
1512 For FT_BOOLEAN fields that are also bitfields, 'display' is used to tell
1513 the proto_tree how wide the parent bitfield is. With integers this is
1514 not needed since the type of integer itself (FT_UINT8, FT_UINT16,
1515 FT_UINT24, FT_UINT32, etc.) tells the proto_tree how wide the parent
1518 Additionally, BASE_NONE is used for 'display' as a NULL-value. That is,
1519 for non-integers and non-bitfield FT_BOOLEANs, you'll want to use BASE_NONE
1520 in the 'display' field. You may not use BASE_NONE for integers.
1522 It is possible that in the future we will record the endianness of
1523 integers. If so, it is likely that we'll use a bitmask on the display field
1524 so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX.
1525 But that has not happened yet.
1529 Some integer fields, of type FT_UINT*, need labels to represent the true
1530 value of a field. You could think of those fields as having an
1531 enumerated data type, rather than an integral data type.
1533 A 'value_string' structure is a way to map values to strings.
1535 typedef struct _value_string {
1540 For fields of that type, you would declare an array of "value_string"s:
1542 static const value_string valstringname[] = {
1543 { INTVAL1, "Descriptive String 1" },
1544 { INTVAL2, "Descriptive String 2" },
1548 (the last entry in the array must have a NULL 'strptr' value, to
1549 indicate the end of the array). The 'strings' field would be set to
1550 'VALS(valstringname)'.
1552 If the field has a numeric rather than an enumerated type, the 'strings'
1553 field would be set to NULL.
1555 FT_BOOLEANS have a default map of 0 = "False", 1 (or anything else) = "True".
1556 Sometimes it is useful to change the labels for boolean values (e.g.,
1557 to "Yes"/"No", "Fast"/"Slow", etc.). For these mappings, a struct called
1558 true_false_string is used. (This struct is new as of Wireshark 0.7.6).
1560 typedef struct true_false_string {
1563 } true_false_string;
1565 For Boolean fields for which "False" and "True" aren't the desired
1566 labels, you would declare a "true_false_string"s:
1568 static const true_false_string boolstringname = {
1573 Its two fields are pointers to the string representing truth, and the
1574 string representing falsehood. For FT_BOOLEAN fields that need a
1575 'true_false_string' struct, the 'strings' field would be set to
1576 'TFS(&boolstringname)'.
1578 If the Boolean field is to be displayed as "False" or "True", the
1579 'strings' field would be set to NULL.
1583 If the field is a bitfield, then the bitmask is the mask which will
1584 leave only the bits needed to make the field when ANDed with a value.
1585 The proto_tree routines will calculate 'bitshift' automatically
1586 from 'bitmask', by finding the rightmost set bit in the bitmask.
1587 If the field is not a bitfield, then bitmask should be set to 0.
1591 This is a string giving a proper description of the field.
1592 It should be at least one grammatically complete sentence.
1593 It is meant to provide a more detailed description of the field than the
1594 name alone provides. This information will be used in the man page, and
1595 in a future GUI display-filter creation tool. We might also add tooltips
1596 to the labels in the GUI protocol tree, in which case the blurb would
1597 be used as the tooltip text.
1600 1.6.1 Field Registration.
1602 Protocol registration is handled by creating an instance of the
1603 header_field_info struct (or an array of such structs), and
1604 calling the registration function along with the registration ID of
1605 the protocol that is the parent of the fields. Here is a complete example:
1607 static int proto_eg = -1;
1608 static int hf_field_a = -1;
1609 static int hf_field_b = -1;
1611 static hf_register_info hf[] = {
1614 { "Field A", "proto.field_a", FT_UINT8, BASE_HEX, NULL,
1615 0xf0, "Field A represents Apples", HFILL }},
1618 { "Field B", "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
1619 0x0, "Field B represents Bananas", HFILL }}
1622 proto_eg = proto_register_protocol("Example Protocol",
1624 proto_register_field_array(proto_eg, hf, array_length(hf));
1626 Be sure that your array of hf_register_info structs is declared 'static',
1627 since the proto_register_field_array() function does not create a copy
1628 of the information in the array... it uses that static copy of the
1629 information that the compiler created inside your array. Here's the
1630 layout of the hf_register_info struct:
1632 typedef struct hf_register_info {
1633 int *p_id; /* pointer to parent variable */
1634 header_field_info hfinfo;
1637 Also be sure to use the handy array_length() macro found in packet.h
1638 to have the compiler compute the array length for you at compile time.
1640 If you don't have any fields to register, do *NOT* create a zero-length
1641 "hf" array; not all compilers used to compile Wireshark support them.
1642 Just omit the "hf" array, and the "proto_register_field_array()" call,
1645 It is OK to have header fields with a different format be registered with
1646 the same abbreviation. For instance, the following is valid:
1648 static hf_register_info hf[] = {
1650 { &hf_field_8bit, /* 8-bit version of proto.field */
1651 { "Field (8 bit)", "proto.field", FT_UINT8, BASE_DEC, NULL,
1652 0x00, "Field represents FOO", HFILL }},
1654 { &hf_field_32bit, /* 32-bit version of proto.field */
1655 { "Field (32 bit)", "proto.field", FT_UINT32, BASE_DEC, NULL,
1656 0x00, "Field represents FOO", HFILL }}
1659 This way a filter expression can match a header field, irrespective of the
1660 representation of it in the specific protocol context. This is interesting
1661 for protocols with variable-width header fields.
1663 The HFILL macro at the end of the struct will set reasonable default values
1664 for internally used fields.
1666 1.6.2 Adding Items and Values to the Protocol Tree.
1668 A protocol item is added to an existing protocol tree with one of a
1669 handful of proto_XXX_DO_YYY() functions.
1671 Remember that it only makes sense to add items to a protocol tree if its
1672 proto_tree pointer is not null. Should you add an item to a NULL tree, then
1673 the proto_XXX_DO_YYY() function will immediately return. The cost of this
1674 function call can be avoided by checking for the tree pointer.
1676 Subtrees can be made with the proto_item_add_subtree() function:
1678 item = proto_tree_add_item(....);
1679 new_tree = proto_item_add_subtree(item, tree_type);
1681 This will add a subtree under the item in question; a subtree can be
1682 created under an item made by any of the "proto_tree_add_XXX" functions,
1683 so that the tree can be given an arbitrary depth.
1685 Subtree types are integers, assigned by
1686 "proto_register_subtree_array()". To register subtree types, pass an
1687 array of pointers to "gint" variables to hold the subtree type values to
1688 "proto_register_subtree_array()":
1690 static gint ett_eg = -1;
1691 static gint ett_field_a = -1;
1693 static gint *ett[] = {
1698 proto_register_subtree_array(ett, array_length(ett));
1700 in your "register" routine, just as you register the protocol and the
1701 fields for that protocol.
1703 There are several functions that the programmer can use to add either
1704 protocol or field labels to the proto_tree:
1707 proto_tree_add_item(tree, id, tvb, start, length, little_endian);
1710 proto_tree_add_item_hidden(tree, id, tvb, start, length, little_endian);
1713 proto_tree_add_none_format(tree, id, tvb, start, length, format, ...);
1716 proto_tree_add_protocol_format(tree, id, tvb, start, length,
1720 proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr);
1723 proto_tree_add_bytes_hidden(tree, id, tvb, start, length, start_ptr);
1726 proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr,
1730 proto_tree_add_bytes_format_value(tree, id, tvb, start, length,
1731 start_ptr, format, ...);
1734 proto_tree_add_time(tree, id, tvb, start, length, value_ptr);
1737 proto_tree_add_time_hidden(tree, id, tvb, start, length, value_ptr);
1740 proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr,
1744 proto_tree_add_time_format_value(tree, id, tvb, start, length,
1745 value_ptr, format, ...);
1748 proto_tree_add_ipxnet(tree, id, tvb, start, length, value);
1751 proto_tree_add_ipxnet_hidden(tree, id, tvb, start, length, value);
1754 proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value,
1758 proto_tree_add_ipxnet_format_value(tree, id, tvb, start, length,
1759 value, format, ...);
1762 proto_tree_add_ipv4(tree, id, tvb, start, length, value);
1765 proto_tree_add_ipv4_hidden(tree, id, tvb, start, length, value);
1768 proto_tree_add_ipv4_format(tree, id, tvb, start, length, value,
1772 proto_tree_add_ipv4_format_value(tree, id, tvb, start, length,
1773 value, format, ...);
1776 proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr);
1779 proto_tree_add_ipv6_hidden(tree, id, tvb, start, length, value_ptr);
1782 proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr,
1786 proto_tree_add_ipv6_format_value(tree, id, tvb, start, length,
1787 value_ptr, format, ...);
1790 proto_tree_add_ether(tree, id, tvb, start, length, value_ptr);
1793 proto_tree_add_ether_hidden(tree, id, tvb, start, length, value_ptr);
1796 proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr,
1800 proto_tree_add_ether_format_value(tree, id, tvb, start, length,
1801 value_ptr, format, ...);
1804 proto_tree_add_string(tree, id, tvb, start, length, value_ptr);
1807 proto_tree_add_string_hidden(tree, id, tvb, start, length, value_ptr);
1810 proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr,
1814 proto_tree_add_string_format_value(tree, id, tvb, start, length,
1815 value_ptr, format, ...);
1818 proto_tree_add_boolean(tree, id, tvb, start, length, value);
1821 proto_tree_add_boolean_hidden(tree, id, tvb, start, length, value);
1824 proto_tree_add_boolean_format(tree, id, tvb, start, length, value,
1828 proto_tree_add_boolean_format_value(tree, id, tvb, start, length,
1829 value, format, ...);
1832 proto_tree_add_float(tree, id, tvb, start, length, value);
1835 proto_tree_add_float_hidden(tree, id, tvb, start, length, value);
1838 proto_tree_add_float_format(tree, id, tvb, start, length, value,
1842 proto_tree_add_float_format_value(tree, id, tvb, start, length,
1843 value, format, ...);
1846 proto_tree_add_double(tree, id, tvb, start, length, value);
1849 proto_tree_add_double_hidden(tree, id, tvb, start, length, value);
1852 proto_tree_add_double_format(tree, id, tvb, start, length, value,
1856 proto_tree_add_double_format_value(tree, id, tvb, start, length,
1857 value, format, ...);
1860 proto_tree_add_uint(tree, id, tvb, start, length, value);
1863 proto_tree_add_uint_hidden(tree, id, tvb, start, length, value);
1866 proto_tree_add_uint_format(tree, id, tvb, start, length, value,
1870 proto_tree_add_uint_format_value(tree, id, tvb, start, length,
1871 value, format, ...);
1874 proto_tree_add_uint64(tree, id, tvb, start, length, value);
1877 proto_tree_add_uint64_format(tree, id, tvb, start, length, value,
1881 proto_tree_add_uint64_format_value(tree, id, tvb, start, length,
1882 value, format, ...);
1885 proto_tree_add_int(tree, id, tvb, start, length, value);
1888 proto_tree_add_int_hidden(tree, id, tvb, start, length, value);
1891 proto_tree_add_int_format(tree, id, tvb, start, length, value,
1895 proto_tree_add_int_format_value(tree, id, tvb, start, length,
1896 value, format, ...);
1899 proto_tree_add_int64(tree, id, tvb, start, length, value);
1902 proto_tree_add_int64_format(tree, id, tvb, start, length, value,
1906 proto_tree_add_int64_format_value(tree, id, tvb, start, length,
1907 value, format, ...);
1910 proto_tree_add_text(tree, tvb, start, length, format, ...);
1913 proto_tree_add_text_valist(tree, tvb, start, length, format, ap);
1916 proto_tree_add_guid(tree, id, tvb, start, length, value_ptr);
1919 proto_tree_add_guid_hidden(tree, id, tvb, start, length, value_ptr);
1922 proto_tree_add_guid_format(tree, id, tvb, start, length, value_ptr,
1926 proto_tree_add_guid_format_value(tree, id, tvb, start, length,
1927 value_ptr, format, ...);
1930 proto_tree_add_oid(tree, id, tvb, start, length, value_ptr);
1933 proto_tree_add_oid_hidden(tree, id, tvb, start, length, value_ptr);
1936 proto_tree_add_oid_format(tree, id, tvb, start, length, value_ptr,
1940 proto_tree_add_oid_format_value(tree, id, tvb, start, length,
1941 value_ptr, format, ...);
1943 The 'tree' argument is the tree to which the item is to be added. The
1944 'tvb' argument is the tvbuff from which the item's value is being
1945 extracted; the 'start' argument is the offset from the beginning of that
1946 tvbuff of the item being added, and the 'length' argument is the length,
1947 in bytes, of the item.
1949 The length of some items cannot be determined until the item has been
1950 dissected; to add such an item, add it with a length of -1, and, when the
1951 dissection is complete, set the length with 'proto_item_set_len()':
1954 proto_item_set_len(ti, length);
1956 The "ti" argument is the value returned by the call that added the item
1957 to the tree, and the "length" argument is the length of the item.
1959 proto_tree_add_item()
1960 ---------------------
1961 proto_tree_add_item is used when you wish to do no special formatting.
1962 The item added to the GUI tree will contain the name (as passed in the
1963 proto_register_*() function) and a value. The value will be fetched
1964 from the tvbuff by proto_tree_add_item(), based on the type of the field
1965 and, for integral and Boolean fields, the byte order of the value; the
1966 byte order is specified by the 'little_endian' argument, which is TRUE
1967 if the value is little-endian and FALSE if it is big-endian.
1969 Now that definitions of fields have detailed information about bitfield
1970 fields, you can use proto_tree_add_item() with no extra processing to
1971 add bitfield values to your tree. Here's an example. Take the Format
1972 Identifier (FID) field in the Transmission Header (TH) portion of the SNA
1973 protocol. The FID is the high nibble of the first byte of the TH. The
1974 FID would be registered like this:
1976 name = "Format Identifier"
1977 abbrev = "sna.th.fid"
1980 strings = sna_th_fid_vals
1983 The bitmask contains the value which would leave only the FID if bitwise-ANDed
1984 against the parent field, the first byte of the TH.
1986 The code to add the FID to the tree would be;
1988 proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, TRUE);
1990 The definition of the field already has the information about bitmasking
1991 and bitshifting, so it does the work of masking and shifting for us!
1992 This also means that you no longer have to create value_string structs
1993 with the values bitshifted. The value_string for FID looks like this,
1994 even though the FID value is actually contained in the high nibble.
1995 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
1997 /* Format Identifier */
1998 static const value_string sna_th_fid_vals[] = {
1999 { 0x0, "SNA device <--> Non-SNA Device" },
2000 { 0x1, "Subarea Node <--> Subarea Node" },
2001 { 0x2, "Subarea Node <--> PU2" },
2002 { 0x3, "Subarea Node or SNA host <--> Subarea Node" },
2005 { 0xf, "Adjaced Subarea Nodes" },
2009 The final implication of this is that display filters work the way you'd
2010 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
2011 Subarea Nodes. The user does not have to shift the value of the FID to
2012 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
2015 proto_tree_add_item_hidden()
2016 ----------------------------
2017 proto_tree_add_item_hidden is used to add fields and values to a tree,
2018 but not show them on a GUI tree. The caller may want a value to be
2019 included in a tree so that the packet can be filtered on this field, but
2020 the representation of that field in the tree is not appropriate. An
2021 example is the token-ring routing information field (RIF). The best way
2022 to show the RIF in a GUI is by a sequence of ring and bridge numbers.
2023 Rings are 3-digit hex numbers, and bridges are single hex digits:
2025 RIF: 001-A-013-9-C0F-B-555
2027 In the case of RIF, the programmer should use a field with no value and
2028 use proto_tree_add_none_format() to build the above representation. The
2029 programmer can then add the ring and bridge values, one-by-one, with
2030 proto_tree_add_item_hidden() so that the user can then filter on or
2031 search for a particular ring or bridge. Here's a skeleton of how the
2032 programmer might code this.
2035 rif = create_rif_string(...);
2037 proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
2039 for(i = 0; i < num_rings; i++) {
2040 proto_tree_add_item_hidden(tree, hf_tr_rif_ring, ..., FALSE);
2042 for(i = 0; i < num_rings - 1; i++) {
2043 proto_tree_add_item_hidden(tree, hf_tr_rif_bridge, ..., FALSE);
2046 The logical tree has these items:
2048 hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
2049 hf_tr_rif_ring, hidden, value=0x001
2050 hf_tr_rif_bridge, hidden, value=0xA
2051 hf_tr_rif_ring, hidden, value=0x013
2052 hf_tr_rif_bridge, hidden, value=0x9
2053 hf_tr_rif_ring, hidden, value=0xC0F
2054 hf_tr_rif_bridge, hidden, value=0xB
2055 hf_tr_rif_ring, hidden, value=0x555
2057 GUI or print code will not display the hidden fields, but a display
2058 filter or "packet grep" routine will still see the values. The possible
2059 filter is then possible:
2061 tr.rif_ring eq 0x013
2063 proto_tree_add_protocol_format()
2064 ----------------------------
2065 proto_tree_add_protocol_format is used to add the top-level item for the
2066 protocol when the dissector routines wants complete control over how the
2067 field and value will be represented on the GUI tree. The ID value for
2068 the protocol is passed in as the "id" argument; the rest of the
2069 arguments are a "printf"-style format and any arguments for that format.
2070 The caller must include the name of the protocol in the format; it is
2071 not added automatically as in proto_tree_add_item().
2073 proto_tree_add_none_format()
2074 ----------------------------
2075 proto_tree_add_none_format is used to add an item of type FT_NONE.
2076 The caller must include the name of the field in the format; it is
2077 not added automatically as in proto_tree_add_item().
2079 proto_tree_add_bytes()
2080 proto_tree_add_time()
2081 proto_tree_add_ipxnet()
2082 proto_tree_add_ipv4()
2083 proto_tree_add_ipv6()
2084 proto_tree_add_ether()
2085 proto_tree_add_string()
2086 proto_tree_add_boolean()
2087 proto_tree_add_float()
2088 proto_tree_add_double()
2089 proto_tree_add_uint()
2090 proto_tree_add_uint64()
2091 proto_tree_add_int()
2092 proto_tree_add_int64()
2093 proto_tree_add_guid()
2094 proto_tree_add_oid()
2095 ----------------------------
2096 These routines are used to add items to the protocol tree if either:
2098 the value of the item to be added isn't just extracted from the
2099 packet data, but is computed from data in the packet;
2101 the value was fetched into a variable.
2103 The 'value' argument has the value to be added to the tree.
2105 NOTE: in all cases where the 'value' argument is a pointer, a copy is
2106 made of the object pointed to; if you have dynamically allocated a
2107 buffer for the object, that buffer will not be freed when the protocol
2108 tree is freed - you must free the buffer yourself when you don't need it
2111 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
2114 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
2115 "nstime_t", which is a structure containing the time to be added; it has
2116 'secs' and 'nsecs' members, giving the integral part and the fractional
2117 part of a time in units of seconds, with 'nsecs' being the number of
2118 nanoseconds. For absolute times, "secs" is a UNIX-style seconds since
2119 January 1, 1970, 00:00:00 GMT value.
2121 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
2124 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
2125 address, in network byte order.
2127 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
2128 128-bit IPv6 address.
2130 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
2133 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
2136 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer;
2137 zero means "false", and non-zero means "true".
2139 For proto_tree_add_float(), the 'value' argument is a 'float' in the
2140 host's floating-point format.
2142 For proto_tree_add_double(), the 'value' argument is a 'double' in the
2143 host's floating-point format.
2145 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
2146 integer value, in host byte order. (This routine cannot be used to add
2149 For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned
2150 integer value, in host byte order.
2152 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
2153 integer value, in host byte order. (This routine cannot be used to add
2156 For proto_tree_add_int64(), the 'value' argument is a 64-bit signed
2157 integer value, in host byte order.
2159 For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an
2162 For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an
2163 ASN.1 Object Identifier.
2165 proto_tree_add_bytes_hidden()
2166 proto_tree_add_time_hidden()
2167 proto_tree_add_ipxnet_hidden()
2168 proto_tree_add_ipv4_hidden()
2169 proto_tree_add_ipv6_hidden()
2170 proto_tree_add_ether_hidden()
2171 proto_tree_add_string_hidden()
2172 proto_tree_add_boolean_hidden()
2173 proto_tree_add_float_hidden()
2174 proto_tree_add_double_hidden()
2175 proto_tree_add_uint_hidden()
2176 proto_tree_add_int_hidden()
2177 proto_tree_add_guid_hidden()
2178 proto_tree_add_oid_hidden()
2179 ----------------------------
2180 These routines add fields and values to a tree, but don't show them in
2181 the GUI tree. They are used for the same reason that
2182 proto_tree_add_item() is used.
2184 proto_tree_add_bytes_format()
2185 proto_tree_add_time_format()
2186 proto_tree_add_ipxnet_format()
2187 proto_tree_add_ipv4_format()
2188 proto_tree_add_ipv6_format()
2189 proto_tree_add_ether_format()
2190 proto_tree_add_string_format()
2191 proto_tree_add_boolean_format()
2192 proto_tree_add_float_format()
2193 proto_tree_add_double_format()
2194 proto_tree_add_uint_format()
2195 proto_tree_add_uint64_format()
2196 proto_tree_add_int_format()
2197 proto_tree_add_int64_format()
2198 proto_tree_add_guid_format()
2199 proto_tree_add_oid_format()
2200 ----------------------------
2201 These routines are used to add items to the protocol tree when the
2202 dissector routines wants complete control over how the field and value
2203 will be represented on the GUI tree. The argument giving the value is
2204 the same as the corresponding proto_tree_add_XXX() function; the rest of
2205 the arguments are a "printf"-style format and any arguments for that
2206 format. The caller must include the name of the field in the format; it
2207 is not added automatically as in the proto_tree_add_XXX() functions.
2209 proto_tree_add_bytes_format_value()
2210 proto_tree_add_time_format_value()
2211 proto_tree_add_ipxnet_format_value()
2212 proto_tree_add_ipv4_format_value()
2213 proto_tree_add_ipv6_format_value()
2214 proto_tree_add_ether_format_value()
2215 proto_tree_add_string_format_value()
2216 proto_tree_add_boolean_format_value()
2217 proto_tree_add_float_format_value()
2218 proto_tree_add_double_format_value()
2219 proto_tree_add_uint_format_value()
2220 proto_tree_add_uint64_format_value()
2221 proto_tree_add_int_format_value()
2222 proto_tree_add_int64_format_value()
2223 proto_tree_add_guid_format_value()
2224 proto_tree_add_oid_format_value()
2225 ----------------------------
2227 These routines are used to add items to the protocol tree when the
2228 dissector routines wants complete control over how the value will be
2229 represented on the GUI tree. The argument giving the value is the same
2230 as the corresponding proto_tree_add_XXX() function; the rest of the
2231 arguments are a "printf"-style format and any arguments for that format.
2232 With these routines, unlike the proto_tree_add_XXX_format() routines,
2233 the name of the field is added automatically as in the
2234 proto_tree_add_XXX() functions; only the value is added with the format.
2236 proto_tree_add_text()
2237 ---------------------
2238 proto_tree_add_text() is used to add a label to the GUI tree. It will
2239 contain no value, so it is not searchable in the display filter process.
2240 This function was needed in the transition from the old-style proto_tree
2241 to this new-style proto_tree so that Wireshark would still decode all
2242 protocols w/o being able to filter on all protocols and fields.
2243 Otherwise we would have had to cripple Wireshark's functionality while we
2244 converted all the old-style proto_tree calls to the new-style proto_tree
2247 This can also be used for items with subtrees, which may not have values
2248 themselves - the items in the subtree are the ones with values.
2250 For a subtree, the label on the subtree might reflect some of the items
2251 in the subtree. This means the label can't be set until at least some
2252 of the items in the subtree have been dissected. To do this, use
2253 'proto_item_set_text()' or 'proto_item_append_text()':
2256 proto_item_set_text(proto_item *ti, ...);
2259 proto_item_append_text(proto_item *ti, ...);
2261 'proto_item_set_text()' takes as an argument the value returned by
2262 'proto_tree_add_text()', a 'printf'-style format string, and a set of
2263 arguments corresponding to '%' format items in that string, and replaces
2264 the text for the item created by 'proto_tree_add_text()' with the result
2265 of applying the arguments to the format string.
2267 'proto_item_append_text()' is similar, but it appends to the text for
2268 the item the result of applying the arguments to the format string.
2270 For example, early in the dissection, one might do:
2272 ti = proto_tree_add_text(tree, tvb, offset, length, <label>);
2276 proto_item_set_text(ti, "%s: %s", type, value);
2278 after the "type" and "value" fields have been extracted and dissected.
2279 <label> would be a label giving what information about the subtree is
2280 available without dissecting any of the data in the subtree.
2282 Note that an exception might thrown when trying to extract the values of
2283 the items used to set the label, if not all the bytes of the item are
2284 available. Thus, one should create the item with text that is as
2285 meaningful as possible, and set it or append additional information to
2286 it as the values needed to supply that information is extracted.
2288 proto_tree_add_text_valist()
2289 ---------------------
2290 This is like proto_tree_add_text(), but takes, as the last argument, a
2291 'va_list'; it is used to allow routines that take a printf-like
2292 variable-length list of arguments to add a text item to the protocol
2295 1.7 Utility routines.
2297 1.7.1 match_strval and val_to_str.
2299 A dissector may need to convert a value to a string, using a
2300 'value_string' structure, by hand, rather than by declaring a field with
2301 an associated 'value_string' structure; this might be used, for example,
2302 to generate a COL_INFO line for a frame.
2304 'match_strval()' will do that:
2307 match_strval(guint32 val, const value_string *vs)
2309 It will look up the value 'val' in the 'value_string' table pointed to
2310 by 'vs', and return either the corresponding string, or NULL if the
2311 value could not be found in the table. Note that, unless 'val' is
2312 guaranteed to be a value in the 'value_string' table ("guaranteed" as in
2313 "the code has already checked that it's one of those values" or "the
2314 table handles all possible values of the size of 'val'", not "the
2315 protocol spec says it has to be" - protocol specs do not prevent invalid
2316 packets from being put onto a network or into a purported packet capture
2317 file), you must check whether 'match_strval()' returns NULL, and arrange
2318 that its return value not be dereferenced if it's NULL. In particular,
2319 don't use it in a call to generate a COL_INFO line for a frame such as
2321 col_add_fstr(COL_INFO, ", %s", match_strval(val, table));
2323 unless is it certain that 'val' is in 'table'.
2325 'val_to_str()' can be used to generate a string for values not found in
2329 val_to_str(guint32 val, const value_string *vs, const char *fmt)
2331 If the value 'val' is found in the 'value_string' table pointed to by
2332 'vs', 'val_to_str' will return the corresponding string; otherwise, it
2333 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
2334 to generate a string, and will return a pointer to that string.
2335 (Currently, it has three 64-byte static buffers, and cycles through
2336 them; this permits the results of up to three calls to 'val_to_str' to
2337 be passed as arguments to a routine using those strings.)
2340 1.8 Calling Other Dissectors.
2342 As each dissector completes its portion of the protocol analysis, it
2343 is expected to create a new tvbuff of type TVBUFF_SUBSET which
2344 contains the payload portion of the protocol (that is, the bytes
2345 that are relevant to the next dissector).
2347 The syntax for creating a new TVBUFF_SUBSET is:
2349 next_tvb = tvb_new_subset(tvb, offset, length, reported_length)
2352 tvb is the tvbuff that the dissector has been working on. It
2353 can be a tvbuff of any type.
2355 next_tvb is the new TVBUFF_SUBSET.
2357 offset is the byte offset of 'tvb' at which the new tvbuff
2358 should start. The first byte is the 0th byte.
2360 length is the number of bytes in the new TVBUFF_SUBSET. A length
2361 argument of -1 says to use as many bytes as are available in
2364 reported_length is the number of bytes that the current protocol
2365 says should be in the payload. A reported_length of -1 says that
2366 the protocol doesn't say anything about the size of its payload.
2369 An example from packet-ipx.c -
2372 dissect_ipx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2375 int reported_length, available_length;
2378 /* Make the next tvbuff */
2380 /* IPX does have a length value in the header, so calculate report_length */
2381 Set this to -1 if there isn't any length information in the protocol
2383 reported_length = ipx_length - IPX_HEADER_LEN;
2385 /* Calculate the available data in the packet,
2386 set this to -1 to use all the data in the tv_buffer
2388 available_length = tvb_length(tvb) - IPX_HEADER_LEN;
2390 /* Create the tvbuffer for the next dissector */
2391 next_tvb = tvb_new_subset(tvb, IPX_HEADER_LEN,
2392 MIN(available_length, reported_length),
2395 /* call the next dissector */
2396 dissector_next( next_tvb, pinfo, tree);
2399 1.9 Editing Makefile.common to add your dissector.
2401 To arrange that your dissector will be built as part of Wireshark, you
2402 must add the name of the source file for your dissector to the
2403 'DISSECTOR_SRC' macro in the 'Makefile.common' file in the 'epan/dissectors'
2404 directory. (Note that this is for modern versions of UNIX, so there
2405 is no 14-character limitation on file names, and for modern versions of
2406 Windows, so there is no 8.3-character limitation on file names.)
2408 If your dissector also has its own header file or files, you must add
2409 them to the 'DISSECTOR_INCLUDES' macro in the 'Makefile.common' file in
2410 the 'epan/dissectors' directory, so that it's included when release source
2411 tarballs are built (otherwise, the source in the release tarballs won't
2414 1.10 Using the SVN source code tree.
2416 See <http://www.wireshark.org/develop.html>
2418 1.11 Submitting code for your new dissector.
2420 - TEST YOUR DISSECTOR BEFORE SUBMITTING IT.
2421 Use fuzz-test.sh and/or randpkt against your dissector. These are
2422 described at <http://wiki.wireshark.org/FuzzTesting>.
2424 - Subscribe to <mailto:wireshark-dev[AT]wireshark.org> by sending an email to
2425 <mailto:wireshark-dev-request[AT]wireshark.org?body="help"> or visiting
2426 <http://www.wireshark.org/lists/>.
2428 - 'svn add' all the files of your new dissector.
2430 - 'svn diff' the workspace and save the result to a file.
2432 - Edit the diff file - remove any changes unrelated to your new dissector,
2433 e.g. changes in config.nmake
2435 - Send a note with the attached diff file requesting its inclusion to
2436 <mailto:wireshark-dev[AT]wireshark.org>. You can also use this procedure for
2437 providing patches to your dissector or any other part of Wireshark.
2439 - Create a Wiki page on the protocol at <http://wiki.wireshark.org>.
2440 A template is provided so it is easy to setup in a consistent style.
2442 - If possible, add sample capture files to the sample captures page at
2443 <http://wiki.wireshark.org/SampleCaptures>. These files are used by
2444 the automated build system for fuzz testing.
2446 - If you find that you are contributing a lot to wireshark on an ongoing
2447 basis you can request to become a committer which will allow you to
2448 commit files to subversion directly.
2450 2. Advanced dissector topics.
2454 Some of the advanced features are being worked on constantly. When using them
2455 it is wise to check the relevant header and source files for additional details.
2457 2.2 Following "conversations".
2459 In wireshark a conversation is defined as a series of data packet between two
2460 address:port combinations. A conversation is not sensitive to the direction of
2461 the packet. The same conversation will be returned for a packet bound from
2462 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
2464 There are five routines that you will use to work with a conversation:
2465 conversation_new, find_conversation, conversation_add_proto_data,
2466 conversation_get_proto_data, and conversation_delete_proto_data.
2469 2.2.1 The conversation_init function.
2471 This is an internal routine for the conversation code. As such the you
2472 will not have to call this routine. Just be aware that this routine is
2473 called at the start of each capture and before the packets are filtered
2474 with a display filter. The routine will destroy all stored
2475 conversations. This routine does NOT clean up any data pointers that are
2476 passed in the conversation_new 'data' variable. You are responsible for
2477 this clean up if you pass a malloc'ed pointer in this variable.
2479 See item 2.2.7 for more information about the 'data' pointer.
2482 2.2.2 The conversation_new function.
2484 This routine will create a new conversation based upon two address/port
2485 pairs. If you want to associate with the conversation a pointer to a
2486 private data structure you must use the conversation_add_proto_data
2487 function. The ptype variable is used to differentiate between
2488 conversations over different protocols, i.e. TCP and UDP. The options
2489 variable is used to define a conversation that will accept any destination
2490 address and/or port. Set options = 0 if the destination port and address
2491 are know when conversation_new is called. See section 2.4 for more
2492 information on usage of the options parameter.
2494 The conversation_new prototype:
2495 conversation_t *conversation_new(guint32 setup_frame, address *addr1,
2496 address *addr2, port_type ptype, guint32 port1, guint32 port2,
2500 guint32 setup_frame = The lowest numbered frame for this conversation
2501 address* addr1 = first data packet address
2502 address* addr2 = second data packet address
2503 port_type ptype = port type, this is defined in packet.h
2504 guint32 port1 = first data packet port
2505 guint32 port2 = second data packet port
2506 guint options = conversation options, NO_ADDR2 and/or NO_PORT2
2508 setup_frame indicates the first frame for this conversation, and is used to
2509 distinguish multiple conversations with the same addr1/port1 and addr2/port2
2510 pair that occur within the same capture session.
2512 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
2513 are the second address/port pair. A conversation doesn't have source
2514 and destination address/port pairs - packets in a conversation go in
2515 both directions - so "addr1"/"port1" may be the source or destination
2516 address/port pair; "addr2"/"port2" would be the other pair.
2518 If NO_ADDR2 is specified, the conversation is set up so that a
2519 conversation lookup will match only the "addr1" address; if NO_PORT2 is
2520 specified, the conversation is set up so that a conversation lookup will
2521 match only the "port1" port; if both are specified, i.e.
2522 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
2523 match only the "addr1"/"port1" address/port pair. This can be used if a
2524 packet indicates that, later in the capture, a conversation will be
2525 created using certain addresses and ports, in the case where the packet
2526 doesn't specify the addresses and ports of both sides.
2528 2.2.3 The find_conversation function.
2530 Call this routine to look up a conversation. If no conversation is found,
2531 the routine will return a NULL value.
2533 The find_conversation prototype:
2535 conversation_t *find_conversation(guint32 frame_num, address *addr_a,
2536 address *addr_b, port_type ptype, guint32 port_a, guint32 port_b,
2540 guint32 frame_num = a frame number to match
2541 address* addr_a = first address
2542 address* addr_b = second address
2543 port_type ptype = port type
2544 guint32 port_a = first data packet port
2545 guint32 port_b = second data packet port
2546 guint options = conversation options, NO_ADDR_B and/or NO_PORT_B
2548 frame_num is a frame number to match. The conversation returned is where
2549 (frame_num >= conversation->setup_frame
2550 && frame_num < conversation->next->setup_frame)
2551 Suppose there are a total of 3 conversations (A, B, and C) that match
2552 addr_a/port_a and addr_b/port_b, where the setup_frame used in
2553 conversation_new() for A, B and C are 10, 50, and 100 respectively. The
2554 frame_num passed in find_conversation is compared to the setup_frame of each
2555 conversation. So if (frame_num >= 10 && frame_num < 50), conversation A is
2556 returned. If (frame_num >= 50 && frame_num < 100), conversation B is returned.
2557 If (frame_num >= 100) conversation C is returned.
2559 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
2560 "port_b" are the second address/port pair. Again, as a conversation
2561 doesn't have source and destination address/port pairs, so
2562 "addr_a"/"port_a" may be the source or destination address/port pair;
2563 "addr_b"/"port_b" would be the other pair. The search will match the
2564 "a" address/port pair against both the "1" and "2" address/port pairs,
2565 and match the "b" address/port pair against both the "2" and "1"
2566 address/port pairs; you don't have to worry about which side the "a" or
2567 "b" pairs correspond to.
2569 If the NO_ADDR_B flag was specified to "find_conversation()", the
2570 "addr_b" address will be treated as matching any "wildcarded" address;
2571 if the NO_PORT_B flag was specified, the "port_b" port will be treated
2572 as matching any "wildcarded" port. If both flags are specified, i.e.
2573 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
2574 any "wildcarded" address and the "port_b" port will be treated as
2575 matching any "wildcarded" port.
2578 2.2.4 The conversation_add_proto_data function.
2580 Once you have created a conversation with conversation_new, you can
2581 associate data with it using this function.
2583 The conversation_add_proto_data prototype:
2585 void conversation_add_proto_data(conversation_t *conv, int proto,
2589 conversation_t *conv = the conversation in question
2590 int proto = registered protocol number
2591 void *data = dissector data structure
2593 "conversation" is the value returned by conversation_new. "proto" is a
2594 unique protocol number created with proto_register_protocol. Protocols
2595 are typically registered in the proto_register_XXXX section of your
2596 dissector. "data" is a pointer to the data you wish to associate with the
2597 conversation. Using the protocol number allows several dissectors to
2598 associate data with a given conversation.
2601 2.2.5 The conversation_get_proto_data function.
2603 After you have located a conversation with find_conversation, you can use
2604 this function to retrieve any data associated with it.
2606 The conversation_get_proto_data prototype:
2608 void *conversation_get_proto_data(conversation_t *conv, int proto);
2611 conversation_t *conv = the conversation in question
2612 int proto = registered protocol number
2614 "conversation" is the conversation created with conversation_new. "proto"
2615 is a unique protocol number created with proto_register_protocol,
2616 typically in the proto_register_XXXX portion of a dissector. The function
2617 returns a pointer to the data requested, or NULL if no data was found.
2620 2.2.6 The conversation_delete_proto_data function.
2622 After you are finished with a conversation, you can remove your association
2623 with this function. Please note that ONLY the conversation entry is
2624 removed. If you have allocated any memory for your data, you must free it
2627 The conversation_delete_proto_data prototype:
2629 void conversation_delete_proto_data(conversation_t *conv, int proto);
2632 conversation_t *conv = the conversation in question
2633 int proto = registered protocol number
2635 "conversation" is the conversation created with conversation_new. "proto"
2636 is a unique protocol number created with proto_register_protocol,
2637 typically in the proto_register_XXXX portion of a dissector.
2639 2.2.7 The example conversation code with GMemChunk's.
2641 For a conversation between two IP addresses and ports you can use this as an
2642 example. This example uses the GMemChunk to allocate memory and stores the data
2643 pointer in the conversation 'data' variable.
2645 NOTE: Remember to register the init routine (my_dissector_init) in the
2646 protocol_register routine.
2649 /************************ Globals values ************************/
2651 /* the number of entries in the memory chunk array */
2652 #define my_init_count 10
2654 /* define your structure here */
2659 /* the GMemChunk base structure */
2660 static GMemChunk *my_vals = NULL;
2662 /* Registered protocol number
2663 static int my_proto = -1;
2666 /********************* in the dissector routine *********************/
2668 /* the local variables in the dissector */
2670 conversation_t *conversation;
2671 my_entry_t *data_ptr
2674 /* look up the conversation */
2676 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2677 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2679 /* if conversation found get the data pointer that you stored */
2681 data_ptr = (my_entry_t*)conversation_get_proto_data(conversation, my_proto);
2684 /* new conversation create local data structure */
2686 data_ptr = g_mem_chunk_alloc(my_vals);
2688 /*** add your code here to setup the new data structure ***/
2690 /* create the conversation with your data pointer */
2692 conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
2693 pinfo->srcport, pinfo->destport, 0);
2694 conversation_add_proto_data(conversation, my_proto, (void *)data_ptr);
2697 /* at this point the conversation data is ready */
2700 /******************* in the dissector init routine *******************/
2702 #define my_init_count 20
2705 my_dissector_init(void)
2708 /* destroy memory chunks if needed */
2711 g_mem_chunk_destroy(my_vals);
2713 /* now create memory chunks */
2715 my_vals = g_mem_chunk_new("my_proto_vals",
2717 my_init_count * sizeof(my_entry_t),
2721 /***************** in the protocol register routine *****************/
2723 /* register re-init routine */
2725 register_init_routine(&my_dissector_init);
2727 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
2730 2.2.8 An example conversation code that starts at a specific frame number.
2732 Sometimes a dissector has determined that a new conversation is needed that
2733 starts at a specific frame number, when a capture session encompasses multiple
2734 conversation that reuse the same src/dest ip/port pairs. You can use the
2735 compare the conversation->setup_frame returned by find_conversation with
2736 pinfo->fd->num to determine whether or not there already exists a conversation
2737 that starts at the specific frame number.
2739 /* in the dissector routine */
2741 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2742 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2743 if (conversation == NULL || (conversation->setup_frame != pinfo->fd->num)) {
2744 /* It's not part of any conversation or the returned
2745 * conversation->setup_frame doesn't match the current frame
2748 conversation = conversation_new(pinfo->fd->num, &pinfo->src,
2749 &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
2754 2.2.9 The example conversation code using conversation index field.
2756 Sometimes the conversation isn't enough to define a unique data storage
2757 value for the network traffic. For example if you are storing information
2758 about requests carried in a conversation, the request may have an
2759 identifier that is used to define the request. In this case the
2760 conversation and the identifier are required to find the data storage
2761 pointer. You can use the conversation data structure index value to
2762 uniquely define the conversation.
2764 See packet-afs.c for an example of how to use the conversation index. In
2765 this dissector multiple requests are sent in the same conversation. To store
2766 information for each request the dissector has an internal hash table based
2767 upon the conversation index and values inside the request packets.
2770 /* in the dissector routine */
2772 /* to find a request value, first lookup conversation to get index */
2773 /* then used the conversation index, and request data to find data */
2774 /* in the local hash table */
2776 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
2777 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
2778 if (conversation == NULL) {
2779 /* It's not part of any conversation - create a new one. */
2780 conversation = conversation_new(pinfo->fd->num, &pinfo->src,
2781 &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport,
2785 request_key.conversation = conversation->index;
2786 request_key.service = pntohs(&rxh->serviceId);
2787 request_key.callnumber = pntohl(&rxh->callNumber);
2789 request_val = (struct afs_request_val *)g_hash_table_lookup(
2790 afs_request_hash, &request_key);
2792 /* only allocate a new hash element when it's a request */
2794 if (!request_val && !reply)
2796 new_request_key = g_mem_chunk_alloc(afs_request_keys);
2797 *new_request_key = request_key;
2799 request_val = g_mem_chunk_alloc(afs_request_vals);
2800 request_val -> opcode = pntohl(&afsh->opcode);
2801 opcode = request_val->opcode;
2803 g_hash_table_insert(afs_request_hash, new_request_key,
2809 2.3 Dynamic conversation dissector registration.
2812 NOTE: This sections assumes that all information is available to
2813 create a complete conversation, source port/address and
2814 destination port/address. If either the destination port or
2815 address is know, see section 2.4 Dynamic server port dissector
2818 For protocols that negotiate a secondary port connection, for example
2819 packet-msproxy.c, a conversation can install a dissector to handle
2820 the secondary protocol dissection. After the conversation is created
2821 for the negotiated ports use the conversation_set_dissector to define
2822 the dissection routine.
2823 Before we create these conversations or assign a dissector to them we should
2824 first check that the conversation does not already exist and if it exists
2825 whether it is registered to our protocol or not.
2826 We should do this because is uncommon but it does happen that multiple
2827 different protocols can use the same socketpair during different stages of
2828 an application cycle. By keeping track of the frame number a conversation
2829 was started in wireshark can still tell these different protocols apart.
2831 The second argument to conversation_set_dissector is a dissector handle,
2832 which is created with a call to create_dissector_handle or
2835 create_dissector_handle takes as arguments a pointer to the dissector
2836 function and a protocol ID as returned by proto_register_protocol;
2837 register_dissector takes as arguments a string giving a name for the
2838 dissector, a pointer to the dissector function, and a protocol ID.
2840 The protocol ID is the ID for the protocol dissected by the function.
2841 The function will not be called if the protocol has been disabled by the
2842 user; instead, the data for the protocol will be dissected as raw data.
2846 /* the handle for the dynamic dissector *
2847 static dissector_handle_t sub_dissector_handle;
2849 /* prototype for the dynamic dissector */
2850 static void sub_dissector(tvbuff_t *tvb, packet_info *pinfo,
2853 /* in the main protocol dissector, where the next dissector is setup */
2855 /* if conversation has a data field, create it and load structure */
2857 /* First check if a conversation already exists for this
2860 conversation = find_conversation(pinfo->fd->num,
2861 &pinfo->src, &pinfo->dst, protocol,
2862 src_port, dst_port, new_conv_info, 0);
2864 /* If there is no such conversation, or if there is one but for
2865 someone else's protocol then we just create a new conversation
2866 and assign our protocol to it.
2868 if ( (conversation == NULL) ||
2869 (conversation->dissector_handle != sub_dissector_handle) ) {
2870 new_conv_info = g_mem_chunk_alloc(new_conv_vals);
2871 new_conv_info->data1 = value1;
2873 /* create the conversation for the dynamic port */
2874 conversation = conversation_new(pinfo->fd->num,
2875 &pinfo->src, &pinfo->dst, protocol,
2876 src_port, dst_port, new_conv_info, 0);
2878 /* set the dissector for the new conversation */
2879 conversation_set_dissector(conversation, sub_dissector_handle);
2884 proto_register_PROTOABBREV(void)
2888 sub_dissector_handle = create_dissector_handle(sub_dissector,
2894 2.4 Dynamic server port dissector registration.
2896 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
2897 conversation with only one port and address set, this isn't a
2898 requirement. Either the second port or the second address can be set
2899 when the conversation is created.
2901 For protocols that define a server address and port for a secondary
2902 protocol, a conversation can be used to link a protocol dissector to
2903 the server port and address. The key is to create the new
2904 conversation with the second address and port set to the "accept
2907 Some server applications can use the same port for different protocols during
2908 different stages of a transaction. For example it might initially use SNMP
2909 to perform some discovery and later switch to use TFTP using the same port.
2910 In order to handle this properly we must first check whether such a
2911 conversation already exists or not and if it exists we also check whether the
2912 registered dissector_handle for that conversation is "our" dissector or not.
2913 If not we create a new conversation on top of the previous one and set this new
2914 conversation to use our protocol.
2915 Since wireshark keeps track of the frame number where a conversation started
2916 wireshark will still be able to keep the packets apart even though they do use
2917 the same socketpair.
2918 (See packet-tftp.c and packet-snmp.c for examples of this)
2920 There are two support routines that will allow the second port and/or
2921 address to be set latter.
2923 conversation_set_port2( conversation_t *conv, guint32 port);
2924 conversation_set_addr2( conversation_t *conv, address addr);
2926 These routines will change the second address or port for the
2927 conversation. So, the server port conversation will be converted into a
2928 more complete conversation definition. Don't use these routines if you
2929 want create a conversation between the server and client and retain the
2930 server port definition, you must create a new conversation.
2935 /* the handle for the dynamic dissector *
2936 static dissector_handle_t sub_dissector_handle;
2940 /* in the main protocol dissector, where the next dissector is setup */
2942 /* if conversation has a data field, create it and load structure */
2944 new_conv_info = g_mem_chunk_alloc(new_conv_vals);
2945 new_conv_info->data1 = value1;
2947 /* create the conversation for the dynamic server address and port */
2948 /* NOTE: The second address and port values don't matter because the */
2949 /* NO_ADDR2 and NO_PORT2 options are set. */
2951 /* First check if a conversation already exists for this
2954 conversation = find_conversation(pinfo->fd->num,
2955 &server_src_addr, 0, protocol,
2956 server_src_port, 0, NO_ADDR2 | NO_PORT_B);
2957 /* If there is no such conversation, or if there is one but for
2958 someone else's protocol then we just create a new conversation
2959 and assign our protocol to it.
2961 if ( (conversation == NULL) ||
2962 (conversation->dissector_handle != sub_dissector_handle) ) {
2963 conversation = conversation_new(pinfo->fd->num,
2964 &server_src_addr, 0, protocol,
2965 server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
2967 /* set the dissector for the new conversation */
2968 conversation_set_dissector(conversation, sub_dissector_handle);
2971 2.5 Per packet information.
2973 Information can be stored for each data packet that is processed by the
2974 dissector. The information is added with the p_add_proto_data function and
2975 retrieved with the p_get_proto_data function. The data pointers passed into
2976 the p_add_proto_data are not managed by the proto_data routines. If you use
2977 malloc or any other dynamic memory allocation scheme, you must release the
2978 data when it isn't required.
2981 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
2983 p_get_proto_data(frame_data *fd, int proto)
2986 fd - The fd pointer in the pinfo structure, pinfo->fd
2987 proto - Protocol id returned by the proto_register_protocol call
2988 during initialization
2989 proto_data - pointer to the dissector data.
2992 2.6 User Preferences.
2994 If the dissector has user options, there is support for adding these preferences
2995 to a configuration dialog.
2997 You must register the module with the preferences routine with -
2999 module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
3001 Where: proto_id - the value returned by "proto_register_protocol()" when
3002 the protocol was registered
3003 apply_cb - Callback routine that is call when preferences are applied
3006 Then you can register the fields that can be configured by the user with these
3009 /* Register a preference with an unsigned integral value. */
3010 void prefs_register_uint_preference(module_t *module, const char *name,
3011 const char *title, const char *description, guint base, guint *var);
3013 /* Register a preference with an Boolean value. */
3014 void prefs_register_bool_preference(module_t *module, const char *name,
3015 const char *title, const char *description, gboolean *var);
3017 /* Register a preference with an enumerated value. */
3018 void prefs_register_enum_preference(module_t *module, const char *name,
3019 const char *title, const char *description, gint *var,
3020 const enum_val_t *enumvals, gboolean radio_buttons)
3022 /* Register a preference with a character-string value. */
3023 void prefs_register_string_preference(module_t *module, const char *name,
3024 const char *title, const char *description, char **var)
3026 /* Register a preference with a range of unsigned integers (e.g.,
3029 void prefs_register_range_preference(module_t *module, const char *name,
3030 const char *title, const char *description, range_t *var,
3033 Where: module - Returned by the prefs_register_protocol routine
3034 name - This is appended to the name of the protocol, with a
3035 "." between them, to construct a name that identifies
3036 the field in the preference file; the name itself
3037 should not include the protocol name, as the name in
3038 the preference file will already have it
3039 title - Field title in the preferences dialog
3040 description - Comments added to the preference file above the
3042 var - pointer to the storage location that is updated when the
3043 field is changed in the preference dialog box
3044 enumvals - an array of enum_val_t structures. This must be
3045 NULL-terminated; the members of that structure are:
3047 a short name, to be used with the "-o" flag - it
3048 should not contain spaces or upper-case letters,
3049 so that it's easier to put in a command line;
3051 a description, which is used in the GUI (and
3052 which, for compatibility reasons, is currently
3053 what's written to the preferences file) - it can
3054 contain spaces, capital letters, punctuation,
3057 the numerical value corresponding to that name
3059 radio_buttons - TRUE if the field is to be displayed in the
3060 preferences dialog as a set of radio buttons,
3061 FALSE if it is to be displayed as an option
3063 max_value - The maximum allowed value for a range (0 is the minimum).
3065 An example from packet-beep.c -
3067 proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
3072 /* Register our configuration options for BEEP, particularly our port */
3074 beep_module = prefs_register_protocol(proto_beep, proto_reg_handoff_beep);
3076 prefs_register_uint_preference(beep_module, "tcp.port", "BEEP TCP Port",
3077 "Set the port for BEEP messages (if other"
3078 " than the default of 10288)",
3079 10, &global_beep_tcp_port);
3081 prefs_register_bool_preference(beep_module, "strict_header_terminator",
3082 "BEEP Header Requires CRLF",
3083 "Specifies that BEEP requires CRLF as a "
3084 "terminator, and not just CR or LF",
3085 &global_beep_strict_term);
3087 This will create preferences "beep.tcp.port" and
3088 "beep.strict_header_terminator", the first of which is an unsigned
3089 integer and the second of which is a Boolean.
3091 2.7 Reassembly/desegmentation for protocols running atop TCP.
3093 There are two main ways of reassembling a Protocol Data Unit (PDU) which
3094 spans across multiple TCP segments. The first approach is simpler, but
3095 assumes you are running atop of TCP when this occurs (but your dissector
3096 might run atop of UDP, too, for example), and that your PDUs consist of a
3097 fixed amount of data that includes enough information to determine the PDU
3098 length, possibly followed by additional data. The second method is more
3099 generic but requires more code and is less efficient.
3101 2.7.1 Using tcp_dissect_pdus().
3103 For the first method, you register two different dissection methods, one
3104 for the TCP case, and one for the other cases. It is a good idea to
3105 also have a dissect_PROTO_common function which will parse the generic
3106 content that you can find in all PDUs which is called from
3107 dissect_PROTO_tcp when the reassembly is complete and from
3108 dissect_PROTO_udp (or dissect_PROTO_other).
3110 To register the distinct dissector functions, consider the following
3111 example, stolen from packet-dns.c:
3113 dissector_handle_t dns_udp_handle;
3114 dissector_handle_t dns_tcp_handle;
3115 dissector_handle_t mdns_udp_handle;
3117 dns_udp_handle = create_dissector_handle(dissect_dns_udp,
3119 dns_tcp_handle = create_dissector_handle(dissect_dns_tcp,
3121 mdns_udp_handle = create_dissector_handle(dissect_mdns_udp,
3124 dissector_add("udp.port", UDP_PORT_DNS, dns_udp_handle);
3125 dissector_add("tcp.port", TCP_PORT_DNS, dns_tcp_handle);
3126 dissector_add("udp.port", UDP_PORT_MDNS, mdns_udp_handle);
3127 dissector_add("tcp.port", TCP_PORT_MDNS, dns_tcp_handle);
3129 The dissect_dns_udp function does very little work and calls
3130 dissect_dns_common, while dissect_dns_tcp calls tcp_dissect_pdus with a
3131 reference to a callback which will be called with reassembled data:
3134 dissect_dns_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3136 tcp_dissect_pdus(tvb, pinfo, tree, dns_desegment, 2,
3137 get_dns_pdu_len, dissect_dns_tcp_pdu);
3140 (The dissect_dns_tcp_pdu function acts similarly to dissect_dns_udp.)
3141 The arguments to tcp_dissect_pdus are:
3143 the tvbuff pointer, packet_info pointer, and proto_tree pointer
3144 passed to the dissector;
3146 a gboolean flag indicating whether desegmentation is enabled for
3149 the number of bytes of PDU data required to determine the length
3152 a routine that takes as arguments a packet_info pointer, a tvbuff
3153 pointer and an offset value representing the offset into the tvbuff
3154 at which a PDU begins and should return - *without* throwing an
3155 exception (it is guaranteed that the number of bytes specified by the
3156 previous argument to tcp_dissect_pdus is available, but more data
3157 might not be available, so don't refer to any data past that) - the
3158 total length of the PDU, in bytes;
3160 a routine that's passed a tvbuff pointer, packet_info pointer,
3161 and proto_tree pointer, with the tvbuff containing a
3162 possibly-reassembled PDU, and that should dissect that PDU.
3164 2.7.2 Modifying the pinfo struct.
3166 The second reassembly mode is preferred when the dissector cannot determine
3167 how many bytes it will need to read in order to determine the size of a PDU.
3169 This reassembly mode relies on Wireshark's mechanism for processing
3170 multiple PDUs per frame. When a dissector processes a PDU from a tvbuff
3171 the PDU may not be aligned to a frame of the underlying protocol.
3172 Wireshark allows dissectors to process PDUs in an idempotent
3173 way--dissectors only need to consider one PDU at a time. If your
3174 dissector discovers that it can not process a complete PDU from the
3175 current tvbuff the dissector should halt processing and request
3176 additional bytes from the lower level dissector.
3178 Your dissect_PROTO will be called by the lower level dissector whenever
3179 sufficient new bytes become available. Each time your dissector is called it is
3180 provided a different tvbuff, though the tvbuffs may contain data that your
3181 dissector declined to process during a previous call. When called a dissector
3182 should examine the tvbuff provided and determine if an entire PDU is available.
3183 If sufficient bytes are available the dissector processes the PDU and returns
3184 the length of the PDU from your dissect_PROTO.
3186 Completion of a PDU is signified by dissect_PROTO returning a positive
3187 value. The value is the number of bytes which were processed from the
3188 tvbuff. If there were insufficient bytes in the tvbuff to complete a
3189 PDU then dissect_PROTO must update the pinfo structure to indicate that
3190 more bytes are required. The desegment_offset field is the offset in
3191 the tvbuff at which the dissector will continue processing when next
3192 called. The desegment_len field should contain the estimated number of
3193 additional bytes required for completing the PDU. The dissect_PROTO
3194 will not be called again until the specified number of bytes are
3195 available. pinfo->desegment_len may be set to -1 if dissect_PROTO
3196 cannot determine how many additional bytes are required. Dissectors
3197 should set the desegment_len to a reasonable value when possible rather
3198 than always setting -1 as it will generally be more efficient.
3200 static hf_register_info hf[] = {
3202 {"C String", "c.string", FT_STRING, BASE_NONE, NULL, 0x0,
3208 * Dissect a buffer containing a C string.
3210 * @param tvb The buffer to dissect.
3211 * @param pinfo Packet Info.
3212 * @param tree The protocol tree.
3214 static void dissect_cstr(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
3217 gint available = tvb_reported_length_remaining(tvb, offset);
3218 gint len = tvb_strnlen(tvb, offset, available);
3221 /* No '\0' found, ask for another byte. */
3222 pinfo->desegment_offset = offset;
3223 pinfo->desegment_len = 1;
3227 if (check_col(pinfo->cinfo, COL_INFO)) {
3228 col_set_str(pinfo->cinfo, COL_INFO, "C String");
3231 len += 1; /* Add one for the '\0' */
3234 proto_tree_add_item(tree, hf_cstring, tvb, offset, len, FALSE);
3238 This simple dissector will repeatedly return -1 requesting one more byte until
3239 the tvbuff contains a complete C string. The C string will then be added to the
3240 protocol tree. Unfortunately since there is no way to guess the size of C
3241 String without seeing the entire string this dissector can never request more
3242 than one additional byte.
3246 The ptvcursor API allows a simpler approach to writing dissectors for
3247 simple protocols. The ptvcursor API works best for protocols whose fields
3248 are static and whose format does not depend on the value of other fields.
3249 However, even if only a portion of your protocol is statically defined,
3250 then that portion could make use of ptvcursors.
3252 The ptvcursor API lets you extract data from a tvbuff, and add it to a
3253 protocol tree in one step. It also keeps track of the position in the
3254 tvbuff so that you can extract data again without having to compute any
3255 offsets --- hence the "cursor" name of the API.
3257 The three steps for a simple protocol are:
3258 1. Create a new ptvcursor with ptvcursor_new()
3259 2. Add fields with multiple calls of ptvcursor_add()
3260 3. Delete the ptvcursor with ptvcursor_free()
3262 To use the ptvcursor API, include the "ptvcursor.h" file. The PGM dissector
3263 is an example of how to use it. You don't need to look at it as a guide;
3264 instead, the API description here should be good enough.
3266 2.8.1 ptvcursor API.
3269 ptvcursor_new(proto_tree*, tvbuff_t*, gint offset)
3270 This creates a new ptvcursor_t object for iterating over a tvbuff.
3271 You must call this and use this ptvcursor_t object so you can use the
3275 ptvcursor_add(ptvcursor_t*, int hf, gint length, gboolean endianness)
3276 This will extract 'length' bytes from the tvbuff and place it in
3277 the proto_tree as field 'hf', which is a registered header_field. The
3278 pointer to the proto_item that is created is passed back to you. Internally,
3279 the ptvcursor advances its cursor so the next call to ptvcursor_add
3280 starts where this call finished. The 'endianness' parameter matters for
3281 FT_UINT* and FT_INT* fields.
3284 ptvcursor_add_no_advance(ptvcursor_t*, int hf, gint length, gboolean endianness)
3285 Like ptvcursor_add, but does not advance the internal cursor.
3288 ptvcursor_advance(ptvcursor_t*, gint length)
3289 Advances the internal cursor without adding anything to the proto_tree.
3292 ptvcursor_free(ptvcursor_t*)
3293 Frees the memory associated with the ptvcursor. You must call this
3294 after your dissection with the ptvcursor API is completed.
3296 2.8.2 Miscellaneous functions.
3299 ptvcursor_tvbuff(ptvcursor_t*)
3300 returns the tvbuff associated with the ptvcursor
3303 ptvcursor_current_offset(ptvcursor_t*)
3304 returns the current offset
3307 ptvcursor_tree(ptvcursor_t*)
3308 returns the proto_tree associated with the ptvcursor
3311 ptvcursor_set_tree(ptvcursor_t*, proto_tree *)
3312 sets a new proto_tree for the ptvcursor