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. 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 * @(#)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>
78 #include "netdb_private.h"
79 #include "res_config.h"
84 #define MAXPACKET (64*1024)
88 u_char buf[MAXPACKET];
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.
102 ipreverse(char *in, char *out)
110 /* Fill-in element positions and lengths: pos[], len[]. */
113 if (*p == '.' || *p == '\0') {
115 if (leading && p - start == 1 && *start == '0')
137 /* Copy the entries in reverse order */
140 for (i = 3; i >= 0; i--) {
141 memcpy(p, pos[i], len[i]);
145 /* Need a . separator? */
146 if (!leading && i > 0 && len[i - 1])
153 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
154 struct netent_data *ned, res_state statp)
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 )
172 * | Question | the question for the name server
174 * | Answer | RRs answering the question
176 * | Authority | RRs pointing toward an authority
177 * | Additional | RRs holding additional information
180 eom = answer->buf + anslen;
182 ancount = ntohs(hp->ancount); /* #/records in the answer section */
183 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
185 ep = ned->netbuf + sizeof(ned->netbuf);
186 cp = answer->buf + HFIXEDSZ;
189 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
191 RES_SET_H_ERRNO(statp, TRY_AGAIN);
194 while (qdcount-- > 0)
195 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
196 ap = ned->net_aliases;
198 ne->n_aliases = ned->net_aliases;
200 while (--ancount >= 0 && cp < eom) {
201 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
202 if ((n < 0) || !res_dnok(bp))
206 strncpy(&ans[0], bp, sizeof(ans) - 1);
207 ans[sizeof(ans) - 1] = '\0';
210 cp += INT32SZ; /* TTL */
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)) {
222 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
230 ne->n_name = *ne->n_aliases;
237 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
241 strlcpy(bp, ans, ep - bp);
243 if (strlen(in) + 1 > sizeof(aux)) {
244 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
249 ne->n_net = inet_network(aux);
255 RES_SET_H_ERRNO(statp, TRY_AGAIN);
260 _dns_getnetbyaddr(void *rval, void *cb_data __unused, va_list ap)
266 int *errnop, *h_errnop;
267 struct netent *nptr, ne;
268 struct netent_data *ned;
269 unsigned int netbr[4];
270 int nn, anslen, error;
276 net = va_arg(ap, uint32_t);
277 net_type = va_arg(ap, int);
278 nptr = va_arg(ap, struct netent *);
279 buffer = va_arg(ap, char *);
280 buflen = va_arg(ap, size_t);
281 errnop = va_arg(ap, int *);
282 h_errnop = va_arg(ap, int *);
284 statp = __res_state();
285 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
286 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
287 *h_errnop = statp->res_h_errno;
291 if ((ned = __netent_data_init()) == NULL) {
292 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
293 *h_errnop = statp->res_h_errno;
297 *((struct netent **)rval) = NULL;
299 if (net_type != AF_INET) {
300 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
301 *h_errnop = statp->res_h_errno;
305 for (nn = 4, net2 = net; net2; net2 >>= 8)
306 netbr[--nn] = net2 & 0xff;
308 case 3: /* Class A */
309 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
311 case 2: /* Class B */
312 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
314 case 1: /* Class C */
315 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
318 case 0: /* Class D - E */
319 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
323 if ((buf = malloc(sizeof(*buf))) == NULL) {
324 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
325 *h_errnop = statp->res_h_errno;
326 return (NS_NOTFOUND);
328 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
333 if (statp->options & RES_DEBUG)
334 printf("res_nsearch failed\n");
336 *h_errnop = statp->res_h_errno;
338 } else if (anslen > sizeof(*buf)) {
341 if (statp->options & RES_DEBUG)
342 printf("res_nsearch static buffer too small\n");
344 *h_errnop = statp->res_h_errno;
347 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
350 /* Strip trailing zeros */
351 while ((net & 0xff) == 0 && net != 0)
354 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
355 *h_errnop = statp->res_h_errno;
356 return (NS_NOTFOUND);
358 *((struct netent **)rval) = nptr;
361 *h_errnop = statp->res_h_errno;
362 return (NS_NOTFOUND);
366 _dns_getnetbyname(void *rval, void *cb_data __unused, va_list ap)
371 int *errnop, *h_errnop;
372 struct netent *nptr, ne;
373 struct netent_data *ned;
379 net = va_arg(ap, const char *);
380 nptr = va_arg(ap, struct netent *);
381 buffer = va_arg(ap, char *);
382 buflen = va_arg(ap, size_t);
383 errnop = va_arg(ap, int *);
384 h_errnop = va_arg(ap, int *);
386 statp = __res_state();
387 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
388 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
389 *h_errnop = statp->res_h_errno;
392 if ((ned = __netent_data_init()) == NULL) {
393 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
394 *h_errnop = statp->res_h_errno;
397 if ((buf = malloc(sizeof(*buf))) == NULL) {
398 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
399 *h_errnop = statp->res_h_errno;
400 return (NS_NOTFOUND);
403 *((struct netent **)rval) = NULL;
405 strncpy(qbuf, net, sizeof(qbuf) - 1);
406 qbuf[sizeof(qbuf) - 1] = '\0';
407 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
412 if (statp->options & RES_DEBUG)
413 printf("res_nsearch failed\n");
416 } else if (anslen > sizeof(*buf)) {
419 if (statp->options & RES_DEBUG)
420 printf("res_search static buffer too small\n");
424 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp);
427 *h_errnop = statp->res_h_errno;
428 return (NS_NOTFOUND);
430 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
431 *h_errnop = statp->res_h_errno;
432 return (NS_NOTFOUND);
434 *((struct netent **)rval) = nptr;
439 _setnetdnsent(int stayopen)
443 statp = __res_state();
444 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
447 statp->options |= RES_STAYOPEN | RES_USEVC;
455 statp = __res_state();
456 statp->options &= ~(RES_STAYOPEN | RES_USEVC);