2 * Copyright (c) 1994, Garrett Wollman
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "namespace.h"
30 #include "reentrant.h"
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
43 #include <arpa/nameser.h> /* XXX hack for _res */
44 #include <resolv.h> /* XXX hack for _res */
45 #include "un-namespace.h"
46 #include "netdb_private.h"
51 extern int _ht_gethostbyname(void *, void *, va_list);
52 extern int _dns_gethostbyname(void *, void *, va_list);
53 extern int _nis_gethostbyname(void *, void *, va_list);
54 extern int _ht_gethostbyaddr(void *, void *, va_list);
55 extern int _dns_gethostbyaddr(void *, void *, va_list);
56 extern int _nis_gethostbyaddr(void *, void *, va_list);
58 static int gethostbyname_internal(const char *, int, struct hostent *, char *,
59 size_t, struct hostent **, int *, res_state);
61 /* Host lookup order if nsswitch.conf is broken or nonexistant */
62 static const ns_src default_src[] = {
63 { NSSRC_FILES, NS_SUCCESS },
64 { NSSRC_DNS, NS_SUCCESS },
68 static int host_id_func(char *, size_t *, va_list, void *);
69 static int host_marshal_func(char *, size_t *, void *, va_list, void *);
70 static int host_unmarshal_func(char *, size_t, void *, va_list, void *);
73 NETDB_THREAD_ALLOC(hostent)
74 NETDB_THREAD_ALLOC(hostent_data)
75 NETDB_THREAD_ALLOC(hostdata)
78 hostent_free(void *ptr)
84 hostent_data_free(void *ptr)
86 struct hostent_data *hed = ptr;
96 hostdata_free(void *ptr)
102 __copy_hostent(struct hostent *he, struct hostent *hptr, char *buf,
110 /* Find out the amount of space required to store the answer. */
111 nptr = 2; /* NULL ptrs */
112 len = (char *)ALIGN(buf) - buf;
113 for (i = 0; he->h_addr_list[i]; i++, nptr++) {
116 for (i = 0; he->h_aliases[i]; i++, nptr++) {
117 len += strlen(he->h_aliases[i]) + 1;
119 len += strlen(he->h_name) + 1;
120 len += nptr * sizeof(char*);
127 /* copy address size and type */
128 hptr->h_addrtype = he->h_addrtype;
129 n = hptr->h_length = he->h_length;
131 ptr = (char **)ALIGN(buf);
132 cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
134 /* copy address list */
135 hptr->h_addr_list = ptr;
136 for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
137 memcpy(cp, he->h_addr_list[i], n);
138 hptr->h_addr_list[i] = cp;
141 hptr->h_addr_list[i] = NULL;
144 /* copy official name */
145 n = strlen(he->h_name) + 1;
146 strcpy(cp, he->h_name);
151 hptr->h_aliases = ptr;
152 for (i = 0 ; he->h_aliases[i]; i++) {
153 n = strlen(he->h_aliases[i]) + 1;
154 strcpy(cp, he->h_aliases[i]);
155 hptr->h_aliases[i] = cp;
158 hptr->h_aliases[i] = NULL;
165 host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
176 size_t desired_size, size;
177 enum nss_lookup_type lookup_type;
179 int res = NS_UNAVAIL;
181 statp = __res_state();
182 res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
183 RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
185 lookup_type = (enum nss_lookup_type)cache_mdata;
186 switch (lookup_type) {
188 str = va_arg(ap, char *);
189 type = va_arg(ap, int);
192 desired_size = sizeof(res_options) + sizeof(int) +
193 sizeof(enum nss_lookup_type) + sizeof(int) + size + 1;
195 if (desired_size > *buffer_size) {
202 memcpy(p, &res_options, sizeof(res_options));
203 p += sizeof(res_options);
205 memcpy(p, &op_id, sizeof(int));
208 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
211 memcpy(p, &type, sizeof(int));
214 memcpy(p, str, size + 1);
219 addr = va_arg(ap, void *);
220 len = va_arg(ap, socklen_t);
221 type = va_arg(ap, int);
223 desired_size = sizeof(res_options) + sizeof(int) +
224 sizeof(enum nss_lookup_type) + sizeof(int) +
225 sizeof(socklen_t) + len;
227 if (desired_size > *buffer_size) {
233 memcpy(p, &res_options, sizeof(res_options));
234 p += sizeof(res_options);
236 memcpy(p, &op_id, sizeof(int));
239 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
242 memcpy(p, &type, sizeof(int));
245 memcpy(p, &len, sizeof(socklen_t));
246 p += sizeof(socklen_t);
248 memcpy(p, addr, len);
253 /* should be unreachable */
258 *buffer_size = desired_size;
263 host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
272 struct hostent new_ht;
273 size_t desired_size, aliases_size, addr_size, size;
276 switch ((enum nss_lookup_type)cache_mdata) {
278 str = va_arg(ap, char *);
279 type = va_arg(ap, int);
282 addr = va_arg(ap, void *);
283 len = va_arg(ap, socklen_t);
284 type = va_arg(ap, int);
287 /* should be unreachable */
290 ht = va_arg(ap, struct hostent *);
292 desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *);
293 if (ht->h_name != NULL)
294 desired_size += strlen(ht->h_name) + 1;
296 if (ht->h_aliases != NULL) {
298 for (iter = ht->h_aliases; *iter; ++iter) {
299 desired_size += strlen(*iter) + 1;
303 desired_size += _ALIGNBYTES +
304 (aliases_size + 1) * sizeof(char *);
307 if (ht->h_addr_list != NULL) {
309 for (iter = ht->h_addr_list; *iter; ++iter)
312 desired_size += addr_size * _ALIGN(ht->h_length);
313 desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *);
316 if (desired_size > *buffer_size) {
317 /* this assignment is here for future use */
318 *buffer_size = desired_size;
322 memcpy(&new_ht, ht, sizeof(struct hostent));
323 memset(buffer, 0, desired_size);
325 *buffer_size = desired_size;
326 p = buffer + sizeof(struct hostent) + sizeof(char *);
327 memcpy(buffer + sizeof(struct hostent), &p, sizeof(char *));
328 p = (char *)_ALIGN(p);
330 if (new_ht.h_name != NULL) {
331 size = strlen(new_ht.h_name);
332 memcpy(p, new_ht.h_name, size);
337 if (new_ht.h_aliases != NULL) {
338 p = (char *)_ALIGN(p);
339 memcpy(p, new_ht.h_aliases, sizeof(char *) * aliases_size);
340 new_ht.h_aliases = (char **)p;
341 p += sizeof(char *) * (aliases_size + 1);
343 for (iter = new_ht.h_aliases; *iter; ++iter) {
344 size = strlen(*iter);
345 memcpy(p, *iter, size);
351 if (new_ht.h_addr_list != NULL) {
352 p = (char *)_ALIGN(p);
353 memcpy(p, new_ht.h_addr_list, sizeof(char *) * addr_size);
354 new_ht.h_addr_list = (char **)p;
355 p += sizeof(char *) * (addr_size + 1);
357 size = _ALIGN(new_ht.h_length);
358 for (iter = new_ht.h_addr_list; *iter; ++iter) {
359 memcpy(p, *iter, size);
364 memcpy(buffer, &new_ht, sizeof(struct hostent));
369 host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
381 size_t orig_buf_size;
383 switch ((enum nss_lookup_type)cache_mdata) {
385 str = va_arg(ap, char *);
386 type = va_arg(ap, int);
389 addr = va_arg(ap, void *);
390 len = va_arg(ap, socklen_t);
391 type = va_arg(ap, int);
394 /* should be unreachable */
398 ht = va_arg(ap, struct hostent *);
399 orig_buf = va_arg(ap, char *);
400 orig_buf_size = va_arg(ap, size_t);
403 buffer_size - sizeof(struct hostent) - sizeof(char *)) {
408 memcpy(ht, buffer, sizeof(struct hostent));
409 memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *));
411 orig_buf = (char *)_ALIGN(orig_buf);
412 memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) +
413 _ALIGN(p) - (size_t)p,
414 buffer_size - sizeof(struct hostent) - sizeof(char *) -
415 _ALIGN(p) + (size_t)p);
416 p = (char *)_ALIGN(p);
418 NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *);
419 if (ht->h_aliases != NULL) {
420 NS_APPLY_OFFSET(ht->h_aliases, orig_buf, p, char **);
422 for (iter = ht->h_aliases; *iter; ++iter)
423 NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
426 if (ht->h_addr_list != NULL) {
427 NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **);
429 for (iter = ht->h_addr_list; *iter; ++iter)
430 NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
433 *((struct hostent **)retval) = ht;
436 #endif /* NS_CACHING */
439 fakeaddr(const char *name, int af, struct hostent *hp, char *buf,
440 size_t buflen, res_state statp)
442 struct hostent_data *hed;
445 if ((hed = __hostent_data_init()) == NULL) {
447 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
451 if ((af != AF_INET ||
452 inet_aton(name, (struct in_addr *)hed->host_addr) != 1) &&
453 inet_pton(af, name, hed->host_addr) != 1) {
454 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
457 strncpy(hed->hostbuf, name, MAXDNAME);
458 hed->hostbuf[MAXDNAME] = '\0';
459 if (af == AF_INET && (statp->options & RES_USE_INET6) != 0U) {
460 _map_v4v6_address((char *)hed->host_addr,
461 (char *)hed->host_addr);
467 he.h_length = NS_INADDRSZ;
470 he.h_length = NS_IN6ADDRSZ;
473 errno = EAFNOSUPPORT;
474 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
477 he.h_name = hed->hostbuf;
478 he.h_aliases = hed->host_aliases;
479 hed->host_aliases[0] = NULL;
480 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
481 hed->h_addr_ptrs[1] = NULL;
482 he.h_addr_list = hed->h_addr_ptrs;
483 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
484 return (__copy_hostent(&he, hp, buf, buflen));
488 gethostbyname_r(const char *name, struct hostent *he, char *buffer,
489 size_t buflen, struct hostent **result, int *h_errnop)
493 statp = __res_state();
494 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
495 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
498 if (statp->options & RES_USE_INET6) {
499 if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) {
503 if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen,
504 result, h_errnop, statp) == 0)
507 return (gethostbyname_internal(name, AF_INET, he, buffer, buflen,
508 result, h_errnop, statp));
512 gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer,
513 size_t buflen, struct hostent **result, int *h_errnop)
517 statp = __res_state();
518 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
519 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
522 return (gethostbyname_internal(name, af, he, buffer, buflen, result,
527 gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf,
528 size_t buflen, struct hostent **result, int *h_errnop, res_state statp)
535 static const nss_cache_info cache_info =
536 NS_COMMON_CACHE_INFO_INITIALIZER(
537 hosts, (void *)nss_lt_name,
538 host_id_func, host_marshal_func, host_unmarshal_func);
540 static const ns_dtab dtab[] = {
541 NS_FILES_CB(_ht_gethostbyname, NULL)
542 { NSSRC_DNS, _dns_gethostbyname, NULL },
543 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
545 NS_CACHE_CB(&cache_info)
555 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
556 *h_errnop = statp->res_h_errno;
557 errno = EAFNOSUPPORT;
562 * if there aren't any dots, it could be a user-level alias.
563 * this is also done in res_query() since we are not the only
564 * function that looks up host names.
566 if (!strchr(name, '.') &&
567 (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
570 if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
575 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
576 "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
577 &ret_errno, h_errnop);
579 return ((rval == NS_SUCCESS) ? 0 : -1);
583 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
584 char *buf, size_t buflen, struct hostent **result, int *h_errnop)
586 const u_char *uaddr = (const u_char *)addr;
587 const struct in6_addr *addr6;
593 static const nss_cache_info cache_info =
594 NS_COMMON_CACHE_INFO_INITIALIZER(
595 hosts, (void *)nss_lt_id,
596 host_id_func, host_marshal_func, host_unmarshal_func);
598 static const ns_dtab dtab[] = {
599 NS_FILES_CB(_ht_gethostbyaddr, NULL)
600 { NSSRC_DNS, _dns_gethostbyaddr, NULL },
601 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
603 NS_CACHE_CB(&cache_info)
608 statp = __res_state();
609 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
610 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
611 *h_errnop = statp->res_h_errno;
615 if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
616 addr6 = (const struct in6_addr *)addr;
617 if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
618 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
619 *h_errnop = statp->res_h_errno;
622 if (IN6_IS_ADDR_V4MAPPED(addr6) ||
623 IN6_IS_ADDR_V4COMPAT(addr6)) {
625 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
638 errno = EAFNOSUPPORT;
639 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
640 *h_errnop = statp->res_h_errno;
645 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
646 *h_errnop = statp->res_h_errno;
650 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
651 "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
652 &ret_errno, h_errnop);
654 return ((rval == NS_SUCCESS) ? 0 : -1);
658 gethostbyname(const char *name)
661 struct hostent *rval;
664 if ((hd = __hostdata_init()) == NULL)
666 if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
673 gethostbyname2(const char *name, int af)
676 struct hostent *rval;
679 if ((hd = __hostdata_init()) == NULL)
681 if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
682 &rval, &ret_h_errno) != 0)
688 gethostbyaddr(const void *addr, socklen_t len, int af)
691 struct hostent *rval;
694 if ((hd = __hostdata_init()) == NULL)
696 if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
697 sizeof(hd->data), &rval, &ret_h_errno) != 0)
703 sethostent(int stayopen)
705 struct hostent_data *hed;
707 if ((hed = __hostent_data_init()) == NULL)
709 _sethosthtent(stayopen, hed);
710 _sethostdnsent(stayopen);
716 struct hostent_data *hed;
718 if ((hed = __hostent_data_init()) == NULL)