Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / rdata / generic / rt_21.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: rt_21.c,v 1.37.2.3 2004/03/09 06:11:33 marka Exp $ */
19
20 /* reviewed: Thu Mar 16 15:02:31 PST 2000 by brister */
21
22 /* RFC 1183 */
23
24 #ifndef RDATA_GENERIC_RT_21_C
25 #define RDATA_GENERIC_RT_21_C
26
27 #define RRTYPE_RT_ATTRIBUTES (0)
28
29 static inline isc_result_t
30 fromtext_rt(ARGS_FROMTEXT) {
31         isc_token_t token;
32         dns_name_t name;
33         isc_buffer_t buffer;
34
35         REQUIRE(type == 21);
36
37         UNUSED(type);
38         UNUSED(rdclass);
39         UNUSED(callbacks);
40
41         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
42                                       ISC_FALSE));
43         if (token.value.as_ulong > 0xffffU)
44                 RETTOK(ISC_R_RANGE);
45         RETERR(uint16_tobuffer(token.value.as_ulong, target));
46
47         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
48                                       ISC_FALSE));
49
50         dns_name_init(&name, NULL);
51         buffer_fromregion(&buffer, &token.value.as_region);
52         origin = (origin != NULL) ? origin : dns_rootname;
53         RETTOK(dns_name_fromtext(&name, &buffer, origin, downcase, target));
54         return (ISC_R_SUCCESS);
55 }
56
57 static inline isc_result_t
58 totext_rt(ARGS_TOTEXT) {
59         isc_region_t region;
60         dns_name_t name;
61         dns_name_t prefix;
62         isc_boolean_t sub;
63         char buf[sizeof "64000"];
64         unsigned short num;
65
66         REQUIRE(rdata->type == 21);
67         REQUIRE(rdata->length != 0);
68
69         dns_name_init(&name, NULL);
70         dns_name_init(&prefix, NULL);
71
72         dns_rdata_toregion(rdata, &region);
73         num = uint16_fromregion(&region);
74         isc_region_consume(&region, 2);
75         sprintf(buf, "%u", num);
76         RETERR(str_totext(buf, target));
77         RETERR(str_totext(" ", target));
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
84 fromwire_rt(ARGS_FROMWIRE) {
85         dns_name_t name;
86         isc_region_t sregion;
87         isc_region_t tregion;
88
89         REQUIRE(type == 21);
90
91         UNUSED(type);
92         UNUSED(rdclass);
93
94         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
95
96         dns_name_init(&name, NULL);
97
98         isc_buffer_activeregion(source, &sregion);
99         isc_buffer_availableregion(target, &tregion);
100         if (tregion.length < 2)
101                 return (ISC_R_NOSPACE);
102         if (sregion.length < 2)
103                 return (ISC_R_UNEXPECTEDEND);
104         memcpy(tregion.base, sregion.base, 2);
105         isc_buffer_forward(source, 2);
106         isc_buffer_add(target, 2);
107         return (dns_name_fromwire(&name, source, dctx, downcase, target));
108 }
109
110 static inline isc_result_t
111 towire_rt(ARGS_TOWIRE) {
112         dns_name_t name;
113         dns_offsets_t offsets;
114         isc_region_t region;
115         isc_region_t tr;
116
117         REQUIRE(rdata->type == 21);
118         REQUIRE(rdata->length != 0);
119
120         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
121         isc_buffer_availableregion(target, &tr);
122         dns_rdata_toregion(rdata, &region);
123         if (tr.length < 2)
124                 return (ISC_R_NOSPACE);
125         memcpy(tr.base, region.base, 2);
126         isc_region_consume(&region, 2);
127         isc_buffer_add(target, 2);
128
129         dns_name_init(&name, offsets);
130         dns_name_fromregion(&name, &region);
131
132         return (dns_name_towire(&name, cctx, target));
133 }
134
135 static inline int
136 compare_rt(ARGS_COMPARE) {
137         dns_name_t name1;
138         dns_name_t name2;
139         isc_region_t region1;
140         isc_region_t region2;
141         int order;
142
143         REQUIRE(rdata1->type == rdata2->type);
144         REQUIRE(rdata1->rdclass == rdata2->rdclass);
145         REQUIRE(rdata1->type == 21);
146         REQUIRE(rdata1->length != 0);
147         REQUIRE(rdata2->length != 0);
148
149         order = memcmp(rdata1->data, rdata2->data, 2);
150         if (order != 0)
151                 return (order < 0 ? -1 : 1);
152
153         dns_name_init(&name1, NULL);
154         dns_name_init(&name2, NULL);
155
156         dns_rdata_toregion(rdata1, &region1);
157         dns_rdata_toregion(rdata2, &region2);
158
159         isc_region_consume(&region1, 2);
160         isc_region_consume(&region2, 2);
161
162         dns_name_fromregion(&name1, &region1);
163         dns_name_fromregion(&name2, &region2);
164
165         return (dns_name_rdatacompare(&name1, &name2));
166 }
167
168 static inline isc_result_t
169 fromstruct_rt(ARGS_FROMSTRUCT) {
170         dns_rdata_rt_t *rt = source;
171         isc_region_t region;
172
173         REQUIRE(type == 21);
174         REQUIRE(source != NULL);
175         REQUIRE(rt->common.rdtype == type);
176         REQUIRE(rt->common.rdclass == rdclass);
177
178         UNUSED(type);
179         UNUSED(rdclass);
180
181         RETERR(uint16_tobuffer(rt->preference, target));
182         dns_name_toregion(&rt->host, &region);
183         return (isc_buffer_copyregion(target, &region));
184 }
185
186 static inline isc_result_t
187 tostruct_rt(ARGS_TOSTRUCT) {
188         isc_region_t region;
189         dns_rdata_rt_t *rt = target;
190         dns_name_t name;
191
192         REQUIRE(rdata->type == 21);
193         REQUIRE(target != NULL);
194         REQUIRE(rdata->length != 0);
195
196         rt->common.rdclass = rdata->rdclass;
197         rt->common.rdtype = rdata->type;
198         ISC_LINK_INIT(&rt->common, link);
199
200         dns_name_init(&name, NULL);
201         dns_rdata_toregion(rdata, &region);
202         rt->preference = uint16_fromregion(&region);
203         isc_region_consume(&region, 2);
204         dns_name_fromregion(&name, &region);
205         dns_name_init(&rt->host, NULL);
206         RETERR(name_duporclone(&name, mctx, &rt->host));
207
208         rt->mctx = mctx;
209         return (ISC_R_SUCCESS);
210 }
211
212 static inline void
213 freestruct_rt(ARGS_FREESTRUCT) {
214         dns_rdata_rt_t *rt = source;
215
216         REQUIRE(source != NULL);
217         REQUIRE(rt->common.rdtype == 21);
218
219         if (rt->mctx == NULL)
220                 return;
221
222         dns_name_free(&rt->host, rt->mctx);
223         rt->mctx = NULL;
224 }
225
226 static inline isc_result_t
227 additionaldata_rt(ARGS_ADDLDATA) {
228         dns_name_t name;
229         dns_offsets_t offsets;
230         isc_region_t region;
231         isc_result_t result;
232
233         REQUIRE(rdata->type == 21);
234
235         dns_name_init(&name, offsets);
236         dns_rdata_toregion(rdata, &region);
237         isc_region_consume(&region, 2);
238         dns_name_fromregion(&name, &region);
239
240         result = (add)(arg, &name, dns_rdatatype_x25);
241         if (result != ISC_R_SUCCESS)
242                 return (result);
243         result = (add)(arg, &name, dns_rdatatype_isdn);
244         if (result != ISC_R_SUCCESS)
245                 return (result);
246         return ((add)(arg, &name, dns_rdatatype_a));
247 }
248
249 static inline isc_result_t
250 digest_rt(ARGS_DIGEST) {
251         isc_region_t r1, r2;
252         isc_result_t result;
253         dns_name_t name;
254
255         REQUIRE(rdata->type == 21);
256
257         dns_rdata_toregion(rdata, &r1);
258         r2 = r1;
259         isc_region_consume(&r2, 2);
260         r1.length = 2;
261         result = (digest)(arg, &r1);
262         if (result != ISC_R_SUCCESS)
263                 return (result);
264         dns_name_init(&name, NULL);
265         dns_name_fromregion(&name, &r2);
266         return (dns_name_digest(&name, digest, arg));
267 }
268
269 #endif  /* RDATA_GENERIC_RT_21_C */