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 #ifndef OPENSSL_NO_SOCK
60 #include <openssl/err.h>
61 #include <openssl/buffer.h>
65 * Throughout this file and bio_lcl.h, the existence of the macro
66 * AI_PASSIVE is used to detect the availability of struct addrinfo,
67 * getnameinfo() and getaddrinfo(). If that macro doesn't exist,
68 * we use our own implementation instead, using gethostbyname,
69 * getservbyname and a few other.
72 /**********************************************************************
78 BIO_ADDR *BIO_ADDR_new(void)
80 BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret));
82 ret->sa.sa_family = AF_UNSPEC;
86 void BIO_ADDR_free(BIO_ADDR *ap)
91 void BIO_ADDR_clear(BIO_ADDR *ap)
93 memset(ap, 0, sizeof(*ap));
94 ap->sa.sa_family = AF_UNSPEC;
98 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
99 * of a struct sockaddr.
101 int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa)
103 if (sa->sa_family == AF_INET) {
104 ap->s_in = *(const struct sockaddr_in *)sa;
108 if (sa->sa_family == AF_INET6) {
109 ap->s_in6 = *(const struct sockaddr_in6 *)sa;
114 if (ap->sa.sa_family == AF_UNIX) {
115 ap->s_un = *(const struct sockaddr_un *)sa;
123 int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
124 const void *where, size_t wherelen,
128 if (family == AF_UNIX) {
129 if (wherelen + 1 > sizeof(ap->s_un.sun_path))
131 memset(&ap->s_un, 0, sizeof(ap->s_un));
132 ap->s_un.sun_family = family;
133 strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1);
137 if (family == AF_INET) {
138 if (wherelen != sizeof(struct in_addr))
140 memset(&ap->s_in, 0, sizeof(ap->s_in));
141 ap->s_in.sin_family = family;
142 ap->s_in.sin_port = port;
143 ap->s_in.sin_addr = *(struct in_addr *)where;
147 if (family == AF_INET6) {
148 if (wherelen != sizeof(struct in6_addr))
150 memset(&ap->s_in6, 0, sizeof(ap->s_in6));
151 ap->s_in6.sin6_family = family;
152 ap->s_in6.sin6_port = port;
153 ap->s_in6.sin6_addr = *(struct in6_addr *)where;
161 int BIO_ADDR_family(const BIO_ADDR *ap)
163 return ap->sa.sa_family;
166 int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
169 const void *addrptr = NULL;
171 if (ap->sa.sa_family == AF_INET) {
172 len = sizeof(ap->s_in.sin_addr);
173 addrptr = &ap->s_in.sin_addr;
176 else if (ap->sa.sa_family == AF_INET6) {
177 len = sizeof(ap->s_in6.sin6_addr);
178 addrptr = &ap->s_in6.sin6_addr;
182 else if (ap->sa.sa_family == AF_UNIX) {
183 len = strlen(ap->s_un.sun_path);
184 addrptr = &ap->s_un.sun_path;
192 memcpy(p, addrptr, len);
200 unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
202 if (ap->sa.sa_family == AF_INET)
203 return ap->s_in.sin_port;
205 if (ap->sa.sa_family == AF_INET6)
206 return ap->s_in6.sin6_port;
212 * addr_strings - helper function to get host and service names
213 * @ap: the BIO_ADDR that has the input info
214 * @numeric: 0 if actual names should be returned, 1 if the numeric
215 * representation should be returned.
216 * @hostname: a pointer to a pointer to a memory area to store the
217 * host name or numeric representation. Unused if NULL.
218 * @service: a pointer to a pointer to a memory area to store the
219 * service name or numeric representation. Unused if NULL.
221 * The return value is 0 on failure, with the error code in the error
222 * stack, and 1 on success.
224 static int addr_strings(const BIO_ADDR *ap, int numeric,
225 char **hostname, char **service)
227 if (BIO_sock_init() != 1)
233 char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
237 flags |= NI_NUMERICHOST | NI_NUMERICSERV;
239 if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap),
240 BIO_ADDR_sockaddr_size(ap),
241 host, sizeof(host), serv, sizeof(serv),
244 if (ret == EAI_SYSTEM) {
245 SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
246 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
250 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
251 ERR_add_error_data(1, gai_strerror(ret));
256 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
257 * leaves it with whatever garbage that happens to be there.
258 * However, we initialise serv with the empty string (serv[0]
259 * is therefore NUL), so it gets real easy to detect when things
260 * didn't go the way one might expect.
262 if (serv[0] == '\0') {
263 BIO_snprintf(serv, sizeof(serv), "%d",
264 ntohs(BIO_ADDR_rawport(ap)));
268 *hostname = OPENSSL_strdup(host);
270 *service = OPENSSL_strdup(serv);
274 *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
276 char serv[6]; /* port is 16 bits => max 5 decimal digits */
277 BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
278 *service = OPENSSL_strdup(serv);
285 char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
287 char *hostname = NULL;
289 if (addr_strings(ap, numeric, &hostname, NULL))
295 char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
297 char *service = NULL;
299 if (addr_strings(ap, numeric, NULL, &service))
305 char *BIO_ADDR_path_string(const BIO_ADDR *ap)
308 if (ap->sa.sa_family == AF_UNIX)
309 return OPENSSL_strdup(ap->s_un.sun_path);
315 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
316 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
317 * The returned struct sockaddr is const, so it can't be tampered with.
319 const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
325 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
326 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
327 * it allows you to tamper with the data (and thereby the contents
328 * of the input BIO_ADDR).
330 struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
336 * BIO_ADDR_sockaddr_size - non-public function that returns the size
337 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
338 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
339 * the size of the BIO_ADDR type is returned.
341 socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
343 if (ap->sa.sa_family == AF_INET)
344 return sizeof(ap->s_in);
346 if (ap->sa.sa_family == AF_INET6)
347 return sizeof(ap->s_in6);
350 if (ap->sa.sa_family == AF_UNIX)
351 return sizeof(ap->s_un);
356 /**********************************************************************
358 * Address info database
362 const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
365 return bai->bai_next;
369 int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
372 return bai->bai_family;
376 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
379 return bai->bai_socktype;
383 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
386 if (bai->bai_protocol != 0)
387 return bai->bai_protocol;
390 if (bai->bai_family == AF_UNIX)
394 switch (bai->bai_socktype) {
407 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
408 * of the struct sockaddr inside the BIO_ADDRINFO.
410 socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
413 return bai->bai_addrlen;
418 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
419 * as the struct sockaddr it is.
421 const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
424 return bai->bai_addr;
428 const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
431 return (BIO_ADDR *)bai->bai_addr;
435 void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
442 # define _cond bai->bai_family != AF_UNIX
452 /* Free manually when we know that addrinfo_wrap() was used.
453 * See further comment above addrinfo_wrap()
455 while (bai != NULL) {
456 BIO_ADDRINFO *next = bai->bai_next;
457 OPENSSL_free(bai->bai_addr);
463 /**********************************************************************
470 * The specs in hostserv can take these forms:
472 * host:service => *host = "host", *service = "service"
473 * host:* => *host = "host", *service = NULL
474 * host: => *host = "host", *service = NULL
475 * :service => *host = NULL, *service = "service"
476 * *:service => *host = NULL, *service = "service"
478 * in case no : is present in the string, the result depends on
479 * hostserv_prio, as follows:
481 * when hostserv_prio == BIO_PARSE_PRIO_HOST
482 * host => *host = "host", *service untouched
484 * when hostserv_prio == BIO_PARSE_PRIO_SERV
485 * service => *host untouched, *service = "service"
488 int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
489 enum BIO_hostserv_priorities hostserv_prio)
491 const char *h = NULL; size_t hl = 0;
492 const char *p = NULL; size_t pl = 0;
494 if (*hostserv == '[') {
495 if ((p = strchr(hostserv, ']')) == NULL)
509 const char *p2 = strrchr(hostserv, ':');
510 p = strchr(hostserv, ':');
513 * Check for more than one colon. There are three possible
515 * 1. IPv6 address with port number, last colon being separator.
516 * 2. IPv6 address only.
517 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
518 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
519 * Because of this ambiguity, we currently choose to make it an
530 } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
539 if (p != NULL && strchr(p, ':'))
542 if (h != NULL && host != NULL) {
544 || (hl == 1 && h[0] == '*')) {
547 *host = OPENSSL_strndup(h, hl);
552 if (p != NULL && service != NULL) {
554 || (pl == 1 && p[0] == '*')) {
557 *service = OPENSSL_strndup(p, pl);
558 if (*service == NULL)
565 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
568 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
571 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
575 /* addrinfo_wrap is used to build our own addrinfo "chain".
576 * (it has only one entry, so calling it a chain may be a stretch)
577 * It should ONLY be called when getaddrinfo() and friends
578 * aren't available, OR when dealing with a non IP protocol
579 * family, such as AF_UNIX
581 * the return value is 1 on success, or 0 on failure, which
582 * only happens if a memory allocation error occured.
584 static int addrinfo_wrap(int family, int socktype,
585 const void *where, size_t wherelen,
589 OPENSSL_assert(bai != NULL);
591 *bai = OPENSSL_zalloc(sizeof(**bai));
595 (*bai)->bai_family = family;
596 (*bai)->bai_socktype = socktype;
597 if (socktype == SOCK_STREAM)
598 (*bai)->bai_protocol = IPPROTO_TCP;
599 if (socktype == SOCK_DGRAM)
600 (*bai)->bai_protocol = IPPROTO_UDP;
602 if (family == AF_UNIX)
603 (*bai)->bai_protocol = 0;
606 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
607 just an advanced cast of BIO_ADDR* to struct sockaddr *
608 by the power of union, so while it may seem that we're
609 creating a memory leak here, we are not. It will be
611 BIO_ADDR *addr = BIO_ADDR_new();
613 BIO_ADDR_rawmake(addr, family, where, wherelen, port);
614 (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
617 (*bai)->bai_next = NULL;
618 if ((*bai)->bai_addr == NULL) {
619 BIO_ADDRINFO_free(*bai);
627 * BIO_lookup - look up the node and service you want to connect to.
628 * @node: the node you want to connect to.
629 * @service: the service you want to connect to.
630 * @lookup_type: declare intent with the result, client or server.
631 * @family: the address family you want to use. Use AF_UNSPEC for any, or
632 * AF_INET, AF_INET6 or AF_UNIX.
633 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
635 * @res: Storage place for the resulting list of returned addresses
637 * This will do a lookup of the node and service that you want to connect to.
638 * It returns a linked list of different addresses you can try to connect to.
640 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
642 * The return value is 1 on success or 0 in case of error.
644 int BIO_lookup(const char *host, const char *service,
645 enum BIO_lookup_type lookup_type,
646 int family, int socktype, BIO_ADDRINFO **res)
648 int ret = 0; /* Assume failure */
663 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
668 if (family == AF_UNIX) {
669 if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
672 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
677 if (BIO_sock_init() != 1)
683 struct addrinfo hints;
686 # ifdef AI_ADDRCONFIG
687 hints.ai_flags = AI_ADDRCONFIG;
689 hints.ai_family = family;
690 hints.ai_socktype = socktype;
691 hints.ai_protocol = 0;
692 hints.ai_addrlen = 0;
693 hints.ai_addr = NULL;
694 hints.ai_canonname = NULL;
695 hints.ai_next = NULL;
697 if (lookup_type == BIO_LOOKUP_SERVER)
698 hints.ai_flags |= AI_PASSIVE;
700 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
701 * macro magic in bio_lcl.h
703 switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
706 SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
707 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
711 ret = 1; /* Success */
714 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
715 ERR_add_error_data(1, gai_strerror(gai_ret));
720 const struct hostent *he;
721 /* Windows doesn't seem to have in_addr_t */
722 #ifdef OPENSSL_SYS_WINDOWS
723 static uint32_t he_fallback_address;
724 static const uint32_t *he_fallback_addresses[] =
725 { &he_fallback_address, NULL };
727 static in_addr_t he_fallback_address;
728 static const in_addr_t *he_fallback_addresses[] =
729 { &he_fallback_address, NULL };
731 static const struct hostent he_fallback =
732 { NULL, NULL, AF_INET, sizeof(he_fallback_address),
733 (char **)&he_fallback_addresses };
735 /* Apprently, on WIN64, s_proto and s_port have traded places... */
737 struct servent se_fallback = { NULL, NULL, NULL, 0 };
739 struct servent se_fallback = { NULL, NULL, 0, NULL };
743 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
744 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
745 he_fallback_address = INADDR_ANY;
748 switch(lookup_type) {
749 case BIO_LOOKUP_CLIENT:
750 he_fallback_address = INADDR_LOOPBACK;
752 case BIO_LOOKUP_SERVER:
753 he_fallback_address = INADDR_ANY;
756 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
760 he = gethostbyname(host);
763 #ifndef OPENSSL_SYS_WINDOWS
764 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
765 ERR_add_error_data(1, hstrerror(h_errno));
767 SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
774 if (service == NULL) {
775 se_fallback.s_port = 0;
776 se_fallback.s_proto = proto;
780 long portnum = strtol(service, &endp, 10);
782 if (endp != service && *endp == '\0'
783 && portnum > 0 && portnum < 65536) {
784 se_fallback.s_port = htons(portnum);
785 se_fallback.s_proto = proto;
787 } else if (endp == service) {
796 se = getservbyname(service, proto);
799 #ifndef OPENSSL_SYS_WINDOWS
800 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
801 ERR_add_error_data(1, hstrerror(h_errno));
803 SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
808 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
818 BIO_ADDRINFO *tmp_bai = NULL;
820 /* The easiest way to create a linked list from an
821 array is to start from the back */
822 for(addrlistp = he->h_addr_list; *addrlistp != NULL;
826 for(addresses = addrlistp - he->h_addr_list;
827 addrlistp--, addresses-- > 0; ) {
828 if (!addrinfo_wrap(he->h_addrtype, socktype,
829 *addrlistp, he->h_length,
830 se->s_port, &tmp_bai))
831 goto addrinfo_malloc_err;
832 tmp_bai->bai_next = *res;
836 BIO_ADDRINFO_free(*res);
838 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
846 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
847 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
852 #endif /* OPENSSL_NO_SOCK */