Home | History | Annotate | Line # | Download | only in net
getnetnamadr.c revision 1.31.2.1
      1  1.31.2.1      tron /*	$NetBSD: getnetnamadr.c,v 1.31.2.1 2005/06/11 12:12:11 tron 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.31.2.1      tron __RCSID("$NetBSD: getnetnamadr.c,v 1.31.2.1 2005/06/11 12:12:11 tron 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.30  christos static struct netent *getnetanswer(querybuf *, int, int);
    108      1.30  christos int	_files_getnetbyaddr(void *, void *, va_list);
    109      1.30  christos int	_files_getnetbyname(void *, void *, va_list);
    110      1.30  christos int	_dns_getnetbyaddr(void *, void *, va_list);
    111      1.30  christos int	_dns_getnetbyname(void *, void *, va_list);
    112      1.10     lukem #ifdef YP
    113      1.30  christos int	_yp_getnetbyaddr(void *, void *, va_list);
    114      1.30  christos int	_yp_getnetbyname(void *, void *, va_list);
    115      1.30  christos struct netent *_ypnetent(char *);
    116      1.10     lukem #endif
    117      1.10     lukem 
    118       1.1       mrg static struct netent *
    119      1.30  christos getnetanswer(querybuf *answer, int anslen, int net_i)
    120       1.1       mrg {
    121       1.9     lukem 	HEADER *hp;
    122       1.9     lukem 	u_char *cp;
    123       1.9     lukem 	int n;
    124       1.1       mrg 	u_char *eom;
    125      1.22    itojun 	int type, class, ancount, qdcount, haveanswer, i, nchar;
    126      1.25    itojun 	char aux1[MAXDNAME], aux2[MAXDNAME], ans[MAXDNAME];
    127      1.25    itojun 	char *in, *st, *pauxt, *bp, **ap;
    128      1.26    itojun 	char *paux1 = &aux1[0], *paux2 = &aux2[0], *ep;
    129      1.10     lukem 	static	char netbuf[PACKETSZ];
    130       1.1       mrg 
    131      1.17     lukem 	_DIAGASSERT(answer != NULL);
    132      1.17     lukem 
    133       1.1       mrg 	/*
    134       1.1       mrg 	 * find first satisfactory answer
    135       1.1       mrg 	 *
    136       1.1       mrg 	 *      answer --> +------------+  ( MESSAGE )
    137       1.1       mrg 	 *		   |   Header   |
    138       1.1       mrg 	 *		   +------------+
    139       1.1       mrg 	 *		   |  Question  | the question for the name server
    140       1.1       mrg 	 *		   +------------+
    141       1.1       mrg 	 *		   |   Answer   | RRs answering the question
    142       1.1       mrg 	 *		   +------------+
    143       1.1       mrg 	 *		   | Authority  | RRs pointing toward an authority
    144       1.1       mrg 	 *		   | Additional | RRs holding additional information
    145       1.1       mrg 	 *		   +------------+
    146       1.1       mrg 	 */
    147       1.1       mrg 	eom = answer->buf + anslen;
    148       1.1       mrg 	hp = &answer->hdr;
    149       1.1       mrg 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
    150       1.1       mrg 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
    151       1.1       mrg 	bp = netbuf;
    152      1.22    itojun 	ep = netbuf + sizeof(netbuf);
    153       1.1       mrg 	cp = answer->buf + HFIXEDSZ;
    154       1.1       mrg 	if (!qdcount) {
    155       1.1       mrg 		if (hp->aa)
    156       1.1       mrg 			h_errno = HOST_NOT_FOUND;
    157       1.1       mrg 		else
    158       1.1       mrg 			h_errno = TRY_AGAIN;
    159      1.30  christos 		return NULL;
    160       1.1       mrg 	}
    161      1.26    itojun 	while (qdcount-- > 0) {
    162      1.26    itojun 		n = __dn_skipname(cp, eom);
    163      1.26    itojun 		if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
    164      1.26    itojun 			h_errno = NO_RECOVERY;
    165      1.26    itojun 			return(NULL);
    166      1.26    itojun 		}
    167      1.26    itojun 		cp += n + QFIXEDSZ;
    168      1.26    itojun 	}
    169       1.1       mrg 	ap = net_aliases;
    170       1.1       mrg 	*ap = NULL;
    171       1.1       mrg 	net_entry.n_aliases = net_aliases;
    172       1.1       mrg 	haveanswer = 0;
    173       1.1       mrg 	while (--ancount >= 0 && cp < eom) {
    174      1.22    itojun 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    175       1.1       mrg 		if ((n < 0) || !res_dnok(bp))
    176       1.1       mrg 			break;
    177       1.1       mrg 		cp += n;
    178       1.1       mrg 		ans[0] = '\0';
    179      1.26    itojun 		(void)strlcpy(ans, bp, sizeof(ans));
    180       1.1       mrg 		GETSHORT(type, cp);
    181       1.1       mrg 		GETSHORT(class, cp);
    182       1.1       mrg 		cp += INT32SZ;		/* TTL */
    183       1.1       mrg 		GETSHORT(n, cp);
    184       1.1       mrg 		if (class == C_IN && type == T_PTR) {
    185      1.22    itojun 			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    186       1.1       mrg 			if ((n < 0) || !res_hnok(bp)) {
    187       1.1       mrg 				cp += n;
    188      1.30  christos 				return NULL;
    189       1.1       mrg 			}
    190       1.1       mrg 			cp += n;
    191       1.1       mrg 			*ap++ = bp;
    192       1.1       mrg 			bp += strlen(bp) + 1;
    193       1.1       mrg 			net_entry.n_addrtype =
    194       1.1       mrg 				(class == C_IN) ? AF_INET : AF_UNSPEC;
    195       1.1       mrg 			haveanswer++;
    196       1.1       mrg 		}
    197       1.1       mrg 	}
    198       1.1       mrg 	if (haveanswer) {
    199       1.1       mrg 		*ap = NULL;
    200       1.1       mrg 		switch (net_i) {
    201       1.1       mrg 		case BYADDR:
    202       1.1       mrg 			net_entry.n_name = *net_entry.n_aliases;
    203       1.1       mrg 			net_entry.n_net = 0L;
    204       1.1       mrg 			break;
    205       1.1       mrg 		case BYNAME:
    206      1.26    itojun 			ap = net_entry.n_aliases;
    207      1.26    itojun 		next_alias:
    208      1.26    itojun 			in = *ap++;
    209      1.26    itojun 			if (in == NULL) {
    210      1.26    itojun 				h_errno = HOST_NOT_FOUND;
    211      1.30  christos 				return NULL;
    212      1.26    itojun 			}
    213      1.26    itojun 			net_entry.n_name = ans;
    214       1.1       mrg 			aux2[0] = '\0';
    215       1.1       mrg 			for (i = 0; i < 4; i++) {
    216       1.1       mrg 				for (st = in, nchar = 0;
    217      1.26    itojun 				     isdigit((unsigned char)*st);
    218       1.1       mrg 				     st++, nchar++)
    219       1.1       mrg 					;
    220      1.26    itojun 				if (*st != '.' || nchar == 0 || nchar > 3)
    221      1.26    itojun 					goto next_alias;
    222      1.26    itojun 				if (i != 0)
    223      1.26    itojun 					nchar++;
    224      1.26    itojun 				(void)strlcpy(paux1, in, (size_t)nchar);
    225      1.26    itojun 				paux1[nchar] = '\0';
    226      1.26    itojun 				pauxt = paux2;
    227      1.26    itojun 				paux2 = strcat(paux1, paux2);
    228      1.26    itojun 				paux1 = pauxt;
    229       1.1       mrg 				in = ++st;
    230       1.1       mrg 			}
    231  1.31.2.1      tron 			if (strcasecmp(in, "IN-ADDR.ARPA") != 0)
    232  1.31.2.1      tron 				goto next_alias;
    233       1.1       mrg 			net_entry.n_net = inet_network(paux2);
    234       1.1       mrg 			break;
    235       1.1       mrg 		}
    236       1.1       mrg 		net_entry.n_aliases++;
    237      1.29    kleink #if (defined(__sparc__) && defined(_LP64)) ||		\
    238      1.29    kleink     defined(__alpha__) ||				\
    239      1.29    kleink     (defined(__i386__) && defined(_LP64)) ||		\
    240      1.29    kleink     (defined(__sh__) && defined(_LP64))
    241      1.29    kleink 		net_entry.__n_pad0 = 0;
    242      1.29    kleink #endif
    243      1.30  christos 		return &net_entry;
    244       1.1       mrg 	}
    245       1.1       mrg 	h_errno = TRY_AGAIN;
    246      1.30  christos 	return NULL;
    247       1.1       mrg }
    248       1.1       mrg 
    249      1.13  christos /*ARGSUSED*/
    250       1.8     lukem int
    251      1.30  christos _files_getnetbyaddr(void *rv, void *cb_data, va_list ap)
    252       1.8     lukem {
    253       1.8     lukem 	struct netent *p;
    254       1.8     lukem 	unsigned long net;
    255       1.8     lukem 	int type;
    256       1.8     lukem 
    257      1.17     lukem 	_DIAGASSERT(rv != NULL);
    258      1.17     lukem 
    259       1.8     lukem 	net = va_arg(ap, unsigned long);
    260       1.8     lukem 	type = va_arg(ap, int);
    261       1.8     lukem 
    262       1.8     lukem 	setnetent(_net_stayopen);
    263       1.8     lukem 	while ((p = getnetent()) != NULL)
    264       1.8     lukem 		if (p->n_addrtype == type && p->n_net == net)
    265       1.8     lukem 			break;
    266       1.8     lukem 	if (!_net_stayopen)
    267       1.8     lukem 		endnetent();
    268       1.8     lukem 	*((struct netent **)rv) = p;
    269       1.8     lukem 	if (p==NULL) {
    270       1.8     lukem 		h_errno = HOST_NOT_FOUND;
    271       1.8     lukem 		return NS_NOTFOUND;
    272       1.8     lukem 	}
    273       1.8     lukem 	return NS_SUCCESS;
    274       1.8     lukem }
    275       1.8     lukem 
    276      1.13  christos /*ARGSUSED*/
    277       1.8     lukem int
    278      1.30  christos _dns_getnetbyaddr(void *rv, void *cb_data, va_list ap)
    279       1.1       mrg {
    280       1.1       mrg 	unsigned int netbr[4];
    281       1.9     lukem 	int nn, anslen;
    282      1.24    itojun 	querybuf *buf;
    283       1.1       mrg 	char qbuf[MAXDNAME];
    284       1.1       mrg 	unsigned long net2;
    285      1.10     lukem 	struct netent *np;
    286       1.8     lukem 	unsigned long net;
    287       1.8     lukem 	int type;
    288      1.30  christos 	res_state res;
    289       1.1       mrg 
    290      1.17     lukem 	_DIAGASSERT(rv != NULL);
    291      1.17     lukem 
    292       1.8     lukem 	net = va_arg(ap, unsigned long);
    293       1.8     lukem 	type = va_arg(ap, int);
    294       1.8     lukem 
    295       1.8     lukem 	if (type != AF_INET)
    296       1.8     lukem 		return NS_UNAVAIL;
    297       1.8     lukem 
    298       1.8     lukem 	for (nn = 4, net2 = net; net2; net2 >>= 8)
    299       1.8     lukem 		netbr[--nn] = (unsigned int)(net2 & 0xff);
    300       1.8     lukem 	switch (nn) {
    301       1.8     lukem 	default:
    302       1.8     lukem 		return NS_UNAVAIL;
    303       1.8     lukem 	case 3: 	/* Class A */
    304      1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
    305       1.8     lukem 		break;
    306       1.8     lukem 	case 2: 	/* Class B */
    307      1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
    308      1.23    itojun 		    netbr[3], netbr[2]);
    309       1.8     lukem 		break;
    310       1.8     lukem 	case 1: 	/* Class C */
    311      1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
    312      1.23    itojun 		    netbr[3], netbr[2], netbr[1]);
    313       1.8     lukem 		break;
    314       1.8     lukem 	case 0: 	/* Class D - E */
    315      1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
    316      1.23    itojun 		    netbr[3], netbr[2], netbr[1], netbr[0]);
    317       1.8     lukem 		break;
    318       1.8     lukem 	}
    319      1.24    itojun 	buf = malloc(sizeof(*buf));
    320      1.24    itojun 	if (buf == NULL) {
    321      1.24    itojun 		h_errno = NETDB_INTERNAL;
    322      1.24    itojun 		return NS_NOTFOUND;
    323      1.24    itojun 	}
    324      1.30  christos 	res = __res_get_state();
    325      1.31  christos 	if (res == NULL)
    326      1.30  christos 		return NS_NOTFOUND;
    327      1.30  christos 	anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
    328       1.8     lukem 	if (anslen < 0) {
    329      1.24    itojun 		free(buf);
    330       1.8     lukem #ifdef DEBUG
    331      1.30  christos 		if (res->options & RES_DEBUG)
    332       1.8     lukem 			printf("res_query failed\n");
    333       1.8     lukem #endif
    334      1.30  christos 		__res_put_state(res);
    335       1.8     lukem 		return NS_NOTFOUND;
    336       1.8     lukem 	}
    337      1.30  christos 	__res_put_state(res);
    338      1.24    itojun 	np = getnetanswer(buf, anslen, BYADDR);
    339      1.24    itojun 	free(buf);
    340      1.10     lukem 	if (np) {
    341       1.8     lukem 		/* maybe net should be unsigned? */
    342       1.8     lukem 		unsigned long u_net = net;
    343       1.8     lukem 
    344       1.8     lukem 		/* Strip trailing zeros */
    345       1.8     lukem 		while ((u_net & 0xff) == 0 && u_net != 0)
    346       1.8     lukem 			u_net >>= 8;
    347      1.10     lukem 		np->n_net = u_net;
    348       1.8     lukem 	}
    349      1.10     lukem 	*((struct netent **)rv) = np;
    350      1.10     lukem 	if (np == NULL) {
    351       1.8     lukem 		h_errno = HOST_NOT_FOUND;
    352       1.8     lukem 		return NS_NOTFOUND;
    353       1.8     lukem 	}
    354       1.8     lukem 	return NS_SUCCESS;
    355       1.8     lukem }
    356       1.8     lukem 
    357      1.10     lukem 
    358       1.8     lukem struct netent *
    359      1.30  christos getnetbyaddr(uint32_t net, int net_type)
    360       1.8     lukem {
    361      1.10     lukem 	struct netent *np;
    362      1.12     lukem 	static const ns_dtab dtab[] = {
    363      1.16    kleink 		NS_FILES_CB(_files_getnetbyaddr, NULL)
    364       1.8     lukem 		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },	/* force -DHESIOD */
    365      1.11     lukem 		NS_NIS_CB(_yp_getnetbyaddr, NULL)
    366      1.11     lukem 		{ 0 }
    367       1.8     lukem 	};
    368       1.1       mrg 
    369      1.10     lukem 	np = NULL;
    370      1.10     lukem 	h_errno = NETDB_INTERNAL;
    371      1.11     lukem 	if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyaddr", __nsdefaultsrc,
    372      1.11     lukem 	    net, net_type) != NS_SUCCESS)
    373      1.30  christos 		return NULL;
    374      1.10     lukem 	h_errno = NETDB_SUCCESS;
    375      1.30  christos 	return np;
    376       1.8     lukem }
    377       1.3   thorpej 
    378      1.13  christos /*ARGSUSED*/
    379       1.8     lukem int
    380      1.30  christos _files_getnetbyname(void *rv, void *cb_data, va_list ap)
    381       1.8     lukem {
    382       1.8     lukem 	struct netent *p;
    383       1.8     lukem 	char **cp;
    384       1.8     lukem 	const char *name;
    385       1.8     lukem 
    386      1.17     lukem 	_DIAGASSERT(rv != NULL);
    387      1.17     lukem 
    388       1.8     lukem 	name = va_arg(ap, const char *);
    389       1.8     lukem 	setnetent(_net_stayopen);
    390       1.8     lukem 	while ((p = getnetent()) != NULL) {
    391       1.8     lukem 		if (strcasecmp(p->n_name, name) == 0)
    392       1.8     lukem 			break;
    393       1.8     lukem 		for (cp = p->n_aliases; *cp != 0; cp++)
    394       1.8     lukem 			if (strcasecmp(*cp, name) == 0)
    395       1.8     lukem 				goto found;
    396       1.8     lukem 	}
    397       1.8     lukem found:
    398       1.8     lukem 	if (!_net_stayopen)
    399       1.8     lukem 		endnetent();
    400       1.8     lukem 	*((struct netent **)rv) = p;
    401       1.8     lukem 	if (p==NULL) {
    402       1.8     lukem 		h_errno = HOST_NOT_FOUND;
    403       1.8     lukem 		return NS_NOTFOUND;
    404       1.8     lukem 	}
    405       1.8     lukem 	return NS_SUCCESS;
    406       1.8     lukem }
    407       1.8     lukem 
    408      1.13  christos /*ARGSUSED*/
    409       1.8     lukem int
    410      1.30  christos _dns_getnetbyname(void *rv, void *cb_data, va_list ap)
    411       1.8     lukem {
    412       1.9     lukem 	int anslen;
    413      1.24    itojun 	querybuf *buf;
    414       1.8     lukem 	char qbuf[MAXDNAME];
    415      1.10     lukem 	struct netent *np;
    416       1.8     lukem 	const char *net;
    417      1.30  christos 	res_state res;
    418       1.8     lukem 
    419      1.17     lukem 	_DIAGASSERT(rv != NULL);
    420      1.17     lukem 
    421       1.8     lukem 	net = va_arg(ap, const char *);
    422      1.25    itojun 	strlcpy(&qbuf[0], net, sizeof(qbuf));
    423      1.24    itojun 	buf = malloc(sizeof(*buf));
    424      1.24    itojun 	if (buf == NULL) {
    425      1.24    itojun 		h_errno = NETDB_INTERNAL;
    426      1.24    itojun 		return NS_NOTFOUND;
    427      1.24    itojun 	}
    428      1.30  christos 	res = __res_get_state();
    429      1.31  christos 	if (res == NULL)
    430      1.30  christos 		return NS_NOTFOUND;
    431      1.30  christos 	anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf,
    432      1.30  christos 	    sizeof(buf->buf));
    433       1.8     lukem 	if (anslen < 0) {
    434      1.24    itojun 		free(buf);
    435       1.1       mrg #ifdef DEBUG
    436      1.30  christos 		if (res->options & RES_DEBUG)
    437      1.24    itojun 			printf("res_search failed\n");
    438       1.1       mrg #endif
    439      1.30  christos 		__res_put_state(res);
    440       1.8     lukem 		return NS_NOTFOUND;
    441       1.8     lukem 	}
    442      1.30  christos 	__res_put_state(res);
    443      1.24    itojun 	np = getnetanswer(buf, anslen, BYNAME);
    444      1.24    itojun 	free(buf);
    445      1.10     lukem 	*((struct netent **)rv) = np;
    446      1.10     lukem 	if (np == NULL) {
    447       1.8     lukem 		h_errno = HOST_NOT_FOUND;
    448       1.8     lukem 		return NS_NOTFOUND;
    449       1.1       mrg 	}
    450       1.8     lukem 	return NS_SUCCESS;
    451       1.1       mrg }
    452       1.1       mrg 
    453       1.1       mrg struct netent *
    454      1.30  christos getnetbyname(const char *net)
    455       1.1       mrg {
    456      1.10     lukem 	struct netent *np;
    457      1.12     lukem 	static const ns_dtab dtab[] = {
    458      1.16    kleink 		NS_FILES_CB(_files_getnetbyname, NULL)
    459       1.8     lukem 		{ NSSRC_DNS, _dns_getnetbyname, NULL },	/* force -DHESIOD */
    460      1.11     lukem 		NS_NIS_CB(_yp_getnetbyname, NULL)
    461      1.11     lukem 		{ 0 }
    462       1.8     lukem 	};
    463       1.1       mrg 
    464      1.17     lukem 	_DIAGASSERT(net != NULL);
    465      1.17     lukem 
    466      1.10     lukem 	np = NULL;
    467      1.10     lukem 	h_errno = NETDB_INTERNAL;
    468      1.11     lukem 	if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyname", __nsdefaultsrc,
    469      1.11     lukem 	    net) != NS_SUCCESS)
    470      1.30  christos 		return NULL;
    471      1.10     lukem 	h_errno = NETDB_SUCCESS;
    472      1.30  christos 	return np;
    473      1.10     lukem }
    474      1.10     lukem 
    475      1.10     lukem #ifdef YP
    476      1.15  christos /*ARGSUSED*/
    477      1.10     lukem int
    478      1.30  christos _yp_getnetbyaddr(void *rv, void *cb_data, va_list ap)
    479      1.10     lukem {
    480      1.10     lukem 	struct netent *np;
    481      1.10     lukem 	char qbuf[MAXDNAME];
    482      1.10     lukem 	unsigned int netbr[4];
    483      1.10     lukem 	unsigned long net, net2;
    484      1.10     lukem 	int type, r;
    485      1.10     lukem 
    486      1.17     lukem 	_DIAGASSERT(rv != NULL);
    487      1.17     lukem 
    488      1.10     lukem 	net = va_arg(ap, unsigned long);
    489      1.10     lukem 	type = va_arg(ap, int);
    490      1.10     lukem 
    491      1.10     lukem 	if (type != AF_INET)
    492      1.10     lukem 		return NS_UNAVAIL;
    493      1.10     lukem 
    494      1.10     lukem 	if (!__ypdomain) {
    495      1.10     lukem 		if (_yp_check(&__ypdomain) == 0)
    496      1.10     lukem 			return NS_UNAVAIL;
    497      1.10     lukem 	}
    498      1.10     lukem 	np = NULL;
    499      1.10     lukem 	if (__ypcurrent)
    500      1.10     lukem 		free(__ypcurrent);
    501      1.10     lukem 	__ypcurrent = NULL;
    502      1.10     lukem 	for (r = 4, net2 = net; net2; net2 >>= 8)
    503      1.10     lukem 		netbr[--r] = (unsigned int)(net2 & 0xff);
    504      1.10     lukem 	switch (r) {
    505      1.10     lukem 	default:
    506      1.10     lukem 		return NS_UNAVAIL;
    507      1.10     lukem 	case 3: 	/* Class A */
    508      1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
    509      1.10     lukem 		break;
    510      1.10     lukem 	case 2: 	/* Class B */
    511      1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
    512      1.10     lukem 		break;
    513      1.10     lukem 	case 1: 	/* Class C */
    514      1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
    515      1.28     lukem 		    netbr[3]);
    516      1.10     lukem 		break;
    517      1.10     lukem 	case 0: 	/* Class D - E */
    518      1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
    519      1.23    itojun 		    netbr[2], netbr[3]);
    520      1.10     lukem 		break;
    521      1.10     lukem 	}
    522      1.10     lukem 	r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
    523      1.10     lukem 	    &__ypcurrent, &__ypcurrentlen);
    524      1.10     lukem 	if (r == 0)
    525      1.10     lukem 		np = _ypnetent(__ypcurrent);
    526      1.10     lukem 
    527      1.10     lukem 	*((struct netent **)rv) = np;
    528      1.10     lukem 	if (np == NULL) {
    529      1.10     lukem 		h_errno = HOST_NOT_FOUND;
    530      1.10     lukem 		return NS_NOTFOUND;
    531      1.10     lukem 	}
    532      1.10     lukem 	return NS_SUCCESS;
    533      1.10     lukem 
    534      1.10     lukem }
    535      1.10     lukem 
    536      1.10     lukem int
    537      1.15  christos /*ARGSUSED*/
    538      1.30  christos _yp_getnetbyname(void *rv, void *cb_data, va_list ap)
    539      1.10     lukem {
    540      1.10     lukem 	struct netent *np;
    541      1.10     lukem 	const char *name;
    542      1.10     lukem 	int r;
    543      1.10     lukem 
    544      1.17     lukem 	_DIAGASSERT(rv != NULL);
    545      1.17     lukem 
    546      1.10     lukem 	name = va_arg(ap, const char *);
    547      1.10     lukem 
    548      1.10     lukem 	if (!__ypdomain) {
    549      1.10     lukem 		if (_yp_check(&__ypdomain) == 0)
    550      1.10     lukem 			return NS_UNAVAIL;
    551      1.10     lukem 	}
    552      1.10     lukem 	np = NULL;
    553      1.10     lukem 	if (__ypcurrent)
    554      1.10     lukem 		free(__ypcurrent);
    555      1.10     lukem 	__ypcurrent = NULL;
    556      1.10     lukem 	r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
    557      1.10     lukem 	    &__ypcurrent, &__ypcurrentlen);
    558      1.10     lukem 	if (r == 0)
    559      1.10     lukem 		np = _ypnetent(__ypcurrent);
    560      1.10     lukem 
    561      1.10     lukem 	*((struct netent **)rv) = np;
    562      1.10     lukem 	if (np == NULL) {
    563      1.10     lukem 		h_errno = HOST_NOT_FOUND;
    564      1.10     lukem 		return NS_NOTFOUND;
    565      1.10     lukem 	}
    566      1.10     lukem 	return NS_SUCCESS;
    567      1.10     lukem }
    568      1.10     lukem 
    569      1.10     lukem struct netent *
    570      1.30  christos _ypnetent(char *line)
    571      1.10     lukem {
    572      1.10     lukem 	char *cp, *p, **q;
    573      1.17     lukem 
    574      1.17     lukem 	_DIAGASSERT(line != NULL);
    575      1.10     lukem 
    576      1.10     lukem 	net_entry.n_name = line;
    577      1.10     lukem 	cp = strpbrk(line, " \t");
    578      1.10     lukem 	if (cp == NULL)
    579      1.30  christos 		return NULL;
    580      1.10     lukem 	*cp++ = '\0';
    581      1.10     lukem 	while (*cp == ' ' || *cp == '\t')
    582      1.10     lukem 		cp++;
    583      1.10     lukem 	p = strpbrk(cp, " \t");
    584      1.10     lukem 	if (p != NULL)
    585      1.10     lukem 		*p++ = '\0';
    586      1.10     lukem 	net_entry.n_net = inet_network(cp);
    587      1.29    kleink #if (defined(__sparc__) && defined(_LP64)) ||		\
    588      1.29    kleink     defined(__alpha__) ||				\
    589      1.29    kleink     (defined(__i386__) && defined(_LP64)) ||		\
    590      1.29    kleink     (defined(__sh__) && defined(_LP64))
    591      1.29    kleink 	net_entry.__n_pad0 = 0;
    592      1.29    kleink #endif
    593      1.10     lukem 	net_entry.n_addrtype = AF_INET;
    594      1.10     lukem 	q = net_entry.n_aliases = net_aliases;
    595      1.10     lukem 	if (p != NULL)  {
    596      1.10     lukem 		cp = p;
    597      1.10     lukem 		while (cp && *cp) {
    598      1.10     lukem 			if (*cp == ' ' || *cp == '\t') {
    599      1.10     lukem 				cp++;
    600      1.10     lukem 				continue;
    601      1.10     lukem 			}
    602      1.10     lukem 			if (q < &net_aliases[MAXALIASES - 1])
    603      1.10     lukem 				*q++ = cp;
    604      1.10     lukem 			cp = strpbrk(cp, " \t");
    605      1.10     lukem 			if (cp != NULL)
    606      1.10     lukem 				*cp++ = '\0';
    607      1.10     lukem 		}
    608       1.1       mrg 	}
    609      1.10     lukem 	*q = NULL;
    610      1.10     lukem 
    611      1.30  christos 	return &net_entry;
    612       1.1       mrg }
    613      1.10     lukem #endif
    614