Tabsize: 4 This file is a HOWTO for Wireshark developers interested in writing or working on Wireshark protocol dissectors. It describes expected code patterns and the use of some of the important functions and variables. This file is compiled to give in depth information on Wireshark. It is by no means all inclusive and complete. Please feel free to send remarks and patches to the developer mailing list. If you haven't read README.developer, read that first! 0. Prerequisites. Before starting to develop a new dissector, a "running" Wireshark build environment is required - there's no such thing as a standalone "dissector build toolkit". How to setup such an environment is platform dependent; detailed information about these steps can be found in the "Developer's Guide" (available from: https://www.wireshark.org) and in the INSTALL and README files of the sources root dir. 0.1. Dissector related README files. You'll find additional dissector related information in the following README files: - README.heuristic - what are heuristic dissectors and how to write them - README.plugins - how to "pluginize" a dissector - README.request_response_tracking - how to track req./resp. times and such - README.wmem - how to obtain "memory leak free" memory 0.2 Contributors James Coe Gilbert Ramirez Jeff Foster Olivier Abad Laurent Deniel Gerald Combs Guy Harris Ulf Lamping Barbu Paul - Gheorghe 1. Setting up your protocol dissector code. This section provides skeleton code for a protocol dissector. It also explains the basic functions needed to enter values in the traffic summary columns, add to the protocol tree, and work with registered header fields. 1.1 Skeleton code. Wireshark requires certain things when setting up a protocol dissector. We provide basic skeleton code for a dissector that you can copy to a new file and fill in. Your dissector should follow the naming convention of "packet-" followed by the abbreviated name for the protocol. It is recommended that where possible you keep to the IANA abbreviated name for the protocol, if there is one, or a commonly-used abbreviation for the protocol, if any. The skeleton code lives in the file "packet-PROTOABBREV.c" in the same source directory as this README. If instead of using the skeleton you base your dissector on an existing real dissector, please put a little note in the copyright header indicating which dissector you started with. Usually, you will put your newly created dissector file into the directory epan/dissectors/, just like all the other packet-*.c files already in there. Also, please add your dissector file to the corresponding makefiles, described in section "1.8 Editing Makefile.common and CMakeLists.txt to add your dissector" below. Dissectors that use the dissector registration API to register with a lower level protocol (this is the vast majority) don't need to define a prototype in their .h file. For other dissectors the main dissector routine should have a prototype in a header file whose name is "packet-", followed by the abbreviated name for the protocol, followed by ".h"; any dissector file that calls your dissector should be changed to include that file. You may not need to include all the headers listed in the skeleton, and you may need to include additional headers. 1.2 Explanation of needed substitutions in code skeleton. In the skeleton sample code the following strings should be substituted with your information. YOUR_NAME Your name, of course. You do want credit, don't you? It's the only payment you will receive.... YOUR_EMAIL_ADDRESS Keep those cards and letters coming. PROTONAME The name of the protocol; this is displayed in the top-level protocol tree item for that protocol. PROTOSHORTNAME An abbreviated name for the protocol; this is displayed in the "Preferences" dialog box if your dissector has any preferences, in the dialog box of enabled protocols, and in the dialog box for filter fields when constructing a filter expression. PROTOABBREV A name for the protocol for use in filter expressions; it may contain only lower-case letters, digits, and hyphens, underscores, and periods. FIELDNAME The displayed name for the header field. FIELDABBREV The abbreviated name for the header field; it may contain only letters, digits, hyphens, underscores, and periods. FIELDTYPE FT_NONE, FT_BOOLEAN, FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64, FT_INT8, FT_INT16, FT_INT24, FT_INT32, FT_INT40, FT_INT48, FT_INT56, FT_INT64, FT_FLOAT, FT_DOUBLE, FT_ABSOLUTE_TIME, FT_RELATIVE_TIME, FT_STRING, FT_STRINGZ, FT_EUI64, FT_UINT_STRING, FT_ETHER, FT_BYTES, FT_UINT_BYTES, FT_IPv4, FT_IPv6, FT_IPXNET, FT_FRAMENUM, FT_PROTOCOL, FT_GUID, FT_OID, FT_REL_OID, FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FC, FT_FCWWN FIELDDISPLAY --For FT_UINT{8,16,24,32,40,48,56,64} and FT_INT{8,16,24,32,40,48,56,64): BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC, or BASE_CUSTOM, possibly ORed with BASE_RANGE_STRING, BASE_EXT_STRING or BASE_VAL64_STRING --For FT_UINT16: BASE_PT_UDP, BASE_PT_TCP, BASE_PT_DCCP or BASE_PT_SCTP --For FT_ABSOLUTE_TIME: ABSOLUTE_TIME_LOCAL, ABSOLUTE_TIME_UTC, or ABSOLUTE_TIME_DOY_UTC --For FT_BOOLEAN: if BITMASK is non-zero: Number of bits in the field containing the FT_BOOLEAN bitfield. otherwise: (must be) BASE_NONE --For FT_STRING, FT_STRINGZ and FT_UINT_STRING: STR_ASCII or STR_UNICODE --For FT_BYTES: SEP_DOT, SEP_DASH, SEP_COLON, or SEP_SPACE to provide a separator between bytes. BASE_NONE has no separator between bytes BASE_ALLOW_ZERO displays instead of for zero sized byte array --For FT_IPv4: BASE_NETMASK - Used for IPv4 address that should never attempted to be resolved (like netmasks) otherwise: (must be) BASE_NONE --For all other types: BASE_NONE FIELDCONVERT VALS(x), VALS64(x), RVALS(x), TFS(x), CF_FUNC(x), NULL BITMASK Used to mask a field not 8-bit aligned or with a size other than a multiple of 8 bits FIELDDESCR A brief description of the field, or NULL. [Please do not use ""]. If, for example, PROTONAME is "Internet Bogosity Discovery Protocol", PROTOSHORTNAME would be "IBDP", and PROTOABBREV would be "ibdp". Try to conform with IANA names. 1.3 The dissector and the data it receives. 1.3.1 Header file. This is only needed if the dissector doesn't use self-registration to register itself with the lower level dissector, or if the protocol dissector wants/needs to expose code to other subdissectors. The dissector must be declared exactly as follows in the file packet-PROTOABBREV.h: int dissect_PROTOABBREV(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree); 1.3.2 Extracting data from packets. NOTE: See the file /epan/tvbuff.h for more details. The "tvb" argument to a dissector points to a buffer containing the raw data to be analyzed by the dissector; for example, for a protocol running atop UDP, it contains the UDP payload (but not the UDP header, or any protocol headers above it). A tvbuffer is an opaque data structure, the internal data structures are hidden and the data must be accessed via the tvbuffer accessors. The accessors are: Bit accessors for a maximum of 8-bits, 16-bits 32-bits and 64-bits: guint8 tvb_get_bits8(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits); guint16 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding); guint32 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding); guint64 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding); Single-byte accessor: guint8 tvb_get_guint8(tvbuff_t *tvb, const gint offset); Network-to-host-order accessors for 16-bit integers (guint16), 24-bit integers, 32-bit integers (guint32), 40-bit integers, 48-bit integers, 56-bit integers and 64-bit integers (guint64): guint16 tvb_get_ntohs(tvbuff_t *tvb, const gint offset); guint32 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset); guint32 tvb_get_ntohl(tvbuff_t *tvb, const gint offset); guint64 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset); guint64 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset); guint64 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset); guint64 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset); Network-to-host-order accessors for single-precision and double-precision IEEE floating-point numbers: gfloat tvb_get_ntohieee_float(tvbuff_t *tvb, const gint offset); gdouble tvb_get_ntohieee_double(tvbuff_t *tvb, const gint offset); Little-Endian-to-host-order accessors for 16-bit integers (guint16), 24-bit integers, 32-bit integers (guint32), 40-bit integers, 48-bit integers, 56-bit integers, and 64-bit integers (guint64): guint16 tvb_get_letohs(tvbuff_t *tvb, const gint offset); guint32 tvb_get_letoh24(tvbuff_t *tvb, const gint offset); guint32 tvb_get_letohl(tvbuff_t *tvb, const gint offset); guint64 tvb_get_letoh40(tvbuff_t *tvb, const gint offset); guint64 tvb_get_letoh48(tvbuff_t *tvb, const gint offset); guint64 tvb_get_letoh56(tvbuff_t *tvb, const gint offset); guint64 tvb_get_letoh64(tvbuff_t *tvb, const gint offset); Little-Endian-to-host-order accessors for single-precision and double-precision IEEE floating-point numbers: gfloat tvb_get_letohieee_float(tvbuff_t *tvb, const gint offset); gdouble tvb_get_letohieee_double(tvbuff_t *tvb, const gint offset); Encoding-to_host-order accessors for 16-bit integers (guint16), 24-bit integers, 32-bit integers (guint32), 40-bit integers, 48-bit integers, 56-bit integers, and 64-bit integers (guint64): guint16 tvb_get_guint16(tvbuff_t *tvb, const gint offset, const guint encoding); guint32 tvb_get_guint24(tvbuff_t *tvb, const gint offset, const guint encoding); guint32 tvb_get_guint32(tvbuff_t *tvb, const gint offset, const guint encoding); guint64 tvb_get_guint40(tvbuff_t *tvb, const gint offset, const guint encoding); gint64 tvb_get_gint40(tvbuff_t *tvb, const gint offset, const guint encoding); guint64 tvb_get_guint48(tvbuff_t *tvb, const gint offset, const guint encoding); gint64 tvb_get_gint48(tvbuff_t *tvb, const gint offset, const guint encoding); guint64 tvb_get_guint56(tvbuff_t *tvb, const gint offset, const guint encoding); gint64 tvb_get_gint56(tvbuff_t *tvb, const gint offset, const guint encoding); guint64 tvb_get_guint64(tvbuff_t *tvb, const gint offset, const guint encoding); "encoding" should be ENC_BIG_ENDIAN for Network-to-host-order and ENC_LITTLE_ENDIAN for Little-Endian-to-host-order. Endian-to-host-order accessors for single-precision and double-precision IEEE floating-point numbers: gfloat tvb_get_ieee_float(tvbuff_t *tvb, const gint offset, const guint encoding); gdouble tvb_get_ieee_double(tvbuff_t *tvb, const gint offset, const guint encoding); Just as above, "encoding" should be ENC_BIG_ENDIAN for Network-to-host-order and ENC_LITTLE_ENDIAN for Little-Endian-to-host-order. Accessors for IPv4 and IPv6 addresses: guint32 tvb_get_ipv4(tvbuff_t *tvb, const gint offset); void tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr); NOTE: IPv4 addresses are not to be converted to host byte order before being passed to "proto_tree_add_ipv4()". You should use "tvb_get_ipv4()" to fetch them, not "tvb_get_ntohl()" *OR* "tvb_get_letohl()" - don't, for example, try to use "tvb_get_ntohl()", find that it gives you the wrong answer on the PC on which you're doing development, and try "tvb_get_letohl()" instead, as "tvb_get_letohl()" will give the wrong answer on big-endian machines. gchar *tvb_ip_to_str(tvbuff_t *tvb, const gint offset) gchar *tvb_ip6_to_str(tvbuff_t *tvb, const gint offset) Returns a null-terminated buffer containing a string with IPv4 or IPv6 Address from the specified tvbuff, starting at the specified offset. Accessors for GUID: void tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid); void tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid); void tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation); String accessors: guint8 *tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length, const guint encoding); Returns a null-terminated buffer allocated from the specified scope, containing data from the specified tvbuff, starting at the specified offset, and containing the specified length worth of characters. Reads data in the specified encoding and produces UTF-8 in the buffer. See below for a list of input encoding values. The buffer is allocated in the given wmem scope (see README.wmem for more information). guint8 *tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding); Returns a null-terminated buffer allocated from the specified scope, containing data from the specified tvbuff, starting at the specified offset, and containing all characters from the tvbuff up to and including a terminating null character in the tvbuff. Reads data in the specified encoding and produces UTF-8 in the buffer. See below for a list of input encoding values. "*lengthp" will be set to the length of the string, including the terminating null. The buffer is allocated in the given wmem scope (see README.wmem for more information). const guint8 *tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp); Returns a null-terminated const buffer containing data from the specified tvbuff, starting at the specified offset, and containing all bytes from the tvbuff up to and including a terminating null character in the tvbuff. "*lengthp" will be set to the length of the string, including the terminating null. You do not need to free() this buffer; it will happen automatically once the next packet is dissected. This function is slightly more efficient than the others because it does not allocate memory and copy the string, but it does not do any mapping to UTF-8 or checks for valid octet sequences. gint tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer); gint tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer); Returns a null-terminated buffer containing data from the specified tvbuff, starting at the specified offset, and containing all characters from the tvbuff up to and including a terminating null character in the tvbuff. "*lengthp" will be set to the length of the string, including the terminating null. gchar *tvb_get_ts_23_038_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint bit_offset, gint no_of_chars); tvb_get_ts_23_038_7bits_string() returns a string of a given number of characters and encoded according to 3GPP TS 23.038 7 bits alphabet. The buffer is allocated in the given wmem scope (see README.wmem for more information). Byte Array Accessors: gchar *tvb_bytes_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint len); Formats a bunch of data from a tvbuff as bytes, returning a pointer to the string with the data formatted as two hex digits for each byte. The string pointed to is stored in an "wmem_alloc'd" buffer which will be freed depending on its scope (typically wmem_packet_scope which is freed after the frame). The formatted string will contain the hex digits for at most the first 16 bytes of the data. If len is greater than 16 bytes, a trailing "..." will be added to the string. gchar *tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint len, gchar punct); This function is similar to tvb_bytes_to_str(...) except that 'punct' is inserted between the hex representation of each byte. GByteArray *tvb_get_string_bytes(tvbuff_t *tvb, const gint offset, const gint length, const guint encoding, GByteArray* bytes, gint *endoff) Given a tvbuff, an offset into the tvbuff, and a length that starts at that offset (which may be -1 for "all the way to the end of the tvbuff"), fetch the hex-decoded byte values of the tvbuff into the passed-in 'bytes' array, based on the passed-in encoding. In other words, convert from a hex-ascii string in tvbuff, into the supplied GByteArray. gchar *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first); Given a tvbuff, an offset into the tvbuff, and a length that starts at that offset (which may be -1 for "all the way to the end of the tvbuff"), fetch BCD encoded digits from a tvbuff starting from either the low or high half byte, formatting the digits according to an input digit set, if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used. A pointer to the packet scope allocated string will be returned. Note: a tvbuff content of 0xf is considered a 'filler' and will end the conversion. Copying memory: guint8* tvb_memcpy(tvbuff_t *tvb, guint8* target, gint offset, gint length); Copies into the specified target the specified length's worth of data from the specified tvbuff, starting at the specified offset. guint8* tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length); Returns a buffer containing a copy of the given TVB bytes. The buffer is allocated in the given wmem scope (see README.wmem for more information). Pointer-retrieval: /* WARNING! Don't use this function. There is almost always a better way. * It's dangerous because once this pointer is given to the user, there's * no guarantee that the user will honor the 'length' and not overstep the * boundaries of the buffer. Also see the warning in the Portability section. */ guint8* tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length); Length query: Get amount of captured data in the buffer (which is *NOT* necessarily the length of the packet). You probably want tvb_reported_length instead: guint tvb_captured_length(const tvbuff_t *tvb); Get reported length of buffer: guint tvb_reported_length(const tvbuff_t *tvb); 1.4 Functions to handle columns in the traffic summary window. The topmost pane of the main window is a list of the packets in the capture, possibly filtered by a display filter. Each line corresponds to a packet, and has one or more columns, as configured by the user. Many of the columns are handled by code outside individual dissectors; most dissectors need only specify the value to put in the "Protocol" and "Info" columns. Columns are specified by COL_ values; the COL_ value for the "Protocol" field, typically giving an abbreviated name for the protocol (but not the all-lower-case abbreviation used elsewhere) is COL_PROTOCOL, and the COL_ value for the "Info" field, giving a summary of the contents of the packet for that protocol, is COL_INFO. The value for a column can be specified with one of several functions, all of which take the 'fd' argument to the dissector as their first argument, and the COL_ value for the column as their second argument. 1.4.1 The col_set_str function. 'col_set_str' takes a string as its third argument, and sets the value for the column to that value. It assumes that the pointer passed to it points to a string constant or a static "const" array, not to a variable, as it doesn't copy the string, it merely saves the pointer value; the argument can itself be a variable, as long as it always points to a string constant or a static "const" array. It is more efficient than 'col_add_str' or 'col_add_fstr'; however, if the dissector will be using 'col_append_str' or 'col_append_fstr" to append more information to the column, the string will have to be copied anyway, so it's best to use 'col_add_str' rather than 'col_set_str' in that case. For example, to set the "Protocol" column to "PROTOABBREV": col_set_str(pinfo->cinfo, COL_PROTOCOL, "PROTOABBREV"); 1.4.2 The col_add_str function. 'col_add_str' takes a string as its third argument, and sets the value for the column to that value. It takes the same arguments as 'col_set_str', but copies the string, so that if the string is, for example, an automatic variable that won't remain in scope when the dissector returns, it's safe to use. 1.4.3 The col_add_fstr function. 'col_add_fstr' takes a 'printf'-style format string as its third argument, and 'printf'-style arguments corresponding to '%' format items in that string as its subsequent arguments. For example, to set the "Info" field to " request, bytes", where "reqtype" is a string containing the type of the request in the packet and "n" is an unsigned integer containing the number of bytes in the request: col_add_fstr(pinfo->cinfo, COL_INFO, "%s request, %u bytes", reqtype, n); Don't use 'col_add_fstr' with a format argument of just "%s" - 'col_add_str', or possibly even 'col_set_str' if the string that matches the "%s" is a static constant string, will do the same job more efficiently. 1.4.4 The col_clear function. If the Info column will be filled with information from the packet, that means that some data will be fetched from the packet before the Info column is filled in. If the packet is so small that the data in question cannot be fetched, the routines to fetch the data will throw an exception (see the comment at the beginning about tvbuffers improving the handling of short packets - the tvbuffers keep track of how much data is in the packet, and throw an exception on an attempt to fetch data past the end of the packet, so that the dissector won't process bogus data), causing the Info column not to be filled in. This means that the Info column will have data for the previous protocol, which would be confusing if, for example, the Protocol column had data for this protocol. Therefore, before a dissector fetches any data whatsoever from the packet (unless it's a heuristic dissector fetching data to determine whether the packet is one that it should dissect, in which case it should check, before fetching the data, whether there's any data to fetch; if there isn't, it should return FALSE), it should set the Protocol column and the Info column. If the Protocol column will ultimately be set to, for example, a value containing a protocol version number, with the version number being a field in the packet, the dissector should, before fetching the version number field or any other field from the packet, set it to a value without a version number, using 'col_set_str', and should later set it to a value with the version number after it's fetched the version number. If the Info column will ultimately be set to a value containing information from the packet, the dissector should, before fetching any fields from the packet, clear the column using 'col_clear' (which is more efficient than clearing it by calling 'col_set_str' or 'col_add_str' with a null string), and should later set it to the real string after it's fetched the data to use when doing that. 1.4.5 The col_append_str function. Sometimes the value of a column, especially the "Info" column, can't be conveniently constructed at a single point in the dissection process; for example, it might contain small bits of information from many of the fields in the packet. 'col_append_str' takes, as arguments, the same arguments as 'col_add_str', but the string is appended to the end of the current value for the column, rather than replacing the value for that column. (Note that no blank separates the appended string from the string to which it is appended; if you want a blank there, you must add it yourself as part of the string being appended.) 1.4.6 The col_append_fstr function. 'col_append_fstr' is to 'col_add_fstr' as 'col_append_str' is to 'col_add_str' - it takes, as arguments, the same arguments as 'col_add_fstr', but the formatted string is appended to the end of the current value for the column, rather than replacing the value for that column. 1.4.7 The col_append_sep_str and col_append_sep_fstr functions. In specific situations the developer knows that a column's value will be created in a stepwise manner, where the appended values are listed. Both 'col_append_sep_str' and 'col_append_sep_fstr' functions will add an item separator between two consecutive items, and will not add the separator at the beginning of the column. The remainder of the work both functions do is identical to what 'col_append_str' and 'col_append_fstr' do. 1.4.8 The col_set_fence and col_prepend_fence_fstr functions. Sometimes a dissector may be called multiple times for different PDUs in the same frame (for example in the case of SCTP chunk bundling: several upper layer data packets may be contained in one SCTP packet). If the upper layer dissector calls 'col_set_str()' or 'col_clear()' on the Info column when it begins dissecting each of those PDUs then when the frame is fully dissected the Info column would contain only the string from the last PDU in the frame. The 'col_set_fence' function erects a "fence" in the column that prevents subsequent 'col_...' calls from clearing the data currently in that column. For example, the SCTP dissector calls 'col_set_fence' on the Info column after it has called any subdissectors for that chunk so that subdissectors of any subsequent chunks may only append to the Info column. 'col_prepend_fence_fstr' prepends data before a fence (moving it if necessary). It will create a fence at the end of the prepended data if the fence does not already exist. 1.4.9 The col_set_time function. The 'col_set_time' function takes an nstime value as its third argument. This nstime value is a relative value and will be added as such to the column. The fourth argument is the filtername holding this value. This way, rightclicking on the column makes it possible to build a filter based on the time-value. For example: col_set_time(pinfo->cinfo, COL_REL_TIME, &ts, "s4607.ploc.time"); 1.5 Constructing the protocol tree. The middle pane of the main window, and the topmost pane of a packet popup window, are constructed from the "protocol tree" for a packet. The protocol tree, or proto_tree, is a GNode, the N-way tree structure available within GLIB. Of course the protocol dissectors don't care what a proto_tree really is; they just pass the proto_tree pointer as an argument to the routines which allow them to add items and new branches to the tree. When a packet is selected in the packet-list pane, or a packet popup window is created, a new logical protocol tree (proto_tree) is created. The pointer to the proto_tree (in this case, 'protocol tree'), is passed to the top-level protocol dissector, and then to all subsequent protocol dissectors for that packet, and then the GUI tree is drawn via proto_tree_draw(). The logical proto_tree needs to know detailed information about the protocols and fields about which information will be collected from the dissection routines. By strictly defining (or "typing") the data that can be attached to a proto tree, searching and filtering becomes possible. This means that for every protocol and field (which I also call "header fields", since they are fields in the protocol headers) which might be attached to a tree, some information is needed. Every dissector routine will need to register its protocols and fields with the central protocol routines (in proto.c). At first I thought I might keep all the protocol and field information about all the dissectors in one file, but decentralization seemed like a better idea. That one file would have gotten very large; one small change would have required a re-compilation of the entire file. Also, by allowing registration of protocols and fields at run-time, loadable modules of protocol dissectors (perhaps even user-supplied) is feasible. To do this, each protocol should have a register routine, which will be called when Wireshark starts. The code to call the register routines is generated automatically; to arrange that a protocol's register routine be called at startup: the file containing a dissector's "register" routine must be added to "DISSECTOR_SRC" in "epan/dissectors/Makefile.common" (and in "epan/dissectors/CMakeLists.txt"); the "register" routine must have a name of the form "proto_register_XXX"; the "register" routine must take no argument, and return no value; the "register" routine's name must appear in the source file either at the beginning of the line, or preceded only by "void " at the beginning of the line (that would typically be the definition) - other white space shouldn't cause a problem, e.g.: void proto_register_XXX(void) { ... } and void proto_register_XXX( void ) { ... } and so on should work. For every protocol or field that a dissector wants to register, a variable of type int needs to be used to keep track of the protocol. The IDs are needed for establishing parent/child relationships between protocols and fields, as well as associating data with a particular field so that it can be stored in the logical tree and displayed in the GUI protocol tree. Some dissectors will need to create branches within their tree to help organize header fields. These branches should be registered as header fields. Only true protocols should be registered as protocols. This is so that a display filter user interface knows how to distinguish protocols from fields. A protocol is registered with the name of the protocol and its abbreviation. Here is how the frame "protocol" is registered. int proto_frame; proto_frame = proto_register_protocol ( /* name */ "Frame", /* short name */ "Frame", /* abbrev */ "frame" ); A header field is also registered with its name and abbreviation, but information about its data type is needed. It helps to look at the header_field_info struct to see what information is expected: struct header_field_info { const char *name; const char *abbrev; enum ftenum type; int display; const void *strings; guint64 bitmask; const char *blurb; ..... }; name (FIELDNAME) ---------------- A string representing the name of the field. This is the name that will appear in the graphical protocol tree. It must be a non-empty string. abbrev (FIELDABBREV) -------------------- A string with an abbreviation of the field. The abbreviation should start with the abbreviation of the parent protocol followed by a period as a separator. For example, the "src" field in an IP packet would have "ip.src" as an abbreviation. It is acceptable to have multiple levels of periods if, for example, you have fields in your protocol that are then subdivided into subfields. For example, TRMAC has multiple error fields, so the abbreviations follow this pattern: "trmac.errors.iso", "trmac.errors.noniso", etc. The abbreviation is the identifier used in a display filter. As such it cannot be an empty string. type (FIELDTYPE) ---------------- The type of value this field holds. The current field types are: FT_NONE No field type. Used for fields that aren't given a value, and that can only be tested for presence or absence; a field that represents a data structure, with a subtree below it containing fields for the members of the structure, or that represents an array with a subtree below it containing fields for the members of the array, might be an FT_NONE field. FT_PROTOCOL Used for protocols which will be placing themselves as top-level items in the "Packet Details" pane of the UI. FT_BOOLEAN 0 means "false", any other value means "true". FT_FRAMENUM A frame number; if this is used, the "Go To Corresponding Frame" menu item can work on that field. FT_UINT8 An 8-bit unsigned integer. FT_UINT16 A 16-bit unsigned integer. FT_UINT24 A 24-bit unsigned integer. FT_UINT32 A 32-bit unsigned integer. FT_UINT40 A 40-bit unsigned integer. FT_UINT48 A 48-bit unsigned integer. FT_UINT56 A 56-bit unsigned integer. FT_UINT64 A 64-bit unsigned integer. FT_INT8 An 8-bit signed integer. FT_INT16 A 16-bit signed integer. FT_INT24 A 24-bit signed integer. FT_INT32 A 32-bit signed integer. FT_INT40 A 40-bit signed integer. FT_INT48 A 48-bit signed integer. FT_INT56 A 56-bit signed integer. FT_INT64 A 64-bit signed integer. FT_FLOAT A single-precision floating point number. FT_DOUBLE A double-precision floating point number. FT_ABSOLUTE_TIME An absolute time from some fixed point in time, displayed as the date, followed by the time, as hours, minutes, and seconds with 9 digits after the decimal point. FT_RELATIVE_TIME Seconds (4 bytes) and nanoseconds (4 bytes) of time relative to an arbitrary time. displayed as seconds and 9 digits after the decimal point. FT_STRING A string of characters, not necessarily NULL-terminated, but possibly NULL-padded. This, and the other string-of-characters types, are to be used for text strings, not raw binary data. FT_STRINGZ A NULL-terminated string of characters. The string length is normally the length given in the proto_tree_add_item() call. However if the length given in the call is -1, then the length used is that returned by calling tvb_strsize(). FT_UINT_STRING A counted string of characters, consisting of a count (represented as an integral value, of width given in the proto_tree_add_item() call) followed immediately by that number of characters. FT_ETHER A six octet string displayed in Ethernet-address format. FT_BYTES A string of bytes with arbitrary values; used for raw binary data. FT_UINT_BYTES A counted string of bytes, consisting of a count (represented as an integral value, of width given in the proto_tree_add_item() call) followed immediately by that number of arbitrary values; used for raw binary data. FT_IPv4 A version 4 IP address (4 bytes) displayed in dotted-quad IP address format (4 decimal numbers separated by dots). FT_IPv6 A version 6 IP address (16 bytes) displayed in standard IPv6 address format. FT_IPXNET An IPX address displayed in hex as a 6-byte network number followed by a 6-byte station address. FT_GUID A Globally Unique Identifier FT_OID An ASN.1 Object Identifier FT_REL_OID An ASN.1 Relative Object Identifier FT_EUI64 A EUI-64 Address FT_AX25 A AX-25 Address FT_VINES A Vines Address FT_SYSTEM_ID An OSI System-ID FT_FCWWN A Fibre Channel WWN Address Some of these field types are still not handled in the display filter routines, but the most common ones are. The FT_UINT* variables all represent unsigned integers, and the FT_INT* variables all represent signed integers; the number on the end represent how many bits are used to represent the number. Some constraints are imposed on the header fields depending on the type (e.g. FT_BYTES) of the field. Fields of type FT_ABSOLUTE_TIME must use 'ABSOLUTE_TIME_{LOCAL,UTC,DOY_UTC}, NULL, 0x0' as values for the 'display, 'strings', and 'bitmask' fields, and all other non-integral types (i.e.. types that are _not_ FT_INT* and FT_UINT*) must use 'BASE_NONE, NULL, 0x0' as values for the 'display', 'strings', 'bitmask' fields. The reason is simply that the type itself implicitly defines the nature of 'display', 'strings', 'bitmask'. display (FIELDDISPLAY) ---------------------- The display field has a couple of overloaded uses. This is unfortunate, but since we're using C as an application programming language, this sometimes makes for cleaner programs. Right now I still think that overloading this variable was okay. For integer fields (FT_UINT* and FT_INT*), this variable represents the base in which you would like the value displayed. The acceptable bases are: BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX, BASE_HEX_DEC, BASE_CUSTOM BASE_DEC, BASE_HEX, and BASE_OCT are decimal, hexadecimal, and octal, respectively. BASE_DEC_HEX and BASE_HEX_DEC display value in two bases (the 1st representation followed by the 2nd in parenthesis). BASE_CUSTOM allows one to specify a callback function pointer that will format the value. For 32-bit and smaller values, custom_fmt_func_t can be used to declare the callback function pointer. Specifically, this is defined as: void func(gchar *, guint32); For values larger than 32-bits, custom_fmt_func_64_t can be used to declare the callback function pointer. Specifically, this is defined as: void func(gchar *, guint64); The first argument is a pointer to a buffer of the ITEM_LABEL_LENGTH size and the second argument is the value to be formatted. Both custom_fmt_func_t and custom_fmt_func_64_t are defined in epan/proto.h. For FT_UINT16 'display' can be used to select a transport layer protocol using one of BASE_PT_UDP, BASE_PT_TCP, BASE_PT_DCCP or BASE_PT_SCTP. If transport name resolution is enabled the port field label is displayed in decimal and as a well-known service name (if one is available). For FT_BOOLEAN fields that are also bitfields (i.e., 'bitmask' is non-zero), 'display' is used specify a "field-width" (i.e., tell the proto_tree how wide the parent bitfield is). (If the FT_BOOLEAN 'bitmask' is zero, then 'display' must be BASE_NONE). For integer fields a "field-width" is not needed since the type of integer itself (FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64, etc) tells the proto_tree how wide the parent bitfield is. For FT_ABSOLUTE_TIME fields, 'display' is used to indicate whether the time is to be displayed as a time in the time zone for the machine on which Wireshark/TShark is running or as UTC and, for UTC, whether the date should be displayed as "{monthname} {day_of_month}, {year}" or as "{year/day_of_year}". Additionally, BASE_NONE is used for 'display' as a NULL-value. That is, for non-integers other than FT_ABSOLUTE_TIME fields, and non-bitfield FT_BOOLEANs, you'll want to use BASE_NONE in the 'display' field. You may not use BASE_NONE for integers. It is possible that in the future we will record the endianness of integers. If so, it is likely that we'll use a bitmask on the display field so that integers would be represented as BEND|BASE_DEC or LEND|BASE_HEX. But that has not happened yet; note that there are protocols for which no endianness is specified, such as the X11 protocol and the DCE RPC protocol, so it would not be possible to record the endianness of all integral fields. strings (FIELDCONVERT) ---------------------- -- value_string Some integer fields, of type FT_UINT*, need labels to represent the true value of a field. You could think of those fields as having an enumerated data type, rather than an integral data type. A 'value_string' structure is a way to map values to strings. typedef struct _value_string { guint32 value; gchar *strptr; } value_string; For fields of that type, you would declare an array of "value_string"s: static const value_string valstringname[] = { { INTVAL1, "Descriptive String 1" }, { INTVAL2, "Descriptive String 2" }, { 0, NULL } }; (the last entry in the array must have a NULL 'strptr' value, to indicate the end of the array). The 'strings' field would be set to 'VALS(valstringname)'. If the field has a numeric rather than an enumerated type, the 'strings' field would be set to NULL. -- Extended value strings You can also use an extended version of the value_string for faster lookups. It requires a value_string array as input. If all of a contiguous range of values from min to max are present in the array in ascending order the value will be used as a direct index into a value_string array. If the values in the array are not contiguous (ie: there are "gaps"), but are in ascending order a binary search will be used. Note: "gaps" in a value_string array can be filled with "empty" entries eg: {value, "Unknown"} so that direct access to the array is is possible. Note: the value_string array values are *unsigned*; IOW: -1 is greater than 0. So: { -2, -1, 1, 2 }; wrong: linear search will be used (note gap) { 1, 2, -2, -1 }; correct: binary search will be used As a special case: { -2, -1, 0, 1, 2 }; OK: direct(indexed) access will be used (note no gap) The init macro (see below) will perform a check on the value string the first time it is used to determine which search algorithm fits and fall back to a linear search if the value_string does not meet the criteria above. Use this macro to initialize the extended value_string at compile time: static value_string_ext valstringname_ext = VALUE_STRING_EXT_INIT(valstringname); Extended value strings can be created at run time by calling value_string_ext_new(, , /* include {0, NULL} entry */ ); For hf[] array FT_(U)INT* fields that need a 'valstringname_ext' struct, the 'strings' field would be set to '&valstringname_ext'. Furthermore, the 'display' field must be ORed with 'BASE_EXT_STRING' (e.g. BASE_DEC|BASE_EXT_STRING). -- val64_string val64_strings are like value_strings, except that the integer type used is a guint64 (instead of guint32). Instead of using the VALS() macro for the 'strings' field in the header_field_info struct array, 'VALS64()' is used. -- Ranges If the field has a numeric type that might logically fit in ranges of values one can use a range_string struct. Thus a 'range_string' structure is a way to map ranges to strings. typedef struct _range_string { guint32 value_min; guint32 value_max; const gchar *strptr; } range_string; For fields of that type, you would declare an array of "range_string"s: static const range_string rvalstringname[] = { { INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" }, { INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" }, { 0, 0, NULL } }; If INTVAL_MIN equals INTVAL_MAX for a given entry the range_string behavior collapses to the one of value_string. For FT_(U)INT* fields that need a 'range_string' struct, the 'strings' field would be set to 'RVALS(rvalstringname)'. Furthermore, 'display' field must be ORed with 'BASE_RANGE_STRING' (e.g. BASE_DEC|BASE_RANGE_STRING). -- Booleans FT_BOOLEANs have a default map of 0 = "False", 1 (or anything else) = "True". Sometimes it is useful to change the labels for boolean values (e.g., to "Yes"/"No", "Fast"/"Slow", etc.). For these mappings, a struct called true_false_string is used. typedef struct true_false_string { char *true_string; char *false_string; } true_false_string; For Boolean fields for which "False" and "True" aren't the desired labels, you would declare a "true_false_string"s: static const true_false_string boolstringname = { "String for True", "String for False" }; Its two fields are pointers to the string representing truth, and the string representing falsehood. For FT_BOOLEAN fields that need a 'true_false_string' struct, the 'strings' field would be set to 'TFS(&boolstringname)'. If the Boolean field is to be displayed as "False" or "True", the 'strings' field would be set to NULL. Wireshark predefines a whole range of ready made "true_false_string"s in tfs.h, included via packet.h. -- Custom Custom fields (BASE_CUSTOM) should use CF_FUNC(&custom_format_func) for the 'strings' field. bitmask (BITMASK) ----------------- If the field is a bitfield, then the bitmask is the mask which will leave only the bits needed to make the field when ANDed with a value. The proto_tree routines will calculate 'bitshift' automatically from 'bitmask', by finding the rightmost set bit in the bitmask. This shift is applied before applying string mapping functions or filtering. If the field is not a bitfield, then bitmask should be set to 0. blurb (FIELDDESCR) ------------------ This is a string giving a proper description of the field. It should be at least one grammatically complete sentence, or NULL in which case the name field is used. (Please do not use ""). It is meant to provide a more detailed description of the field than the name alone provides. This information will be used in the man page, and in a future GUI display-filter creation tool. We might also add tooltips to the labels in the GUI protocol tree, in which case the blurb would be used as the tooltip text. 1.5.1 Field Registration. Protocol registration is handled by creating an instance of the header_field_info struct (or an array of such structs), and calling the registration function along with the registration ID of the protocol that is the parent of the fields. Here is a complete example: static int proto_eg = -1; static int hf_field_a = -1; static int hf_field_b = -1; static hf_register_info hf[] = { { &hf_field_a, { "Field A", "proto.field_a", FT_UINT8, BASE_HEX, NULL, 0xf0, "Field A represents Apples", HFILL }}, { &hf_field_b, { "Field B", "proto.field_b", FT_UINT16, BASE_DEC, VALS(vs), 0x0, "Field B represents Bananas", HFILL }} }; proto_eg = proto_register_protocol("Example Protocol", "PROTO", "proto"); proto_register_field_array(proto_eg, hf, array_length(hf)); Be sure that your array of hf_register_info structs is declared 'static', since the proto_register_field_array() function does not create a copy of the information in the array... it uses that static copy of the information that the compiler created inside your array. Here's the layout of the hf_register_info struct: typedef struct hf_register_info { int *p_id; /* pointer to parent variable */ header_field_info hfinfo; } hf_register_info; Also be sure to use the handy array_length() macro found in packet.h to have the compiler compute the array length for you at compile time. If you don't have any fields to register, do *NOT* create a zero-length "hf" array; not all compilers used to compile Wireshark support them. Just omit the "hf" array, and the "proto_register_field_array()" call, entirely. It is OK to have header fields with a different format be registered with the same abbreviation. For instance, the following is valid: static hf_register_info hf[] = { { &hf_field_8bit, /* 8-bit version of proto.field */ { "Field (8 bit)", "proto.field", FT_UINT8, BASE_DEC, NULL, 0x00, "Field represents FOO", HFILL }}, { &hf_field_32bit, /* 32-bit version of proto.field */ { "Field (32 bit)", "proto.field", FT_UINT32, BASE_DEC, NULL, 0x00, "Field represents FOO", HFILL }} }; This way a filter expression can match a header field, irrespective of the representation of it in the specific protocol context. This is interesting for protocols with variable-width header fields. Note that the formats used must all belong to the same group as defined below: - FT_INT8, FT_INT16, FT_INT24 and FT_INT32 - FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, FT_IPXNET and FT_FRAMENUM - FT_INT40, FT_INT48, FT_INT56 and FT_INT64 - FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64 and FT_EUI64 - FT_ABSOLUTE_TIME and FT_RELATIVE_TIME - FT_STRING, FT_STRINGZ, FT_UINT_STRING and FT_STRINGZPAD - FT_FLOAT and FT_DOUBLE - FT_BYTES, FT_UINT_BYTES, FT_ETHER, FT_AX25, FT_VINES and FT_FCWWN - FT_OID, FT_REL_OID and FT_SYSTEM_ID Any field not in a grouping above should *NOT* be used in duplicate field abbrevations. The current code does not prevent it, but someday in the future it might. The HFILL macro at the end of the struct will set reasonable default values for internally used fields. 1.5.2 Adding Items and Values to the Protocol Tree. A protocol item is added to an existing protocol tree with one of a handful of proto_XXX_DO_YYY() functions. Subtrees can be made with the proto_item_add_subtree() function: item = proto_tree_add_item(....); new_tree = proto_item_add_subtree(item, tree_type); This will add a subtree under the item in question; a subtree can be created under an item made by any of the "proto_tree_add_XXX" functions, so that the tree can be given an arbitrary depth. Subtree types are integers, assigned by "proto_register_subtree_array()". To register subtree types, pass an array of pointers to "gint" variables to hold the subtree type values to "proto_register_subtree_array()": static gint ett_eg = -1; static gint ett_field_a = -1; static gint *ett[] = { &ett_eg, &ett_field_a }; proto_register_subtree_array(ett, array_length(ett)); in your "register" routine, just as you register the protocol and the fields for that protocol. The ett_ variables identify particular type of subtree so that if you expand one of them, Wireshark keeps track of that and, when you click on another packet, it automatically opens all subtrees of that type. If you close one of them, all subtrees of that type will be closed when you move to another packet. There are several functions that the programmer can use to add either protocol or field labels to the proto_tree: proto_item* proto_tree_add_item(tree, id, tvb, start, length, encoding); proto_item* proto_tree_add_item_ret_int(tree, id, tvb, start, length, encoding, *retval); proto_item* proto_tree_add_item_ret_uint(tree, id, tvb, start, length, encoding, *retval); proto_item* proto_tree_add_subtree(tree, tvb, start, length, idx, tree_item, text); proto_item* proto_tree_add_subtree_format(tree, tvb, start, length, idx, tree_item, format, ...); proto_item* proto_tree_add_none_format(tree, id, tvb, start, length, format, ...); proto_item* proto_tree_add_protocol_format(tree, id, tvb, start, length, format, ...); proto_item * proto_tree_add_bytes(tree, id, tvb, start, length, start_ptr); proto_item * proto_tree_add_bytes_item(tree, id, tvb, start, length, encoding, retval, endoff, err); proto_item * proto_tree_add_bytes_format(tree, id, tvb, start, length, start_ptr, format, ...); proto_item * proto_tree_add_bytes_format_value(tree, id, tvb, start, length, start_ptr, format, ...); proto_item * proto_tree_add_bytes_with_length(tree, id, tvb, start, tvb_length, start_ptr, ptr_length); proto_item * proto_tree_add_time(tree, id, tvb, start, length, value_ptr); proto_item * proto_tree_add_time_item(tree, id, tvb, start, length, encoding, retval, endoff, err); proto_item * proto_tree_add_time_format(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_time_format_value(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_ipxnet(tree, id, tvb, start, length, value); proto_item * proto_tree_add_ipxnet_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_ipxnet_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_ipv4(tree, id, tvb, start, length, value); proto_item * proto_tree_add_ipv4_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_ipv4_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_ipv6(tree, id, tvb, start, length, value_ptr); proto_item * proto_tree_add_ipv6_format(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_ipv6_format_value(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_ether(tree, id, tvb, start, length, value_ptr); proto_item * proto_tree_add_ether_format(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_ether_format_value(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_guid(tree, id, tvb, start, length, value_ptr); proto_item * proto_tree_add_guid_format(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_guid_format_value(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_oid(tree, id, tvb, start, length, value_ptr); proto_item * proto_tree_add_oid_format(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_oid_format_value(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_string(tree, id, tvb, start, length, value_ptr); proto_item * proto_tree_add_string_format(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_string_format_value(tree, id, tvb, start, length, value_ptr, format, ...); proto_item * proto_tree_add_boolean(tree, id, tvb, start, length, value); proto_item * proto_tree_add_boolean_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_boolean_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_float(tree, id, tvb, start, length, value); proto_item * proto_tree_add_float_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_float_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_double(tree, id, tvb, start, length, value); proto_item * proto_tree_add_double_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_double_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_uint(tree, id, tvb, start, length, value); proto_item * proto_tree_add_uint_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_uint_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_uint64(tree, id, tvb, start, length, value); proto_item * proto_tree_add_uint64_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_uint64_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_int(tree, id, tvb, start, length, value); proto_item * proto_tree_add_int_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_int_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_int64(tree, id, tvb, start, length, value); proto_item * proto_tree_add_int64_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_int64_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_eui64(tree, id, tvb, start, length, value); proto_item * proto_tree_add_eui64_format(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_eui64_format_value(tree, id, tvb, start, length, value, format, ...); proto_item * proto_tree_add_bitmask(tree, tvb, start, header, ett, fields, encoding); proto_item * proto_tree_add_bitmask_len(tree, tvb, start, len, header, ett, fields, encoding); proto_item * proto_tree_add_bitmask_text(tree, tvb, offset, len, name, fallback, ett, fields, encoding, flags); proto_item* proto_tree_add_bits_item(tree, id, tvb, bit_offset, no_of_bits, encoding); proto_item * proto_tree_add_split_bits_item_ret_val(tree, hf_index, tvb, bit_offset, crumb_spec, return_value); void proto_tree_add_split_bits_crumb(tree, hf_index, tvb, bit_offset, crumb_spec, crumb_index); proto_item * proto_tree_add_bits_ret_val(tree, id, tvb, bit_offset, no_of_bits, return_value, encoding); proto_item * proto_tree_add_uint_bits_format_value(tree, id, tvb, bit_offset, no_of_bits, value, format, ...); proto_item * proto_tree_add_boolean_bits_format_value(tree, id, tvb, bit_offset, no_of_bits, value, format, ...); proto_item * proto_tree_add_int_bits_format_value(tree, id, tvb, bit_offset, no_of_bits, value, format, ...); proto_item * proto_tree_add_float_bits_format_value(tree, id, tvb, bit_offset, no_of_bits, value, format, ...); proto_item * proto_tree_add_ts_23_038_7bits_item(tree, hf_index, tvb, bit_offset, no_of_chars); The 'tree' argument is the tree to which the item is to be added. The 'tvb' argument is the tvbuff from which the item's value is being extracted; the 'start' argument is the offset from the beginning of that tvbuff of the item being added, and the 'length' argument is the length, in bytes, of the item, bit_offset is the offset in bits and no_of_bits is the length in bits. The length of some items cannot be determined until the item has been dissected; to add such an item, add it with a length of -1, and, when the dissection is complete, set the length with 'proto_item_set_len()': void proto_item_set_len(ti, length); The "ti" argument is the value returned by the call that added the item to the tree, and the "length" argument is the length of the item. proto_tree_add_item() --------------------- proto_tree_add_item is used when you wish to do no special formatting. The item added to the GUI tree will contain the name (as passed in the proto_register_*() function) and a value. The value will be fetched from the tvbuff by proto_tree_add_item(), based on the type of the field and the encoding of the value as specified by the "encoding" argument. For FT_NONE, FT_BYTES, FT_ETHER, FT_IPv6, FT_IPXNET, FT_OID, FT_REL_OID, FT_AX25, FT_VINES, FT_SYSTEM_ID, FT_FCWWN fields, and 'protocol' fields the encoding is not relevant; the 'encoding' argument should be ENC_NA (Not Applicable). For integral, floating-point, Boolean, FT_GUID, and FT_EUI64 fields, the encoding specifies the byte order of the value; the 'encoding' argument should be ENC_LITTLE_ENDIAN if the value is little-endian and ENC_BIG_ENDIAN if it is big-endian. For FT_IPv4 fields, the encoding also specifies the byte order of the value. In almost all cases, the encoding is in network byte order, hence big-endian, but in at least one protocol dissected by Wireshark, at least one IPv4 address is byte-swapped, so it's in little-endian order. For string fields, the encoding specifies the character set used for the string and the way individual code points in that character set are encoded. For FT_UINT_STRING fields, the byte order of the count must be specified; for UCS-2 and UTF-16, the byte order of the encoding must be specified (for counted UCS-2 and UTF-16 strings, the byte order of the count and the 16-bit values in the string must be the same). In other cases, ENC_NA should be used. The character encodings that are currently supported are: ENC_ASCII - ASCII (currently treated as UTF-8; in the future, all bytes with the 8th bit set will be treated as errors) ENC_UTF_8 - UTF-8-encoded Unicode ENC_UTF_16 - UTF-16-encoded Unicode, with surrogate pairs ENC_UCS_2 - UCS-2-encoded subset of Unicode, with no surrogate pairs and thus no code points above 0xFFFF ENC_UCS_4 - UCS-4-encoded Unicode ENC_WINDOWS_1250 - Windows-1250 code page ENC_ISO_8859_1 - ISO 8859-1 ENC_ISO_8859_2 - ISO 8859-2 ENC_ISO_8859_3 - ISO 8859-3 ENC_ISO_8859_4 - ISO 8859-4 ENC_ISO_8859_5 - ISO 8859-5 ENC_ISO_8859_6 - ISO 8859-6 ENC_ISO_8859_7 - ISO 8859-7 ENC_ISO_8859_8 - ISO 8859-8 ENC_ISO_8859_9 - ISO 8859-9 ENC_ISO_8859_10 - ISO 8859-10 ENC_ISO_8859_11 - ISO 8859-11 ENC_ISO_8859_13 - ISO 8859-13 ENC_ISO_8859_14 - ISO 8859-14 ENC_ISO_8859_15 - ISO 8859-15 ENC_ISO_8859_16 - ISO 8859-16 ENC_WINDOWS_1250 - Windows-1250 ENC_3GPP_TS_23_038_7BITS - GSM 7 bits alphabet as described in 3GPP TS 23.038 ENC_EBCDIC - EBCDIC ENC_MAC_ROMAN - MAC ROMAN ENC_CP437 - DOS CP437 ENC_ASCII_7BITS - 7 bits ASCII Other encodings will be added in the future. For FT_ABSOLUTE_TIME fields, the encoding specifies the form in which the time stamp is specified, as well as its byte order. The time stamp encodings that are currently supported are: ENC_TIME_TIMESPEC - seconds (4 bytes) and nanoseconds (4 bytes) of time since January 1, 1970, midnight UTC. (If the field length is 4, then nanosecs is defaulted to 0). ENC_TIME_NTP - an NTP timestamp, represented as a 64-bit unsigned fixed-point number, in seconds relative to 0h on 1 January 1900. The integer part is in the first 32 bits and the fraction part in the last 32 bits. For other types, there is no support for proto_tree_add_item(). Now that definitions of fields have detailed information about bitfield fields, you can use proto_tree_add_item() with no extra processing to add bitfield values to your tree. Here's an example. Take the Format Identifier (FID) field in the Transmission Header (TH) portion of the SNA protocol. The FID is the high nibble of the first byte of the TH. The FID would be registered like this: name = "Format Identifier" abbrev = "sna.th.fid" type = FT_UINT8 display = BASE_HEX strings = sna_th_fid_vals bitmask = 0xf0 The bitmask contains the value which would leave only the FID if bitwise-ANDed against the parent field, the first byte of the TH. The code to add the FID to the tree would be; proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, ENC_BIG_ENDIAN); The definition of the field already has the information about bitmasking and bitshifting, so it does the work of masking and shifting for us! This also means that you no longer have to create value_string structs with the values bitshifted. The value_string for FID looks like this, even though the FID value is actually contained in the high nibble. (You'd expect the values to be 0x0, 0x10, 0x20, etc.) /* Format Identifier */ static const value_string sna_th_fid_vals[] = { { 0x0, "SNA device <--> Non-SNA Device" }, { 0x1, "Subarea Node <--> Subarea Node" }, { 0x2, "Subarea Node <--> PU2" }, { 0x3, "Subarea Node or SNA host <--> Subarea Node" }, { 0x4, "?" }, { 0x5, "?" }, { 0xf, "Adjacent Subarea Nodes" }, { 0, NULL } }; The final implication of this is that display filters work the way you'd naturally expect them to. You'd type "sna.th.fid == 0xf" to find Adjacent Subarea Nodes. The user does not have to shift the value of the FID to the high nibble of the byte ("sna.th.fid == 0xf0") as was necessary in the past. proto_tree_add_item_ret_XXX() ------------------------------ proto_tree_add_item_ret_XXX is used when you want the displayed value returned for futher processing only integer and unsigned integer types up to 32 bits are supported usage of proper FT_ is checked. proto_tree_add_XXX_item() --------------------- proto_tree_add_XXX_item is used when you wish to do no special formatting, but also either wish for the retrieved value from the tvbuff to be handed back (to avoid doing tvb_get_...), and/or wish to have the value be decoded from the tvbuff in a string-encoded format. The item added to the GUI tree will contain the name (as passed in the proto_register_*() function) and a value. The value will be fetched from the tvbuff, based on the type of the XXX name and the encoding of the value as specified by the "encoding" argument. This function retrieves the value even if the passed-in tree param is NULL, so that it can be used by dissectors at all times to both get the value and set the tree item to it. Like other proto_tree_add functions, if there is a tree and the value cannot be decoded from the tvbuff, then an expert info error is reported. For string encoding, this means that a failure to decode the hex value from the string results in an expert info error being added to the tree. For string-decoding, the passed-in encoding argument needs to specify the string encoding (e.g., ENC_ASCII, ENC_UTF_8) as well as the format. For some XXX types, the format is constrained - for example for the encoding format for proto_tree_add_time_item() can only be one of the ENC_ISO_8601_* ones or ENC_RFC_822 or ENC_RFC_1123. For proto_tree_add_bytes_item() it can only be ENC_STR_HEX bit-or'ed with one or more of the ENC_SEP_* separator types. proto_tree_add_protocol_format() -------------------------------- proto_tree_add_protocol_format is used to add the top-level item for the protocol when the dissector routine wants complete control over how the field and value will be represented on the GUI tree. The ID value for the protocol is passed in as the "id" argument; the rest of the arguments are a "printf"-style format and any arguments for that format. The caller must include the name of the protocol in the format; it is not added automatically as in proto_tree_add_item(). proto_tree_add_none_format() ---------------------------- proto_tree_add_none_format is used to add an item of type FT_NONE. The caller must include the name of the field in the format; it is not added automatically as in proto_tree_add_item(). proto_tree_add_bytes() proto_tree_add_time() proto_tree_add_ipxnet() proto_tree_add_ipv4() proto_tree_add_ipv6() proto_tree_add_ether() proto_tree_add_string() proto_tree_add_boolean() proto_tree_add_float() proto_tree_add_double() proto_tree_add_uint() proto_tree_add_uint64() proto_tree_add_int() proto_tree_add_int64() proto_tree_add_guid() proto_tree_add_oid() proto_tree_add_eui64() ------------------------ These routines are used to add items to the protocol tree if either: the value of the item to be added isn't just extracted from the packet data, but is computed from data in the packet; the value was fetched into a variable. The 'value' argument has the value to be added to the tree. NOTE: in all cases where the 'value' argument is a pointer, a copy is made of the object pointed to; if you have dynamically allocated a buffer for the object, that buffer will not be freed when the protocol tree is freed - you must free the buffer yourself when you don't need it any more. For proto_tree_add_bytes(), the 'value_ptr' argument is a pointer to a sequence of bytes. proto_tree_add_bytes_with_length() is similar to proto_tree_add_bytes, except that the length is not derived from the tvb length. Instead, the displayed data size is controlled by 'ptr_length'. For proto_tree_add_bytes_format() and proto_tree_add_bytes_format_value(), the 'value_ptr' argument is a pointer to a sequence of bytes or NULL if the bytes should be taken from the given TVB using the given offset and length. For proto_tree_add_time(), the 'value_ptr' argument is a pointer to an "nstime_t", which is a structure containing the time to be added; it has 'secs' and 'nsecs' members, giving the integral part and the fractional part of a time in units of seconds, with 'nsecs' being the number of nanoseconds. For absolute times, "secs" is a UNIX-style seconds since January 1, 1970, 00:00:00 GMT value. For proto_tree_add_ipxnet(), the 'value' argument is a 32-bit IPX network address. For proto_tree_add_ipv4(), the 'value' argument is a 32-bit IPv4 address, in network byte order. For proto_tree_add_ipv6(), the 'value_ptr' argument is a pointer to a 128-bit IPv6 address. For proto_tree_add_ether(), the 'value_ptr' argument is a pointer to a 48-bit MAC address. For proto_tree_add_string(), the 'value_ptr' argument is a pointer to a text string; this string must be NULL terminated even if the string in the TVB is not (as may be the case with FT_STRINGs). For proto_tree_add_boolean(), the 'value' argument is a 32-bit integer. It is masked and shifted as defined by the field info after which zero means "false", and non-zero means "true". For proto_tree_add_float(), the 'value' argument is a 'float' in the host's floating-point format. For proto_tree_add_double(), the 'value' argument is a 'double' in the host's floating-point format. For proto_tree_add_uint(), the 'value' argument is a 32-bit unsigned integer value, in host byte order. (This routine cannot be used to add 64-bit integers.) For proto_tree_add_uint64(), the 'value' argument is a 64-bit unsigned integer value, in host byte order. For proto_tree_add_int(), the 'value' argument is a 32-bit signed integer value, in host byte order. (This routine cannot be used to add 64-bit integers.) For proto_tree_add_int64(), the 'value' argument is a 64-bit signed integer value, in host byte order. For proto_tree_add_guid(), the 'value_ptr' argument is a pointer to an e_guid_t structure. For proto_tree_add_oid(), the 'value_ptr' argument is a pointer to an ASN.1 Object Identifier. For proto_tree_add_eui64(), the 'value' argument is a 64-bit integer value proto_tree_add_bytes_format() proto_tree_add_time_format() proto_tree_add_ipxnet_format() proto_tree_add_ipv4_format() proto_tree_add_ipv6_format() proto_tree_add_ether_format() proto_tree_add_string_format() proto_tree_add_boolean_format() proto_tree_add_float_format() proto_tree_add_double_format() proto_tree_add_uint_format() proto_tree_add_uint64_format() proto_tree_add_int_format() proto_tree_add_int64_format() proto_tree_add_guid_format() proto_tree_add_oid_format() proto_tree_add_eui64_format() ---------------------------- These routines are used to add items to the protocol tree when the dissector routine wants complete control over how the field and value will be represented on the GUI tree. The argument giving the value is the same as the corresponding proto_tree_add_XXX() function; the rest of the arguments are a "printf"-style format and any arguments for that format. The caller must include the name of the field in the format; it is not added automatically as in the proto_tree_add_XXX() functions. proto_tree_add_bytes_format_value() proto_tree_add_time_format_value() proto_tree_add_ipxnet_format_value() proto_tree_add_ipv4_format_value() proto_tree_add_ipv6_format_value() proto_tree_add_ether_format_value() proto_tree_add_string_format_value() proto_tree_add_boolean_format_value() proto_tree_add_float_format_value() proto_tree_add_double_format_value() proto_tree_add_uint_format_value() proto_tree_add_uint64_format_value() proto_tree_add_int_format_value() proto_tree_add_int64_format_value() proto_tree_add_guid_format_value() proto_tree_add_oid_format_value() proto_tree_add_eui64_format_value() ------------------------------------ These routines are used to add items to the protocol tree when the dissector routine wants complete control over how the value will be represented on the GUI tree. The argument giving the value is the same as the corresponding proto_tree_add_XXX() function; the rest of the arguments are a "printf"-style format and any arguments for that format. With these routines, unlike the proto_tree_add_XXX_format() routines, the name of the field is added automatically as in the proto_tree_add_XXX() functions; only the value is added with the format. proto_tree_add_subtree() --------------------- proto_tree_add_subtree() is used to add a label to the GUI tree and create a subtree for other fields. It will contain no value, so it is not searchable in the display filter process. This should only be used for items with subtrees, which may not have values themselves - the items in the subtree are the ones with values. For a subtree, the label on the subtree might reflect some of the items in the subtree. This means the label can't be set until at least some of the items in the subtree have been dissected. To do this, use 'proto_item_set_text()' or 'proto_item_append_text()': void proto_item_set_text(proto_item *ti, ...); void proto_item_append_text(proto_item *ti, ...); 'proto_item_set_text()' takes as an argument the proto_item value returned by one of the parameters in 'proto_tree_add_subtree()', a 'printf'-style format string, and a set of arguments corresponding to '%' format items in that string, and replaces the text for the item created by 'proto_tree_add_subtree()' with the result of applying the arguments to the format string. 'proto_item_append_text()' is similar, but it appends to the text for the item the result of applying the arguments to the format string. For example, early in the dissection, one might do: subtree = proto_tree_add_subtree(tree, tvb, offset, length, ett, &ti,