ecs.c revision 1.1.2.2 1 1.1.2.2 martin /* $NetBSD: ecs.c,v 1.1.2.2 2024/02/24 13:06:57 martin Exp $ */
2 1.1.2.2 martin
3 1.1.2.2 martin /*
4 1.1.2.2 martin * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1.2.2 martin *
6 1.1.2.2 martin * SPDX-License-Identifier: MPL-2.0
7 1.1.2.2 martin *
8 1.1.2.2 martin * This Source Code Form is subject to the terms of the Mozilla Public
9 1.1.2.2 martin * License, v. 2.0. If a copy of the MPL was not distributed with this
10 1.1.2.2 martin * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 1.1.2.2 martin *
12 1.1.2.2 martin * See the COPYRIGHT file distributed with this work for additional
13 1.1.2.2 martin * information regarding copyright ownership.
14 1.1.2.2 martin */
15 1.1.2.2 martin
16 1.1.2.2 martin /*! \file */
17 1.1.2.2 martin
18 1.1.2.2 martin #include <string.h>
19 1.1.2.2 martin
20 1.1.2.2 martin #include <isc/buffer.h>
21 1.1.2.2 martin #include <isc/mem.h>
22 1.1.2.2 martin #include <isc/netaddr.h>
23 1.1.2.2 martin #include <isc/print.h>
24 1.1.2.2 martin #include <isc/util.h>
25 1.1.2.2 martin
26 1.1.2.2 martin #include <dns/ecs.h>
27 1.1.2.2 martin #include <dns/nsec.h>
28 1.1.2.2 martin #include <dns/rbt.h>
29 1.1.2.2 martin #include <dns/rdata.h>
30 1.1.2.2 martin #include <dns/rdatatype.h>
31 1.1.2.2 martin #include <dns/result.h>
32 1.1.2.2 martin #include <dns/types.h>
33 1.1.2.2 martin
34 1.1.2.2 martin void
35 1.1.2.2 martin dns_ecs_init(dns_ecs_t *ecs) {
36 1.1.2.2 martin isc_netaddr_unspec(&ecs->addr);
37 1.1.2.2 martin ecs->source = 0;
38 1.1.2.2 martin ecs->scope = 0xff;
39 1.1.2.2 martin }
40 1.1.2.2 martin
41 1.1.2.2 martin bool
42 1.1.2.2 martin dns_ecs_equals(const dns_ecs_t *ecs1, const dns_ecs_t *ecs2) {
43 1.1.2.2 martin const unsigned char *addr1, *addr2;
44 1.1.2.2 martin uint8_t mask;
45 1.1.2.2 martin size_t alen;
46 1.1.2.2 martin
47 1.1.2.2 martin REQUIRE(ecs1 != NULL && ecs2 != NULL);
48 1.1.2.2 martin
49 1.1.2.2 martin if (ecs1->source != ecs2->source ||
50 1.1.2.2 martin ecs1->addr.family != ecs2->addr.family)
51 1.1.2.2 martin {
52 1.1.2.2 martin return (false);
53 1.1.2.2 martin }
54 1.1.2.2 martin
55 1.1.2.2 martin alen = (ecs1->source + 7) / 8;
56 1.1.2.2 martin if (alen == 0) {
57 1.1.2.2 martin return (true);
58 1.1.2.2 martin }
59 1.1.2.2 martin
60 1.1.2.2 martin switch (ecs1->addr.family) {
61 1.1.2.2 martin case AF_INET:
62 1.1.2.2 martin INSIST(alen <= 4);
63 1.1.2.2 martin addr1 = (const unsigned char *)&ecs1->addr.type.in;
64 1.1.2.2 martin addr2 = (const unsigned char *)&ecs2->addr.type.in;
65 1.1.2.2 martin break;
66 1.1.2.2 martin case AF_INET6:
67 1.1.2.2 martin INSIST(alen <= 16);
68 1.1.2.2 martin addr1 = (const unsigned char *)&ecs1->addr.type.in6;
69 1.1.2.2 martin addr2 = (const unsigned char *)&ecs2->addr.type.in6;
70 1.1.2.2 martin break;
71 1.1.2.2 martin default:
72 1.1.2.2 martin UNREACHABLE();
73 1.1.2.2 martin }
74 1.1.2.2 martin
75 1.1.2.2 martin /*
76 1.1.2.2 martin * Compare all octets except the final octet of the address
77 1.1.2.2 martin * prefix.
78 1.1.2.2 martin */
79 1.1.2.2 martin if (alen > 1 && memcmp(addr1, addr2, alen - 1) != 0) {
80 1.1.2.2 martin return (false);
81 1.1.2.2 martin }
82 1.1.2.2 martin
83 1.1.2.2 martin /*
84 1.1.2.2 martin * It should not be necessary to mask the final octet; all
85 1.1.2.2 martin * bits past the source prefix length are supposed to be 0.
86 1.1.2.2 martin * However, it seems prudent not to omit them from the
87 1.1.2.2 martin * comparison anyway.
88 1.1.2.2 martin */
89 1.1.2.2 martin mask = (~0U << (8 - (ecs1->source % 8))) & 0xff;
90 1.1.2.2 martin if (mask == 0) {
91 1.1.2.2 martin mask = 0xff;
92 1.1.2.2 martin }
93 1.1.2.2 martin
94 1.1.2.2 martin if ((addr1[alen - 1] & mask) != (addr2[alen - 1] & mask)) {
95 1.1.2.2 martin return (false);
96 1.1.2.2 martin }
97 1.1.2.2 martin
98 1.1.2.2 martin return (true);
99 1.1.2.2 martin }
100 1.1.2.2 martin
101 1.1.2.2 martin void
102 1.1.2.2 martin dns_ecs_format(const dns_ecs_t *ecs, char *buf, size_t size) {
103 1.1.2.2 martin size_t len;
104 1.1.2.2 martin char *p;
105 1.1.2.2 martin
106 1.1.2.2 martin REQUIRE(ecs != NULL);
107 1.1.2.2 martin REQUIRE(buf != NULL);
108 1.1.2.2 martin REQUIRE(size >= DNS_ECS_FORMATSIZE);
109 1.1.2.2 martin
110 1.1.2.2 martin isc_netaddr_format(&ecs->addr, buf, size);
111 1.1.2.2 martin len = strlen(buf);
112 1.1.2.2 martin p = buf + len;
113 1.1.2.2 martin snprintf(p, size - len, "/%d/%d", ecs->source,
114 1.1.2.2 martin ecs->scope == 0xff ? 0 : ecs->scope);
115 1.1.2.2 martin }
116