2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
33 * Issues to be discussed:
34 * - Thread safe-ness must be checked.
35 * - Return values. There are nonstandard return values defined and used
36 * in the source code. This is because RFC2553 is silent about which error
37 * code must be returned for which situation.
39 * - We use getipnodebyname() just for thread-safeness. There's no intent
40 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
42 * - The code filters out AFs that are not supported by the kernel,
43 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
44 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
49 * Mingw64 has its own implementation of getaddrinfo, mingw32 no
58 #include <pcap-stdinc.h>
60 #include <sys/sysctl.h>
63 #include <arpa/nameser.h>
72 #ifndef HAVE_PORTABLE_PROTOTYPE
73 #include "cdecl_ext.h"
76 #ifndef HAVE_U_INT32_T
80 #ifndef HAVE_SOCKADDR_STORAGE
82 #include "sockstorage.h"
86 #ifdef NEED_ADDRINFO_H
94 #if defined(__KAME__) && defined(INET6)
104 static int translate
= NO
;
105 static struct in6_addr faith_prefix
= IN6ADDR_ANY_INIT
;
108 static const char in_addrany
[] = { 0, 0, 0, 0 };
109 static const char in6_addrany
[] = {
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
112 static const char in_loopback
[] = { 127, 0, 0, 1 };
113 static const char in6_loopback
[] = {
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
121 u_int32_t si_scope_id
;
124 static const struct afd
{
129 const char *a_addrany
;
130 const char *a_loopback
;
134 {PF_INET6
, sizeof(struct in6_addr
),
135 sizeof(struct sockaddr_in6
),
136 offsetof(struct sockaddr_in6
, sin6_addr
),
137 in6_addrany
, in6_loopback
, 1},
139 {PF_INET
, sizeof(struct in_addr
),
140 sizeof(struct sockaddr_in
),
141 offsetof(struct sockaddr_in
, sin_addr
),
142 in_addrany
, in_loopback
, 0},
143 {0, 0, 0, 0, NULL
, NULL
, 0},
150 const char *e_protostr
;
152 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
153 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
154 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
157 static const struct explore explore
[] = {
159 { PF_LOCAL
, 0, ANY
, ANY
, NULL
, 0x01 },
162 { PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
163 { PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
164 { PF_INET6
, SOCK_RAW
, ANY
, NULL
, 0x05 },
166 { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
167 { PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
168 { PF_INET
, SOCK_RAW
, ANY
, NULL
, 0x05 },
169 { -1, 0, 0, NULL
, 0 },
179 static int str_isnumber
__P((const char *));
180 static int explore_fqdn
__P((const struct addrinfo
*, const char *,
181 const char *, struct addrinfo
**));
182 static int explore_null
__P((const struct addrinfo
*, const char *,
183 const char *, struct addrinfo
**));
184 static int explore_numeric
__P((const struct addrinfo
*, const char *,
185 const char *, struct addrinfo
**));
186 static int explore_numeric_scope
__P((const struct addrinfo
*, const char *,
187 const char *, struct addrinfo
**));
188 static int get_name
__P((const char *, const struct afd
*, struct addrinfo
**,
189 char *, const struct addrinfo
*, const char *));
190 static int get_canonname
__P((const struct addrinfo
*,
191 struct addrinfo
*, const char *));
192 static struct addrinfo
*get_ai
__P((const struct addrinfo
*,
193 const struct afd
*, const char *));
194 static int get_portmatch
__P((const struct addrinfo
*, const char *));
195 static int get_port
__P((struct addrinfo
*, const char *, int));
196 static const struct afd
*find_afd
__P((int));
198 static char *ai_errlist
[] = {
200 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
201 "Temporary failure in name resolution", /* EAI_AGAIN */
202 "Invalid value for ai_flags", /* EAI_BADFLAGS */
203 "Non-recoverable failure in name resolution", /* EAI_FAIL */
204 "ai_family not supported", /* EAI_FAMILY */
205 "Memory allocation failure", /* EAI_MEMORY */
206 "No address associated with hostname", /* EAI_NODATA */
207 "hostname nor servname provided, or not known", /* EAI_NONAME */
208 "servname not supported for ai_socktype", /* EAI_SERVICE */
209 "ai_socktype not supported", /* EAI_SOCKTYPE */
210 "System error returned in errno", /* EAI_SYSTEM */
211 "Invalid value for hints", /* EAI_BADHINTS */
212 "Resolved protocol is unknown", /* EAI_PROTOCOL */
213 "Unknown error", /* EAI_MAX */
216 /* XXX macros that make external reference is BAD. */
218 #define GET_AI(ai, afd, addr) \
220 /* external reference: pai, error, and label free */ \
221 (ai) = get_ai(pai, (afd), (addr)); \
222 if ((ai) == NULL) { \
223 error = EAI_MEMORY; \
228 #define GET_PORT(ai, serv) \
230 /* external reference: error and label free */ \
231 error = get_port((ai), (serv), 0); \
236 #define GET_CANONNAME(ai, str) \
238 /* external reference: pai, error and label free */ \
239 error = get_canonname(pai, (ai), (str)); \
246 /* external reference: error, and label bad */ \
251 #define MATCH_FAMILY(x, y, w) \
252 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
253 #define MATCH(x, y, w) \
254 ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
256 #if defined(DEFINE_ADDITIONAL_IPV6_STUFF)
261 if (ecode
< 0 || ecode
> EAI_MAX
)
263 return ai_errlist
[ecode
];
271 struct addrinfo
*next
;
275 if (ai
->ai_canonname
)
276 free(ai
->ai_canonname
);
277 /* no need to free(ai->ai_addr) */
279 } while ((ai
= next
) != NULL
);
296 getaddrinfo(hostname
, servname
, hints
, res
)
297 const char *hostname
, *servname
;
298 const struct addrinfo
*hints
;
299 struct addrinfo
**res
;
301 struct addrinfo sentinel
;
302 struct addrinfo
*cur
;
306 struct addrinfo
*pai
;
307 const struct afd
*afd
;
308 const struct explore
*ex
;
311 static int firsttime
= 1;
314 /* translator hack */
315 char *q
= getenv("GAI");
316 if (q
&& inet_pton(AF_INET6
, q
, &faith_prefix
) == 1)
322 sentinel
.ai_next
= NULL
;
326 pai
->ai_family
= PF_UNSPEC
;
327 pai
->ai_socktype
= ANY
;
328 pai
->ai_protocol
= ANY
;
330 pai
->ai_canonname
= NULL
;
334 if (hostname
== NULL
&& servname
== NULL
)
337 /* error check for hints */
338 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
339 hints
->ai_addr
|| hints
->ai_next
)
340 ERR(EAI_BADHINTS
); /* xxx */
341 if (hints
->ai_flags
& ~AI_MASK
)
343 switch (hints
->ai_family
) {
353 memcpy(pai
, hints
, sizeof(*pai
));
356 * if both socktype/protocol are specified, check if they
357 * are meaningful combination.
359 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
360 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
361 if (pai
->ai_family
!= ex
->e_af
)
363 if (ex
->e_socktype
== ANY
)
365 if (ex
->e_protocol
== ANY
)
367 if (pai
->ai_socktype
== ex
->e_socktype
368 && pai
->ai_protocol
!= ex
->e_protocol
) {
376 * check for special cases. (1) numeric servname is disallowed if
377 * socktype/protocol are left unspecified. (2) servname is disallowed
378 * for raw and other inet{,6} sockets.
380 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
382 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
387 if (pai
->ai_family
== PF_UNSPEC
) {
389 pai
->ai_family
= PF_INET6
;
391 pai
->ai_family
= PF_INET
;
394 error
= get_portmatch(pai
, servname
);
403 /* NULL hostname, or numeric hostname */
404 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
407 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
409 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
411 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
414 if (pai
->ai_family
== PF_UNSPEC
)
415 pai
->ai_family
= ex
->e_af
;
416 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
417 pai
->ai_socktype
= ex
->e_socktype
;
418 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
419 pai
->ai_protocol
= ex
->e_protocol
;
421 if (hostname
== NULL
)
422 error
= explore_null(pai
, hostname
, servname
, &cur
->ai_next
);
424 error
= explore_numeric_scope(pai
, hostname
, servname
, &cur
->ai_next
);
429 while (cur
&& cur
->ai_next
)
435 * If numreic representation of AF1 can be interpreted as FQDN
436 * representation of AF2, we need to think again about the code below.
438 if (sentinel
.ai_next
)
441 if (pai
->ai_flags
& AI_NUMERICHOST
)
443 if (hostname
== NULL
)
447 * hostname as alphabetical name.
448 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
451 for (afd
= afdl
; afd
->a_af
; afd
++) {
454 if (!MATCH_FAMILY(pai
->ai_family
, afd
->a_af
, 1))
457 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
460 if (pai
->ai_family
== PF_UNSPEC
)
461 pai
->ai_family
= afd
->a_af
;
463 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
465 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
466 WILD_SOCKTYPE(ex
))) {
469 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
470 WILD_PROTOCOL(ex
))) {
474 if (pai
->ai_family
== PF_UNSPEC
)
475 pai
->ai_family
= ex
->e_af
;
476 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
477 pai
->ai_socktype
= ex
->e_socktype
;
478 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
479 pai
->ai_protocol
= ex
->e_protocol
;
481 error
= explore_fqdn(pai
, hostname
, servname
,
484 while (cur
&& cur
->ai_next
)
490 if (sentinel
.ai_next
)
496 if (sentinel
.ai_next
) {
498 *res
= sentinel
.ai_next
;
505 if (sentinel
.ai_next
)
506 freeaddrinfo(sentinel
.ai_next
);
512 * FQDN hostname, DNS lookup
515 explore_fqdn(pai
, hostname
, servname
, res
)
516 const struct addrinfo
*pai
;
517 const char *hostname
;
518 const char *servname
;
519 struct addrinfo
**res
;
524 char **aplist
= NULL
, *apbuf
= NULL
;
526 struct addrinfo sentinel
, *cur
;
528 #ifndef USE_GETIPNODEBY
531 const struct afd
*afd
;
535 sentinel
.ai_next
= NULL
;
539 * Do not filter unsupported AFs here. We need to honor content of
540 * databases (/etc/hosts, DNS and others). Otherwise we cannot
541 * replace gethostbyname() by getaddrinfo().
545 * if the servname does not match socktype/protocol, ignore it.
547 if (get_portmatch(pai
, servname
) != 0)
550 afd
= find_afd(pai
->ai_family
);
553 * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
554 * rather than hardcoding it. we may need to add AI_ADDRCONFIG
555 * handling code by ourselves in case we don't have getipnodebyname().
557 #ifdef USE_GETIPNODEBY
558 hp
= getipnodebyname(hostname
, pai
->ai_family
, AI_ADDRCONFIG
, &h_error
);
560 #ifdef HAVE_GETHOSTBYNAME2
561 hp
= gethostbyname2(hostname
, pai
->ai_family
);
563 if (pai
->ai_family
!= AF_INET
)
565 hp
= gethostbyname(hostname
);
571 #endif /*HAVE_GETHOSTBYNAME2*/
572 #endif /*USE_GETIPNODEBY*/
589 } else if ((hp
->h_name
== NULL
) || (hp
->h_name
[0] == 0)
590 || (hp
->h_addr_list
[0] == NULL
)) {
591 #ifdef USE_GETIPNODEBY
601 #ifdef USE_GETIPNODEBY
602 aplist
= hp
->h_addr_list
;
605 * hp will be overwritten if we use gethostbyname2().
606 * always deep copy for simplification.
608 for (naddrs
= 0; hp
->h_addr_list
[naddrs
] != NULL
; naddrs
++)
611 aplist
= (char **)malloc(sizeof(aplist
[0]) * naddrs
);
612 apbuf
= (char *)malloc(hp
->h_length
* naddrs
);
613 if (aplist
== NULL
|| apbuf
== NULL
) {
617 memset(aplist
, 0, sizeof(aplist
[0]) * naddrs
);
618 for (i
= 0; i
< naddrs
; i
++) {
619 if (hp
->h_addr_list
[i
] == NULL
) {
623 memcpy(&apbuf
[i
* hp
->h_length
], hp
->h_addr_list
[i
],
625 aplist
[i
] = &apbuf
[i
* hp
->h_length
];
629 for (i
= 0; aplist
[i
] != NULL
; i
++) {
634 && IN6_IS_ADDR_V4MAPPED((struct in6_addr
*)ap
)) {
636 ap
= ap
+ sizeof(struct in6_addr
)
637 - sizeof(struct in_addr
);
641 if (af
!= pai
->ai_family
)
644 if ((pai
->ai_flags
& AI_CANONNAME
) == 0) {
645 GET_AI(cur
->ai_next
, afd
, ap
);
646 GET_PORT(cur
->ai_next
, servname
);
649 * if AI_CANONNAME and if reverse lookup
650 * fail, return ai anyway to pacify
651 * calling application.
653 * XXX getaddrinfo() is a name->address
654 * translation function, and it looks
655 * strange that we do addr->name
658 get_name(ap
, afd
, &cur
->ai_next
,
662 while (cur
&& cur
->ai_next
)
666 *res
= sentinel
.ai_next
;
670 #ifdef USE_GETIPNODEBY
678 if (sentinel
.ai_next
)
679 freeaddrinfo(sentinel
.ai_next
);
685 * passive socket -> anyaddr (0.0.0.0 or ::)
686 * non-passive socket -> localhost (127.0.0.1 or ::1)
689 explore_null(pai
, hostname
, servname
, res
)
690 const struct addrinfo
*pai
;
691 const char *hostname
;
692 const char *servname
;
693 struct addrinfo
**res
;
696 const struct afd
*afd
;
697 struct addrinfo
*cur
;
698 struct addrinfo sentinel
;
702 sentinel
.ai_next
= NULL
;
706 * filter out AFs that are not supported by the kernel
709 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
717 * if the servname does not match socktype/protocol, ignore it.
719 if (get_portmatch(pai
, servname
) != 0)
722 afd
= find_afd(pai
->ai_family
);
724 if (pai
->ai_flags
& AI_PASSIVE
) {
725 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
727 * GET_CANONNAME(cur->ai_next, "anyaddr");
729 GET_PORT(cur
->ai_next
, servname
);
731 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
733 * GET_CANONNAME(cur->ai_next, "localhost");
735 GET_PORT(cur
->ai_next
, servname
);
739 *res
= sentinel
.ai_next
;
743 if (sentinel
.ai_next
)
744 freeaddrinfo(sentinel
.ai_next
);
752 explore_numeric(pai
, hostname
, servname
, res
)
753 const struct addrinfo
*pai
;
754 const char *hostname
;
755 const char *servname
;
756 struct addrinfo
**res
;
758 const struct afd
*afd
;
759 struct addrinfo
*cur
;
760 struct addrinfo sentinel
;
766 sentinel
.ai_next
= NULL
;
770 * if the servname does not match socktype/protocol, ignore it.
772 if (get_portmatch(pai
, servname
) != 0)
775 afd
= find_afd(pai
->ai_family
);
776 flags
= pai
->ai_flags
;
778 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
786 v4a
= (u_int32_t
)ntohl(((struct in_addr
*)pton
)->s_addr
);
787 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
788 flags
&= ~AI_CANONNAME
;
789 v4a
>>= IN_CLASSA_NSHIFT
;
790 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
791 flags
&= ~AI_CANONNAME
;
795 pfx
= ((struct in6_addr
*)pton
)->s6_addr
[0];
796 if (pfx
== 0 || pfx
== 0xfe || pfx
== 0xff)
797 flags
&= ~AI_CANONNAME
;
802 if (pai
->ai_family
== afd
->a_af
||
803 pai
->ai_family
== PF_UNSPEC
/*?*/) {
804 if ((flags
& AI_CANONNAME
) == 0) {
805 GET_AI(cur
->ai_next
, afd
, pton
);
806 GET_PORT(cur
->ai_next
, servname
);
809 * if AI_CANONNAME and if reverse lookup
810 * fail, return ai anyway to pacify
811 * calling application.
813 * XXX getaddrinfo() is a name->address
814 * translation function, and it looks
815 * strange that we do addr->name
818 get_name(pton
, afd
, &cur
->ai_next
,
819 pton
, pai
, servname
);
821 while (cur
&& cur
->ai_next
)
824 ERR(EAI_FAMILY
); /*xxx*/
827 *res
= sentinel
.ai_next
;
832 if (sentinel
.ai_next
)
833 freeaddrinfo(sentinel
.ai_next
);
838 * numeric hostname with scope
841 explore_numeric_scope(pai
, hostname
, servname
, res
)
842 const struct addrinfo
*pai
;
843 const char *hostname
;
844 const char *servname
;
845 struct addrinfo
**res
;
847 #ifndef SCOPE_DELIMITER
848 return explore_numeric(pai
, hostname
, servname
, res
);
850 const struct afd
*afd
;
851 struct addrinfo
*cur
;
853 char *cp
, *hostname2
= NULL
;
855 struct sockaddr_in6
*sin6
;
858 * if the servname does not match socktype/protocol, ignore it.
860 if (get_portmatch(pai
, servname
) != 0)
863 afd
= find_afd(pai
->ai_family
);
865 return explore_numeric(pai
, hostname
, servname
, res
);
867 cp
= strchr(hostname
, SCOPE_DELIMITER
);
869 return explore_numeric(pai
, hostname
, servname
, res
);
872 * Handle special case of <scoped_address><delimiter><scope id>
874 hostname2
= strdup(hostname
);
875 if (hostname2
== NULL
)
877 /* terminate at the delimiter */
878 hostname2
[cp
- hostname
] = '\0';
881 switch (pai
->ai_family
) {
884 scope
= if_nametoindex(cp
);
893 error
= explore_numeric(pai
, hostname2
, servname
, res
);
895 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
896 if (cur
->ai_family
!= AF_INET6
)
898 sin6
= (struct sockaddr_in6
*)cur
->ai_addr
;
899 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
900 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
->sin6_addr
))
901 sin6
->sin6_scope_id
= scope
;
912 get_name(addr
, afd
, res
, numaddr
, pai
, servname
)
914 const struct afd
*afd
;
915 struct addrinfo
**res
;
917 const struct addrinfo
*pai
;
918 const char *servname
;
920 struct hostent
*hp
= NULL
;
921 struct addrinfo
*cur
= NULL
;
923 char *ap
= NULL
, *cn
= NULL
;
924 #ifdef USE_GETIPNODEBY
927 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
929 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
931 if (hp
&& hp
->h_name
&& hp
->h_name
[0] && hp
->h_addr_list
[0]) {
932 #ifdef USE_GETIPNODEBY
933 GET_AI(cur
, afd
, hp
->h_addr_list
[0]);
934 GET_PORT(cur
, servname
);
935 GET_CANONNAME(cur
, hp
->h_name
);
937 /* hp will be damaged if we use gethostbyaddr() */
938 if ((ap
= (char *)malloc(hp
->h_length
)) == NULL
) {
942 memcpy(ap
, hp
->h_addr_list
[0], hp
->h_length
);
943 if ((cn
= strdup(hp
->h_name
)) == NULL
) {
948 GET_AI(cur
, afd
, ap
);
949 GET_PORT(cur
, servname
);
950 GET_CANONNAME(cur
, cn
);
955 GET_AI(cur
, afd
, numaddr
);
956 GET_PORT(cur
, servname
);
959 #ifdef USE_GETIPNODEBY
972 #ifdef USE_GETIPNODEBY
981 get_canonname(pai
, ai
, str
)
982 const struct addrinfo
*pai
;
986 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
987 ai
->ai_canonname
= strdup(str
);
988 if (ai
->ai_canonname
== NULL
)
994 static struct addrinfo
*
995 get_ai(pai
, afd
, addr
)
996 const struct addrinfo
*pai
;
997 const struct afd
*afd
;
1001 struct addrinfo
*ai
;
1003 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
)
1004 + (afd
->a_socklen
));
1008 memcpy(ai
, pai
, sizeof(struct addrinfo
));
1009 ai
->ai_addr
= (struct sockaddr
*)(ai
+ 1);
1010 memset(ai
->ai_addr
, 0, afd
->a_socklen
);
1011 #ifdef HAVE_SOCKADDR_SA_LEN
1012 ai
->ai_addr
->sa_len
= afd
->a_socklen
;
1014 ai
->ai_addrlen
= afd
->a_socklen
;
1015 ai
->ai_addr
->sa_family
= ai
->ai_family
= afd
->a_af
;
1016 p
= (char *)(ai
->ai_addr
);
1017 memcpy(p
+ afd
->a_off
, addr
, afd
->a_addrlen
);
1022 get_portmatch(ai
, servname
)
1023 const struct addrinfo
*ai
;
1024 const char *servname
;
1027 /* get_port does not touch first argument. when matchonly == 1. */
1028 return get_port((struct addrinfo
*)ai
, servname
, 1);
1032 get_port(ai
, servname
, matchonly
)
1033 struct addrinfo
*ai
;
1034 const char *servname
;
1042 if (servname
== NULL
)
1044 switch (ai
->ai_family
) {
1054 switch (ai
->ai_socktype
) {
1065 return EAI_SOCKTYPE
;
1068 if (str_isnumber(servname
)) {
1071 port
= htons(atoi(servname
));
1072 if (port
< 0 || port
> 65535)
1075 switch (ai
->ai_socktype
) {
1087 if ((sp
= getservbyname(servname
, proto
)) == NULL
)
1093 switch (ai
->ai_family
) {
1095 ((struct sockaddr_in
*)ai
->ai_addr
)->sin_port
= port
;
1099 ((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_port
= port
;
1108 static const struct afd
*
1112 const struct afd
*afd
;
1114 if (af
== PF_UNSPEC
)
1116 for (afd
= afdl
; afd
->a_af
; afd
++) {
1117 if (afd
->a_af
== af
)
1124 #endif /*__MING64__*/