kqueue: Make sure that kqueue sleep counter will not wrap around
[dragonfly.git] / lib / libc / net / getnetbydns.c
blob733cd714dbbe1247974dbb4a833bd43611f8bbe5
1 /*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 * -
29 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
31 * Permission to use, copy, modify, and distribute this software for any
32 * purpose with or without fee is hereby granted, provided that the above
33 * copyright notice and this permission notice appear in all copies, and that
34 * the name of Digital Equipment Corporation not be used in advertising or
35 * publicity pertaining to distribution of the document or software without
36 * specific, written prior permission.
38 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
41 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 * SOFTWARE.
46 * -
47 * --Copyright--
49 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
50 * $FreeBSD: src/lib/libc/net/getnetbydns.c,v 1.34 2007/01/09 00:28:02 imp Exp $
52 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
53 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
55 * Permission to use, copy, modify, and distribute this software for any
56 * purpose with or without fee is hereby granted, provided that the above
57 * copyright notice and this permission notice appear in all copies.
60 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <arpa/nameser.h>
66 #include <errno.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <syslog.h>
75 #include <stdarg.h>
76 #include <nsswitch.h>
78 #include "netdb_private.h"
79 #include "res_config.h"
81 #define BYADDR 0
82 #define BYNAME 1
84 #define MAXPACKET (64*1024)
86 typedef union {
87 HEADER hdr;
88 u_char buf[MAXPACKET];
89 } querybuf;
91 typedef union {
92 long al;
93 char ac;
94 } align;
97 * Reverse the order of first four dotted entries of in.
98 * Out must contain space for at least strlen(in) characters.
99 * The result does not include any leading 0s of in.
101 static void
102 ipreverse(char *in, char *out)
104 char *pos[4];
105 int len[4];
106 char *p, *start;
107 int i = 0;
108 int leading = 1;
110 /* Fill-in element positions and lengths: pos[], len[]. */
111 start = p = in;
112 for (;;) {
113 if (*p == '.' || *p == '\0') {
114 /* Leading 0? */
115 if (leading && p - start == 1 && *start == '0')
116 len[i] = 0;
117 else {
118 len[i] = p - start;
119 leading = 0;
121 pos[i] = start;
122 start = p + 1;
123 i++;
125 if (i == 4)
126 break;
127 if (*p == 0) {
128 for (; i < 4; i++) {
129 pos[i] = p;
130 len[i] = 0;
132 break;
134 p++;
137 /* Copy the entries in reverse order */
138 p = out;
139 leading = 1;
140 for (i = 3; i >= 0; i--) {
141 memcpy(p, pos[i], len[i]);
142 if (len[i])
143 leading = 0;
144 p += len[i];
145 /* Need a . separator? */
146 if (!leading && i > 0 && len[i - 1])
147 *p++ = '.';
149 *p = '\0';
152 static int
153 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
154 struct netent_data *ned, res_state statp)
157 HEADER *hp;
158 u_char *cp;
159 int n;
160 u_char *eom;
161 int type, class, ancount, qdcount, haveanswer;
162 char aux[MAXHOSTNAMELEN];
163 char ans[MAXHOSTNAMELEN];
164 char *in, *bp, *ep, **ap;
167 * find first satisfactory answer
169 * answer --> +------------+ ( MESSAGE )
170 * | Header |
171 * +------------+
172 * | Question | the question for the name server
173 * +------------+
174 * | Answer | RRs answering the question
175 * +------------+
176 * | Authority | RRs pointing toward an authority
177 * | Additional | RRs holding additional information
178 * +------------+
180 eom = answer->buf + anslen;
181 hp = &answer->hdr;
182 ancount = ntohs(hp->ancount); /* #/records in the answer section */
183 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
184 bp = ned->netbuf;
185 ep = ned->netbuf + sizeof(ned->netbuf);
186 cp = answer->buf + HFIXEDSZ;
187 if (!qdcount) {
188 if (hp->aa)
189 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
190 else
191 RES_SET_H_ERRNO(statp, TRY_AGAIN);
192 return (-1);
194 while (qdcount-- > 0)
195 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
196 ap = ned->net_aliases;
197 *ap = NULL;
198 ne->n_aliases = ned->net_aliases;
199 haveanswer = 0;
200 while (--ancount >= 0 && cp < eom) {
201 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
202 if ((n < 0) || !res_dnok(bp))
203 break;
204 cp += n;
205 ans[0] = '\0';
206 strncpy(&ans[0], bp, sizeof(ans) - 1);
207 ans[sizeof(ans) - 1] = '\0';
208 GETSHORT(type, cp);
209 GETSHORT(class, cp);
210 cp += INT32SZ; /* TTL */
211 GETSHORT(n, cp);
212 if (class == C_IN && type == T_PTR) {
213 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
214 if ((n < 0) || !res_hnok(bp)) {
215 cp += n;
216 return (-1);
218 cp += n;
219 *ap++ = bp;
220 n = strlen(bp) + 1;
221 bp += n;
222 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
223 haveanswer++;
226 if (haveanswer) {
227 *ap = NULL;
228 switch (net_i) {
229 case BYADDR:
230 ne->n_name = *ne->n_aliases;
231 ne->n_net = 0L;
232 break;
233 case BYNAME:
234 in = *ne->n_aliases;
235 n = strlen(ans) + 1;
236 if (ep - bp < n) {
237 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
238 errno = ENOBUFS;
239 return (-1);
241 strlcpy(bp, ans, ep - bp);
242 ne->n_name = bp;
243 if (strlen(in) + 1 > sizeof(aux)) {
244 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
245 errno = ENOBUFS;
246 return (-1);
248 ipreverse(in, aux);
249 ne->n_net = inet_network(aux);
250 break;
252 ne->n_aliases++;
253 return (0);
255 RES_SET_H_ERRNO(statp, TRY_AGAIN);
256 return (-1);
260 _dns_getnetbyaddr(void *rval, void *cb_data __unused, va_list ap)
262 uint32_t net;
263 int net_type;
264 char *buffer;
265 size_t buflen;
266 int *errnop __unused;
267 int *h_errnop;
268 struct netent *nptr, ne;
269 struct netent_data *ned;
270 unsigned int netbr[4];
271 int nn, anslen, error;
272 querybuf *buf;
273 char qbuf[MAXDNAME];
274 uint32_t net2;
275 res_state statp;
277 net = va_arg(ap, uint32_t);
278 net_type = va_arg(ap, int);
279 nptr = va_arg(ap, struct netent *);
280 buffer = va_arg(ap, char *);
281 buflen = va_arg(ap, size_t);
282 errnop = va_arg(ap, int *);
283 h_errnop = va_arg(ap, int *);
285 statp = __res_state();
286 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
287 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
288 *h_errnop = statp->res_h_errno;
289 return (NS_UNAVAIL);
292 if ((ned = __netent_data_init()) == NULL) {
293 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
294 *h_errnop = statp->res_h_errno;
295 return (NS_UNAVAIL);
298 *((struct netent **)rval) = NULL;
300 if (net_type != AF_INET) {
301 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
302 *h_errnop = statp->res_h_errno;
303 return (NS_UNAVAIL);
306 for (nn = 4, net2 = net; net2; net2 >>= 8)
307 netbr[--nn] = net2 & 0xff;
308 switch (nn) {
309 case 3: /* Class A */
310 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
311 break;
312 case 2: /* Class B */
313 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
314 break;
315 case 1: /* Class C */
316 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
317 netbr[1]);
318 break;
319 case 0: /* Class D - E */
320 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
321 netbr[1], netbr[0]);
322 break;
324 if ((buf = malloc(sizeof(*buf))) == NULL) {
325 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
326 *h_errnop = statp->res_h_errno;
327 return (NS_NOTFOUND);
329 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
330 sizeof(*buf));
331 if (anslen < 0) {
332 free(buf);
333 #ifdef DEBUG
334 if (statp->options & RES_DEBUG)
335 printf("res_nsearch failed\n");
336 #endif
337 *h_errnop = statp->res_h_errno;
338 return (NS_UNAVAIL);
339 } else if (anslen > sizeof(*buf)) {
340 free(buf);
341 #ifdef DEBUG
342 if (statp->options & RES_DEBUG)
343 printf("res_nsearch static buffer too small\n");
344 #endif
345 *h_errnop = statp->res_h_errno;
346 return (NS_UNAVAIL);
348 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
349 free(buf);
350 if (error == 0) {
351 /* Strip trailing zeros */
352 while ((net & 0xff) == 0 && net != 0)
353 net >>= 8;
354 ne.n_net = net;
355 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
356 *h_errnop = statp->res_h_errno;
357 return (NS_NOTFOUND);
359 *((struct netent **)rval) = nptr;
360 return (NS_SUCCESS);
362 *h_errnop = statp->res_h_errno;
363 return (NS_NOTFOUND);
367 _dns_getnetbyname(void *rval, void *cb_data __unused, va_list ap)
369 const char *net;
370 char *buffer;
371 size_t buflen;
372 int *errnop __unused;
373 int *h_errnop;
374 struct netent *nptr, ne;
375 struct netent_data *ned;
376 int anslen, error;
377 querybuf *buf;
378 char qbuf[MAXDNAME];
379 res_state statp;
381 net = va_arg(ap, const char *);
382 nptr = va_arg(ap, struct netent *);
383 buffer = va_arg(ap, char *);
384 buflen = va_arg(ap, size_t);
385 errnop = va_arg(ap, int *);
386 h_errnop = va_arg(ap, int *);
388 statp = __res_state();
389 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
390 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
391 *h_errnop = statp->res_h_errno;
392 return (NS_UNAVAIL);
394 if ((ned = __netent_data_init()) == NULL) {
395 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
396 *h_errnop = statp->res_h_errno;
397 return (NS_UNAVAIL);
399 if ((buf = malloc(sizeof(*buf))) == NULL) {
400 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
401 *h_errnop = statp->res_h_errno;
402 return (NS_NOTFOUND);
405 *((struct netent **)rval) = NULL;
407 strncpy(qbuf, net, sizeof(qbuf) - 1);
408 qbuf[sizeof(qbuf) - 1] = '\0';
409 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
410 sizeof(*buf));
411 if (anslen < 0) {
412 free(buf);
413 #ifdef DEBUG
414 if (statp->options & RES_DEBUG)
415 printf("res_nsearch failed\n");
416 #endif
417 return (NS_UNAVAIL);
418 } else if (anslen > sizeof(*buf)) {
419 free(buf);
420 #ifdef DEBUG
421 if (statp->options & RES_DEBUG)
422 printf("res_search static buffer too small\n");
423 #endif
424 return (NS_UNAVAIL);
426 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp);
427 free(buf);
428 if (error != 0) {
429 *h_errnop = statp->res_h_errno;
430 return (NS_NOTFOUND);
432 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
433 *h_errnop = statp->res_h_errno;
434 return (NS_NOTFOUND);
436 *((struct netent **)rval) = nptr;
437 return (NS_SUCCESS);
440 void
441 _setnetdnsent(int stayopen)
443 res_state statp;
445 statp = __res_state();
446 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
447 return;
448 if (stayopen)
449 statp->options |= RES_STAYOPEN | RES_USEVC;
452 void
453 _endnetdnsent(void)
455 res_state statp;
457 statp = __res_state();
458 statp->options &= ~(RES_STAYOPEN | RES_USEVC);
459 res_nclose(statp);