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