gethostnamadr.c revision 81586
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) 27static const char rcsid[] = 28 "$FreeBSD: head/lib/libc/net/gethostnamadr.c 81586 2001-08-13 14:06:34Z ru $"; 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 <string.h> 39#include <stdarg.h> 40#include <nsswitch.h> 41#include <arpa/nameser.h> /* XXX hack for _res */ 42#include <resolv.h> /* XXX hack for _res */ 43 44extern int _ht_gethostbyname(void *, void *, va_list); 45extern int _dns_gethostbyname(void *, void *, va_list); 46extern int _nis_gethostbyname(void *, void *, va_list); 47extern int _ht_gethostbyaddr(void *, void *, va_list); 48extern int _dns_gethostbyaddr(void *, void *, va_list); 49extern int _nis_gethostbyaddr(void *, void *, va_list); 50 51/* Host lookup order if nsswitch.conf is broken or nonexistant */ 52static const ns_src default_src[] = { 53 { NSSRC_FILES, NS_SUCCESS }, 54 { NSSRC_DNS, NS_SUCCESS }, 55 { 0 } 56}; 57 58struct hostent * 59gethostbyname(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 75struct hostent * 76gethostbyname2(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 97struct hostent * 98gethostbyaddr(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 119struct hostent_data; 120 121/* 122 * Temporary function (not thread safe) 123 */ 124int gethostbyaddr_r(const char *addr, int len, int type, 125 struct hostent *result, struct hostent_data *buffer) 126{ 127 struct hostent *hp; 128 int ret; 129 if ((hp = gethostbyaddr(addr, len, type)) == NULL) { 130 ret = -1; 131 } else { 132 memcpy(result, hp, sizeof(struct hostent)); 133 ret = 0; 134 } 135 return(ret); 136} 137 138void 139sethostent(stayopen) 140 int stayopen; 141{ 142 _sethosthtent(stayopen); 143 _sethostdnsent(stayopen); 144} 145 146void 147endhostent() 148{ 149 _endhosthtent(); 150 _endhostdnsent(); 151} 152