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.
27 #include <ftypes-int.h>
28 #include <epan/ipv4.h>
29 #include <epan/addr_resolv.h>
30 #include <epan/emem.h>
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);
41 value_get(fvalue_t
*fv
)
43 return &(fv
->value
.ipv4
);
47 val_from_unparsed(fvalue_t
*fv
, char *s
, gboolean allow_partial_value _U_
, LogFunc logfunc
)
50 unsigned int nmask_bits
;
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
, '/');
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
);
67 if (!get_host_ipaddr(addr_str
, &addr
)) {
68 logfunc("\"%s\" is not a valid hostname or IPv4 address.",
73 ipv4_addr_set_net_order_addr(&(fv
->value
.ipv4
), addr
);
75 /* If CIDR, get netmask bits. */
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
);
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",
93 ipv4_addr_set_netmask_bits(&fv
->value
.ipv4
, nmask_bits
);
96 /* Not CIDR; mask covers entire address. */
97 ipv4_addr_set_netmask_bits(&(fv
->value
.ipv4
), 32);
104 val_repr_len(fvalue_t
*fv _U_
, ftrepr_t rtype _U_
)
107 * 15 characters for "XXX.XXX.XXX.XXX".
113 val_to_repr(fvalue_t
*fv
, ftrepr_t rtype _U_
, char *buf
)
115 ipv4_addr_str_buf(&fv
->value
.ipv4
, buf
);
119 cmp_eq(const fvalue_t
*a
, const fvalue_t
*b
)
121 return ipv4_addr_eq(&a
->value
.ipv4
, &b
->value
.ipv4
);
125 cmp_ne(const fvalue_t
*a
, const fvalue_t
*b
)
127 return ipv4_addr_ne(&a
->value
.ipv4
, &b
->value
.ipv4
);
131 cmp_gt(const fvalue_t
*a
, const fvalue_t
*b
)
133 return ipv4_addr_gt(&a
->value
.ipv4
, &b
->value
.ipv4
);
137 cmp_ge(const fvalue_t
*a
, const fvalue_t
*b
)
139 return ipv4_addr_ge(&a
->value
.ipv4
, &b
->value
.ipv4
);
143 cmp_lt(const fvalue_t
*a
, const fvalue_t
*b
)
145 return ipv4_addr_lt(&a
->value
.ipv4
, &b
->value
.ipv4
);
149 cmp_le(const fvalue_t
*a
, const fvalue_t
*b
)
151 return ipv4_addr_le(&a
->value
.ipv4
, &b
->value
.ipv4
);
155 cmp_bitwise_and(const fvalue_t
*fv_a
, const fvalue_t
*fv_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);
166 ftype_register_ipv4(void)
169 static ftype_t ipv4_type
= {
171 "FT_IPv4", /* name */
172 "IPv4 address", /* pretty_name */
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 */
200 NULL
, /* cmp_contains */
201 NULL
, /* cmp_matches */
207 ftype_register(FT_IPv4
, &ipv4_type
);