diff options
Diffstat (limited to 'contrib/bind/lib/irs/getnetent.c')
-rw-r--r-- | contrib/bind/lib/irs/getnetent.c | 250 |
1 files changed, 159 insertions, 91 deletions
diff --git a/contrib/bind/lib/irs/getnetent.c b/contrib/bind/lib/irs/getnetent.c index 17132f62f362..b63ddaf3c82e 100644 --- a/contrib/bind/lib/irs/getnetent.c +++ b/contrib/bind/lib/irs/getnetent.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996 by Internet Software Consortium. + * Copyright (c) 1996,1999 by Internet Software Consortium. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -16,15 +16,18 @@ */ #if !defined(LINT) && !defined(CODECENTER) -static char rcsid[] = "$Id: getnetent.c,v 1.10 1997/12/04 04:57:53 halley Exp $"; +static const char rcsid[] = "$Id: getnetent.c,v 1.17 1999/10/13 16:39:30 vixie Exp $"; #endif /* Imports */ #include "port_before.h" +#if !defined(__BIND_NOSTATIC) + #include <sys/types.h> #include <sys/socket.h> + #include <netinet/in.h> #include <arpa/nameser.h> #include <arpa/inet.h> @@ -32,6 +35,7 @@ static char rcsid[] = "$Id: getnetent.c,v 1.10 1997/12/04 04:57:53 halley Exp $" #include <ctype.h> #include <errno.h> #include <netdb.h> +#include <resolv.h> #include <stdlib.h> #include <string.h> @@ -52,10 +56,10 @@ struct pvt { /* Forward */ -static struct irs_nw * init(void); -static struct netent * nw_to_net(struct nwent *); -static void freepvt(void); -static struct netent * fakeaddr(const char *, int af); +static struct net_data *init(void); +static struct netent *nw_to_net(struct nwent *, struct net_data *); +static void freepvt(struct net_data *); +static struct netent *fakeaddr(const char *, int af, struct net_data *); /* Portability */ @@ -67,118 +71,182 @@ static struct netent * fakeaddr(const char *, int af); struct netent * getnetent() { - struct irs_nw *nw = init(); - - if (!nw) - return (NULL); - net_data.nw_last = nw_to_net((*nw->next)(nw)); - return (net_data.nw_last); + struct net_data *net_data = init(); + + return (getnetent_p(net_data)); } struct netent * getnetbyname(const char *name) { - struct irs_nw *nw = init(); + struct net_data *net_data = init(); + + return (getnetbyname_p(name, net_data)); +} + +struct netent * +getnetbyaddr(unsigned long net, int type) { + struct net_data *net_data = init(); + + return (getnetbyaddr_p(net, type, net_data)); +} + +void +setnetent(int stayopen) { + struct net_data *net_data = init(); + + setnetent_p(stayopen, net_data); +} + + +void +endnetent() { + struct net_data *net_data = init(); + + endnetent_p(net_data); +} + +/* Shared private. */ + +struct netent * +getnetent_p(struct net_data *net_data) { + struct irs_nw *nw; + + if (!net_data || !(nw = net_data->nw)) + return (NULL); + net_data->nww_last = (*nw->next)(nw); + net_data->nw_last = nw_to_net(net_data->nww_last, net_data); + return (net_data->nw_last); +} + +struct netent * +getnetbyname_p(const char *name, struct net_data *net_data) { + struct irs_nw *nw; struct netent *np; char **nap; - - if (!nw) + + if (!net_data || !(nw = net_data->nw)) return (NULL); - if (net_data.nw_stayopen && net_data.nw_last) { - if (!strcmp(net_data.nw_last->n_name, name)) - return (net_data.nw_last); - for (nap = net_data.nw_last->n_aliases; nap && *nap; nap++) + if (net_data->nw_stayopen && net_data->nw_last) { + if (!strcmp(net_data->nw_last->n_name, name)) + return (net_data->nw_last); + for (nap = net_data->nw_last->n_aliases; nap && *nap; nap++) if (!strcmp(name, *nap)) - return (net_data.nw_last); + return (net_data->nw_last); } - if ((np = fakeaddr(name, AF_INET)) != NULL) + if ((np = fakeaddr(name, AF_INET, net_data)) != NULL) return (np); - net_data.nw_last = nw_to_net((*nw->byname)(nw, name, AF_INET)); - if (!net_data.nw_stayopen) + net_data->nww_last = (*nw->byname)(nw, name, AF_INET); + net_data->nw_last = nw_to_net(net_data->nww_last, net_data); + if (!net_data->nw_stayopen) endnetent(); - return (net_data.nw_last); + return (net_data->nw_last); } struct netent * -getnetbyaddr(unsigned long net, int type) { - struct irs_nw *nw = init(); +getnetbyaddr_p(unsigned long net, int type, struct net_data *net_data) { + struct irs_nw *nw; u_char addr[4]; int bits; - - if (!nw) + + if (!net_data || !(nw = net_data->nw)) return (NULL); - if (net_data.nw_stayopen && net_data.nw_last) - if (type == net_data.nw_last->n_addrtype && - net == net_data.nw_last->n_net) - return (net_data.nw_last); - - addr[3] = (0xFF000000 & net) >> 24; - addr[2] = (0x00FF0000 & net) >> 16; - addr[1] = (0x0000FF00 & net) >> 8; - addr[0] = (0x000000FF & net); - - /* Use the old class rules to figure out the network bits. */ - if (addr[3] >= 240) - bits = 32; - else if (addr[3] >= 224) - bits = 4; - else if (addr[3] >= 192) - bits = 24; - else if (addr[3] >= 128) - bits = 16; - else + if (net_data->nw_stayopen && net_data->nw_last) + if (type == net_data->nw_last->n_addrtype && + net == net_data->nw_last->n_net) + return (net_data->nw_last); + + /* cannonize net(host order) */ + if (net < 256) { + net <<= 24; bits = 8; - - net_data.nw_last = nw_to_net((*nw->byaddr)(nw, addr, bits, AF_INET)); - if (!net_data.nw_stayopen) + } else if (net < 65536) { + net <<= 16; + bits = 16; + } else if (net < 16777216) { + net <<= 8; + bits = 24; + } else + bits = 32; + + /* convert to net order */ + addr[0] = (0xFF000000 & net) >> 24; + addr[1] = (0x00FF0000 & net) >> 16; + addr[2] = (0x0000FF00 & net) >> 8; + addr[3] = (0x000000FF & net); + + /* reduce bits to as close to natural number as possible */ + if ((bits == 32) && (addr[0] < 224) && (addr[3] == 0)) + if ((addr[0] < 192) && (addr[2] == 0)) + if ((addr[0] < 128) && (addr[1] == 0)) + bits = 8; + else + bits = 16; + else + bits = 24; + + net_data->nww_last = (*nw->byaddr)(nw, addr, bits, AF_INET); + net_data->nw_last = nw_to_net(net_data->nww_last, net_data); + if (!net_data->nw_stayopen) endnetent(); - return (net_data.nw_last); + return (net_data->nw_last); } + + + void -setnetent(int stayopen) { - struct irs_nw *nw = init(); - - if (!nw) +setnetent_p(int stayopen, struct net_data *net_data) { + struct irs_nw *nw; + + if (!net_data || !(nw = net_data->nw)) return; - freepvt(); + freepvt(net_data); (*nw->rewind)(nw); - net_data.nw_stayopen = (stayopen != 0); + net_data->nw_stayopen = (stayopen != 0); + if (stayopen == 0) + net_data_minimize(net_data); } void -endnetent() { - struct irs_nw *nw = init(); +endnetent_p(struct net_data *net_data) { + struct irs_nw *nw; - if (nw != NULL) + if ((net_data != NULL) && ((nw = net_data->nw) != NULL)) (*nw->minimize)(nw); } /* Private */ -static struct irs_nw * +static struct net_data * init() { - if (!net_data_init()) + struct net_data *net_data; + + if (!(net_data = net_data_init(NULL))) goto error; - if (!net_data.nw) - net_data.nw = (*net_data.irs->nw_map)(net_data.irs); - if (!net_data.nw) { + if (!net_data->nw) { + net_data->nw = (*net_data->irs->nw_map)(net_data->irs); + + if (!net_data->nw || !net_data->res) { error: - errno = EIO; - return (NULL); + errno = EIO; + return (NULL); + } + (*net_data->nw->res_set)(net_data->nw, net_data->res, NULL); } - return (net_data.nw); + + return (net_data); } static void -freepvt() { - if (net_data.nw_data) { - free(net_data.nw_data); - net_data.nw_data = NULL; +freepvt(struct net_data *net_data) { + if (net_data->nw_data) { + free(net_data->nw_data); + net_data->nw_data = NULL; } } static struct netent * -fakeaddr(const char *name, int af) { +fakeaddr(const char *name, int af, struct net_data *net_data) { struct pvt *pvt; const char *cp; u_long tmp; @@ -186,7 +254,7 @@ fakeaddr(const char *name, int af) { if (af != AF_INET) { /* XXX should support IPv6 some day */ errno = EAFNOSUPPORT; - h_errno = NETDB_INTERNAL; + RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); return (NULL); } if (!isascii(name[0]) || !isdigit(name[0])) @@ -201,7 +269,7 @@ fakeaddr(const char *name, int af) { tmp = inet_network(name); if (tmp == INADDR_NONE) { - h_errno = HOST_NOT_FOUND; + RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND); return (NULL); } @@ -209,14 +277,14 @@ fakeaddr(const char *name, int af) { * Fake up a netent as if we'd actually * done a lookup. */ - freepvt(); - net_data.nw_data = malloc(sizeof(struct pvt)); - if (!net_data.nw_data) { + freepvt(net_data); + net_data->nw_data = malloc(sizeof (struct pvt)); + if (!net_data->nw_data) { errno = ENOMEM; - h_errno = NETDB_INTERNAL; + RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); return (NULL); } - pvt = net_data.nw_data; + pvt = net_data->nw_data; strncpy(pvt->name, name, MAXDNAME); pvt->name[MAXDNAME] = '\0'; @@ -230,29 +298,29 @@ fakeaddr(const char *name, int af) { } static struct netent * -nw_to_net(struct nwent *nwent) { +nw_to_net(struct nwent *nwent, struct net_data *net_data) { struct pvt *pvt; u_long addr = 0; - int i; + int i; int msbyte; if (!nwent || nwent->n_addrtype != AF_INET) return (NULL); - freepvt(); - net_data.nw_data = malloc(sizeof(struct pvt)); - if (!net_data.nw_data) { + freepvt(net_data); + net_data->nw_data = malloc(sizeof (struct pvt)); + if (!net_data->nw_data) { errno = ENOMEM; - h_errno = NETDB_INTERNAL; + RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); return (NULL); } - pvt = net_data.nw_data; + pvt = net_data->nw_data; pvt->netent.n_name = nwent->n_name; pvt->netent.n_aliases = nwent->n_aliases; pvt->netent.n_addrtype = nwent->n_addrtype; - + /* * What this code does: Converts net addresses from network to host form. - * + * * msbyte: the index of the most significant byte in the n_addr array. * * Shift bytes in significant order into addr. When all signicant @@ -269,4 +337,4 @@ nw_to_net(struct nwent *nwent) { return (&pvt->netent); } - +#endif /*__BIND_NOSTATIC*/ |