1/*	$NetBSD: eui64_109.c,v 1.8 2024/02/21 22:52:12 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#ifndef RDATA_GENERIC_EUI64_109_C
17#define RDATA_GENERIC_EUI64_109_C
18
19#include <string.h>
20
21#define RRTYPE_EUI64_ATTRIBUTES (0)
22
23static isc_result_t
24fromtext_eui64(ARGS_FROMTEXT) {
25	isc_token_t token;
26	unsigned char eui64[8];
27	unsigned int l0, l1, l2, l3, l4, l5, l6, l7;
28	int n;
29
30	REQUIRE(type == dns_rdatatype_eui64);
31
32	UNUSED(type);
33	UNUSED(rdclass);
34	UNUSED(origin);
35	UNUSED(options);
36	UNUSED(callbacks);
37
38	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
39				      false));
40	n = sscanf(DNS_AS_STR(token), "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x", &l0,
41		   &l1, &l2, &l3, &l4, &l5, &l6, &l7);
42	if (n != 8 || l0 > 255U || l1 > 255U || l2 > 255U || l3 > 255U ||
43	    l4 > 255U || l5 > 255U || l6 > 255U || l7 > 255U)
44	{
45		return (DNS_R_BADEUI);
46	}
47
48	eui64[0] = l0;
49	eui64[1] = l1;
50	eui64[2] = l2;
51	eui64[3] = l3;
52	eui64[4] = l4;
53	eui64[5] = l5;
54	eui64[6] = l6;
55	eui64[7] = l7;
56	return (mem_tobuffer(target, eui64, sizeof(eui64)));
57}
58
59static isc_result_t
60totext_eui64(ARGS_TOTEXT) {
61	char buf[sizeof("xx-xx-xx-xx-xx-xx-xx-xx")];
62
63	REQUIRE(rdata->type == dns_rdatatype_eui64);
64	REQUIRE(rdata->length == 8);
65
66	UNUSED(tctx);
67
68	(void)snprintf(
69		buf, sizeof(buf), "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
70		rdata->data[0], rdata->data[1], rdata->data[2], rdata->data[3],
71		rdata->data[4], rdata->data[5], rdata->data[6], rdata->data[7]);
72	return (str_totext(buf, target));
73}
74
75static isc_result_t
76fromwire_eui64(ARGS_FROMWIRE) {
77	isc_region_t sregion;
78
79	REQUIRE(type == dns_rdatatype_eui64);
80
81	UNUSED(type);
82	UNUSED(options);
83	UNUSED(rdclass);
84	UNUSED(dctx);
85
86	isc_buffer_activeregion(source, &sregion);
87	if (sregion.length != 8) {
88		return (DNS_R_FORMERR);
89	}
90	isc_buffer_forward(source, sregion.length);
91	return (mem_tobuffer(target, sregion.base, sregion.length));
92}
93
94static isc_result_t
95towire_eui64(ARGS_TOWIRE) {
96	REQUIRE(rdata->type == dns_rdatatype_eui64);
97	REQUIRE(rdata->length == 8);
98
99	UNUSED(cctx);
100
101	return (mem_tobuffer(target, rdata->data, rdata->length));
102}
103
104static int
105compare_eui64(ARGS_COMPARE) {
106	isc_region_t region1;
107	isc_region_t region2;
108
109	REQUIRE(rdata1->type == rdata2->type);
110	REQUIRE(rdata1->rdclass == rdata2->rdclass);
111	REQUIRE(rdata1->type == dns_rdatatype_eui64);
112	REQUIRE(rdata1->length == 8);
113	REQUIRE(rdata2->length == 8);
114
115	dns_rdata_toregion(rdata1, &region1);
116	dns_rdata_toregion(rdata2, &region2);
117	return (isc_region_compare(&region1, &region2));
118}
119
120static isc_result_t
121fromstruct_eui64(ARGS_FROMSTRUCT) {
122	dns_rdata_eui64_t *eui64 = source;
123
124	REQUIRE(type == dns_rdatatype_eui64);
125	REQUIRE(eui64 != NULL);
126	REQUIRE(eui64->common.rdtype == type);
127	REQUIRE(eui64->common.rdclass == rdclass);
128
129	UNUSED(type);
130	UNUSED(rdclass);
131
132	return (mem_tobuffer(target, eui64->eui64, sizeof(eui64->eui64)));
133}
134
135static isc_result_t
136tostruct_eui64(ARGS_TOSTRUCT) {
137	dns_rdata_eui64_t *eui64 = target;
138
139	REQUIRE(rdata->type == dns_rdatatype_eui64);
140	REQUIRE(eui64 != NULL);
141	REQUIRE(rdata->length == 8);
142
143	UNUSED(mctx);
144
145	eui64->common.rdclass = rdata->rdclass;
146	eui64->common.rdtype = rdata->type;
147	ISC_LINK_INIT(&eui64->common, link);
148
149	memmove(eui64->eui64, rdata->data, rdata->length);
150	return (ISC_R_SUCCESS);
151}
152
153static void
154freestruct_eui64(ARGS_FREESTRUCT) {
155	dns_rdata_eui64_t *eui64 = source;
156
157	REQUIRE(eui64 != NULL);
158	REQUIRE(eui64->common.rdtype == dns_rdatatype_eui64);
159
160	return;
161}
162
163static isc_result_t
164additionaldata_eui64(ARGS_ADDLDATA) {
165	REQUIRE(rdata->type == dns_rdatatype_eui64);
166	REQUIRE(rdata->length == 8);
167
168	UNUSED(rdata);
169	UNUSED(owner);
170	UNUSED(add);
171	UNUSED(arg);
172
173	return (ISC_R_SUCCESS);
174}
175
176static isc_result_t
177digest_eui64(ARGS_DIGEST) {
178	isc_region_t r;
179
180	REQUIRE(rdata->type == dns_rdatatype_eui64);
181	REQUIRE(rdata->length == 8);
182
183	dns_rdata_toregion(rdata, &r);
184
185	return ((digest)(arg, &r));
186}
187
188static bool
189checkowner_eui64(ARGS_CHECKOWNER) {
190	REQUIRE(type == dns_rdatatype_eui64);
191
192	UNUSED(name);
193	UNUSED(type);
194	UNUSED(rdclass);
195	UNUSED(wildcard);
196
197	return (true);
198}
199
200static bool
201checknames_eui64(ARGS_CHECKNAMES) {
202	REQUIRE(rdata->type == dns_rdatatype_eui64);
203	REQUIRE(rdata->length == 8);
204
205	UNUSED(rdata);
206	UNUSED(owner);
207	UNUSED(bad);
208
209	return (true);
210}
211
212static int
213casecompare_eui64(ARGS_COMPARE) {
214	return (compare_eui64(rdata1, rdata2));
215}
216
217#endif /* RDATA_GENERIC_EUI64_109_C */
218