1 /*	$NetBSD: lp_107.c,v 1.1.1.3 2014/12/10 03:34:42 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef RDATA_GENERIC_LP_107_C
20 #define RDATA_GENERIC_LP_107_C
21 
22 #include <string.h>
23 
24 #include <isc/net.h>
25 
26 #define RRTYPE_LP_ATTRIBUTES (0)
27 
28 static inline isc_result_t
fromtext_lp(ARGS_FROMTEXT)29 fromtext_lp(ARGS_FROMTEXT) {
30 	isc_token_t token;
31 	dns_name_t name;
32 	isc_buffer_t buffer;
33 
34 	REQUIRE(type == 107);
35 
36 	UNUSED(type);
37 	UNUSED(rdclass);
38 	UNUSED(callbacks);
39 
40 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
41 				      ISC_FALSE));
42 	if (token.value.as_ulong > 0xffffU)
43 		RETTOK(ISC_R_RANGE);
44 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
45 
46 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
47 				      ISC_FALSE));
48 
49 	dns_name_init(&name, NULL);
50 	buffer_fromregion(&buffer, &token.value.as_region);
51 	origin = (origin != NULL) ? origin : dns_rootname;
52 	return (dns_name_fromtext(&name, &buffer, origin, options, target));
53 }
54 
55 static inline isc_result_t
totext_lp(ARGS_TOTEXT)56 totext_lp(ARGS_TOTEXT) {
57 	isc_region_t region;
58 	dns_name_t name;
59 	dns_name_t prefix;
60 	isc_boolean_t sub;
61 	char buf[sizeof("64000")];
62 	unsigned short num;
63 
64 	REQUIRE(rdata->type == 107);
65 	REQUIRE(rdata->length != 0);
66 
67 	dns_name_init(&name, NULL);
68 	dns_name_init(&prefix, NULL);
69 
70 	dns_rdata_toregion(rdata, &region);
71 	num = uint16_fromregion(&region);
72 	isc_region_consume(&region, 2);
73 	sprintf(buf, "%u", num);
74 	RETERR(str_totext(buf, target));
75 
76 	RETERR(str_totext(" ", target));
77 
78 	dns_name_fromregion(&name, &region);
79 	sub = name_prefix(&name, tctx->origin, &prefix);
80 	return (dns_name_totext(&prefix, sub, target));
81 }
82 
83 static inline isc_result_t
fromwire_lp(ARGS_FROMWIRE)84 fromwire_lp(ARGS_FROMWIRE) {
85 	dns_name_t name;
86 	isc_region_t sregion;
87 
88 	REQUIRE(type == 107);
89 
90 	UNUSED(type);
91 	UNUSED(rdclass);
92 
93 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
94 
95 	dns_name_init(&name, NULL);
96 
97 	isc_buffer_activeregion(source, &sregion);
98 	if (sregion.length < 2)
99 		return (ISC_R_UNEXPECTEDEND);
100 	RETERR(mem_tobuffer(target, sregion.base, 2));
101 	isc_buffer_forward(source, 2);
102 	return (dns_name_fromwire(&name, source, dctx, options, target));
103 }
104 
105 static inline isc_result_t
towire_lp(ARGS_TOWIRE)106 towire_lp(ARGS_TOWIRE) {
107 
108 	REQUIRE(rdata->type == 107);
109 	REQUIRE(rdata->length != 0);
110 
111 	UNUSED(cctx);
112 
113 	return (mem_tobuffer(target, rdata->data, rdata->length));
114 }
115 
116 static inline int
compare_lp(ARGS_COMPARE)117 compare_lp(ARGS_COMPARE) {
118 	isc_region_t region1;
119 	isc_region_t region2;
120 
121 	REQUIRE(rdata1->type == rdata2->type);
122 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
123 	REQUIRE(rdata1->type == 107);
124 	REQUIRE(rdata1->length != 0);
125 	REQUIRE(rdata2->length != 0);
126 
127 	dns_rdata_toregion(rdata1, &region1);
128 	dns_rdata_toregion(rdata2, &region2);
129 
130 	return (isc_region_compare(&region1, &region2));
131 }
132 
133 static inline isc_result_t
fromstruct_lp(ARGS_FROMSTRUCT)134 fromstruct_lp(ARGS_FROMSTRUCT) {
135 	dns_rdata_lp_t *lp = source;
136 	isc_region_t region;
137 
138 	REQUIRE(type == 107);
139 	REQUIRE(source != NULL);
140 	REQUIRE(lp->common.rdtype == type);
141 	REQUIRE(lp->common.rdclass == rdclass);
142 
143 	UNUSED(type);
144 	UNUSED(rdclass);
145 
146 	RETERR(uint16_tobuffer(lp->pref, target));
147 	dns_name_toregion(&lp->lp, &region);
148 	return (isc_buffer_copyregion(target, &region));
149 }
150 
151 static inline isc_result_t
tostruct_lp(ARGS_TOSTRUCT)152 tostruct_lp(ARGS_TOSTRUCT) {
153 	isc_region_t region;
154 	dns_rdata_lp_t *lp = target;
155 	dns_name_t name;
156 
157 	REQUIRE(rdata->type == 107);
158 	REQUIRE(target != NULL);
159 	REQUIRE(rdata->length != 0);
160 
161 	lp->common.rdclass = rdata->rdclass;
162 	lp->common.rdtype = rdata->type;
163 	ISC_LINK_INIT(&lp->common, link);
164 
165 	dns_name_init(&name, NULL);
166 	dns_rdata_toregion(rdata, &region);
167 	lp->pref = uint16_fromregion(&region);
168 	isc_region_consume(&region, 2);
169 	dns_name_fromregion(&name, &region);
170 	dns_name_init(&lp->lp, NULL);
171 	RETERR(name_duporclone(&name, mctx, &lp->lp));
172 	lp->mctx = mctx;
173 	return (ISC_R_SUCCESS);
174 }
175 
176 static inline void
freestruct_lp(ARGS_FREESTRUCT)177 freestruct_lp(ARGS_FREESTRUCT) {
178 	dns_rdata_lp_t *lp = source;
179 
180 	REQUIRE(source != NULL);
181 	REQUIRE(lp->common.rdtype == 107);
182 
183 	if (lp->mctx == NULL)
184 		return;
185 
186 	dns_name_free(&lp->lp, lp->mctx);
187 	lp->mctx = NULL;
188 }
189 
190 static inline isc_result_t
additionaldata_lp(ARGS_ADDLDATA)191 additionaldata_lp(ARGS_ADDLDATA) {
192 	dns_name_t name;
193 	dns_offsets_t offsets;
194 	isc_region_t region;
195 	isc_result_t result;
196 
197 	REQUIRE(rdata->type == 107);
198 
199 	dns_name_init(&name, offsets);
200 	dns_rdata_toregion(rdata, &region);
201 	isc_region_consume(&region, 2);
202 	dns_name_fromregion(&name, &region);
203 
204 	result = (add)(arg, &name, dns_rdatatype_l32);
205 	if (result != ISC_R_SUCCESS)
206 		return (result);
207 	return ((add)(arg, &name, dns_rdatatype_l64));
208 }
209 
210 static inline isc_result_t
digest_lp(ARGS_DIGEST)211 digest_lp(ARGS_DIGEST) {
212 	isc_region_t region;
213 
214 	REQUIRE(rdata->type == 107);
215 
216 	dns_rdata_toregion(rdata, &region);
217 	return ((digest)(arg, &region));
218 }
219 
220 static inline isc_boolean_t
checkowner_lp(ARGS_CHECKOWNER)221 checkowner_lp(ARGS_CHECKOWNER) {
222 
223 	REQUIRE(type == 107);
224 
225 	UNUSED(type);
226 	UNUSED(rdclass);
227 	UNUSED(name);
228 	UNUSED(wildcard);
229 
230 	return (ISC_TRUE);
231 }
232 
233 static inline isc_boolean_t
checknames_lp(ARGS_CHECKNAMES)234 checknames_lp(ARGS_CHECKNAMES) {
235 
236 	REQUIRE(rdata->type == 107);
237 
238 	UNUSED(bad);
239 	UNUSED(owner);
240 
241 	return (ISC_TRUE);
242 }
243 
244 static inline int
casecompare_lp(ARGS_COMPARE)245 casecompare_lp(ARGS_COMPARE) {
246 	dns_name_t name1;
247 	dns_name_t name2;
248 	isc_region_t region1;
249 	isc_region_t region2;
250 	int order;
251 
252 	REQUIRE(rdata1->type == rdata2->type);
253 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
254 	REQUIRE(rdata1->type == 107);
255 	REQUIRE(rdata1->length != 0);
256 	REQUIRE(rdata2->length != 0);
257 
258 	order = memcmp(rdata1->data, rdata2->data, 2);
259 	if (order != 0)
260 		return (order < 0 ? -1 : 1);
261 
262 	dns_name_init(&name1, NULL);
263 	dns_name_init(&name2, NULL);
264 
265 	dns_rdata_toregion(rdata1, &region1);
266 	dns_rdata_toregion(rdata2, &region2);
267 
268 	isc_region_consume(&region1, 2);
269 	isc_region_consume(&region2, 2);
270 
271 	dns_name_fromregion(&name1, &region1);
272 	dns_name_fromregion(&name2, &region2);
273 
274 	return (dns_name_rdatacompare(&name1, &name2));
275 }
276 
277 #endif	/* RDATA_GENERIC_LP_107_C */
278