1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright 1997-2002 Sun Microsystems, Inc. All rights reserved. 3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate /* 7*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 10*7c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 11*7c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 12*7c478bd9Sstevel@tonic-gate * 13*7c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 14*7c478bd9Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 15*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 16*7c478bd9Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 17*7c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 18*7c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 19*7c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 20*7c478bd9Sstevel@tonic-gate * SOFTWARE. 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER) 26*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: getnetent.c,v 1.19 2001/05/29 05:48:47 marka Exp $"; 27*7c478bd9Sstevel@tonic-gate #endif 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* Imports */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #include "port_before.h" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC) 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 39*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 40*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include <ctype.h> 43*7c478bd9Sstevel@tonic-gate #include <errno.h> 44*7c478bd9Sstevel@tonic-gate #include <netdb.h> 45*7c478bd9Sstevel@tonic-gate #include <resolv.h> 46*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 47*7c478bd9Sstevel@tonic-gate #include <string.h> 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #include <irs.h> 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate #include "port_after.h" 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate #include "irs_p.h" 54*7c478bd9Sstevel@tonic-gate #include "irs_data.h" 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate /* Definitions */ 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate struct pvt { 59*7c478bd9Sstevel@tonic-gate struct netent netent; 60*7c478bd9Sstevel@tonic-gate char * aliases[1]; 61*7c478bd9Sstevel@tonic-gate char name[MAXDNAME + 1]; 62*7c478bd9Sstevel@tonic-gate }; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* Forward */ 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate static struct net_data *init(void); 67*7c478bd9Sstevel@tonic-gate static struct netent *nw_to_net(struct nwent *, struct net_data *); 68*7c478bd9Sstevel@tonic-gate static void freepvt(struct net_data *); 69*7c478bd9Sstevel@tonic-gate static struct netent *fakeaddr(const char *, int af, struct net_data *); 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate /* Portability */ 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate #ifndef INADDR_NONE 74*7c478bd9Sstevel@tonic-gate # define INADDR_NONE 0xffffffff 75*7c478bd9Sstevel@tonic-gate #endif 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /* Public */ 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate struct netent * 80*7c478bd9Sstevel@tonic-gate getnetent() { 81*7c478bd9Sstevel@tonic-gate struct net_data *net_data = init(); 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate return (getnetent_p(net_data)); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate struct netent * 87*7c478bd9Sstevel@tonic-gate getnetbyname(const char *name) { 88*7c478bd9Sstevel@tonic-gate struct net_data *net_data = init(); 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate return (getnetbyname_p(name, net_data)); 91*7c478bd9Sstevel@tonic-gate } 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate struct netent * 94*7c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 95*7c478bd9Sstevel@tonic-gate getnetbyaddr(unsigned long net, int type) { 96*7c478bd9Sstevel@tonic-gate #else 97*7c478bd9Sstevel@tonic-gate getnetbyaddr(in_addr_t net, int type) { 98*7c478bd9Sstevel@tonic-gate #endif 99*7c478bd9Sstevel@tonic-gate struct net_data *net_data = init(); 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate return (getnetbyaddr_p(net, type, net_data)); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 105*7c478bd9Sstevel@tonic-gate void 106*7c478bd9Sstevel@tonic-gate #else 107*7c478bd9Sstevel@tonic-gate int 108*7c478bd9Sstevel@tonic-gate #endif 109*7c478bd9Sstevel@tonic-gate setnetent(int stayopen) { 110*7c478bd9Sstevel@tonic-gate struct net_data *net_data = init(); 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate setnetent_p(stayopen, net_data); 113*7c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 114*7c478bd9Sstevel@tonic-gate #else 115*7c478bd9Sstevel@tonic-gate return (0); 116*7c478bd9Sstevel@tonic-gate #endif 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 121*7c478bd9Sstevel@tonic-gate void 122*7c478bd9Sstevel@tonic-gate #else 123*7c478bd9Sstevel@tonic-gate int 124*7c478bd9Sstevel@tonic-gate #endif 125*7c478bd9Sstevel@tonic-gate endnetent() { 126*7c478bd9Sstevel@tonic-gate struct net_data *net_data = init(); 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate endnetent_p(net_data); 129*7c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 130*7c478bd9Sstevel@tonic-gate #else 131*7c478bd9Sstevel@tonic-gate return (0); 132*7c478bd9Sstevel@tonic-gate #endif 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* Shared private. */ 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate struct netent * 138*7c478bd9Sstevel@tonic-gate getnetent_p(struct net_data *net_data) { 139*7c478bd9Sstevel@tonic-gate struct irs_nw *nw; 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate if (!net_data || !(nw = net_data->nw)) 142*7c478bd9Sstevel@tonic-gate return (NULL); 143*7c478bd9Sstevel@tonic-gate net_data->nww_last = (*nw->next)(nw); 144*7c478bd9Sstevel@tonic-gate net_data->nw_last = nw_to_net(net_data->nww_last, net_data); 145*7c478bd9Sstevel@tonic-gate return (net_data->nw_last); 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate struct netent * 149*7c478bd9Sstevel@tonic-gate getnetbyname_p(const char *name, struct net_data *net_data) { 150*7c478bd9Sstevel@tonic-gate struct irs_nw *nw; 151*7c478bd9Sstevel@tonic-gate struct netent *np; 152*7c478bd9Sstevel@tonic-gate char **nap; 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate if (!net_data || !(nw = net_data->nw)) 155*7c478bd9Sstevel@tonic-gate return (NULL); 156*7c478bd9Sstevel@tonic-gate if (net_data->nw_stayopen && net_data->nw_last) { 157*7c478bd9Sstevel@tonic-gate if (!strcmp(net_data->nw_last->n_name, name)) 158*7c478bd9Sstevel@tonic-gate return (net_data->nw_last); 159*7c478bd9Sstevel@tonic-gate for (nap = net_data->nw_last->n_aliases; nap && *nap; nap++) 160*7c478bd9Sstevel@tonic-gate if (!strcmp(name, *nap)) 161*7c478bd9Sstevel@tonic-gate return (net_data->nw_last); 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate if ((np = fakeaddr(name, AF_INET, net_data)) != NULL) 164*7c478bd9Sstevel@tonic-gate return (np); 165*7c478bd9Sstevel@tonic-gate net_data->nww_last = (*nw->byname)(nw, name, AF_INET); 166*7c478bd9Sstevel@tonic-gate net_data->nw_last = nw_to_net(net_data->nww_last, net_data); 167*7c478bd9Sstevel@tonic-gate if (!net_data->nw_stayopen) 168*7c478bd9Sstevel@tonic-gate endnetent(); 169*7c478bd9Sstevel@tonic-gate return (net_data->nw_last); 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate struct netent * 173*7c478bd9Sstevel@tonic-gate getnetbyaddr_p(unsigned long net, int type, struct net_data *net_data) { 174*7c478bd9Sstevel@tonic-gate struct irs_nw *nw; 175*7c478bd9Sstevel@tonic-gate u_char addr[4]; 176*7c478bd9Sstevel@tonic-gate int bits; 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate if (!net_data || !(nw = net_data->nw)) 179*7c478bd9Sstevel@tonic-gate return (NULL); 180*7c478bd9Sstevel@tonic-gate if (net_data->nw_stayopen && net_data->nw_last) 181*7c478bd9Sstevel@tonic-gate if (type == net_data->nw_last->n_addrtype && 182*7c478bd9Sstevel@tonic-gate net == net_data->nw_last->n_net) 183*7c478bd9Sstevel@tonic-gate return (net_data->nw_last); 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate /* cannonize net(host order) */ 186*7c478bd9Sstevel@tonic-gate if (net < 256) { 187*7c478bd9Sstevel@tonic-gate net <<= 24; 188*7c478bd9Sstevel@tonic-gate bits = 8; 189*7c478bd9Sstevel@tonic-gate } else if (net < 65536) { 190*7c478bd9Sstevel@tonic-gate net <<= 16; 191*7c478bd9Sstevel@tonic-gate bits = 16; 192*7c478bd9Sstevel@tonic-gate } else if (net < 16777216) { 193*7c478bd9Sstevel@tonic-gate net <<= 8; 194*7c478bd9Sstevel@tonic-gate bits = 24; 195*7c478bd9Sstevel@tonic-gate } else 196*7c478bd9Sstevel@tonic-gate bits = 32; 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate /* convert to net order */ 199*7c478bd9Sstevel@tonic-gate addr[0] = (0xFF000000 & net) >> 24; 200*7c478bd9Sstevel@tonic-gate addr[1] = (0x00FF0000 & net) >> 16; 201*7c478bd9Sstevel@tonic-gate addr[2] = (0x0000FF00 & net) >> 8; 202*7c478bd9Sstevel@tonic-gate addr[3] = (0x000000FF & net); 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate /* reduce bits to as close to natural number as possible */ 205*7c478bd9Sstevel@tonic-gate if ((bits == 32) && (addr[0] < 224) && (addr[3] == 0)) { 206*7c478bd9Sstevel@tonic-gate if ((addr[0] < 192) && (addr[2] == 0)) { 207*7c478bd9Sstevel@tonic-gate if ((addr[0] < 128) && (addr[1] == 0)) 208*7c478bd9Sstevel@tonic-gate bits = 8; 209*7c478bd9Sstevel@tonic-gate else 210*7c478bd9Sstevel@tonic-gate bits = 16; 211*7c478bd9Sstevel@tonic-gate } else { 212*7c478bd9Sstevel@tonic-gate bits = 24; 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate net_data->nww_last = (*nw->byaddr)(nw, addr, bits, AF_INET); 217*7c478bd9Sstevel@tonic-gate net_data->nw_last = nw_to_net(net_data->nww_last, net_data); 218*7c478bd9Sstevel@tonic-gate if (!net_data->nw_stayopen) 219*7c478bd9Sstevel@tonic-gate endnetent(); 220*7c478bd9Sstevel@tonic-gate return (net_data->nw_last); 221*7c478bd9Sstevel@tonic-gate } 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate void 227*7c478bd9Sstevel@tonic-gate setnetent_p(int stayopen, struct net_data *net_data) { 228*7c478bd9Sstevel@tonic-gate struct irs_nw *nw; 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate if (!net_data || !(nw = net_data->nw)) 231*7c478bd9Sstevel@tonic-gate return; 232*7c478bd9Sstevel@tonic-gate freepvt(net_data); 233*7c478bd9Sstevel@tonic-gate (*nw->rewind)(nw); 234*7c478bd9Sstevel@tonic-gate net_data->nw_stayopen = (stayopen != 0); 235*7c478bd9Sstevel@tonic-gate if (stayopen == 0) 236*7c478bd9Sstevel@tonic-gate net_data_minimize(net_data); 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate void 240*7c478bd9Sstevel@tonic-gate endnetent_p(struct net_data *net_data) { 241*7c478bd9Sstevel@tonic-gate struct irs_nw *nw; 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate if ((net_data != NULL) && ((nw = net_data->nw) != NULL)) 244*7c478bd9Sstevel@tonic-gate (*nw->minimize)(nw); 245*7c478bd9Sstevel@tonic-gate } 246*7c478bd9Sstevel@tonic-gate 247*7c478bd9Sstevel@tonic-gate /* Private */ 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate static struct net_data * 250*7c478bd9Sstevel@tonic-gate init() { 251*7c478bd9Sstevel@tonic-gate struct net_data *net_data; 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate if (!(net_data = net_data_init(NULL))) 254*7c478bd9Sstevel@tonic-gate goto error; 255*7c478bd9Sstevel@tonic-gate if (!net_data->nw) { 256*7c478bd9Sstevel@tonic-gate net_data->nw = (*net_data->irs->nw_map)(net_data->irs); 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate if (!net_data->nw || !net_data->res) { 259*7c478bd9Sstevel@tonic-gate error: 260*7c478bd9Sstevel@tonic-gate errno = EIO; 261*7c478bd9Sstevel@tonic-gate return (NULL); 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate (*net_data->nw->res_set)(net_data->nw, net_data->res, NULL); 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate return (net_data); 267*7c478bd9Sstevel@tonic-gate } 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate static void 270*7c478bd9Sstevel@tonic-gate freepvt(struct net_data *net_data) { 271*7c478bd9Sstevel@tonic-gate if (net_data->nw_data) { 272*7c478bd9Sstevel@tonic-gate free(net_data->nw_data); 273*7c478bd9Sstevel@tonic-gate net_data->nw_data = NULL; 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate static struct netent * 278*7c478bd9Sstevel@tonic-gate fakeaddr(const char *name, int af, struct net_data *net_data) { 279*7c478bd9Sstevel@tonic-gate struct pvt *pvt; 280*7c478bd9Sstevel@tonic-gate const char *cp; 281*7c478bd9Sstevel@tonic-gate u_long tmp; 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate if (af != AF_INET) { 284*7c478bd9Sstevel@tonic-gate /* XXX should support IPv6 some day */ 285*7c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT; 286*7c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); 287*7c478bd9Sstevel@tonic-gate return (NULL); 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate if (!isascii((unsigned char)(name[0])) || 290*7c478bd9Sstevel@tonic-gate !isdigit((unsigned char)(name[0]))) 291*7c478bd9Sstevel@tonic-gate return (NULL); 292*7c478bd9Sstevel@tonic-gate for (cp = name; *cp; ++cp) 293*7c478bd9Sstevel@tonic-gate if (!isascii(*cp) || (!isdigit((unsigned char)*cp) && *cp != '.')) 294*7c478bd9Sstevel@tonic-gate return (NULL); 295*7c478bd9Sstevel@tonic-gate if (*--cp == '.') 296*7c478bd9Sstevel@tonic-gate return (NULL); 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate /* All-numeric, no dot at the end. */ 299*7c478bd9Sstevel@tonic-gate 300*7c478bd9Sstevel@tonic-gate tmp = inet_network(name); 301*7c478bd9Sstevel@tonic-gate if (tmp == INADDR_NONE) { 302*7c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND); 303*7c478bd9Sstevel@tonic-gate return (NULL); 304*7c478bd9Sstevel@tonic-gate } 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate /* Valid network number specified. 307*7c478bd9Sstevel@tonic-gate * Fake up a netent as if we'd actually 308*7c478bd9Sstevel@tonic-gate * done a lookup. 309*7c478bd9Sstevel@tonic-gate */ 310*7c478bd9Sstevel@tonic-gate freepvt(net_data); 311*7c478bd9Sstevel@tonic-gate net_data->nw_data = malloc(sizeof (struct pvt)); 312*7c478bd9Sstevel@tonic-gate if (!net_data->nw_data) { 313*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 314*7c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); 315*7c478bd9Sstevel@tonic-gate return (NULL); 316*7c478bd9Sstevel@tonic-gate } 317*7c478bd9Sstevel@tonic-gate pvt = net_data->nw_data; 318*7c478bd9Sstevel@tonic-gate 319*7c478bd9Sstevel@tonic-gate strncpy(pvt->name, name, MAXDNAME); 320*7c478bd9Sstevel@tonic-gate pvt->name[MAXDNAME] = '\0'; 321*7c478bd9Sstevel@tonic-gate pvt->netent.n_name = pvt->name; 322*7c478bd9Sstevel@tonic-gate pvt->netent.n_addrtype = AF_INET; 323*7c478bd9Sstevel@tonic-gate pvt->netent.n_aliases = pvt->aliases; 324*7c478bd9Sstevel@tonic-gate pvt->aliases[0] = NULL; 325*7c478bd9Sstevel@tonic-gate pvt->netent.n_net = tmp; 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate return (&pvt->netent); 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate static struct netent * 331*7c478bd9Sstevel@tonic-gate nw_to_net(struct nwent *nwent, struct net_data *net_data) { 332*7c478bd9Sstevel@tonic-gate struct pvt *pvt; 333*7c478bd9Sstevel@tonic-gate u_long addr = 0; 334*7c478bd9Sstevel@tonic-gate int i; 335*7c478bd9Sstevel@tonic-gate int msbyte; 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate if (!nwent || nwent->n_addrtype != AF_INET) 338*7c478bd9Sstevel@tonic-gate return (NULL); 339*7c478bd9Sstevel@tonic-gate freepvt(net_data); 340*7c478bd9Sstevel@tonic-gate net_data->nw_data = malloc(sizeof (struct pvt)); 341*7c478bd9Sstevel@tonic-gate if (!net_data->nw_data) { 342*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 343*7c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); 344*7c478bd9Sstevel@tonic-gate return (NULL); 345*7c478bd9Sstevel@tonic-gate } 346*7c478bd9Sstevel@tonic-gate pvt = net_data->nw_data; 347*7c478bd9Sstevel@tonic-gate pvt->netent.n_name = nwent->n_name; 348*7c478bd9Sstevel@tonic-gate pvt->netent.n_aliases = nwent->n_aliases; 349*7c478bd9Sstevel@tonic-gate pvt->netent.n_addrtype = nwent->n_addrtype; 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate /* 352*7c478bd9Sstevel@tonic-gate * What this code does: Converts net addresses from network to host form. 353*7c478bd9Sstevel@tonic-gate * 354*7c478bd9Sstevel@tonic-gate * msbyte: the index of the most significant byte in the n_addr array. 355*7c478bd9Sstevel@tonic-gate * 356*7c478bd9Sstevel@tonic-gate * Shift bytes in significant order into addr. When all signicant 357*7c478bd9Sstevel@tonic-gate * bytes are in, zero out bits in the LSB that are not part of the network. 358*7c478bd9Sstevel@tonic-gate */ 359*7c478bd9Sstevel@tonic-gate msbyte = nwent->n_length / 8 + 360*7c478bd9Sstevel@tonic-gate ((nwent->n_length % 8) != 0 ? 1 : 0) - 1; 361*7c478bd9Sstevel@tonic-gate for (i = 0; i <= msbyte; i++) 362*7c478bd9Sstevel@tonic-gate addr = (addr << 8) | ((unsigned char *)nwent->n_addr)[i]; 363*7c478bd9Sstevel@tonic-gate i = (32 - nwent->n_length) % 8; 364*7c478bd9Sstevel@tonic-gate if (i != 0) 365*7c478bd9Sstevel@tonic-gate addr &= ~((1 << (i + 1)) - 1); 366*7c478bd9Sstevel@tonic-gate pvt->netent.n_net = addr; 367*7c478bd9Sstevel@tonic-gate return (&pvt->netent); 368*7c478bd9Sstevel@tonic-gate } 369*7c478bd9Sstevel@tonic-gate 370*7c478bd9Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/ 371