From Jakub Zawadzki :

Add support for prefix in IPv6 address filter.
 
https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=5538

svn path=/trunk/; revision=38916
This commit is contained in:
Anders Broman 2011-09-07 11:03:47 +00:00
parent 3407eb314b
commit d785c79f88
7 changed files with 265 additions and 76 deletions

View File

@ -246,7 +246,7 @@ static void mark_lval_deprecated(const char *s);
[-[:alnum:]_\.]+\/[[:digit:]]+ {
[-[:alnum:]_\.:]+\/[[:digit:]]+ {
/* CIDR */
return set_lval(TOKEN_UNPARSED, yytext);
}

View File

@ -34,6 +34,7 @@ NONGENERATED_C_FILES = \
ftype-double.c \
ftype-integer.c \
ftype-ipv4.c \
ftype-ipv6.c \
ftype-guid.c \
ftype-none.c \
ftype-pcre.c \

View File

@ -150,13 +150,6 @@ ether_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
common_fvalue_set(fv, value, FT_ETHER_LEN);
}
static void
ipv6_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
{
g_assert(!already_copied);
common_fvalue_set(fv, value, FT_IPv6_LEN);
}
static void
oid_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
{
@ -252,35 +245,6 @@ ether_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value, LogFunc
return TRUE;
}
static gboolean
ipv6_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
{
guint8 buffer[16];
if (!get_host_ipaddr6(s, (struct e_in6_addr*)buffer)) {
logfunc("\"%s\" is not a valid hostname or IPv6 address.", s);
return FALSE;
}
ipv6_fvalue_set(fv, buffer, FALSE);
return TRUE;
}
static int
ipv6_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
{
/*
* 39 characters for "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX".
*/
return 39;
}
static void
ipv6_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
{
ip6_to_str_buf((struct e_in6_addr *)fv->value.bytes->data, buf);
}
static gboolean
oid_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
{
@ -610,44 +574,6 @@ ftype_register_bytes(void)
slice,
};
static ftype_t ipv6_type = {
FT_IPv6, /* ftype */
"FT_IPv6", /* name */
"IPv6 address", /* pretty_name */
FT_IPv6_LEN, /* wire_size */
bytes_fvalue_new, /* new_value */
bytes_fvalue_free, /* free_value */
ipv6_from_unparsed, /* val_from_unparsed */
NULL, /* val_from_string */
ipv6_to_repr, /* val_to_string_repr */
ipv6_repr_len, /* len_string_repr */
ipv6_fvalue_set, /* set_value */
NULL, /* set_value_uinteger */
NULL, /* set_value_sinteger */
NULL, /* set_value_integer64 */
NULL, /* set_value_floating */
value_get, /* get_value */
NULL, /* get_value_uinteger */
NULL, /* get_value_sinteger */
NULL, /* get_value_integer64 */
NULL, /* get_value_floating */
cmp_eq,
cmp_ne,
cmp_gt,
cmp_ge,
cmp_lt,
cmp_le,
cmp_bitwise_and,
cmp_contains,
NULL, /* cmp_matches */
len,
slice,
};
static ftype_t oid_type = {
FT_OID, /* ftype */
"FT_OID", /* name */
@ -689,6 +615,5 @@ ftype_register_bytes(void)
ftype_register(FT_BYTES, &bytes_type);
ftype_register(FT_UINT_BYTES, &uint_bytes_type);
ftype_register(FT_ETHER, &ether_type);
ftype_register(FT_IPv6, &ipv6_type);
ftype_register(FT_OID, &oid_type);
}

259
epan/ftypes/ftype-ipv6.c Normal file
View File

@ -0,0 +1,259 @@
/*
* $Id$
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 2001 Gerald Combs
*
* 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.
*
* 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.
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <ftypes-int.h>
#include <epan/ipv6-utils.h>
#include <epan/addr_resolv.h>
#include <epan/emem.h>
static void
ipv6_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
{
g_assert(!already_copied);
memcpy(fv->value.ipv6.addr.bytes, value, FT_IPv6_LEN);
fv->value.ipv6.prefix = 128;
}
static gboolean
ipv6_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
{
char *has_slash, *addr_str;
unsigned int nmask_bits;
fvalue_t *nmask_fvalue;
/* Look for prefix: Is there a single slash in the string? */
if ((has_slash = strchr(s, '/')))
addr_str = ep_strndup(s, has_slash-s);
else
addr_str = s;
if (!get_host_ipaddr6(addr_str, &(fv->value.ipv6.addr))) {
logfunc("\"%s\" is not a valid hostname or IPv6 address.", s);
return FALSE;
}
/* If prefix */
if (has_slash) {
/* XXX - this is inefficient */
nmask_fvalue = fvalue_from_unparsed(FT_UINT32, has_slash+1, FALSE, logfunc);
if (!nmask_fvalue) {
return FALSE;
}
nmask_bits = fvalue_get_uinteger(nmask_fvalue);
FVALUE_FREE(nmask_fvalue);
if (nmask_bits > 128) {
logfunc("Prefix in a IPv6 address should be <= 128, not %u",
nmask_bits);
return FALSE;
}
fv->value.ipv6.prefix = nmask_bits;
} else {
/* Not CIDR; mask covers entire address. */
fv->value.ipv6.prefix = 128;
}
return TRUE;
}
static int
ipv6_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
{
/*
* 39 characters for "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX".
*/
return 39;
}
static void
ipv6_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
{
ip6_to_str_buf(&(fv->value.ipv6.addr), buf);
}
static gpointer
value_get(fvalue_t *fv)
{
return fv->value.ipv6.addr.bytes;
}
static const guint8 bitmasks[9] =
{ 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
static gint
cmp_compare(const fvalue_t *fv_a, const fvalue_t *fv_b)
{
const ipv6_addr *a = &(fv_a->value.ipv6);
const ipv6_addr *b = &(fv_b->value.ipv6);
guint32 prefix;
int pos = 0;
prefix = MIN(a->prefix, b->prefix); /* MIN() like IPv4 */
prefix = MIN(prefix, 128); /* sanitize, max prefix is 128 */
while (prefix >= 8) {
gint byte_a = (gint) (a->addr.bytes[pos]);
gint byte_b = (gint) (b->addr.bytes[pos]);
if (byte_a != byte_b)
return byte_a - byte_b;
prefix -= 8;
pos++;
}
if (prefix != 0) {
gint byte_a = (gint) (a->addr.bytes[pos] & (bitmasks[prefix]));
gint byte_b = (gint) (b->addr.bytes[pos] & (bitmasks[prefix]));
if (byte_a != byte_b)
return byte_a - byte_b;
}
return 0;
}
static gboolean
cmp_eq(fvalue_t *fv_a, fvalue_t *fv_b)
{
return (cmp_compare(fv_a, fv_b) == 0);
}
static gboolean
cmp_ne(fvalue_t *fv_a, fvalue_t *fv_b)
{
return (cmp_compare(fv_a, fv_b) != 0);
}
static gboolean
cmp_gt(fvalue_t *fv_a, fvalue_t *fv_b)
{
return (cmp_compare(fv_a, fv_b) > 0);
}
static gboolean
cmp_ge(fvalue_t *fv_a, fvalue_t *fv_b)
{
return (cmp_compare(fv_a, fv_b) >= 0);
}
static gboolean
cmp_lt(fvalue_t *fv_a, fvalue_t *fv_b)
{
return (cmp_compare(fv_a, fv_b) < 0);
}
static gboolean
cmp_le(fvalue_t *fv_a, fvalue_t *fv_b)
{
return (cmp_compare(fv_a, fv_b) <= 0);
}
static gboolean
cmp_bitwise_and(fvalue_t *fv_a, fvalue_t *fv_b)
{
ipv6_addr *a = &(fv_a->value.ipv6);
ipv6_addr *b = &(fv_b->value.ipv6);
guint32 prefix;
int pos = 0;
prefix = MIN(a->prefix, b->prefix); /* MIN() like in IPv4 */
prefix = MIN(prefix, 128); /* sanitize, max prefix is 128 */
while (prefix >= 8) {
if (a->addr.bytes[pos] & b->addr.bytes[pos])
return TRUE;
prefix -= 8;
pos++;
}
if (prefix != 0) {
guint8 byte_a = (a->addr.bytes[pos] & (bitmasks[prefix]));
guint8 byte_b = (b->addr.bytes[pos] & (bitmasks[prefix]));
if (byte_a & byte_b)
return TRUE;
}
return FALSE;
}
static void
slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
{
/* XXX needed? ipv4 doesn't support slice() */
guint8* data;
data = fv->value.ipv6.addr.bytes + offset;
g_byte_array_append(bytes, data, length);
}
void
ftype_register_ipv6(void)
{
static ftype_t ipv6_type = {
FT_IPv6, /* ftype */
"FT_IPv6", /* name */
"IPv6 address", /* pretty_name */
FT_IPv6_LEN, /* wire_size */
NULL, /* new_value */
NULL, /* free_value */
ipv6_from_unparsed, /* val_from_unparsed */
NULL, /* val_from_string */
ipv6_to_repr, /* val_to_string_repr */
ipv6_repr_len, /* len_string_repr */
ipv6_fvalue_set, /* set_value */
NULL, /* set_value_uinteger */
NULL, /* set_value_sinteger */
NULL, /* set_value_integer64 */
NULL, /* set_value_floating */
value_get, /* get_value */
NULL, /* get_value_uinteger */
NULL, /* get_value_sinteger */
NULL, /* get_value_integer64 */
NULL, /* get_value_floating */
cmp_eq,
cmp_ne,
cmp_gt,
cmp_ge,
cmp_lt,
cmp_le,
cmp_bitwise_and,
NULL, /* XXX, cmp_contains, needed? ipv4 doesn't support it */
NULL, /* cmp_matches */
NULL,
slice,
};
ftype_register(FT_IPv6, &ipv6_type);
}

View File

@ -38,6 +38,7 @@ void ftype_register_bytes(void);
void ftype_register_double(void);
void ftype_register_integers(void);
void ftype_register_ipv4(void);
void ftype_register_ipv6(void);
void ftype_register_guid(void);
void ftype_register_none(void);
void ftype_register_string(void);

View File

@ -45,6 +45,7 @@ ftypes_initialize(void)
ftype_register_double();
ftype_register_integers();
ftype_register_ipv4();
ftype_register_ipv6();
ftype_register_guid();
ftype_register_none();
ftype_register_string();

View File

@ -143,6 +143,7 @@ ftype_can_matches(enum ftenum ftype);
/* ---------------- FVALUE ----------------- */
#include <epan/ipv4.h>
#include <epan/ipv6-utils.h>
#include <epan/guid-utils.h>
#include <epan/tvbuff.h>
@ -161,6 +162,7 @@ typedef struct _fvalue_t {
guchar *ustring;
GByteArray *bytes;
ipv4_addr ipv4;
ipv6_addr ipv6;
e_guid_t guid;
nstime_t time;
tvbuff_t *tvb;