xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/generic/isdn_20.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: isdn_20.c,v 1.10 2025/01/26 16:25: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 /* RFC1183 */
17 
18 #ifndef RDATA_GENERIC_ISDN_20_C
19 #define RDATA_GENERIC_ISDN_20_C
20 
21 #define RRTYPE_ISDN_ATTRIBUTES (0)
22 
23 static isc_result_t
24 fromtext_isdn(ARGS_FROMTEXT) {
25 	isc_token_t token;
26 
27 	REQUIRE(type == dns_rdatatype_isdn);
28 
29 	UNUSED(type);
30 	UNUSED(rdclass);
31 	UNUSED(origin);
32 	UNUSED(options);
33 	UNUSED(callbacks);
34 
35 	/* ISDN-address */
36 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
37 				      false));
38 	RETTOK(txt_fromtext(&token.value.as_textregion, target));
39 
40 	/* sa: optional */
41 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
42 				      true));
43 	if (token.type != isc_tokentype_string &&
44 	    token.type != isc_tokentype_qstring)
45 	{
46 		isc_lex_ungettoken(lexer, &token);
47 		return ISC_R_SUCCESS;
48 	}
49 	RETTOK(txt_fromtext(&token.value.as_textregion, target));
50 	return ISC_R_SUCCESS;
51 }
52 
53 static isc_result_t
54 totext_isdn(ARGS_TOTEXT) {
55 	isc_region_t region;
56 
57 	REQUIRE(rdata->type == dns_rdatatype_isdn);
58 	REQUIRE(rdata->length != 0);
59 
60 	UNUSED(tctx);
61 
62 	dns_rdata_toregion(rdata, &region);
63 	RETERR(txt_totext(&region, true, target));
64 	if (region.length == 0) {
65 		return ISC_R_SUCCESS;
66 	}
67 	RETERR(str_totext(" ", target));
68 	return txt_totext(&region, true, target);
69 }
70 
71 static isc_result_t
72 fromwire_isdn(ARGS_FROMWIRE) {
73 	REQUIRE(type == dns_rdatatype_isdn);
74 
75 	UNUSED(type);
76 	UNUSED(dctx);
77 	UNUSED(rdclass);
78 
79 	RETERR(txt_fromwire(source, target));
80 	if (buffer_empty(source)) {
81 		return ISC_R_SUCCESS;
82 	}
83 	return txt_fromwire(source, target);
84 }
85 
86 static isc_result_t
87 towire_isdn(ARGS_TOWIRE) {
88 	UNUSED(cctx);
89 
90 	REQUIRE(rdata->type == dns_rdatatype_isdn);
91 	REQUIRE(rdata->length != 0);
92 
93 	return mem_tobuffer(target, rdata->data, rdata->length);
94 }
95 
96 static int
97 compare_isdn(ARGS_COMPARE) {
98 	isc_region_t r1;
99 	isc_region_t r2;
100 
101 	REQUIRE(rdata1->type == rdata2->type);
102 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
103 	REQUIRE(rdata1->type == dns_rdatatype_isdn);
104 	REQUIRE(rdata1->length != 0);
105 	REQUIRE(rdata2->length != 0);
106 
107 	dns_rdata_toregion(rdata1, &r1);
108 	dns_rdata_toregion(rdata2, &r2);
109 	return isc_region_compare(&r1, &r2);
110 }
111 
112 static isc_result_t
113 fromstruct_isdn(ARGS_FROMSTRUCT) {
114 	dns_rdata_isdn_t *isdn = source;
115 
116 	REQUIRE(type == dns_rdatatype_isdn);
117 	REQUIRE(isdn != NULL);
118 	REQUIRE(isdn->common.rdtype == type);
119 	REQUIRE(isdn->common.rdclass == rdclass);
120 
121 	UNUSED(type);
122 	UNUSED(rdclass);
123 
124 	RETERR(uint8_tobuffer(isdn->isdn_len, target));
125 	RETERR(mem_tobuffer(target, isdn->isdn, isdn->isdn_len));
126 	if (isdn->subaddress == NULL) {
127 		return ISC_R_SUCCESS;
128 	}
129 	RETERR(uint8_tobuffer(isdn->subaddress_len, target));
130 	return mem_tobuffer(target, isdn->subaddress, isdn->subaddress_len);
131 }
132 
133 static isc_result_t
134 tostruct_isdn(ARGS_TOSTRUCT) {
135 	dns_rdata_isdn_t *isdn = target;
136 	isc_region_t r;
137 
138 	REQUIRE(rdata->type == dns_rdatatype_isdn);
139 	REQUIRE(isdn != NULL);
140 	REQUIRE(rdata->length != 0);
141 
142 	isdn->common.rdclass = rdata->rdclass;
143 	isdn->common.rdtype = rdata->type;
144 	ISC_LINK_INIT(&isdn->common, link);
145 
146 	dns_rdata_toregion(rdata, &r);
147 
148 	isdn->isdn_len = uint8_fromregion(&r);
149 	isc_region_consume(&r, 1);
150 	isdn->isdn = mem_maybedup(mctx, r.base, isdn->isdn_len);
151 	isc_region_consume(&r, isdn->isdn_len);
152 
153 	if (r.length == 0) {
154 		isdn->subaddress_len = 0;
155 		isdn->subaddress = NULL;
156 	} else {
157 		isdn->subaddress_len = uint8_fromregion(&r);
158 		isc_region_consume(&r, 1);
159 		isdn->subaddress = mem_maybedup(mctx, r.base,
160 						isdn->subaddress_len);
161 	}
162 
163 	isdn->mctx = mctx;
164 	return ISC_R_SUCCESS;
165 }
166 
167 static void
168 freestruct_isdn(ARGS_FREESTRUCT) {
169 	dns_rdata_isdn_t *isdn = source;
170 
171 	REQUIRE(isdn != NULL);
172 
173 	if (isdn->mctx == NULL) {
174 		return;
175 	}
176 
177 	if (isdn->isdn != NULL) {
178 		isc_mem_free(isdn->mctx, isdn->isdn);
179 	}
180 	if (isdn->subaddress != NULL) {
181 		isc_mem_free(isdn->mctx, isdn->subaddress);
182 	}
183 	isdn->mctx = NULL;
184 }
185 
186 static isc_result_t
187 additionaldata_isdn(ARGS_ADDLDATA) {
188 	REQUIRE(rdata->type == dns_rdatatype_isdn);
189 
190 	UNUSED(rdata);
191 	UNUSED(owner);
192 	UNUSED(add);
193 	UNUSED(arg);
194 
195 	return ISC_R_SUCCESS;
196 }
197 
198 static isc_result_t
199 digest_isdn(ARGS_DIGEST) {
200 	isc_region_t r;
201 
202 	REQUIRE(rdata->type == dns_rdatatype_isdn);
203 
204 	dns_rdata_toregion(rdata, &r);
205 
206 	return (digest)(arg, &r);
207 }
208 
209 static bool
210 checkowner_isdn(ARGS_CHECKOWNER) {
211 	REQUIRE(type == dns_rdatatype_isdn);
212 
213 	UNUSED(name);
214 	UNUSED(type);
215 	UNUSED(rdclass);
216 	UNUSED(wildcard);
217 
218 	return true;
219 }
220 
221 static bool
222 checknames_isdn(ARGS_CHECKNAMES) {
223 	REQUIRE(rdata->type == dns_rdatatype_isdn);
224 
225 	UNUSED(rdata);
226 	UNUSED(owner);
227 	UNUSED(bad);
228 
229 	return true;
230 }
231 
232 static int
233 casecompare_isdn(ARGS_COMPARE) {
234 	return compare_isdn(rdata1, rdata2);
235 }
236 
237 #endif /* RDATA_GENERIC_ISDN_20_C */
238