]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/rdata/in_1/px_26.c
MFV r306384:
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / rdata / in_1 / px_26.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001, 2003  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: px_26.c,v 1.45 2009/12/04 22:06:37 tbox Exp $ */
19
20 /* Reviewed: Mon Mar 20 10:44:27 PST 2000 */
21
22 /* RFC2163 */
23
24 #ifndef RDATA_IN_1_PX_26_C
25 #define RDATA_IN_1_PX_26_C
26
27 #define RRTYPE_PX_ATTRIBUTES (0)
28
29 static inline isc_result_t
30 fromtext_in_px(ARGS_FROMTEXT) {
31         isc_token_t token;
32         dns_name_t name;
33         isc_buffer_t buffer;
34
35         REQUIRE(type == dns_rdatatype_px);
36         REQUIRE(rdclass == dns_rdataclass_in);
37
38         UNUSED(type);
39         UNUSED(rdclass);
40         UNUSED(callbacks);
41
42         if (origin == NULL)
43                 origin = dns_rootname;
44
45         /*
46          * Preference.
47          */
48         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
49                                       ISC_FALSE));
50         if (token.value.as_ulong > 0xffffU)
51                 RETTOK(ISC_R_RANGE);
52         RETERR(uint16_tobuffer(token.value.as_ulong, target));
53
54         /*
55          * MAP822.
56          */
57         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
58                                       ISC_FALSE));
59         dns_name_init(&name, NULL);
60         buffer_fromregion(&buffer, &token.value.as_region);
61         RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
62
63         /*
64          * MAPX400.
65          */
66         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
67                                       ISC_FALSE));
68         dns_name_init(&name, NULL);
69         buffer_fromregion(&buffer, &token.value.as_region);
70         RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
71         return (ISC_R_SUCCESS);
72 }
73
74 static inline isc_result_t
75 totext_in_px(ARGS_TOTEXT) {
76         isc_region_t region;
77         dns_name_t name;
78         dns_name_t prefix;
79         isc_boolean_t sub;
80         char buf[sizeof("64000")];
81         unsigned short num;
82
83         REQUIRE(rdata->type == dns_rdatatype_px);
84         REQUIRE(rdata->rdclass == dns_rdataclass_in);
85         REQUIRE(rdata->length != 0);
86
87         dns_name_init(&name, NULL);
88         dns_name_init(&prefix, NULL);
89
90         /*
91          * Preference.
92          */
93         dns_rdata_toregion(rdata, &region);
94         num = uint16_fromregion(&region);
95         isc_region_consume(&region, 2);
96         sprintf(buf, "%u", num);
97         RETERR(str_totext(buf, target));
98         RETERR(str_totext(" ", target));
99
100         /*
101          * MAP822.
102          */
103         dns_name_fromregion(&name, &region);
104         sub = name_prefix(&name, tctx->origin, &prefix);
105         isc_region_consume(&region, name_length(&name));
106         RETERR(dns_name_totext(&prefix, sub, target));
107         RETERR(str_totext(" ", target));
108
109         /*
110          * MAPX400.
111          */
112         dns_name_fromregion(&name, &region);
113         sub = name_prefix(&name, tctx->origin, &prefix);
114         return(dns_name_totext(&prefix, sub, target));
115 }
116
117 static inline isc_result_t
118 fromwire_in_px(ARGS_FROMWIRE) {
119         dns_name_t name;
120         isc_region_t sregion;
121
122         REQUIRE(type == dns_rdatatype_px);
123         REQUIRE(rdclass == dns_rdataclass_in);
124
125         UNUSED(type);
126         UNUSED(rdclass);
127
128         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
129
130         dns_name_init(&name, NULL);
131
132         /*
133          * Preference.
134          */
135         isc_buffer_activeregion(source, &sregion);
136         if (sregion.length < 2)
137                 return (ISC_R_UNEXPECTEDEND);
138         RETERR(mem_tobuffer(target, sregion.base, 2));
139         isc_buffer_forward(source, 2);
140
141         /*
142          * MAP822.
143          */
144         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
145
146         /*
147          * MAPX400.
148          */
149         return (dns_name_fromwire(&name, source, dctx, options, target));
150 }
151
152 static inline isc_result_t
153 towire_in_px(ARGS_TOWIRE) {
154         dns_name_t name;
155         dns_offsets_t offsets;
156         isc_region_t region;
157
158         REQUIRE(rdata->type == dns_rdatatype_px);
159         REQUIRE(rdata->rdclass == dns_rdataclass_in);
160         REQUIRE(rdata->length != 0);
161
162         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
163         /*
164          * Preference.
165          */
166         dns_rdata_toregion(rdata, &region);
167         RETERR(mem_tobuffer(target, region.base, 2));
168         isc_region_consume(&region, 2);
169
170         /*
171          * MAP822.
172          */
173         dns_name_init(&name, offsets);
174         dns_name_fromregion(&name, &region);
175         RETERR(dns_name_towire(&name, cctx, target));
176         isc_region_consume(&region, name_length(&name));
177
178         /*
179          * MAPX400.
180          */
181         dns_name_init(&name, offsets);
182         dns_name_fromregion(&name, &region);
183         return (dns_name_towire(&name, cctx, target));
184 }
185
186 static inline int
187 compare_in_px(ARGS_COMPARE) {
188         dns_name_t name1;
189         dns_name_t name2;
190         isc_region_t region1;
191         isc_region_t region2;
192         int order;
193
194         REQUIRE(rdata1->type == rdata2->type);
195         REQUIRE(rdata1->rdclass == rdata2->rdclass);
196         REQUIRE(rdata1->type == dns_rdatatype_px);
197         REQUIRE(rdata1->rdclass == dns_rdataclass_in);
198         REQUIRE(rdata1->length != 0);
199         REQUIRE(rdata2->length != 0);
200
201         order = memcmp(rdata1->data, rdata2->data, 2);
202         if (order != 0)
203                 return (order < 0 ? -1 : 1);
204
205         dns_name_init(&name1, NULL);
206         dns_name_init(&name2, NULL);
207
208         dns_rdata_toregion(rdata1, &region1);
209         dns_rdata_toregion(rdata2, &region2);
210
211         isc_region_consume(&region1, 2);
212         isc_region_consume(&region2, 2);
213
214         dns_name_fromregion(&name1, &region1);
215         dns_name_fromregion(&name2, &region2);
216
217         order = dns_name_rdatacompare(&name1, &name2);
218         if (order != 0)
219                 return (order);
220
221         isc_region_consume(&region1, name_length(&name1));
222         isc_region_consume(&region2, name_length(&name2));
223
224         dns_name_fromregion(&name1, &region1);
225         dns_name_fromregion(&name2, &region2);
226
227         return (dns_name_rdatacompare(&name1, &name2));
228 }
229
230 static inline isc_result_t
231 fromstruct_in_px(ARGS_FROMSTRUCT) {
232         dns_rdata_in_px_t *px = source;
233         isc_region_t region;
234
235         REQUIRE(type == dns_rdatatype_px);
236         REQUIRE(rdclass == dns_rdataclass_in);
237         REQUIRE(source != NULL);
238         REQUIRE(px->common.rdtype == type);
239         REQUIRE(px->common.rdclass == rdclass);
240
241         UNUSED(type);
242         UNUSED(rdclass);
243
244         RETERR(uint16_tobuffer(px->preference, target));
245         dns_name_toregion(&px->map822, &region);
246         RETERR(isc_buffer_copyregion(target, &region));
247         dns_name_toregion(&px->mapx400, &region);
248         return (isc_buffer_copyregion(target, &region));
249 }
250
251 static inline isc_result_t
252 tostruct_in_px(ARGS_TOSTRUCT) {
253         dns_rdata_in_px_t *px = target;
254         dns_name_t name;
255         isc_region_t region;
256         isc_result_t result;
257
258         REQUIRE(rdata->type == dns_rdatatype_px);
259         REQUIRE(rdata->rdclass == dns_rdataclass_in);
260         REQUIRE(target != NULL);
261         REQUIRE(rdata->length != 0);
262
263         px->common.rdclass = rdata->rdclass;
264         px->common.rdtype = rdata->type;
265         ISC_LINK_INIT(&px->common, link);
266
267         dns_name_init(&name, NULL);
268         dns_rdata_toregion(rdata, &region);
269
270         px->preference = uint16_fromregion(&region);
271         isc_region_consume(&region, 2);
272
273         dns_name_fromregion(&name, &region);
274
275         dns_name_init(&px->map822, NULL);
276         RETERR(name_duporclone(&name, mctx, &px->map822));
277         isc_region_consume(&region, name_length(&px->map822));
278
279         dns_name_init(&px->mapx400, NULL);
280         result = name_duporclone(&name, mctx, &px->mapx400);
281         if (result != ISC_R_SUCCESS)
282                 goto cleanup;
283
284         px->mctx = mctx;
285         return (result);
286
287  cleanup:
288         dns_name_free(&px->map822, mctx);
289         return (ISC_R_NOMEMORY);
290 }
291
292 static inline void
293 freestruct_in_px(ARGS_FREESTRUCT) {
294         dns_rdata_in_px_t *px = source;
295
296         REQUIRE(source != NULL);
297         REQUIRE(px->common.rdclass == dns_rdataclass_in);
298         REQUIRE(px->common.rdtype == dns_rdatatype_px);
299
300         if (px->mctx == NULL)
301                 return;
302
303         dns_name_free(&px->map822, px->mctx);
304         dns_name_free(&px->mapx400, px->mctx);
305         px->mctx = NULL;
306 }
307
308 static inline isc_result_t
309 additionaldata_in_px(ARGS_ADDLDATA) {
310         REQUIRE(rdata->type == dns_rdatatype_px);
311         REQUIRE(rdata->rdclass == dns_rdataclass_in);
312
313         UNUSED(rdata);
314         UNUSED(add);
315         UNUSED(arg);
316
317         return (ISC_R_SUCCESS);
318 }
319
320 static inline isc_result_t
321 digest_in_px(ARGS_DIGEST) {
322         isc_region_t r1, r2;
323         dns_name_t name;
324         isc_result_t result;
325
326         REQUIRE(rdata->type == dns_rdatatype_px);
327         REQUIRE(rdata->rdclass == dns_rdataclass_in);
328
329         dns_rdata_toregion(rdata, &r1);
330         r2 = r1;
331         isc_region_consume(&r2, 2);
332         r1.length = 2;
333         result = (digest)(arg, &r1);
334         if (result != ISC_R_SUCCESS)
335                 return (result);
336         dns_name_init(&name, NULL);
337         dns_name_fromregion(&name, &r2);
338         result = dns_name_digest(&name, digest, arg);
339         if (result != ISC_R_SUCCESS)
340                 return (result);
341         isc_region_consume(&r2, name_length(&name));
342         dns_name_init(&name, NULL);
343         dns_name_fromregion(&name, &r2);
344
345         return (dns_name_digest(&name, digest, arg));
346 }
347
348 static inline isc_boolean_t
349 checkowner_in_px(ARGS_CHECKOWNER) {
350
351         REQUIRE(type == dns_rdatatype_px);
352         REQUIRE(rdclass == dns_rdataclass_in);
353
354         UNUSED(name);
355         UNUSED(type);
356         UNUSED(rdclass);
357         UNUSED(wildcard);
358
359         return (ISC_TRUE);
360 }
361
362 static inline isc_boolean_t
363 checknames_in_px(ARGS_CHECKNAMES) {
364
365         REQUIRE(rdata->type == dns_rdatatype_px);
366         REQUIRE(rdata->rdclass == dns_rdataclass_in);
367
368         UNUSED(rdata);
369         UNUSED(owner);
370         UNUSED(bad);
371
372         return (ISC_TRUE);
373 }
374
375 static inline int
376 casecompare_in_px(ARGS_COMPARE) {
377         return (compare_in_px(rdata1, rdata2));
378 }
379
380 #endif  /* RDATA_IN_1_PX_26_C */