4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid
[] = "Id: getnetent.c,v 1.7 2005/04/27 04:56:25 sra Exp";
26 #include "port_before.h"
28 #if !defined(__BIND_NOSTATIC)
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <arpa/inet.h>
46 #include "port_after.h"
56 char name
[MAXDNAME
+ 1];
61 static struct net_data
*init(void);
62 static struct netent
*nw_to_net(struct nwent
*, struct net_data
*);
63 static void freepvt(struct net_data
*);
64 static struct netent
*fakeaddr(const char *, int af
, struct net_data
*);
69 # define INADDR_NONE 0xffffffff
76 struct net_data
*net_data
= init();
78 return (getnetent_p(net_data
));
82 getnetbyname(const char *name
) {
83 struct net_data
*net_data
= init();
85 return (getnetbyname_p(name
, net_data
));
89 getnetbyaddr(unsigned long net
, int type
) {
90 struct net_data
*net_data
= init();
92 return (getnetbyaddr_p(net
, type
, net_data
));
96 setnetent(int stayopen
) {
97 struct net_data
*net_data
= init();
99 setnetent_p(stayopen
, net_data
);
105 struct net_data
*net_data
= init();
107 endnetent_p(net_data
);
110 /* Shared private. */
113 getnetent_p(struct net_data
*net_data
) {
116 if (!net_data
|| !(nw
= net_data
->nw
))
118 net_data
->nww_last
= (*nw
->next
)(nw
);
119 net_data
->nw_last
= nw_to_net(net_data
->nww_last
, net_data
);
120 return (net_data
->nw_last
);
124 getnetbyname_p(const char *name
, struct net_data
*net_data
) {
129 if (!net_data
|| !(nw
= net_data
->nw
))
131 if (net_data
->nw_stayopen
&& net_data
->nw_last
) {
132 if (!strcmp(net_data
->nw_last
->n_name
, name
))
133 return (net_data
->nw_last
);
134 for (nap
= net_data
->nw_last
->n_aliases
; nap
&& *nap
; nap
++)
135 if (!strcmp(name
, *nap
))
136 return (net_data
->nw_last
);
138 if ((np
= fakeaddr(name
, AF_INET
, net_data
)) != NULL
)
140 net_data
->nww_last
= (*nw
->byname
)(nw
, name
, AF_INET
);
141 net_data
->nw_last
= nw_to_net(net_data
->nww_last
, net_data
);
142 if (!net_data
->nw_stayopen
)
144 return (net_data
->nw_last
);
148 getnetbyaddr_p(unsigned long net
, int type
, struct net_data
*net_data
) {
153 if (!net_data
|| !(nw
= net_data
->nw
))
155 if (net_data
->nw_stayopen
&& net_data
->nw_last
)
156 if (type
== net_data
->nw_last
->n_addrtype
&&
157 net
== net_data
->nw_last
->n_net
)
158 return (net_data
->nw_last
);
160 /* cannonize net(host order) */
164 } else if (net
< 65536UL) {
167 } else if (net
< 16777216UL) {
173 /* convert to net order */
174 addr
[0] = (0xFF000000 & net
) >> 24;
175 addr
[1] = (0x00FF0000 & net
) >> 16;
176 addr
[2] = (0x0000FF00 & net
) >> 8;
177 addr
[3] = (0x000000FF & net
);
179 /* reduce bits to as close to natural number as possible */
180 if ((bits
== 32) && (addr
[0] < 224) && (addr
[3] == 0)) {
181 if ((addr
[0] < 192) && (addr
[2] == 0)) {
182 if ((addr
[0] < 128) && (addr
[1] == 0))
191 net_data
->nww_last
= (*nw
->byaddr
)(nw
, addr
, bits
, AF_INET
);
192 net_data
->nw_last
= nw_to_net(net_data
->nww_last
, net_data
);
193 if (!net_data
->nw_stayopen
)
195 return (net_data
->nw_last
);
202 setnetent_p(int stayopen
, struct net_data
*net_data
) {
205 if (!net_data
|| !(nw
= net_data
->nw
))
209 net_data
->nw_stayopen
= (stayopen
!= 0);
211 net_data_minimize(net_data
);
215 endnetent_p(struct net_data
*net_data
) {
218 if ((net_data
!= NULL
) && ((nw
= net_data
->nw
) != NULL
))
224 static struct net_data
*
226 struct net_data
*net_data
;
228 if (!(net_data
= net_data_init(NULL
)))
231 net_data
->nw
= (*net_data
->irs
->nw_map
)(net_data
->irs
);
233 if (!net_data
->nw
|| !net_data
->res
) {
238 (*net_data
->nw
->res_set
)(net_data
->nw
, net_data
->res
, NULL
);
245 freepvt(struct net_data
*net_data
) {
246 if (net_data
->nw_data
) {
247 free(net_data
->nw_data
);
248 net_data
->nw_data
= NULL
;
252 static struct netent
*
253 fakeaddr(const char *name
, int af
, struct net_data
*net_data
) {
259 /* XXX should support IPv6 some day */
260 errno
= EAFNOSUPPORT
;
261 RES_SET_H_ERRNO(net_data
->res
, NETDB_INTERNAL
);
264 if (!isascii((unsigned char)(name
[0])) ||
265 !isdigit((unsigned char)(name
[0])))
267 for (cp
= name
; *cp
; ++cp
)
268 if (!isascii(*cp
) || (!isdigit((unsigned char)*cp
) && *cp
!= '.'))
273 /* All-numeric, no dot at the end. */
275 tmp
= inet_network(name
);
276 if (tmp
== INADDR_NONE
) {
277 RES_SET_H_ERRNO(net_data
->res
, HOST_NOT_FOUND
);
281 /* Valid network number specified.
282 * Fake up a netent as if we'd actually
286 net_data
->nw_data
= malloc(sizeof (struct pvt
));
287 if (!net_data
->nw_data
) {
289 RES_SET_H_ERRNO(net_data
->res
, NETDB_INTERNAL
);
292 pvt
= net_data
->nw_data
;
294 strncpy(pvt
->name
, name
, MAXDNAME
);
295 pvt
->name
[MAXDNAME
] = '\0';
296 pvt
->netent
.n_name
= pvt
->name
;
297 pvt
->netent
.n_addrtype
= AF_INET
;
298 pvt
->netent
.n_aliases
= pvt
->aliases
;
299 pvt
->aliases
[0] = NULL
;
300 pvt
->netent
.n_net
= tmp
;
302 return (&pvt
->netent
);
305 static struct netent
*
306 nw_to_net(struct nwent
*nwent
, struct net_data
*net_data
) {
312 if (!nwent
|| nwent
->n_addrtype
!= AF_INET
)
315 net_data
->nw_data
= malloc(sizeof (struct pvt
));
316 if (!net_data
->nw_data
) {
318 RES_SET_H_ERRNO(net_data
->res
, NETDB_INTERNAL
);
321 pvt
= net_data
->nw_data
;
322 pvt
->netent
.n_name
= nwent
->n_name
;
323 pvt
->netent
.n_aliases
= nwent
->n_aliases
;
324 pvt
->netent
.n_addrtype
= nwent
->n_addrtype
;
327 * What this code does: Converts net addresses from network to host form.
329 * msbyte: the index of the most significant byte in the n_addr array.
331 * Shift bytes in significant order into addr. When all signicant
332 * bytes are in, zero out bits in the LSB that are not part of the network.
334 msbyte
= nwent
->n_length
/ 8 +
335 ((nwent
->n_length
% 8) != 0 ? 1 : 0) - 1;
336 for (i
= 0; i
<= msbyte
; i
++)
337 addr
= (addr
<< 8) | ((unsigned char *)nwent
->n_addr
)[i
];
338 i
= (32 - nwent
->n_length
) % 8;
340 addr
&= ~((1 << (i
+ 1)) - 1);
341 pvt
->netent
.n_net
= addr
;
342 return (&pvt
->netent
);
345 #endif /*__BIND_NOSTATIC*/