regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / ftypes / ftype-ipv4.c
blob3b762cd90ef06036e587ad17b54703cfb99cddb8
1 /*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "config.h"
11 #include <ftypes-int.h>
12 #include <epan/addr_resolv.h>
13 #include <wsutil/bits_count_ones.h>
14 #include <wsutil/strtoi.h>
15 #include <wsutil/inet_cidr.h>
16 #include <wsutil/array.h>
18 static void
19 value_set_ipv4(fvalue_t *fv, const ipv4_addr_and_mask *ipv4)
21 fv->value.ipv4 = *ipv4;
24 static const ipv4_addr_and_mask *
25 value_get_ipv4(fvalue_t *fv)
27 return &fv->value.ipv4;
30 static bool
31 val_from_literal(fvalue_t *fv, const char *s, bool allow_partial_value _U_, char **err_msg)
33 uint32_t addr;
34 uint32_t nmask_bits;
35 const char *endptr;
37 const char *slash, *net_str;
38 const char *addr_str;
39 char *addr_str_to_free = NULL;
41 /* Look for CIDR: Is there a single slash in the string? */
42 slash = strchr(s, '/');
43 if (slash) {
44 /* Make a copy of the string up to but not including the
45 * slash; that's the address portion. */
46 addr_str_to_free = wmem_strndup(NULL, s, slash - s);
47 addr_str = addr_str_to_free;
49 else {
50 addr_str = s;
53 if (!get_host_ipaddr(addr_str, &addr)) {
54 if (err_msg != NULL) {
55 *err_msg = ws_strdup_printf("\"%s\" is not a valid hostname or IPv4 address.",
56 addr_str);
58 if (addr_str_to_free)
59 wmem_free(NULL, addr_str_to_free);
60 return false;
63 if (addr_str_to_free)
64 wmem_free(NULL, addr_str_to_free);
65 fv->value.ipv4.addr = g_ntohl(addr);
67 /* If CIDR, get netmask bits. */
68 if (slash) {
69 /* Skip past the slash */
70 net_str = slash + 1;
72 if(!ws_strtou32(net_str, &endptr, &nmask_bits) || *endptr != '\0') {
73 if (err_msg != NULL) {
74 *err_msg = ws_strdup_printf("%s in not a valid mask", slash+1);
76 return false;
78 if (nmask_bits > 32) {
79 if (err_msg != NULL) {
80 *err_msg = ws_strdup_printf("Netmask bits in a CIDR IPv4 address should be <= 32, not %u",
81 nmask_bits);
83 return false;
85 fv->value.ipv4.nmask = ws_ipv4_get_subnet_mask(nmask_bits);
87 else {
88 /* Not CIDR; mask covers entire address. */
89 fv->value.ipv4.nmask = ws_ipv4_get_subnet_mask(32);
92 return true;
95 static char *
96 val_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_)
98 char buf[WS_INET_ADDRSTRLEN];
99 char *repr;
101 ip_num_to_str_buf(fv->value.ipv4.addr, buf, sizeof(buf));
103 if (fv->value.ipv4.nmask != 0 && fv->value.ipv4.nmask != 0xffffffff)
104 repr = wmem_strdup_printf(scope, "%s/%d", buf, ws_count_ones(fv->value.ipv4.nmask));
105 else
106 repr = wmem_strdup(scope, buf);
108 return repr;
112 /* Compares two ipv4_addr_and_masks, taking into account the less restrictive of the
113 * two netmasks, applying that netmask to both addrs.
115 * So, for example, w.x.y.z/32 eq w.x.y.0/24 is true.
118 static enum ft_result
119 cmp_order(const fvalue_t *fv_a, const fvalue_t *fv_b, int *cmp)
121 uint32_t addr_a, addr_b, nmask;
123 nmask = MIN(fv_a->value.ipv4.nmask, fv_b->value.ipv4.nmask);
124 addr_a = fv_a->value.ipv4.addr & nmask;
125 addr_b = fv_b->value.ipv4.addr & nmask;
126 if (addr_a == addr_b)
127 *cmp = 0;
128 else
129 *cmp = addr_a < addr_b ? -1 : 1;
130 return FT_OK;
133 static enum ft_result
134 bitwise_and(fvalue_t *dst, const fvalue_t *fv_a, const fvalue_t *fv_b, char **err_ptr _U_)
136 dst->value.ipv4 = fv_a->value.ipv4;
137 dst->value.ipv4.addr &= (fv_b->value.ipv4.addr & fv_b->value.ipv4.nmask);
138 return FT_OK;
141 static unsigned
142 len(fvalue_t *fv _U_)
144 return 4;
147 static void
148 slice(fvalue_t *fv, GByteArray *bytes, unsigned offset, unsigned length)
150 uint8_t* data;
151 uint32_t addr = g_htonl(fv->value.ipv4.addr);
152 data = ((uint8_t*)&addr)+offset;
153 g_byte_array_append(bytes, data, length);
156 static unsigned
157 ipv4_hash(const fvalue_t *fv)
159 int64_t val1 = fv->value.ipv4.addr;
160 int64_t val2 = fv->value.ipv4.nmask;
161 return g_int64_hash(&val1) ^ g_int64_hash(&val2);
164 static bool
165 is_zero(const fvalue_t *fv_a)
167 return fv_a->value.ipv4.addr == 0;
170 void
171 ftype_register_ipv4(void)
174 static const ftype_t ipv4_type = {
175 FT_IPv4, /* ftype */
176 4, /* wire_size */
177 NULL, /* new_value */
178 NULL, /* copy_value */
179 NULL, /* free_value */
180 val_from_literal, /* val_from_literal */
181 NULL, /* val_from_string */
182 NULL, /* val_from_charconst */
183 NULL, /* val_from_uinteger64 */
184 NULL, /* val_from_sinteger64 */
185 NULL, /* val_from_double */
186 val_to_repr, /* val_to_string_repr */
188 NULL, /* val_to_uinteger64 */
189 NULL, /* val_to_sinteger64 */
190 NULL, /* val_to_double */
192 { .set_value_ipv4 = value_set_ipv4 }, /* union set_value */
193 { .get_value_ipv4 = value_get_ipv4 }, /* union get_value */
195 cmp_order,
196 NULL, /* cmp_contains */
197 NULL, /* cmp_matches */
199 ipv4_hash,
200 is_zero,
201 NULL,
202 len,
203 (FvalueSlice)slice,
204 bitwise_and,
205 NULL, /* unary_minus */
206 NULL, /* add */
207 NULL, /* subtract */
208 NULL, /* multiply */
209 NULL, /* divide */
210 NULL, /* modulo */
213 ftype_register(FT_IPv4, &ipv4_type);
216 void
217 ftype_register_pseudofields_ipv4(int proto)
219 static int hf_ft_ipv4;
221 static hf_register_info hf_ftypes[] = {
222 { &hf_ft_ipv4,
223 { "FT_IPv4", "_ws.ftypes.ipv4",
224 FT_IPv4, BASE_NONE, NULL, 0x00,
225 NULL, HFILL }
229 proto_register_field_array(proto, hf_ftypes, array_length(hf_ftypes));
233 * Editor modelines - https://www.wireshark.org/tools/modelines.html
235 * Local variables:
236 * c-basic-offset: 8
237 * tab-width: 8
238 * indent-tabs-mode: t
239 * End:
241 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
242 * :indentSize=8:tabSize=8:noTabs=false: