1 /*	$NetBSD: nid_104.c,v 1.7 2022/09/23 12:15: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 #ifndef RDATA_GENERIC_NID_104_C
17 #define RDATA_GENERIC_NID_104_C
18 
19 #include <string.h>
20 
21 #include <isc/net.h>
22 
23 #define RRTYPE_NID_ATTRIBUTES (0)
24 
25 static isc_result_t
fromtext_nid(ARGS_FROMTEXT)26 fromtext_nid(ARGS_FROMTEXT) {
27 	isc_token_t token;
28 	unsigned char locator[NS_LOCATORSZ];
29 
30 	REQUIRE(type == dns_rdatatype_nid);
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_number,
39 				      false));
40 	if (token.value.as_ulong > 0xffffU) {
41 		RETTOK(ISC_R_RANGE);
42 	}
43 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
44 
45 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
46 				      false));
47 
48 	if (locator_pton(DNS_AS_STR(token), locator) != 1) {
49 		RETTOK(DNS_R_SYNTAX);
50 	}
51 	return (mem_tobuffer(target, locator, NS_LOCATORSZ));
52 }
53 
54 static isc_result_t
totext_nid(ARGS_TOTEXT)55 totext_nid(ARGS_TOTEXT) {
56 	isc_region_t region;
57 	char buf[sizeof("xxxx:xxxx:xxxx:xxxx")];
58 	unsigned short num;
59 
60 	REQUIRE(rdata->type == dns_rdatatype_nid);
61 	REQUIRE(rdata->length != 0);
62 
63 	UNUSED(tctx);
64 
65 	dns_rdata_toregion(rdata, &region);
66 	num = uint16_fromregion(&region);
67 	isc_region_consume(&region, 2);
68 	snprintf(buf, sizeof(buf), "%u", num);
69 	RETERR(str_totext(buf, target));
70 
71 	RETERR(str_totext(" ", target));
72 
73 	snprintf(buf, sizeof(buf), "%x:%x:%x:%x",
74 		 region.base[0] << 8 | region.base[1],
75 		 region.base[2] << 8 | region.base[3],
76 		 region.base[4] << 8 | region.base[5],
77 		 region.base[6] << 8 | region.base[7]);
78 	return (str_totext(buf, target));
79 }
80 
81 static isc_result_t
fromwire_nid(ARGS_FROMWIRE)82 fromwire_nid(ARGS_FROMWIRE) {
83 	isc_region_t sregion;
84 
85 	REQUIRE(type == dns_rdatatype_nid);
86 
87 	UNUSED(type);
88 	UNUSED(options);
89 	UNUSED(rdclass);
90 	UNUSED(dctx);
91 
92 	isc_buffer_activeregion(source, &sregion);
93 	if (sregion.length != 10) {
94 		return (DNS_R_FORMERR);
95 	}
96 	isc_buffer_forward(source, sregion.length);
97 	return (mem_tobuffer(target, sregion.base, sregion.length));
98 }
99 
100 static isc_result_t
towire_nid(ARGS_TOWIRE)101 towire_nid(ARGS_TOWIRE) {
102 	REQUIRE(rdata->type == dns_rdatatype_nid);
103 	REQUIRE(rdata->length == 10);
104 
105 	UNUSED(cctx);
106 
107 	return (mem_tobuffer(target, rdata->data, rdata->length));
108 }
109 
110 static int
compare_nid(ARGS_COMPARE)111 compare_nid(ARGS_COMPARE) {
112 	isc_region_t region1;
113 	isc_region_t region2;
114 
115 	REQUIRE(rdata1->type == rdata2->type);
116 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
117 	REQUIRE(rdata1->type == dns_rdatatype_nid);
118 	REQUIRE(rdata1->length == 10);
119 	REQUIRE(rdata2->length == 10);
120 
121 	dns_rdata_toregion(rdata1, &region1);
122 	dns_rdata_toregion(rdata2, &region2);
123 	return (isc_region_compare(&region1, &region2));
124 }
125 
126 static isc_result_t
fromstruct_nid(ARGS_FROMSTRUCT)127 fromstruct_nid(ARGS_FROMSTRUCT) {
128 	dns_rdata_nid_t *nid = source;
129 
130 	REQUIRE(type == dns_rdatatype_nid);
131 	REQUIRE(nid != NULL);
132 	REQUIRE(nid->common.rdtype == type);
133 	REQUIRE(nid->common.rdclass == rdclass);
134 
135 	UNUSED(type);
136 	UNUSED(rdclass);
137 
138 	RETERR(uint16_tobuffer(nid->pref, target));
139 	return (mem_tobuffer(target, nid->nid, sizeof(nid->nid)));
140 }
141 
142 static isc_result_t
tostruct_nid(ARGS_TOSTRUCT)143 tostruct_nid(ARGS_TOSTRUCT) {
144 	isc_region_t region;
145 	dns_rdata_nid_t *nid = target;
146 
147 	REQUIRE(rdata->type == dns_rdatatype_nid);
148 	REQUIRE(nid != NULL);
149 	REQUIRE(rdata->length == 10);
150 
151 	UNUSED(mctx);
152 
153 	nid->common.rdclass = rdata->rdclass;
154 	nid->common.rdtype = rdata->type;
155 	ISC_LINK_INIT(&nid->common, link);
156 
157 	dns_rdata_toregion(rdata, &region);
158 	nid->pref = uint16_fromregion(&region);
159 	memmove(nid->nid, region.base, region.length);
160 	return (ISC_R_SUCCESS);
161 }
162 
163 static void
freestruct_nid(ARGS_FREESTRUCT)164 freestruct_nid(ARGS_FREESTRUCT) {
165 	dns_rdata_nid_t *nid = source;
166 
167 	REQUIRE(nid != NULL);
168 	REQUIRE(nid->common.rdtype == dns_rdatatype_nid);
169 
170 	return;
171 }
172 
173 static isc_result_t
additionaldata_nid(ARGS_ADDLDATA)174 additionaldata_nid(ARGS_ADDLDATA) {
175 	REQUIRE(rdata->type == dns_rdatatype_nid);
176 	REQUIRE(rdata->length == 10);
177 
178 	UNUSED(rdata);
179 	UNUSED(add);
180 	UNUSED(arg);
181 
182 	return (ISC_R_SUCCESS);
183 }
184 
185 static isc_result_t
digest_nid(ARGS_DIGEST)186 digest_nid(ARGS_DIGEST) {
187 	isc_region_t r;
188 
189 	REQUIRE(rdata->type == dns_rdatatype_nid);
190 	REQUIRE(rdata->length == 10);
191 
192 	dns_rdata_toregion(rdata, &r);
193 
194 	return ((digest)(arg, &r));
195 }
196 
197 static bool
checkowner_nid(ARGS_CHECKOWNER)198 checkowner_nid(ARGS_CHECKOWNER) {
199 	REQUIRE(type == dns_rdatatype_nid);
200 
201 	UNUSED(name);
202 	UNUSED(type);
203 	UNUSED(rdclass);
204 	UNUSED(wildcard);
205 
206 	return (true);
207 }
208 
209 static bool
checknames_nid(ARGS_CHECKNAMES)210 checknames_nid(ARGS_CHECKNAMES) {
211 	REQUIRE(rdata->type == dns_rdatatype_nid);
212 	REQUIRE(rdata->length == 10);
213 
214 	UNUSED(rdata);
215 	UNUSED(owner);
216 	UNUSED(bad);
217 
218 	return (true);
219 }
220 
221 static int
casecompare_nid(ARGS_COMPARE)222 casecompare_nid(ARGS_COMPARE) {
223 	return (compare_nid(rdata1, rdata2));
224 }
225 
226 #endif /* RDATA_GENERIC_NID_104_C */
227