2001-02-01 20:31:21 +00:00
|
|
|
/*
|
2004-02-27 12:00:32 +00:00
|
|
|
* $Id: ftype-tvbuff.c,v 1.18 2004/02/27 12:00:32 obiot Exp $
|
2001-02-01 20:31:21 +00:00
|
|
|
*
|
|
|
|
* Ethereal - Network traffic analyzer
|
2002-01-21 07:37:49 +00:00
|
|
|
* By Gerald Combs <gerald@ethereal.com>
|
2001-02-01 20:31:21 +00:00
|
|
|
* Copyright 2001 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-02-01 20:31:21 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-02-01 20:31:21 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-02-01 20:31:21 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ftypes-int.h>
|
2003-08-27 21:11:39 +00:00
|
|
|
#include <string.h>
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/gdebug.h>
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2003-12-17 22:42:02 +00:00
|
|
|
#ifdef HAVE_LIBPCRE
|
|
|
|
#include <pcre.h>
|
|
|
|
#define CMP_MATCHES cmp_matches
|
|
|
|
#else
|
|
|
|
#define CMP_MATCHES NULL
|
|
|
|
#endif
|
|
|
|
|
2003-08-27 15:23:11 +00:00
|
|
|
#define tvb_is_private fvalue_gboolean1
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
static void
|
|
|
|
value_new(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
fv->value.tvb = NULL;
|
2003-08-27 15:23:11 +00:00
|
|
|
fv->tvb_is_private = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
value_free(fvalue_t *fv)
|
|
|
|
{
|
2003-10-29 23:48:14 +00:00
|
|
|
if (fv->value.tvb && fv->tvb_is_private) {
|
2003-08-27 15:23:11 +00:00
|
|
|
tvb_free_chain(fv->value.tvb);
|
|
|
|
}
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
value_set(fvalue_t *fv, gpointer value, gboolean already_copied)
|
|
|
|
{
|
|
|
|
g_assert(already_copied);
|
2003-10-29 23:48:14 +00:00
|
|
|
|
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
value_free(fv);
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
fv->value.tvb = value;
|
|
|
|
}
|
|
|
|
|
2003-08-27 15:23:11 +00:00
|
|
|
static void
|
|
|
|
free_tvb_data(void *data)
|
|
|
|
{
|
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
val_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
|
|
|
|
{
|
|
|
|
tvbuff_t *new_tvb;
|
|
|
|
guint8 *private_data;
|
|
|
|
|
2003-10-29 23:48:14 +00:00
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
value_free(fv);
|
|
|
|
|
2003-08-27 15:23:11 +00:00
|
|
|
/* Make a tvbuff from the string. We can drop the
|
|
|
|
* terminating NUL. */
|
|
|
|
private_data = g_memdup(s, strlen(s));
|
|
|
|
new_tvb = tvb_new_real_data(private_data,
|
|
|
|
strlen(s), strlen(s));
|
|
|
|
|
|
|
|
/* Let the tvbuff know how to delete the data. */
|
|
|
|
tvb_set_free_cb(new_tvb, free_tvb_data);
|
|
|
|
|
|
|
|
/* And let us know that we need to free the tvbuff */
|
|
|
|
fv->tvb_is_private = TRUE;
|
|
|
|
fv->value.tvb = new_tvb;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
|
|
|
|
{
|
|
|
|
fvalue_t *fv_bytes;
|
|
|
|
tvbuff_t *new_tvb;
|
|
|
|
guint8 *private_data;
|
|
|
|
|
2003-10-29 23:48:14 +00:00
|
|
|
/* Free up the old value, if we have one */
|
|
|
|
value_free(fv);
|
|
|
|
|
2003-08-27 15:23:11 +00:00
|
|
|
/* Does this look like a byte string? */
|
|
|
|
fv_bytes = fvalue_from_unparsed(FT_BYTES, s, TRUE, NULL);
|
|
|
|
if (fv_bytes) {
|
|
|
|
/* Make a tvbuff from the bytes */
|
|
|
|
private_data = g_memdup(fv_bytes->value.bytes->data,
|
|
|
|
fv_bytes->value.bytes->len);
|
|
|
|
new_tvb = tvb_new_real_data(private_data,
|
|
|
|
fv_bytes->value.bytes->len,
|
|
|
|
fv_bytes->value.bytes->len);
|
|
|
|
|
|
|
|
/* Let the tvbuff know how to delete the data. */
|
|
|
|
tvb_set_free_cb(new_tvb, free_tvb_data);
|
|
|
|
|
|
|
|
/* And let us know that we need to free the tvbuff */
|
|
|
|
fv->tvb_is_private = TRUE;
|
|
|
|
fv->value.tvb = new_tvb;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Treat it as a string. */
|
|
|
|
return val_from_string(fv, s, logfunc);
|
|
|
|
}
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
static gpointer
|
|
|
|
value_get(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
return fv->value.tvb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
len(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
if (fv->value.tvb)
|
|
|
|
return tvb_length(fv->value.tvb);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
|
|
|
|
{
|
2001-03-13 21:34:28 +00:00
|
|
|
const guint8* data;
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
if (fv->value.tvb) {
|
2001-02-01 23:39:18 +00:00
|
|
|
TRY {
|
|
|
|
data = tvb_get_ptr(fv->value.tvb, offset, length);
|
|
|
|
g_byte_array_append(bytes, data, length);
|
|
|
|
}
|
|
|
|
CATCH_ALL {
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
ENDTRY;
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-27 15:23:11 +00:00
|
|
|
static gboolean
|
|
|
|
cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
|
|
|
|
{
|
|
|
|
if (tvb_find_tvb(fv_a->value.tvb, fv_b->value.tvb, 0) > -1) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:42:02 +00:00
|
|
|
#ifdef HAVE_LIBPCRE
|
|
|
|
static gboolean
|
|
|
|
cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
|
|
|
|
{
|
2003-12-18 13:02:19 +00:00
|
|
|
tvbuff_t *tvb = fv_a->value.tvb;
|
|
|
|
pcre_tuple_t *pcre = fv_b->value.re;
|
2003-12-17 22:42:02 +00:00
|
|
|
int options = 0;
|
|
|
|
int rc;
|
2003-12-18 13:02:19 +00:00
|
|
|
const char *data = NULL; /* tvb data */
|
|
|
|
guint32 tvb_len; /* tvb length */
|
2003-12-17 22:42:02 +00:00
|
|
|
|
|
|
|
/* fv_b is always a FT_PCRE, otherwise the dfilter semcheck() would have
|
|
|
|
* warned us. For the same reason (and because we're using g_malloc()),
|
|
|
|
* fv_b->value.re is not NULL.
|
|
|
|
*/
|
|
|
|
if (strcmp(fv_b->ftype->name, "FT_PCRE") != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-12-18 13:02:19 +00:00
|
|
|
if (! pcre) {
|
2003-12-17 22:42:02 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
TRY {
|
2003-12-18 13:02:19 +00:00
|
|
|
tvb_len = tvb_length(tvb);
|
2004-02-01 22:42:24 +00:00
|
|
|
data = (const char *)tvb_get_ptr(tvb, 0, tvb_len);
|
2003-12-17 22:42:02 +00:00
|
|
|
rc = pcre_exec(
|
2003-12-18 13:02:19 +00:00
|
|
|
pcre->re, /* Compiled PCRE */
|
|
|
|
pcre->ex, /* PCRE extra from pcre_study() */
|
2003-12-17 22:42:02 +00:00
|
|
|
data, /* The data to check for the pattern... */
|
|
|
|
tvb_len, /* ... and its length */
|
|
|
|
0, /* Start offset within data */
|
|
|
|
options, /* PCRE options */
|
|
|
|
NULL, /* We are not interested in the matched string */
|
|
|
|
0 /* of the pattern; only in success or failure. */
|
|
|
|
);
|
|
|
|
/* NOTE - DO NOT g_free(data) */
|
|
|
|
}
|
|
|
|
CATCH_ALL {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ENDTRY;
|
|
|
|
if (rc == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
void
|
|
|
|
ftype_register_tvbuff(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
static ftype_t protocol_type = {
|
2003-08-27 15:23:11 +00:00
|
|
|
"FT_PROTOCOL", /* name */
|
|
|
|
"protocol", /* pretty_name */
|
|
|
|
0, /* wire_size */
|
|
|
|
value_new, /* new_value */
|
|
|
|
value_free, /* free_value */
|
|
|
|
val_from_unparsed, /* val_from_unparsed */
|
|
|
|
val_from_string, /* val_from_string */
|
2003-02-08 04:22:37 +00:00
|
|
|
NULL, /* val_to_string_repr */
|
|
|
|
NULL, /* len_string_repr */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
value_set,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
value_get,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
2004-02-22 03:04:41 +00:00
|
|
|
|
|
|
|
/* TODO - tvb's *can* do 'eq', etc. */
|
|
|
|
|
2003-12-06 16:35:20 +00:00
|
|
|
NULL, /* cmp_eq */
|
|
|
|
NULL, /* cmp_ne */
|
|
|
|
NULL, /* cmp_gt */
|
|
|
|
NULL, /* cmp_ge */
|
|
|
|
NULL, /* cmp_lt */
|
|
|
|
NULL, /* cmp_le */
|
2004-02-27 12:00:32 +00:00
|
|
|
NULL, /* cmp_bitwise_and */
|
2003-12-06 16:35:20 +00:00
|
|
|
cmp_contains,
|
2003-12-17 22:42:02 +00:00
|
|
|
CMP_MATCHES,
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
len,
|
|
|
|
slice,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ftype_register(FT_PROTOCOL, &protocol_type);
|
|
|
|
}
|