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