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