/src/bind9/lib/dns/rdata/generic/amtrelay_260.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
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_AMTRELAY_260_C |
15 | | #define RDATA_GENERIC_AMTRELAY_260_C |
16 | | |
17 | | #include <string.h> |
18 | | |
19 | | #include <isc/net.h> |
20 | | |
21 | 15.8k | #define RRTYPE_AMTRELAY_ATTRIBUTES (0) |
22 | | |
23 | | static isc_result_t |
24 | 1.40k | fromtext_amtrelay(ARGS_FROMTEXT) { |
25 | 1.40k | isc_token_t token; |
26 | 1.40k | isc_buffer_t buffer; |
27 | 1.40k | unsigned int discovery; |
28 | 1.40k | unsigned int gateway; |
29 | 1.40k | struct in_addr addr; |
30 | 1.40k | unsigned char addr6[16]; |
31 | 1.40k | isc_region_t region; |
32 | | |
33 | 1.40k | REQUIRE(type == dns_rdatatype_amtrelay); |
34 | | |
35 | 1.40k | UNUSED(type); |
36 | 1.40k | UNUSED(rdclass); |
37 | 1.40k | UNUSED(callbacks); |
38 | | |
39 | | /* |
40 | | * Precedence. |
41 | | */ |
42 | 1.40k | RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, |
43 | 1.40k | false)); |
44 | 1.39k | if (token.value.as_ulong > 0xffU) { |
45 | 26 | RETTOK(ISC_R_RANGE); |
46 | 26 | } |
47 | 1.37k | RETERR(uint8_tobuffer(token.value.as_ulong, target)); |
48 | | |
49 | | /* |
50 | | * Discovery. |
51 | | */ |
52 | 1.37k | RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, |
53 | 1.37k | false)); |
54 | 1.36k | if (token.value.as_ulong > 1U) { |
55 | 58 | RETTOK(ISC_R_RANGE); |
56 | 58 | } |
57 | 1.30k | discovery = token.value.as_ulong; |
58 | | |
59 | | /* |
60 | | * Gateway type. |
61 | | */ |
62 | 1.30k | RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, |
63 | 1.30k | false)); |
64 | 1.30k | if (token.value.as_ulong > 0x7fU) { |
65 | 22 | RETTOK(ISC_R_RANGE); |
66 | 22 | } |
67 | 1.28k | RETERR(uint8_tobuffer(token.value.as_ulong | (discovery << 7), target)); |
68 | 1.28k | gateway = token.value.as_ulong; |
69 | | |
70 | 1.28k | if (gateway == 0) { |
71 | 1.04k | return ISC_R_SUCCESS; |
72 | 1.04k | } |
73 | | |
74 | 242 | if (gateway > 3) { |
75 | 8 | return ISC_R_NOTIMPLEMENTED; |
76 | 8 | } |
77 | | |
78 | | /* |
79 | | * Gateway. |
80 | | */ |
81 | 234 | RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string, |
82 | 234 | false)); |
83 | | |
84 | 231 | switch (gateway) { |
85 | 6 | case 1: |
86 | 6 | if (inet_pton(AF_INET, DNS_AS_STR(token), &addr) != 1) { |
87 | 2 | RETTOK(DNS_R_BADDOTTEDQUAD); |
88 | 2 | } |
89 | 4 | isc_buffer_availableregion(target, ®ion); |
90 | 4 | if (region.length < 4) { |
91 | 0 | return ISC_R_NOSPACE; |
92 | 0 | } |
93 | 4 | memmove(region.base, &addr, 4); |
94 | 4 | isc_buffer_add(target, 4); |
95 | 4 | return ISC_R_SUCCESS; |
96 | | |
97 | 5 | case 2: |
98 | 5 | if (inet_pton(AF_INET6, DNS_AS_STR(token), addr6) != 1) { |
99 | 2 | RETTOK(DNS_R_BADAAAA); |
100 | 2 | } |
101 | 3 | isc_buffer_availableregion(target, ®ion); |
102 | 3 | if (region.length < 16) { |
103 | 0 | return ISC_R_NOSPACE; |
104 | 0 | } |
105 | 3 | memmove(region.base, addr6, 16); |
106 | 3 | isc_buffer_add(target, 16); |
107 | 3 | return ISC_R_SUCCESS; |
108 | | |
109 | 220 | case 3: |
110 | 220 | buffer_fromregion(&buffer, &token.value.as_region); |
111 | 220 | if (origin == NULL) { |
112 | 1 | origin = dns_rootname; |
113 | 1 | } |
114 | 220 | return dns_name_wirefromtext(&buffer, origin, options, target); |
115 | 0 | default: |
116 | 0 | UNREACHABLE(); |
117 | 231 | } |
118 | 231 | } |
119 | | |
120 | | static isc_result_t |
121 | 3.63k | totext_amtrelay(ARGS_TOTEXT) { |
122 | 3.63k | isc_region_t region; |
123 | 3.63k | dns_name_t name; |
124 | 3.63k | char buf[sizeof("0 255 ")]; |
125 | 3.63k | unsigned char precedence; |
126 | 3.63k | unsigned char discovery; |
127 | 3.63k | unsigned char gateway; |
128 | 3.63k | const char *space; |
129 | | |
130 | 3.63k | UNUSED(tctx); |
131 | | |
132 | 3.63k | REQUIRE(rdata->type == dns_rdatatype_amtrelay); |
133 | 3.63k | REQUIRE(rdata->length >= 2); |
134 | | |
135 | 3.63k | if ((rdata->data[1] & 0x7f) > 3U) { |
136 | 1.78k | return ISC_R_NOTIMPLEMENTED; |
137 | 1.78k | } |
138 | | |
139 | | /* |
140 | | * Precedence. |
141 | | */ |
142 | 1.85k | dns_rdata_toregion(rdata, ®ion); |
143 | 1.85k | precedence = uint8_fromregion(®ion); |
144 | 1.85k | isc_region_consume(®ion, 1); |
145 | 1.85k | snprintf(buf, sizeof(buf), "%u ", precedence); |
146 | 1.85k | RETERR(str_totext(buf, target)); |
147 | | |
148 | | /* |
149 | | * Discovery and Gateway type. |
150 | | */ |
151 | 1.85k | gateway = uint8_fromregion(®ion); |
152 | 1.85k | discovery = gateway >> 7; |
153 | 1.85k | gateway &= 0x7f; |
154 | 1.85k | space = (gateway != 0U) ? " " : ""; |
155 | 1.85k | isc_region_consume(®ion, 1); |
156 | 1.85k | snprintf(buf, sizeof(buf), "%u %u%s", discovery, gateway, space); |
157 | 1.85k | RETERR(str_totext(buf, target)); |
158 | | |
159 | | /* |
160 | | * Gateway. |
161 | | */ |
162 | 1.85k | switch (gateway) { |
163 | 533 | case 0: |
164 | 533 | break; |
165 | 492 | case 1: |
166 | 492 | return inet_totext(AF_INET, tctx->flags, ®ion, target); |
167 | | |
168 | 556 | case 2: |
169 | 556 | return inet_totext(AF_INET6, tctx->flags, ®ion, target); |
170 | | |
171 | 272 | case 3: |
172 | 272 | dns_name_init(&name); |
173 | 272 | dns_name_fromregion(&name, ®ion); |
174 | 272 | return dns_name_totext(&name, 0, target); |
175 | | |
176 | 0 | default: |
177 | 0 | UNREACHABLE(); |
178 | 1.85k | } |
179 | 533 | return ISC_R_SUCCESS; |
180 | 1.85k | } |
181 | | |
182 | | static isc_result_t |
183 | 5.24k | fromwire_amtrelay(ARGS_FROMWIRE) { |
184 | 5.24k | dns_name_t name; |
185 | 5.24k | isc_region_t region; |
186 | | |
187 | 5.24k | REQUIRE(type == dns_rdatatype_amtrelay); |
188 | | |
189 | 5.24k | UNUSED(type); |
190 | 5.24k | UNUSED(rdclass); |
191 | | |
192 | 5.24k | dctx = dns_decompress_setpermitted(dctx, false); |
193 | | |
194 | 5.24k | isc_buffer_activeregion(source, ®ion); |
195 | 5.24k | if (region.length < 2) { |
196 | 10 | return ISC_R_UNEXPECTEDEND; |
197 | 10 | } |
198 | | |
199 | 5.23k | switch (region.base[1] & 0x7f) { |
200 | 938 | case 0: |
201 | 938 | if (region.length != 2) { |
202 | 7 | return DNS_R_FORMERR; |
203 | 7 | } |
204 | 931 | isc_buffer_forward(source, region.length); |
205 | 931 | return mem_tobuffer(target, region.base, region.length); |
206 | | |
207 | 602 | case 1: |
208 | 602 | if (region.length != 6) { |
209 | 5 | return DNS_R_FORMERR; |
210 | 5 | } |
211 | 597 | isc_buffer_forward(source, region.length); |
212 | 597 | return mem_tobuffer(target, region.base, region.length); |
213 | | |
214 | 653 | case 2: |
215 | 653 | if (region.length != 18) { |
216 | 21 | return DNS_R_FORMERR; |
217 | 21 | } |
218 | 632 | isc_buffer_forward(source, region.length); |
219 | 632 | return mem_tobuffer(target, region.base, region.length); |
220 | | |
221 | 757 | case 3: |
222 | 757 | RETERR(mem_tobuffer(target, region.base, 2)); |
223 | 535 | isc_buffer_forward(source, 2); |
224 | 535 | dns_name_init(&name); |
225 | 535 | return dns_name_fromwire(&name, source, dctx, target); |
226 | | |
227 | 2.28k | default: |
228 | 2.28k | isc_buffer_forward(source, region.length); |
229 | 2.28k | return mem_tobuffer(target, region.base, region.length); |
230 | 5.23k | } |
231 | 5.23k | } |
232 | | |
233 | | static isc_result_t |
234 | 1.88k | towire_amtrelay(ARGS_TOWIRE) { |
235 | 1.88k | isc_region_t region; |
236 | | |
237 | 1.88k | REQUIRE(rdata->type == dns_rdatatype_amtrelay); |
238 | 1.88k | REQUIRE(rdata->length != 0); |
239 | | |
240 | 1.88k | UNUSED(cctx); |
241 | | |
242 | 1.88k | dns_rdata_toregion(rdata, ®ion); |
243 | 1.88k | return mem_tobuffer(target, region.base, region.length); |
244 | 1.88k | } |
245 | | |
246 | | static int |
247 | 3.00k | compare_amtrelay(ARGS_COMPARE) { |
248 | 3.00k | isc_region_t region1; |
249 | 3.00k | isc_region_t region2; |
250 | | |
251 | 3.00k | REQUIRE(rdata1->type == rdata2->type); |
252 | 3.00k | REQUIRE(rdata1->rdclass == rdata2->rdclass); |
253 | 3.00k | REQUIRE(rdata1->type == dns_rdatatype_amtrelay); |
254 | 3.00k | REQUIRE(rdata1->length >= 2); |
255 | 3.00k | REQUIRE(rdata2->length >= 2); |
256 | | |
257 | 3.00k | dns_rdata_toregion(rdata1, ®ion1); |
258 | 3.00k | dns_rdata_toregion(rdata2, ®ion2); |
259 | | |
260 | 3.00k | return isc_region_compare(®ion1, ®ion2); |
261 | 3.00k | } |
262 | | |
263 | | static isc_result_t |
264 | 0 | fromstruct_amtrelay(ARGS_FROMSTRUCT) { |
265 | 0 | dns_rdata_amtrelay_t *amtrelay = source; |
266 | 0 | isc_region_t region; |
267 | 0 | uint32_t n; |
268 | |
|
269 | 0 | REQUIRE(type == dns_rdatatype_amtrelay); |
270 | 0 | REQUIRE(amtrelay != NULL); |
271 | 0 | REQUIRE(amtrelay->common.rdtype == type); |
272 | 0 | REQUIRE(amtrelay->common.rdclass == rdclass); |
273 | |
|
274 | 0 | UNUSED(type); |
275 | 0 | UNUSED(rdclass); |
276 | |
|
277 | 0 | RETERR(uint8_tobuffer(amtrelay->precedence, target)); |
278 | 0 | n = (amtrelay->discovery ? 0x80 : 0) | amtrelay->gateway_type; |
279 | 0 | RETERR(uint8_tobuffer(n, target)); |
280 | | |
281 | 0 | switch (amtrelay->gateway_type) { |
282 | 0 | case 0: |
283 | 0 | return ISC_R_SUCCESS; |
284 | | |
285 | 0 | case 1: |
286 | 0 | n = ntohl(amtrelay->in_addr.s_addr); |
287 | 0 | return uint32_tobuffer(n, target); |
288 | | |
289 | 0 | case 2: |
290 | 0 | return mem_tobuffer(target, amtrelay->in6_addr.s6_addr, 16); |
291 | 0 | break; |
292 | | |
293 | 0 | case 3: |
294 | 0 | dns_name_toregion(&amtrelay->gateway, ®ion); |
295 | 0 | return isc_buffer_copyregion(target, ®ion); |
296 | 0 | break; |
297 | | |
298 | 0 | default: |
299 | 0 | return mem_tobuffer(target, amtrelay->data, amtrelay->length); |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | | static isc_result_t |
304 | 0 | tostruct_amtrelay(ARGS_TOSTRUCT) { |
305 | 0 | isc_region_t region; |
306 | 0 | dns_rdata_amtrelay_t *amtrelay = target; |
307 | 0 | dns_name_t name; |
308 | 0 | uint32_t n; |
309 | |
|
310 | 0 | REQUIRE(rdata->type == dns_rdatatype_amtrelay); |
311 | 0 | REQUIRE(amtrelay != NULL); |
312 | 0 | REQUIRE(rdata->length >= 2); |
313 | |
|
314 | 0 | DNS_RDATACOMMON_INIT(amtrelay, rdata->type, rdata->rdclass); |
315 | |
|
316 | 0 | dns_name_init(&amtrelay->gateway); |
317 | 0 | amtrelay->data = NULL; |
318 | |
|
319 | 0 | dns_name_init(&name); |
320 | 0 | dns_rdata_toregion(rdata, ®ion); |
321 | |
|
322 | 0 | amtrelay->precedence = uint8_fromregion(®ion); |
323 | 0 | isc_region_consume(®ion, 1); |
324 | |
|
325 | 0 | amtrelay->gateway_type = uint8_fromregion(®ion); |
326 | 0 | amtrelay->discovery = (amtrelay->gateway_type & 0x80) != 0; |
327 | 0 | amtrelay->gateway_type &= 0x7f; |
328 | 0 | isc_region_consume(®ion, 1); |
329 | |
|
330 | 0 | switch (amtrelay->gateway_type) { |
331 | 0 | case 0: |
332 | 0 | break; |
333 | | |
334 | 0 | case 1: |
335 | 0 | n = uint32_fromregion(®ion); |
336 | 0 | amtrelay->in_addr.s_addr = htonl(n); |
337 | 0 | isc_region_consume(®ion, 4); |
338 | 0 | break; |
339 | | |
340 | 0 | case 2: |
341 | 0 | memmove(amtrelay->in6_addr.s6_addr, region.base, 16); |
342 | 0 | isc_region_consume(®ion, 16); |
343 | 0 | break; |
344 | | |
345 | 0 | case 3: |
346 | 0 | dns_name_fromregion(&name, ®ion); |
347 | 0 | name_duporclone(&name, mctx, &amtrelay->gateway); |
348 | 0 | isc_region_consume(®ion, name_length(&name)); |
349 | 0 | break; |
350 | | |
351 | 0 | default: |
352 | 0 | if (region.length != 0) { |
353 | 0 | amtrelay->data = mem_maybedup(mctx, region.base, |
354 | 0 | region.length); |
355 | 0 | } |
356 | 0 | amtrelay->length = region.length; |
357 | 0 | } |
358 | 0 | amtrelay->mctx = mctx; |
359 | 0 | return ISC_R_SUCCESS; |
360 | 0 | } |
361 | | |
362 | | static void |
363 | 0 | freestruct_amtrelay(ARGS_FREESTRUCT) { |
364 | 0 | dns_rdata_amtrelay_t *amtrelay = source; |
365 | |
|
366 | 0 | REQUIRE(amtrelay != NULL); |
367 | 0 | REQUIRE(amtrelay->common.rdtype == dns_rdatatype_amtrelay); |
368 | |
|
369 | 0 | if (amtrelay->mctx == NULL) { |
370 | 0 | return; |
371 | 0 | } |
372 | | |
373 | 0 | if (amtrelay->gateway_type == 3) { |
374 | 0 | dns_name_free(&amtrelay->gateway, amtrelay->mctx); |
375 | 0 | } |
376 | |
|
377 | 0 | if (amtrelay->data != NULL) { |
378 | 0 | isc_mem_free(amtrelay->mctx, amtrelay->data); |
379 | 0 | } |
380 | |
|
381 | 0 | amtrelay->mctx = NULL; |
382 | 0 | } |
383 | | |
384 | | static isc_result_t |
385 | 0 | additionaldata_amtrelay(ARGS_ADDLDATA) { |
386 | 0 | REQUIRE(rdata->type == dns_rdatatype_amtrelay); |
387 | |
|
388 | 0 | UNUSED(rdata); |
389 | 0 | UNUSED(owner); |
390 | 0 | UNUSED(add); |
391 | 0 | UNUSED(arg); |
392 | |
|
393 | 0 | return ISC_R_SUCCESS; |
394 | 0 | } |
395 | | |
396 | | static isc_result_t |
397 | 0 | digest_amtrelay(ARGS_DIGEST) { |
398 | 0 | isc_region_t region; |
399 | |
|
400 | 0 | REQUIRE(rdata->type == dns_rdatatype_amtrelay); |
401 | |
|
402 | 0 | dns_rdata_toregion(rdata, ®ion); |
403 | 0 | return (digest)(arg, ®ion); |
404 | 0 | } |
405 | | |
406 | | static bool |
407 | 0 | checkowner_amtrelay(ARGS_CHECKOWNER) { |
408 | 0 | REQUIRE(type == dns_rdatatype_amtrelay); |
409 | |
|
410 | 0 | UNUSED(name); |
411 | 0 | UNUSED(type); |
412 | 0 | UNUSED(rdclass); |
413 | 0 | UNUSED(wildcard); |
414 | |
|
415 | 0 | return true; |
416 | 0 | } |
417 | | |
418 | | static bool |
419 | 0 | checknames_amtrelay(ARGS_CHECKNAMES) { |
420 | 0 | REQUIRE(rdata->type == dns_rdatatype_amtrelay); |
421 | |
|
422 | 0 | UNUSED(rdata); |
423 | 0 | UNUSED(owner); |
424 | 0 | UNUSED(bad); |
425 | |
|
426 | 0 | return true; |
427 | 0 | } |
428 | | |
429 | | static int |
430 | 0 | casecompare_amtrelay(ARGS_COMPARE) { |
431 | 0 | isc_region_t region1; |
432 | 0 | isc_region_t region2; |
433 | 0 | dns_name_t name1; |
434 | 0 | dns_name_t name2; |
435 | |
|
436 | 0 | REQUIRE(rdata1->type == rdata2->type); |
437 | 0 | REQUIRE(rdata1->rdclass == rdata2->rdclass); |
438 | 0 | REQUIRE(rdata1->type == dns_rdatatype_amtrelay); |
439 | 0 | REQUIRE(rdata1->length >= 2); |
440 | 0 | REQUIRE(rdata2->length >= 2); |
441 | |
|
442 | 0 | dns_rdata_toregion(rdata1, ®ion1); |
443 | 0 | dns_rdata_toregion(rdata2, ®ion2); |
444 | |
|
445 | 0 | if (memcmp(region1.base, region2.base, 2) != 0 || |
446 | 0 | (region1.base[1] & 0x7f) != 3) |
447 | 0 | { |
448 | 0 | return isc_region_compare(®ion1, ®ion2); |
449 | 0 | } |
450 | | |
451 | 0 | dns_name_init(&name1); |
452 | 0 | dns_name_init(&name2); |
453 | |
|
454 | 0 | isc_region_consume(®ion1, 2); |
455 | 0 | isc_region_consume(®ion2, 2); |
456 | |
|
457 | 0 | dns_name_fromregion(&name1, ®ion1); |
458 | 0 | dns_name_fromregion(&name2, ®ion2); |
459 | |
|
460 | 0 | return dns_name_rdatacompare(&name1, &name2); |
461 | 0 | } |
462 | | |
463 | | #endif /* RDATA_GENERIC_AMTRELAY_260_C */ |