xref: /netbsd-src/lib/libc/net/getnetnamadr.c (revision d06dd93f7d0495ff803e5179eb4a784cb7d6e86a)
1*d06dd93fSnia /*	$NetBSD: getnetnamadr.c,v 1.45 2020/06/05 11:16:15 nia Exp $	*/
2efba316dSchristos 
3efba316dSchristos /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
4efba316dSchristos  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
5efba316dSchristos  *
6efba316dSchristos  * Permission to use, copy, modify, and distribute this software for any
7efba316dSchristos  * purpose with or without fee is hereby granted, provided that the above
8efba316dSchristos  * copyright notice and this permission notice appear in all copies.
9efba316dSchristos  */
10efba316dSchristos /*
11efba316dSchristos  * Copyright (c) 1983, 1993
12efba316dSchristos  *	The Regents of the University of California.  All rights reserved.
13efba316dSchristos  *
14efba316dSchristos  * Redistribution and use in source and binary forms, with or without
15efba316dSchristos  * modification, are permitted provided that the following conditions
16efba316dSchristos  * are met:
17efba316dSchristos  * 1. Redistributions of source code must retain the above copyright
18efba316dSchristos  *    notice, this list of conditions and the following disclaimer.
19efba316dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
20efba316dSchristos  *    notice, this list of conditions and the following disclaimer in the
21efba316dSchristos  *    documentation and/or other materials provided with the distribution.
22efba316dSchristos  * 3. Neither the name of the University nor the names of its contributors
23efba316dSchristos  *    may be used to endorse or promote products derived from this software
24efba316dSchristos  *    without specific prior written permission.
25efba316dSchristos  *
26efba316dSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27efba316dSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28efba316dSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29efba316dSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30efba316dSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31efba316dSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32efba316dSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33efba316dSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34efba316dSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35efba316dSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36efba316dSchristos  * SUCH DAMAGE.
37efba316dSchristos  */
38efba316dSchristos 
39efba316dSchristos #include <sys/cdefs.h>
40efba316dSchristos #if defined(LIBC_SCCS) && !defined(lint)
41efba316dSchristos #if 0
42efba316dSchristos static char sccsid[] = "@(#)getnetbyaddr.c	8.1 (Berkeley) 6/4/93";
43efba316dSchristos static char sccsid_[] = "from getnetnamadr.c	1.4 (Coimbra) 93/06/03";
44efba316dSchristos static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
45efba316dSchristos #else
46*d06dd93fSnia __RCSID("$NetBSD: getnetnamadr.c,v 1.45 2020/06/05 11:16:15 nia Exp $");
47efba316dSchristos #endif
48efba316dSchristos #endif /* LIBC_SCCS and not lint */
49efba316dSchristos 
50efba316dSchristos #include "namespace.h"
51efba316dSchristos #include <sys/types.h>
52efba316dSchristos #include <sys/param.h>
53efba316dSchristos #include <sys/socket.h>
54efba316dSchristos #include <netinet/in.h>
55efba316dSchristos #include <arpa/inet.h>
56efba316dSchristos #include <arpa/nameser.h>
57efba316dSchristos 
58efba316dSchristos #include <assert.h>
59efba316dSchristos #include <ctype.h>
60efba316dSchristos #include <errno.h>
61efba316dSchristos #include <netdb.h>
62efba316dSchristos #include <nsswitch.h>
63efba316dSchristos #include <resolv.h>
64efba316dSchristos #include <stdarg.h>
65efba316dSchristos #include <stdio.h>
66efba316dSchristos #include <stdlib.h>
67efba316dSchristos #include <string.h>
68efba316dSchristos 
69efba316dSchristos #ifdef YP
70efba316dSchristos #include <rpc/rpc.h>
71efba316dSchristos #include <rpcsvc/yp_prot.h>
72efba316dSchristos #include <rpcsvc/ypclnt.h>
73efba316dSchristos #endif
74efba316dSchristos 
75efba316dSchristos #ifdef __weak_alias
76efba316dSchristos __weak_alias(getnetbyaddr,_getnetbyaddr)
77efba316dSchristos __weak_alias(getnetbyname,_getnetbyname)
78efba316dSchristos #endif
79efba316dSchristos 
80208f1289Schristos #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
81208f1289Schristos                                (ok)(nm) != 0)
82208f1289Schristos #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
83208f1289Schristos #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
84208f1289Schristos 
85208f1289Schristos 
86efba316dSchristos extern int _net_stayopen;
87efba316dSchristos 
88efba316dSchristos #define BYADDR 0
89efba316dSchristos #define BYNAME 1
90efba316dSchristos #define	MAXALIASES	35
91efba316dSchristos 
92efba316dSchristos #define	MAXPACKET	(64*1024)
93efba316dSchristos 
94efba316dSchristos typedef union {
95efba316dSchristos 	HEADER	hdr;
96efba316dSchristos 	u_char	buf[MAXPACKET];
97efba316dSchristos } querybuf;
98efba316dSchristos 
99efba316dSchristos typedef union {
100efba316dSchristos 	long	al;
101efba316dSchristos 	char	ac;
102efba316dSchristos } align;
103efba316dSchristos 
104efba316dSchristos #ifdef YP
105efba316dSchristos static char *__ypdomain;
106efba316dSchristos static char *__ypcurrent;
107efba316dSchristos static int   __ypcurrentlen;
108efba316dSchristos #endif
109efba316dSchristos 
110efba316dSchristos static	struct netent net_entry;
111efba316dSchristos static	char *net_aliases[MAXALIASES];
112efba316dSchristos 
1132713f767Slukem static int		parse_reversed_addr(const char *, in_addr_t *);
114208f1289Schristos static struct netent	*getnetanswer(res_state, querybuf *, int, int);
1152713f767Slukem static int		_files_getnetbyaddr(void *, void *, va_list);
1162713f767Slukem static int		_files_getnetbyname(void *, void *, va_list);
1172713f767Slukem static int		_dns_getnetbyaddr(void *, void *, va_list);
1182713f767Slukem static int		_dns_getnetbyname(void *, void *, va_list);
119efba316dSchristos #ifdef YP
1202713f767Slukem static int		_yp_getnetbyaddr(void *, void *, va_list);
1212713f767Slukem static int		_yp_getnetbyname(void *, void *, va_list);
1222713f767Slukem static struct netent	*_ypnetent(char *);
123efba316dSchristos #endif
124efba316dSchristos 
1256e5e2748Slukem /*
1266e5e2748Slukem  * parse_reversed_addr --
1276e5e2748Slukem  *	parse str, which should be of the form 'd.c.b.a.IN-ADDR.ARPA'
1286e5e2748Slukem  *	(a PTR as per RFC 1101) and convert into an in_addr_t of the
1296e5e2748Slukem  *	address 'a.b.c.d'.
1306e5e2748Slukem  *	returns 0 on success (storing in *result), or -1 on error.
1316e5e2748Slukem  */
1326e5e2748Slukem static int
parse_reversed_addr(const char * str,in_addr_t * result)1336e5e2748Slukem parse_reversed_addr(const char *str, in_addr_t *result)
1346e5e2748Slukem {
1356e5e2748Slukem 	unsigned long	octet[4];
1366e5e2748Slukem 	const char	*sp;
1376e5e2748Slukem 	char		*ep;
1386e5e2748Slukem 	int		octidx;
1396e5e2748Slukem 
1406e5e2748Slukem 	sp = str;
1416e5e2748Slukem 				/* find the four octets 'd.b.c.a.' */
1426e5e2748Slukem 	for (octidx = 0; octidx < 4; octidx++) {
1436e5e2748Slukem 					/* ensure it's a number */
1446e5e2748Slukem 		if (!isdigit((unsigned char)*sp))
1456e5e2748Slukem 			return -1;
1466e5e2748Slukem 		octet[octidx] = strtoul(sp, &ep, 10);
1476e5e2748Slukem 					/* with a trailing '.' */
1486e5e2748Slukem 		if (*ep != '.')
1496e5e2748Slukem 			return -1;
1506e5e2748Slukem 					/* and is 0 <= octet <= 255 */
1516e5e2748Slukem 		if (octet[octidx] > 255)
1526e5e2748Slukem 			return -1;
1536e5e2748Slukem 		sp = ep + 1;
1546e5e2748Slukem 	}
1556e5e2748Slukem 				/* ensure trailer is correct */
1566e5e2748Slukem 	if (strcasecmp(sp, "IN-ADDR.ARPA") != 0)
1576e5e2748Slukem 		return -1;
1586e5e2748Slukem 	*result = 0;
1596e5e2748Slukem 				/* build result from octets in reverse */
1606e5e2748Slukem 	for (octidx = 3; octidx >= 0; octidx--) {
1616e5e2748Slukem 		*result <<= 8;
162c5e820caSchristos 		*result |= (in_addr_t)(octet[octidx] & 0xff);
1636e5e2748Slukem 	}
1646e5e2748Slukem 	return 0;
1656e5e2748Slukem }
1666e5e2748Slukem 
167efba316dSchristos static struct netent *
getnetanswer(res_state res,querybuf * answer,int anslen,int net_i)168208f1289Schristos getnetanswer(res_state res, querybuf *answer, int anslen, int net_i)
169efba316dSchristos {
1702713f767Slukem 	static char	n_name[MAXDNAME];
1712713f767Slukem 	static char	netbuf[PACKETSZ];
1722713f767Slukem 
173efba316dSchristos 	HEADER		*hp;
174efba316dSchristos 	u_char		*cp;
175efba316dSchristos 	int		n;
176efba316dSchristos 	u_char		*eom;
1776e5e2748Slukem 	int		type, class, ancount, qdcount, haveanswer;
1786e5e2748Slukem 	char		*in, *bp, **ap, *ep;
179efba316dSchristos 
180efba316dSchristos 	_DIAGASSERT(answer != NULL);
181208f1289Schristos 	_DIAGASSERT(res != NULL);
182efba316dSchristos 
183efba316dSchristos 	/*
184efba316dSchristos 	 * find first satisfactory answer
185efba316dSchristos 	 *
186efba316dSchristos 	 *      answer --> +------------+  ( MESSAGE )
187efba316dSchristos 	 *		   |   Header   |
188efba316dSchristos 	 *		   +------------+
189efba316dSchristos 	 *		   |  Question  | the question for the name server
190efba316dSchristos 	 *		   +------------+
191efba316dSchristos 	 *		   |   Answer   | RRs answering the question
192efba316dSchristos 	 *		   +------------+
193efba316dSchristos 	 *		   | Authority  | RRs pointing toward an authority
194efba316dSchristos 	 *		   | Additional | RRs holding additional information
195efba316dSchristos 	 *		   +------------+
196efba316dSchristos 	 */
197efba316dSchristos 	eom = answer->buf + anslen;
198efba316dSchristos 	hp = &answer->hdr;
199efba316dSchristos 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
200efba316dSchristos 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
201efba316dSchristos 	bp = netbuf;
202efba316dSchristos 	ep = netbuf + sizeof(netbuf);
203efba316dSchristos 	cp = answer->buf + HFIXEDSZ;
204efba316dSchristos 	if (!qdcount) {
205efba316dSchristos 		if (hp->aa)
206efba316dSchristos 			h_errno = HOST_NOT_FOUND;
207efba316dSchristos 		else
208efba316dSchristos 			h_errno = TRY_AGAIN;
209efba316dSchristos 		return NULL;
210efba316dSchristos 	}
211efba316dSchristos 	while (qdcount-- > 0) {
212efba316dSchristos 		n = __dn_skipname(cp, eom);
213efba316dSchristos 		if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
214efba316dSchristos 			h_errno = NO_RECOVERY;
215efba316dSchristos 			return(NULL);
216efba316dSchristos 		}
217efba316dSchristos 		cp += n + QFIXEDSZ;
218efba316dSchristos 	}
219efba316dSchristos 	ap = net_aliases;
220efba316dSchristos 	*ap = NULL;
221efba316dSchristos 	net_entry.n_aliases = net_aliases;
222efba316dSchristos 	haveanswer = 0;
223d8aac843Slukem 	n_name[0] = '\0';
224efba316dSchristos 	while (--ancount >= 0 && cp < eom) {
225c5e820caSchristos 		n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
226208f1289Schristos 		if ((n < 0) || !maybe_dnok(res, bp))
227efba316dSchristos 			break;
228efba316dSchristos 		cp += n;
229d8aac843Slukem 		(void)strlcpy(n_name, bp, sizeof(n_name));
230efba316dSchristos 		GETSHORT(type, cp);
231efba316dSchristos 		GETSHORT(class, cp);
232efba316dSchristos 		cp += INT32SZ;		/* TTL */
233efba316dSchristos 		GETSHORT(n, cp);
234efba316dSchristos 		if (class == C_IN && type == T_PTR) {
235c5e820caSchristos 			n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
236208f1289Schristos 			if ((n < 0) || !maybe_hnok(res, bp)) {
237efba316dSchristos 				cp += n;
238efba316dSchristos 				return NULL;
239efba316dSchristos 			}
240efba316dSchristos 			cp += n;
241efba316dSchristos 			*ap++ = bp;
242efba316dSchristos 			bp += strlen(bp) + 1;
243efba316dSchristos 			net_entry.n_addrtype =
244efba316dSchristos 				(class == C_IN) ? AF_INET : AF_UNSPEC;
245efba316dSchristos 			haveanswer++;
246efba316dSchristos 		}
247efba316dSchristos 	}
248efba316dSchristos 	if (haveanswer) {
249efba316dSchristos 		*ap = NULL;
250efba316dSchristos 		switch (net_i) {
251efba316dSchristos 		case BYADDR:
252efba316dSchristos 			net_entry.n_name = *net_entry.n_aliases;
253efba316dSchristos 			net_entry.n_net = 0L;
254efba316dSchristos 			break;
255efba316dSchristos 		case BYNAME:
256efba316dSchristos 			ap = net_entry.n_aliases;
257efba316dSchristos 		next_alias:
258efba316dSchristos 			in = *ap++;
259efba316dSchristos 			if (in == NULL) {
260efba316dSchristos 				h_errno = HOST_NOT_FOUND;
261efba316dSchristos 				return NULL;
262efba316dSchristos 			}
263d8aac843Slukem 			net_entry.n_name = n_name;
2646e5e2748Slukem 			if (parse_reversed_addr(in, &net_entry.n_net) == -1)
265efba316dSchristos 				goto next_alias;
266efba316dSchristos 			break;
267efba316dSchristos 		}
268efba316dSchristos 		net_entry.n_aliases++;
269efba316dSchristos #if (defined(__sparc__) && defined(_LP64)) ||		\
270*d06dd93fSnia     defined(__alpha__)
271efba316dSchristos 		net_entry.__n_pad0 = 0;
272efba316dSchristos #endif
273efba316dSchristos 		return &net_entry;
274efba316dSchristos 	}
275efba316dSchristos 	h_errno = TRY_AGAIN;
276efba316dSchristos 	return NULL;
277efba316dSchristos }
278efba316dSchristos 
279efba316dSchristos /*ARGSUSED*/
2802713f767Slukem static int
_files_getnetbyaddr(void * cbrv,void * cbdata,va_list ap)2812713f767Slukem _files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
282efba316dSchristos {
2832713f767Slukem 	struct netent	**retval = va_arg(ap, struct netent **);
2842713f767Slukem 	uint32_t	  net	 = va_arg(ap, uint32_t);
2852713f767Slukem 	int		  type	 = va_arg(ap, int);
286efba316dSchristos 
2872713f767Slukem 	struct netent	 *np;
288efba316dSchristos 
289efba316dSchristos 	setnetent(_net_stayopen);
2902713f767Slukem 	while ((np = getnetent()) != NULL)
2912713f767Slukem 		if (np->n_addrtype == type && np->n_net == net)
292efba316dSchristos 			break;
293efba316dSchristos 	if (!_net_stayopen)
294efba316dSchristos 		endnetent();
2952713f767Slukem 
2962713f767Slukem 	if (np != NULL) {
2972713f767Slukem 		*retval = np;
2982713f767Slukem 		return NS_SUCCESS;
2992713f767Slukem 	} else {
300efba316dSchristos 		h_errno = HOST_NOT_FOUND;
301efba316dSchristos 		return NS_NOTFOUND;
302efba316dSchristos 	}
303efba316dSchristos }
304efba316dSchristos 
305efba316dSchristos /*ARGSUSED*/
3062713f767Slukem static int
_dns_getnetbyaddr(void * cbrv,void * cbdata,va_list ap)3072713f767Slukem _dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
308efba316dSchristos {
3092713f767Slukem 	struct netent	**retval = va_arg(ap, struct netent **);
3102713f767Slukem 	uint32_t	  net	 = va_arg(ap, uint32_t);
3112713f767Slukem 	int		  type	 = va_arg(ap, int);
3122713f767Slukem 
313efba316dSchristos 	unsigned int	 netbr[4];
314efba316dSchristos 	int		 nn, anslen;
315efba316dSchristos 	querybuf	*buf;
316efba316dSchristos 	char		 qbuf[MAXDNAME];
317efba316dSchristos 	uint32_t	 net2;
318efba316dSchristos 	struct netent	*np;
319efba316dSchristos 	res_state	 res;
320efba316dSchristos 
321efba316dSchristos 	if (type != AF_INET)
322efba316dSchristos 		return NS_UNAVAIL;
323efba316dSchristos 
324efba316dSchristos 	for (nn = 4, net2 = net; net2; net2 >>= 8)
325efba316dSchristos 		netbr[--nn] = (unsigned int)(net2 & 0xff);
326efba316dSchristos 	switch (nn) {
327efba316dSchristos 	default:
328efba316dSchristos 		return NS_UNAVAIL;
329efba316dSchristos 	case 3: 	/* Class A */
330efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
331efba316dSchristos 		break;
332efba316dSchristos 	case 2: 	/* Class B */
333efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
334efba316dSchristos 		    netbr[3], netbr[2]);
335efba316dSchristos 		break;
336efba316dSchristos 	case 1: 	/* Class C */
337efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
338efba316dSchristos 		    netbr[3], netbr[2], netbr[1]);
339efba316dSchristos 		break;
340efba316dSchristos 	case 0: 	/* Class D - E */
341efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
342efba316dSchristos 		    netbr[3], netbr[2], netbr[1], netbr[0]);
343efba316dSchristos 		break;
344efba316dSchristos 	}
345efba316dSchristos 	buf = malloc(sizeof(*buf));
346efba316dSchristos 	if (buf == NULL) {
347efba316dSchristos 		h_errno = NETDB_INTERNAL;
348efba316dSchristos 		return NS_NOTFOUND;
349efba316dSchristos 	}
350efba316dSchristos 	res = __res_get_state();
351edf03256Slukem 	if (res == NULL) {
352edf03256Slukem 		free(buf);
353efba316dSchristos 		return NS_NOTFOUND;
354edf03256Slukem 	}
355c5e820caSchristos 	anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf,
356c5e820caSchristos 	    (int)sizeof(buf->buf));
357efba316dSchristos 	if (anslen < 0) {
358efba316dSchristos 		free(buf);
359efba316dSchristos #ifdef DEBUG
360efba316dSchristos 		if (res->options & RES_DEBUG)
361efba316dSchristos 			printf("res_query failed\n");
362efba316dSchristos #endif
363efba316dSchristos 		__res_put_state(res);
364efba316dSchristos 		return NS_NOTFOUND;
365efba316dSchristos 	}
366208f1289Schristos 	np = getnetanswer(res, buf, anslen, BYADDR);
3674dddbc1fSchristos 	__res_put_state(res);
368efba316dSchristos 	free(buf);
369efba316dSchristos 	if (np) {
370efba316dSchristos 		/* maybe net should be unsigned? */
371efba316dSchristos 		uint32_t u_net = net;
372efba316dSchristos 
373efba316dSchristos 		/* Strip trailing zeros */
374efba316dSchristos 		while ((u_net & 0xff) == 0 && u_net != 0)
375efba316dSchristos 			u_net >>= 8;
376efba316dSchristos 		np->n_net = u_net;
377efba316dSchristos 	}
3782713f767Slukem 
3792713f767Slukem 	if (np != NULL) {
3802713f767Slukem 		*retval = np;
3812713f767Slukem 		return NS_SUCCESS;
3822713f767Slukem 	} else {
383efba316dSchristos 		h_errno = HOST_NOT_FOUND;
384efba316dSchristos 		return NS_NOTFOUND;
385efba316dSchristos 	}
386efba316dSchristos }
387efba316dSchristos 
388efba316dSchristos struct netent *
getnetbyaddr(uint32_t net,int net_type)389efba316dSchristos getnetbyaddr(uint32_t net, int net_type)
390efba316dSchristos {
3912713f767Slukem 	int		 rv;
3922713f767Slukem 	struct netent	*retval;
3932713f767Slukem 
394efba316dSchristos 	static const ns_dtab dtab[] = {
395efba316dSchristos 		NS_FILES_CB(_files_getnetbyaddr, NULL)
396efba316dSchristos 		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },	/* force -DHESIOD */
397efba316dSchristos 		NS_NIS_CB(_yp_getnetbyaddr, NULL)
398efba316dSchristos 		NS_NULL_CB
399efba316dSchristos 	};
400efba316dSchristos 
4012713f767Slukem 	retval = NULL;
402efba316dSchristos 	h_errno = NETDB_INTERNAL;
4032713f767Slukem 	rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr",
4042713f767Slukem 	    __nsdefaultsrc, &retval, net, net_type);
4052713f767Slukem 	if (rv == NS_SUCCESS) {
406efba316dSchristos 		h_errno = NETDB_SUCCESS;
4072713f767Slukem 		return retval;
4082713f767Slukem 	}
4092713f767Slukem 	return NULL;
410efba316dSchristos }
411efba316dSchristos 
412efba316dSchristos /*ARGSUSED*/
4132713f767Slukem static int
_files_getnetbyname(void * cbrv,void * cbdata,va_list ap)4142713f767Slukem _files_getnetbyname(void *cbrv, void *cbdata, va_list ap)
415efba316dSchristos {
4162713f767Slukem 	struct netent	**retval = va_arg(ap, struct netent **);
4172713f767Slukem 	const char	 *name	 = va_arg(ap, const char *);
4182713f767Slukem 
4192713f767Slukem 	struct netent	 *np;
420efba316dSchristos 	char		**cp;
421efba316dSchristos 
422efba316dSchristos 	setnetent(_net_stayopen);
4232713f767Slukem 	while ((np = getnetent()) != NULL) {
4242713f767Slukem 		if (strcasecmp(np->n_name, name) == 0)
425efba316dSchristos 			break;
4262713f767Slukem 		for (cp = np->n_aliases; *cp != 0; cp++)
427efba316dSchristos 			if (strcasecmp(*cp, name) == 0)
428efba316dSchristos 				goto found;
429efba316dSchristos 	}
430efba316dSchristos found:
431efba316dSchristos 	if (!_net_stayopen)
432efba316dSchristos 		endnetent();
4332713f767Slukem 
4342713f767Slukem 	if (np != NULL) {
4352713f767Slukem 		*retval = np;
4362713f767Slukem 		return NS_SUCCESS;
4372713f767Slukem 	} else {
438efba316dSchristos 		h_errno = HOST_NOT_FOUND;
439efba316dSchristos 		return NS_NOTFOUND;
440efba316dSchristos 	}
441efba316dSchristos }
442efba316dSchristos 
443efba316dSchristos /*ARGSUSED*/
4442713f767Slukem static int
_dns_getnetbyname(void * cbrv,void * cbdata,va_list ap)4452713f767Slukem _dns_getnetbyname(void *cbrv, void *cbdata, va_list ap)
446efba316dSchristos {
4472713f767Slukem 	struct netent	**retval = va_arg(ap, struct netent **);
4482713f767Slukem 	const char	 *name	 = va_arg(ap, const char *);
4492713f767Slukem 
450efba316dSchristos 	int		 anslen;
451efba316dSchristos 	querybuf	*buf;
452efba316dSchristos 	char		 qbuf[MAXDNAME];
453efba316dSchristos 	struct netent	*np;
454efba316dSchristos 	res_state	 res;
455efba316dSchristos 
4562713f767Slukem 	strlcpy(&qbuf[0], name, sizeof(qbuf));
457efba316dSchristos 	buf = malloc(sizeof(*buf));
458efba316dSchristos 	if (buf == NULL) {
459efba316dSchristos 		h_errno = NETDB_INTERNAL;
460efba316dSchristos 		return NS_NOTFOUND;
461efba316dSchristos 	}
462efba316dSchristos 	res = __res_get_state();
463efba316dSchristos 	if (res == NULL) {
464efba316dSchristos 		free(buf);
465efba316dSchristos 		return NS_NOTFOUND;
466efba316dSchristos 	}
467efba316dSchristos 	anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf,
468c5e820caSchristos 	    (int)sizeof(buf->buf));
469efba316dSchristos 	if (anslen < 0) {
470efba316dSchristos 		free(buf);
471efba316dSchristos #ifdef DEBUG
472efba316dSchristos 		if (res->options & RES_DEBUG)
473efba316dSchristos 			printf("res_search failed\n");
474efba316dSchristos #endif
475efba316dSchristos 		__res_put_state(res);
476efba316dSchristos 		return NS_NOTFOUND;
477efba316dSchristos 	}
478208f1289Schristos 	np = getnetanswer(res, buf, anslen, BYNAME);
4794dddbc1fSchristos 	__res_put_state(res);
480efba316dSchristos 	free(buf);
4812713f767Slukem 
4822713f767Slukem 	if (np != NULL) {
4832713f767Slukem 		*retval = np;
4842713f767Slukem 		return NS_SUCCESS;
4852713f767Slukem 	} else {
486efba316dSchristos 		h_errno = HOST_NOT_FOUND;
487efba316dSchristos 		return NS_NOTFOUND;
488efba316dSchristos 	}
489efba316dSchristos }
490efba316dSchristos 
491efba316dSchristos struct netent *
getnetbyname(const char * name)4922713f767Slukem getnetbyname(const char *name)
493efba316dSchristos {
4942713f767Slukem 	int		 rv;
4952713f767Slukem 	struct netent	*retval;
4962713f767Slukem 
497efba316dSchristos 	static const ns_dtab dtab[] = {
498efba316dSchristos 		NS_FILES_CB(_files_getnetbyname, NULL)
499efba316dSchristos 		{ NSSRC_DNS, _dns_getnetbyname, NULL },	/* force -DHESIOD */
500efba316dSchristos 		NS_NIS_CB(_yp_getnetbyname, NULL)
501efba316dSchristos 		NS_NULL_CB
502efba316dSchristos 	};
503efba316dSchristos 
5042713f767Slukem 	_DIAGASSERT(name != NULL);
505efba316dSchristos 
5062713f767Slukem 	retval = NULL;
507efba316dSchristos 	h_errno = NETDB_INTERNAL;
5082713f767Slukem 	rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname",
5092713f767Slukem 	    __nsdefaultsrc, &retval, name);
5102713f767Slukem 	if (rv == NS_SUCCESS) {
511efba316dSchristos 		h_errno = NETDB_SUCCESS;
5122713f767Slukem 		return retval;
5132713f767Slukem 	}
5142713f767Slukem 	return NULL;
515efba316dSchristos }
516efba316dSchristos 
517efba316dSchristos #ifdef YP
518efba316dSchristos /*ARGSUSED*/
5192713f767Slukem static int
_yp_getnetbyaddr(void * cbrv,void * cb_data,va_list ap)5202713f767Slukem _yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap)
521efba316dSchristos {
5222713f767Slukem 	struct netent	**retval = va_arg(ap, struct netent **);
5232713f767Slukem 	uint32_t	  net	 = va_arg(ap, uint32_t);
5242713f767Slukem 	int		  type	 = va_arg(ap, int);
5252713f767Slukem 
526efba316dSchristos 	struct netent	*np;
527efba316dSchristos 	char		 qbuf[MAXDNAME];
528efba316dSchristos 	unsigned int	 netbr[4];
5292713f767Slukem 	uint32_t	 net2;
5302713f767Slukem 	int		 r;
531efba316dSchristos 
532efba316dSchristos 	if (type != AF_INET)
533efba316dSchristos 		return NS_UNAVAIL;
534efba316dSchristos 
535efba316dSchristos 	if (!__ypdomain) {
536efba316dSchristos 		if (_yp_check(&__ypdomain) == 0)
537efba316dSchristos 			return NS_UNAVAIL;
538efba316dSchristos 	}
539efba316dSchristos 	np = NULL;
540efba316dSchristos 	if (__ypcurrent)
541efba316dSchristos 		free(__ypcurrent);
542efba316dSchristos 	__ypcurrent = NULL;
543efba316dSchristos 	for (r = 4, net2 = net; net2; net2 >>= 8)
544efba316dSchristos 		netbr[--r] = (unsigned int)(net2 & 0xff);
545efba316dSchristos 	switch (r) {
546efba316dSchristos 	default:
547efba316dSchristos 		return NS_UNAVAIL;
548efba316dSchristos 	case 3: 	/* Class A */
549efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
550efba316dSchristos 		break;
551efba316dSchristos 	case 2: 	/* Class B */
552efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
553efba316dSchristos 		break;
554efba316dSchristos 	case 1: 	/* Class C */
555efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
556efba316dSchristos 		    netbr[3]);
557efba316dSchristos 		break;
558efba316dSchristos 	case 0: 	/* Class D - E */
559efba316dSchristos 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
560efba316dSchristos 		    netbr[2], netbr[3]);
561efba316dSchristos 		break;
562efba316dSchristos 	}
563efba316dSchristos 	r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
564efba316dSchristos 	    &__ypcurrent, &__ypcurrentlen);
565efba316dSchristos 	if (r == 0)
566efba316dSchristos 		np = _ypnetent(__ypcurrent);
567efba316dSchristos 
5682713f767Slukem 	if (np != NULL) {
5692713f767Slukem 		*retval = np;
5702713f767Slukem 		return NS_SUCCESS;
5712713f767Slukem 	} else {
572efba316dSchristos 		h_errno = HOST_NOT_FOUND;
573efba316dSchristos 		return NS_NOTFOUND;
574efba316dSchristos 	}
575efba316dSchristos }
576efba316dSchristos 
577efba316dSchristos /*ARGSUSED*/
5782713f767Slukem static int
_yp_getnetbyname(void * cbrv,void * cbdata,va_list ap)5792713f767Slukem _yp_getnetbyname(void *cbrv, void *cbdata, va_list ap)
580efba316dSchristos {
5812713f767Slukem 	struct netent	**retval = va_arg(ap, struct netent **);
5822713f767Slukem 	const char	 *name	 = va_arg(ap, const char *);
5832713f767Slukem 
584efba316dSchristos 	struct netent	*np;
585efba316dSchristos 	int		 r;
586efba316dSchristos 
587efba316dSchristos 	if (!__ypdomain) {
588efba316dSchristos 		if (_yp_check(&__ypdomain) == 0)
589efba316dSchristos 			return NS_UNAVAIL;
590efba316dSchristos 	}
591efba316dSchristos 	np = NULL;
592efba316dSchristos 	if (__ypcurrent)
593efba316dSchristos 		free(__ypcurrent);
594efba316dSchristos 	__ypcurrent = NULL;
595efba316dSchristos 	r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
596efba316dSchristos 	    &__ypcurrent, &__ypcurrentlen);
597efba316dSchristos 	if (r == 0)
598efba316dSchristos 		np = _ypnetent(__ypcurrent);
599efba316dSchristos 
6002713f767Slukem 	if (np != NULL) {
6012713f767Slukem 		*retval = np;
6022713f767Slukem 		return NS_SUCCESS;
6032713f767Slukem 	} else {
604efba316dSchristos 		h_errno = HOST_NOT_FOUND;
605efba316dSchristos 		return NS_NOTFOUND;
606efba316dSchristos 	}
607efba316dSchristos }
608efba316dSchristos 
6092713f767Slukem static struct netent *
_ypnetent(char * line)610efba316dSchristos _ypnetent(char *line)
611efba316dSchristos {
612efba316dSchristos 	char *cp, *p, **q;
613efba316dSchristos 
614efba316dSchristos 	_DIAGASSERT(line != NULL);
615efba316dSchristos 
616efba316dSchristos 	net_entry.n_name = line;
617efba316dSchristos 	cp = strpbrk(line, " \t");
618efba316dSchristos 	if (cp == NULL)
619efba316dSchristos 		return NULL;
620efba316dSchristos 	*cp++ = '\0';
621efba316dSchristos 	while (*cp == ' ' || *cp == '\t')
622efba316dSchristos 		cp++;
623efba316dSchristos 	p = strpbrk(cp, " \t");
624efba316dSchristos 	if (p != NULL)
625efba316dSchristos 		*p++ = '\0';
626efba316dSchristos 	net_entry.n_net = inet_network(cp);
627efba316dSchristos #if (defined(__sparc__) && defined(_LP64)) ||		\
628*d06dd93fSnia     defined(__alpha__)
629efba316dSchristos 	net_entry.__n_pad0 = 0;
630efba316dSchristos #endif
631efba316dSchristos 	net_entry.n_addrtype = AF_INET;
632efba316dSchristos 	q = net_entry.n_aliases = net_aliases;
633efba316dSchristos 	if (p != NULL)  {
634efba316dSchristos 		cp = p;
635efba316dSchristos 		while (cp && *cp) {
636efba316dSchristos 			if (*cp == ' ' || *cp == '\t') {
637efba316dSchristos 				cp++;
638efba316dSchristos 				continue;
639efba316dSchristos 			}
640efba316dSchristos 			if (q < &net_aliases[MAXALIASES - 1])
641efba316dSchristos 				*q++ = cp;
642efba316dSchristos 			cp = strpbrk(cp, " \t");
643efba316dSchristos 			if (cp != NULL)
644efba316dSchristos 				*cp++ = '\0';
645efba316dSchristos 		}
646efba316dSchristos 	}
647efba316dSchristos 	*q = NULL;
648efba316dSchristos 
649efba316dSchristos 	return &net_entry;
650efba316dSchristos }
651efba316dSchristos #endif
652