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