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