2 * $Id: ftype-ipv4.c,v 1.8 2002/02/05 22:50:17 guy Exp $
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.
29 #include <ftypes-int.h>
30 #include <epan/ipv4.h>
31 #include <epan/resolv.h>
35 set_integer(fvalue_t *fv, guint32 value)
37 ipv4_addr_set_net_order_addr(&(fv->value.ipv4), value);
38 ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
42 value_get(fvalue_t *fv)
44 return &(fv->value.ipv4);
48 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc)
51 unsigned int nmask_bits;
53 char *has_slash, *s_copy = NULL;
54 char *net_str, *addr_str;
55 fvalue_t *nmask_fvalue;
57 /* Look for CIDR: Is there a single slash in the string? */
58 has_slash = strchr(s, '/');
60 /* Make a copy of the string and use strtok() to
61 * get the address portion. */
63 addr_str = strtok(s_copy, "/");
65 /* I just checked for slash! I shouldn't get NULL here.
66 * Double check just in case. */
68 logfunc("Unexpected strtok() error parsing IP address: %s",
78 if (!get_host_ipaddr(addr_str, &addr)) {
79 logfunc("\"%s\" is not a valid hostname or IPv4 address.",
87 ipv4_addr_set_host_order_addr(&(fv->value.ipv4), addr);
89 /* If CIDR, get netmask bits. */
91 net_str = strtok(NULL, "/");
92 /* I checked for slash! I shouldn't get NULL here.
93 * Double check just in case. */
95 logfunc("Unexpected strtok() error parsing netmask: %s",
101 nmask_fvalue = fvalue_from_string(FT_UINT32, net_str, logfunc);
106 nmask_bits = fvalue_get_integer(nmask_fvalue);
107 fvalue_free(nmask_fvalue);
109 if (nmask_bits > 32) {
110 logfunc("Netmask bits in a CIDR IPv4 address should be <= 32, not %u",
114 ipv4_addr_set_netmask_bits(&fv->value.ipv4, nmask_bits);
117 /* Not CIDR; mask covers entire address. */
118 ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
125 cmp_eq(fvalue_t *a, fvalue_t *b)
127 return ipv4_addr_eq(&a->value.ipv4, &b->value.ipv4);
131 cmp_ne(fvalue_t *a, fvalue_t *b)
133 return ipv4_addr_ne(&a->value.ipv4, &b->value.ipv4);
137 cmp_gt(fvalue_t *a, fvalue_t *b)
139 return ipv4_addr_gt(&a->value.ipv4, &b->value.ipv4);
143 cmp_ge(fvalue_t *a, fvalue_t *b)
145 return ipv4_addr_ge(&a->value.ipv4, &b->value.ipv4);
149 cmp_lt(fvalue_t *a, fvalue_t *b)
151 return ipv4_addr_lt(&a->value.ipv4, &b->value.ipv4);
155 cmp_le(fvalue_t *a, fvalue_t *b)
157 return ipv4_addr_le(&a->value.ipv4, &b->value.ipv4);
161 ftype_register_ipv4(void)
164 static ftype_t ipv4_type = {
191 ftype_register(FT_IPv4, &ipv4_type);