2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <ftypes-int.h>
26 #include <epan/ipv4.h>
27 #include <epan/addr_resolv.h>
31 set_uinteger(fvalue_t *fv, guint32 value)
33 ipv4_addr_and_mask_set_net_order_addr(&(fv->value.ipv4), value);
34 ipv4_addr_and_mask_set_netmask_bits(&(fv->value.ipv4), 32);
38 value_get(fvalue_t *fv)
40 return &(fv->value.ipv4);
44 val_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, gchar **err_msg)
47 unsigned int nmask_bits;
49 const char *slash, *net_str;
51 char *addr_str_to_free = NULL;
52 fvalue_t *nmask_fvalue;
54 /* Look for CIDR: Is there a single slash in the string? */
55 slash = strchr(s, '/');
57 /* Make a copy of the string up to but not including the
58 * slash; that's the address portion. */
59 addr_str_to_free = wmem_strndup(NULL, s, slash - s);
60 addr_str = addr_str_to_free;
66 if (!get_host_ipaddr(addr_str, &addr)) {
67 if (err_msg != NULL) {
68 *err_msg = g_strdup_printf("\"%s\" is not a valid hostname or IPv4 address.",
72 wmem_free(NULL, addr_str_to_free);
77 wmem_free(NULL, addr_str_to_free);
78 ipv4_addr_and_mask_set_net_order_addr(&(fv->value.ipv4), addr);
80 /* If CIDR, get netmask bits. */
82 /* Skip past the slash */
85 /* XXX - this is inefficient */
86 nmask_fvalue = fvalue_from_unparsed(FT_UINT32, net_str, FALSE, err_msg);
90 nmask_bits = fvalue_get_uinteger(nmask_fvalue);
91 FVALUE_FREE(nmask_fvalue);
93 if (nmask_bits > 32) {
94 if (err_msg != NULL) {
95 *err_msg = g_strdup_printf("Netmask bits in a CIDR IPv4 address should be <= 32, not %u",
100 ipv4_addr_and_mask_set_netmask_bits(&fv->value.ipv4, nmask_bits);
103 /* Not CIDR; mask covers entire address. */
104 ipv4_addr_and_mask_set_netmask_bits(&(fv->value.ipv4), 32);
111 val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
114 * 15 characters for "XXX.XXX.XXX.XXX".
120 val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf, unsigned int size _U_)
122 ipv4_addr_and_mask_str_buf(&fv->value.ipv4, buf);
126 cmp_eq(const fvalue_t *a, const fvalue_t *b)
128 return ipv4_addr_and_mask_eq(&a->value.ipv4, &b->value.ipv4);
132 cmp_ne(const fvalue_t *a, const fvalue_t *b)
134 return ipv4_addr_and_mask_ne(&a->value.ipv4, &b->value.ipv4);
138 cmp_gt(const fvalue_t *a, const fvalue_t *b)
140 return ipv4_addr_and_mask_gt(&a->value.ipv4, &b->value.ipv4);
144 cmp_ge(const fvalue_t *a, const fvalue_t *b)
146 return ipv4_addr_and_mask_ge(&a->value.ipv4, &b->value.ipv4);
150 cmp_lt(const fvalue_t *a, const fvalue_t *b)
152 return ipv4_addr_and_mask_lt(&a->value.ipv4, &b->value.ipv4);
156 cmp_le(const fvalue_t *a, const fvalue_t *b)
158 return ipv4_addr_and_mask_le(&a->value.ipv4, &b->value.ipv4);
162 cmp_bitwise_and(const fvalue_t *fv_a, const fvalue_t *fv_b)
167 addr_a = fv_a->value.ipv4.addr & fv_a->value.ipv4.nmask;
168 addr_b = fv_b->value.ipv4.addr & fv_b->value.ipv4.nmask;
169 return ((addr_a & addr_b) != 0);
173 ftype_register_ipv4(void)
176 static ftype_t ipv4_type = {
178 "FT_IPv4", /* name */
179 "IPv4 address", /* pretty_name */
181 NULL, /* new_value */
182 NULL, /* free_value */
183 val_from_unparsed, /* val_from_unparsed */
184 NULL, /* val_from_string */
185 val_to_repr, /* val_to_string_repr */
186 val_repr_len, /* len_string_repr */
188 { .set_value_uinteger = set_uinteger }, /* union set_value */
191 value_get, /* get_value */
192 NULL, /* get_value_uinteger */
193 NULL, /* get_value_sinteger */
194 NULL, /* get_value_uinteger64 */
195 NULL, /* get_value_sinteger64 */
196 NULL, /* get_value_floating */
205 NULL, /* cmp_contains */
206 NULL, /* cmp_matches */
212 ftype_register(FT_IPv4, &ipv4_type);
216 * Editor modelines - http://www.wireshark.org/tools/modelines.html
221 * indent-tabs-mode: t
224 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
225 * :indentSize=8:tabSize=8:noTabs=false: