2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
52 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
54 * Permission to use, copy, modify, and distribute this software for any
55 * purpose with or without fee is hereby granted, provided that the above
56 * copyright notice and this permission notice appear in all copies.
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include <sys/param.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <arpa/nameser.h>
83 #include "netdb_private.h"
84 #include "res_config.h"
89 #define MAXPACKET (64*1024)
93 u_char buf[MAXPACKET];
102 * Reverse the order of first four dotted entries of in.
103 * Out must contain space for at least strlen(in) characters.
104 * The result does not include any leading 0s of in.
107 ipreverse(char *in, char *out)
115 /* Fill-in element positions and lengths: pos[], len[]. */
118 if (*p == '.' || *p == '\0') {
120 if (leading && p - start == 1 && *start == '0')
142 /* Copy the entries in reverse order */
145 for (i = 3; i >= 0; i--) {
146 memcpy(p, pos[i], len[i]);
150 /* Need a . separator? */
151 if (!leading && i > 0 && len[i - 1])
158 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
159 struct netent_data *ned, res_state statp)
166 int type, class, ancount, qdcount, haveanswer;
167 char aux[MAXHOSTNAMELEN];
168 char ans[MAXHOSTNAMELEN];
169 char *in, *bp, *ep, **ap;
172 * find first satisfactory answer
174 * answer --> +------------+ ( MESSAGE )
177 * | Question | the question for the name server
179 * | Answer | RRs answering the question
181 * | Authority | RRs pointing toward an authority
182 * | Additional | RRs holding additional information
185 eom = answer->buf + anslen;
187 ancount = ntohs(hp->ancount); /* #/records in the answer section */
188 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
190 ep = ned->netbuf + sizeof(ned->netbuf);
191 cp = answer->buf + HFIXEDSZ;
194 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
196 RES_SET_H_ERRNO(statp, TRY_AGAIN);
199 while (qdcount-- > 0)
200 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
201 ap = ned->net_aliases;
203 ne->n_aliases = ned->net_aliases;
205 while (--ancount >= 0 && cp < eom) {
206 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
207 if ((n < 0) || !res_dnok(bp))
211 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
212 ans[sizeof(ans) - 1] = '\0';
215 cp += INT32SZ; /* TTL */
217 if (class == C_IN && type == T_PTR) {
218 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
219 if ((n < 0) || !res_hnok(bp)) {
227 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
235 ne->n_name = *ne->n_aliases;
242 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
246 strlcpy(bp, ans, ep - bp);
248 if (strlen(in) + 1 > sizeof(aux)) {
249 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
254 ne->n_net = inet_network(aux);
260 RES_SET_H_ERRNO(statp, TRY_AGAIN);
265 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
271 int *errnop, *h_errnop;
272 struct netent *nptr, ne;
273 struct netent_data *ned;
274 unsigned int netbr[4];
275 int nn, anslen, error;
281 net = va_arg(ap, uint32_t);
282 net_type = va_arg(ap, int);
283 nptr = va_arg(ap, struct netent *);
284 buffer = va_arg(ap, char *);
285 buflen = va_arg(ap, size_t);
286 errnop = va_arg(ap, int *);
287 h_errnop = va_arg(ap, int *);
289 statp = __res_state();
290 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
291 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
292 *h_errnop = statp->res_h_errno;
296 if ((ned = __netent_data_init()) == NULL) {
297 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
298 *h_errnop = statp->res_h_errno;
302 *((struct netent **)rval) = NULL;
304 if (net_type != AF_INET) {
305 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
306 *h_errnop = statp->res_h_errno;
310 for (nn = 4, net2 = net; net2; net2 >>= 8)
311 netbr[--nn] = net2 & 0xff;
313 case 3: /* Class A */
314 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
316 case 2: /* Class B */
317 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
319 case 1: /* Class C */
320 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
323 case 0: /* Class D - E */
324 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
328 if ((buf = malloc(sizeof(*buf))) == NULL) {
329 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
330 *h_errnop = statp->res_h_errno;
331 return (NS_NOTFOUND);
333 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
338 if (statp->options & RES_DEBUG)
339 printf("res_nsearch failed\n");
341 *h_errnop = statp->res_h_errno;
343 } else if (anslen > sizeof(*buf)) {
346 if (statp->options & RES_DEBUG)
347 printf("res_nsearch static buffer too small\n");
349 *h_errnop = statp->res_h_errno;
352 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
355 /* Strip trailing zeros */
356 while ((net & 0xff) == 0 && net != 0)
359 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
361 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
362 *h_errnop = statp->res_h_errno;
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) {
439 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
440 *h_errnop = statp->res_h_errno;
443 *((struct netent **)rval) = nptr;
448 _setnetdnsent(int stayopen)
452 statp = __res_state();
453 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
456 statp->options |= RES_STAYOPEN | RES_USEVC;
464 statp = __res_state();
465 statp->options &= ~(RES_STAYOPEN | RES_USEVC);