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));
86 ret->sa.sa_family = AF_UNSPEC;
90 void BIO_ADDR_free(BIO_ADDR *ap)
95 void BIO_ADDR_clear(BIO_ADDR *ap)
97 memset(ap, 0, sizeof(*ap));
98 ap->sa.sa_family = AF_UNSPEC;
102 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
103 * of a struct sockaddr.
105 int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa)
107 if (sa->sa_family == AF_INET) {
108 ap->s_in = *(const struct sockaddr_in *)sa;
112 if (sa->sa_family == AF_INET6) {
113 ap->s_in6 = *(const struct sockaddr_in6 *)sa;
118 if (ap->sa.sa_family == AF_UNIX) {
119 ap->s_un = *(const struct sockaddr_un *)sa;
127 int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
128 const void *where, size_t wherelen,
132 if (family == AF_UNIX) {
133 if (wherelen + 1 > sizeof(ap->s_un.sun_path))
135 memset(&ap->s_un, 0, sizeof(ap->s_un));
136 ap->s_un.sun_family = family;
137 strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1);
141 if (family == AF_INET) {
142 if (wherelen != sizeof(struct in_addr))
144 memset(&ap->s_in, 0, sizeof(ap->s_in));
145 ap->s_in.sin_family = family;
146 ap->s_in.sin_port = port;
147 ap->s_in.sin_addr = *(struct in_addr *)where;
151 if (family == AF_INET6) {
152 if (wherelen != sizeof(struct in6_addr))
154 memset(&ap->s_in6, 0, sizeof(ap->s_in6));
155 ap->s_in6.sin6_family = family;
156 ap->s_in6.sin6_port = port;
157 ap->s_in6.sin6_addr = *(struct in6_addr *)where;
165 int BIO_ADDR_family(const BIO_ADDR *ap)
167 return ap->sa.sa_family;
170 int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
173 const void *addrptr = NULL;
175 if (ap->sa.sa_family == AF_INET) {
176 len = sizeof(ap->s_in.sin_addr);
177 addrptr = &ap->s_in.sin_addr;
180 else if (ap->sa.sa_family == AF_INET6) {
181 len = sizeof(ap->s_in6.sin6_addr);
182 addrptr = &ap->s_in6.sin6_addr;
186 else if (ap->sa.sa_family == AF_UNIX) {
187 len = strlen(ap->s_un.sun_path);
188 addrptr = &ap->s_un.sun_path;
196 memcpy(p, addrptr, len);
204 unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
206 if (ap->sa.sa_family == AF_INET)
207 return ap->s_in.sin_port;
209 if (ap->sa.sa_family == AF_INET6)
210 return ap->s_in6.sin6_port;
216 * addr_strings - helper function to get host and service names
217 * @ap: the BIO_ADDR that has the input info
218 * @numeric: 0 if actual names should be returned, 1 if the numeric
219 * representation should be returned.
220 * @hostname: a pointer to a pointer to a memory area to store the
221 * host name or numeric representation. Unused if NULL.
222 * @service: a pointer to a pointer to a memory area to store the
223 * service name or numeric representation. Unused if NULL.
225 * The return value is 0 on failure, with the error code in the error
226 * stack, and 1 on success.
228 static int addr_strings(const BIO_ADDR *ap, int numeric,
229 char **hostname, char **service)
231 if (BIO_sock_init() != 1)
237 char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
241 flags |= NI_NUMERICHOST | NI_NUMERICSERV;
243 if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap),
244 BIO_ADDR_sockaddr_size(ap),
245 host, sizeof(host), serv, sizeof(serv),
248 if (ret == EAI_SYSTEM) {
249 SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
250 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
254 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
255 ERR_add_error_data(1, gai_strerror(ret));
260 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
261 * leaves it with whatever garbage that happens to be there.
262 * However, we initialise serv with the empty string (serv[0]
263 * is therefore NUL), so it gets real easy to detect when things
264 * didn't go the way one might expect.
266 if (serv[0] == '\0') {
267 BIO_snprintf(serv, sizeof(serv), "%d",
268 ntohs(BIO_ADDR_rawport(ap)));
272 *hostname = OPENSSL_strdup(host);
274 *service = OPENSSL_strdup(serv);
278 *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
280 char serv[6]; /* port is 16 bits => max 5 decimal digits */
281 BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
282 *service = OPENSSL_strdup(serv);
289 char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
291 char *hostname = NULL;
293 if (addr_strings(ap, numeric, &hostname, NULL))
299 char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
301 char *service = NULL;
303 if (addr_strings(ap, numeric, NULL, &service))
309 char *BIO_ADDR_path_string(const BIO_ADDR *ap)
312 if (ap->sa.sa_family == AF_UNIX)
313 return OPENSSL_strdup(ap->s_un.sun_path);
319 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
320 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
321 * The returned struct sockaddr is const, so it can't be tampered with.
323 const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
329 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
330 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
331 * it allows you to tamper with the data (and thereby the contents
332 * of the input BIO_ADDR).
334 struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
340 * BIO_ADDR_sockaddr_size - non-public function that returns the size
341 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
342 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
343 * the size of the BIO_ADDR type is returned.
345 socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
347 if (ap->sa.sa_family == AF_INET)
348 return sizeof(ap->s_in);
350 if (ap->sa.sa_family == AF_INET6)
351 return sizeof(ap->s_in6);
354 if (ap->sa.sa_family == AF_UNIX)
355 return sizeof(ap->s_un);
360 /**********************************************************************
362 * Address info database
366 const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
369 return bai->bai_next;
373 int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
376 return bai->bai_family;
380 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
383 return bai->bai_socktype;
387 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
390 if (bai->bai_protocol != 0)
391 return bai->bai_protocol;
394 if (bai->bai_family == AF_UNIX)
398 switch (bai->bai_socktype) {
411 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
412 * of the struct sockaddr inside the BIO_ADDRINFO.
414 socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
417 return bai->bai_addrlen;
422 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
423 * as the struct sockaddr it is.
425 const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
428 return bai->bai_addr;
432 const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
435 return (BIO_ADDR *)bai->bai_addr;
439 void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
446 # define _cond bai->bai_family != AF_UNIX
456 /* Free manually when we know that addrinfo_wrap() was used.
457 * See further comment above addrinfo_wrap()
459 while (bai != NULL) {
460 BIO_ADDRINFO *next = bai->bai_next;
461 OPENSSL_free(bai->bai_addr);
467 /**********************************************************************
474 * The specs in hostserv can take these forms:
476 * host:service => *host = "host", *service = "service"
477 * host:* => *host = "host", *service = NULL
478 * host: => *host = "host", *service = NULL
479 * :service => *host = NULL, *service = "service"
480 * *:service => *host = NULL, *service = "service"
482 * in case no : is present in the string, the result depends on
483 * hostserv_prio, as follows:
485 * when hostserv_prio == BIO_PARSE_PRIO_HOST
486 * host => *host = "host", *service untouched
488 * when hostserv_prio == BIO_PARSE_PRIO_SERV
489 * service => *host untouched, *service = "service"
492 int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
493 enum BIO_hostserv_priorities hostserv_prio)
495 const char *h = NULL; size_t hl = 0;
496 const char *p = NULL; size_t pl = 0;
498 if (*hostserv == '[') {
499 if ((p = strchr(hostserv, ']')) == NULL)
513 const char *p2 = strrchr(hostserv, ':');
514 p = strchr(hostserv, ':');
517 * Check for more than one colon. There are three possible
519 * 1. IPv6 address with port number, last colon being separator.
520 * 2. IPv6 address only.
521 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
522 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
523 * Because of this ambiguity, we currently choose to make it an
534 } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
543 if (p != NULL && strchr(p, ':'))
546 if (h != NULL && host != NULL) {
548 || (hl == 1 && h[0] == '*')) {
551 *host = OPENSSL_strndup(h, hl);
556 if (p != NULL && service != NULL) {
558 || (pl == 1 && p[0] == '*')) {
561 *service = OPENSSL_strndup(p, pl);
562 if (*service == NULL)
569 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
572 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
575 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
579 /* addrinfo_wrap is used to build our own addrinfo "chain".
580 * (it has only one entry, so calling it a chain may be a stretch)
581 * It should ONLY be called when getaddrinfo() and friends
582 * aren't available, OR when dealing with a non IP protocol
583 * family, such as AF_UNIX
585 * the return value is 1 on success, or 0 on failure, which
586 * only happens if a memory allocation error occurred.
588 static int addrinfo_wrap(int family, int socktype,
589 const void *where, size_t wherelen,
593 OPENSSL_assert(bai != NULL);
595 *bai = OPENSSL_zalloc(sizeof(**bai));
599 (*bai)->bai_family = family;
600 (*bai)->bai_socktype = socktype;
601 if (socktype == SOCK_STREAM)
602 (*bai)->bai_protocol = IPPROTO_TCP;
603 if (socktype == SOCK_DGRAM)
604 (*bai)->bai_protocol = IPPROTO_UDP;
606 if (family == AF_UNIX)
607 (*bai)->bai_protocol = 0;
610 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
611 just an advanced cast of BIO_ADDR* to struct sockaddr *
612 by the power of union, so while it may seem that we're
613 creating a memory leak here, we are not. It will be
615 BIO_ADDR *addr = BIO_ADDR_new();
617 BIO_ADDR_rawmake(addr, family, where, wherelen, port);
618 (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
621 (*bai)->bai_next = NULL;
622 if ((*bai)->bai_addr == NULL) {
623 BIO_ADDRINFO_free(*bai);
630 static void do_bio_lookup_init(void)
632 bio_lookup_lock = CRYPTO_THREAD_lock_new();
636 * BIO_lookup - look up the node and service you want to connect to.
637 * @node: the node you want to connect to.
638 * @service: the service you want to connect to.
639 * @lookup_type: declare intent with the result, client or server.
640 * @family: the address family you want to use. Use AF_UNSPEC for any, or
641 * AF_INET, AF_INET6 or AF_UNIX.
642 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
644 * @res: Storage place for the resulting list of returned addresses
646 * This will do a lookup of the node and service that you want to connect to.
647 * It returns a linked list of different addresses you can try to connect to.
649 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
651 * The return value is 1 on success or 0 in case of error.
653 int BIO_lookup(const char *host, const char *service,
654 enum BIO_lookup_type lookup_type,
655 int family, int socktype, BIO_ADDRINFO **res)
657 int ret = 0; /* Assume failure */
672 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
677 if (family == AF_UNIX) {
678 if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
681 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
686 if (BIO_sock_init() != 1)
692 struct addrinfo hints;
693 memset(&hints, 0, sizeof hints);
695 # ifdef AI_ADDRCONFIG
696 hints.ai_flags = AI_ADDRCONFIG;
698 hints.ai_family = family;
699 hints.ai_socktype = socktype;
701 if (lookup_type == BIO_LOOKUP_SERVER)
702 hints.ai_flags |= AI_PASSIVE;
704 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
705 * macro magic in bio_lcl.h
707 switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
710 SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
711 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
715 ret = 1; /* Success */
718 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
719 ERR_add_error_data(1, gai_strerror(gai_ret));
724 const struct hostent *he;
726 * Because struct hostent is defined for 32-bit pointers only with
727 * VMS C, we need to make sure that '&he_fallback_address' and
728 * '&he_fallback_addresses' are 32-bit pointers
730 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
731 # pragma pointer_size save
732 # pragma pointer_size 32
734 /* Windows doesn't seem to have in_addr_t */
735 #ifdef OPENSSL_SYS_WINDOWS
736 static uint32_t he_fallback_address;
737 static const uint32_t *he_fallback_addresses[] =
738 { &he_fallback_address, NULL };
740 static in_addr_t he_fallback_address;
741 static const in_addr_t *he_fallback_addresses[] =
742 { &he_fallback_address, NULL };
744 static const struct hostent he_fallback =
745 { NULL, NULL, AF_INET, sizeof(he_fallback_address),
746 (char **)&he_fallback_addresses };
747 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
748 # pragma pointer_size restore
752 /* Apparently, on WIN64, s_proto and s_port have traded places... */
754 struct servent se_fallback = { NULL, NULL, NULL, 0 };
756 struct servent se_fallback = { NULL, NULL, 0, NULL };
759 CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
761 CRYPTO_THREAD_write_lock(bio_lookup_lock);
762 he_fallback_address = INADDR_ANY;
765 switch(lookup_type) {
766 case BIO_LOOKUP_CLIENT:
767 he_fallback_address = INADDR_LOOPBACK;
769 case BIO_LOOKUP_SERVER:
770 he_fallback_address = INADDR_ANY;
773 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
777 he = gethostbyname(host);
780 #ifndef OPENSSL_SYS_WINDOWS
781 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
782 ERR_add_error_data(1, hstrerror(h_errno));
784 SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
791 if (service == NULL) {
792 se_fallback.s_port = 0;
793 se_fallback.s_proto = NULL;
797 long portnum = strtol(service, &endp, 10);
800 * Because struct servent is defined for 32-bit pointers only with
801 * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
803 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
804 # pragma pointer_size save
805 # pragma pointer_size 32
808 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
809 # pragma pointer_size restore
821 if (endp != service && *endp == '\0'
822 && portnum > 0 && portnum < 65536) {
823 se_fallback.s_port = htons(portnum);
824 se_fallback.s_proto = proto;
826 } else if (endp == service) {
827 se = getservbyname(service, proto);
830 #ifndef OPENSSL_SYS_WINDOWS
831 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
832 ERR_add_error_data(1, hstrerror(h_errno));
834 SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
839 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
848 * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
849 * we must make sure our iterator designates the same element type, hence
850 * the pointer size dance.
852 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
853 # pragma pointer_size save
854 # pragma pointer_size 32
857 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
858 # pragma pointer_size restore
861 BIO_ADDRINFO *tmp_bai = NULL;
863 /* The easiest way to create a linked list from an
864 array is to start from the back */
865 for(addrlistp = he->h_addr_list; *addrlistp != NULL;
869 for(addresses = addrlistp - he->h_addr_list;
870 addrlistp--, addresses-- > 0; ) {
871 if (!addrinfo_wrap(he->h_addrtype, socktype,
872 *addrlistp, he->h_length,
873 se->s_port, &tmp_bai))
874 goto addrinfo_malloc_err;
875 tmp_bai->bai_next = *res;
879 BIO_ADDRINFO_free(*res);
881 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
889 CRYPTO_THREAD_unlock(bio_lookup_lock);
895 #endif /* OPENSSL_NO_SOCK */