| 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 | |
| 15 | #undef _POSIX_C_SOURCE |
| 16 | #define _POSIX_C_SOURCE 200112L |
| 17 | |
| 18 | #include <openssl/bio.h> |
| 19 | #include <openssl/err.h> |
| 20 | |
| 21 | #if !defined(OPENSSL_TRUSTY) |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #if !defined(OPENSSL_WINDOWS) |
| 28 | #include <netdb.h> |
| 29 | #include <unistd.h> |
| 30 | #else |
| 31 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 32 | #include <winsock2.h> |
| 33 | #include <ws2tcpip.h> |
| 34 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 35 | #endif |
| 36 | |
| 37 | #include "internal.h" |
| 38 | #include "../internal.h" |
| 39 | |
| 40 | |
| 41 | int bio_ip_and_port_to_socket_and_addr(int *out_sock, |
| 42 | struct sockaddr_storage *out_addr, |
| 43 | socklen_t *out_addr_length, |
| 44 | const char *hostname, |
| 45 | const char *port_str) { |
| 46 | struct addrinfo hint, *result, *cur; |
| 47 | int ret; |
| 48 | |
| 49 | *out_sock = -1; |
| 50 | |
| 51 | OPENSSL_memset(&hint, 0, sizeof(hint)); |
| 52 | hint.ai_family = AF_UNSPEC; |
| 53 | hint.ai_socktype = SOCK_STREAM; |
| 54 | |
| 55 | ret = getaddrinfo(hostname, port_str, &hint, &result); |
| 56 | if (ret != 0) { |
| 57 | OPENSSL_PUT_ERROR(SYS, 0); |
| 58 | ERR_add_error_data(1, gai_strerror(ret)); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | ret = 0; |
| 63 | |
| 64 | for (cur = result; cur; cur = cur->ai_next) { |
| 65 | if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) { |
| 66 | continue; |
| 67 | } |
| 68 | OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage)); |
| 69 | OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen); |
| 70 | *out_addr_length = cur->ai_addrlen; |
| 71 | |
| 72 | *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); |
| 73 | if (*out_sock < 0) { |
| 74 | OPENSSL_PUT_SYSTEM_ERROR(); |
| 75 | goto out; |
| 76 | } |
| 77 | |
| 78 | ret = 1; |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | out: |
| 83 | freeaddrinfo(result); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | int bio_socket_nbio(int sock, int on) { |
| 88 | #if defined(OPENSSL_WINDOWS) |
| 89 | u_long arg = on; |
| 90 | |
| 91 | return 0 == ioctlsocket(sock, FIONBIO, &arg); |
| 92 | #else |
| 93 | int flags = fcntl(sock, F_GETFL, 0); |
| 94 | if (flags < 0) { |
| 95 | return 0; |
| 96 | } |
| 97 | if (!on) { |
| 98 | flags &= ~O_NONBLOCK; |
| 99 | } else { |
| 100 | flags |= O_NONBLOCK; |
| 101 | } |
| 102 | return fcntl(sock, F_SETFL, flags) == 0; |
| 103 | #endif |
| 104 | } |
| 105 | |
| 106 | void bio_clear_socket_error(void) {} |
| 107 | |
| 108 | int bio_sock_error(int sock) { |
| 109 | int error; |
| 110 | socklen_t error_size = sizeof(error); |
| 111 | |
| 112 | if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) { |
| 113 | return 1; |
| 114 | } |
| 115 | return error; |
| 116 | } |
| 117 | |
| 118 | #endif // OPENSSL_TRUSTY |
| 119 | |