Home | History | Annotate | Line # | Download | only in libhack
gethost.c revision 1.4
      1  1.4  sommerfe /*	$NetBSD: gethost.c,v 1.4 1999/03/13 19:08:44 sommerfe Exp $	*/
      2  1.1       gwr 
      3  1.1       gwr /*-
      4  1.1       gwr  * Copyright (c) 1985, 1988, 1993
      5  1.1       gwr  *	The Regents of the University of California.  All rights reserved.
      6  1.1       gwr  *
      7  1.1       gwr  * Redistribution and use in source and binary forms, with or without
      8  1.1       gwr  * modification, are permitted provided that the following conditions
      9  1.1       gwr  * are met:
     10  1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     11  1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     12  1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       gwr  *    documentation and/or other materials provided with the distribution.
     15  1.1       gwr  * 3. All advertising materials mentioning features or use of this software
     16  1.1       gwr  *    must display the following acknowledgement:
     17  1.1       gwr  *	This product includes software developed by the University of
     18  1.1       gwr  *	California, Berkeley and its contributors.
     19  1.1       gwr  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       gwr  *    may be used to endorse or promote products derived from this software
     21  1.1       gwr  *    without specific prior written permission.
     22  1.1       gwr  *
     23  1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       gwr  * SUCH DAMAGE.
     34  1.1       gwr  * -
     35  1.1       gwr  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     36  1.1       gwr  *
     37  1.1       gwr  * Permission to use, copy, modify, and distribute this software for any
     38  1.1       gwr  * purpose with or without fee is hereby granted, provided that the above
     39  1.1       gwr  * copyright notice and this permission notice appear in all copies, and that
     40  1.1       gwr  * the name of Digital Equipment Corporation not be used in advertising or
     41  1.1       gwr  * publicity pertaining to distribution of the document or software without
     42  1.1       gwr  * specific, written prior permission.
     43  1.1       gwr  *
     44  1.1       gwr  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     45  1.1       gwr  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     46  1.1       gwr  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     47  1.1       gwr  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     48  1.1       gwr  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     49  1.1       gwr  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     50  1.1       gwr  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     51  1.1       gwr  * SOFTWARE.
     52  1.1       gwr  * -
     53  1.1       gwr  * --Copyright--
     54  1.1       gwr  */
     55  1.1       gwr 
     56  1.2       gwr /*
     57  1.2       gwr  * Copied from:  lib/libc/net/gethostnamadr.c
     58  1.2       gwr  * and then gutted, leaving only /etc/hosts support.
     59  1.2       gwr  */
     60  1.1       gwr 
     61  1.1       gwr #include <sys/param.h>
     62  1.1       gwr #include <sys/socket.h>
     63  1.1       gwr #include <netinet/in.h>
     64  1.1       gwr #include <arpa/inet.h>
     65  1.1       gwr #include <arpa/nameser.h>
     66  1.1       gwr #include <netdb.h>
     67  1.1       gwr #include <resolv.h>
     68  1.1       gwr #include <stdio.h>
     69  1.1       gwr #include <ctype.h>
     70  1.1       gwr #include <errno.h>
     71  1.1       gwr #include <string.h>
     72  1.1       gwr 
     73  1.1       gwr #define	MAXALIASES	35
     74  1.1       gwr #define	MAXADDRS	35
     75  1.1       gwr 
     76  1.1       gwr static char *h_addr_ptrs[MAXADDRS + 1];
     77  1.1       gwr 
     78  1.1       gwr static struct hostent host;
     79  1.1       gwr static char *host_aliases[MAXALIASES];
     80  1.1       gwr static char hostbuf[BUFSIZ+1];
     81  1.1       gwr static struct in_addr host_addr;
     82  1.1       gwr static FILE *hostf = NULL;
     83  1.1       gwr static int stayopen = 0;
     84  1.1       gwr 
     85  1.4  sommerfe void _sethtent __P((int));
     86  1.4  sommerfe void _endhtent __P((void));
     87  1.4  sommerfe struct hostent *_gethtent __P((void));
     88  1.4  sommerfe struct hostent *_gethtbyname __P((const char *));
     89  1.4  sommerfe struct hostent *_gethtbyaddr __P((const char *, int, int));
     90  1.4  sommerfe 
     91  1.4  sommerfe 
     92  1.1       gwr #if PACKETSZ > 1024
     93  1.1       gwr #define	MAXPACKET	PACKETSZ
     94  1.1       gwr #else
     95  1.1       gwr #define	MAXPACKET	1024
     96  1.1       gwr #endif
     97  1.1       gwr 
     98  1.1       gwr extern int h_errno;
     99  1.1       gwr 
    100  1.1       gwr struct hostent *
    101  1.1       gwr gethostbyname(name)
    102  1.1       gwr 	const char *name;
    103  1.1       gwr {
    104  1.1       gwr 	register const char *cp;
    105  1.1       gwr 
    106  1.1       gwr 	/*
    107  1.1       gwr 	 * disallow names consisting only of digits/dots, unless
    108  1.1       gwr 	 * they end in a dot.
    109  1.1       gwr 	 */
    110  1.1       gwr 	if (isdigit(name[0]))
    111  1.1       gwr 		for (cp = name;; ++cp) {
    112  1.1       gwr 			if (!*cp) {
    113  1.1       gwr 				if (*--cp == '.')
    114  1.1       gwr 					break;
    115  1.1       gwr 				/*
    116  1.1       gwr 				 * All-numeric, no dot at the end.
    117  1.1       gwr 				 * Fake up a hostent as if we'd actually
    118  1.1       gwr 				 * done a lookup.
    119  1.1       gwr 				 */
    120  1.1       gwr 				if (!inet_aton(name, &host_addr)) {
    121  1.1       gwr 					h_errno = HOST_NOT_FOUND;
    122  1.1       gwr 					return((struct hostent *) NULL);
    123  1.1       gwr 				}
    124  1.1       gwr 				host.h_name = (char *)name;
    125  1.1       gwr 				host.h_aliases = host_aliases;
    126  1.1       gwr 				host_aliases[0] = NULL;
    127  1.1       gwr 				host.h_addrtype = AF_INET;
    128  1.1       gwr 				host.h_length = sizeof(u_int32_t);
    129  1.1       gwr 				h_addr_ptrs[0] = (char *)&host_addr;
    130  1.1       gwr 				h_addr_ptrs[1] = NULL;
    131  1.1       gwr 				host.h_addr_list = h_addr_ptrs;
    132  1.1       gwr 				return (&host);
    133  1.1       gwr 			}
    134  1.1       gwr 			if (!isdigit(*cp) && *cp != '.')
    135  1.1       gwr 				break;
    136  1.1       gwr 		}
    137  1.1       gwr 
    138  1.1       gwr 	/* XXX - Force host table lookup. */
    139  1.1       gwr 	return (_gethtbyname(name));
    140  1.1       gwr }
    141  1.1       gwr 
    142  1.1       gwr struct hostent *
    143  1.1       gwr gethostbyaddr(addr, len, type)
    144  1.1       gwr 	const char *addr;
    145  1.1       gwr 	int len, type;
    146  1.1       gwr {
    147  1.1       gwr 	char qbuf[MAXDNAME];
    148  1.2       gwr 
    149  1.1       gwr 	if (type != AF_INET)
    150  1.1       gwr 		return ((struct hostent *) NULL);
    151  1.1       gwr 	(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
    152  1.1       gwr 		((unsigned)addr[3] & 0xff),
    153  1.1       gwr 		((unsigned)addr[2] & 0xff),
    154  1.1       gwr 		((unsigned)addr[1] & 0xff),
    155  1.1       gwr 		((unsigned)addr[0] & 0xff));
    156  1.1       gwr 
    157  1.1       gwr 	/* XXX - Force host table lookup. */
    158  1.1       gwr 	return (_gethtbyaddr(addr, len, type));
    159  1.1       gwr }
    160  1.1       gwr 
    161  1.1       gwr void
    162  1.1       gwr _sethtent(f)
    163  1.1       gwr 	int f;
    164  1.1       gwr {
    165  1.1       gwr 	if (hostf == NULL)
    166  1.1       gwr 		hostf = fopen(_PATH_HOSTS, "r" );
    167  1.1       gwr 	else
    168  1.1       gwr 		rewind(hostf);
    169  1.1       gwr 	stayopen = f;
    170  1.1       gwr }
    171  1.1       gwr 
    172  1.1       gwr void
    173  1.1       gwr _endhtent()
    174  1.1       gwr {
    175  1.1       gwr 	if (hostf && !stayopen) {
    176  1.1       gwr 		(void) fclose(hostf);
    177  1.1       gwr 		hostf = NULL;
    178  1.1       gwr 	}
    179  1.1       gwr }
    180  1.1       gwr 
    181  1.1       gwr struct hostent *
    182  1.1       gwr _gethtent()
    183  1.1       gwr {
    184  1.1       gwr 	char *p;
    185  1.1       gwr 	register char *cp, **q;
    186  1.1       gwr 
    187  1.1       gwr 	if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
    188  1.1       gwr 		return (NULL);
    189  1.1       gwr again:
    190  1.1       gwr 	if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
    191  1.1       gwr 		return (NULL);
    192  1.1       gwr 	if (*p == '#')
    193  1.1       gwr 		goto again;
    194  1.1       gwr 	cp = strpbrk(p, "#\n");
    195  1.1       gwr 	if (cp == NULL)
    196  1.1       gwr 		goto again;
    197  1.1       gwr 	*cp = '\0';
    198  1.1       gwr 	cp = strpbrk(p, " \t");
    199  1.1       gwr 	if (cp == NULL)
    200  1.1       gwr 		goto again;
    201  1.1       gwr 	*cp++ = '\0';
    202  1.1       gwr 	/* THIS STUFF IS INTERNET SPECIFIC */
    203  1.1       gwr 	h_addr_ptrs[0] = (char *)&host_addr;
    204  1.1       gwr 	h_addr_ptrs[1] = NULL;
    205  1.1       gwr 	(void) inet_aton(p, &host_addr);
    206  1.1       gwr 	host.h_addr_list = h_addr_ptrs;
    207  1.1       gwr 	host.h_length = sizeof(u_int32_t);
    208  1.1       gwr 	host.h_addrtype = AF_INET;
    209  1.1       gwr 	while (*cp == ' ' || *cp == '\t')
    210  1.1       gwr 		cp++;
    211  1.1       gwr 	host.h_name = cp;
    212  1.1       gwr 	q = host.h_aliases = host_aliases;
    213  1.1       gwr 	cp = strpbrk(cp, " \t");
    214  1.1       gwr 	if (cp != NULL)
    215  1.1       gwr 		*cp++ = '\0';
    216  1.1       gwr 	while (cp && *cp) {
    217  1.1       gwr 		if (*cp == ' ' || *cp == '\t') {
    218  1.1       gwr 			cp++;
    219  1.1       gwr 			continue;
    220  1.1       gwr 		}
    221  1.1       gwr 		if (q < &host_aliases[MAXALIASES - 1])
    222  1.1       gwr 			*q++ = cp;
    223  1.1       gwr 		cp = strpbrk(cp, " \t");
    224  1.1       gwr 		if (cp != NULL)
    225  1.1       gwr 			*cp++ = '\0';
    226  1.1       gwr 	}
    227  1.1       gwr 	*q = NULL;
    228  1.1       gwr 	return (&host);
    229  1.1       gwr }
    230  1.1       gwr 
    231  1.1       gwr struct hostent *
    232  1.1       gwr _gethtbyname(name)
    233  1.4  sommerfe 	const char *name;
    234  1.1       gwr {
    235  1.1       gwr 	register struct hostent *p;
    236  1.1       gwr 	register char **cp;
    237  1.1       gwr 
    238  1.1       gwr 	_sethtent(0);
    239  1.4  sommerfe 	while ((p = _gethtent()) != NULL) {
    240  1.1       gwr 		if (strcasecmp(p->h_name, name) == 0)
    241  1.1       gwr 			break;
    242  1.1       gwr 		for (cp = p->h_aliases; *cp != 0; cp++)
    243  1.1       gwr 			if (strcasecmp(*cp, name) == 0)
    244  1.1       gwr 				goto found;
    245  1.1       gwr 	}
    246  1.1       gwr found:
    247  1.1       gwr 	_endhtent();
    248  1.1       gwr 	if (p==NULL)
    249  1.1       gwr 		h_errno = HOST_NOT_FOUND;
    250  1.1       gwr 	return (p);
    251  1.1       gwr }
    252  1.1       gwr 
    253  1.1       gwr struct hostent *
    254  1.1       gwr _gethtbyaddr(addr, len, type)
    255  1.1       gwr 	const char *addr;
    256  1.1       gwr 	int len, type;
    257  1.1       gwr {
    258  1.1       gwr 	register struct hostent *p;
    259  1.1       gwr 
    260  1.1       gwr 	_sethtent(0);
    261  1.4  sommerfe 	while ((p = _gethtent()) != NULL)
    262  1.3     perry 		if (p->h_addrtype == type && !memcmp(p->h_addr, addr, len))
    263  1.1       gwr 			break;
    264  1.1       gwr 	_endhtent();
    265  1.1       gwr 	if (p==NULL)
    266  1.1       gwr 		h_errno = HOST_NOT_FOUND;
    267  1.1       gwr 	return (p);
    268  1.1       gwr }
    269  1.1       gwr 
    270