1 /* ====================================================================
2 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * 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.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
58 #include "internal/threads.h"
60 #ifndef OPENSSL_NO_SOCK
61 #include <openssl/err.h>
62 #include <openssl/buffer.h>
65 CRYPTO_RWLOCK *bio_lookup_lock;
66 static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT;
69 * Throughout this file and bio_lcl.h, the existence of the macro
70 * AI_PASSIVE is used to detect the availability of struct addrinfo,
71 * getnameinfo() and getaddrinfo(). If that macro doesn't exist,
72 * we use our own implementation instead, using gethostbyname,
73 * getservbyname and a few other.
76 /**********************************************************************
82 BIO_ADDR *BIO_ADDR_new(void)
84 BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret));
87 BIOerr(BIO_F_BIO_ADDR_NEW, ERR_R_MALLOC_FAILURE);
91 ret->sa.sa_family = AF_UNSPEC;
95 void BIO_ADDR_free(BIO_ADDR *ap)
100 void BIO_ADDR_clear(BIO_ADDR *ap)
102 memset(ap, 0, sizeof(*ap));
103 ap->sa.sa_family = AF_UNSPEC;
107 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
108 * of a struct sockaddr.
110 int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa)
112 if (sa->sa_family == AF_INET) {
113 ap->s_in = *(const struct sockaddr_in *)sa;
117 if (sa->sa_family == AF_INET6) {
118 ap->s_in6 = *(const struct sockaddr_in6 *)sa;
123 if (ap->sa.sa_family == AF_UNIX) {
124 ap->s_un = *(const struct sockaddr_un *)sa;
132 int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
133 const void *where, size_t wherelen,
137 if (family == AF_UNIX) {
138 if (wherelen + 1 > sizeof(ap->s_un.sun_path))
140 memset(&ap->s_un, 0, sizeof(ap->s_un));
141 ap->s_un.sun_family = family;
142 strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1);
146 if (family == AF_INET) {
147 if (wherelen != sizeof(struct in_addr))
149 memset(&ap->s_in, 0, sizeof(ap->s_in));
150 ap->s_in.sin_family = family;
151 ap->s_in.sin_port = port;
152 ap->s_in.sin_addr = *(struct in_addr *)where;
156 if (family == AF_INET6) {
157 if (wherelen != sizeof(struct in6_addr))
159 memset(&ap->s_in6, 0, sizeof(ap->s_in6));
160 ap->s_in6.sin6_family = family;
161 ap->s_in6.sin6_port = port;
162 ap->s_in6.sin6_addr = *(struct in6_addr *)where;
170 int BIO_ADDR_family(const BIO_ADDR *ap)
172 return ap->sa.sa_family;
175 int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
178 const void *addrptr = NULL;
180 if (ap->sa.sa_family == AF_INET) {
181 len = sizeof(ap->s_in.sin_addr);
182 addrptr = &ap->s_in.sin_addr;
185 else if (ap->sa.sa_family == AF_INET6) {
186 len = sizeof(ap->s_in6.sin6_addr);
187 addrptr = &ap->s_in6.sin6_addr;
191 else if (ap->sa.sa_family == AF_UNIX) {
192 len = strlen(ap->s_un.sun_path);
193 addrptr = &ap->s_un.sun_path;
201 memcpy(p, addrptr, len);
209 unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
211 if (ap->sa.sa_family == AF_INET)
212 return ap->s_in.sin_port;
214 if (ap->sa.sa_family == AF_INET6)
215 return ap->s_in6.sin6_port;
221 * addr_strings - helper function to get host and service names
222 * @ap: the BIO_ADDR that has the input info
223 * @numeric: 0 if actual names should be returned, 1 if the numeric
224 * representation should be returned.
225 * @hostname: a pointer to a pointer to a memory area to store the
226 * host name or numeric representation. Unused if NULL.
227 * @service: a pointer to a pointer to a memory area to store the
228 * service name or numeric representation. Unused if NULL.
230 * The return value is 0 on failure, with the error code in the error
231 * stack, and 1 on success.
233 static int addr_strings(const BIO_ADDR *ap, int numeric,
234 char **hostname, char **service)
236 if (BIO_sock_init() != 1)
242 char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
246 flags |= NI_NUMERICHOST | NI_NUMERICSERV;
248 if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap),
249 BIO_ADDR_sockaddr_size(ap),
250 host, sizeof(host), serv, sizeof(serv),
253 if (ret == EAI_SYSTEM) {
254 SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
255 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
259 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
260 ERR_add_error_data(1, gai_strerror(ret));
265 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
266 * leaves it with whatever garbage that happens to be there.
267 * However, we initialise serv with the empty string (serv[0]
268 * is therefore NUL), so it gets real easy to detect when things
269 * didn't go the way one might expect.
271 if (serv[0] == '\0') {
272 BIO_snprintf(serv, sizeof(serv), "%d",
273 ntohs(BIO_ADDR_rawport(ap)));
277 *hostname = OPENSSL_strdup(host);
279 *service = OPENSSL_strdup(serv);
283 *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
285 char serv[6]; /* port is 16 bits => max 5 decimal digits */
286 BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
287 *service = OPENSSL_strdup(serv);
294 char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
296 char *hostname = NULL;
298 if (addr_strings(ap, numeric, &hostname, NULL))
304 char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
306 char *service = NULL;
308 if (addr_strings(ap, numeric, NULL, &service))
314 char *BIO_ADDR_path_string(const BIO_ADDR *ap)
317 if (ap->sa.sa_family == AF_UNIX)
318 return OPENSSL_strdup(ap->s_un.sun_path);
324 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
325 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
326 * The returned struct sockaddr is const, so it can't be tampered with.
328 const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
334 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
335 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
336 * it allows you to tamper with the data (and thereby the contents
337 * of the input BIO_ADDR).
339 struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
345 * BIO_ADDR_sockaddr_size - non-public function that returns the size
346 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
347 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
348 * the size of the BIO_ADDR type is returned.
350 socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
352 if (ap->sa.sa_family == AF_INET)
353 return sizeof(ap->s_in);
355 if (ap->sa.sa_family == AF_INET6)
356 return sizeof(ap->s_in6);
359 if (ap->sa.sa_family == AF_UNIX)
360 return sizeof(ap->s_un);
365 /**********************************************************************
367 * Address info database
371 const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
374 return bai->bai_next;
378 int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
381 return bai->bai_family;
385 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
388 return bai->bai_socktype;
392 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
395 if (bai->bai_protocol != 0)
396 return bai->bai_protocol;
399 if (bai->bai_family == AF_UNIX)
403 switch (bai->bai_socktype) {
416 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
417 * of the struct sockaddr inside the BIO_ADDRINFO.
419 socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
422 return bai->bai_addrlen;
427 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
428 * as the struct sockaddr it is.
430 const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
433 return bai->bai_addr;
437 const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
440 return (BIO_ADDR *)bai->bai_addr;
444 void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
451 # define _cond bai->bai_family != AF_UNIX
461 /* Free manually when we know that addrinfo_wrap() was used.
462 * See further comment above addrinfo_wrap()
464 while (bai != NULL) {
465 BIO_ADDRINFO *next = bai->bai_next;
466 OPENSSL_free(bai->bai_addr);
472 /**********************************************************************
479 * The specs in hostserv can take these forms:
481 * host:service => *host = "host", *service = "service"
482 * host:* => *host = "host", *service = NULL
483 * host: => *host = "host", *service = NULL
484 * :service => *host = NULL, *service = "service"
485 * *:service => *host = NULL, *service = "service"
487 * in case no : is present in the string, the result depends on
488 * hostserv_prio, as follows:
490 * when hostserv_prio == BIO_PARSE_PRIO_HOST
491 * host => *host = "host", *service untouched
493 * when hostserv_prio == BIO_PARSE_PRIO_SERV
494 * service => *host untouched, *service = "service"
497 int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
498 enum BIO_hostserv_priorities hostserv_prio)
500 const char *h = NULL; size_t hl = 0;
501 const char *p = NULL; size_t pl = 0;
503 if (*hostserv == '[') {
504 if ((p = strchr(hostserv, ']')) == NULL)
518 const char *p2 = strrchr(hostserv, ':');
519 p = strchr(hostserv, ':');
522 * Check for more than one colon. There are three possible
524 * 1. IPv6 address with port number, last colon being separator.
525 * 2. IPv6 address only.
526 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
527 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
528 * Because of this ambiguity, we currently choose to make it an
539 } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
548 if (p != NULL && strchr(p, ':'))
551 if (h != NULL && host != NULL) {
553 || (hl == 1 && h[0] == '*')) {
556 *host = OPENSSL_strndup(h, hl);
561 if (p != NULL && service != NULL) {
563 || (pl == 1 && p[0] == '*')) {
566 *service = OPENSSL_strndup(p, pl);
567 if (*service == NULL)
574 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
577 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
580 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
584 /* addrinfo_wrap is used to build our own addrinfo "chain".
585 * (it has only one entry, so calling it a chain may be a stretch)
586 * It should ONLY be called when getaddrinfo() and friends
587 * aren't available, OR when dealing with a non IP protocol
588 * family, such as AF_UNIX
590 * the return value is 1 on success, or 0 on failure, which
591 * only happens if a memory allocation error occurred.
593 static int addrinfo_wrap(int family, int socktype,
594 const void *where, size_t wherelen,
598 OPENSSL_assert(bai != NULL);
600 *bai = OPENSSL_zalloc(sizeof(**bai));
604 (*bai)->bai_family = family;
605 (*bai)->bai_socktype = socktype;
606 if (socktype == SOCK_STREAM)
607 (*bai)->bai_protocol = IPPROTO_TCP;
608 if (socktype == SOCK_DGRAM)
609 (*bai)->bai_protocol = IPPROTO_UDP;
611 if (family == AF_UNIX)
612 (*bai)->bai_protocol = 0;
615 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
616 just an advanced cast of BIO_ADDR* to struct sockaddr *
617 by the power of union, so while it may seem that we're
618 creating a memory leak here, we are not. It will be
620 BIO_ADDR *addr = BIO_ADDR_new();
622 BIO_ADDR_rawmake(addr, family, where, wherelen, port);
623 (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
626 (*bai)->bai_next = NULL;
627 if ((*bai)->bai_addr == NULL) {
628 BIO_ADDRINFO_free(*bai);
635 static void do_bio_lookup_init(void)
637 bio_lookup_lock = CRYPTO_THREAD_lock_new();
641 * BIO_lookup - look up the node and service you want to connect to.
642 * @node: the node you want to connect to.
643 * @service: the service you want to connect to.
644 * @lookup_type: declare intent with the result, client or server.
645 * @family: the address family you want to use. Use AF_UNSPEC for any, or
646 * AF_INET, AF_INET6 or AF_UNIX.
647 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
649 * @res: Storage place for the resulting list of returned addresses
651 * This will do a lookup of the node and service that you want to connect to.
652 * It returns a linked list of different addresses you can try to connect to.
654 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
656 * The return value is 1 on success or 0 in case of error.
658 int BIO_lookup(const char *host, const char *service,
659 enum BIO_lookup_type lookup_type,
660 int family, int socktype, BIO_ADDRINFO **res)
662 int ret = 0; /* Assume failure */
677 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
682 if (family == AF_UNIX) {
683 if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
686 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
691 if (BIO_sock_init() != 1)
697 struct addrinfo hints;
698 memset(&hints, 0, sizeof hints);
700 # ifdef AI_ADDRCONFIG
701 hints.ai_flags = AI_ADDRCONFIG;
703 hints.ai_family = family;
704 hints.ai_socktype = socktype;
706 if (lookup_type == BIO_LOOKUP_SERVER)
707 hints.ai_flags |= AI_PASSIVE;
709 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
710 * macro magic in bio_lcl.h
712 switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
715 SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
716 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
720 ret = 1; /* Success */
723 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
724 ERR_add_error_data(1, gai_strerror(gai_ret));
729 const struct hostent *he;
731 * Because struct hostent is defined for 32-bit pointers only with
732 * VMS C, we need to make sure that '&he_fallback_address' and
733 * '&he_fallback_addresses' are 32-bit pointers
735 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
736 # pragma pointer_size save
737 # pragma pointer_size 32
739 /* Windows doesn't seem to have in_addr_t */
740 #ifdef OPENSSL_SYS_WINDOWS
741 static uint32_t he_fallback_address;
742 static const uint32_t *he_fallback_addresses[] =
743 { &he_fallback_address, NULL };
745 static in_addr_t he_fallback_address;
746 static const in_addr_t *he_fallback_addresses[] =
747 { &he_fallback_address, NULL };
749 static const struct hostent he_fallback =
750 { NULL, NULL, AF_INET, sizeof(he_fallback_address),
751 (char **)&he_fallback_addresses };
752 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
753 # pragma pointer_size restore
757 /* Apparently, on WIN64, s_proto and s_port have traded places... */
759 struct servent se_fallback = { NULL, NULL, NULL, 0 };
761 struct servent se_fallback = { NULL, NULL, 0, NULL };
764 CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
766 CRYPTO_THREAD_write_lock(bio_lookup_lock);
767 he_fallback_address = INADDR_ANY;
770 switch(lookup_type) {
771 case BIO_LOOKUP_CLIENT:
772 he_fallback_address = INADDR_LOOPBACK;
774 case BIO_LOOKUP_SERVER:
775 he_fallback_address = INADDR_ANY;
778 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
782 he = gethostbyname(host);
785 #ifndef OPENSSL_SYS_WINDOWS
786 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
787 ERR_add_error_data(1, hstrerror(h_errno));
789 SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
796 if (service == NULL) {
797 se_fallback.s_port = 0;
798 se_fallback.s_proto = NULL;
802 long portnum = strtol(service, &endp, 10);
805 * Because struct servent is defined for 32-bit pointers only with
806 * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
808 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
809 # pragma pointer_size save
810 # pragma pointer_size 32
813 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
814 # pragma pointer_size restore
826 if (endp != service && *endp == '\0'
827 && portnum > 0 && portnum < 65536) {
828 se_fallback.s_port = htons(portnum);
829 se_fallback.s_proto = proto;
831 } else if (endp == service) {
832 se = getservbyname(service, proto);
835 #ifndef OPENSSL_SYS_WINDOWS
836 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
837 ERR_add_error_data(1, hstrerror(h_errno));
839 SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
844 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
853 * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
854 * we must make sure our iterator designates the same element type, hence
855 * the pointer size dance.
857 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
858 # pragma pointer_size save
859 # pragma pointer_size 32
862 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
863 # pragma pointer_size restore
866 BIO_ADDRINFO *tmp_bai = NULL;
868 /* The easiest way to create a linked list from an
869 array is to start from the back */
870 for(addrlistp = he->h_addr_list; *addrlistp != NULL;
874 for(addresses = addrlistp - he->h_addr_list;
875 addrlistp--, addresses-- > 0; ) {
876 if (!addrinfo_wrap(he->h_addrtype, socktype,
877 *addrlistp, he->h_length,
878 se->s_port, &tmp_bai))
879 goto addrinfo_malloc_err;
880 tmp_bai->bai_next = *res;
884 BIO_ADDRINFO_free(*res);
886 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
894 CRYPTO_THREAD_unlock(bio_lookup_lock);
900 #endif /* OPENSSL_NO_SOCK */