getnetbydns.c revision 32299
1/*-
2 * Copyright (c) 1985, 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
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.
41 *
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
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54 *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
55 *
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.
59 */
60
61#if defined(LIBC_SCCS) && !defined(lint)
62static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
63static char rcsid[] = "$Id: getnetbydns.c,v 1.11 1997/06/27 08:22:01 peter Exp $";
64#endif /* LIBC_SCCS and not lint */
65
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>
71
72#include <stdio.h>
73#include <netdb.h>
74#include <resolv.h>
75#include <ctype.h>
76#include <errno.h>
77#include <string.h>
78#include <unistd.h>
79#include <syslog.h>
80
81#include "res_config.h"
82
83extern int h_errno;
84
85#define BYADDR 0
86#define BYNAME 1
87#define	MAXALIASES	35
88
89#if PACKETSZ > 1024
90#define	MAXPACKET	PACKETSZ
91#else
92#define	MAXPACKET	1024
93#endif
94
95typedef union {
96	HEADER	hdr;
97	u_char	buf[MAXPACKET];
98} querybuf;
99
100typedef union {
101	long	al;
102	char	ac;
103} align;
104
105static struct netent *
106getnetanswer(answer, anslen, net_i)
107	querybuf *answer;
108	int anslen;
109	int net_i;
110{
111
112	register HEADER *hp;
113	register u_char *cp;
114	register int n;
115	u_char *eom;
116	int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
117	char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
118	char *in, *st, *pauxt, *bp, **ap;
119	char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
120static	struct netent net_entry;
121static	char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
122
123	/*
124	 * find first satisfactory answer
125	 *
126	 *      answer --> +------------+  ( MESSAGE )
127	 *		   |   Header   |
128	 *		   +------------+
129	 *		   |  Question  | the question for the name server
130	 *		   +------------+
131	 *		   |   Answer   | RRs answering the question
132	 *		   +------------+
133	 *		   | Authority  | RRs pointing toward an authority
134	 *		   | Additional | RRs holding additional information
135	 *		   +------------+
136	 */
137	eom = answer->buf + anslen;
138	hp = &answer->hdr;
139	ancount = ntohs(hp->ancount); /* #/records in the answer section */
140	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
141	bp = netbuf;
142	buflen = sizeof(netbuf);
143	cp = answer->buf + HFIXEDSZ;
144	if (!qdcount) {
145		if (hp->aa)
146			h_errno = HOST_NOT_FOUND;
147		else
148			h_errno = TRY_AGAIN;
149		return (NULL);
150	}
151	while (qdcount-- > 0)
152		cp += __dn_skipname(cp, eom) + QFIXEDSZ;
153	ap = net_aliases;
154	*ap = NULL;
155	net_entry.n_aliases = net_aliases;
156	haveanswer = 0;
157	while (--ancount >= 0 && cp < eom) {
158		n = dn_expand(answer->buf, eom, cp, bp, buflen);
159		if ((n < 0) || !res_dnok(bp))
160			break;
161		cp += n;
162		ans[0] = '\0';
163		(void)strncpy(&ans[0], bp, sizeof(ans) - 1);
164		ans[sizeof(ans) - 1] = '\0';
165		GETSHORT(type, cp);
166		GETSHORT(class, cp);
167		cp += INT32SZ;		/* TTL */
168		GETSHORT(n, cp);
169		if (class == C_IN && type == T_PTR) {
170			n = dn_expand(answer->buf, eom, cp, bp, buflen);
171			if ((n < 0) || !res_hnok(bp)) {
172				cp += n;
173				return (NULL);
174			}
175			cp += n;
176			*ap++ = bp;
177			bp += strlen(bp) + 1;
178			net_entry.n_addrtype =
179				(class == C_IN) ? AF_INET : AF_UNSPEC;
180			haveanswer++;
181		}
182	}
183	if (haveanswer) {
184		*ap = NULL;
185		switch (net_i) {
186		case BYADDR:
187			net_entry.n_name = *net_entry.n_aliases;
188			net_entry.n_net = 0L;
189			break;
190		case BYNAME:
191			in = *net_entry.n_aliases;
192			net_entry.n_name = &ans[0];
193			aux2[0] = '\0';
194			for (i = 0; i < 4; i++) {
195				for (st = in, nchar = 0;
196				     *st != '.';
197				     st++, nchar++)
198					;
199				if (nchar != 1 || *in != '0' || flag) {
200					flag = 1;
201					(void)strncpy(paux1,
202						      (i==0) ? in : in-1,
203						      (i==0) ?nchar : nchar+1);
204					paux1[(i==0) ? nchar : nchar+1] = '\0';
205					pauxt = paux2;
206					paux2 = strcat(paux1, paux2);
207					paux1 = pauxt;
208				}
209				in = ++st;
210			}
211			net_entry.n_net = inet_network(paux2);
212			break;
213		}
214		net_entry.n_aliases++;
215		return (&net_entry);
216	}
217	h_errno = TRY_AGAIN;
218	return (NULL);
219}
220
221struct netent *
222_getnetbydnsaddr(net, net_type)
223	register unsigned long net;
224	register int net_type;
225{
226	unsigned int netbr[4];
227	int nn, anslen;
228	querybuf buf;
229	char qbuf[MAXDNAME];
230	unsigned long net2;
231	struct netent *net_entry;
232
233	if (net_type != AF_INET)
234		return (NULL);
235
236	for (nn = 4, net2 = net; net2; net2 >>= 8)
237		netbr[--nn] = net2 & 0xff;
238	switch (nn) {
239	case 3: 	/* Class A */
240		sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
241		break;
242	case 2: 	/* Class B */
243		sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
244		break;
245	case 1: 	/* Class C */
246		sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
247		    netbr[1]);
248		break;
249	case 0: 	/* Class D - E */
250		sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
251		    netbr[1], netbr[0]);
252		break;
253	}
254	anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
255	if (anslen < 0) {
256#ifdef DEBUG
257		if (_res.options & RES_DEBUG)
258			printf("res_query failed\n");
259#endif
260		return (NULL);
261	}
262	net_entry = getnetanswer(&buf, anslen, BYADDR);
263	if (net_entry) {
264		unsigned u_net = net;	/* maybe net should be unsigned ? */
265
266		/* Strip trailing zeros */
267		while ((u_net & 0xff) == 0 && u_net != 0)
268			u_net >>= 8;
269		net_entry->n_net = u_net;
270		return (net_entry);
271	}
272	return (NULL);
273}
274
275struct netent *
276_getnetbydnsname(net)
277	register const char *net;
278{
279	int anslen;
280	querybuf buf;
281	char qbuf[MAXDNAME];
282
283	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
284		h_errno = NETDB_INTERNAL;
285		return (NULL);
286	}
287	strncpy(qbuf, net, sizeof(qbuf) - 1);
288	qbuf[sizeof(qbuf) - 1] = '\0';
289	anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
290	if (anslen < 0) {
291#ifdef DEBUG
292		if (_res.options & RES_DEBUG)
293			printf("res_query failed\n");
294#endif
295		return (NULL);
296	}
297	return getnetanswer(&buf, anslen, BYNAME);
298}
299
300void
301_setnetdnsent(stayopen)
302	int stayopen;
303{
304	if (stayopen)
305		_res.options |= RES_STAYOPEN | RES_USEVC;
306}
307
308void
309_endnetdnsent()
310{
311	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
312	res_close();
313}
314