gethostnamadr.c revision 113977
11573Srgrimes/*- 23070Spst * Copyright (c) 1994, Garrett Wollman 31573Srgrimes * 41573Srgrimes * Redistribution and use in source and binary forms, with or without 51573Srgrimes * modification, are permitted provided that the following conditions 61573Srgrimes * are met: 71573Srgrimes * 1. Redistributions of source code must retain the above copyright 81573Srgrimes * notice, this list of conditions and the following disclaimer. 91573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 101573Srgrimes * notice, this list of conditions and the following disclaimer in the 111573Srgrimes * documentation and/or other materials provided with the distribution. 121573Srgrimes * 133070Spst * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 141573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 151573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 161573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 171573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 181573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 191573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 201573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 211573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 221573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 231573Srgrimes * SUCH DAMAGE. 241573Srgrimes */ 251573Srgrimes 2692986Sobrien#include <sys/cdefs.h> 2792986Sobrien__FBSDID("$FreeBSD: head/lib/libc/net/gethostnamadr.c 113977 2003-04-24 18:05:48Z nectar $"); 281573Srgrimes 29113977Snectar#include "namespace.h" 301573Srgrimes#include <sys/param.h> 311573Srgrimes#include <sys/socket.h> 321573Srgrimes#include <netinet/in.h> 331573Srgrimes#include <arpa/inet.h> 341573Srgrimes#include <netdb.h> 351573Srgrimes#include <stdio.h> 361573Srgrimes#include <ctype.h> 371573Srgrimes#include <string.h> 3865532Snectar#include <stdarg.h> 3965532Snectar#include <nsswitch.h> 4017903Speter#include <arpa/nameser.h> /* XXX hack for _res */ 4117903Speter#include <resolv.h> /* XXX hack for _res */ 42113977Snectar#include "un-namespace.h" 431573Srgrimes 4465532Snectarextern int _ht_gethostbyname(void *, void *, va_list); 4565532Snectarextern int _dns_gethostbyname(void *, void *, va_list); 4665532Snectarextern int _nis_gethostbyname(void *, void *, va_list); 4765532Snectarextern int _ht_gethostbyaddr(void *, void *, va_list); 4865532Snectarextern int _dns_gethostbyaddr(void *, void *, va_list); 4965532Snectarextern int _nis_gethostbyaddr(void *, void *, va_list); 501991Swollman 5165532Snectar/* Host lookup order if nsswitch.conf is broken or nonexistant */ 5265532Snectarstatic const ns_src default_src[] = { 5365532Snectar { NSSRC_FILES, NS_SUCCESS }, 5465532Snectar { NSSRC_DNS, NS_SUCCESS }, 5565532Snectar { 0 } 561991Swollman}; 571991Swollman 581573Srgrimesstruct hostent * 591991Swollmangethostbyname(const char *name) 601991Swollman{ 6117903Speter struct hostent *hp; 6217903Speter 6367709Sume if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 6467709Sume h_errno = NETDB_INTERNAL; 6567709Sume return (NULL); 6667709Sume } 6717903Speter if (_res.options & RES_USE_INET6) { /* XXX */ 6817903Speter hp = gethostbyname2(name, AF_INET6); /* XXX */ 6917903Speter if (hp) /* XXX */ 7017903Speter return (hp); /* XXX */ 7117903Speter } /* XXX */ 7217903Speter return (gethostbyname2(name, AF_INET)); 7317903Speter} 7417903Speter 7517903Speterstruct hostent * 7617903Spetergethostbyname2(const char *name, int type) 7717903Speter{ 781991Swollman struct hostent *hp = 0; 7965532Snectar int rval; 801991Swollman 8165532Snectar static const ns_dtab dtab[] = { 8265532Snectar NS_FILES_CB(_ht_gethostbyname, NULL) 8365532Snectar { NSSRC_DNS, _dns_gethostbyname, NULL }, 8465532Snectar NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */ 8565532Snectar { 0 } 8665532Snectar }; 8765532Snectar 88113977Snectar rval = _nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyname", 8965532Snectar default_src, name, type); 901991Swollman 9165532Snectar if (rval != NS_SUCCESS) 9265532Snectar return NULL; 9365532Snectar else 9465532Snectar return hp; 951991Swollman} 961991Swollman 971991Swollmanstruct hostent * 981991Swollmangethostbyaddr(const char *addr, int len, int type) 991991Swollman{ 1001991Swollman struct hostent *hp = 0; 10165532Snectar int rval; 1021991Swollman 10365532Snectar static const ns_dtab dtab[] = { 10465532Snectar NS_FILES_CB(_ht_gethostbyaddr, NULL) 10565532Snectar { NSSRC_DNS, _dns_gethostbyaddr, NULL }, 10665532Snectar NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */ 10765532Snectar { 0 } 10865532Snectar }; 1091991Swollman 110113977Snectar rval = _nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyaddr", 11165532Snectar default_src, addr, len, type); 11265532Snectar 11365532Snectar if (rval != NS_SUCCESS) 11465532Snectar return NULL; 11565532Snectar else 11665532Snectar return hp; 1171991Swollman} 1181991Swollman 11917706Sjulianstruct hostent_data; 12017706Sjulian 12117706Sjulian/* 12217706Sjulian * Temporary function (not thread safe) 12317706Sjulian */ 12417706Sjulianint gethostbyaddr_r(const char *addr, int len, int type, 12517706Sjulian struct hostent *result, struct hostent_data *buffer) 12617706Sjulian{ 12731984Salex struct hostent *hp; 12817706Sjulian int ret; 12917706Sjulian if ((hp = gethostbyaddr(addr, len, type)) == NULL) { 13017706Sjulian ret = -1; 13117706Sjulian } else { 13217706Sjulian memcpy(result, hp, sizeof(struct hostent)); 13317706Sjulian ret = 0; 13417706Sjulian } 13517706Sjulian return(ret); 13617706Sjulian} 13717706Sjulian 1383070Spstvoid 1393070Spstsethostent(stayopen) 1403070Spst int stayopen; 1413070Spst{ 1423070Spst _sethosthtent(stayopen); 1433070Spst _sethostdnsent(stayopen); 1443070Spst} 1453070Spst 1463070Spstvoid 1473070Spstendhostent() 1483070Spst{ 1493070Spst _endhosthtent(); 1503070Spst _endhostdnsent(); 1513070Spst} 152