17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: getnetent.c,v 1.7 2005/04/27 04:56:25 sra Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /* Imports */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/socket.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <netinet/in.h>
327c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
337c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <netdb.h>
387c478bd9Sstevel@tonic-gate #include <resolv.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <irs.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "port_after.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include "irs_p.h"
477c478bd9Sstevel@tonic-gate #include "irs_data.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* Definitions */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate struct pvt {
527c478bd9Sstevel@tonic-gate 	struct netent	netent;
537c478bd9Sstevel@tonic-gate 	char *		aliases[1];
547c478bd9Sstevel@tonic-gate 	char		name[MAXDNAME + 1];
557c478bd9Sstevel@tonic-gate };
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* Forward */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct net_data *init(void);
607c478bd9Sstevel@tonic-gate static struct netent   *nw_to_net(struct nwent *, struct net_data *);
617c478bd9Sstevel@tonic-gate static void		freepvt(struct net_data *);
627c478bd9Sstevel@tonic-gate static struct netent   *fakeaddr(const char *, int af, struct net_data *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* Portability */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifndef INADDR_NONE
677c478bd9Sstevel@tonic-gate # define INADDR_NONE 0xffffffff
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* Public */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct netent *
737c478bd9Sstevel@tonic-gate getnetent() {
747c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	return (getnetent_p(net_data));
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct netent *
807c478bd9Sstevel@tonic-gate getnetbyname(const char *name) {
817c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	return (getnetbyname_p(name, net_data));
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate struct netent *
877c478bd9Sstevel@tonic-gate getnetbyaddr(unsigned long net, int type) {
887c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (getnetbyaddr_p(net, type, net_data));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate void
947c478bd9Sstevel@tonic-gate setnetent(int stayopen) {
957c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	setnetent_p(stayopen, net_data);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate void
1027c478bd9Sstevel@tonic-gate endnetent() {
1037c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	endnetent_p(net_data);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* Shared private. */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct netent *
1117c478bd9Sstevel@tonic-gate getnetent_p(struct net_data *net_data) {
1127c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (!net_data || !(nw = net_data->nw))
1157c478bd9Sstevel@tonic-gate 		return (NULL);
1167c478bd9Sstevel@tonic-gate 	net_data->nww_last = (*nw->next)(nw);
1177c478bd9Sstevel@tonic-gate 	net_data->nw_last = nw_to_net(net_data->nww_last, net_data);
1187c478bd9Sstevel@tonic-gate 	return (net_data->nw_last);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate struct netent *
1227c478bd9Sstevel@tonic-gate getnetbyname_p(const char *name, struct net_data *net_data) {
1237c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
1247c478bd9Sstevel@tonic-gate 	struct netent *np;
1257c478bd9Sstevel@tonic-gate 	char **nap;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (!net_data || !(nw = net_data->nw))
1287c478bd9Sstevel@tonic-gate 		return (NULL);
1297c478bd9Sstevel@tonic-gate 	if (net_data->nw_stayopen && net_data->nw_last) {
1307c478bd9Sstevel@tonic-gate 		if (!strcmp(net_data->nw_last->n_name, name))
1317c478bd9Sstevel@tonic-gate 			return (net_data->nw_last);
1327c478bd9Sstevel@tonic-gate 		for (nap = net_data->nw_last->n_aliases; nap && *nap; nap++)
1337c478bd9Sstevel@tonic-gate 			if (!strcmp(name, *nap))
1347c478bd9Sstevel@tonic-gate 				return (net_data->nw_last);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	if ((np = fakeaddr(name, AF_INET, net_data)) != NULL)
1377c478bd9Sstevel@tonic-gate 		return (np);
1387c478bd9Sstevel@tonic-gate 	net_data->nww_last = (*nw->byname)(nw, name, AF_INET);
1397c478bd9Sstevel@tonic-gate 	net_data->nw_last = nw_to_net(net_data->nww_last, net_data);
1407c478bd9Sstevel@tonic-gate 	if (!net_data->nw_stayopen)
1417c478bd9Sstevel@tonic-gate 		endnetent();
1427c478bd9Sstevel@tonic-gate 	return (net_data->nw_last);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate struct netent *
1467c478bd9Sstevel@tonic-gate getnetbyaddr_p(unsigned long net, int type, struct net_data *net_data) {
1477c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
1487c478bd9Sstevel@tonic-gate 	u_char addr[4];
1497c478bd9Sstevel@tonic-gate 	int bits;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (!net_data || !(nw = net_data->nw))
1527c478bd9Sstevel@tonic-gate 		return (NULL);
1537c478bd9Sstevel@tonic-gate 	if (net_data->nw_stayopen && net_data->nw_last)
1547c478bd9Sstevel@tonic-gate 		if (type == net_data->nw_last->n_addrtype &&
1557c478bd9Sstevel@tonic-gate 		    net == net_data->nw_last->n_net)
1567c478bd9Sstevel@tonic-gate 			return (net_data->nw_last);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* cannonize net(host order) */
159*9525b14bSRao Shoaib 	if (net < 256UL) {
1607c478bd9Sstevel@tonic-gate 		net <<= 24;
1617c478bd9Sstevel@tonic-gate 		bits = 8;
162*9525b14bSRao Shoaib 	} else if (net < 65536UL) {
1637c478bd9Sstevel@tonic-gate 		net <<= 16;
1647c478bd9Sstevel@tonic-gate 		bits = 16;
165*9525b14bSRao Shoaib 	} else if (net < 16777216UL) {
1667c478bd9Sstevel@tonic-gate 		net <<= 8;
1677c478bd9Sstevel@tonic-gate 		bits = 24;
1687c478bd9Sstevel@tonic-gate 	} else
1697c478bd9Sstevel@tonic-gate 		bits = 32;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* convert to net order */
1727c478bd9Sstevel@tonic-gate 	addr[0] = (0xFF000000 & net) >> 24;
1737c478bd9Sstevel@tonic-gate 	addr[1] = (0x00FF0000 & net) >> 16;
1747c478bd9Sstevel@tonic-gate 	addr[2] = (0x0000FF00 & net) >> 8;
1757c478bd9Sstevel@tonic-gate 	addr[3] = (0x000000FF & net);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* reduce bits to as close to natural number as possible */
1787c478bd9Sstevel@tonic-gate 	if ((bits == 32) && (addr[0] < 224) && (addr[3] == 0)) {
1797c478bd9Sstevel@tonic-gate 		if ((addr[0] < 192) && (addr[2] == 0)) {
1807c478bd9Sstevel@tonic-gate 			if ((addr[0] < 128) && (addr[1] == 0))
1817c478bd9Sstevel@tonic-gate 				bits = 8;
1827c478bd9Sstevel@tonic-gate 			else
1837c478bd9Sstevel@tonic-gate 				bits = 16;
1847c478bd9Sstevel@tonic-gate 		} else {
1857c478bd9Sstevel@tonic-gate 			bits = 24;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	net_data->nww_last = (*nw->byaddr)(nw, addr, bits, AF_INET);
1907c478bd9Sstevel@tonic-gate 	net_data->nw_last = nw_to_net(net_data->nww_last, net_data);
1917c478bd9Sstevel@tonic-gate 	if (!net_data->nw_stayopen)
1927c478bd9Sstevel@tonic-gate 		endnetent();
1937c478bd9Sstevel@tonic-gate 	return (net_data->nw_last);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate void
2007c478bd9Sstevel@tonic-gate setnetent_p(int stayopen, struct net_data *net_data) {
2017c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (!net_data || !(nw = net_data->nw))
2047c478bd9Sstevel@tonic-gate 		return;
2057c478bd9Sstevel@tonic-gate 	freepvt(net_data);
2067c478bd9Sstevel@tonic-gate 	(*nw->rewind)(nw);
2077c478bd9Sstevel@tonic-gate 	net_data->nw_stayopen = (stayopen != 0);
2087c478bd9Sstevel@tonic-gate 	if (stayopen == 0)
2097c478bd9Sstevel@tonic-gate 		net_data_minimize(net_data);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate void
2137c478bd9Sstevel@tonic-gate endnetent_p(struct net_data *net_data) {
2147c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if ((net_data != NULL) && ((nw	= net_data->nw) != NULL))
2177c478bd9Sstevel@tonic-gate 		(*nw->minimize)(nw);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /* Private */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static struct net_data *
2237c478bd9Sstevel@tonic-gate init() {
2247c478bd9Sstevel@tonic-gate 	struct net_data *net_data;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
2277c478bd9Sstevel@tonic-gate 		goto error;
2287c478bd9Sstevel@tonic-gate 	if (!net_data->nw) {
2297c478bd9Sstevel@tonic-gate 		net_data->nw = (*net_data->irs->nw_map)(net_data->irs);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if (!net_data->nw || !net_data->res) {
2327c478bd9Sstevel@tonic-gate  error:
2337c478bd9Sstevel@tonic-gate 			errno = EIO;
2347c478bd9Sstevel@tonic-gate 			return (NULL);
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 		(*net_data->nw->res_set)(net_data->nw, net_data->res, NULL);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (net_data);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static void
2437c478bd9Sstevel@tonic-gate freepvt(struct net_data *net_data) {
2447c478bd9Sstevel@tonic-gate 	if (net_data->nw_data) {
2457c478bd9Sstevel@tonic-gate 		free(net_data->nw_data);
2467c478bd9Sstevel@tonic-gate 		net_data->nw_data = NULL;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static struct netent *
2517c478bd9Sstevel@tonic-gate fakeaddr(const char *name, int af, struct net_data *net_data) {
2527c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
2537c478bd9Sstevel@tonic-gate 	const char *cp;
2547c478bd9Sstevel@tonic-gate 	u_long tmp;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (af != AF_INET) {
2577c478bd9Sstevel@tonic-gate 		/* XXX should support IPv6 some day */
2587c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
2597c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
2607c478bd9Sstevel@tonic-gate 		return (NULL);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	if (!isascii((unsigned char)(name[0])) ||
2637c478bd9Sstevel@tonic-gate 	    !isdigit((unsigned char)(name[0])))
2647c478bd9Sstevel@tonic-gate 		return (NULL);
2657c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; ++cp)
2667c478bd9Sstevel@tonic-gate 		if (!isascii(*cp) || (!isdigit((unsigned char)*cp) && *cp != '.'))
2677c478bd9Sstevel@tonic-gate 			return (NULL);
2687c478bd9Sstevel@tonic-gate 	if (*--cp == '.')
2697c478bd9Sstevel@tonic-gate 		return (NULL);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* All-numeric, no dot at the end. */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	tmp = inet_network(name);
2747c478bd9Sstevel@tonic-gate 	if (tmp == INADDR_NONE) {
2757c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND);
2767c478bd9Sstevel@tonic-gate 		return (NULL);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/* Valid network number specified.
2807c478bd9Sstevel@tonic-gate 	 * Fake up a netent as if we'd actually
2817c478bd9Sstevel@tonic-gate 	 * done a lookup.
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 	freepvt(net_data);
2847c478bd9Sstevel@tonic-gate 	net_data->nw_data = malloc(sizeof (struct pvt));
2857c478bd9Sstevel@tonic-gate 	if (!net_data->nw_data) {
2867c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
2877c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
2887c478bd9Sstevel@tonic-gate 		return (NULL);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 	pvt = net_data->nw_data;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	strncpy(pvt->name, name, MAXDNAME);
2937c478bd9Sstevel@tonic-gate 	pvt->name[MAXDNAME] = '\0';
2947c478bd9Sstevel@tonic-gate 	pvt->netent.n_name = pvt->name;
2957c478bd9Sstevel@tonic-gate 	pvt->netent.n_addrtype = AF_INET;
2967c478bd9Sstevel@tonic-gate 	pvt->netent.n_aliases = pvt->aliases;
2977c478bd9Sstevel@tonic-gate 	pvt->aliases[0] = NULL;
2987c478bd9Sstevel@tonic-gate 	pvt->netent.n_net = tmp;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return (&pvt->netent);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static struct netent *
3047c478bd9Sstevel@tonic-gate nw_to_net(struct nwent *nwent, struct net_data *net_data) {
3057c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
3067c478bd9Sstevel@tonic-gate 	u_long addr = 0;
3077c478bd9Sstevel@tonic-gate 	int i;
3087c478bd9Sstevel@tonic-gate 	int msbyte;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if (!nwent || nwent->n_addrtype != AF_INET)
3117c478bd9Sstevel@tonic-gate 		return (NULL);
3127c478bd9Sstevel@tonic-gate 	freepvt(net_data);
3137c478bd9Sstevel@tonic-gate 	net_data->nw_data = malloc(sizeof (struct pvt));
3147c478bd9Sstevel@tonic-gate 	if (!net_data->nw_data) {
3157c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
3167c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
3177c478bd9Sstevel@tonic-gate 		return (NULL);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 	pvt = net_data->nw_data;
3207c478bd9Sstevel@tonic-gate 	pvt->netent.n_name = nwent->n_name;
3217c478bd9Sstevel@tonic-gate 	pvt->netent.n_aliases = nwent->n_aliases;
3227c478bd9Sstevel@tonic-gate 	pvt->netent.n_addrtype = nwent->n_addrtype;
3237c478bd9Sstevel@tonic-gate 
324*9525b14bSRao Shoaib /*%
3257c478bd9Sstevel@tonic-gate  * What this code does: Converts net addresses from network to host form.
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * msbyte: the index of the most significant byte in the n_addr array.
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  * Shift bytes in significant order into addr. When all signicant
3307c478bd9Sstevel@tonic-gate  * bytes are in, zero out bits in the LSB that are not part of the network.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate 	msbyte = nwent->n_length / 8 +
3337c478bd9Sstevel@tonic-gate 		((nwent->n_length % 8) != 0 ? 1 : 0) - 1;
3347c478bd9Sstevel@tonic-gate 	for (i = 0; i <= msbyte; i++)
3357c478bd9Sstevel@tonic-gate 		addr = (addr << 8) | ((unsigned char *)nwent->n_addr)[i];
3367c478bd9Sstevel@tonic-gate 	i = (32 - nwent->n_length) % 8;
3377c478bd9Sstevel@tonic-gate 	if (i != 0)
3387c478bd9Sstevel@tonic-gate 		addr &= ~((1 << (i + 1)) - 1);
3397c478bd9Sstevel@tonic-gate 	pvt->netent.n_net = addr;
3407c478bd9Sstevel@tonic-gate 	return (&pvt->netent);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
344*9525b14bSRao Shoaib 
345*9525b14bSRao Shoaib /*! \file */
346