]>
The Tcpdump Group git mirrors - tcpdump/blob - missing/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/tcpdump/missing/getaddrinfo.c,v 1.11.2.2 2003-11-16 08:52:00 guy Exp $";
57 #include <sys/types.h>
58 #include <sys/param.h>
60 #include <sys/sysctl.h>
62 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
77 #ifndef HAVE_U_INT32_T
81 #ifndef HAVE_SOCKADDR_STORAGE
82 #include "sockstorage.h"
85 #ifdef NEED_ADDRINFO_H
89 #if defined(__KAME__) && defined(INET6)
99 static int translate
= NO
;
100 static struct in6_addr faith_prefix
= IN6ADDR_ANY_INIT
;
103 static const char in_addrany
[] = { 0, 0, 0, 0 };
104 static const char in6_addrany
[] = {
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
107 static const char in_loopback
[] = { 127, 0, 0, 1 };
108 static const char in6_loopback
[] = {
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
116 u_int32_t si_scope_id
;
119 static const struct afd
{
124 const char *a_addrany
;
125 const char *a_loopback
;
129 {PF_INET6
, sizeof(struct in6_addr
),
130 sizeof(struct sockaddr_in6
),
131 offsetof(struct sockaddr_in6
, sin6_addr
),
132 in6_addrany
, in6_loopback
, 1},
134 {PF_INET
, sizeof(struct in_addr
),
135 sizeof(struct sockaddr_in
),
136 offsetof(struct sockaddr_in
, sin_addr
),
137 in_addrany
, in_loopback
, 0},
138 {0, 0, 0, 0, NULL
, NULL
, 0},
145 const char *e_protostr
;
147 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
148 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
149 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
152 static const struct explore explore
[] = {
154 { PF_LOCAL
, 0, ANY
, ANY
, NULL
, 0x01 },
157 { PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
158 { PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
159 { PF_INET6
, SOCK_RAW
, ANY
, NULL
, 0x05 },
161 { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
162 { PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
163 { PF_INET
, SOCK_RAW
, ANY
, NULL
, 0x05 },
164 { -1, 0, 0, NULL
, 0 },
174 static int str_isnumber (const char *);
175 static int explore_fqdn (const struct addrinfo
*, const char *,
176 const char *, struct addrinfo
**);
177 static int explore_null (const struct addrinfo
*, const char *,
178 const char *, struct addrinfo
**);
179 static int explore_numeric (const struct addrinfo
*, const char *,
180 const char *, struct addrinfo
**);
181 static int explore_numeric_scope (const struct addrinfo
*, const char *,
182 const char *, struct addrinfo
**);
183 static int get_name (const char *, const struct afd
*, struct addrinfo
**,
184 char *, const struct addrinfo
*, const char *);
185 static int get_canonname (const struct addrinfo
*,
186 struct addrinfo
*, const char *);
187 static struct addrinfo
*get_ai (const struct addrinfo
*,
188 const struct afd
*, const char *);
189 static int get_portmatch (const struct addrinfo
*, const char *);
190 static int get_port (struct addrinfo
*, const char *, int);
191 static const struct afd
*find_afd (int);
193 static char *ai_errlist
[] = {
195 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
196 "Temporary failure in name resolution", /* EAI_AGAIN */
197 "Invalid value for ai_flags", /* EAI_BADFLAGS */
198 "Non-recoverable failure in name resolution", /* EAI_FAIL */
199 "ai_family not supported", /* EAI_FAMILY */
200 "Memory allocation failure", /* EAI_MEMORY */
201 "No address associated with hostname", /* EAI_NODATA */
202 "hostname nor servname provided, or not known", /* EAI_NONAME */
203 "servname not supported for ai_socktype", /* EAI_SERVICE */
204 "ai_socktype not supported", /* EAI_SOCKTYPE */
205 "System error returned in errno", /* EAI_SYSTEM */
206 "Invalid value for hints", /* EAI_BADHINTS */
207 "Resolved protocol is unknown", /* EAI_PROTOCOL */
208 "Unknown error", /* EAI_MAX */
211 /* XXX macros that make external reference is BAD. */
213 #define GET_AI(ai, afd, addr) \
215 /* external reference: pai, error, and label free */ \
216 (ai) = get_ai(pai, (afd), (addr)); \
217 if ((ai) == NULL) { \
218 error = EAI_MEMORY; \
223 #define GET_PORT(ai, serv) \
225 /* external reference: error and label free */ \
226 error = get_port((ai), (serv), 0); \
231 #define GET_CANONNAME(ai, str) \
233 /* external reference: pai, error and label free */ \
234 error = get_canonname(pai, (ai), (str)); \
241 /* external reference: error, and label bad */ \
246 #define MATCH_FAMILY(x, y, w) \
247 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
248 #define MATCH(x, y, w) \
249 ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
255 if (ecode
< 0 || ecode
> EAI_MAX
)
257 return ai_errlist
[ecode
];
264 struct addrinfo
*next
;
268 if (ai
->ai_canonname
)
269 free(ai
->ai_canonname
);
270 /* no need to free(ai->ai_addr) */
272 } while ((ai
= next
) != NULL
);
289 getaddrinfo(hostname
, servname
, hints
, res
)
290 const char *hostname
, *servname
;
291 const struct addrinfo
*hints
;
292 struct addrinfo
**res
;
294 struct addrinfo sentinel
;
295 struct addrinfo
*cur
;
299 struct addrinfo
*pai
;
300 const struct afd
*afd
;
301 const struct explore
*ex
;
304 static int firsttime
= 1;
307 /* translator hack */
308 char *q
= getenv("GAI");
309 if (q
&& inet_pton(AF_INET6
, q
, &faith_prefix
) == 1)
315 sentinel
.ai_next
= NULL
;
319 pai
->ai_family
= PF_UNSPEC
;
320 pai
->ai_socktype
= ANY
;
321 pai
->ai_protocol
= ANY
;
323 pai
->ai_canonname
= NULL
;
327 if (hostname
== NULL
&& servname
== NULL
)
330 /* error check for hints */
331 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
332 hints
->ai_addr
|| hints
->ai_next
)
333 ERR(EAI_BADHINTS
); /* xxx */
334 if (hints
->ai_flags
& ~AI_MASK
)
336 switch (hints
->ai_family
) {
346 memcpy(pai
, hints
, sizeof(*pai
));
349 * if both socktype/protocol are specified, check if they
350 * are meaningful combination.
352 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
353 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
354 if (pai
->ai_family
!= ex
->e_af
)
356 if (ex
->e_socktype
== ANY
)
358 if (ex
->e_protocol
== ANY
)
360 if (pai
->ai_socktype
== ex
->e_socktype
361 && pai
->ai_protocol
!= ex
->e_protocol
) {
369 * check for special cases. (1) numeric servname is disallowed if
370 * socktype/protocol are left unspecified. (2) servname is disallowed
371 * for raw and other inet{,6} sockets.
373 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
375 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
380 if (pai
->ai_family
== PF_UNSPEC
) {
382 pai
->ai_family
= PF_INET6
;
384 pai
->ai_family
= PF_INET
;
387 error
= get_portmatch(pai
, servname
);
396 /* NULL hostname, or numeric hostname */
397 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
400 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
402 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
404 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
407 if (pai
->ai_family
== PF_UNSPEC
)
408 pai
->ai_family
= ex
->e_af
;
409 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
410 pai
->ai_socktype
= ex
->e_socktype
;
411 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
412 pai
->ai_protocol
= ex
->e_protocol
;
414 if (hostname
== NULL
)
415 error
= explore_null(pai
, hostname
, servname
, &cur
->ai_next
);
417 error
= explore_numeric_scope(pai
, hostname
, servname
, &cur
->ai_next
);
422 while (cur
&& cur
->ai_next
)
428 * If numreic representation of AF1 can be interpreted as FQDN
429 * representation of AF2, we need to think again about the code below.
431 if (sentinel
.ai_next
)
434 if (pai
->ai_flags
& AI_NUMERICHOST
)
436 if (hostname
== NULL
)
440 * hostname as alphabetical name.
441 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
444 for (afd
= afdl
; afd
->a_af
; afd
++) {
447 if (!MATCH_FAMILY(pai
->ai_family
, afd
->a_af
, 1))
450 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
453 if (pai
->ai_family
== PF_UNSPEC
)
454 pai
->ai_family
= afd
->a_af
;
456 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
458 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
459 WILD_SOCKTYPE(ex
))) {
462 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
463 WILD_PROTOCOL(ex
))) {
467 if (pai
->ai_family
== PF_UNSPEC
)
468 pai
->ai_family
= ex
->e_af
;
469 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
470 pai
->ai_socktype
= ex
->e_socktype
;
471 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
472 pai
->ai_protocol
= ex
->e_protocol
;
474 error
= explore_fqdn(pai
, hostname
, servname
,
477 while (cur
&& cur
->ai_next
)
483 if (sentinel
.ai_next
)
489 if (sentinel
.ai_next
) {
491 *res
= sentinel
.ai_next
;
498 if (sentinel
.ai_next
)
499 freeaddrinfo(sentinel
.ai_next
);
505 * FQDN hostname, DNS lookup
508 explore_fqdn(pai
, hostname
, servname
, res
)
509 const struct addrinfo
*pai
;
510 const char *hostname
;
511 const char *servname
;
512 struct addrinfo
**res
;
517 char **aplist
= NULL
, *apbuf
= NULL
;
519 struct addrinfo sentinel
, *cur
;
521 #ifndef USE_GETIPNODEBY
524 const struct afd
*afd
;
528 sentinel
.ai_next
= NULL
;
532 * Do not filter unsupported AFs here. We need to honor content of
533 * databases (/etc/hosts, DNS and others). Otherwise we cannot
534 * replace gethostbyname() by getaddrinfo().
538 * if the servname does not match socktype/protocol, ignore it.
540 if (get_portmatch(pai
, servname
) != 0)
543 afd
= find_afd(pai
->ai_family
);
546 * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
547 * rather than hardcoding it. we may need to add AI_ADDRCONFIG
548 * handling code by ourselves in case we don't have getipnodebyname().
550 #ifdef USE_GETIPNODEBY
551 hp
= getipnodebyname(hostname
, pai
->ai_family
, AI_ADDRCONFIG
, &h_error
);
553 #ifdef HAVE_GETHOSTBYNAME2
554 hp
= gethostbyname2(hostname
, pai
->ai_family
);
556 if (pai
->ai_family
!= AF_INET
)
558 hp
= gethostbyname(hostname
);
564 #endif /*HAVE_GETHOSTBYNAME2*/
565 #endif /*USE_GETIPNODEBY*/
582 } else if ((hp
->h_name
== NULL
) || (hp
->h_name
[0] == 0)
583 || (hp
->h_addr_list
[0] == NULL
)) {
584 #ifdef USE_GETIPNODEBY
594 #ifdef USE_GETIPNODEBY
595 aplist
= hp
->h_addr_list
;
598 * hp will be overwritten if we use gethostbyname2().
599 * always deep copy for simplification.
601 for (naddrs
= 0; hp
->h_addr_list
[naddrs
] != NULL
; naddrs
++)
604 aplist
= (char **)malloc(sizeof(aplist
[0]) * naddrs
);
605 apbuf
= (char *)malloc(hp
->h_length
* naddrs
);
606 if (aplist
== NULL
|| apbuf
== NULL
) {
610 memset(aplist
, 0, sizeof(aplist
[0]) * naddrs
);
611 for (i
= 0; i
< naddrs
; i
++) {
612 if (hp
->h_addr_list
[i
] == NULL
) {
616 memcpy(&apbuf
[i
* hp
->h_length
], hp
->h_addr_list
[i
],
618 aplist
[i
] = &apbuf
[i
* hp
->h_length
];
622 for (i
= 0; aplist
[i
] != NULL
; i
++) {
627 && IN6_IS_ADDR_V4MAPPED((struct in6_addr
*)ap
)) {
629 ap
= ap
+ sizeof(struct in6_addr
)
630 - sizeof(struct in_addr
);
634 if (af
!= pai
->ai_family
)
637 if ((pai
->ai_flags
& AI_CANONNAME
) == 0) {
638 GET_AI(cur
->ai_next
, afd
, ap
);
639 GET_PORT(cur
->ai_next
, servname
);
642 * if AI_CANONNAME and if reverse lookup
643 * fail, return ai anyway to pacify
644 * calling application.
646 * XXX getaddrinfo() is a name->address
647 * translation function, and it looks
648 * strange that we do addr->name
651 get_name(ap
, afd
, &cur
->ai_next
,
655 while (cur
&& cur
->ai_next
)
659 *res
= sentinel
.ai_next
;
663 #ifdef USE_GETIPNODEBY
671 if (sentinel
.ai_next
)
672 freeaddrinfo(sentinel
.ai_next
);
678 * passive socket -> anyaddr (0.0.0.0 or ::)
679 * non-passive socket -> localhost (127.0.0.1 or ::1)
682 explore_null(pai
, hostname
, servname
, res
)
683 const struct addrinfo
*pai
;
684 const char *hostname
;
685 const char *servname
;
686 struct addrinfo
**res
;
689 const struct afd
*afd
;
690 struct addrinfo
*cur
;
691 struct addrinfo sentinel
;
695 sentinel
.ai_next
= NULL
;
699 * filter out AFs that are not supported by the kernel
702 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
710 * if the servname does not match socktype/protocol, ignore it.
712 if (get_portmatch(pai
, servname
) != 0)
715 afd
= find_afd(pai
->ai_family
);
717 if (pai
->ai_flags
& AI_PASSIVE
) {
718 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
720 * GET_CANONNAME(cur->ai_next, "anyaddr");
722 GET_PORT(cur
->ai_next
, servname
);
724 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
726 * GET_CANONNAME(cur->ai_next, "localhost");
728 GET_PORT(cur
->ai_next
, servname
);
732 *res
= sentinel
.ai_next
;
736 if (sentinel
.ai_next
)
737 freeaddrinfo(sentinel
.ai_next
);
745 explore_numeric(pai
, hostname
, servname
, res
)
746 const struct addrinfo
*pai
;
747 const char *hostname
;
748 const char *servname
;
749 struct addrinfo
**res
;
751 const struct afd
*afd
;
752 struct addrinfo
*cur
;
753 struct addrinfo sentinel
;
759 sentinel
.ai_next
= NULL
;
763 * if the servname does not match socktype/protocol, ignore it.
765 if (get_portmatch(pai
, servname
) != 0)
768 afd
= find_afd(pai
->ai_family
);
769 flags
= pai
->ai_flags
;
771 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
779 v4a
= (u_int32_t
)ntohl(((struct in_addr
*)pton
)->s_addr
);
780 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
781 flags
&= ~AI_CANONNAME
;
782 v4a
>>= IN_CLASSA_NSHIFT
;
783 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
784 flags
&= ~AI_CANONNAME
;
788 pfx
= ((struct in6_addr
*)pton
)->s6_addr
[0];
789 if (pfx
== 0 || pfx
== 0xfe || pfx
== 0xff)
790 flags
&= ~AI_CANONNAME
;
795 if (pai
->ai_family
== afd
->a_af
||
796 pai
->ai_family
== PF_UNSPEC
/*?*/) {
797 if ((flags
& AI_CANONNAME
) == 0) {
798 GET_AI(cur
->ai_next
, afd
, pton
);
799 GET_PORT(cur
->ai_next
, servname
);
802 * if AI_CANONNAME and if reverse lookup
803 * fail, return ai anyway to pacify
804 * calling application.
806 * XXX getaddrinfo() is a name->address
807 * translation function, and it looks
808 * strange that we do addr->name
811 get_name(pton
, afd
, &cur
->ai_next
,
812 pton
, pai
, servname
);
814 while (cur
&& cur
->ai_next
)
817 ERR(EAI_FAMILY
); /*xxx*/
820 *res
= sentinel
.ai_next
;
825 if (sentinel
.ai_next
)
826 freeaddrinfo(sentinel
.ai_next
);
831 * numeric hostname with scope
834 explore_numeric_scope(pai
, hostname
, servname
, res
)
835 const struct addrinfo
*pai
;
836 const char *hostname
;
837 const char *servname
;
838 struct addrinfo
**res
;
840 #ifndef SCOPE_DELIMITER
841 return explore_numeric(pai
, hostname
, servname
, res
);
843 const struct afd
*afd
;
844 struct addrinfo
*cur
;
846 char *cp
, *hostname2
= NULL
;
848 struct sockaddr_in6
*sin6
;
851 * if the servname does not match socktype/protocol, ignore it.
853 if (get_portmatch(pai
, servname
) != 0)
856 afd
= find_afd(pai
->ai_family
);
858 return explore_numeric(pai
, hostname
, servname
, res
);
860 cp
= strchr(hostname
, SCOPE_DELIMITER
);
862 return explore_numeric(pai
, hostname
, servname
, res
);
865 * Handle special case of <scoped_address><delimiter><scope id>
867 hostname2
= strdup(hostname
);
868 if (hostname2
== NULL
)
870 /* terminate at the delimiter */
871 hostname2
[cp
- hostname
] = '\0';
874 switch (pai
->ai_family
) {
877 scope
= if_nametoindex(cp
);
886 error
= explore_numeric(pai
, hostname2
, servname
, res
);
888 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
889 if (cur
->ai_family
!= AF_INET6
)
891 sin6
= (struct sockaddr_in6
*)cur
->ai_addr
;
892 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
893 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
->sin6_addr
))
894 sin6
->sin6_scope_id
= scope
;
905 get_name(addr
, afd
, res
, numaddr
, pai
, servname
)
907 const struct afd
*afd
;
908 struct addrinfo
**res
;
910 const struct addrinfo
*pai
;
911 const char *servname
;
913 struct hostent
*hp
= NULL
;
914 struct addrinfo
*cur
= NULL
;
916 char *ap
= NULL
, *cn
= NULL
;
917 #ifdef USE_GETIPNODEBY
920 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
922 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
924 if (hp
&& hp
->h_name
&& hp
->h_name
[0] && hp
->h_addr_list
[0]) {
925 #ifdef USE_GETIPNODEBY
926 GET_AI(cur
, afd
, hp
->h_addr_list
[0]);
927 GET_PORT(cur
, servname
);
928 GET_CANONNAME(cur
, hp
->h_name
);
930 /* hp will be damaged if we use gethostbyaddr() */
931 if ((ap
= (char *)malloc(hp
->h_length
)) == NULL
) {
935 memcpy(ap
, hp
->h_addr_list
[0], hp
->h_length
);
936 if ((cn
= strdup(hp
->h_name
)) == NULL
) {
941 GET_AI(cur
, afd
, ap
);
942 GET_PORT(cur
, servname
);
943 GET_CANONNAME(cur
, cn
);
948 GET_AI(cur
, afd
, numaddr
);
949 GET_PORT(cur
, servname
);
952 #ifdef USE_GETIPNODEBY
965 #ifdef USE_GETIPNODEBY
974 get_canonname(pai
, ai
, str
)
975 const struct addrinfo
*pai
;
979 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
980 ai
->ai_canonname
= (char *)malloc(strlen(str
) + 1);
981 if (ai
->ai_canonname
== NULL
)
983 strcpy(ai
->ai_canonname
, str
);
988 static struct addrinfo
*
989 get_ai(pai
, afd
, addr
)
990 const struct addrinfo
*pai
;
991 const struct afd
*afd
;
997 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
)
1002 memcpy(ai
, pai
, sizeof(struct addrinfo
));
1003 ai
->ai_addr
= (struct sockaddr
*)(ai
+ 1);
1004 memset(ai
->ai_addr
, 0, afd
->a_socklen
);
1005 #ifdef HAVE_SOCKADDR_SA_LEN
1006 ai
->ai_addr
->sa_len
= afd
->a_socklen
;
1008 ai
->ai_addrlen
= afd
->a_socklen
;
1009 ai
->ai_addr
->sa_family
= ai
->ai_family
= afd
->a_af
;
1010 p
= (char *)(ai
->ai_addr
);
1011 memcpy(p
+ afd
->a_off
, addr
, afd
->a_addrlen
);
1016 get_portmatch(ai
, servname
)
1017 const struct addrinfo
*ai
;
1018 const char *servname
;
1021 /* get_port does not touch first argument. when matchonly == 1. */
1022 return get_port((struct addrinfo
*)ai
, servname
, 1);
1026 get_port(ai
, servname
, matchonly
)
1027 struct addrinfo
*ai
;
1028 const char *servname
;
1036 if (servname
== NULL
)
1038 switch (ai
->ai_family
) {
1048 switch (ai
->ai_socktype
) {
1059 return EAI_SOCKTYPE
;
1062 if (str_isnumber(servname
)) {
1065 port
= htons(atoi(servname
));
1066 if (port
< 0 || port
> 65535)
1069 switch (ai
->ai_socktype
) {
1081 if ((sp
= getservbyname(servname
, proto
)) == NULL
)
1087 switch (ai
->ai_family
) {
1089 ((struct sockaddr_in
*)ai
->ai_addr
)->sin_port
= port
;
1093 ((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_port
= port
;
1102 static const struct afd
*
1106 const struct afd
*afd
;
1108 if (af
== PF_UNSPEC
)
1110 for (afd
= afdl
; afd
->a_af
; afd
++) {
1111 if (afd
->a_af
== af
)