bind - Upgraded vendor branch to 9.5.2-P1
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / rdata / in_1 / wks_11.c
1 /*
2  * Copyright (C) 2004, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  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: wks_11.c,v 1.54.128.2 2009/02/16 23:46:44 tbox 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         INSIST(sr.length <= 8*1024);
162         for (i = 0; i < sr.length; i++) {
163                 if (sr.base[i] != 0)
164                         for (j = 0; j < 8; j++)
165                                 if ((sr.base[i] & (0x80 >> j)) != 0) {
166                                         sprintf(buf, "%u", i * 8 + j);
167                                         RETERR(str_totext(" ", target));
168                                         RETERR(str_totext(buf, target));
169                                 }
170         }
171
172         return (ISC_R_SUCCESS);
173 }
174
175 static inline isc_result_t
176 fromwire_in_wks(ARGS_FROMWIRE) {
177         isc_region_t sr;
178         isc_region_t tr;
179
180         REQUIRE(type == 11);
181         REQUIRE(rdclass == 1);
182
183         UNUSED(type);
184         UNUSED(dctx);
185         UNUSED(options);
186         UNUSED(rdclass);
187
188         isc_buffer_activeregion(source, &sr);
189         isc_buffer_availableregion(target, &tr);
190
191         if (sr.length < 5)
192                 return (ISC_R_UNEXPECTEDEND);
193         if (sr.length > 8 * 1024 + 5)
194                 return (DNS_R_EXTRADATA);
195         if (tr.length < sr.length)
196                 return (ISC_R_NOSPACE);
197
198         memcpy(tr.base, sr.base, sr.length);
199         isc_buffer_add(target, sr.length);
200         isc_buffer_forward(source, sr.length);
201
202         return (ISC_R_SUCCESS);
203 }
204
205 static inline isc_result_t
206 towire_in_wks(ARGS_TOWIRE) {
207         isc_region_t sr;
208
209         UNUSED(cctx);
210
211         REQUIRE(rdata->type == 11);
212         REQUIRE(rdata->rdclass == 1);
213         REQUIRE(rdata->length != 0);
214
215         dns_rdata_toregion(rdata, &sr);
216         return (mem_tobuffer(target, sr.base, sr.length));
217 }
218
219 static inline int
220 compare_in_wks(ARGS_COMPARE) {
221         isc_region_t r1;
222         isc_region_t r2;
223
224         REQUIRE(rdata1->type == rdata2->type);
225         REQUIRE(rdata1->rdclass == rdata2->rdclass);
226         REQUIRE(rdata1->type == 11);
227         REQUIRE(rdata1->rdclass == 1);
228         REQUIRE(rdata1->length != 0);
229         REQUIRE(rdata2->length != 0);
230
231         dns_rdata_toregion(rdata1, &r1);
232         dns_rdata_toregion(rdata2, &r2);
233         return (isc_region_compare(&r1, &r2));
234 }
235
236 static inline isc_result_t
237 fromstruct_in_wks(ARGS_FROMSTRUCT) {
238         dns_rdata_in_wks_t *wks = source;
239         isc_uint32_t a;
240
241         REQUIRE(type == 11);
242         REQUIRE(rdclass == 1);
243         REQUIRE(source != NULL);
244         REQUIRE(wks->common.rdtype == type);
245         REQUIRE(wks->common.rdclass == rdclass);
246         REQUIRE((wks->map != NULL && wks->map_len <= 8*1024) ||
247                  wks->map_len == 0);
248
249         UNUSED(type);
250         UNUSED(rdclass);
251
252         a = ntohl(wks->in_addr.s_addr);
253         RETERR(uint32_tobuffer(a, target));
254         RETERR(uint16_tobuffer(wks->protocol, target));
255         return (mem_tobuffer(target, wks->map, wks->map_len));
256 }
257
258 static inline isc_result_t
259 tostruct_in_wks(ARGS_TOSTRUCT) {
260         dns_rdata_in_wks_t *wks = target;
261         isc_uint32_t n;
262         isc_region_t region;
263
264         REQUIRE(rdata->type == 11);
265         REQUIRE(rdata->rdclass == 1);
266         REQUIRE(rdata->length != 0);
267
268         wks->common.rdclass = rdata->rdclass;
269         wks->common.rdtype = rdata->type;
270         ISC_LINK_INIT(&wks->common, link);
271
272         dns_rdata_toregion(rdata, &region);
273         n = uint32_fromregion(&region);
274         wks->in_addr.s_addr = htonl(n);
275         isc_region_consume(&region, 4);
276         wks->protocol = uint16_fromregion(&region);
277         isc_region_consume(&region, 2);
278         wks->map_len = region.length;
279         wks->map = mem_maybedup(mctx, region.base, region.length);
280         if (wks->map == NULL)
281                 return (ISC_R_NOMEMORY);
282         wks->mctx = mctx;
283         return (ISC_R_SUCCESS);
284 }
285
286 static inline void
287 freestruct_in_wks(ARGS_FREESTRUCT) {
288         dns_rdata_in_wks_t *wks = source;
289
290         REQUIRE(source != NULL);
291         REQUIRE(wks->common.rdtype == 11);
292         REQUIRE(wks->common.rdclass == 1);
293
294         if (wks->mctx == NULL)
295                 return;
296
297         if (wks->map != NULL)
298                 isc_mem_free(wks->mctx, wks->map);
299         wks->mctx = NULL;
300 }
301
302 static inline isc_result_t
303 additionaldata_in_wks(ARGS_ADDLDATA) {
304         UNUSED(rdata);
305         UNUSED(add);
306         UNUSED(arg);
307
308         REQUIRE(rdata->type == 11);
309         REQUIRE(rdata->rdclass == 1);
310
311         return (ISC_R_SUCCESS);
312 }
313
314 static inline isc_result_t
315 digest_in_wks(ARGS_DIGEST) {
316         isc_region_t r;
317
318         REQUIRE(rdata->type == 11);
319         REQUIRE(rdata->rdclass == 1);
320
321         dns_rdata_toregion(rdata, &r);
322
323         return ((digest)(arg, &r));
324 }
325
326 static inline isc_boolean_t
327 checkowner_in_wks(ARGS_CHECKOWNER) {
328
329         REQUIRE(type == 11);
330         REQUIRE(rdclass == 1);
331
332         UNUSED(type);
333         UNUSED(rdclass);
334
335         return (dns_name_ishostname(name, wildcard));
336 }
337
338 static inline isc_boolean_t
339 checknames_in_wks(ARGS_CHECKNAMES) {
340
341         REQUIRE(rdata->type == 11);
342         REQUIRE(rdata->rdclass == 1);
343
344         UNUSED(rdata);
345         UNUSED(owner);
346         UNUSED(bad);
347
348         return (ISC_TRUE);
349 }
350
351 #endif  /* RDATA_IN_1_WKS_11_C */