2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include <openssl/crypto.h>
15 #ifndef OPENSSL_NO_SOCK
16 #include <openssl/err.h>
17 #include <openssl/buffer.h>
18 #include <internal/thread_once.h>
22 static const char *ossl_hstrerror(int herr
)
26 return strerror(errno
);
30 return "Host not found";
31 case NO_DATA
: /* NO_ADDRESS is a synonym */
34 return "Non recoverable error";
40 return "unknown error";
42 # define hstrerror(e) ossl_hstrerror(e)
45 CRYPTO_RWLOCK
*bio_lookup_lock
;
46 static CRYPTO_ONCE bio_lookup_init
= CRYPTO_ONCE_STATIC_INIT
;
49 * Throughout this file and bio_lcl.h, the existence of the macro
50 * AI_PASSIVE is used to detect the availability of struct addrinfo,
51 * getnameinfo() and getaddrinfo(). If that macro doesn't exist,
52 * we use our own implementation instead, using gethostbyname,
53 * getservbyname and a few other.
56 /**********************************************************************
62 BIO_ADDR
*BIO_ADDR_new(void)
64 BIO_ADDR
*ret
= OPENSSL_zalloc(sizeof(*ret
));
67 BIOerr(BIO_F_BIO_ADDR_NEW
, ERR_R_MALLOC_FAILURE
);
71 ret
->sa
.sa_family
= AF_UNSPEC
;
75 void BIO_ADDR_free(BIO_ADDR
*ap
)
80 void BIO_ADDR_clear(BIO_ADDR
*ap
)
82 memset(ap
, 0, sizeof(*ap
));
83 ap
->sa
.sa_family
= AF_UNSPEC
;
87 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
88 * of a struct sockaddr.
90 int BIO_ADDR_make(BIO_ADDR
*ap
, const struct sockaddr
*sa
)
92 if (sa
->sa_family
== AF_INET
) {
93 ap
->s_in
= *(const struct sockaddr_in
*)sa
;
97 if (sa
->sa_family
== AF_INET6
) {
98 ap
->s_in6
= *(const struct sockaddr_in6
*)sa
;
103 if (ap
->sa
.sa_family
== AF_UNIX
) {
104 ap
->s_un
= *(const struct sockaddr_un
*)sa
;
112 int BIO_ADDR_rawmake(BIO_ADDR
*ap
, int family
,
113 const void *where
, size_t wherelen
,
117 if (family
== AF_UNIX
) {
118 if (wherelen
+ 1 > sizeof(ap
->s_un
.sun_path
))
120 memset(&ap
->s_un
, 0, sizeof(ap
->s_un
));
121 ap
->s_un
.sun_family
= family
;
122 strncpy(ap
->s_un
.sun_path
, where
, sizeof(ap
->s_un
.sun_path
) - 1);
126 if (family
== AF_INET
) {
127 if (wherelen
!= sizeof(struct in_addr
))
129 memset(&ap
->s_in
, 0, sizeof(ap
->s_in
));
130 ap
->s_in
.sin_family
= family
;
131 ap
->s_in
.sin_port
= port
;
132 ap
->s_in
.sin_addr
= *(struct in_addr
*)where
;
136 if (family
== AF_INET6
) {
137 if (wherelen
!= sizeof(struct in6_addr
))
139 memset(&ap
->s_in6
, 0, sizeof(ap
->s_in6
));
140 ap
->s_in6
.sin6_family
= family
;
141 ap
->s_in6
.sin6_port
= port
;
142 ap
->s_in6
.sin6_addr
= *(struct in6_addr
*)where
;
150 int BIO_ADDR_family(const BIO_ADDR
*ap
)
152 return ap
->sa
.sa_family
;
155 int BIO_ADDR_rawaddress(const BIO_ADDR
*ap
, void *p
, size_t *l
)
158 const void *addrptr
= NULL
;
160 if (ap
->sa
.sa_family
== AF_INET
) {
161 len
= sizeof(ap
->s_in
.sin_addr
);
162 addrptr
= &ap
->s_in
.sin_addr
;
165 else if (ap
->sa
.sa_family
== AF_INET6
) {
166 len
= sizeof(ap
->s_in6
.sin6_addr
);
167 addrptr
= &ap
->s_in6
.sin6_addr
;
171 else if (ap
->sa
.sa_family
== AF_UNIX
) {
172 len
= strlen(ap
->s_un
.sun_path
);
173 addrptr
= &ap
->s_un
.sun_path
;
181 memcpy(p
, addrptr
, len
);
189 unsigned short BIO_ADDR_rawport(const BIO_ADDR
*ap
)
191 if (ap
->sa
.sa_family
== AF_INET
)
192 return ap
->s_in
.sin_port
;
194 if (ap
->sa
.sa_family
== AF_INET6
)
195 return ap
->s_in6
.sin6_port
;
201 * addr_strings - helper function to get host and service names
202 * @ap: the BIO_ADDR that has the input info
203 * @numeric: 0 if actual names should be returned, 1 if the numeric
204 * representation should be returned.
205 * @hostname: a pointer to a pointer to a memory area to store the
206 * host name or numeric representation. Unused if NULL.
207 * @service: a pointer to a pointer to a memory area to store the
208 * service name or numeric representation. Unused if NULL.
210 * The return value is 0 on failure, with the error code in the error
211 * stack, and 1 on success.
213 static int addr_strings(const BIO_ADDR
*ap
, int numeric
,
214 char **hostname
, char **service
)
216 if (BIO_sock_init() != 1)
222 char host
[NI_MAXHOST
] = "", serv
[NI_MAXSERV
] = "";
226 flags
|= NI_NUMERICHOST
| NI_NUMERICSERV
;
228 if ((ret
= getnameinfo(BIO_ADDR_sockaddr(ap
),
229 BIO_ADDR_sockaddr_size(ap
),
230 host
, sizeof(host
), serv
, sizeof(serv
),
233 if (ret
== EAI_SYSTEM
) {
234 SYSerr(SYS_F_GETNAMEINFO
, get_last_socket_error());
235 BIOerr(BIO_F_ADDR_STRINGS
, ERR_R_SYS_LIB
);
239 BIOerr(BIO_F_ADDR_STRINGS
, ERR_R_SYS_LIB
);
240 ERR_add_error_data(1, gai_strerror(ret
));
245 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
246 * leaves it with whatever garbage that happens to be there.
247 * However, we initialise serv with the empty string (serv[0]
248 * is therefore NUL), so it gets real easy to detect when things
249 * didn't go the way one might expect.
251 if (serv
[0] == '\0') {
252 BIO_snprintf(serv
, sizeof(serv
), "%d",
253 ntohs(BIO_ADDR_rawport(ap
)));
256 if (hostname
!= NULL
)
257 *hostname
= OPENSSL_strdup(host
);
259 *service
= OPENSSL_strdup(serv
);
262 if (hostname
!= NULL
)
263 *hostname
= OPENSSL_strdup(inet_ntoa(ap
->s_in
.sin_addr
));
264 if (service
!= NULL
) {
265 char serv
[6]; /* port is 16 bits => max 5 decimal digits */
266 BIO_snprintf(serv
, sizeof(serv
), "%d", ntohs(ap
->s_in
.sin_port
));
267 *service
= OPENSSL_strdup(serv
);
271 if ((hostname
!= NULL
&& *hostname
== NULL
)
272 || (service
!= NULL
&& *service
== NULL
)) {
273 if (hostname
!= NULL
) {
274 OPENSSL_free(*hostname
);
277 if (service
!= NULL
) {
278 OPENSSL_free(*service
);
281 BIOerr(BIO_F_ADDR_STRINGS
, ERR_R_MALLOC_FAILURE
);
288 char *BIO_ADDR_hostname_string(const BIO_ADDR
*ap
, int numeric
)
290 char *hostname
= NULL
;
292 if (addr_strings(ap
, numeric
, &hostname
, NULL
))
298 char *BIO_ADDR_service_string(const BIO_ADDR
*ap
, int numeric
)
300 char *service
= NULL
;
302 if (addr_strings(ap
, numeric
, NULL
, &service
))
308 char *BIO_ADDR_path_string(const BIO_ADDR
*ap
)
311 if (ap
->sa
.sa_family
== AF_UNIX
)
312 return OPENSSL_strdup(ap
->s_un
.sun_path
);
318 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
319 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
320 * The returned struct sockaddr is const, so it can't be tampered with.
322 const struct sockaddr
*BIO_ADDR_sockaddr(const BIO_ADDR
*ap
)
328 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
329 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
330 * it allows you to tamper with the data (and thereby the contents
331 * of the input BIO_ADDR).
333 struct sockaddr
*BIO_ADDR_sockaddr_noconst(BIO_ADDR
*ap
)
339 * BIO_ADDR_sockaddr_size - non-public function that returns the size
340 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
341 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
342 * the size of the BIO_ADDR type is returned.
344 socklen_t
BIO_ADDR_sockaddr_size(const BIO_ADDR
*ap
)
346 if (ap
->sa
.sa_family
== AF_INET
)
347 return sizeof(ap
->s_in
);
349 if (ap
->sa
.sa_family
== AF_INET6
)
350 return sizeof(ap
->s_in6
);
353 if (ap
->sa
.sa_family
== AF_UNIX
)
354 return sizeof(ap
->s_un
);
359 /**********************************************************************
361 * Address info database
365 const BIO_ADDRINFO
*BIO_ADDRINFO_next(const BIO_ADDRINFO
*bai
)
368 return bai
->bai_next
;
372 int BIO_ADDRINFO_family(const BIO_ADDRINFO
*bai
)
375 return bai
->bai_family
;
379 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO
*bai
)
382 return bai
->bai_socktype
;
386 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO
*bai
)
389 if (bai
->bai_protocol
!= 0)
390 return bai
->bai_protocol
;
393 if (bai
->bai_family
== AF_UNIX
)
397 switch (bai
->bai_socktype
) {
410 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
411 * of the struct sockaddr inside the BIO_ADDRINFO.
413 socklen_t
BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO
*bai
)
416 return bai
->bai_addrlen
;
421 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
422 * as the struct sockaddr it is.
424 const struct sockaddr
*BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO
*bai
)
427 return bai
->bai_addr
;
431 const BIO_ADDR
*BIO_ADDRINFO_address(const BIO_ADDRINFO
*bai
)
434 return (BIO_ADDR
*)bai
->bai_addr
;
438 void BIO_ADDRINFO_free(BIO_ADDRINFO
*bai
)
445 # define _cond bai->bai_family != AF_UNIX
455 /* Free manually when we know that addrinfo_wrap() was used.
456 * See further comment above addrinfo_wrap()
458 while (bai
!= NULL
) {
459 BIO_ADDRINFO
*next
= bai
->bai_next
;
460 OPENSSL_free(bai
->bai_addr
);
466 /**********************************************************************
473 * The specs in hostserv can take these forms:
475 * host:service => *host = "host", *service = "service"
476 * host:* => *host = "host", *service = NULL
477 * host: => *host = "host", *service = NULL
478 * :service => *host = NULL, *service = "service"
479 * *:service => *host = NULL, *service = "service"
481 * in case no : is present in the string, the result depends on
482 * hostserv_prio, as follows:
484 * when hostserv_prio == BIO_PARSE_PRIO_HOST
485 * host => *host = "host", *service untouched
487 * when hostserv_prio == BIO_PARSE_PRIO_SERV
488 * service => *host untouched, *service = "service"
491 int BIO_parse_hostserv(const char *hostserv
, char **host
, char **service
,
492 enum BIO_hostserv_priorities hostserv_prio
)
494 const char *h
= NULL
; size_t hl
= 0;
495 const char *p
= NULL
; size_t pl
= 0;
497 if (*hostserv
== '[') {
498 if ((p
= strchr(hostserv
, ']')) == NULL
)
512 const char *p2
= strrchr(hostserv
, ':');
513 p
= strchr(hostserv
, ':');
516 * Check for more than one colon. There are three possible
518 * 1. IPv6 address with port number, last colon being separator.
519 * 2. IPv6 address only.
520 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
521 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
522 * Because of this ambiguity, we currently choose to make it an
533 } else if (hostserv_prio
== BIO_PARSE_PRIO_HOST
) {
542 if (p
!= NULL
&& strchr(p
, ':'))
545 if (h
!= NULL
&& host
!= NULL
) {
547 || (hl
== 1 && h
[0] == '*')) {
550 *host
= OPENSSL_strndup(h
, hl
);
555 if (p
!= NULL
&& service
!= NULL
) {
557 || (pl
== 1 && p
[0] == '*')) {
560 *service
= OPENSSL_strndup(p
, pl
);
561 if (*service
== NULL
)
568 BIOerr(BIO_F_BIO_PARSE_HOSTSERV
, BIO_R_AMBIGUOUS_HOST_OR_SERVICE
);
571 BIOerr(BIO_F_BIO_PARSE_HOSTSERV
, BIO_R_MALFORMED_HOST_OR_SERVICE
);
574 BIOerr(BIO_F_BIO_PARSE_HOSTSERV
, ERR_R_MALLOC_FAILURE
);
578 /* addrinfo_wrap is used to build our own addrinfo "chain".
579 * (it has only one entry, so calling it a chain may be a stretch)
580 * It should ONLY be called when getaddrinfo() and friends
581 * aren't available, OR when dealing with a non IP protocol
582 * family, such as AF_UNIX
584 * the return value is 1 on success, or 0 on failure, which
585 * only happens if a memory allocation error occurred.
587 static int addrinfo_wrap(int family
, int socktype
,
588 const void *where
, size_t wherelen
,
592 OPENSSL_assert(bai
!= NULL
);
594 *bai
= OPENSSL_zalloc(sizeof(**bai
));
598 (*bai
)->bai_family
= family
;
599 (*bai
)->bai_socktype
= socktype
;
600 if (socktype
== SOCK_STREAM
)
601 (*bai
)->bai_protocol
= IPPROTO_TCP
;
602 if (socktype
== SOCK_DGRAM
)
603 (*bai
)->bai_protocol
= IPPROTO_UDP
;
605 if (family
== AF_UNIX
)
606 (*bai
)->bai_protocol
= 0;
609 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
610 just an advanced cast of BIO_ADDR* to struct sockaddr *
611 by the power of union, so while it may seem that we're
612 creating a memory leak here, we are not. It will be
614 BIO_ADDR
*addr
= BIO_ADDR_new();
616 BIO_ADDR_rawmake(addr
, family
, where
, wherelen
, port
);
617 (*bai
)->bai_addr
= BIO_ADDR_sockaddr_noconst(addr
);
620 (*bai
)->bai_next
= NULL
;
621 if ((*bai
)->bai_addr
== NULL
) {
622 BIO_ADDRINFO_free(*bai
);
629 DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init
)
631 OPENSSL_init_crypto(0, NULL
);
632 bio_lookup_lock
= CRYPTO_THREAD_lock_new();
633 return bio_lookup_lock
!= NULL
;
637 * BIO_lookup - look up the node and service you want to connect to.
638 * @node: the node you want to connect to.
639 * @service: the service you want to connect to.
640 * @lookup_type: declare intent with the result, client or server.
641 * @family: the address family you want to use. Use AF_UNSPEC for any, or
642 * AF_INET, AF_INET6 or AF_UNIX.
643 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
645 * @res: Storage place for the resulting list of returned addresses
647 * This will do a lookup of the node and service that you want to connect to.
648 * It returns a linked list of different addresses you can try to connect to.
650 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
652 * The return value is 1 on success or 0 in case of error.
654 int BIO_lookup(const char *host
, const char *service
,
655 enum BIO_lookup_type lookup_type
,
656 int family
, int socktype
, BIO_ADDRINFO
**res
)
658 int ret
= 0; /* Assume failure */
673 BIOerr(BIO_F_BIO_LOOKUP
, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY
);
678 if (family
== AF_UNIX
) {
679 if (addrinfo_wrap(family
, socktype
, host
, strlen(host
), 0, res
))
682 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_MALLOC_FAILURE
);
687 if (BIO_sock_init() != 1)
693 struct addrinfo hints
;
694 memset(&hints
, 0, sizeof hints
);
696 hints
.ai_family
= family
;
697 hints
.ai_socktype
= socktype
;
699 if (lookup_type
== BIO_LOOKUP_SERVER
)
700 hints
.ai_flags
|= AI_PASSIVE
;
702 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
703 * macro magic in bio_lcl.h
705 switch ((gai_ret
= getaddrinfo(host
, service
, &hints
, res
))) {
708 SYSerr(SYS_F_GETADDRINFO
, get_last_socket_error());
709 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_SYS_LIB
);
713 ret
= 1; /* Success */
716 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_SYS_LIB
);
717 ERR_add_error_data(1, gai_strerror(gai_ret
));
722 const struct hostent
*he
;
724 * Because struct hostent is defined for 32-bit pointers only with
725 * VMS C, we need to make sure that '&he_fallback_address' and
726 * '&he_fallback_addresses' are 32-bit pointers
728 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
729 # pragma pointer_size save
730 # pragma pointer_size 32
732 /* Windows doesn't seem to have in_addr_t */
733 #ifdef OPENSSL_SYS_WINDOWS
734 static uint32_t he_fallback_address
;
735 static const char *he_fallback_addresses
[] =
736 { (char *)&he_fallback_address
, NULL
};
738 static in_addr_t he_fallback_address
;
739 static const char *he_fallback_addresses
[] =
740 { (char *)&he_fallback_address
, NULL
};
742 static const struct hostent he_fallback
=
743 { NULL
, NULL
, AF_INET
, sizeof(he_fallback_address
),
744 (char **)&he_fallback_addresses
};
745 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
746 # pragma pointer_size restore
750 /* Apparently, on WIN64, s_proto and s_port have traded places... */
752 struct servent se_fallback
= { NULL
, NULL
, NULL
, 0 };
754 struct servent se_fallback
= { NULL
, NULL
, 0, NULL
};
757 if (!RUN_ONCE(&bio_lookup_init
, do_bio_lookup_init
)) {
758 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_MALLOC_FAILURE
);
763 CRYPTO_THREAD_write_lock(bio_lookup_lock
);
764 he_fallback_address
= INADDR_ANY
;
767 switch(lookup_type
) {
768 case BIO_LOOKUP_CLIENT
:
769 he_fallback_address
= INADDR_LOOPBACK
;
771 case BIO_LOOKUP_SERVER
:
772 he_fallback_address
= INADDR_ANY
;
775 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
779 he
= gethostbyname(host
);
782 #ifndef OPENSSL_SYS_WINDOWS
783 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_SYS_LIB
);
784 ERR_add_error_data(1, hstrerror(h_errno
));
786 SYSerr(SYS_F_GETHOSTBYNAME
, WSAGetLastError());
793 if (service
== NULL
) {
794 se_fallback
.s_port
= 0;
795 se_fallback
.s_proto
= NULL
;
799 long portnum
= strtol(service
, &endp
, 10);
802 * Because struct servent is defined for 32-bit pointers only with
803 * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
805 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
806 # pragma pointer_size save
807 # pragma pointer_size 32
810 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
811 # pragma pointer_size restore
823 if (endp
!= service
&& *endp
== '\0'
824 && portnum
> 0 && portnum
< 65536) {
825 se_fallback
.s_port
= htons(portnum
);
826 se_fallback
.s_proto
= proto
;
828 } else if (endp
== service
) {
829 se
= getservbyname(service
, proto
);
832 #ifndef OPENSSL_SYS_WINDOWS
833 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_SYS_LIB
);
834 ERR_add_error_data(1, hstrerror(h_errno
));
836 SYSerr(SYS_F_GETSERVBYNAME
, WSAGetLastError());
841 BIOerr(BIO_F_BIO_LOOKUP
, BIO_R_MALFORMED_HOST_OR_SERVICE
);
850 * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
851 * we must make sure our iterator designates the same element type, hence
852 * the pointer size dance.
854 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
855 # pragma pointer_size save
856 # pragma pointer_size 32
859 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
860 # pragma pointer_size restore
863 BIO_ADDRINFO
*tmp_bai
= NULL
;
865 /* The easiest way to create a linked list from an
866 array is to start from the back */
867 for(addrlistp
= he
->h_addr_list
; *addrlistp
!= NULL
;
871 for(addresses
= addrlistp
- he
->h_addr_list
;
872 addrlistp
--, addresses
-- > 0; ) {
873 if (!addrinfo_wrap(he
->h_addrtype
, socktype
,
874 *addrlistp
, he
->h_length
,
875 se
->s_port
, &tmp_bai
))
876 goto addrinfo_malloc_err
;
877 tmp_bai
->bai_next
= *res
;
881 BIO_ADDRINFO_free(*res
);
883 BIOerr(BIO_F_BIO_LOOKUP
, ERR_R_MALLOC_FAILURE
);
891 CRYPTO_THREAD_unlock(bio_lookup_lock
);
897 #endif /* OPENSSL_NO_SOCK */