xref: /freebsd/lib/libc/net/getnetbydns.c (revision 4faad310d32e8bc634c1ffd58dc8d47f96d19576)
11363f04cSPaul Traina /*-
21363f04cSPaul Traina  * Copyright (c) 1985, 1988, 1993
31363f04cSPaul Traina  *	The Regents of the University of California.  All rights reserved.
41363f04cSPaul Traina  *
51363f04cSPaul Traina  * Redistribution and use in source and binary forms, with or without
61363f04cSPaul Traina  * modification, are permitted provided that the following conditions
71363f04cSPaul Traina  * are met:
81363f04cSPaul Traina  * 1. Redistributions of source code must retain the above copyright
91363f04cSPaul Traina  *    notice, this list of conditions and the following disclaimer.
101363f04cSPaul Traina  * 2. Redistributions in binary form must reproduce the above copyright
111363f04cSPaul Traina  *    notice, this list of conditions and the following disclaimer in the
121363f04cSPaul Traina  *    documentation and/or other materials provided with the distribution.
131363f04cSPaul Traina  * 3. All advertising materials mentioning features or use of this software
141363f04cSPaul Traina  *    must display the following acknowledgement:
151363f04cSPaul Traina  *	This product includes software developed by the University of
161363f04cSPaul Traina  *	California, Berkeley and its contributors.
171363f04cSPaul Traina  * 4. Neither the name of the University nor the names of its contributors
181363f04cSPaul Traina  *    may be used to endorse or promote products derived from this software
191363f04cSPaul Traina  *    without specific prior written permission.
201363f04cSPaul Traina  *
211363f04cSPaul Traina  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221363f04cSPaul Traina  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231363f04cSPaul Traina  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241363f04cSPaul Traina  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251363f04cSPaul Traina  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261363f04cSPaul Traina  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271363f04cSPaul Traina  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281363f04cSPaul Traina  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291363f04cSPaul Traina  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301363f04cSPaul Traina  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311363f04cSPaul Traina  * SUCH DAMAGE.
321363f04cSPaul Traina  * -
331363f04cSPaul Traina  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
341363f04cSPaul Traina  *
351363f04cSPaul Traina  * Permission to use, copy, modify, and distribute this software for any
361363f04cSPaul Traina  * purpose with or without fee is hereby granted, provided that the above
371363f04cSPaul Traina  * copyright notice and this permission notice appear in all copies, and that
381363f04cSPaul Traina  * the name of Digital Equipment Corporation not be used in advertising or
391363f04cSPaul Traina  * publicity pertaining to distribution of the document or software without
401363f04cSPaul Traina  * specific, written prior permission.
411363f04cSPaul Traina  *
421363f04cSPaul Traina  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
431363f04cSPaul Traina  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
441363f04cSPaul Traina  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
451363f04cSPaul Traina  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
461363f04cSPaul Traina  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
471363f04cSPaul Traina  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
481363f04cSPaul Traina  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
491363f04cSPaul Traina  * SOFTWARE.
501363f04cSPaul Traina  * -
511363f04cSPaul Traina  * --Copyright--
521363f04cSPaul Traina  */
534faad310SPeter Wemm /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
544faad310SPeter Wemm  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
554faad310SPeter Wemm  *
564faad310SPeter Wemm  * Permission to use, copy, modify, and distribute this software for any
574faad310SPeter Wemm  * purpose with or without fee is hereby granted, provided that the above
584faad310SPeter Wemm  * copyright notice and this permission notice appear in all copies.
594faad310SPeter Wemm  */
601363f04cSPaul Traina 
611363f04cSPaul Traina #if defined(LIBC_SCCS) && !defined(lint)
621363f04cSPaul Traina static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
634faad310SPeter Wemm static char rcsid[] = "$Id: getnetbydns.c,v 1.4 1995/10/22 14:39:04 phk Exp $";
641363f04cSPaul Traina #endif /* LIBC_SCCS and not lint */
651363f04cSPaul Traina 
661363f04cSPaul Traina #include <sys/param.h>
671363f04cSPaul Traina #include <sys/socket.h>
681363f04cSPaul Traina #include <netinet/in.h>
691363f04cSPaul Traina #include <arpa/inet.h>
701363f04cSPaul Traina #include <arpa/nameser.h>
714faad310SPeter Wemm 
724faad310SPeter Wemm #include <stdio.h>
731363f04cSPaul Traina #include <netdb.h>
741363f04cSPaul Traina #include <resolv.h>
751363f04cSPaul Traina #include <ctype.h>
761363f04cSPaul Traina #include <errno.h>
771363f04cSPaul Traina #include <string.h>
784faad310SPeter Wemm #include <unistd.h>
791363f04cSPaul Traina #include <syslog.h>
801363f04cSPaul Traina 
814faad310SPeter Wemm #include "res_config.h"
821363f04cSPaul Traina 
834faad310SPeter Wemm extern int h_errno;
844faad310SPeter Wemm 
854faad310SPeter Wemm #define BYADDR 0
864faad310SPeter Wemm #define BYNAME 1
871363f04cSPaul Traina #define	MAXALIASES	35
881363f04cSPaul Traina 
891363f04cSPaul Traina #if PACKETSZ > 1024
901363f04cSPaul Traina #define	MAXPACKET	PACKETSZ
911363f04cSPaul Traina #else
921363f04cSPaul Traina #define	MAXPACKET	1024
931363f04cSPaul Traina #endif
941363f04cSPaul Traina 
951363f04cSPaul Traina typedef union {
961363f04cSPaul Traina 	HEADER	hdr;
971363f04cSPaul Traina 	u_char	buf[MAXPACKET];
981363f04cSPaul Traina } querybuf;
991363f04cSPaul Traina 
1001363f04cSPaul Traina typedef union {
1014faad310SPeter Wemm 	long	al;
1021363f04cSPaul Traina 	char	ac;
1031363f04cSPaul Traina } align;
1041363f04cSPaul Traina 
1051363f04cSPaul Traina static struct netent *
1061363f04cSPaul Traina getnetanswer(answer, anslen, net_i)
1071363f04cSPaul Traina 	querybuf *answer;
1081363f04cSPaul Traina 	int anslen;
1091363f04cSPaul Traina 	int net_i;
1101363f04cSPaul Traina {
1111363f04cSPaul Traina 
1121363f04cSPaul Traina 	register HEADER *hp;
1131363f04cSPaul Traina 	register u_char *cp;
1141363f04cSPaul Traina 	register int n;
1151363f04cSPaul Traina 	u_char *eom;
1164faad310SPeter Wemm 	int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
1174faad310SPeter Wemm 	char aux1[30], aux2[30], ans[30], *in, *st, *pauxt, *bp, **ap,
1184faad310SPeter Wemm 		*paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
1191363f04cSPaul Traina static	struct netent net_entry;
1204faad310SPeter Wemm static	char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1];
1211363f04cSPaul Traina 
1221363f04cSPaul Traina 	/*
1231363f04cSPaul Traina 	 * find first satisfactory answer
1241363f04cSPaul Traina 	 *
1251363f04cSPaul Traina 	 *      answer --> +------------+  ( MESSAGE )
1261363f04cSPaul Traina 	 *		   |   Header   |
1271363f04cSPaul Traina 	 *		   +------------+
1281363f04cSPaul Traina 	 *		   |  Question  | the question for the name server
1291363f04cSPaul Traina 	 *		   +------------+
1301363f04cSPaul Traina 	 *		   |   Answer   | RRs answering the question
1311363f04cSPaul Traina 	 *		   +------------+
1321363f04cSPaul Traina 	 *		   | Authority  | RRs pointing toward an authority
1331363f04cSPaul Traina 	 *		   | Additional | RRs holding additional information
1341363f04cSPaul Traina 	 *		   +------------+
1351363f04cSPaul Traina 	 */
1361363f04cSPaul Traina 	eom = answer->buf + anslen;
1371363f04cSPaul Traina 	hp = &answer->hdr;
1381363f04cSPaul Traina 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
1391363f04cSPaul Traina 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
1401363f04cSPaul Traina 	bp = netbuf;
1411363f04cSPaul Traina 	buflen = sizeof(netbuf);
1421363f04cSPaul Traina 	cp = answer->buf + HFIXEDSZ;
1431363f04cSPaul Traina 	if (!qdcount) {
1441363f04cSPaul Traina 		if (hp->aa)
1451363f04cSPaul Traina 			h_errno = HOST_NOT_FOUND;
1461363f04cSPaul Traina 		else
1471363f04cSPaul Traina 			h_errno = TRY_AGAIN;
1484faad310SPeter Wemm 		return (NULL);
1491363f04cSPaul Traina 	}
1504faad310SPeter Wemm 	while (qdcount-- > 0)
1511363f04cSPaul Traina 		cp += __dn_skipname(cp, eom) + QFIXEDSZ;
1521363f04cSPaul Traina 	ap = net_aliases;
1531363f04cSPaul Traina 	*ap = NULL;
1541363f04cSPaul Traina 	net_entry.n_aliases = net_aliases;
1551363f04cSPaul Traina 	haveanswer = 0;
1561363f04cSPaul Traina 	while (--ancount >= 0 && cp < eom) {
1571363f04cSPaul Traina 		n = dn_expand(answer->buf, eom, cp, bp, buflen);
1581363f04cSPaul Traina 		if (n < 0)
1591363f04cSPaul Traina 			break;
1601363f04cSPaul Traina 		cp += n;
1611363f04cSPaul Traina 		ans[0] = '\0';
1621363f04cSPaul Traina 		(void)strcpy(&ans[0], bp);
1631363f04cSPaul Traina 		GETSHORT(type, cp);
1641363f04cSPaul Traina 		GETSHORT(class, cp);
1651363f04cSPaul Traina 		cp += INT32SZ;		/* TTL */
1661363f04cSPaul Traina 		GETSHORT(n, cp);
1671363f04cSPaul Traina 		if (class == C_IN && type == T_PTR) {
1681363f04cSPaul Traina 			n = dn_expand(answer->buf, eom, cp, bp, buflen);
1691363f04cSPaul Traina 			if (n < 0) {
1701363f04cSPaul Traina 				cp += n;
1711363f04cSPaul Traina 				return (NULL);
1721363f04cSPaul Traina 			}
1731363f04cSPaul Traina 			cp += n;
1741363f04cSPaul Traina 			*ap++ = bp;
1754faad310SPeter Wemm 			bp += strlen(bp) + 1;
1764faad310SPeter Wemm 			net_entry.n_addrtype =
1774faad310SPeter Wemm 				(class == C_IN) ? AF_INET : AF_UNSPEC;
1781363f04cSPaul Traina 			haveanswer++;
1791363f04cSPaul Traina 		}
1801363f04cSPaul Traina 	}
1811363f04cSPaul Traina 	if (haveanswer) {
1821363f04cSPaul Traina 		*ap = NULL;
1831363f04cSPaul Traina 		switch (net_i) {
1841363f04cSPaul Traina 		case BYADDR:
1851363f04cSPaul Traina 			net_entry.n_name = *net_entry.n_aliases;
1861363f04cSPaul Traina 			net_entry.n_net = 0L;
1871363f04cSPaul Traina 			break;
1881363f04cSPaul Traina 		case BYNAME:
1891363f04cSPaul Traina 			in = *net_entry.n_aliases;
1901363f04cSPaul Traina 			net_entry.n_name = &ans[0];
1911363f04cSPaul Traina 			aux2[0] = '\0';
1921363f04cSPaul Traina 			for (i = 0; i < 4; i++) {
1931363f04cSPaul Traina 				for (st = in, nchar = 0;
1941363f04cSPaul Traina 				     *st != '.';
1951363f04cSPaul Traina 				     st++, nchar++)
1961363f04cSPaul Traina 					;
1971363f04cSPaul Traina 				if (nchar != 1 || *in != '0' || flag) {
1981363f04cSPaul Traina 					flag = 1;
1991363f04cSPaul Traina 					(void)strncpy(paux1,
2001363f04cSPaul Traina 						      (i==0) ? in : in-1,
2011363f04cSPaul Traina 						      (i==0) ?nchar : nchar+1);
2021363f04cSPaul Traina 					paux1[(i==0) ? nchar : nchar+1] = '\0';
2031363f04cSPaul Traina 					pauxt = paux2;
2041363f04cSPaul Traina 					paux2 = strcat(paux1, paux2);
2051363f04cSPaul Traina 					paux1 = pauxt;
2061363f04cSPaul Traina 				}
2071363f04cSPaul Traina 				in = ++st;
2081363f04cSPaul Traina 			}
2091363f04cSPaul Traina 			net_entry.n_net = inet_network(paux2);
2104faad310SPeter Wemm 			break;
2111363f04cSPaul Traina 		}
2121363f04cSPaul Traina 		net_entry.n_aliases++;
2131363f04cSPaul Traina 		return (&net_entry);
2141363f04cSPaul Traina 	}
2154faad310SPeter Wemm 	h_errno = TRY_AGAIN;
2164faad310SPeter Wemm 	return (NULL);
2171363f04cSPaul Traina }
2181363f04cSPaul Traina 
2191363f04cSPaul Traina struct netent *
2201363f04cSPaul Traina _getnetbydnsaddr(net, net_type)
2211363f04cSPaul Traina 	register long net;
2221363f04cSPaul Traina 	register int net_type;
2231363f04cSPaul Traina {
2241363f04cSPaul Traina 	unsigned int netbr[4];
2251363f04cSPaul Traina 	int nn, anslen;
2261363f04cSPaul Traina 	querybuf buf;
2271363f04cSPaul Traina 	char qbuf[MAXDNAME];
2281363f04cSPaul Traina 	unsigned long net2;
2291363f04cSPaul Traina 	struct netent *net_entry;
2301363f04cSPaul Traina 
2311363f04cSPaul Traina 	if (net_type != AF_INET)
2321363f04cSPaul Traina 		return (NULL);
2331363f04cSPaul Traina 
2344faad310SPeter Wemm 	for (nn = 4, net2 = net; net2; net2 >>= 8)
2351363f04cSPaul Traina 		netbr[--nn] = net2 & 0xff;
2361363f04cSPaul Traina 	switch (nn) {
2371363f04cSPaul Traina 	case 3: 	/* Class A */
2384faad310SPeter Wemm 		sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
2391363f04cSPaul Traina 		break;
2401363f04cSPaul Traina 	case 2: 	/* Class B */
2414faad310SPeter Wemm 		sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
2421363f04cSPaul Traina 		break;
2431363f04cSPaul Traina 	case 1: 	/* Class C */
2444faad310SPeter Wemm 		sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
2454faad310SPeter Wemm 		    netbr[1]);
2461363f04cSPaul Traina 		break;
2471363f04cSPaul Traina 	case 0: 	/* Class D - E */
2484faad310SPeter Wemm 		sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
2494faad310SPeter Wemm 		    netbr[1], netbr[0]);
2501363f04cSPaul Traina 		break;
2511363f04cSPaul Traina 	}
2524faad310SPeter Wemm 	anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
2531363f04cSPaul Traina 	if (anslen < 0) {
2544faad310SPeter Wemm #ifdef DEBUG
2551363f04cSPaul Traina 		if (_res.options & RES_DEBUG)
2561363f04cSPaul Traina 			printf("res_query failed\n");
2574faad310SPeter Wemm #endif
2581363f04cSPaul Traina 		return (NULL);
2591363f04cSPaul Traina 	}
2601363f04cSPaul Traina 	net_entry = getnetanswer(&buf, anslen, BYADDR);
2611363f04cSPaul Traina 	if (net_entry) {
2621363f04cSPaul Traina 		unsigned u_net = net;	/* maybe net should be unsigned ? */
2631363f04cSPaul Traina 
2641363f04cSPaul Traina 		/* Strip trailing zeros */
2654faad310SPeter Wemm 		while ((u_net & 0xff) == 0 && u_net != 0)
2661363f04cSPaul Traina 			u_net >>= 8;
2671363f04cSPaul Traina 		net_entry->n_net = u_net;
2681363f04cSPaul Traina 		return (net_entry);
2691363f04cSPaul Traina 	}
2704faad310SPeter Wemm 	return (NULL);
2714faad310SPeter Wemm }
2721363f04cSPaul Traina 
2731363f04cSPaul Traina struct netent *
2741363f04cSPaul Traina _getnetbydnsname(net)
2751363f04cSPaul Traina 	register const char *net;
2761363f04cSPaul Traina {
2771363f04cSPaul Traina 	int anslen;
2781363f04cSPaul Traina 	querybuf buf;
2791363f04cSPaul Traina 	char qbuf[MAXDNAME];
2801363f04cSPaul Traina 
2814faad310SPeter Wemm 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2824faad310SPeter Wemm 		h_errno = NETDB_INTERNAL;
2834faad310SPeter Wemm 		return (NULL);
2844faad310SPeter Wemm 	}
2854faad310SPeter Wemm 	strcpy(&qbuf[0], net);
2864faad310SPeter Wemm 	anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
2871363f04cSPaul Traina 	if (anslen < 0) {
2884faad310SPeter Wemm #ifdef DEBUG
2891363f04cSPaul Traina 		if (_res.options & RES_DEBUG)
2901363f04cSPaul Traina 			printf("res_query failed\n");
2914faad310SPeter Wemm #endif
2924faad310SPeter Wemm 		return (NULL);
2931363f04cSPaul Traina 	}
2941363f04cSPaul Traina 	return getnetanswer(&buf, anslen, BYNAME);
2951363f04cSPaul Traina }
2961363f04cSPaul Traina 
2971363f04cSPaul Traina void
2981363f04cSPaul Traina _setnetdnsent(stayopen)
2991363f04cSPaul Traina 	int stayopen;
3001363f04cSPaul Traina {
3011363f04cSPaul Traina 	if (stayopen)
3021363f04cSPaul Traina 		_res.options |= RES_STAYOPEN | RES_USEVC;
3031363f04cSPaul Traina }
3041363f04cSPaul Traina 
3051363f04cSPaul Traina void
3061363f04cSPaul Traina _endnetdnsent()
3071363f04cSPaul Traina {
3081363f04cSPaul Traina 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
3091363f04cSPaul Traina 	_res_close();
3101363f04cSPaul Traina }
311