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).
59 #include <openssl/err.h>
60 #include <openssl/buffer.h>
63 * Throughout this file and bio_lcl.h, the existence of the macro
64 * AI_PASSIVE is used to detect the availability of struct addrinfo,
65 * getnameinfo() and getaddrinfo(). If that macro doesn't exist,
66 * we use our own implementation instead, using gethostbyname,
67 * getservbyname and a few other.
70 /**********************************************************************
76 BIO_ADDR *BIO_ADDR_new(void)
78 BIO_ADDR *ret = (BIO_ADDR *)OPENSSL_zalloc(sizeof(BIO_ADDR));
82 void BIO_ADDR_free(BIO_ADDR *ap)
88 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
89 * of a struct sockaddr.
91 int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa)
93 if (sa->sa_family == AF_INET) {
94 ap->sin = *(const struct sockaddr_in *)sa;
98 if (sa->sa_family == AF_INET6) {
99 ap->sin6 = *(const struct sockaddr_in6 *)sa;
104 if (ap->sa.sa_family == AF_UNIX) {
105 ap->sun = *(const struct sockaddr_un *)sa;
113 int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
114 const void *where, size_t wherelen,
118 if (family == AF_UNIX) {
119 if (wherelen + 1 > sizeof(ap->sun.sun_path))
121 memset(&ap->sun, 0, sizeof(ap->sun));
122 ap->sun.sun_family = family;
123 strncpy(ap->sun.sun_path, where, sizeof(ap->sun.sun_path) - 1);
127 if (family == AF_INET) {
128 if (wherelen != sizeof(struct in_addr))
130 memset(&ap->sin, 0, sizeof(ap->sin));
131 ap->sin.sin_family = family;
132 ap->sin.sin_port = port;
133 ap->sin.sin_addr = *(struct in_addr *)where;
137 if (family == AF_INET6) {
138 if (wherelen != sizeof(struct in6_addr))
140 memset(&ap->sin6, 0, sizeof(ap->sin6));
141 ap->sin6.sin6_family = family;
142 ap->sin6.sin6_port = port;
143 ap->sin6.sin6_addr = *(struct in6_addr *)where;
151 int BIO_ADDR_family(const BIO_ADDR *ap)
153 return ap->sa.sa_family;
156 int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
159 const void *addrptr = NULL;
161 if (ap->sa.sa_family == AF_INET) {
162 len = sizeof(ap->sin.sin_addr);
163 addrptr = &ap->sin.sin_addr;
166 else if (ap->sa.sa_family == AF_INET6) {
167 len = sizeof(ap->sin6.sin6_addr);
168 addrptr = &ap->sin6.sin6_addr;
172 else if (ap->sa.sa_family == AF_UNIX) {
173 len = strlen(ap->sun.sun_path);
174 addrptr = &ap->sun.sun_path;
182 memcpy(p, addrptr, len);
190 unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
192 if (ap->sa.sa_family == AF_INET)
193 return ap->sin.sin_port;
195 if (ap->sa.sa_family == AF_INET6)
196 return ap->sin6.sin6_port;
202 * addr_strings - helper function to get host and service names
203 * @ap: the BIO_ADDR that has the input info
204 * @numeric: 0 if actual names should be returned, 1 if the numeric
205 * representation should be returned.
206 * @hostname: a pointer to a pointer to a memory area to store the
207 * host name or numeric representation. Unused if NULL.
208 * @service: a pointer to a pointer to a memory area to store the
209 * service name or numeric representation. Unused if NULL.
211 * The return value is 0 on failure, with the error code in the error
212 * stack, and 1 on success.
214 static int addr_strings(const BIO_ADDR *ap, int numeric,
215 char **hostname, char **service)
220 char host[NI_MAXHOST], serv[NI_MAXSERV];
224 flags |= NI_NUMERICHOST | NI_NUMERICSERV;
226 if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap),
227 BIO_ADDR_sockaddr_size(ap),
228 host, sizeof(host), serv, sizeof(serv),
231 if (ret == EAI_SYSTEM) {
232 SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
233 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
237 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
238 ERR_add_error_data(1, gai_strerror(ret));
243 *hostname = OPENSSL_strdup(host);
245 *service = OPENSSL_strdup(serv);
249 *hostname = OPENSSL_strdup(inet_ntoa(ap->sin.sin_addr));
251 char serv[6]; /* port is 16 bits => max 5 decimal digits */
252 BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->sin.sin_port));
253 *service = OPENSSL_strdup(serv);
260 char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
262 char *hostname = NULL;
264 if (addr_strings(ap, numeric, &hostname, NULL))
270 char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
272 char *service = NULL;
274 if (addr_strings(ap, numeric, NULL, &service))
280 char *BIO_ADDR_path_string(const BIO_ADDR *ap)
283 if (ap->sa.sa_family == AF_UNIX)
284 return OPENSSL_strdup(ap->sun.sun_path);
290 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
291 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
292 * The returned struct sockaddr is const, so it can't be tampered with.
294 const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
300 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
301 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
302 * it allows you to tamper with the data (and thereby the contents
303 * of the input BIO_ADDR).
305 struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
311 * BIO_ADDR_sockaddr_size - non-public function that returns the size
312 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
313 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
314 * the size of the BIO_ADDR type is returned.
316 socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
318 if (ap->sa.sa_family == AF_INET)
319 return sizeof(ap->sin);
321 if (ap->sa.sa_family == AF_INET6)
322 return sizeof(ap->sin6);
325 if (ap->sa.sa_family == AF_UNIX)
326 return sizeof(ap->sun);
331 /**********************************************************************
333 * Address into database
337 const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
340 return bai->bai_next;
344 int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
347 return bai->bai_family;
351 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
354 return bai->bai_socktype;
358 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
361 return bai->bai_protocol;
366 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
367 * of the struct sockaddr inside the BIO_ADDRINFO.
369 socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
372 return bai->bai_addrlen;
377 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
378 * as the struct sockaddr it is.
380 const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
383 return bai->bai_addr;
387 const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
390 return (BIO_ADDR *)bai->bai_addr;
394 void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
401 # define _cond bai->bai_family != AF_UNIX
411 /* Free manually when we know that addrinfo_wrap() was used.
412 * See further comment above addrinfo_wrap()
414 while (bai != NULL) {
415 BIO_ADDRINFO *next = bai->bai_next;
416 OPENSSL_free(bai->bai_addr);
422 /**********************************************************************
429 * The specs in hostserv can take these forms:
431 * host:service => *host = "host", *service = "service"
432 * host:* => *host = "host", *service = NULL
433 * host: => *host = "host", *service = NULL
434 * :service => *host = NULL, *service = "service"
435 * *:service => *host = NULL, *service = "service"
437 * in case no : is present in the string, the result depends on
438 * hostserv_prio, as follows:
440 * when hostserv_prio == BIO_PARSE_PRIO_HOST
441 * host => *host = "host", *service untouched
443 * when hostserv_prio == BIO_PARSE_PRIO_SERV
444 * service => *host untouched, *service = "service"
447 int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
448 enum BIO_hostserv_priorities hostserv_prio)
450 const char *h = NULL; size_t hl = 0;
451 const char *p = NULL; size_t pl = 0;
453 if (*hostserv == '[') {
454 if ((p = strchr(hostserv, ']')) == NULL)
468 const char *p2 = strrchr(hostserv, ':');
469 p = strchr(hostserv, ':');
472 * Check for more than one colon. There are three possible
474 * 1. IPv6 address with port number, last colon being separator.
475 * 2. IPv6 address only.
476 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
477 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
478 * Because of this ambiguity, we currently choose to make it an
489 } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
501 if (h != NULL && host != NULL) {
503 || (hl == 1 && h[0] == '*')) {
506 *host = OPENSSL_strndup(h, hl);
511 if (p != NULL && service != NULL) {
513 || (pl == 1 && p[0] == '*')) {
516 *service = OPENSSL_strndup(p, pl);
517 if (*service == NULL)
524 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
527 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
530 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
534 /* addrinfo_wrap is used to build our own addrinfo "chain".
535 * (it has only one entry, so calling it a chain may be a stretch)
536 * It should ONLY be called when getaddrinfo() and friends
537 * aren't available, OR when dealing with a non IP protocol
538 * family, such as AF_UNIX
540 * the return value is 1 on success, or 0 on failure, which
541 * only happens if a memory allocation error occured.
543 static int addrinfo_wrap(int family, int socktype,
544 const void *where, size_t wherelen,
548 OPENSSL_assert(bai != NULL);
550 *bai = (BIO_ADDRINFO *)OPENSSL_zalloc(sizeof(**bai));
554 (*bai)->bai_family = family;
555 (*bai)->bai_socktype = socktype;
556 if (socktype == SOCK_STREAM)
557 (*bai)->bai_protocol = IPPROTO_TCP;
558 if (socktype == SOCK_DGRAM)
559 (*bai)->bai_protocol = IPPROTO_UDP;
561 if (family == AF_UNIX)
562 (*bai)->bai_protocol = 0;
565 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
566 just an advanced cast of BIO_ADDR* to struct sockaddr *
567 by the power of union, so while it may seem that we're
568 creating a memory leak here, we are not. It will be
570 BIO_ADDR *addr = BIO_ADDR_new();
572 BIO_ADDR_rawmake(addr, family, where, wherelen, port);
573 (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
576 (*bai)->bai_next = NULL;
577 if ((*bai)->bai_addr == NULL) {
578 BIO_ADDRINFO_free(*bai);
586 * BIO_lookup - look up the node and service you want to connect to.
587 * @node: the node you want to connect to.
588 * @service: the service you want to connect to.
589 * @lookup_type: declare intent with the result, client or server.
590 * @family: the address family you want to use. Use AF_UNSPEC for any, or
591 * AF_INET, AF_INET6 or AF_UNIX.
592 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
594 * @res: Storage place for the resulting list of returned addresses
596 * This will do a lookup of the node and service that you want to connect to.
597 * It returns a linked list of different addresses you can try to connect to.
599 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
601 * The return value is 1 on success or 0 in case of error.
603 int BIO_lookup(const char *host, const char *service,
604 enum BIO_lookup_type lookup_type,
605 int family, int socktype, BIO_ADDRINFO **res)
607 int ret = 0; /* Assume failure */
622 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
627 if (family == AF_UNIX) {
628 if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
631 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
638 struct addrinfo hints;
641 # ifdef AI_ADDRCONFIG
642 hints.ai_flags = AI_ADDRCONFIG;
644 hints.ai_family = family;
645 hints.ai_socktype = socktype;
646 hints.ai_protocol = 0;
647 hints.ai_addrlen = 0;
648 hints.ai_addr = NULL;
649 hints.ai_canonname = NULL;
650 hints.ai_next = NULL;
652 if (lookup_type == BIO_LOOKUP_SERVER)
653 hints.ai_flags |= AI_PASSIVE;
655 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
656 * macro magic in bio_lcl.h
658 switch (getaddrinfo(host, service, &hints, res)) {
661 SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
662 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
666 ret = 1; /* Success */
669 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
670 ERR_add_error_data(1, gai_strerror(ret));
675 const struct hostent *he;
676 /* Windows doesn't seem to have in_addr_t */
677 #ifdef OPENSSL_SYS_WINDOWS
678 static uint32_t he_fallback_address;
679 static const uint32_t *he_fallback_addresses[] =
680 { &he_fallback_address, NULL };
682 static in_addr_t he_fallback_address;
683 static const in_addr_t *he_fallback_addresses[] =
684 { &he_fallback_address, NULL };
686 static const struct hostent he_fallback =
687 { NULL, NULL, AF_INET, sizeof(he_fallback_address),
688 (char **)&he_fallback_addresses };
690 /* Apprently, on WIN64, s_proto and s_port have traded places... */
692 struct servent se_fallback = { NULL, NULL, NULL, 0 };
694 struct servent se_fallback = { NULL, NULL, 0, NULL };
698 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
699 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
700 he_fallback_address = INADDR_ANY;
703 switch(lookup_type) {
704 case BIO_LOOKUP_CLIENT:
705 he_fallback_address = INADDR_LOOPBACK;
707 case BIO_LOOKUP_SERVER:
708 he_fallback_address = INADDR_ANY;
711 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
715 he = gethostbyname(host);
718 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
719 ERR_add_error_data(1, hstrerror(h_errno));
725 if (service == NULL) {
726 se_fallback.s_port = 0;
727 se_fallback.s_proto = proto;
731 long portnum = strtol(service, &endp, 10);
733 if (endp != service && *endp == '\0'
734 && portnum > 0 && portnum < 65536) {
735 se_fallback.s_port = htons(portnum);
736 se_fallback.s_proto = proto;
738 } else if (endp == service) {
747 se = getservbyname(service, proto);
750 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
751 ERR_add_error_data(1, hstrerror(h_errno));
755 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
765 BIO_ADDRINFO *tmp_bai = NULL;
767 /* The easiest way to create a linked list from an
768 array is to start from the back */
769 for(addrlistp = he->h_addr_list; *addrlistp != NULL;
773 for(addresses = addrlistp - he->h_addr_list;
774 addrlistp--, addresses-- > 0; ) {
775 if (!addrinfo_wrap(he->h_addrtype, socktype,
776 *addrlistp, he->h_length,
777 se->s_port, &tmp_bai))
778 goto addrinfo_malloc_err;
779 tmp_bai->bai_next = *res;
783 BIO_ADDRINFO_free(*res);
785 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
793 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
794 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);