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