ca3722f5d6dcd825533bb8c8c7463162deb96aba
[obnox/wireshark/wip.git] / epan / ftypes / ftypes.h
1 /* ftypes.h
2  * Definitions for field types
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 2001 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 FTYPES_H
27 #define FTYPES_H
28
29 #include <glib.h>
30 #include "../slab.h"
31
32 /* field types */
33 enum ftenum {
34         FT_NONE,        /* used for text labels with no value */
35         FT_PROTOCOL,
36         FT_BOOLEAN,     /* TRUE and FALSE come from <glib.h> */
37         FT_UINT8,
38         FT_UINT16,
39         FT_UINT24,      /* really a UINT32, but displayed as 3 hex-digits if FD_HEX*/
40         FT_UINT32,
41         FT_UINT64,
42         FT_INT8,
43         FT_INT16,
44         FT_INT24,       /* same as for UINT24 */
45         FT_INT32,
46         FT_INT64,
47         FT_FLOAT,
48         FT_DOUBLE,
49         FT_ABSOLUTE_TIME,
50         FT_RELATIVE_TIME,
51         FT_STRING,
52         FT_STRINGZ,     /* for use with proto_tree_add_item() */
53         FT_EBCDIC,      /* for use with proto_tree_add_item() */
54         FT_UINT_STRING, /* for use with proto_tree_add_item() */
55         /*FT_UCS2_LE, */    /* Unicode, 2 byte, Little Endian     */
56         FT_ETHER,
57         FT_BYTES,
58         FT_UINT_BYTES,
59         FT_IPv4,
60         FT_IPv6,
61         FT_IPXNET,
62         FT_FRAMENUM,    /* a UINT32, but if selected lets you go to frame with that number */
63         FT_PCRE,        /* a compiled Perl-Compatible Regular Expression object */
64         FT_GUID,        /* GUID, UUID */
65         FT_OID,         /* OBJECT IDENTIFIER */
66         FT_NUM_TYPES /* last item number plus one */
67 };
68
69 #define IS_FT_INT(ft)    ((ft)==FT_INT8||(ft)==FT_INT16||(ft)==FT_INT24||(ft)==FT_INT32||(ft)==FT_INT64)
70 #define IS_FT_UINT(ft)   ((ft)==FT_UINT8||(ft)==FT_UINT16||(ft)==FT_UINT24||(ft)==FT_UINT32||(ft)==FT_UINT64||(ft)==FT_FRAMENUM)
71 #define IS_FT_TIME(ft)   ((ft)==FT_ABSOLUTE_TIME||(ft)==FT_RELATIVE_TIME)
72 #define IS_FT_STRING(ft) ((ft)==FT_STRING||(ft)==FT_STRINGZ)
73
74 /* field types lengths */
75 #define FT_ETHER_LEN        6
76 #define FT_GUID_LEN         16
77 #define FT_IPv4_LEN         4
78 #define FT_IPv6_LEN         16
79 #define FT_IPXNET_LEN       4
80
81 typedef enum ftenum ftenum_t;
82 typedef struct _ftype_t ftype_t;
83
84 /* String representation types. */
85 enum ftrepr {
86         FTREPR_DISPLAY,
87         FTREPR_DFILTER
88 };
89
90 typedef enum ftrepr ftrepr_t;
91
92 #ifdef HAVE_LIBPCRE
93 typedef struct _pcre_tuple_t pcre_tuple_t;
94 #endif /* HAVE_LIBPCRE */
95
96 /* Initialize the ftypes subsytem. Called once. */
97 void
98 ftypes_initialize(void);
99
100 /* ---------------- FTYPE ----------------- */
101
102 /* Return a string representing the name of the type */
103 const char*
104 ftype_name(ftenum_t ftype);
105
106 /* Return a string presenting a "pretty" representation of the
107  * name of the type. The pretty name means more to the user than
108  * that "FT_*" name. */
109 const char*
110 ftype_pretty_name(ftenum_t ftype);
111
112 /* Returns length of field in packet, or 0 if not determinable/defined. */
113 int
114 ftype_length(ftenum_t ftype);
115
116 gboolean
117 ftype_can_slice(enum ftenum ftype);
118
119 gboolean
120 ftype_can_eq(enum ftenum ftype);
121
122 gboolean
123 ftype_can_ne(enum ftenum ftype);
124
125 gboolean
126 ftype_can_gt(enum ftenum ftype);
127
128 gboolean
129 ftype_can_ge(enum ftenum ftype);
130
131 gboolean
132 ftype_can_lt(enum ftenum ftype);
133
134 gboolean
135 ftype_can_le(enum ftenum ftype);
136
137 gboolean
138 ftype_can_bitwise_and(enum ftenum ftype);
139
140 gboolean
141 ftype_can_contains(enum ftenum ftype);
142
143 gboolean
144 ftype_can_matches(enum ftenum ftype);
145
146 /* ---------------- FVALUE ----------------- */
147
148 #include <epan/ipv4.h>
149 #include <epan/guid-utils.h>
150
151 #include <epan/tvbuff.h>
152 #include <epan/nstime.h>
153 #include <epan/dfilter/drange.h>
154
155 typedef struct _fvalue_t {
156         ftype_t *ftype;
157         union {
158                 /* Put a few basic types in here */
159                 guint32         uinteger;
160                 gint32          sinteger;
161                 guint64         integer64;
162                 gdouble         floating;
163                 gchar           *string;
164                 guchar          *ustring;
165                 GByteArray      *bytes;
166                 ipv4_addr       ipv4;
167                 e_guid_t        guid;
168                 nstime_t        time;
169                 tvbuff_t        *tvb;
170 #ifdef HAVE_LIBPCRE
171                 pcre_tuple_t    *re;
172 #elif GLIB_CHECK_VERSION(2,14,0) /* Try falling back to GRegex. */
173                 GRegex          *re;
174 #endif /* HAVE_LIBPCRE */
175         } value;
176
177         /* The following is provided for private use
178          * by the fvalue. */
179         gboolean        fvalue_gboolean1;
180
181 } fvalue_t;
182
183 typedef void (*FvalueNewFunc)(fvalue_t*);
184 typedef void (*FvalueFreeFunc)(fvalue_t*);
185 typedef void (*LogFunc)(const char*,...);
186
187 typedef gboolean (*FvalueFromUnparsed)(fvalue_t*, char*, gboolean, LogFunc);
188 typedef gboolean (*FvalueFromString)(fvalue_t*, char*, LogFunc);
189 typedef void (*FvalueToStringRepr)(fvalue_t*, ftrepr_t, char*);
190 typedef int (*FvalueStringReprLen)(fvalue_t*, ftrepr_t);
191
192 typedef void (*FvalueSetFunc)(fvalue_t*, gpointer, gboolean);
193 typedef void (*FvalueSetUnsignedIntegerFunc)(fvalue_t*, guint32);
194 typedef void (*FvalueSetSignedIntegerFunc)(fvalue_t*, gint32);
195 typedef void (*FvalueSetInteger64Func)(fvalue_t*, guint64);
196 typedef void (*FvalueSetFloatingFunc)(fvalue_t*, gdouble);
197
198 typedef gpointer (*FvalueGetFunc)(fvalue_t*);
199 typedef guint32 (*FvalueGetUnsignedIntegerFunc)(fvalue_t*);
200 typedef gint32  (*FvalueGetSignedIntegerFunc)(fvalue_t*);
201 typedef guint64 (*FvalueGetInteger64Func)(fvalue_t*);
202 typedef double (*FvalueGetFloatingFunc)(fvalue_t*);
203
204 typedef gboolean (*FvalueCmp)(fvalue_t*, fvalue_t*);
205
206 typedef guint (*FvalueLen)(fvalue_t*);
207 typedef void (*FvalueSlice)(fvalue_t*, GByteArray *, guint offset, guint length);
208
209 struct _ftype_t {
210         ftenum_t                ftype;
211         const char              *name;
212         const char              *pretty_name;
213         int                     wire_size;
214         FvalueNewFunc           new_value;
215         FvalueFreeFunc          free_value;
216         FvalueFromUnparsed      val_from_unparsed;
217         FvalueFromString        val_from_string;
218         FvalueToStringRepr      val_to_string_repr;
219         FvalueStringReprLen     len_string_repr;
220
221         /* could be union */
222         FvalueSetFunc           set_value;
223         FvalueSetUnsignedIntegerFunc    set_value_uinteger;
224         FvalueSetSignedIntegerFunc              set_value_sinteger;
225         FvalueSetInteger64Func  set_value_integer64;
226         FvalueSetFloatingFunc   set_value_floating;
227
228         /* could be union */
229         FvalueGetFunc           get_value;
230         FvalueGetUnsignedIntegerFunc    get_value_uinteger;
231         FvalueGetSignedIntegerFunc              get_value_sinteger;
232         FvalueGetInteger64Func  get_value_integer64;
233         FvalueGetFloatingFunc   get_value_floating;
234
235         FvalueCmp               cmp_eq;
236         FvalueCmp               cmp_ne;
237         FvalueCmp               cmp_gt;
238         FvalueCmp               cmp_ge;
239         FvalueCmp               cmp_lt;
240         FvalueCmp               cmp_le;
241         FvalueCmp               cmp_bitwise_and;
242         FvalueCmp               cmp_contains;
243         FvalueCmp               cmp_matches;
244
245         FvalueLen               len;
246         FvalueSlice             slice;
247 };
248
249
250 fvalue_t*
251 fvalue_new(ftenum_t ftype);
252
253 void
254 fvalue_init(fvalue_t *fv, ftenum_t ftype);
255
256
257 /* Define type needed for the fvalue_t free list. */
258 SLAB_ITEM_TYPE_DEFINE(fvalue_t)
259
260 /* Free all memory used by an fvalue_t. With MSVC and a
261  * libwireshark.dll, we need a special declaration.
262  */
263 WS_VAR_IMPORT SLAB_FREE_LIST_DECLARE(fvalue_t)
264
265
266 #define FVALUE_CLEANUP(fv)                                      \
267         {                                                       \
268                 register FvalueFreeFunc free_value;             \
269                 free_value = (fv)->ftype->free_value;   \
270                 if (free_value) {                               \
271                         free_value((fv));                       \
272                 }                                               \
273         }
274
275 #define FVALUE_FREE(fv)                                         \
276         {                                                       \
277                 FVALUE_CLEANUP(fv)                              \
278                 SLAB_FREE(fv, fvalue_t);                        \
279         }
280
281
282 fvalue_t*
283 fvalue_from_unparsed(ftenum_t ftype, char *s, gboolean allow_partial_value, LogFunc logfunc);
284
285 fvalue_t*
286 fvalue_from_string(ftenum_t ftype, char *s, LogFunc logfunc);
287
288 /* Returns the length of the string required to hold the
289  * string representation of the the field value.
290  *
291  * Returns -1 if the string cannot be represented in the given rtype.
292  *
293  * The length DOES NOT include the terminating NUL. */
294 int
295 fvalue_string_repr_len(fvalue_t *fv, ftrepr_t rtype);
296
297 /* Creates the string representation of the field value.
298  * If given non-NULL 'buf', the string is written at the memory
299  * location pointed to by 'buf'. If 'buf' is NULL, new memory
300  * is malloc'ed and the string representation is written there.
301  * The pointer to the beginning of the string representation is
302  * returned. If 'buf' was NULL, this points to the newly-allocated
303  * memory. if 'buf' was non-NULL, then the return value will be
304  * 'buf'.
305  *
306  * Returns NULL if the string cannot be represented in the given rtype.*/
307 extern char *
308 fvalue_to_string_repr(fvalue_t *fv, ftrepr_t rtype, char *buf);
309
310 ftype_t*
311 fvalue_ftype(fvalue_t *fv);
312
313 const char*
314 fvalue_type_name(fvalue_t *fv);
315
316 void
317 fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied);
318
319 void
320 fvalue_set_uinteger(fvalue_t *fv, guint32 value);
321
322 void
323 fvalue_set_sinteger(fvalue_t *fv, gint32 value);
324
325 void
326 fvalue_set_integer64(fvalue_t *fv, guint64 value);
327
328 void
329 fvalue_set_floating(fvalue_t *fv, gdouble value);
330
331 gpointer
332 fvalue_get(fvalue_t *fv);
333
334 extern guint32
335 fvalue_get_uinteger(fvalue_t *fv);
336
337 extern gint32
338 fvalue_get_sinteger(fvalue_t *fv);
339
340 guint64
341 fvalue_get_integer64(fvalue_t *fv);
342
343 extern double
344 fvalue_get_floating(fvalue_t *fv);
345
346 gboolean
347 fvalue_eq(fvalue_t *a, fvalue_t *b);
348
349 gboolean
350 fvalue_ne(fvalue_t *a, fvalue_t *b);
351
352 gboolean
353 fvalue_gt(fvalue_t *a, fvalue_t *b);
354
355 gboolean
356 fvalue_ge(fvalue_t *a, fvalue_t *b);
357
358 gboolean
359 fvalue_lt(fvalue_t *a, fvalue_t *b);
360
361 gboolean
362 fvalue_le(fvalue_t *a, fvalue_t *b);
363
364 gboolean
365 fvalue_bitwise_and(fvalue_t *a, fvalue_t *b);
366
367 gboolean
368 fvalue_contains(fvalue_t *a, fvalue_t *b);
369
370 gboolean
371 fvalue_matches(fvalue_t *a, fvalue_t *b);
372
373 guint
374 fvalue_length(fvalue_t *fv);
375
376 fvalue_t*
377 fvalue_slice(fvalue_t *fv, drange *dr);
378
379 #endif /* ftypes.h */