xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/dns/rdata/generic/eui64_109.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: eui64_109.c,v 1.1 2024/02/18 20:57:41 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 
23 static isc_result_t
fromtext_eui64(ARGS_FROMTEXT)24 fromtext_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 
59 static isc_result_t
totext_eui64(ARGS_TOTEXT)60 totext_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 
75 static isc_result_t
fromwire_eui64(ARGS_FROMWIRE)76 fromwire_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 
94 static isc_result_t
towire_eui64(ARGS_TOWIRE)95 towire_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 
104 static int
compare_eui64(ARGS_COMPARE)105 compare_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 
120 static isc_result_t
fromstruct_eui64(ARGS_FROMSTRUCT)121 fromstruct_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 
135 static isc_result_t
tostruct_eui64(ARGS_TOSTRUCT)136 tostruct_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 
153 static void
freestruct_eui64(ARGS_FREESTRUCT)154 freestruct_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 
163 static isc_result_t
additionaldata_eui64(ARGS_ADDLDATA)164 additionaldata_eui64(ARGS_ADDLDATA) {
165 	REQUIRE(rdata->type == dns_rdatatype_eui64);
166 	REQUIRE(rdata->length == 8);
167 
168 	UNUSED(rdata);
169 	UNUSED(add);
170 	UNUSED(arg);
171 
172 	return (ISC_R_SUCCESS);
173 }
174 
175 static isc_result_t
digest_eui64(ARGS_DIGEST)176 digest_eui64(ARGS_DIGEST) {
177 	isc_region_t r;
178 
179 	REQUIRE(rdata->type == dns_rdatatype_eui64);
180 	REQUIRE(rdata->length == 8);
181 
182 	dns_rdata_toregion(rdata, &r);
183 
184 	return ((digest)(arg, &r));
185 }
186 
187 static bool
checkowner_eui64(ARGS_CHECKOWNER)188 checkowner_eui64(ARGS_CHECKOWNER) {
189 	REQUIRE(type == dns_rdatatype_eui64);
190 
191 	UNUSED(name);
192 	UNUSED(type);
193 	UNUSED(rdclass);
194 	UNUSED(wildcard);
195 
196 	return (true);
197 }
198 
199 static bool
checknames_eui64(ARGS_CHECKNAMES)200 checknames_eui64(ARGS_CHECKNAMES) {
201 	REQUIRE(rdata->type == dns_rdatatype_eui64);
202 	REQUIRE(rdata->length == 8);
203 
204 	UNUSED(rdata);
205 	UNUSED(owner);
206 	UNUSED(bad);
207 
208 	return (true);
209 }
210 
211 static int
casecompare_eui64(ARGS_COMPARE)212 casecompare_eui64(ARGS_COMPARE) {
213 	return (compare_eui64(rdata1, rdata2));
214 }
215 
216 #endif /* RDATA_GENERIC_EUI64_109_C */
217