FIXUP: WIP: verification_trailer
[wireshark-wip.git] / epan / ftypes / ftype-ipv4.c
blobf3d59c4bc8818a9ebd4f51b69e87481dbe82a69a
1 /*
2 * $Id$
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <string.h>
27 #include <ftypes-int.h>
28 #include <epan/ipv4.h>
29 #include <epan/addr_resolv.h>
30 #include <epan/emem.h>
33 static void
34 set_uinteger(fvalue_t *fv, guint32 value)
36 ipv4_addr_set_net_order_addr(&(fv->value.ipv4), value);
37 ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
40 static gpointer
41 value_get(fvalue_t *fv)
43 return &(fv->value.ipv4);
46 static gboolean
47 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
49 guint32 addr;
50 unsigned int nmask_bits;
52 char *has_slash;
53 char *net_str, *addr_str;
54 fvalue_t *nmask_fvalue;
56 /* Look for CIDR: Is there a single slash in the string? */
57 has_slash = strchr(s, '/');
58 if (has_slash) {
59 /* Make a copy of the string up to but not including the
60 * slash; that's the address portion. */
61 addr_str = ep_strndup(s, has_slash - s);
63 else {
64 addr_str = s;
67 if (!get_host_ipaddr(addr_str, &addr)) {
68 logfunc("\"%s\" is not a valid hostname or IPv4 address.",
69 addr_str);
70 return FALSE;
73 ipv4_addr_set_net_order_addr(&(fv->value.ipv4), addr);
75 /* If CIDR, get netmask bits. */
76 if (has_slash) {
77 /* Skip past the slash */
78 net_str = has_slash + 1;
80 /* XXX - this is inefficient */
81 nmask_fvalue = fvalue_from_unparsed(FT_UINT32, net_str, FALSE, logfunc);
82 if (!nmask_fvalue) {
83 return FALSE;
85 nmask_bits = fvalue_get_uinteger(nmask_fvalue);
86 FVALUE_FREE(nmask_fvalue);
88 if (nmask_bits > 32) {
89 logfunc("Netmask bits in a CIDR IPv4 address should be <= 32, not %u",
90 nmask_bits);
91 return FALSE;
93 ipv4_addr_set_netmask_bits(&fv->value.ipv4, nmask_bits);
95 else {
96 /* Not CIDR; mask covers entire address. */
97 ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32);
100 return TRUE;
103 static int
104 val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
107 * 15 characters for "XXX.XXX.XXX.XXX".
109 return 15;
112 static void
113 val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
115 ipv4_addr_str_buf(&fv->value.ipv4, buf);
118 static gboolean
119 cmp_eq(const fvalue_t *a, const fvalue_t *b)
121 return ipv4_addr_eq(&a->value.ipv4, &b->value.ipv4);
124 static gboolean
125 cmp_ne(const fvalue_t *a, const fvalue_t *b)
127 return ipv4_addr_ne(&a->value.ipv4, &b->value.ipv4);
130 static gboolean
131 cmp_gt(const fvalue_t *a, const fvalue_t *b)
133 return ipv4_addr_gt(&a->value.ipv4, &b->value.ipv4);
136 static gboolean
137 cmp_ge(const fvalue_t *a, const fvalue_t *b)
139 return ipv4_addr_ge(&a->value.ipv4, &b->value.ipv4);
142 static gboolean
143 cmp_lt(const fvalue_t *a, const fvalue_t *b)
145 return ipv4_addr_lt(&a->value.ipv4, &b->value.ipv4);
148 static gboolean
149 cmp_le(const fvalue_t *a, const fvalue_t *b)
151 return ipv4_addr_le(&a->value.ipv4, &b->value.ipv4);
154 static gboolean
155 cmp_bitwise_and(const fvalue_t *fv_a, const fvalue_t *fv_b)
157 guint32 addr_a;
158 guint32 addr_b;
160 addr_a = fv_a->value.ipv4.addr & fv_a->value.ipv4.nmask;
161 addr_b = fv_b->value.ipv4.addr & fv_b->value.ipv4.nmask;
162 return ((addr_a & addr_b) != 0);
165 void
166 ftype_register_ipv4(void)
169 static ftype_t ipv4_type = {
170 FT_IPv4, /* ftype */
171 "FT_IPv4", /* name */
172 "IPv4 address", /* pretty_name */
173 4, /* wire_size */
174 NULL, /* new_value */
175 NULL, /* free_value */
176 val_from_unparsed, /* val_from_unparsed */
177 NULL, /* val_from_string */
178 val_to_repr, /* val_to_string_repr */
179 val_repr_len, /* len_string_repr */
181 NULL, /* set_value */
182 set_uinteger, /* set_value_uinteger */
183 NULL, /* set_value_sinteger */
184 NULL, /* set_value_integer64 */
185 NULL, /* set_value_floating */
187 value_get, /* get_value */
188 NULL, /* get_value_uinteger */
189 NULL, /* get_value_sinteger */
190 NULL, /* get_value_integer64 */
191 NULL, /* get_value_floating */
193 cmp_eq,
194 cmp_ne,
195 cmp_gt,
196 cmp_ge,
197 cmp_lt,
198 cmp_le,
199 cmp_bitwise_and,
200 NULL, /* cmp_contains */
201 NULL, /* cmp_matches */
203 NULL,
204 NULL,
207 ftype_register(FT_IPv4, &ipv4_type);