1/*
2 * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: nsap_22.c,v 1.44 2009/12/04 22:06:37 tbox Exp $ */
19
20/* Reviewed: Fri Mar 17 10:41:07 PST 2000 by gson */
21
22/* RFC1706 */
23
24#ifndef RDATA_IN_1_NSAP_22_C
25#define RDATA_IN_1_NSAP_22_C
26
27#define RRTYPE_NSAP_ATTRIBUTES (0)
28
29static inline isc_result_t
30fromtext_in_nsap(ARGS_FROMTEXT) {
31	isc_token_t token;
32	isc_textregion_t *sr;
33	int n;
34	int digits;
35	unsigned char c = 0;
36
37	REQUIRE(type == 22);
38	REQUIRE(rdclass == 1);
39
40	UNUSED(type);
41	UNUSED(origin);
42	UNUSED(options);
43	UNUSED(rdclass);
44	UNUSED(callbacks);
45
46	/* 0x<hex.string.with.periods> */
47	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
48				      ISC_FALSE));
49	sr = &token.value.as_textregion;
50	if (sr->length < 2)
51		RETTOK(ISC_R_UNEXPECTEDEND);
52	if (sr->base[0] != '0' || (sr->base[1] != 'x' && sr->base[1] != 'X'))
53		RETTOK(DNS_R_SYNTAX);
54	isc_textregion_consume(sr, 2);
55	digits = 0;
56	n = 0;
57	while (sr->length > 0) {
58		if (sr->base[0] == '.') {
59			isc_textregion_consume(sr, 1);
60			continue;
61		}
62		if ((n = hexvalue(sr->base[0])) == -1)
63			RETTOK(DNS_R_SYNTAX);
64		c <<= 4;
65		c += n;
66		if (++digits == 2) {
67			RETERR(mem_tobuffer(target, &c, 1));
68			digits = 0;
69		}
70		isc_textregion_consume(sr, 1);
71	}
72	if (digits)
73		RETTOK(ISC_R_UNEXPECTEDEND);
74	return (ISC_R_SUCCESS);
75}
76
77static inline isc_result_t
78totext_in_nsap(ARGS_TOTEXT) {
79	isc_region_t region;
80	char buf[sizeof("xx")];
81
82	REQUIRE(rdata->type == 22);
83	REQUIRE(rdata->rdclass == 1);
84	REQUIRE(rdata->length != 0);
85
86	UNUSED(tctx);
87
88	dns_rdata_toregion(rdata, &region);
89	RETERR(str_totext("0x", target));
90	while (region.length != 0) {
91		sprintf(buf, "%02x", region.base[0]);
92		isc_region_consume(&region, 1);
93		RETERR(str_totext(buf, target));
94	}
95	return (ISC_R_SUCCESS);
96}
97
98static inline isc_result_t
99fromwire_in_nsap(ARGS_FROMWIRE) {
100	isc_region_t region;
101
102	REQUIRE(type == 22);
103	REQUIRE(rdclass == 1);
104
105	UNUSED(type);
106	UNUSED(dctx);
107	UNUSED(options);
108	UNUSED(rdclass);
109
110	isc_buffer_activeregion(source, &region);
111	if (region.length < 1)
112		return (ISC_R_UNEXPECTEDEND);
113
114	RETERR(mem_tobuffer(target, region.base, region.length));
115	isc_buffer_forward(source, region.length);
116	return (ISC_R_SUCCESS);
117}
118
119static inline isc_result_t
120towire_in_nsap(ARGS_TOWIRE) {
121	REQUIRE(rdata->type == 22);
122	REQUIRE(rdata->rdclass == 1);
123	REQUIRE(rdata->length != 0);
124
125	UNUSED(cctx);
126
127	return (mem_tobuffer(target, rdata->data, rdata->length));
128}
129
130static inline int
131compare_in_nsap(ARGS_COMPARE) {
132	isc_region_t r1;
133	isc_region_t r2;
134
135	REQUIRE(rdata1->type == rdata2->type);
136	REQUIRE(rdata1->rdclass == rdata2->rdclass);
137	REQUIRE(rdata1->type == 22);
138	REQUIRE(rdata1->rdclass == 1);
139	REQUIRE(rdata1->length != 0);
140	REQUIRE(rdata2->length != 0);
141
142	dns_rdata_toregion(rdata1, &r1);
143	dns_rdata_toregion(rdata2, &r2);
144	return (isc_region_compare(&r1, &r2));
145}
146
147static inline isc_result_t
148fromstruct_in_nsap(ARGS_FROMSTRUCT) {
149	dns_rdata_in_nsap_t *nsap = source;
150
151	REQUIRE(type == 22);
152	REQUIRE(rdclass == 1);
153	REQUIRE(source != NULL);
154	REQUIRE(nsap->common.rdtype == type);
155	REQUIRE(nsap->common.rdclass == rdclass);
156	REQUIRE(nsap->nsap != NULL || nsap->nsap_len == 0);
157
158	UNUSED(type);
159	UNUSED(rdclass);
160
161	return (mem_tobuffer(target, nsap->nsap, nsap->nsap_len));
162}
163
164static inline isc_result_t
165tostruct_in_nsap(ARGS_TOSTRUCT) {
166	dns_rdata_in_nsap_t *nsap = target;
167	isc_region_t r;
168
169	REQUIRE(rdata->type == 22);
170	REQUIRE(rdata->rdclass == 1);
171	REQUIRE(target != NULL);
172	REQUIRE(rdata->length != 0);
173
174	nsap->common.rdclass = rdata->rdclass;
175	nsap->common.rdtype = rdata->type;
176	ISC_LINK_INIT(&nsap->common, link);
177
178	dns_rdata_toregion(rdata, &r);
179	nsap->nsap_len = r.length;
180	nsap->nsap = mem_maybedup(mctx, r.base, r.length);
181	if (nsap->nsap == NULL)
182		return (ISC_R_NOMEMORY);
183
184	nsap->mctx = mctx;
185	return (ISC_R_SUCCESS);
186}
187
188static inline void
189freestruct_in_nsap(ARGS_FREESTRUCT) {
190	dns_rdata_in_nsap_t *nsap = source;
191
192	REQUIRE(source != NULL);
193	REQUIRE(nsap->common.rdclass == 1);
194	REQUIRE(nsap->common.rdtype == 22);
195
196	if (nsap->mctx == NULL)
197		return;
198
199	if (nsap->nsap != NULL)
200		isc_mem_free(nsap->mctx, nsap->nsap);
201	nsap->mctx = NULL;
202}
203
204static inline isc_result_t
205additionaldata_in_nsap(ARGS_ADDLDATA) {
206	REQUIRE(rdata->type == 22);
207	REQUIRE(rdata->rdclass == 1);
208
209	UNUSED(rdata);
210	UNUSED(add);
211	UNUSED(arg);
212
213	return (ISC_R_SUCCESS);
214}
215
216static inline isc_result_t
217digest_in_nsap(ARGS_DIGEST) {
218	isc_region_t r;
219
220	REQUIRE(rdata->type == 22);
221	REQUIRE(rdata->rdclass == 1);
222
223	dns_rdata_toregion(rdata, &r);
224
225	return ((digest)(arg, &r));
226}
227
228static inline isc_boolean_t
229checkowner_in_nsap(ARGS_CHECKOWNER) {
230
231	REQUIRE(type == 22);
232	REQUIRE(rdclass == 1);
233
234	UNUSED(name);
235	UNUSED(type);
236	UNUSED(rdclass);
237	UNUSED(wildcard);
238
239	return (ISC_TRUE);
240}
241
242static inline isc_boolean_t
243checknames_in_nsap(ARGS_CHECKNAMES) {
244
245	REQUIRE(rdata->type == 22);
246	REQUIRE(rdata->rdclass == 1);
247
248	UNUSED(rdata);
249	UNUSED(owner);
250	UNUSED(bad);
251
252	return (ISC_TRUE);
253}
254
255static inline int
256casecompare_in_nsap(ARGS_COMPARE) {
257	return (compare_in_nsap(rdata1, rdata2));
258}
259
260#endif	/* RDATA_IN_1_NSAP_22_C */
261