2001-02-01 20:31:21 +00:00
|
|
|
/*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
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
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-02-01 20:31:21 +00:00
|
|
|
*/
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2001-06-22 16:29:15 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
#include <ftypes-int.h>
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/ipv4.h>
|
2004-08-06 19:57:49 +00:00
|
|
|
#include <epan/addr_resolv.h>
|
2006-12-23 00:10:49 +00:00
|
|
|
#include <epan/emem.h>
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-01-18 11:02:26 +00:00
|
|
|
set_uinteger(fvalue_t *fv, guint32 value)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
ipv4_addr_set_net_order_addr(&(fv->value.ipv4), value);
|
|
|
|
ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
value_get(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
return &(fv->value.ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-01-15 01:05:03 +00:00
|
|
|
val_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
guint32 addr;
|
2001-06-22 16:29:15 +00:00
|
|
|
unsigned int nmask_bits;
|
|
|
|
|
2014-01-15 01:05:03 +00:00
|
|
|
const char *has_slash;
|
|
|
|
const char *net_str, *addr_str;
|
2001-06-22 16:29:15 +00:00
|
|
|
fvalue_t *nmask_fvalue;
|
|
|
|
|
|
|
|
/* Look for CIDR: Is there a single slash in the string? */
|
2001-06-23 19:10:27 +00:00
|
|
|
has_slash = strchr(s, '/');
|
2001-06-22 16:29:15 +00:00
|
|
|
if (has_slash) {
|
2012-12-26 05:57:06 +00:00
|
|
|
/* Make a copy of the string up to but not including the
|
|
|
|
* slash; that's the address portion. */
|
|
|
|
addr_str = ep_strndup(s, has_slash - s);
|
2001-06-22 16:29:15 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
addr_str = s;
|
|
|
|
}
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2001-06-22 16:29:15 +00:00
|
|
|
if (!get_host_ipaddr(addr_str, &addr)) {
|
2002-02-05 22:50:17 +00:00
|
|
|
logfunc("\"%s\" is not a valid hostname or IPv4 address.",
|
|
|
|
addr_str);
|
2001-02-01 20:21:25 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-06-22 16:29:15 +00:00
|
|
|
|
2011-04-26 17:13:37 +00:00
|
|
|
ipv4_addr_set_net_order_addr(&(fv->value.ipv4), addr);
|
2001-06-22 16:29:15 +00:00
|
|
|
|
|
|
|
/* If CIDR, get netmask bits. */
|
|
|
|
if (has_slash) {
|
2012-12-26 05:57:06 +00:00
|
|
|
/* Skip past the slash */
|
|
|
|
net_str = has_slash + 1;
|
2001-06-22 16:29:15 +00:00
|
|
|
|
2003-07-25 03:44:05 +00:00
|
|
|
/* XXX - this is inefficient */
|
2003-08-27 15:23:11 +00:00
|
|
|
nmask_fvalue = fvalue_from_unparsed(FT_UINT32, net_str, FALSE, logfunc);
|
2001-06-22 16:29:15 +00:00
|
|
|
if (!nmask_fvalue) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-01-18 11:02:26 +00:00
|
|
|
nmask_bits = fvalue_get_uinteger(nmask_fvalue);
|
2006-12-22 09:01:12 +00:00
|
|
|
FVALUE_FREE(nmask_fvalue);
|
2001-06-22 16:29:15 +00:00
|
|
|
|
|
|
|
if (nmask_bits > 32) {
|
2002-02-05 22:50:17 +00:00
|
|
|
logfunc("Netmask bits in a CIDR IPv4 address should be <= 32, not %u",
|
2001-06-22 16:29:15 +00:00
|
|
|
nmask_bits);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ipv4_addr_set_netmask_bits(&fv->value.ipv4, nmask_bits);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Not CIDR; mask covers entire address. */
|
|
|
|
ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
|
|
|
|
}
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-07-31 04:18:01 +00:00
|
|
|
static int
|
2015-01-04 02:56:16 +00:00
|
|
|
val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
|
2003-07-31 04:18:01 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-10-30 22:55:30 +00:00
|
|
|
* 15 characters for "XXX.XXX.XXX.XXX".
|
2003-07-31 04:18:01 +00:00
|
|
|
*/
|
2005-10-30 22:55:30 +00:00
|
|
|
return 15;
|
2003-07-31 04:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-01-04 02:56:16 +00:00
|
|
|
val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf)
|
2003-07-31 04:18:01 +00:00
|
|
|
{
|
|
|
|
ipv4_addr_str_buf(&fv->value.ipv4, buf);
|
|
|
|
}
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_eq(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return ipv4_addr_eq(&a->value.ipv4, &b->value.ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_ne(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return ipv4_addr_ne(&a->value.ipv4, &b->value.ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_gt(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return ipv4_addr_gt(&a->value.ipv4, &b->value.ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_ge(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return ipv4_addr_ge(&a->value.ipv4, &b->value.ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_lt(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return ipv4_addr_lt(&a->value.ipv4, &b->value.ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_le(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return ipv4_addr_le(&a->value.ipv4, &b->value.ipv4);
|
|
|
|
}
|
|
|
|
|
2007-11-06 09:19:59 +00:00
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_bitwise_and(const fvalue_t *fv_a, const fvalue_t *fv_b)
|
2004-02-27 12:00:32 +00:00
|
|
|
{
|
2007-11-06 09:19:59 +00:00
|
|
|
guint32 addr_a;
|
|
|
|
guint32 addr_b;
|
2004-02-27 12:00:32 +00:00
|
|
|
|
2007-11-06 09:19:59 +00:00
|
|
|
addr_a = fv_a->value.ipv4.addr & fv_a->value.ipv4.nmask;
|
|
|
|
addr_b = fv_b->value.ipv4.addr & fv_b->value.ipv4.nmask;
|
|
|
|
return ((addr_a & addr_b) != 0);
|
2004-02-27 12:00:32 +00:00
|
|
|
}
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
void
|
|
|
|
ftype_register_ipv4(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
static ftype_t ipv4_type = {
|
2006-05-02 14:26:17 +00:00
|
|
|
FT_IPv4, /* ftype */
|
2004-08-22 00:31:58 +00:00
|
|
|
"FT_IPv4", /* name */
|
|
|
|
"IPv4 address", /* pretty_name */
|
|
|
|
4, /* wire_size */
|
|
|
|
NULL, /* new_value */
|
|
|
|
NULL, /* free_value */
|
2003-07-25 03:44:05 +00:00
|
|
|
val_from_unparsed, /* val_from_unparsed */
|
|
|
|
NULL, /* val_from_string */
|
2003-07-31 04:18:01 +00:00
|
|
|
val_to_repr, /* val_to_string_repr */
|
|
|
|
val_repr_len, /* len_string_repr */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2014-01-15 01:05:03 +00:00
|
|
|
NULL, /* set_value_byte_array */
|
|
|
|
NULL, /* set_value_bytes */
|
|
|
|
NULL, /* set_value_guid */
|
|
|
|
NULL, /* set_value_time */
|
|
|
|
NULL, /* set_value_string */
|
|
|
|
NULL, /* set_value_tvbuff */
|
2007-01-18 11:02:26 +00:00
|
|
|
set_uinteger, /* set_value_uinteger */
|
|
|
|
NULL, /* set_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value_integer64 */
|
|
|
|
NULL, /* set_value_floating */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
value_get, /* get_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* get_value_uinteger */
|
|
|
|
NULL, /* get_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* get_value_integer64 */
|
|
|
|
NULL, /* get_value_floating */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
cmp_eq,
|
|
|
|
cmp_ne,
|
|
|
|
cmp_gt,
|
|
|
|
cmp_ge,
|
|
|
|
cmp_lt,
|
|
|
|
cmp_le,
|
2007-11-06 09:19:59 +00:00
|
|
|
cmp_bitwise_and,
|
2003-08-27 15:23:11 +00:00
|
|
|
NULL, /* cmp_contains */
|
2003-12-06 16:35:20 +00:00
|
|
|
NULL, /* cmp_matches */
|
2001-07-13 00:55:58 +00:00
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2001-02-01 20:21:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ftype_register(FT_IPv4, &ipv4_type);
|
|
|
|
}
|