]>
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.2.4.1 2008-09-15 23:38:45 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)))
259 if (ecode
< 0 || ecode
> EAI_MAX
)
261 return ai_errlist
[ecode
];
268 struct addrinfo
*next
;
272 if (ai
->ai_canonname
)
273 free(ai
->ai_canonname
);
274 /* no need to free(ai->ai_addr) */
276 } while ((ai
= next
) != NULL
);
293 getaddrinfo(hostname
, servname
, hints
, res
)
294 const char *hostname
, *servname
;
295 const struct addrinfo
*hints
;
296 struct addrinfo
**res
;
298 struct addrinfo sentinel
;
299 struct addrinfo
*cur
;
303 struct addrinfo
*pai
;
304 const struct afd
*afd
;
305 const struct explore
*ex
;
308 static int firsttime
= 1;
311 /* translator hack */
312 char *q
= getenv("GAI");
313 if (q
&& inet_pton(AF_INET6
, q
, &faith_prefix
) == 1)
319 sentinel
.ai_next
= NULL
;
323 pai
->ai_family
= PF_UNSPEC
;
324 pai
->ai_socktype
= ANY
;
325 pai
->ai_protocol
= ANY
;
327 pai
->ai_canonname
= NULL
;
331 if (hostname
== NULL
&& servname
== NULL
)
334 /* error check for hints */
335 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
336 hints
->ai_addr
|| hints
->ai_next
)
337 ERR(EAI_BADHINTS
); /* xxx */
338 if (hints
->ai_flags
& ~AI_MASK
)
340 switch (hints
->ai_family
) {
350 memcpy(pai
, hints
, sizeof(*pai
));
353 * if both socktype/protocol are specified, check if they
354 * are meaningful combination.
356 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
357 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
358 if (pai
->ai_family
!= ex
->e_af
)
360 if (ex
->e_socktype
== ANY
)
362 if (ex
->e_protocol
== ANY
)
364 if (pai
->ai_socktype
== ex
->e_socktype
365 && pai
->ai_protocol
!= ex
->e_protocol
) {
373 * check for special cases. (1) numeric servname is disallowed if
374 * socktype/protocol are left unspecified. (2) servname is disallowed
375 * for raw and other inet{,6} sockets.
377 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
379 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
384 if (pai
->ai_family
== PF_UNSPEC
) {
386 pai
->ai_family
= PF_INET6
;
388 pai
->ai_family
= PF_INET
;
391 error
= get_portmatch(pai
, servname
);
400 /* NULL hostname, or numeric hostname */
401 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
404 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
406 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
408 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
411 if (pai
->ai_family
== PF_UNSPEC
)
412 pai
->ai_family
= ex
->e_af
;
413 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
414 pai
->ai_socktype
= ex
->e_socktype
;
415 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
416 pai
->ai_protocol
= ex
->e_protocol
;
418 if (hostname
== NULL
)
419 error
= explore_null(pai
, hostname
, servname
, &cur
->ai_next
);
421 error
= explore_numeric_scope(pai
, hostname
, servname
, &cur
->ai_next
);
426 while (cur
&& cur
->ai_next
)
432 * If numreic representation of AF1 can be interpreted as FQDN
433 * representation of AF2, we need to think again about the code below.
435 if (sentinel
.ai_next
)
438 if (pai
->ai_flags
& AI_NUMERICHOST
)
440 if (hostname
== NULL
)
444 * hostname as alphabetical name.
445 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
448 for (afd
= afdl
; afd
->a_af
; afd
++) {
451 if (!MATCH_FAMILY(pai
->ai_family
, afd
->a_af
, 1))
454 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
457 if (pai
->ai_family
== PF_UNSPEC
)
458 pai
->ai_family
= afd
->a_af
;
460 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
462 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
463 WILD_SOCKTYPE(ex
))) {
466 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
467 WILD_PROTOCOL(ex
))) {
471 if (pai
->ai_family
== PF_UNSPEC
)
472 pai
->ai_family
= ex
->e_af
;
473 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
474 pai
->ai_socktype
= ex
->e_socktype
;
475 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
476 pai
->ai_protocol
= ex
->e_protocol
;
478 error
= explore_fqdn(pai
, hostname
, servname
,
481 while (cur
&& cur
->ai_next
)
487 if (sentinel
.ai_next
)
493 if (sentinel
.ai_next
) {
495 *res
= sentinel
.ai_next
;
502 if (sentinel
.ai_next
)
503 freeaddrinfo(sentinel
.ai_next
);
509 * FQDN hostname, DNS lookup
512 explore_fqdn(pai
, hostname
, servname
, res
)
513 const struct addrinfo
*pai
;
514 const char *hostname
;
515 const char *servname
;
516 struct addrinfo
**res
;
521 char **aplist
= NULL
, *apbuf
= NULL
;
523 struct addrinfo sentinel
, *cur
;
525 #ifndef USE_GETIPNODEBY
528 const struct afd
*afd
;
532 sentinel
.ai_next
= NULL
;
536 * Do not filter unsupported AFs here. We need to honor content of
537 * databases (/etc/hosts, DNS and others). Otherwise we cannot
538 * replace gethostbyname() by getaddrinfo().
542 * if the servname does not match socktype/protocol, ignore it.
544 if (get_portmatch(pai
, servname
) != 0)
547 afd
= find_afd(pai
->ai_family
);
550 * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
551 * rather than hardcoding it. we may need to add AI_ADDRCONFIG
552 * handling code by ourselves in case we don't have getipnodebyname().
554 #ifdef USE_GETIPNODEBY
555 hp
= getipnodebyname(hostname
, pai
->ai_family
, AI_ADDRCONFIG
, &h_error
);
557 #ifdef HAVE_GETHOSTBYNAME2
558 hp
= gethostbyname2(hostname
, pai
->ai_family
);
560 if (pai
->ai_family
!= AF_INET
)
562 hp
= gethostbyname(hostname
);
568 #endif /*HAVE_GETHOSTBYNAME2*/
569 #endif /*USE_GETIPNODEBY*/
586 } else if ((hp
->h_name
== NULL
) || (hp
->h_name
[0] == 0)
587 || (hp
->h_addr_list
[0] == NULL
)) {
588 #ifdef USE_GETIPNODEBY
598 #ifdef USE_GETIPNODEBY
599 aplist
= hp
->h_addr_list
;
602 * hp will be overwritten if we use gethostbyname2().
603 * always deep copy for simplification.
605 for (naddrs
= 0; hp
->h_addr_list
[naddrs
] != NULL
; naddrs
++)
608 aplist
= (char **)malloc(sizeof(aplist
[0]) * naddrs
);
609 apbuf
= (char *)malloc(hp
->h_length
* naddrs
);
610 if (aplist
== NULL
|| apbuf
== NULL
) {
614 memset(aplist
, 0, sizeof(aplist
[0]) * naddrs
);
615 for (i
= 0; i
< naddrs
; i
++) {
616 if (hp
->h_addr_list
[i
] == NULL
) {
620 memcpy(&apbuf
[i
* hp
->h_length
], hp
->h_addr_list
[i
],
622 aplist
[i
] = &apbuf
[i
* hp
->h_length
];
626 for (i
= 0; aplist
[i
] != NULL
; i
++) {
631 && IN6_IS_ADDR_V4MAPPED((struct in6_addr
*)ap
)) {
633 ap
= ap
+ sizeof(struct in6_addr
)
634 - sizeof(struct in_addr
);
638 if (af
!= pai
->ai_family
)
641 if ((pai
->ai_flags
& AI_CANONNAME
) == 0) {
642 GET_AI(cur
->ai_next
, afd
, ap
);
643 GET_PORT(cur
->ai_next
, servname
);
646 * if AI_CANONNAME and if reverse lookup
647 * fail, return ai anyway to pacify
648 * calling application.
650 * XXX getaddrinfo() is a name->address
651 * translation function, and it looks
652 * strange that we do addr->name
655 get_name(ap
, afd
, &cur
->ai_next
,
659 while (cur
&& cur
->ai_next
)
663 *res
= sentinel
.ai_next
;
667 #ifdef USE_GETIPNODEBY
675 if (sentinel
.ai_next
)
676 freeaddrinfo(sentinel
.ai_next
);
682 * passive socket -> anyaddr (0.0.0.0 or ::)
683 * non-passive socket -> localhost (127.0.0.1 or ::1)
686 explore_null(pai
, hostname
, servname
, res
)
687 const struct addrinfo
*pai
;
688 const char *hostname
;
689 const char *servname
;
690 struct addrinfo
**res
;
693 const struct afd
*afd
;
694 struct addrinfo
*cur
;
695 struct addrinfo sentinel
;
699 sentinel
.ai_next
= NULL
;
703 * filter out AFs that are not supported by the kernel
706 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
714 * if the servname does not match socktype/protocol, ignore it.
716 if (get_portmatch(pai
, servname
) != 0)
719 afd
= find_afd(pai
->ai_family
);
721 if (pai
->ai_flags
& AI_PASSIVE
) {
722 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
724 * GET_CANONNAME(cur->ai_next, "anyaddr");
726 GET_PORT(cur
->ai_next
, servname
);
728 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
730 * GET_CANONNAME(cur->ai_next, "localhost");
732 GET_PORT(cur
->ai_next
, servname
);
736 *res
= sentinel
.ai_next
;
740 if (sentinel
.ai_next
)
741 freeaddrinfo(sentinel
.ai_next
);
749 explore_numeric(pai
, hostname
, servname
, res
)
750 const struct addrinfo
*pai
;
751 const char *hostname
;
752 const char *servname
;
753 struct addrinfo
**res
;
755 const struct afd
*afd
;
756 struct addrinfo
*cur
;
757 struct addrinfo sentinel
;
763 sentinel
.ai_next
= NULL
;
767 * if the servname does not match socktype/protocol, ignore it.
769 if (get_portmatch(pai
, servname
) != 0)
772 afd
= find_afd(pai
->ai_family
);
773 flags
= pai
->ai_flags
;
775 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
783 v4a
= (u_int32_t
)ntohl(((struct in_addr
*)pton
)->s_addr
);
784 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
785 flags
&= ~AI_CANONNAME
;
786 v4a
>>= IN_CLASSA_NSHIFT
;
787 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
788 flags
&= ~AI_CANONNAME
;
792 pfx
= ((struct in6_addr
*)pton
)->s6_addr
[0];
793 if (pfx
== 0 || pfx
== 0xfe || pfx
== 0xff)
794 flags
&= ~AI_CANONNAME
;
799 if (pai
->ai_family
== afd
->a_af
||
800 pai
->ai_family
== PF_UNSPEC
/*?*/) {
801 if ((flags
& AI_CANONNAME
) == 0) {
802 GET_AI(cur
->ai_next
, afd
, pton
);
803 GET_PORT(cur
->ai_next
, servname
);
806 * if AI_CANONNAME and if reverse lookup
807 * fail, return ai anyway to pacify
808 * calling application.
810 * XXX getaddrinfo() is a name->address
811 * translation function, and it looks
812 * strange that we do addr->name
815 get_name(pton
, afd
, &cur
->ai_next
,
816 pton
, pai
, servname
);
818 while (cur
&& cur
->ai_next
)
821 ERR(EAI_FAMILY
); /*xxx*/
824 *res
= sentinel
.ai_next
;
829 if (sentinel
.ai_next
)
830 freeaddrinfo(sentinel
.ai_next
);
835 * numeric hostname with scope
838 explore_numeric_scope(pai
, hostname
, servname
, res
)
839 const struct addrinfo
*pai
;
840 const char *hostname
;
841 const char *servname
;
842 struct addrinfo
**res
;
844 #ifndef SCOPE_DELIMITER
845 return explore_numeric(pai
, hostname
, servname
, res
);
847 const struct afd
*afd
;
848 struct addrinfo
*cur
;
850 char *cp
, *hostname2
= NULL
;
852 struct sockaddr_in6
*sin6
;
855 * if the servname does not match socktype/protocol, ignore it.
857 if (get_portmatch(pai
, servname
) != 0)
860 afd
= find_afd(pai
->ai_family
);
862 return explore_numeric(pai
, hostname
, servname
, res
);
864 cp
= strchr(hostname
, SCOPE_DELIMITER
);
866 return explore_numeric(pai
, hostname
, servname
, res
);
869 * Handle special case of <scoped_address><delimiter><scope id>
871 hostname2
= strdup(hostname
);
872 if (hostname2
== NULL
)
874 /* terminate at the delimiter */
875 hostname2
[cp
- hostname
] = '\0';
878 switch (pai
->ai_family
) {
881 scope
= if_nametoindex(cp
);
890 error
= explore_numeric(pai
, hostname2
, servname
, res
);
892 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
893 if (cur
->ai_family
!= AF_INET6
)
895 sin6
= (struct sockaddr_in6
*)cur
->ai_addr
;
896 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
897 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
->sin6_addr
))
898 sin6
->sin6_scope_id
= scope
;
909 get_name(addr
, afd
, res
, numaddr
, pai
, servname
)
911 const struct afd
*afd
;
912 struct addrinfo
**res
;
914 const struct addrinfo
*pai
;
915 const char *servname
;
917 struct hostent
*hp
= NULL
;
918 struct addrinfo
*cur
= NULL
;
920 char *ap
= NULL
, *cn
= NULL
;
921 #ifdef USE_GETIPNODEBY
924 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
926 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
928 if (hp
&& hp
->h_name
&& hp
->h_name
[0] && hp
->h_addr_list
[0]) {
929 #ifdef USE_GETIPNODEBY
930 GET_AI(cur
, afd
, hp
->h_addr_list
[0]);
931 GET_PORT(cur
, servname
);
932 GET_CANONNAME(cur
, hp
->h_name
);
934 /* hp will be damaged if we use gethostbyaddr() */
935 if ((ap
= (char *)malloc(hp
->h_length
)) == NULL
) {
939 memcpy(ap
, hp
->h_addr_list
[0], hp
->h_length
);
940 if ((cn
= strdup(hp
->h_name
)) == NULL
) {
945 GET_AI(cur
, afd
, ap
);
946 GET_PORT(cur
, servname
);
947 GET_CANONNAME(cur
, cn
);
952 GET_AI(cur
, afd
, numaddr
);
953 GET_PORT(cur
, servname
);
956 #ifdef USE_GETIPNODEBY
969 #ifdef USE_GETIPNODEBY
978 get_canonname(pai
, ai
, str
)
979 const struct addrinfo
*pai
;
983 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
984 ai
->ai_canonname
= strdup(str
);
985 if (ai
->ai_canonname
== NULL
)
991 static struct addrinfo
*
992 get_ai(pai
, afd
, addr
)
993 const struct addrinfo
*pai
;
994 const struct afd
*afd
;
1000 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
)
1001 + (afd
->a_socklen
));
1005 memcpy(ai
, pai
, sizeof(struct addrinfo
));
1006 ai
->ai_addr
= (struct sockaddr
*)(ai
+ 1);
1007 memset(ai
->ai_addr
, 0, afd
->a_socklen
);
1008 #ifdef HAVE_SOCKADDR_SA_LEN
1009 ai
->ai_addr
->sa_len
= afd
->a_socklen
;
1011 ai
->ai_addrlen
= afd
->a_socklen
;
1012 ai
->ai_addr
->sa_family
= ai
->ai_family
= afd
->a_af
;
1013 p
= (char *)(ai
->ai_addr
);
1014 memcpy(p
+ afd
->a_off
, addr
, afd
->a_addrlen
);
1019 get_portmatch(ai
, servname
)
1020 const struct addrinfo
*ai
;
1021 const char *servname
;
1024 /* get_port does not touch first argument. when matchonly == 1. */
1025 return get_port((struct addrinfo
*)ai
, servname
, 1);
1029 get_port(ai
, servname
, matchonly
)
1030 struct addrinfo
*ai
;
1031 const char *servname
;
1039 if (servname
== NULL
)
1041 switch (ai
->ai_family
) {
1051 switch (ai
->ai_socktype
) {
1062 return EAI_SOCKTYPE
;
1065 if (str_isnumber(servname
)) {
1068 port
= htons(atoi(servname
));
1069 if (port
< 0 || port
> 65535)
1072 switch (ai
->ai_socktype
) {
1084 if ((sp
= getservbyname(servname
, proto
)) == NULL
)
1090 switch (ai
->ai_family
) {
1092 ((struct sockaddr_in
*)ai
->ai_addr
)->sin_port
= port
;
1096 ((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_port
= port
;
1105 static const struct afd
*
1109 const struct afd
*afd
;
1111 if (af
== PF_UNSPEC
)
1113 for (afd
= afdl
; afd
->a_af
; afd
++) {
1114 if (afd
->a_af
== af
)