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
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies.
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <arpa/nameser.h>
85 #include "netdb_private.h"
86 #include "res_config.h"
91 #define MAXPACKET (64*1024)
95 u_char buf[MAXPACKET];
104 * Reverse the order of first four dotted entries of in.
105 * Out must contain space for at least strlen(in) characters.
106 * The result does not include any leading 0s of in.
109 ipreverse(char *in, char *out)
117 /* Fill-in element positions and lengths: pos[], len[]. */
120 if (*p == '.' || *p == '\0') {
122 if (leading && p - start == 1 && *start == '0')
144 /* Copy the entries in reverse order */
147 for (i = 3; i >= 0; i--) {
148 memcpy(p, pos[i], len[i]);
152 /* Need a . separator? */
153 if (!leading && i > 0 && len[i - 1])
160 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
161 struct netent_data *ned, res_state statp)
168 int type, class, ancount, qdcount, haveanswer;
169 char aux[MAXHOSTNAMELEN];
170 char ans[MAXHOSTNAMELEN];
171 char *in, *bp, *ep, **ap;
174 * find first satisfactory answer
176 * answer --> +------------+ ( MESSAGE )
179 * | Question | the question for the name server
181 * | Answer | RRs answering the question
183 * | Authority | RRs pointing toward an authority
184 * | Additional | RRs holding additional information
187 eom = answer->buf + anslen;
189 ancount = ntohs(hp->ancount); /* #/records in the answer section */
190 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
192 ep = ned->netbuf + sizeof(ned->netbuf);
193 cp = answer->buf + HFIXEDSZ;
196 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
198 RES_SET_H_ERRNO(statp, TRY_AGAIN);
201 while (qdcount-- > 0)
202 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
203 ap = ned->net_aliases;
205 ne->n_aliases = ned->net_aliases;
207 while (--ancount >= 0 && cp < eom) {
208 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
209 if ((n < 0) || !res_dnok(bp))
213 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
214 ans[sizeof(ans) - 1] = '\0';
217 cp += INT32SZ; /* TTL */
219 if (class == C_IN && type == T_PTR) {
220 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
221 if ((n < 0) || !res_hnok(bp)) {
229 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
237 ne->n_name = *ne->n_aliases;
244 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
248 strlcpy(bp, ans, ep - bp);
250 if (strlen(in) + 1 > sizeof(aux)) {
251 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
256 ne->n_net = inet_network(aux);
262 RES_SET_H_ERRNO(statp, TRY_AGAIN);
267 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
273 int *errnop, *h_errnop;
274 struct netent *nptr, ne;
275 struct netent_data *ned;
276 unsigned int netbr[4];
277 int nn, anslen, error;
283 net = va_arg(ap, uint32_t);
284 net_type = va_arg(ap, int);
285 nptr = va_arg(ap, struct netent *);
286 buffer = va_arg(ap, char *);
287 buflen = va_arg(ap, size_t);
288 errnop = va_arg(ap, int *);
289 h_errnop = va_arg(ap, int *);
291 statp = __res_state();
292 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
293 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
294 *h_errnop = statp->res_h_errno;
298 if ((ned = __netent_data_init()) == NULL) {
299 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
300 *h_errnop = statp->res_h_errno;
304 *((struct netent **)rval) = NULL;
306 if (net_type != AF_INET) {
307 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
308 *h_errnop = statp->res_h_errno;
312 for (nn = 4, net2 = net; net2; net2 >>= 8)
313 netbr[--nn] = net2 & 0xff;
315 case 3: /* Class A */
316 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
318 case 2: /* Class B */
319 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
321 case 1: /* Class C */
322 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
325 case 0: /* Class D - E */
326 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
330 if ((buf = malloc(sizeof(*buf))) == NULL) {
331 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
332 *h_errnop = statp->res_h_errno;
333 return (NS_NOTFOUND);
335 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
340 if (statp->options & RES_DEBUG)
341 printf("res_nsearch failed\n");
343 *h_errnop = statp->res_h_errno;
345 } else if (anslen > sizeof(*buf)) {
348 if (statp->options & RES_DEBUG)
349 printf("res_nsearch static buffer too small\n");
351 *h_errnop = statp->res_h_errno;
354 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
357 /* Strip trailing zeros */
358 while ((net & 0xff) == 0 && net != 0)
361 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
362 *h_errnop = statp->res_h_errno;
363 return (NS_NOTFOUND);
365 *((struct netent **)rval) = nptr;
368 *h_errnop = statp->res_h_errno;
369 return (NS_NOTFOUND);
373 _dns_getnetbyname(void *rval, void *cb_data, va_list ap)
378 int *errnop, *h_errnop;
379 struct netent *nptr, ne;
380 struct netent_data *ned;
386 net = va_arg(ap, const char *);
387 nptr = va_arg(ap, struct netent *);
388 buffer = va_arg(ap, char *);
389 buflen = va_arg(ap, size_t);
390 errnop = va_arg(ap, int *);
391 h_errnop = va_arg(ap, int *);
393 statp = __res_state();
394 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
395 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
396 *h_errnop = statp->res_h_errno;
399 if ((ned = __netent_data_init()) == NULL) {
400 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
401 *h_errnop = statp->res_h_errno;
404 if ((buf = malloc(sizeof(*buf))) == NULL) {
405 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
406 *h_errnop = statp->res_h_errno;
407 return (NS_NOTFOUND);
410 *((struct netent **)rval) = NULL;
412 strncpy(qbuf, net, sizeof(qbuf) - 1);
413 qbuf[sizeof(qbuf) - 1] = '\0';
414 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
419 if (statp->options & RES_DEBUG)
420 printf("res_nsearch failed\n");
423 } else if (anslen > sizeof(*buf)) {
426 if (statp->options & RES_DEBUG)
427 printf("res_search static buffer too small\n");
431 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp);
434 *h_errnop = statp->res_h_errno;
435 return (NS_NOTFOUND);
437 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
438 *h_errnop = statp->res_h_errno;
439 return (NS_NOTFOUND);
441 *((struct netent **)rval) = nptr;
446 _setnetdnsent(int stayopen)
450 statp = __res_state();
451 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
454 statp->options |= RES_STAYOPEN | RES_USEVC;
462 statp = __res_state();
463 statp->options &= ~(RES_STAYOPEN | RES_USEVC);