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. 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. 16 * 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 27 * SUCH DAMAGE. 28 * - 29 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 30 * 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. 37 * 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 45 * SOFTWARE. 46 * - 47 * --Copyright-- 48 * 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 $ 52 */ 53 54 #include <sys/param.h> 55 #include <sys/socket.h> 56 #include <netinet/in.h> 57 #include <arpa/inet.h> 58 #include <netdb.h> 59 #include <stdio.h> 60 #include <ctype.h> 61 #include <string.h> 62 #include <arpa/nameser.h> /* XXX */ 63 #include <resolv.h> /* XXX */ 64 65 #define MAXALIASES 35 66 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; 74 75 void 76 _sethosthtent(int f) 77 { 78 if (!hostf) 79 hostf = fopen(_PATH_HOSTS, "r" ); 80 else 81 rewind(hostf); 82 stayopen = f; 83 } 84 85 void 86 _endhosthtent(void) 87 { 88 if (hostf && !stayopen) { 89 fclose(hostf); 90 hostf = NULL; 91 } 92 } 93 94 struct hostent * 95 gethostent(void) 96 { 97 char *p; 98 char *cp, **q; 99 int af, len; 100 101 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { 102 h_errno = NETDB_INTERNAL; 103 return (NULL); 104 } 105 again: 106 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { 107 h_errno = HOST_NOT_FOUND; 108 return (NULL); 109 } 110 if (*p == '#') 111 goto again; 112 if (!(cp = strpbrk(p, "#\n"))) 113 goto again; 114 *cp = '\0'; 115 if (!(cp = strpbrk(p, " \t"))) 116 goto again; 117 *cp++ = '\0'; 118 if (inet_pton(AF_INET6, p, host_addr) > 0) { 119 af = AF_INET6; 120 len = IN6ADDRSZ; 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); 124 af = AF_INET6; 125 len = IN6ADDRSZ; 126 } else { 127 af = AF_INET; 128 len = INADDRSZ; 129 } 130 } else { 131 goto again; 132 } 133 h_addr_ptrs[0] = (char *)host_addr; 134 h_addr_ptrs[1] = NULL; 135 host.h_addr_list = h_addr_ptrs; 136 host.h_length = len; 137 host.h_addrtype = af; 138 while (*cp == ' ' || *cp == '\t') 139 cp++; 140 host.h_name = cp; 141 q = host.h_aliases = host_aliases; 142 if ((cp = strpbrk(cp, " \t")) != NULL) 143 *cp++ = '\0'; 144 while (cp && *cp) { 145 if (*cp == ' ' || *cp == '\t') { 146 cp++; 147 continue; 148 } 149 if (q < &host_aliases[MAXALIASES - 1]) 150 *q++ = cp; 151 if ((cp = strpbrk(cp, " \t")) != NULL) 152 *cp++ = '\0'; 153 } 154 *q = NULL; 155 h_errno = NETDB_SUCCESS; 156 return (&host); 157 } 158 159 struct hostent * 160 _gethostbyhtname(const char *name, int af) 161 { 162 struct hostent *p; 163 char **cp; 164 165 sethostent(0); 166 while ((p = gethostent()) != NULL) { 167 if (p->h_addrtype != af) 168 continue; 169 if (strcasecmp(p->h_name, name) == 0) 170 break; 171 for (cp = p->h_aliases; *cp != 0; cp++) 172 if (strcasecmp(*cp, name) == 0) 173 goto found; 174 } 175 found: 176 endhostent(); 177 return (p); 178 } 179 180 struct hostent * 181 _gethostbyhtaddr(const char *addr, int len, int af) 182 { 183 struct hostent *p; 184 185 sethostent(0); 186 while ((p = gethostent()) != NULL) 187 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) 188 break; 189 endhostent(); 190 return (p); 191 } 192