Ringbuffer rework.
[obnox/wireshark/wip.git] / doc / README.developer
1 $Id: README.developer,v 1.75 2003/06/12 10:15:25 guy Exp $
2
3 This file is a HOWTO for Ethereal developers. It describes how to start coding
4 a Ethereal protocol dissector and the use some of the important functions and
5 variables.
6
7 1. Setting up your protocol dissector code.
8
9 This section provides skeleton code for a protocol dissector. It also explains
10 the basic functions needed to enter values in the traffic summary columns,
11 add to the protocol tree, and work with registered header fields.
12
13 1.1 Code style.
14
15 1.1.1 Portability.
16
17 Ethereal runs on many platforms, and can be compiled with a number of
18 different compilers; here are some rules for writing code that will work
19 on multiple platforms.
20
21 Don't use C++-style comments (comments beginning with "//" and running
22 to the end of the line); Ethereal's dissectors are written in C, and
23 thus run through C rather than C++ compilers, and not all C compilers
24 support C++-style comments (GCC does, but IBM's C compiler for AIX, for
25 example, doesn't do so by default).
26
27 Don't use zero-length arrays; not all compilers support them.  If an
28 array would have no members, just leave it out.
29
30 Don't use "inline"; not all compilers support it.  If you want to have a
31 function be an inline function if the compiler supports it, use
32 G_INLINE_FUNC, which is declared by <glib.h>.  This may not work with
33 functions declared in header files; if it doesn't work, don't declare
34 the function in a header file, even if this requires that you not make
35 it inline on any platform.
36
37 Don't use "long long"; use "gint64" or "guint64", and only do so if
38 G_HAVE_GINT64 is defined.  Make sure your code works even if
39 G_HAVE_GINT64 isn't defined, even if that means treating 64-bit integral
40 data types as opaque arrays of bytes on platforms where it's not
41 defined.  Also, don't assume you can use "%lld", "%llu", "%llx", or
42 "%llo" to print 64-bit integral data types - not all platforms support
43 "%ll" for printing them.
44
45 Don't use "uint", "ulong" or "ushort"; they aren't defined on all
46 platforms.  If you want an "int-sized" unsigned quantity, use "uint"; if
47 you want a 32-bit unsigned quantity, use "guint32"; and if you want a
48 16-bit unsigned quantity, use "guint16".  Use "%d", "%u", "%x", and "%o"
49 to print those types; don't use "%ld", "%lu", "%lx", or "%lo", as longs
50 are 64 bits long on many platforms, but "guint32" is 32 bits long.
51
52 Don't use "long" to mean "signed 32-bit integer", and don't use
53 "unsigned long" to mean "unsigned 32-bit integer"; "long"s are 64 bits
54 long on many platforms.  Use "gint32" for signed 32-bit integers and use
55 "guint32" for unsigned 32-bit integers.
56
57 Don't use a label without a statement following it.  For example,
58 something such as
59
60         if (...) {
61
62                 ...
63
64         done:
65         }
66         
67 will not work with all compilers - you have to do
68
69         if (...) {
70
71                 ...
72
73         done:
74                 ;
75         }
76
77 with some statement, even if it's a null statement, after the label.
78
79 Don't use "bzero()", "bcopy()", or "bcmp()"; instead, use the ANSI C
80 routines
81
82         "memset()" (with zero as the second argument, so that it sets
83         all the bytes to zero);
84
85         "memcpy()" or "memmove()" (note that the first and second
86         arguments to "memcpy()" are in the reverse order to the
87         arguments to "bcopy()"; note also that "bcopy()" is typically
88         guaranteed to work on overlapping memory regions, while
89         "memcpy()" isn't, so if you may be copying from one region to a
90         region that overlaps it, use "memmove()", not "memcpy()" - but
91         "memcpy()" might be faster as a result of not guaranteeing
92         correct operation on overlapping memory regions);
93
94         and "memcmp()" (note that "memcmp()" returns 0, 1, or -1, doing
95         an ordered comparison, rather than just returning 0 for "equal"
96         and 1 for "not equal", as "bcmp()" does).
97
98 Not all platforms necessarily have "bzero()"/"bcopy()"/"bcmp()", and
99 those that do might not declare them in the header file on which they're
100 declared on your platform.
101
102 Don't use "index()" or "rindex()"; instead, use the ANSI C equivalents,
103 "strchr()" and "strrchr()".  Not all platforms necessarily have
104 "index()" or "rindex()", and those that do might not declare them in the
105 header file on which they're declared on your platform.
106
107 Don't fetch data from packets by getting a pointer to data in the packet
108 with "tvb_get_ptr()", casting that pointer to a pointer to a structure,
109 and dereferencing that pointer.  That point won't necessarily be aligned
110 on the proper boundary, which can cause crashes on some platforms (even
111 if it doesn't crash on an x86-based PC); furthermore, the data in a
112 packet is not necessarily in the byte order of the machine on which
113 Ethereal is running.  Use the tvbuff routines to extract individual
114 items from the packet, or use "proto_tree_add_item()" and let it extract
115 the items for you.
116
117 Don't use "ntohs()", "ntohl()", "htons()", or "htonl()"; the header
118 files required to define or declare them differ between platforms, and
119 you might be able to get away with not including the appropriate header
120 file on your platform but that might not work on other platforms. 
121 Instead, use "g_ntohs()", "g_ntohl()", "g_htons()", and "g_htonl()";
122 those are declared by <glib.h>, and you'll need to include that anyway,
123 as Ethereal header files that all dissectors must include use stuff from
124 <glib.h>.
125
126 Don't put a comma after the last element of an enum - some compilers may
127 either warn about it (producing extra noise) or refuse to accept it.
128
129 Don't include <unistd.h> without protecting it with
130
131         #ifdef HAVE_UNISTD_H
132
133                 ...
134
135         #endif
136
137 and, if you're including it to get routines such as "open()", "close()",
138 "read()", and "write()" declared, also include <io.h> if present:
139
140         #ifdef HAVE_IO_H
141         #include <io.h>
142         #endif
143
144 in order to declare the Windows C library routines "_open()",
145 "_close()", "_read()", and "_write()".  Your file must include <glib.h>
146 - which many of the Ethereal header files include, so you might not have
147 to include it explicitly - in order to get "open()", "close()",
148 "read()", "write()", etc. mapped to "_open()", "_close()", "_read()",
149 "_write()", etc..
150
151 When opening a file with "fopen()", "freopen()", or "fdopen()", if the
152 file contains ASCII text, use "r", "w", "a", and so on as the open mode
153 - but if it contains binary data, use "rb", "wb", and so on.  On
154 Windows, if a file is opened in a text mode, writing a byte with the
155 value of octal 12 (newline) to the file causes two bytes, one with the
156 value octal 15 (carriage return) and one with the value octal 12, to be
157 written to the file, and causes bytes with the value octal 15 to be
158 discarded when reading the file (to translate between C's UNIX-style
159 lines that end with newline and Windows' DEC-style lines that end with
160 carriage return/line feed).
161
162 In addition, that also means that when opening or creating a binary
163 file, you must use "open()" (with O_CREAT and possibly O_TRUNC if the
164 file is to be created if it doesn't exist), and OR in the O_BINARY flag. 
165 That flag is not present on most, if not all, UNIX systems, so you must
166 also do
167
168         #ifndef O_BINARY
169         #define O_BINARY        0
170         #endif
171
172 to properly define it for UNIX (it's not necessary on UNIX).
173
174 1.1.2 Name convention.
175
176 Ethereal uses the underscore_convention rather than the InterCapConvention for
177 function names, so new code should probably use underscores rather than
178 intercaps for functions and variable names. This is especially important if you
179 are writing code that will be called from outside your code.  We are just
180 trying to keep thing consistent for other users.
181
182 1.2 Skeleton code.
183
184 Ethereal requires certain things when setting up a protocol dissector. 
185 Below is skeleton code for a dissector that you can copy to a file and
186 fill in.  Your dissector should follow the naming convention of packet-
187 followed by the abbreviated name for the protocol.  It is recommended
188 that where possible you keep to the IANA abbreviated name for the
189 protocol, if there is one, or a commonly-used abbreviation for the
190 protocol, if any.
191
192 Dissectors that use the dissector registration to tell a lower level
193 dissector don't need to define a prototype in the .h file. For other
194 dissectors the main dissector routine should have a prototype in a header
195 file whose name is "packet-", followed by the abbreviated name for the
196 protocol, followed by ".h"; any dissector file that calls your dissector
197 should be changed to include that file.
198
199 You may not need to include all the headers listed in the skeleton
200 below, and you may need to include additional headers.  For example, the
201 code inside
202
203         #ifdef NEED_SNPRINTF_H
204
205                 ...
206
207         #endif
208
209 is needed only if you are using the "snprintf()" function.
210
211 The "$Id: README.developer,v 1.75 2003/06/12 10:15:25 guy Exp $"
212 in the comment will be updated by CVS when the file is
213 checked in; it will allow the RCS "ident" command to report which
214 version of the file is currently checked out.
215
216 ------------------------------------Cut here------------------------------------
217 /* packet-PROTOABBREV.c
218  * Routines for PROTONAME dissection
219  * Copyright 2000, YOUR_NAME <YOUR_EMAIL_ADDRESS>
220  *
221  * $Id: README.developer,v 1.75 2003/06/12 10:15:25 guy Exp $
222  *
223  * Ethereal - Network traffic analyzer
224  * By Gerald Combs <gerald@ethereal.com>
225  * Copyright 1998 Gerald Combs
226  *
227  * Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
228  * is a dissector file; if you just copied this from README.developer,
229  * don't bother with the "Copied from" - you don't even need to put
230  * in a "Copied from" if you copied an existing dissector, especially
231  * if the bulk of the code in the new dissector is your code)
232  * 
233  * This program is free software; you can redistribute it and/or
234  * modify it under the terms of the GNU General Public License
235  * as published by the Free Software Foundation; either version 2
236  * of the License, or (at your option) any later version.
237  * 
238  * This program is distributed in the hope that it will be useful,
239  * but WITHOUT ANY WARRANTY; without even the implied warranty of
240  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
241  * GNU General Public License for more details.
242  * 
243  * You should have received a copy of the GNU General Public License
244  * along with this program; if not, write to the Free Software
245  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
246  */
247
248 #ifdef HAVE_CONFIG_H
249 # include "config.h"
250 #endif
251
252 #include <stdio.h>
253 #include <stdlib.h>
254 #include <string.h>
255
256 #include <glib.h>
257
258 #ifdef NEED_SNPRINTF_H
259 # include "snprintf.h"
260 #endif
261
262 #include <epan/packet.h>
263 #include "packet-PROTOABBREV.h"
264
265 /* Initialize the protocol and registered fields */
266 static int proto_PROTOABBREV = -1;
267 static int hf_PROTOABBREV_FIELDABBREV = -1;
268
269 /* Initialize the subtree pointers */
270 static gint ett_PROTOABBREV = -1;
271
272 /* Code to actually dissect the packets */
273 static void
274 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
275 {
276
277 /* Set up structures needed to add the protocol subtree and manage it */
278         proto_item *ti;
279         proto_tree *PROTOABBREV_tree;
280
281 /* Make entries in Protocol column and Info column on summary display */
282         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
283                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
284     
285 /* This field shows up as the "Info" column in the display; you should make
286    it, if possible, summarize what's in the packet, so that a user looking
287    at the list of packets can tell what type of packet it is. See section 1.5
288    for more information.
289
290    If you are setting it to a constant string, use "col_set_str()", as
291    it's more efficient than the other "col_set_XXX()" calls.
292
293    If you're setting it to a string you've constructed, or will be
294    appending to the column later, use "col_add_str()".
295
296    "col_add_fstr()" can be used instead of "col_add_str()"; it takes
297    "printf()"-like arguments.  Don't use "col_add_fstr()" with a format
298    string of "%s" - just use "col_add_str()" or "col_set_str()", as it's
299    more efficient than "col_add_fstr()".
300
301    If you will be fetching any data from the packet before filling in
302    the Info column, clear that column first, in case the calls to fetch
303    data from the packet throw an exception because they're fetching data
304    past the end of the packet, so that the Info column doesn't have data
305    left over from the previous dissector; do
306
307         if (check_col(pinfo->cinfo, COL_INFO)) 
308                 col_clear(pinfo->cinfo, COL_INFO);
309
310    */
311
312         if (check_col(pinfo->cinfo, COL_INFO)) 
313                 col_set_str(pinfo->cinfo, COL_INFO, "XXX Request");
314
315 /* In the interest of speed, if "tree" is NULL, avoid building a
316    protocol tree and adding stuff to it if possible.  Note,
317    however, that you must call subdissectors regardless of whether
318    "tree" is NULL or not. */
319         if (tree) {
320
321 /* NOTE: The offset and length values in the call to
322    "proto_tree_add_item()" define what data bytes to highlight in the hex
323    display window when the line in the protocol tree display
324    corresponding to that item is selected.
325
326    Supplying a length of -1 is the way to highlight all data from the
327    offset to the end of the packet. */
328
329 /* create display subtree for the protocol */
330                 ti = proto_tree_add_item(tree, proto_PROTOABBREV, tvb, 0, -1, FALSE);
331
332                 PROTOABBREV_tree = proto_item_add_subtree(ti, ett_PROTOABBREV);
333
334 /* add an item to the subtree, see section 1.6 for more information */
335                 proto_tree_add_item(PROTOABBREV_tree,
336                     hf_PROTOABBREV_FIELDABBREV, tvb, offset, len, FALSE)
337
338
339 /* Continue adding tree items to process the packet here */
340
341
342         }
343
344 /* If this protocol has a sub-dissector call it here, see section 1.8 */
345 }
346
347
348 /* Register the protocol with Ethereal */
349
350 /* this format is require because a script is used to build the C function
351    that calls all the protocol registration.
352 */
353
354 void
355 proto_register_PROTOABBREV(void)
356 {                 
357
358 /* Setup list of header fields  See Section 1.6.1 for details*/
359         static hf_register_info hf[] = {
360                 { &hf_PROTOABBREV_FIELDABBREV,
361                         { "FIELDNAME",           "PROTOABBREV.FIELDABBREV",
362                         FIELDTYPE, FIELDBASE, FIELDCONVERT, BITMASK,          
363                         "FIELDDESCR" }
364                 },
365         };
366
367 /* Setup protocol subtree array */
368         static gint *ett[] = {
369                 &ett_PROTOABBREV,
370         };
371
372 /* Register the protocol name and description */
373         proto_PROTOABBREV = proto_register_protocol("PROTONAME",
374             "PROTOSHORTNAME", "PROTOABBREV");
375
376 /* Required function calls to register the header fields and subtrees used */
377         proto_register_field_array(proto_PROTOABBREV, hf, array_length(hf));
378         proto_register_subtree_array(ett, array_length(ett));
379 }
380
381
382 /* If this dissector uses sub-dissector registration add a registration routine.
383    This format is required because a script is used to find these routines and
384    create the code that calls these routines.
385 */
386 void
387 proto_reg_handoff_PROTOABBREV(void)
388 {
389         dissector_handle_t PROTOABBREV_handle;
390
391         PROTOABBREV_handle = create_dissector_handle(dissect_PROTOABBREV,
392             proto_PROTOABBREV);
393         dissector_add("PARENT_SUBFIELD", ID_VALUE, PROTOABBREV_handle);
394 }
395
396 ------------------------------------Cut here------------------------------------
397
398 1.3 Explanation of needed substitutions in code skeleton.
399
400 In the above code block the following strings should be substituted with
401 your information.
402
403 YOUR_NAME       Your name, of course.  You do want credit, don't you?
404                 It's the only payment you will receive....
405 YOUR_EMAIL_ADDRESS      Keep those cards and letters coming.
406 WHATEVER_FILE_YOU_USED  Add this line if you are using another file as a
407                 starting point.
408 PROTONAME       The name of the protocol; this is displayed in the
409                 top-level protocol tree item for that protocol.
410 PROTOSHORTNAME  An abbreviated name for the protocol; this is displayed
411                 in the "Preferences" dialog box if your dissector has
412                 any preferences, and in the dialog box for filter fields
413                 when constructing a filter expression.
414 PROTOABBREV     A name for the protocol for use in filter expressions;
415                 it should contain only lower-case letters, digits, and
416                 hyphens.
417 FIELDNAME       The displayed name for the header field.
418 FIELDABBREV     The abbreviated name for the header field. (NO SPACES)
419 FIELDTYPE       FT_NONE, FT_BOOLEAN, FT_UINT8, FT_UINT16, FT_UINT24,
420                 FT_UINT32, FT_UINT64, FT_INT8, FT_INT16, FT_INT24, FT_INT32,
421                 FT_INT64, FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME,
422                 FT_RELATIVE_TIME, FT_STRING, FT_STRINGZ, FT_UINT_STRING,
423                 FT_ETHER, FT_BYTES, FT_IPv4, FT_IPv6, FT_IPXNET,
424                 FT_FRAMENUM
425 FIELDBASE       BASE_NONE, BASE_DEC, BASE_HEX, BASE_OCT
426 FIELDCONVERT    VALS(x), TFS(x), NULL
427 BITMASK         Usually 0x0 unless using the TFS(x) field conversion.
428 FIELDDESCR      A brief description of the field.
429 PARENT_SUBFIELD Lower level protocol field used for lookup, i.e. "tcp.port"
430 ID_VALUE        Lower level protocol field value that identifies this protocol
431                 For example the TCP or UDP port number
432
433 If, for example, PROTONAME is "Internet Bogosity Discovery Protocol",
434 PROTOSHORTNAME would be "IBDP", and PROTOABBREV would be "ibdp".  Try to
435 conform with IANA names.
436
437 1.4 The dissector and the data it receives.
438
439
440 1.4.1 Header file.
441
442 This is only needed if the dissector doesn't use self-registration to
443 register itself with the lower level dissector.
444
445 The dissector has the following header that must be placed into
446 packet-PROTOABBREV.h.
447
448 void
449 dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
450
451
452 1.4.2 Extracting data from packets.
453
454 NOTE: See the README.tvbuff for more details
455
456 The "tvb" argument to a dissector points to a buffer containing the raw
457 data to be analyzed by the dissector; for example, for a protocol
458 running atop UDP, it contains the UDP payload (but not the UDP header,
459 or any protocol headers above it).  A tvbuffer is a opaque data
460 structure, the internal data structures are hidden and the data must be
461 access via the tvbuffer accessors.
462
463 The accessors are:
464
465 Single-byte accessor:
466
467 guint8  tvb_get_guint8(tvbuff_t*, gint offset);
468
469 Network-to-host-order access for 16-bit integers (guint16), 32-bit
470 integers (guint32), and 24-bit integers:
471
472 guint16 tvb_get_ntohs(tvbuff_t*, gint offset);
473 guint32 tvb_get_ntohl(tvbuff_t*, gint offset);
474 guint32 tvb_get_ntoh24(tvbuff_t*, gint offset);
475
476 Network-to-host-order access for single-precision and double-precision
477 IEEE floating-point numbers:
478
479 gfloat tvb_get_ntohieee_float(tvbuff_t*, gint offset);
480 gdouble tvb_get_ntohieee_double(tvbuff_t*, gint offset);
481
482 Little-Endian-to-host-order access for 16-bit integers (guint16), 32-bit
483 integers (guint32), and 24-bit integers:
484
485 guint16 tvb_get_letohs(tvbuff_t*, gint offset);
486 guint32 tvb_get_letohl(tvbuff_t*, gint offset);
487 guint32 tvb_get_letoh24(tvbuff_t*, gint offset);
488
489 Little-Endian-to-host-order access for single-precision and
490 double-precision IEEE floating-point numbers:
491
492 gfloat tvb_get_letohieee_float(tvbuff_t*, gint offset);
493 gdouble tvb_get_letohieee_double(tvbuff_t*, gint offset);
494
495 NOTE: IPv4 addresses are not to be converted to host byte order before
496 being passed to "proto_tree_add_ipv4()".  You should use "tvb_memcpy()"
497 to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't,
498 for example, try to use "tvb_get_ntohl()", find that it gives you the
499 wrong answer on the PC on which you're doing development, and try
500 "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong
501 answer on big-endian machines.
502
503 Copying memory:
504 guint8* tvb_memcpy(tvbuff_t*, guint8* target, gint offset, gint length);
505 guint8* tvb_memdup(tvbuff_t*, gint offset, gint length);
506
507
508 Pointer-retrieval:
509 /* WARNING! This function is possibly expensive, temporarily allocating
510  * another copy of the packet data. Furthermore, it's dangerous because once
511  * this pointer is given to the user, there's no guarantee that the user will
512  * honor the 'length' and not overstep the boundaries of the buffer.
513  */ 
514 guint8* tvb_get_ptr(tvbuff_t*, gint offset, gint length);
515
516 The reason that tvb_get_ptr() have to allocate a copy of its data only
517 occurs with TVBUFF_COMPOSITES, data that spans multiple tvbuffers. If the
518 user request a pointer to a range of bytes that spans the member tvbuffs that
519 make up the TVBUFF_COMPOSITE, the data will have to be copied to another
520 memory region to assure that all the bytes are contiguous.
521
522
523
524 1.5 Functions to handle columns in the traffic summary window.
525
526 The topmost pane of the main window is a list of the packets in the
527 capture, possibly filtered by a display filter.
528
529 Each line corresponds to a packet, and has one or more columns, as
530 configured by the user.
531
532 Many of the columns are handled by code outside individual dissectors;
533 most dissectors need only specify the value to put in the "Protocol" and
534 "Info" columns.
535
536 Columns are specified by COL_ values; the COL_ value for the "Protocol"
537 field, typically giving an abbreviated name for the protocol (but not
538 the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the
539 COL_ value for the "Info" field, giving a summary of the contents of the
540 packet for that protocol, is COL_INFO. 
541
542 A value for a column should only be added if the user specified that it
543 be displayed; to check whether a given column is to be displayed, call
544 'check_col' with the COL_ value for that field as an argument - it will
545 return TRUE if the column is to be displayed and FALSE if it is not to
546 be displayed.
547
548 The value for a column can be specified with one of several functions,
549 all of which take the 'fd' argument to the dissector as their first
550 argument, and the COL_ value for the column as their second argument.
551
552 1.5.1 The col_set_str function.
553
554 'col_set_str' takes a string as its third argument, and sets the value
555 for the column to that value.  It assumes that the pointer passed to it
556 points to a string constant or a static "const" array, not to a
557 variable, as it doesn't copy the string, it merely saves the pointer
558 value; the argument can itself be a variable, as long as it always
559 points to a string constant or a static "const" array.
560
561 It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if
562 the dissector will be using 'col_append_str' or 'col_append_fstr" to
563 append more information to the column, the string will have to be copied
564 anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in
565 that case.
566
567 For example, to set the "Protocol" column
568 to "PROTOABBREV":
569
570         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
571                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV");
572
573
574 1.5.2 The col_add_str function.
575
576 'col_add_str' takes a string as its third argument, and sets the value
577 for the column to that value.  It takes the same arguments as
578 'col_set_str', but copies the string, so that if the string is, for
579 example, an automatic variable that won't remain in scope when the
580 dissector returns, it's safe to use.
581
582
583 1.5.3 The col_add_fstr function.
584
585 'col_add_fstr' takes a 'printf'-style format string as its third
586 argument, and 'printf'-style arguments corresponding to '%' format
587 items in that string as its subsequent arguments.  For example, to set
588 the "Info" field to "<XXX> request, <N> bytes", where "reqtype" is a
589 string containing the type of the request in the packet and "n" is an
590 unsigned integer containing the number of bytes in the request:
591
592         if (check_col(pinfo->cinfo, COL_INFO)) 
593                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes",
594                     reqtype, n);
595
596 Don't use 'col_add_fstr' with a format argument of just "%s" -
597 'col_add_str', or possibly even 'col_set_str' if the string that matches
598 the "%s" is a static constant string, will do the same job more
599 efficiently.
600
601
602 1.5.4 The col_clear function.
603
604 If the Info column will be filled with information from the packet, that
605 means that some data will be fetched from the packet before the Info
606 column is filled in.  If the packet is so small that the data in
607 question cannot be fetched, the routines to fetch the data will throw an
608 exception (see the comment at the beginning about tvbuffers improving
609 the handling of short packets - the tvbuffers keep track of how much
610 data is in the packet, and throw an exception on an attempt to fetch
611 data past the end of the packet, so that the dissector won't process
612 bogus data), causing the Info column not to be filled in.
613
614 This means that the Info column will have data for the previous
615 protocol, which would be confusing if, for example, the Protocol column
616 had data for this protocol.
617
618 Therefore, before a dissector fetches any data whatsoever from the
619 packet (unless it's a heuristic dissector fetching data to determine
620 whether the packet is one that it should dissect, in which case it
621 should check, before fetching the data, whether there's any data to
622 fetch; if there isn't, it should return FALSE), it should set the
623 Protocol column and the Info column.
624
625 If the Protocol column will ultimately be set to, for example, a value
626 containing a protocol version number, with the version number being a
627 field in the packet, the dissector should, before fetching the version
628 number field or any other field from the packet, set it to a value
629 without a version number, using 'col_set_str', and should later set it
630 to a value with the version number after it's fetched the version
631 number.
632
633 If the Info column will ultimately be set to a value containing
634 information from the packet, the dissector should, before fetching any
635 fields from the packet, clear the column using 'col_clear' (which is
636 more efficient than clearing it by calling 'col_set_str' or
637 'col_add_str' with a null string), and should later set it to the real
638 string after it's fetched the data to use when doing that.
639
640
641 1.5.5 The col_append_str function.
642
643 Sometimes the value of a column, especially the "Info" column, can't be
644 conveniently constructed at a single point in the dissection process;
645 for example, it might contain small bits of information from many of the
646 fields in the packet.  'col_append_str' takes, as arguments, the same
647 arguments as 'col_add_str', but the string is appended to the end of the
648 current value for the column, rather than replacing the value for that
649 column.  (Note that no blank separates the appended string from the
650 string to which it is appended; if you want a blank there, you must add
651 it yourself as part of the string being appended.)
652
653
654 1.5.6 The col_append_fstr function.
655
656 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to
657 'col_add_str' - it takes, as arguments, the same arguments as
658 'col_add_fstr', but the formatted string is appended to the end of the
659 current value for the column, rather than replacing the value for that
660 column.
661
662
663 1.6 Constructing the protocol tree.
664
665 The middle pane of the main window, and the topmost pane of a packet
666 popup window, are constructed from the "protocol tree" for a packet.
667
668 The protocol tree, or proto_tree, is a GNode, the N-way tree structure
669 available within GLIB. Of course the protocol dissectors don't care
670 what a proto_tree really is; they just pass the proto_tree pointer as an
671 argument to the routines which allow them to add items and new branches
672 to the tree.
673
674 When a packet is selected in the packet-list pane, or a packet popup
675 window is created, a new logical protocol tree (proto_tree) is created. 
676 The pointer to the proto_tree (in this case, 'protocol tree'), is passed
677 to the top-level protocol dissector, and then to all subsequent protocol
678 dissectors for that packet, and then the GUI tree is drawn via
679 proto_tree_draw().
680
681 The logical proto_tree needs to know detailed information about the
682 protocols and fields about which information will be collected from the
683 dissection routines. By strictly defining (or "typing") the data that can
684 be attached to a proto tree, searching and filtering becomes possible.
685 This means that the for every protocol and field (which I also call
686 "header fields", since they are fields in the protocol headers) which
687 might be attached to a tree, some information is needed.
688
689 Every dissector routine will need to register its protocols and fields
690 with the central protocol routines (in proto.c). At first I thought I
691 might keep all the protocol and field information about all the
692 dissectors in one file, but decentralization seemed like a better idea.
693 That one file would have gotten very large; one small change would have
694 required a re-compilation of the entire file. Also, by allowing
695 registration of protocols and fields at run-time, loadable modules of
696 protocol dissectors (perhaps even user-supplied) is feasible.
697
698 To do this, each protocol should have a register routine, which will be
699 called when Ethereal starts.  The code to call the register routines is
700 generated automatically; to arrange that a protocol's register routine
701 be called at startup:
702
703         the file containing a dissector's "register" routine must be
704         added to "DISSECTOR_SOURCES" in "Makefile.am";
705  
706         the "register" routine must have a name of the form
707         "proto_register_XXX";
708   
709         the "register" routine must take no argument, and return no
710         value;
711  
712         the "register" routine's name must appear in the source file
713         either at the beginning of the line, or preceded only by "void "
714         at the beginning of the line (that'd typically be the
715         definition) - other white space shouldn't cause a problem, e.g.:
716  
717 void proto_register_XXX(void) {
718  
719         ...
720  
721 }
722  
723 and
724  
725 void
726 proto_register_XXX( void )
727 {
728  
729         ...
730  
731 }
732  
733         and so on should work.
734
735 For every protocol or field that a dissector wants to register, a variable of
736 type int needs to be used to keep track of the protocol. The IDs are
737 needed for establishing parent/child relationships between protocols and
738 fields, as well as associating data with a particular field so that it
739 can be stored in the logical tree and displayed in the GUI protocol
740 tree.
741
742 Some dissectors will need to create branches within their tree to help
743 organize header fields. These branches should be registered as header
744 fields. Only true protocols should be registered as protocols. This is
745 so that a display filter user interface knows how to distinguish
746 protocols from fields.
747
748 A protocol is registered with the name of the protocol and its
749 abbreviation.
750
751 Here is how the frame "protocol" is registered.
752
753         int proto_frame;
754
755         proto_frame = proto_register_protocol (
756                 /* name */            "Frame",
757                 /* short name */      "Frame",
758                 /* abbrev */          "frame" );
759
760 A header field is also registered with its name and abbreviation, but
761 information about the its data type is needed. It helps to look at
762 the header_field_info struct to see what information is expected:
763
764 struct header_field_info {
765         char                            *name;
766         char                            *abbrev;
767         enum ftenum                     type;
768         int                             display;
769         void                            *strings;
770         guint                           bitmask;
771         char                            *blurb;
772
773         int                             id;       /* calculated */
774         int                             parent;
775         int                             bitshift; /* calculated */
776 };
777
778 name
779 ----
780 A string representing the name of the field. This is the name
781 that will appear in the graphical protocol tree.
782
783 abbrev
784 ------
785 A string with an abbreviation of the field. We concatenate the
786 abbreviation of the parent protocol with an abbreviation for the field,
787 using a period as a separator. For example, the "src" field in an IP packet
788 would have "ip.addr" as an abbreviation. It is acceptable to have
789 multiple levels of periods if, for example, you have fields in your
790 protocol that are then subdivided into subfields. For example, TRMAC
791 has multiple error fields, so the abbreviations follow this pattern:
792 "trmac.errors.iso", "trmac.errors.noniso", etc.
793
794 The abbreviation is the identifier used in a display filter.
795
796 type
797 ----
798 The type of value this field holds. The current field types are:
799
800         FT_NONE                 No field type. Used for fields that
801                                 aren't given a value, and that can only
802                                 be tested for presence or absence; a
803                                 field that represents a data structure,
804                                 with a subtree below it containing
805                                 fields for the members of the structure,
806                                 or that represents an array with a
807                                 subtree below it containing fields for
808                                 the members of the array, might be an
809                                 FT_NONE field.
810         FT_BOOLEAN              0 means "false", any other value means
811                                 "true".
812         FT_FRAMENUM             A frame number; if this is used, the "Go
813                                 To Corresponding Frame" menu item can
814                                 work on that field.
815         FT_UINT8                An 8-bit unsigned integer.
816         FT_UINT16               A 16-bit unsigned integer.
817         FT_UINT24               A 24-bit unsigned integer.
818         FT_UINT32               A 32-bit unsigned integer.
819         FT_UINT64               A 64-bit unsigned integer.
820         FT_INT8                 An 8-bit signed integer.
821         FT_INT16                A 16-bit signed integer.
822         FT_INT24                A 24-bit signed integer.
823         FT_INT32                A 32-bit signed integer.
824         FT_INT64                A 64-bit signed integer.
825         FT_FLOAT                A single-precision floating point number.
826         FT_DOUBLE               A double-precision floating point number.
827         FT_ABSOLUTE_TIME        Seconds (4 bytes) and nanoseconds (4 bytes)
828                                 of time displayed as month name, month day,
829                                 year, hours, minutes, and seconds with 9
830                                 digits after the decimal point.
831         FT_RELATIVE_TIME        Seconds (4 bytes) and nanoseconds (4 bytes)
832                                 of time displayed as seconds and 9 digits
833                                 after the decimal point.
834         FT_STRING               A string of characters, not necessarily
835                                 NUL-terminated, but possibly NUL-padded.
836                                 This, and the other string-of-characters
837                                 types, are to be used for text strings,
838                                 not raw binary data.
839         FT_STRINGZ              A NUL-terminated string of characters.
840         FT_UINT_STRING          A counted string of characters, consisting
841                                 of a count (represented as an integral
842                                 value) followed immediately by the
843                                 specified number of characters.
844         FT_ETHER                A six octet string displayed in
845                                 Ethernet-address format.
846         FT_BYTES                A string of bytes with arbitrary values;
847                                 used for raw binary data.
848         FT_IPv4                 A version 4 IP address (4 bytes) displayed
849                                 in dotted-quad IP address format (4
850                                 decimal numbers separated by dots).
851         FT_IPv6                 A version 6 IP address (16 bytes) displayed
852                                 in standard IPv6 address format.
853         FT_IPXNET               An IPX address displayed in hex as a 6-byte
854                                 network number followed by a 6-byte station
855                                 address. 
856
857 Some of these field types are still not handled in the display filter
858 routines, but the most common ones are. The FT_UINT* variables all
859 represent unsigned integers, and the FT_INT* variables all represent
860 signed integers; the number on the end represent how many bits are used
861 to represent the number.
862
863 display
864 -------
865 The display field has a couple of overloaded uses. This is unfortunate,
866 but since we're C as an application programming language, this sometimes
867 makes for cleaner programs. Right now I still think that overloading
868 this variable was okay.
869
870 For integer fields (FT_UINT* and FT_INT*), this variable represents the
871 base in which you would like the value displayed.  The acceptable bases
872 are:
873
874         BASE_DEC,
875         BASE_HEX,
876         BASE_OCT
877
878 BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal,
879 respectively.
880
881 For FT_BOOLEAN fields that are also bitfields, 'display' is used to tell
882 the proto_tree how wide the parent bitfield is.  With integers this is
883 not needed since the type of integer itself (FT_UINT8, FT_UINT16,
884 FT_UINT24, FT_UINT32, etc.) tells the proto_tree how wide the parent
885 bitfield is.
886
887 Additionally, BASE_NONE is used for 'display' as a NULL-value. That is,
888 for non-integers and non-bitfield FT_BOOLEANs, you'll want to use BASE_NONE
889 in the 'display' field.  You may not use BASE_NONE for integers.
890
891 It is possible that in the future we will record the endianness of
892 integers. If so, it is likely that we'll use a bitmask on the display field
893 so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX.
894 But that has not happened yet.
895
896 strings
897 -------
898 Some integer fields, of type FT_UINT*, need labels to represent the true
899 value of a field.  You could think of those fields as having an
900 enumerated data type, rather than an integral data type.
901
902 A 'value_string' structure is a way to map values to strings. 
903
904         typedef struct _value_string {
905                 guint32  value;
906                 gchar   *strptr;
907         } value_string;
908
909 For fields of that type, you would declare an array of "value_string"s:
910
911         static const value_string valstringname[] = {
912                 { INTVAL1, "Descriptive String 1" }, 
913                 { INTVAL2, "Descriptive String 2" }, 
914                 { 0,       NULL },
915         };
916
917 (the last entry in the array must have a NULL 'strptr' value, to
918 indicate the end of the array).  The 'strings' field would be set to
919 'VALS(valstringname)'.
920
921 (Note: before Ethereal 0.7.6, we had separate field types like
922 FT_VALS_UINT8 which denoted the use of value_strings.  Now, the
923 non-NULLness of the pointer lets the proto_tree know that a value_string
924 is meant for this field).
925
926 If the field has a numeric rather than an enumerated type, the 'strings'
927 field would be set to NULL.
928
929 FT_BOOLEANS have a default map of 0 = "False", 1 (or anything else) = "True".
930 Sometimes it is useful to change the labels for boolean values (e.g.,
931 to "Yes"/"No", "Fast"/"Slow", etc.).  For these mappings, a struct called
932 true_false_string is used. (This struct is new as of Ethereal 0.7.6).
933
934         typedef struct true_false_string {
935                 char    *true_string;
936                 char    *false_string;
937         } true_false_string;
938
939 For Boolean fields for which "False" and "True" aren't the desired
940 labels, you would declare a "true_false_string"s:
941
942         static const true_false_string boolstringname = {
943                 "String for True",
944                 "String for False"
945         };
946
947 Its two fields are pointers to the string representing truth, and the
948 string representing falsehood.  For FT_BOOLEAN fields that need a
949 'true_false_string' struct, the 'strings' field would be set to
950 'TFS(&boolstringname)'. 
951
952 If the Boolean field is to be displayed as "False" or "True", the
953 'strings' field would be set to NULL.
954
955 bitmask
956 -------
957 If the field is a bitfield, then the bitmask is the mask which will
958 leave only the bits needed to make the field when ANDed with a value.
959 The proto_tree routines will calculate 'bitshift' automatically
960 from 'bitmask', by finding the rightmost set bit in the bitmask.
961 If the field is not a bitfield, then bitmask should be set to 0.
962
963 blurb
964 -----
965 This is a string giving a proper description of the field.
966 It should be at least one grammatically complete sentence.
967 It is meant to provide a more detailed description of the field than the
968 name alone provides. This information will be used in the man page, and
969 in a future GUI display-filter creation tool. We might also add tooltips
970 to the labels in the GUI protocol tree, in which case the blurb would
971 be used as the tooltip text.
972
973
974 1.6.1 Field Registration.
975
976 Protocol registration is handled by creating an instance of the
977 header_field_info struct (or an array of such structs), and
978 calling the registration function along with the registration ID of
979 the protocol that is the parent of the fields. Here is a complete example:
980
981         static int proto_eg = -1;
982         static int hf_field_a = -1;
983         static int hf_field_b = -1;
984
985         static hf_register_info hf[] = {
986
987                 { &hf_field_a,
988                 { "Field A",    "proto.field_a", FT_UINT8, BASE_HEX, NULL,
989                         0xf0, "Field A represents Apples" }},
990
991                 { &hf_field_b,
992                 { "Field B",    "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs),
993                         0x0, "Field B represents Bananas" }}
994         };
995
996         proto_eg = proto_register_protocol("Example Protocol",
997             "PROTO", "proto");
998         proto_register_field_array(proto_eg, hf, array_length(hf));
999
1000 Be sure that your array of hf_register_info structs is declared 'static',
1001 since the proto_register_field_array() function does not create a copy
1002 of the information in the array... it uses that static copy of the
1003 information that the compiler created inside your array. Here's the
1004 layout of the hf_register_info struct:
1005
1006 typedef struct hf_register_info {
1007         int                     *p_id;  /* pointer to parent variable */
1008         header_field_info       hfinfo;
1009 } hf_register_info;
1010
1011 Also be sure to use the handy array_length() macro found in packet.h
1012 to have the compiler compute the array length for you at compile time.
1013
1014 If you don't have any fields to register, do *NOT* create a zero-length
1015 "hf" array; not all compilers used to compile Ethereal support them. 
1016 Just omit the "hf" array, and the "proto_register_field_array()" call,
1017 entirely.
1018
1019 1.6.2 Adding Items and Values to the Protocol Tree.
1020
1021 A protocol item is added to an existing protocol tree with one of a
1022 handful of proto_tree_add_XXX() functions.
1023
1024 Subtrees can be made with the proto_item_add_subtree() function:
1025
1026         item = proto_tree_add_item(....);
1027         new_tree = proto_item_add_subtree(item, tree_type);
1028
1029 This will add a subtree under the item in question; a subtree can be
1030 created under an item made by any of the "proto_tree_add_XXX" functions,
1031 so that the tree can be given an arbitrary depth.
1032
1033 Subtree types are integers, assigned by
1034 "proto_register_subtree_array()".  To register subtree types, pass an
1035 array of pointers to "gint" variables to hold the subtree type values to
1036 "proto_register_subtree_array()":
1037
1038         static gint ett_eg = -1;
1039         static gint ett_field_a = -1;
1040
1041         static gint *ett[] = {
1042                 &ett_eg,
1043                 &ett_field_a,
1044         };
1045
1046         proto_register_subtree_array(ett, array_length(ett));
1047
1048 in your "register" routine, just as you register the protocol and the
1049 fields for that protocol.
1050
1051 There are several functions that the programmer can use to add either
1052 protocol or field labels to the proto_tree:
1053
1054         proto_item*
1055         proto_tree_add_item(tree, id, tvb, start, length, little_endian);
1056
1057         proto_item*
1058         proto_tree_add_item_hidden(tree, id, tvb, start, length, little_endian);
1059
1060         proto_item*
1061         proto_tree_add_none_format(tree, id, tvb, start, length, format, ...);
1062
1063         proto_item*
1064         proto_tree_add_protocol_format(tree, id, tvb, start, length,
1065             format, ...);
1066
1067         proto_item *
1068         proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr);
1069
1070         proto_item *
1071         proto_tree_add_bytes_hidden(tree, id, tvb, start, length, start_ptr);
1072
1073         proto_item *
1074         proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr,
1075             format, ...);
1076
1077         proto_item *
1078         proto_tree_add_time(tree, id, tvb, start, length, value_ptr);
1079
1080         proto_item *
1081         proto_tree_add_time_hidden(tree, id, tvb, start, length, value_ptr);
1082
1083         proto_item *
1084         proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr,
1085             format, ...);
1086
1087         proto_item *
1088         proto_tree_add_ipxnet(tree, id, tvb, start, length, value);
1089
1090         proto_item *
1091         proto_tree_add_ipxnet_hidden(tree, id, tvb, start, length, value);
1092
1093         proto_item *
1094         proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value,
1095             format, ...);
1096
1097         proto_item *
1098         proto_tree_add_ipv4(tree, id, tvb, start, length, value);
1099
1100         proto_item *
1101         proto_tree_add_ipv4_hidden(tree, id, tvb, start, length, value);
1102
1103         proto_item *
1104         proto_tree_add_ipv4_format(tree, id, tvb, start, length, value,
1105             format, ...);
1106
1107         proto_item *
1108         proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr);
1109
1110         proto_item *
1111         proto_tree_add_ipv6_hidden(tree, id, tvb, start, length, value_ptr);
1112
1113         proto_item *
1114         proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr,
1115             format, ...);
1116
1117         proto_item *
1118         proto_tree_add_ether(tree, id, tvb, start, length, value_ptr);
1119
1120         proto_item *
1121         proto_tree_add_ether_hidden(tree, id, tvb, start, length, value_ptr);
1122
1123         proto_item *
1124         proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr,
1125             format, ...);
1126
1127         proto_item *
1128         proto_tree_add_string(tree, id, tvb, start, length, value_ptr);
1129
1130         proto_item *
1131         proto_tree_add_string_hidden(tree, id, tvb, start, length, value_ptr);
1132
1133         proto_item *
1134         proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr,
1135             format, ...);
1136
1137         proto_item *
1138         proto_tree_add_boolean(tree, id, tvb, start, length, value);
1139
1140         proto_item *
1141         proto_tree_add_boolean_hidden(tree, id, tvb, start, length, value);
1142
1143         proto_item *
1144         proto_tree_add_boolean_format(tree, id, tvb, start, length, value,
1145             format, ...);
1146
1147         proto_item *
1148         proto_tree_add_float(tree, id, tvb, start, length, value);
1149
1150         proto_item *
1151         proto_tree_add_float_hidden(tree, id, tvb, start, length, value);
1152
1153         proto_item *
1154         proto_tree_add_float_format(tree, id, tvb, start, length, value,
1155             format, ...);
1156
1157         proto_item *
1158         proto_tree_add_double(tree, id, tvb, start, length, value);
1159
1160         proto_item *
1161         proto_tree_add_double_hidden(tree, id, tvb, start, length, value);
1162
1163         proto_item *
1164         proto_tree_add_double_format(tree, id, tvb, start, length, value,
1165             format, ...);
1166
1167         proto_item *
1168         proto_tree_add_uint(tree, id, tvb, start, length, value);
1169
1170         proto_item *
1171         proto_tree_add_uint_hidden(tree, id, tvb, start, length, value);
1172
1173         proto_item *
1174         proto_tree_add_uint_format(tree, id, tvb, start, length, value,
1175             format, ...);
1176
1177         proto_item *
1178         proto_tree_add_int(tree, id, tvb, start, length, value);
1179
1180         proto_item *
1181         proto_tree_add_int_hidden(tree, id, tvb, start, length, value);
1182
1183         proto_item *
1184         proto_tree_add_int_format(tree, id, tvb, start, length, value,
1185             format, ...);
1186
1187         proto_item*
1188         proto_tree_add_text(tree, tvb, start, length, format, ...);
1189
1190         proto_item*
1191         proto_tree_add_text_valist(tree, tvb, start, length, format, ap);
1192
1193 The 'tree' argument is the tree to which the item is to be added.  The
1194 'tvb' argument is the tvbuff from which the item's value is being
1195 extracted; the 'start' argument is the offset from the beginning of that
1196 tvbuff of the item being added, and the 'length' argument is the length,
1197 in bytes, of the item.
1198
1199 The length of some items cannot be determined until the item has been
1200 dissected; to add such an item, add it with a length of -1, and, when the
1201 dissection is complete, set the length with 'proto_item_set_len()':
1202
1203         void
1204         proto_item_set_len(ti, length);
1205
1206 The "ti" argument is the value returned by the call that added the item
1207 to the tree, and the "length" argument is the length of the item.
1208
1209 proto_tree_add_item()
1210 ---------------------
1211 proto_tree_add_item is used when you wish to do no special formatting. 
1212 The item added to the GUI tree will contain the name (as passed in the
1213 proto_register_*() function) and a value.  The value will be fetched
1214 from the tvbuff by proto_tree_add_item(), based on the type of the field
1215 and, for integral and Boolean fields, the byte order of the value; the
1216 byte order is specified by the 'little_endian' argument, which is TRUE
1217 if the value is little-endian and FALSE if it is big-endian.
1218
1219 Now that definitions of fields have detailed information about bitfield
1220 fields, you can use proto_tree_add_item() with no extra processing to
1221 add bitfield values to your tree.  Here's an example.  Take the Format
1222 Identifer (FID) field in the Transmission Header (TH) portion of the SNA
1223 protocol.  The FID is the high nibble of the first byte of the TH.  The
1224 FID would be registered like this:
1225
1226         name            = "Format Identifer"
1227         abbrev          = "sna.th.fid"
1228         type            = FT_UINT8
1229         display         = BASE_HEX
1230         strings         = sna_th_fid_vals
1231         bitmask         = 0xf0
1232
1233 The bitmask contains the value which would leave only the FID if bitwise-ANDed
1234 against the parent field, the first byte of the TH.
1235
1236 The code to add the FID to the tree would be;
1237
1238         proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, TRUE);
1239
1240 The definition of the field already has the information about bitmasking
1241 and bitshifting, so it does the work of masking and shifting for us!
1242 This also means that you no longer have to create value_string structs
1243 with the values bitshifted.  The value_string for FID looks like this,
1244 even though the FID value is actually contained in the high nibble. 
1245 (You'd expect the values to be 0x0, 0x10, 0x20, etc.)
1246
1247 /* Format Identifier */
1248 static const value_string sna_th_fid_vals[] = {
1249         { 0x0,  "SNA device <--> Non-SNA Device" },
1250         { 0x1,  "Subarea Node <--> Subarea Node" },
1251         { 0x2,  "Subarea Node <--> PU2" },
1252         { 0x3,  "Subarea Node or SNA host <--> Subarea Node" },
1253         { 0x4,  "?" },
1254         { 0x5,  "?" },
1255         { 0xf,  "Adjaced Subarea Nodes" },
1256         { 0,    NULL }
1257 };
1258
1259 The final implication of this is that display filters work the way you'd
1260 naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent
1261 Subarea Nodes. The user does not have to shift the value of the FID to
1262 the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary
1263 before Ethereal 0.7.6.
1264
1265 proto_tree_add_item_hidden()
1266 ----------------------------
1267 proto_tree_add_item_hidden is used to add fields and values to a tree,
1268 but not show them on a GUI tree.  The caller may want a value to be
1269 included in a tree so that the packet can be filtered on this field, but
1270 the representation of that field in the tree is not appropriate.  An
1271 example is the token-ring routing information field (RIF).  The best way
1272 to show the RIF in a GUI is by a sequence of ring and bridge numbers. 
1273 Rings are 3-digit hex numbers, and bridges are single hex digits:
1274
1275         RIF: 001-A-013-9-C0F-B-555
1276
1277 In the case of RIF, the programmer should use a field with no value and
1278 use proto_tree_add_none_format() to build the above representation. The
1279 programmer can then add the ring and bridge values, one-by-one, with
1280 proto_tree_add_item_hidden() so that the user can then filter on or
1281 search for a particular ring or bridge. Here's a skeleton of how the
1282 programmer might code this.
1283
1284         char *rif;
1285         rif = create_rif_string(...);
1286
1287         proto_tree_add_none_format(tree, hf_tr_rif_label, ..., "RIF: %s", rif);
1288
1289         for(i = 0; i < num_rings; i++) {
1290                 proto_tree_add_item_hidden(tree, hf_tr_rif_ring, ..., FALSE);
1291         }
1292         for(i = 0; i < num_rings - 1; i++) {
1293                 proto_tree_add_item_hidden(tree, hf_tr_rif_bridge, ..., FALSE);
1294         }
1295
1296 The logical tree has these items:
1297
1298         hf_tr_rif_label, text="RIF: 001-A-013-9-C0F-B-555", value = NONE
1299         hf_tr_rif_ring,  hidden, value=0x001
1300         hf_tr_rif_bridge, hidden, value=0xA
1301         hf_tr_rif_ring,  hidden, value=0x013
1302         hf_tr_rif_bridge, hidden, value=0x9
1303         hf_tr_rif_ring,  hidden, value=0xC0F
1304         hf_tr_rif_bridge, hidden, value=0xB
1305         hf_tr_rif_ring,  hidden, value=0x555
1306
1307 GUI or print code will not display the hidden fields, but a display
1308 filter or "packet grep" routine will still see the values. The possible
1309 filter is then possible:
1310
1311         tr.rif_ring eq 0x013
1312
1313 proto_tree_add_protocol_format()
1314 ----------------------------
1315 proto_tree_add_protocol_format is used to add the top-level item for the
1316 protocol when the dissector routines wants complete control over how the
1317 field and value will be represented on the GUI tree.  The ID value for
1318 the protocol is passed in as the "id" argument; the rest of the
1319 arguments are a "printf"-style format and any arguments for that format. 
1320 The caller must include the name of the protocol in the format; it is
1321 not added automatically as in proto_tree_add_item().
1322
1323 proto_tree_add_none_format()
1324 ----------------------------
1325 proto_tree_add_none_format is used to add an item of type FT_NONE.
1326 The caller must include the name of the field in the format; it is
1327 not added automatically as in proto_tree_add_item().
1328
1329 proto_tree_add_bytes()
1330 proto_tree_add_time()
1331 proto_tree_add_ipxnet()
1332 proto_tree_add_ipv4()
1333 proto_tree_add_ipv6()
1334 proto_tree_add_ether()
1335 proto_tree_add_string()
1336 proto_tree_add_boolean()
1337 proto_tree_add_float()
1338 proto_tree_add_double()
1339 proto_tree_add_uint()
1340 proto_tree_add_int()
1341 ----------------------------
1342 These routines are used to add items to the protocol tree if either:
1343
1344         the value of the item to be added isn't just extracted from the
1345         packet data, but is computed from data in the packet;
1346
1347         the value was fetched into a variable.
1348
1349 The 'value' argument has the value to be added to the tree.
1350
1351 For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a
1352 sequence of bytes.
1353
1354 For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an
1355 "nstime_t", which is a structure containing the time to be added; it has
1356 'secs' and 'nsecs' members, giving the integral part and the fractional
1357 part of a time in units of seconds, with 'nsecs' being the number of
1358 nanoseconds.  For absolute times, "secs" is a UNIX-style seconds since
1359 January 1, 1970, 00:00:00 GMT value.
1360
1361 For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX
1362 network address.
1363
1364 For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4
1365 address, in network byte order.
1366
1367 For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a
1368 128-bit IPv6 address.
1369
1370 For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a
1371 48-bit MAC address.
1372
1373 For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a
1374 text string.
1375
1376 For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer;
1377 zero means "false", and non-zero means "true".
1378
1379 For proto_tree_add_float(), the 'value' argument is a 'float' in the
1380 host's floating-point format.
1381
1382 For proto_tree_add_double(), the 'value' argument is a 'double' in the
1383 host's floating-point format.
1384
1385 For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned
1386 integer value, in host byte order.  (This routine cannot be used to add
1387 64-bit integers; they can only be added with proto_tree_add_item().)
1388
1389 For proto_tree_add_int(), the 'value' argument is a 32-bit signed
1390 integer value, in host byte order.  (This routine cannot be used to add
1391 64-bit integers; they can only be added with proto_tree_add_item().)
1392
1393 proto_tree_add_bytes_hidden()
1394 proto_tree_add_time_hidden()
1395 proto_tree_add_ipxnet_hidden()
1396 proto_tree_add_ipv4_hidden()
1397 proto_tree_add_ipv6_hidden()
1398 proto_tree_add_ether_hidden()
1399 proto_tree_add_string_hidden()
1400 proto_tree_add_boolean_hidden()
1401 proto_tree_add_float_hidden()
1402 proto_tree_add_double_hidden()
1403 proto_tree_add_uint_hidden()
1404 proto_tree_add_int_hidden()
1405 ----------------------------
1406 These routines add fields and values to a tree, but don't show them in
1407 the GUI tree.  They are used for the same reason that
1408 proto_tree_add_item() is used.
1409
1410 proto_tree_add_bytes_format()
1411 proto_tree_add_time_format()
1412 proto_tree_add_ipxnet_format()
1413 proto_tree_add_ipv4_format()
1414 proto_tree_add_ipv6_format()
1415 proto_tree_add_ether_format()
1416 proto_tree_add_string_format()
1417 proto_tree_add_boolean_format()
1418 proto_tree_add_float_format()
1419 proto_tree_add_double_format()
1420 proto_tree_add_uint_format()
1421 proto_tree_add_int_format()
1422 ----------------------------
1423 These routines are used to add items to the protocol tree when the
1424 dissector routines wants complete control over how the field and value
1425 will be represented on the GUI tree.  The argument giving the value is
1426 the same as the corresponding proto_tree_add_XXX() function; the rest of
1427 the arguments are a "printf"-style format and any arguments for that
1428 format.  The caller must include the name of the field in the format; it
1429 is not added automatically as in the proto_tree_add_XXX() functions.
1430
1431 proto_tree_add_text()
1432 ---------------------
1433 proto_tree_add_text() is used to add a label to the GUI tree.  It will
1434 contain no value, so it is not searchable in the display filter process. 
1435 This function was needed in the transition from the old-style proto_tree
1436 to this new-style proto_tree so that Ethereal would still decode all
1437 protocols w/o being able to filter on all protocols and fields. 
1438 Otherwise we would have had to cripple Ethereal's functionality while we
1439 converted all the old-style proto_tree calls to the new-style proto_tree
1440 calls.
1441
1442 This can also be used for items with subtrees, which may not have values
1443 themselves - the items in the subtree are the ones with values.
1444
1445 For a subtree, the label on the subtree might reflect some of the items
1446 in the subtree.  This means the label can't be set until at least some
1447 of the items in the subtree have been dissected.  To do this, use
1448 'proto_item_set_text()' or 'proto_item_append_text()':
1449
1450         void
1451         proto_item_set_text(proto_item *ti, ...);
1452
1453         void
1454         proto_item_append_text(proto_item *ti, ...);
1455
1456 'proto_item_set_text()' takes as an argument the value returned by
1457 'proto_tree_add_text()', a 'printf'-style format string, and a set of
1458 arguments corresponding to '%' format items in that string, and replaces
1459 the text for the item created by 'proto_tree_add_text()' with the result
1460 of applying the arguments to the format string. 
1461
1462 'proto_item_append_text()' is similar, but it appends to the text for
1463 the item the result of applying the arguments to the format string.
1464
1465 For example, early in the dissection, one might do:
1466
1467         ti = proto_tree_add_text(tree, tvb, offset, length, <label>);
1468
1469 and later do
1470
1471         proto_item_set_text(ti, "%s: %s", type, value);
1472
1473 after the "type" and "value" fields have been extracted and dissected. 
1474 <label> would be a label giving what information about the subtree is
1475 available without dissecting any of the data in the subtree.
1476
1477 Note that an exception might thrown when trying to extract the values of
1478 the items used to set the label, if not all the bytes of the item are
1479 available.  Thus, one should create the item with text that is as
1480 meaningful as possible, and set it or append additional information to
1481 it as the values needed to supply that information is extracted.
1482
1483 proto_tree_add_text_valist()
1484 ---------------------
1485 This is like proto_tree_add_text(), but takes, as the last argument, a
1486 'va_list'; it is used to allow routines that take a printf-like
1487 variable-length list of arguments to add a text item to the protocol
1488 tree.
1489
1490 1.7 Utility routines
1491
1492 1.7.1 match_strval and val_to_str
1493
1494 A dissector may need to convert a value to a string, using a
1495 'value_string' structure, by hand, rather than by declaring a field with
1496 an associated 'value_string' structure; this might be used, for example,
1497 to generate a COL_INFO line for a frame.
1498
1499 'match_strval()' will do that:
1500
1501         gchar*
1502         match_strval(guint32 val, const value_string *vs)
1503
1504 It will look up the value 'val' in the 'value_string' table pointed to
1505 by 'vs', and return either the corresponding string, or NULL if the
1506 value could not be found in the table.
1507
1508 'val_to_str()' can be used to generate a string for values not found in
1509 the table:
1510
1511         gchar*
1512         val_to_str(guint32 val, const value_string *vs, const char *fmt)
1513
1514 If the value 'val' is found in the 'value_string' table pointed to by
1515 'vs', 'val_to_str' will return the corresponding string; otherwise, it
1516 will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
1517 to generate a string, and will return a pointer to that string. 
1518 (Currently, it has three 64-byte static buffers, and cycles through
1519 them; this permits the results of up to three calls to 'val_to_str' to
1520 be passed as arguments to a routine using those strings.)
1521
1522
1523 1.8 Calling Other Dissector 
1524
1525 NOTE: This is discussed in the README.tvbuff file.  For more 
1526 information on tvbuffers consult that file.
1527
1528 As each dissector completes its portion of the protocol analysis, it
1529 is expected to create a new tvbuff of type TVBUFF_SUBSET which
1530 contains the payload portion of the protocol (that is, the bytes
1531 that are relevant to the next dissector).
1532
1533 The syntax for creating a new TVBUFF_SUBSET is:
1534
1535 next_tvb = tvb_new_subset(tvb, offset, length, reported_length)
1536
1537 Where:
1538         tvb is the tvbuff that the dissector has been working on. It
1539         can be a tvbuff of any type.
1540
1541         next_tvb is the new TVBUFF_SUBSET.
1542
1543         offset is the byte offset of 'tvb' at which the new tvbuff
1544         should start.  The first byte is the 0th byte.
1545
1546         length is the number of bytes in the new TVBUFF_SUBSET. A length
1547         argument of -1 says to use as many bytes as are available in
1548         'tvb'.
1549
1550         reported_length is the number of bytes that the current protocol
1551         says should be in the payload. A reported_length of -1 says that
1552         the protocol doesn't say anything about the size of its payload.
1553
1554
1555 An example from packet-ipx.c -
1556
1557 void
1558 dissect_ipx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1559 {
1560         tvbuff_t        *next_tvb;
1561         int             reported_length, available_length;
1562
1563  
1564         /* Make the next tvbuff */
1565
1566 /* IPX does have a length value in the header, so calculate report_length */
1567    Set this to -1 if there isn't any length information in the protocol
1568 */
1569         reported_length = ipx_length - IPX_HEADER_LEN;
1570
1571 /* Calculate the available data in the packet, 
1572    set this to -1 to use all the data in the tv_buffer
1573 */
1574         available_length = tvb_length(tvb) - IPX_HEADER_LEN;
1575
1576 /* Create the tvbuffer for the next dissector */
1577         next_tvb = tvb_new_subset(tvb, IPX_HEADER_LEN,
1578                         MIN(available_length, reported_length),
1579                         reported_length);
1580
1581 /* call the next dissector */
1582         dissector_next( next_tvb, pinfo, tree);
1583
1584
1585 1.9 Editing Makefile.am and Makefile.nmake to add your dissector.
1586
1587 To arrange that your dissector will be built as part of Ethereal, you
1588 must add the name of the source file for your dissector, and the header
1589 file that declares your main dissector routine, to the
1590 'DISSECTOR_SOURCES' macro in the 'Makefile.am' and 'Makefile.nmake'
1591 files in the top-level directory.  (Note that this is for modern
1592 versions of UNIX, so there is no 14-character limitation on file names,
1593 and for modern versions of Windows, so there is no 8.3-character
1594 limitation on file names.)
1595
1596 If your dissector also has its own header files, you must add them to
1597 the 'noinst_HEADERS' macro in the 'Makefile.am' file in the top-level
1598 directory, so that it's included when release source tarballs are built
1599 (otherwise, the source in the release tarballs won't compile).
1600
1601 Please remember to update both files; it may not be necessary to do so
1602 in order for you to build Ethereal on your machine, but both changes
1603 will need to be checked in to the Ethereal source code, to allow it to
1604 build on all platforms.
1605
1606 1.10 Using the CVS source code tree.
1607
1608 1.11 Submitting code for your new dissector.
1609
1610 2. Advanced dissector topics.
1611
1612 2.1 ?? 
1613
1614 2.2 Following "conversations".
1615
1616 In ethereal a conversation is defined as a series of data packet between two
1617 address:port combinations.  A conversation is not sensitive to the direction of
1618 the packet.  The same conversation will be returned for a packet bound from
1619 ServerA:1000 to ClientA:2000 and the packet from ClientA:2000 to ServerA:1000.
1620
1621 There are five routines that you will use to work with a conversation:
1622 conversation_new, find_conversation, conversation_add_proto_data,
1623 conversation_get_proto_data, and conversation_delete_proto_data.
1624
1625
1626 2.2.1 The conversation_init function.
1627
1628 This is an internal routine for the conversation code.  As such the you
1629 will not have to call this routine.  Just be aware that this routine is
1630 called at the start of each capture and before the packets are filtered
1631 with a display filter.  The routine will destroy all stored
1632 conversations.  This routine does NOT clean up any data pointers that are
1633 passed in the conversation_new 'data' variable.  You are responsible for
1634 this clean up if you pass a malloc'ed pointer in this variable.
1635
1636 See item 2.2.7 for more information about the 'data' pointer.
1637
1638
1639 2.2.2 The conversation_new function.
1640
1641 This routine will create a new conversation based upon two address/port
1642 pairs.  If you want to associate with the conversation a pointer to a
1643 private data structure you must use the conversation_add_proto_data
1644 function.  The ptype variable is used to differentiate between
1645 conversations over different protocols, i.e. TCP and UDP.  The options
1646 variable is used to define a conversation that will accept any destination
1647 address and/or port.  Set options = 0 if the destination port and address
1648 are know when conversation_new is called.  See section 2.4 for more
1649 information on usage of the options parameter.
1650
1651 The conversation_new prototype:
1652         conversation_t *conversation_new(address *addr1, address *addr2,
1653             port_type ptype, guint32 port1, guint32 port2, guint options);
1654
1655 Where:
1656         address* addr1  = first data packet address
1657         address* addr2  = second data packet address
1658         port_type ptype = port type, this is defined in packet.h
1659         guint32 port1   = first data packet port
1660         guint32 port2   = second data packet port
1661         guint options   = conversation options, NO_ADDR2 and/or NO_PORT2
1662
1663 "addr1" and "port1" are the first address/port pair; "addr2" and "port2"
1664 are the second address/port pair.  A conversation doesn't have source
1665 and destination address/port pairs - packets in a conversation go in
1666 both directions - so "addr1"/"port1" may be the source or destination
1667 address/port pair; "addr2"/"port2" would be the other pair.
1668
1669 If NO_ADDR2 is specified, the conversation is set up so that a
1670 conversation lookup will match only the "addr1" address; if NO_PORT2 is
1671 specified, the conversation is set up so that a conversation lookup will
1672 match only the "port1" port; if both are specified, i.e.
1673 NO_ADDR2|NO_PORT2, the conversation is set up so that the lookup will
1674 match only the "addr1"/"port1" address/port pair.  This can be used if a
1675 packet indicates that, later in the capture, a conversation will be
1676 created using certain addresses and ports, in the case where the packet
1677 doesn't specify the addresses and ports of both sides.
1678
1679 2.2.3 The find_conversation function.
1680
1681 Call this routine to look up a conversation.  If no conversation is found,
1682 the routine will return a NULL value.
1683
1684 The find_conversation prototype:
1685
1686         conversation_t *find_conversation(address *addr_a, address *addr_b,
1687             port_type ptype, guint32 port_a, guint32 port_b, guint options);
1688
1689 Where:
1690         address* addr_a = first address
1691         address* addr_b = second address
1692         port_type ptype = port type
1693         guint32 port_a  = first data packet port
1694         guint32 port_b  = second data packet port
1695         guint options   = conversation options, NO_ADDR_B and/or NO_PORT_B
1696
1697 "addr_a" and "port_a" are the first address/port pair; "addr_b" and
1698 "port_b" are the second address/port pair.  Again, as a conversation
1699 doesn't have source and destination address/port pairs, so
1700 "addr_a"/"port_a" may be the source or destination address/port pair;
1701 "addr_b"/"port_b" would be the other pair.  The search will match the
1702 "a" address/port pair against both the "1" and "2" address/port pairs,
1703 and match the "b" address/port pair against both the "2" and "1"
1704 address/port pairs; you don't have to worry about which side the "a" or
1705 "b" pairs correspond to.
1706
1707 If the NO_ADDR_B flag was specified to "find_conversation()", the
1708 "addr_b" address will be treated as matching any "wildcarded" address;
1709 if the NO_PORT_B flag was specified, the "port_b" port will be treated
1710 as matching any "wildcarded" port.  If both flags are specified, i.e. 
1711 NO_ADDR_B|NO_PORT_B, the "addr_b" address will be treated as matching
1712 any "wildcarded" address and the "port_b" port will be treated as
1713 matching any "wildcarded" port.
1714
1715
1716 2.2.4 The conversation_add_proto_data function.
1717
1718 Once you have created a conversation with conversation_new, you can
1719 associate data with it using this function.
1720
1721 The conversation_add_proto_data prototype:
1722
1723         void conversation_add_proto_data(conversation_t *conv, int proto,
1724             void *proto_data);
1725
1726 Where:
1727         conversation_t *conv = the conversation in question
1728         int proto            = registered protocol number
1729         void *data           = dissector data structure
1730
1731 "conversation" is the value returned by conversation_new.  "proto" is a
1732 unique protocol number created with proto_register_protocol.  Protocols
1733 are typically registered in the proto_register_XXXX section of your
1734 dissector.  "data" is a pointer to the data you wish to associate with the
1735 conversation.  Using the protocol number allows several dissectors to
1736 associate data with a given conversation.
1737
1738
1739 2.2.5 The conversation_get_proto_data function.
1740
1741 After you have located a conversation with find_conversation, you can use
1742 this function to retrieve any data associated with it.
1743
1744 The conversation_get_proto_data prototype:
1745
1746         void *conversation_get_proto_data(conversation_t *conv, int proto);
1747
1748 Where:
1749         conversation_t *conv = the conversation in question
1750         int proto            = registered protocol number
1751         
1752 "conversation" is the conversation created with conversation_new.  "proto"
1753 is a unique protocol number acreated with proto_register_protocol,
1754 typically in the proto_register_XXXX portion of a dissector.  The function
1755 returns a pointer to the data requested, or NULL if no data was found.
1756
1757
1758 2.2.6 The conversation_delete_proto_data function.
1759
1760 After you are finished with a conversation, you can remove your assocation
1761 with this function.  Please note that ONLY the conversation entry is
1762 removed.  If you have allocated any memory for your data, you must free it
1763 as well.
1764
1765 The conversation_delete_proto_data prototype:
1766
1767         void conversation_delete_proto_data(conversation_t *conv, int proto);
1768         
1769 Where:
1770         conversation_t *conv = the conversation in question
1771         int proto            = registered protocol number
1772
1773 "conversation" is the conversation created with conversation_new.  "proto"
1774 is a unique protocol number acreated with proto_register_protocol,
1775 typically in the proto_register_XXXX portion of a dissector.
1776
1777
1778 2.2.7 The example conversation code with GMemChunk's
1779
1780 For a conversation between two IP addresses and ports you can use this as an
1781 example.  This example uses the GMemChunk to allocate memory and stores the data
1782 pointer in the conversation 'data' variable.
1783
1784 NOTE: Remember to register the init routine (my_dissector_init) in the
1785 protocol_register routine.
1786
1787
1788 /************************ Globals values ************************/
1789
1790 /* the number of entries in the memory chunk array */
1791 #define my_init_count 10
1792
1793 /* define your structure here */
1794 typedef struct {
1795
1796 }my_entry_t;
1797
1798 /* the GMemChunk base structure */
1799 static GMemChunk *my_vals = NULL;
1800
1801 /* Registered protocol number
1802 static int my_proto = -1;
1803
1804
1805 /********************* in the dissector routine *********************/
1806
1807 /* the local variables in the dissector */
1808
1809 conversation_t *conversation;
1810 my_entry_t *data_ptr
1811
1812
1813 /* look up the conversation */
1814
1815 conversation = find_conversation( &pinfo->src, &pinfo->dst, pinfo->ptype,
1816         pinfo->srcport, pinfo->destport, 0);
1817
1818 /* if conversation found get the data pointer that you stored */
1819 if ( conversation)
1820     data_ptr = (my_entry_t*)conversation_get_proto_data(conversation,
1821             my_proto);
1822 else {
1823
1824     /* new conversation create local data structure */
1825
1826     data_ptr = g_mem_chunk_alloc(my_protocol_vals);
1827
1828     /*** add your code here to setup the new data structure ***/
1829
1830     /* create the conversation with your data pointer  */
1831
1832     conversation_new( &pinfo->src, &pinfo->dst, pinfo->ptype,
1833             pinfo->srcport, pinfo->destport, 0);
1834     conversation_add_proto_data(conversation, my_proto, (void *) data_ptr);
1835 }
1836
1837 /* at this point the conversation data is ready */
1838
1839
1840 /******************* in the dissector init routine *******************/
1841
1842 #define my_init_count 20
1843
1844 static void
1845 my_dissector_init( void){
1846
1847     /* destroy memory chunks if needed */
1848
1849     if ( my_vals)
1850         g_mem_chunk_destroy(my_vals);
1851
1852     /* now create memory chunks */
1853
1854     my_vals = g_mem_chunk_new( "my_proto_vals",
1855             sizeof( _entry_t),
1856             my_init_count * sizeof( my_entry_t),
1857             G_ALLOC_AND_FREE);
1858 }
1859
1860 /***************** in the protocol register routine *****************/
1861
1862 /* register re-init routine */
1863
1864 register_init_routine( &my_dissector_init);
1865
1866 my_proto = proto_register_protocol("My Protocol", "My Protocol", "my_proto");
1867
1868
1869 2.2.8 The example conversation code using conversation index field
1870
1871 Sometimes the conversation isn't enough to define a unique data storage
1872 value for the network traffic.  For example if you are storing information
1873 about requests carried in a conversation, the request may have an
1874 identifier that is used to  define the request. In this case the
1875 conversation and the identifier are required to find the data storage
1876 pointer.  You can use the conversation data structure index value to
1877 uniquely define the conversation.  
1878
1879 See packet-afs.c for an example of how to use the conversation index.  In
1880 this dissector multiple requests are sent in the same conversation.  To store
1881 information for each request the dissector has an internal hash table based
1882 upon the conversation index and values inside the request packets. 
1883
1884
1885 /* in the dissector routine */
1886
1887 /* to find a request value, first lookup conversation to get index */
1888 /* then used the conversation index, and request data to find data */
1889 /* in the local hash table */
1890
1891         conversation = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
1892             pinfo->srcport, pinfo->destport, 0);
1893         if (conversation == NULL) {
1894                 /* It's not part of any conversation - create a new one. */
1895                 conversation = conversation_new(&pinfo->src, &pinfo->dst, pinfo->ptype,
1896                     pinfo->srcport, pinfo->destport, NULL, 0);
1897         }
1898
1899         request_key.conversation = conversation->index; 
1900         request_key.service = pntohs(&rxh->serviceId);
1901         request_key.callnumber = pntohl(&rxh->callNumber);
1902
1903         request_val = (struct afs_request_val *) g_hash_table_lookup(
1904                 afs_request_hash, &request_key);
1905
1906         /* only allocate a new hash element when it's a request */
1907         opcode = 0;
1908         if ( !request_val && !reply)
1909         {
1910                 new_request_key = g_mem_chunk_alloc(afs_request_keys);
1911                 *new_request_key = request_key;
1912
1913                 request_val = g_mem_chunk_alloc(afs_request_vals);
1914                 request_val -> opcode = pntohl(&afsh->opcode);
1915                 opcode = request_val->opcode;
1916
1917                 g_hash_table_insert(afs_request_hash, new_request_key,
1918                         request_val);
1919         }
1920
1921
1922
1923 2.3 Dynamic conversation dissector registration
1924
1925
1926 NOTE: This sections assumes that all information is available to
1927         create a complete conversation, source port/address and
1928         destination port/address.  If either the destination port or
1929         address is know, see section 2.4 Dynamic server port dissector
1930         registration.
1931
1932 For protocols that negotiate a secondary port connection, for example
1933 packet-msproxy.c, a conversation can install a dissector to handle 
1934 the secondary protocol dissection.  After the conversation is created
1935 for the negotiated ports use the conversation_set_dissector to define
1936 the dissection routine.
1937
1938 The second argument to conversation_set_dissector is a dissector handle,
1939 which is created with a call to create_dissector_handle or
1940 register_dissector.
1941
1942 create_dissector_handle takes as arguments a pointer to the dissector
1943 function and a protocol ID as returned by proto_register_protocol;
1944 register_dissector takes as arguments a string giving a name for the
1945 dissector, a pointer to the dissector function, and a protocol ID.
1946
1947 The protocol ID is the ID for the protocol dissected by the function. 
1948 The function will not be called if the protocol has been disabled by the
1949 user; instead, the data for the protocol will be dissected as raw data.
1950
1951 An example -
1952
1953 /* the handle for the dynamic dissector *
1954 static dissector_handle_t sub_dissector_handle;
1955
1956 /* prototype for the dynamic dissector */
1957 static void sub_dissector( tvbuff_t *tvb, packet_info *pinfo,
1958                 proto_tree *tree);
1959
1960 /* in the main protocol dissector, where the next dissector is setup */
1961
1962 /* if conversation has a data field, create it and load structure */
1963
1964         new_conv_info = g_mem_chunk_alloc( new_conv_vals);
1965         new_conv_info->data1 = value1;
1966
1967 /* create the conversation for the dynamic port */
1968         conversation = conversation_new( &pinfo->src, &pinfo->dst, protocol,
1969                 src_port, dst_port, new_conv_info, 0);
1970
1971 /* set the dissector for the new conversation */
1972         conversation_set_dissector(conversation, sub_dissector_handle);
1973
1974                 ...
1975
1976 void
1977 proto_register_PROTOABBREV(void)
1978 {                 
1979         ...
1980
1981         sub_dissector_handle = create_dissector_handle(sub_dissector,
1982             proto);
1983
1984         ...
1985 }
1986
1987 2.4 Dynamic server port dissector registration
1988
1989 NOTE: While this example used both NO_ADDR2 and NO_PORT2 to create a
1990 conversation with only one port and address set, this isn't a
1991 requirement.  Either the second port or the second address can be set
1992 when the conversation is created.
1993
1994 For protocols that define a server address and port for a secondary
1995 protocol, a conversation can be used to link a protocol dissector to
1996 the server port and address.  The key is to create the new 
1997 conversation with the second address and port set to the "accept
1998 any" values.  
1999
2000 There are two support routines that will allow the second port and/or
2001 address to be set latter.  
2002
2003 conversation_set_port2( conversation_t *conv, guint32 port);
2004 conversation_set_addr2( conversation_t *conv, address addr);
2005
2006 These routines will change the second address or port for the
2007 conversation.  So, the server port conversation will be converted into a
2008 more complete conversation definition.  Don't use these routines if you
2009 want create a conversation between the server and client and retain the
2010 server port definition, you must create a new conversation.
2011
2012
2013 An example -
2014
2015 /* the handle for the dynamic dissector *
2016 static dissector_handle_t sub_dissector_handle;
2017
2018         ...
2019
2020 /* in the main protocol dissector, where the next dissector is setup */
2021
2022 /* if conversation has a data field, create it and load structure */
2023
2024         new_conv_info = g_mem_chunk_alloc( new_conv_vals);
2025         new_conv_info->data1 = value1;
2026
2027 /* create the conversation for the dynamic server address and port      */
2028 /* NOTE: The second address and port values don't matter because the    */
2029 /* NO_ADDR2 and NO_PORT2 options are set.                               */
2030
2031         conversation = conversation_new( &server_src_addr, 0, protocol,
2032                 server_src_port, 0, new_conv_info, NO_ADDR2 | NO_PORT2);
2033
2034 /* set the dissector for the new conversation */
2035         conversation_set_dissector(conversation, sub_dissector_handle);
2036
2037
2038 2.5 Per packet information
2039
2040 Information can be stored for each data packet that is process by the dissector.
2041 The information is added with the p_add_proto_data function and retreived with the 
2042 p_get_proto_data function.  The data pointers passed into the p_add_proto_data are
2043 not managed by the proto_data routines. If you use malloc or any other dynamic 
2044 memory allocation scheme, you must release the data when it isn't required.
2045
2046 void
2047 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
2048 void *
2049 p_get_proto_data(frame_data *fd, int proto)
2050
2051 Where: 
2052         fd         - The fd pointer in the pinfo structure, pinfo->fd
2053         proto      - Protocol id returned by the proto_register_protocol call during initialization
2054         proto_data - pointer to the dissector data.
2055
2056
2057 2.6 User Preferences
2058
2059 If the dissector has user options, there is support for adding these preferences
2060 to a configuration dialog.
2061
2062 You must register the module with the preferences routine with -
2063
2064 module_t *prefs_register_protocol(proto_id, void (*apply_cb)(void))
2065
2066 Where: proto_id   - the value returned by "proto_register_protocol()" when
2067                     the protocol was registered
2068          apply_cb - Callback routine that is call when preferences are applied
2069
2070
2071 Then you can register the fields that can be configured by the user with these routines -
2072
2073         /* Register a preference with an unsigned integral value. */
2074         void prefs_register_uint_preference(module_t *module, const char *name,
2075             const char *title, const char *description, guint base, guint *var);
2076
2077         /* Register a preference with an Boolean value. */
2078         void prefs_register_bool_preference(module_t *module, const char *name,
2079             const char *title, const char *description, gboolean *var);
2080
2081         /* Register a preference with an enumerated value. */
2082         void prefs_register_enum_preference(module_t *module, const char *name,
2083             const char *title, const char *description, gint *var,
2084             const enum_val *enumvals, gboolean radio_buttons)
2085
2086         /* Register a preference with a character-string value. */
2087         void prefs_register_string_preference(module_t *module, const char *name,
2088             const char *title, const char *description, char **var)
2089
2090 Where: module - Returned by the prefs_register_protocol routine
2091          name   - This is appended to the name of the protocol, with a
2092                         "." between them, to construct a name that
2093                         identifies the field in the preference file;
2094                         the name itself should not include the protocol
2095                         name, as the name in the preference file will
2096                         already have it
2097          title  - Field title in the preferences dialog
2098          description - Comments added to the preference file above the 
2099                         preference value
2100          var      - pointer to the storage location that is updated when the
2101                     field is changed in the preference dialog box
2102          enumvals - an array of enum_val structures.  This must be NULL terminated
2103          radio_buttons - Is the enumvals a list of radio buttons?
2104
2105
2106 An example from packet-beep.c -
2107         
2108   proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
2109                                        "BEEP", "beep");
2110
2111         ...
2112
2113   /* Register our configuration options for BEEP, particularly our port */
2114
2115   beep_module = prefs_register_protocol(proto_beep, proto_reg_handoff_beep);
2116
2117   prefs_register_uint_preference(beep_module, "tcp.port", "BEEP TCP Port",
2118                                  "Set the port for BEEP messages (if other"
2119                                  " than the default of 10288)",
2120                                  10, &global_beep_tcp_port);
2121
2122   prefs_register_bool_preference(beep_module, "strict_header_terminator", 
2123                                  "BEEP Header Requires CRLF", 
2124                                  "Specifies that BEEP requires CRLF as a "
2125                                  "terminator, and not just CR or LF",
2126                                  &global_beep_strict_term);
2127
2128 This will create preferences "beep.tcp.port" and
2129 "beep.strict_header_terminator", the first of which is an unsigned
2130 integer and the second of which is a Boolean.
2131
2132 3. Plugins
2133
2134 See the README.plugins for more information on how to "pluginize" 
2135 a dissector.
2136
2137 4.0 Extending Wiretap.
2138
2139 5.0 Adding new capabilities.
2140
2141
2142
2143
2144 James Coe <jammer@cin.net>
2145 Gilbert Ramirez <gram@alumni.rice.edu>
2146 Jeff Foster <jfoste@woodward.com>
2147 Olivier Abad <oabad@cybercable.fr>
2148 Laurent Deniel <laurent.deniel@free.fr>
2149 Gerald Combs <gerald@ethereal.com>