xref: /minix3/external/bsd/bind/dist/lib/lwres/gethost.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: gethost.c,v 1.7 2015/07/08 17:29:00 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2013-2015  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or 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.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: gethost.c,v 1.34 2007/06/19 23:47:22 tbox Exp  */
21 
22 /*! \file */
23 
24 /**
25  *    These functions provide hostname-to-address and address-to-hostname
26  *    lookups by means of the lightweight resolver. They are similar to the
27  *    standard gethostent(3) functions provided by most operating systems.
28  *    They use a struct hostent which is usually defined in <namedb.h>.
29  *
30  * \code
31  * struct  hostent {
32  *         char    *h_name;        // official name of host
33  * 	   char    **h_aliases;    // alias list
34  *         int     h_addrtype;     // host address type
35  *         int     h_length;       // length of address
36  *         char    **h_addr_list;  // list of addresses from name server
37  * };
38  * #define h_addr  h_addr_list[0]  // address, for backward compatibility
39  * \endcode
40  *
41  *    The members of this structure are:
42  *
43  * \li   h_name:
44  *           The official (canonical) name of the host.
45  *
46  * \li   h_aliases:
47  *           A NULL-terminated array of alternate names (nicknames) for the
48  *           host.
49  *
50  * \li   h_addrtype:
51  *           The type of address being returned -- PF_INET or PF_INET6.
52  *
53  * \li   h_length:
54  *           The length of the address in bytes.
55  *
56  * \li   h_addr_list:
57  *           A NULL terminated array of network addresses for the host. Host
58  *           addresses are returned in network byte order.
59  *
60  *    For backward compatibility with very old software, h_addr is the first
61  *    address in h_addr_list.
62  *
63  *    lwres_gethostent(), lwres_sethostent(), lwres_endhostent(),
64  *    lwres_gethostent_r(), lwres_sethostent_r() and lwres_endhostent_r()
65  *    provide iteration over the known host entries on systems that provide
66  *    such functionality through facilities like /etc/hosts or NIS. The
67  *    lightweight resolver does not currently implement these functions; it
68  *    only provides them as stub functions that always return failure.
69  *
70  *    lwres_gethostbyname() and lwres_gethostbyname2() look up the hostname
71  *    name. lwres_gethostbyname() always looks for an IPv4 address while
72  *    lwres_gethostbyname2() looks for an address of protocol family af:
73  *    either PF_INET or PF_INET6 -- IPv4 or IPV6 addresses respectively.
74  *    Successful calls of the functions return a struct hostent for the name
75  *    that was looked up. NULL is returned if the lookups by
76  *    lwres_gethostbyname() or lwres_gethostbyname2() fail.
77  *
78  *    Reverse lookups of addresses are performed by lwres_gethostbyaddr().
79  *    addr is an address of length len bytes and protocol family type --
80  *    PF_INET or PF_INET6. lwres_gethostbyname_r() is a thread-safe function
81  *    for forward lookups. If an error occurs, an error code is returned in
82  *    *error. resbuf is a pointer to a struct hostent which is initialised
83  *    by a successful call to lwres_gethostbyname_r() . buf is a buffer of
84  *    length len bytes which is used to store the h_name, h_aliases, and
85  *    h_addr_list elements of the struct hostent returned in resbuf.
86  *    Successful calls to lwres_gethostbyname_r() return resbuf, which is a
87  *    pointer to the struct hostent it created.
88  *
89  *    lwres_gethostbyaddr_r() is a thread-safe function that performs a
90  *    reverse lookup of address addr which is len bytes long and is of
91  *    protocol family type -- PF_INET or PF_INET6. If an error occurs, the
92  *    error code is returned in *error. The other function parameters are
93  *    identical to those in lwres_gethostbyname_r(). resbuf is a pointer to
94  *    a struct hostent which is initialised by a successful call to
95  *    lwres_gethostbyaddr_r(). buf is a buffer of length len bytes which is
96  *    used to store the h_name, h_aliases, and h_addr_list elements of the
97  *    struct hostent returned in resbuf. Successful calls to
98  *    lwres_gethostbyaddr_r() return resbuf, which is a pointer to the
99  *    struct hostent it created.
100  *
101  * \section gethost_return Return Values
102  *
103  *    The functions lwres_gethostbyname(), lwres_gethostbyname2(),
104  *    lwres_gethostbyaddr(), and lwres_gethostent() return NULL to indicate
105  *    an error. In this case the global variable lwres_h_errno will contain
106  *    one of the following error codes defined in \link netdb.h <lwres/netdb.h>:\endlink
107  *
108  * \li #HOST_NOT_FOUND:
109  *           The host or address was not found.
110  *
111  * \li #TRY_AGAIN:
112  *           A recoverable error occurred, e.g., a timeout. Retrying the
113  *           lookup may succeed.
114  *
115  * \li #NO_RECOVERY:
116  *           A non-recoverable error occurred.
117  *
118  * \li #NO_DATA:
119  *           The name exists, but has no address information associated with
120  *           it (or vice versa in the case of a reverse lookup). The code
121  *           NO_ADDRESS is accepted as a synonym for NO_DATA for backwards
122  *           compatibility.
123  *
124  *    lwres_hstrerror() translates these error codes to suitable error
125  *    messages.
126  *
127  *    lwres_gethostent() and lwres_gethostent_r() always return NULL.
128  *
129  *    Successful calls to lwres_gethostbyname_r() and
130  *    lwres_gethostbyaddr_r() return resbuf, a pointer to the struct hostent
131  *    that was initialised by these functions. They return NULL if the
132  *    lookups fail or if buf was too small to hold the list of addresses and
133  *    names referenced by the h_name, h_aliases, and h_addr_list elements of
134  *    the struct hostent. If buf was too small, both lwres_gethostbyname_r()
135  *    and lwres_gethostbyaddr_r() set the global variable errno to ERANGE.
136  *
137  * \section gethost_see See Also
138  *
139  *    gethostent(), \link getipnode.c getipnode\endlink, lwres_hstrerror()
140  *
141  * \section gethost_bugs Bugs
142  *
143  *    lwres_gethostbyname(), lwres_gethostbyname2(), lwres_gethostbyaddr()
144  *    and lwres_endhostent() are not thread safe; they return pointers to
145  *    static data and provide error codes through a global variable.
146  *    Thread-safe versions for name and address lookup are provided by
147  *    lwres_gethostbyname_r(), and lwres_gethostbyaddr_r() respectively.
148  *
149  *    The resolver daemon does not currently support any non-DNS name
150  *    services such as /etc/hosts or NIS, consequently the above functions
151  *    don't, either.
152  */
153 
154 #include <config.h>
155 
156 #include <errno.h>
157 #include <string.h>
158 #ifdef HAVE_INTTYPES_H
159 #include <inttypes.h> /* uintptr_t */
160 #endif
161 
162 #include <lwres/net.h>
163 #include <lwres/netdb.h>
164 
165 #include "assert_p.h"
166 
167 #define LWRES_ALIGNBYTES (sizeof(char *) - 1)
168 #define LWRES_ALIGN(p) \
169 	(((uintptr_t)(p) + LWRES_ALIGNBYTES) &~ LWRES_ALIGNBYTES)
170 
171 static struct hostent *he = NULL;
172 static int copytobuf(struct hostent *, struct hostent *, char *, int);
173 
174 /*% Always looks for an IPv4 address. */
175 struct hostent *
lwres_gethostbyname(const char * name)176 lwres_gethostbyname(const char *name) {
177 
178 	if (he != NULL)
179 		lwres_freehostent(he);
180 
181 	he = lwres_getipnodebyname(name, AF_INET, 0, &lwres_h_errno);
182 	return (he);
183 }
184 
185 /*% Looks for either an IPv4 or IPv6 address. */
186 struct hostent *
lwres_gethostbyname2(const char * name,int af)187 lwres_gethostbyname2(const char *name, int af) {
188 	if (he != NULL)
189 		lwres_freehostent(he);
190 
191 	he = lwres_getipnodebyname(name, af, 0, &lwres_h_errno);
192 	return (he);
193 }
194 
195 /*% Reverse lookup of addresses. */
196 struct hostent *
lwres_gethostbyaddr(const char * addr,int len,int type)197 lwres_gethostbyaddr(const char *addr, int len, int type) {
198 
199 	if (he != NULL)
200 		lwres_freehostent(he);
201 
202 	he = lwres_getipnodebyaddr(addr, len, type, &lwres_h_errno);
203 	return (he);
204 }
205 
206 /*% Stub function.  Always returns failure. */
207 struct hostent *
lwres_gethostent(void)208 lwres_gethostent(void) {
209 	if (he != NULL)
210 		lwres_freehostent(he);
211 
212 	return (NULL);
213 }
214 
215 /*% Stub function.  Always returns failure. */
216 void
lwres_sethostent(int stayopen)217 lwres_sethostent(int stayopen) {
218 	/*
219 	 * Empty.
220 	 */
221 	UNUSED(stayopen);
222 }
223 
224 /*% Stub function.  Always returns failure. */
225 void
lwres_endhostent(void)226 lwres_endhostent(void) {
227 	/*
228 	 * Empty.
229 	 */
230 }
231 
232 /*% Thread-safe function for forward lookups. */
233 struct hostent *
lwres_gethostbyname_r(const char * name,struct hostent * resbuf,char * buf,int buflen,int * error)234 lwres_gethostbyname_r(const char *name, struct hostent *resbuf,
235 		char *buf, int buflen, int *error)
236 {
237 	struct hostent *myhe;
238 	int res;
239 
240 	myhe = lwres_getipnodebyname(name, AF_INET, 0, error);
241 	if (myhe == NULL)
242 		return (NULL);
243 	res = copytobuf(myhe, resbuf, buf, buflen);
244 	lwres_freehostent(myhe);
245 	if (res != 0) {
246 		errno = ERANGE;
247 		return (NULL);
248 	}
249 	return (resbuf);
250 }
251 
252 /*% Thread-safe reverse lookup. */
253 struct hostent  *
lwres_gethostbyaddr_r(const char * addr,int len,int type,struct hostent * resbuf,char * buf,int buflen,int * error)254 lwres_gethostbyaddr_r(const char *addr, int len, int type,
255 		      struct hostent *resbuf, char *buf, int buflen,
256 		      int *error)
257 {
258 	struct hostent *myhe;
259 	int res;
260 
261 	myhe = lwres_getipnodebyaddr(addr, len, type, error);
262 	if (myhe == NULL)
263 		return (NULL);
264 	res = copytobuf(myhe, resbuf, buf, buflen);
265 	lwres_freehostent(myhe);
266 	if (res != 0) {
267 		errno = ERANGE;
268 		return (NULL);
269 	}
270 	return (resbuf);
271 }
272 
273 /*% Stub function.  Always returns failure. */
274 struct hostent  *
lwres_gethostent_r(struct hostent * resbuf,char * buf,int buflen,int * error)275 lwres_gethostent_r(struct hostent *resbuf, char *buf, int buflen, int *error) {
276 	UNUSED(resbuf);
277 	UNUSED(buf);
278 	UNUSED(buflen);
279 	*error = 0;
280 	return (NULL);
281 }
282 
283 /*% Stub function.  Always returns failure. */
284 void
lwres_sethostent_r(int stayopen)285 lwres_sethostent_r(int stayopen) {
286 	/*
287 	 * Empty.
288 	 */
289 	UNUSED(stayopen);
290 }
291 
292 /*% Stub function.  Always returns failure. */
293 void
lwres_endhostent_r(void)294 lwres_endhostent_r(void) {
295 	/*
296 	 * Empty.
297 	 */
298 }
299 
300 static int
copytobuf(struct hostent * src,struct hostent * hptr,char * buf,int buflen)301 copytobuf(struct hostent *src, struct hostent *hptr, char *buf, int buflen) {
302 	char *cp;
303 	char **ptr;
304 	int i, n;
305 	int nptr, len;
306 
307 	/*
308 	 * Find out the amount of space required to store the answer.
309 	 */
310 	nptr = 2; /* NULL ptrs */
311 	len = (int)((char *)LWRES_ALIGN(buf) - buf);
312 	for (i = 0; src->h_addr_list[i]; i++, nptr++) {
313 		len += src->h_length;
314 	}
315 	for (i = 0; src->h_aliases[i]; i++, nptr++) {
316 		len += strlen(src->h_aliases[i]) + 1;
317 	}
318 	len += strlen(src->h_name) + 1;
319 	len += nptr * sizeof(char*);
320 
321 	if (len > buflen) {
322 		return (-1);
323 	}
324 
325 	/*
326 	 * Copy address size and type.
327 	 */
328 	hptr->h_addrtype = src->h_addrtype;
329 	n = hptr->h_length = src->h_length;
330 
331 	ptr = (char **)LWRES_ALIGN(buf);
332 	cp = (char *)LWRES_ALIGN(buf) + nptr * sizeof(char *);
333 
334 	/*
335 	 * Copy address list.
336 	 */
337 	hptr->h_addr_list = ptr;
338 	for (i = 0; src->h_addr_list[i]; i++, ptr++) {
339 		memmove(cp, src->h_addr_list[i], n);
340 		hptr->h_addr_list[i] = cp;
341 		cp += n;
342 	}
343 	hptr->h_addr_list[i] = NULL;
344 	ptr++;
345 
346 	/*
347 	 * Copy official name.
348 	 */
349 	n = strlen(src->h_name) + 1;
350 	strcpy(cp, src->h_name);
351 	hptr->h_name = cp;
352 	cp += n;
353 
354 	/*
355 	 * Copy aliases.
356 	 */
357 	hptr->h_aliases = ptr;
358 	for (i = 0; src->h_aliases[i]; i++) {
359 		n = strlen(src->h_aliases[i]) + 1;
360 		strcpy(cp, src->h_aliases[i]);
361 		hptr->h_aliases[i] = cp;
362 		cp += n;
363 	}
364 	hptr->h_aliases[i] = NULL;
365 
366 	return (0);
367 }
368