Deleted Added
full compact
name6.c (233770) name6.c (245225)
1/* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31/*
32 * ++Copyright++ 1985, 1988, 1993
33 * -
34 * Copyright (c) 1985, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 * -
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
66 *
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies, and that
70 * the name of Digital Equipment Corporation not be used in advertising or
71 * publicity pertaining to distribution of the document or software without
72 * specific, written prior permission.
73 *
74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 * SOFTWARE.
82 * -
83 * --Copyright--
84 */
85
86/*
87 * Atsushi Onoe <onoe@sm.sony.co.jp>
88 */
89
90#include <sys/cdefs.h>
1/* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31/*
32 * ++Copyright++ 1985, 1988, 1993
33 * -
34 * Copyright (c) 1985, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 * -
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
66 *
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies, and that
70 * the name of Digital Equipment Corporation not be used in advertising or
71 * publicity pertaining to distribution of the document or software without
72 * specific, written prior permission.
73 *
74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 * SOFTWARE.
82 * -
83 * --Copyright--
84 */
85
86/*
87 * Atsushi Onoe <onoe@sm.sony.co.jp>
88 */
89
90#include <sys/cdefs.h>
91__FBSDID("$FreeBSD: head/lib/libc/net/name6.c 233770 2012-04-02 07:42:17Z delphij $");
91__FBSDID("$FreeBSD: head/lib/libc/net/name6.c 245225 2013-01-09 15:22:37Z ume $");
92
93#include "namespace.h"
94#include <sys/param.h>
95#include <sys/socket.h>
96#include <sys/time.h>
97#include <sys/queue.h>
98#include <netinet/in.h>
99#ifdef INET6
100#include <net/if.h>
101#include <net/if_var.h>
102#include <sys/sysctl.h>
103#include <sys/ioctl.h>
104#include <netinet6/in6_var.h> /* XXX */
105#endif
106
107#include <arpa/inet.h>
108#include <arpa/nameser.h>
109
110#include <errno.h>
111#include <netdb.h>
112#include <resolv.h>
113#include <stdio.h>
114#include <stdlib.h>
115#include <string.h>
116#include <stdarg.h>
117#include <nsswitch.h>
118#include <unistd.h>
119#include "un-namespace.h"
120#include "netdb_private.h"
121#include "res_private.h"
122
123#ifndef MAXALIASES
124#define MAXALIASES 10
125#endif
126#ifndef MAXADDRS
127#define MAXADDRS 20
128#endif
129#ifndef MAXDNAME
130#define MAXDNAME 1025
131#endif
132
133#ifdef INET6
134#define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
135 sizeof(struct in_addr))
136#else
137#define ADDRLEN(af) sizeof(struct in_addr)
138#endif
139
140#define MAPADDR(ab, ina) \
141do { \
142 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
143 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
144 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
145} while (0)
146#define MAPADDRENABLED(flags) \
147 (((flags) & AI_V4MAPPED) || \
148 (((flags) & AI_V4MAPPED_CFG)))
149
150union inx_addr {
151 struct in_addr in_addr;
152#ifdef INET6
153 struct in6_addr in6_addr;
154#endif
155 struct {
156 u_char mau_zero[10];
157 u_char mau_one[2];
158 struct in_addr mau_inaddr;
159 } map_addr_un;
160#define map_zero map_addr_un.mau_zero
161#define map_one map_addr_un.mau_one
162#define map_inaddr map_addr_un.mau_inaddr
163};
164
165struct policyqueue {
166 TAILQ_ENTRY(policyqueue) pc_entry;
167#ifdef INET6
168 struct in6_addrpolicy pc_policy;
169#endif
170};
171TAILQ_HEAD(policyhead, policyqueue);
172
173#define AIO_SRCFLAG_DEPRECATED 0x1
174
175struct hp_order {
176 union {
177 struct sockaddr_storage aiou_ss;
178 struct sockaddr aiou_sa;
179 } aio_src_un;
180#define aio_srcsa aio_src_un.aiou_sa
181 u_int32_t aio_srcflag;
182 int aio_srcscope;
183 int aio_dstscope;
184 struct policyqueue *aio_srcpolicy;
185 struct policyqueue *aio_dstpolicy;
186 union {
187 struct sockaddr_storage aiou_ss;
188 struct sockaddr aiou_sa;
189 } aio_un;
190#define aio_sa aio_un.aiou_sa
191 int aio_matchlen;
192 char *aio_h_addr;
193};
194
195static struct hostent *_hpcopy(struct hostent *, int *);
196static struct hostent *_hpaddr(int, const char *, void *, int *);
197#ifdef INET6
198static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *);
199static struct hostent *_hpmapv6(struct hostent *, int *);
200#endif
201static struct hostent *_hpsort(struct hostent *, res_state);
202
92
93#include "namespace.h"
94#include <sys/param.h>
95#include <sys/socket.h>
96#include <sys/time.h>
97#include <sys/queue.h>
98#include <netinet/in.h>
99#ifdef INET6
100#include <net/if.h>
101#include <net/if_var.h>
102#include <sys/sysctl.h>
103#include <sys/ioctl.h>
104#include <netinet6/in6_var.h> /* XXX */
105#endif
106
107#include <arpa/inet.h>
108#include <arpa/nameser.h>
109
110#include <errno.h>
111#include <netdb.h>
112#include <resolv.h>
113#include <stdio.h>
114#include <stdlib.h>
115#include <string.h>
116#include <stdarg.h>
117#include <nsswitch.h>
118#include <unistd.h>
119#include "un-namespace.h"
120#include "netdb_private.h"
121#include "res_private.h"
122
123#ifndef MAXALIASES
124#define MAXALIASES 10
125#endif
126#ifndef MAXADDRS
127#define MAXADDRS 20
128#endif
129#ifndef MAXDNAME
130#define MAXDNAME 1025
131#endif
132
133#ifdef INET6
134#define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
135 sizeof(struct in_addr))
136#else
137#define ADDRLEN(af) sizeof(struct in_addr)
138#endif
139
140#define MAPADDR(ab, ina) \
141do { \
142 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \
143 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \
144 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \
145} while (0)
146#define MAPADDRENABLED(flags) \
147 (((flags) & AI_V4MAPPED) || \
148 (((flags) & AI_V4MAPPED_CFG)))
149
150union inx_addr {
151 struct in_addr in_addr;
152#ifdef INET6
153 struct in6_addr in6_addr;
154#endif
155 struct {
156 u_char mau_zero[10];
157 u_char mau_one[2];
158 struct in_addr mau_inaddr;
159 } map_addr_un;
160#define map_zero map_addr_un.mau_zero
161#define map_one map_addr_un.mau_one
162#define map_inaddr map_addr_un.mau_inaddr
163};
164
165struct policyqueue {
166 TAILQ_ENTRY(policyqueue) pc_entry;
167#ifdef INET6
168 struct in6_addrpolicy pc_policy;
169#endif
170};
171TAILQ_HEAD(policyhead, policyqueue);
172
173#define AIO_SRCFLAG_DEPRECATED 0x1
174
175struct hp_order {
176 union {
177 struct sockaddr_storage aiou_ss;
178 struct sockaddr aiou_sa;
179 } aio_src_un;
180#define aio_srcsa aio_src_un.aiou_sa
181 u_int32_t aio_srcflag;
182 int aio_srcscope;
183 int aio_dstscope;
184 struct policyqueue *aio_srcpolicy;
185 struct policyqueue *aio_dstpolicy;
186 union {
187 struct sockaddr_storage aiou_ss;
188 struct sockaddr aiou_sa;
189 } aio_un;
190#define aio_sa aio_un.aiou_sa
191 int aio_matchlen;
192 char *aio_h_addr;
193};
194
195static struct hostent *_hpcopy(struct hostent *, int *);
196static struct hostent *_hpaddr(int, const char *, void *, int *);
197#ifdef INET6
198static struct hostent *_hpmerge(struct hostent *, struct hostent *, int *);
199static struct hostent *_hpmapv6(struct hostent *, int *);
200#endif
201static struct hostent *_hpsort(struct hostent *, res_state);
202
203#ifdef ENABLE_IP6ADDRCTL
203static struct hostent *_hpreorder(struct hostent *);
204static int get_addrselectpolicy(struct policyhead *);
205static void free_addrselectpolicy(struct policyhead *);
206static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
207 struct policyhead *);
208static void set_source(struct hp_order *, struct policyhead *);
209static int matchlen(struct sockaddr *, struct sockaddr *);
210static int comp_dst(const void *, const void *);
211static int gai_addr2scopetype(struct sockaddr *);
204static struct hostent *_hpreorder(struct hostent *);
205static int get_addrselectpolicy(struct policyhead *);
206static void free_addrselectpolicy(struct policyhead *);
207static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
208 struct policyhead *);
209static void set_source(struct hp_order *, struct policyhead *);
210static int matchlen(struct sockaddr *, struct sockaddr *);
211static int comp_dst(const void *, const void *);
212static int gai_addr2scopetype(struct sockaddr *);
213#endif
212
213/*
214 * Functions defined in RFC2553
215 * getipnodebyname, getipnodebyaddr, freehostent
216 */
217
218struct hostent *
219getipnodebyname(const char *name, int af, int flags, int *errp)
220{
221 struct hostent *hp;
222 union inx_addr addrbuf;
223 res_state statp;
224 u_long options;
225
226 switch (af) {
227 case AF_INET:
228#ifdef INET6
229 case AF_INET6:
230#endif
231 break;
232 default:
233 *errp = NO_RECOVERY;
234 return NULL;
235 }
236
237 if (flags & AI_ADDRCONFIG) {
238 int s;
239
240 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
241 return NULL;
242 /*
243 * TODO:
244 * Note that implementation dependent test for address
245 * configuration should be done everytime called
246 * (or apropriate interval),
247 * because addresses will be dynamically assigned or deleted.
248 */
249 _close(s);
250 }
251
252#ifdef INET6
253 /* special case for literal address */
254 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
255 if (af != AF_INET6) {
256 *errp = HOST_NOT_FOUND;
257 return NULL;
258 }
259 return _hpaddr(af, name, &addrbuf, errp);
260 }
261#endif
262 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
263 if (af != AF_INET) {
264 if (MAPADDRENABLED(flags)) {
265 MAPADDR(&addrbuf, &addrbuf.in_addr);
266 } else {
267 *errp = HOST_NOT_FOUND;
268 return NULL;
269 }
270 }
271 return _hpaddr(af, name, &addrbuf, errp);
272 }
273
274
275 statp = __res_state();
276 if ((statp->options & RES_INIT) == 0) {
277 if (res_ninit(statp) < 0) {
278 *errp = NETDB_INTERNAL;
279 return NULL;
280 }
281 }
282
283 options = statp->options;
284 statp->options &= ~RES_USE_INET6;
285
286 hp = gethostbyname2(name, af);
287 hp = _hpcopy(hp, errp);
288
289#ifdef INET6
290 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
291 MAPADDRENABLED(flags)) {
292 struct hostent *hp2 = gethostbyname2(name, AF_INET);
293 if (hp == NULL)
294 if (hp2 == NULL)
295 *errp = statp->res_h_errno;
296 else
297 hp = _hpmapv6(hp2, errp);
298 else {
299 if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
300 struct hostent *hpb = hp;
301 hp = _hpmerge(hpb, hp2, errp);
302 freehostent(hpb);
303 }
304 }
305 }
306#endif
307
308 if (hp == NULL)
309 *errp = statp->res_h_errno;
310
311 statp->options = options;
214
215/*
216 * Functions defined in RFC2553
217 * getipnodebyname, getipnodebyaddr, freehostent
218 */
219
220struct hostent *
221getipnodebyname(const char *name, int af, int flags, int *errp)
222{
223 struct hostent *hp;
224 union inx_addr addrbuf;
225 res_state statp;
226 u_long options;
227
228 switch (af) {
229 case AF_INET:
230#ifdef INET6
231 case AF_INET6:
232#endif
233 break;
234 default:
235 *errp = NO_RECOVERY;
236 return NULL;
237 }
238
239 if (flags & AI_ADDRCONFIG) {
240 int s;
241
242 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
243 return NULL;
244 /*
245 * TODO:
246 * Note that implementation dependent test for address
247 * configuration should be done everytime called
248 * (or apropriate interval),
249 * because addresses will be dynamically assigned or deleted.
250 */
251 _close(s);
252 }
253
254#ifdef INET6
255 /* special case for literal address */
256 if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
257 if (af != AF_INET6) {
258 *errp = HOST_NOT_FOUND;
259 return NULL;
260 }
261 return _hpaddr(af, name, &addrbuf, errp);
262 }
263#endif
264 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
265 if (af != AF_INET) {
266 if (MAPADDRENABLED(flags)) {
267 MAPADDR(&addrbuf, &addrbuf.in_addr);
268 } else {
269 *errp = HOST_NOT_FOUND;
270 return NULL;
271 }
272 }
273 return _hpaddr(af, name, &addrbuf, errp);
274 }
275
276
277 statp = __res_state();
278 if ((statp->options & RES_INIT) == 0) {
279 if (res_ninit(statp) < 0) {
280 *errp = NETDB_INTERNAL;
281 return NULL;
282 }
283 }
284
285 options = statp->options;
286 statp->options &= ~RES_USE_INET6;
287
288 hp = gethostbyname2(name, af);
289 hp = _hpcopy(hp, errp);
290
291#ifdef INET6
292 if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
293 MAPADDRENABLED(flags)) {
294 struct hostent *hp2 = gethostbyname2(name, AF_INET);
295 if (hp == NULL)
296 if (hp2 == NULL)
297 *errp = statp->res_h_errno;
298 else
299 hp = _hpmapv6(hp2, errp);
300 else {
301 if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
302 struct hostent *hpb = hp;
303 hp = _hpmerge(hpb, hp2, errp);
304 freehostent(hpb);
305 }
306 }
307 }
308#endif
309
310 if (hp == NULL)
311 *errp = statp->res_h_errno;
312
313 statp->options = options;
314#ifdef ENABLE_IP6ADDRCTL
312 return _hpreorder(_hpsort(hp, statp));
315 return _hpreorder(_hpsort(hp, statp));
316#else
317 return _hpsort(hp, statp);
318#endif
313}
314
315struct hostent *
316getipnodebyaddr(const void *src, size_t len, int af, int *errp)
317{
318 struct hostent *hp;
319 res_state statp;
320 u_long options;
321
322#ifdef INET6
323 struct in6_addr addrbuf;
324#else
325 struct in_addr addrbuf;
326#endif
327
328 switch (af) {
329 case AF_INET:
330 if (len != sizeof(struct in_addr)) {
331 *errp = NO_RECOVERY;
332 return NULL;
333 }
334 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
335 memcpy(&addrbuf, src, len);
336 src = &addrbuf;
337 }
338 if (((struct in_addr *)src)->s_addr == 0)
339 return NULL;
340 break;
341#ifdef INET6
342 case AF_INET6:
343 if (len != sizeof(struct in6_addr)) {
344 *errp = NO_RECOVERY;
345 return NULL;
346 }
347 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
348 memcpy(&addrbuf, src, len);
349 src = &addrbuf;
350 }
351 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
352 return NULL;
353 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
354 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
355 src = (char *)src +
356 (sizeof(struct in6_addr) - sizeof(struct in_addr));
357 af = AF_INET;
358 len = sizeof(struct in_addr);
359 }
360 break;
361#endif
362 default:
363 *errp = NO_RECOVERY;
364 return NULL;
365 }
366
367 statp = __res_state();
368 if ((statp->options & RES_INIT) == 0) {
369 if (res_ninit(statp) < 0) {
370 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
371 return NULL;
372 }
373 }
374
375 options = statp->options;
376 statp->options &= ~RES_USE_INET6;
377
378 hp = gethostbyaddr(src, len, af);
379 if (hp == NULL)
380 *errp = statp->res_h_errno;
381
382 statp->options = options;
383 return (_hpcopy(hp, errp));
384}
385
386void
387freehostent(struct hostent *ptr)
388{
389 free(ptr);
390}
391
392/*
393 * Private utility functions
394 */
395
396/*
397 * _hpcopy: allocate and copy hostent structure
398 */
399static struct hostent *
400_hpcopy(struct hostent *hp, int *errp)
401{
402 struct hostent *nhp;
403 char *cp, **pp;
404 int size, addrsize;
405 int nalias = 0, naddr = 0;
406 int al_off;
407 int i;
408
409 if (hp == NULL)
410 return hp;
411
412 /* count size to be allocated */
413 size = sizeof(struct hostent);
414 if (hp->h_name != NULL)
415 size += strlen(hp->h_name) + 1;
416 if ((pp = hp->h_aliases) != NULL) {
417 for (i = 0; *pp != NULL; i++, pp++) {
418 if (**pp != '\0') {
419 size += strlen(*pp) + 1;
420 nalias++;
421 }
422 }
423 }
424 /* adjust alignment */
425 size = ALIGN(size);
426 al_off = size;
427 size += sizeof(char *) * (nalias + 1);
428 addrsize = ALIGN(hp->h_length);
429 if ((pp = hp->h_addr_list) != NULL) {
430 while (*pp++ != NULL)
431 naddr++;
432 }
433 size += addrsize * naddr;
434 size += sizeof(char *) * (naddr + 1);
435
436 /* copy */
437 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
438 *errp = TRY_AGAIN;
439 return NULL;
440 }
441 cp = (char *)&nhp[1];
442 if (hp->h_name != NULL) {
443 nhp->h_name = cp;
444 strcpy(cp, hp->h_name);
445 cp += strlen(cp) + 1;
446 } else
447 nhp->h_name = NULL;
448 nhp->h_aliases = (char **)((char *)nhp + al_off);
449 if ((pp = hp->h_aliases) != NULL) {
450 for (i = 0; *pp != NULL; pp++) {
451 if (**pp != '\0') {
452 nhp->h_aliases[i++] = cp;
453 strcpy(cp, *pp);
454 cp += strlen(cp) + 1;
455 }
456 }
457 }
458 nhp->h_aliases[nalias] = NULL;
459 cp = (char *)&nhp->h_aliases[nalias + 1];
460 nhp->h_addrtype = hp->h_addrtype;
461 nhp->h_length = hp->h_length;
462 nhp->h_addr_list = (char **)cp;
463 if ((pp = hp->h_addr_list) != NULL) {
464 cp = (char *)&nhp->h_addr_list[naddr + 1];
465 for (i = 0; *pp != NULL; pp++) {
466 nhp->h_addr_list[i++] = cp;
467 memcpy(cp, *pp, hp->h_length);
468 cp += addrsize;
469 }
470 }
471 nhp->h_addr_list[naddr] = NULL;
472 return nhp;
473}
474
475/*
476 * _hpaddr: construct hostent structure with one address
477 */
478static struct hostent *
479_hpaddr(int af, const char *name, void *addr, int *errp)
480{
481 struct hostent *hp, hpbuf;
482 char *addrs[2];
483
484 hp = &hpbuf;
485 hp->h_name = (char *)name;
486 hp->h_aliases = NULL;
487 hp->h_addrtype = af;
488 hp->h_length = ADDRLEN(af);
489 hp->h_addr_list = addrs;
490 addrs[0] = (char *)addr;
491 addrs[1] = NULL;
492 return (_hpcopy(hp, errp));
493}
494
495#ifdef INET6
496/*
497 * _hpmerge: merge 2 hostent structure, arguments will be freed
498 */
499static struct hostent *
500_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
501{
502 int i, j;
503 int naddr, nalias;
504 char **pp;
505 struct hostent *hp, hpbuf;
506 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
507 union inx_addr addrbuf[MAXADDRS];
508
509 if (hp1 == NULL)
510 return _hpcopy(hp2, errp);
511 if (hp2 == NULL)
512 return _hpcopy(hp1, errp);
513
514#define HP(i) (i == 1 ? hp1 : hp2)
515 hp = &hpbuf;
516 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
517 hp->h_aliases = aliases;
518 nalias = 0;
519 for (i = 1; i <= 2; i++) {
520 if ((pp = HP(i)->h_aliases) == NULL)
521 continue;
522 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
523 /* check duplicates */
524 for (j = 0; j < nalias; j++)
525 if (strcasecmp(*pp, aliases[j]) == 0)
526 break;
527 if (j == nalias)
528 aliases[nalias++] = *pp;
529 }
530 }
531 aliases[nalias] = NULL;
532 if (hp1->h_length != hp2->h_length) {
533 hp->h_addrtype = AF_INET6;
534 hp->h_length = sizeof(struct in6_addr);
535 } else {
536 hp->h_addrtype = hp1->h_addrtype;
537 hp->h_length = hp1->h_length;
538 }
539
540 hp->h_addr_list = addrs;
541 naddr = 0;
542 for (i = 1; i <= 2; i++) {
543 if ((pp = HP(i)->h_addr_list) == NULL)
544 continue;
545 if (HP(i)->h_length == hp->h_length) {
546 while (naddr < MAXADDRS && *pp != NULL)
547 addrs[naddr++] = *pp++;
548 } else {
549 /* copy IPv4 addr as mapped IPv6 addr */
550 while (naddr < MAXADDRS && *pp != NULL) {
551 MAPADDR(&addrbuf[naddr], *pp++);
552 addrs[naddr] = (char *)&addrbuf[naddr];
553 naddr++;
554 }
555 }
556 }
557 addrs[naddr] = NULL;
558 return (_hpcopy(hp, errp));
559}
560#endif
561
562/*
563 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
564 */
565#ifdef INET6
566static struct hostent *
567_hpmapv6(struct hostent *hp, int *errp)
568{
569 struct hostent hp6;
570
571 if (hp == NULL)
572 return NULL;
573 if (hp->h_addrtype == AF_INET6)
574 return _hpcopy(hp, errp);
575
576 memset(&hp6, 0, sizeof(struct hostent));
577 hp6.h_addrtype = AF_INET6;
578 hp6.h_length = sizeof(struct in6_addr);
579 return _hpmerge(&hp6, hp, errp);
580}
581#endif
582
583/*
584 * _hpsort: sort address by sortlist
585 */
586static struct hostent *
587_hpsort(struct hostent *hp, res_state statp)
588{
589 int i, j, n;
590 u_char *ap, *sp, *mp, **pp;
591 char t;
592 char order[MAXADDRS];
593 int nsort = statp->nsort;
594
595 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
596 return hp;
597 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
598 for (j = 0; j < nsort; j++) {
599#ifdef INET6
600 if (statp->_u._ext.ext->sort_list[j].af !=
601 hp->h_addrtype)
602 continue;
603 sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
604 mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
605#else
606 sp = (u_char *)&statp->sort_list[j].addr;
607 mp = (u_char *)&statp->sort_list[j].mask;
608#endif
609 for (n = 0; n < hp->h_length; n++) {
610 if ((ap[n] & mp[n]) != sp[n])
611 break;
612 }
613 if (n == hp->h_length)
614 break;
615 }
616 order[i] = j;
617 }
618 n = i;
619 pp = (u_char **)hp->h_addr_list;
620 for (i = 0; i < n - 1; i++) {
621 for (j = i + 1; j < n; j++) {
622 if (order[i] > order[j]) {
623 ap = pp[i];
624 pp[i] = pp[j];
625 pp[j] = ap;
626 t = order[i];
627 order[i] = order[j];
628 order[j] = t;
629 }
630 }
631 }
632 return hp;
633}
634
319}
320
321struct hostent *
322getipnodebyaddr(const void *src, size_t len, int af, int *errp)
323{
324 struct hostent *hp;
325 res_state statp;
326 u_long options;
327
328#ifdef INET6
329 struct in6_addr addrbuf;
330#else
331 struct in_addr addrbuf;
332#endif
333
334 switch (af) {
335 case AF_INET:
336 if (len != sizeof(struct in_addr)) {
337 *errp = NO_RECOVERY;
338 return NULL;
339 }
340 if ((long)src & ~(sizeof(struct in_addr) - 1)) {
341 memcpy(&addrbuf, src, len);
342 src = &addrbuf;
343 }
344 if (((struct in_addr *)src)->s_addr == 0)
345 return NULL;
346 break;
347#ifdef INET6
348 case AF_INET6:
349 if (len != sizeof(struct in6_addr)) {
350 *errp = NO_RECOVERY;
351 return NULL;
352 }
353 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/
354 memcpy(&addrbuf, src, len);
355 src = &addrbuf;
356 }
357 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
358 return NULL;
359 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
360 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
361 src = (char *)src +
362 (sizeof(struct in6_addr) - sizeof(struct in_addr));
363 af = AF_INET;
364 len = sizeof(struct in_addr);
365 }
366 break;
367#endif
368 default:
369 *errp = NO_RECOVERY;
370 return NULL;
371 }
372
373 statp = __res_state();
374 if ((statp->options & RES_INIT) == 0) {
375 if (res_ninit(statp) < 0) {
376 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
377 return NULL;
378 }
379 }
380
381 options = statp->options;
382 statp->options &= ~RES_USE_INET6;
383
384 hp = gethostbyaddr(src, len, af);
385 if (hp == NULL)
386 *errp = statp->res_h_errno;
387
388 statp->options = options;
389 return (_hpcopy(hp, errp));
390}
391
392void
393freehostent(struct hostent *ptr)
394{
395 free(ptr);
396}
397
398/*
399 * Private utility functions
400 */
401
402/*
403 * _hpcopy: allocate and copy hostent structure
404 */
405static struct hostent *
406_hpcopy(struct hostent *hp, int *errp)
407{
408 struct hostent *nhp;
409 char *cp, **pp;
410 int size, addrsize;
411 int nalias = 0, naddr = 0;
412 int al_off;
413 int i;
414
415 if (hp == NULL)
416 return hp;
417
418 /* count size to be allocated */
419 size = sizeof(struct hostent);
420 if (hp->h_name != NULL)
421 size += strlen(hp->h_name) + 1;
422 if ((pp = hp->h_aliases) != NULL) {
423 for (i = 0; *pp != NULL; i++, pp++) {
424 if (**pp != '\0') {
425 size += strlen(*pp) + 1;
426 nalias++;
427 }
428 }
429 }
430 /* adjust alignment */
431 size = ALIGN(size);
432 al_off = size;
433 size += sizeof(char *) * (nalias + 1);
434 addrsize = ALIGN(hp->h_length);
435 if ((pp = hp->h_addr_list) != NULL) {
436 while (*pp++ != NULL)
437 naddr++;
438 }
439 size += addrsize * naddr;
440 size += sizeof(char *) * (naddr + 1);
441
442 /* copy */
443 if ((nhp = (struct hostent *)malloc(size)) == NULL) {
444 *errp = TRY_AGAIN;
445 return NULL;
446 }
447 cp = (char *)&nhp[1];
448 if (hp->h_name != NULL) {
449 nhp->h_name = cp;
450 strcpy(cp, hp->h_name);
451 cp += strlen(cp) + 1;
452 } else
453 nhp->h_name = NULL;
454 nhp->h_aliases = (char **)((char *)nhp + al_off);
455 if ((pp = hp->h_aliases) != NULL) {
456 for (i = 0; *pp != NULL; pp++) {
457 if (**pp != '\0') {
458 nhp->h_aliases[i++] = cp;
459 strcpy(cp, *pp);
460 cp += strlen(cp) + 1;
461 }
462 }
463 }
464 nhp->h_aliases[nalias] = NULL;
465 cp = (char *)&nhp->h_aliases[nalias + 1];
466 nhp->h_addrtype = hp->h_addrtype;
467 nhp->h_length = hp->h_length;
468 nhp->h_addr_list = (char **)cp;
469 if ((pp = hp->h_addr_list) != NULL) {
470 cp = (char *)&nhp->h_addr_list[naddr + 1];
471 for (i = 0; *pp != NULL; pp++) {
472 nhp->h_addr_list[i++] = cp;
473 memcpy(cp, *pp, hp->h_length);
474 cp += addrsize;
475 }
476 }
477 nhp->h_addr_list[naddr] = NULL;
478 return nhp;
479}
480
481/*
482 * _hpaddr: construct hostent structure with one address
483 */
484static struct hostent *
485_hpaddr(int af, const char *name, void *addr, int *errp)
486{
487 struct hostent *hp, hpbuf;
488 char *addrs[2];
489
490 hp = &hpbuf;
491 hp->h_name = (char *)name;
492 hp->h_aliases = NULL;
493 hp->h_addrtype = af;
494 hp->h_length = ADDRLEN(af);
495 hp->h_addr_list = addrs;
496 addrs[0] = (char *)addr;
497 addrs[1] = NULL;
498 return (_hpcopy(hp, errp));
499}
500
501#ifdef INET6
502/*
503 * _hpmerge: merge 2 hostent structure, arguments will be freed
504 */
505static struct hostent *
506_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
507{
508 int i, j;
509 int naddr, nalias;
510 char **pp;
511 struct hostent *hp, hpbuf;
512 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
513 union inx_addr addrbuf[MAXADDRS];
514
515 if (hp1 == NULL)
516 return _hpcopy(hp2, errp);
517 if (hp2 == NULL)
518 return _hpcopy(hp1, errp);
519
520#define HP(i) (i == 1 ? hp1 : hp2)
521 hp = &hpbuf;
522 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
523 hp->h_aliases = aliases;
524 nalias = 0;
525 for (i = 1; i <= 2; i++) {
526 if ((pp = HP(i)->h_aliases) == NULL)
527 continue;
528 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
529 /* check duplicates */
530 for (j = 0; j < nalias; j++)
531 if (strcasecmp(*pp, aliases[j]) == 0)
532 break;
533 if (j == nalias)
534 aliases[nalias++] = *pp;
535 }
536 }
537 aliases[nalias] = NULL;
538 if (hp1->h_length != hp2->h_length) {
539 hp->h_addrtype = AF_INET6;
540 hp->h_length = sizeof(struct in6_addr);
541 } else {
542 hp->h_addrtype = hp1->h_addrtype;
543 hp->h_length = hp1->h_length;
544 }
545
546 hp->h_addr_list = addrs;
547 naddr = 0;
548 for (i = 1; i <= 2; i++) {
549 if ((pp = HP(i)->h_addr_list) == NULL)
550 continue;
551 if (HP(i)->h_length == hp->h_length) {
552 while (naddr < MAXADDRS && *pp != NULL)
553 addrs[naddr++] = *pp++;
554 } else {
555 /* copy IPv4 addr as mapped IPv6 addr */
556 while (naddr < MAXADDRS && *pp != NULL) {
557 MAPADDR(&addrbuf[naddr], *pp++);
558 addrs[naddr] = (char *)&addrbuf[naddr];
559 naddr++;
560 }
561 }
562 }
563 addrs[naddr] = NULL;
564 return (_hpcopy(hp, errp));
565}
566#endif
567
568/*
569 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
570 */
571#ifdef INET6
572static struct hostent *
573_hpmapv6(struct hostent *hp, int *errp)
574{
575 struct hostent hp6;
576
577 if (hp == NULL)
578 return NULL;
579 if (hp->h_addrtype == AF_INET6)
580 return _hpcopy(hp, errp);
581
582 memset(&hp6, 0, sizeof(struct hostent));
583 hp6.h_addrtype = AF_INET6;
584 hp6.h_length = sizeof(struct in6_addr);
585 return _hpmerge(&hp6, hp, errp);
586}
587#endif
588
589/*
590 * _hpsort: sort address by sortlist
591 */
592static struct hostent *
593_hpsort(struct hostent *hp, res_state statp)
594{
595 int i, j, n;
596 u_char *ap, *sp, *mp, **pp;
597 char t;
598 char order[MAXADDRS];
599 int nsort = statp->nsort;
600
601 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
602 return hp;
603 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
604 for (j = 0; j < nsort; j++) {
605#ifdef INET6
606 if (statp->_u._ext.ext->sort_list[j].af !=
607 hp->h_addrtype)
608 continue;
609 sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
610 mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
611#else
612 sp = (u_char *)&statp->sort_list[j].addr;
613 mp = (u_char *)&statp->sort_list[j].mask;
614#endif
615 for (n = 0; n < hp->h_length; n++) {
616 if ((ap[n] & mp[n]) != sp[n])
617 break;
618 }
619 if (n == hp->h_length)
620 break;
621 }
622 order[i] = j;
623 }
624 n = i;
625 pp = (u_char **)hp->h_addr_list;
626 for (i = 0; i < n - 1; i++) {
627 for (j = i + 1; j < n; j++) {
628 if (order[i] > order[j]) {
629 ap = pp[i];
630 pp[i] = pp[j];
631 pp[j] = ap;
632 t = order[i];
633 order[i] = order[j];
634 order[j] = t;
635 }
636 }
637 }
638 return hp;
639}
640
641#ifdef ENABLE_IP6ADDRCTL
635/*
636 * _hpreorder: sort address by default address selection
637 */
638static struct hostent *
639_hpreorder(struct hostent *hp)
640{
641 struct hp_order *aio;
642 int i, n;
643 char *ap;
644 struct sockaddr *sa;
645 struct policyhead policyhead;
646
647 if (hp == NULL)
648 return hp;
649
650 switch (hp->h_addrtype) {
651 case AF_INET:
652#ifdef INET6
653 case AF_INET6:
654#endif
655 break;
656 default:
657 free_addrselectpolicy(&policyhead);
658 return hp;
659 }
660
661 /* count the number of addrinfo elements for sorting. */
662 for (n = 0; hp->h_addr_list[n] != NULL; n++)
663 ;
664
665 /*
666 * If the number is small enough, we can skip the reordering process.
667 */
668 if (n <= 1)
669 return hp;
670
671 /* allocate a temporary array for sort and initialization of it. */
672 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
673 return hp; /* give up reordering */
674 memset(aio, 0, sizeof(*aio) * n);
675
676 /* retrieve address selection policy from the kernel */
677 TAILQ_INIT(&policyhead);
678 if (!get_addrselectpolicy(&policyhead)) {
679 /* no policy is installed into kernel, we don't sort. */
680 free(aio);
681 return hp;
682 }
683
684 for (i = 0; i < n; i++) {
685 ap = hp->h_addr_list[i];
686 aio[i].aio_h_addr = ap;
687 sa = &aio[i].aio_sa;
688 switch (hp->h_addrtype) {
689 case AF_INET:
690 sa->sa_family = AF_INET;
691 sa->sa_len = sizeof(struct sockaddr_in);
692 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
693 sizeof(struct in_addr));
694 break;
695#ifdef INET6
696 case AF_INET6:
697 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
698 sa->sa_family = AF_INET;
699 sa->sa_len = sizeof(struct sockaddr_in);
700 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
701 &ap[12], sizeof(struct in_addr));
702 } else {
703 sa->sa_family = AF_INET6;
704 sa->sa_len = sizeof(struct sockaddr_in6);
705 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
706 ap, sizeof(struct in6_addr));
707 }
708 break;
709#endif
710 }
711 aio[i].aio_dstscope = gai_addr2scopetype(sa);
712 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
713 set_source(&aio[i], &policyhead);
714 }
715
716 /* perform sorting. */
717 qsort(aio, n, sizeof(*aio), comp_dst);
718
719 /* reorder the h_addr_list. */
720 for (i = 0; i < n; i++)
721 hp->h_addr_list[i] = aio[i].aio_h_addr;
722
723 /* cleanup and return */
724 free(aio);
725 free_addrselectpolicy(&policyhead);
726 return hp;
727}
728
729static int
730get_addrselectpolicy(struct policyhead *head)
731{
732#ifdef INET6
733 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
734 size_t l;
735 char *buf;
736 struct in6_addrpolicy *pol, *ep;
737
738 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
739 return (0);
740 if ((buf = malloc(l)) == NULL)
741 return (0);
742 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
743 free(buf);
744 return (0);
745 }
746
747 ep = (struct in6_addrpolicy *)(buf + l);
748 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
749 struct policyqueue *new;
750
751 if ((new = malloc(sizeof(*new))) == NULL) {
752 free_addrselectpolicy(head); /* make the list empty */
753 break;
754 }
755 new->pc_policy = *pol;
756 TAILQ_INSERT_TAIL(head, new, pc_entry);
757 }
758
759 free(buf);
760 return (1);
761#else
762 return (0);
763#endif
764}
765
766static void
767free_addrselectpolicy(struct policyhead *head)
768{
769 struct policyqueue *ent, *nent;
770
771 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
772 nent = TAILQ_NEXT(ent, pc_entry);
773 TAILQ_REMOVE(head, ent, pc_entry);
774 free(ent);
775 }
776}
777
778static struct policyqueue *
779match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
780{
781#ifdef INET6
782 struct policyqueue *ent, *bestent = NULL;
783 struct in6_addrpolicy *pol;
784 int matchlen, bestmatchlen = -1;
785 u_char *mp, *ep, *k, *p, m;
786 struct sockaddr_in6 key;
787
788 switch(addr->sa_family) {
789 case AF_INET6:
790 key = *(struct sockaddr_in6 *)addr;
791 break;
792 case AF_INET:
793 /* convert the address into IPv4-mapped IPv6 address. */
794 memset(&key, 0, sizeof(key));
795 key.sin6_family = AF_INET6;
796 key.sin6_len = sizeof(key);
797 key.sin6_addr.s6_addr[10] = 0xff;
798 key.sin6_addr.s6_addr[11] = 0xff;
799 memcpy(&key.sin6_addr.s6_addr[12],
800 &((struct sockaddr_in *)addr)->sin_addr, 4);
801 break;
802 default:
803 return(NULL);
804 }
805
806 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
807 pol = &ent->pc_policy;
808 matchlen = 0;
809
810 mp = (u_char *)&pol->addrmask.sin6_addr;
811 ep = mp + 16; /* XXX: scope field? */
812 k = (u_char *)&key.sin6_addr;
813 p = (u_char *)&pol->addr.sin6_addr;
814 for (; mp < ep && *mp; mp++, k++, p++) {
815 m = *mp;
816 if ((*k & m) != *p)
817 goto next; /* not match */
818 if (m == 0xff) /* short cut for a typical case */
819 matchlen += 8;
820 else {
821 while (m >= 0x80) {
822 matchlen++;
823 m <<= 1;
824 }
825 }
826 }
827
828 /* matched. check if this is better than the current best. */
829 if (matchlen > bestmatchlen) {
830 bestent = ent;
831 bestmatchlen = matchlen;
832 }
833
834 next:
835 continue;
836 }
837
838 return(bestent);
839#else
840 return(NULL);
841#endif
842
843}
844
845static void
846set_source(struct hp_order *aio, struct policyhead *ph)
847{
848 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
849 socklen_t srclen;
850 int s;
851
852 /* set unspec ("no source is available"), just in case */
853 aio->aio_srcsa.sa_family = AF_UNSPEC;
854 aio->aio_srcscope = -1;
855
856 switch(ss.ss_family) {
857 case AF_INET:
858 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
859 break;
860#ifdef INET6
861 case AF_INET6:
862 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
863 break;
864#endif
865 default: /* ignore unsupported AFs explicitly */
866 return;
867 }
868
869 /* open a socket to get the source address for the given dst */
870 if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
871 return; /* give up */
872 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
873 goto cleanup;
874 srclen = ss.ss_len;
875 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
876 aio->aio_srcsa.sa_family = AF_UNSPEC;
877 goto cleanup;
878 }
879 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
880 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
881 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
882#ifdef INET6
883 if (ss.ss_family == AF_INET6) {
884 struct in6_ifreq ifr6;
885 u_int32_t flags6;
886
887 memset(&ifr6, 0, sizeof(ifr6));
888 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
889 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
890 flags6 = ifr6.ifr_ifru.ifru_flags6;
891 if ((flags6 & IN6_IFF_DEPRECATED))
892 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
893 }
894 }
895#endif
896
897 cleanup:
898 _close(s);
899 return;
900}
901
902static int
903matchlen(struct sockaddr *src, struct sockaddr *dst)
904{
905 int match = 0;
906 u_char *s, *d;
907 u_char *lim, r;
908 int addrlen;
909
910 switch (src->sa_family) {
911#ifdef INET6
912 case AF_INET6:
913 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
914 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
915 addrlen = sizeof(struct in6_addr);
916 lim = s + addrlen;
917 break;
918#endif
919 case AF_INET:
920 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
921 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
922 addrlen = sizeof(struct in_addr);
923 lim = s + addrlen;
924 break;
925 default:
926 return(0);
927 }
928
929 while (s < lim)
930 if ((r = (*d++ ^ *s++)) != 0) {
931 while (r < addrlen * 8) {
932 match++;
933 r <<= 1;
934 }
935 break;
936 } else
937 match += 8;
938 return(match);
939}
940
941static int
942comp_dst(const void *arg1, const void *arg2)
943{
944 const struct hp_order *dst1 = arg1, *dst2 = arg2;
945
946 /*
947 * Rule 1: Avoid unusable destinations.
948 * XXX: we currently do not consider if an appropriate route exists.
949 */
950 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
951 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
952 return(-1);
953 }
954 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
955 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
956 return(1);
957 }
958
959 /* Rule 2: Prefer matching scope. */
960 if (dst1->aio_dstscope == dst1->aio_srcscope &&
961 dst2->aio_dstscope != dst2->aio_srcscope) {
962 return(-1);
963 }
964 if (dst1->aio_dstscope != dst1->aio_srcscope &&
965 dst2->aio_dstscope == dst2->aio_srcscope) {
966 return(1);
967 }
968
969 /* Rule 3: Avoid deprecated addresses. */
970 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
971 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
972 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
973 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
974 return(-1);
975 }
976 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
977 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
978 return(1);
979 }
980 }
981
982 /* Rule 4: Prefer home addresses. */
983 /* XXX: not implemented yet */
984
985 /* Rule 5: Prefer matching label. */
986#ifdef INET6
987 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
988 dst1->aio_srcpolicy->pc_policy.label ==
989 dst1->aio_dstpolicy->pc_policy.label &&
990 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
991 dst2->aio_srcpolicy->pc_policy.label !=
992 dst2->aio_dstpolicy->pc_policy.label)) {
993 return(-1);
994 }
995 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
996 dst2->aio_srcpolicy->pc_policy.label ==
997 dst2->aio_dstpolicy->pc_policy.label &&
998 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
999 dst1->aio_srcpolicy->pc_policy.label !=
1000 dst1->aio_dstpolicy->pc_policy.label)) {
1001 return(1);
1002 }
1003#endif
1004
1005 /* Rule 6: Prefer higher precedence. */
1006#ifdef INET6
1007 if (dst1->aio_dstpolicy &&
1008 (dst2->aio_dstpolicy == NULL ||
1009 dst1->aio_dstpolicy->pc_policy.preced >
1010 dst2->aio_dstpolicy->pc_policy.preced)) {
1011 return(-1);
1012 }
1013 if (dst2->aio_dstpolicy &&
1014 (dst1->aio_dstpolicy == NULL ||
1015 dst2->aio_dstpolicy->pc_policy.preced >
1016 dst1->aio_dstpolicy->pc_policy.preced)) {
1017 return(1);
1018 }
1019#endif
1020
1021 /* Rule 7: Prefer native transport. */
1022 /* XXX: not implemented yet */
1023
1024 /* Rule 8: Prefer smaller scope. */
1025 if (dst1->aio_dstscope >= 0 &&
1026 dst1->aio_dstscope < dst2->aio_dstscope) {
1027 return(-1);
1028 }
1029 if (dst2->aio_dstscope >= 0 &&
1030 dst2->aio_dstscope < dst1->aio_dstscope) {
1031 return(1);
1032 }
1033
1034 /*
1035 * Rule 9: Use longest matching prefix.
1036 * We compare the match length in a same AF only.
1037 */
1038 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1039 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1040 return(-1);
1041 }
1042 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1043 return(1);
1044 }
1045 }
1046
1047 /* Rule 10: Otherwise, leave the order unchanged. */
1048 return(-1);
1049}
1050
1051/*
1052 * Copy from scope.c.
1053 * XXX: we should standardize the functions and link them as standard
1054 * library.
1055 */
1056static int
1057gai_addr2scopetype(struct sockaddr *sa)
1058{
1059#ifdef INET6
1060 struct sockaddr_in6 *sa6;
1061#endif
1062 struct sockaddr_in *sa4;
1063
1064 switch(sa->sa_family) {
1065#ifdef INET6
1066 case AF_INET6:
1067 sa6 = (struct sockaddr_in6 *)sa;
1068 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1069 /* just use the scope field of the multicast address */
1070 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1071 }
1072 /*
1073 * Unicast addresses: map scope type to corresponding scope
1074 * value defined for multcast addresses.
1075 * XXX: hardcoded scope type values are bad...
1076 */
1077 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1078 return(1); /* node local scope */
1079 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1080 return(2); /* link-local scope */
1081 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1082 return(5); /* site-local scope */
1083 return(14); /* global scope */
1084 break;
1085#endif
1086 case AF_INET:
1087 /*
1088 * IPv4 pseudo scoping according to RFC 3484.
1089 */
1090 sa4 = (struct sockaddr_in *)sa;
1091 /* IPv4 autoconfiguration addresses have link-local scope. */
1092 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1093 ((u_char *)&sa4->sin_addr)[1] == 254)
1094 return(2);
1095 /* Private addresses have site-local scope. */
1096 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1097 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1098 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1099 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1100 ((u_char *)&sa4->sin_addr)[1] == 168))
1101 return(14); /* XXX: It should be 5 unless NAT */
1102 /* Loopback addresses have link-local scope. */
1103 if (((u_char *)&sa4->sin_addr)[0] == 127)
1104 return(2);
1105 return(14);
1106 break;
1107 default:
1108 errno = EAFNOSUPPORT; /* is this a good error? */
1109 return(-1);
1110 }
1111}
642/*
643 * _hpreorder: sort address by default address selection
644 */
645static struct hostent *
646_hpreorder(struct hostent *hp)
647{
648 struct hp_order *aio;
649 int i, n;
650 char *ap;
651 struct sockaddr *sa;
652 struct policyhead policyhead;
653
654 if (hp == NULL)
655 return hp;
656
657 switch (hp->h_addrtype) {
658 case AF_INET:
659#ifdef INET6
660 case AF_INET6:
661#endif
662 break;
663 default:
664 free_addrselectpolicy(&policyhead);
665 return hp;
666 }
667
668 /* count the number of addrinfo elements for sorting. */
669 for (n = 0; hp->h_addr_list[n] != NULL; n++)
670 ;
671
672 /*
673 * If the number is small enough, we can skip the reordering process.
674 */
675 if (n <= 1)
676 return hp;
677
678 /* allocate a temporary array for sort and initialization of it. */
679 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
680 return hp; /* give up reordering */
681 memset(aio, 0, sizeof(*aio) * n);
682
683 /* retrieve address selection policy from the kernel */
684 TAILQ_INIT(&policyhead);
685 if (!get_addrselectpolicy(&policyhead)) {
686 /* no policy is installed into kernel, we don't sort. */
687 free(aio);
688 return hp;
689 }
690
691 for (i = 0; i < n; i++) {
692 ap = hp->h_addr_list[i];
693 aio[i].aio_h_addr = ap;
694 sa = &aio[i].aio_sa;
695 switch (hp->h_addrtype) {
696 case AF_INET:
697 sa->sa_family = AF_INET;
698 sa->sa_len = sizeof(struct sockaddr_in);
699 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
700 sizeof(struct in_addr));
701 break;
702#ifdef INET6
703 case AF_INET6:
704 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
705 sa->sa_family = AF_INET;
706 sa->sa_len = sizeof(struct sockaddr_in);
707 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
708 &ap[12], sizeof(struct in_addr));
709 } else {
710 sa->sa_family = AF_INET6;
711 sa->sa_len = sizeof(struct sockaddr_in6);
712 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
713 ap, sizeof(struct in6_addr));
714 }
715 break;
716#endif
717 }
718 aio[i].aio_dstscope = gai_addr2scopetype(sa);
719 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
720 set_source(&aio[i], &policyhead);
721 }
722
723 /* perform sorting. */
724 qsort(aio, n, sizeof(*aio), comp_dst);
725
726 /* reorder the h_addr_list. */
727 for (i = 0; i < n; i++)
728 hp->h_addr_list[i] = aio[i].aio_h_addr;
729
730 /* cleanup and return */
731 free(aio);
732 free_addrselectpolicy(&policyhead);
733 return hp;
734}
735
736static int
737get_addrselectpolicy(struct policyhead *head)
738{
739#ifdef INET6
740 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
741 size_t l;
742 char *buf;
743 struct in6_addrpolicy *pol, *ep;
744
745 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
746 return (0);
747 if ((buf = malloc(l)) == NULL)
748 return (0);
749 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
750 free(buf);
751 return (0);
752 }
753
754 ep = (struct in6_addrpolicy *)(buf + l);
755 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
756 struct policyqueue *new;
757
758 if ((new = malloc(sizeof(*new))) == NULL) {
759 free_addrselectpolicy(head); /* make the list empty */
760 break;
761 }
762 new->pc_policy = *pol;
763 TAILQ_INSERT_TAIL(head, new, pc_entry);
764 }
765
766 free(buf);
767 return (1);
768#else
769 return (0);
770#endif
771}
772
773static void
774free_addrselectpolicy(struct policyhead *head)
775{
776 struct policyqueue *ent, *nent;
777
778 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
779 nent = TAILQ_NEXT(ent, pc_entry);
780 TAILQ_REMOVE(head, ent, pc_entry);
781 free(ent);
782 }
783}
784
785static struct policyqueue *
786match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
787{
788#ifdef INET6
789 struct policyqueue *ent, *bestent = NULL;
790 struct in6_addrpolicy *pol;
791 int matchlen, bestmatchlen = -1;
792 u_char *mp, *ep, *k, *p, m;
793 struct sockaddr_in6 key;
794
795 switch(addr->sa_family) {
796 case AF_INET6:
797 key = *(struct sockaddr_in6 *)addr;
798 break;
799 case AF_INET:
800 /* convert the address into IPv4-mapped IPv6 address. */
801 memset(&key, 0, sizeof(key));
802 key.sin6_family = AF_INET6;
803 key.sin6_len = sizeof(key);
804 key.sin6_addr.s6_addr[10] = 0xff;
805 key.sin6_addr.s6_addr[11] = 0xff;
806 memcpy(&key.sin6_addr.s6_addr[12],
807 &((struct sockaddr_in *)addr)->sin_addr, 4);
808 break;
809 default:
810 return(NULL);
811 }
812
813 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
814 pol = &ent->pc_policy;
815 matchlen = 0;
816
817 mp = (u_char *)&pol->addrmask.sin6_addr;
818 ep = mp + 16; /* XXX: scope field? */
819 k = (u_char *)&key.sin6_addr;
820 p = (u_char *)&pol->addr.sin6_addr;
821 for (; mp < ep && *mp; mp++, k++, p++) {
822 m = *mp;
823 if ((*k & m) != *p)
824 goto next; /* not match */
825 if (m == 0xff) /* short cut for a typical case */
826 matchlen += 8;
827 else {
828 while (m >= 0x80) {
829 matchlen++;
830 m <<= 1;
831 }
832 }
833 }
834
835 /* matched. check if this is better than the current best. */
836 if (matchlen > bestmatchlen) {
837 bestent = ent;
838 bestmatchlen = matchlen;
839 }
840
841 next:
842 continue;
843 }
844
845 return(bestent);
846#else
847 return(NULL);
848#endif
849
850}
851
852static void
853set_source(struct hp_order *aio, struct policyhead *ph)
854{
855 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
856 socklen_t srclen;
857 int s;
858
859 /* set unspec ("no source is available"), just in case */
860 aio->aio_srcsa.sa_family = AF_UNSPEC;
861 aio->aio_srcscope = -1;
862
863 switch(ss.ss_family) {
864 case AF_INET:
865 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
866 break;
867#ifdef INET6
868 case AF_INET6:
869 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
870 break;
871#endif
872 default: /* ignore unsupported AFs explicitly */
873 return;
874 }
875
876 /* open a socket to get the source address for the given dst */
877 if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
878 return; /* give up */
879 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
880 goto cleanup;
881 srclen = ss.ss_len;
882 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
883 aio->aio_srcsa.sa_family = AF_UNSPEC;
884 goto cleanup;
885 }
886 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
887 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
888 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
889#ifdef INET6
890 if (ss.ss_family == AF_INET6) {
891 struct in6_ifreq ifr6;
892 u_int32_t flags6;
893
894 memset(&ifr6, 0, sizeof(ifr6));
895 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
896 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
897 flags6 = ifr6.ifr_ifru.ifru_flags6;
898 if ((flags6 & IN6_IFF_DEPRECATED))
899 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
900 }
901 }
902#endif
903
904 cleanup:
905 _close(s);
906 return;
907}
908
909static int
910matchlen(struct sockaddr *src, struct sockaddr *dst)
911{
912 int match = 0;
913 u_char *s, *d;
914 u_char *lim, r;
915 int addrlen;
916
917 switch (src->sa_family) {
918#ifdef INET6
919 case AF_INET6:
920 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
921 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
922 addrlen = sizeof(struct in6_addr);
923 lim = s + addrlen;
924 break;
925#endif
926 case AF_INET:
927 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
928 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
929 addrlen = sizeof(struct in_addr);
930 lim = s + addrlen;
931 break;
932 default:
933 return(0);
934 }
935
936 while (s < lim)
937 if ((r = (*d++ ^ *s++)) != 0) {
938 while (r < addrlen * 8) {
939 match++;
940 r <<= 1;
941 }
942 break;
943 } else
944 match += 8;
945 return(match);
946}
947
948static int
949comp_dst(const void *arg1, const void *arg2)
950{
951 const struct hp_order *dst1 = arg1, *dst2 = arg2;
952
953 /*
954 * Rule 1: Avoid unusable destinations.
955 * XXX: we currently do not consider if an appropriate route exists.
956 */
957 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
958 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
959 return(-1);
960 }
961 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
962 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
963 return(1);
964 }
965
966 /* Rule 2: Prefer matching scope. */
967 if (dst1->aio_dstscope == dst1->aio_srcscope &&
968 dst2->aio_dstscope != dst2->aio_srcscope) {
969 return(-1);
970 }
971 if (dst1->aio_dstscope != dst1->aio_srcscope &&
972 dst2->aio_dstscope == dst2->aio_srcscope) {
973 return(1);
974 }
975
976 /* Rule 3: Avoid deprecated addresses. */
977 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
978 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
979 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
980 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
981 return(-1);
982 }
983 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
984 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
985 return(1);
986 }
987 }
988
989 /* Rule 4: Prefer home addresses. */
990 /* XXX: not implemented yet */
991
992 /* Rule 5: Prefer matching label. */
993#ifdef INET6
994 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
995 dst1->aio_srcpolicy->pc_policy.label ==
996 dst1->aio_dstpolicy->pc_policy.label &&
997 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
998 dst2->aio_srcpolicy->pc_policy.label !=
999 dst2->aio_dstpolicy->pc_policy.label)) {
1000 return(-1);
1001 }
1002 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
1003 dst2->aio_srcpolicy->pc_policy.label ==
1004 dst2->aio_dstpolicy->pc_policy.label &&
1005 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1006 dst1->aio_srcpolicy->pc_policy.label !=
1007 dst1->aio_dstpolicy->pc_policy.label)) {
1008 return(1);
1009 }
1010#endif
1011
1012 /* Rule 6: Prefer higher precedence. */
1013#ifdef INET6
1014 if (dst1->aio_dstpolicy &&
1015 (dst2->aio_dstpolicy == NULL ||
1016 dst1->aio_dstpolicy->pc_policy.preced >
1017 dst2->aio_dstpolicy->pc_policy.preced)) {
1018 return(-1);
1019 }
1020 if (dst2->aio_dstpolicy &&
1021 (dst1->aio_dstpolicy == NULL ||
1022 dst2->aio_dstpolicy->pc_policy.preced >
1023 dst1->aio_dstpolicy->pc_policy.preced)) {
1024 return(1);
1025 }
1026#endif
1027
1028 /* Rule 7: Prefer native transport. */
1029 /* XXX: not implemented yet */
1030
1031 /* Rule 8: Prefer smaller scope. */
1032 if (dst1->aio_dstscope >= 0 &&
1033 dst1->aio_dstscope < dst2->aio_dstscope) {
1034 return(-1);
1035 }
1036 if (dst2->aio_dstscope >= 0 &&
1037 dst2->aio_dstscope < dst1->aio_dstscope) {
1038 return(1);
1039 }
1040
1041 /*
1042 * Rule 9: Use longest matching prefix.
1043 * We compare the match length in a same AF only.
1044 */
1045 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1046 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1047 return(-1);
1048 }
1049 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1050 return(1);
1051 }
1052 }
1053
1054 /* Rule 10: Otherwise, leave the order unchanged. */
1055 return(-1);
1056}
1057
1058/*
1059 * Copy from scope.c.
1060 * XXX: we should standardize the functions and link them as standard
1061 * library.
1062 */
1063static int
1064gai_addr2scopetype(struct sockaddr *sa)
1065{
1066#ifdef INET6
1067 struct sockaddr_in6 *sa6;
1068#endif
1069 struct sockaddr_in *sa4;
1070
1071 switch(sa->sa_family) {
1072#ifdef INET6
1073 case AF_INET6:
1074 sa6 = (struct sockaddr_in6 *)sa;
1075 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1076 /* just use the scope field of the multicast address */
1077 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1078 }
1079 /*
1080 * Unicast addresses: map scope type to corresponding scope
1081 * value defined for multcast addresses.
1082 * XXX: hardcoded scope type values are bad...
1083 */
1084 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1085 return(1); /* node local scope */
1086 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1087 return(2); /* link-local scope */
1088 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1089 return(5); /* site-local scope */
1090 return(14); /* global scope */
1091 break;
1092#endif
1093 case AF_INET:
1094 /*
1095 * IPv4 pseudo scoping according to RFC 3484.
1096 */
1097 sa4 = (struct sockaddr_in *)sa;
1098 /* IPv4 autoconfiguration addresses have link-local scope. */
1099 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1100 ((u_char *)&sa4->sin_addr)[1] == 254)
1101 return(2);
1102 /* Private addresses have site-local scope. */
1103 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1104 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1105 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1106 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1107 ((u_char *)&sa4->sin_addr)[1] == 168))
1108 return(14); /* XXX: It should be 5 unless NAT */
1109 /* Loopback addresses have link-local scope. */
1110 if (((u_char *)&sa4->sin_addr)[0] == 127)
1111 return(2);
1112 return(14);
1113 break;
1114 default:
1115 errno = EAFNOSUPPORT; /* is this a good error? */
1116 return(-1);
1117 }
1118}
1119#endif