1 /* Copyright (c) 2014, Google Inc.
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.
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. */
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
21 #include <sys/types.h>
23 #if !defined(OPENSSL_WINDOWS)
29 typedef int socklen_t;
35 int bio_ip_and_port_to_socket_and_addr(int *out_sock,
36 struct sockaddr_storage *out_addr,
37 socklen_t *out_addr_length,
39 const char *port_str) {
40 struct addrinfo hint, *result, *cur;
45 memset(&hint, 0, sizeof(hint));
46 hint.ai_family = AF_UNSPEC;
47 hint.ai_socktype = SOCK_STREAM;
49 ret = getaddrinfo(hostname, port_str, &hint, &result);
51 OPENSSL_PUT_ERROR(SYS, getaddrinfo, 0);
52 ERR_add_error_data(2, gai_strerror(ret));
58 for (cur = result; cur; cur = cur->ai_next) {
59 if (cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
62 memset(out_addr, 0, sizeof(struct sockaddr_storage));
63 memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
64 *out_addr_length = cur->ai_addrlen;
66 *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
68 OPENSSL_PUT_SYSTEM_ERROR(socket);
81 int bio_socket_nbio(int sock, int on) {
82 #if defined(OPENSSL_WINDOWS)
85 return 0 == ioctlsocket(sock, FIONBIO, &arg);
87 int flags = fcntl(sock, F_GETFL, 0);
96 return fcntl(sock, F_SETFL, flags) == 0;
100 void bio_clear_socket_error(void) {}
102 int bio_sock_error(int sock) {
104 socklen_t error_size = sizeof(error);
106 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {