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
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>
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
;
31 val_from_literal(fvalue_t
*fv
, const char *s
, bool allow_partial_value _U_
, char **err_msg
)
37 const char *slash
, *net_str
;
39 char *addr_str_to_free
= NULL
;
41 /* Look for CIDR: Is there a single slash in the string? */
42 slash
= strchr(s
, '/');
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
;
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.",
59 wmem_free(NULL
, 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. */
69 /* Skip past the slash */
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);
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",
85 fv
->value
.ipv4
.nmask
= ws_ipv4_get_subnet_mask(nmask_bits
);
88 /* Not CIDR; mask covers entire address. */
89 fv
->value
.ipv4
.nmask
= ws_ipv4_get_subnet_mask(32);
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
];
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
));
106 repr
= wmem_strdup(scope
, buf
);
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
)
129 *cmp
= addr_a
< addr_b
? -1 : 1;
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
);
142 len(fvalue_t
*fv _U_
)
148 slice(fvalue_t
*fv
, GByteArray
*bytes
, unsigned offset
, unsigned length
)
151 uint32_t addr
= g_htonl(fv
->value
.ipv4
.addr
);
152 data
= ((uint8_t*)&addr
)+offset
;
153 g_byte_array_append(bytes
, data
, length
);
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
);
165 is_zero(const fvalue_t
*fv_a
)
167 return fv_a
->value
.ipv4
.addr
== 0;
171 ftype_register_ipv4(void)
174 static const ftype_t ipv4_type
= {
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 */
196 NULL
, /* cmp_contains */
197 NULL
, /* cmp_matches */
205 NULL
, /* unary_minus */
213 ftype_register(FT_IPv4
, &ipv4_type
);
217 ftype_register_pseudofields_ipv4(int proto
)
219 static int hf_ft_ipv4
;
221 static hf_register_info hf_ftypes
[] = {
223 { "FT_IPv4", "_ws.ftypes.ipv4",
224 FT_IPv4
, BASE_NONE
, NULL
, 0x00,
229 proto_register_field_array(proto
, hf_ftypes
, array_length(hf_ftypes
));
233 * Editor modelines - https://www.wireshark.org/tools/modelines.html
238 * indent-tabs-mode: t
241 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
242 * :indentSize=8:tabSize=8:noTabs=false: