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 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include "namespace.h" 30 #include <sys/param.h> 31 #include <sys/socket.h> 32 #include <netinet/in.h> 33 #include <arpa/inet.h> 34 #include <netdb.h> 35 #include <stdio.h> 36 #include <ctype.h> 37 #include <string.h> 38 #include <stdarg.h> 39 #include <nsswitch.h> 40 #include <arpa/nameser.h> /* XXX hack for _res */ 41 #include <resolv.h> /* XXX hack for _res */ 42 #include "un-namespace.h" 43 44 extern int _ht_gethostbyname(void *, void *, va_list); 45 extern int _dns_gethostbyname(void *, void *, va_list); 46 extern int _nis_gethostbyname(void *, void *, va_list); 47 extern int _ht_gethostbyaddr(void *, void *, va_list); 48 extern int _dns_gethostbyaddr(void *, void *, va_list); 49 extern int _nis_gethostbyaddr(void *, void *, va_list); 50 51 /* Host lookup order if nsswitch.conf is broken or nonexistant */ 52 static const ns_src default_src[] = { 53 { NSSRC_FILES, NS_SUCCESS }, 54 { NSSRC_DNS, NS_SUCCESS }, 55 { 0 } 56 }; 57 58 struct hostent * 59 gethostbyname(const char *name) 60 { 61 struct hostent *hp; 62 63 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 64 h_errno = NETDB_INTERNAL; 65 return (NULL); 66 } 67 if (_res.options & RES_USE_INET6) { /* XXX */ 68 hp = gethostbyname2(name, AF_INET6); /* XXX */ 69 if (hp) /* XXX */ 70 return (hp); /* XXX */ 71 } /* XXX */ 72 return (gethostbyname2(name, AF_INET)); 73 } 74 75 struct hostent * 76 gethostbyname2(const char *name, int type) 77 { 78 struct hostent *hp = 0; 79 int rval; 80 81 static const ns_dtab dtab[] = { 82 NS_FILES_CB(_ht_gethostbyname, NULL) 83 { NSSRC_DNS, _dns_gethostbyname, NULL }, 84 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */ 85 { 0 } 86 }; 87 88 rval = _nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyname", 89 default_src, name, type); 90 91 if (rval != NS_SUCCESS) 92 return NULL; 93 else 94 return hp; 95 } 96 97 struct hostent * 98 gethostbyaddr(const char *addr, int len, int type) 99 { 100 struct hostent *hp = 0; 101 int rval; 102 103 static const ns_dtab dtab[] = { 104 NS_FILES_CB(_ht_gethostbyaddr, NULL) 105 { NSSRC_DNS, _dns_gethostbyaddr, NULL }, 106 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */ 107 { 0 } 108 }; 109 110 rval = _nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyaddr", 111 default_src, addr, len, type); 112 113 if (rval != NS_SUCCESS) 114 return NULL; 115 else 116 return hp; 117 } 118 119 void 120 sethostent(stayopen) 121 int stayopen; 122 { 123 _sethosthtent(stayopen); 124 _sethostdnsent(stayopen); 125 } 126 127 void 128 endhostent() 129 { 130 _endhosthtent(); 131 _endhostdnsent(); 132 } 133