1 /*	$NetBSD: x25_19.c,v 1.4 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2002  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: x25_19.c,v 1.41 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Thu Mar 16 16:15:57 PST 2000 by bwelling */
23 
24 /* RFC1183 */
25 
26 #ifndef RDATA_GENERIC_X25_19_C
27 #define RDATA_GENERIC_X25_19_C
28 
29 #define RRTYPE_X25_ATTRIBUTES (0)
30 
31 static inline isc_result_t
fromtext_x25(ARGS_FROMTEXT)32 fromtext_x25(ARGS_FROMTEXT) {
33 	isc_token_t token;
34 	unsigned int i;
35 
36 	REQUIRE(type == 19);
37 
38 	UNUSED(type);
39 	UNUSED(rdclass);
40 	UNUSED(origin);
41 	UNUSED(options);
42 	UNUSED(callbacks);
43 
44 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
45 				      ISC_FALSE));
46 	if (token.value.as_textregion.length < 4)
47 		RETTOK(DNS_R_SYNTAX);
48 	for (i = 0; i < token.value.as_textregion.length; i++)
49 		if (!isdigit(token.value.as_textregion.base[i] & 0xff))
50 			RETTOK(ISC_R_RANGE);
51 	RETTOK(txt_fromtext(&token.value.as_textregion, target));
52 	return (ISC_R_SUCCESS);
53 }
54 
55 static inline isc_result_t
totext_x25(ARGS_TOTEXT)56 totext_x25(ARGS_TOTEXT) {
57 	isc_region_t region;
58 
59 	UNUSED(tctx);
60 
61 	REQUIRE(rdata->type == 19);
62 	REQUIRE(rdata->length != 0);
63 
64 	dns_rdata_toregion(rdata, &region);
65 	return (txt_totext(&region, ISC_TRUE, target));
66 }
67 
68 static inline isc_result_t
fromwire_x25(ARGS_FROMWIRE)69 fromwire_x25(ARGS_FROMWIRE) {
70 	isc_region_t sr;
71 
72 	REQUIRE(type == 19);
73 
74 	UNUSED(type);
75 	UNUSED(dctx);
76 	UNUSED(rdclass);
77 	UNUSED(options);
78 
79 	isc_buffer_activeregion(source, &sr);
80 	if (sr.length < 5)
81 		return (DNS_R_FORMERR);
82 	return (txt_fromwire(source, target));
83 }
84 
85 static inline isc_result_t
towire_x25(ARGS_TOWIRE)86 towire_x25(ARGS_TOWIRE) {
87 	UNUSED(cctx);
88 
89 	REQUIRE(rdata->type == 19);
90 	REQUIRE(rdata->length != 0);
91 
92 	return (mem_tobuffer(target, rdata->data, rdata->length));
93 }
94 
95 static inline int
compare_x25(ARGS_COMPARE)96 compare_x25(ARGS_COMPARE) {
97 	isc_region_t r1;
98 	isc_region_t r2;
99 
100 	REQUIRE(rdata1->type == rdata2->type);
101 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
102 	REQUIRE(rdata1->type == 19);
103 	REQUIRE(rdata1->length != 0);
104 	REQUIRE(rdata2->length != 0);
105 
106 	dns_rdata_toregion(rdata1, &r1);
107 	dns_rdata_toregion(rdata2, &r2);
108 	return (isc_region_compare(&r1, &r2));
109 }
110 
111 static inline isc_result_t
fromstruct_x25(ARGS_FROMSTRUCT)112 fromstruct_x25(ARGS_FROMSTRUCT) {
113 	dns_rdata_x25_t *x25 = source;
114 	isc_uint8_t i;
115 
116 	REQUIRE(type == 19);
117 	REQUIRE(source != NULL);
118 	REQUIRE(x25->common.rdtype == type);
119 	REQUIRE(x25->common.rdclass == rdclass);
120 	REQUIRE(x25->x25 != NULL && x25->x25_len != 0);
121 
122 	UNUSED(type);
123 	UNUSED(rdclass);
124 
125 	if (x25->x25_len < 4)
126 		return (ISC_R_RANGE);
127 
128 	for (i = 0; i < x25->x25_len; i++)
129 		if (!isdigit(x25->x25[i] & 0xff))
130 			return (ISC_R_RANGE);
131 
132 	RETERR(uint8_tobuffer(x25->x25_len, target));
133 	return (mem_tobuffer(target, x25->x25, x25->x25_len));
134 }
135 
136 static inline isc_result_t
tostruct_x25(ARGS_TOSTRUCT)137 tostruct_x25(ARGS_TOSTRUCT) {
138 	dns_rdata_x25_t *x25 = target;
139 	isc_region_t r;
140 
141 	REQUIRE(rdata->type == 19);
142 	REQUIRE(target != NULL);
143 	REQUIRE(rdata->length != 0);
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 = source;
163 	REQUIRE(source != NULL);
164 	REQUIRE(x25->common.rdtype == 19);
165 
166 	if (x25->mctx == NULL)
167 		return;
168 
169 	if (x25->x25 != NULL)
170 		isc_mem_free(x25->mctx, x25->x25);
171 	x25->mctx = NULL;
172 }
173 
174 static inline isc_result_t
additionaldata_x25(ARGS_ADDLDATA)175 additionaldata_x25(ARGS_ADDLDATA) {
176 	REQUIRE(rdata->type == 19);
177 
178 	UNUSED(rdata);
179 	UNUSED(add);
180 	UNUSED(arg);
181 
182 	return (ISC_R_SUCCESS);
183 }
184 
185 static inline isc_result_t
digest_x25(ARGS_DIGEST)186 digest_x25(ARGS_DIGEST) {
187 	isc_region_t r;
188 
189 	REQUIRE(rdata->type == 19);
190 
191 	dns_rdata_toregion(rdata, &r);
192 
193 	return ((digest)(arg, &r));
194 }
195 
196 static inline isc_boolean_t
checkowner_x25(ARGS_CHECKOWNER)197 checkowner_x25(ARGS_CHECKOWNER) {
198 
199 	REQUIRE(type == 19);
200 
201 	UNUSED(name);
202 	UNUSED(type);
203 	UNUSED(rdclass);
204 	UNUSED(wildcard);
205 
206 	return (ISC_TRUE);
207 }
208 
209 static inline isc_boolean_t
checknames_x25(ARGS_CHECKNAMES)210 checknames_x25(ARGS_CHECKNAMES) {
211 
212 	REQUIRE(rdata->type == 19);
213 
214 	UNUSED(rdata);
215 	UNUSED(owner);
216 	UNUSED(bad);
217 
218 	return (ISC_TRUE);
219 }
220 
221 static inline int
casecompare_x25(ARGS_COMPARE)222 casecompare_x25(ARGS_COMPARE) {
223 	return (compare_x25(rdata1, rdata2));
224 }
225 
226 #endif	/* RDATA_GENERIC_X25_19_C */
227