1 /*- 2 * Copyright (c) 1994, Garrett Wollman 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #if defined(LIBC_SCCS) && !defined(lint) 27 static char sccsid[] = "@(#)$FreeBSD$"; 28 static char rcsid[] = "$FreeBSD$"; 29 #endif /* LIBC_SCCS and not lint */ 30 31 #include <sys/param.h> 32 #include <sys/socket.h> 33 #include <netinet/in.h> 34 #include <arpa/inet.h> 35 #include <arpa/nameser.h> 36 #include <netdb.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <ctype.h> 40 #include <errno.h> 41 #include <string.h> 42 #ifdef YP 43 #include <rpc/rpc.h> 44 #include <rpcsvc/yp_prot.h> 45 #include <rpcsvc/ypclnt.h> 46 #endif 47 48 #define MAXALIASES 35 49 #define MAXADDRS 35 50 51 #ifdef YP 52 static char *host_aliases[MAXALIASES]; 53 static char hostaddr[MAXADDRS]; 54 static char *host_addrs[2]; 55 #endif /* YP */ 56 57 static struct hostent * 58 _gethostbynis(name, map, af) 59 const char *name; 60 char *map; 61 int af; 62 { 63 #ifdef YP 64 register char *cp, **q; 65 char *result; 66 int resultlen,size; 67 static struct hostent h; 68 static char *domain = (char *)NULL; 69 static char ypbuf[YPMAXRECORD + 2]; 70 71 switch(af) { 72 case AF_INET: 73 size = NS_INADDRSZ; 74 break; 75 default: 76 case AF_INET6: 77 size = NS_IN6ADDRSZ; 78 errno = EAFNOSUPPORT; 79 return NULL; 80 } 81 82 if (domain == (char *)NULL) 83 if (yp_get_default_domain (&domain)) 84 return ((struct hostent *)NULL); 85 86 if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) 87 return ((struct hostent *)NULL); 88 89 /* avoid potential memory leak */ 90 bcopy((char *)result, (char *)&ypbuf, resultlen); 91 ypbuf[resultlen] = '\0'; 92 free(result); 93 result = (char *)&ypbuf; 94 95 if ((cp = index(result, '\n'))) 96 *cp = '\0'; 97 98 cp = strpbrk(result, " \t"); 99 *cp++ = '\0'; 100 h.h_addr_list = host_addrs; 101 h.h_addr = hostaddr; 102 *((u_long *)h.h_addr) = inet_addr(result); 103 h.h_length = size; 104 h.h_addrtype = AF_INET; 105 while (*cp == ' ' || *cp == '\t') 106 cp++; 107 h.h_name = cp; 108 q = h.h_aliases = host_aliases; 109 cp = strpbrk(cp, " \t"); 110 if (cp != NULL) 111 *cp++ = '\0'; 112 while (cp && *cp) { 113 if (*cp == ' ' || *cp == '\t') { 114 cp++; 115 continue; 116 } 117 if (q < &host_aliases[MAXALIASES - 1]) 118 *q++ = cp; 119 cp = strpbrk(cp, " \t"); 120 if (cp != NULL) 121 *cp++ = '\0'; 122 } 123 *q = NULL; 124 return (&h); 125 #else 126 return (NULL); 127 #endif /* YP */ 128 } 129 130 struct hostent * 131 _gethostbynisname(name, af) 132 const char *name; 133 int af; 134 { 135 return _gethostbynis(name, "hosts.byname", af); 136 } 137 138 struct hostent * 139 _gethostbynisaddr(addr, len, af) 140 const char *addr; 141 int len; 142 int af; 143 { 144 return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af); 145 } 146