1 /* $NetBSD: nsap_22.c,v 1.1 2024/02/18 20:57:46 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 /* RFC1706 */
17
18 #ifndef RDATA_IN_1_NSAP_22_C
19 #define RDATA_IN_1_NSAP_22_C
20
21 #define RRTYPE_NSAP_ATTRIBUTES (0)
22
23 static isc_result_t
fromtext_in_nsap(ARGS_FROMTEXT)24 fromtext_in_nsap(ARGS_FROMTEXT) {
25 isc_token_t token;
26 isc_textregion_t *sr;
27 int n;
28 bool valid = false;
29 int digits = 0;
30 unsigned char c = 0;
31
32 REQUIRE(type == dns_rdatatype_nsap);
33 REQUIRE(rdclass == dns_rdataclass_in);
34
35 UNUSED(type);
36 UNUSED(origin);
37 UNUSED(options);
38 UNUSED(rdclass);
39 UNUSED(callbacks);
40
41 /* 0x<hex.string.with.periods> */
42 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
43 false));
44 sr = &token.value.as_textregion;
45 if (sr->length < 2) {
46 RETTOK(ISC_R_UNEXPECTEDEND);
47 }
48 if (sr->base[0] != '0' || (sr->base[1] != 'x' && sr->base[1] != 'X')) {
49 RETTOK(DNS_R_SYNTAX);
50 }
51 isc_textregion_consume(sr, 2);
52 while (sr->length > 0) {
53 if (sr->base[0] == '.') {
54 isc_textregion_consume(sr, 1);
55 continue;
56 }
57 if ((n = hexvalue(sr->base[0])) == -1) {
58 RETTOK(DNS_R_SYNTAX);
59 }
60 c <<= 4;
61 c += n;
62 if (++digits == 2) {
63 RETERR(mem_tobuffer(target, &c, 1));
64 valid = true;
65 digits = 0;
66 c = 0;
67 }
68 isc_textregion_consume(sr, 1);
69 }
70 if (digits != 0 || !valid) {
71 RETTOK(ISC_R_UNEXPECTEDEND);
72 }
73 return (ISC_R_SUCCESS);
74 }
75
76 static isc_result_t
totext_in_nsap(ARGS_TOTEXT)77 totext_in_nsap(ARGS_TOTEXT) {
78 isc_region_t region;
79 char buf[sizeof("xx")];
80
81 REQUIRE(rdata->type == dns_rdatatype_nsap);
82 REQUIRE(rdata->rdclass == dns_rdataclass_in);
83 REQUIRE(rdata->length != 0);
84
85 UNUSED(tctx);
86
87 dns_rdata_toregion(rdata, ®ion);
88 RETERR(str_totext("0x", target));
89 while (region.length != 0) {
90 snprintf(buf, sizeof(buf), "%02x", region.base[0]);
91 isc_region_consume(®ion, 1);
92 RETERR(str_totext(buf, target));
93 }
94 return (ISC_R_SUCCESS);
95 }
96
97 static isc_result_t
fromwire_in_nsap(ARGS_FROMWIRE)98 fromwire_in_nsap(ARGS_FROMWIRE) {
99 isc_region_t region;
100
101 REQUIRE(type == dns_rdatatype_nsap);
102 REQUIRE(rdclass == dns_rdataclass_in);
103
104 UNUSED(type);
105 UNUSED(dctx);
106 UNUSED(options);
107 UNUSED(rdclass);
108
109 isc_buffer_activeregion(source, ®ion);
110 if (region.length < 1) {
111 return (ISC_R_UNEXPECTEDEND);
112 }
113
114 RETERR(mem_tobuffer(target, region.base, region.length));
115 isc_buffer_forward(source, region.length);
116 return (ISC_R_SUCCESS);
117 }
118
119 static isc_result_t
towire_in_nsap(ARGS_TOWIRE)120 towire_in_nsap(ARGS_TOWIRE) {
121 REQUIRE(rdata->type == dns_rdatatype_nsap);
122 REQUIRE(rdata->rdclass == dns_rdataclass_in);
123 REQUIRE(rdata->length != 0);
124
125 UNUSED(cctx);
126
127 return (mem_tobuffer(target, rdata->data, rdata->length));
128 }
129
130 static int
compare_in_nsap(ARGS_COMPARE)131 compare_in_nsap(ARGS_COMPARE) {
132 isc_region_t r1;
133 isc_region_t r2;
134
135 REQUIRE(rdata1->type == rdata2->type);
136 REQUIRE(rdata1->rdclass == rdata2->rdclass);
137 REQUIRE(rdata1->type == dns_rdatatype_nsap);
138 REQUIRE(rdata1->rdclass == dns_rdataclass_in);
139 REQUIRE(rdata1->length != 0);
140 REQUIRE(rdata2->length != 0);
141
142 dns_rdata_toregion(rdata1, &r1);
143 dns_rdata_toregion(rdata2, &r2);
144 return (isc_region_compare(&r1, &r2));
145 }
146
147 static isc_result_t
fromstruct_in_nsap(ARGS_FROMSTRUCT)148 fromstruct_in_nsap(ARGS_FROMSTRUCT) {
149 dns_rdata_in_nsap_t *nsap = source;
150
151 REQUIRE(type == dns_rdatatype_nsap);
152 REQUIRE(rdclass == dns_rdataclass_in);
153 REQUIRE(nsap != NULL);
154 REQUIRE(nsap->common.rdtype == type);
155 REQUIRE(nsap->common.rdclass == rdclass);
156 REQUIRE(nsap->nsap != NULL || nsap->nsap_len == 0);
157
158 UNUSED(type);
159 UNUSED(rdclass);
160
161 return (mem_tobuffer(target, nsap->nsap, nsap->nsap_len));
162 }
163
164 static isc_result_t
tostruct_in_nsap(ARGS_TOSTRUCT)165 tostruct_in_nsap(ARGS_TOSTRUCT) {
166 dns_rdata_in_nsap_t *nsap = target;
167 isc_region_t r;
168
169 REQUIRE(rdata->type == dns_rdatatype_nsap);
170 REQUIRE(rdata->rdclass == dns_rdataclass_in);
171 REQUIRE(nsap != NULL);
172 REQUIRE(rdata->length != 0);
173
174 nsap->common.rdclass = rdata->rdclass;
175 nsap->common.rdtype = rdata->type;
176 ISC_LINK_INIT(&nsap->common, link);
177
178 dns_rdata_toregion(rdata, &r);
179 nsap->nsap_len = r.length;
180 nsap->nsap = mem_maybedup(mctx, r.base, r.length);
181 if (nsap->nsap == NULL) {
182 return (ISC_R_NOMEMORY);
183 }
184
185 nsap->mctx = mctx;
186 return (ISC_R_SUCCESS);
187 }
188
189 static void
freestruct_in_nsap(ARGS_FREESTRUCT)190 freestruct_in_nsap(ARGS_FREESTRUCT) {
191 dns_rdata_in_nsap_t *nsap = source;
192
193 REQUIRE(nsap != NULL);
194 REQUIRE(nsap->common.rdclass == dns_rdataclass_in);
195 REQUIRE(nsap->common.rdtype == dns_rdatatype_nsap);
196
197 if (nsap->mctx == NULL) {
198 return;
199 }
200
201 if (nsap->nsap != NULL) {
202 isc_mem_free(nsap->mctx, nsap->nsap);
203 }
204 nsap->mctx = NULL;
205 }
206
207 static isc_result_t
additionaldata_in_nsap(ARGS_ADDLDATA)208 additionaldata_in_nsap(ARGS_ADDLDATA) {
209 REQUIRE(rdata->type == dns_rdatatype_nsap);
210 REQUIRE(rdata->rdclass == dns_rdataclass_in);
211
212 UNUSED(rdata);
213 UNUSED(add);
214 UNUSED(arg);
215
216 return (ISC_R_SUCCESS);
217 }
218
219 static isc_result_t
digest_in_nsap(ARGS_DIGEST)220 digest_in_nsap(ARGS_DIGEST) {
221 isc_region_t r;
222
223 REQUIRE(rdata->type == dns_rdatatype_nsap);
224 REQUIRE(rdata->rdclass == dns_rdataclass_in);
225
226 dns_rdata_toregion(rdata, &r);
227
228 return ((digest)(arg, &r));
229 }
230
231 static bool
checkowner_in_nsap(ARGS_CHECKOWNER)232 checkowner_in_nsap(ARGS_CHECKOWNER) {
233 REQUIRE(type == dns_rdatatype_nsap);
234 REQUIRE(rdclass == dns_rdataclass_in);
235
236 UNUSED(name);
237 UNUSED(type);
238 UNUSED(rdclass);
239 UNUSED(wildcard);
240
241 return (true);
242 }
243
244 static bool
checknames_in_nsap(ARGS_CHECKNAMES)245 checknames_in_nsap(ARGS_CHECKNAMES) {
246 REQUIRE(rdata->type == dns_rdatatype_nsap);
247 REQUIRE(rdata->rdclass == dns_rdataclass_in);
248
249 UNUSED(rdata);
250 UNUSED(owner);
251 UNUSED(bad);
252
253 return (true);
254 }
255
256 static int
casecompare_in_nsap(ARGS_COMPARE)257 casecompare_in_nsap(ARGS_COMPARE) {
258 return (compare_in_nsap(rdata1, rdata2));
259 }
260
261 #endif /* RDATA_IN_1_NSAP_22_C */
262