summaryrefslogtreecommitdiff
path: root/usr.sbin/bind/lib/lwres/gethost.c
blob: f06ee54ff9bb58cf0c189d51561846a3106a1cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
 * Copyright (C) 2000, 2001  Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/* $ISC: gethost.c,v 1.30.18.2 2005/04/29 00:17:17 marka Exp $ */

/*! \file */

/**
 *    These functions provide hostname-to-address and address-to-hostname
 *    lookups by means of the lightweight resolver. They are similar to the
 *    standard gethostent(3) functions provided by most operating systems.
 *    They use a struct hostent which is usually defined in <namedb.h>.
 * 
 * \code
 * struct  hostent {
 *         char    *h_name;        // official name of host
 * 	   char    **h_aliases;    // alias list
 *         int     h_addrtype;     // host address type
 *         int     h_length;       // length of address
 *         char    **h_addr_list;  // list of addresses from name server
 * };
 * #define h_addr  h_addr_list[0]  // address, for backward compatibility
 * \endcode
 * 
 *    The members of this structure are:
 * 
 * \li   h_name:
 *           The official (canonical) name of the host.
 * 
 * \li   h_aliases:
 *           A NULL-terminated array of alternate names (nicknames) for the
 *           host.
 * 
 * \li   h_addrtype:
 *           The type of address being returned -- PF_INET or PF_INET6.
 * 
 * \li   h_length:
 *           The length of the address in bytes.
 * 
 * \li   h_addr_list:
 *           A NULL terminated array of network addresses for the host. Host
 *           addresses are returned in network byte order.
 * 
 *    For backward compatibility with very old software, h_addr is the first
 *    address in h_addr_list.
 * 
 *    lwres_gethostent(), lwres_sethostent(), lwres_endhostent(),
 *    lwres_gethostent_r(), lwres_sethostent_r() and lwres_endhostent_r()
 *    provide iteration over the known host entries on systems that provide
 *    such functionality through facilities like /etc/hosts or NIS. The
 *    lightweight resolver does not currently implement these functions; it
 *    only provides them as stub functions that always return failure.
 * 
 *    lwres_gethostbyname() and lwres_gethostbyname2() look up the hostname
 *    name. lwres_gethostbyname() always looks for an IPv4 address while
 *    lwres_gethostbyname2() looks for an address of protocol family af:
 *    either PF_INET or PF_INET6 -- IPv4 or IPV6 addresses respectively.
 *    Successful calls of the functions return a struct hostent for the name
 *    that was looked up. NULL is returned if the lookups by
 *    lwres_gethostbyname() or lwres_gethostbyname2() fail.
 * 
 *    Reverse lookups of addresses are performed by lwres_gethostbyaddr().
 *    addr is an address of length len bytes and protocol family type --
 *    PF_INET or PF_INET6. lwres_gethostbyname_r() is a thread-safe function
 *    for forward lookups. If an error occurs, an error code is returned in
 *    *error. resbuf is a pointer to a struct hostent which is initialised
 *    by a successful call to lwres_gethostbyname_r() . buf is a buffer of
 *    length len bytes which is used to store the h_name, h_aliases, and
 *    h_addr_list elements of the struct hostent returned in resbuf.
 *    Successful calls to lwres_gethostbyname_r() return resbuf, which is a
 *    pointer to the struct hostent it created.
 * 
 *    lwres_gethostbyaddr_r() is a thread-safe function that performs a
 *    reverse lookup of address addr which is len bytes long and is of
 *    protocol family type -- PF_INET or PF_INET6. If an error occurs, the
 *    error code is returned in *error. The other function parameters are
 *    identical to those in lwres_gethostbyname_r(). resbuf is a pointer to
 *    a struct hostent which is initialised by a successful call to
 *    lwres_gethostbyaddr_r(). buf is a buffer of length len bytes which is
 *    used to store the h_name, h_aliases, and h_addr_list elements of the
 *    struct hostent returned in resbuf. Successful calls to
 *    lwres_gethostbyaddr_r() return resbuf, which is a pointer to the
 *    struct hostent it created.
 * 
 * \section gethost_return Return Values
 * 
 *    The functions lwres_gethostbyname(), lwres_gethostbyname2(),
 *    lwres_gethostbyaddr(), and lwres_gethostent() return NULL to indicate
 *    an error. In this case the global variable lwres_h_errno will contain
 *    one of the following error codes defined in \link netdb.h <lwres/netdb.h>:\endlink
 * 
 * \li #HOST_NOT_FOUND:
 *           The host or address was not found.
 * 
 * \li #TRY_AGAIN:
 *           A recoverable error occurred, e.g., a timeout. Retrying the
 *           lookup may succeed.
 * 
 * \li #NO_RECOVERY:
 *           A non-recoverable error occurred.
 * 
 * \li #NO_DATA:
 *           The name exists, but has no address information associated with
 *           it (or vice versa in the case of a reverse lookup). The code
 *           NO_ADDRESS is accepted as a synonym for NO_DATA for backwards
 *           compatibility.
 * 
 *    lwres_hstrerror() translates these error codes to suitable error
 *    messages.
 * 
 *    lwres_gethostent() and lwres_gethostent_r() always return NULL.
 * 
 *    Successful calls to lwres_gethostbyname_r() and
 *    lwres_gethostbyaddr_r() return resbuf, a pointer to the struct hostent
 *    that was initialised by these functions. They return NULL if the
 *    lookups fail or if buf was too small to hold the list of addresses and
 *    names referenced by the h_name, h_aliases, and h_addr_list elements of
 *    the struct hostent. If buf was too small, both lwres_gethostbyname_r()
 *    and lwres_gethostbyaddr_r() set the global variable errno to ERANGE.
 * 
 * \section gethost_see See Also
 * 
 *    gethostent(), \link getipnode.c getipnode\endlink, lwres_hstrerror()
 * 
 * \section gethost_bugs Bugs
 * 
 *    lwres_gethostbyname(), lwres_gethostbyname2(), lwres_gethostbyaddr()
 *    and lwres_endhostent() are not thread safe; they return pointers to
 *    static data and provide error codes through a global variable.
 *    Thread-safe versions for name and address lookup are provided by
 *    lwres_gethostbyname_r(), and lwres_gethostbyaddr_r() respectively.
 * 
 *    The resolver daemon does not currently support any non-DNS name
 *    services such as /etc/hosts or NIS, consequently the above functions
 *    don't, either.
 */

#include <config.h>

#include <errno.h>
#include <string.h>

#include <lwres/net.h>
#include <lwres/netdb.h>

#include "assert_p.h"

#define LWRES_ALIGNBYTES (sizeof(char *) - 1)
#define LWRES_ALIGN(p) \
	(((unsigned long)(p) + LWRES_ALIGNBYTES) &~ LWRES_ALIGNBYTES)

static struct hostent *he = NULL;
static int copytobuf(struct hostent *, struct hostent *, char *, int);

/*% Always looks for an IPv4 address. */
struct hostent *
lwres_gethostbyname(const char *name) {

	if (he != NULL)
		lwres_freehostent(he);

	he = lwres_getipnodebyname(name, AF_INET, 0, &lwres_h_errno);
	return (he);
}

/*% Looks for either an IPv4 or IPv6 address. */
struct hostent *
lwres_gethostbyname2(const char *name, int af) {
	if (he != NULL)
		lwres_freehostent(he);

	he = lwres_getipnodebyname(name, af, 0, &lwres_h_errno);
	return (he);
}

/*% Reverse lookup of addresses. */
struct hostent *
lwres_gethostbyaddr(const char *addr, int len, int type) {

	if (he != NULL)
		lwres_freehostent(he);

	he = lwres_getipnodebyaddr(addr, len, type, &lwres_h_errno);
	return (he);
}

/*% Stub function.  Always returns failure. */
struct hostent *
lwres_gethostent(void) {
	if (he != NULL)
		lwres_freehostent(he);

	return (NULL);
}

/*% Stub function.  Always returns failure. */
void
lwres_sethostent(int stayopen) {
	/*
	 * Empty.
	 */
	UNUSED(stayopen);
}

/*% Stub function.  Always returns failure. */
void
lwres_endhostent(void) {
	/*
	 * Empty.
	 */
}

/*% Thread-safe function for forward lookups. */
struct hostent *
lwres_gethostbyname_r(const char *name, struct hostent *resbuf,
		char *buf, int buflen, int *error)
{
	struct hostent *he;
	int res;

	he = lwres_getipnodebyname(name, AF_INET, 0, error);
	if (he == NULL)
		return (NULL);
	res = copytobuf(he, resbuf, buf, buflen);
	lwres_freehostent(he);
	if (res != 0) {
		errno = ERANGE;
		return (NULL);
	}
	return (resbuf);
}

/*% Thread-safe reverse lookup. */
struct hostent  *
lwres_gethostbyaddr_r(const char *addr, int len, int type,
		      struct hostent *resbuf, char *buf, int buflen,
		      int *error)
{
	struct hostent *he;
	int res;

	he = lwres_getipnodebyaddr(addr, len, type, error);
	if (he == NULL)
		return (NULL);
	res = copytobuf(he, resbuf, buf, buflen);
	lwres_freehostent(he);
	if (res != 0) {
		errno = ERANGE;
		return (NULL);
	}
	return (resbuf);
}

/*% Stub function.  Always returns failure. */
struct hostent  *
lwres_gethostent_r(struct hostent *resbuf, char *buf, int buflen, int *error) {
	UNUSED(resbuf);
	UNUSED(buf);
	UNUSED(buflen);
	*error = 0;
	return (NULL);
}

/*% Stub function.  Always returns failure. */
void
lwres_sethostent_r(int stayopen) {
	/*
	 * Empty.
	 */
	UNUSED(stayopen);
}

/*% Stub function.  Always returns failure. */
void
lwres_endhostent_r(void) {
	/*
	 * Empty.
	 */
}

static int
copytobuf(struct hostent *he, struct hostent *hptr, char *buf, int buflen) {
        char *cp;
        char **ptr;
        int i, n;
        int nptr, len;

        /*
	 * Find out the amount of space required to store the answer.
	 */
        nptr = 2; /* NULL ptrs */
        len = (char *)LWRES_ALIGN(buf) - buf;
        for (i = 0; he->h_addr_list[i]; i++, nptr++) {
                len += he->h_length;
        }
        for (i = 0; he->h_aliases[i]; i++, nptr++) {
                len += strlen(he->h_aliases[i]) + 1;
        }
        len += strlen(he->h_name) + 1;
        len += nptr * sizeof(char*);

        if (len > buflen) {
                return (-1);
        }

        /*
	 * Copy address size and type.
	 */
        hptr->h_addrtype = he->h_addrtype;
        n = hptr->h_length = he->h_length;

        ptr = (char **)LWRES_ALIGN(buf);
        cp = (char *)LWRES_ALIGN(buf) + nptr * sizeof(char *);

        /*
	 * Copy address list.
	 */
        hptr->h_addr_list = ptr;
        for (i = 0; he->h_addr_list[i]; i++, ptr++) {
                memcpy(cp, he->h_addr_list[i], n);
                hptr->h_addr_list[i] = cp;
                cp += n;
        }
        hptr->h_addr_list[i] = NULL;
        ptr++;

        /*
	 * Copy official name.
	 */
	n = (buf + buflen) - cp;
	{
		int r;
        	r = strlcpy(cp, he->h_name, n);
		if ( r < n )
			n = r;
	}
        hptr->h_name = cp;
        cp += n;

        /*
	 * Copy aliases.
	 */
        hptr->h_aliases = ptr;
        for (i = 0; he->h_aliases[i]; i++) {
                n = (buf + buflen) - cp;
		{
			int r;
                	r = strlcpy(cp, he->h_aliases[i], n);
			if ( r < n )
				n = r;
		}
			
                hptr->h_aliases[i] = cp;
                cp += n;
        }
        hptr->h_aliases[i] = NULL;

        return (0);
}