Merge from vendor branch ZLIB:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / rdata / in_1 / srv_33.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: srv_33.c,v 1.36.2.3 2004/03/09 06:11:38 marka Exp $ */
19
20 /* Reviewed: Fri Mar 17 13:01:00 PST 2000 by bwelling */
21
22 /* RFC 2782 */
23
24 #ifndef RDATA_IN_1_SRV_33_C
25 #define RDATA_IN_1_SRV_33_C
26
27 #define RRTYPE_SRV_ATTRIBUTES (0)
28
29 static inline isc_result_t
30 fromtext_in_srv(ARGS_FROMTEXT) {
31         isc_token_t token;
32         dns_name_t name;
33         isc_buffer_t buffer;
34
35         REQUIRE(type == 33);
36         REQUIRE(rdclass == 1);
37
38         UNUSED(type);
39         UNUSED(rdclass);
40         UNUSED(callbacks);
41
42         /*
43          * Priority.
44          */
45         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
46                                       ISC_FALSE));
47         if (token.value.as_ulong > 0xffffU)
48                 RETTOK(ISC_R_RANGE);
49         RETERR(uint16_tobuffer(token.value.as_ulong, target));
50
51         /*
52          * Weight.
53          */
54         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
55                                       ISC_FALSE));
56         if (token.value.as_ulong > 0xffffU)
57                 RETTOK(ISC_R_RANGE);
58         RETERR(uint16_tobuffer(token.value.as_ulong, target));
59
60         /*
61          * Port.
62          */
63         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
64                                       ISC_FALSE));
65         if (token.value.as_ulong > 0xffffU)
66                 RETTOK(ISC_R_RANGE);
67         RETERR(uint16_tobuffer(token.value.as_ulong, target));
68
69         /*
70          * Target.
71          */
72         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
73                                       ISC_FALSE));
74         dns_name_init(&name, NULL);
75         buffer_fromregion(&buffer, &token.value.as_region);
76         origin = (origin != NULL) ? origin : dns_rootname;
77         RETTOK(dns_name_fromtext(&name, &buffer, origin, downcase, target));
78         return (ISC_R_SUCCESS);
79 }
80
81 static inline isc_result_t
82 totext_in_srv(ARGS_TOTEXT) {
83         isc_region_t region;
84         dns_name_t name;
85         dns_name_t prefix;
86         isc_boolean_t sub;
87         char buf[sizeof "64000"];
88         unsigned short num;
89
90         REQUIRE(rdata->type == 33);
91         REQUIRE(rdata->rdclass == 1);
92         REQUIRE(rdata->length != 0);
93
94         dns_name_init(&name, NULL);
95         dns_name_init(&prefix, NULL);
96
97         /*
98          * Priority.
99          */
100         dns_rdata_toregion(rdata, &region);
101         num = uint16_fromregion(&region);
102         isc_region_consume(&region, 2);
103         sprintf(buf, "%u", num);
104         RETERR(str_totext(buf, target));
105         RETERR(str_totext(" ", target));
106
107         /*
108          * Weight.
109          */
110         num = uint16_fromregion(&region);
111         isc_region_consume(&region, 2);
112         sprintf(buf, "%u", num);
113         RETERR(str_totext(buf, target));
114         RETERR(str_totext(" ", target));
115
116         /*
117          * Port.
118          */
119         num = uint16_fromregion(&region);
120         isc_region_consume(&region, 2);
121         sprintf(buf, "%u", num);
122         RETERR(str_totext(buf, target));
123         RETERR(str_totext(" ", target));
124
125         /*
126          * Target.
127          */
128         dns_name_fromregion(&name, &region);
129         sub = name_prefix(&name, tctx->origin, &prefix);
130         return (dns_name_totext(&prefix, sub, target));
131 }
132
133 static inline isc_result_t
134 fromwire_in_srv(ARGS_FROMWIRE) {
135         dns_name_t name;
136         isc_region_t sr;
137
138         REQUIRE(type == 33);
139         REQUIRE(rdclass == 1);
140
141         UNUSED(type);
142         UNUSED(rdclass);
143
144         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
145
146         dns_name_init(&name, NULL);
147
148         /*
149          * Priority, weight, port.
150          */
151         isc_buffer_activeregion(source, &sr);
152         if (sr.length < 6)
153                 return (ISC_R_UNEXPECTEDEND);
154         RETERR(mem_tobuffer(target, sr.base, 6));
155         isc_buffer_forward(source, 6);
156
157         /*
158          * Target.
159          */
160         return (dns_name_fromwire(&name, source, dctx, downcase, target));
161 }
162
163 static inline isc_result_t
164 towire_in_srv(ARGS_TOWIRE) {
165         dns_name_t name;
166         dns_offsets_t offsets;
167         isc_region_t sr;
168
169         REQUIRE(rdata->type == 33);
170         REQUIRE(rdata->length != 0);
171
172         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
173         /*
174          * Priority, weight, port.
175          */
176         dns_rdata_toregion(rdata, &sr);
177         RETERR(mem_tobuffer(target, sr.base, 6));
178         isc_region_consume(&sr, 6);
179
180         /*
181          * Target.
182          */
183         dns_name_init(&name, offsets);
184         dns_name_fromregion(&name, &sr);
185         return (dns_name_towire(&name, cctx, target));
186 }
187
188 static inline int
189 compare_in_srv(ARGS_COMPARE) {
190         dns_name_t name1;
191         dns_name_t name2;
192         isc_region_t region1;
193         isc_region_t region2;
194         int order;
195
196         REQUIRE(rdata1->type == rdata2->type);
197         REQUIRE(rdata1->rdclass == rdata2->rdclass);
198         REQUIRE(rdata1->type == 33);
199         REQUIRE(rdata1->rdclass == 1);
200         REQUIRE(rdata1->length != 0);
201         REQUIRE(rdata2->length != 0);
202
203         /*
204          * Priority, weight, port.
205          */
206         order = memcmp(rdata1->data, rdata2->data, 6);
207         if (order != 0)
208                 return (order < 0 ? -1 : 1);
209
210         /*
211          * Target.
212          */
213         dns_name_init(&name1, NULL);
214         dns_name_init(&name2, NULL);
215
216         dns_rdata_toregion(rdata1, &region1);
217         dns_rdata_toregion(rdata2, &region2);
218
219         isc_region_consume(&region1, 6);
220         isc_region_consume(&region2, 6);
221
222         dns_name_fromregion(&name1, &region1);
223         dns_name_fromregion(&name2, &region2);
224
225         return (dns_name_rdatacompare(&name1, &name2));
226 }
227
228 static inline isc_result_t
229 fromstruct_in_srv(ARGS_FROMSTRUCT) {
230         dns_rdata_in_srv_t *srv = source;
231         isc_region_t region;
232
233         REQUIRE(type == 33);
234         REQUIRE(rdclass == 1);
235         REQUIRE(source != NULL);
236         REQUIRE(srv->common.rdtype == type);
237         REQUIRE(srv->common.rdclass == rdclass);
238
239         UNUSED(type);
240         UNUSED(rdclass);
241
242         RETERR(uint16_tobuffer(srv->priority, target));
243         RETERR(uint16_tobuffer(srv->weight, target));
244         RETERR(uint16_tobuffer(srv->port, target));
245         dns_name_toregion(&srv->target, &region);
246         return (isc_buffer_copyregion(target, &region));
247 }
248
249 static inline isc_result_t
250 tostruct_in_srv(ARGS_TOSTRUCT) {
251         isc_region_t region;
252         dns_rdata_in_srv_t *srv = target;
253         dns_name_t name;
254
255         REQUIRE(rdata->rdclass == 1);
256         REQUIRE(rdata->type == 33);
257         REQUIRE(target != NULL);
258         REQUIRE(rdata->length != 0);
259
260         srv->common.rdclass = rdata->rdclass;
261         srv->common.rdtype = rdata->type;
262         ISC_LINK_INIT(&srv->common, link);
263
264         dns_name_init(&name, NULL);
265         dns_rdata_toregion(rdata, &region);
266         srv->priority = uint16_fromregion(&region);
267         isc_region_consume(&region, 2);
268         srv->weight = uint16_fromregion(&region);
269         isc_region_consume(&region, 2);
270         srv->port = uint16_fromregion(&region);
271         isc_region_consume(&region, 2);
272         dns_name_fromregion(&name, &region);
273         dns_name_init(&srv->target, NULL);
274         RETERR(name_duporclone(&name, mctx, &srv->target));
275         srv->mctx = mctx;
276         return (ISC_R_SUCCESS);
277 }
278
279 static inline void
280 freestruct_in_srv(ARGS_FREESTRUCT) {
281         dns_rdata_in_srv_t *srv = source;
282
283         REQUIRE(source != NULL);
284         REQUIRE(srv->common.rdclass == 1);
285         REQUIRE(srv->common.rdtype == 33);
286
287         if (srv->mctx == NULL)
288                 return;
289
290         dns_name_free(&srv->target, srv->mctx);
291         srv->mctx = NULL;
292 }
293
294 static inline isc_result_t
295 additionaldata_in_srv(ARGS_ADDLDATA) {
296         dns_name_t name;
297         dns_offsets_t offsets;
298         isc_region_t region;
299
300         REQUIRE(rdata->type == 33);
301         REQUIRE(rdata->rdclass == 1);
302
303         dns_name_init(&name, offsets);
304         dns_rdata_toregion(rdata, &region);
305         isc_region_consume(&region, 6);
306         dns_name_fromregion(&name, &region);
307
308         return ((add)(arg, &name, dns_rdatatype_a));
309 }
310
311 static inline isc_result_t
312 digest_in_srv(ARGS_DIGEST) {
313         isc_region_t r1, r2;
314         dns_name_t name;
315
316         REQUIRE(rdata->type == 33);
317         REQUIRE(rdata->rdclass == 1);
318
319         dns_rdata_toregion(rdata, &r1);
320         r2 = r1;
321         isc_region_consume(&r2, 6);
322         r1.length = 6;
323         RETERR((digest)(arg, &r1));
324         dns_name_init(&name, NULL);
325         dns_name_fromregion(&name, &r2);
326         return (dns_name_digest(&name, digest, arg));
327 }
328
329 #endif  /* RDATA_IN_1_SRV_33_C */