source: rtems/cpukit/libnetworking/libc/gethostbyht.c @ 232d6fec

5
Last change on this file since 232d6fec was 232d6fec, checked in by Christian Mauderer <Christian.Mauderer@…>, on 05/02/16 at 12:49:32

libnetworking: Import current <netdb.h>

Import the <netdb.h> from current FreeBSD. This allows to build some
current software (e.g. libressl).

Add h_errno().

Update gethostent_r() API. Linux and FreeBSD use a common API now.
Adapt the RTEMS one to provide the same one.

Match gethostbyname_r() with prototype.

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