xref: /openbsd/usr.bin/dig/lib/dns/rdata/generic/nxt_30.c (revision d415bd75)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: nxt_30.c,v 1.13 2020/09/14 08:40:43 florian Exp $ */
18 
19 /* reviewed: Wed Mar 15 18:21:15 PST 2000 by brister */
20 
21 /* RFC2535 */
22 
23 #ifndef RDATA_GENERIC_NXT_30_C
24 #define RDATA_GENERIC_NXT_30_C
25 
26 /*
27  * The attributes do not include DNS_RDATATYPEATTR_SINGLETON
28  * because we must be able to handle a parent/child NXT pair.
29  */
30 
31 static inline isc_result_t
32 totext_nxt(ARGS_TOTEXT) {
33 	isc_region_t sr;
34 	unsigned int i, j;
35 	dns_name_t name;
36 	dns_name_t prefix;
37 	int sub;
38 
39 	REQUIRE(rdata->type == dns_rdatatype_nxt);
40 	REQUIRE(rdata->length != 0);
41 
42 	dns_name_init(&name, NULL);
43 	dns_name_init(&prefix, NULL);
44 	dns_rdata_toregion(rdata, &sr);
45 	dns_name_fromregion(&name, &sr);
46 	isc_region_consume(&sr, name_length(&name));
47 	sub = name_prefix(&name, tctx->origin, &prefix);
48 	RETERR(dns_name_totext(&prefix, sub, target));
49 
50 	for (i = 0; i < sr.length; i++) {
51 		if (sr.base[i] != 0)
52 			for (j = 0; j < 8; j++)
53 				if ((sr.base[i] & (0x80 >> j)) != 0) {
54 					dns_rdatatype_t t = i * 8 + j;
55 					RETERR(isc_str_tobuffer(" ", target));
56 					RETERR(dns_rdatatype_totext(t, target));
57 				}
58 	}
59 	return (ISC_R_SUCCESS);
60 }
61 
62 static inline isc_result_t
63 fromwire_nxt(ARGS_FROMWIRE) {
64 	isc_region_t sr;
65 	dns_name_t name;
66 
67 	REQUIRE(type == dns_rdatatype_nxt);
68 
69 	UNUSED(type);
70 	UNUSED(rdclass);
71 
72 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
73 
74 	dns_name_init(&name, NULL);
75 	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
76 
77 	isc_buffer_activeregion(source, &sr);
78 	if (sr.length > 0 && (sr.base[0] & 0x80) == 0 &&
79 	    ((sr.length > 16) || sr.base[sr.length - 1] == 0))
80 		return (DNS_R_BADBITMAP);
81 	RETERR(isc_mem_tobuffer(target, sr.base, sr.length));
82 	isc_buffer_forward(source, sr.length);
83 	return (ISC_R_SUCCESS);
84 }
85 
86 static inline isc_result_t
87 towire_nxt(ARGS_TOWIRE) {
88 	isc_region_t sr;
89 	dns_name_t name;
90 	dns_offsets_t offsets;
91 
92 	REQUIRE(rdata->type == dns_rdatatype_nxt);
93 	REQUIRE(rdata->length != 0);
94 
95 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
96 	dns_name_init(&name, offsets);
97 	dns_rdata_toregion(rdata, &sr);
98 	dns_name_fromregion(&name, &sr);
99 	isc_region_consume(&sr, name_length(&name));
100 	RETERR(dns_name_towire(&name, cctx, target));
101 
102 	return (isc_mem_tobuffer(target, sr.base, sr.length));
103 }
104 
105 #endif	/* RDATA_GENERIC_NXT_30_C */
106