2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "port_before.h"
22 #if !defined(__BIND_NOSTATIC)
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
29 #include <arpa/inet.h>
40 #include "port_after.h"
50 char name
[MAXDNAME
+ 1];
55 static struct net_data
*init(void);
56 static struct netent
*nw_to_net(struct nwent
*, struct net_data
*);
57 static void freepvt(struct net_data
*);
58 static struct netent
*fakeaddr(const char *, int af
, struct net_data
*);
63 # define INADDR_NONE 0xffffffff
70 struct net_data
*net_data
= init();
72 return (getnetent_p(net_data
));
76 getnetbyname(const char *name
) {
77 struct net_data
*net_data
= init();
79 return (getnetbyname_p(name
, net_data
));
83 getnetbyaddr(unsigned long net
, int type
) {
84 struct net_data
*net_data
= init();
86 return (getnetbyaddr_p(net
, type
, net_data
));
90 setnetent(int stayopen
) {
91 struct net_data
*net_data
= init();
93 setnetent_p(stayopen
, net_data
);
99 struct net_data
*net_data
= init();
101 endnetent_p(net_data
);
104 /* Shared private. */
107 getnetent_p(struct net_data
*net_data
) {
110 if (!net_data
|| !(nw
= net_data
->nw
))
112 net_data
->nww_last
= (*nw
->next
)(nw
);
113 net_data
->nw_last
= nw_to_net(net_data
->nww_last
, net_data
);
114 return (net_data
->nw_last
);
118 getnetbyname_p(const char *name
, struct net_data
*net_data
) {
123 if (!net_data
|| !(nw
= net_data
->nw
))
125 if (net_data
->nw_stayopen
&& net_data
->nw_last
) {
126 if (!strcmp(net_data
->nw_last
->n_name
, name
))
127 return (net_data
->nw_last
);
128 for (nap
= net_data
->nw_last
->n_aliases
; nap
&& *nap
; nap
++)
129 if (!strcmp(name
, *nap
))
130 return (net_data
->nw_last
);
132 if ((np
= fakeaddr(name
, AF_INET
, net_data
)) != NULL
)
134 net_data
->nww_last
= (*nw
->byname
)(nw
, name
, AF_INET
);
135 net_data
->nw_last
= nw_to_net(net_data
->nww_last
, net_data
);
136 if (!net_data
->nw_stayopen
)
138 return (net_data
->nw_last
);
142 getnetbyaddr_p(unsigned long net
, int type
, struct net_data
*net_data
) {
147 if (!net_data
|| !(nw
= net_data
->nw
))
149 if (net_data
->nw_stayopen
&& net_data
->nw_last
)
150 if (type
== net_data
->nw_last
->n_addrtype
&&
151 net
== net_data
->nw_last
->n_net
)
152 return (net_data
->nw_last
);
154 /* cannonize net(host order) */
158 } else if (net
< 65536UL) {
161 } else if (net
< 16777216UL) {
167 /* convert to net order */
168 addr
[0] = (0xFF000000 & net
) >> 24;
169 addr
[1] = (0x00FF0000 & net
) >> 16;
170 addr
[2] = (0x0000FF00 & net
) >> 8;
171 addr
[3] = (0x000000FF & net
);
173 /* reduce bits to as close to natural number as possible */
174 if ((bits
== 32) && (addr
[0] < 224) && (addr
[3] == 0)) {
175 if ((addr
[0] < 192) && (addr
[2] == 0)) {
176 if ((addr
[0] < 128) && (addr
[1] == 0))
185 net_data
->nww_last
= (*nw
->byaddr
)(nw
, addr
, bits
, AF_INET
);
186 net_data
->nw_last
= nw_to_net(net_data
->nww_last
, net_data
);
187 if (!net_data
->nw_stayopen
)
189 return (net_data
->nw_last
);
196 setnetent_p(int stayopen
, struct net_data
*net_data
) {
199 if (!net_data
|| !(nw
= net_data
->nw
))
203 net_data
->nw_stayopen
= (stayopen
!= 0);
205 net_data_minimize(net_data
);
209 endnetent_p(struct net_data
*net_data
) {
212 if ((net_data
!= NULL
) && ((nw
= net_data
->nw
) != NULL
))
218 static struct net_data
*
220 struct net_data
*net_data
;
222 if (!(net_data
= net_data_init(NULL
)))
225 net_data
->nw
= (*net_data
->irs
->nw_map
)(net_data
->irs
);
227 if (!net_data
->nw
|| !net_data
->res
) {
232 (*net_data
->nw
->res_set
)(net_data
->nw
, net_data
->res
, NULL
);
239 freepvt(struct net_data
*net_data
) {
240 if (net_data
->nw_data
) {
241 free(net_data
->nw_data
);
242 net_data
->nw_data
= NULL
;
246 static struct netent
*
247 fakeaddr(const char *name
, int af
, struct net_data
*net_data
) {
253 /* XXX should support IPv6 some day */
254 errno
= EAFNOSUPPORT
;
255 RES_SET_H_ERRNO(net_data
->res
, NETDB_INTERNAL
);
258 if (!isascii((unsigned char)(name
[0])) ||
259 !isdigit((unsigned char)(name
[0])))
261 for (cp
= name
; *cp
; ++cp
)
262 if (!isascii(*cp
) || (!isdigit((unsigned char)*cp
) && *cp
!= '.'))
267 /* All-numeric, no dot at the end. */
269 tmp
= inet_network(name
);
270 if (tmp
== INADDR_NONE
) {
271 RES_SET_H_ERRNO(net_data
->res
, HOST_NOT_FOUND
);
275 /* Valid network number specified.
276 * Fake up a netent as if we'd actually
280 net_data
->nw_data
= malloc(sizeof (struct pvt
));
281 if (!net_data
->nw_data
) {
283 RES_SET_H_ERRNO(net_data
->res
, NETDB_INTERNAL
);
286 pvt
= net_data
->nw_data
;
288 strncpy(pvt
->name
, name
, MAXDNAME
);
289 pvt
->name
[MAXDNAME
] = '\0';
290 pvt
->netent
.n_name
= pvt
->name
;
291 pvt
->netent
.n_addrtype
= AF_INET
;
292 pvt
->netent
.n_aliases
= pvt
->aliases
;
293 pvt
->aliases
[0] = NULL
;
294 pvt
->netent
.n_net
= tmp
;
296 return (&pvt
->netent
);
299 static struct netent
*
300 nw_to_net(struct nwent
*nwent
, struct net_data
*net_data
) {
306 if (!nwent
|| nwent
->n_addrtype
!= AF_INET
)
309 net_data
->nw_data
= malloc(sizeof (struct pvt
));
310 if (!net_data
->nw_data
) {
312 RES_SET_H_ERRNO(net_data
->res
, NETDB_INTERNAL
);
315 pvt
= net_data
->nw_data
;
316 pvt
->netent
.n_name
= nwent
->n_name
;
317 pvt
->netent
.n_aliases
= nwent
->n_aliases
;
318 pvt
->netent
.n_addrtype
= nwent
->n_addrtype
;
321 * What this code does: Converts net addresses from network to host form.
323 * msbyte: the index of the most significant byte in the n_addr array.
325 * Shift bytes in significant order into addr. When all signicant
326 * bytes are in, zero out bits in the LSB that are not part of the network.
328 msbyte
= nwent
->n_length
/ 8 +
329 ((nwent
->n_length
% 8) != 0 ? 1 : 0) - 1;
330 for (i
= 0; i
<= msbyte
; i
++)
331 addr
= (addr
<< 8) | ((unsigned char *)nwent
->n_addr
)[i
];
332 i
= (32 - nwent
->n_length
) % 8;
334 addr
&= ~((1 << (i
+ 1)) - 1);
335 pvt
->netent
.n_net
= addr
;
336 return (&pvt
->netent
);
339 #endif /*__BIND_NOSTATIC*/