1174294Sobrien/*	$NetBSD: ecs.c,v 1.1 2024/02/18 20:57:31 christos Exp $	*/
2174294Sobrien
3174294Sobrien/*
4174294Sobrien * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5174294Sobrien *
6174294Sobrien * SPDX-License-Identifier: MPL-2.0
7174294Sobrien *
8174294Sobrien * This Source Code Form is subject to the terms of the Mozilla Public
9174294Sobrien * License, v. 2.0. If a copy of the MPL was not distributed with this
10174294Sobrien * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11174294Sobrien *
12174294Sobrien * See the COPYRIGHT file distributed with this work for additional
13174294Sobrien * information regarding copyright ownership.
14174294Sobrien */
15174294Sobrien
16174294Sobrien/*! \file */
17174294Sobrien
18174294Sobrien#include <string.h>
19174294Sobrien
20174294Sobrien#include <isc/buffer.h>
21174294Sobrien#include <isc/mem.h>
22174294Sobrien#include <isc/netaddr.h>
23174294Sobrien#include <isc/print.h>
24174294Sobrien#include <isc/util.h>
25174294Sobrien
26174294Sobrien#include <dns/ecs.h>
27174294Sobrien#include <dns/nsec.h>
28174294Sobrien#include <dns/rbt.h>
29174294Sobrien#include <dns/rdata.h>
30174294Sobrien#include <dns/rdatatype.h>
31174294Sobrien#include <dns/result.h>
32174294Sobrien#include <dns/types.h>
33174294Sobrien
34174294Sobrienvoid
35174294Sobriendns_ecs_init(dns_ecs_t *ecs) {
36174294Sobrien	isc_netaddr_unspec(&ecs->addr);
37174294Sobrien	ecs->source = 0;
38174294Sobrien	ecs->scope = 0xff;
39174294Sobrien}
40174294Sobrien
41174294Sobrienbool
42174294Sobriendns_ecs_equals(const dns_ecs_t *ecs1, const dns_ecs_t *ecs2) {
43174294Sobrien	const unsigned char *addr1, *addr2;
44174294Sobrien	uint8_t mask;
45174294Sobrien	size_t alen;
46174294Sobrien
47174294Sobrien	REQUIRE(ecs1 != NULL && ecs2 != NULL);
48174294Sobrien
49174294Sobrien	if (ecs1->source != ecs2->source ||
50174294Sobrien	    ecs1->addr.family != ecs2->addr.family)
51174294Sobrien	{
52174294Sobrien		return (false);
53174294Sobrien	}
54174294Sobrien
55174294Sobrien	alen = (ecs1->source + 7) / 8;
56174294Sobrien	if (alen == 0) {
57174294Sobrien		return (true);
58174294Sobrien	}
59174294Sobrien
60174294Sobrien	switch (ecs1->addr.family) {
61174294Sobrien	case AF_INET:
62174294Sobrien		INSIST(alen <= 4);
63174294Sobrien		addr1 = (const unsigned char *)&ecs1->addr.type.in;
64174294Sobrien		addr2 = (const unsigned char *)&ecs2->addr.type.in;
65174294Sobrien		break;
66174294Sobrien	case AF_INET6:
67174294Sobrien		INSIST(alen <= 16);
68174294Sobrien		addr1 = (const unsigned char *)&ecs1->addr.type.in6;
69174294Sobrien		addr2 = (const unsigned char *)&ecs2->addr.type.in6;
70174294Sobrien		break;
71174294Sobrien	default:
72174294Sobrien		UNREACHABLE();
73174294Sobrien	}
74174294Sobrien
75174294Sobrien	/*
76174294Sobrien	 * Compare all octets except the final octet of the address
77174294Sobrien	 * prefix.
78174294Sobrien	 */
79174294Sobrien	if (alen > 1 && memcmp(addr1, addr2, alen - 1) != 0) {
80174294Sobrien		return (false);
81174294Sobrien	}
82174294Sobrien
83174294Sobrien	/*
84174294Sobrien	 * It should not be necessary to mask the final octet; all
85174294Sobrien	 * bits past the source prefix length are supposed to be 0.
86174294Sobrien	 * However, it seems prudent not to omit them from the
87174294Sobrien	 * comparison anyway.
88174294Sobrien	 */
89174294Sobrien	mask = (~0U << (8 - (ecs1->source % 8))) & 0xff;
90174294Sobrien	if (mask == 0) {
91174294Sobrien		mask = 0xff;
92174294Sobrien	}
93174294Sobrien
94174294Sobrien	if ((addr1[alen - 1] & mask) != (addr2[alen - 1] & mask)) {
95174294Sobrien		return (false);
96174294Sobrien	}
97174294Sobrien
98174294Sobrien	return (true);
99174294Sobrien}
100174294Sobrien
101174294Sobrienvoid
102174294Sobriendns_ecs_format(const dns_ecs_t *ecs, char *buf, size_t size) {
103174294Sobrien	size_t len;
104174294Sobrien	char *p;
105174294Sobrien
106174294Sobrien	REQUIRE(ecs != NULL);
107174294Sobrien	REQUIRE(buf != NULL);
108174294Sobrien	REQUIRE(size >= DNS_ECS_FORMATSIZE);
109174294Sobrien
110174294Sobrien	isc_netaddr_format(&ecs->addr, buf, size);
111174294Sobrien	len = strlen(buf);
112174294Sobrien	p = buf + len;
113174294Sobrien	snprintf(p, size - len, "/%d/%d", ecs->source,
114174294Sobrien		 ecs->scope == 0xff ? 0 : ecs->scope);
115174294Sobrien}
116174294Sobrien