gethostnamadr.c revision 52865
1255736Sdavidch/*- 2255736Sdavidch * Copyright (c) 1994, Garrett Wollman 3255736Sdavidch * 4255736Sdavidch * Redistribution and use in source and binary forms, with or without 5255736Sdavidch * modification, are permitted provided that the following conditions 6255736Sdavidch * are met: 7255736Sdavidch * 1. Redistributions of source code must retain the above copyright 8255736Sdavidch * notice, this list of conditions and the following disclaimer. 9255736Sdavidch * 2. Redistributions in binary form must reproduce the above copyright 10255736Sdavidch * notice, this list of conditions and the following disclaimer in the 11255736Sdavidch * documentation and/or other materials provided with the distribution. 12255736Sdavidch * 13255736Sdavidch * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14255736Sdavidch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15255736Sdavidch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16255736Sdavidch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17255736Sdavidch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18255736Sdavidch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19255736Sdavidch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20255736Sdavidch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21255736Sdavidch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22255736Sdavidch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23255736Sdavidch * SUCH DAMAGE. 24255736Sdavidch */ 25255736Sdavidch 26255736Sdavidch#if defined(LIBC_SCCS) && !defined(lint) 27255736Sdavidchstatic char sccsid[] = "@(#)$FreeBSD: head/lib/libc/net/gethostnamadr.c 52865 1999-11-04 05:01:28Z ache $"; 28255736Sdavidchstatic char rcsid[] = "$FreeBSD: head/lib/libc/net/gethostnamadr.c 52865 1999-11-04 05:01:28Z ache $"; 29255736Sdavidch#endif /* LIBC_SCCS and not lint */ 30255736Sdavidch 31255736Sdavidch#include <sys/param.h> 32255736Sdavidch#include <sys/socket.h> 33255736Sdavidch#include <netinet/in.h> 34255736Sdavidch#include <arpa/inet.h> 35255736Sdavidch#include <netdb.h> 36255736Sdavidch#include <stdio.h> 37260415Sedavis#include <ctype.h> 38255736Sdavidch#include <errno.h> 39255736Sdavidch#include <string.h> 40255736Sdavidch#include <arpa/nameser.h> /* XXX hack for _res */ 41255736Sdavidch#include <resolv.h> /* XXX hack for _res */ 42255736Sdavidch 43255736Sdavidch#define _PATH_HOSTCONF "/etc/host.conf" 44255736Sdavidch 45255736Sdavidchenum service_type { 46255736Sdavidch SERVICE_NONE = 0, 47255736Sdavidch SERVICE_BIND, 48255736Sdavidch SERVICE_HOSTS, 49255736Sdavidch SERVICE_NIS }; 50255736Sdavidch#define SERVICE_MAX SERVICE_NIS 51255736Sdavidch 52255736Sdavidchstatic struct { 53255736Sdavidch const char *name; 54255736Sdavidch enum service_type type; 55255736Sdavidch} service_names[] = { 56255736Sdavidch { "hosts", SERVICE_HOSTS }, 57255736Sdavidch { "/etc/hosts", SERVICE_HOSTS }, 58255736Sdavidch { "hosttable", SERVICE_HOSTS }, 59255736Sdavidch { "htable", SERVICE_HOSTS }, 60255736Sdavidch { "bind", SERVICE_BIND }, 61255736Sdavidch { "dns", SERVICE_BIND }, 62255736Sdavidch { "domain", SERVICE_BIND }, 63255736Sdavidch { "yp", SERVICE_NIS }, 64255736Sdavidch { "yellowpages", SERVICE_NIS }, 65255736Sdavidch { "nis", SERVICE_NIS }, 66255736Sdavidch { 0, SERVICE_NONE } 67255736Sdavidch}; 68255736Sdavidch 69255736Sdavidchstatic enum service_type service_order[SERVICE_MAX + 1]; 70255736Sdavidchstatic int service_done = 0; 71255736Sdavidch 72255736Sdavidchstatic enum service_type 73255736Sdavidchget_service_name(const char *name) { 74255736Sdavidch int i; 75255736Sdavidch for(i = 0; service_names[i].type != SERVICE_NONE; i++) { 76255736Sdavidch if(!strcasecmp(name, service_names[i].name)) { 77255736Sdavidch return service_names[i].type; 78255736Sdavidch } 79255736Sdavidch } 80255736Sdavidch return SERVICE_NONE; 81255736Sdavidch} 82255736Sdavidch 83255736Sdavidchstatic void 84255736Sdavidchinit_services() 85255736Sdavidch{ 86255736Sdavidch char *cp, *p, buf[BUFSIZ]; 87255736Sdavidch register int cc = 0; 88255736Sdavidch FILE *fd; 89255736Sdavidch 90255736Sdavidch if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) { 91255736Sdavidch /* make some assumptions */ 92255736Sdavidch service_order[0] = SERVICE_BIND; 93255736Sdavidch service_order[1] = SERVICE_HOSTS; 94255736Sdavidch service_order[2] = SERVICE_NONE; 95255736Sdavidch } else { 96255736Sdavidch while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { 97255736Sdavidch if(buf[0] == '#') 98255736Sdavidch continue; 99255736Sdavidch 100255736Sdavidch p = buf; 101255736Sdavidch while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') 102255736Sdavidch ; 103255736Sdavidch if (cp == NULL) 104255736Sdavidch continue; 105255736Sdavidch do { 106255736Sdavidch if (isalpha((unsigned char)cp[0])) { 107255736Sdavidch service_order[cc] = get_service_name(cp); 108255736Sdavidch if(service_order[cc] != SERVICE_NONE) 109255736Sdavidch cc++; 110255736Sdavidch } 111255736Sdavidch while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') 112255736Sdavidch ; 113255736Sdavidch } while(cp != NULL && cc < SERVICE_MAX); 114255736Sdavidch } 115255736Sdavidch service_order[cc] = SERVICE_NONE; 116255736Sdavidch fclose(fd); 117255736Sdavidch } 118255736Sdavidch service_done = 1; 119255736Sdavidch} 120255736Sdavidch 121255736Sdavidchstruct hostent * 122255736Sdavidchgethostbyname(const char *name) 123255736Sdavidch{ 124255736Sdavidch struct hostent *hp; 125255736Sdavidch 126255736Sdavidch if (_res.options & RES_USE_INET6) { /* XXX */ 127255736Sdavidch hp = gethostbyname2(name, AF_INET6); /* XXX */ 128255736Sdavidch if (hp) /* XXX */ 129255736Sdavidch return (hp); /* XXX */ 130255736Sdavidch } /* XXX */ 131255736Sdavidch return (gethostbyname2(name, AF_INET)); 132255736Sdavidch} 133255736Sdavidch 134255736Sdavidchstruct hostent * 135255736Sdavidchgethostbyname2(const char *name, int type) 136255736Sdavidch{ 137255736Sdavidch struct hostent *hp = 0; 138255736Sdavidch int nserv = 0; 139255736Sdavidch 140255736Sdavidch if (!service_done) 141255736Sdavidch init_services(); 142255736Sdavidch 143255736Sdavidch while (!hp) { 144255736Sdavidch switch (service_order[nserv]) { 145255736Sdavidch case SERVICE_NONE: 146255736Sdavidch return NULL; 147255736Sdavidch case SERVICE_HOSTS: 148255736Sdavidch hp = _gethostbyhtname(name, type); 149255736Sdavidch break; 150255736Sdavidch case SERVICE_BIND: 151255736Sdavidch hp = _gethostbydnsname(name, type); 152255736Sdavidch break; 153255736Sdavidch case SERVICE_NIS: 154255736Sdavidch hp = _gethostbynisname(name, type); 155255736Sdavidch break; 156255736Sdavidch } 157255736Sdavidch nserv++; 158255736Sdavidch } 159255736Sdavidch return hp; 160255736Sdavidch} 161255736Sdavidch 162255736Sdavidchstruct hostent * 163255736Sdavidchgethostbyaddr(const char *addr, int len, int type) 164255736Sdavidch{ 165255736Sdavidch struct hostent *hp = 0; 166255736Sdavidch int nserv = 0; 167255736Sdavidch 168255736Sdavidch if (!service_done) 169255736Sdavidch init_services(); 170255736Sdavidch 171255736Sdavidch while (!hp) { 172255736Sdavidch switch (service_order[nserv]) { 173255736Sdavidch case SERVICE_NONE: 174255736Sdavidch return 0; 175255736Sdavidch case SERVICE_HOSTS: 176255736Sdavidch hp = _gethostbyhtaddr(addr, len, type); 177255736Sdavidch break; 178255736Sdavidch case SERVICE_BIND: 179255736Sdavidch hp = _gethostbydnsaddr(addr, len, type); 180255736Sdavidch break; 181255736Sdavidch case SERVICE_NIS: 182255736Sdavidch hp = _gethostbynisaddr(addr, len, type); 183255736Sdavidch break; 184255736Sdavidch } 185255736Sdavidch nserv++; 186255736Sdavidch } 187255736Sdavidch return hp; 188255736Sdavidch} 189255736Sdavidch 190255736Sdavidch#ifdef _THREAD_SAFE 191255736Sdavidchstruct hostent_data; 192255736Sdavidch 193255736Sdavidch/* 194255736Sdavidch * Temporary function (not thread safe) 195255736Sdavidch */ 196255736Sdavidchint gethostbyaddr_r(const char *addr, int len, int type, 197255736Sdavidch struct hostent *result, struct hostent_data *buffer) 198255736Sdavidch{ 199255736Sdavidch struct hostent *hp; 200255736Sdavidch int ret; 201255736Sdavidch if ((hp = gethostbyaddr(addr, len, type)) == NULL) { 202255736Sdavidch ret = -1; 203255736Sdavidch } else { 204255736Sdavidch memcpy(result, hp, sizeof(struct hostent)); 205255736Sdavidch ret = 0; 206255736Sdavidch } 207255736Sdavidch return(ret); 208255736Sdavidch} 209255736Sdavidch#endif 210255736Sdavidch 211255736Sdavidchvoid 212255736Sdavidchsethostent(stayopen) 213255736Sdavidch int stayopen; 214255736Sdavidch{ 215255736Sdavidch _sethosthtent(stayopen); 216255736Sdavidch _sethostdnsent(stayopen); 217255736Sdavidch} 218255736Sdavidch 219255736Sdavidchvoid 220255736Sdavidchendhostent() 221255736Sdavidch{ 222255736Sdavidch _endhosthtent(); 223255736Sdavidch _endhostdnsent(); 224255736Sdavidch} 225255736Sdavidch