xref: /freebsd/lib/libc/net/gethostnamadr.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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 <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <nsswitch.h>
39 #include <arpa/nameser.h>		/* XXX hack for _res */
40 #include <resolv.h>			/* XXX hack for _res */
41 
42 extern int _ht_gethostbyname(void *, void *, va_list);
43 extern int _dns_gethostbyname(void *, void *, va_list);
44 extern int _nis_gethostbyname(void *, void *, va_list);
45 extern int _ht_gethostbyaddr(void *, void *, va_list);
46 extern int _dns_gethostbyaddr(void *, void *, va_list);
47 extern int _nis_gethostbyaddr(void *, void *, va_list);
48 
49 /* Host lookup order if nsswitch.conf is broken or nonexistant */
50 static const ns_src default_src[] = {
51 	{ NSSRC_FILES, NS_SUCCESS },
52 	{ NSSRC_DNS, NS_SUCCESS },
53 	{ 0 }
54 };
55 
56 struct hostent *
57 gethostbyname(const char *name)
58 {
59 	struct hostent *hp;
60 
61 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
62 		h_errno = NETDB_INTERNAL;
63 		return (NULL);
64 	}
65 	if (_res.options & RES_USE_INET6) {		/* XXX */
66 		hp = gethostbyname2(name, AF_INET6);	/* XXX */
67 		if (hp)					/* XXX */
68 			return (hp);			/* XXX */
69 	}						/* XXX */
70 	return (gethostbyname2(name, AF_INET));
71 }
72 
73 struct hostent *
74 gethostbyname2(const char *name, int type)
75 {
76 	struct hostent *hp = 0;
77 	int rval;
78 
79 	static const ns_dtab dtab[] = {
80 		NS_FILES_CB(_ht_gethostbyname, NULL)
81 		{ NSSRC_DNS, _dns_gethostbyname, NULL },
82 		NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
83 		{ 0 }
84 	};
85 
86 	rval = nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyname",
87 			  default_src, name, type);
88 
89 	if (rval != NS_SUCCESS)
90 		return NULL;
91 	else
92 		return hp;
93 }
94 
95 struct hostent *
96 gethostbyaddr(const char *addr, int len, int type)
97 {
98 	struct hostent *hp = 0;
99 	int rval;
100 
101 	static const ns_dtab dtab[] = {
102 		NS_FILES_CB(_ht_gethostbyaddr, NULL)
103 		{ NSSRC_DNS, _dns_gethostbyaddr, NULL },
104 		NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
105 		{ 0 }
106 	};
107 
108 	rval = nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
109 			  default_src, addr, len, type);
110 
111 	if (rval != NS_SUCCESS)
112 		return NULL;
113 	else
114 		return hp;
115 }
116 
117 struct hostent_data;
118 
119 /*
120  * Temporary function (not thread safe)
121  */
122 int 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 
136 void
137 sethostent(stayopen)
138 	int stayopen;
139 {
140 	_sethosthtent(stayopen);
141 	_sethostdnsent(stayopen);
142 }
143 
144 void
145 endhostent()
146 {
147 	_endhosthtent();
148 	_endhostdnsent();
149 }
150