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