1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /* RFC1183 */
13 
14 #ifndef RDATA_GENERIC_X25_19_C
15 #define RDATA_GENERIC_X25_19_C
16 
17 #define RRTYPE_X25_ATTRIBUTES (0)
18 
19 static inline isc_result_t
fromtext_x25(ARGS_FROMTEXT)20 fromtext_x25(ARGS_FROMTEXT) {
21 	isc_token_t token;
22 	unsigned int i;
23 
24 	REQUIRE(type == dns_rdatatype_x25);
25 
26 	UNUSED(type);
27 	UNUSED(rdclass);
28 	UNUSED(origin);
29 	UNUSED(options);
30 	UNUSED(callbacks);
31 
32 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
33 				      false));
34 	if (token.value.as_textregion.length < 4)
35 		RETTOK(DNS_R_SYNTAX);
36 	for (i = 0; i < token.value.as_textregion.length; i++)
37 		if (!isdigit(token.value.as_textregion.base[i] & 0xff))
38 			RETTOK(ISC_R_RANGE);
39 	RETTOK(txt_fromtext(&token.value.as_textregion, target));
40 	return (ISC_R_SUCCESS);
41 }
42 
43 static inline isc_result_t
totext_x25(ARGS_TOTEXT)44 totext_x25(ARGS_TOTEXT) {
45 	isc_region_t region;
46 
47 	UNUSED(tctx);
48 
49 	REQUIRE(rdata->type == dns_rdatatype_x25);
50 	REQUIRE(rdata->length != 0);
51 
52 	dns_rdata_toregion(rdata, &region);
53 	return (txt_totext(&region, true, target));
54 }
55 
56 static inline isc_result_t
fromwire_x25(ARGS_FROMWIRE)57 fromwire_x25(ARGS_FROMWIRE) {
58 	isc_region_t sr;
59 	unsigned int i;
60 
61 	REQUIRE(type == dns_rdatatype_x25);
62 
63 	UNUSED(type);
64 	UNUSED(dctx);
65 	UNUSED(rdclass);
66 	UNUSED(options);
67 
68 	isc_buffer_activeregion(source, &sr);
69 	if (sr.length < 5 || sr.base[0] != (sr.length - 1)) {
70 		return (DNS_R_FORMERR);
71 	}
72 	for (i = 1; i < sr.length; i++) {
73 		if (sr.base[i] < 0x30 || sr.base[i] > 0x39) {
74 			return (DNS_R_FORMERR);
75 		}
76 	}
77 	return (txt_fromwire(source, target));
78 }
79 
80 static inline isc_result_t
towire_x25(ARGS_TOWIRE)81 towire_x25(ARGS_TOWIRE) {
82 	UNUSED(cctx);
83 
84 	REQUIRE(rdata->type == dns_rdatatype_x25);
85 	REQUIRE(rdata->length != 0);
86 
87 	return (mem_tobuffer(target, rdata->data, rdata->length));
88 }
89 
90 static inline int
compare_x25(ARGS_COMPARE)91 compare_x25(ARGS_COMPARE) {
92 	isc_region_t r1;
93 	isc_region_t r2;
94 
95 	REQUIRE(rdata1->type == rdata2->type);
96 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
97 	REQUIRE(rdata1->type == dns_rdatatype_x25);
98 	REQUIRE(rdata1->length != 0);
99 	REQUIRE(rdata2->length != 0);
100 
101 	dns_rdata_toregion(rdata1, &r1);
102 	dns_rdata_toregion(rdata2, &r2);
103 	return (isc_region_compare(&r1, &r2));
104 }
105 
106 static inline isc_result_t
fromstruct_x25(ARGS_FROMSTRUCT)107 fromstruct_x25(ARGS_FROMSTRUCT) {
108 	dns_rdata_x25_t *x25;
109 	uint8_t i;
110 
111 	REQUIRE(type == dns_rdatatype_x25);
112 	REQUIRE(((dns_rdata_x25_t *)source) != NULL);
113 	REQUIRE(((dns_rdata_x25_t *)source)->common.rdtype == type);
114 	REQUIRE(((dns_rdata_x25_t *)source)->common.rdclass == rdclass);
115 	REQUIRE(((dns_rdata_x25_t *)source)->x25 != NULL);
116 	REQUIRE(((dns_rdata_x25_t *)source)->x25_len != 0);
117 
118 	x25 = source;
119 
120 	UNUSED(type);
121 	UNUSED(rdclass);
122 
123 	if (x25->x25_len < 4)
124 		return (ISC_R_RANGE);
125 
126 	for (i = 0; i < x25->x25_len; i++)
127 		if (!isdigit(x25->x25[i] & 0xff))
128 			return (ISC_R_RANGE);
129 
130 	RETERR(uint8_tobuffer(x25->x25_len, target));
131 	return (mem_tobuffer(target, x25->x25, x25->x25_len));
132 }
133 
134 static inline isc_result_t
tostruct_x25(ARGS_TOSTRUCT)135 tostruct_x25(ARGS_TOSTRUCT) {
136 	dns_rdata_x25_t *x25;
137 	isc_region_t r;
138 
139 	REQUIRE(((dns_rdata_x25_t *)target) != NULL);
140 	REQUIRE(rdata->type == dns_rdatatype_x25);
141 	REQUIRE(rdata->length != 0);
142 
143 	x25 = target;
144 
145 	x25->common.rdclass = rdata->rdclass;
146 	x25->common.rdtype = rdata->type;
147 	ISC_LINK_INIT(&x25->common, link);
148 
149 	dns_rdata_toregion(rdata, &r);
150 	x25->x25_len = uint8_fromregion(&r);
151 	isc_region_consume(&r, 1);
152 	x25->x25 = mem_maybedup(mctx, r.base, x25->x25_len);
153 	if (x25->x25 == NULL)
154 		return (ISC_R_NOMEMORY);
155 
156 	x25->mctx = mctx;
157 	return (ISC_R_SUCCESS);
158 }
159 
160 static inline void
freestruct_x25(ARGS_FREESTRUCT)161 freestruct_x25(ARGS_FREESTRUCT) {
162 	dns_rdata_x25_t *x25;
163 
164 	REQUIRE(((dns_rdata_x25_t *)source) != NULL);
165 	REQUIRE(((dns_rdata_x25_t *)source)->common.rdtype ==
166 		dns_rdatatype_x25);
167 
168 	x25 = source;
169 
170 	if (x25->mctx == NULL)
171 		return;
172 
173 	if (x25->x25 != NULL)
174 		isc_mem_free(x25->mctx, x25->x25);
175 	x25->mctx = NULL;
176 }
177 
178 static inline isc_result_t
additionaldata_x25(ARGS_ADDLDATA)179 additionaldata_x25(ARGS_ADDLDATA) {
180 	REQUIRE(rdata->type == dns_rdatatype_x25);
181 
182 	UNUSED(rdata);
183 	UNUSED(add);
184 	UNUSED(arg);
185 
186 	return (ISC_R_SUCCESS);
187 }
188 
189 static inline isc_result_t
digest_x25(ARGS_DIGEST)190 digest_x25(ARGS_DIGEST) {
191 	isc_region_t r;
192 
193 	REQUIRE(rdata->type == dns_rdatatype_x25);
194 
195 	dns_rdata_toregion(rdata, &r);
196 
197 	return ((digest)(arg, &r));
198 }
199 
200 static inline bool
checkowner_x25(ARGS_CHECKOWNER)201 checkowner_x25(ARGS_CHECKOWNER) {
202 
203 	REQUIRE(type == dns_rdatatype_x25);
204 
205 	UNUSED(name);
206 	UNUSED(type);
207 	UNUSED(rdclass);
208 	UNUSED(wildcard);
209 
210 	return (true);
211 }
212 
213 static inline bool
checknames_x25(ARGS_CHECKNAMES)214 checknames_x25(ARGS_CHECKNAMES) {
215 
216 	REQUIRE(rdata->type == dns_rdatatype_x25);
217 
218 	UNUSED(rdata);
219 	UNUSED(owner);
220 	UNUSED(bad);
221 
222 	return (true);
223 }
224 
225 static inline int
casecompare_x25(ARGS_COMPARE)226 casecompare_x25(ARGS_COMPARE) {
227 	return (compare_x25(rdata1, rdata2));
228 }
229 
230 #endif	/* RDATA_GENERIC_X25_19_C */
231