[39e6e65a] | 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 | |
---|
[09fdb5e8] | 54 | #if HAVE_CONFIG_H |
---|
| 55 | #include "config.h" |
---|
| 56 | #endif |
---|
[39e6e65a] | 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> |
---|
[082d4a2] | 64 | #include <stdlib.h> /* realloc, malloc, free */ |
---|
[39e6e65a] | 65 | #include <ctype.h> |
---|
| 66 | #include <errno.h> |
---|
| 67 | #include <string.h> |
---|
[39f13cb] | 68 | #ifdef HAVE_STRINGS_H |
---|
| 69 | #include <strings.h> |
---|
| 70 | #endif |
---|
[39e6e65a] | 71 | #include <arpa/nameser.h> /* XXX */ |
---|
| 72 | #include <resolv.h> /* XXX */ |
---|
[068c3ee1] | 73 | #include <sys/fcntl.h> |
---|
[39e6e65a] | 74 | |
---|
| 75 | #define MAXALIASES 35 |
---|
| 76 | |
---|
| 77 | static struct hostent host; |
---|
| 78 | static char *host_aliases[MAXALIASES]; |
---|
| 79 | static char hostbuf[BUFSIZ+1]; |
---|
| 80 | static FILE *hostf = NULL; |
---|
| 81 | static u_char host_addr[16]; /* IPv4 or IPv6 */ |
---|
| 82 | static char *h_addr_ptrs[2]; |
---|
| 83 | static int stayopen = 0; |
---|
| 84 | |
---|
[07d0ce8] | 85 | #ifdef _THREAD_SAFE |
---|
[068c3ee1] | 86 | static char* hostmap = NULL; |
---|
| 87 | static unsigned int hostlen = 0; |
---|
| 88 | static char *cur; |
---|
[07d0ce8] | 89 | #endif |
---|
[068c3ee1] | 90 | |
---|
[39e6e65a] | 91 | void |
---|
[029c374c] | 92 | _sethosthtent(int f) |
---|
[39e6e65a] | 93 | { |
---|
| 94 | if (!hostf) |
---|
| 95 | hostf = fopen(_PATH_HOSTS, "r" ); |
---|
| 96 | else |
---|
| 97 | rewind(hostf); |
---|
| 98 | stayopen = f; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void |
---|
[029c374c] | 102 | _endhosthtent(void) |
---|
[39e6e65a] | 103 | { |
---|
| 104 | if (hostf && !stayopen) { |
---|
| 105 | (void) fclose(hostf); |
---|
| 106 | hostf = NULL; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | struct hostent * |
---|
[029c374c] | 111 | gethostent(void) |
---|
[39e6e65a] | 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 | |
---|
| 175 | struct hostent * |
---|
[029c374c] | 176 | _gethostbyhtname( |
---|
| 177 | const char *name, |
---|
| 178 | int af) |
---|
[39e6e65a] | 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 | } |
---|
| 193 | found: |
---|
| 194 | endhostent(); |
---|
| 195 | return (p); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | struct hostent * |
---|
[029c374c] | 199 | _gethostbyhtaddr( |
---|
| 200 | const char *addr, |
---|
| 201 | int len, |
---|
| 202 | int af) |
---|
[39e6e65a] | 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 | } |
---|
[068c3ee1] | 213 | |
---|
| 214 | |
---|
| 215 | #ifdef _THREAD_SAFE |
---|
| 216 | struct 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 | |
---|
[a1399ba] | 226 | if (!hostf) return 0; |
---|
[068c3ee1] | 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; |
---|
| 248 | again: |
---|
| 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; |
---|
[bab5c5fa] | 258 | while (cur<last && !isspace((unsigned char)*cur)) cur++; |
---|
[068c3ee1] | 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) { |
---|
[bab5c5fa] | 284 | while (cur<last && isblank((unsigned char)*cur)) ++cur; |
---|
[068c3ee1] | 285 | pe->h_aliases[aliasidx]=cur; |
---|
[bab5c5fa] | 286 | while (cur<last && !isspace((unsigned char)*cur)) ++cur; |
---|
[068c3ee1] | 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; } |
---|
[bab5c5fa] | 297 | if (cur>=last || !isblank((unsigned char)*cur)) break; |
---|
[068c3ee1] | 298 | cur++; |
---|
| 299 | } |
---|
| 300 | pe->h_aliases[aliasidx]=0; |
---|
| 301 | pe->h_name=pe->h_aliases[0]; |
---|
| 302 | pe->h_aliases++; |
---|
| 303 | return pe; |
---|
| 304 | parseerror: |
---|
| 305 | while (cur<last && *cur!='\n') cur++; |
---|
| 306 | cur++; |
---|
| 307 | goto again; |
---|
| 308 | nospace: |
---|
| 309 | errno=ERANGE; |
---|
| 310 | goto __error; |
---|
| 311 | error: |
---|
| 312 | errno=ENOMEM; |
---|
| 313 | __error: |
---|
| 314 | if (hostmap!=NULL) free(hostmap); |
---|
| 315 | hostmap=NULL; |
---|
| 316 | return 0; |
---|
| 317 | } |
---|
| 318 | #endif |
---|