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