4 * Ethereal - Network traffic analyzer
5 * By Gerald Combs <gerald@ethereal.com>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <ftypes-int.h>
32 #define CMP_MATCHES cmp_matches
34 #define CMP_MATCHES NULL
37 #define tvb_is_private fvalue_gboolean1
40 value_new(fvalue_t *fv)
43 fv->tvb_is_private = FALSE;
47 value_free(fvalue_t *fv)
49 if (fv->value.tvb && fv->tvb_is_private) {
50 tvb_free_chain(fv->value.tvb);
55 value_set(fvalue_t *fv, gpointer value, gboolean already_copied)
57 g_assert(already_copied);
59 /* Free up the old value, if we have one */
62 fv->value.tvb = value;
66 free_tvb_data(void *data)
72 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
77 /* Free up the old value, if we have one */
80 /* Make a tvbuff from the string. We can drop the
82 private_data = g_memdup(s, strlen(s));
83 new_tvb = tvb_new_real_data(private_data,
84 strlen(s), strlen(s));
86 /* Let the tvbuff know how to delete the data. */
87 tvb_set_free_cb(new_tvb, free_tvb_data);
89 /* And let us know that we need to free the tvbuff */
90 fv->tvb_is_private = TRUE;
91 fv->value.tvb = new_tvb;
96 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
100 guint8 *private_data;
102 /* Free up the old value, if we have one */
105 /* Does this look like a byte string? */
106 fv_bytes = fvalue_from_unparsed(FT_BYTES, s, TRUE, NULL);
108 /* Make a tvbuff from the bytes */
109 private_data = g_memdup(fv_bytes->value.bytes->data,
110 fv_bytes->value.bytes->len);
111 new_tvb = tvb_new_real_data(private_data,
112 fv_bytes->value.bytes->len,
113 fv_bytes->value.bytes->len);
115 /* Let the tvbuff know how to delete the data. */
116 tvb_set_free_cb(new_tvb, free_tvb_data);
118 /* And let us know that we need to free the tvbuff */
119 fv->tvb_is_private = TRUE;
120 fv->value.tvb = new_tvb;
124 /* Treat it as a string. */
125 return val_from_string(fv, s, logfunc);
127 g_assert_not_reached();
131 val_repr_len(fvalue_t *fv, ftrepr_t rtype)
135 g_assert(rtype == FTREPR_DFILTER);
136 length = tvb_length(fv->value.tvb);
137 /* 3 bytes for each byte of the byte "NN:" minus 1 byte
138 * as there's no trailing ":". */
139 return length * 3 - 1;
143 val_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
150 g_assert(rtype == FTREPR_DFILTER);
151 length = tvb_length(fv->value.tvb);
152 c = tvb_get_ptr(fv->value.tvb, 0, length);
155 for (i = 0; i < length; i++) {
157 sprintf(write_cursor, "%02x", *c++);
161 sprintf(write_cursor, ":%02x", *c++);
168 value_get(fvalue_t *fv)
170 return fv->value.tvb;
177 return tvb_length(fv->value.tvb);
183 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
189 data = tvb_get_ptr(fv->value.tvb, offset, length);
190 g_byte_array_append(bytes, data, length);
201 cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
203 if (tvb_find_tvb(fv_a->value.tvb, fv_b->value.tvb, 0) > -1) {
213 cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
215 tvbuff_t *tvb = fv_a->value.tvb;
216 pcre_tuple_t *pcre = fv_b->value.re;
219 const char *data = NULL; /* tvb data */
220 guint32 tvb_len; /* tvb length */
222 /* fv_b is always a FT_PCRE, otherwise the dfilter semcheck() would have
223 * warned us. For the same reason (and because we're using g_malloc()),
224 * fv_b->value.re is not NULL.
226 if (strcmp(fv_b->ftype->name, "FT_PCRE") != 0) {
233 tvb_len = tvb_length(tvb);
234 data = (const char *)tvb_get_ptr(tvb, 0, tvb_len);
236 pcre->re, /* Compiled PCRE */
237 pcre->ex, /* PCRE extra from pcre_study() */
238 data, /* The data to check for the pattern... */
239 tvb_len, /* ... and its length */
240 0, /* Start offset within data */
241 options, /* PCRE options */
242 NULL, /* We are not interested in the matched string */
243 0 /* of the pattern; only in success or failure. */
245 /* NOTE - DO NOT g_free(data) */
259 ftype_register_tvbuff(void)
262 static ftype_t protocol_type = {
263 FT_PROTOCOL, /* ftype */
264 "FT_PROTOCOL", /* name */
265 "protocol", /* pretty_name */
267 value_new, /* new_value */
268 value_free, /* free_value */
269 val_from_unparsed, /* val_from_unparsed */
270 val_from_string, /* val_from_string */
271 val_to_repr, /* val_to_string_repr */
272 val_repr_len, /* len_string_repr */
274 value_set, /* set_value */
275 NULL, /* set_value_integer */
276 NULL, /* set_value_integer64 */
277 NULL, /* set_value_floating */
279 value_get, /* get_value */
280 NULL, /* get_value_integer */
281 NULL, /* get_value_integer64 */
282 NULL, /* get_value_floating */
285 /* TODO - tvb's *can* do 'eq', etc. */
293 NULL, /* cmp_bitwise_and */
303 ftype_register(FT_PROTOCOL, &protocol_type);