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 <netdb.h> 36 #include <stdio.h> 37 #include <ctype.h> 38 #include <errno.h> 39 #include <string.h> 40 #include <stdarg.h> 41 #include <nsswitch.h> 42 #include <arpa/nameser.h> /* XXX hack for _res */ 43 #include <resolv.h> /* XXX hack for _res */ 44 45 extern int _ht_gethostbyname(void *, void *, va_list); 46 extern int _dns_gethostbyname(void *, void *, va_list); 47 extern int _nis_gethostbyname(void *, void *, va_list); 48 extern int _ht_gethostbyaddr(void *, void *, va_list); 49 extern int _dns_gethostbyaddr(void *, void *, va_list); 50 extern int _nis_gethostbyaddr(void *, void *, va_list); 51 52 /* Host lookup order if nsswitch.conf is broken or nonexistant */ 53 static const ns_src default_src[] = { 54 { NSSRC_FILES, NS_SUCCESS }, 55 { NSSRC_DNS, NS_SUCCESS }, 56 { 0 } 57 }; 58 59 struct hostent * 60 gethostbyname(const char *name) 61 { 62 struct hostent *hp; 63 64 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 65 h_errno = NETDB_INTERNAL; 66 return (NULL); 67 } 68 if (_res.options & RES_USE_INET6) { /* XXX */ 69 hp = gethostbyname2(name, AF_INET6); /* XXX */ 70 if (hp) /* XXX */ 71 return (hp); /* XXX */ 72 } /* XXX */ 73 return (gethostbyname2(name, AF_INET)); 74 } 75 76 struct hostent * 77 gethostbyname2(const char *name, int type) 78 { 79 struct hostent *hp = 0; 80 int rval; 81 82 static const ns_dtab dtab[] = { 83 NS_FILES_CB(_ht_gethostbyname, NULL) 84 { NSSRC_DNS, _dns_gethostbyname, NULL }, 85 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */ 86 { 0 } 87 }; 88 89 rval = nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyname", 90 default_src, name, type); 91 92 if (rval != NS_SUCCESS) 93 return NULL; 94 else 95 return hp; 96 } 97 98 struct hostent * 99 gethostbyaddr(const char *addr, int len, int type) 100 { 101 struct hostent *hp = 0; 102 int rval; 103 104 static const ns_dtab dtab[] = { 105 NS_FILES_CB(_ht_gethostbyaddr, NULL) 106 { NSSRC_DNS, _dns_gethostbyaddr, NULL }, 107 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */ 108 { 0 } 109 }; 110 111 rval = nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyaddr", 112 default_src, addr, len, type); 113 114 if (rval != NS_SUCCESS) 115 return NULL; 116 else 117 return hp; 118 } 119 120 #ifdef _THREAD_SAFE 121 struct hostent_data; 122 123 /* 124 * Temporary function (not thread safe) 125 */ 126 int gethostbyaddr_r(const char *addr, int len, int type, 127 struct hostent *result, struct hostent_data *buffer) 128 { 129 struct hostent *hp; 130 int ret; 131 if ((hp = gethostbyaddr(addr, len, type)) == NULL) { 132 ret = -1; 133 } else { 134 memcpy(result, hp, sizeof(struct hostent)); 135 ret = 0; 136 } 137 return(ret); 138 } 139 #endif 140 141 void 142 sethostent(stayopen) 143 int stayopen; 144 { 145 _sethosthtent(stayopen); 146 _sethostdnsent(stayopen); 147 } 148 149 void 150 endhostent() 151 { 152 _endhosthtent(); 153 _endhostdnsent(); 154 } 155