1bf838688SHajimu UMEMOTO /* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */
2b826397aSJun-ichiro itojun Hagino
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni *
67d56d374SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
77d56d374SYoshinobu Inoue * All rights reserved.
87d56d374SYoshinobu Inoue *
97d56d374SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without
107d56d374SYoshinobu Inoue * modification, are permitted provided that the following conditions
117d56d374SYoshinobu Inoue * are met:
127d56d374SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright
137d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer.
147d56d374SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright
157d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the
167d56d374SYoshinobu Inoue * documentation and/or other materials provided with the distribution.
177d56d374SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors
187d56d374SYoshinobu Inoue * may be used to endorse or promote products derived from this software
197d56d374SYoshinobu Inoue * without specific prior written permission.
207d56d374SYoshinobu Inoue *
217d56d374SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
227d56d374SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237d56d374SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247d56d374SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
257d56d374SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267d56d374SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277d56d374SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287d56d374SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297d56d374SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307d56d374SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317d56d374SYoshinobu Inoue * SUCH DAMAGE.
327d56d374SYoshinobu Inoue */
337d56d374SYoshinobu Inoue
347d56d374SYoshinobu Inoue /*
357d56d374SYoshinobu Inoue * Issues to be discussed:
367d56d374SYoshinobu Inoue * - Return values. There are nonstandard return values defined and used
377d56d374SYoshinobu Inoue * in the source code. This is because RFC2553 is silent about which error
387d56d374SYoshinobu Inoue * code must be returned for which situation.
39a42af91cSHajimu UMEMOTO * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
40587e285eSKonstantin Belousov * invalid. Current code accepts NULL to be compatible with other OSes.
41dd521ef1SHajimu UMEMOTO *
4200a8a579SYoshinobu Inoue * Note:
4300a8a579SYoshinobu Inoue * - The code filters out AFs that are not supported by the kernel,
4400a8a579SYoshinobu Inoue * when globbing NULL hostname (to loopback, or wildcard). Is it the right
4500a8a579SYoshinobu Inoue * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
4600a8a579SYoshinobu Inoue * in ai_flags?
47a42af91cSHajimu UMEMOTO * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
48a42af91cSHajimu UMEMOTO * (1) what should we do against numeric hostname (2) what should we do
49a42af91cSHajimu UMEMOTO * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
50a42af91cSHajimu UMEMOTO * non-loopback address configured? global address configured?
51dd521ef1SHajimu UMEMOTO *
52dd521ef1SHajimu UMEMOTO * OS specific notes for freebsd4:
53dd521ef1SHajimu UMEMOTO * - FreeBSD supported $GAI. The code does not.
547d56d374SYoshinobu Inoue */
557d56d374SYoshinobu Inoue
56d201fe46SDaniel Eischen #include "namespace.h"
577d56d374SYoshinobu Inoue #include <sys/param.h>
587d56d374SYoshinobu Inoue #include <sys/socket.h>
597d56d374SYoshinobu Inoue #include <net/if.h>
607d56d374SYoshinobu Inoue #include <netinet/in.h>
616f63a114SHajimu UMEMOTO #include <net/if_types.h>
626f63a114SHajimu UMEMOTO #include <ifaddrs.h>
634c6867a8SHajimu UMEMOTO #include <sys/queue.h>
644c6867a8SHajimu UMEMOTO #ifdef INET6
654c6867a8SHajimu UMEMOTO #include <sys/sysctl.h>
664d489f47SHajimu UMEMOTO #include <sys/ioctl.h>
676f63a114SHajimu UMEMOTO #include <netinet6/in6_var.h>
686f63a114SHajimu UMEMOTO #include <netinet6/nd6.h>
694c6867a8SHajimu UMEMOTO #endif
707d56d374SYoshinobu Inoue #include <arpa/inet.h>
717d56d374SYoshinobu Inoue #include <arpa/nameser.h>
72bf838688SHajimu UMEMOTO #include <rpc/rpc.h>
73bf838688SHajimu UMEMOTO #include <rpcsvc/yp_prot.h>
74bf838688SHajimu UMEMOTO #include <rpcsvc/ypclnt.h>
757d56d374SYoshinobu Inoue #include <netdb.h>
767d56d374SYoshinobu Inoue #include <resolv.h>
777d56d374SYoshinobu Inoue #include <string.h>
787d56d374SYoshinobu Inoue #include <stdlib.h>
797d56d374SYoshinobu Inoue #include <stddef.h>
807d56d374SYoshinobu Inoue #include <ctype.h>
817d56d374SYoshinobu Inoue #include <unistd.h>
827d56d374SYoshinobu Inoue #include <stdio.h>
83a42af91cSHajimu UMEMOTO #include <errno.h>
8469b0a4b6SJim Pirzyk
8569b0a4b6SJim Pirzyk #include "res_config.h"
8669b0a4b6SJim Pirzyk
87ce6282dbSHajimu UMEMOTO #ifdef DEBUG
88ce6282dbSHajimu UMEMOTO #include <syslog.h>
89ce6282dbSHajimu UMEMOTO #endif
907d56d374SYoshinobu Inoue
91248aee62SJacques Vidrine #include <stdarg.h>
92248aee62SJacques Vidrine #include <nsswitch.h>
93d201fe46SDaniel Eischen #include "un-namespace.h"
94b848a37dSHajimu UMEMOTO #include "netdb_private.h"
952bbd7cf8SJacques Vidrine #include "libc_private.h"
9606a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
9706a99fe3SHajimu UMEMOTO #include "nscache.h"
9806a99fe3SHajimu UMEMOTO #endif
99248aee62SJacques Vidrine
1007d56d374SYoshinobu Inoue #define ANY 0
1017d56d374SYoshinobu Inoue #define YES 1
1027d56d374SYoshinobu Inoue #define NO 0
1037d56d374SYoshinobu Inoue
1047d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 };
105dd521ef1SHajimu UMEMOTO static const char in_loopback[] = { 127, 0, 0, 1 };
106dd521ef1SHajimu UMEMOTO #ifdef INET6
1077d56d374SYoshinobu Inoue static const char in6_addrany[] = {
1087d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1097d56d374SYoshinobu Inoue };
1107d56d374SYoshinobu Inoue static const char in6_loopback[] = {
1117d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1127d56d374SYoshinobu Inoue };
113dd521ef1SHajimu UMEMOTO #endif
1147d56d374SYoshinobu Inoue
1154c6867a8SHajimu UMEMOTO struct policyqueue {
1164c6867a8SHajimu UMEMOTO TAILQ_ENTRY(policyqueue) pc_entry;
1174c6867a8SHajimu UMEMOTO #ifdef INET6
1184c6867a8SHajimu UMEMOTO struct in6_addrpolicy pc_policy;
1194c6867a8SHajimu UMEMOTO #endif
1204c6867a8SHajimu UMEMOTO };
1214c6867a8SHajimu UMEMOTO TAILQ_HEAD(policyhead, policyqueue);
1224c6867a8SHajimu UMEMOTO
1237d56d374SYoshinobu Inoue static const struct afd {
1247d56d374SYoshinobu Inoue int a_af;
1257d56d374SYoshinobu Inoue int a_addrlen;
126036ae3ddSHajimu UMEMOTO socklen_t a_socklen;
1277d56d374SYoshinobu Inoue int a_off;
1287d56d374SYoshinobu Inoue const char *a_addrany;
1297d56d374SYoshinobu Inoue const char *a_loopback;
1307d56d374SYoshinobu Inoue int a_scoped;
1317d56d374SYoshinobu Inoue } afdl [] = {
1327d56d374SYoshinobu Inoue #ifdef INET6
1337d56d374SYoshinobu Inoue #define N_INET6 0
1347d56d374SYoshinobu Inoue {PF_INET6, sizeof(struct in6_addr),
1357d56d374SYoshinobu Inoue sizeof(struct sockaddr_in6),
1367d56d374SYoshinobu Inoue offsetof(struct sockaddr_in6, sin6_addr),
1377d56d374SYoshinobu Inoue in6_addrany, in6_loopback, 1},
1387d56d374SYoshinobu Inoue #define N_INET 1
139bb957021SHiroki Sato #define N_LOCAL 2
1407d56d374SYoshinobu Inoue #else
1417d56d374SYoshinobu Inoue #define N_INET 0
142bb957021SHiroki Sato #define N_LOCAL 1
1437d56d374SYoshinobu Inoue #endif
1447d56d374SYoshinobu Inoue {PF_INET, sizeof(struct in_addr),
1457d56d374SYoshinobu Inoue sizeof(struct sockaddr_in),
1467d56d374SYoshinobu Inoue offsetof(struct sockaddr_in, sin_addr),
1477d56d374SYoshinobu Inoue in_addrany, in_loopback, 0},
148bb957021SHiroki Sato #define sizeofmember(type, member) (sizeof(((type *)0)->member))
149bb957021SHiroki Sato {PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
150bb957021SHiroki Sato sizeof(struct sockaddr_un),
151bb957021SHiroki Sato offsetof(struct sockaddr_un, sun_path),
152bb957021SHiroki Sato NULL, NULL, 0},
1537d56d374SYoshinobu Inoue {0, 0, 0, 0, NULL, NULL, 0},
1547d56d374SYoshinobu Inoue };
1557d56d374SYoshinobu Inoue
1567d56d374SYoshinobu Inoue struct explore {
157a42af91cSHajimu UMEMOTO int e_af;
1587d56d374SYoshinobu Inoue int e_socktype;
1597d56d374SYoshinobu Inoue int e_protocol;
1607d56d374SYoshinobu Inoue int e_wild;
161bb957021SHiroki Sato #define AF_ANY 0x01
162bb957021SHiroki Sato #define SOCKTYPE_ANY 0x02
163bb957021SHiroki Sato #define PROTOCOL_ANY 0x04
164bb957021SHiroki Sato #define WILD_AF(ex) ((ex)->e_wild & AF_ANY)
165bb957021SHiroki Sato #define WILD_SOCKTYPE(ex) ((ex)->e_wild & SOCKTYPE_ANY)
166bb957021SHiroki Sato #define WILD_PROTOCOL(ex) ((ex)->e_wild & PROTOCOL_ANY)
1677d56d374SYoshinobu Inoue };
1687d56d374SYoshinobu Inoue
1697d56d374SYoshinobu Inoue static const struct explore explore[] = {
170a42af91cSHajimu UMEMOTO #ifdef INET6
171bb957021SHiroki Sato { PF_INET6, SOCK_DGRAM, IPPROTO_UDP,
172bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
173bb957021SHiroki Sato { PF_INET6, SOCK_STREAM, IPPROTO_TCP,
174bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
175bb957021SHiroki Sato { PF_INET6, SOCK_STREAM, IPPROTO_SCTP,
176bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY },
177bb957021SHiroki Sato { PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP,
178bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
179bb957021SHiroki Sato { PF_INET6, SOCK_DGRAM, IPPROTO_UDPLITE,
180bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY },
181bb957021SHiroki Sato { PF_INET6, SOCK_RAW, ANY,
182bb957021SHiroki Sato AF_ANY | PROTOCOL_ANY },
183a42af91cSHajimu UMEMOTO #endif
184bb957021SHiroki Sato { PF_INET, SOCK_DGRAM, IPPROTO_UDP,
185bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
186bb957021SHiroki Sato { PF_INET, SOCK_STREAM, IPPROTO_TCP,
187bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
188bb957021SHiroki Sato { PF_INET, SOCK_STREAM, IPPROTO_SCTP,
189bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY },
190bb957021SHiroki Sato { PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP,
191bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
192bb957021SHiroki Sato { PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE,
193bb957021SHiroki Sato AF_ANY | SOCKTYPE_ANY },
194bb957021SHiroki Sato { PF_INET, SOCK_RAW, ANY,
195bb957021SHiroki Sato AF_ANY | PROTOCOL_ANY },
1964c60a05dSHiroki Sato { PF_LOCAL, SOCK_DGRAM, ANY,
1974c60a05dSHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
1984c60a05dSHiroki Sato { PF_LOCAL, SOCK_STREAM, ANY,
1994c60a05dSHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
2004c60a05dSHiroki Sato { PF_LOCAL, SOCK_SEQPACKET, ANY,
2014c60a05dSHiroki Sato AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
2022bd56eacSJilles Tjoelker { -1, 0, 0, 0 },
2037d56d374SYoshinobu Inoue };
2047d56d374SYoshinobu Inoue
2057d56d374SYoshinobu Inoue #ifdef INET6
2067d56d374SYoshinobu Inoue #define PTON_MAX 16
2077d56d374SYoshinobu Inoue #else
2087d56d374SYoshinobu Inoue #define PTON_MAX 4
2097d56d374SYoshinobu Inoue #endif
2107d56d374SYoshinobu Inoue
2114c6867a8SHajimu UMEMOTO #define AIO_SRCFLAG_DEPRECATED 0x1
2124c6867a8SHajimu UMEMOTO
2134c6867a8SHajimu UMEMOTO struct ai_order {
2144c6867a8SHajimu UMEMOTO union {
2154c6867a8SHajimu UMEMOTO struct sockaddr_storage aiou_ss;
2164c6867a8SHajimu UMEMOTO struct sockaddr aiou_sa;
2174c6867a8SHajimu UMEMOTO } aio_src_un;
2184c6867a8SHajimu UMEMOTO #define aio_srcsa aio_src_un.aiou_sa
2194c6867a8SHajimu UMEMOTO u_int32_t aio_srcflag;
2204c6867a8SHajimu UMEMOTO int aio_srcscope;
2214c6867a8SHajimu UMEMOTO int aio_dstscope;
2224c6867a8SHajimu UMEMOTO struct policyqueue *aio_srcpolicy;
2234c6867a8SHajimu UMEMOTO struct policyqueue *aio_dstpolicy;
2244c6867a8SHajimu UMEMOTO struct addrinfo *aio_ai;
2254c6867a8SHajimu UMEMOTO int aio_matchlen;
226ebbc4458SAndrey A. Chernov int aio_initial_sequence;
2274c6867a8SHajimu UMEMOTO };
2284c6867a8SHajimu UMEMOTO
229248aee62SJacques Vidrine static const ns_src default_dns_files[] = {
230248aee62SJacques Vidrine { NSSRC_FILES, NS_SUCCESS },
231248aee62SJacques Vidrine { NSSRC_DNS, NS_SUCCESS },
232248aee62SJacques Vidrine { 0 }
233248aee62SJacques Vidrine };
234248aee62SJacques Vidrine
235a42af91cSHajimu UMEMOTO struct res_target {
236a42af91cSHajimu UMEMOTO struct res_target *next;
237a42af91cSHajimu UMEMOTO const char *name; /* domain name */
238b826397aSJun-ichiro itojun Hagino int qclass, qtype; /* class and type of query */
239a42af91cSHajimu UMEMOTO u_char *answer; /* buffer to put answer */
240a42af91cSHajimu UMEMOTO int anslen; /* size of answer buffer */
241a42af91cSHajimu UMEMOTO int n; /* result length */
242a42af91cSHajimu UMEMOTO };
243a42af91cSHajimu UMEMOTO
244688a5c3eSHajimu UMEMOTO #define MAXPACKET (64*1024)
245688a5c3eSHajimu UMEMOTO
246688a5c3eSHajimu UMEMOTO typedef union {
247688a5c3eSHajimu UMEMOTO HEADER hdr;
248688a5c3eSHajimu UMEMOTO u_char buf[MAXPACKET];
249688a5c3eSHajimu UMEMOTO } querybuf;
250688a5c3eSHajimu UMEMOTO
251e24e9d99SHajimu UMEMOTO static int str2number(const char *, int *);
252bc421492SHajimu UMEMOTO static int explore_copy(const struct addrinfo *, const struct addrinfo *,
253bc421492SHajimu UMEMOTO struct addrinfo **);
2541372519bSDavid E. O'Brien static int explore_null(const struct addrinfo *,
2551372519bSDavid E. O'Brien const char *, struct addrinfo **);
2561372519bSDavid E. O'Brien static int explore_numeric(const struct addrinfo *, const char *,
2578755b918SHajimu UMEMOTO const char *, struct addrinfo **, const char *);
2581372519bSDavid E. O'Brien static int explore_numeric_scope(const struct addrinfo *, const char *,
2591372519bSDavid E. O'Brien const char *, struct addrinfo **);
2601372519bSDavid E. O'Brien static int get_canonname(const struct addrinfo *,
2611372519bSDavid E. O'Brien struct addrinfo *, const char *);
2621372519bSDavid E. O'Brien static struct addrinfo *get_ai(const struct addrinfo *,
2631372519bSDavid E. O'Brien const struct afd *, const char *);
264bc421492SHajimu UMEMOTO static struct addrinfo *copy_ai(const struct addrinfo *);
265c05ac53bSDavid E. O'Brien static int get_portmatch(const struct addrinfo *, const char *);
266c05ac53bSDavid E. O'Brien static int get_port(struct addrinfo *, const char *, int);
267c05ac53bSDavid E. O'Brien static const struct afd *find_afd(int);
268bf838688SHajimu UMEMOTO static int addrconfig(struct addrinfo *);
2696f63a114SHajimu UMEMOTO #ifdef INET6
2706f63a114SHajimu UMEMOTO static int is_ifdisabled(char *);
2716f63a114SHajimu UMEMOTO #endif
2724d489f47SHajimu UMEMOTO static void set_source(struct ai_order *, struct policyhead *);
2734c6867a8SHajimu UMEMOTO static int comp_dst(const void *, const void *);
274a42af91cSHajimu UMEMOTO #ifdef INET6
275a89c1d30SHajimu UMEMOTO static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
276a42af91cSHajimu UMEMOTO #endif
2774c6867a8SHajimu UMEMOTO static int gai_addr2scopetype(struct sockaddr *);
278a42af91cSHajimu UMEMOTO
279688a5c3eSHajimu UMEMOTO static int explore_fqdn(const struct addrinfo *, const char *,
280688a5c3eSHajimu UMEMOTO const char *, struct addrinfo **);
281688a5c3eSHajimu UMEMOTO
2824c6867a8SHajimu UMEMOTO static int reorder(struct addrinfo *);
2834c6867a8SHajimu UMEMOTO static int get_addrselectpolicy(struct policyhead *);
2844c6867a8SHajimu UMEMOTO static void free_addrselectpolicy(struct policyhead *);
2854c6867a8SHajimu UMEMOTO static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
2864c6867a8SHajimu UMEMOTO struct policyhead *);
2874d489f47SHajimu UMEMOTO static int matchlen(struct sockaddr *, struct sockaddr *);
2884c6867a8SHajimu UMEMOTO
2891372519bSDavid E. O'Brien static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
2905342d17fSHajimu UMEMOTO const struct addrinfo *, res_state);
291688a5c3eSHajimu UMEMOTO #if defined(RESOLVSORT)
2925342d17fSHajimu UMEMOTO static int addr4sort(struct addrinfo *, res_state);
293688a5c3eSHajimu UMEMOTO #endif
294a89c1d30SHajimu UMEMOTO static int _dns_getaddrinfo(void *, void *, va_list);
2959def31dcSHajimu UMEMOTO static void _sethtent(FILE **);
2969def31dcSHajimu UMEMOTO static void _endhtent(FILE **);
2979def31dcSHajimu UMEMOTO static struct addrinfo *_gethtent(FILE **, const char *,
2989def31dcSHajimu UMEMOTO const struct addrinfo *);
299c05ac53bSDavid E. O'Brien static int _files_getaddrinfo(void *, void *, va_list);
300a42af91cSHajimu UMEMOTO #ifdef YP
301c05ac53bSDavid E. O'Brien static struct addrinfo *_yphostent(char *, const struct addrinfo *);
302c05ac53bSDavid E. O'Brien static int _yp_getaddrinfo(void *, void *, va_list);
303a42af91cSHajimu UMEMOTO #endif
30406a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
30506a99fe3SHajimu UMEMOTO static int addrinfo_id_func(char *, size_t *, va_list, void *);
30606a99fe3SHajimu UMEMOTO static int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *);
30706a99fe3SHajimu UMEMOTO static int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *);
30806a99fe3SHajimu UMEMOTO #endif
309a42af91cSHajimu UMEMOTO
3105342d17fSHajimu UMEMOTO static int res_queryN(const char *, struct res_target *, res_state);
3115342d17fSHajimu UMEMOTO static int res_searchN(const char *, struct res_target *, res_state);
3121372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *,
3135342d17fSHajimu UMEMOTO struct res_target *, res_state);
314a42af91cSHajimu UMEMOTO
3157d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */
3167d56d374SYoshinobu Inoue
3177d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \
3187d56d374SYoshinobu Inoue do { \
3197d56d374SYoshinobu Inoue /* external reference: pai, error, and label free */ \
3207d56d374SYoshinobu Inoue (ai) = get_ai(pai, (afd), (addr)); \
3217d56d374SYoshinobu Inoue if ((ai) == NULL) { \
3227d56d374SYoshinobu Inoue error = EAI_MEMORY; \
3237d56d374SYoshinobu Inoue goto free; \
3247d56d374SYoshinobu Inoue } \
325a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3267d56d374SYoshinobu Inoue
3277d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \
3287d56d374SYoshinobu Inoue do { \
3297d56d374SYoshinobu Inoue /* external reference: error and label free */ \
3307d56d374SYoshinobu Inoue error = get_port((ai), (serv), 0); \
3317d56d374SYoshinobu Inoue if (error != 0) \
3327d56d374SYoshinobu Inoue goto free; \
333a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3347d56d374SYoshinobu Inoue
3357d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \
3367d56d374SYoshinobu Inoue do { \
3377d56d374SYoshinobu Inoue /* external reference: pai, error and label free */ \
3387d56d374SYoshinobu Inoue error = get_canonname(pai, (ai), (str)); \
3397d56d374SYoshinobu Inoue if (error != 0) \
3407d56d374SYoshinobu Inoue goto free; \
341a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3427d56d374SYoshinobu Inoue
3437d56d374SYoshinobu Inoue #define ERR(err) \
3447d56d374SYoshinobu Inoue do { \
3457d56d374SYoshinobu Inoue /* external reference: error, and label bad */ \
3467d56d374SYoshinobu Inoue error = (err); \
3477d56d374SYoshinobu Inoue goto bad; \
348a42af91cSHajimu UMEMOTO /*NOTREACHED*/ \
349a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3507d56d374SYoshinobu Inoue
3517d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \
352a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
3537d56d374SYoshinobu Inoue #define MATCH(x, y, w) \
354a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
3557d56d374SYoshinobu Inoue
3567d56d374SYoshinobu Inoue void
freeaddrinfo(struct addrinfo * ai)35790d1d883SHajimu UMEMOTO freeaddrinfo(struct addrinfo *ai)
3587d56d374SYoshinobu Inoue {
3597d56d374SYoshinobu Inoue struct addrinfo *next;
3607d56d374SYoshinobu Inoue
361587e285eSKonstantin Belousov while (ai != NULL) {
3627d56d374SYoshinobu Inoue next = ai->ai_next;
3637d56d374SYoshinobu Inoue free(ai->ai_canonname);
3647d56d374SYoshinobu Inoue /* no need to free(ai->ai_addr) */
3657d56d374SYoshinobu Inoue free(ai);
366a42af91cSHajimu UMEMOTO ai = next;
367587e285eSKonstantin Belousov }
3687d56d374SYoshinobu Inoue }
3697d56d374SYoshinobu Inoue
3707d56d374SYoshinobu Inoue static int
str2number(const char * p,int * portp)371e24e9d99SHajimu UMEMOTO str2number(const char *p, int *portp)
3727d56d374SYoshinobu Inoue {
373ec20fe00SJun-ichiro itojun Hagino char *ep;
374c224435eSHajimu UMEMOTO unsigned long v;
375ec20fe00SJun-ichiro itojun Hagino
376ec20fe00SJun-ichiro itojun Hagino if (*p == '\0')
377c224435eSHajimu UMEMOTO return -1;
378ec20fe00SJun-ichiro itojun Hagino ep = NULL;
379a89c1d30SHajimu UMEMOTO errno = 0;
380c224435eSHajimu UMEMOTO v = strtoul(p, &ep, 10);
381e24e9d99SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) {
382e24e9d99SHajimu UMEMOTO *portp = v;
383e24e9d99SHajimu UMEMOTO return 0;
384e24e9d99SHajimu UMEMOTO } else
385c224435eSHajimu UMEMOTO return -1;
3867d56d374SYoshinobu Inoue }
3877d56d374SYoshinobu Inoue
3887d56d374SYoshinobu Inoue int
getaddrinfo(const char * hostname,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)38990d1d883SHajimu UMEMOTO getaddrinfo(const char *hostname, const char *servname,
39090d1d883SHajimu UMEMOTO const struct addrinfo *hints, struct addrinfo **res)
3917d56d374SYoshinobu Inoue {
3927d56d374SYoshinobu Inoue struct addrinfo sentinel;
3937d56d374SYoshinobu Inoue struct addrinfo *cur;
3947d56d374SYoshinobu Inoue int error = 0;
395bc421492SHajimu UMEMOTO struct addrinfo ai, ai0, *afai;
3967d56d374SYoshinobu Inoue struct addrinfo *pai;
397bc421492SHajimu UMEMOTO const struct afd *afd;
3987d56d374SYoshinobu Inoue const struct explore *ex;
399bf51882aSPedro F. Giffuni struct addrinfo *afailist[nitems(afdl)];
400bc421492SHajimu UMEMOTO struct addrinfo *afai_unspec;
401bc421492SHajimu UMEMOTO int found;
4024c6867a8SHajimu UMEMOTO int numeric = 0;
4037d56d374SYoshinobu Inoue
404bc421492SHajimu UMEMOTO /* ensure we return NULL on errors */
405bc421492SHajimu UMEMOTO *res = NULL;
406bc421492SHajimu UMEMOTO
407bc421492SHajimu UMEMOTO memset(&ai, 0, sizeof(ai));
408bc421492SHajimu UMEMOTO
409bc421492SHajimu UMEMOTO memset(afailist, 0, sizeof(afailist));
410bc421492SHajimu UMEMOTO afai_unspec = NULL;
411bc421492SHajimu UMEMOTO
412a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel));
4137d56d374SYoshinobu Inoue cur = &sentinel;
4147d56d374SYoshinobu Inoue pai = &ai;
4157d56d374SYoshinobu Inoue pai->ai_flags = 0;
4167d56d374SYoshinobu Inoue pai->ai_family = PF_UNSPEC;
4177d56d374SYoshinobu Inoue pai->ai_socktype = ANY;
4187d56d374SYoshinobu Inoue pai->ai_protocol = ANY;
4197d56d374SYoshinobu Inoue pai->ai_addrlen = 0;
4207d56d374SYoshinobu Inoue pai->ai_canonname = NULL;
4217d56d374SYoshinobu Inoue pai->ai_addr = NULL;
4227d56d374SYoshinobu Inoue pai->ai_next = NULL;
4237d56d374SYoshinobu Inoue
4247d56d374SYoshinobu Inoue if (hostname == NULL && servname == NULL)
4257d56d374SYoshinobu Inoue return EAI_NONAME;
4267d56d374SYoshinobu Inoue if (hints) {
4277d56d374SYoshinobu Inoue /* error check for hints */
4287d56d374SYoshinobu Inoue if (hints->ai_addrlen || hints->ai_canonname ||
4297d56d374SYoshinobu Inoue hints->ai_addr || hints->ai_next)
4307d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); /* xxx */
4317d56d374SYoshinobu Inoue if (hints->ai_flags & ~AI_MASK)
4327d56d374SYoshinobu Inoue ERR(EAI_BADFLAGS);
4337d56d374SYoshinobu Inoue switch (hints->ai_family) {
4347d56d374SYoshinobu Inoue case PF_UNSPEC:
435bb957021SHiroki Sato case PF_LOCAL:
4367d56d374SYoshinobu Inoue case PF_INET:
4377d56d374SYoshinobu Inoue #ifdef INET6
4387d56d374SYoshinobu Inoue case PF_INET6:
4397d56d374SYoshinobu Inoue #endif
4407d56d374SYoshinobu Inoue break;
4417d56d374SYoshinobu Inoue default:
4427d56d374SYoshinobu Inoue ERR(EAI_FAMILY);
4437d56d374SYoshinobu Inoue }
4447d56d374SYoshinobu Inoue memcpy(pai, hints, sizeof(*pai));
4457d56d374SYoshinobu Inoue
4467d56d374SYoshinobu Inoue /*
4477d56d374SYoshinobu Inoue * if both socktype/protocol are specified, check if they
4487d56d374SYoshinobu Inoue * are meaningful combination.
4497d56d374SYoshinobu Inoue */
4507d56d374SYoshinobu Inoue if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
451a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) {
452bc421492SHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af,
453bc421492SHajimu UMEMOTO WILD_AF(ex)))
454a42af91cSHajimu UMEMOTO continue;
455bc421492SHajimu UMEMOTO if (!MATCH(pai->ai_socktype, ex->e_socktype,
456bc421492SHajimu UMEMOTO WILD_SOCKTYPE(ex)))
4577d56d374SYoshinobu Inoue continue;
458bc421492SHajimu UMEMOTO if (!MATCH(pai->ai_protocol, ex->e_protocol,
459bc421492SHajimu UMEMOTO WILD_PROTOCOL(ex)))
4607d56d374SYoshinobu Inoue continue;
461bc421492SHajimu UMEMOTO
462bc421492SHajimu UMEMOTO /* matched */
4634d756307SHajimu UMEMOTO break;
4644d756307SHajimu UMEMOTO }
4654d756307SHajimu UMEMOTO
4664d756307SHajimu UMEMOTO if (ex->e_af < 0)
4677d56d374SYoshinobu Inoue ERR(EAI_BADHINTS);
4687d56d374SYoshinobu Inoue }
4697d56d374SYoshinobu Inoue }
4707d56d374SYoshinobu Inoue
4717d56d374SYoshinobu Inoue /*
472b848a37dSHajimu UMEMOTO * RFC 3493: AI_ALL and AI_V4MAPPED are effective only against
473b848a37dSHajimu UMEMOTO * AF_INET6 query. They need to be ignored if specified in other
47432223c1bSPedro F. Giffuni * occasions.
475b848a37dSHajimu UMEMOTO */
476b848a37dSHajimu UMEMOTO switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
477b848a37dSHajimu UMEMOTO case AI_V4MAPPED:
478b848a37dSHajimu UMEMOTO case AI_ALL | AI_V4MAPPED:
479b848a37dSHajimu UMEMOTO #ifdef INET6
480b848a37dSHajimu UMEMOTO if (pai->ai_family != AF_INET6)
481b848a37dSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
482b848a37dSHajimu UMEMOTO break;
483b848a37dSHajimu UMEMOTO #endif
484b848a37dSHajimu UMEMOTO case AI_ALL:
485b848a37dSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
486b848a37dSHajimu UMEMOTO break;
487b848a37dSHajimu UMEMOTO }
488b848a37dSHajimu UMEMOTO
489b848a37dSHajimu UMEMOTO /*
490a42af91cSHajimu UMEMOTO * check for special cases. (1) numeric servname is disallowed if
491a42af91cSHajimu UMEMOTO * socktype/protocol are left unspecified. (2) servname is disallowed
492a42af91cSHajimu UMEMOTO * for raw and other inet{,6} sockets.
4937d56d374SYoshinobu Inoue */
4947d56d374SYoshinobu Inoue if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
495a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4967d56d374SYoshinobu Inoue || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
4977d56d374SYoshinobu Inoue #endif
4987d56d374SYoshinobu Inoue ) {
499a42af91cSHajimu UMEMOTO ai0 = *pai; /* backup *pai */
5007d56d374SYoshinobu Inoue
501a42af91cSHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC) {
502a42af91cSHajimu UMEMOTO #ifdef PF_INET6
5037d56d374SYoshinobu Inoue pai->ai_family = PF_INET6;
5047d56d374SYoshinobu Inoue #else
5057d56d374SYoshinobu Inoue pai->ai_family = PF_INET;
5067d56d374SYoshinobu Inoue #endif
507a42af91cSHajimu UMEMOTO }
5087d56d374SYoshinobu Inoue error = get_portmatch(pai, servname);
5097d56d374SYoshinobu Inoue if (error)
510f61ac9d9SDimitry Andric goto bad;
511a42af91cSHajimu UMEMOTO
512a42af91cSHajimu UMEMOTO *pai = ai0;
5137d56d374SYoshinobu Inoue }
5147d56d374SYoshinobu Inoue
515a42af91cSHajimu UMEMOTO ai0 = *pai;
516a42af91cSHajimu UMEMOTO
517bc421492SHajimu UMEMOTO /*
518bc421492SHajimu UMEMOTO * NULL hostname, or numeric hostname.
519bc421492SHajimu UMEMOTO * If numeric representation of AF1 can be interpreted as FQDN
520bc421492SHajimu UMEMOTO * representation of AF2, we need to think again about the code below.
521bc421492SHajimu UMEMOTO */
522bc421492SHajimu UMEMOTO found = 0;
523bc421492SHajimu UMEMOTO for (afd = afdl; afd->a_af; afd++) {
5247d56d374SYoshinobu Inoue *pai = ai0;
5257d56d374SYoshinobu Inoue
526bc421492SHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
5277d56d374SYoshinobu Inoue continue;
5287d56d374SYoshinobu Inoue
5297d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC)
530bc421492SHajimu UMEMOTO pai->ai_family = afd->a_af;
5317d56d374SYoshinobu Inoue
532bc421492SHajimu UMEMOTO if (hostname == NULL) {
533bc421492SHajimu UMEMOTO error = explore_null(pai, servname,
534bc421492SHajimu UMEMOTO &afailist[afd - afdl]);
5357d56d374SYoshinobu Inoue
536bf838688SHajimu UMEMOTO /*
537bc421492SHajimu UMEMOTO * Errors from explore_null should be unexpected and
538bc421492SHajimu UMEMOTO * be caught to avoid returning an incomplete result.
539bf838688SHajimu UMEMOTO */
540bc421492SHajimu UMEMOTO if (error != 0)
541bc421492SHajimu UMEMOTO goto bad;
542bc421492SHajimu UMEMOTO } else {
543bc421492SHajimu UMEMOTO error = explore_numeric_scope(pai, hostname, servname,
544bc421492SHajimu UMEMOTO &afailist[afd - afdl]);
545bc421492SHajimu UMEMOTO
546bc421492SHajimu UMEMOTO /*
547bc421492SHajimu UMEMOTO * explore_numeric_scope returns an error for address
548bc421492SHajimu UMEMOTO * families that do not match that of hostname.
549bc421492SHajimu UMEMOTO * Thus we should not catch the error at this moment.
550bc421492SHajimu UMEMOTO */
551bc421492SHajimu UMEMOTO }
552bc421492SHajimu UMEMOTO
553bc421492SHajimu UMEMOTO if (!error && afailist[afd - afdl])
554bc421492SHajimu UMEMOTO found++;
555bc421492SHajimu UMEMOTO }
556bc421492SHajimu UMEMOTO if (found) {
5574c6867a8SHajimu UMEMOTO numeric = 1;
558bc421492SHajimu UMEMOTO goto globcopy;
5594c6867a8SHajimu UMEMOTO }
560bf838688SHajimu UMEMOTO
561bf838688SHajimu UMEMOTO if (hostname == NULL)
562bf838688SHajimu UMEMOTO ERR(EAI_NONAME); /* used to be EAI_NODATA */
563bf838688SHajimu UMEMOTO if (pai->ai_flags & AI_NUMERICHOST)
564bf838688SHajimu UMEMOTO ERR(EAI_NONAME);
565bf838688SHajimu UMEMOTO
566bf838688SHajimu UMEMOTO if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
567bf838688SHajimu UMEMOTO ERR(EAI_FAIL);
568a42af91cSHajimu UMEMOTO
569f95d4633SHajimu UMEMOTO /*
570bf838688SHajimu UMEMOTO * hostname as alphabetical name.
571f95d4633SHajimu UMEMOTO */
572bc421492SHajimu UMEMOTO *pai = ai0;
573bc421492SHajimu UMEMOTO error = explore_fqdn(pai, hostname, servname, &afai_unspec);
574bc421492SHajimu UMEMOTO
575bc421492SHajimu UMEMOTO globcopy:
576bf838688SHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) {
577bf838688SHajimu UMEMOTO *pai = ai0;
578f95d4633SHajimu UMEMOTO
579bc421492SHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
580f95d4633SHajimu UMEMOTO continue;
581bf838688SHajimu UMEMOTO if (!MATCH(pai->ai_socktype, ex->e_socktype,
582bc421492SHajimu UMEMOTO WILD_SOCKTYPE(ex)))
583bf838688SHajimu UMEMOTO continue;
584bf838688SHajimu UMEMOTO if (!MATCH(pai->ai_protocol, ex->e_protocol,
585bc421492SHajimu UMEMOTO WILD_PROTOCOL(ex)))
586bf838688SHajimu UMEMOTO continue;
587f95d4633SHajimu UMEMOTO
588bc421492SHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC)
589bc421492SHajimu UMEMOTO pai->ai_family = ex->e_af;
590bf838688SHajimu UMEMOTO if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
591bf838688SHajimu UMEMOTO pai->ai_socktype = ex->e_socktype;
592bf838688SHajimu UMEMOTO if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
593bf838688SHajimu UMEMOTO pai->ai_protocol = ex->e_protocol;
594bf838688SHajimu UMEMOTO
595bc421492SHajimu UMEMOTO /*
596bc421492SHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it.
597bc421492SHajimu UMEMOTO */
598bc421492SHajimu UMEMOTO if (get_portmatch(pai, servname) != 0)
599bc421492SHajimu UMEMOTO continue;
600bc421492SHajimu UMEMOTO
601bc421492SHajimu UMEMOTO if (afai_unspec)
602bc421492SHajimu UMEMOTO afai = afai_unspec;
603bc421492SHajimu UMEMOTO else {
604bc421492SHajimu UMEMOTO if ((afd = find_afd(pai->ai_family)) == NULL)
605bc421492SHajimu UMEMOTO continue;
606bc421492SHajimu UMEMOTO /* XXX assumes that afd points inside afdl[] */
607bc421492SHajimu UMEMOTO afai = afailist[afd - afdl];
608bc421492SHajimu UMEMOTO }
609bc421492SHajimu UMEMOTO if (!afai)
610bc421492SHajimu UMEMOTO continue;
611bc421492SHajimu UMEMOTO
612bc421492SHajimu UMEMOTO error = explore_copy(pai, afai, &cur->ai_next);
613bc421492SHajimu UMEMOTO if (error != 0)
614bc421492SHajimu UMEMOTO goto bad;
615bf838688SHajimu UMEMOTO
616bf838688SHajimu UMEMOTO while (cur && cur->ai_next)
617f95d4633SHajimu UMEMOTO cur = cur->ai_next;
618f95d4633SHajimu UMEMOTO }
619f95d4633SHajimu UMEMOTO
6204c6867a8SHajimu UMEMOTO /*
6214c6867a8SHajimu UMEMOTO * ensure we return either:
6224c6867a8SHajimu UMEMOTO * - error == 0, non-NULL *res
6234c6867a8SHajimu UMEMOTO * - error != 0, NULL *res
6244c6867a8SHajimu UMEMOTO */
625bf838688SHajimu UMEMOTO if (error == 0) {
626bf838688SHajimu UMEMOTO if (sentinel.ai_next) {
6274c6867a8SHajimu UMEMOTO /*
6284c6867a8SHajimu UMEMOTO * If the returned entry is for an active connection,
6294c6867a8SHajimu UMEMOTO * and the given name is not numeric, reorder the
6304c6867a8SHajimu UMEMOTO * list, so that the application would try the list
631e47c82edSJINMEI Tatuya * in the most efficient order. Since the head entry
632e47c82edSJINMEI Tatuya * of the original list may contain ai_canonname and
633e47c82edSJINMEI Tatuya * that entry may be moved elsewhere in the new list,
634e47c82edSJINMEI Tatuya * we keep the pointer and will restore it in the new
635e47c82edSJINMEI Tatuya * head entry. (Note that RFC3493 requires the head
636e47c82edSJINMEI Tatuya * entry store it when requested by the caller).
6374c6867a8SHajimu UMEMOTO */
6384c6867a8SHajimu UMEMOTO if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
639e47c82edSJINMEI Tatuya if (!numeric) {
640e47c82edSJINMEI Tatuya char *canonname;
641e47c82edSJINMEI Tatuya
642e47c82edSJINMEI Tatuya canonname =
643e47c82edSJINMEI Tatuya sentinel.ai_next->ai_canonname;
644e47c82edSJINMEI Tatuya sentinel.ai_next->ai_canonname = NULL;
6454c6867a8SHajimu UMEMOTO (void)reorder(&sentinel);
646e47c82edSJINMEI Tatuya if (sentinel.ai_next->ai_canonname ==
647e47c82edSJINMEI Tatuya NULL) {
648e47c82edSJINMEI Tatuya sentinel.ai_next->ai_canonname
649e47c82edSJINMEI Tatuya = canonname;
650e47c82edSJINMEI Tatuya } else if (canonname != NULL)
651e47c82edSJINMEI Tatuya free(canonname);
652e47c82edSJINMEI Tatuya }
6534c6867a8SHajimu UMEMOTO }
654bf838688SHajimu UMEMOTO *res = sentinel.ai_next;
655bf838688SHajimu UMEMOTO } else
656bf838688SHajimu UMEMOTO error = EAI_FAIL;
657bf838688SHajimu UMEMOTO }
658bc421492SHajimu UMEMOTO
659bf838688SHajimu UMEMOTO bad:
660bc421492SHajimu UMEMOTO if (afai_unspec)
661bc421492SHajimu UMEMOTO freeaddrinfo(afai_unspec);
662bc421492SHajimu UMEMOTO for (afd = afdl; afd->a_af; afd++) {
663bc421492SHajimu UMEMOTO if (afailist[afd - afdl])
664bc421492SHajimu UMEMOTO freeaddrinfo(afailist[afd - afdl]);
665bc421492SHajimu UMEMOTO }
666bc421492SHajimu UMEMOTO if (!*res)
667bf838688SHajimu UMEMOTO if (sentinel.ai_next)
668f95d4633SHajimu UMEMOTO freeaddrinfo(sentinel.ai_next);
669bc421492SHajimu UMEMOTO
670bc421492SHajimu UMEMOTO return (error);
6717d56d374SYoshinobu Inoue }
6727d56d374SYoshinobu Inoue
6734c6867a8SHajimu UMEMOTO static int
reorder(struct addrinfo * sentinel)67490d1d883SHajimu UMEMOTO reorder(struct addrinfo *sentinel)
6754c6867a8SHajimu UMEMOTO {
6764c6867a8SHajimu UMEMOTO struct addrinfo *ai, **aip;
6774c6867a8SHajimu UMEMOTO struct ai_order *aio;
6784c6867a8SHajimu UMEMOTO int i, n;
6794c6867a8SHajimu UMEMOTO struct policyhead policyhead;
6804c6867a8SHajimu UMEMOTO
6814c6867a8SHajimu UMEMOTO /* count the number of addrinfo elements for sorting. */
6824c6867a8SHajimu UMEMOTO for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
6834c6867a8SHajimu UMEMOTO ;
6844c6867a8SHajimu UMEMOTO
6854c6867a8SHajimu UMEMOTO /*
6864c6867a8SHajimu UMEMOTO * If the number is small enough, we can skip the reordering process.
6874c6867a8SHajimu UMEMOTO */
6884c6867a8SHajimu UMEMOTO if (n <= 1)
6894c6867a8SHajimu UMEMOTO return(n);
6904c6867a8SHajimu UMEMOTO
6914c6867a8SHajimu UMEMOTO /* allocate a temporary array for sort and initialization of it. */
69237950668SPedro F. Giffuni if ((aio = calloc(n, sizeof(*aio))) == NULL)
6934c6867a8SHajimu UMEMOTO return(n); /* give up reordering */
6944c6867a8SHajimu UMEMOTO
6954c6867a8SHajimu UMEMOTO /* retrieve address selection policy from the kernel */
6964c6867a8SHajimu UMEMOTO TAILQ_INIT(&policyhead);
6974c6867a8SHajimu UMEMOTO if (!get_addrselectpolicy(&policyhead)) {
6984c6867a8SHajimu UMEMOTO /* no policy is installed into kernel, we don't sort. */
6994c6867a8SHajimu UMEMOTO free(aio);
7004c6867a8SHajimu UMEMOTO return (n);
7014c6867a8SHajimu UMEMOTO }
7024c6867a8SHajimu UMEMOTO
7034c6867a8SHajimu UMEMOTO for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
7044c6867a8SHajimu UMEMOTO aio[i].aio_ai = ai;
7054c6867a8SHajimu UMEMOTO aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
7064c6867a8SHajimu UMEMOTO aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
7074c6867a8SHajimu UMEMOTO &policyhead);
7084d489f47SHajimu UMEMOTO set_source(&aio[i], &policyhead);
709ebbc4458SAndrey A. Chernov aio[i].aio_initial_sequence = i;
7104c6867a8SHajimu UMEMOTO }
7114c6867a8SHajimu UMEMOTO
7124c6867a8SHajimu UMEMOTO /* perform sorting. */
7134c6867a8SHajimu UMEMOTO qsort(aio, n, sizeof(*aio), comp_dst);
7144c6867a8SHajimu UMEMOTO
7154c6867a8SHajimu UMEMOTO /* reorder the addrinfo chain. */
7164c6867a8SHajimu UMEMOTO for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
7174c6867a8SHajimu UMEMOTO *aip = aio[i].aio_ai;
7184c6867a8SHajimu UMEMOTO aip = &aio[i].aio_ai->ai_next;
7194c6867a8SHajimu UMEMOTO }
7204c6867a8SHajimu UMEMOTO *aip = NULL;
7214c6867a8SHajimu UMEMOTO
7224c6867a8SHajimu UMEMOTO /* cleanup and return */
7234c6867a8SHajimu UMEMOTO free(aio);
7244c6867a8SHajimu UMEMOTO free_addrselectpolicy(&policyhead);
7254c6867a8SHajimu UMEMOTO return(n);
7264c6867a8SHajimu UMEMOTO }
7274c6867a8SHajimu UMEMOTO
7284c6867a8SHajimu UMEMOTO static int
get_addrselectpolicy(struct policyhead * head)72990d1d883SHajimu UMEMOTO get_addrselectpolicy(struct policyhead *head)
7304c6867a8SHajimu UMEMOTO {
7314c6867a8SHajimu UMEMOTO #ifdef INET6
7324c6867a8SHajimu UMEMOTO int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
7334c6867a8SHajimu UMEMOTO size_t l;
7344c6867a8SHajimu UMEMOTO char *buf;
7354c6867a8SHajimu UMEMOTO struct in6_addrpolicy *pol, *ep;
7364c6867a8SHajimu UMEMOTO
737bf51882aSPedro F. Giffuni if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0)
7384c6867a8SHajimu UMEMOTO return (0);
7390c19b5deSMaksim Yevmenkin if (l == 0)
7400c19b5deSMaksim Yevmenkin return (0);
7414c6867a8SHajimu UMEMOTO if ((buf = malloc(l)) == NULL)
7424c6867a8SHajimu UMEMOTO return (0);
743bf51882aSPedro F. Giffuni if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
7444c6867a8SHajimu UMEMOTO free(buf);
7454c6867a8SHajimu UMEMOTO return (0);
7464c6867a8SHajimu UMEMOTO }
7474c6867a8SHajimu UMEMOTO
7484c6867a8SHajimu UMEMOTO ep = (struct in6_addrpolicy *)(buf + l);
7494c6867a8SHajimu UMEMOTO for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
7504c6867a8SHajimu UMEMOTO struct policyqueue *new;
7514c6867a8SHajimu UMEMOTO
7524c6867a8SHajimu UMEMOTO if ((new = malloc(sizeof(*new))) == NULL) {
7534c6867a8SHajimu UMEMOTO free_addrselectpolicy(head); /* make the list empty */
7544c6867a8SHajimu UMEMOTO break;
7554c6867a8SHajimu UMEMOTO }
7564c6867a8SHajimu UMEMOTO new->pc_policy = *pol;
7574c6867a8SHajimu UMEMOTO TAILQ_INSERT_TAIL(head, new, pc_entry);
7584c6867a8SHajimu UMEMOTO }
7594c6867a8SHajimu UMEMOTO
7604c6867a8SHajimu UMEMOTO free(buf);
7614c6867a8SHajimu UMEMOTO return (1);
7624c6867a8SHajimu UMEMOTO #else
7634c6867a8SHajimu UMEMOTO return (0);
7644c6867a8SHajimu UMEMOTO #endif
7654c6867a8SHajimu UMEMOTO }
7664c6867a8SHajimu UMEMOTO
7674c6867a8SHajimu UMEMOTO static void
free_addrselectpolicy(struct policyhead * head)76890d1d883SHajimu UMEMOTO free_addrselectpolicy(struct policyhead *head)
7694c6867a8SHajimu UMEMOTO {
7704c6867a8SHajimu UMEMOTO struct policyqueue *ent, *nent;
7714c6867a8SHajimu UMEMOTO
7724c6867a8SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = nent) {
7734c6867a8SHajimu UMEMOTO nent = TAILQ_NEXT(ent, pc_entry);
7744c6867a8SHajimu UMEMOTO TAILQ_REMOVE(head, ent, pc_entry);
7754c6867a8SHajimu UMEMOTO free(ent);
7764c6867a8SHajimu UMEMOTO }
7774c6867a8SHajimu UMEMOTO }
7784c6867a8SHajimu UMEMOTO
7794c6867a8SHajimu UMEMOTO static struct policyqueue *
match_addrselectpolicy(struct sockaddr * addr,struct policyhead * head)78090d1d883SHajimu UMEMOTO match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
7814c6867a8SHajimu UMEMOTO {
7824c6867a8SHajimu UMEMOTO #ifdef INET6
7834c6867a8SHajimu UMEMOTO struct policyqueue *ent, *bestent = NULL;
7844c6867a8SHajimu UMEMOTO struct in6_addrpolicy *pol;
7854c6867a8SHajimu UMEMOTO int matchlen, bestmatchlen = -1;
7864c6867a8SHajimu UMEMOTO u_char *mp, *ep, *k, *p, m;
7874c6867a8SHajimu UMEMOTO struct sockaddr_in6 key;
7884c6867a8SHajimu UMEMOTO
7894c6867a8SHajimu UMEMOTO switch(addr->sa_family) {
7904c6867a8SHajimu UMEMOTO case AF_INET6:
7914c6867a8SHajimu UMEMOTO key = *(struct sockaddr_in6 *)addr;
7924c6867a8SHajimu UMEMOTO break;
7934c6867a8SHajimu UMEMOTO case AF_INET:
7944c6867a8SHajimu UMEMOTO /* convert the address into IPv4-mapped IPv6 address. */
7954c6867a8SHajimu UMEMOTO memset(&key, 0, sizeof(key));
7964c6867a8SHajimu UMEMOTO key.sin6_family = AF_INET6;
7974c6867a8SHajimu UMEMOTO key.sin6_len = sizeof(key);
798844eacfeSHajimu UMEMOTO _map_v4v6_address(
799844eacfeSHajimu UMEMOTO (char *)&((struct sockaddr_in *)addr)->sin_addr,
800844eacfeSHajimu UMEMOTO (char *)&key.sin6_addr);
8014c6867a8SHajimu UMEMOTO break;
8024c6867a8SHajimu UMEMOTO default:
8034c6867a8SHajimu UMEMOTO return(NULL);
8044c6867a8SHajimu UMEMOTO }
8054c6867a8SHajimu UMEMOTO
8064c6867a8SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
8074c6867a8SHajimu UMEMOTO pol = &ent->pc_policy;
8084c6867a8SHajimu UMEMOTO matchlen = 0;
8094c6867a8SHajimu UMEMOTO
8104c6867a8SHajimu UMEMOTO mp = (u_char *)&pol->addrmask.sin6_addr;
8114c6867a8SHajimu UMEMOTO ep = mp + 16; /* XXX: scope field? */
8124c6867a8SHajimu UMEMOTO k = (u_char *)&key.sin6_addr;
8134c6867a8SHajimu UMEMOTO p = (u_char *)&pol->addr.sin6_addr;
8144c6867a8SHajimu UMEMOTO for (; mp < ep && *mp; mp++, k++, p++) {
8154c6867a8SHajimu UMEMOTO m = *mp;
8164c6867a8SHajimu UMEMOTO if ((*k & m) != *p)
8174c6867a8SHajimu UMEMOTO goto next; /* not match */
8184c6867a8SHajimu UMEMOTO if (m == 0xff) /* short cut for a typical case */
8194c6867a8SHajimu UMEMOTO matchlen += 8;
8204c6867a8SHajimu UMEMOTO else {
8214c6867a8SHajimu UMEMOTO while (m >= 0x80) {
8224c6867a8SHajimu UMEMOTO matchlen++;
8234c6867a8SHajimu UMEMOTO m <<= 1;
8244c6867a8SHajimu UMEMOTO }
8254c6867a8SHajimu UMEMOTO }
8264c6867a8SHajimu UMEMOTO }
8274c6867a8SHajimu UMEMOTO
8284c6867a8SHajimu UMEMOTO /* matched. check if this is better than the current best. */
8294c6867a8SHajimu UMEMOTO if (matchlen > bestmatchlen) {
8304c6867a8SHajimu UMEMOTO bestent = ent;
8314c6867a8SHajimu UMEMOTO bestmatchlen = matchlen;
8324c6867a8SHajimu UMEMOTO }
8334c6867a8SHajimu UMEMOTO
8344c6867a8SHajimu UMEMOTO next:
8354c6867a8SHajimu UMEMOTO continue;
8364c6867a8SHajimu UMEMOTO }
8374c6867a8SHajimu UMEMOTO
8384c6867a8SHajimu UMEMOTO return(bestent);
8394c6867a8SHajimu UMEMOTO #else
8404c6867a8SHajimu UMEMOTO return(NULL);
8414c6867a8SHajimu UMEMOTO #endif
8424c6867a8SHajimu UMEMOTO
8434c6867a8SHajimu UMEMOTO }
8444c6867a8SHajimu UMEMOTO
8454d489f47SHajimu UMEMOTO static void
set_source(struct ai_order * aio,struct policyhead * ph)84690d1d883SHajimu UMEMOTO set_source(struct ai_order *aio, struct policyhead *ph)
8474d489f47SHajimu UMEMOTO {
8484d489f47SHajimu UMEMOTO struct addrinfo ai = *aio->aio_ai;
8494d489f47SHajimu UMEMOTO struct sockaddr_storage ss;
850943db5a2SHajimu UMEMOTO socklen_t srclen;
851943db5a2SHajimu UMEMOTO int s;
8524d489f47SHajimu UMEMOTO
8534d489f47SHajimu UMEMOTO /* set unspec ("no source is available"), just in case */
8544d489f47SHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC;
8554d489f47SHajimu UMEMOTO aio->aio_srcscope = -1;
8564d489f47SHajimu UMEMOTO
8574d489f47SHajimu UMEMOTO switch(ai.ai_family) {
8584d489f47SHajimu UMEMOTO case AF_INET:
8594d489f47SHajimu UMEMOTO #ifdef INET6
8604d489f47SHajimu UMEMOTO case AF_INET6:
8614d489f47SHajimu UMEMOTO #endif
8624d489f47SHajimu UMEMOTO break;
8634d489f47SHajimu UMEMOTO default: /* ignore unsupported AFs explicitly */
8644d489f47SHajimu UMEMOTO return;
8654d489f47SHajimu UMEMOTO }
8664d489f47SHajimu UMEMOTO
8674d489f47SHajimu UMEMOTO /* XXX: make a dummy addrinfo to call connect() */
8684d489f47SHajimu UMEMOTO ai.ai_socktype = SOCK_DGRAM;
8694d489f47SHajimu UMEMOTO ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
8704d489f47SHajimu UMEMOTO ai.ai_next = NULL;
8714d489f47SHajimu UMEMOTO memset(&ss, 0, sizeof(ss));
8724d489f47SHajimu UMEMOTO memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
8734d489f47SHajimu UMEMOTO ai.ai_addr = (struct sockaddr *)&ss;
8744d489f47SHajimu UMEMOTO get_port(&ai, "1", 0);
8754d489f47SHajimu UMEMOTO
8764d489f47SHajimu UMEMOTO /* open a socket to get the source address for the given dst */
87772531978SJilles Tjoelker if ((s = _socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC,
87872531978SJilles Tjoelker ai.ai_protocol)) < 0)
8794d489f47SHajimu UMEMOTO return; /* give up */
880b848a37dSHajimu UMEMOTO #ifdef INET6
881b848a37dSHajimu UMEMOTO if (ai.ai_family == AF_INET6) {
882b848a37dSHajimu UMEMOTO struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai.ai_addr;
883b848a37dSHajimu UMEMOTO int off = 0;
884b848a37dSHajimu UMEMOTO
885b848a37dSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
886b848a37dSHajimu UMEMOTO (void)_setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
887b848a37dSHajimu UMEMOTO (char *)&off, sizeof(off));
888b848a37dSHajimu UMEMOTO }
889b848a37dSHajimu UMEMOTO #endif
8904d489f47SHajimu UMEMOTO if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
8914d489f47SHajimu UMEMOTO goto cleanup;
8924d489f47SHajimu UMEMOTO srclen = ai.ai_addrlen;
8934d489f47SHajimu UMEMOTO if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
8944d489f47SHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC;
8954d489f47SHajimu UMEMOTO goto cleanup;
8964d489f47SHajimu UMEMOTO }
8974d489f47SHajimu UMEMOTO aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
8984d489f47SHajimu UMEMOTO aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
8994d489f47SHajimu UMEMOTO aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
9004d489f47SHajimu UMEMOTO #ifdef INET6
9014d489f47SHajimu UMEMOTO if (ai.ai_family == AF_INET6) {
9024d489f47SHajimu UMEMOTO struct in6_ifreq ifr6;
9034d489f47SHajimu UMEMOTO u_int32_t flags6;
9044d489f47SHajimu UMEMOTO
9054d489f47SHajimu UMEMOTO memset(&ifr6, 0, sizeof(ifr6));
9064d489f47SHajimu UMEMOTO memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
9074d489f47SHajimu UMEMOTO if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
9084d489f47SHajimu UMEMOTO flags6 = ifr6.ifr_ifru.ifru_flags6;
9094d489f47SHajimu UMEMOTO if ((flags6 & IN6_IFF_DEPRECATED))
9104d489f47SHajimu UMEMOTO aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
9114d489f47SHajimu UMEMOTO }
9124d489f47SHajimu UMEMOTO }
9134d489f47SHajimu UMEMOTO #endif
9144d489f47SHajimu UMEMOTO
9154d489f47SHajimu UMEMOTO cleanup:
9164d489f47SHajimu UMEMOTO _close(s);
9174d489f47SHajimu UMEMOTO return;
9184d489f47SHajimu UMEMOTO }
9194d489f47SHajimu UMEMOTO
9204d489f47SHajimu UMEMOTO static int
matchlen(struct sockaddr * src,struct sockaddr * dst)92190d1d883SHajimu UMEMOTO matchlen(struct sockaddr *src, struct sockaddr *dst)
9224d489f47SHajimu UMEMOTO {
9234d489f47SHajimu UMEMOTO int match = 0;
9244d489f47SHajimu UMEMOTO u_char *s, *d;
9254d489f47SHajimu UMEMOTO u_char *lim, r;
9264d489f47SHajimu UMEMOTO int addrlen;
9274d489f47SHajimu UMEMOTO
9284d489f47SHajimu UMEMOTO switch (src->sa_family) {
9294d489f47SHajimu UMEMOTO #ifdef INET6
9304d489f47SHajimu UMEMOTO case AF_INET6:
9314d489f47SHajimu UMEMOTO s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
9324d489f47SHajimu UMEMOTO d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
9334d489f47SHajimu UMEMOTO addrlen = sizeof(struct in6_addr);
9344d489f47SHajimu UMEMOTO lim = s + addrlen;
9354d489f47SHajimu UMEMOTO break;
9364d489f47SHajimu UMEMOTO #endif
9374d489f47SHajimu UMEMOTO case AF_INET:
938fd57e549SGeorge V. Neville-Neil s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
939fd57e549SGeorge V. Neville-Neil d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
9404d489f47SHajimu UMEMOTO addrlen = sizeof(struct in_addr);
9414d489f47SHajimu UMEMOTO lim = s + addrlen;
9424d489f47SHajimu UMEMOTO break;
9434d489f47SHajimu UMEMOTO default:
9444d489f47SHajimu UMEMOTO return(0);
9454d489f47SHajimu UMEMOTO }
9464d489f47SHajimu UMEMOTO
9474d489f47SHajimu UMEMOTO while (s < lim)
9484d489f47SHajimu UMEMOTO if ((r = (*d++ ^ *s++)) != 0) {
949e1ec67bbSAndrey A. Chernov while ((r & 0x80) == 0) {
9504d489f47SHajimu UMEMOTO match++;
9514d489f47SHajimu UMEMOTO r <<= 1;
9524d489f47SHajimu UMEMOTO }
9534d489f47SHajimu UMEMOTO break;
9544d489f47SHajimu UMEMOTO } else
9554d489f47SHajimu UMEMOTO match += 8;
9564d489f47SHajimu UMEMOTO return(match);
9574d489f47SHajimu UMEMOTO }
9584d489f47SHajimu UMEMOTO
9594c6867a8SHajimu UMEMOTO static int
comp_dst(const void * arg1,const void * arg2)96090d1d883SHajimu UMEMOTO comp_dst(const void *arg1, const void *arg2)
9614c6867a8SHajimu UMEMOTO {
9624c6867a8SHajimu UMEMOTO const struct ai_order *dst1 = arg1, *dst2 = arg2;
9634c6867a8SHajimu UMEMOTO
9644c6867a8SHajimu UMEMOTO /*
9654c6867a8SHajimu UMEMOTO * Rule 1: Avoid unusable destinations.
9664c6867a8SHajimu UMEMOTO * XXX: we currently do not consider if an appropriate route exists.
9674c6867a8SHajimu UMEMOTO */
9684c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
9694c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family == AF_UNSPEC) {
9704c6867a8SHajimu UMEMOTO return(-1);
9714c6867a8SHajimu UMEMOTO }
9724c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
9734c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9744c6867a8SHajimu UMEMOTO return(1);
9754c6867a8SHajimu UMEMOTO }
9764c6867a8SHajimu UMEMOTO
9774c6867a8SHajimu UMEMOTO /* Rule 2: Prefer matching scope. */
9784c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope == dst1->aio_srcscope &&
9794c6867a8SHajimu UMEMOTO dst2->aio_dstscope != dst2->aio_srcscope) {
9804c6867a8SHajimu UMEMOTO return(-1);
9814c6867a8SHajimu UMEMOTO }
9824c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope != dst1->aio_srcscope &&
9834c6867a8SHajimu UMEMOTO dst2->aio_dstscope == dst2->aio_srcscope) {
9844c6867a8SHajimu UMEMOTO return(1);
9854c6867a8SHajimu UMEMOTO }
9864c6867a8SHajimu UMEMOTO
9874c6867a8SHajimu UMEMOTO /* Rule 3: Avoid deprecated addresses. */
9884c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
9894c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9904c6867a8SHajimu UMEMOTO if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9914c6867a8SHajimu UMEMOTO (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9924c6867a8SHajimu UMEMOTO return(-1);
9934c6867a8SHajimu UMEMOTO }
9944c6867a8SHajimu UMEMOTO if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9954c6867a8SHajimu UMEMOTO !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9964c6867a8SHajimu UMEMOTO return(1);
9974c6867a8SHajimu UMEMOTO }
9984c6867a8SHajimu UMEMOTO }
9994c6867a8SHajimu UMEMOTO
10004c6867a8SHajimu UMEMOTO /* Rule 4: Prefer home addresses. */
10014c6867a8SHajimu UMEMOTO /* XXX: not implemented yet */
10024c6867a8SHajimu UMEMOTO
10034c6867a8SHajimu UMEMOTO /* Rule 5: Prefer matching label. */
10044c6867a8SHajimu UMEMOTO #ifdef INET6
10054c6867a8SHajimu UMEMOTO if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
10064c6867a8SHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label ==
10074c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label &&
10084c6867a8SHajimu UMEMOTO (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
10094c6867a8SHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label !=
10104c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label)) {
10114c6867a8SHajimu UMEMOTO return(-1);
10124c6867a8SHajimu UMEMOTO }
10134c6867a8SHajimu UMEMOTO if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
10144c6867a8SHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label ==
10154c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label &&
10164c6867a8SHajimu UMEMOTO (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
10174c6867a8SHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label !=
10184c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label)) {
10194c6867a8SHajimu UMEMOTO return(1);
10204c6867a8SHajimu UMEMOTO }
10214c6867a8SHajimu UMEMOTO #endif
10224c6867a8SHajimu UMEMOTO
10234c6867a8SHajimu UMEMOTO /* Rule 6: Prefer higher precedence. */
10244c6867a8SHajimu UMEMOTO #ifdef INET6
10254c6867a8SHajimu UMEMOTO if (dst1->aio_dstpolicy &&
10264c6867a8SHajimu UMEMOTO (dst2->aio_dstpolicy == NULL ||
10274c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced >
10284c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced)) {
10294c6867a8SHajimu UMEMOTO return(-1);
10304c6867a8SHajimu UMEMOTO }
10314c6867a8SHajimu UMEMOTO if (dst2->aio_dstpolicy &&
10324c6867a8SHajimu UMEMOTO (dst1->aio_dstpolicy == NULL ||
10334c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced >
10344c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced)) {
10354c6867a8SHajimu UMEMOTO return(1);
10364c6867a8SHajimu UMEMOTO }
10374c6867a8SHajimu UMEMOTO #endif
10384c6867a8SHajimu UMEMOTO
10394c6867a8SHajimu UMEMOTO /* Rule 7: Prefer native transport. */
10404c6867a8SHajimu UMEMOTO /* XXX: not implemented yet */
10414c6867a8SHajimu UMEMOTO
10424c6867a8SHajimu UMEMOTO /* Rule 8: Prefer smaller scope. */
10434c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope >= 0 &&
10444c6867a8SHajimu UMEMOTO dst1->aio_dstscope < dst2->aio_dstscope) {
10454c6867a8SHajimu UMEMOTO return(-1);
10464c6867a8SHajimu UMEMOTO }
10474c6867a8SHajimu UMEMOTO if (dst2->aio_dstscope >= 0 &&
10484c6867a8SHajimu UMEMOTO dst2->aio_dstscope < dst1->aio_dstscope) {
10494c6867a8SHajimu UMEMOTO return(1);
10504c6867a8SHajimu UMEMOTO }
10514c6867a8SHajimu UMEMOTO
10524c6867a8SHajimu UMEMOTO /*
10534c6867a8SHajimu UMEMOTO * Rule 9: Use longest matching prefix.
10544c6867a8SHajimu UMEMOTO * We compare the match length in a same AF only.
10554c6867a8SHajimu UMEMOTO */
10564c6867a8SHajimu UMEMOTO if (dst1->aio_ai->ai_addr->sa_family ==
105729078909SHajimu UMEMOTO dst2->aio_ai->ai_addr->sa_family &&
105829078909SHajimu UMEMOTO dst1->aio_ai->ai_addr->sa_family != AF_INET) {
10594c6867a8SHajimu UMEMOTO if (dst1->aio_matchlen > dst2->aio_matchlen) {
10604c6867a8SHajimu UMEMOTO return(-1);
10614c6867a8SHajimu UMEMOTO }
10624c6867a8SHajimu UMEMOTO if (dst1->aio_matchlen < dst2->aio_matchlen) {
10634c6867a8SHajimu UMEMOTO return(1);
10644c6867a8SHajimu UMEMOTO }
10654c6867a8SHajimu UMEMOTO }
10664c6867a8SHajimu UMEMOTO
10674c6867a8SHajimu UMEMOTO /* Rule 10: Otherwise, leave the order unchanged. */
1068ebbc4458SAndrey A. Chernov
1069ebbc4458SAndrey A. Chernov /*
1070ebbc4458SAndrey A. Chernov * Note that qsort is unstable; so, we can't return zero and
1071ebbc4458SAndrey A. Chernov * expect the order to be unchanged.
1072ebbc4458SAndrey A. Chernov * That also means we can't depend on the current position of
1073ebbc4458SAndrey A. Chernov * dst2 being after dst1. We must enforce the initial order
1074ebbc4458SAndrey A. Chernov * with an explicit compare on the original position.
1075ebbc4458SAndrey A. Chernov * The qsort specification requires that "When the same objects
1076ebbc4458SAndrey A. Chernov * (consisting of width bytes, irrespective of their current
1077ebbc4458SAndrey A. Chernov * positions in the array) are passed more than once to the
1078ebbc4458SAndrey A. Chernov * comparison function, the results shall be consistent with one
1079ebbc4458SAndrey A. Chernov * another."
1080ebbc4458SAndrey A. Chernov * In other words, If A < B, then we must also return B > A.
1081ebbc4458SAndrey A. Chernov */
1082ebbc4458SAndrey A. Chernov if (dst2->aio_initial_sequence < dst1->aio_initial_sequence)
1083ebbc4458SAndrey A. Chernov return(1);
1084ebbc4458SAndrey A. Chernov
10854c6867a8SHajimu UMEMOTO return(-1);
10864c6867a8SHajimu UMEMOTO }
10874c6867a8SHajimu UMEMOTO
10884c6867a8SHajimu UMEMOTO /*
10894c6867a8SHajimu UMEMOTO * Copy from scope.c.
10904c6867a8SHajimu UMEMOTO * XXX: we should standardize the functions and link them as standard
10914c6867a8SHajimu UMEMOTO * library.
10924c6867a8SHajimu UMEMOTO */
10934c6867a8SHajimu UMEMOTO static int
gai_addr2scopetype(struct sockaddr * sa)109490d1d883SHajimu UMEMOTO gai_addr2scopetype(struct sockaddr *sa)
10954c6867a8SHajimu UMEMOTO {
10964c6867a8SHajimu UMEMOTO #ifdef INET6
10974c6867a8SHajimu UMEMOTO struct sockaddr_in6 *sa6;
10984c6867a8SHajimu UMEMOTO #endif
10994c6867a8SHajimu UMEMOTO struct sockaddr_in *sa4;
11004c6867a8SHajimu UMEMOTO
11014c6867a8SHajimu UMEMOTO switch(sa->sa_family) {
11024c6867a8SHajimu UMEMOTO #ifdef INET6
11034c6867a8SHajimu UMEMOTO case AF_INET6:
11044c6867a8SHajimu UMEMOTO sa6 = (struct sockaddr_in6 *)sa;
11054c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
11064c6867a8SHajimu UMEMOTO /* just use the scope field of the multicast address */
11074c6867a8SHajimu UMEMOTO return(sa6->sin6_addr.s6_addr[2] & 0x0f);
11084c6867a8SHajimu UMEMOTO }
11094c6867a8SHajimu UMEMOTO /*
11104c6867a8SHajimu UMEMOTO * Unicast addresses: map scope type to corresponding scope
11114c6867a8SHajimu UMEMOTO * value defined for multcast addresses.
11124c6867a8SHajimu UMEMOTO * XXX: hardcoded scope type values are bad...
11134c6867a8SHajimu UMEMOTO */
11144c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
11154c6867a8SHajimu UMEMOTO return(1); /* node local scope */
11164c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
11174c6867a8SHajimu UMEMOTO return(2); /* link-local scope */
11184c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
11194c6867a8SHajimu UMEMOTO return(5); /* site-local scope */
11204c6867a8SHajimu UMEMOTO return(14); /* global scope */
11214c6867a8SHajimu UMEMOTO break;
11224c6867a8SHajimu UMEMOTO #endif
11234c6867a8SHajimu UMEMOTO case AF_INET:
11244c6867a8SHajimu UMEMOTO /*
11254c6867a8SHajimu UMEMOTO * IPv4 pseudo scoping according to RFC 3484.
11264c6867a8SHajimu UMEMOTO */
11274c6867a8SHajimu UMEMOTO sa4 = (struct sockaddr_in *)sa;
11284c6867a8SHajimu UMEMOTO /* IPv4 autoconfiguration addresses have link-local scope. */
11294c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 169 &&
11304c6867a8SHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 254)
11314c6867a8SHajimu UMEMOTO return(2);
11324c6867a8SHajimu UMEMOTO /* Private addresses have site-local scope. */
11334c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 10 ||
11344c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 172 &&
11354c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
11364c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 192 &&
11374c6867a8SHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 168))
1138251b48a1SHajimu UMEMOTO return(14); /* XXX: It should be 5 unless NAT */
11394c6867a8SHajimu UMEMOTO /* Loopback addresses have link-local scope. */
11404c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 127)
11414c6867a8SHajimu UMEMOTO return(2);
11424c6867a8SHajimu UMEMOTO return(14);
11434c6867a8SHajimu UMEMOTO break;
11444c6867a8SHajimu UMEMOTO default:
11454c6867a8SHajimu UMEMOTO errno = EAFNOSUPPORT; /* is this a good error? */
11464c6867a8SHajimu UMEMOTO return(-1);
11474c6867a8SHajimu UMEMOTO }
11484c6867a8SHajimu UMEMOTO }
11494c6867a8SHajimu UMEMOTO
1150bc421492SHajimu UMEMOTO static int
explore_copy(const struct addrinfo * pai,const struct addrinfo * src0,struct addrinfo ** res)1151bc421492SHajimu UMEMOTO explore_copy(const struct addrinfo *pai, const struct addrinfo *src0,
1152bc421492SHajimu UMEMOTO struct addrinfo **res)
1153bc421492SHajimu UMEMOTO {
1154bc421492SHajimu UMEMOTO int error;
1155bc421492SHajimu UMEMOTO struct addrinfo sentinel, *cur;
1156bc421492SHajimu UMEMOTO const struct addrinfo *src;
1157bc421492SHajimu UMEMOTO
1158bc421492SHajimu UMEMOTO error = 0;
1159bc421492SHajimu UMEMOTO sentinel.ai_next = NULL;
1160bc421492SHajimu UMEMOTO cur = &sentinel;
1161bc421492SHajimu UMEMOTO
1162bc421492SHajimu UMEMOTO for (src = src0; src != NULL; src = src->ai_next) {
1163bc421492SHajimu UMEMOTO if (src->ai_family != pai->ai_family)
1164bc421492SHajimu UMEMOTO continue;
1165bc421492SHajimu UMEMOTO
1166bc421492SHajimu UMEMOTO cur->ai_next = copy_ai(src);
1167bc421492SHajimu UMEMOTO if (!cur->ai_next) {
1168bc421492SHajimu UMEMOTO error = EAI_MEMORY;
1169bc421492SHajimu UMEMOTO goto fail;
1170bc421492SHajimu UMEMOTO }
1171bc421492SHajimu UMEMOTO
1172bc421492SHajimu UMEMOTO cur->ai_next->ai_socktype = pai->ai_socktype;
1173bc421492SHajimu UMEMOTO cur->ai_next->ai_protocol = pai->ai_protocol;
1174bc421492SHajimu UMEMOTO cur = cur->ai_next;
1175bc421492SHajimu UMEMOTO }
1176bc421492SHajimu UMEMOTO
1177bc421492SHajimu UMEMOTO *res = sentinel.ai_next;
1178bc421492SHajimu UMEMOTO return 0;
1179bc421492SHajimu UMEMOTO
1180bc421492SHajimu UMEMOTO fail:
1181bc421492SHajimu UMEMOTO freeaddrinfo(sentinel.ai_next);
1182bc421492SHajimu UMEMOTO return error;
1183bc421492SHajimu UMEMOTO }
1184bc421492SHajimu UMEMOTO
11857d56d374SYoshinobu Inoue /*
11867d56d374SYoshinobu Inoue * hostname == NULL.
11877d56d374SYoshinobu Inoue * passive socket -> anyaddr (0.0.0.0 or ::)
11887d56d374SYoshinobu Inoue * non-passive socket -> localhost (127.0.0.1 or ::1)
11897d56d374SYoshinobu Inoue */
11907d56d374SYoshinobu Inoue static int
explore_null(const struct addrinfo * pai,const char * servname,struct addrinfo ** res)119190d1d883SHajimu UMEMOTO explore_null(const struct addrinfo *pai, const char *servname,
119290d1d883SHajimu UMEMOTO struct addrinfo **res)
11937d56d374SYoshinobu Inoue {
1194bf838688SHajimu UMEMOTO int s;
11957d56d374SYoshinobu Inoue const struct afd *afd;
11960aceddceSHajimu UMEMOTO struct addrinfo *ai;
11977d56d374SYoshinobu Inoue int error;
11987d56d374SYoshinobu Inoue
11997d56d374SYoshinobu Inoue *res = NULL;
12000aceddceSHajimu UMEMOTO ai = NULL;
12017d56d374SYoshinobu Inoue
1202bb957021SHiroki Sato if (pai->ai_family == PF_LOCAL)
1203bb957021SHiroki Sato return (0);
1204bb957021SHiroki Sato
12057d56d374SYoshinobu Inoue /*
1206bf838688SHajimu UMEMOTO * filter out AFs that are not supported by the kernel
1207bf838688SHajimu UMEMOTO * XXX errno?
1208bf838688SHajimu UMEMOTO */
120972531978SJilles Tjoelker s = _socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1210bf838688SHajimu UMEMOTO if (s < 0) {
1211bf838688SHajimu UMEMOTO if (errno != EMFILE)
1212bf838688SHajimu UMEMOTO return 0;
1213bf838688SHajimu UMEMOTO } else
1214bf838688SHajimu UMEMOTO _close(s);
1215bf838688SHajimu UMEMOTO
12167d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family);
12177d56d374SYoshinobu Inoue if (afd == NULL)
12187d56d374SYoshinobu Inoue return 0;
12197d56d374SYoshinobu Inoue
1220a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_PASSIVE) {
12210aceddceSHajimu UMEMOTO GET_AI(ai, afd, afd->a_addrany);
12220aceddceSHajimu UMEMOTO GET_PORT(ai, servname);
1223a42af91cSHajimu UMEMOTO } else {
12240aceddceSHajimu UMEMOTO GET_AI(ai, afd, afd->a_loopback);
12250aceddceSHajimu UMEMOTO GET_PORT(ai, servname);
1226a42af91cSHajimu UMEMOTO }
12277d56d374SYoshinobu Inoue
12280aceddceSHajimu UMEMOTO *res = ai;
12297d56d374SYoshinobu Inoue return 0;
12307d56d374SYoshinobu Inoue
12317d56d374SYoshinobu Inoue free:
12320aceddceSHajimu UMEMOTO if (ai != NULL)
12330aceddceSHajimu UMEMOTO freeaddrinfo(ai);
12347d56d374SYoshinobu Inoue return error;
12357d56d374SYoshinobu Inoue }
12367d56d374SYoshinobu Inoue
12377d56d374SYoshinobu Inoue /*
12387d56d374SYoshinobu Inoue * numeric hostname
12397d56d374SYoshinobu Inoue */
12407d56d374SYoshinobu Inoue static int
explore_numeric(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res,const char * canonname)124190d1d883SHajimu UMEMOTO explore_numeric(const struct addrinfo *pai, const char *hostname,
124290d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res, const char *canonname)
12437d56d374SYoshinobu Inoue {
12447d56d374SYoshinobu Inoue const struct afd *afd;
1245b848a37dSHajimu UMEMOTO struct addrinfo *ai, ai0;
12467d56d374SYoshinobu Inoue int error;
1247bb957021SHiroki Sato char pton[PTON_MAX], path[PATH_MAX], *p;
12487d56d374SYoshinobu Inoue
1249bb957021SHiroki Sato #ifdef CTASSERT
1250bb957021SHiroki Sato CTASSERT(sizeofmember(struct sockaddr_un, sun_path) <= PATH_MAX);
1251bb957021SHiroki Sato #endif
12527d56d374SYoshinobu Inoue *res = NULL;
12530aceddceSHajimu UMEMOTO ai = NULL;
12547d56d374SYoshinobu Inoue
12557d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family);
12567d56d374SYoshinobu Inoue if (afd == NULL)
12577d56d374SYoshinobu Inoue return 0;
12587d56d374SYoshinobu Inoue
1259b826397aSJun-ichiro itojun Hagino switch (afd->a_af) {
1260bb957021SHiroki Sato case AF_LOCAL:
1261bb957021SHiroki Sato if (hostname[0] != '/')
1262bb957021SHiroki Sato ERR(EAI_NONAME);
1263bb957021SHiroki Sato if (strlen(hostname) > afd->a_addrlen)
1264bb957021SHiroki Sato ERR(EAI_MEMORY);
1265bb957021SHiroki Sato /* NUL-termination does not need to be guaranteed. */
1266bb957021SHiroki Sato strncpy(path, hostname, afd->a_addrlen);
1267bb957021SHiroki Sato p = &path[0];
1268bb957021SHiroki Sato break;
1269b826397aSJun-ichiro itojun Hagino case AF_INET:
12700cf41987SHajimu UMEMOTO /*
12710cf41987SHajimu UMEMOTO * RFC3493 requires getaddrinfo() to accept AF_INET formats
12720cf41987SHajimu UMEMOTO * that are accepted by inet_addr() and its family. The
12730cf41987SHajimu UMEMOTO * accepted forms includes the "classful" one, which inet_pton
12740cf41987SHajimu UMEMOTO * does not accept. So we need to separate the case for
12750cf41987SHajimu UMEMOTO * AF_INET.
12760cf41987SHajimu UMEMOTO */
127723e1a2d7SHajimu UMEMOTO if (inet_aton(hostname, (struct in_addr *)pton) != 1 ||
127823e1a2d7SHajimu UMEMOTO hostname[strspn(hostname, "0123456789.xabcdefXABCDEF")] != '\0')
1279da83bf18SHajimu UMEMOTO return 0;
1280bb957021SHiroki Sato p = pton;
1281b826397aSJun-ichiro itojun Hagino break;
1282b826397aSJun-ichiro itojun Hagino default:
1283b848a37dSHajimu UMEMOTO if (inet_pton(afd->a_af, hostname, pton) != 1) {
1284b848a37dSHajimu UMEMOTO if (pai->ai_family != AF_INET6 ||
1285b848a37dSHajimu UMEMOTO (pai->ai_flags & AI_V4MAPPED) != AI_V4MAPPED)
1286da83bf18SHajimu UMEMOTO return 0;
1287b848a37dSHajimu UMEMOTO if (inet_aton(hostname, (struct in_addr *)pton) != 1)
1288b848a37dSHajimu UMEMOTO return 0;
1289b848a37dSHajimu UMEMOTO afd = &afdl[N_INET];
1290b848a37dSHajimu UMEMOTO ai0 = *pai;
1291b848a37dSHajimu UMEMOTO ai0.ai_family = AF_INET;
1292b848a37dSHajimu UMEMOTO pai = &ai0;
1293b848a37dSHajimu UMEMOTO }
1294bb957021SHiroki Sato p = pton;
1295da83bf18SHajimu UMEMOTO break;
1296da83bf18SHajimu UMEMOTO }
1297da83bf18SHajimu UMEMOTO
1298da83bf18SHajimu UMEMOTO if (pai->ai_family == afd->a_af) {
1299bb957021SHiroki Sato GET_AI(ai, afd, p);
13000aceddceSHajimu UMEMOTO GET_PORT(ai, servname);
13014c60a05dSHiroki Sato if ((pai->ai_family == AF_INET ||
13024c60a05dSHiroki Sato pai->ai_family == AF_INET6) &&
13034c60a05dSHiroki Sato (pai->ai_flags & AI_CANONNAME)) {
13048755b918SHajimu UMEMOTO /*
1305da83bf18SHajimu UMEMOTO * Set the numeric address itself as the canonical
1306da83bf18SHajimu UMEMOTO * name, based on a clarification in RFC3493.
13078755b918SHajimu UMEMOTO */
13080aceddceSHajimu UMEMOTO GET_CANONNAME(ai, canonname);
13098755b918SHajimu UMEMOTO }
1310da83bf18SHajimu UMEMOTO } else {
1311da83bf18SHajimu UMEMOTO /*
1312da83bf18SHajimu UMEMOTO * XXX: This should not happen since we already matched the AF
1313da83bf18SHajimu UMEMOTO * by find_afd.
1314da83bf18SHajimu UMEMOTO */
1315da83bf18SHajimu UMEMOTO ERR(EAI_FAMILY);
1316b826397aSJun-ichiro itojun Hagino }
13177d56d374SYoshinobu Inoue
13180aceddceSHajimu UMEMOTO *res = ai;
13197d56d374SYoshinobu Inoue return 0;
13207d56d374SYoshinobu Inoue
13217d56d374SYoshinobu Inoue free:
13227d56d374SYoshinobu Inoue bad:
13230aceddceSHajimu UMEMOTO if (ai != NULL)
13240aceddceSHajimu UMEMOTO freeaddrinfo(ai);
13257d56d374SYoshinobu Inoue return error;
13267d56d374SYoshinobu Inoue }
13277d56d374SYoshinobu Inoue
13287d56d374SYoshinobu Inoue /*
13297d56d374SYoshinobu Inoue * numeric hostname with scope
13307d56d374SYoshinobu Inoue */
13317d56d374SYoshinobu Inoue static int
explore_numeric_scope(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res)133290d1d883SHajimu UMEMOTO explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
133390d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res)
13347d56d374SYoshinobu Inoue {
1335a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6)
13368755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname);
13377d56d374SYoshinobu Inoue #else
13387d56d374SYoshinobu Inoue const struct afd *afd;
13397d56d374SYoshinobu Inoue struct addrinfo *cur;
13407d56d374SYoshinobu Inoue int error;
1341a42af91cSHajimu UMEMOTO char *cp, *hostname2 = NULL, *scope, *addr;
13427d56d374SYoshinobu Inoue struct sockaddr_in6 *sin6;
13437d56d374SYoshinobu Inoue
13447d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family);
13457d56d374SYoshinobu Inoue if (afd == NULL)
13467d56d374SYoshinobu Inoue return 0;
1347248aee62SJacques Vidrine
13487d56d374SYoshinobu Inoue if (!afd->a_scoped)
13498755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname);
13507d56d374SYoshinobu Inoue
13517d56d374SYoshinobu Inoue cp = strchr(hostname, SCOPE_DELIMITER);
13527d56d374SYoshinobu Inoue if (cp == NULL)
13538755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname);
13547d56d374SYoshinobu Inoue
13557d56d374SYoshinobu Inoue /*
13567d56d374SYoshinobu Inoue * Handle special case of <scoped_address><delimiter><scope id>
13577d56d374SYoshinobu Inoue */
13587d56d374SYoshinobu Inoue hostname2 = strdup(hostname);
13597d56d374SYoshinobu Inoue if (hostname2 == NULL)
13607d56d374SYoshinobu Inoue return EAI_MEMORY;
13617d56d374SYoshinobu Inoue /* terminate at the delimiter */
13627d56d374SYoshinobu Inoue hostname2[cp - hostname] = '\0';
1363a42af91cSHajimu UMEMOTO addr = hostname2;
1364a42af91cSHajimu UMEMOTO scope = cp + 1;
13657d56d374SYoshinobu Inoue
13668755b918SHajimu UMEMOTO error = explore_numeric(pai, addr, servname, res, hostname);
13677d56d374SYoshinobu Inoue if (error == 0) {
1368a89c1d30SHajimu UMEMOTO u_int32_t scopeid;
1369a42af91cSHajimu UMEMOTO
13707d56d374SYoshinobu Inoue for (cur = *res; cur; cur = cur->ai_next) {
13717d56d374SYoshinobu Inoue if (cur->ai_family != AF_INET6)
13727d56d374SYoshinobu Inoue continue;
1373a42af91cSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1374a89c1d30SHajimu UMEMOTO if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1375a42af91cSHajimu UMEMOTO free(hostname2);
1376bc421492SHajimu UMEMOTO freeaddrinfo(*res);
1377bc421492SHajimu UMEMOTO *res = NULL;
137837b3e941SHajimu UMEMOTO return(EAI_NONAME); /* XXX: is return OK? */
1379a42af91cSHajimu UMEMOTO }
1380a42af91cSHajimu UMEMOTO sin6->sin6_scope_id = scopeid;
13817d56d374SYoshinobu Inoue }
13827d56d374SYoshinobu Inoue }
13837d56d374SYoshinobu Inoue
13847d56d374SYoshinobu Inoue free(hostname2);
13857d56d374SYoshinobu Inoue
1386bc421492SHajimu UMEMOTO if (error && *res) {
1387bc421492SHajimu UMEMOTO freeaddrinfo(*res);
1388bc421492SHajimu UMEMOTO *res = NULL;
1389bc421492SHajimu UMEMOTO }
13907d56d374SYoshinobu Inoue return error;
13917d56d374SYoshinobu Inoue #endif
13927d56d374SYoshinobu Inoue }
13937d56d374SYoshinobu Inoue
13947d56d374SYoshinobu Inoue static int
get_canonname(const struct addrinfo * pai,struct addrinfo * ai,const char * str)139590d1d883SHajimu UMEMOTO get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
13967d56d374SYoshinobu Inoue {
13977d56d374SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) {
13988743b270SHajimu UMEMOTO ai->ai_canonname = strdup(str);
13997d56d374SYoshinobu Inoue if (ai->ai_canonname == NULL)
14007d56d374SYoshinobu Inoue return EAI_MEMORY;
14017d56d374SYoshinobu Inoue }
14027d56d374SYoshinobu Inoue return 0;
14037d56d374SYoshinobu Inoue }
14047d56d374SYoshinobu Inoue
14057d56d374SYoshinobu Inoue static struct addrinfo *
get_ai(const struct addrinfo * pai,const struct afd * afd,const char * addr)140690d1d883SHajimu UMEMOTO get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
14077d56d374SYoshinobu Inoue {
14087d56d374SYoshinobu Inoue char *p;
14097d56d374SYoshinobu Inoue struct addrinfo *ai;
1410b848a37dSHajimu UMEMOTO #ifdef INET6
1411b848a37dSHajimu UMEMOTO struct in6_addr mapaddr;
1412b848a37dSHajimu UMEMOTO
1413b848a37dSHajimu UMEMOTO if (afd->a_af == AF_INET && (pai->ai_flags & AI_V4MAPPED) != 0) {
1414b848a37dSHajimu UMEMOTO afd = &afdl[N_INET6];
1415b848a37dSHajimu UMEMOTO _map_v4v6_address(addr, (char *)&mapaddr);
1416b848a37dSHajimu UMEMOTO addr = (char *)&mapaddr;
1417b848a37dSHajimu UMEMOTO }
1418b848a37dSHajimu UMEMOTO #endif
14197d56d374SYoshinobu Inoue
14207d56d374SYoshinobu Inoue ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
14217d56d374SYoshinobu Inoue + (afd->a_socklen));
14227d56d374SYoshinobu Inoue if (ai == NULL)
14237d56d374SYoshinobu Inoue return NULL;
14247d56d374SYoshinobu Inoue
14257d56d374SYoshinobu Inoue memcpy(ai, pai, sizeof(struct addrinfo));
1426a42af91cSHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1427a42af91cSHajimu UMEMOTO memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
14287d56d374SYoshinobu Inoue ai->ai_addr->sa_len = afd->a_socklen;
14297d56d374SYoshinobu Inoue ai->ai_addrlen = afd->a_socklen;
1430bb957021SHiroki Sato if (ai->ai_family == PF_LOCAL) {
1431bb957021SHiroki Sato size_t n = strnlen(addr, afd->a_addrlen);
1432bb957021SHiroki Sato
1433bb957021SHiroki Sato ai->ai_addrlen -= afd->a_addrlen - n;
1434bb957021SHiroki Sato ai->ai_addr->sa_len -= afd->a_addrlen - n;
1435bb957021SHiroki Sato }
14367d56d374SYoshinobu Inoue ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1437248aee62SJacques Vidrine p = (char *)(void *)(ai->ai_addr);
1438248aee62SJacques Vidrine memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
14397d56d374SYoshinobu Inoue return ai;
14407d56d374SYoshinobu Inoue }
14417d56d374SYoshinobu Inoue
1442bc421492SHajimu UMEMOTO /* XXX need to malloc() the same way we do from other functions! */
1443bc421492SHajimu UMEMOTO static struct addrinfo *
copy_ai(const struct addrinfo * pai)1444bc421492SHajimu UMEMOTO copy_ai(const struct addrinfo *pai)
1445bc421492SHajimu UMEMOTO {
1446bc421492SHajimu UMEMOTO struct addrinfo *ai;
1447bc421492SHajimu UMEMOTO size_t l;
1448bc421492SHajimu UMEMOTO
1449bc421492SHajimu UMEMOTO l = sizeof(*ai) + pai->ai_addrlen;
1450c5c2166dSEnji Cooper if ((ai = calloc(1, l)) == NULL)
1451bc421492SHajimu UMEMOTO return NULL;
1452bc421492SHajimu UMEMOTO memcpy(ai, pai, sizeof(*ai));
1453bc421492SHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1454bc421492SHajimu UMEMOTO memcpy(ai->ai_addr, pai->ai_addr, pai->ai_addrlen);
1455bc421492SHajimu UMEMOTO
1456bc421492SHajimu UMEMOTO if (pai->ai_canonname) {
1457bc421492SHajimu UMEMOTO l = strlen(pai->ai_canonname) + 1;
1458bc421492SHajimu UMEMOTO if ((ai->ai_canonname = malloc(l)) == NULL) {
1459bc421492SHajimu UMEMOTO free(ai);
1460bc421492SHajimu UMEMOTO return NULL;
1461bc421492SHajimu UMEMOTO }
1462bc421492SHajimu UMEMOTO strlcpy(ai->ai_canonname, pai->ai_canonname, l);
1463bc421492SHajimu UMEMOTO } else {
1464bc421492SHajimu UMEMOTO /* just to make sure */
1465bc421492SHajimu UMEMOTO ai->ai_canonname = NULL;
1466bc421492SHajimu UMEMOTO }
1467bc421492SHajimu UMEMOTO
1468bc421492SHajimu UMEMOTO ai->ai_next = NULL;
1469bc421492SHajimu UMEMOTO
1470bc421492SHajimu UMEMOTO return ai;
1471bc421492SHajimu UMEMOTO }
1472bc421492SHajimu UMEMOTO
14737d56d374SYoshinobu Inoue static int
get_portmatch(const struct addrinfo * ai,const char * servname)147490d1d883SHajimu UMEMOTO get_portmatch(const struct addrinfo *ai, const char *servname)
14757d56d374SYoshinobu Inoue {
1476a42af91cSHajimu UMEMOTO
1477c224435eSHajimu UMEMOTO /* get_port does not touch first argument when matchonly == 1. */
1478a42af91cSHajimu UMEMOTO /* LINTED const cast */
14797d56d374SYoshinobu Inoue return get_port((struct addrinfo *)ai, servname, 1);
14807d56d374SYoshinobu Inoue }
14817d56d374SYoshinobu Inoue
14827d56d374SYoshinobu Inoue static int
get_port(struct addrinfo * ai,const char * servname,int matchonly)148390d1d883SHajimu UMEMOTO get_port(struct addrinfo *ai, const char *servname, int matchonly)
14847d56d374SYoshinobu Inoue {
14857d56d374SYoshinobu Inoue const char *proto;
14867d56d374SYoshinobu Inoue struct servent *sp;
1487e24e9d99SHajimu UMEMOTO int port, error;
14887d56d374SYoshinobu Inoue int allownumeric;
14897d56d374SYoshinobu Inoue
14907d56d374SYoshinobu Inoue if (servname == NULL)
14917d56d374SYoshinobu Inoue return 0;
1492a42af91cSHajimu UMEMOTO switch (ai->ai_family) {
1493bb957021SHiroki Sato case AF_LOCAL:
1494bb957021SHiroki Sato /* AF_LOCAL ignores servname silently. */
1495bb957021SHiroki Sato return (0);
1496a42af91cSHajimu UMEMOTO case AF_INET:
1497a42af91cSHajimu UMEMOTO #ifdef AF_INET6
1498a42af91cSHajimu UMEMOTO case AF_INET6:
14997d56d374SYoshinobu Inoue #endif
1500a42af91cSHajimu UMEMOTO break;
1501a42af91cSHajimu UMEMOTO default:
15027d56d374SYoshinobu Inoue return 0;
1503a42af91cSHajimu UMEMOTO }
15047d56d374SYoshinobu Inoue
15057d56d374SYoshinobu Inoue switch (ai->ai_socktype) {
15067d56d374SYoshinobu Inoue case SOCK_RAW:
15077d56d374SYoshinobu Inoue return EAI_SERVICE;
15087d56d374SYoshinobu Inoue case SOCK_DGRAM:
15097d56d374SYoshinobu Inoue case SOCK_STREAM:
15104d756307SHajimu UMEMOTO case SOCK_SEQPACKET:
15117d56d374SYoshinobu Inoue allownumeric = 1;
15127d56d374SYoshinobu Inoue break;
15137d56d374SYoshinobu Inoue case ANY:
1514d53978e3SHajimu UMEMOTO switch (ai->ai_family) {
1515d53978e3SHajimu UMEMOTO case AF_INET:
1516d53978e3SHajimu UMEMOTO #ifdef AF_INET6
1517d53978e3SHajimu UMEMOTO case AF_INET6:
1518d53978e3SHajimu UMEMOTO #endif
1519d53978e3SHajimu UMEMOTO allownumeric = 1;
1520d53978e3SHajimu UMEMOTO break;
1521d53978e3SHajimu UMEMOTO default:
15227d56d374SYoshinobu Inoue allownumeric = 0;
15237d56d374SYoshinobu Inoue break;
1524d53978e3SHajimu UMEMOTO }
1525d53978e3SHajimu UMEMOTO break;
15267d56d374SYoshinobu Inoue default:
15277d56d374SYoshinobu Inoue return EAI_SOCKTYPE;
15287d56d374SYoshinobu Inoue }
15297d56d374SYoshinobu Inoue
1530e24e9d99SHajimu UMEMOTO error = str2number(servname, &port);
1531e24e9d99SHajimu UMEMOTO if (error == 0) {
15327d56d374SYoshinobu Inoue if (!allownumeric)
15337d56d374SYoshinobu Inoue return EAI_SERVICE;
15347d56d374SYoshinobu Inoue if (port < 0 || port > 65535)
15357d56d374SYoshinobu Inoue return EAI_SERVICE;
1536dd521ef1SHajimu UMEMOTO port = htons(port);
15377d56d374SYoshinobu Inoue } else {
1538c224435eSHajimu UMEMOTO if (ai->ai_flags & AI_NUMERICSERV)
1539c224435eSHajimu UMEMOTO return EAI_NONAME;
15404d756307SHajimu UMEMOTO
15414d756307SHajimu UMEMOTO switch (ai->ai_protocol) {
15424d756307SHajimu UMEMOTO case IPPROTO_UDP:
15437d56d374SYoshinobu Inoue proto = "udp";
15447d56d374SYoshinobu Inoue break;
15454d756307SHajimu UMEMOTO case IPPROTO_TCP:
15467d56d374SYoshinobu Inoue proto = "tcp";
15477d56d374SYoshinobu Inoue break;
15484d756307SHajimu UMEMOTO case IPPROTO_SCTP:
15494d756307SHajimu UMEMOTO proto = "sctp";
15504d756307SHajimu UMEMOTO break;
1551e06e816fSKevin Lo case IPPROTO_UDPLITE:
1552e06e816fSKevin Lo proto = "udplite";
1553e06e816fSKevin Lo break;
15547d56d374SYoshinobu Inoue default:
15557d56d374SYoshinobu Inoue proto = NULL;
15567d56d374SYoshinobu Inoue break;
15577d56d374SYoshinobu Inoue }
15587d56d374SYoshinobu Inoue
155962e8b17dSHajimu UMEMOTO if ((sp = getservbyname(servname, proto)) == NULL)
15607d56d374SYoshinobu Inoue return EAI_SERVICE;
15617d56d374SYoshinobu Inoue port = sp->s_port;
15627d56d374SYoshinobu Inoue }
15637d56d374SYoshinobu Inoue
15647d56d374SYoshinobu Inoue if (!matchonly) {
15657d56d374SYoshinobu Inoue switch (ai->ai_family) {
15667d56d374SYoshinobu Inoue case AF_INET:
1567a42af91cSHajimu UMEMOTO ((struct sockaddr_in *)(void *)
1568a42af91cSHajimu UMEMOTO ai->ai_addr)->sin_port = port;
15697d56d374SYoshinobu Inoue break;
15707d56d374SYoshinobu Inoue #ifdef INET6
15717d56d374SYoshinobu Inoue case AF_INET6:
1572a42af91cSHajimu UMEMOTO ((struct sockaddr_in6 *)(void *)
1573a42af91cSHajimu UMEMOTO ai->ai_addr)->sin6_port = port;
15747d56d374SYoshinobu Inoue break;
15757d56d374SYoshinobu Inoue #endif
15767d56d374SYoshinobu Inoue }
15777d56d374SYoshinobu Inoue }
15787d56d374SYoshinobu Inoue
15797d56d374SYoshinobu Inoue return 0;
15807d56d374SYoshinobu Inoue }
15817d56d374SYoshinobu Inoue
15827d56d374SYoshinobu Inoue static const struct afd *
find_afd(int af)158390d1d883SHajimu UMEMOTO find_afd(int af)
15847d56d374SYoshinobu Inoue {
15857d56d374SYoshinobu Inoue const struct afd *afd;
15867d56d374SYoshinobu Inoue
15877d56d374SYoshinobu Inoue if (af == PF_UNSPEC)
15887d56d374SYoshinobu Inoue return NULL;
15897d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) {
15907d56d374SYoshinobu Inoue if (afd->a_af == af)
15917d56d374SYoshinobu Inoue return afd;
15927d56d374SYoshinobu Inoue }
15937d56d374SYoshinobu Inoue return NULL;
15947d56d374SYoshinobu Inoue }
1595a42af91cSHajimu UMEMOTO
1596a42af91cSHajimu UMEMOTO /*
159717f53388SHajimu UMEMOTO * RFC 3493: AI_ADDRCONFIG check. Determines which address families are
15986f63a114SHajimu UMEMOTO * configured on the local system and correlates with pai->ai_family value.
15996f63a114SHajimu UMEMOTO * If an address family is not configured on the system, it will not be
16006f63a114SHajimu UMEMOTO * queried for. For this purpose, loopback addresses are not considered
16016f63a114SHajimu UMEMOTO * configured addresses.
1602bf838688SHajimu UMEMOTO *
1603bf838688SHajimu UMEMOTO * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1604bf838688SHajimu UMEMOTO * _dns_getaddrinfo.
1605a42af91cSHajimu UMEMOTO */
1606a42af91cSHajimu UMEMOTO static int
addrconfig(struct addrinfo * pai)160790d1d883SHajimu UMEMOTO addrconfig(struct addrinfo *pai)
1608a42af91cSHajimu UMEMOTO {
16096f63a114SHajimu UMEMOTO struct ifaddrs *ifaddrs, *ifa;
161017f53388SHajimu UMEMOTO struct sockaddr_in *sin;
16115d9d18f2SHajimu UMEMOTO #ifdef INET6
161217f53388SHajimu UMEMOTO struct sockaddr_in6 *sin6;
16135d9d18f2SHajimu UMEMOTO #endif
16146f63a114SHajimu UMEMOTO int seen_inet = 0, seen_inet6 = 0;
1615a42af91cSHajimu UMEMOTO
16166f63a114SHajimu UMEMOTO if (getifaddrs(&ifaddrs) != 0)
161717f53388SHajimu UMEMOTO return (0);
16186f63a114SHajimu UMEMOTO
16196f63a114SHajimu UMEMOTO for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
16206f63a114SHajimu UMEMOTO if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_UP) == 0)
16216f63a114SHajimu UMEMOTO continue;
16226f63a114SHajimu UMEMOTO switch (ifa->ifa_addr->sa_family) {
16236f63a114SHajimu UMEMOTO case AF_INET:
162417f53388SHajimu UMEMOTO if (seen_inet)
162517f53388SHajimu UMEMOTO continue;
162617f53388SHajimu UMEMOTO sin = (struct sockaddr_in *)(ifa->ifa_addr);
1627ea24ff47SHajimu UMEMOTO if (htonl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
162817f53388SHajimu UMEMOTO continue;
16296f63a114SHajimu UMEMOTO seen_inet = 1;
16306f63a114SHajimu UMEMOTO break;
16316f63a114SHajimu UMEMOTO #ifdef INET6
16326f63a114SHajimu UMEMOTO case AF_INET6:
163317f53388SHajimu UMEMOTO if (seen_inet6)
163417f53388SHajimu UMEMOTO continue;
163517f53388SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
163617f53388SHajimu UMEMOTO if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
163717f53388SHajimu UMEMOTO continue;
163817f53388SHajimu UMEMOTO if ((ifa->ifa_flags & IFT_LOOP) != 0 &&
163917f53388SHajimu UMEMOTO IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
164017f53388SHajimu UMEMOTO continue;
164117f53388SHajimu UMEMOTO if (is_ifdisabled(ifa->ifa_name))
164217f53388SHajimu UMEMOTO continue;
16436f63a114SHajimu UMEMOTO seen_inet6 = 1;
16446f63a114SHajimu UMEMOTO break;
16456f63a114SHajimu UMEMOTO #endif
1646bf838688SHajimu UMEMOTO }
16476f63a114SHajimu UMEMOTO }
16486f63a114SHajimu UMEMOTO freeifaddrs(ifaddrs);
16496f63a114SHajimu UMEMOTO
16506f63a114SHajimu UMEMOTO switch(pai->ai_family) {
16516f63a114SHajimu UMEMOTO case AF_INET6:
165217f53388SHajimu UMEMOTO return (seen_inet6);
16536f63a114SHajimu UMEMOTO case AF_INET:
165417f53388SHajimu UMEMOTO return (seen_inet);
16556f63a114SHajimu UMEMOTO case AF_UNSPEC:
16566f63a114SHajimu UMEMOTO if (seen_inet == seen_inet6)
165717f53388SHajimu UMEMOTO return (seen_inet);
16586f63a114SHajimu UMEMOTO pai->ai_family = seen_inet ? AF_INET : AF_INET6;
165917f53388SHajimu UMEMOTO return (1);
16606f63a114SHajimu UMEMOTO }
166117f53388SHajimu UMEMOTO return (1);
1662a42af91cSHajimu UMEMOTO }
1663a42af91cSHajimu UMEMOTO
1664a42af91cSHajimu UMEMOTO #ifdef INET6
16656f63a114SHajimu UMEMOTO static int
is_ifdisabled(char * name)16666f63a114SHajimu UMEMOTO is_ifdisabled(char *name)
16676f63a114SHajimu UMEMOTO {
16686f63a114SHajimu UMEMOTO struct in6_ndireq nd;
16696f63a114SHajimu UMEMOTO int fd;
16706f63a114SHajimu UMEMOTO
1671cfaa2a09SHajimu UMEMOTO if ((fd = _socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
167217f53388SHajimu UMEMOTO return (-1);
16736f63a114SHajimu UMEMOTO memset(&nd, 0, sizeof(nd));
16746f63a114SHajimu UMEMOTO strlcpy(nd.ifname, name, sizeof(nd.ifname));
16756f63a114SHajimu UMEMOTO if (_ioctl(fd, SIOCGIFINFO_IN6, &nd) < 0) {
16766f63a114SHajimu UMEMOTO _close(fd);
167717f53388SHajimu UMEMOTO return (-1);
16786f63a114SHajimu UMEMOTO }
16796f63a114SHajimu UMEMOTO _close(fd);
16806f63a114SHajimu UMEMOTO return ((nd.ndi.flags & ND6_IFF_IFDISABLED) != 0);
16816f63a114SHajimu UMEMOTO }
16826f63a114SHajimu UMEMOTO
1683a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */
1684a42af91cSHajimu UMEMOTO static int
ip6_str2scopeid(char * scope,struct sockaddr_in6 * sin6,u_int32_t * scopeid)168590d1d883SHajimu UMEMOTO ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1686a42af91cSHajimu UMEMOTO {
1687a89c1d30SHajimu UMEMOTO u_long lscopeid;
1688bf838688SHajimu UMEMOTO struct in6_addr *a6;
1689a42af91cSHajimu UMEMOTO char *ep;
1690a42af91cSHajimu UMEMOTO
1691bf838688SHajimu UMEMOTO a6 = &sin6->sin6_addr;
1692bf838688SHajimu UMEMOTO
1693ec20fe00SJun-ichiro itojun Hagino /* empty scopeid portion is invalid */
1694ec20fe00SJun-ichiro itojun Hagino if (*scope == '\0')
1695ec20fe00SJun-ichiro itojun Hagino return -1;
1696ec20fe00SJun-ichiro itojun Hagino
1697324930d7SHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
1698324930d7SHajimu UMEMOTO IN6_IS_ADDR_MC_NODELOCAL(a6)) {
1699a42af91cSHajimu UMEMOTO /*
1700a42af91cSHajimu UMEMOTO * We currently assume a one-to-one mapping between links
1701a42af91cSHajimu UMEMOTO * and interfaces, so we simply use interface indices for
1702a42af91cSHajimu UMEMOTO * like-local scopes.
1703a42af91cSHajimu UMEMOTO */
1704a89c1d30SHajimu UMEMOTO *scopeid = if_nametoindex(scope);
1705a89c1d30SHajimu UMEMOTO if (*scopeid == 0)
1706a42af91cSHajimu UMEMOTO goto trynumeric;
1707a89c1d30SHajimu UMEMOTO return 0;
1708a42af91cSHajimu UMEMOTO }
1709a42af91cSHajimu UMEMOTO
1710a42af91cSHajimu UMEMOTO /* still unclear about literal, allow numeric only - placeholder */
1711a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1712a42af91cSHajimu UMEMOTO goto trynumeric;
1713a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1714a42af91cSHajimu UMEMOTO goto trynumeric;
1715a42af91cSHajimu UMEMOTO else
1716a42af91cSHajimu UMEMOTO goto trynumeric; /* global */
1717a42af91cSHajimu UMEMOTO
1718a42af91cSHajimu UMEMOTO /* try to convert to a numeric id as a last resort */
1719a42af91cSHajimu UMEMOTO trynumeric:
1720a89c1d30SHajimu UMEMOTO errno = 0;
1721a89c1d30SHajimu UMEMOTO lscopeid = strtoul(scope, &ep, 10);
1722a89c1d30SHajimu UMEMOTO *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1723a89c1d30SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1724a89c1d30SHajimu UMEMOTO return 0;
1725a42af91cSHajimu UMEMOTO else
1726a42af91cSHajimu UMEMOTO return -1;
1727a42af91cSHajimu UMEMOTO }
1728a42af91cSHajimu UMEMOTO #endif
1729a42af91cSHajimu UMEMOTO
173006a99fe3SHajimu UMEMOTO
173106a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
173206a99fe3SHajimu UMEMOTO static int
addrinfo_id_func(char * buffer,size_t * buffer_size,va_list ap,void * cache_mdata)173306a99fe3SHajimu UMEMOTO addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap,
173406a99fe3SHajimu UMEMOTO void *cache_mdata)
173506a99fe3SHajimu UMEMOTO {
173606a99fe3SHajimu UMEMOTO res_state statp;
173706a99fe3SHajimu UMEMOTO u_long res_options;
173806a99fe3SHajimu UMEMOTO
173906a99fe3SHajimu UMEMOTO const int op_id = 0; /* identifies the getaddrinfo for the cache */
174006a99fe3SHajimu UMEMOTO char *hostname;
174106a99fe3SHajimu UMEMOTO struct addrinfo *hints;
174206a99fe3SHajimu UMEMOTO
174306a99fe3SHajimu UMEMOTO char *p;
174406a99fe3SHajimu UMEMOTO int ai_flags, ai_family, ai_socktype, ai_protocol;
174506a99fe3SHajimu UMEMOTO size_t desired_size, size;
174606a99fe3SHajimu UMEMOTO
174706a99fe3SHajimu UMEMOTO statp = __res_state();
174806a99fe3SHajimu UMEMOTO res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
174906a99fe3SHajimu UMEMOTO RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
175006a99fe3SHajimu UMEMOTO
175106a99fe3SHajimu UMEMOTO hostname = va_arg(ap, char *);
175206a99fe3SHajimu UMEMOTO hints = va_arg(ap, struct addrinfo *);
175306a99fe3SHajimu UMEMOTO
175406a99fe3SHajimu UMEMOTO desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4;
175506a99fe3SHajimu UMEMOTO if (hostname != NULL) {
175606a99fe3SHajimu UMEMOTO size = strlen(hostname);
175706a99fe3SHajimu UMEMOTO desired_size += size + 1;
175806a99fe3SHajimu UMEMOTO } else
175906a99fe3SHajimu UMEMOTO size = 0;
176006a99fe3SHajimu UMEMOTO
176106a99fe3SHajimu UMEMOTO if (desired_size > *buffer_size) {
176206a99fe3SHajimu UMEMOTO *buffer_size = desired_size;
176306a99fe3SHajimu UMEMOTO return (NS_RETURN);
176406a99fe3SHajimu UMEMOTO }
176506a99fe3SHajimu UMEMOTO
176606a99fe3SHajimu UMEMOTO if (hints == NULL)
176706a99fe3SHajimu UMEMOTO ai_flags = ai_family = ai_socktype = ai_protocol = 0;
176806a99fe3SHajimu UMEMOTO else {
176906a99fe3SHajimu UMEMOTO ai_flags = hints->ai_flags;
177006a99fe3SHajimu UMEMOTO ai_family = hints->ai_family;
177106a99fe3SHajimu UMEMOTO ai_socktype = hints->ai_socktype;
177206a99fe3SHajimu UMEMOTO ai_protocol = hints->ai_protocol;
177306a99fe3SHajimu UMEMOTO }
177406a99fe3SHajimu UMEMOTO
177506a99fe3SHajimu UMEMOTO p = buffer;
177606a99fe3SHajimu UMEMOTO memcpy(p, &res_options, sizeof(res_options));
177706a99fe3SHajimu UMEMOTO p += sizeof(res_options);
177806a99fe3SHajimu UMEMOTO
177906a99fe3SHajimu UMEMOTO memcpy(p, &op_id, sizeof(int));
178006a99fe3SHajimu UMEMOTO p += sizeof(int);
178106a99fe3SHajimu UMEMOTO
178206a99fe3SHajimu UMEMOTO memcpy(p, &ai_flags, sizeof(int));
178306a99fe3SHajimu UMEMOTO p += sizeof(int);
178406a99fe3SHajimu UMEMOTO
178506a99fe3SHajimu UMEMOTO memcpy(p, &ai_family, sizeof(int));
178606a99fe3SHajimu UMEMOTO p += sizeof(int);
178706a99fe3SHajimu UMEMOTO
178806a99fe3SHajimu UMEMOTO memcpy(p, &ai_socktype, sizeof(int));
178906a99fe3SHajimu UMEMOTO p += sizeof(int);
179006a99fe3SHajimu UMEMOTO
179106a99fe3SHajimu UMEMOTO memcpy(p, &ai_protocol, sizeof(int));
179206a99fe3SHajimu UMEMOTO p += sizeof(int);
179306a99fe3SHajimu UMEMOTO
179406a99fe3SHajimu UMEMOTO if (hostname != NULL)
179506a99fe3SHajimu UMEMOTO memcpy(p, hostname, size);
179606a99fe3SHajimu UMEMOTO
179706a99fe3SHajimu UMEMOTO *buffer_size = desired_size;
179806a99fe3SHajimu UMEMOTO return (NS_SUCCESS);
179906a99fe3SHajimu UMEMOTO }
180006a99fe3SHajimu UMEMOTO
180106a99fe3SHajimu UMEMOTO static int
addrinfo_marshal_func(char * buffer,size_t * buffer_size,void * retval,va_list ap,void * cache_mdata)180206a99fe3SHajimu UMEMOTO addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval,
180306a99fe3SHajimu UMEMOTO va_list ap, void *cache_mdata)
180406a99fe3SHajimu UMEMOTO {
180506a99fe3SHajimu UMEMOTO struct addrinfo *ai, *cai;
180606a99fe3SHajimu UMEMOTO char *p;
180706a99fe3SHajimu UMEMOTO size_t desired_size, size, ai_size;
180806a99fe3SHajimu UMEMOTO
180906a99fe3SHajimu UMEMOTO ai = *((struct addrinfo **)retval);
181006a99fe3SHajimu UMEMOTO
181106a99fe3SHajimu UMEMOTO desired_size = sizeof(size_t);
181206a99fe3SHajimu UMEMOTO ai_size = 0;
181306a99fe3SHajimu UMEMOTO for (cai = ai; cai != NULL; cai = cai->ai_next) {
181406a99fe3SHajimu UMEMOTO desired_size += sizeof(struct addrinfo) + cai->ai_addrlen;
181506a99fe3SHajimu UMEMOTO if (cai->ai_canonname != NULL)
181606a99fe3SHajimu UMEMOTO desired_size += sizeof(size_t) +
181706a99fe3SHajimu UMEMOTO strlen(cai->ai_canonname);
181806a99fe3SHajimu UMEMOTO ++ai_size;
181906a99fe3SHajimu UMEMOTO }
182006a99fe3SHajimu UMEMOTO
182106a99fe3SHajimu UMEMOTO if (desired_size > *buffer_size) {
182206a99fe3SHajimu UMEMOTO /* this assignment is here for future use */
182306a99fe3SHajimu UMEMOTO errno = ERANGE;
182406a99fe3SHajimu UMEMOTO *buffer_size = desired_size;
182506a99fe3SHajimu UMEMOTO return (NS_RETURN);
182606a99fe3SHajimu UMEMOTO }
182706a99fe3SHajimu UMEMOTO
182806a99fe3SHajimu UMEMOTO memset(buffer, 0, desired_size);
182906a99fe3SHajimu UMEMOTO p = buffer;
183006a99fe3SHajimu UMEMOTO
183106a99fe3SHajimu UMEMOTO memcpy(p, &ai_size, sizeof(size_t));
183206a99fe3SHajimu UMEMOTO p += sizeof(size_t);
183306a99fe3SHajimu UMEMOTO for (cai = ai; cai != NULL; cai = cai->ai_next) {
183406a99fe3SHajimu UMEMOTO memcpy(p, cai, sizeof(struct addrinfo));
183506a99fe3SHajimu UMEMOTO p += sizeof(struct addrinfo);
183606a99fe3SHajimu UMEMOTO
183706a99fe3SHajimu UMEMOTO memcpy(p, cai->ai_addr, cai->ai_addrlen);
183806a99fe3SHajimu UMEMOTO p += cai->ai_addrlen;
183906a99fe3SHajimu UMEMOTO
184006a99fe3SHajimu UMEMOTO if (cai->ai_canonname != NULL) {
184106a99fe3SHajimu UMEMOTO size = strlen(cai->ai_canonname);
184206a99fe3SHajimu UMEMOTO memcpy(p, &size, sizeof(size_t));
184306a99fe3SHajimu UMEMOTO p += sizeof(size_t);
184406a99fe3SHajimu UMEMOTO
184506a99fe3SHajimu UMEMOTO memcpy(p, cai->ai_canonname, size);
184606a99fe3SHajimu UMEMOTO p += size;
184706a99fe3SHajimu UMEMOTO }
184806a99fe3SHajimu UMEMOTO }
184906a99fe3SHajimu UMEMOTO
185006a99fe3SHajimu UMEMOTO return (NS_SUCCESS);
185106a99fe3SHajimu UMEMOTO }
185206a99fe3SHajimu UMEMOTO
185306a99fe3SHajimu UMEMOTO static int
addrinfo_unmarshal_func(char * buffer,size_t buffer_size,void * retval,va_list ap,void * cache_mdata)185406a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
185506a99fe3SHajimu UMEMOTO va_list ap, void *cache_mdata)
185606a99fe3SHajimu UMEMOTO {
185706a99fe3SHajimu UMEMOTO struct addrinfo new_ai, *result, *sentinel, *lasts;
185806a99fe3SHajimu UMEMOTO
185906a99fe3SHajimu UMEMOTO char *p;
186006a99fe3SHajimu UMEMOTO size_t ai_size, ai_i, size;
186106a99fe3SHajimu UMEMOTO
186206a99fe3SHajimu UMEMOTO p = buffer;
186306a99fe3SHajimu UMEMOTO memcpy(&ai_size, p, sizeof(size_t));
186406a99fe3SHajimu UMEMOTO p += sizeof(size_t);
186506a99fe3SHajimu UMEMOTO
186606a99fe3SHajimu UMEMOTO result = NULL;
186706a99fe3SHajimu UMEMOTO lasts = NULL;
186806a99fe3SHajimu UMEMOTO for (ai_i = 0; ai_i < ai_size; ++ai_i) {
186906a99fe3SHajimu UMEMOTO memcpy(&new_ai, p, sizeof(struct addrinfo));
187006a99fe3SHajimu UMEMOTO p += sizeof(struct addrinfo);
187106a99fe3SHajimu UMEMOTO size = new_ai.ai_addrlen + sizeof(struct addrinfo) +
187206a99fe3SHajimu UMEMOTO _ALIGNBYTES;
187306a99fe3SHajimu UMEMOTO
1874c5c2166dSEnji Cooper sentinel = calloc(1, size);
187506a99fe3SHajimu UMEMOTO
187606a99fe3SHajimu UMEMOTO memcpy(sentinel, &new_ai, sizeof(struct addrinfo));
187706a99fe3SHajimu UMEMOTO sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel +
187806a99fe3SHajimu UMEMOTO sizeof(struct addrinfo));
187906a99fe3SHajimu UMEMOTO
188006a99fe3SHajimu UMEMOTO memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen);
188106a99fe3SHajimu UMEMOTO p += new_ai.ai_addrlen;
188206a99fe3SHajimu UMEMOTO
188306a99fe3SHajimu UMEMOTO if (new_ai.ai_canonname != NULL) {
188406a99fe3SHajimu UMEMOTO memcpy(&size, p, sizeof(size_t));
188506a99fe3SHajimu UMEMOTO p += sizeof(size_t);
188606a99fe3SHajimu UMEMOTO
1887c5c2166dSEnji Cooper sentinel->ai_canonname = calloc(1, size + 1);
188806a99fe3SHajimu UMEMOTO
188906a99fe3SHajimu UMEMOTO memcpy(sentinel->ai_canonname, p, size);
189006a99fe3SHajimu UMEMOTO p += size;
189106a99fe3SHajimu UMEMOTO }
189206a99fe3SHajimu UMEMOTO
189306a99fe3SHajimu UMEMOTO if (result == NULL) {
189406a99fe3SHajimu UMEMOTO result = sentinel;
189506a99fe3SHajimu UMEMOTO lasts = sentinel;
189606a99fe3SHajimu UMEMOTO } else {
189706a99fe3SHajimu UMEMOTO lasts->ai_next = sentinel;
189806a99fe3SHajimu UMEMOTO lasts = sentinel;
189906a99fe3SHajimu UMEMOTO }
190006a99fe3SHajimu UMEMOTO }
190106a99fe3SHajimu UMEMOTO
190206a99fe3SHajimu UMEMOTO *((struct addrinfo **)retval) = result;
190306a99fe3SHajimu UMEMOTO return (NS_SUCCESS);
190406a99fe3SHajimu UMEMOTO }
190506a99fe3SHajimu UMEMOTO #endif /* NS_CACHING */
190606a99fe3SHajimu UMEMOTO
1907688a5c3eSHajimu UMEMOTO /*
1908688a5c3eSHajimu UMEMOTO * FQDN hostname, DNS lookup
1909688a5c3eSHajimu UMEMOTO */
1910688a5c3eSHajimu UMEMOTO static int
explore_fqdn(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res)191190d1d883SHajimu UMEMOTO explore_fqdn(const struct addrinfo *pai, const char *hostname,
191290d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res)
1913688a5c3eSHajimu UMEMOTO {
1914688a5c3eSHajimu UMEMOTO struct addrinfo *result;
1915688a5c3eSHajimu UMEMOTO struct addrinfo *cur;
1916688a5c3eSHajimu UMEMOTO int error = 0;
191706a99fe3SHajimu UMEMOTO
191806a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
191906a99fe3SHajimu UMEMOTO static const nss_cache_info cache_info =
192006a99fe3SHajimu UMEMOTO NS_COMMON_CACHE_INFO_INITIALIZER(
192106a99fe3SHajimu UMEMOTO hosts, NULL, addrinfo_id_func, addrinfo_marshal_func,
192206a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func);
192306a99fe3SHajimu UMEMOTO #endif
1924688a5c3eSHajimu UMEMOTO static const ns_dtab dtab[] = {
1925688a5c3eSHajimu UMEMOTO NS_FILES_CB(_files_getaddrinfo, NULL)
1926688a5c3eSHajimu UMEMOTO { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
1927688a5c3eSHajimu UMEMOTO NS_NIS_CB(_yp_getaddrinfo, NULL)
192806a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
192906a99fe3SHajimu UMEMOTO NS_CACHE_CB(&cache_info)
193006a99fe3SHajimu UMEMOTO #endif
1931688a5c3eSHajimu UMEMOTO { 0 }
193269b0a4b6SJim Pirzyk };
193369b0a4b6SJim Pirzyk
1934688a5c3eSHajimu UMEMOTO result = NULL;
193569b0a4b6SJim Pirzyk
1936688a5c3eSHajimu UMEMOTO /*
1937688a5c3eSHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it.
1938688a5c3eSHajimu UMEMOTO */
193933dee819SBrian Feldman if (get_portmatch(pai, servname) != 0)
194069b0a4b6SJim Pirzyk return 0;
194169b0a4b6SJim Pirzyk
1942688a5c3eSHajimu UMEMOTO switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1943688a5c3eSHajimu UMEMOTO default_dns_files, hostname, pai)) {
1944688a5c3eSHajimu UMEMOTO case NS_TRYAGAIN:
1945688a5c3eSHajimu UMEMOTO error = EAI_AGAIN;
1946688a5c3eSHajimu UMEMOTO goto free;
1947688a5c3eSHajimu UMEMOTO case NS_UNAVAIL:
1948688a5c3eSHajimu UMEMOTO error = EAI_FAIL;
1949688a5c3eSHajimu UMEMOTO goto free;
1950688a5c3eSHajimu UMEMOTO case NS_NOTFOUND:
1951688a5c3eSHajimu UMEMOTO error = EAI_NONAME;
1952688a5c3eSHajimu UMEMOTO goto free;
195314436138SMike Karels case NS_ADDRFAMILY:
195414436138SMike Karels error = EAI_ADDRFAMILY;
195514436138SMike Karels goto free;
1956688a5c3eSHajimu UMEMOTO case NS_SUCCESS:
1957688a5c3eSHajimu UMEMOTO error = 0;
1958688a5c3eSHajimu UMEMOTO for (cur = result; cur; cur = cur->ai_next) {
1959688a5c3eSHajimu UMEMOTO GET_PORT(cur, servname);
1960688a5c3eSHajimu UMEMOTO /* canonname should be filled already */
1961688a5c3eSHajimu UMEMOTO }
196269b0a4b6SJim Pirzyk break;
196369b0a4b6SJim Pirzyk }
196469b0a4b6SJim Pirzyk
1965688a5c3eSHajimu UMEMOTO *res = result;
1966688a5c3eSHajimu UMEMOTO
196769b0a4b6SJim Pirzyk return 0;
1968688a5c3eSHajimu UMEMOTO
1969688a5c3eSHajimu UMEMOTO free:
1970688a5c3eSHajimu UMEMOTO if (result)
1971688a5c3eSHajimu UMEMOTO freeaddrinfo(result);
1972688a5c3eSHajimu UMEMOTO return error;
197369b0a4b6SJim Pirzyk }
197469b0a4b6SJim Pirzyk
1975a42af91cSHajimu UMEMOTO #ifdef DEBUG
1976a42af91cSHajimu UMEMOTO static const char AskedForGot[] =
1977a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1978a42af91cSHajimu UMEMOTO #endif
1979a42af91cSHajimu UMEMOTO
1980a42af91cSHajimu UMEMOTO static struct addrinfo *
getanswer(const querybuf * answer,int anslen,const char * qname,int qtype,const struct addrinfo * pai,res_state res)198190d1d883SHajimu UMEMOTO getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
198290d1d883SHajimu UMEMOTO const struct addrinfo *pai, res_state res)
1983a42af91cSHajimu UMEMOTO {
1984a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur;
1985a42af91cSHajimu UMEMOTO struct addrinfo ai;
1986a42af91cSHajimu UMEMOTO const struct afd *afd;
1987a42af91cSHajimu UMEMOTO char *canonname;
1988a42af91cSHajimu UMEMOTO const HEADER *hp;
1989a42af91cSHajimu UMEMOTO const u_char *cp;
1990a42af91cSHajimu UMEMOTO int n;
1991a42af91cSHajimu UMEMOTO const u_char *eom;
1992dd521ef1SHajimu UMEMOTO char *bp, *ep;
1993dd521ef1SHajimu UMEMOTO int type, class, ancount, qdcount;
1994a42af91cSHajimu UMEMOTO int haveanswer, had_error;
1995a42af91cSHajimu UMEMOTO char tbuf[MAXDNAME];
1996c05ac53bSDavid E. O'Brien int (*name_ok)(const char *);
1997a42af91cSHajimu UMEMOTO char hostbuf[8*1024];
1998a42af91cSHajimu UMEMOTO
1999a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel));
2000a42af91cSHajimu UMEMOTO cur = &sentinel;
2001a42af91cSHajimu UMEMOTO
2002a42af91cSHajimu UMEMOTO canonname = NULL;
2003a42af91cSHajimu UMEMOTO eom = answer->buf + anslen;
2004a42af91cSHajimu UMEMOTO switch (qtype) {
2005a42af91cSHajimu UMEMOTO case T_A:
2006a42af91cSHajimu UMEMOTO case T_AAAA:
2007a42af91cSHajimu UMEMOTO case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
2008a42af91cSHajimu UMEMOTO name_ok = res_hnok;
2009a42af91cSHajimu UMEMOTO break;
2010a42af91cSHajimu UMEMOTO default:
2011a42af91cSHajimu UMEMOTO return (NULL); /* XXX should be abort(); */
2012a42af91cSHajimu UMEMOTO }
2013a42af91cSHajimu UMEMOTO /*
2014a42af91cSHajimu UMEMOTO * find first satisfactory answer
2015a42af91cSHajimu UMEMOTO */
2016a42af91cSHajimu UMEMOTO hp = &answer->hdr;
2017a42af91cSHajimu UMEMOTO ancount = ntohs(hp->ancount);
2018a42af91cSHajimu UMEMOTO qdcount = ntohs(hp->qdcount);
2019a42af91cSHajimu UMEMOTO bp = hostbuf;
2020dd521ef1SHajimu UMEMOTO ep = hostbuf + sizeof hostbuf;
2021a42af91cSHajimu UMEMOTO cp = answer->buf + HFIXEDSZ;
2022a42af91cSHajimu UMEMOTO if (qdcount != 1) {
20235342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
2024a42af91cSHajimu UMEMOTO return (NULL);
2025a42af91cSHajimu UMEMOTO }
2026dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
2027a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) {
20285342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
2029a42af91cSHajimu UMEMOTO return (NULL);
2030a42af91cSHajimu UMEMOTO }
2031a42af91cSHajimu UMEMOTO cp += n + QFIXEDSZ;
2032a42af91cSHajimu UMEMOTO if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
2033a42af91cSHajimu UMEMOTO /* res_send() has already verified that the query name is the
2034a42af91cSHajimu UMEMOTO * same as the one we sent; this just gets the expanded name
2035a42af91cSHajimu UMEMOTO * (i.e., with the succeeding search-domain tacked on).
2036a42af91cSHajimu UMEMOTO */
2037a42af91cSHajimu UMEMOTO n = strlen(bp) + 1; /* for the \0 */
2038a42af91cSHajimu UMEMOTO if (n >= MAXHOSTNAMELEN) {
20395342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
2040a42af91cSHajimu UMEMOTO return (NULL);
2041a42af91cSHajimu UMEMOTO }
2042a42af91cSHajimu UMEMOTO canonname = bp;
2043a42af91cSHajimu UMEMOTO bp += n;
2044a42af91cSHajimu UMEMOTO /* The qname can be abbreviated, but h_name is now absolute. */
2045a42af91cSHajimu UMEMOTO qname = canonname;
2046a42af91cSHajimu UMEMOTO }
2047a42af91cSHajimu UMEMOTO haveanswer = 0;
2048a42af91cSHajimu UMEMOTO had_error = 0;
2049a42af91cSHajimu UMEMOTO while (ancount-- > 0 && cp < eom && !had_error) {
2050dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
2051a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) {
2052a42af91cSHajimu UMEMOTO had_error++;
2053a42af91cSHajimu UMEMOTO continue;
2054a42af91cSHajimu UMEMOTO }
2055a42af91cSHajimu UMEMOTO cp += n; /* name */
2056a42af91cSHajimu UMEMOTO type = _getshort(cp);
2057a42af91cSHajimu UMEMOTO cp += INT16SZ; /* type */
2058a42af91cSHajimu UMEMOTO class = _getshort(cp);
2059a42af91cSHajimu UMEMOTO cp += INT16SZ + INT32SZ; /* class, TTL */
2060a42af91cSHajimu UMEMOTO n = _getshort(cp);
2061a42af91cSHajimu UMEMOTO cp += INT16SZ; /* len */
2062a42af91cSHajimu UMEMOTO if (class != C_IN) {
2063a42af91cSHajimu UMEMOTO /* XXX - debug? syslog? */
2064a42af91cSHajimu UMEMOTO cp += n;
2065a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */
2066a42af91cSHajimu UMEMOTO }
2067a42af91cSHajimu UMEMOTO if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
2068a42af91cSHajimu UMEMOTO type == T_CNAME) {
2069a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
2070a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(tbuf)) {
2071a42af91cSHajimu UMEMOTO had_error++;
2072a42af91cSHajimu UMEMOTO continue;
2073a42af91cSHajimu UMEMOTO }
2074a42af91cSHajimu UMEMOTO cp += n;
2075a42af91cSHajimu UMEMOTO /* Get canonical name. */
2076a42af91cSHajimu UMEMOTO n = strlen(tbuf) + 1; /* for the \0 */
2077dd521ef1SHajimu UMEMOTO if (n > ep - bp || n >= MAXHOSTNAMELEN) {
2078a42af91cSHajimu UMEMOTO had_error++;
2079a42af91cSHajimu UMEMOTO continue;
2080a42af91cSHajimu UMEMOTO }
2081d0509082SJacques Vidrine strlcpy(bp, tbuf, ep - bp);
2082a42af91cSHajimu UMEMOTO canonname = bp;
2083a42af91cSHajimu UMEMOTO bp += n;
2084a42af91cSHajimu UMEMOTO continue;
2085a42af91cSHajimu UMEMOTO }
2086a42af91cSHajimu UMEMOTO if (qtype == T_ANY) {
2087a42af91cSHajimu UMEMOTO if (!(type == T_A || type == T_AAAA)) {
2088a42af91cSHajimu UMEMOTO cp += n;
2089a42af91cSHajimu UMEMOTO continue;
2090a42af91cSHajimu UMEMOTO }
2091a42af91cSHajimu UMEMOTO } else if (type != qtype) {
2092a42af91cSHajimu UMEMOTO #ifdef DEBUG
2093aba831deSHajimu UMEMOTO if (type != T_KEY && type != T_SIG &&
2094*817f1f30SDag-Erling Smørgrav type != T_DNAME && type != T_RRSIG)
2095a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH,
2096a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
2097a42af91cSHajimu UMEMOTO qname, p_class(C_IN), p_type(qtype),
2098a42af91cSHajimu UMEMOTO p_type(type));
2099a42af91cSHajimu UMEMOTO #endif
2100a42af91cSHajimu UMEMOTO cp += n;
2101a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */
2102a42af91cSHajimu UMEMOTO }
2103a42af91cSHajimu UMEMOTO switch (type) {
2104a42af91cSHajimu UMEMOTO case T_A:
2105a42af91cSHajimu UMEMOTO case T_AAAA:
2106a42af91cSHajimu UMEMOTO if (strcasecmp(canonname, bp) != 0) {
2107a42af91cSHajimu UMEMOTO #ifdef DEBUG
2108a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH,
2109a42af91cSHajimu UMEMOTO AskedForGot, canonname, bp);
2110a42af91cSHajimu UMEMOTO #endif
2111a42af91cSHajimu UMEMOTO cp += n;
2112a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */
2113a42af91cSHajimu UMEMOTO }
2114a42af91cSHajimu UMEMOTO if (type == T_A && n != INADDRSZ) {
2115a42af91cSHajimu UMEMOTO cp += n;
2116a42af91cSHajimu UMEMOTO continue;
2117a42af91cSHajimu UMEMOTO }
2118a42af91cSHajimu UMEMOTO if (type == T_AAAA && n != IN6ADDRSZ) {
2119a42af91cSHajimu UMEMOTO cp += n;
2120a42af91cSHajimu UMEMOTO continue;
2121a42af91cSHajimu UMEMOTO }
2122a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
2123a42af91cSHajimu UMEMOTO if (type == T_AAAA) {
2124a42af91cSHajimu UMEMOTO struct in6_addr in6;
2125a42af91cSHajimu UMEMOTO memcpy(&in6, cp, sizeof(in6));
2126a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&in6)) {
2127a42af91cSHajimu UMEMOTO cp += n;
2128a42af91cSHajimu UMEMOTO continue;
2129a42af91cSHajimu UMEMOTO }
2130a42af91cSHajimu UMEMOTO }
2131a42af91cSHajimu UMEMOTO #endif
2132a42af91cSHajimu UMEMOTO if (!haveanswer) {
2133a42af91cSHajimu UMEMOTO int nn;
2134a42af91cSHajimu UMEMOTO
2135a42af91cSHajimu UMEMOTO canonname = bp;
2136a42af91cSHajimu UMEMOTO nn = strlen(bp) + 1; /* for the \0 */
2137a42af91cSHajimu UMEMOTO bp += nn;
2138a42af91cSHajimu UMEMOTO }
2139a42af91cSHajimu UMEMOTO
2140a42af91cSHajimu UMEMOTO /* don't overwrite pai */
2141a42af91cSHajimu UMEMOTO ai = *pai;
2142a42af91cSHajimu UMEMOTO ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
2143a42af91cSHajimu UMEMOTO afd = find_afd(ai.ai_family);
2144a42af91cSHajimu UMEMOTO if (afd == NULL) {
2145a42af91cSHajimu UMEMOTO cp += n;
2146a42af91cSHajimu UMEMOTO continue;
2147a42af91cSHajimu UMEMOTO }
2148a42af91cSHajimu UMEMOTO cur->ai_next = get_ai(&ai, afd, (const char *)cp);
2149a42af91cSHajimu UMEMOTO if (cur->ai_next == NULL)
2150a42af91cSHajimu UMEMOTO had_error++;
2151a42af91cSHajimu UMEMOTO while (cur && cur->ai_next)
2152a42af91cSHajimu UMEMOTO cur = cur->ai_next;
2153a42af91cSHajimu UMEMOTO cp += n;
2154a42af91cSHajimu UMEMOTO break;
2155a42af91cSHajimu UMEMOTO default:
2156a42af91cSHajimu UMEMOTO abort();
2157a42af91cSHajimu UMEMOTO }
2158a42af91cSHajimu UMEMOTO if (!had_error)
2159a42af91cSHajimu UMEMOTO haveanswer++;
2160a42af91cSHajimu UMEMOTO }
2161a42af91cSHajimu UMEMOTO if (haveanswer) {
216269b0a4b6SJim Pirzyk #if defined(RESOLVSORT)
216369b0a4b6SJim Pirzyk /*
216469b0a4b6SJim Pirzyk * We support only IPv4 address for backward
216569b0a4b6SJim Pirzyk * compatibility against gethostbyname(3).
216669b0a4b6SJim Pirzyk */
21675342d17fSHajimu UMEMOTO if (res->nsort && qtype == T_A) {
21685342d17fSHajimu UMEMOTO if (addr4sort(&sentinel, res) < 0) {
216969b0a4b6SJim Pirzyk freeaddrinfo(sentinel.ai_next);
21705342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
217169b0a4b6SJim Pirzyk return NULL;
217269b0a4b6SJim Pirzyk }
217369b0a4b6SJim Pirzyk }
217469b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/
2175a42af91cSHajimu UMEMOTO if (!canonname)
2176a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, qname);
2177a42af91cSHajimu UMEMOTO else
2178a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, canonname);
21795342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_SUCCESS);
2180a42af91cSHajimu UMEMOTO return sentinel.ai_next;
2181a42af91cSHajimu UMEMOTO }
2182a42af91cSHajimu UMEMOTO
2183e85324c9SHajimu UMEMOTO /*
2184e85324c9SHajimu UMEMOTO * We could have walked a CNAME chain, but the ultimate target
2185e85324c9SHajimu UMEMOTO * may not have what we looked for.
2186e85324c9SHajimu UMEMOTO */
2187e85324c9SHajimu UMEMOTO RES_SET_H_ERRNO(res, ntohs(hp->ancount) > 0 ? NO_DATA : NO_RECOVERY);
2188a42af91cSHajimu UMEMOTO return NULL;
2189a42af91cSHajimu UMEMOTO }
2190a42af91cSHajimu UMEMOTO
2191688a5c3eSHajimu UMEMOTO #ifdef RESOLVSORT
2192688a5c3eSHajimu UMEMOTO struct addr_ptr {
2193688a5c3eSHajimu UMEMOTO struct addrinfo *ai;
2194688a5c3eSHajimu UMEMOTO int aval;
2195688a5c3eSHajimu UMEMOTO };
2196688a5c3eSHajimu UMEMOTO
2197688a5c3eSHajimu UMEMOTO static int
addr4sort(struct addrinfo * sentinel,res_state res)21985342d17fSHajimu UMEMOTO addr4sort(struct addrinfo *sentinel, res_state res)
2199688a5c3eSHajimu UMEMOTO {
2200688a5c3eSHajimu UMEMOTO struct addrinfo *ai;
2201688a5c3eSHajimu UMEMOTO struct addr_ptr *addrs, addr;
2202688a5c3eSHajimu UMEMOTO struct sockaddr_in *sin;
2203688a5c3eSHajimu UMEMOTO int naddrs, i, j;
2204688a5c3eSHajimu UMEMOTO int needsort = 0;
2205688a5c3eSHajimu UMEMOTO
2206688a5c3eSHajimu UMEMOTO if (!sentinel)
2207688a5c3eSHajimu UMEMOTO return -1;
2208688a5c3eSHajimu UMEMOTO naddrs = 0;
2209688a5c3eSHajimu UMEMOTO for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
2210688a5c3eSHajimu UMEMOTO naddrs++;
2211688a5c3eSHajimu UMEMOTO if (naddrs < 2)
2212688a5c3eSHajimu UMEMOTO return 0; /* We don't need sorting. */
2213688a5c3eSHajimu UMEMOTO if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
2214688a5c3eSHajimu UMEMOTO return -1;
2215688a5c3eSHajimu UMEMOTO i = 0;
2216688a5c3eSHajimu UMEMOTO for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
2217688a5c3eSHajimu UMEMOTO sin = (struct sockaddr_in *)ai->ai_addr;
22185342d17fSHajimu UMEMOTO for (j = 0; (unsigned)j < res->nsort; j++) {
22195342d17fSHajimu UMEMOTO if (res->sort_list[j].addr.s_addr ==
22205342d17fSHajimu UMEMOTO (sin->sin_addr.s_addr & res->sort_list[j].mask))
2221688a5c3eSHajimu UMEMOTO break;
2222688a5c3eSHajimu UMEMOTO }
2223688a5c3eSHajimu UMEMOTO addrs[i].ai = ai;
2224688a5c3eSHajimu UMEMOTO addrs[i].aval = j;
2225688a5c3eSHajimu UMEMOTO if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
2226688a5c3eSHajimu UMEMOTO needsort = i;
2227688a5c3eSHajimu UMEMOTO i++;
2228688a5c3eSHajimu UMEMOTO }
2229688a5c3eSHajimu UMEMOTO if (!needsort) {
2230688a5c3eSHajimu UMEMOTO free(addrs);
2231688a5c3eSHajimu UMEMOTO return 0;
2232688a5c3eSHajimu UMEMOTO }
2233688a5c3eSHajimu UMEMOTO
2234688a5c3eSHajimu UMEMOTO while (needsort < naddrs) {
2235688a5c3eSHajimu UMEMOTO for (j = needsort - 1; j >= 0; j--) {
2236688a5c3eSHajimu UMEMOTO if (addrs[j].aval > addrs[j+1].aval) {
2237688a5c3eSHajimu UMEMOTO addr = addrs[j];
2238688a5c3eSHajimu UMEMOTO addrs[j] = addrs[j + 1];
2239688a5c3eSHajimu UMEMOTO addrs[j + 1] = addr;
2240688a5c3eSHajimu UMEMOTO } else
2241688a5c3eSHajimu UMEMOTO break;
2242688a5c3eSHajimu UMEMOTO }
2243688a5c3eSHajimu UMEMOTO needsort++;
2244688a5c3eSHajimu UMEMOTO }
2245688a5c3eSHajimu UMEMOTO
2246688a5c3eSHajimu UMEMOTO ai = sentinel;
2247688a5c3eSHajimu UMEMOTO for (i = 0; i < naddrs; ++i) {
2248688a5c3eSHajimu UMEMOTO ai->ai_next = addrs[i].ai;
2249688a5c3eSHajimu UMEMOTO ai = ai->ai_next;
2250688a5c3eSHajimu UMEMOTO }
2251688a5c3eSHajimu UMEMOTO ai->ai_next = NULL;
2252688a5c3eSHajimu UMEMOTO free(addrs);
2253688a5c3eSHajimu UMEMOTO return 0;
2254688a5c3eSHajimu UMEMOTO }
2255688a5c3eSHajimu UMEMOTO #endif /*RESOLVSORT*/
2256688a5c3eSHajimu UMEMOTO
2257a42af91cSHajimu UMEMOTO /*ARGSUSED*/
2258a42af91cSHajimu UMEMOTO static int
_dns_getaddrinfo(void * rv,void * cb_data,va_list ap)225990d1d883SHajimu UMEMOTO _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
2260a42af91cSHajimu UMEMOTO {
2261b848a37dSHajimu UMEMOTO struct addrinfo *ai, ai0;
22625cd588cbSHajimu UMEMOTO querybuf *buf, *buf2;
2263a214cd1aSHajimu UMEMOTO const char *hostname;
2264248aee62SJacques Vidrine const struct addrinfo *pai;
2265a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur;
2266a42af91cSHajimu UMEMOTO struct res_target q, q2;
22675342d17fSHajimu UMEMOTO res_state res;
2268a42af91cSHajimu UMEMOTO
22698b8a0b88SEnji Cooper ai = NULL;
22708b8a0b88SEnji Cooper
2271a214cd1aSHajimu UMEMOTO hostname = va_arg(ap, char *);
2272248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *);
2273248aee62SJacques Vidrine
2274f1fa5b4fSXin LI memset(&q, 0, sizeof(q));
2275a42af91cSHajimu UMEMOTO memset(&q2, 0, sizeof(q2));
2276a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel));
2277a42af91cSHajimu UMEMOTO cur = &sentinel;
2278a42af91cSHajimu UMEMOTO
22794eb17fd7SHajimu UMEMOTO res = __res_state();
22804eb17fd7SHajimu UMEMOTO
22815cd588cbSHajimu UMEMOTO buf = malloc(sizeof(*buf));
22825cd588cbSHajimu UMEMOTO if (!buf) {
22835342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL);
22845cd588cbSHajimu UMEMOTO return NS_NOTFOUND;
22855cd588cbSHajimu UMEMOTO }
22865cd588cbSHajimu UMEMOTO buf2 = malloc(sizeof(*buf2));
22875cd588cbSHajimu UMEMOTO if (!buf2) {
22885cd588cbSHajimu UMEMOTO free(buf);
22895342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL);
22905cd588cbSHajimu UMEMOTO return NS_NOTFOUND;
22915cd588cbSHajimu UMEMOTO }
22925cd588cbSHajimu UMEMOTO
2293b848a37dSHajimu UMEMOTO if (pai->ai_family == AF_INET6 &&
2294b848a37dSHajimu UMEMOTO (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED) {
2295b848a37dSHajimu UMEMOTO ai0 = *pai;
2296b848a37dSHajimu UMEMOTO ai0.ai_family = AF_UNSPEC;
2297b848a37dSHajimu UMEMOTO pai = &ai0;
2298b848a37dSHajimu UMEMOTO }
2299b848a37dSHajimu UMEMOTO
2300a42af91cSHajimu UMEMOTO switch (pai->ai_family) {
2301a42af91cSHajimu UMEMOTO case AF_UNSPEC:
2302a214cd1aSHajimu UMEMOTO q.name = hostname;
2303b826397aSJun-ichiro itojun Hagino q.qclass = C_IN;
2304d0d6a2c7SHajimu UMEMOTO q.qtype = T_A;
23055cd588cbSHajimu UMEMOTO q.answer = buf->buf;
23065cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf);
2307a42af91cSHajimu UMEMOTO q.next = &q2;
2308a214cd1aSHajimu UMEMOTO q2.name = hostname;
2309b826397aSJun-ichiro itojun Hagino q2.qclass = C_IN;
2310d0d6a2c7SHajimu UMEMOTO q2.qtype = T_AAAA;
23115cd588cbSHajimu UMEMOTO q2.answer = buf2->buf;
23125cd588cbSHajimu UMEMOTO q2.anslen = sizeof(buf2->buf);
2313a42af91cSHajimu UMEMOTO break;
2314a42af91cSHajimu UMEMOTO case AF_INET:
2315a214cd1aSHajimu UMEMOTO q.name = hostname;
2316b826397aSJun-ichiro itojun Hagino q.qclass = C_IN;
2317b826397aSJun-ichiro itojun Hagino q.qtype = T_A;
23185cd588cbSHajimu UMEMOTO q.answer = buf->buf;
23195cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf);
2320a42af91cSHajimu UMEMOTO break;
2321a42af91cSHajimu UMEMOTO case AF_INET6:
2322a214cd1aSHajimu UMEMOTO q.name = hostname;
2323b826397aSJun-ichiro itojun Hagino q.qclass = C_IN;
2324b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA;
23255cd588cbSHajimu UMEMOTO q.answer = buf->buf;
23265cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf);
2327a42af91cSHajimu UMEMOTO break;
2328a42af91cSHajimu UMEMOTO default:
23295cd588cbSHajimu UMEMOTO free(buf);
23305cd588cbSHajimu UMEMOTO free(buf2);
2331248aee62SJacques Vidrine return NS_UNAVAIL;
2332a42af91cSHajimu UMEMOTO }
23335342d17fSHajimu UMEMOTO
23345342d17fSHajimu UMEMOTO if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
23355342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL);
23365342d17fSHajimu UMEMOTO free(buf);
23375342d17fSHajimu UMEMOTO free(buf2);
23385342d17fSHajimu UMEMOTO return NS_NOTFOUND;
23395342d17fSHajimu UMEMOTO }
23405342d17fSHajimu UMEMOTO
23415342d17fSHajimu UMEMOTO if (res_searchN(hostname, &q, res) < 0) {
23425cd588cbSHajimu UMEMOTO free(buf);
23435cd588cbSHajimu UMEMOTO free(buf2);
234414436138SMike Karels if (res->res_h_errno == NO_DATA)
234514436138SMike Karels return (NS_ADDRFAMILY);
234614436138SMike Karels return (NS_NOTFOUND);
23475cd588cbSHajimu UMEMOTO }
2348d0d6a2c7SHajimu UMEMOTO /* prefer IPv6 */
2349d0d6a2c7SHajimu UMEMOTO if (q.next) {
23505342d17fSHajimu UMEMOTO ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
23518b8a0b88SEnji Cooper if (ai != NULL) {
2352a42af91cSHajimu UMEMOTO cur->ai_next = ai;
2353a42af91cSHajimu UMEMOTO while (cur && cur->ai_next)
2354a42af91cSHajimu UMEMOTO cur = cur->ai_next;
2355a42af91cSHajimu UMEMOTO }
2356d0d6a2c7SHajimu UMEMOTO }
23578b8a0b88SEnji Cooper if (ai == NULL || pai->ai_family != AF_UNSPEC ||
2358b848a37dSHajimu UMEMOTO (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) != AI_V4MAPPED) {
23595342d17fSHajimu UMEMOTO ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
23608b8a0b88SEnji Cooper if (ai != NULL)
2361a42af91cSHajimu UMEMOTO cur->ai_next = ai;
2362b848a37dSHajimu UMEMOTO }
23635cd588cbSHajimu UMEMOTO free(buf);
23645cd588cbSHajimu UMEMOTO free(buf2);
2365a42af91cSHajimu UMEMOTO if (sentinel.ai_next == NULL)
23665342d17fSHajimu UMEMOTO switch (res->res_h_errno) {
2367a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND:
236814436138SMike Karels return (NS_NOTFOUND);
2369e85324c9SHajimu UMEMOTO case NO_DATA:
237014436138SMike Karels return (NS_ADDRFAMILY);
2371a42af91cSHajimu UMEMOTO case TRY_AGAIN:
237214436138SMike Karels return (NS_TRYAGAIN);
2373a42af91cSHajimu UMEMOTO default:
237414436138SMike Karels return (NS_UNAVAIL);
2375a42af91cSHajimu UMEMOTO }
2376248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next;
237714436138SMike Karels return (NS_SUCCESS);
2378248aee62SJacques Vidrine }
2379248aee62SJacques Vidrine
2380248aee62SJacques Vidrine static void
_sethtent(FILE ** hostf)23819def31dcSHajimu UMEMOTO _sethtent(FILE **hostf)
2382248aee62SJacques Vidrine {
23839def31dcSHajimu UMEMOTO if (!*hostf)
2384a93705b0SJilles Tjoelker *hostf = fopen(_PATH_HOSTS, "re");
2385248aee62SJacques Vidrine else
23869def31dcSHajimu UMEMOTO rewind(*hostf);
2387248aee62SJacques Vidrine }
2388248aee62SJacques Vidrine
2389248aee62SJacques Vidrine static void
_endhtent(FILE ** hostf)23909def31dcSHajimu UMEMOTO _endhtent(FILE **hostf)
2391248aee62SJacques Vidrine {
23929def31dcSHajimu UMEMOTO if (*hostf) {
23939def31dcSHajimu UMEMOTO (void) fclose(*hostf);
23949def31dcSHajimu UMEMOTO *hostf = NULL;
2395248aee62SJacques Vidrine }
2396a42af91cSHajimu UMEMOTO }
2397a42af91cSHajimu UMEMOTO
2398a42af91cSHajimu UMEMOTO static struct addrinfo *
_gethtent(FILE ** hostf,const char * name,const struct addrinfo * pai)23999def31dcSHajimu UMEMOTO _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2400a42af91cSHajimu UMEMOTO {
2401a42af91cSHajimu UMEMOTO char *p;
2402a42af91cSHajimu UMEMOTO char *cp, *tname, *cname;
2403a42af91cSHajimu UMEMOTO struct addrinfo hints, *res0, *res;
2404a42af91cSHajimu UMEMOTO int error;
2405a42af91cSHajimu UMEMOTO const char *addr;
2406a42af91cSHajimu UMEMOTO char hostbuf[8*1024];
2407a42af91cSHajimu UMEMOTO
2408a93705b0SJilles Tjoelker if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
2409248aee62SJacques Vidrine return (NULL);
2410a42af91cSHajimu UMEMOTO again:
24119def31dcSHajimu UMEMOTO if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
2412a42af91cSHajimu UMEMOTO return (NULL);
2413a42af91cSHajimu UMEMOTO if (*p == '#')
2414a42af91cSHajimu UMEMOTO goto again;
24157b6cc404SMaxim Sobolev cp = strpbrk(p, "#\n");
24167b6cc404SMaxim Sobolev if (cp != NULL)
2417a42af91cSHajimu UMEMOTO *cp = '\0';
2418a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, " \t")))
2419a42af91cSHajimu UMEMOTO goto again;
2420a42af91cSHajimu UMEMOTO *cp++ = '\0';
2421a42af91cSHajimu UMEMOTO addr = p;
2422a42af91cSHajimu UMEMOTO cname = NULL;
2423a42af91cSHajimu UMEMOTO /* if this is not something we're looking for, skip it. */
2424a42af91cSHajimu UMEMOTO while (cp && *cp) {
2425a42af91cSHajimu UMEMOTO if (*cp == ' ' || *cp == '\t') {
2426a42af91cSHajimu UMEMOTO cp++;
2427a42af91cSHajimu UMEMOTO continue;
2428a42af91cSHajimu UMEMOTO }
2429a42af91cSHajimu UMEMOTO tname = cp;
2430a42af91cSHajimu UMEMOTO if (cname == NULL)
2431a42af91cSHajimu UMEMOTO cname = cp;
2432a42af91cSHajimu UMEMOTO if ((cp = strpbrk(cp, " \t")) != NULL)
2433a42af91cSHajimu UMEMOTO *cp++ = '\0';
2434a42af91cSHajimu UMEMOTO if (strcasecmp(name, tname) == 0)
2435a42af91cSHajimu UMEMOTO goto found;
2436a42af91cSHajimu UMEMOTO }
2437a42af91cSHajimu UMEMOTO goto again;
2438a42af91cSHajimu UMEMOTO
2439a42af91cSHajimu UMEMOTO found:
2440dd521ef1SHajimu UMEMOTO /* we should not glob socktype/protocol here */
2441dd521ef1SHajimu UMEMOTO memset(&hints, 0, sizeof(hints));
2442dd521ef1SHajimu UMEMOTO hints.ai_family = pai->ai_family;
2443dd521ef1SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM;
2444dd521ef1SHajimu UMEMOTO hints.ai_protocol = 0;
2445a42af91cSHajimu UMEMOTO hints.ai_flags = AI_NUMERICHOST;
2446b848a37dSHajimu UMEMOTO if (pai->ai_family == AF_INET6 &&
2447b848a37dSHajimu UMEMOTO (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED)
2448b848a37dSHajimu UMEMOTO hints.ai_flags |= AI_V4MAPPED;
2449dd521ef1SHajimu UMEMOTO error = getaddrinfo(addr, "0", &hints, &res0);
2450a42af91cSHajimu UMEMOTO if (error)
2451a42af91cSHajimu UMEMOTO goto again;
2452a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
2453a42af91cSHajimu UMEMOTO /* XXX should check all items in the chain */
2454a42af91cSHajimu UMEMOTO if (res0->ai_family == AF_INET6 &&
2455a42af91cSHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2456a42af91cSHajimu UMEMOTO freeaddrinfo(res0);
2457a42af91cSHajimu UMEMOTO goto again;
2458a42af91cSHajimu UMEMOTO }
2459a42af91cSHajimu UMEMOTO #endif
2460a42af91cSHajimu UMEMOTO for (res = res0; res; res = res->ai_next) {
2461a42af91cSHajimu UMEMOTO /* cover it up */
2462a42af91cSHajimu UMEMOTO res->ai_flags = pai->ai_flags;
2463dd521ef1SHajimu UMEMOTO res->ai_socktype = pai->ai_socktype;
2464dd521ef1SHajimu UMEMOTO res->ai_protocol = pai->ai_protocol;
2465a42af91cSHajimu UMEMOTO
2466a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_CANONNAME) {
2467a42af91cSHajimu UMEMOTO if (get_canonname(pai, res, cname) != 0) {
2468a42af91cSHajimu UMEMOTO freeaddrinfo(res0);
2469a42af91cSHajimu UMEMOTO goto again;
2470a42af91cSHajimu UMEMOTO }
2471a42af91cSHajimu UMEMOTO }
2472a42af91cSHajimu UMEMOTO }
2473a42af91cSHajimu UMEMOTO return res0;
2474a42af91cSHajimu UMEMOTO }
2475a42af91cSHajimu UMEMOTO
2476b848a37dSHajimu UMEMOTO static struct addrinfo *
_getht(FILE ** hostf,const char * name,const struct addrinfo * pai,struct addrinfo * cur)2477b848a37dSHajimu UMEMOTO _getht(FILE **hostf, const char *name, const struct addrinfo *pai,
2478b848a37dSHajimu UMEMOTO struct addrinfo *cur)
2479b848a37dSHajimu UMEMOTO {
2480b848a37dSHajimu UMEMOTO struct addrinfo *p;
2481b848a37dSHajimu UMEMOTO
2482b848a37dSHajimu UMEMOTO while ((p = _gethtent(hostf, name, pai)) != NULL) {
2483b848a37dSHajimu UMEMOTO cur->ai_next = p;
2484b848a37dSHajimu UMEMOTO while (cur && cur->ai_next)
2485b848a37dSHajimu UMEMOTO cur = cur->ai_next;
2486b848a37dSHajimu UMEMOTO }
2487b848a37dSHajimu UMEMOTO return (cur);
2488b848a37dSHajimu UMEMOTO }
2489b848a37dSHajimu UMEMOTO
2490a42af91cSHajimu UMEMOTO /*ARGSUSED*/
2491a42af91cSHajimu UMEMOTO static int
_files_getaddrinfo(void * rv,void * cb_data,va_list ap)249290d1d883SHajimu UMEMOTO _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2493a42af91cSHajimu UMEMOTO {
2494248aee62SJacques Vidrine const char *name;
2495248aee62SJacques Vidrine const struct addrinfo *pai;
2496a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur;
24979def31dcSHajimu UMEMOTO FILE *hostf = NULL;
2498a42af91cSHajimu UMEMOTO
2499248aee62SJacques Vidrine name = va_arg(ap, char *);
2500248aee62SJacques Vidrine pai = va_arg(ap, struct addrinfo *);
2501248aee62SJacques Vidrine
2502248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel));
2503a42af91cSHajimu UMEMOTO cur = &sentinel;
2504a42af91cSHajimu UMEMOTO
25059def31dcSHajimu UMEMOTO _sethtent(&hostf);
2506b848a37dSHajimu UMEMOTO if (pai->ai_family == AF_INET6 &&
2507b848a37dSHajimu UMEMOTO (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) == AI_V4MAPPED) {
2508b848a37dSHajimu UMEMOTO struct addrinfo ai0 = *pai;
2509b848a37dSHajimu UMEMOTO
2510b848a37dSHajimu UMEMOTO ai0.ai_flags &= ~AI_V4MAPPED;
2511b848a37dSHajimu UMEMOTO cur = _getht(&hostf, name, &ai0, cur);
2512b848a37dSHajimu UMEMOTO if (sentinel.ai_next == NULL) {
2513b848a37dSHajimu UMEMOTO _sethtent(&hostf);
2514b848a37dSHajimu UMEMOTO ai0.ai_flags |= AI_V4MAPPED;
2515b848a37dSHajimu UMEMOTO cur = _getht(&hostf, name, &ai0, cur);
2516a42af91cSHajimu UMEMOTO }
2517b848a37dSHajimu UMEMOTO } else
2518b848a37dSHajimu UMEMOTO cur = _getht(&hostf, name, pai, cur);
25199def31dcSHajimu UMEMOTO _endhtent(&hostf);
2520a42af91cSHajimu UMEMOTO
2521248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next;
2522248aee62SJacques Vidrine if (sentinel.ai_next == NULL)
2523248aee62SJacques Vidrine return NS_NOTFOUND;
2524248aee62SJacques Vidrine return NS_SUCCESS;
2525a42af91cSHajimu UMEMOTO }
2526a42af91cSHajimu UMEMOTO
2527a42af91cSHajimu UMEMOTO #ifdef YP
2528248aee62SJacques Vidrine /*ARGSUSED*/
2529248aee62SJacques Vidrine static struct addrinfo *
_yphostent(char * line,const struct addrinfo * pai)253090d1d883SHajimu UMEMOTO _yphostent(char *line, const struct addrinfo *pai)
2531248aee62SJacques Vidrine {
2532248aee62SJacques Vidrine struct addrinfo sentinel, *cur;
2533248aee62SJacques Vidrine struct addrinfo hints, *res, *res0;
2534248aee62SJacques Vidrine int error;
2535248aee62SJacques Vidrine char *p = line;
2536248aee62SJacques Vidrine const char *addr, *canonname;
2537248aee62SJacques Vidrine char *nextline;
2538248aee62SJacques Vidrine char *cp;
2539248aee62SJacques Vidrine
2540248aee62SJacques Vidrine addr = canonname = NULL;
2541248aee62SJacques Vidrine
2542248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel));
2543a42af91cSHajimu UMEMOTO cur = &sentinel;
2544a42af91cSHajimu UMEMOTO
2545248aee62SJacques Vidrine nextline:
2546248aee62SJacques Vidrine /* terminate line */
2547248aee62SJacques Vidrine cp = strchr(p, '\n');
2548248aee62SJacques Vidrine if (cp) {
2549248aee62SJacques Vidrine *cp++ = '\0';
2550248aee62SJacques Vidrine nextline = cp;
2551248aee62SJacques Vidrine } else
2552248aee62SJacques Vidrine nextline = NULL;
2553a42af91cSHajimu UMEMOTO
2554248aee62SJacques Vidrine cp = strpbrk(p, " \t");
2555248aee62SJacques Vidrine if (cp == NULL) {
2556248aee62SJacques Vidrine if (canonname == NULL)
2557248aee62SJacques Vidrine return (NULL);
2558248aee62SJacques Vidrine else
2559248aee62SJacques Vidrine goto done;
2560a42af91cSHajimu UMEMOTO }
2561248aee62SJacques Vidrine *cp++ = '\0';
2562a42af91cSHajimu UMEMOTO
2563248aee62SJacques Vidrine addr = p;
2564a42af91cSHajimu UMEMOTO
2565248aee62SJacques Vidrine while (cp && *cp) {
2566248aee62SJacques Vidrine if (*cp == ' ' || *cp == '\t') {
2567248aee62SJacques Vidrine cp++;
2568a42af91cSHajimu UMEMOTO continue;
2569248aee62SJacques Vidrine }
2570248aee62SJacques Vidrine if (!canonname)
2571248aee62SJacques Vidrine canonname = cp;
2572248aee62SJacques Vidrine if ((cp = strpbrk(cp, " \t")) != NULL)
2573248aee62SJacques Vidrine *cp++ = '\0';
2574a42af91cSHajimu UMEMOTO }
2575a42af91cSHajimu UMEMOTO
2576bf838688SHajimu UMEMOTO hints = *pai;
2577248aee62SJacques Vidrine hints.ai_flags = AI_NUMERICHOST;
2578b848a37dSHajimu UMEMOTO if (pai->ai_family == AF_INET6 &&
2579b848a37dSHajimu UMEMOTO (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED)
2580b848a37dSHajimu UMEMOTO hints.ai_flags |= AI_V4MAPPED;
2581bf838688SHajimu UMEMOTO error = getaddrinfo(addr, NULL, &hints, &res0);
2582248aee62SJacques Vidrine if (error == 0) {
2583248aee62SJacques Vidrine for (res = res0; res; res = res->ai_next) {
2584248aee62SJacques Vidrine /* cover it up */
2585248aee62SJacques Vidrine res->ai_flags = pai->ai_flags;
2586248aee62SJacques Vidrine
2587248aee62SJacques Vidrine if (pai->ai_flags & AI_CANONNAME)
2588248aee62SJacques Vidrine (void)get_canonname(pai, res, canonname);
2589248aee62SJacques Vidrine }
2590248aee62SJacques Vidrine } else
2591248aee62SJacques Vidrine res0 = NULL;
2592248aee62SJacques Vidrine if (res0) {
2593248aee62SJacques Vidrine cur->ai_next = res0;
2594a42af91cSHajimu UMEMOTO while (cur && cur->ai_next)
2595a42af91cSHajimu UMEMOTO cur = cur->ai_next;
2596a42af91cSHajimu UMEMOTO }
2597a42af91cSHajimu UMEMOTO
2598248aee62SJacques Vidrine if (nextline) {
2599248aee62SJacques Vidrine p = nextline;
2600248aee62SJacques Vidrine goto nextline;
2601248aee62SJacques Vidrine }
2602a42af91cSHajimu UMEMOTO
2603248aee62SJacques Vidrine done:
2604248aee62SJacques Vidrine return sentinel.ai_next;
2605248aee62SJacques Vidrine }
2606248aee62SJacques Vidrine
2607248aee62SJacques Vidrine /*ARGSUSED*/
2608248aee62SJacques Vidrine static int
_yp_getaddrinfo(void * rv,void * cb_data,va_list ap)260990d1d883SHajimu UMEMOTO _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2610248aee62SJacques Vidrine {
2611248aee62SJacques Vidrine struct addrinfo sentinel, *cur;
2612248aee62SJacques Vidrine struct addrinfo *ai = NULL;
2613d7057edbSHajimu UMEMOTO char *ypbuf;
2614d7057edbSHajimu UMEMOTO int ypbuflen, r;
2615248aee62SJacques Vidrine const char *name;
2616248aee62SJacques Vidrine const struct addrinfo *pai;
2617d7057edbSHajimu UMEMOTO char *ypdomain;
2618d7057edbSHajimu UMEMOTO
2619d7057edbSHajimu UMEMOTO if (_yp_check(&ypdomain) == 0)
2620d7057edbSHajimu UMEMOTO return NS_UNAVAIL;
2621248aee62SJacques Vidrine
2622248aee62SJacques Vidrine name = va_arg(ap, char *);
2623248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *);
2624248aee62SJacques Vidrine
2625248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel));
2626248aee62SJacques Vidrine cur = &sentinel;
2627248aee62SJacques Vidrine
2628b848a37dSHajimu UMEMOTO /* ipnodes.byname can hold both IPv4/v6 */
2629b848a37dSHajimu UMEMOTO r = yp_match(ypdomain, "ipnodes.byname", name,
2630b848a37dSHajimu UMEMOTO (int)strlen(name), &ypbuf, &ypbuflen);
2631b848a37dSHajimu UMEMOTO if (r == 0) {
2632b848a37dSHajimu UMEMOTO ai = _yphostent(ypbuf, pai);
2633b848a37dSHajimu UMEMOTO if (ai) {
2634b848a37dSHajimu UMEMOTO cur->ai_next = ai;
2635b848a37dSHajimu UMEMOTO while (cur && cur->ai_next)
2636b848a37dSHajimu UMEMOTO cur = cur->ai_next;
2637b848a37dSHajimu UMEMOTO }
2638b848a37dSHajimu UMEMOTO free(ypbuf);
2639b848a37dSHajimu UMEMOTO }
2640b848a37dSHajimu UMEMOTO
2641b848a37dSHajimu UMEMOTO if (ai != NULL) {
2642b848a37dSHajimu UMEMOTO struct sockaddr_in6 *sin6;
2643b848a37dSHajimu UMEMOTO
2644b848a37dSHajimu UMEMOTO switch (ai->ai_family) {
2645b848a37dSHajimu UMEMOTO case AF_INET:
2646b848a37dSHajimu UMEMOTO goto done;
2647b848a37dSHajimu UMEMOTO case AF_INET6:
2648b848a37dSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)ai->ai_addr;
2649b848a37dSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2650b848a37dSHajimu UMEMOTO goto done;
2651b848a37dSHajimu UMEMOTO break;
2652b848a37dSHajimu UMEMOTO }
2653b848a37dSHajimu UMEMOTO }
2654b848a37dSHajimu UMEMOTO
2655248aee62SJacques Vidrine /* hosts.byname is only for IPv4 (Solaris8) */
2656b848a37dSHajimu UMEMOTO if (pai->ai_family == AF_UNSPEC || pai->ai_family == AF_INET ||
2657b848a37dSHajimu UMEMOTO ((pai->ai_family == AF_INET6 &&
2658b848a37dSHajimu UMEMOTO (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED) &&
2659b848a37dSHajimu UMEMOTO (ai == NULL || (pai->ai_flags & AI_ALL) == AI_ALL))) {
2660d7057edbSHajimu UMEMOTO r = yp_match(ypdomain, "hosts.byname", name,
2661d7057edbSHajimu UMEMOTO (int)strlen(name), &ypbuf, &ypbuflen);
2662248aee62SJacques Vidrine if (r == 0) {
2663248aee62SJacques Vidrine struct addrinfo ai4;
2664248aee62SJacques Vidrine
2665248aee62SJacques Vidrine ai4 = *pai;
2666b848a37dSHajimu UMEMOTO if (pai->ai_family == AF_UNSPEC)
2667248aee62SJacques Vidrine ai4.ai_family = AF_INET;
2668d7057edbSHajimu UMEMOTO ai = _yphostent(ypbuf, &ai4);
2669248aee62SJacques Vidrine if (ai) {
2670248aee62SJacques Vidrine cur->ai_next = ai;
2671248aee62SJacques Vidrine while (cur && cur->ai_next)
2672248aee62SJacques Vidrine cur = cur->ai_next;
2673248aee62SJacques Vidrine }
2674d7057edbSHajimu UMEMOTO free(ypbuf);
2675248aee62SJacques Vidrine }
2676f8997ca2SHajimu UMEMOTO }
2677248aee62SJacques Vidrine
2678b848a37dSHajimu UMEMOTO done:
2679248aee62SJacques Vidrine if (sentinel.ai_next == NULL) {
26805342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
2681248aee62SJacques Vidrine return NS_NOTFOUND;
2682248aee62SJacques Vidrine }
2683248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next;
2684248aee62SJacques Vidrine return NS_SUCCESS;
2685a42af91cSHajimu UMEMOTO }
2686a42af91cSHajimu UMEMOTO #endif
2687a42af91cSHajimu UMEMOTO
2688a42af91cSHajimu UMEMOTO /* resolver logic */
2689a42af91cSHajimu UMEMOTO
2690a42af91cSHajimu UMEMOTO /*
2691a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and await answer.
2692a42af91cSHajimu UMEMOTO * Returned answer is placed in supplied buffer "answer".
2693a42af91cSHajimu UMEMOTO * Perform preliminary check of answer, returning success only
2694a42af91cSHajimu UMEMOTO * if no error is indicated and the answer count is nonzero.
2695a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error.
2696a42af91cSHajimu UMEMOTO * Error number is left in h_errno.
2697a42af91cSHajimu UMEMOTO *
2698a42af91cSHajimu UMEMOTO * Caller must parse answer and determine whether it answers the question.
2699a42af91cSHajimu UMEMOTO */
2700a42af91cSHajimu UMEMOTO static int
res_queryN(const char * name,struct res_target * target,res_state res)270190d1d883SHajimu UMEMOTO res_queryN(const char *name, struct res_target *target, res_state res)
2702a42af91cSHajimu UMEMOTO {
27035cd588cbSHajimu UMEMOTO u_char *buf;
2704a42af91cSHajimu UMEMOTO HEADER *hp;
2705a42af91cSHajimu UMEMOTO int n;
27061259a17aSHajimu UMEMOTO u_int oflags;
2707a42af91cSHajimu UMEMOTO struct res_target *t;
2708a42af91cSHajimu UMEMOTO int rcode;
2709a42af91cSHajimu UMEMOTO int ancount;
2710a42af91cSHajimu UMEMOTO
2711a42af91cSHajimu UMEMOTO rcode = NOERROR;
2712a42af91cSHajimu UMEMOTO ancount = 0;
2713a42af91cSHajimu UMEMOTO
27145cd588cbSHajimu UMEMOTO buf = malloc(MAXPACKET);
27155cd588cbSHajimu UMEMOTO if (!buf) {
27165342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL);
27175cd588cbSHajimu UMEMOTO return -1;
27185cd588cbSHajimu UMEMOTO }
27195cd588cbSHajimu UMEMOTO
2720a42af91cSHajimu UMEMOTO for (t = target; t; t = t->next) {
2721a42af91cSHajimu UMEMOTO int class, type;
2722a42af91cSHajimu UMEMOTO u_char *answer;
2723a42af91cSHajimu UMEMOTO int anslen;
2724a42af91cSHajimu UMEMOTO
2725a42af91cSHajimu UMEMOTO hp = (HEADER *)(void *)t->answer;
2726a42af91cSHajimu UMEMOTO
2727a42af91cSHajimu UMEMOTO /* make it easier... */
2728b826397aSJun-ichiro itojun Hagino class = t->qclass;
2729b826397aSJun-ichiro itojun Hagino type = t->qtype;
2730a42af91cSHajimu UMEMOTO answer = t->answer;
2731a42af91cSHajimu UMEMOTO anslen = t->anslen;
27321259a17aSHajimu UMEMOTO
27331259a17aSHajimu UMEMOTO oflags = res->_flags;
27341259a17aSHajimu UMEMOTO
27351259a17aSHajimu UMEMOTO again:
27361259a17aSHajimu UMEMOTO hp->rcode = NOERROR; /* default */
27371259a17aSHajimu UMEMOTO
2738a42af91cSHajimu UMEMOTO #ifdef DEBUG
27395342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG)
2740a42af91cSHajimu UMEMOTO printf(";; res_query(%s, %d, %d)\n", name, class, type);
2741a42af91cSHajimu UMEMOTO #endif
2742a42af91cSHajimu UMEMOTO
27435342d17fSHajimu UMEMOTO n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
27445cd588cbSHajimu UMEMOTO buf, MAXPACKET);
27451259a17aSHajimu UMEMOTO if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
27461259a17aSHajimu UMEMOTO (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U)
27475342d17fSHajimu UMEMOTO n = res_nopt(res, n, buf, MAXPACKET, anslen);
2748a42af91cSHajimu UMEMOTO if (n <= 0) {
2749a42af91cSHajimu UMEMOTO #ifdef DEBUG
27505342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG)
2751a42af91cSHajimu UMEMOTO printf(";; res_query: mkquery failed\n");
2752a42af91cSHajimu UMEMOTO #endif
27535cd588cbSHajimu UMEMOTO free(buf);
27545342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
2755a42af91cSHajimu UMEMOTO return (n);
2756a42af91cSHajimu UMEMOTO }
27575342d17fSHajimu UMEMOTO n = res_nsend(res, buf, n, answer, anslen);
2758a42af91cSHajimu UMEMOTO if (n < 0) {
27591259a17aSHajimu UMEMOTO /*
27601259a17aSHajimu UMEMOTO * if the query choked with EDNS0, retry
27611259a17aSHajimu UMEMOTO * without EDNS0
27621259a17aSHajimu UMEMOTO */
27631259a17aSHajimu UMEMOTO if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC))
27641259a17aSHajimu UMEMOTO != 0U &&
27651259a17aSHajimu UMEMOTO ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
27661259a17aSHajimu UMEMOTO res->_flags |= RES_F_EDNS0ERR;
27671259a17aSHajimu UMEMOTO if (res->options & RES_DEBUG)
27681259a17aSHajimu UMEMOTO printf(";; res_nquery: retry without EDNS0\n");
27691259a17aSHajimu UMEMOTO goto again;
27701259a17aSHajimu UMEMOTO }
27711259a17aSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */
2772a42af91cSHajimu UMEMOTO #ifdef DEBUG
27735342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG)
2774a42af91cSHajimu UMEMOTO printf(";; res_query: send error\n");
2775a42af91cSHajimu UMEMOTO #endif
27761259a17aSHajimu UMEMOTO continue;
2777a42af91cSHajimu UMEMOTO }
2778a42af91cSHajimu UMEMOTO
2779d05ad2abSHajimu UMEMOTO if (n > anslen)
278054384cf3SJacques Vidrine hp->rcode = FORMERR; /* XXX not very informative */
27811259a17aSHajimu UMEMOTO if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2782a42af91cSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */
2783a42af91cSHajimu UMEMOTO #ifdef DEBUG
27845342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG)
2785dd521ef1SHajimu UMEMOTO printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2786a42af91cSHajimu UMEMOTO ntohs(hp->ancount));
2787a42af91cSHajimu UMEMOTO #endif
2788a42af91cSHajimu UMEMOTO continue;
2789a42af91cSHajimu UMEMOTO }
2790a42af91cSHajimu UMEMOTO
2791a42af91cSHajimu UMEMOTO ancount += ntohs(hp->ancount);
2792a42af91cSHajimu UMEMOTO
2793a42af91cSHajimu UMEMOTO t->n = n;
2794a42af91cSHajimu UMEMOTO }
2795a42af91cSHajimu UMEMOTO
27965cd588cbSHajimu UMEMOTO free(buf);
27975cd588cbSHajimu UMEMOTO
2798a42af91cSHajimu UMEMOTO if (ancount == 0) {
2799a42af91cSHajimu UMEMOTO switch (rcode) {
2800a42af91cSHajimu UMEMOTO case NXDOMAIN:
28015342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, HOST_NOT_FOUND);
2802a42af91cSHajimu UMEMOTO break;
2803a42af91cSHajimu UMEMOTO case SERVFAIL:
28045342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN);
2805a42af91cSHajimu UMEMOTO break;
2806a42af91cSHajimu UMEMOTO case NOERROR:
28075342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_DATA);
2808a42af91cSHajimu UMEMOTO break;
2809a42af91cSHajimu UMEMOTO case FORMERR:
2810a42af91cSHajimu UMEMOTO case NOTIMP:
2811a42af91cSHajimu UMEMOTO case REFUSED:
2812a42af91cSHajimu UMEMOTO default:
28135342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
2814a42af91cSHajimu UMEMOTO break;
2815a42af91cSHajimu UMEMOTO }
2816a42af91cSHajimu UMEMOTO return (-1);
2817a42af91cSHajimu UMEMOTO }
2818a42af91cSHajimu UMEMOTO return (ancount);
2819a42af91cSHajimu UMEMOTO }
2820a42af91cSHajimu UMEMOTO
2821a42af91cSHajimu UMEMOTO /*
2822a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and retrieve answer in supplied buffer.
2823a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error.
2824a42af91cSHajimu UMEMOTO * If enabled, implement search rules until answer or unrecoverable failure
2825a42af91cSHajimu UMEMOTO * is detected. Error code, if any, is left in h_errno.
2826a42af91cSHajimu UMEMOTO */
2827a42af91cSHajimu UMEMOTO static int
res_searchN(const char * name,struct res_target * target,res_state res)282890d1d883SHajimu UMEMOTO res_searchN(const char *name, struct res_target *target, res_state res)
2829a42af91cSHajimu UMEMOTO {
2830a42af91cSHajimu UMEMOTO const char *cp, * const *domain;
2831a42af91cSHajimu UMEMOTO HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/
2832a42af91cSHajimu UMEMOTO u_int dots;
2833a42af91cSHajimu UMEMOTO int trailing_dot, ret, saved_herrno;
28345ecccefbSHajimu UMEMOTO int got_nodata = 0, got_servfail = 0, root_on_list = 0;
28355ecccefbSHajimu UMEMOTO int tried_as_is = 0;
28365ecccefbSHajimu UMEMOTO int searched = 0;
2837159d2a98SHajimu UMEMOTO char abuf[MAXDNAME];
2838a42af91cSHajimu UMEMOTO
2839a42af91cSHajimu UMEMOTO errno = 0;
28405342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */
2841a42af91cSHajimu UMEMOTO dots = 0;
2842a42af91cSHajimu UMEMOTO for (cp = name; *cp; cp++)
2843a42af91cSHajimu UMEMOTO dots += (*cp == '.');
2844a42af91cSHajimu UMEMOTO trailing_dot = 0;
2845a42af91cSHajimu UMEMOTO if (cp > name && *--cp == '.')
2846a42af91cSHajimu UMEMOTO trailing_dot++;
2847a42af91cSHajimu UMEMOTO
2848a42af91cSHajimu UMEMOTO /*
2849a42af91cSHajimu UMEMOTO * if there aren't any dots, it could be a user-level alias
2850a42af91cSHajimu UMEMOTO */
28515342d17fSHajimu UMEMOTO if (!dots &&
28525342d17fSHajimu UMEMOTO (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL)
28535342d17fSHajimu UMEMOTO return (res_queryN(cp, target, res));
2854a42af91cSHajimu UMEMOTO
2855a42af91cSHajimu UMEMOTO /*
28565ecccefbSHajimu UMEMOTO * If there are enough dots in the name, let's just give it a
28575ecccefbSHajimu UMEMOTO * try 'as is'. The threshold can be set with the "ndots" option.
28585ecccefbSHajimu UMEMOTO * Also, query 'as is', if there is a trailing dot in the name.
2859a42af91cSHajimu UMEMOTO */
2860a42af91cSHajimu UMEMOTO saved_herrno = -1;
28615342d17fSHajimu UMEMOTO if (dots >= res->ndots || trailing_dot) {
28625342d17fSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target, res);
28635ecccefbSHajimu UMEMOTO if (ret > 0 || trailing_dot)
2864a42af91cSHajimu UMEMOTO return (ret);
28653a0b3b67SHajimu UMEMOTO if (errno == ECONNREFUSED) {
28665342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN);
28673a0b3b67SHajimu UMEMOTO return (-1);
28683a0b3b67SHajimu UMEMOTO }
28695342d17fSHajimu UMEMOTO switch (res->res_h_errno) {
28703a0b3b67SHajimu UMEMOTO case NO_DATA:
28713a0b3b67SHajimu UMEMOTO case HOST_NOT_FOUND:
28723a0b3b67SHajimu UMEMOTO break;
2873aebc6ec2SHajimu UMEMOTO case TRY_AGAIN:
2874aebc6ec2SHajimu UMEMOTO if (hp->rcode == SERVFAIL)
2875aebc6ec2SHajimu UMEMOTO break;
2876aebc6ec2SHajimu UMEMOTO /* FALLTHROUGH */
28773a0b3b67SHajimu UMEMOTO default:
28783a0b3b67SHajimu UMEMOTO return (-1);
28793a0b3b67SHajimu UMEMOTO }
28805342d17fSHajimu UMEMOTO saved_herrno = res->res_h_errno;
2881a42af91cSHajimu UMEMOTO tried_as_is++;
2882a42af91cSHajimu UMEMOTO }
2883a42af91cSHajimu UMEMOTO
2884a42af91cSHajimu UMEMOTO /*
2885a42af91cSHajimu UMEMOTO * We do at least one level of search if
2886a42af91cSHajimu UMEMOTO * - there is no dot and RES_DEFNAME is set, or
2887a42af91cSHajimu UMEMOTO * - there is at least one dot, there is no trailing dot,
2888a42af91cSHajimu UMEMOTO * and RES_DNSRCH is set.
2889a42af91cSHajimu UMEMOTO */
28905342d17fSHajimu UMEMOTO if ((!dots && (res->options & RES_DEFNAMES)) ||
28915342d17fSHajimu UMEMOTO (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2892a42af91cSHajimu UMEMOTO int done = 0;
2893a42af91cSHajimu UMEMOTO
28945342d17fSHajimu UMEMOTO for (domain = (const char * const *)res->dnsrch;
2895a42af91cSHajimu UMEMOTO *domain && !done;
2896a42af91cSHajimu UMEMOTO domain++) {
28975ecccefbSHajimu UMEMOTO searched = 1;
28985ecccefbSHajimu UMEMOTO
28995ecccefbSHajimu UMEMOTO if (domain[0][0] == '\0' ||
29005ecccefbSHajimu UMEMOTO (domain[0][0] == '.' && domain[0][1] == '\0'))
29015ecccefbSHajimu UMEMOTO root_on_list++;
29025ecccefbSHajimu UMEMOTO
29035ecccefbSHajimu UMEMOTO if (root_on_list && tried_as_is)
29045ecccefbSHajimu UMEMOTO continue;
2905a42af91cSHajimu UMEMOTO
29065342d17fSHajimu UMEMOTO ret = res_querydomainN(name, *domain, target, res);
2907a42af91cSHajimu UMEMOTO if (ret > 0)
2908a42af91cSHajimu UMEMOTO return (ret);
2909a42af91cSHajimu UMEMOTO
2910a42af91cSHajimu UMEMOTO /*
2911a42af91cSHajimu UMEMOTO * If no server present, give up.
2912a42af91cSHajimu UMEMOTO * If name isn't found in this domain,
2913a42af91cSHajimu UMEMOTO * keep trying higher domains in the search list
2914a42af91cSHajimu UMEMOTO * (if that's enabled).
2915a42af91cSHajimu UMEMOTO * On a NO_DATA error, keep trying, otherwise
2916a42af91cSHajimu UMEMOTO * a wildcard entry of another type could keep us
2917a42af91cSHajimu UMEMOTO * from finding this entry higher in the domain.
2918a42af91cSHajimu UMEMOTO * If we get some other error (negative answer or
2919a42af91cSHajimu UMEMOTO * server failure), then stop searching up,
2920a42af91cSHajimu UMEMOTO * but try the input name below in case it's
2921a42af91cSHajimu UMEMOTO * fully-qualified.
2922a42af91cSHajimu UMEMOTO */
2923a42af91cSHajimu UMEMOTO if (errno == ECONNREFUSED) {
29245342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN);
2925a42af91cSHajimu UMEMOTO return (-1);
2926a42af91cSHajimu UMEMOTO }
2927a42af91cSHajimu UMEMOTO
29285342d17fSHajimu UMEMOTO switch (res->res_h_errno) {
2929a42af91cSHajimu UMEMOTO case NO_DATA:
2930a42af91cSHajimu UMEMOTO got_nodata++;
2931a42af91cSHajimu UMEMOTO /* FALLTHROUGH */
2932a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND:
2933a42af91cSHajimu UMEMOTO /* keep trying */
2934a42af91cSHajimu UMEMOTO break;
2935a42af91cSHajimu UMEMOTO case TRY_AGAIN:
2936aebc6ec2SHajimu UMEMOTO got_servfail++;
2937a42af91cSHajimu UMEMOTO if (hp->rcode == SERVFAIL) {
2938a42af91cSHajimu UMEMOTO /* try next search element, if any */
2939a42af91cSHajimu UMEMOTO break;
2940a42af91cSHajimu UMEMOTO }
2941a42af91cSHajimu UMEMOTO /* FALLTHROUGH */
2942a42af91cSHajimu UMEMOTO default:
2943a42af91cSHajimu UMEMOTO /* anything else implies that we're done */
2944a42af91cSHajimu UMEMOTO done++;
2945a42af91cSHajimu UMEMOTO }
2946a42af91cSHajimu UMEMOTO /*
2947a42af91cSHajimu UMEMOTO * if we got here for some reason other than DNSRCH,
2948a42af91cSHajimu UMEMOTO * we only wanted one iteration of the loop, so stop.
2949a42af91cSHajimu UMEMOTO */
29505342d17fSHajimu UMEMOTO if (!(res->options & RES_DNSRCH))
2951a42af91cSHajimu UMEMOTO done++;
2952a42af91cSHajimu UMEMOTO }
2953a42af91cSHajimu UMEMOTO }
2954a42af91cSHajimu UMEMOTO
29555342d17fSHajimu UMEMOTO switch (res->res_h_errno) {
29563a0b3b67SHajimu UMEMOTO case NO_DATA:
29573a0b3b67SHajimu UMEMOTO case HOST_NOT_FOUND:
29583a0b3b67SHajimu UMEMOTO break;
2959aebc6ec2SHajimu UMEMOTO case TRY_AGAIN:
2960aebc6ec2SHajimu UMEMOTO if (hp->rcode == SERVFAIL)
2961aebc6ec2SHajimu UMEMOTO break;
2962aebc6ec2SHajimu UMEMOTO /* FALLTHROUGH */
29633a0b3b67SHajimu UMEMOTO default:
29643a0b3b67SHajimu UMEMOTO goto giveup;
29653a0b3b67SHajimu UMEMOTO }
29663a0b3b67SHajimu UMEMOTO
2967a42af91cSHajimu UMEMOTO /*
29685ecccefbSHajimu UMEMOTO * If the query has not already been tried as is then try it
29695ecccefbSHajimu UMEMOTO * unless RES_NOTLDQUERY is set and there were no dots.
2970a42af91cSHajimu UMEMOTO */
29715342d17fSHajimu UMEMOTO if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) &&
29725ecccefbSHajimu UMEMOTO !(tried_as_is || root_on_list)) {
29735342d17fSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target, res);
2974a42af91cSHajimu UMEMOTO if (ret > 0)
2975a42af91cSHajimu UMEMOTO return (ret);
2976a42af91cSHajimu UMEMOTO }
2977a42af91cSHajimu UMEMOTO
2978a42af91cSHajimu UMEMOTO /*
2979a42af91cSHajimu UMEMOTO * if we got here, we didn't satisfy the search.
2980a42af91cSHajimu UMEMOTO * if we did an initial full query, return that query's h_errno
2981a42af91cSHajimu UMEMOTO * (note that we wouldn't be here if that query had succeeded).
2982a42af91cSHajimu UMEMOTO * else if we ever got a nodata, send that back as the reason.
2983a42af91cSHajimu UMEMOTO * else send back meaningless h_errno, that being the one from
2984a42af91cSHajimu UMEMOTO * the last DNSRCH we did.
2985a42af91cSHajimu UMEMOTO */
29863a0b3b67SHajimu UMEMOTO giveup:
2987a42af91cSHajimu UMEMOTO if (saved_herrno != -1)
29885342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, saved_herrno);
2989a42af91cSHajimu UMEMOTO else if (got_nodata)
29905342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_DATA);
2991a42af91cSHajimu UMEMOTO else if (got_servfail)
29925342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN);
2993a42af91cSHajimu UMEMOTO return (-1);
2994a42af91cSHajimu UMEMOTO }
2995a42af91cSHajimu UMEMOTO
2996a42af91cSHajimu UMEMOTO /*
2997a42af91cSHajimu UMEMOTO * Perform a call on res_query on the concatenation of name and domain,
2998a42af91cSHajimu UMEMOTO * removing a trailing dot from name if domain is NULL.
2999a42af91cSHajimu UMEMOTO */
3000a42af91cSHajimu UMEMOTO static int
res_querydomainN(const char * name,const char * domain,struct res_target * target,res_state res)300190d1d883SHajimu UMEMOTO res_querydomainN(const char *name, const char *domain,
300290d1d883SHajimu UMEMOTO struct res_target *target, res_state res)
3003a42af91cSHajimu UMEMOTO {
3004a42af91cSHajimu UMEMOTO char nbuf[MAXDNAME];
3005a42af91cSHajimu UMEMOTO const char *longname = nbuf;
3006a42af91cSHajimu UMEMOTO size_t n, d;
3007a42af91cSHajimu UMEMOTO
3008a42af91cSHajimu UMEMOTO #ifdef DEBUG
30095342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG)
3010a42af91cSHajimu UMEMOTO printf(";; res_querydomain(%s, %s)\n",
3011a42af91cSHajimu UMEMOTO name, domain?domain:"<Nil>");
3012a42af91cSHajimu UMEMOTO #endif
3013a42af91cSHajimu UMEMOTO if (domain == NULL) {
3014a42af91cSHajimu UMEMOTO /*
3015a42af91cSHajimu UMEMOTO * Check for trailing '.';
3016a42af91cSHajimu UMEMOTO * copy without '.' if present.
3017a42af91cSHajimu UMEMOTO */
3018a42af91cSHajimu UMEMOTO n = strlen(name);
3019a42af91cSHajimu UMEMOTO if (n >= MAXDNAME) {
30205342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
3021a42af91cSHajimu UMEMOTO return (-1);
3022a42af91cSHajimu UMEMOTO }
3023a42af91cSHajimu UMEMOTO if (n > 0 && name[--n] == '.') {
3024a42af91cSHajimu UMEMOTO strncpy(nbuf, name, n);
3025a42af91cSHajimu UMEMOTO nbuf[n] = '\0';
3026a42af91cSHajimu UMEMOTO } else
3027a42af91cSHajimu UMEMOTO longname = name;
3028a42af91cSHajimu UMEMOTO } else {
3029a42af91cSHajimu UMEMOTO n = strlen(name);
3030a42af91cSHajimu UMEMOTO d = strlen(domain);
3031a42af91cSHajimu UMEMOTO if (n + d + 1 >= MAXDNAME) {
30325342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY);
3033a42af91cSHajimu UMEMOTO return (-1);
3034a42af91cSHajimu UMEMOTO }
3035dd521ef1SHajimu UMEMOTO snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
3036a42af91cSHajimu UMEMOTO }
30375342d17fSHajimu UMEMOTO return (res_queryN(longname, target, res));
3038a42af91cSHajimu UMEMOTO }
3039