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[] = "@(#)$Id: gethostbynis.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $"; 28 static char rcsid[] = "$Id: gethostbynis.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $"; 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 <netdb.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <ctype.h> 39 #include <errno.h> 40 #include <string.h> 41 #ifdef YP 42 #include <rpc/rpc.h> 43 #include <rpcsvc/yp_prot.h> 44 #include <rpcsvc/ypclnt.h> 45 #endif 46 47 #define MAXALIASES 35 48 #define MAXADDRS 35 49 50 #ifdef YP 51 static char *host_aliases[MAXALIASES]; 52 static char hostaddr[MAXADDRS]; 53 static char *host_addrs[2]; 54 #endif /* YP */ 55 56 static struct hostent * 57 _gethostbynis(const char *name, char *map, int af) 58 { 59 #ifdef YP 60 register char *cp, **q; 61 char *result; 62 int resultlen; 63 static struct hostent h; 64 static char *domain = (char *)NULL; 65 static char ypbuf[YPMAXRECORD + 2]; 66 67 switch(af) { 68 case AF_INET: 69 break; 70 default: 71 case AF_INET6: 72 errno = EAFNOSUPPORT; 73 return NULL; 74 } 75 76 if (domain == (char *)NULL) 77 if (yp_get_default_domain (&domain)) 78 return ((struct hostent *)NULL); 79 80 if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) 81 return ((struct hostent *)NULL); 82 83 /* avoid potential memory leak */ 84 bcopy((char *)result, (char *)&ypbuf, resultlen); 85 ypbuf[resultlen] = '\0'; 86 free(result); 87 result = (char *)&ypbuf; 88 89 if ((cp = index(result, '\n'))) 90 *cp = '\0'; 91 92 cp = strpbrk(result, " \t"); 93 *cp++ = '\0'; 94 h.h_addr_list = host_addrs; 95 h.h_addr = hostaddr; 96 *((u_long *)h.h_addr) = inet_addr(result); 97 h.h_length = sizeof(u_long); 98 h.h_addrtype = AF_INET; 99 while (*cp == ' ' || *cp == '\t') 100 cp++; 101 h.h_name = cp; 102 q = h.h_aliases = host_aliases; 103 cp = strpbrk(cp, " \t"); 104 if (cp != NULL) 105 *cp++ = '\0'; 106 while (cp && *cp) { 107 if (*cp == ' ' || *cp == '\t') { 108 cp++; 109 continue; 110 } 111 if (q < &host_aliases[MAXALIASES - 1]) 112 *q++ = cp; 113 cp = strpbrk(cp, " \t"); 114 if (cp != NULL) 115 *cp++ = '\0'; 116 } 117 *q = NULL; 118 return (&h); 119 #else 120 return (NULL); 121 #endif /* YP */ 122 } 123 124 struct hostent * 125 _gethostbynisname(const char *name, int af) 126 { 127 return _gethostbynis(name, "hosts.byname", af); 128 } 129 130 struct hostent * 131 _gethostbynisaddr(const char *addr, int len, int af) 132 { 133 return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af); 134 } 135