]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/net/gethostnamadr.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libc / net / gethostnamadr.c
1 /*-
2  * Copyright (c) 1994, Garrett Wollman
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
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>
35 #include <netdb.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <nsswitch.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"
47 #ifdef NS_CACHING
48 #include "nscache.h"
49 #endif
50
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);
57
58 static int gethostbyname_internal(const char *, int, struct hostent *, char *,
59     size_t, struct hostent **, int *, res_state);
60
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 },
65         { 0 }
66 };
67 #ifdef NS_CACHING
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 *);
71 #endif
72
73 NETDB_THREAD_ALLOC(hostent)
74 NETDB_THREAD_ALLOC(hostent_data)
75 NETDB_THREAD_ALLOC(hostdata)
76
77 static void
78 hostent_free(void *ptr)
79 {
80         free(ptr);
81 }
82
83 static void
84 hostent_data_free(void *ptr)
85 {
86         struct hostent_data *hed = ptr;
87
88         if (hed == NULL)
89                 return;
90         hed->stayopen = 0;
91         _endhosthtent(hed);
92         free(hed);
93 }
94
95 static void
96 hostdata_free(void *ptr)
97 {
98         free(ptr);
99 }
100
101 int
102 __copy_hostent(struct hostent *he, struct hostent *hptr, char *buf,
103     size_t buflen)
104 {
105         char *cp;
106         char **ptr;
107         int i, n;
108         int nptr, len;
109
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++) {
114                 len += he->h_length;
115         }
116         for (i = 0; he->h_aliases[i]; i++, nptr++) {
117                 len += strlen(he->h_aliases[i]) + 1;
118         }
119         len += strlen(he->h_name) + 1;
120         len += nptr * sizeof(char*);
121
122         if (len > buflen) {
123                 errno = ERANGE;
124                 return (-1);
125         }
126
127         /* copy address size and type */
128         hptr->h_addrtype = he->h_addrtype;
129         n = hptr->h_length = he->h_length;
130
131         ptr = (char **)ALIGN(buf);
132         cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
133
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;
139                 cp += n;
140         }
141         hptr->h_addr_list[i] = NULL;
142         ptr++;
143
144         /* copy official name */
145         n = strlen(he->h_name) + 1;
146         strcpy(cp, he->h_name);
147         hptr->h_name = cp;
148         cp += n;
149
150         /* copy aliases */
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;
156                 cp += n;
157         }
158         hptr->h_aliases[i] = NULL;
159
160         return (0);
161 }
162
163 #ifdef NS_CACHING
164 static int
165 host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
166 {
167         res_state statp;
168         u_long res_options;
169
170         const int op_id = 1;
171         char *str;
172         void *addr;
173         socklen_t len;
174         int type;
175
176         size_t desired_size, size;
177         enum nss_lookup_type lookup_type;
178         char *p;
179         int res = NS_UNAVAIL;
180
181         statp = __res_state();
182         res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
183             RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
184
185         lookup_type = (enum nss_lookup_type)cache_mdata;
186         switch (lookup_type) {
187         case nss_lt_name:
188                 str = va_arg(ap, char *);
189                 type = va_arg(ap, int);
190
191                 size = strlen(str);
192                 desired_size = sizeof(res_options) + sizeof(int) +
193                     sizeof(enum nss_lookup_type) + sizeof(int) + size + 1;
194
195                 if (desired_size > *buffer_size) {
196                         res = NS_RETURN;
197                         goto fin;
198                 }
199
200                 p = buffer;
201
202                 memcpy(p, &res_options, sizeof(res_options));
203                 p += sizeof(res_options);
204
205                 memcpy(p, &op_id, sizeof(int));
206                 p += sizeof(int);
207
208                 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
209                 p += sizeof(int);
210
211                 memcpy(p, &type, sizeof(int));
212                 p += sizeof(int);
213
214                 memcpy(p, str, size + 1);
215
216                 res = NS_SUCCESS;
217                 break;
218         case nss_lt_id:
219                 addr = va_arg(ap, void *);
220                 len = va_arg(ap, socklen_t);
221                 type = va_arg(ap, int);
222
223                 desired_size = sizeof(res_options) + sizeof(int) +
224                     sizeof(enum nss_lookup_type) + sizeof(int) +
225                     sizeof(socklen_t) + len;
226
227                 if (desired_size > *buffer_size) {
228                         res = NS_RETURN;
229                         goto fin;
230                 }
231
232                 p = buffer;
233                 memcpy(p, &res_options, sizeof(res_options));
234                 p += sizeof(res_options);
235
236                 memcpy(p, &op_id, sizeof(int));
237                 p += sizeof(int);
238
239                 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
240                 p += sizeof(int);
241
242                 memcpy(p, &type, sizeof(int));
243                 p += sizeof(int);
244
245                 memcpy(p, &len, sizeof(socklen_t));
246                 p += sizeof(socklen_t);
247
248                 memcpy(p, addr, len);
249
250                 res = NS_SUCCESS;
251                 break;
252         default:
253                 /* should be unreachable */
254                 return (NS_UNAVAIL);
255         }
256
257 fin:
258         *buffer_size = desired_size;
259         return (res);
260 }
261
262 static int
263 host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
264     void *cache_mdata)
265 {
266         char *str;
267         void *addr;
268         socklen_t len;
269         int type;
270         struct hostent *ht;
271
272         struct hostent new_ht;
273         size_t desired_size, aliases_size, addr_size, size;
274         char *p, **iter;
275
276         switch ((enum nss_lookup_type)cache_mdata) {
277         case nss_lt_name:
278                 str = va_arg(ap, char *);
279                 type = va_arg(ap, int);
280                 break;
281         case nss_lt_id:
282                 addr = va_arg(ap, void *);
283                 len = va_arg(ap, socklen_t);
284                 type = va_arg(ap, int);
285                 break;
286         default:
287                 /* should be unreachable */
288                 return (NS_UNAVAIL);
289         }
290         ht = va_arg(ap, struct hostent *);
291
292         desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *);
293         if (ht->h_name != NULL)
294                 desired_size += strlen(ht->h_name) + 1;
295
296         if (ht->h_aliases != NULL) {
297                 aliases_size = 0;
298                 for (iter = ht->h_aliases; *iter; ++iter) {
299                         desired_size += strlen(*iter) + 1;
300                         ++aliases_size;
301                 }
302
303                 desired_size += _ALIGNBYTES +
304                     (aliases_size + 1) * sizeof(char *);
305         }
306
307         if (ht->h_addr_list != NULL) {
308                 addr_size = 0;
309                 for (iter = ht->h_addr_list; *iter; ++iter)
310                         ++addr_size;
311
312                 desired_size += addr_size * _ALIGN(ht->h_length);
313                 desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *);
314         }
315
316         if (desired_size > *buffer_size) {
317                 /* this assignment is here for future use */
318                 *buffer_size = desired_size;
319                 return (NS_RETURN);
320         }
321
322         memcpy(&new_ht, ht, sizeof(struct hostent));
323         memset(buffer, 0, desired_size);
324
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);
329
330         if (new_ht.h_name != NULL) {
331                 size = strlen(new_ht.h_name);
332                 memcpy(p, new_ht.h_name, size);
333                 new_ht.h_name = p;
334                 p += size + 1;
335         }
336
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);
342
343                 for (iter = new_ht.h_aliases; *iter; ++iter) {
344                         size = strlen(*iter);
345                         memcpy(p, *iter, size);
346                         *iter = p;
347                         p += size + 1;
348                 }
349         }
350
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);
356
357                 size = _ALIGN(new_ht.h_length);
358                 for (iter = new_ht.h_addr_list; *iter; ++iter) {
359                         memcpy(p, *iter, size);
360                         *iter = p;
361                         p += size + 1;
362                 }
363         }
364         memcpy(buffer, &new_ht, sizeof(struct hostent));
365         return (NS_SUCCESS);
366 }
367
368 static int
369 host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
370     void *cache_mdata)
371 {
372         char *str;
373         void *addr;
374         socklen_t len;
375         int type;
376         struct hostent *ht;
377
378         char *p;
379         char **iter;
380         char *orig_buf;
381         size_t orig_buf_size;
382
383         switch ((enum nss_lookup_type)cache_mdata) {
384         case nss_lt_name:
385                 str = va_arg(ap, char *);
386                 type = va_arg(ap, int);
387                 break;
388         case nss_lt_id:
389                 addr = va_arg(ap, void *);
390                 len = va_arg(ap, socklen_t);
391                 type = va_arg(ap, int);
392                 break;
393         default:
394                 /* should be unreachable */
395                 return (NS_UNAVAIL);
396         }
397
398         ht = va_arg(ap, struct hostent *);
399         orig_buf = va_arg(ap, char *);
400         orig_buf_size = va_arg(ap, size_t);
401
402         if (orig_buf_size <
403             buffer_size - sizeof(struct hostent) - sizeof(char *)) {
404                 errno = ERANGE;
405                 return (NS_RETURN);
406         }
407
408         memcpy(ht, buffer, sizeof(struct hostent));
409         memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *));
410
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);
417
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 **);
421
422                 for (iter = ht->h_aliases; *iter; ++iter)
423                         NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
424         }
425
426         if (ht->h_addr_list != NULL) {
427                 NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **);
428
429                 for (iter = ht->h_addr_list; *iter; ++iter)
430                         NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
431         }
432
433         *((struct hostent **)retval) = ht;
434         return (NS_SUCCESS);
435 }
436 #endif /* NS_CACHING */
437
438 static int
439 fakeaddr(const char *name, int af, struct hostent *hp, char *buf,
440     size_t buflen, res_state statp)
441 {
442         struct hostent_data *hed;
443         struct hostent he;
444
445         if ((hed = __hostent_data_init()) == NULL) {
446                 errno = ENOMEM;
447                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
448                 return (-1);
449         }
450
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);
455                 return (-1);
456         }
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);
462                 af = AF_INET6;
463         }
464         he.h_addrtype = af;
465         switch(af) {
466         case AF_INET:
467                 he.h_length = NS_INADDRSZ;
468                 break;
469         case AF_INET6:
470                 he.h_length = NS_IN6ADDRSZ;
471                 break;
472         default:
473                 errno = EAFNOSUPPORT;
474                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
475                 return (-1);
476         }
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         if (__copy_hostent(&he, hp, buf, buflen) != 0) {
484                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
485                 return (-1);
486         }
487         RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
488         return (0);
489 }
490
491 int
492 gethostbyname_r(const char *name, struct hostent *he, char *buffer,
493     size_t buflen, struct hostent **result, int *h_errnop)
494 {
495         res_state statp;
496
497         statp = __res_state();
498         if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
499                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
500                 return (-1);
501         }
502         if (statp->options & RES_USE_INET6) {
503                 if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) {
504                         *result = he;
505                         return (0);
506                 }
507                 if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen,
508                     result, h_errnop, statp) == 0)
509                         return (0);
510         }
511         return (gethostbyname_internal(name, AF_INET, he, buffer, buflen,
512             result, h_errnop, statp));
513 }
514
515 int
516 gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer,
517     size_t buflen, struct hostent **result, int *h_errnop)
518 {
519         res_state statp;
520
521         statp = __res_state();
522         if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
523                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
524                 return (-1);
525         }
526         return (gethostbyname_internal(name, af, he, buffer, buflen, result,
527             h_errnop, statp));
528 }
529
530 int
531 gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf,
532     size_t buflen, struct hostent **result, int *h_errnop, res_state statp)
533 {
534         const char *cp;
535         int rval, ret_errno = 0;
536         char abuf[MAXDNAME];
537
538 #ifdef NS_CACHING
539         static const nss_cache_info cache_info =
540                 NS_COMMON_CACHE_INFO_INITIALIZER(
541                 hosts, (void *)nss_lt_name,
542                 host_id_func, host_marshal_func, host_unmarshal_func);
543 #endif
544         static const ns_dtab dtab[] = {
545                 NS_FILES_CB(_ht_gethostbyname, NULL)
546                 { NSSRC_DNS, _dns_gethostbyname, NULL },
547                 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
548 #ifdef NS_CACHING
549                 NS_CACHE_CB(&cache_info)
550 #endif
551                 { 0 }
552         };
553
554         switch (af) {
555         case AF_INET:
556         case AF_INET6:
557                 break;
558         default:
559                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
560                 *h_errnop = statp->res_h_errno;
561                 errno = EAFNOSUPPORT;
562                 return (-1);
563         }
564
565         /*
566          * if there aren't any dots, it could be a user-level alias.
567          * this is also done in res_query() since we are not the only
568          * function that looks up host names.
569          */
570         if (!strchr(name, '.') &&
571             (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
572                 name = cp;
573
574         if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
575                 *result = hp;
576                 return (0);
577         }
578
579         rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
580             "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
581             &ret_errno, h_errnop);
582
583         if (rval != NS_SUCCESS) {
584                 errno = ret_errno;
585                 return ((ret_errno != 0) ? ret_errno : -1);
586         }
587         return (0);
588 }
589
590 int
591 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
592     char *buf, size_t buflen, struct hostent **result, int *h_errnop)
593 {
594         const u_char *uaddr = (const u_char *)addr;
595         const struct in6_addr *addr6;
596         socklen_t size;
597         int rval, ret_errno = 0;
598         res_state statp;
599
600 #ifdef NS_CACHING
601         static const nss_cache_info cache_info =
602                 NS_COMMON_CACHE_INFO_INITIALIZER(
603                 hosts, (void *)nss_lt_id,
604                 host_id_func, host_marshal_func, host_unmarshal_func);
605 #endif
606         static const ns_dtab dtab[] = {
607                 NS_FILES_CB(_ht_gethostbyaddr, NULL)
608                 { NSSRC_DNS, _dns_gethostbyaddr, NULL },
609                 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
610 #ifdef NS_CACHING
611                 NS_CACHE_CB(&cache_info)
612 #endif
613                 { 0 }
614         };
615
616         statp = __res_state();
617         if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
618                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
619                 *h_errnop = statp->res_h_errno;
620                 return (-1);
621         }
622
623         if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
624                 addr6 = (const struct in6_addr *)addr;
625                 if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
626                         RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
627                         *h_errnop = statp->res_h_errno;
628                         return (-1);
629                 }
630                 if (IN6_IS_ADDR_V4MAPPED(addr6) ||
631                     IN6_IS_ADDR_V4COMPAT(addr6)) {
632                         /* Unmap. */
633                         uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
634                         af = AF_INET;
635                         len = NS_INADDRSZ;
636                 }
637         }
638         switch (af) {
639         case AF_INET:
640                 size = NS_INADDRSZ;
641                 break;
642         case AF_INET6:
643                 size = NS_IN6ADDRSZ;
644                 break;
645         default:
646                 errno = EAFNOSUPPORT;
647                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
648                 *h_errnop = statp->res_h_errno;
649                 return (-1);
650         }
651         if (size != len) {
652                 errno = EINVAL;
653                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
654                 *h_errnop = statp->res_h_errno;
655                 return (-1);
656         }
657
658         rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
659             "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
660             &ret_errno, h_errnop);
661
662         if (rval != NS_SUCCESS) {
663                 errno = ret_errno;
664                 return ((ret_errno != 0) ? ret_errno : -1);
665         }
666         return (0);
667 }
668
669 struct hostent *
670 gethostbyname(const char *name)
671 {
672         struct hostdata *hd;
673         struct hostent *rval;
674         int ret_h_errno;
675
676         if ((hd = __hostdata_init()) == NULL)
677                 return (NULL);
678         if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
679             &ret_h_errno) != 0)
680                 return (NULL);
681         return (rval);
682 }
683
684 struct hostent *
685 gethostbyname2(const char *name, int af)
686 {
687         struct hostdata *hd;
688         struct hostent *rval;
689         int ret_h_errno;
690
691         if ((hd = __hostdata_init()) == NULL)
692                 return (NULL);
693         if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
694             &rval, &ret_h_errno) != 0)
695                 return (NULL);
696         return (rval);
697 }
698
699 struct hostent *
700 gethostbyaddr(const void *addr, socklen_t len, int af)
701 {
702         struct hostdata *hd;
703         struct hostent *rval;
704         int ret_h_errno;
705
706         if ((hd = __hostdata_init()) == NULL)
707                 return (NULL);
708         if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
709             sizeof(hd->data), &rval, &ret_h_errno) != 0)
710                 return (NULL);
711         return (rval);
712 }
713
714 void
715 sethostent(int stayopen)
716 {
717         struct hostent_data *hed;
718
719         if ((hed = __hostent_data_init()) == NULL)
720                 return;
721         _sethosthtent(stayopen, hed);
722         _sethostdnsent(stayopen);
723 }
724
725 void
726 endhostent(void)
727 {
728         struct hostent_data *hed;
729
730         if ((hed = __hostent_data_init()) == NULL)
731                 return;
732         _endhosthtent(hed);
733         _endhostdnsent();
734 }