pull nsswitch up to main branch

convert getnetby*() to nsswitch (from resolv.conf lookup)
This commit is contained in:
lukem 1999-01-16 07:48:23 +00:00
parent 0eb8645e3a
commit 2bf40fa435
6 changed files with 442 additions and 426 deletions

View File

@ -1,18 +1,17 @@
# $NetBSD: Makefile.inc,v 1.38 1999/01/15 12:53:23 lukem Exp $
# $NetBSD: Makefile.inc,v 1.39 1999/01/16 07:48:23 lukem Exp $
# @(#)Makefile.inc 8.2 (Berkeley) 9/5/93
# net sources
.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/net ${.CURDIR}/net
SRCS+= base64.c ethers.c gethnamaddr.c getnetbyaddr.c getnetbyname.c \
getnetnamadr.c getnetent.c getproto.c getprotoent.c getprotoname.c \
getservbyname.c getservbyport.c getservent.c herror.c hesiod.c \
inet_lnaof.c inet_makeaddr.c inet_net_ntop.c inet_net_pton.c \
inet_neta.c inet_ntop.c inet_pton.c inet_netof.c inet_network.c \
inet_ntoa.c iso_addr.c linkaddr.c nsdispatch.c nslexer.c nsparser.c \
ns_addr.c ns_ntoa.c nsap_addr.c rcmd.c recv.c res_comp.c res_data.c \
res_debug.c res_init.c res_mkquery.c res_query.c res_send.c send.c \
sethostent.c
SRCS+= base64.c ethers.c gethnamaddr.c getnetnamadr.c getnetent.c getproto.c \
getprotoent.c getprotoname.c getservbyname.c getservbyport.c \
getservent.c herror.c hesiod.c inet_lnaof.c inet_makeaddr.c \
inet_net_ntop.c inet_net_pton.c inet_neta.c inet_ntop.c inet_pton.c \
inet_netof.c inet_network.c inet_ntoa.c iso_addr.c linkaddr.c \
nsdispatch.c nslexer.c nsparser.c ns_addr.c ns_ntoa.c nsap_addr.c \
rcmd.c recv.c res_comp.c res_data.c res_debug.c res_init.c \
res_mkquery.c res_query.c res_send.c send.c sethostent.c
SRCS+= __dn_comp.c __res_close.c __res_send.c
CPPFLAGS+=-DRESOLVSORT -I.

View File

@ -1,4 +1,4 @@
/* $NetBSD: gethnamaddr.c,v 1.8 1998/11/13 15:46:53 christos Exp $ */
/* $NetBSD: gethnamaddr.c,v 1.9 1999/01/16 07:48:23 lukem Exp $ */
/*
* ++Copyright++ 1985, 1988, 1993
@ -61,7 +61,7 @@
static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
#else
__RCSID("$NetBSD: gethnamaddr.c,v 1.8 1998/11/13 15:46:53 christos Exp $");
__RCSID("$NetBSD: gethnamaddr.c,v 1.9 1999/01/16 07:48:23 lukem Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -88,6 +88,7 @@ __RCSID("$NetBSD: gethnamaddr.c,v 1.8 1998/11/13 15:46:53 christos Exp $");
#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
#include <nsswitch.h>
#include <stdlib.h>
#include <string.h>
@ -157,9 +158,7 @@ struct hostent *gethostbyaddr __P((const char *, int, int ));
void _sethtent __P((int));
void _endhtent __P((void));
struct hostent *_gethtent __P((void));
struct hostent *_gethtbyname __P((const char *));
struct hostent *_gethtbyname2 __P((const char *, int));
struct hostent *_gethtbyaddr __P((const char *, int, int ));
void ht_sethostent __P((int));
void ht_endhostent __P((void));
struct hostent *ht_gethostbyname __P((char *));
@ -167,10 +166,14 @@ struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
struct hostent *gethostent __P((void));
void dns_service __P((void));
int dn_skipname __P((const u_char *, const u_char *));
int _gethtbyaddr __P((void *, void *, va_list));
int _gethtbyname __P((void *, void *, va_list));
int _dns_gethtbyaddr __P((void *, void *, va_list));
int _dns_gethtbyname __P((void *, void *, va_list));
#ifdef YP
struct hostent *_yphostent __P((char *));
struct hostent *_yp_gethtbyaddr __P((const char *, int, int ));
struct hostent *_yp_gethtbyname __P((const char *));
struct hostent *_yphostent __P((char *, int));
int _yp_gethtbyaddr __P((void *, void *, va_list));
int _yp_gethtbyname __P((void *, void *, va_list));
#endif
@ -488,24 +491,23 @@ gethostbyname2(name, af)
const char *name;
int af;
{
querybuf buf;
const char *cp;
char *bp;
int n, size, type, len, i;
char lookups[MAXDNSLUS];
int size, len;
struct hostent *hp;
if ((_res.options & RES_INIT) == 0 && res_init() == -1)
return (_gethtbyname(name));
static ns_dtab dtab[] = {
NS_FILES_CB(_gethtbyname, NULL),
{ NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
NS_NIS_CB(_yp_gethtbyname, NULL),
{ NULL, NULL, NULL }
};
switch (af) {
case AF_INET:
size = INADDRSZ;
type = T_A;
break;
case AF_INET6:
size = IN6ADDRSZ;
type = T_AAAA;
break;
default:
h_errno = NETDB_INTERNAL;
@ -592,31 +594,12 @@ gethostbyname2(name, af)
break;
}
(void)memcpy(lookups, _res.lookups, sizeof lookups);
if (lookups[0] == '\0')
strncpy(lookups, "bf", sizeof lookups);
hp = (struct hostent *)NULL;
for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
switch (lookups[i]) {
#ifdef YP
case 'y':
hp = _yp_gethtbyname(name);
break;
#endif
case 'b':
if ((n = res_search(name, C_IN, T_A, buf.buf,
sizeof(buf))) < 0) {
dprintf("res_search failed (%d)\n", n);
break;
}
hp = getanswer(&buf, n, name, type);
break;
case 'f':
hp = _gethtbyname(name);
break;
}
if (nsdispatch(&hp, dtab, NSDB_HOSTS, name, len, af) != NS_SUCCESS) {
h_errno = HOST_NOT_FOUND;
return (struct hostent *)NULL;
}
h_errno = NETDB_SUCCESS;
return (hp);
}
@ -628,15 +611,15 @@ gethostbyaddr(addr, len, af)
const u_char *uaddr = (const u_char *)addr;
static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
int n, size, i;
querybuf buf;
int size;
struct hostent *hp;
char qbuf[MAXDNAME+1], *qp;
char lookups[MAXDNSLUS];
static ns_dtab dtab[] = {
NS_FILES_CB(_gethtbyaddr, NULL),
{ NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
NS_NIS_CB(_yp_gethtbyaddr, NULL),
{ NULL, NULL, NULL }
};
if ((_res.options & RES_INIT) == 0 && res_init() == -1)
return (_gethtbyaddr(addr, len, af));
if (af == AF_INET6 && len == IN6ADDRSZ &&
(!memcmp(uaddr, mapped, sizeof mapped) ||
!memcmp(uaddr, tunnelled, sizeof tunnelled))) {
@ -663,66 +646,10 @@ gethostbyaddr(addr, len, af)
h_errno = NETDB_INTERNAL;
return (NULL);
}
switch (af) {
case AF_INET:
(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
(uaddr[3] & 0xff),
(uaddr[2] & 0xff),
(uaddr[1] & 0xff),
(uaddr[0] & 0xff));
break;
case AF_INET6:
qp = qbuf;
for (n = IN6ADDRSZ - 1; n >= 0; n--) {
qp += sprintf(qp, "%x.%x.",
uaddr[n] & 0xf,
((u_int32_t)uaddr[n] >> 4) & 0xf);
}
strcpy(qp, "ip6.int");
break;
default:
abort();
}
(void)memcpy(lookups, _res.lookups, sizeof lookups);
if (lookups[0] == '\0')
strncpy(lookups, "bf", sizeof lookups);
hp = (struct hostent *)NULL;
for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
switch (lookups[i]) {
#ifdef YP
case 'y':
hp = _yp_gethtbyaddr(addr, len, af);
break;
#endif
case 'b':
n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
sizeof(buf));
if (n < 0) {
dprintf("res_query failed (%d)\n", n);
break;
}
hp = getanswer(&buf, n, qbuf, T_PTR);
if (hp == NULL)
break;
hp->h_addrtype = af;
hp->h_length = len;
(void)memcpy(host_addr, addr, (size_t)len);
h_addr_ptrs[0] = (char *)&host_addr;
h_addr_ptrs[1] = (char *)0;
if (af == AF_INET && (_res.options & RES_USE_INET6)) {
map_v4v6_address((char*)host_addr,
(char*)host_addr);
hp->h_addrtype = AF_INET6;
hp->h_length = IN6ADDRSZ;
}
h_errno = NETDB_SUCCESS;
break;
case 'f':
hp = _gethtbyaddr(addr, len, af);
break;
}
if (nsdispatch(&hp, dtab, NSDB_HOSTS, uaddr, len, af) != NS_SUCCESS) {
h_errno = HOST_NOT_FOUND;
return (struct hostent *)NULL;
}
return (hp);
}
@ -812,18 +739,31 @@ _gethtent()
return (&host);
}
struct hostent *
_gethtbyname(name)
const char *name;
int
_gethtbyname(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
struct hostent *hp;
const char *name;
int len, af;
if (_res.options & RES_USE_INET6) {
name = va_arg(ap, char *);
len = va_arg(ap, int);
af = va_arg(ap, int);
hp = NULL;
if (_res.options & RES_USE_INET6)
hp = _gethtbyname2(name, AF_INET6);
if (hp)
return (hp);
if (hp==NULL)
hp = _gethtbyname2(name, AF_INET);
*((struct hostent **)rv) = hp;
if (hp==NULL) {
h_errno = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return (_gethtbyname2(name, AF_INET));
return NS_SUCCESS;
}
struct hostent *
@ -849,12 +789,20 @@ _gethtbyname2(name, af)
return (p);
}
struct hostent *
_gethtbyaddr(addr, len, af)
const char *addr;
int len, af;
int
_gethtbyaddr(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
register struct hostent *p;
const unsigned char *addr;
int len, af;
addr = va_arg(ap, unsigned char *);
len = va_arg(ap, int);
af = va_arg(ap, int);
_sethtent(0);
while ((p = _gethtent()) != NULL)
@ -862,7 +810,12 @@ _gethtbyaddr(addr, len, af)
(size_t)len))
break;
_endhtent();
return (p);
*((struct hostent **)rv) = p;
if (p==NULL) {
h_errno = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
static void
@ -1009,10 +962,131 @@ dn_skipname(comp_dn, eom)
}
#endif /*old-style libc with yp junk in it*/
int
_dns_gethtbyname(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
querybuf buf;
int n, type;
struct hostent *hp;
const char *name;
int len, af;
name = va_arg(ap, char *);
len = va_arg(ap, int);
#ifdef __GNUC__ /* to shut up gcc warnings */
(void)&len;
#endif
af = va_arg(ap, int);
switch (af) {
case AF_INET:
type = T_A;
break;
case AF_INET6:
type = T_AAAA;
break;
default:
return NS_UNAVAIL;
}
if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
dprintf("res_search failed (%d)\n", n);
return NS_NOTFOUND;
}
hp = getanswer(&buf, n, name, type);
if (hp == NULL)
switch (h_errno) {
case HOST_NOT_FOUND:
return NS_NOTFOUND;
case TRY_AGAIN:
return NS_TRYAGAIN;
default:
return NS_UNAVAIL;
}
*((struct hostent **)rv) = hp;
return NS_SUCCESS;
}
int
_dns_gethtbyaddr(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
char qbuf[MAXDNAME + 1], *qp;
int n;
querybuf buf;
struct hostent *hp;
const unsigned char *uaddr;
int len, af;
uaddr = va_arg(ap, unsigned char *);
len = va_arg(ap, int);
#ifdef __GNUC__ /* to shut up gcc warnings */
(void)&len;
#endif
af = va_arg(ap, int);
switch (af) {
case AF_INET:
(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
(uaddr[3] & 0xff),
(uaddr[2] & 0xff),
(uaddr[1] & 0xff),
(uaddr[0] & 0xff));
break;
case AF_INET6:
qp = qbuf;
for (n = IN6ADDRSZ - 1; n >= 0; n--) {
qp += sprintf(qp, "%x.%x.",
uaddr[n] & 0xf,
(uaddr[n] >> 4) & 0xf);
}
strcpy(qp, "ip6.int");
break;
default:
abort();
}
n = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
if (n < 0) {
dprintf("res_query failed (%d)\n", n);
return NS_NOTFOUND;
}
hp = getanswer(&buf, n, qbuf, T_PTR);
if (hp == NULL)
switch (h_errno) {
case HOST_NOT_FOUND:
return NS_NOTFOUND;
case TRY_AGAIN:
return NS_TRYAGAIN;
default:
return NS_UNAVAIL;
}
hp->h_addrtype = af;
hp->h_length = len;
(void)memcpy(host_addr, (char *)uaddr, (size_t)len);
h_addr_ptrs[0] = (char *)&host_addr;
h_addr_ptrs[1] = (char *)0;
if (af == AF_INET && (_res.options & RES_USE_INET6)) {
map_v4v6_address((char*)host_addr, (char*)host_addr);
hp->h_addrtype = AF_INET6;
hp->h_length = IN6ADDRSZ;
}
*((struct hostent **)rv) = hp;
h_errno = NETDB_SUCCESS;
return NS_SUCCESS;
}
#ifdef YP
struct hostent *
_yphostent(line)
_yphostent(line, af)
char *line;
int af;
{
static struct in_addr host_addrs[MAXADDRS];
char *p = line;
@ -1029,6 +1103,9 @@ _yphostent(line)
buf = host_addrs;
q = host.h_aliases = host_aliases;
/*
* XXX: maybe support IPv6 parsing, based on 'af' setting
*/
nextline:
more = 0;
cp = strpbrk(p, " \t");
@ -1083,49 +1160,77 @@ done:
return (&host);
}
/* ARGSUSED */
struct hostent *
_yp_gethtbyaddr(addr, len, type)
const char *addr;
int len, type;
int
_yp_gethtbyaddr(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
struct hostent *hp = (struct hostent *)NULL;
static char *__ypcurrent;
int __ypcurrentlen, r;
char name[sizeof("xxx.xxx.xxx.xxx") + 1];
const unsigned char *uaddr;
int len, af;
uaddr = va_arg(ap, unsigned char *);
len = va_arg(ap, int);
#ifdef __GNUC__ /* to shut up gcc warnings */
(void)&len;
#endif
af = va_arg(ap, int);
if (!__ypdomain) {
if (_yp_check(&__ypdomain) == 0)
return (hp);
return NS_UNAVAIL;
}
/*
* XXX: based on the value of af, it would be possible to lookup
* IPv6 names in YP, by changing the following snprintf().
* Is it worth it?
*/
(void)snprintf(name, sizeof name, "%u.%u.%u.%u",
((unsigned)addr[0] & 0xff),
((unsigned)addr[1] & 0xff),
((unsigned)addr[2] & 0xff),
((unsigned)addr[3] & 0xff));
(uaddr[0] & 0xff),
(uaddr[1] & 0xff),
(uaddr[2] & 0xff),
(uaddr[3] & 0xff));
if (__ypcurrent)
free(__ypcurrent);
__ypcurrent = NULL;
r = yp_match(__ypdomain, "hosts.byaddr", name,
(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
if (r==0)
hp = _yphostent(__ypcurrent);
if (hp==NULL)
hp = _yphostent(__ypcurrent, af);
if (hp==NULL) {
h_errno = HOST_NOT_FOUND;
return (hp);
return NS_NOTFOUND;
}
*((struct hostent **)rv) = hp;
return NS_SUCCESS;
}
struct hostent *
_yp_gethtbyname(name)
const char *name;
int
_yp_gethtbyname(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
struct hostent *hp = (struct hostent *)NULL;
static char *__ypcurrent;
int __ypcurrentlen, r;
const char *name;
int len, af;
name = va_arg(ap, char *);
len = va_arg(ap, int);
#ifdef __GNUC__ /* to shut up gcc warnings */
(void)&len;
#endif
af = va_arg(ap, int);
if (!__ypdomain) {
if (_yp_check(&__ypdomain) == 0)
return (hp);
return NS_UNAVAIL;
}
if (__ypcurrent)
free(__ypcurrent);
@ -1133,9 +1238,12 @@ _yp_gethtbyname(name)
r = yp_match(__ypdomain, "hosts.byname", name,
(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
if (r==0)
hp = _yphostent(__ypcurrent);
if (hp==NULL)
hp = _yphostent(__ypcurrent, af);
if (hp==NULL) {
h_errno = HOST_NOT_FOUND;
return (hp);
return NS_NOTFOUND;
}
*((struct hostent **)rv) = hp;
return NS_SUCCESS;
}
#endif

View File

@ -1,70 +0,0 @@
/* $NetBSD: getnetbyaddr.c,v 1.13 1998/10/14 19:52:57 kleink Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid_[] = "from getnetbyaddr.c 1.1 (Coimbra) 93/06/02";
static char rcsid[] = "Id: getnetbyaddr.c,v 8.2 1996/05/09 05:59:13 vixie Exp ";
static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: getnetbyaddr.c,v 1.13 1998/10/14 19:52:57 kleink Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <netdb.h>
#include <stddef.h>
extern int _net_stayopen;
/* XXX private header! */
struct netent *__getnetbyaddr __P((unsigned long, int type));
struct netent *
__getnetbyaddr(net, type)
unsigned long net;
int type;
{
struct netent *p;
setnetent(_net_stayopen);
while ((p = getnetent()) != NULL)
if (p->n_addrtype == type && p->n_net == net)
break;
if (!_net_stayopen)
endnetent();
return (p);
}

View File

@ -1,75 +0,0 @@
/* $NetBSD: getnetbyname.c,v 1.11 1998/10/14 19:52:58 kleink Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid_[] = "from getnetbyname.c 1.1 (Coimbra) 93/06/02";
static char rcsid[] = "Id: getnetbyname.c,v 8.2 1995/06/19 08:35:01 vixie Exp ";
static char sccsid[] = "@(#)getnetbyname.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: getnetbyname.c,v 1.11 1998/10/14 19:52:58 kleink Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <netdb.h>
#include <string.h>
extern int _net_stayopen;
/* XXX private header! */
struct netent * __getnetbyname __P((const char *name));
struct netent *
__getnetbyname(name)
const char *name;
{
struct netent *p;
char **cp;
setnetent(_net_stayopen);
while ((p = getnetent()) != NULL) {
if (strcasecmp(p->n_name, name) == 0)
break;
for (cp = p->n_aliases; *cp != 0; cp++)
if (strcasecmp(*cp, name) == 0)
goto found;
}
found:
if (!_net_stayopen)
endnetent();
return (p);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: getnetnamadr.c,v 1.7 1998/12/05 13:17:55 pk Exp $ */
/* $NetBSD: getnetnamadr.c,v 1.8 1999/01/16 07:48:24 lukem Exp $ */
/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
* Dep. Matematica Universidade de Coimbra, Portugal, Europe
@ -47,7 +47,7 @@ static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
#else
__RCSID("$NetBSD: getnetnamadr.c,v 1.7 1998/12/05 13:17:55 pk Exp $");
__RCSID("$NetBSD: getnetnamadr.c,v 1.8 1999/01/16 07:48:24 lukem Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -59,11 +59,12 @@ __RCSID("$NetBSD: getnetnamadr.c,v 1.7 1998/12/05 13:17:55 pk Exp $");
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <stdio.h>
#include <netdb.h>
#include <resolv.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <nsswitch.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#ifdef __weak_alias
@ -72,14 +73,16 @@ __weak_alias(getnetbyname,_getnetbyname);
#endif
extern int h_errno;
extern int _net_stayopen;
#if defined(mips) && defined(SYSTYPE_BSD43)
extern int errno;
#endif
/* XXX private header! */
struct netent *__getnetbyaddr __P((unsigned long net, int type));
struct netent *__getnetbyname __P((const char *name));
int _getnetbyaddr __P((void *, void *, va_list));
int _getnetbyname __P((void *, void *, va_list));
int _dns_getnetbyaddr __P((void *, void *, va_list));
int _dns_getnetbyname __P((void *, void *, va_list));
#define BYADDR 0
#define BYNAME 1
@ -216,10 +219,38 @@ static char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
return (NULL);
}
struct netent *
getnetbyaddr(net, net_type)
register u_long net;
register int net_type;
int
_getnetbyaddr(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
struct netent *p;
unsigned long net;
int type;
net = va_arg(ap, unsigned long);
type = va_arg(ap, int);
setnetent(_net_stayopen);
while ((p = getnetent()) != NULL)
if (p->n_addrtype == type && p->n_net == net)
break;
if (!_net_stayopen)
endnetent();
*((struct netent **)rv) = p;
if (p==NULL) {
h_errno = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
int
_dns_getnetbyaddr(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
unsigned int netbr[4];
int nn, anslen, i;
@ -227,118 +258,171 @@ getnetbyaddr(net, net_type)
char qbuf[MAXDNAME];
unsigned long net2;
struct netent *net_entry;
char lookups[MAXDNSLUS];
unsigned long net;
int type;
if (net_type != AF_INET)
return (__getnetbyaddr(net, net_type));
net = va_arg(ap, unsigned long);
type = va_arg(ap, int);
if (type != AF_INET)
return NS_UNAVAIL;
for (nn = 4, net2 = net; net2; net2 >>= 8)
netbr[--nn] = (unsigned int)(net2 & 0xff);
switch (nn) {
default:
return NS_UNAVAIL;
case 3: /* Class A */
sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
break;
case 2: /* Class B */
sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
break;
case 1: /* Class C */
sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
netbr[1]);
break;
case 0: /* Class D - E */
sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
netbr[1], netbr[0]);
break;
}
anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
sizeof(buf));
if (anslen < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query failed\n");
#endif
return NS_NOTFOUND;
}
net_entry = getnetanswer(&buf, anslen, BYADDR);
if (net_entry) {
/* maybe net should be unsigned? */
unsigned long u_net = net;
/* Strip trailing zeros */
while ((u_net & 0xff) == 0 && u_net != 0)
u_net >>= 8;
net_entry->n_net = u_net;
}
*((struct netent **)rv) = net_entry;
if (net_entry == NULL) {
h_errno = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
struct netent *
getnetbyaddr(net, net_type)
register u_long net;
register int net_type;
{
struct netent *net_entry;
static ns_dtab dtab[] = {
NS_FILES_CB(_getnetbyaddr, NULL),
{ NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */
{ NULL, NULL, NULL }
};
if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
h_errno = NETDB_INTERNAL;
return (NULL);
}
memcpy(lookups, _res.lookups, sizeof(lookups));
if (lookups[0] == '\0')
strncpy(lookups, "bf", sizeof(lookups));
net_entry = NULL;
for (i = 0; i < MAXDNSLUS && net_entry == NULL && lookups[i]; i++) {
switch (lookups[i]) {
case 'b':
for (nn = 4, net2 = net; net2; net2 >>= 8)
netbr[--nn] = (unsigned int)(net2 & 0xff);
switch (nn) {
default:
return (NULL);
case 3: /* Class A */
sprintf(qbuf, "0.0.0.%u.in-addr.arpa",
netbr[3]);
break;
case 2: /* Class B */
sprintf(qbuf, "0.0.%u.%u.in-addr.arpa",
netbr[3], netbr[2]);
break;
case 1: /* Class C */
sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa",
netbr[3], netbr[2], netbr[1]);
break;
case 0: /* Class D - E */
sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
netbr[3], netbr[2], netbr[1], netbr[0]);
break;
}
anslen = res_query(qbuf, C_IN, T_PTR,
(u_char *)(void *)&buf, sizeof(buf));
if (anslen < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query failed\n");
#endif
break;
}
net_entry = getnetanswer(&buf, anslen, BYADDR);
if (net_entry) {
/* maybe net should be unsigned? */
unsigned long u_net = net;
/* Strip trailing zeros */
while ((u_net & 0xff) == 0 && u_net != 0)
u_net >>= 8;
net_entry->n_net = u_net;
break;
}
break;
case 'f':
net_entry = __getnetbyaddr(net, net_type);
break;
}
if (nsdispatch(&net_entry, dtab, NSDB_NETWORKS, net, net_type) !=
NS_SUCCESS) {
h_errno = NETDB_INTERNAL;
return (NULL);
}
return (net_entry);
}
int
_getnetbyname(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
struct netent *p;
char **cp;
const char *name;
name = va_arg(ap, const char *);
setnetent(_net_stayopen);
while ((p = getnetent()) != NULL) {
if (strcasecmp(p->n_name, name) == 0)
break;
for (cp = p->n_aliases; *cp != 0; cp++)
if (strcasecmp(*cp, name) == 0)
goto found;
}
found:
if (!_net_stayopen)
endnetent();
*((struct netent **)rv) = p;
if (p==NULL) {
h_errno = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
int
_dns_getnetbyname(rv, cb_data, ap)
void *rv;
void *cb_data;
va_list ap;
{
int anslen, i;
querybuf buf;
char qbuf[MAXDNAME];
struct netent *net_entry;
const char *net;
net = va_arg(ap, const char *);
strcpy(&qbuf[0], net);
anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
sizeof(buf));
if (anslen < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query failed\n");
#endif
return NS_NOTFOUND;
}
net_entry = getnetanswer(&buf, anslen, BYNAME);
*((struct netent **)rv) = net_entry;
if (net_entry == NULL) {
h_errno = HOST_NOT_FOUND;
return NS_NOTFOUND;
}
return NS_SUCCESS;
}
struct netent *
getnetbyname(net)
register const char *net;
{
int anslen, i;
querybuf buf;
char qbuf[MAXDNAME];
struct netent *net_entry;
char lookups[MAXDNSLUS];
static ns_dtab dtab[] = {
NS_FILES_CB(_getnetbyname, NULL),
{ NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */
{ NULL, NULL, NULL }
};
if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
h_errno = NETDB_INTERNAL;
return (NULL);
}
memcpy(lookups, _res.lookups, sizeof(lookups));
if (lookups[0] == '\0')
strncpy(lookups, "bf", sizeof(lookups));
net_entry = NULL;
for (i = 0; i < MAXDNSLUS && net_entry == NULL && lookups[i]; i++) {
switch (lookups[i]) {
case 'b':
strcpy(&qbuf[0], net);
anslen = res_search(qbuf, C_IN, T_PTR,
(u_char *)(void *)&buf, sizeof(buf));
if (anslen < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query failed\n");
#endif
break;
}
net_entry = getnetanswer(&buf, anslen, BYNAME);
break;
case 'f':
net_entry = __getnetbyname(net);
break;
}
if (nsdispatch(&net_entry, dtab, NSDB_NETWORKS, net) != NS_SUCCESS) {
h_errno = NETDB_INTERNAL;
return (NULL);
}
return (net_entry);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: res_init.c,v 1.22 1998/11/13 15:46:57 christos Exp $ */
/* $NetBSD: res_init.c,v 1.23 1999/01/16 07:48:24 lukem Exp $ */
/*-
* Copyright (c) 1985, 1989, 1993
@ -59,7 +59,7 @@
static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
static char rcsid[] = "Id: res_init.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
#else
__RCSID("$NetBSD: res_init.c,v 1.22 1998/11/13 15:46:57 christos Exp $");
__RCSID("$NetBSD: res_init.c,v 1.23 1999/01/16 07:48:24 lukem Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -147,8 +147,6 @@ res_init()
_res.nscount = 1;
_res.ndots = 1;
_res.pfcode = 0;
_res.lookups[0] = 'f';
_res.lookups[1] = '\0';
/* Allow user to override the local domain definition */
if ((cp = getenv("LOCALDOMAIN")) != NULL) {
@ -192,10 +190,6 @@ res_init()
line[sizeof(name) - 1] == '\t'))
if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
_res.lookups[0] = 'b';
_res.lookups[1] = 'f';
_res.lookups[2] = '\0';
/* read the config file */
while (fgets(buf, sizeof(buf), fp) != NULL) {
/* skip comments */
@ -217,33 +211,9 @@ res_init()
havesearch = 0;
continue;
}
/* lookup types */
if (!strncmp(buf, "lookup", sizeof("lookup") -1)) {
char *sp = NULL;
(void)memset(_res.lookups, 0, sizeof _res.lookups);
cp = buf + sizeof("lookup") - 1;
for (n = 0;; cp++) {
if (n == MAXDNSLUS)
break;
if ((*cp == '\0') || (*cp == '\n')) {
if (sp) {
if (*sp=='y' || *sp=='b' || *sp=='f')
_res.lookups[n++] = *sp;
sp = NULL;
}
break;
} else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) {
if (sp) {
if (*sp=='y' || *sp=='b' || *sp=='f')
_res.lookups[n++] = *sp;
sp = NULL;
}
} else if (sp == NULL)
sp = cp;
}
/* lookup types; deprecated in favour of nsswitch.conf(5) */
if (MATCH(buf, "lookup"))
continue;
}
/* set search list */
if (MATCH(buf, "search")) {
if (haveenv) /* skip if have from environ */