etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / lib / bind9 / getaddresses.c
blob39aef434dad57034becc4a2c3239ee20b521c501
1 /* $NetBSD: getaddresses.c,v 1.6 2015/07/08 17:28:58 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001, 2002 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: getaddresses.c,v 1.22 2007/06/19 23:47:16 tbox Exp */
22 /*! \file */
24 #include <config.h>
25 #include <string.h>
27 #include <isc/net.h>
28 #include <isc/netaddr.h>
29 #include <isc/netdb.h>
30 #include <isc/netscope.h>
31 #include <isc/result.h>
32 #include <isc/sockaddr.h>
33 #include <isc/util.h>
35 #include <bind9/getaddresses.h>
37 #ifdef HAVE_ADDRINFO
38 #ifdef HAVE_GETADDRINFO
39 #ifdef HAVE_GAISTRERROR
40 #define USE_GETADDRINFO
41 #endif
42 #endif
43 #endif
45 #ifndef USE_GETADDRINFO
46 #ifndef ISC_PLATFORM_NONSTDHERRNO
47 extern int h_errno;
48 #endif
49 #endif
51 isc_result_t
52 bind9_getaddresses(const char *hostname, in_port_t port,
53 isc_sockaddr_t *addrs, int addrsize, int *addrcount)
55 struct in_addr in4;
56 struct in6_addr in6;
57 isc_boolean_t have_ipv4, have_ipv6;
58 int i;
60 #ifdef USE_GETADDRINFO
61 struct addrinfo *ai = NULL, *tmpai, hints;
62 int result;
63 #else
64 struct hostent *he;
65 #endif
67 REQUIRE(hostname != NULL);
68 REQUIRE(addrs != NULL);
69 REQUIRE(addrcount != NULL);
70 REQUIRE(addrsize > 0);
72 have_ipv4 = ISC_TF((isc_net_probeipv4() == ISC_R_SUCCESS));
73 have_ipv6 = ISC_TF((isc_net_probeipv6() == ISC_R_SUCCESS));
76 * Try IPv4, then IPv6. In order to handle the extended format
77 * for IPv6 scoped addresses (address%scope_ID), we'll use a local
78 * working buffer of 128 bytes. The length is an ad-hoc value, but
79 * should be enough for this purpose; the buffer can contain a string
80 * of at least 80 bytes for scope_ID in addition to any IPv6 numeric
81 * addresses (up to 46 bytes), the delimiter character and the
82 * terminating NULL character.
84 if (inet_pton(AF_INET, hostname, &in4) == 1) {
85 if (have_ipv4)
86 isc_sockaddr_fromin(&addrs[0], &in4, port);
87 else
88 isc_sockaddr_v6fromin(&addrs[0], &in4, port);
89 *addrcount = 1;
90 return (ISC_R_SUCCESS);
91 } else if (strlen(hostname) <= 127U) {
92 char tmpbuf[128], *d;
93 isc_uint32_t zone = 0;
95 strcpy(tmpbuf, hostname);
96 d = strchr(tmpbuf, '%');
97 if (d != NULL)
98 *d = '\0';
100 if (inet_pton(AF_INET6, tmpbuf, &in6) == 1) {
101 isc_netaddr_t na;
103 if (!have_ipv6)
104 return (ISC_R_FAMILYNOSUPPORT);
106 if (d != NULL) {
107 #ifdef ISC_PLATFORM_HAVESCOPEID
108 isc_result_t iresult;
110 iresult = isc_netscope_pton(AF_INET6, d + 1,
111 &in6, &zone);
113 if (iresult != ISC_R_SUCCESS)
114 return (iresult);
115 #else
117 * The extended format is specified while the
118 * system does not provide the ability to use
119 * it. Throw an explicit error instead of
120 * ignoring the specified value.
122 return (ISC_R_BADADDRESSFORM);
123 #endif
126 isc_netaddr_fromin6(&na, &in6);
127 isc_netaddr_setzone(&na, zone);
128 isc_sockaddr_fromnetaddr(&addrs[0],
129 (const isc_netaddr_t *)&na,
130 port);
132 *addrcount = 1;
133 return (ISC_R_SUCCESS);
136 #ifdef USE_GETADDRINFO
137 memset(&hints, 0, sizeof(hints));
138 if (!have_ipv6)
139 hints.ai_family = PF_INET;
140 else if (!have_ipv4)
141 hints.ai_family = PF_INET6;
142 else {
143 hints.ai_family = PF_UNSPEC;
144 #ifdef AI_ADDRCONFIG
145 hints.ai_flags = AI_ADDRCONFIG;
146 #endif
148 hints.ai_socktype = SOCK_STREAM;
149 #ifdef AI_ADDRCONFIG
150 again:
151 #endif
152 result = getaddrinfo(hostname, NULL, &hints, &ai);
153 switch (result) {
154 case 0:
155 break;
156 case EAI_NONAME:
157 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
158 case EAI_NODATA:
159 #endif
160 return (ISC_R_NOTFOUND);
161 #ifdef AI_ADDRCONFIG
162 case EAI_BADFLAGS:
163 if ((hints.ai_flags & AI_ADDRCONFIG) != 0) {
164 hints.ai_flags &= ~AI_ADDRCONFIG;
165 goto again;
167 #endif
168 default:
169 return (ISC_R_FAILURE);
171 for (tmpai = ai, i = 0;
172 tmpai != NULL && i < addrsize;
173 tmpai = tmpai->ai_next)
175 if (tmpai->ai_family != AF_INET &&
176 tmpai->ai_family != AF_INET6)
177 continue;
178 if (tmpai->ai_family == AF_INET) {
179 struct sockaddr_in *sin;
180 sin = (struct sockaddr_in *)tmpai->ai_addr;
181 isc_sockaddr_fromin(&addrs[i], &sin->sin_addr, port);
182 } else {
183 struct sockaddr_in6 *sin6;
184 sin6 = (struct sockaddr_in6 *)tmpai->ai_addr;
185 isc_sockaddr_fromin6(&addrs[i], &sin6->sin6_addr,
186 port);
188 i++;
191 freeaddrinfo(ai);
192 *addrcount = i;
193 #else
194 he = gethostbyname(hostname);
195 if (he == NULL) {
196 switch (h_errno) {
197 case HOST_NOT_FOUND:
198 #ifdef NO_DATA
199 case NO_DATA:
200 #endif
201 #if defined(NO_ADDRESS) && (!defined(NO_DATA) || (NO_DATA != NO_ADDRESS))
202 case NO_ADDRESS:
203 #endif
204 return (ISC_R_NOTFOUND);
205 default:
206 return (ISC_R_FAILURE);
209 if (he->h_addrtype != AF_INET && he->h_addrtype != AF_INET6)
210 return (ISC_R_NOTFOUND);
211 for (i = 0; i < addrsize; i++) {
212 if (he->h_addrtype == AF_INET) {
213 struct in_addr *inp;
214 inp = (struct in_addr *)(he->h_addr_list[i]);
215 if (inp == NULL)
216 break;
217 isc_sockaddr_fromin(&addrs[i], inp, port);
218 } else {
219 struct in6_addr *in6p;
220 in6p = (struct in6_addr *)(he->h_addr_list[i]);
221 if (in6p == NULL)
222 break;
223 isc_sockaddr_fromin6(&addrs[i], in6p, port);
226 *addrcount = i;
227 #endif
228 if (*addrcount == 0)
229 return (ISC_R_NOTFOUND);
230 else
231 return (ISC_R_SUCCESS);