1 /* $NetBSD: sethostent.c,v 1.19 2013/08/27 09:56:12 christos Exp $ */
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93";
36 static char rcsid
[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
38 __RCSID("$NetBSD: sethostent.c,v 1.19 2013/08/27 09:56:12 christos Exp $");
40 #endif /* LIBC_SCCS and not lint */
42 #include "namespace.h"
43 #include <sys/param.h>
44 #include <netinet/in.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
58 __weak_alias(sethostent
,_sethostent
)
59 __weak_alias(endhostent
,_endhostent
)
66 static struct hostent
*_hf_gethtbyname2(const char *, int, struct getnamaddr
*);
70 sethostent(int stayopen
)
73 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
76 _res
.options
|= RES_STAYOPEN
| RES_USEVC
;
78 sethostent_r(&_h_file
);
85 _res
.options
&= ~(RES_STAYOPEN
| RES_USEVC
);
88 endhostent_r(&_h_file
);
90 static const char *_h_hosts
= _PATH_HOSTS
;
93 _hf_sethostsfile(const char *f
) {
98 sethostent_r(FILE **hf
)
101 *hf
= fopen(_h_hosts
, "re");
107 endhostent_r(FILE **hf
)
117 _hf_gethtbyname(void *rv
, void *cb_data
, va_list ap
)
122 struct getnamaddr
*info
= rv
;
124 _DIAGASSERT(rv
!= NULL
);
126 name
= va_arg(ap
, char *);
127 /* NOSTRICT skip string len */(void)va_arg(ap
, int);
128 af
= va_arg(ap
, int);
132 res_state res
= __res_get_state();
135 if (res
->options
& RES_USE_INET6
)
136 hp
= _hf_gethtbyname2(name
, AF_INET6
, info
);
140 hp
= _hf_gethtbyname2(name
, AF_INET
, info
);
141 __res_put_state(res
);
144 hp
= _hf_gethtbyname2(name
, af
, info
);
147 *info
->he
= HOST_NOT_FOUND
;
154 _hf_gethtbyname2(const char *name
, int af
, struct getnamaddr
*info
)
156 struct hostent
*hp
, hent
;
158 size_t len
, anum
, num
, i
;
160 char *aliases
[MAXALIASES
];
161 char *addr_ptrs
[MAXADDRS
];
163 _DIAGASSERT(name
!= NULL
);
169 *info
->he
= NETDB_INTERNAL
;
173 if ((ptr
= buf
= malloc(len
= info
->buflen
)) == NULL
) {
174 *info
->he
= NETDB_INTERNAL
;
178 anum
= 0; /* XXX: gcc */
179 hent
.h_name
= NULL
; /* XXX: gcc */
180 hent
.h_addrtype
= 0; /* XXX: gcc */
181 hent
.h_length
= 0; /* XXX: gcc */
183 for (num
= 0; num
< MAXADDRS
;) {
184 info
->hp
->h_addrtype
= af
;
185 info
->hp
->h_length
= 0;
187 hp
= gethostent_r(hf
, info
->hp
, info
->buf
, info
->buflen
,
192 if (strcasecmp(hp
->h_name
, name
) != 0) {
194 for (cp
= hp
->h_aliases
; *cp
!= NULL
; cp
++)
195 if (strcasecmp(*cp
, name
) == 0)
197 if (*cp
== NULL
) continue;
201 hent
.h_addrtype
= af
= hp
->h_addrtype
;
202 hent
.h_length
= hp
->h_length
;
204 HENT_SCOPY(hent
.h_name
, hp
->h_name
, ptr
, len
);
205 for (anum
= 0; hp
->h_aliases
[anum
]; anum
++) {
206 if (anum
>= __arraycount(aliases
))
208 HENT_SCOPY(aliases
[anum
], hp
->h_aliases
[anum
],
211 ptr
= (void *)ALIGN(ptr
);
212 if ((size_t)(ptr
- buf
) >= info
->buflen
)
216 if (num
>= __arraycount(addr_ptrs
))
218 HENT_COPY(addr_ptrs
[num
], hp
->h_addr_list
[0], hp
->h_length
, ptr
,
225 *info
->he
= HOST_NOT_FOUND
;
233 hp
->h_addrtype
= hent
.h_addrtype
;
234 hp
->h_length
= hent
.h_length
;
236 HENT_ARRAY(hp
->h_aliases
, anum
, ptr
, len
);
237 HENT_ARRAY(hp
->h_addr_list
, num
, ptr
, len
);
239 for (i
= 0; i
< num
; i
++)
240 HENT_COPY(hp
->h_addr_list
[i
], addr_ptrs
[i
], hp
->h_length
, ptr
,
242 hp
->h_addr_list
[num
] = NULL
;
244 HENT_SCOPY(hp
->h_name
, hent
.h_name
, ptr
, len
);
246 for (i
= 0; i
< anum
; i
++)
247 HENT_SCOPY(hp
->h_aliases
[i
], aliases
[i
], ptr
, len
);
248 hp
->h_aliases
[anum
] = NULL
;
252 *info
->he
= NETDB_INTERNAL
;
259 _hf_gethtbyaddr(void *rv
, void *cb_data
, va_list ap
)
262 const unsigned char *addr
;
263 struct getnamaddr
*info
= rv
;
266 _DIAGASSERT(rv
!= NULL
);
268 addr
= va_arg(ap
, unsigned char *);
269 info
->hp
->h_length
= va_arg(ap
, int);
270 info
->hp
->h_addrtype
= va_arg(ap
, int);
275 *info
->he
= NETDB_INTERNAL
;
278 while ((hp
= gethostent_r(hf
, info
->hp
, info
->buf
, info
->buflen
,
280 if (!memcmp(hp
->h_addr_list
[0], addr
, (size_t)hp
->h_length
))
285 *info
->he
= HOST_NOT_FOUND
;