source: rtems-libbsd/freebsd-userspace/lib/libc/net/gethostbyht.c @ 68e0948

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 68e0948 was 68e0948, checked in by Joel Sherrill <joel.sherrill@…>, on 10/23/12 at 18:31:30

Makefile: Conditionalize IPV6 files and build some commands as .rel

At least ifconfig has "static" global C constructors and you have
to force the objects in.

  • Property mode set to 100644
File size: 9.2 KB
Line 
1#include "port_before.h"
2
3/*-
4 * Copyright (c) 1985, 1988, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 * -
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32 *
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 * SOFTWARE.
48 * -
49 * --Copyright--
50 */
51
52#if defined(LIBC_SCCS) && !defined(lint)
53static char sccsid[] = "@(#)gethostnamadr.c     8.1 (Berkeley) 6/4/93";
54#endif /* LIBC_SCCS and not lint */
55#include <sys/cdefs.h>
56__FBSDID("$FreeBSD$");
57
58#include <sys/param.h>
59#include <sys/socket.h>
60#include <netinet/in.h>
61#include <arpa/inet.h>
62#include <netdb.h>
63#include <stdio.h>
64#include <ctype.h>
65#include <errno.h>
66#include <string.h>
67#include <stdarg.h>
68#include <nsswitch.h>
69#include <arpa/nameser.h>       /* XXX */
70#include <resolv.h>             /* XXX */
71#include "netdb_private.h"
72
73void
74_sethosthtent(int f, struct hostent_data *hed)
75{
76        if (!hed->hostf)
77                hed->hostf = fopen(_PATH_HOSTS, "r");
78        else
79                rewind(hed->hostf);
80        hed->stayopen = f;
81}
82
83void
84_endhosthtent(struct hostent_data *hed)
85{
86        if (hed->hostf && !hed->stayopen) {
87                (void) fclose(hed->hostf);
88                hed->hostf = NULL;
89        }
90}
91
92static int
93gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
94    res_state statp)
95{
96        char *p, *bp, *ep;
97        char *cp, **q;
98        int af, len;
99        char hostbuf[BUFSIZ + 1];
100
101        if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) {
102                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
103                return (-1);
104        }
105 again:
106        if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
107                RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
108                return (-1);
109        }
110        if (*p == '#')
111                goto again;
112        cp = strpbrk(p, "#\n");
113        if (cp != NULL)
114                *cp = '\0';
115        if (!(cp = strpbrk(p, " \t")))
116                goto again;
117        *cp++ = '\0';
118#ifdef INET6
119        if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
120                af = AF_INET6;
121                len = IN6ADDRSZ;
122        } else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
123                if (mapped) {
124                        _map_v4v6_address((char *)hed->host_addr,
125                            (char *)hed->host_addr);
126                        af = AF_INET6;
127                        len = IN6ADDRSZ;
128                } else {
129                        af = AF_INET;
130                        len = INADDRSZ;
131                }
132        } else {
133                goto again;
134        }
135#else
136        if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
137                af = AF_INET;
138                len = INADDRSZ;
139        }
140#endif
141        hed->h_addr_ptrs[0] = (char *)hed->host_addr;
142        hed->h_addr_ptrs[1] = NULL;
143        he->h_addr_list = hed->h_addr_ptrs;
144        he->h_length = len;
145        he->h_addrtype = af;
146        while (*cp == ' ' || *cp == '\t')
147                cp++;
148        bp = hed->hostbuf;
149        ep = hed->hostbuf + sizeof hed->hostbuf;
150        he->h_name = bp;
151        q = he->h_aliases = hed->host_aliases;
152        if ((p = strpbrk(cp, " \t")) != NULL)
153                *p++ = '\0';
154        len = strlen(cp) + 1;
155        if (ep - bp < len) {
156                RES_SET_H_ERRNO(statp, NO_RECOVERY);
157                return (-1);
158        }
159        strlcpy(bp, cp, ep - bp);
160        bp += len;
161        cp = p;
162        while (cp && *cp) {
163                if (*cp == ' ' || *cp == '\t') {
164                        cp++;
165                        continue;
166                }
167                if (q >= &hed->host_aliases[_MAXALIASES - 1])
168                        break;
169                if ((p = strpbrk(cp, " \t")) != NULL)
170                        *p++ = '\0';
171                len = strlen(cp) + 1;
172                if (ep - bp < len)
173                        break;
174                strlcpy(bp, cp, ep - bp);
175                *q++ = bp;
176                bp += len;
177                cp = p;
178        }
179        *q = NULL;
180        RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
181        return (0);
182}
183
184int
185gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
186    struct hostent **result, int *h_errnop)
187{
188        struct hostent_data *hed;
189        struct hostent he;
190        res_state statp;
191
192        statp = __res_state();
193        if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
194                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
195                *h_errnop = statp->res_h_errno;
196                return (-1);
197        }
198        if ((hed = __hostent_data_init()) == NULL) {
199                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
200                *h_errnop = statp->res_h_errno;
201                return (-1);
202        }
203#ifdef INET6
204        if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
205                return (-1);
206#endif
207        if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
208                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
209                *h_errnop = statp->res_h_errno;
210                return ((errno != 0) ? errno : -1);
211        }
212        *result = hptr;
213        return (0);
214}
215
216struct hostent *
217gethostent(void)
218{
219        struct hostdata *hd;
220        struct hostent *rval;
221        int ret_h_errno;
222
223        if ((hd = __hostdata_init()) == NULL)
224                return (NULL);
225        if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
226            &ret_h_errno) != 0)
227                return (NULL);
228        return (rval);
229}
230
231int
232_ht_gethostbyname(void *rval, void *cb_data, va_list ap)
233{
234        const char *name;
235        int af;
236        char *buffer;
237        size_t buflen;
238        int *errnop, *h_errnop;
239        struct hostent *hptr, he;
240        struct hostent_data *hed;
241        char **cp;
242        res_state statp;
243        int error;
244
245        name = va_arg(ap, const char *);
246        af = va_arg(ap, int);
247        hptr = va_arg(ap, struct hostent *);
248        buffer = va_arg(ap, char *);
249        buflen = va_arg(ap, size_t);
250        errnop = va_arg(ap, int *);
251        h_errnop = va_arg(ap, int *);
252
253        *((struct hostent **)rval) = NULL;
254
255        statp = __res_state();
256        if ((hed = __hostent_data_init()) == NULL) {
257                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
258                *h_errnop = statp->res_h_errno;
259                return (NS_NOTFOUND);
260        }
261
262        _sethosthtent(0, hed);
263        while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
264                if (he.h_addrtype != af)
265                        continue;
266#ifdef INET6
267                if (he.h_addrtype == AF_INET &&
268                    statp->options & RES_USE_INET6) {
269                        _map_v4v6_address(he.h_addr, he.h_addr);
270                        he.h_length = IN6ADDRSZ;
271                        he.h_addrtype = AF_INET6;
272                }
273#endif
274                if (strcasecmp(he.h_name, name) == 0)
275                        break;
276                for (cp = he.h_aliases; *cp != 0; cp++)
277                        if (strcasecmp(*cp, name) == 0)
278                                goto found;
279        }
280found:
281        _endhosthtent(hed);
282
283        if (error != 0) {
284                *h_errnop = statp->res_h_errno;
285                return (NS_NOTFOUND);
286        }
287        if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
288                *errnop = errno;
289                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
290                *h_errnop = statp->res_h_errno;
291                return (NS_RETURN);
292        }
293        *((struct hostent **)rval) = hptr;
294        return (NS_SUCCESS);
295}
296
297int
298_ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
299{
300        const void *addr;
301        socklen_t len;
302        int af;
303        char *buffer;
304        size_t buflen;
305        int *errnop, *h_errnop;
306        struct hostent *hptr, he;
307        struct hostent_data *hed;
308        res_state statp;
309        int error;
310
311        addr = va_arg(ap, const void *);
312        len = va_arg(ap, socklen_t);
313        af = va_arg(ap, int);
314        hptr = va_arg(ap, struct hostent *);
315        buffer = va_arg(ap, char *);
316        buflen = va_arg(ap, size_t);
317        errnop = va_arg(ap, int *);
318        h_errnop = va_arg(ap, int *);
319
320        *((struct hostent **)rval) = NULL;
321
322        statp = __res_state();
323        if ((hed = __hostent_data_init()) == NULL) {
324                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
325                *h_errnop = statp->res_h_errno;
326                return (NS_NOTFOUND);
327        }
328
329        _sethosthtent(0, hed);
330        while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
331#ifdef INET6
332                if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
333                        if (he.h_addrtype == AF_INET &&
334                            statp->options & RES_USE_INET6) {
335                                _map_v4v6_address(he.h_addr, he.h_addr);
336                                he.h_length = IN6ADDRSZ;
337                                he.h_addrtype = AF_INET6;
338                        }
339                        break;
340                }
341#endif
342        _endhosthtent(hed);
343
344        if (error != 0)
345                return (NS_NOTFOUND);
346        if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
347                *errnop = errno;
348                RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
349                *h_errnop = statp->res_h_errno;
350                return (NS_RETURN);
351        }
352        *((struct hostent **)rval) = hptr;
353        return (NS_SUCCESS);
354}
Note: See TracBrowser for help on using the repository browser.