2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
31 * Permission to use, copy, modify, and distribute this software for any
32 * purpose with or without fee is hereby granted, provided that the above
33 * copyright notice and this permission notice appear in all copies, and that
34 * the name of Digital Equipment Corporation not be used in advertising or
35 * publicity pertaining to distribution of the document or software without
36 * specific, written prior permission.
38 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
41 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
50 * $FreeBSD: src/lib/libc/net/gethostbyht.c,v 1.12 1999/08/28 00:00:05 peter Exp $
51 * $DragonFly: src/lib/libc/net/gethostbyht.c,v 1.6 2005/11/13 02:04:47 swildner Exp $
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
62 #include <arpa/nameser.h> /* XXX */
63 #include <resolv.h> /* XXX */
67 static struct hostent host;
68 static char *host_aliases[MAXALIASES];
69 static char hostbuf[BUFSIZ+1];
70 static FILE *hostf = NULL;
71 static u_int32_t host_addr[4]; /* IPv4 or IPv6 */
72 static char *h_addr_ptrs[2];
73 static int stayopen = 0;
79 hostf = fopen(_PATH_HOSTS, "r" );
88 if (hostf && !stayopen) {
101 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
102 h_errno = NETDB_INTERNAL;
106 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
107 h_errno = HOST_NOT_FOUND;
112 if (!(cp = strpbrk(p, "#\n")))
115 if (!(cp = strpbrk(p, " \t")))
118 if (inet_pton(AF_INET6, p, host_addr) > 0) {
121 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
122 if (_res.options & RES_USE_INET6) {
123 _map_v4v6_address((char*)host_addr, (char*)host_addr);
133 h_addr_ptrs[0] = (char *)host_addr;
134 h_addr_ptrs[1] = NULL;
135 host.h_addr_list = h_addr_ptrs;
137 host.h_addrtype = af;
138 while (*cp == ' ' || *cp == '\t')
141 q = host.h_aliases = host_aliases;
142 if ((cp = strpbrk(cp, " \t")) != NULL)
145 if (*cp == ' ' || *cp == '\t') {
149 if (q < &host_aliases[MAXALIASES - 1])
151 if ((cp = strpbrk(cp, " \t")) != NULL)
155 h_errno = NETDB_SUCCESS;
160 _gethostbyhtname(const char *name, int af)
166 while ((p = gethostent()) != NULL) {
167 if (p->h_addrtype != af)
169 if (strcasecmp(p->h_name, name) == 0)
171 for (cp = p->h_aliases; *cp != 0; cp++)
172 if (strcasecmp(*cp, name) == 0)
181 _gethostbyhtaddr(const char *addr, int len, int af)
186 while ((p = gethostent()) != NULL)
187 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))