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 * 4. 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
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
49 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
50 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
52 * Permission to use, copy, modify, and distribute this software for any
53 * purpose with or without fee is hereby granted, provided that the above
54 * copyright notice and this permission notice appear in all copies.
57 #if defined(LIBC_SCCS) && !defined(lint)
58 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
59 #endif /* LIBC_SCCS and not lint */
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/param.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <arpa/nameser.h>
81 #include "netdb_private.h"
82 #include "res_config.h"
87 #define MAXPACKET (64*1024)
91 u_char buf[MAXPACKET];
100 * Reverse the order of first four dotted entries of in.
101 * Out must contain space for at least strlen(in) characters.
102 * The result does not include any leading 0s of in.
105 ipreverse(char *in, char *out)
113 /* Fill-in element positions and lengths: pos[], len[]. */
116 if (*p == '.' || *p == '\0') {
118 if (leading && p - start == 1 && *start == '0')
140 /* Copy the entries in reverse order */
143 for (i = 3; i >= 0; i--) {
144 memcpy(p, pos[i], len[i]);
148 /* Need a . separator? */
149 if (!leading && i > 0 && len[i - 1])
156 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
157 struct netent_data *ned, res_state statp)
164 int type, class, ancount, qdcount, haveanswer;
165 char aux[MAXHOSTNAMELEN];
166 char ans[MAXHOSTNAMELEN];
167 char *in, *bp, *ep, **ap;
170 * find first satisfactory answer
172 * answer --> +------------+ ( MESSAGE )
175 * | Question | the question for the name server
177 * | Answer | RRs answering the question
179 * | Authority | RRs pointing toward an authority
180 * | Additional | RRs holding additional information
183 eom = answer->buf + anslen;
185 ancount = ntohs(hp->ancount); /* #/records in the answer section */
186 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
188 ep = ned->netbuf + sizeof(ned->netbuf);
189 cp = answer->buf + HFIXEDSZ;
192 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
194 RES_SET_H_ERRNO(statp, TRY_AGAIN);
197 while (qdcount-- > 0)
198 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
199 ap = ned->net_aliases;
201 ne->n_aliases = ned->net_aliases;
203 while (--ancount >= 0 && cp < eom) {
204 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
205 if ((n < 0) || !res_dnok(bp))
209 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
210 ans[sizeof(ans) - 1] = '\0';
213 cp += INT32SZ; /* TTL */
215 if (class == C_IN && type == T_PTR) {
216 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
217 if ((n < 0) || !res_hnok(bp)) {
225 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
233 ne->n_name = *ne->n_aliases;
240 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
244 strlcpy(bp, ans, ep - bp);
246 if (strlen(in) + 1 > sizeof(aux)) {
247 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
252 ne->n_net = inet_network(aux);
258 RES_SET_H_ERRNO(statp, TRY_AGAIN);
263 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
269 int *errnop, *h_errnop;
270 struct netent *nptr, ne;
271 struct netent_data *ned;
272 unsigned int netbr[4];
273 int nn, anslen, error;
279 net = va_arg(ap, uint32_t);
280 net_type = va_arg(ap, int);
281 nptr = va_arg(ap, struct netent *);
282 buffer = va_arg(ap, char *);
283 buflen = va_arg(ap, size_t);
284 errnop = va_arg(ap, int *);
285 h_errnop = va_arg(ap, int *);
287 statp = __res_state();
288 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
289 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
290 *h_errnop = statp->res_h_errno;
294 if ((ned = __netent_data_init()) == NULL) {
295 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
296 *h_errnop = statp->res_h_errno;
300 *((struct netent **)rval) = NULL;
302 if (net_type != AF_INET) {
303 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
304 *h_errnop = statp->res_h_errno;
308 for (nn = 4, net2 = net; net2; net2 >>= 8)
309 netbr[--nn] = net2 & 0xff;
311 case 3: /* Class A */
312 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
314 case 2: /* Class B */
315 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
317 case 1: /* Class C */
318 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
321 case 0: /* Class D - E */
322 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
326 if ((buf = malloc(sizeof(*buf))) == NULL) {
327 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
328 *h_errnop = statp->res_h_errno;
329 return (NS_NOTFOUND);
331 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
336 if (statp->options & RES_DEBUG)
337 printf("res_nsearch failed\n");
339 *h_errnop = statp->res_h_errno;
341 } else if (anslen > sizeof(*buf)) {
344 if (statp->options & RES_DEBUG)
345 printf("res_nsearch static buffer too small\n");
347 *h_errnop = statp->res_h_errno;
350 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
353 /* Strip trailing zeros */
354 while ((net & 0xff) == 0 && net != 0)
357 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
358 *h_errnop = statp->res_h_errno;
359 return (NS_NOTFOUND);
361 *((struct netent **)rval) = nptr;
364 *h_errnop = statp->res_h_errno;
365 return (NS_NOTFOUND);
369 _dns_getnetbyname(void *rval, void *cb_data, va_list ap)
374 int *errnop, *h_errnop;
375 struct netent *nptr, ne;
376 struct netent_data *ned;
382 net = va_arg(ap, const char *);
383 nptr = va_arg(ap, struct netent *);
384 buffer = va_arg(ap, char *);
385 buflen = va_arg(ap, size_t);
386 errnop = va_arg(ap, int *);
387 h_errnop = va_arg(ap, int *);
389 statp = __res_state();
390 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
391 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
392 *h_errnop = statp->res_h_errno;
395 if ((ned = __netent_data_init()) == NULL) {
396 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
397 *h_errnop = statp->res_h_errno;
400 if ((buf = malloc(sizeof(*buf))) == NULL) {
401 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
402 *h_errnop = statp->res_h_errno;
403 return (NS_NOTFOUND);
406 *((struct netent **)rval) = NULL;
408 strncpy(qbuf, net, sizeof(qbuf) - 1);
409 qbuf[sizeof(qbuf) - 1] = '\0';
410 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
415 if (statp->options & RES_DEBUG)
416 printf("res_nsearch failed\n");
419 } else if (anslen > sizeof(*buf)) {
422 if (statp->options & RES_DEBUG)
423 printf("res_search static buffer too small\n");
427 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp);
430 *h_errnop = statp->res_h_errno;
431 return (NS_NOTFOUND);
433 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
434 *h_errnop = statp->res_h_errno;
435 return (NS_NOTFOUND);
437 *((struct netent **)rval) = nptr;
442 _setnetdnsent(int stayopen)
446 statp = __res_state();
447 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
450 statp->options |= RES_STAYOPEN | RES_USEVC;
458 statp = __res_state();
459 statp->options &= ~(RES_STAYOPEN | RES_USEVC);