Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
David Benjamin | 7ca4b42 | 2015-07-13 16:43:47 -0400 | [diff] [blame] | 15 | #undef _POSIX_C_SOURCE |
Khem Raj | 241364c | 2015-06-27 13:29:52 -0700 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200112L |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 17 | |
| 18 | #include <openssl/bio.h> |
| 19 | #include <openssl/err.h> |
| 20 | |
| 21 | #include <fcntl.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 22 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | |
| 25 | #if !defined(OPENSSL_WINDOWS) |
| 26 | #include <netdb.h> |
| 27 | #include <unistd.h> |
| 28 | #else |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 29 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 30 | #include <winsock2.h> |
| 31 | #include <ws2tcpip.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 32 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #include "internal.h" |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 36 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | int bio_ip_and_port_to_socket_and_addr(int *out_sock, |
| 40 | struct sockaddr_storage *out_addr, |
David Benjamin | e77dff6 | 2014-07-17 17:23:22 -0400 | [diff] [blame] | 41 | socklen_t *out_addr_length, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 42 | const char *hostname, |
| 43 | const char *port_str) { |
| 44 | struct addrinfo hint, *result, *cur; |
| 45 | int ret; |
| 46 | |
| 47 | *out_sock = -1; |
| 48 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 49 | OPENSSL_memset(&hint, 0, sizeof(hint)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 50 | hint.ai_family = AF_UNSPEC; |
| 51 | hint.ai_socktype = SOCK_STREAM; |
| 52 | |
| 53 | ret = getaddrinfo(hostname, port_str, &hint, &result); |
| 54 | if (ret != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 55 | OPENSSL_PUT_ERROR(SYS, 0); |
David Benjamin | 3fa65f0 | 2015-05-15 19:11:57 -0400 | [diff] [blame] | 56 | ERR_add_error_data(1, gai_strerror(ret)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | ret = 0; |
| 61 | |
| 62 | for (cur = result; cur; cur = cur->ai_next) { |
Adam Langley | d9e8173 | 2015-10-30 13:43:49 -0700 | [diff] [blame] | 63 | if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | continue; |
| 65 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 66 | OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage)); |
| 67 | OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen); |
David Benjamin | e77dff6 | 2014-07-17 17:23:22 -0400 | [diff] [blame] | 68 | *out_addr_length = cur->ai_addrlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 69 | |
| 70 | *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); |
| 71 | if (*out_sock < 0) { |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 72 | OPENSSL_PUT_SYSTEM_ERROR(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 73 | goto out; |
| 74 | } |
| 75 | |
| 76 | ret = 1; |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | out: |
| 81 | freeaddrinfo(result); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | int bio_socket_nbio(int sock, int on) { |
| 86 | #if defined(OPENSSL_WINDOWS) |
| 87 | u_long arg = on; |
| 88 | |
| 89 | return 0 == ioctlsocket(sock, FIONBIO, &arg); |
| 90 | #else |
| 91 | int flags = fcntl(sock, F_GETFL, 0); |
| 92 | if (flags < 0) { |
| 93 | return 0; |
| 94 | } |
| 95 | if (!on) { |
| 96 | flags &= ~O_NONBLOCK; |
| 97 | } else { |
| 98 | flags |= O_NONBLOCK; |
| 99 | } |
| 100 | return fcntl(sock, F_SETFL, flags) == 0; |
| 101 | #endif |
| 102 | } |
| 103 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 104 | void bio_clear_socket_error(void) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | |
| 106 | int bio_sock_error(int sock) { |
| 107 | int error; |
| 108 | socklen_t error_size = sizeof(error); |
| 109 | |
| 110 | if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) { |
| 111 | return 1; |
| 112 | } |
| 113 | return error; |
| 114 | } |