Merge from vendor branch BIND:
[dragonfly.git] / contrib / bind-9.3 / lib / dns / rdata / in_1 / wks_11.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  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: wks_11.c,v 1.44.12.8 2004/09/16 01:00:58 marka Exp $ */
19
20 /* Reviewed: Fri Mar 17 15:01:49 PST 2000 by explorer */
21
22 #ifndef RDATA_IN_1_WKS_11_C
23 #define RDATA_IN_1_WKS_11_C
24
25 #include <limits.h>
26 #include <stdlib.h>
27
28 #include <isc/net.h>
29 #include <isc/netdb.h>
30
31 #define RRTYPE_WKS_ATTRIBUTES (0)
32
33 static inline isc_result_t
34 fromtext_in_wks(ARGS_FROMTEXT) {
35         isc_token_t token;
36         isc_region_t region;
37         struct in_addr addr;
38         struct protoent *pe;
39         struct servent *se;
40         char *e;
41         long proto;
42         unsigned char bm[8*1024]; /* 64k bits */
43         long port;
44         long maxport = -1;
45         const char *ps = NULL;
46         unsigned int n;
47         char service[32];
48         int i;
49
50         REQUIRE(type == 11);
51         REQUIRE(rdclass == 1);
52
53         UNUSED(type);
54         UNUSED(origin);
55         UNUSED(options);
56         UNUSED(rdclass);
57
58         /*
59          * IPv4 dotted quad.
60          */
61         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
62                                       ISC_FALSE));
63
64         isc_buffer_availableregion(target, &region);
65         if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
66                 RETTOK(DNS_R_BADDOTTEDQUAD);
67         if (region.length < 4)
68                 return (ISC_R_NOSPACE);
69         memcpy(region.base, &addr, 4);
70         isc_buffer_add(target, 4);
71
72         /*
73          * Protocol.
74          */
75         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
76                                       ISC_FALSE));
77
78         proto = strtol(DNS_AS_STR(token), &e, 10);
79         if (*e == 0)
80                 ;
81         else if ((pe = getprotobyname(DNS_AS_STR(token))) != NULL)
82                 proto = pe->p_proto;
83         else
84                 RETTOK(DNS_R_UNKNOWNPROTO);
85         if (proto < 0 || proto > 0xff)
86                 RETTOK(ISC_R_RANGE);
87
88         if (proto == IPPROTO_TCP)
89                 ps = "tcp";
90         else if (proto == IPPROTO_UDP)
91                 ps = "udp";
92
93         RETERR(uint8_tobuffer(proto, target));
94
95         memset(bm, 0, sizeof(bm));
96         do {
97                 RETERR(isc_lex_getmastertoken(lexer, &token,
98                                               isc_tokentype_string, ISC_TRUE));
99                 if (token.type != isc_tokentype_string)
100                         break;
101
102                 /*
103                  * Lowercase the service string as some getservbyname() are
104                  * case sensitive and the database is usually in lowercase.
105                  */
106                 strncpy(service, DNS_AS_STR(token), sizeof(service));
107                 service[sizeof(service)-1] = '\0';
108                 for (i = strlen(service) - 1; i >= 0; i--)
109                         if (isupper(service[i]&0xff))
110                                 service[i] = tolower(service[i]&0xff);
111
112                 port = strtol(DNS_AS_STR(token), &e, 10);
113                 if (*e == 0)
114                         ;
115                 else if ((se = getservbyname(service, ps)) != NULL)
116                         port = ntohs(se->s_port);
117                 else if ((se = getservbyname(DNS_AS_STR(token), ps))
118                           != NULL)
119                         port = ntohs(se->s_port);
120                 else
121                         RETTOK(DNS_R_UNKNOWNSERVICE);
122                 if (port < 0 || port > 0xffff)
123                         RETTOK(ISC_R_RANGE);
124                 if (port > maxport)
125                         maxport = port;
126                 bm[port / 8] |= (0x80 >> (port % 8));
127         } while (1);
128
129         /*
130          * Let upper layer handle eol/eof.
131          */
132         isc_lex_ungettoken(lexer, &token);
133
134         n = (maxport + 8) / 8;
135         return (mem_tobuffer(target, bm, n));
136 }
137
138 static inline isc_result_t
139 totext_in_wks(ARGS_TOTEXT) {
140         isc_region_t sr;
141         unsigned short proto;
142         char buf[sizeof("65535")];
143         unsigned int i, j;
144
145         UNUSED(tctx);
146
147         REQUIRE(rdata->type == 11);
148         REQUIRE(rdata->rdclass == 1);
149         REQUIRE(rdata->length >= 5);
150
151         dns_rdata_toregion(rdata, &sr);
152         RETERR(inet_totext(AF_INET, &sr, target));
153         isc_region_consume(&sr, 4);
154
155         proto = uint8_fromregion(&sr);
156         sprintf(buf, "%u", proto);
157         RETERR(str_totext(" ", target));
158         RETERR(str_totext(buf, target));
159         isc_region_consume(&sr, 1);
160
161         for (i = 0; i < sr.length; i++) {
162                 if (sr.base[i] != 0)
163                         for (j = 0; j < 8; j++)
164                                 if ((sr.base[i] & (0x80 >> j)) != 0) {
165                                         sprintf(buf, "%u", i * 8 + j);
166                                         RETERR(str_totext(" ", target));
167                                         RETERR(str_totext(buf, target));
168                                 }
169         }
170
171         return (ISC_R_SUCCESS);
172 }
173
174 static inline isc_result_t
175 fromwire_in_wks(ARGS_FROMWIRE) {
176         isc_region_t sr;
177         isc_region_t tr;
178
179         REQUIRE(type == 11);
180         REQUIRE(rdclass == 1);
181
182         UNUSED(type);
183         UNUSED(dctx);
184         UNUSED(options);
185         UNUSED(rdclass);
186
187         isc_buffer_activeregion(source, &sr);
188         isc_buffer_availableregion(target, &tr);
189
190         if (sr.length < 5)
191                 return (ISC_R_UNEXPECTEDEND);
192         if (sr.length > 8 * 1024 + 5)
193                 return (DNS_R_EXTRADATA);
194         if (tr.length < sr.length)
195                 return (ISC_R_NOSPACE);
196
197         memcpy(tr.base, sr.base, sr.length);
198         isc_buffer_add(target, sr.length);
199         isc_buffer_forward(source, sr.length);
200
201         return (ISC_R_SUCCESS);
202 }
203
204 static inline isc_result_t
205 towire_in_wks(ARGS_TOWIRE) {
206         isc_region_t sr;
207
208         UNUSED(cctx);
209
210         REQUIRE(rdata->type == 11);
211         REQUIRE(rdata->rdclass == 1);
212         REQUIRE(rdata->length != 0);
213
214         dns_rdata_toregion(rdata, &sr);
215         return (mem_tobuffer(target, sr.base, sr.length));
216 }
217
218 static inline int
219 compare_in_wks(ARGS_COMPARE) {
220         isc_region_t r1;
221         isc_region_t r2;
222
223         REQUIRE(rdata1->type == rdata2->type);
224         REQUIRE(rdata1->rdclass == rdata2->rdclass);
225         REQUIRE(rdata1->type == 11);
226         REQUIRE(rdata1->rdclass == 1);
227         REQUIRE(rdata1->length != 0);
228         REQUIRE(rdata2->length != 0);
229
230         dns_rdata_toregion(rdata1, &r1);
231         dns_rdata_toregion(rdata2, &r2);
232         return (isc_region_compare(&r1, &r2));
233 }
234
235 static inline isc_result_t
236 fromstruct_in_wks(ARGS_FROMSTRUCT) {
237         dns_rdata_in_wks_t *wks = source;
238         isc_uint32_t a;
239
240         REQUIRE(type == 11);
241         REQUIRE(rdclass == 1);
242         REQUIRE(source != NULL);
243         REQUIRE(wks->common.rdtype == type);
244         REQUIRE(wks->common.rdclass == rdclass);
245         REQUIRE(wks->map != NULL || wks->map_len == 0);
246
247         UNUSED(type);
248         UNUSED(rdclass);
249
250         a = ntohl(wks->in_addr.s_addr);
251         RETERR(uint32_tobuffer(a, target));
252         RETERR(uint16_tobuffer(wks->protocol, target));
253         return (mem_tobuffer(target, wks->map, wks->map_len));
254 }
255
256 static inline isc_result_t
257 tostruct_in_wks(ARGS_TOSTRUCT) {
258         dns_rdata_in_wks_t *wks = target;
259         isc_uint32_t n;
260         isc_region_t region;
261
262         REQUIRE(rdata->type == 11);
263         REQUIRE(rdata->rdclass == 1);
264         REQUIRE(rdata->length != 0);
265
266         wks->common.rdclass = rdata->rdclass;
267         wks->common.rdtype = rdata->type;
268         ISC_LINK_INIT(&wks->common, link);
269
270         dns_rdata_toregion(rdata, &region);
271         n = uint32_fromregion(&region);
272         wks->in_addr.s_addr = htonl(n);
273         isc_region_consume(&region, 4);
274         wks->protocol = uint16_fromregion(&region);
275         isc_region_consume(&region, 2);
276         wks->map_len = region.length;
277         wks->map = mem_maybedup(mctx, region.base, region.length);
278         if (wks->map == NULL)
279                 return (ISC_R_NOMEMORY);
280         wks->mctx = mctx;
281         return (ISC_R_SUCCESS);
282 }
283
284 static inline void
285 freestruct_in_wks(ARGS_FREESTRUCT) {
286         dns_rdata_in_wks_t *wks = source;
287
288         REQUIRE(source != NULL);
289         REQUIRE(wks->common.rdtype == 11);
290         REQUIRE(wks->common.rdclass == 1);
291
292         if (wks->mctx == NULL)
293                 return;
294
295         if (wks->map != NULL)
296                 isc_mem_free(wks->mctx, wks->map);
297         wks->mctx = NULL;
298 }
299
300 static inline isc_result_t
301 additionaldata_in_wks(ARGS_ADDLDATA) {
302         UNUSED(rdata);
303         UNUSED(add);
304         UNUSED(arg);
305
306         REQUIRE(rdata->type == 11);
307         REQUIRE(rdata->rdclass == 1);
308
309         return (ISC_R_SUCCESS);
310 }
311
312 static inline isc_result_t
313 digest_in_wks(ARGS_DIGEST) {
314         isc_region_t r;
315
316         REQUIRE(rdata->type == 11);
317         REQUIRE(rdata->rdclass == 1);
318
319         dns_rdata_toregion(rdata, &r);
320
321         return ((digest)(arg, &r));
322 }
323
324 static inline isc_boolean_t
325 checkowner_in_wks(ARGS_CHECKOWNER) {
326
327         REQUIRE(type == 11);
328         REQUIRE(rdclass == 1);
329
330         UNUSED(type);
331         UNUSED(rdclass);
332
333         return (dns_name_ishostname(name, wildcard));
334 }
335
336 static inline isc_boolean_t
337 checknames_in_wks(ARGS_CHECKNAMES) {
338
339         REQUIRE(rdata->type == 11);
340         REQUIRE(rdata->rdclass == 1);
341
342         UNUSED(rdata);
343         UNUSED(owner);
344         UNUSED(bad);
345
346         return (ISC_TRUE);
347 }
348
349 #endif  /* RDATA_IN_1_WKS_11_C */