Home | History | Annotate | Line # | Download | only in net
getnetnamadr.c revision 1.30
      1  1.30  christos /*	$NetBSD: getnetnamadr.c,v 1.30 2004/05/21 02:30:03 christos 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.30  christos __RCSID("$NetBSD: getnetnamadr.c,v 1.30 2004/05/21 02:30:03 christos 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.1       mrg 			net_entry.n_net = inet_network(paux2);
    232   1.1       mrg 			break;
    233   1.1       mrg 		}
    234  1.26    itojun 		if (strcasecmp(in, "IN-ADDR.ARPA") != 0)
    235  1.26    itojun 			goto next_alias;
    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.30  christos 	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
    326  1.30  christos 		h_errno = NETDB_INTERNAL;
    327  1.30  christos 		__res_put_state(res);
    328  1.30  christos 		return NS_NOTFOUND;
    329  1.30  christos 	}
    330  1.30  christos 	anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
    331   1.8     lukem 	if (anslen < 0) {
    332  1.24    itojun 		free(buf);
    333   1.8     lukem #ifdef DEBUG
    334  1.30  christos 		if (res->options & RES_DEBUG)
    335   1.8     lukem 			printf("res_query failed\n");
    336   1.8     lukem #endif
    337  1.30  christos 		__res_put_state(res);
    338   1.8     lukem 		return NS_NOTFOUND;
    339   1.8     lukem 	}
    340  1.30  christos 	__res_put_state(res);
    341  1.24    itojun 	np = getnetanswer(buf, anslen, BYADDR);
    342  1.24    itojun 	free(buf);
    343  1.10     lukem 	if (np) {
    344   1.8     lukem 		/* maybe net should be unsigned? */
    345   1.8     lukem 		unsigned long u_net = net;
    346   1.8     lukem 
    347   1.8     lukem 		/* Strip trailing zeros */
    348   1.8     lukem 		while ((u_net & 0xff) == 0 && u_net != 0)
    349   1.8     lukem 			u_net >>= 8;
    350  1.10     lukem 		np->n_net = u_net;
    351   1.8     lukem 	}
    352  1.10     lukem 	*((struct netent **)rv) = np;
    353  1.10     lukem 	if (np == NULL) {
    354   1.8     lukem 		h_errno = HOST_NOT_FOUND;
    355   1.8     lukem 		return NS_NOTFOUND;
    356   1.8     lukem 	}
    357   1.8     lukem 	return NS_SUCCESS;
    358   1.8     lukem }
    359   1.8     lukem 
    360  1.10     lukem 
    361   1.8     lukem struct netent *
    362  1.30  christos getnetbyaddr(uint32_t net, int net_type)
    363   1.8     lukem {
    364  1.10     lukem 	struct netent *np;
    365  1.12     lukem 	static const ns_dtab dtab[] = {
    366  1.16    kleink 		NS_FILES_CB(_files_getnetbyaddr, NULL)
    367   1.8     lukem 		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },	/* force -DHESIOD */
    368  1.11     lukem 		NS_NIS_CB(_yp_getnetbyaddr, NULL)
    369  1.11     lukem 		{ 0 }
    370   1.8     lukem 	};
    371   1.1       mrg 
    372  1.10     lukem 	np = NULL;
    373  1.10     lukem 	h_errno = NETDB_INTERNAL;
    374  1.11     lukem 	if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyaddr", __nsdefaultsrc,
    375  1.11     lukem 	    net, net_type) != NS_SUCCESS)
    376  1.30  christos 		return NULL;
    377  1.10     lukem 	h_errno = NETDB_SUCCESS;
    378  1.30  christos 	return np;
    379   1.8     lukem }
    380   1.3   thorpej 
    381  1.13  christos /*ARGSUSED*/
    382   1.8     lukem int
    383  1.30  christos _files_getnetbyname(void *rv, void *cb_data, va_list ap)
    384   1.8     lukem {
    385   1.8     lukem 	struct netent *p;
    386   1.8     lukem 	char **cp;
    387   1.8     lukem 	const char *name;
    388   1.8     lukem 
    389  1.17     lukem 	_DIAGASSERT(rv != NULL);
    390  1.17     lukem 
    391   1.8     lukem 	name = va_arg(ap, const char *);
    392   1.8     lukem 	setnetent(_net_stayopen);
    393   1.8     lukem 	while ((p = getnetent()) != NULL) {
    394   1.8     lukem 		if (strcasecmp(p->n_name, name) == 0)
    395   1.8     lukem 			break;
    396   1.8     lukem 		for (cp = p->n_aliases; *cp != 0; cp++)
    397   1.8     lukem 			if (strcasecmp(*cp, name) == 0)
    398   1.8     lukem 				goto found;
    399   1.8     lukem 	}
    400   1.8     lukem found:
    401   1.8     lukem 	if (!_net_stayopen)
    402   1.8     lukem 		endnetent();
    403   1.8     lukem 	*((struct netent **)rv) = p;
    404   1.8     lukem 	if (p==NULL) {
    405   1.8     lukem 		h_errno = HOST_NOT_FOUND;
    406   1.8     lukem 		return NS_NOTFOUND;
    407   1.8     lukem 	}
    408   1.8     lukem 	return NS_SUCCESS;
    409   1.8     lukem }
    410   1.8     lukem 
    411  1.13  christos /*ARGSUSED*/
    412   1.8     lukem int
    413  1.30  christos _dns_getnetbyname(void *rv, void *cb_data, va_list ap)
    414   1.8     lukem {
    415   1.9     lukem 	int anslen;
    416  1.24    itojun 	querybuf *buf;
    417   1.8     lukem 	char qbuf[MAXDNAME];
    418  1.10     lukem 	struct netent *np;
    419   1.8     lukem 	const char *net;
    420  1.30  christos 	res_state res;
    421   1.8     lukem 
    422  1.17     lukem 	_DIAGASSERT(rv != NULL);
    423  1.17     lukem 
    424   1.8     lukem 	net = va_arg(ap, const char *);
    425  1.25    itojun 	strlcpy(&qbuf[0], net, sizeof(qbuf));
    426  1.24    itojun 	buf = malloc(sizeof(*buf));
    427  1.24    itojun 	if (buf == NULL) {
    428  1.24    itojun 		h_errno = NETDB_INTERNAL;
    429  1.24    itojun 		return NS_NOTFOUND;
    430  1.24    itojun 	}
    431  1.30  christos 	res = __res_get_state();
    432  1.30  christos 	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
    433  1.30  christos 		h_errno = NETDB_INTERNAL;
    434  1.30  christos 		__res_put_state(res);
    435  1.30  christos 		return NS_NOTFOUND;
    436  1.30  christos 	}
    437  1.30  christos 
    438  1.30  christos 	anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf,
    439  1.30  christos 	    sizeof(buf->buf));
    440   1.8     lukem 	if (anslen < 0) {
    441  1.24    itojun 		free(buf);
    442   1.1       mrg #ifdef DEBUG
    443  1.30  christos 		if (res->options & RES_DEBUG)
    444  1.24    itojun 			printf("res_search failed\n");
    445   1.1       mrg #endif
    446  1.30  christos 		__res_put_state(res);
    447   1.8     lukem 		return NS_NOTFOUND;
    448   1.8     lukem 	}
    449  1.30  christos 	__res_put_state(res);
    450  1.24    itojun 	np = getnetanswer(buf, anslen, BYNAME);
    451  1.24    itojun 	free(buf);
    452  1.10     lukem 	*((struct netent **)rv) = np;
    453  1.10     lukem 	if (np == NULL) {
    454   1.8     lukem 		h_errno = HOST_NOT_FOUND;
    455   1.8     lukem 		return NS_NOTFOUND;
    456   1.1       mrg 	}
    457   1.8     lukem 	return NS_SUCCESS;
    458   1.1       mrg }
    459   1.1       mrg 
    460   1.1       mrg struct netent *
    461  1.30  christos getnetbyname(const char *net)
    462   1.1       mrg {
    463  1.10     lukem 	struct netent *np;
    464  1.12     lukem 	static const ns_dtab dtab[] = {
    465  1.16    kleink 		NS_FILES_CB(_files_getnetbyname, NULL)
    466   1.8     lukem 		{ NSSRC_DNS, _dns_getnetbyname, NULL },	/* force -DHESIOD */
    467  1.11     lukem 		NS_NIS_CB(_yp_getnetbyname, NULL)
    468  1.11     lukem 		{ 0 }
    469   1.8     lukem 	};
    470   1.1       mrg 
    471  1.17     lukem 	_DIAGASSERT(net != NULL);
    472  1.17     lukem 
    473  1.10     lukem 	np = NULL;
    474  1.10     lukem 	h_errno = NETDB_INTERNAL;
    475  1.11     lukem 	if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyname", __nsdefaultsrc,
    476  1.11     lukem 	    net) != NS_SUCCESS)
    477  1.30  christos 		return NULL;
    478  1.10     lukem 	h_errno = NETDB_SUCCESS;
    479  1.30  christos 	return np;
    480  1.10     lukem }
    481  1.10     lukem 
    482  1.10     lukem #ifdef YP
    483  1.15  christos /*ARGSUSED*/
    484  1.10     lukem int
    485  1.30  christos _yp_getnetbyaddr(void *rv, void *cb_data, va_list ap)
    486  1.10     lukem {
    487  1.10     lukem 	struct netent *np;
    488  1.10     lukem 	char qbuf[MAXDNAME];
    489  1.10     lukem 	unsigned int netbr[4];
    490  1.10     lukem 	unsigned long net, net2;
    491  1.10     lukem 	int type, r;
    492  1.10     lukem 
    493  1.17     lukem 	_DIAGASSERT(rv != NULL);
    494  1.17     lukem 
    495  1.10     lukem 	net = va_arg(ap, unsigned long);
    496  1.10     lukem 	type = va_arg(ap, int);
    497  1.10     lukem 
    498  1.10     lukem 	if (type != AF_INET)
    499  1.10     lukem 		return NS_UNAVAIL;
    500  1.10     lukem 
    501  1.10     lukem 	if (!__ypdomain) {
    502  1.10     lukem 		if (_yp_check(&__ypdomain) == 0)
    503  1.10     lukem 			return NS_UNAVAIL;
    504  1.10     lukem 	}
    505  1.10     lukem 	np = NULL;
    506  1.10     lukem 	if (__ypcurrent)
    507  1.10     lukem 		free(__ypcurrent);
    508  1.10     lukem 	__ypcurrent = NULL;
    509  1.10     lukem 	for (r = 4, net2 = net; net2; net2 >>= 8)
    510  1.10     lukem 		netbr[--r] = (unsigned int)(net2 & 0xff);
    511  1.10     lukem 	switch (r) {
    512  1.10     lukem 	default:
    513  1.10     lukem 		return NS_UNAVAIL;
    514  1.10     lukem 	case 3: 	/* Class A */
    515  1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
    516  1.10     lukem 		break;
    517  1.10     lukem 	case 2: 	/* Class B */
    518  1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
    519  1.10     lukem 		break;
    520  1.10     lukem 	case 1: 	/* Class C */
    521  1.28     lukem 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
    522  1.28     lukem 		    netbr[3]);
    523  1.10     lukem 		break;
    524  1.10     lukem 	case 0: 	/* Class D - E */
    525  1.23    itojun 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
    526  1.23    itojun 		    netbr[2], netbr[3]);
    527  1.10     lukem 		break;
    528  1.10     lukem 	}
    529  1.10     lukem 	r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
    530  1.10     lukem 	    &__ypcurrent, &__ypcurrentlen);
    531  1.10     lukem 	if (r == 0)
    532  1.10     lukem 		np = _ypnetent(__ypcurrent);
    533  1.10     lukem 
    534  1.10     lukem 	*((struct netent **)rv) = np;
    535  1.10     lukem 	if (np == NULL) {
    536  1.10     lukem 		h_errno = HOST_NOT_FOUND;
    537  1.10     lukem 		return NS_NOTFOUND;
    538  1.10     lukem 	}
    539  1.10     lukem 	return NS_SUCCESS;
    540  1.10     lukem 
    541  1.10     lukem }
    542  1.10     lukem 
    543  1.10     lukem int
    544  1.15  christos /*ARGSUSED*/
    545  1.30  christos _yp_getnetbyname(void *rv, void *cb_data, va_list ap)
    546  1.10     lukem {
    547  1.10     lukem 	struct netent *np;
    548  1.10     lukem 	const char *name;
    549  1.10     lukem 	int r;
    550  1.10     lukem 
    551  1.17     lukem 	_DIAGASSERT(rv != NULL);
    552  1.17     lukem 
    553  1.10     lukem 	name = va_arg(ap, const char *);
    554  1.10     lukem 
    555  1.10     lukem 	if (!__ypdomain) {
    556  1.10     lukem 		if (_yp_check(&__ypdomain) == 0)
    557  1.10     lukem 			return NS_UNAVAIL;
    558  1.10     lukem 	}
    559  1.10     lukem 	np = NULL;
    560  1.10     lukem 	if (__ypcurrent)
    561  1.10     lukem 		free(__ypcurrent);
    562  1.10     lukem 	__ypcurrent = NULL;
    563  1.10     lukem 	r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
    564  1.10     lukem 	    &__ypcurrent, &__ypcurrentlen);
    565  1.10     lukem 	if (r == 0)
    566  1.10     lukem 		np = _ypnetent(__ypcurrent);
    567  1.10     lukem 
    568  1.10     lukem 	*((struct netent **)rv) = np;
    569  1.10     lukem 	if (np == NULL) {
    570  1.10     lukem 		h_errno = HOST_NOT_FOUND;
    571  1.10     lukem 		return NS_NOTFOUND;
    572  1.10     lukem 	}
    573  1.10     lukem 	return NS_SUCCESS;
    574  1.10     lukem }
    575  1.10     lukem 
    576  1.10     lukem struct netent *
    577  1.30  christos _ypnetent(char *line)
    578  1.10     lukem {
    579  1.10     lukem 	char *cp, *p, **q;
    580  1.17     lukem 
    581  1.17     lukem 	_DIAGASSERT(line != NULL);
    582  1.10     lukem 
    583  1.10     lukem 	net_entry.n_name = line;
    584  1.10     lukem 	cp = strpbrk(line, " \t");
    585  1.10     lukem 	if (cp == NULL)
    586  1.30  christos 		return NULL;
    587  1.10     lukem 	*cp++ = '\0';
    588  1.10     lukem 	while (*cp == ' ' || *cp == '\t')
    589  1.10     lukem 		cp++;
    590  1.10     lukem 	p = strpbrk(cp, " \t");
    591  1.10     lukem 	if (p != NULL)
    592  1.10     lukem 		*p++ = '\0';
    593  1.10     lukem 	net_entry.n_net = inet_network(cp);
    594  1.29    kleink #if (defined(__sparc__) && defined(_LP64)) ||		\
    595  1.29    kleink     defined(__alpha__) ||				\
    596  1.29    kleink     (defined(__i386__) && defined(_LP64)) ||		\
    597  1.29    kleink     (defined(__sh__) && defined(_LP64))
    598  1.29    kleink 	net_entry.__n_pad0 = 0;
    599  1.29    kleink #endif
    600  1.10     lukem 	net_entry.n_addrtype = AF_INET;
    601  1.10     lukem 	q = net_entry.n_aliases = net_aliases;
    602  1.10     lukem 	if (p != NULL)  {
    603  1.10     lukem 		cp = p;
    604  1.10     lukem 		while (cp && *cp) {
    605  1.10     lukem 			if (*cp == ' ' || *cp == '\t') {
    606  1.10     lukem 				cp++;
    607  1.10     lukem 				continue;
    608  1.10     lukem 			}
    609  1.10     lukem 			if (q < &net_aliases[MAXALIASES - 1])
    610  1.10     lukem 				*q++ = cp;
    611  1.10     lukem 			cp = strpbrk(cp, " \t");
    612  1.10     lukem 			if (cp != NULL)
    613  1.10     lukem 				*cp++ = '\0';
    614  1.10     lukem 		}
    615   1.1       mrg 	}
    616  1.10     lukem 	*q = NULL;
    617  1.10     lukem 
    618  1.30  christos 	return &net_entry;
    619   1.1       mrg }
    620  1.10     lukem #endif
    621