]>
The Tcpdump Group git mirrors - libpcap/blob - Win32/Src/getaddrinfo.c
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
53 static const char rcsid
[] _U_
=
54 "@(#) $Header: /tcpdump/master/libpcap/Win32/Src/getaddrinfo.c,v 1.3 2008-09-15 23:37:51 guy Exp $";
57 #include <pcap-stdinc.h>
59 #include <sys/sysctl.h>
62 #include <arpa/nameser.h>
71 #ifndef HAVE_PORTABLE_PROTOTYPE
72 #include "cdecl_ext.h"
75 #ifndef HAVE_U_INT32_T
79 #ifndef HAVE_SOCKADDR_STORAGE
81 #include "sockstorage.h"
85 #ifdef NEED_ADDRINFO_H
93 #if defined(__KAME__) && defined(INET6)
103 static int translate
= NO
;
104 static struct in6_addr faith_prefix
= IN6ADDR_ANY_INIT
;
107 static const char in_addrany
[] = { 0, 0, 0, 0 };
108 static const char in6_addrany
[] = {
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
111 static const char in_loopback
[] = { 127, 0, 0, 1 };
112 static const char in6_loopback
[] = {
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
120 u_int32_t si_scope_id
;
123 static const struct afd
{
128 const char *a_addrany
;
129 const char *a_loopback
;
133 {PF_INET6
, sizeof(struct in6_addr
),
134 sizeof(struct sockaddr_in6
),
135 offsetof(struct sockaddr_in6
, sin6_addr
),
136 in6_addrany
, in6_loopback
, 1},
138 {PF_INET
, sizeof(struct in_addr
),
139 sizeof(struct sockaddr_in
),
140 offsetof(struct sockaddr_in
, sin_addr
),
141 in_addrany
, in_loopback
, 0},
142 {0, 0, 0, 0, NULL
, NULL
, 0},
149 const char *e_protostr
;
151 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
152 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
153 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
156 static const struct explore explore
[] = {
158 { PF_LOCAL
, 0, ANY
, ANY
, NULL
, 0x01 },
161 { PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
162 { PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
163 { PF_INET6
, SOCK_RAW
, ANY
, NULL
, 0x05 },
165 { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
166 { PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
167 { PF_INET
, SOCK_RAW
, ANY
, NULL
, 0x05 },
168 { -1, 0, 0, NULL
, 0 },
178 static int str_isnumber
__P((const char *));
179 static int explore_fqdn
__P((const struct addrinfo
*, const char *,
180 const char *, struct addrinfo
**));
181 static int explore_null
__P((const struct addrinfo
*, const char *,
182 const char *, struct addrinfo
**));
183 static int explore_numeric
__P((const struct addrinfo
*, const char *,
184 const char *, struct addrinfo
**));
185 static int explore_numeric_scope
__P((const struct addrinfo
*, const char *,
186 const char *, struct addrinfo
**));
187 static int get_name
__P((const char *, const struct afd
*, struct addrinfo
**,
188 char *, const struct addrinfo
*, const char *));
189 static int get_canonname
__P((const struct addrinfo
*,
190 struct addrinfo
*, const char *));
191 static struct addrinfo
*get_ai
__P((const struct addrinfo
*,
192 const struct afd
*, const char *));
193 static int get_portmatch
__P((const struct addrinfo
*, const char *));
194 static int get_port
__P((struct addrinfo
*, const char *, int));
195 static const struct afd
*find_afd
__P((int));
197 static char *ai_errlist
[] = {
199 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
200 "Temporary failure in name resolution", /* EAI_AGAIN */
201 "Invalid value for ai_flags", /* EAI_BADFLAGS */
202 "Non-recoverable failure in name resolution", /* EAI_FAIL */
203 "ai_family not supported", /* EAI_FAMILY */
204 "Memory allocation failure", /* EAI_MEMORY */
205 "No address associated with hostname", /* EAI_NODATA */
206 "hostname nor servname provided, or not known", /* EAI_NONAME */
207 "servname not supported for ai_socktype", /* EAI_SERVICE */
208 "ai_socktype not supported", /* EAI_SOCKTYPE */
209 "System error returned in errno", /* EAI_SYSTEM */
210 "Invalid value for hints", /* EAI_BADHINTS */
211 "Resolved protocol is unknown", /* EAI_PROTOCOL */
212 "Unknown error", /* EAI_MAX */
215 /* XXX macros that make external reference is BAD. */
217 #define GET_AI(ai, afd, addr) \
219 /* external reference: pai, error, and label free */ \
220 (ai) = get_ai(pai, (afd), (addr)); \
221 if ((ai) == NULL) { \
222 error = EAI_MEMORY; \
227 #define GET_PORT(ai, serv) \
229 /* external reference: error and label free */ \
230 error = get_port((ai), (serv), 0); \
235 #define GET_CANONNAME(ai, str) \
237 /* external reference: pai, error and label free */ \
238 error = get_canonname(pai, (ai), (str)); \
245 /* external reference: error, and label bad */ \
250 #define MATCH_FAMILY(x, y, w) \
251 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
252 #define MATCH(x, y, w) \
253 ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
255 #if defined(DEFINE_ADDITIONAL_IPV6_STUFF)
260 if (ecode
< 0 || ecode
> EAI_MAX
)
262 return ai_errlist
[ecode
];
270 struct addrinfo
*next
;
274 if (ai
->ai_canonname
)
275 free(ai
->ai_canonname
);
276 /* no need to free(ai->ai_addr) */
278 } while ((ai
= next
) != NULL
);
295 getaddrinfo(hostname
, servname
, hints
, res
)
296 const char *hostname
, *servname
;
297 const struct addrinfo
*hints
;
298 struct addrinfo
**res
;
300 struct addrinfo sentinel
;
301 struct addrinfo
*cur
;
305 struct addrinfo
*pai
;
306 const struct afd
*afd
;
307 const struct explore
*ex
;
310 static int firsttime
= 1;
313 /* translator hack */
314 char *q
= getenv("GAI");
315 if (q
&& inet_pton(AF_INET6
, q
, &faith_prefix
) == 1)
321 sentinel
.ai_next
= NULL
;
325 pai
->ai_family
= PF_UNSPEC
;
326 pai
->ai_socktype
= ANY
;
327 pai
->ai_protocol
= ANY
;
329 pai
->ai_canonname
= NULL
;
333 if (hostname
== NULL
&& servname
== NULL
)
336 /* error check for hints */
337 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
338 hints
->ai_addr
|| hints
->ai_next
)
339 ERR(EAI_BADHINTS
); /* xxx */
340 if (hints
->ai_flags
& ~AI_MASK
)
342 switch (hints
->ai_family
) {
352 memcpy(pai
, hints
, sizeof(*pai
));
355 * if both socktype/protocol are specified, check if they
356 * are meaningful combination.
358 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
359 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
360 if (pai
->ai_family
!= ex
->e_af
)
362 if (ex
->e_socktype
== ANY
)
364 if (ex
->e_protocol
== ANY
)
366 if (pai
->ai_socktype
== ex
->e_socktype
367 && pai
->ai_protocol
!= ex
->e_protocol
) {
375 * check for special cases. (1) numeric servname is disallowed if
376 * socktype/protocol are left unspecified. (2) servname is disallowed
377 * for raw and other inet{,6} sockets.
379 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
381 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
386 if (pai
->ai_family
== PF_UNSPEC
) {
388 pai
->ai_family
= PF_INET6
;
390 pai
->ai_family
= PF_INET
;
393 error
= get_portmatch(pai
, servname
);
402 /* NULL hostname, or numeric hostname */
403 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
406 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
408 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
410 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
413 if (pai
->ai_family
== PF_UNSPEC
)
414 pai
->ai_family
= ex
->e_af
;
415 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
416 pai
->ai_socktype
= ex
->e_socktype
;
417 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
418 pai
->ai_protocol
= ex
->e_protocol
;
420 if (hostname
== NULL
)
421 error
= explore_null(pai
, hostname
, servname
, &cur
->ai_next
);
423 error
= explore_numeric_scope(pai
, hostname
, servname
, &cur
->ai_next
);
428 while (cur
&& cur
->ai_next
)
434 * If numreic representation of AF1 can be interpreted as FQDN
435 * representation of AF2, we need to think again about the code below.
437 if (sentinel
.ai_next
)
440 if (pai
->ai_flags
& AI_NUMERICHOST
)
442 if (hostname
== NULL
)
446 * hostname as alphabetical name.
447 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
450 for (afd
= afdl
; afd
->a_af
; afd
++) {
453 if (!MATCH_FAMILY(pai
->ai_family
, afd
->a_af
, 1))
456 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
459 if (pai
->ai_family
== PF_UNSPEC
)
460 pai
->ai_family
= afd
->a_af
;
462 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
464 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
465 WILD_SOCKTYPE(ex
))) {
468 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
469 WILD_PROTOCOL(ex
))) {
473 if (pai
->ai_family
== PF_UNSPEC
)
474 pai
->ai_family
= ex
->e_af
;
475 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
476 pai
->ai_socktype
= ex
->e_socktype
;
477 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
478 pai
->ai_protocol
= ex
->e_protocol
;
480 error
= explore_fqdn(pai
, hostname
, servname
,
483 while (cur
&& cur
->ai_next
)
489 if (sentinel
.ai_next
)
495 if (sentinel
.ai_next
) {
497 *res
= sentinel
.ai_next
;
504 if (sentinel
.ai_next
)
505 freeaddrinfo(sentinel
.ai_next
);
511 * FQDN hostname, DNS lookup
514 explore_fqdn(pai
, hostname
, servname
, res
)
515 const struct addrinfo
*pai
;
516 const char *hostname
;
517 const char *servname
;
518 struct addrinfo
**res
;
523 char **aplist
= NULL
, *apbuf
= NULL
;
525 struct addrinfo sentinel
, *cur
;
527 #ifndef USE_GETIPNODEBY
530 const struct afd
*afd
;
534 sentinel
.ai_next
= NULL
;
538 * Do not filter unsupported AFs here. We need to honor content of
539 * databases (/etc/hosts, DNS and others). Otherwise we cannot
540 * replace gethostbyname() by getaddrinfo().
544 * if the servname does not match socktype/protocol, ignore it.
546 if (get_portmatch(pai
, servname
) != 0)
549 afd
= find_afd(pai
->ai_family
);
552 * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
553 * rather than hardcoding it. we may need to add AI_ADDRCONFIG
554 * handling code by ourselves in case we don't have getipnodebyname().
556 #ifdef USE_GETIPNODEBY
557 hp
= getipnodebyname(hostname
, pai
->ai_family
, AI_ADDRCONFIG
, &h_error
);
559 #ifdef HAVE_GETHOSTBYNAME2
560 hp
= gethostbyname2(hostname
, pai
->ai_family
);
562 if (pai
->ai_family
!= AF_INET
)
564 hp
= gethostbyname(hostname
);
570 #endif /*HAVE_GETHOSTBYNAME2*/
571 #endif /*USE_GETIPNODEBY*/
588 } else if ((hp
->h_name
== NULL
) || (hp
->h_name
[0] == 0)
589 || (hp
->h_addr_list
[0] == NULL
)) {
590 #ifdef USE_GETIPNODEBY
600 #ifdef USE_GETIPNODEBY
601 aplist
= hp
->h_addr_list
;
604 * hp will be overwritten if we use gethostbyname2().
605 * always deep copy for simplification.
607 for (naddrs
= 0; hp
->h_addr_list
[naddrs
] != NULL
; naddrs
++)
610 aplist
= (char **)malloc(sizeof(aplist
[0]) * naddrs
);
611 apbuf
= (char *)malloc(hp
->h_length
* naddrs
);
612 if (aplist
== NULL
|| apbuf
== NULL
) {
616 memset(aplist
, 0, sizeof(aplist
[0]) * naddrs
);
617 for (i
= 0; i
< naddrs
; i
++) {
618 if (hp
->h_addr_list
[i
] == NULL
) {
622 memcpy(&apbuf
[i
* hp
->h_length
], hp
->h_addr_list
[i
],
624 aplist
[i
] = &apbuf
[i
* hp
->h_length
];
628 for (i
= 0; aplist
[i
] != NULL
; i
++) {
633 && IN6_IS_ADDR_V4MAPPED((struct in6_addr
*)ap
)) {
635 ap
= ap
+ sizeof(struct in6_addr
)
636 - sizeof(struct in_addr
);
640 if (af
!= pai
->ai_family
)
643 if ((pai
->ai_flags
& AI_CANONNAME
) == 0) {
644 GET_AI(cur
->ai_next
, afd
, ap
);
645 GET_PORT(cur
->ai_next
, servname
);
648 * if AI_CANONNAME and if reverse lookup
649 * fail, return ai anyway to pacify
650 * calling application.
652 * XXX getaddrinfo() is a name->address
653 * translation function, and it looks
654 * strange that we do addr->name
657 get_name(ap
, afd
, &cur
->ai_next
,
661 while (cur
&& cur
->ai_next
)
665 *res
= sentinel
.ai_next
;
669 #ifdef USE_GETIPNODEBY
677 if (sentinel
.ai_next
)
678 freeaddrinfo(sentinel
.ai_next
);
684 * passive socket -> anyaddr (0.0.0.0 or ::)
685 * non-passive socket -> localhost (127.0.0.1 or ::1)
688 explore_null(pai
, hostname
, servname
, res
)
689 const struct addrinfo
*pai
;
690 const char *hostname
;
691 const char *servname
;
692 struct addrinfo
**res
;
695 const struct afd
*afd
;
696 struct addrinfo
*cur
;
697 struct addrinfo sentinel
;
701 sentinel
.ai_next
= NULL
;
705 * filter out AFs that are not supported by the kernel
708 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
716 * if the servname does not match socktype/protocol, ignore it.
718 if (get_portmatch(pai
, servname
) != 0)
721 afd
= find_afd(pai
->ai_family
);
723 if (pai
->ai_flags
& AI_PASSIVE
) {
724 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
726 * GET_CANONNAME(cur->ai_next, "anyaddr");
728 GET_PORT(cur
->ai_next
, servname
);
730 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
732 * GET_CANONNAME(cur->ai_next, "localhost");
734 GET_PORT(cur
->ai_next
, servname
);
738 *res
= sentinel
.ai_next
;
742 if (sentinel
.ai_next
)
743 freeaddrinfo(sentinel
.ai_next
);
751 explore_numeric(pai
, hostname
, servname
, res
)
752 const struct addrinfo
*pai
;
753 const char *hostname
;
754 const char *servname
;
755 struct addrinfo
**res
;
757 const struct afd
*afd
;
758 struct addrinfo
*cur
;
759 struct addrinfo sentinel
;
765 sentinel
.ai_next
= NULL
;
769 * if the servname does not match socktype/protocol, ignore it.
771 if (get_portmatch(pai
, servname
) != 0)
774 afd
= find_afd(pai
->ai_family
);
775 flags
= pai
->ai_flags
;
777 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
785 v4a
= (u_int32_t
)ntohl(((struct in_addr
*)pton
)->s_addr
);
786 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
787 flags
&= ~AI_CANONNAME
;
788 v4a
>>= IN_CLASSA_NSHIFT
;
789 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
790 flags
&= ~AI_CANONNAME
;
794 pfx
= ((struct in6_addr
*)pton
)->s6_addr
[0];
795 if (pfx
== 0 || pfx
== 0xfe || pfx
== 0xff)
796 flags
&= ~AI_CANONNAME
;
801 if (pai
->ai_family
== afd
->a_af
||
802 pai
->ai_family
== PF_UNSPEC
/*?*/) {
803 if ((flags
& AI_CANONNAME
) == 0) {
804 GET_AI(cur
->ai_next
, afd
, pton
);
805 GET_PORT(cur
->ai_next
, servname
);
808 * if AI_CANONNAME and if reverse lookup
809 * fail, return ai anyway to pacify
810 * calling application.
812 * XXX getaddrinfo() is a name->address
813 * translation function, and it looks
814 * strange that we do addr->name
817 get_name(pton
, afd
, &cur
->ai_next
,
818 pton
, pai
, servname
);
820 while (cur
&& cur
->ai_next
)
823 ERR(EAI_FAMILY
); /*xxx*/
826 *res
= sentinel
.ai_next
;
831 if (sentinel
.ai_next
)
832 freeaddrinfo(sentinel
.ai_next
);
837 * numeric hostname with scope
840 explore_numeric_scope(pai
, hostname
, servname
, res
)
841 const struct addrinfo
*pai
;
842 const char *hostname
;
843 const char *servname
;
844 struct addrinfo
**res
;
846 #ifndef SCOPE_DELIMITER
847 return explore_numeric(pai
, hostname
, servname
, res
);
849 const struct afd
*afd
;
850 struct addrinfo
*cur
;
852 char *cp
, *hostname2
= NULL
;
854 struct sockaddr_in6
*sin6
;
857 * if the servname does not match socktype/protocol, ignore it.
859 if (get_portmatch(pai
, servname
) != 0)
862 afd
= find_afd(pai
->ai_family
);
864 return explore_numeric(pai
, hostname
, servname
, res
);
866 cp
= strchr(hostname
, SCOPE_DELIMITER
);
868 return explore_numeric(pai
, hostname
, servname
, res
);
871 * Handle special case of <scoped_address><delimiter><scope id>
873 hostname2
= strdup(hostname
);
874 if (hostname2
== NULL
)
876 /* terminate at the delimiter */
877 hostname2
[cp
- hostname
] = '\0';
880 switch (pai
->ai_family
) {
883 scope
= if_nametoindex(cp
);
892 error
= explore_numeric(pai
, hostname2
, servname
, res
);
894 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
895 if (cur
->ai_family
!= AF_INET6
)
897 sin6
= (struct sockaddr_in6
*)cur
->ai_addr
;
898 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
899 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
->sin6_addr
))
900 sin6
->sin6_scope_id
= scope
;
911 get_name(addr
, afd
, res
, numaddr
, pai
, servname
)
913 const struct afd
*afd
;
914 struct addrinfo
**res
;
916 const struct addrinfo
*pai
;
917 const char *servname
;
919 struct hostent
*hp
= NULL
;
920 struct addrinfo
*cur
= NULL
;
922 char *ap
= NULL
, *cn
= NULL
;
923 #ifdef USE_GETIPNODEBY
926 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
928 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
930 if (hp
&& hp
->h_name
&& hp
->h_name
[0] && hp
->h_addr_list
[0]) {
931 #ifdef USE_GETIPNODEBY
932 GET_AI(cur
, afd
, hp
->h_addr_list
[0]);
933 GET_PORT(cur
, servname
);
934 GET_CANONNAME(cur
, hp
->h_name
);
936 /* hp will be damaged if we use gethostbyaddr() */
937 if ((ap
= (char *)malloc(hp
->h_length
)) == NULL
) {
941 memcpy(ap
, hp
->h_addr_list
[0], hp
->h_length
);
942 if ((cn
= strdup(hp
->h_name
)) == NULL
) {
947 GET_AI(cur
, afd
, ap
);
948 GET_PORT(cur
, servname
);
949 GET_CANONNAME(cur
, cn
);
954 GET_AI(cur
, afd
, numaddr
);
955 GET_PORT(cur
, servname
);
958 #ifdef USE_GETIPNODEBY
971 #ifdef USE_GETIPNODEBY
980 get_canonname(pai
, ai
, str
)
981 const struct addrinfo
*pai
;
985 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
986 ai
->ai_canonname
= strdup(str
);
987 if (ai
->ai_canonname
== NULL
)
993 static struct addrinfo
*
994 get_ai(pai
, afd
, addr
)
995 const struct addrinfo
*pai
;
996 const struct afd
*afd
;
1000 struct addrinfo
*ai
;
1002 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
)
1003 + (afd
->a_socklen
));
1007 memcpy(ai
, pai
, sizeof(struct addrinfo
));
1008 ai
->ai_addr
= (struct sockaddr
*)(ai
+ 1);
1009 memset(ai
->ai_addr
, 0, afd
->a_socklen
);
1010 #ifdef HAVE_SOCKADDR_SA_LEN
1011 ai
->ai_addr
->sa_len
= afd
->a_socklen
;
1013 ai
->ai_addrlen
= afd
->a_socklen
;
1014 ai
->ai_addr
->sa_family
= ai
->ai_family
= afd
->a_af
;
1015 p
= (char *)(ai
->ai_addr
);
1016 memcpy(p
+ afd
->a_off
, addr
, afd
->a_addrlen
);
1021 get_portmatch(ai
, servname
)
1022 const struct addrinfo
*ai
;
1023 const char *servname
;
1026 /* get_port does not touch first argument. when matchonly == 1. */
1027 return get_port((struct addrinfo
*)ai
, servname
, 1);
1031 get_port(ai
, servname
, matchonly
)
1032 struct addrinfo
*ai
;
1033 const char *servname
;
1041 if (servname
== NULL
)
1043 switch (ai
->ai_family
) {
1053 switch (ai
->ai_socktype
) {
1064 return EAI_SOCKTYPE
;
1067 if (str_isnumber(servname
)) {
1070 port
= htons(atoi(servname
));
1071 if (port
< 0 || port
> 65535)
1074 switch (ai
->ai_socktype
) {
1086 if ((sp
= getservbyname(servname
, proto
)) == NULL
)
1092 switch (ai
->ai_family
) {
1094 ((struct sockaddr_in
*)ai
->ai_addr
)->sin_port
= port
;
1098 ((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_port
= port
;
1107 static const struct afd
*
1111 const struct afd
*afd
;
1113 if (af
== PF_UNSPEC
)
1115 for (afd
= afdl
; afd
->a_af
; afd
++) {
1116 if (afd
->a_af
== af
)