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