Add "class" that understands IPv4 addresses and subnet masks.
[metze/wireshark/wip.git] / proto.h
1 /* proto.h
2  * Definitions for protocol display
3  *
4  * $Id: proto.h,v 1.19 1999/11/15 06:32:15 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 # ifndef _SYS_TIME_H
32 #  include <sys/time.h>
33 # endif
34 #endif
35
36 #ifdef HAVE_WINSOCK_H
37 # include <winsock.h>
38 #endif
39
40 #ifndef __IPV4_H__
41 #include "ipv4.h"
42 #endif
43
44 /* needs glib.h */
45 typedef struct GNode proto_tree;
46 typedef struct GNode proto_item;
47 struct value_string;
48
49 #define ITEM_LABEL_LENGTH       240
50
51 /* In order to make a const value_string[] look like a value_string*, I
52  * need this macro */
53 #define VALS(x) (struct value_string*)(x)
54
55 /* ... and similarly, */
56 #define TFS(x)  (struct true_false_string*)(x)
57
58 /* field types */
59 enum ftenum {
60         FT_NONE,        /* used for protocol labels (thus no field type) */
61         FT_BOOLEAN,     /* TRUE and FALSE come from <glib.h> */
62         FT_UINT8,
63         FT_UINT16,
64         FT_UINT24,      /* really a UINT32, but displayed as 3 hex-digits if FD_HEX*/
65         FT_UINT32,
66         FT_INT8,
67         FT_INT16,
68         FT_INT24,
69         FT_INT32,
70         FT_DOUBLE,
71         FT_ABSOLUTE_TIME,
72         FT_RELATIVE_TIME,
73         FT_STRING,
74         FT_ETHER,
75         FT_BYTES,
76         FT_IPv4,
77         FT_IPv6,
78         FT_IPXNET,
79         FT_TEXT_ONLY,   /* non-filterable, used when converting ethereal
80                                 from old-style proto_tree to new-style proto_tree */
81         NUM_FIELD_TYPES /* last item number plus one */
82 };
83
84 enum {
85         BASE_NONE,
86         BASE_DEC,
87         BASE_HEX,
88         BASE_OCT,
89         BASE_BIN
90 };
91
92 /* information describing a header field */
93 typedef struct header_field_info {
94         char                            *name;
95         char                            *abbrev;
96         enum ftenum                     type;
97         int                             display;        /* for integers only, so far. Base and Endianness */
98         void                            *strings;       /* val_string or true_false_string */
99         guint32                         bitmask;
100         char                            *blurb;         /* Brief description of field. */
101
102         int                             id;             /* assigned by registration function, not programmer */
103         int                             parent;         /* parent protocol */
104         int                             bitshift;       /* bits to shift */
105 } header_field_info;
106
107 /* Used when registering many fields at once */
108 typedef struct hf_register_info {
109         int                     *p_id;  /* pointer to int; written to by register() function */
110         header_field_info       hfinfo;
111 } hf_register_info;
112
113
114 /* Info stored in each proto_item GNode */
115 typedef struct field_info {
116         struct header_field_info        *hfinfo;
117         gint                            start;
118         gint                            length;
119         gint                            tree_type; /* ETT_* */
120         char                            *representation; /* for GUI tree */
121         int                             visible;
122         union {
123                 guint32         numeric;
124                 struct timeval  time; /* the whole struct, not a pointer */
125                 gdouble         floating;
126                 gchar           *string;
127                 guint8          *bytes;
128                 guint8          ether[6];
129                 ipv4_addr       ipv4;
130                 guint8          ipv6[16];
131         }                               value;
132 } field_info;
133
134
135 /* used when calling proto search functions */
136 typedef struct proto_tree_search_info {
137         int                     target;
138         int                     parent;
139         const guint8            *packet_data;
140         guint                   packet_len;
141         GNodeTraverseFunc       traverse_func;
142         union {
143                 GArray                  *array;
144                 GNode                   *node;
145         }                       result;
146 } proto_tree_search_info;
147
148 /* Sets up memory used by proto routines. Called at program startup */
149 void proto_init(void);
150
151 /* Frees memory used by proto routines. Called at program shutdown */
152 void proto_cleanup(void);
153
154 /* Set length of proto_item after having already been created. */
155 void proto_item_set_len(proto_item *ti, gint length);
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 *abbrev);
172
173 void
174 proto_register_field_array(int parent, hf_register_info *hf, int num_records);
175
176 proto_item *
177 proto_tree_add_item(proto_tree *tree, int hfindex, gint start,
178         gint length, ...);
179
180 proto_item *
181 proto_tree_add_item_hidden(proto_tree *tree, int hfindex, gint start,
182         gint length, ...);
183
184 proto_item *
185 proto_tree_add_item_format(proto_tree *tree, int hfindex, gint start,
186         gint length, ...);
187
188 proto_item *
189 proto_tree_add_text(proto_tree *tree, gint start, gint length, ...);
190
191 void
192 proto_item_fill_label(field_info *fi, gchar *label_str);
193
194 /* Returns number of items (protocols or header fields) registered. */
195 int proto_registrar_n(void);
196
197 /* Returns char* to name for item # n (0-indexed) */
198 char* proto_registrar_get_name(int n);
199
200 /* Returns char* to abbrev for item # n (0-indexed) */
201 char* proto_registrar_get_abbrev(int n);
202
203 /* Returns enum ftenum for item # n */
204 int proto_registrar_get_ftype(int n);
205
206 /* Returns parent protocol for item # n.
207  * Returns -1 if item _is_ a protocol */
208 int proto_registrar_get_parent(int n);
209
210 /* Is item #n a protocol? */
211 gboolean proto_registrar_is_protocol(int n);
212
213 /* Get length of registered field according to field type.
214  * 0 means undeterminable at registration time.
215  * -1 means unknown field */
216 gint proto_registrar_get_length(int n);
217
218 /* Checks for existence any protocol or field within a tree.
219  * TRUE = found, FALSE = not found */
220 gboolean proto_check_for_protocol_or_field(proto_tree* tree, int id);
221
222 /* Search for a protocol subtree, which can occur more than once, and for each successful
223  * find, call the calback function, passing sinfo as the second argument */
224 void proto_find_protocol_multi(proto_tree* tree, int target, GNodeTraverseFunc callback,
225                         proto_tree_search_info *sinfo);
226
227 /* Just a wrapper to call sinfo->traverse_func() for all nodes in the subtree, with the GNode
228  * and sinfo as the two arguments to sinfo->traverse_func(). Useful when you have to process
229  * all nodes in a subtree. */
230 gboolean proto_get_field_values(proto_tree* subtree, proto_tree_search_info *sinfo);
231
232 /* Dumps a glossary of the protocol and field registrations to STDOUT */
233 void proto_registrar_dump(void);
234
235 /* Is the parsing being done for a visible proto_tree or an invisible one?
236  * By setting this correctly, the proto_tree creation is sped up by not
237  * having to call vsnprintf and copy strings around.
238  */
239 extern gboolean proto_tree_is_visible;
240
241 #endif /* proto.h */