dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / wsutil / inet_cidr.c
blob89777e161cd64c458c6aa06347c5365a943b37c8
1 /* ipv4.c
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "inet_cidr.h"
13 uint32_t
14 ws_ipv4_get_subnet_mask(const uint32_t mask_length)
16 static const uint32_t masks[33] = {
17 0x00000000,
18 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
19 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
20 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
21 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
22 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
23 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
24 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
25 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
28 ws_assert(mask_length <= 32);
30 return masks[mask_length];
33 static int
34 compare_ipv4(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
36 uint32_t addr_a, addr_b, nmask;
38 nmask = MIN(a->nmask, b->nmask);
39 addr_a = a->addr & nmask;
40 addr_b = b->addr & nmask;
41 if (addr_a < addr_b)
42 return -1;
43 if (addr_a > addr_b)
44 return 1;
45 return 0;
48 void
49 ws_ipv4_addr_and_mask_init(ipv4_addr_and_mask *dst, ws_in4_addr src_addr, int src_bits)
51 dst->addr = g_ntohl(src_addr);
52 dst->nmask = ws_ipv4_get_subnet_mask(src_bits);
55 bool
56 ws_ipv4_addr_and_mask_contains(const ipv4_addr_and_mask *ipv4, const ws_in4_addr *in_addr)
58 ipv4_addr_and_mask addr_and_mask;
60 addr_and_mask.addr = g_ntohl(*in_addr);
61 addr_and_mask.nmask = ws_ipv4_get_subnet_mask(32);
62 return compare_ipv4(ipv4, &addr_and_mask) == 0;
65 static const uint8_t bitmasks[9] =
66 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
68 static int
69 compare_ipv6(const ipv6_addr_and_prefix *a, const ipv6_addr_and_prefix *b)
71 uint32_t prefix;
72 int pos = 0;
74 prefix = MIN(a->prefix, b->prefix); /* MIN() like IPv4 */
75 prefix = MIN(prefix, 128); /* sanitize, max prefix is 128 */
77 while (prefix >= 8) {
78 int byte_a = (int) (a->addr.bytes[pos]);
79 int byte_b = (int) (b->addr.bytes[pos]);
81 if (byte_a != byte_b) {
82 return byte_a - byte_b;
85 prefix -= 8;
86 pos++;
89 if (prefix != 0) {
90 int byte_a = (int) (a->addr.bytes[pos] & (bitmasks[prefix]));
91 int byte_b = (int) (b->addr.bytes[pos] & (bitmasks[prefix]));
93 if (byte_a != byte_b) {
94 return byte_a - byte_b;
98 return 0;
102 bool
103 ws_ipv6_addr_and_prefix_contains(const ipv6_addr_and_prefix *ipv6, const ws_in6_addr *in_addr)
105 ipv6_addr_and_prefix addr_and_mask;
107 addr_and_mask.addr = *in_addr;
108 addr_and_mask.prefix = 128;
109 return compare_ipv6(ipv6, &addr_and_mask) == 0;