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

4.8
Last change on this file since d8fce03 was 09fdb5e8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/30/07 at 05:15:58

Eliminate SCCS, LINT. Add HAVE_CONFIG_H.

  • Property mode set to 100644
File size: 8.0 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 <ctype.h>
65#include <errno.h>
66#include <string.h>
67#include <arpa/nameser.h>       /* XXX */
68#include <resolv.h>             /* XXX */
69#include <sys/fcntl.h>
70
71#define MAXALIASES      35
72
73static struct hostent host;
74static char *host_aliases[MAXALIASES];
75static char hostbuf[BUFSIZ+1];
76static FILE *hostf = NULL;
77static u_char host_addr[16];    /* IPv4 or IPv6 */
78static char *h_addr_ptrs[2];
79static int stayopen = 0;
80
81#ifdef _THREAD_SAFE
82static char* hostmap = NULL;
83static unsigned int hostlen = 0;
84static char *cur;
85#endif
86
87void
88_sethosthtent(f)
89        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()
100{
101        if (hostf && !stayopen) {
102                (void) fclose(hostf);
103                hostf = NULL;
104        }
105}
106
107struct hostent *
108gethostent()
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(name, af)
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(addr, len, af)
197        const char *addr;
198        int len, af;
199{
200        register struct hostent *p;
201
202        sethostent(0);
203        while ((p = gethostent()) != NULL)
204                if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
205                        break;
206        endhostent();
207        return (p);
208}
209
210
211#ifdef _THREAD_SAFE
212struct hostent* gethostent_r(char* buf, int len)
213{
214  char  *dest;
215  struct hostent* pe=(struct hostent*)buf;
216  char*  last;
217  char*  max=buf+len;
218  int    aliasidx;
219  int    curlen;
220 
221   
222  if (hostf<0) return 0;
223  fseek(hostf,0,SEEK_END);
224  curlen=ftell(hostf);
225  fseek(hostf,0,SEEK_SET);
226 
227  if (curlen > hostlen) {
228    if (hostmap) {
229      hostmap = realloc(hostmap,curlen);
230    }
231    else {
232      hostmap = malloc(curlen);
233    }
234  }
235  hostlen = curlen;
236 
237  if (hostmap) {
238    if (fread(hostmap,hostlen,1,hostf) != hostlen) {
239        hostmap=0; goto error;
240    }
241    cur=hostmap;
242  }
243  last=hostmap+hostlen;
244again:
245  if ((size_t)len<sizeof(struct hostent)+11*sizeof(char*)) goto nospace;
246  dest=buf+sizeof(struct hostent);
247  pe->h_name=0;
248  pe->h_aliases=(char**)dest; pe->h_aliases[0]=0; dest+=10*sizeof(char*);
249  pe->h_addr_list=(char**)dest; dest+=2*sizeof(char**);
250  if (cur>=last) return 0;
251  if (*cur=='#' || *cur=='\n') goto parseerror;
252  /* first, the ip number */
253  pe->h_name=cur;
254  while (cur<last && !isspace(*cur)) cur++;
255  if (cur>=last) return 0;
256  if (*cur=='\n') goto parseerror;
257  {
258    char save=*cur;
259    *cur=0;
260    pe->h_addr_list[0]=dest;
261    pe->h_addr_list[1]=0;
262    if (max-dest<16) goto nospace;
263    if (inet_pton(AF_INET6,pe->h_name,dest)>0) {
264      pe->h_addrtype=AF_INET6;
265      pe->h_length=16;
266      dest+=16;
267    } else if (inet_pton(AF_INET,pe->h_name,dest)>0) {
268      pe->h_addrtype=AF_INET;
269      pe->h_length=4;
270      dest+=4;
271    } else {
272      *cur=save;
273      goto parseerror;
274    }
275    *cur=save;
276  }
277  ++cur;
278  /* now the aliases */
279  for (aliasidx=0;aliasidx<9;++aliasidx) {
280    while (cur<last && isblank(*cur)) ++cur;
281    pe->h_aliases[aliasidx]=cur;
282    while (cur<last && !isspace(*cur)) ++cur;
283    {
284      char *from=pe->h_aliases[aliasidx];
285      int len=cur-from;
286      if (max-dest<len+2) goto nospace;
287      pe->h_aliases[aliasidx]=dest;
288      memmove(dest,from,(size_t)(cur-from));
289      dest+=len;
290      *dest=0; ++dest;
291    }
292    if (*cur=='\n') { ++cur; ++aliasidx; break; }
293    if (cur>=last || !isblank(*cur)) break;
294    cur++;
295  }
296  pe->h_aliases[aliasidx]=0;
297  pe->h_name=pe->h_aliases[0];
298  pe->h_aliases++;
299  return pe;
300parseerror:
301  while (cur<last && *cur!='\n') cur++;
302  cur++;
303  goto again;
304nospace:
305  errno=ERANGE;
306  goto __error;
307error:
308  errno=ENOMEM;
309__error:
310  if (hostmap!=NULL) free(hostmap);
311  hostmap=NULL;
312  return 0;
313}
314#endif
Note: See TracBrowser for help on using the repository browser.