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