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