source: rtems/cpukit/libnetworking/libc/gethostbyht.c @ 294039d8

4.9
Last change on this file since 294039d8 was 294039d8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/07/11 at 06:54:43

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