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