source: rtems/cpukit/libnetworking/libc/gethostbyht.c @ adfc907

4.115
Last change on this file since adfc907 was a1399ba, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/07/11 at 06:49:32

2011-12-07 Ralf Corsépius <ralf.corsepius@…>

PR 1983/networking

  • libnetworking/libc/gethostbyht.c (gethostent_r): Abort if (!hostf).
  • Property mode set to 100644
File size: 8.2 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 <stdio.h>
64#include <stdlib.h> /* realloc, malloc, free */
65#include <ctype.h>
66#include <errno.h>
67#include <string.h>
68#ifdef HAVE_STRINGS_H
69#include <strings.h>
70#endif
71#include <arpa/nameser.h>       /* XXX */
72#include <resolv.h>             /* XXX */
73#include <sys/fcntl.h>
74
75#define MAXALIASES      35
76
77static struct hostent host;
78static char *host_aliases[MAXALIASES];
79static char hostbuf[BUFSIZ+1];
80static FILE *hostf = NULL;
81static u_char host_addr[16];    /* IPv4 or IPv6 */
82static char *h_addr_ptrs[2];
83static int stayopen = 0;
84
85#ifdef _THREAD_SAFE
86static char* hostmap = NULL;
87static unsigned int hostlen = 0;
88static char *cur;
89#endif
90
91void
92_sethosthtent(int f)
93{
94        if (!hostf)
95                hostf = fopen(_PATH_HOSTS, "r" );
96        else
97                rewind(hostf);
98        stayopen = f;
99}
100
101void
102_endhosthtent(void)
103{
104        if (hostf && !stayopen) {
105                (void) fclose(hostf);
106                hostf = NULL;
107        }
108}
109
110struct hostent *
111gethostent(void)
112{
113        char *p;
114        register char *cp, **q;
115        int af, len;
116
117        if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
118                h_errno = NETDB_INTERNAL;
119                return (NULL);
120        }
121 again:
122        if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
123                h_errno = HOST_NOT_FOUND;
124                return (NULL);
125        }
126        if (*p == '#')
127                goto again;
128        if (!(cp = strpbrk(p, "#\n")))
129                goto again;
130        *cp = '\0';
131        if (!(cp = strpbrk(p, " \t")))
132                goto again;
133        *cp++ = '\0';
134        if (inet_pton(AF_INET6, p, host_addr) > 0) {
135                af = AF_INET6;
136                len = IN6ADDRSZ;
137        } else if (inet_pton(AF_INET, p, host_addr) > 0) {
138                if (_res.options & RES_USE_INET6) {
139                        _map_v4v6_address((char*)host_addr, (char*)host_addr);
140                        af = AF_INET6;
141                        len = IN6ADDRSZ;
142                } else {
143                        af = AF_INET;
144                        len = INADDRSZ;
145                }
146        } else {
147                goto again;
148        }
149        h_addr_ptrs[0] = (char *)host_addr;
150        h_addr_ptrs[1] = NULL;
151        host.h_addr_list = h_addr_ptrs;
152        host.h_length = len;
153        host.h_addrtype = af;
154        while (*cp == ' ' || *cp == '\t')
155                cp++;
156        host.h_name = cp;
157        q = host.h_aliases = host_aliases;
158        if ((cp = strpbrk(cp, " \t")) != NULL)
159                *cp++ = '\0';
160        while (cp && *cp) {
161                if (*cp == ' ' || *cp == '\t') {
162                        cp++;
163                        continue;
164                }
165                if (q < &host_aliases[MAXALIASES - 1])
166                        *q++ = cp;
167                if ((cp = strpbrk(cp, " \t")) != NULL)
168                        *cp++ = '\0';
169        }
170        *q = NULL;
171        h_errno = NETDB_SUCCESS;
172        return (&host);
173}
174
175struct hostent *
176_gethostbyhtname(
177        const char *name,
178        int af)
179{
180        register struct hostent *p;
181        register char **cp;
182       
183        sethostent(0);
184        while ((p = gethostent()) != NULL) {
185                if (p->h_addrtype != af)
186                        continue;
187                if (strcasecmp(p->h_name, name) == 0)
188                        break;
189                for (cp = p->h_aliases; *cp != 0; cp++)
190                        if (strcasecmp(*cp, name) == 0)
191                                goto found;
192        }
193found:
194        endhostent();
195        return (p);
196}
197
198struct hostent *
199_gethostbyhtaddr(
200        const char *addr,
201        int len,
202        int af)
203{
204        register struct hostent *p;
205
206        sethostent(0);
207        while ((p = gethostent()) != NULL)
208                if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
209                        break;
210        endhostent();
211        return (p);
212}
213
214
215#ifdef _THREAD_SAFE
216struct hostent* gethostent_r(char* buf, int len)
217{
218  char  *dest;
219  struct hostent* pe=(struct hostent*)buf;
220  char*  last;
221  char*  max=buf+len;
222  int    aliasidx;
223  int    curlen;
224 
225   
226  if (!hostf) return 0;
227  fseek(hostf,0,SEEK_END);
228  curlen=ftell(hostf);
229  fseek(hostf,0,SEEK_SET);
230 
231  if (curlen > hostlen) {
232    if (hostmap) {
233      hostmap = realloc(hostmap,curlen);
234    }
235    else {
236      hostmap = malloc(curlen);
237    }
238  }
239  hostlen = curlen;
240 
241  if (hostmap) {
242    if (fread(hostmap,hostlen,1,hostf) != hostlen) {
243        hostmap=0; goto error;
244    }
245    cur=hostmap;
246  }
247  last=hostmap+hostlen;
248again:
249  if ((size_t)len<sizeof(struct hostent)+11*sizeof(char*)) goto nospace;
250  dest=buf+sizeof(struct hostent);
251  pe->h_name=0;
252  pe->h_aliases=(char**)dest; pe->h_aliases[0]=0; dest+=10*sizeof(char*);
253  pe->h_addr_list=(char**)dest; dest+=2*sizeof(char**);
254  if (cur>=last) return 0;
255  if (*cur=='#' || *cur=='\n') goto parseerror;
256  /* first, the ip number */
257  pe->h_name=cur;
258  while (cur<last && !isspace((unsigned char)*cur)) cur++;
259  if (cur>=last) return 0;
260  if (*cur=='\n') goto parseerror;
261  {
262    char save=*cur;
263    *cur=0;
264    pe->h_addr_list[0]=dest;
265    pe->h_addr_list[1]=0;
266    if (max-dest<16) goto nospace;
267    if (inet_pton(AF_INET6,pe->h_name,dest)>0) {
268      pe->h_addrtype=AF_INET6;
269      pe->h_length=16;
270      dest+=16;
271    } else if (inet_pton(AF_INET,pe->h_name,dest)>0) {
272      pe->h_addrtype=AF_INET;
273      pe->h_length=4;
274      dest+=4;
275    } else {
276      *cur=save;
277      goto parseerror;
278    }
279    *cur=save;
280  }
281  ++cur;
282  /* now the aliases */
283  for (aliasidx=0;aliasidx<9;++aliasidx) {
284    while (cur<last && isblank((unsigned char)*cur)) ++cur;
285    pe->h_aliases[aliasidx]=cur;
286    while (cur<last && !isspace((unsigned char)*cur)) ++cur;
287    {
288      char *from=pe->h_aliases[aliasidx];
289      int len=cur-from;
290      if (max-dest<len+2) goto nospace;
291      pe->h_aliases[aliasidx]=dest;
292      memmove(dest,from,(size_t)(cur-from));
293      dest+=len;
294      *dest=0; ++dest;
295    }
296    if (*cur=='\n') { ++cur; ++aliasidx; break; }
297    if (cur>=last || !isblank((unsigned char)*cur)) break;
298    cur++;
299  }
300  pe->h_aliases[aliasidx]=0;
301  pe->h_name=pe->h_aliases[0];
302  pe->h_aliases++;
303  return pe;
304parseerror:
305  while (cur<last && *cur!='\n') cur++;
306  cur++;
307  goto again;
308nospace:
309  errno=ERANGE;
310  goto __error;
311error:
312  errno=ENOMEM;
313__error:
314  if (hostmap!=NULL) free(hostmap);
315  hostmap=NULL;
316  return 0;
317}
318#endif
Note: See TracBrowser for help on using the repository browser.