1 /* $NetBSD: sink_40.c,v 1.1 2024/02/18 20:57:44 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_SINK_40_C
17 #define RDATA_GENERIC_SINK_40_C
18
19 #include <dst/dst.h>
20
21 #define RRTYPE_SINK_ATTRIBUTES (0)
22
23 static isc_result_t
fromtext_sink(ARGS_FROMTEXT)24 fromtext_sink(ARGS_FROMTEXT) {
25 isc_token_t token;
26
27 REQUIRE(type == dns_rdatatype_sink);
28
29 UNUSED(type);
30 UNUSED(rdclass);
31 UNUSED(origin);
32 UNUSED(options);
33 UNUSED(callbacks);
34
35 /* meaning */
36 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
37 false));
38 if (token.value.as_ulong > 0xffU) {
39 RETTOK(ISC_R_RANGE);
40 }
41 RETERR(uint8_tobuffer(token.value.as_ulong, target));
42
43 /* coding */
44 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
45 false));
46 if (token.value.as_ulong > 0xffU) {
47 RETTOK(ISC_R_RANGE);
48 }
49 RETERR(uint8_tobuffer(token.value.as_ulong, target));
50
51 /* subcoding */
52 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
53 false));
54 if (token.value.as_ulong > 0xffU) {
55 RETTOK(ISC_R_RANGE);
56 }
57 RETERR(uint8_tobuffer(token.value.as_ulong, target));
58
59 return (isc_base64_tobuffer(lexer, target, -1));
60 }
61
62 static isc_result_t
totext_sink(ARGS_TOTEXT)63 totext_sink(ARGS_TOTEXT) {
64 isc_region_t sr;
65 char buf[sizeof("255 255 255")];
66 uint8_t meaning, coding, subcoding;
67
68 REQUIRE(rdata->type == dns_rdatatype_sink);
69 REQUIRE(rdata->length >= 3);
70
71 dns_rdata_toregion(rdata, &sr);
72
73 /* Meaning, Coding and Subcoding */
74 meaning = uint8_fromregion(&sr);
75 isc_region_consume(&sr, 1);
76 coding = uint8_fromregion(&sr);
77 isc_region_consume(&sr, 1);
78 subcoding = uint8_fromregion(&sr);
79 isc_region_consume(&sr, 1);
80 snprintf(buf, sizeof(buf), "%u %u %u", meaning, coding, subcoding);
81 RETERR(str_totext(buf, target));
82
83 if (sr.length == 0U) {
84 return (ISC_R_SUCCESS);
85 }
86
87 /* data */
88 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
89 RETERR(str_totext(" (", target));
90 }
91
92 RETERR(str_totext(tctx->linebreak, target));
93
94 if (tctx->width == 0) { /* No splitting */
95 RETERR(isc_base64_totext(&sr, 60, "", target));
96 } else {
97 RETERR(isc_base64_totext(&sr, tctx->width - 2, tctx->linebreak,
98 target));
99 }
100
101 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
102 RETERR(str_totext(" )", target));
103 }
104
105 return (ISC_R_SUCCESS);
106 }
107
108 static isc_result_t
fromwire_sink(ARGS_FROMWIRE)109 fromwire_sink(ARGS_FROMWIRE) {
110 isc_region_t sr;
111
112 REQUIRE(type == dns_rdatatype_sink);
113
114 UNUSED(type);
115 UNUSED(rdclass);
116 UNUSED(dctx);
117 UNUSED(options);
118
119 isc_buffer_activeregion(source, &sr);
120 if (sr.length < 3) {
121 return (ISC_R_UNEXPECTEDEND);
122 }
123
124 RETERR(mem_tobuffer(target, sr.base, sr.length));
125 isc_buffer_forward(source, sr.length);
126 return (ISC_R_SUCCESS);
127 }
128
129 static isc_result_t
towire_sink(ARGS_TOWIRE)130 towire_sink(ARGS_TOWIRE) {
131 REQUIRE(rdata->type == dns_rdatatype_sink);
132 REQUIRE(rdata->length >= 3);
133
134 UNUSED(cctx);
135
136 return (mem_tobuffer(target, rdata->data, rdata->length));
137 }
138
139 static int
compare_sink(ARGS_COMPARE)140 compare_sink(ARGS_COMPARE) {
141 isc_region_t r1;
142 isc_region_t r2;
143
144 REQUIRE(rdata1->type == rdata2->type);
145 REQUIRE(rdata1->rdclass == rdata2->rdclass);
146 REQUIRE(rdata1->type == dns_rdatatype_sink);
147 REQUIRE(rdata1->length >= 3);
148 REQUIRE(rdata2->length >= 3);
149
150 dns_rdata_toregion(rdata1, &r1);
151 dns_rdata_toregion(rdata2, &r2);
152 return (isc_region_compare(&r1, &r2));
153 }
154
155 static isc_result_t
fromstruct_sink(ARGS_FROMSTRUCT)156 fromstruct_sink(ARGS_FROMSTRUCT) {
157 dns_rdata_sink_t *sink = source;
158
159 REQUIRE(type == dns_rdatatype_sink);
160 REQUIRE(sink != NULL);
161 REQUIRE(sink->common.rdtype == type);
162 REQUIRE(sink->common.rdclass == rdclass);
163
164 UNUSED(type);
165 UNUSED(rdclass);
166
167 /* Meaning */
168 RETERR(uint8_tobuffer(sink->meaning, target));
169
170 /* Coding */
171 RETERR(uint8_tobuffer(sink->coding, target));
172
173 /* Subcoding */
174 RETERR(uint8_tobuffer(sink->subcoding, target));
175
176 /* Data */
177 return (mem_tobuffer(target, sink->data, sink->datalen));
178 }
179
180 static isc_result_t
tostruct_sink(ARGS_TOSTRUCT)181 tostruct_sink(ARGS_TOSTRUCT) {
182 dns_rdata_sink_t *sink = target;
183 isc_region_t sr;
184
185 REQUIRE(rdata->type == dns_rdatatype_sink);
186 REQUIRE(sink != NULL);
187 REQUIRE(rdata->length >= 3);
188
189 sink->common.rdclass = rdata->rdclass;
190 sink->common.rdtype = rdata->type;
191 ISC_LINK_INIT(&sink->common, link);
192
193 dns_rdata_toregion(rdata, &sr);
194
195 /* Meaning */
196 if (sr.length < 1) {
197 return (ISC_R_UNEXPECTEDEND);
198 }
199 sink->meaning = uint8_fromregion(&sr);
200 isc_region_consume(&sr, 1);
201
202 /* Coding */
203 if (sr.length < 1) {
204 return (ISC_R_UNEXPECTEDEND);
205 }
206 sink->coding = uint8_fromregion(&sr);
207 isc_region_consume(&sr, 1);
208
209 /* Subcoding */
210 if (sr.length < 1) {
211 return (ISC_R_UNEXPECTEDEND);
212 }
213 sink->subcoding = uint8_fromregion(&sr);
214 isc_region_consume(&sr, 1);
215
216 /* Data */
217 sink->datalen = sr.length;
218 sink->data = mem_maybedup(mctx, sr.base, sink->datalen);
219 if (sink->data == NULL) {
220 return (ISC_R_NOMEMORY);
221 }
222
223 sink->mctx = mctx;
224 return (ISC_R_SUCCESS);
225 }
226
227 static void
freestruct_sink(ARGS_FREESTRUCT)228 freestruct_sink(ARGS_FREESTRUCT) {
229 dns_rdata_sink_t *sink = (dns_rdata_sink_t *)source;
230
231 REQUIRE(sink != NULL);
232 REQUIRE(sink->common.rdtype == dns_rdatatype_sink);
233
234 if (sink->mctx == NULL) {
235 return;
236 }
237
238 if (sink->data != NULL) {
239 isc_mem_free(sink->mctx, sink->data);
240 }
241 sink->mctx = NULL;
242 }
243
244 static isc_result_t
additionaldata_sink(ARGS_ADDLDATA)245 additionaldata_sink(ARGS_ADDLDATA) {
246 REQUIRE(rdata->type == dns_rdatatype_sink);
247
248 UNUSED(rdata);
249 UNUSED(add);
250 UNUSED(arg);
251
252 return (ISC_R_SUCCESS);
253 }
254
255 static isc_result_t
digest_sink(ARGS_DIGEST)256 digest_sink(ARGS_DIGEST) {
257 isc_region_t r;
258
259 REQUIRE(rdata->type == dns_rdatatype_sink);
260
261 dns_rdata_toregion(rdata, &r);
262
263 return ((digest)(arg, &r));
264 }
265
266 static bool
checkowner_sink(ARGS_CHECKOWNER)267 checkowner_sink(ARGS_CHECKOWNER) {
268 REQUIRE(type == dns_rdatatype_sink);
269
270 UNUSED(name);
271 UNUSED(type);
272 UNUSED(rdclass);
273 UNUSED(wildcard);
274
275 return (true);
276 }
277
278 static bool
checknames_sink(ARGS_CHECKNAMES)279 checknames_sink(ARGS_CHECKNAMES) {
280 REQUIRE(rdata->type == dns_rdatatype_sink);
281
282 UNUSED(rdata);
283 UNUSED(owner);
284 UNUSED(bad);
285
286 return (true);
287 }
288
289 static int
casecompare_sink(ARGS_COMPARE)290 casecompare_sink(ARGS_COMPARE) {
291 return (compare_sink(rdata1, rdata2));
292 }
293 #endif /* RDATA_GENERIC_SINK_40_C */
294