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