source: rtems-libbsd/freebsd/lib/libc/net/gethostnamadr.c @ e599318

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

  • Property mode set to 100644
File size: 17.3 KB
Line 
1#include "port_before.h"
2
3/*-
4 * Copyright (c) 1994, Garrett Wollman
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include "namespace.h"
32#include "reentrant.h"
33#include <rtems/bsd/sys/param.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <netdb.h>
38#include <stdio.h>
39#include <ctype.h>
40#include <errno.h>
41#include <stdlib.h>
42#include <string.h>
43#include <stdarg.h>
44#include <nsswitch.h>
45#include <arpa/nameser.h>               /* XXX hack for _res */
46#include <resolv.h>                     /* XXX hack for _res */
47#include "un-namespace.h"
48#include "netdb_private.h"
49#ifdef NS_CACHING
50#include "nscache.h"
51#endif
52
53extern int _ht_gethostbyname(void *, void *, va_list);
54extern int _dns_gethostbyname(void *, void *, va_list);
55extern int _nis_gethostbyname(void *, void *, va_list);
56extern int _ht_gethostbyaddr(void *, void *, va_list);
57extern int _dns_gethostbyaddr(void *, void *, va_list);
58extern int _nis_gethostbyaddr(void *, void *, va_list);
59
60static int gethostbyname_internal(const char *, int, struct hostent *, char *,
61    size_t, struct hostent **, int *, res_state);
62
63/* Host lookup order if nsswitch.conf is broken or nonexistant */
64static const ns_src default_src[] = {
65        { NSSRC_FILES, NS_SUCCESS },
66        { NSSRC_DNS, NS_SUCCESS },
67        { 0 }
68};
69#ifdef NS_CACHING
70static int host_id_func(char *, size_t *, va_list, void *);
71static int host_marshal_func(char *, size_t *, void *, va_list, void *);
72static int host_unmarshal_func(char *, size_t, void *, va_list, void *);
73#endif
74
75NETDB_THREAD_ALLOC(hostent)
76NETDB_THREAD_ALLOC(hostent_data)
77NETDB_THREAD_ALLOC(hostdata)
78
79static void
80hostent_free(void *ptr)
81{
82        free(ptr);
83}
84
85static void
86hostent_data_free(void *ptr)
87{
88        struct hostent_data *hed = ptr;
89
90        if (hed == NULL)
91                return;
92        hed->stayopen = 0;
93        _endhosthtent(hed);
94        free(hed);
95}
96
97static void
98hostdata_free(void *ptr)
99{
100        free(ptr);
101}
102
103int
104__copy_hostent(struct hostent *he, struct hostent *hptr, char *buf,
105    size_t buflen)
106{
107        char *cp;
108        char **ptr;
109        int i, n;
110        int nptr, len;
111
112        /* Find out the amount of space required to store the answer. */
113        nptr = 2; /* NULL ptrs */
114        len = (char *)ALIGN(buf) - buf;
115        for (i = 0; he->h_addr_list[i]; i++, nptr++) {
116                len += he->h_length;
117        }
118        for (i = 0; he->h_aliases[i]; i++, nptr++) {
119                len += strlen(he->h_aliases[i]) + 1;
120        }
121        len += strlen(he->h_name) + 1;
122        len += nptr * sizeof(char*);
123
124        if (len > buflen) {
125                errno = ERANGE;
126                return (-1);
127        }
128
129        /* copy address size and type */
130        hptr->h_addrtype = he->h_addrtype;
131        n = hptr->h_length = he->h_length;
132
133        ptr = (char **)ALIGN(buf);
134        cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
135
136        /* copy address list */
137        hptr->h_addr_list = ptr;
138        for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
139                memcpy(cp, he->h_addr_list[i], n);
140                hptr->h_addr_list[i] = cp;
141                cp += n;
142        }
143        hptr->h_addr_list[i] = NULL;
144        ptr++;
145
146        /* copy official name */
147        n = strlen(he->h_name) + 1;
148        strcpy(cp, he->h_name);
149        hptr->h_name = cp;
150        cp += n;
151
152        /* copy aliases */
153        hptr->h_aliases = ptr;
154        for (i = 0 ; he->h_aliases[i]; i++) {
155                n = strlen(he->h_aliases[i]) + 1;
156                strcpy(cp, he->h_aliases[i]);
157                hptr->h_aliases[i] = cp;
158                cp += n;
159        }
160        hptr->h_aliases[i] = NULL;
161
162        return (0);
163}
164
165#ifdef NS_CACHING
166static int
167host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
168{
169        res_state statp;
170        u_long res_options;
171
172        const int op_id = 1;
173        char *str;
174        void *addr;
175        socklen_t len;
176        int type;
177
178        size_t desired_size, size;
179        enum nss_lookup_type lookup_type;
180        char *p;
181        int res = NS_UNAVAIL;
182
183        statp = __res_state();
184        res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
185            RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
186
187        lookup_type = (enum nss_lookup_type)cache_mdata;
188        switch (lookup_type) {
189        case nss_lt_name:
190                str = va_arg(ap, char *);
191                type = va_arg(ap, int);
192
193                size = strlen(str);
194                desired_size = sizeof(res_options) + sizeof(int) +
195                    sizeof(enum nss_lookup_type) + sizeof(int) + size + 1;
196
197                if (desired_size > *buffer_size) {
198                        res = NS_RETURN;
199                        goto fin;
200                }
201
202                p = buffer;
203
204                memcpy(p, &res_options, sizeof(res_options));
205                p += sizeof(res_options);
206
207                memcpy(p, &op_id, sizeof(int));
208                p += sizeof(int);
209
210                memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
211                p += sizeof(int);
212
213                memcpy(p, &type, sizeof(int));
214                p += sizeof(int);
215
216                memcpy(p, str, size + 1);
217
218                res = NS_SUCCESS;
219                break;
220        case nss_lt_id:
221                addr = va_arg(ap, void *);
222                len = va_arg(ap, socklen_t);
223                type = va_arg(ap, int);
224
225                desired_size = sizeof(res_options) + sizeof(int) +
226                    sizeof(enum nss_lookup_type) + sizeof(int) +
227                    sizeof(socklen_t) + len;
228
229                if (desired_size > *buffer_size) {
230                        res = NS_RETURN;
231                        goto fin;
232                }
233
234                p = buffer;
235                memcpy(p, &res_options, sizeof(res_options));
236                p += sizeof(res_options);
237
238                memcpy(p, &op_id, sizeof(int));
239                p += sizeof(int);
240
241                memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
242                p += sizeof(int);
243
244                memcpy(p, &type, sizeof(int));
245                p += sizeof(int);
246
247                memcpy(p, &len, sizeof(socklen_t));
248                p += sizeof(socklen_t);
249
250                memcpy(p, addr, len);
251
252                res = NS_SUCCESS;
253                break;
254        default:
255                /* should be unreachable */
256                return (NS_UNAVAIL);
257        }
258
259fin:
260        *buffer_size = desired_size;
261        return (res);
262}
263
264static int
265host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
266    void *cache_mdata)
267{
268        char *str;
269        void *addr;
270        socklen_t len;
271        int type;
272        struct hostent *ht;
273
274        struct hostent new_ht;
275        size_t desired_size, aliases_size, addr_size, size;
276        char *p, **iter;
277
278        switch ((enum nss_lookup_type)cache_mdata) {
279        case nss_lt_name:
280                str = va_arg(ap, char *);
281                type = va_arg(ap, int);
282                break;
283        case nss_lt_id:
284                addr = va_arg(ap, void *);
285                len = va_arg(ap, socklen_t);
286                type = va_arg(ap, int);
287                break;
288        default:
289                /* should be unreachable */
290                return (NS_UNAVAIL);
291        }
292        ht = va_arg(ap, struct hostent *);
293
294        desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *);
295        if (ht->h_name != NULL)
296                desired_size += strlen(ht->h_name) + 1;
297
298        if (ht->h_aliases != NULL) {
299                aliases_size = 0;
300                for (iter = ht->h_aliases; *iter; ++iter) {
301                        desired_size += strlen(*iter) + 1;
302                        ++aliases_size;
303                }
304
305                desired_size += _ALIGNBYTES +
306                    (aliases_size + 1) * sizeof(char *);
307        }
308
309        if (ht->h_addr_list != NULL) {
310                addr_size = 0;
311                for (iter = ht->h_addr_list; *iter; ++iter)
312                        ++addr_size;
313
314                desired_size += addr_size * _ALIGN(ht->h_length);
315                desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *);
316        }
317
318        if (desired_size > *buffer_size) {
319                /* this assignment is here for future use */
320                *buffer_size = desired_size;
321                return (NS_RETURN);
322        }
323
324        memcpy(&new_ht, ht, sizeof(struct hostent));
325        memset(buffer, 0, desired_size);
326
327        *buffer_size = desired_size;
328        p = buffer + sizeof(struct hostent) + sizeof(char *);
329        memcpy(buffer + sizeof(struct hostent), &p, sizeof(char *));
330        p = (char *)_ALIGN(p);
331
332        if (new_ht.h_name != NULL) {
333                size = strlen(new_ht.h_name);
334                memcpy(p, new_ht.h_name, size);
335                new_ht.h_name = p;
336                p += size + 1;
337        }
338
339        if (new_ht.h_aliases != NULL) {
340                p = (char *)_ALIGN(p);
341                memcpy(p, new_ht.h_aliases, sizeof(char *) * aliases_size);
342                new_ht.h_aliases = (char **)p;
343                p += sizeof(char *) * (aliases_size + 1);
344
345                for (iter = new_ht.h_aliases; *iter; ++iter) {
346                        size = strlen(*iter);
347                        memcpy(p, *iter, size);
348                        *iter = p;
349                        p += size + 1;
350                }
351        }
352
353        if (new_ht.h_addr_list != NULL) {
354                p = (char *)_ALIGN(p);
355                memcpy(p, new_ht.h_addr_list, sizeof(char *) * addr_size);
356                new_ht.h_addr_list = (char **)p;
357                p += sizeof(char *) * (addr_size + 1);
358
359                size = _ALIGN(new_ht.h_length);
360                for (iter = new_ht.h_addr_list; *iter; ++iter) {
361                        memcpy(p, *iter, size);
362                        *iter = p;
363                        p += size + 1;
364                }
365        }
366        memcpy(buffer, &new_ht, sizeof(struct hostent));
367        return (NS_SUCCESS);
368}
369
370static int
371host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
372    void *cache_mdata)
373{
374        char *str;
375        void *addr;
376        socklen_t len;
377        int type;
378        struct hostent *ht;
379
380        char *p;
381        char **iter;
382        char *orig_buf;
383        size_t orig_buf_size;
384
385        switch ((enum nss_lookup_type)cache_mdata) {
386        case nss_lt_name:
387                str = va_arg(ap, char *);
388                type = va_arg(ap, int);
389                break;
390        case nss_lt_id:
391                addr = va_arg(ap, void *);
392                len = va_arg(ap, socklen_t);
393                type = va_arg(ap, int);
394                break;
395        default:
396                /* should be unreachable */
397                return (NS_UNAVAIL);
398        }
399
400        ht = va_arg(ap, struct hostent *);
401        orig_buf = va_arg(ap, char *);
402        orig_buf_size = va_arg(ap, size_t);
403
404        if (orig_buf_size <
405            buffer_size - sizeof(struct hostent) - sizeof(char *)) {
406                errno = ERANGE;
407                return (NS_RETURN);
408        }
409
410        memcpy(ht, buffer, sizeof(struct hostent));
411        memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *));
412
413        orig_buf = (char *)_ALIGN(orig_buf);
414        memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) +
415            _ALIGN(p) - (size_t)p,
416            buffer_size - sizeof(struct hostent) - sizeof(char *) -
417            _ALIGN(p) + (size_t)p);
418        p = (char *)_ALIGN(p);
419
420        NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *);
421        if (ht->h_aliases != NULL) {
422                NS_APPLY_OFFSET(ht->h_aliases, orig_buf, p, char **);
423
424                for (iter = ht->h_aliases; *iter; ++iter)
425                        NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
426        }
427
428        if (ht->h_addr_list != NULL) {
429                NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **);
430
431                for (iter = ht->h_addr_list; *iter; ++iter)
432                        NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
433        }
434
435        *((struct hostent **)retval) = ht;
436        return (NS_SUCCESS);
437}
438#endif /* NS_CACHING */
439
440static int
441fakeaddr(const char *name, int af, struct hostent *hp, char *buf,
442    size_t buflen, res_state statp)
443{
444        struct hostent_data *hed;
445        struct hostent he;
446
447        if ((hed = __hostent_data_init()) == NULL) {
448                errno = ENOMEM;
449                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
450                return (-1);
451        }
452
453        if ((af != AF_INET ||
454            inet_aton(name, (struct in_addr *)hed->host_addr) != 1) &&
455            inet_pton(af, name, hed->host_addr) != 1) {
456                RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
457                return (-1);
458        }
459        strncpy(hed->hostbuf, name, MAXDNAME);
460        hed->hostbuf[MAXDNAME] = '\0';
461#ifdef INET6
462        if (af == AF_INET && (statp->options & RES_USE_INET6) != 0U) {
463                _map_v4v6_address((char *)hed->host_addr,
464                    (char *)hed->host_addr);
465                af = AF_INET6;
466        }
467#endif
468        he.h_addrtype = af;
469        switch(af) {
470        case AF_INET:
471                he.h_length = NS_INADDRSZ;
472                break;
473#ifdef INET6
474        case AF_INET6:
475                he.h_length = NS_IN6ADDRSZ;
476                break;
477#endif
478        default:
479                errno = EAFNOSUPPORT;
480                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
481                return (-1);
482        }
483        he.h_name = hed->hostbuf;
484        he.h_aliases = hed->host_aliases;
485        hed->host_aliases[0] = NULL;
486        hed->h_addr_ptrs[0] = (char *)hed->host_addr;
487        hed->h_addr_ptrs[1] = NULL;
488        he.h_addr_list = hed->h_addr_ptrs;
489        if (__copy_hostent(&he, hp, buf, buflen) != 0) {
490                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
491                return (-1);
492        }
493        RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
494        return (0);
495}
496
497int
498gethostbyname_r(const char *name, struct hostent *he, char *buffer,
499    size_t buflen, struct hostent **result, int *h_errnop)
500{
501        res_state statp;
502
503        statp = __res_state();
504        if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
505                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
506                return (-1);
507        }
508        if (statp->options & RES_USE_INET6) {
509                if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) {
510                        *result = he;
511                        return (0);
512                }
513                if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen,
514                    result, h_errnop, statp) == 0)
515                        return (0);
516        }
517        return (gethostbyname_internal(name, AF_INET, he, buffer, buflen,
518            result, h_errnop, statp));
519}
520
521int
522gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer,
523    size_t buflen, struct hostent **result, int *h_errnop)
524{
525        res_state statp;
526
527        statp = __res_state();
528        if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
529                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
530                return (-1);
531        }
532        return (gethostbyname_internal(name, af, he, buffer, buflen, result,
533            h_errnop, statp));
534}
535
536int
537gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf,
538    size_t buflen, struct hostent **result, int *h_errnop, res_state statp)
539{
540        const char *cp;
541        int rval, ret_errno = 0;
542        char abuf[MAXDNAME];
543
544#ifdef NS_CACHING
545        static const nss_cache_info cache_info =
546                NS_COMMON_CACHE_INFO_INITIALIZER(
547                hosts, (void *)nss_lt_name,
548                host_id_func, host_marshal_func, host_unmarshal_func);
549#endif
550        static const ns_dtab dtab[] = {
551                NS_FILES_CB(_ht_gethostbyname, NULL)
552                { NSSRC_DNS, _dns_gethostbyname, NULL },
553                NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
554#ifdef NS_CACHING
555                NS_CACHE_CB(&cache_info)
556#endif
557                { 0 }
558        };
559
560        switch (af) {
561        case AF_INET:
562        case AF_INET6:
563                break;
564        default:
565                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
566                *h_errnop = statp->res_h_errno;
567                errno = EAFNOSUPPORT;
568                return (-1);
569        }
570
571        /*
572         * if there aren't any dots, it could be a user-level alias.
573         * this is also done in res_query() since we are not the only
574         * function that looks up host names.
575         */
576        if (!strchr(name, '.') &&
577            (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
578                name = cp;
579
580        if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
581                *result = hp;
582                return (0);
583        }
584
585        rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
586            "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
587            &ret_errno, h_errnop);
588
589        if (rval != NS_SUCCESS) {
590                errno = ret_errno;
591                return ((ret_errno != 0) ? ret_errno : -1);
592        }
593        return (0);
594}
595
596int
597gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
598    char *buf, size_t buflen, struct hostent **result, int *h_errnop)
599{
600        const u_char *uaddr = (const u_char *)addr;
601        const struct in6_addr *addr6;
602        socklen_t size;
603        int rval, ret_errno = 0;
604        res_state statp;
605
606#ifdef NS_CACHING
607        static const nss_cache_info cache_info =
608                NS_COMMON_CACHE_INFO_INITIALIZER(
609                hosts, (void *)nss_lt_id,
610                host_id_func, host_marshal_func, host_unmarshal_func);
611#endif
612        static const ns_dtab dtab[] = {
613                NS_FILES_CB(_ht_gethostbyaddr, NULL)
614                { NSSRC_DNS, _dns_gethostbyaddr, NULL },
615                NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
616#ifdef NS_CACHING
617                NS_CACHE_CB(&cache_info)
618#endif
619                { 0 }
620        };
621
622        statp = __res_state();
623        if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
624                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
625                *h_errnop = statp->res_h_errno;
626                return (-1);
627        }
628
629        if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
630                addr6 = (const struct in6_addr *)addr;
631                if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
632                        RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
633                        *h_errnop = statp->res_h_errno;
634                        return (-1);
635                }
636                if (IN6_IS_ADDR_V4MAPPED(addr6) ||
637                    IN6_IS_ADDR_V4COMPAT(addr6)) {
638                        /* Unmap. */
639                        uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
640                        af = AF_INET;
641                        len = NS_INADDRSZ;
642                }
643        }
644        switch (af) {
645        case AF_INET:
646                size = NS_INADDRSZ;
647                break;
648        case AF_INET6:
649                size = NS_IN6ADDRSZ;
650                break;
651        default:
652                errno = EAFNOSUPPORT;
653                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
654                *h_errnop = statp->res_h_errno;
655                return (-1);
656        }
657        if (size != len) {
658                errno = EINVAL;
659                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
660                *h_errnop = statp->res_h_errno;
661                return (-1);
662        }
663
664        rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
665            "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
666            &ret_errno, h_errnop);
667
668        if (rval != NS_SUCCESS) {
669                errno = ret_errno;
670                return ((ret_errno != 0) ? ret_errno : -1);
671        }
672        return (0);
673}
674
675struct hostent *
676gethostbyname(const char *name)
677{
678        struct hostdata *hd;
679        struct hostent *rval;
680        int ret_h_errno;
681
682        if ((hd = __hostdata_init()) == NULL)
683                return (NULL);
684        if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
685            &ret_h_errno) != 0)
686                return (NULL);
687        return (rval);
688}
689
690struct hostent *
691gethostbyname2(const char *name, int af)
692{
693        struct hostdata *hd;
694        struct hostent *rval;
695        int ret_h_errno;
696
697        if ((hd = __hostdata_init()) == NULL)
698                return (NULL);
699        if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
700            &rval, &ret_h_errno) != 0)
701                return (NULL);
702        return (rval);
703}
704
705struct hostent *
706gethostbyaddr(const void *addr, socklen_t len, int af)
707{
708        struct hostdata *hd;
709        struct hostent *rval;
710        int ret_h_errno;
711
712        if ((hd = __hostdata_init()) == NULL)
713                return (NULL);
714        if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
715            sizeof(hd->data), &rval, &ret_h_errno) != 0)
716                return (NULL);
717        return (rval);
718}
719
720void
721sethostent(int stayopen)
722{
723        struct hostent_data *hed;
724
725        if ((hed = __hostent_data_init()) == NULL)
726                return;
727        _sethosthtent(stayopen, hed);
728        _sethostdnsent(stayopen);
729}
730
731void
732endhostent(void)
733{
734        struct hostent_data *hed;
735
736        if ((hed = __hostent_data_init()) == NULL)
737                return;
738        _endhosthtent(hed);
739        _endhostdnsent();
740}
Note: See TracBrowser for help on using the repository browser.