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 static char rcsid[] = "$FreeBSD: src/lib/libc/net/getnetbydns.c,v 1.13.2.4 2002/10/11 11:07:13 ume Exp $";
64 #endif /* LIBC_SCCS and not lint */
66 #include <sys/param.h>
67 #include <sys/socket.h>
68 #include <netinet/in.h>
69 #include <arpa/inet.h>
70 #include <arpa/nameser.h>
81 #include "res_config.h"
89 #define MAXPACKET (64*1024)
93 u_char buf[MAXPACKET];
101 static struct netent *
102 getnetanswer(answer, anslen, net_i)
112 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
113 char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
114 char *in, *st, *pauxt, *bp, **ap;
115 char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
116 static struct netent net_entry;
117 static char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
120 * find first satisfactory answer
122 * answer --> +------------+ ( MESSAGE )
125 * | Question | the question for the name server
127 * | Answer | RRs answering the question
129 * | Authority | RRs pointing toward an authority
130 * | Additional | RRs holding additional information
133 eom = answer->buf + anslen;
135 ancount = ntohs(hp->ancount); /* #/records in the answer section */
136 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
138 buflen = sizeof(netbuf);
139 cp = answer->buf + HFIXEDSZ;
142 h_errno = HOST_NOT_FOUND;
147 while (qdcount-- > 0)
148 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
151 net_entry.n_aliases = net_aliases;
153 while (--ancount >= 0 && cp < eom) {
154 n = dn_expand(answer->buf, eom, cp, bp, buflen);
155 if ((n < 0) || !res_dnok(bp))
159 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
160 ans[sizeof(ans) - 1] = '\0';
163 cp += INT32SZ; /* TTL */
165 if (class == C_IN && type == T_PTR) {
166 n = dn_expand(answer->buf, eom, cp, bp, buflen);
167 if ((n < 0) || !res_hnok(bp)) {
176 net_entry.n_addrtype =
177 (class == C_IN) ? AF_INET : AF_UNSPEC;
185 net_entry.n_name = *net_entry.n_aliases;
186 net_entry.n_net = 0L;
189 in = *net_entry.n_aliases;
190 net_entry.n_name = &ans[0];
192 for (i = 0; i < 4; i++) {
193 for (st = in, nchar = 0;
197 if (nchar != 1 || *in != '0' || flag) {
201 (i==0) ?nchar : nchar+1);
202 paux1[(i==0) ? nchar : nchar+1] = '\0';
204 paux2 = strcat(paux1, paux2);
209 net_entry.n_net = inet_network(paux2);
212 net_entry.n_aliases++;
220 _getnetbydnsaddr(net, net_type)
221 register unsigned long net;
222 register int net_type;
224 unsigned int netbr[4];
229 struct netent *net_entry;
231 if (net_type != AF_INET)
234 for (nn = 4, net2 = net; net2; net2 >>= 8)
235 netbr[--nn] = net2 & 0xff;
237 case 3: /* Class A */
238 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
240 case 2: /* Class B */
241 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
243 case 1: /* Class C */
244 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
247 case 0: /* Class D - E */
248 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
252 if ((buf = malloc(sizeof(*buf))) == NULL) {
253 h_errno = NETDB_INTERNAL;
256 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
260 if (_res.options & RES_DEBUG)
261 printf("res_search failed\n");
264 } else if (anslen > sizeof(*buf)) {
267 if (_res.options & RES_DEBUG)
268 printf("res_search static buffer too small\n");
272 net_entry = getnetanswer(buf, anslen, BYADDR);
275 unsigned u_net = net; /* maybe net should be unsigned ? */
277 /* Strip trailing zeros */
278 while ((u_net & 0xff) == 0 && u_net != 0)
280 net_entry->n_net = u_net;
287 _getnetbydnsname(net)
288 register const char *net;
293 struct netent *net_entry;
295 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
296 h_errno = NETDB_INTERNAL;
299 if ((buf = malloc(sizeof(*buf))) == NULL) {
300 h_errno = NETDB_INTERNAL;
303 strncpy(qbuf, net, sizeof(qbuf) - 1);
304 qbuf[sizeof(qbuf) - 1] = '\0';
305 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
309 if (_res.options & RES_DEBUG)
310 printf("res_search failed\n");
313 } else if (anslen > sizeof(*buf)) {
316 if (_res.options & RES_DEBUG)
317 printf("res_search static buffer too small\n");
321 net_entry = getnetanswer(buf, anslen, BYNAME);
327 _setnetdnsent(stayopen)
331 _res.options |= RES_STAYOPEN | RES_USEVC;
337 _res.options &= ~(RES_STAYOPEN | RES_USEVC);