xref: /minix3/external/bsd/bind/dist/lib/dns/rdata/in_1/nsap_22.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: nsap_22.c,v 1.5 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2013  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: nsap_22.c,v 1.44 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Fri Mar 17 10:41:07 PST 2000 by gson */
23 
24 /* RFC1706 */
25 
26 #ifndef RDATA_IN_1_NSAP_22_C
27 #define RDATA_IN_1_NSAP_22_C
28 
29 #define RRTYPE_NSAP_ATTRIBUTES (0)
30 
31 static inline isc_result_t
fromtext_in_nsap(ARGS_FROMTEXT)32 fromtext_in_nsap(ARGS_FROMTEXT) {
33 	isc_token_t token;
34 	isc_textregion_t *sr;
35 	int n;
36 	int digits;
37 	unsigned char c = 0;
38 
39 	REQUIRE(type == 22);
40 	REQUIRE(rdclass == 1);
41 
42 	UNUSED(type);
43 	UNUSED(origin);
44 	UNUSED(options);
45 	UNUSED(rdclass);
46 	UNUSED(callbacks);
47 
48 	/* 0x<hex.string.with.periods> */
49 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
50 				      ISC_FALSE));
51 	sr = &token.value.as_textregion;
52 	if (sr->length < 2)
53 		RETTOK(ISC_R_UNEXPECTEDEND);
54 	if (sr->base[0] != '0' || (sr->base[1] != 'x' && sr->base[1] != 'X'))
55 		RETTOK(DNS_R_SYNTAX);
56 	isc_textregion_consume(sr, 2);
57 	digits = 0;
58 	while (sr->length > 0) {
59 		if (sr->base[0] == '.') {
60 			isc_textregion_consume(sr, 1);
61 			continue;
62 		}
63 		if ((n = hexvalue(sr->base[0])) == -1)
64 			RETTOK(DNS_R_SYNTAX);
65 		c <<= 4;
66 		c += n;
67 		if (++digits == 2) {
68 			RETERR(mem_tobuffer(target, &c, 1));
69 			digits = 0;
70 		}
71 		isc_textregion_consume(sr, 1);
72 	}
73 	if (digits)
74 		RETTOK(ISC_R_UNEXPECTEDEND);
75 	return (ISC_R_SUCCESS);
76 }
77 
78 static inline isc_result_t
totext_in_nsap(ARGS_TOTEXT)79 totext_in_nsap(ARGS_TOTEXT) {
80 	isc_region_t region;
81 	char buf[sizeof("xx")];
82 
83 	REQUIRE(rdata->type == 22);
84 	REQUIRE(rdata->rdclass == 1);
85 	REQUIRE(rdata->length != 0);
86 
87 	UNUSED(tctx);
88 
89 	dns_rdata_toregion(rdata, &region);
90 	RETERR(str_totext("0x", target));
91 	while (region.length != 0) {
92 		sprintf(buf, "%02x", region.base[0]);
93 		isc_region_consume(&region, 1);
94 		RETERR(str_totext(buf, target));
95 	}
96 	return (ISC_R_SUCCESS);
97 }
98 
99 static inline isc_result_t
fromwire_in_nsap(ARGS_FROMWIRE)100 fromwire_in_nsap(ARGS_FROMWIRE) {
101 	isc_region_t region;
102 
103 	REQUIRE(type == 22);
104 	REQUIRE(rdclass == 1);
105 
106 	UNUSED(type);
107 	UNUSED(dctx);
108 	UNUSED(options);
109 	UNUSED(rdclass);
110 
111 	isc_buffer_activeregion(source, &region);
112 	if (region.length < 1)
113 		return (ISC_R_UNEXPECTEDEND);
114 
115 	RETERR(mem_tobuffer(target, region.base, region.length));
116 	isc_buffer_forward(source, region.length);
117 	return (ISC_R_SUCCESS);
118 }
119 
120 static inline isc_result_t
towire_in_nsap(ARGS_TOWIRE)121 towire_in_nsap(ARGS_TOWIRE) {
122 	REQUIRE(rdata->type == 22);
123 	REQUIRE(rdata->rdclass == 1);
124 	REQUIRE(rdata->length != 0);
125 
126 	UNUSED(cctx);
127 
128 	return (mem_tobuffer(target, rdata->data, rdata->length));
129 }
130 
131 static inline int
compare_in_nsap(ARGS_COMPARE)132 compare_in_nsap(ARGS_COMPARE) {
133 	isc_region_t r1;
134 	isc_region_t r2;
135 
136 	REQUIRE(rdata1->type == rdata2->type);
137 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
138 	REQUIRE(rdata1->type == 22);
139 	REQUIRE(rdata1->rdclass == 1);
140 	REQUIRE(rdata1->length != 0);
141 	REQUIRE(rdata2->length != 0);
142 
143 	dns_rdata_toregion(rdata1, &r1);
144 	dns_rdata_toregion(rdata2, &r2);
145 	return (isc_region_compare(&r1, &r2));
146 }
147 
148 static inline isc_result_t
fromstruct_in_nsap(ARGS_FROMSTRUCT)149 fromstruct_in_nsap(ARGS_FROMSTRUCT) {
150 	dns_rdata_in_nsap_t *nsap = source;
151 
152 	REQUIRE(type == 22);
153 	REQUIRE(rdclass == 1);
154 	REQUIRE(source != NULL);
155 	REQUIRE(nsap->common.rdtype == type);
156 	REQUIRE(nsap->common.rdclass == rdclass);
157 	REQUIRE(nsap->nsap != NULL || nsap->nsap_len == 0);
158 
159 	UNUSED(type);
160 	UNUSED(rdclass);
161 
162 	return (mem_tobuffer(target, nsap->nsap, nsap->nsap_len));
163 }
164 
165 static inline isc_result_t
tostruct_in_nsap(ARGS_TOSTRUCT)166 tostruct_in_nsap(ARGS_TOSTRUCT) {
167 	dns_rdata_in_nsap_t *nsap = target;
168 	isc_region_t r;
169 
170 	REQUIRE(rdata->type == 22);
171 	REQUIRE(rdata->rdclass == 1);
172 	REQUIRE(target != NULL);
173 	REQUIRE(rdata->length != 0);
174 
175 	nsap->common.rdclass = rdata->rdclass;
176 	nsap->common.rdtype = rdata->type;
177 	ISC_LINK_INIT(&nsap->common, link);
178 
179 	dns_rdata_toregion(rdata, &r);
180 	nsap->nsap_len = r.length;
181 	nsap->nsap = mem_maybedup(mctx, r.base, r.length);
182 	if (nsap->nsap == NULL)
183 		return (ISC_R_NOMEMORY);
184 
185 	nsap->mctx = mctx;
186 	return (ISC_R_SUCCESS);
187 }
188 
189 static inline void
freestruct_in_nsap(ARGS_FREESTRUCT)190 freestruct_in_nsap(ARGS_FREESTRUCT) {
191 	dns_rdata_in_nsap_t *nsap = source;
192 
193 	REQUIRE(source != NULL);
194 	REQUIRE(nsap->common.rdclass == 1);
195 	REQUIRE(nsap->common.rdtype == 22);
196 
197 	if (nsap->mctx == NULL)
198 		return;
199 
200 	if (nsap->nsap != NULL)
201 		isc_mem_free(nsap->mctx, nsap->nsap);
202 	nsap->mctx = NULL;
203 }
204 
205 static inline isc_result_t
additionaldata_in_nsap(ARGS_ADDLDATA)206 additionaldata_in_nsap(ARGS_ADDLDATA) {
207 	REQUIRE(rdata->type == 22);
208 	REQUIRE(rdata->rdclass == 1);
209 
210 	UNUSED(rdata);
211 	UNUSED(add);
212 	UNUSED(arg);
213 
214 	return (ISC_R_SUCCESS);
215 }
216 
217 static inline isc_result_t
digest_in_nsap(ARGS_DIGEST)218 digest_in_nsap(ARGS_DIGEST) {
219 	isc_region_t r;
220 
221 	REQUIRE(rdata->type == 22);
222 	REQUIRE(rdata->rdclass == 1);
223 
224 	dns_rdata_toregion(rdata, &r);
225 
226 	return ((digest)(arg, &r));
227 }
228 
229 static inline isc_boolean_t
checkowner_in_nsap(ARGS_CHECKOWNER)230 checkowner_in_nsap(ARGS_CHECKOWNER) {
231 
232 	REQUIRE(type == 22);
233 	REQUIRE(rdclass == 1);
234 
235 	UNUSED(name);
236 	UNUSED(type);
237 	UNUSED(rdclass);
238 	UNUSED(wildcard);
239 
240 	return (ISC_TRUE);
241 }
242 
243 static inline isc_boolean_t
checknames_in_nsap(ARGS_CHECKNAMES)244 checknames_in_nsap(ARGS_CHECKNAMES) {
245 
246 	REQUIRE(rdata->type == 22);
247 	REQUIRE(rdata->rdclass == 1);
248 
249 	UNUSED(rdata);
250 	UNUSED(owner);
251 	UNUSED(bad);
252 
253 	return (ISC_TRUE);
254 }
255 
256 static inline int
casecompare_in_nsap(ARGS_COMPARE)257 casecompare_in_nsap(ARGS_COMPARE) {
258 	return (compare_in_nsap(rdata1, rdata2));
259 }
260 
261 #endif	/* RDATA_IN_1_NSAP_22_C */
262