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