2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
17 #include "bio_local.h"
18 #include <openssl/crypto.h>
20 #ifndef OPENSSL_NO_SOCK
21 #include <openssl/err.h>
22 #include <openssl/buffer.h>
23 #include "internal/thread_once.h"
25 CRYPTO_RWLOCK
*bio_lookup_lock
;
26 static CRYPTO_ONCE bio_lookup_init
= CRYPTO_ONCE_STATIC_INIT
;
29 * Throughout this file and bio_local.h, the existence of the macro
30 * AI_PASSIVE is used to detect the availability of struct addrinfo,
31 * getnameinfo() and getaddrinfo(). If that macro doesn't exist,
32 * we use our own implementation instead, using gethostbyname,
33 * getservbyname and a few other.
36 /**********************************************************************
42 BIO_ADDR
*BIO_ADDR_new(void)
44 BIO_ADDR
*ret
= OPENSSL_zalloc(sizeof(*ret
));
47 BIOerr(BIO_F_BIO_ADDR_NEW
, ERR_R_MALLOC_FAILURE
);
51 ret
->sa
.sa_family
= AF_UNSPEC
;
55 void BIO_ADDR_free(BIO_ADDR
*ap
)
60 void BIO_ADDR_clear(BIO_ADDR
*ap
)
62 memset(ap
, 0, sizeof(*ap
));
63 ap
->sa
.sa_family
= AF_UNSPEC
;
67 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
68 * of a struct sockaddr.
70 int BIO_ADDR_make(BIO_ADDR
*ap
, const struct sockaddr
*sa
)
72 if (sa
->sa_family
== AF_INET
) {
73 memcpy(&(ap
->s_in
), sa
, sizeof(struct sockaddr_in
));
77 if (sa
->sa_family
== AF_INET6
) {
78 memcpy(&(ap
->s_in6
), sa
, sizeof(struct sockaddr_in6
));
83 if (sa
->sa_family
== AF_UNIX
) {
84 memcpy(&(ap
->s_un
), sa
, sizeof(struct sockaddr_un
));
92 int BIO_ADDR_rawmake(BIO_ADDR
*ap
, int family
,
93 const void *where
, size_t wherelen
,
97 if (family
== AF_UNIX
) {
98 if (wherelen
+ 1 > sizeof(ap
->s_un
.sun_path
))
100 memset(&ap
->s_un
, 0, sizeof(ap
->s_un
));
101 ap
->s_un
.sun_family
= family
;
102 strncpy(ap
->s_un
.sun_path
, where
, sizeof(ap
->s_un
.sun_path
) - 1);
106 if (family
== AF_INET
) {
107 if (wherelen
!= sizeof(struct in_addr
))
109 memset(&ap
->s_in
, 0, sizeof(ap
->s_in
));
110 ap
->s_in
.sin_family
= family
;
111 ap
->s_in
.sin_port
= port
;
112 ap
->s_in
.sin_addr
= *(struct in_addr
*)where
;
116 if (family
== AF_INET6
) {
117 if (wherelen
!= sizeof(struct in6_addr
))
119 memset(&ap
->s_in6
, 0, sizeof(ap
->s_in6
));
120 ap
->s_in6
.sin6_family
= family
;
121 ap
->s_in6
.sin6_port
= port
;
122 ap
->s_in6
.sin6_addr
= *(struct in6_addr
*)where
;
130 int BIO_ADDR_family(const BIO_ADDR
*ap
)
132 return ap
->sa
.sa_family
;
135 int BIO_ADDR_rawaddress(const BIO_ADDR
*ap
, void *p
, size_t *l
)
138 const void *addrptr
= NULL
;
140 if (ap
->sa
.sa_family
== AF_INET
) {
141 len
= sizeof(ap
->s_in
.sin_addr
);
142 addrptr
= &ap
->s_in
.sin_addr
;
145 else if (ap
->sa
.sa_family
== AF_INET6
) {
146 len
= sizeof(ap
->s_in6
.sin6_addr
);
147 addrptr
= &ap
->s_in6
.sin6_addr
;
151 else if (ap
->sa
.sa_family
== AF_UNIX
) {
152 len
= strlen(ap
->s_un
.sun_path
);
153 addrptr
= &ap
->s_un
.sun_path
;
161 memcpy(p
, addrptr
, len
);
169 unsigned short BIO_ADDR_rawport(const BIO_ADDR
*ap
)
171 if (ap
->sa
.sa_family
== AF_INET
)
172 return ap
->s_in
.sin_port
;
174 if (ap
->sa
.sa_family
== AF_INET6
)
175 return ap
->s_in6
.sin6_port
;
181 * addr_strings - helper function to get host and service names
182 * @ap: the BIO_ADDR that has the input info
183 * @numeric: 0 if actual names should be returned, 1 if the numeric
184 * representation should be returned.
185 * @hostname: a pointer to a pointer to a memory area to store the
186 * host name or numeric representation. Unused if NULL.
187 * @service: a pointer to a pointer to a memory area to store the
188 * service name or numeric representation. Unused if NULL.
190 * The return value is 0 on failure, with the error code in the error
191 * stack, and 1 on success.
193 static int addr_strings(const BIO_ADDR
*ap
, int numeric
,
194 char **hostname
, char **service
)
196 if (BIO_sock_init() != 1)
202 char host
[NI_MAXHOST
] = "", serv
[NI_MAXSERV
] = "";
206 flags
|= NI_NUMERICHOST
| NI_NUMERICSERV
;
208 if ((ret
= getnameinfo(BIO_ADDR_sockaddr(ap
),
209 BIO_ADDR_sockaddr_size(ap
),
210 host
, sizeof(host
), serv
, sizeof(serv
),
213 if (ret
== EAI_SYSTEM
) {
214 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
215 "calling getnameinfo()");
216 BIOerr(BIO_F_ADDR_STRINGS
, ERR_R_SYS_LIB
);
220 BIOerr(BIO_F_ADDR_STRINGS
, ERR_R_SYS_LIB
);
221 ERR_add_error_data(1, gai_strerror(ret
));
226 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
227 * leaves it with whatever garbage that happens to be there.
228 * However, we initialise serv with the empty string (serv[0]
229 * is therefore NUL), so it gets real easy to detect when things
230 * didn't go the way one might expect.
232 if (serv
[0] == '\0') {
233 BIO_snprintf(serv
, sizeof(serv
), "%d",
234 ntohs(BIO_ADDR_rawport(ap
)));
237 if (hostname
!= NULL
)
238 *hostname
= OPENSSL_strdup(host
);
240 *service
= OPENSSL_strdup(serv
);
243 if (hostname
!= NULL
)
244 *hostname
= OPENSSL_strdup(inet_ntoa(ap
->s_in
.sin_addr
));
245 if (service
!= NULL
) {
246 char serv
[6]; /* port is 16 bits => max 5 decimal digits */
247 BIO_snprintf(serv
, sizeof(serv
), "%d", ntohs(ap
->s_in
.sin_port
));
248 *service
= OPENSSL_strdup(serv
);
252 if ((hostname
!= NULL
&& *hostname
== NULL
)
253 || (service
!= NULL
&& *service
== NULL
)) {
254 if (hostname
!= NULL
) {
255 OPENSSL_free(*hostname
);
258 if (service
!= NULL
) {
259 OPENSSL_free(*service
);
262 BIOerr(BIO_F_ADDR_STRINGS
, ERR_R_MALLOC_FAILURE
);
269 char *BIO_ADDR_hostname_string(const BIO_ADDR
*ap
, int numeric
)
271 char *hostname
= NULL
;
273 if (addr_strings(ap
, numeric
, &hostname
, NULL
))
279 char *BIO_ADDR_service_string(const BIO_ADDR
*ap
, int numeric
)
281 char *service
= NULL
;
283 if (addr_strings(ap
, numeric
, NULL
, &service
))
289 char *BIO_ADDR_path_string(const BIO_ADDR
*ap
)
292 if (ap
->sa
.sa_family
== AF_UNIX
)
293 return OPENSSL_strdup(ap
->s_un
.sun_path
);
299 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
300 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
301 * The returned struct sockaddr is const, so it can't be tampered with.
303 const struct sockaddr
*BIO_ADDR_sockaddr(const BIO_ADDR
*ap
)
309 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
310 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
311 * it allows you to tamper with the data (and thereby the contents
312 * of the input BIO_ADDR).
314 struct sockaddr
*BIO_ADDR_sockaddr_noconst(BIO_ADDR
*ap
)
320 * BIO_ADDR_sockaddr_size - non-public function that returns the size
321 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
322 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
323 * the size of the BIO_ADDR type is returned.
325 socklen_t
BIO_ADDR_sockaddr_size(const BIO_ADDR
*ap
)
327 if (ap
->sa
.sa_family
== AF_INET
)
328 return sizeof(ap
->s_in
);
330 if (ap
->sa
.sa_family
== AF_INET6
)
331 return sizeof(ap
->s_in6
);
334 if (ap
->sa
.sa_family
== AF_UNIX
)
335 return sizeof(ap
->s_un
);
340 /**********************************************************************
342 * Address info database
346 const BIO_ADDRINFO
*BIO_ADDRINFO_next(const BIO_ADDRINFO
*bai
)
349 return bai
->bai_next
;
353 int BIO_ADDRINFO_family(const BIO_ADDRINFO
*bai
)
356 return bai
->bai_family
;
360 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO
*bai
)
363 return bai
->bai_socktype
;
367 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO
*bai
)
370 if (bai
->bai_protocol
!= 0)
371 return bai
->bai_protocol
;
374 if (bai
->bai_family
== AF_UNIX
)
378 switch (bai
->bai_socktype
) {
391 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
392 * of the struct sockaddr inside the BIO_ADDRINFO.
394 socklen_t
BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO
*bai
)
397 return bai
->bai_addrlen
;
402 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
403 * as the struct sockaddr it is.
405 const struct sockaddr
*BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO
*bai
)
408 return bai
->bai_addr
;
412 const BIO_ADDR
*BIO_ADDRINFO_address(const BIO_ADDRINFO
*bai
)
415 return (BIO_ADDR
*)bai
->bai_addr
;
419 void BIO_ADDRINFO_free(BIO_ADDRINFO
*bai
)
426 # define _cond bai->bai_family != AF_UNIX
436 /* Free manually when we know that addrinfo_wrap() was used.
437 * See further comment above addrinfo_wrap()
439 while (bai
!= NULL
) {
440 BIO_ADDRINFO
*next
= bai
->bai_next
;
441 OPENSSL_free(bai
->bai_addr
);
447 /**********************************************************************
454 * The specs in hostserv can take these forms:
456 * host:service => *host = "host", *service = "service"
457 * host:* => *host = "host", *service = NULL
458 * host: => *host = "host", *service = NULL
459 * :service => *host = NULL, *service = "service"
460 * *:service => *host = NULL, *service = "service"
462 * in case no : is present in the string, the result depends on
463 * hostserv_prio, as follows:
465 * when hostserv_prio == BIO_PARSE_PRIO_HOST
466 * host => *host = "host", *service untouched
468 * when hostserv_prio == BIO_PARSE_PRIO_SERV
469 * service => *host untouched, *service = "service"
472 int BIO_parse_hostserv(const char *hostserv
, char **host
, char **service
,
473 enum BIO_hostserv_priorities hostserv_prio
)
475 const char *h
= NULL
; size_t hl
= 0;
476 const char *p
= NULL
; size_t pl
= 0;
478 if (*hostserv
== '[') {
479 if ((p
= strchr(hostserv
, ']')) == NULL
)
493 const char *p2
= strrchr(hostserv
, ':');
494 p
= strchr(hostserv
, ':');
497 * Check for more than one colon. There are three possible
499 * 1. IPv6 address with port number, last colon being separator.
500 * 2. IPv6 address only.
501 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
502 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
503 * Because of this ambiguity, we currently choose to make it an
514 } else if (hostserv_prio
== BIO_PARSE_PRIO_HOST
) {
523 if (p
!= NULL
&& strchr(p
, ':'))
526 if (h
!= NULL
&& host
!= NULL
) {
528 || (hl
== 1 && h
[0] == '*')) {
531 *host
= OPENSSL_strndup(h
, hl
);
536 if (p
!= NULL
&& service
!= NULL
) {
538 || (pl
== 1 && p
[0] == '*')) {
541 *service
= OPENSSL_strndup(p
, pl
);
542 if (*service
== NULL
)
549 BIOerr(BIO_F_BIO_PARSE_HOSTSERV
, BIO_R_AMBIGUOUS_HOST_OR_SERVICE
);
552 BIOerr(BIO_F_BIO_PARSE_HOSTSERV
, BIO_R_MALFORMED_HOST_OR_SERVICE
);
555 BIOerr(BIO_F_BIO_PARSE_HOSTSERV
, ERR_R_MALLOC_FAILURE
);
559 /* addrinfo_wrap is used to build our own addrinfo "chain".
560 * (it has only one entry, so calling it a chain may be a stretch)
561 * It should ONLY be called when getaddrinfo() and friends
562 * aren't available, OR when dealing with a non IP protocol
563 * family, such as AF_UNIX
565 * the return value is 1 on success, or 0 on failure, which
566 * only happens if a memory allocation error occurred.
568 static int addrinfo_wrap(int family
, int socktype
,
569 const void *where
, size_t wherelen
,
573 if ((*bai
= OPENSSL_zalloc(sizeof(**bai
))) == NULL
) {
574 BIOerr(BIO_F_ADDRINFO_WRAP
, ERR_R_MALLOC_FAILURE
);
578 (*bai
)->bai_family
= family
;
579 (*bai
)->bai_socktype
= socktype
;
580 if (socktype
== SOCK_STREAM
)
581 (*bai
)->bai_protocol
= IPPROTO_TCP
;
582 if (socktype
== SOCK_DGRAM
)
583 (*bai
)->bai_protocol
= IPPROTO_UDP
;
585 if (family
== AF_UNIX
)
586 (*bai
)->bai_protocol
= 0;
589 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
590 just an advanced cast of BIO_ADDR* to struct sockaddr *
591 by the power of union, so while it may seem that we're
592 creating a memory leak here, we are not. It will be
594 BIO_ADDR
*addr
= BIO_ADDR_new();
596 BIO_ADDR_rawmake(addr
, family
, where
, wherelen
, port
);
597 (*bai
)->bai_addr
= BIO_ADDR_sockaddr_noconst(addr
);
600 (*bai
)->bai_next
= NULL
;
601 if ((*bai
)->bai_addr
== NULL
) {
602 BIO_ADDRINFO_free(*bai
);
609 DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init
)
611 if (!OPENSSL_init_crypto(0, NULL
))
613 bio_lookup_lock
= CRYPTO_THREAD_lock_new();
614 return bio_lookup_lock
!= NULL
;
617 int BIO_lookup(const char *host
, const char *service
,
618 enum BIO_lookup_type lookup_type
,
619 int family
, int socktype
, BIO_ADDRINFO
**res
)
621 return BIO_lookup_ex(host
, service
, lookup_type
, family
, socktype
, 0, res
);
625 * BIO_lookup_ex - look up the node and service you want to connect to.
626 * @node: the node you want to connect to.
627 * @service: the service you want to connect to.
628 * @lookup_type: declare intent with the result, client or server.
629 * @family: the address family you want to use. Use AF_UNSPEC for any, or
630 * AF_INET, AF_INET6 or AF_UNIX.
631 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
633 * @protocol: The protocol to use, e.g. IPPROTO_TCP or IPPROTO_UDP or 0 for all.
634 * Note that some platforms may not return IPPROTO_SCTP without
635 * explicitly requesting it (i.e. IPPROTO_SCTP may not be returned
636 * with 0 for the protocol)
637 * @res: Storage place for the resulting list of returned addresses
639 * This will do a lookup of the node and service that you want to connect to.
640 * It returns a linked list of different addresses you can try to connect to.
642 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
644 * The return value is 1 on success or 0 in case of error.
646 int BIO_lookup_ex(const char *host
, const char *service
, int lookup_type
,
647 int family
, int socktype
, int protocol
, BIO_ADDRINFO
**res
)
649 int ret
= 0; /* Assume failure */
664 BIOerr(BIO_F_BIO_LOOKUP_EX
, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY
);
669 if (family
== AF_UNIX
) {
670 if (addrinfo_wrap(family
, socktype
, host
, strlen(host
), 0, res
))
673 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_MALLOC_FAILURE
);
678 if (BIO_sock_init() != 1)
683 int gai_ret
= 0, old_ret
= 0;
684 struct addrinfo hints
;
686 memset(&hints
, 0, sizeof(hints
));
688 hints
.ai_family
= family
;
689 hints
.ai_socktype
= socktype
;
690 hints
.ai_protocol
= protocol
;
691 # ifdef AI_ADDRCONFIG
693 if (family
== AF_UNSPEC
)
695 hints
.ai_flags
|= AI_ADDRCONFIG
;
698 if (lookup_type
== BIO_LOOKUP_SERVER
)
699 hints
.ai_flags
|= AI_PASSIVE
;
701 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
702 * macro magic in bio_local.h
704 # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST)
707 switch ((gai_ret
= getaddrinfo(host
, service
, &hints
, res
))) {
710 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
711 "calling getaddrinfo()");
712 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_SYS_LIB
);
717 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_MALLOC_FAILURE
);
721 ret
= 1; /* Success */
724 # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST)
725 if (hints
.ai_flags
& AI_ADDRCONFIG
) {
726 hints
.ai_flags
&= ~AI_ADDRCONFIG
;
727 hints
.ai_flags
|= AI_NUMERICHOST
;
732 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_SYS_LIB
);
733 ERR_add_error_data(1, gai_strerror(old_ret
? old_ret
: gai_ret
));
738 const struct hostent
*he
;
740 * Because struct hostent is defined for 32-bit pointers only with
741 * VMS C, we need to make sure that '&he_fallback_address' and
742 * '&he_fallback_addresses' are 32-bit pointers
744 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
745 # pragma pointer_size save
746 # pragma pointer_size 32
748 /* Windows doesn't seem to have in_addr_t */
749 #ifdef OPENSSL_SYS_WINDOWS
750 static uint32_t he_fallback_address
;
751 static const char *he_fallback_addresses
[] =
752 { (char *)&he_fallback_address
, NULL
};
754 static in_addr_t he_fallback_address
;
755 static const char *he_fallback_addresses
[] =
756 { (char *)&he_fallback_address
, NULL
};
758 static const struct hostent he_fallback
=
759 { NULL
, NULL
, AF_INET
, sizeof(he_fallback_address
),
760 (char **)&he_fallback_addresses
};
761 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
762 # pragma pointer_size restore
766 /* Apparently, on WIN64, s_proto and s_port have traded places... */
768 struct servent se_fallback
= { NULL
, NULL
, NULL
, 0 };
770 struct servent se_fallback
= { NULL
, NULL
, 0, NULL
};
773 if (!RUN_ONCE(&bio_lookup_init
, do_bio_lookup_init
)) {
774 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_MALLOC_FAILURE
);
779 CRYPTO_THREAD_write_lock(bio_lookup_lock
);
780 he_fallback_address
= INADDR_ANY
;
783 switch(lookup_type
) {
784 case BIO_LOOKUP_CLIENT
:
785 he_fallback_address
= INADDR_LOOPBACK
;
787 case BIO_LOOKUP_SERVER
:
788 he_fallback_address
= INADDR_ANY
;
791 /* We forgot to handle a lookup type! */
792 assert("We forgot to handle a lookup type!" == NULL
);
793 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_INTERNAL_ERROR
);
798 he
= gethostbyname(host
);
801 #ifndef OPENSSL_SYS_WINDOWS
803 * This might be misleading, because h_errno is used as if
804 * it was errno. To minimize mixup add 1000. Underlying
805 * reason for this is that hstrerror is declared obsolete,
806 * not to mention that a) h_errno is not always guaranteed
807 * to be meaningless; b) hstrerror can reside in yet another
808 * library, linking for sake of hstrerror is an overkill;
809 * c) this path is not executed on contemporary systems
810 * anyway [above getaddrinfo/gai_strerror is]. We just let
811 * system administrator figure this out...
813 # if defined(OPENSSL_SYS_VXWORKS)
814 /* h_errno doesn't exist on VxWorks */
815 ERR_raise_data(ERR_LIB_SYS
, 1000,
816 "calling gethostbyname()");
818 ERR_raise_data(ERR_LIB_SYS
, 1000 + h_errno
,
819 "calling gethostbyname()");
822 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
823 "calling gethostbyname()");
830 if (service
== NULL
) {
831 se_fallback
.s_port
= 0;
832 se_fallback
.s_proto
= NULL
;
836 long portnum
= strtol(service
, &endp
, 10);
839 * Because struct servent is defined for 32-bit pointers only with
840 * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
842 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
843 # pragma pointer_size save
844 # pragma pointer_size 32
847 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
848 # pragma pointer_size restore
860 if (endp
!= service
&& *endp
== '\0'
861 && portnum
> 0 && portnum
< 65536) {
862 se_fallback
.s_port
= htons((unsigned short)portnum
);
863 se_fallback
.s_proto
= proto
;
865 } else if (endp
== service
) {
866 se
= getservbyname(service
, proto
);
869 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
870 "calling getservbyname()");
874 BIOerr(BIO_F_BIO_LOOKUP_EX
, BIO_R_MALFORMED_HOST_OR_SERVICE
);
883 * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
884 * we must make sure our iterator designates the same element type, hence
885 * the pointer size dance.
887 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
888 # pragma pointer_size save
889 # pragma pointer_size 32
892 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
893 # pragma pointer_size restore
896 BIO_ADDRINFO
*tmp_bai
= NULL
;
898 /* The easiest way to create a linked list from an
899 array is to start from the back */
900 for(addrlistp
= he
->h_addr_list
; *addrlistp
!= NULL
;
904 for(addresses
= addrlistp
- he
->h_addr_list
;
905 addrlistp
--, addresses
-- > 0; ) {
906 if (!addrinfo_wrap(he
->h_addrtype
, socktype
,
907 *addrlistp
, he
->h_length
,
908 se
->s_port
, &tmp_bai
))
909 goto addrinfo_malloc_err
;
910 tmp_bai
->bai_next
= *res
;
914 BIO_ADDRINFO_free(*res
);
916 BIOerr(BIO_F_BIO_LOOKUP_EX
, ERR_R_MALLOC_FAILURE
);
924 CRYPTO_THREAD_unlock(bio_lookup_lock
);
930 #endif /* OPENSSL_NO_SOCK */