Provide for per-protocol-tree data in the proto_tree code.
[obnox/wireshark/wip.git] / epan / proto.h
1 /* proto.h
2  * Definitions for protocol display
3  *
4  * $Id: proto.h,v 1.25 2001/12/18 19:09:04 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __PROTO_H__
26 #define __PROTO_H__
27
28 #ifdef HAVE_STDARG_H
29 # include <stdarg.h>
30 #else
31 # include <varargs.h>
32 #endif
33
34 #ifdef HAVE_WINSOCK_H
35 # include <winsock.h>
36 #endif
37
38 #include <glib.h>
39
40 #include "ipv4.h"
41 #include "nstime.h"
42 #include "tvbuff.h"
43 #include "ftypes/ftypes.h"
44
45 struct value_string;
46
47 #define ITEM_LABEL_LENGTH       240
48
49 /* In order to make a const value_string[] look like a value_string*, I
50  * need this macro */
51 #define VALS(x) (struct value_string*)(x)
52
53 /* ... and similarly, */
54 #define TFS(x)  (struct true_false_string*)(x)
55
56 /* check protocol activation */
57 #define CHECK_DISPLAY_AS_X(x_handle,index, tvb, pinfo, tree) {  \
58         if (!proto_is_protocol_enabled(index)) {                \
59                 call_dissector(x_handle,tvb, pinfo, tree);      \
60                 return;                                         \
61         }                                                       \
62   }
63
64 enum {
65         BASE_NONE,
66         BASE_DEC,
67         BASE_HEX,
68         BASE_OCT,
69         BASE_BIN
70 };
71
72 typedef struct _header_field_info header_field_info;
73
74 /* information describing a header field */
75 struct _header_field_info {
76         /* ---------- set by dissector --------- */
77         char                            *name;
78         char                            *abbrev;
79         enum ftenum                     type;
80         int                             display;        /* for integers only, so far. Base */
81         void                            *strings;       /* val_string or true_false_string */
82         guint32                         bitmask;
83         char                            *blurb;         /* Brief description of field. */
84
85         /* ---------- set by proto routines --------- */
86         int                             id;             /* Field ID */
87         int                             parent;         /* parent protocol */
88         int                             bitshift;       /* bits to shift */
89         header_field_info               *same_name_next; /* Link to next hfinfo with same abbrev*/
90         header_field_info               *same_name_prev; /* Link to previous hfinfo with same abbrev*/
91 };
92
93 /*
94  * HFILL initializes all the "set by proto routines" fields in a
95  * "header_field_info"; if new fields are added or removed, it should
96  * be changed as necessary.
97  */
98 #define HFILL 0, 0, 0, NULL, NULL
99
100 /* Used when registering many fields at once */
101 typedef struct hf_register_info {
102         int                     *p_id;  /* pointer to int; written to by register() function */
103         header_field_info       hfinfo;
104 } hf_register_info;
105
106
107 /* Contains the field information for the proto_item. */
108 typedef struct field_info {
109         header_field_info               *hfinfo;
110         gint                            start;
111         gint                            length;
112         gint                            tree_type; /* ETT_* */
113         char                            *representation; /* for GUI tree */
114         int                             visible;
115         fvalue_t                        *value;
116         gchar                           *ds_name;  /* data source name */
117 } field_info;
118
119 /* One of these exists for the entire protocol tree. Each proto_node
120  * in the protocol tree points to the same copy. */
121 typedef struct {
122     GHashTable  *interesting_hfids;
123     gboolean    visible;
124 } tree_data_t;
125
126 /* Each GNode (proto_tree, proto_item) points to one of
127  * these. */
128 typedef struct {
129     field_info  *finfo;    
130     tree_data_t *tree_data;
131 } proto_node;
132
133 typedef GNode proto_tree;
134 typedef GNode proto_item;
135
136 /* Retrieve the proto_node from a GNode. */
137 #define GNODE_PNODE(t)  ((proto_node*)((GNode*)(t))->data)
138
139 /* Retrieve the field_info from a proto_item */
140 #define PITEM_FINFO(t)  (GNODE_PNODE(t)->finfo)
141
142 /* Retrieve the tree_data_t from a proto_tree */
143 #define PTREE_DATA(t)   (GNODE_PNODE(t)->tree_data)
144
145 /* Sets up memory used by proto routines. Called at program startup */
146 extern void proto_init(const char *plugin_dir,
147     void (register_all_protocols)(void), void (register_all_handoffs)(void));
148
149 /* Frees memory used by proto routines. Called at program shutdown */
150 extern void proto_cleanup(void);
151
152 /* Set text of proto_item after having already been created. */
153 #if __GNUC__ >= 2
154 extern void proto_item_set_text(proto_item *ti, const char *format, ...)
155         __attribute__((format (printf, 2, 3)));
156 #else
157 extern void proto_item_set_text(proto_item *ti, const char *format, ...);
158 #endif
159
160 /* Append to text of proto_item after having already been created. */
161 #if __GNUC__ >= 2
162 extern void proto_item_append_text(proto_item *ti, const char *format, ...)
163         __attribute__((format (printf, 2, 3)));
164 #else
165 extern void proto_item_append_text(proto_item *ti, const char *format, ...);
166 #endif
167
168 /* Set length of proto_item after having already been created. */
169 extern void proto_item_set_len(proto_item *ti, gint length);
170
171 /* Get length of proto_item. Useful after using proto_tree_add_item()
172  * to add a variable-length field (e.g., FT_NSTRING_UINT8) */
173 extern int proto_item_get_len(proto_item *ti);
174
175 /* Creates new proto_tree root */
176 extern proto_tree* proto_tree_create_root(void);
177
178 /* Mark a field/protocol ID as "interesting". */    
179 void
180 proto_tree_prime_hfid(proto_tree *tree, int hfid);
181
182 /* Clear memory for entry proto_tree. Clears proto_tree struct also. */
183 extern void proto_tree_free(proto_tree *tree);
184
185 /* Create a subtree under an existing item; returns tree pointer */
186 extern proto_tree* proto_item_add_subtree(proto_item *ti, gint idx);
187
188 extern int
189 proto_register_field(char *name, char *abbrev, enum ftenum type, int parent,
190         struct value_string* vals);
191
192 extern int
193 proto_register_protocol(char *name, char *short_name, char *filter_name);
194
195 extern void
196 proto_register_field_array(int parent, hf_register_info *hf, int num_records);
197
198 extern void
199 proto_register_subtree_array(gint **indices, int num_indices);
200
201 /* Add an item to a proto_tree, using the text label registered to that item;
202    the item is extracted from the tvbuff handed to it. */
203 extern proto_item *
204 proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb,
205     gint start, gint length, gboolean little_endian);
206
207 extern proto_item *
208 proto_tree_add_item_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb,
209     gint start, gint length, gboolean little_endian);
210
211 /* Add a FT_NONE to a proto_tree */
212 #if __GNUC__ >= 2
213 extern proto_item *
214 proto_tree_add_none_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
215         gint length, const char *format, ...)
216         __attribute__((format (printf, 6, 7)));
217 #else
218 extern proto_item *
219 proto_tree_add_none_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
220         gint length, const char *format, ...);
221 #endif
222
223 /* Add a FT_PROTOCOL to a proto_tree */
224 #if __GNUC__ >= 2
225 extern proto_item *
226 proto_tree_add_protocol_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
227         gint length, const char *format, ...)
228         __attribute__((format (printf, 6, 7)));
229 #else
230 extern proto_item *
231 proto_tree_add_protocol_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
232         gint length, const char *format, ...);
233 #endif
234
235 /* Add a FT_BYTES to a proto_tree */
236 extern proto_item *
237 proto_tree_add_bytes(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
238         gint length, const guint8* start_ptr);
239
240 extern proto_item *
241 proto_tree_add_bytes_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
242         gint length, const guint8* start_ptr);
243
244 #if __GNUC__ >= 2
245 extern proto_item *
246 proto_tree_add_bytes_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
247         gint length, const guint8* start_ptr, const char *format, ...)
248         __attribute__((format (printf, 7, 8)));
249 #else
250 extern proto_item *
251 proto_tree_add_bytes_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
252         gint length, const guint8* start_ptr, const char *format, ...);
253 #endif
254
255 /* Add a FT_*TIME to a proto_tree */
256 extern proto_item *
257 proto_tree_add_time(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
258         gint length, nstime_t* value_ptr);
259
260 extern proto_item *
261 proto_tree_add_time_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
262         gint length, nstime_t* value_ptr);
263
264 #if __GNUC__ >= 2
265 extern proto_item *
266 proto_tree_add_time_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
267         gint length, nstime_t* value_ptr, const char *format, ...)
268         __attribute__((format (printf, 7, 8)));
269 #else
270 extern proto_item *
271 proto_tree_add_time_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
272         gint length, nstime_t* value_ptr, const char *format, ...);
273 #endif
274
275 /* Add a FT_IPXNET to a proto_tree */
276 extern proto_item *
277 proto_tree_add_ipxnet(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
278         gint length, guint32 value);
279
280 extern proto_item *
281 proto_tree_add_ipxnet_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
282         gint length, guint32 value);
283
284 #if __GNUC__ >= 2
285 extern proto_item *
286 proto_tree_add_ipxnet_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
287         gint length, guint32 value, const char *format, ...)
288         __attribute__((format (printf, 7, 8)));
289 #else
290 extern proto_item *
291 proto_tree_add_ipxnet_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
292         gint length, guint32 value, const char *format, ...);
293 #endif
294
295 /* Add a FT_IPv4 to a proto_tree */
296 extern proto_item *
297 proto_tree_add_ipv4(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
298         gint length, guint32 value);
299
300 extern proto_item *
301 proto_tree_add_ipv4_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
302         gint length, guint32 value);
303
304 #if __GNUC__ >= 2
305 extern proto_item *
306 proto_tree_add_ipv4_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
307         gint length, guint32 value, const char *format, ...)
308         __attribute__((format (printf, 7, 8)));
309 #else
310 extern proto_item *
311 proto_tree_add_ipv4_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
312         gint length, guint32 value, const char *format, ...);
313 #endif
314
315 /* Add a FT_IPv6 to a proto_tree */
316 extern proto_item *
317 proto_tree_add_ipv6(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
318         gint length, const guint8* value_ptr);
319
320 extern proto_item *
321 proto_tree_add_ipv6_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
322         gint length, const guint8* value_ptr);
323
324 #if __GNUC__ >= 2
325 extern proto_item *
326 proto_tree_add_ipv6_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
327         gint length, const guint8* value_ptr, const char *format, ...)
328         __attribute__((format (printf, 7, 8)));
329 #else
330 extern proto_item *
331 proto_tree_add_ipv6_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
332         gint length, const guint8* value_ptr, const char *format, ...);
333 #endif
334
335 /* Add a FT_ETHER to a proto_tree */
336 extern proto_item *
337 proto_tree_add_ether(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
338         gint length, const guint8* value);
339
340 extern proto_item *
341 proto_tree_add_ether_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
342         gint length, const guint8* value);
343
344 #if __GNUC__ >= 2
345 extern proto_item *
346 proto_tree_add_ether_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
347         gint length, const guint8* value, const char *format, ...)
348         __attribute__((format (printf, 7, 8)));
349 #else
350 extern proto_item *
351 proto_tree_add_ether_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
352         gint length, const guint8* value, const char *format, ...);
353 #endif
354
355 /* Add a FT_STRING to a proto_tree */
356 extern proto_item *
357 proto_tree_add_string(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
358         gint length, const char* value);
359
360 extern proto_item *
361 proto_tree_add_string_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
362         gint length, const char* value);
363
364 #if __GNUC__ >= 2
365 extern proto_item *
366 proto_tree_add_string_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
367         gint length, const char* value, const char *format, ...)
368         __attribute__((format (printf, 7, 8)));
369 #else
370 extern proto_item *
371 proto_tree_add_string_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
372         gint length, const char* value, const char *format, ...);
373 #endif
374
375 /* Add a FT_BOOLEAN to a proto_tree */
376 extern proto_item *
377 proto_tree_add_boolean(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
378         gint length, guint32 value);
379
380 extern proto_item *
381 proto_tree_add_boolean_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
382         gint length, guint32 value);
383
384 #if __GNUC__ >= 2
385 extern proto_item *
386 proto_tree_add_boolean_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
387         gint length, guint32 value, const char *format, ...)
388         __attribute__((format (printf, 7, 8)));
389 #else
390 extern proto_item *
391 proto_tree_add_boolean_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
392         gint length, guint32 value, const char *format, ...);
393 #endif
394
395 /* Add a FT_DOUBLE to a proto_tree */
396 extern proto_item *
397 proto_tree_add_double(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
398         gint length, double value);
399
400 extern proto_item *
401 proto_tree_add_double_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
402         gint length, double value);
403
404 #if __GNUC__ >= 2
405 extern proto_item *
406 proto_tree_add_double_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
407         gint length, double value, const char *format, ...)
408         __attribute__((format (printf, 7, 8)));
409 #else
410 extern proto_item *
411 proto_tree_add_double_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
412         gint length, double value, const char *format, ...);
413 #endif
414
415 /* Add any FT_UINT* to a proto_tree */
416 extern proto_item *
417 proto_tree_add_uint(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
418         gint length, guint32 value);
419
420 extern proto_item *
421 proto_tree_add_uint_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
422         gint length, guint32 value);
423
424 #if __GNUC__ >= 2
425 extern proto_item *
426 proto_tree_add_uint_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
427         gint length, guint32 value, const char *format, ...)
428         __attribute__((format (printf, 7, 8)));
429 #else
430 extern proto_item *
431 proto_tree_add_uint_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
432         gint length, guint32 value, const char *format, ...);
433 #endif
434
435 /* Add any FT_INT* to a proto_tree */
436 extern proto_item *
437 proto_tree_add_int(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
438         gint length, gint32 value);
439
440 extern proto_item *
441 proto_tree_add_int_hidden(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
442         gint length, gint32 value);
443
444 #if __GNUC__ >= 2
445 extern proto_item *
446 proto_tree_add_int_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
447         gint length, gint32 value, const char *format, ...)
448         __attribute__((format (printf, 7, 8)));
449 #else
450 extern proto_item *
451 proto_tree_add_int_format(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
452         gint length, gint32 value, const char *format, ...);
453 #endif
454
455
456 /* Add a text-only node to the proto_tree */
457 #if __GNUC__ >= 2
458 extern proto_item *
459 proto_tree_add_text(proto_tree *tree, tvbuff_t *tvb, gint start, gint length, const char *,
460         ...) __attribute__((format (printf, 5, 6)));
461 #else
462 extern proto_item *
463 proto_tree_add_text(proto_tree *tree, tvbuff_t *tvb, gint start, gint length, const char *,
464         ...);
465 #endif
466
467 extern proto_item *
468 proto_tree_add_text_valist(proto_tree *tree, tvbuff_t *tvb, gint start,
469         gint length, const char *format, va_list ap);
470
471
472 /* Useful for quick debugging. Also sends string to STDOUT, so don't
473  * leave call to this function in production code. */
474 #if __GNUC__ >= 2
475 extern proto_item *
476 proto_tree_add_debug_text(proto_tree *tree, const char *format, ...)
477         __attribute__((format (printf, 2, 3)));
478 #else
479 extern proto_item *
480 proto_tree_add_debug_text(proto_tree *tree, const char *format, ...);
481 #endif
482
483 extern void
484 proto_item_fill_label(field_info *fi, gchar *label_str);
485
486 extern void
487 proto_tree_set_visible(proto_tree *tree, gboolean visible);
488
489 /* Returns number of items (protocols or header fields) registered. */
490 extern int proto_registrar_n(void);
491
492 /* Returns char* to name for item # n (0-indexed) */
493 extern char* proto_registrar_get_name(int n);
494
495 /* Returns char* to abbrev for item # n (0-indexed) */
496 extern char* proto_registrar_get_abbrev(int n);
497
498 /* get the header field information based upon a field or protocol id */
499 extern header_field_info* proto_registrar_get_nth(int hfindex);
500
501 /* Returns enum ftenum for item # n */
502 extern int proto_registrar_get_ftype(int n);
503
504 /* Returns parent protocol for item # n.
505  * Returns -1 if item _is_ a protocol */
506 extern int proto_registrar_get_parent(int n);
507
508 /* Is item #n a protocol? */
509 extern gboolean proto_registrar_is_protocol(int n);
510
511 /* Is item #n decoding enabled ? */
512 extern gboolean proto_is_protocol_enabled(int proto_id);
513
514 /* Can item #n decoding be disabled? */
515 extern gboolean proto_can_disable_protocol(int proto_id);
516
517 /* Routines to use to iterate over the protocols; they return the item
518  * number of the protocol in question, and keep state in "*cookie". */
519 extern int proto_get_first_protocol(void **cookie);
520 extern int proto_get_next_protocol(void **cookie);
521
522 /* Given a protocol's filter_name, return it's proto_id */
523 extern int proto_get_id_by_filter_name(gchar* filter_name);
524
525 /* Given a protocol's item number, return its name. */
526 extern char *proto_get_protocol_name(int n);
527
528 /* Given a protocol's item number, return its short name. */
529 extern char *proto_get_protocol_short_name(int proto_id);
530
531 /* Given a protocol's item number, return its filter name. */
532 extern char *proto_get_protocol_filter_name(int proto_id);
533
534 /* Enable / Disable protocol */
535 extern void proto_set_decoding(int proto_id, gboolean enabled);
536
537 /* Disable disabling of protocol */
538 extern void proto_set_cant_disable(int proto_id);
539
540 /* Get length of registered field according to field type.
541  * 0 means undeterminable at registration time.
542  * -1 means unknown field */
543 extern gint proto_registrar_get_length(int n);
544
545 /* Checks for existence any protocol or field within a tree.
546  * "Protocols" are assumed to be a child of the [empty] root node.
547  * TRUE = found, FALSE = not found */
548 extern gboolean proto_check_for_protocol_or_field(proto_tree* tree, int id);
549
550 /* Return GPtrArray* of field_info pointers for all hfindex that appear in
551  * tree. */
552 extern GPtrArray* proto_get_finfo_ptr_array(proto_tree *tree, int hfindex);
553
554 /* Dumps a glossary of the protocol and field registrations to STDOUT */
555 extern void proto_registrar_dump(void);
556
557 /* Is the parsing being done for a visible proto_tree or an invisible one?
558  * By setting this correctly, the proto_tree creation is sped up by not
559  * having to call vsnprintf and copy strings around.
560  */
561 extern gboolean proto_tree_is_visible;
562
563 /* Points to the first element of an array of Booleans, indexed by
564    a subtree item type; that array element is TRUE if subtrees of
565    an item of that type are to be expanded.
566
567    ETT_NONE is reserved for unregistered subtree types. */
568 #define ETT_NONE        0
569 extern gboolean      *tree_is_expanded;
570
571 /* Number of elements in that array. */
572 extern int           num_tree_types;
573
574 /* glib doesn't have g_ptr_array_len of all things!*/
575 #ifndef g_ptr_array_len
576 #define g_ptr_array_len(a)      ((a)->len)
577 #endif
578
579 extern int
580 hfinfo_bitwidth(header_field_info *hfinfo);
581
582 /*
583  * Returns TRUE if we can do a "match selected" on the field, FALSE
584  * otherwise.
585  */
586 extern gboolean
587 proto_can_match_selected(field_info *finfo);
588
589 extern char*
590 proto_alloc_dfilter_string(field_info *finfo, guint8 *pd);
591
592 extern field_info*
593 proto_find_field_from_offset(proto_tree *tree, guint offset, gchar *ds_name);
594
595 #endif /* proto.h */