Home | History | Annotate | Line # | Download | only in net
getnetnamadr.c revision 1.37.12.1
      1  1.37.12.1  wrstuden /*	$NetBSD: getnetnamadr.c,v 1.37.12.1 2008/06/23 04:29:32 wrstuden Exp $	*/
      2        1.2       mrg 
      3        1.1       mrg /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
      4        1.1       mrg  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
      5        1.1       mrg  *
      6        1.1       mrg  * Permission to use, copy, modify, and distribute this software for any
      7        1.1       mrg  * purpose with or without fee is hereby granted, provided that the above
      8        1.1       mrg  * copyright notice and this permission notice appear in all copies.
      9        1.1       mrg  */
     10        1.1       mrg /*
     11        1.1       mrg  * Copyright (c) 1983, 1993
     12        1.1       mrg  *	The Regents of the University of California.  All rights reserved.
     13        1.1       mrg  *
     14        1.1       mrg  * Redistribution and use in source and binary forms, with or without
     15        1.1       mrg  * modification, are permitted provided that the following conditions
     16        1.1       mrg  * are met:
     17        1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     18        1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     19        1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     20        1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     21        1.1       mrg  *    documentation and/or other materials provided with the distribution.
     22       1.27       agc  * 3. Neither the name of the University nor the names of its contributors
     23        1.1       mrg  *    may be used to endorse or promote products derived from this software
     24        1.1       mrg  *    without specific prior written permission.
     25        1.1       mrg  *
     26        1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27        1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28        1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29        1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30        1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31        1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32        1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33        1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34        1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35        1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36        1.1       mrg  * SUCH DAMAGE.
     37        1.1       mrg  */
     38        1.1       mrg 
     39        1.2       mrg #include <sys/cdefs.h>
     40        1.1       mrg #if defined(LIBC_SCCS) && !defined(lint)
     41        1.2       mrg #if 0
     42        1.1       mrg static char sccsid[] = "@(#)getnetbyaddr.c	8.1 (Berkeley) 6/4/93";
     43        1.1       mrg static char sccsid_[] = "from getnetnamadr.c	1.4 (Coimbra) 93/06/03";
     44        1.5     perry static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
     45        1.2       mrg #else
     46  1.37.12.1  wrstuden __RCSID("$NetBSD: getnetnamadr.c,v 1.37.12.1 2008/06/23 04:29:32 wrstuden Exp $");
     47        1.2       mrg #endif
     48        1.1       mrg #endif /* LIBC_SCCS and not lint */
     49        1.1       mrg 
     50        1.3   thorpej #include "namespace.h"
     51        1.1       mrg #include <sys/types.h>
     52        1.1       mrg #include <sys/param.h>
     53        1.1       mrg #include <sys/socket.h>
     54        1.1       mrg #include <netinet/in.h>
     55        1.1       mrg #include <arpa/inet.h>
     56        1.1       mrg #include <arpa/nameser.h>
     57        1.1       mrg 
     58       1.17     lukem #include <assert.h>
     59        1.8     lukem #include <ctype.h>
     60        1.8     lukem #include <errno.h>
     61        1.1       mrg #include <netdb.h>
     62        1.8     lukem #include <nsswitch.h>
     63        1.1       mrg #include <resolv.h>
     64       1.21       wiz #include <stdarg.h>
     65        1.8     lukem #include <stdio.h>
     66       1.10     lukem #include <stdlib.h>
     67        1.1       mrg #include <string.h>
     68        1.1       mrg 
     69       1.10     lukem #ifdef YP
     70       1.10     lukem #include <rpc/rpc.h>
     71       1.10     lukem #include <rpcsvc/yp_prot.h>
     72       1.10     lukem #include <rpcsvc/ypclnt.h>
     73       1.10     lukem #endif
     74       1.10     lukem 
     75        1.3   thorpej #ifdef __weak_alias
     76       1.19   mycroft __weak_alias(getnetbyaddr,_getnetbyaddr)
     77       1.19   mycroft __weak_alias(getnetbyname,_getnetbyname)
     78        1.3   thorpej #endif
     79        1.3   thorpej 
     80        1.8     lukem extern int _net_stayopen;
     81        1.1       mrg 
     82        1.1       mrg #define BYADDR 0
     83        1.1       mrg #define BYNAME 1
     84        1.1       mrg #define	MAXALIASES	35
     85        1.1       mrg 
     86       1.24    itojun #define	MAXPACKET	(64*1024)
     87        1.1       mrg 
     88        1.1       mrg typedef union {
     89        1.1       mrg 	HEADER	hdr;
     90        1.1       mrg 	u_char	buf[MAXPACKET];
     91        1.1       mrg } querybuf;
     92        1.1       mrg 
     93        1.1       mrg typedef union {
     94        1.1       mrg 	long	al;
     95        1.1       mrg 	char	ac;
     96        1.1       mrg } align;
     97        1.1       mrg 
     98       1.10     lukem #ifdef YP
     99       1.10     lukem static char *__ypdomain;
    100       1.10     lukem static char *__ypcurrent;
    101       1.10     lukem static int   __ypcurrentlen;
    102       1.10     lukem #endif
    103       1.10     lukem 
    104       1.10     lukem static	struct netent net_entry;
    105       1.10     lukem static	char *net_aliases[MAXALIASES];
    106       1.10     lukem 
    107  1.37.12.1  wrstuden static int		parse_reversed_addr(const char *, in_addr_t *);
    108  1.37.12.1  wrstuden static struct netent	*getnetanswer(querybuf *, int, int);
    109  1.37.12.1  wrstuden static int		_files_getnetbyaddr(void *, void *, va_list);
    110  1.37.12.1  wrstuden static int		_files_getnetbyname(void *, void *, va_list);
    111  1.37.12.1  wrstuden static int		_dns_getnetbyaddr(void *, void *, va_list);
    112  1.37.12.1  wrstuden static int		_dns_getnetbyname(void *, void *, va_list);
    113       1.10     lukem #ifdef YP
    114  1.37.12.1  wrstuden static int		_yp_getnetbyaddr(void *, void *, va_list);
    115  1.37.12.1  wrstuden static int		_yp_getnetbyname(void *, void *, va_list);
    116  1.37.12.1  wrstuden static struct netent	*_ypnetent(char *);
    117       1.10     lukem #endif
    118       1.10     lukem 
    119  1.37.12.1  wrstuden /*
    120  1.37.12.1  wrstuden  * parse_reversed_addr --
    121  1.37.12.1  wrstuden  *	parse str, which should be of the form 'd.c.b.a.IN-ADDR.ARPA'
    122  1.37.12.1  wrstuden  *	(a PTR as per RFC 1101) and convert into an in_addr_t of the
    123  1.37.12.1  wrstuden  *	address 'a.b.c.d'.
    124  1.37.12.1  wrstuden  *	returns 0 on success (storing in *result), or -1 on error.
    125  1.37.12.1  wrstuden  */
    126  1.37.12.1  wrstuden static int
    127  1.37.12.1  wrstuden parse_reversed_addr(const char *str, in_addr_t *result)
    128  1.37.12.1  wrstuden {
    129  1.37.12.1  wrstuden 	unsigned long	octet[4];
    130  1.37.12.1  wrstuden 	const char	*sp;
    131  1.37.12.1  wrstuden 	char		*ep;
    132  1.37.12.1  wrstuden 	int		octidx;
    133  1.37.12.1  wrstuden 
    134  1.37.12.1  wrstuden 	sp = str;
    135  1.37.12.1  wrstuden 				/* find the four octets 'd.b.c.a.' */
    136  1.37.12.1  wrstuden 	for (octidx = 0; octidx < 4; octidx++) {
    137  1.37.12.1  wrstuden 					/* ensure it's a number */
    138  1.37.12.1  wrstuden 		if (!isdigit((unsigned char)*sp))
    139  1.37.12.1  wrstuden 			return -1;
    140  1.37.12.1  wrstuden 		octet[octidx] = strtoul(sp, &ep, 10);
    141  1.37.12.1  wrstuden 					/* with a trailing '.' */
    142  1.37.12.1  wrstuden 		if (*ep != '.')
    143  1.37.12.1  wrstuden 			return -1;
    144  1.37.12.1  wrstuden 					/* and is 0 <= octet <= 255 */
    145  1.37.12.1  wrstuden 		if (octet[octidx] > 255)
    146  1.37.12.1  wrstuden 			return -1;
    147  1.37.12.1  wrstuden 		sp = ep + 1;
    148  1.37.12.1  wrstuden 	}
    149  1.37.12.1  wrstuden 				/* ensure trailer is correct */
    150  1.37.12.1  wrstuden 	if (strcasecmp(sp, "IN-ADDR.ARPA") != 0)
    151  1.37.12.1  wrstuden 		return -1;
    152  1.37.12.1  wrstuden 	*result = 0;
    153  1.37.12.1  wrstuden 				/* build result from octets in reverse */
    154  1.37.12.1  wrstuden 	for (octidx = 3; octidx >= 0; octidx--) {
    155  1.37.12.1  wrstuden 		*result <<= 8;
    156  1.37.12.1  wrstuden 		*result |= (octet[octidx] & 0xff);
    157  1.37.12.1  wrstuden 	}
    158  1.37.12.1  wrstuden 	return 0;
    159  1.37.12.1  wrstuden }
    160  1.37.12.1  wrstuden 
    161        1.1       mrg static struct netent *
    162       1.30  christos getnetanswer(querybuf *answer, int anslen, int net_i)
    163        1.1       mrg {
    164  1.37.12.1  wrstuden 	static char	n_name[MAXDNAME];
    165  1.37.12.1  wrstuden 	static char	netbuf[PACKETSZ];
    166  1.37.12.1  wrstuden 
    167  1.37.12.1  wrstuden 	HEADER		*hp;
    168  1.37.12.1  wrstuden 	u_char		*cp;
    169  1.37.12.1  wrstuden 	int		n;
    170  1.37.12.1  wrstuden 	u_char		*eom;
    171  1.37.12.1  wrstuden 	int		type, class, ancount, qdcount, haveanswer;
    172  1.37.12.1  wrstuden 	char		*in, *bp, **ap, *ep;
    173        1.1       mrg 
    174       1.17     lukem 	_DIAGASSERT(answer != NULL);
    175       1.17     lukem 
    176        1.1       mrg 	/*
    177        1.1       mrg 	 * find first satisfactory answer
    178        1.1       mrg 	 *
    179        1.1       mrg 	 *      answer --> +------------+  ( MESSAGE )
    180        1.1       mrg 	 *		   |   Header   |
    181        1.1       mrg 	 *		   +------------+
    182        1.1       mrg 	 *		   |  Question  | the question for the name server
    183        1.1       mrg 	 *		   +------------+
    184        1.1       mrg 	 *		   |   Answer   | RRs answering the question
    185        1.1       mrg 	 *		   +------------+
    186        1.1       mrg 	 *		   | Authority  | RRs pointing toward an authority
    187        1.1       mrg 	 *		   | Additional | RRs holding additional information
    188        1.1       mrg 	 *		   +------------+
    189        1.1       mrg 	 */
    190        1.1       mrg 	eom = answer->buf + anslen;
    191        1.1       mrg 	hp = &answer->hdr;
    192        1.1       mrg 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
    193        1.1       mrg 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
    194        1.1       mrg 	bp = netbuf;
    195       1.22    itojun 	ep = netbuf + sizeof(netbuf);
    196        1.1       mrg 	cp = answer->buf + HFIXEDSZ;
    197        1.1       mrg 	if (!qdcount) {
    198        1.1       mrg 		if (hp->aa)
    199        1.1       mrg 			h_errno = HOST_NOT_FOUND;
    200        1.1       mrg 		else
    201        1.1       mrg 			h_errno = TRY_AGAIN;
    202       1.30  christos 		return NULL;
    203        1.1       mrg 	}
    204       1.26    itojun 	while (qdcount-- > 0) {
    205       1.26    itojun 		n = __dn_skipname(cp, eom);
    206       1.26    itojun 		if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
    207       1.26    itojun 			h_errno = NO_RECOVERY;
    208       1.26    itojun 			return(NULL);
    209       1.26    itojun 		}
    210       1.26    itojun 		cp += n + QFIXEDSZ;
    211       1.26    itojun 	}
    212        1.1       mrg 	ap = net_aliases;
    213        1.1       mrg 	*ap = NULL;
    214        1.1       mrg 	net_entry.n_aliases = net_aliases;
    215        1.1       mrg 	haveanswer = 0;
    216  1.37.12.1  wrstuden 	n_name[0] = '\0';
    217        1.1       mrg 	while (--ancount >= 0 && cp < eom) {
    218       1.22    itojun 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    219        1.1       mrg 		if ((n < 0) || !res_dnok(bp))
    220        1.1       mrg 			break;
    221        1.1       mrg 		cp += n;
    222  1.37.12.1  wrstuden 		(void)strlcpy(n_name, bp, sizeof(n_name));
    223        1.1       mrg 		GETSHORT(type, cp);
    224        1.1       mrg 		GETSHORT(class, cp);
    225        1.1       mrg 		cp += INT32SZ;		/* TTL */
    226        1.1       mrg 		GETSHORT(n, cp);
    227        1.1       mrg 		if (class == C_IN && type == T_PTR) {
    228       1.22    itojun 			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    229        1.1       mrg 			if ((n < 0) || !res_hnok(bp)) {
    230        1.1       mrg 				cp += n;
    231       1.30  christos 				return NULL;
    232        1.1       mrg 			}
    233  1.37.12.1  wrstuden 			cp += n;
    234        1.1       mrg 			*ap++ = bp;
    235        1.1       mrg 			bp += strlen(bp) + 1;
    236        1.1       mrg 			net_entry.n_addrtype =
    237        1.1       mrg 				(class == C_IN) ? AF_INET : AF_UNSPEC;
    238        1.1       mrg 			haveanswer++;
    239        1.1       mrg 		}
    240        1.1       mrg 	}
    241        1.1       mrg 	if (haveanswer) {
    242        1.1       mrg 		*ap = NULL;
    243        1.1       mrg 		switch (net_i) {
    244        1.1       mrg 		case BYADDR:
    245        1.1       mrg 			net_entry.n_name = *net_entry.n_aliases;
    246        1.1       mrg 			net_entry.n_net = 0L;
    247        1.1       mrg 			break;
    248        1.1       mrg 		case BYNAME:
    249       1.26    itojun 			ap = net_entry.n_aliases;
    250       1.26    itojun 		next_alias:
    251       1.26    itojun 			in = *ap++;
    252       1.26    itojun 			if (in == NULL) {
    253       1.26    itojun 				h_errno = HOST_NOT_FOUND;
    254       1.30  christos 				return NULL;
    255       1.26    itojun 			}
    256  1.37.12.1  wrstuden 			net_entry.n_name = n_name;
    257  1.37.12.1  wrstuden 			if (parse_reversed_addr(in, &net_entry.n_net) == -1)
    258       1.32     lukem 				goto next_alias;
    259        1.1       mrg 			break;
    260        1.1       mrg 		}
    261        1.1       mrg 		net_entry.n_aliases++;
    262       1.29    kleink #if (defined(__sparc__) && defined(_LP64)) ||		\
    263       1.29    kleink     defined(__alpha__) ||				\
    264       1.29    kleink     (defined(__i386__) && defined(_LP64)) ||		\
    265       1.29    kleink     (defined(__sh__) && defined(_LP64))
    266       1.29    kleink 		net_entry.__n_pad0 = 0;
    267       1.29    kleink #endif
    268       1.30  christos 		return &net_entry;
    269        1.1       mrg 	}
    270        1.1       mrg 	h_errno = TRY_AGAIN;
    271       1.30  christos 	return NULL;
    272        1.1       mrg }
    273        1.1       mrg 
    274       1.13  christos /*ARGSUSED*/
    275  1.37.12.1  wrstuden static int
    276  1.37.12.1  wrstuden _files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
    277        1.8     lukem {
    278  1.37.12.1  wrstuden 	struct netent	**retval = va_arg(ap, struct netent **);
    279  1.37.12.1  wrstuden 	uint32_t	  net	 = va_arg(ap, uint32_t);
    280  1.37.12.1  wrstuden 	int		  type	 = va_arg(ap, int);
    281        1.8     lukem 
    282  1.37.12.1  wrstuden 	struct netent	 *np;
    283        1.8     lukem 
    284        1.8     lukem 	setnetent(_net_stayopen);
    285  1.37.12.1  wrstuden 	while ((np = getnetent()) != NULL)
    286  1.37.12.1  wrstuden 		if (np->n_addrtype == type && np->n_net == net)
    287        1.8     lukem 			break;
    288        1.8     lukem 	if (!_net_stayopen)
    289        1.8     lukem 		endnetent();
    290  1.37.12.1  wrstuden 
    291  1.37.12.1  wrstuden 	if (np != NULL) {
    292  1.37.12.1  wrstuden 		*retval = np;
    293  1.37.12.1  wrstuden 		return NS_SUCCESS;
    294  1.37.12.1  wrstuden 	} else {
    295        1.8     lukem 		h_errno = HOST_NOT_FOUND;
    296        1.8     lukem 		return NS_NOTFOUND;
    297        1.8     lukem 	}
    298        1.8     lukem }
    299        1.8     lukem 
    300       1.13  christos /*ARGSUSED*/
    301  1.37.12.1  wrstuden static int
    302  1.37.12.1  wrstuden _dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
    303        1.1       mrg {
    304  1.37.12.1  wrstuden 	struct netent	**retval = va_arg(ap, struct netent **);
    305  1.37.12.1  wrstuden 	uint32_t	  net	 = va_arg(ap, uint32_t);
    306  1.37.12.1  wrstuden 	int		  type	 = va_arg(ap, int);
    307  1.37.12.1  wrstuden 
    308  1.37.12.1  wrstuden 	unsigned int	 netbr[4];
    309  1.37.12.1  wrstuden 	int		 nn, anslen;
    310  1.37.12.1  wrstuden 	querybuf	*buf;
    311  1.37.12.1  wrstuden 	char		 qbuf[MAXDNAME];
    312  1.37.12.1  wrstuden 	uint32_t	 net2;
    313  1.37.12.1  wrstuden 	struct netent	*np;
    314  1.37.12.1  wrstuden 	res_state	 res;
    315        1.8     lukem 
    316        1.8     lukem 	if (type != AF_INET)
    317        1.8     lukem 		return NS_UNAVAIL;
    318        1.8     lukem 
    319        1.8     lukem 	for (nn = 4, net2 = net; net2; net2 >>= 8)
    320        1.8     lukem 		netbr[--nn] = (unsigned int)(net2 & 0xff);
    321        1.8     lukem 	switch (nn) {
    322        1.8     lukem 	default:
    323        1.8     lukem 		return NS_UNAVAIL;
    324        1.8     lukem 	case 3: 	/* Class A */
    325       1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
    326        1.8     lukem 		break;
    327        1.8     lukem 	case 2: 	/* Class B */
    328       1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
    329       1.23    itojun 		    netbr[3], netbr[2]);
    330        1.8     lukem 		break;
    331        1.8     lukem 	case 1: 	/* Class C */
    332       1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
    333       1.23    itojun 		    netbr[3], netbr[2], netbr[1]);
    334        1.8     lukem 		break;
    335        1.8     lukem 	case 0: 	/* Class D - E */
    336       1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
    337       1.23    itojun 		    netbr[3], netbr[2], netbr[1], netbr[0]);
    338        1.8     lukem 		break;
    339        1.8     lukem 	}
    340       1.24    itojun 	buf = malloc(sizeof(*buf));
    341       1.24    itojun 	if (buf == NULL) {
    342       1.24    itojun 		h_errno = NETDB_INTERNAL;
    343       1.24    itojun 		return NS_NOTFOUND;
    344       1.24    itojun 	}
    345       1.30  christos 	res = __res_get_state();
    346  1.37.12.1  wrstuden 	if (res == NULL) {
    347  1.37.12.1  wrstuden 		free(buf);
    348       1.30  christos 		return NS_NOTFOUND;
    349  1.37.12.1  wrstuden 	}
    350       1.30  christos 	anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
    351        1.8     lukem 	if (anslen < 0) {
    352       1.24    itojun 		free(buf);
    353        1.8     lukem #ifdef DEBUG
    354       1.30  christos 		if (res->options & RES_DEBUG)
    355        1.8     lukem 			printf("res_query failed\n");
    356        1.8     lukem #endif
    357       1.30  christos 		__res_put_state(res);
    358        1.8     lukem 		return NS_NOTFOUND;
    359        1.8     lukem 	}
    360       1.30  christos 	__res_put_state(res);
    361       1.24    itojun 	np = getnetanswer(buf, anslen, BYADDR);
    362       1.24    itojun 	free(buf);
    363       1.10     lukem 	if (np) {
    364        1.8     lukem 		/* maybe net should be unsigned? */
    365       1.34   tsutsui 		uint32_t u_net = net;
    366        1.8     lukem 
    367        1.8     lukem 		/* Strip trailing zeros */
    368        1.8     lukem 		while ((u_net & 0xff) == 0 && u_net != 0)
    369        1.8     lukem 			u_net >>= 8;
    370       1.10     lukem 		np->n_net = u_net;
    371        1.8     lukem 	}
    372  1.37.12.1  wrstuden 
    373  1.37.12.1  wrstuden 	if (np != NULL) {
    374  1.37.12.1  wrstuden 		*retval = np;
    375  1.37.12.1  wrstuden 		return NS_SUCCESS;
    376  1.37.12.1  wrstuden 	} else {
    377        1.8     lukem 		h_errno = HOST_NOT_FOUND;
    378        1.8     lukem 		return NS_NOTFOUND;
    379        1.8     lukem 	}
    380        1.8     lukem }
    381        1.8     lukem 
    382        1.8     lukem struct netent *
    383       1.30  christos getnetbyaddr(uint32_t net, int net_type)
    384        1.8     lukem {
    385  1.37.12.1  wrstuden 	int		 rv;
    386  1.37.12.1  wrstuden 	struct netent	*retval;
    387  1.37.12.1  wrstuden 
    388       1.12     lukem 	static const ns_dtab dtab[] = {
    389       1.16    kleink 		NS_FILES_CB(_files_getnetbyaddr, NULL)
    390        1.8     lukem 		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },	/* force -DHESIOD */
    391       1.11     lukem 		NS_NIS_CB(_yp_getnetbyaddr, NULL)
    392       1.35  christos 		NS_NULL_CB
    393        1.8     lukem 	};
    394        1.1       mrg 
    395  1.37.12.1  wrstuden 	retval = NULL;
    396       1.10     lukem 	h_errno = NETDB_INTERNAL;
    397  1.37.12.1  wrstuden 	rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr",
    398  1.37.12.1  wrstuden 	    __nsdefaultsrc, &retval, net, net_type);
    399  1.37.12.1  wrstuden 	if (rv == NS_SUCCESS) {
    400  1.37.12.1  wrstuden 		h_errno = NETDB_SUCCESS;
    401  1.37.12.1  wrstuden 		return retval;
    402  1.37.12.1  wrstuden 	}
    403  1.37.12.1  wrstuden 	return NULL;
    404        1.8     lukem }
    405        1.3   thorpej 
    406       1.13  christos /*ARGSUSED*/
    407  1.37.12.1  wrstuden static int
    408  1.37.12.1  wrstuden _files_getnetbyname(void *cbrv, void *cbdata, va_list ap)
    409        1.8     lukem {
    410  1.37.12.1  wrstuden 	struct netent	**retval = va_arg(ap, struct netent **);
    411  1.37.12.1  wrstuden 	const char	 *name	 = va_arg(ap, const char *);
    412        1.8     lukem 
    413  1.37.12.1  wrstuden 	struct netent	 *np;
    414  1.37.12.1  wrstuden 	char		**cp;
    415       1.17     lukem 
    416        1.8     lukem 	setnetent(_net_stayopen);
    417  1.37.12.1  wrstuden 	while ((np = getnetent()) != NULL) {
    418  1.37.12.1  wrstuden 		if (strcasecmp(np->n_name, name) == 0)
    419        1.8     lukem 			break;
    420  1.37.12.1  wrstuden 		for (cp = np->n_aliases; *cp != 0; cp++)
    421        1.8     lukem 			if (strcasecmp(*cp, name) == 0)
    422        1.8     lukem 				goto found;
    423        1.8     lukem 	}
    424        1.8     lukem found:
    425        1.8     lukem 	if (!_net_stayopen)
    426        1.8     lukem 		endnetent();
    427  1.37.12.1  wrstuden 
    428  1.37.12.1  wrstuden 	if (np != NULL) {
    429  1.37.12.1  wrstuden 		*retval = np;
    430  1.37.12.1  wrstuden 		return NS_SUCCESS;
    431  1.37.12.1  wrstuden 	} else {
    432        1.8     lukem 		h_errno = HOST_NOT_FOUND;
    433        1.8     lukem 		return NS_NOTFOUND;
    434        1.8     lukem 	}
    435        1.8     lukem }
    436        1.8     lukem 
    437       1.13  christos /*ARGSUSED*/
    438  1.37.12.1  wrstuden static int
    439  1.37.12.1  wrstuden _dns_getnetbyname(void *cbrv, void *cbdata, va_list ap)
    440        1.8     lukem {
    441  1.37.12.1  wrstuden 	struct netent	**retval = va_arg(ap, struct netent **);
    442  1.37.12.1  wrstuden 	const char	 *name	 = va_arg(ap, const char *);
    443        1.8     lukem 
    444  1.37.12.1  wrstuden 	int		 anslen;
    445  1.37.12.1  wrstuden 	querybuf	*buf;
    446  1.37.12.1  wrstuden 	char		 qbuf[MAXDNAME];
    447  1.37.12.1  wrstuden 	struct netent	*np;
    448  1.37.12.1  wrstuden 	res_state	 res;
    449       1.17     lukem 
    450  1.37.12.1  wrstuden 	strlcpy(&qbuf[0], name, sizeof(qbuf));
    451       1.24    itojun 	buf = malloc(sizeof(*buf));
    452       1.24    itojun 	if (buf == NULL) {
    453       1.24    itojun 		h_errno = NETDB_INTERNAL;
    454       1.24    itojun 		return NS_NOTFOUND;
    455       1.24    itojun 	}
    456       1.30  christos 	res = __res_get_state();
    457       1.33  ginsbach 	if (res == NULL) {
    458       1.33  ginsbach 		free(buf);
    459       1.30  christos 		return NS_NOTFOUND;
    460       1.33  ginsbach 	}
    461       1.30  christos 	anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf,
    462       1.30  christos 	    sizeof(buf->buf));
    463        1.8     lukem 	if (anslen < 0) {
    464       1.24    itojun 		free(buf);
    465        1.1       mrg #ifdef DEBUG
    466       1.30  christos 		if (res->options & RES_DEBUG)
    467       1.24    itojun 			printf("res_search failed\n");
    468        1.1       mrg #endif
    469       1.30  christos 		__res_put_state(res);
    470        1.8     lukem 		return NS_NOTFOUND;
    471        1.8     lukem 	}
    472       1.30  christos 	__res_put_state(res);
    473       1.24    itojun 	np = getnetanswer(buf, anslen, BYNAME);
    474       1.24    itojun 	free(buf);
    475  1.37.12.1  wrstuden 
    476  1.37.12.1  wrstuden 	if (np != NULL) {
    477  1.37.12.1  wrstuden 		*retval = np;
    478  1.37.12.1  wrstuden 		return NS_SUCCESS;
    479  1.37.12.1  wrstuden 	} else {
    480        1.8     lukem 		h_errno = HOST_NOT_FOUND;
    481        1.8     lukem 		return NS_NOTFOUND;
    482        1.1       mrg 	}
    483        1.1       mrg }
    484        1.1       mrg 
    485        1.1       mrg struct netent *
    486  1.37.12.1  wrstuden getnetbyname(const char *name)
    487        1.1       mrg {
    488  1.37.12.1  wrstuden 	int		 rv;
    489  1.37.12.1  wrstuden 	struct netent	*retval;
    490  1.37.12.1  wrstuden 
    491       1.12     lukem 	static const ns_dtab dtab[] = {
    492       1.16    kleink 		NS_FILES_CB(_files_getnetbyname, NULL)
    493        1.8     lukem 		{ NSSRC_DNS, _dns_getnetbyname, NULL },	/* force -DHESIOD */
    494       1.11     lukem 		NS_NIS_CB(_yp_getnetbyname, NULL)
    495       1.35  christos 		NS_NULL_CB
    496        1.8     lukem 	};
    497        1.1       mrg 
    498  1.37.12.1  wrstuden 	_DIAGASSERT(name != NULL);
    499       1.17     lukem 
    500  1.37.12.1  wrstuden 	retval = NULL;
    501       1.10     lukem 	h_errno = NETDB_INTERNAL;
    502  1.37.12.1  wrstuden 	rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname",
    503  1.37.12.1  wrstuden 	    __nsdefaultsrc, &retval, name);
    504  1.37.12.1  wrstuden 	if (rv == NS_SUCCESS) {
    505  1.37.12.1  wrstuden 		h_errno = NETDB_SUCCESS;
    506  1.37.12.1  wrstuden 		return retval;
    507  1.37.12.1  wrstuden 	}
    508  1.37.12.1  wrstuden 	return NULL;
    509       1.10     lukem }
    510       1.10     lukem 
    511       1.10     lukem #ifdef YP
    512       1.15  christos /*ARGSUSED*/
    513  1.37.12.1  wrstuden static int
    514  1.37.12.1  wrstuden _yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap)
    515       1.10     lukem {
    516  1.37.12.1  wrstuden 	struct netent	**retval = va_arg(ap, struct netent **);
    517  1.37.12.1  wrstuden 	uint32_t	  net	 = va_arg(ap, uint32_t);
    518  1.37.12.1  wrstuden 	int		  type	 = va_arg(ap, int);
    519  1.37.12.1  wrstuden 
    520  1.37.12.1  wrstuden 	struct netent	*np;
    521  1.37.12.1  wrstuden 	char		 qbuf[MAXDNAME];
    522  1.37.12.1  wrstuden 	unsigned int	 netbr[4];
    523  1.37.12.1  wrstuden 	uint32_t	 net2;
    524  1.37.12.1  wrstuden 	int		 r;
    525       1.10     lukem 
    526       1.10     lukem 	if (type != AF_INET)
    527       1.10     lukem 		return NS_UNAVAIL;
    528       1.10     lukem 
    529       1.10     lukem 	if (!__ypdomain) {
    530       1.10     lukem 		if (_yp_check(&__ypdomain) == 0)
    531       1.10     lukem 			return NS_UNAVAIL;
    532       1.10     lukem 	}
    533       1.10     lukem 	np = NULL;
    534       1.10     lukem 	if (__ypcurrent)
    535       1.10     lukem 		free(__ypcurrent);
    536       1.10     lukem 	__ypcurrent = NULL;
    537       1.10     lukem 	for (r = 4, net2 = net; net2; net2 >>= 8)
    538       1.10     lukem 		netbr[--r] = (unsigned int)(net2 & 0xff);
    539       1.10     lukem 	switch (r) {
    540       1.10     lukem 	default:
    541       1.10     lukem 		return NS_UNAVAIL;
    542       1.10     lukem 	case 3: 	/* Class A */
    543       1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
    544       1.10     lukem 		break;
    545       1.10     lukem 	case 2: 	/* Class B */
    546       1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
    547       1.10     lukem 		break;
    548       1.10     lukem 	case 1: 	/* Class C */
    549       1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
    550       1.28     lukem 		    netbr[3]);
    551       1.10     lukem 		break;
    552       1.10     lukem 	case 0: 	/* Class D - E */
    553       1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
    554       1.23    itojun 		    netbr[2], netbr[3]);
    555       1.10     lukem 		break;
    556       1.10     lukem 	}
    557       1.10     lukem 	r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
    558       1.10     lukem 	    &__ypcurrent, &__ypcurrentlen);
    559       1.10     lukem 	if (r == 0)
    560       1.10     lukem 		np = _ypnetent(__ypcurrent);
    561       1.10     lukem 
    562  1.37.12.1  wrstuden 	if (np != NULL) {
    563  1.37.12.1  wrstuden 		*retval = np;
    564  1.37.12.1  wrstuden 		return NS_SUCCESS;
    565  1.37.12.1  wrstuden 	} else {
    566       1.10     lukem 		h_errno = HOST_NOT_FOUND;
    567       1.10     lukem 		return NS_NOTFOUND;
    568       1.10     lukem 	}
    569       1.10     lukem }
    570       1.10     lukem 
    571       1.15  christos /*ARGSUSED*/
    572  1.37.12.1  wrstuden static int
    573  1.37.12.1  wrstuden _yp_getnetbyname(void *cbrv, void *cbdata, va_list ap)
    574       1.10     lukem {
    575  1.37.12.1  wrstuden 	struct netent	**retval = va_arg(ap, struct netent **);
    576  1.37.12.1  wrstuden 	const char	 *name	 = va_arg(ap, const char *);
    577       1.17     lukem 
    578  1.37.12.1  wrstuden 	struct netent	*np;
    579  1.37.12.1  wrstuden 	int		 r;
    580       1.10     lukem 
    581       1.10     lukem 	if (!__ypdomain) {
    582       1.10     lukem 		if (_yp_check(&__ypdomain) == 0)
    583       1.10     lukem 			return NS_UNAVAIL;
    584       1.10     lukem 	}
    585       1.10     lukem 	np = NULL;
    586       1.10     lukem 	if (__ypcurrent)
    587       1.10     lukem 		free(__ypcurrent);
    588       1.10     lukem 	__ypcurrent = NULL;
    589       1.10     lukem 	r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
    590       1.10     lukem 	    &__ypcurrent, &__ypcurrentlen);
    591       1.10     lukem 	if (r == 0)
    592       1.10     lukem 		np = _ypnetent(__ypcurrent);
    593       1.10     lukem 
    594  1.37.12.1  wrstuden 	if (np != NULL) {
    595  1.37.12.1  wrstuden 		*retval = np;
    596  1.37.12.1  wrstuden 		return NS_SUCCESS;
    597  1.37.12.1  wrstuden 	} else {
    598       1.10     lukem 		h_errno = HOST_NOT_FOUND;
    599       1.10     lukem 		return NS_NOTFOUND;
    600       1.10     lukem 	}
    601       1.10     lukem }
    602       1.10     lukem 
    603  1.37.12.1  wrstuden static struct netent *
    604       1.30  christos _ypnetent(char *line)
    605       1.10     lukem {
    606       1.10     lukem 	char *cp, *p, **q;
    607       1.17     lukem 
    608       1.17     lukem 	_DIAGASSERT(line != NULL);
    609       1.10     lukem 
    610       1.10     lukem 	net_entry.n_name = line;
    611       1.10     lukem 	cp = strpbrk(line, " \t");
    612       1.10     lukem 	if (cp == NULL)
    613       1.30  christos 		return NULL;
    614       1.10     lukem 	*cp++ = '\0';
    615       1.10     lukem 	while (*cp == ' ' || *cp == '\t')
    616       1.10     lukem 		cp++;
    617       1.10     lukem 	p = strpbrk(cp, " \t");
    618       1.10     lukem 	if (p != NULL)
    619       1.10     lukem 		*p++ = '\0';
    620       1.10     lukem 	net_entry.n_net = inet_network(cp);
    621       1.29    kleink #if (defined(__sparc__) && defined(_LP64)) ||		\
    622       1.29    kleink     defined(__alpha__) ||				\
    623       1.29    kleink     (defined(__i386__) && defined(_LP64)) ||		\
    624       1.29    kleink     (defined(__sh__) && defined(_LP64))
    625       1.29    kleink 	net_entry.__n_pad0 = 0;
    626       1.29    kleink #endif
    627       1.10     lukem 	net_entry.n_addrtype = AF_INET;
    628       1.10     lukem 	q = net_entry.n_aliases = net_aliases;
    629       1.10     lukem 	if (p != NULL)  {
    630       1.10     lukem 		cp = p;
    631       1.10     lukem 		while (cp && *cp) {
    632       1.10     lukem 			if (*cp == ' ' || *cp == '\t') {
    633       1.10     lukem 				cp++;
    634       1.10     lukem 				continue;
    635       1.10     lukem 			}
    636       1.10     lukem 			if (q < &net_aliases[MAXALIASES - 1])
    637       1.10     lukem 				*q++ = cp;
    638       1.10     lukem 			cp = strpbrk(cp, " \t");
    639       1.10     lukem 			if (cp != NULL)
    640       1.10     lukem 				*cp++ = '\0';
    641       1.10     lukem 		}
    642        1.1       mrg 	}
    643       1.10     lukem 	*q = NULL;
    644       1.10     lukem 
    645       1.30  christos 	return &net_entry;
    646        1.1       mrg }
    647       1.10     lukem #endif
    648