Convert getnetbyname(3) and getnetbyaddr(3) to the more "standard"
nsdispatch() backend method API.
This commit is contained in:
parent
ffc24fca06
commit
2713f76747
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: getnetnamadr.c,v 1.39 2008/05/08 12:26:55 lukem Exp $ */
|
||||
/* $NetBSD: getnetnamadr.c,v 1.40 2008/05/08 13:01:42 lukem Exp $ */
|
||||
|
||||
/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
|
||||
* Dep. Matematica Universidade de Coimbra, Portugal, Europe
|
||||
|
@ -43,7 +43,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.39 2008/05/08 12:26:55 lukem Exp $");
|
||||
__RCSID("$NetBSD: getnetnamadr.c,v 1.40 2008/05/08 13:01:42 lukem Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
|
@ -104,15 +104,16 @@ static int __ypcurrentlen;
|
|||
static struct netent net_entry;
|
||||
static char *net_aliases[MAXALIASES];
|
||||
|
||||
static struct netent *getnetanswer(querybuf *, int, int);
|
||||
int _files_getnetbyaddr(void *, void *, va_list);
|
||||
int _files_getnetbyname(void *, void *, va_list);
|
||||
int _dns_getnetbyaddr(void *, void *, va_list);
|
||||
int _dns_getnetbyname(void *, void *, va_list);
|
||||
static int parse_reversed_addr(const char *, in_addr_t *);
|
||||
static struct netent *getnetanswer(querybuf *, int, int);
|
||||
static int _files_getnetbyaddr(void *, void *, va_list);
|
||||
static int _files_getnetbyname(void *, void *, va_list);
|
||||
static int _dns_getnetbyaddr(void *, void *, va_list);
|
||||
static int _dns_getnetbyname(void *, void *, va_list);
|
||||
#ifdef YP
|
||||
int _yp_getnetbyaddr(void *, void *, va_list);
|
||||
int _yp_getnetbyname(void *, void *, va_list);
|
||||
struct netent *_ypnetent(char *);
|
||||
static int _yp_getnetbyaddr(void *, void *, va_list);
|
||||
static int _yp_getnetbyname(void *, void *, va_list);
|
||||
static struct netent *_ypnetent(char *);
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
@ -160,14 +161,15 @@ parse_reversed_addr(const char *str, in_addr_t *result)
|
|||
static struct netent *
|
||||
getnetanswer(querybuf *answer, int anslen, int net_i)
|
||||
{
|
||||
HEADER *hp;
|
||||
u_char *cp;
|
||||
int n;
|
||||
u_char *eom;
|
||||
int type, class, ancount, qdcount, haveanswer;
|
||||
char *in, *bp, **ap, *ep;
|
||||
static char n_name[MAXDNAME];
|
||||
static char netbuf[PACKETSZ];
|
||||
static char n_name[MAXDNAME];
|
||||
static char netbuf[PACKETSZ];
|
||||
|
||||
HEADER *hp;
|
||||
u_char *cp;
|
||||
int n;
|
||||
u_char *eom;
|
||||
int type, class, ancount, qdcount, haveanswer;
|
||||
char *in, *bp, **ap, *ep;
|
||||
|
||||
_DIAGASSERT(answer != NULL);
|
||||
|
||||
|
@ -270,50 +272,46 @@ getnetanswer(querybuf *answer, int anslen, int net_i)
|
|||
}
|
||||
|
||||
/*ARGSUSED*/
|
||||
int
|
||||
_files_getnetbyaddr(void *rv, void *cb_data, va_list ap)
|
||||
static int
|
||||
_files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
|
||||
{
|
||||
struct netent *p;
|
||||
uint32_t net;
|
||||
int type;
|
||||
struct netent **retval = va_arg(ap, struct netent **);
|
||||
uint32_t net = va_arg(ap, uint32_t);
|
||||
int type = va_arg(ap, int);
|
||||
|
||||
_DIAGASSERT(rv != NULL);
|
||||
|
||||
net = va_arg(ap, uint32_t);
|
||||
type = va_arg(ap, int);
|
||||
struct netent *np;
|
||||
|
||||
setnetent(_net_stayopen);
|
||||
while ((p = getnetent()) != NULL)
|
||||
if (p->n_addrtype == type && p->n_net == net)
|
||||
while ((np = getnetent()) != NULL)
|
||||
if (np->n_addrtype == type && np->n_net == net)
|
||||
break;
|
||||
if (!_net_stayopen)
|
||||
endnetent();
|
||||
*((struct netent **)rv) = p;
|
||||
if (p==NULL) {
|
||||
|
||||
if (np != NULL) {
|
||||
*retval = np;
|
||||
return NS_SUCCESS;
|
||||
} else {
|
||||
h_errno = HOST_NOT_FOUND;
|
||||
return NS_NOTFOUND;
|
||||
}
|
||||
return NS_SUCCESS;
|
||||
}
|
||||
|
||||
/*ARGSUSED*/
|
||||
int
|
||||
_dns_getnetbyaddr(void *rv, void *cb_data, va_list ap)
|
||||
static int
|
||||
_dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap)
|
||||
{
|
||||
unsigned int netbr[4];
|
||||
int nn, anslen;
|
||||
querybuf *buf;
|
||||
char qbuf[MAXDNAME];
|
||||
uint32_t net2;
|
||||
struct netent *np;
|
||||
uint32_t net;
|
||||
int type;
|
||||
res_state res;
|
||||
struct netent **retval = va_arg(ap, struct netent **);
|
||||
uint32_t net = va_arg(ap, uint32_t);
|
||||
int type = va_arg(ap, int);
|
||||
|
||||
_DIAGASSERT(rv != NULL);
|
||||
|
||||
net = va_arg(ap, uint32_t);
|
||||
type = va_arg(ap, int);
|
||||
unsigned int netbr[4];
|
||||
int nn, anslen;
|
||||
querybuf *buf;
|
||||
char qbuf[MAXDNAME];
|
||||
uint32_t net2;
|
||||
struct netent *np;
|
||||
res_state res;
|
||||
|
||||
if (type != AF_INET)
|
||||
return NS_UNAVAIL;
|
||||
|
@ -369,19 +367,22 @@ _dns_getnetbyaddr(void *rv, void *cb_data, va_list ap)
|
|||
u_net >>= 8;
|
||||
np->n_net = u_net;
|
||||
}
|
||||
*((struct netent **)rv) = np;
|
||||
if (np == NULL) {
|
||||
|
||||
if (np != NULL) {
|
||||
*retval = np;
|
||||
return NS_SUCCESS;
|
||||
} else {
|
||||
h_errno = HOST_NOT_FOUND;
|
||||
return NS_NOTFOUND;
|
||||
}
|
||||
return NS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
struct netent *
|
||||
getnetbyaddr(uint32_t net, int net_type)
|
||||
{
|
||||
struct netent *np;
|
||||
int rv;
|
||||
struct netent *retval;
|
||||
|
||||
static const ns_dtab dtab[] = {
|
||||
NS_FILES_CB(_files_getnetbyaddr, NULL)
|
||||
{ NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */
|
||||
|
@ -389,60 +390,62 @@ getnetbyaddr(uint32_t net, int net_type)
|
|||
NS_NULL_CB
|
||||
};
|
||||
|
||||
np = NULL;
|
||||
retval = NULL;
|
||||
h_errno = NETDB_INTERNAL;
|
||||
if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyaddr", __nsdefaultsrc,
|
||||
net, net_type) != NS_SUCCESS)
|
||||
return NULL;
|
||||
h_errno = NETDB_SUCCESS;
|
||||
return np;
|
||||
rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr",
|
||||
__nsdefaultsrc, &retval, net, net_type);
|
||||
if (rv == NS_SUCCESS) {
|
||||
h_errno = NETDB_SUCCESS;
|
||||
return retval;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*ARGSUSED*/
|
||||
int
|
||||
_files_getnetbyname(void *rv, void *cb_data, va_list ap)
|
||||
static int
|
||||
_files_getnetbyname(void *cbrv, void *cbdata, va_list ap)
|
||||
{
|
||||
struct netent *p;
|
||||
char **cp;
|
||||
const char *name;
|
||||
struct netent **retval = va_arg(ap, struct netent **);
|
||||
const char *name = va_arg(ap, const char *);
|
||||
|
||||
_DIAGASSERT(rv != NULL);
|
||||
struct netent *np;
|
||||
char **cp;
|
||||
|
||||
name = va_arg(ap, const char *);
|
||||
setnetent(_net_stayopen);
|
||||
while ((p = getnetent()) != NULL) {
|
||||
if (strcasecmp(p->n_name, name) == 0)
|
||||
while ((np = getnetent()) != NULL) {
|
||||
if (strcasecmp(np->n_name, name) == 0)
|
||||
break;
|
||||
for (cp = p->n_aliases; *cp != 0; cp++)
|
||||
for (cp = np->n_aliases; *cp != 0; cp++)
|
||||
if (strcasecmp(*cp, name) == 0)
|
||||
goto found;
|
||||
}
|
||||
found:
|
||||
if (!_net_stayopen)
|
||||
endnetent();
|
||||
*((struct netent **)rv) = p;
|
||||
if (p==NULL) {
|
||||
|
||||
if (np != NULL) {
|
||||
*retval = np;
|
||||
return NS_SUCCESS;
|
||||
} else {
|
||||
h_errno = HOST_NOT_FOUND;
|
||||
return NS_NOTFOUND;
|
||||
}
|
||||
return NS_SUCCESS;
|
||||
}
|
||||
|
||||
/*ARGSUSED*/
|
||||
int
|
||||
_dns_getnetbyname(void *rv, void *cb_data, va_list ap)
|
||||
static int
|
||||
_dns_getnetbyname(void *cbrv, void *cbdata, va_list ap)
|
||||
{
|
||||
int anslen;
|
||||
querybuf *buf;
|
||||
char qbuf[MAXDNAME];
|
||||
struct netent *np;
|
||||
const char *net;
|
||||
res_state res;
|
||||
struct netent **retval = va_arg(ap, struct netent **);
|
||||
const char *name = va_arg(ap, const char *);
|
||||
|
||||
_DIAGASSERT(rv != NULL);
|
||||
int anslen;
|
||||
querybuf *buf;
|
||||
char qbuf[MAXDNAME];
|
||||
struct netent *np;
|
||||
res_state res;
|
||||
|
||||
net = va_arg(ap, const char *);
|
||||
strlcpy(&qbuf[0], net, sizeof(qbuf));
|
||||
strlcpy(&qbuf[0], name, sizeof(qbuf));
|
||||
buf = malloc(sizeof(*buf));
|
||||
if (buf == NULL) {
|
||||
h_errno = NETDB_INTERNAL;
|
||||
|
@ -467,18 +470,22 @@ _dns_getnetbyname(void *rv, void *cb_data, va_list ap)
|
|||
__res_put_state(res);
|
||||
np = getnetanswer(buf, anslen, BYNAME);
|
||||
free(buf);
|
||||
*((struct netent **)rv) = np;
|
||||
if (np == NULL) {
|
||||
|
||||
if (np != NULL) {
|
||||
*retval = np;
|
||||
return NS_SUCCESS;
|
||||
} else {
|
||||
h_errno = HOST_NOT_FOUND;
|
||||
return NS_NOTFOUND;
|
||||
}
|
||||
return NS_SUCCESS;
|
||||
}
|
||||
|
||||
struct netent *
|
||||
getnetbyname(const char *net)
|
||||
getnetbyname(const char *name)
|
||||
{
|
||||
struct netent *np;
|
||||
int rv;
|
||||
struct netent *retval;
|
||||
|
||||
static const ns_dtab dtab[] = {
|
||||
NS_FILES_CB(_files_getnetbyname, NULL)
|
||||
{ NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */
|
||||
|
@ -486,32 +493,33 @@ getnetbyname(const char *net)
|
|||
NS_NULL_CB
|
||||
};
|
||||
|
||||
_DIAGASSERT(net != NULL);
|
||||
_DIAGASSERT(name != NULL);
|
||||
|
||||
np = NULL;
|
||||
retval = NULL;
|
||||
h_errno = NETDB_INTERNAL;
|
||||
if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyname", __nsdefaultsrc,
|
||||
net) != NS_SUCCESS)
|
||||
return NULL;
|
||||
h_errno = NETDB_SUCCESS;
|
||||
return np;
|
||||
rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname",
|
||||
__nsdefaultsrc, &retval, name);
|
||||
if (rv == NS_SUCCESS) {
|
||||
h_errno = NETDB_SUCCESS;
|
||||
return retval;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef YP
|
||||
/*ARGSUSED*/
|
||||
int
|
||||
_yp_getnetbyaddr(void *rv, void *cb_data, va_list ap)
|
||||
static int
|
||||
_yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap)
|
||||
{
|
||||
struct netent *np;
|
||||
char qbuf[MAXDNAME];
|
||||
unsigned int netbr[4];
|
||||
uint32_t net, net2;
|
||||
int type, r;
|
||||
struct netent **retval = va_arg(ap, struct netent **);
|
||||
uint32_t net = va_arg(ap, uint32_t);
|
||||
int type = va_arg(ap, int);
|
||||
|
||||
_DIAGASSERT(rv != NULL);
|
||||
|
||||
net = va_arg(ap, uint32_t);
|
||||
type = va_arg(ap, int);
|
||||
struct netent *np;
|
||||
char qbuf[MAXDNAME];
|
||||
unsigned int netbr[4];
|
||||
uint32_t net2;
|
||||
int r;
|
||||
|
||||
if (type != AF_INET)
|
||||
return NS_UNAVAIL;
|
||||
|
@ -549,26 +557,24 @@ _yp_getnetbyaddr(void *rv, void *cb_data, va_list ap)
|
|||
if (r == 0)
|
||||
np = _ypnetent(__ypcurrent);
|
||||
|
||||
*((struct netent **)rv) = np;
|
||||
if (np == NULL) {
|
||||
if (np != NULL) {
|
||||
*retval = np;
|
||||
return NS_SUCCESS;
|
||||
} else {
|
||||
h_errno = HOST_NOT_FOUND;
|
||||
return NS_NOTFOUND;
|
||||
}
|
||||
return NS_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
/*ARGSUSED*/
|
||||
_yp_getnetbyname(void *rv, void *cb_data, va_list ap)
|
||||
static int
|
||||
_yp_getnetbyname(void *cbrv, void *cbdata, va_list ap)
|
||||
{
|
||||
struct netent *np;
|
||||
const char *name;
|
||||
int r;
|
||||
struct netent **retval = va_arg(ap, struct netent **);
|
||||
const char *name = va_arg(ap, const char *);
|
||||
|
||||
_DIAGASSERT(rv != NULL);
|
||||
|
||||
name = va_arg(ap, const char *);
|
||||
struct netent *np;
|
||||
int r;
|
||||
|
||||
if (!__ypdomain) {
|
||||
if (_yp_check(&__ypdomain) == 0)
|
||||
|
@ -583,15 +589,16 @@ _yp_getnetbyname(void *rv, void *cb_data, va_list ap)
|
|||
if (r == 0)
|
||||
np = _ypnetent(__ypcurrent);
|
||||
|
||||
*((struct netent **)rv) = np;
|
||||
if (np == NULL) {
|
||||
if (np != NULL) {
|
||||
*retval = np;
|
||||
return NS_SUCCESS;
|
||||
} else {
|
||||
h_errno = HOST_NOT_FOUND;
|
||||
return NS_NOTFOUND;
|
||||
}
|
||||
return NS_SUCCESS;
|
||||
}
|
||||
|
||||
struct netent *
|
||||
static struct netent *
|
||||
_ypnetent(char *line)
|
||||
{
|
||||
char *cp, *p, **q;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
.\" $NetBSD: nsdispatch.3,v 1.27 2008/04/30 13:10:50 martin Exp $
|
||||
.\" $NetBSD: nsdispatch.3,v 1.28 2008/05/08 13:01:42 lukem Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1997, 1998, 1999, 2004, 2005, 2008
|
||||
.\" The NetBSD Foundation, Inc.
|
||||
|
@ -28,7 +28,7 @@
|
|||
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
.\" POSSIBILITY OF SUCH DAMAGE.
|
||||
.\"
|
||||
.Dd April 19, 2008
|
||||
.Dd May 8, 2008
|
||||
.Dt NSDISPATCH 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
|
@ -691,24 +691,46 @@ should be set to 1 for a successful match and 0 otherwise.
|
|||
.El
|
||||
.\"
|
||||
.Ss Methods for networks database
|
||||
.Sy NOTE:
|
||||
The method APIs for this database will be changing in the near future.
|
||||
.Bl -tag -width 3n
|
||||
.It Sy getnetbyaddr
|
||||
.Ft "struct netent **retval" ,
|
||||
.Ft "uint32_t net" ,
|
||||
.Ft "int type"
|
||||
.Pp
|
||||
Returns
|
||||
.Fa *retval
|
||||
should be set to a pointer to an internal static
|
||||
.Ft "struct netent"
|
||||
via
|
||||
.Ft "void *cbrv" .
|
||||
.It Sy getnetbyname
|
||||
.Ft "const char *net"
|
||||
on success,
|
||||
.Dv NULL otherwise.
|
||||
.Pp
|
||||
Returns
|
||||
.Xr getnetbyaddr 3
|
||||
returns
|
||||
.Fa *retval
|
||||
if
|
||||
.Fn nsdispatch
|
||||
returns
|
||||
.Dv NS_SUCCESS ,
|
||||
.Dv NULL
|
||||
otherwise.
|
||||
.It Sy getnetbyname
|
||||
.Ft "struct netent **retval" ,
|
||||
.Ft "const char *name"
|
||||
.Pp
|
||||
.Fa *retval
|
||||
should be set to a pointer to an internal static
|
||||
.Ft "struct netent"
|
||||
via
|
||||
.Ft "void *cbrv" .
|
||||
on success,
|
||||
.Dv NULL otherwise.
|
||||
.Pp
|
||||
.Xr getnetbyname 3
|
||||
returns
|
||||
.Fa *retval
|
||||
if
|
||||
.Fn nsdispatch
|
||||
returns
|
||||
.Dv NS_SUCCESS ,
|
||||
.Dv NULL
|
||||
otherwise.
|
||||
.El
|
||||
.\"
|
||||
.Ss Methods for passwd and passwd_compat databases
|
||||
|
|
Loading…
Reference in New Issue