xref: /freebsd/lib/libc/net/getnetbydns.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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)
62 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66 
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <arpa/nameser.h>
72 
73 #include <stdio.h>
74 #include <netdb.h>
75 #include <resolv.h>
76 #include <ctype.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <syslog.h>
80 #include <stdarg.h>
81 #include <nsswitch.h>
82 
83 #include "res_config.h"
84 
85 extern int h_errno;
86 
87 #define BYADDR 0
88 #define BYNAME 1
89 #define	MAXALIASES	35
90 
91 #if PACKETSZ > 1024
92 #define	MAXPACKET	PACKETSZ
93 #else
94 #define	MAXPACKET	1024
95 #endif
96 
97 typedef union {
98 	HEADER	hdr;
99 	u_char	buf[MAXPACKET];
100 } querybuf;
101 
102 typedef union {
103 	long	al;
104 	char	ac;
105 } align;
106 
107 static struct netent *
108 getnetanswer(answer, anslen, net_i)
109 	querybuf *answer;
110 	int anslen;
111 	int net_i;
112 {
113 
114 	HEADER *hp;
115 	u_char *cp;
116 	int n;
117 	u_char *eom;
118 	int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
119 	char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
120 	char *in, *st, *pauxt, *bp, **ap;
121 	char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
122 static	struct netent net_entry;
123 static	char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
124 
125 	/*
126 	 * find first satisfactory answer
127 	 *
128 	 *      answer --> +------------+  ( MESSAGE )
129 	 *		   |   Header   |
130 	 *		   +------------+
131 	 *		   |  Question  | the question for the name server
132 	 *		   +------------+
133 	 *		   |   Answer   | RRs answering the question
134 	 *		   +------------+
135 	 *		   | Authority  | RRs pointing toward an authority
136 	 *		   | Additional | RRs holding additional information
137 	 *		   +------------+
138 	 */
139 	eom = answer->buf + anslen;
140 	hp = &answer->hdr;
141 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
142 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
143 	bp = netbuf;
144 	buflen = sizeof(netbuf);
145 	cp = answer->buf + HFIXEDSZ;
146 	if (!qdcount) {
147 		if (hp->aa)
148 			h_errno = HOST_NOT_FOUND;
149 		else
150 			h_errno = TRY_AGAIN;
151 		return (NULL);
152 	}
153 	while (qdcount-- > 0)
154 		cp += __dn_skipname(cp, eom) + QFIXEDSZ;
155 	ap = net_aliases;
156 	*ap = NULL;
157 	net_entry.n_aliases = net_aliases;
158 	haveanswer = 0;
159 	while (--ancount >= 0 && cp < eom) {
160 		n = dn_expand(answer->buf, eom, cp, bp, buflen);
161 		if ((n < 0) || !res_dnok(bp))
162 			break;
163 		cp += n;
164 		ans[0] = '\0';
165 		(void)strncpy(&ans[0], bp, sizeof(ans) - 1);
166 		ans[sizeof(ans) - 1] = '\0';
167 		GETSHORT(type, cp);
168 		GETSHORT(class, cp);
169 		cp += INT32SZ;		/* TTL */
170 		GETSHORT(n, cp);
171 		if (class == C_IN && type == T_PTR) {
172 			n = dn_expand(answer->buf, eom, cp, bp, buflen);
173 			if ((n < 0) || !res_hnok(bp)) {
174 				cp += n;
175 				return (NULL);
176 			}
177 			cp += n;
178 			*ap++ = bp;
179 			bp += strlen(bp) + 1;
180 			net_entry.n_addrtype =
181 				(class == C_IN) ? AF_INET : AF_UNSPEC;
182 			haveanswer++;
183 		}
184 	}
185 	if (haveanswer) {
186 		*ap = NULL;
187 		switch (net_i) {
188 		case BYADDR:
189 			net_entry.n_name = *net_entry.n_aliases;
190 			net_entry.n_net = 0L;
191 			break;
192 		case BYNAME:
193 			in = *net_entry.n_aliases;
194 			net_entry.n_name = &ans[0];
195 			aux2[0] = '\0';
196 			for (i = 0; i < 4; i++) {
197 				for (st = in, nchar = 0;
198 				     *st != '.';
199 				     st++, nchar++)
200 					;
201 				if (nchar != 1 || *in != '0' || flag) {
202 					flag = 1;
203 					(void)strncpy(paux1,
204 						      (i==0) ? in : in-1,
205 						      (i==0) ?nchar : nchar+1);
206 					paux1[(i==0) ? nchar : nchar+1] = '\0';
207 					pauxt = paux2;
208 					paux2 = strcat(paux1, paux2);
209 					paux1 = pauxt;
210 				}
211 				in = ++st;
212 			}
213 			net_entry.n_net = inet_network(paux2);
214 			break;
215 		}
216 		net_entry.n_aliases++;
217 		return (&net_entry);
218 	}
219 	h_errno = TRY_AGAIN;
220 	return (NULL);
221 }
222 
223 int
224 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
225 {
226 	unsigned long net;
227 	int net_type;
228 	unsigned int netbr[4];
229 	int nn, anslen;
230 	querybuf buf;
231 	char qbuf[MAXDNAME];
232 	unsigned long net2;
233 	struct netent *net_entry;
234 
235 	net = va_arg(ap, unsigned long);
236 	net_type = va_arg(ap, int);
237 
238 	*(struct netent **)rval = NULL;
239 
240 	if (net_type != AF_INET)
241 		return NS_UNAVAIL;
242 
243 	for (nn = 4, net2 = net; net2; net2 >>= 8)
244 		netbr[--nn] = net2 & 0xff;
245 	switch (nn) {
246 	case 3: 	/* Class A */
247 		sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
248 		break;
249 	case 2: 	/* Class B */
250 		sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
251 		break;
252 	case 1: 	/* Class C */
253 		sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
254 		    netbr[1]);
255 		break;
256 	case 0: 	/* Class D - E */
257 		sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
258 		    netbr[1], netbr[0]);
259 		break;
260 	}
261 	anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
262 	if (anslen < 0) {
263 #ifdef DEBUG
264 		if (_res.options & RES_DEBUG)
265 			printf("res_query failed\n");
266 #endif
267 		return NS_UNAVAIL;
268 	}
269 	net_entry = getnetanswer(&buf, anslen, BYADDR);
270 	if (net_entry) {
271 		unsigned u_net = net;	/* maybe net should be unsigned ? */
272 
273 		/* Strip trailing zeros */
274 		while ((u_net & 0xff) == 0 && u_net != 0)
275 			u_net >>= 8;
276 		net_entry->n_net = u_net;
277 		*(struct netent **)rval = net_entry;
278 		return NS_SUCCESS;
279 	}
280 	return NS_NOTFOUND;
281 }
282 
283 int
284 _dns_getnetbyname(void *rval, void *cb_data, va_list ap)
285 {
286 	const char *net;
287 	int anslen;
288 	querybuf buf;
289 	char qbuf[MAXDNAME];
290 
291 	net = va_arg(ap, const char *);
292 
293 	*(struct netent**)rval = NULL;
294 
295 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
296 		h_errno = NETDB_INTERNAL;
297 		return NS_UNAVAIL;
298 	}
299 	strncpy(qbuf, net, sizeof(qbuf) - 1);
300 	qbuf[sizeof(qbuf) - 1] = '\0';
301 	anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
302 	if (anslen < 0) {
303 #ifdef DEBUG
304 		if (_res.options & RES_DEBUG)
305 			printf("res_query failed\n");
306 #endif
307 		return NS_UNAVAIL;
308 	}
309 	*(struct netent**)rval = getnetanswer(&buf, anslen, BYNAME);
310 	return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
311 }
312 
313 void
314 _setnetdnsent(stayopen)
315 	int stayopen;
316 {
317 	if (stayopen)
318 		_res.options |= RES_STAYOPEN | RES_USEVC;
319 }
320 
321 void
322 _endnetdnsent()
323 {
324 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
325 	res_close();
326 }
327