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