blob: 268405a6d8e4fe20f2d73ec840af33d253ca86d7 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Benjamin7ca4b422015-07-13 16:43:47 -040015#undef _POSIX_C_SOURCE
Khem Raj241364c2015-06-27 13:29:52 -070016#define _POSIX_C_SOURCE 200112L
Adam Langley95c29f32014-06-20 12:00:00 -070017
18#include <openssl/bio.h>
19#include <openssl/err.h>
20
21#include <fcntl.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080022#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070023#include <sys/types.h>
24
25#if !defined(OPENSSL_WINDOWS)
26#include <netdb.h>
27#include <unistd.h>
28#else
David Benjamina353cdb2016-06-09 16:48:33 -040029OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -070030#include <winsock2.h>
31#include <ws2tcpip.h>
David Benjamina353cdb2016-06-09 16:48:33 -040032OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langley95c29f32014-06-20 12:00:00 -070033#endif
34
35#include "internal.h"
David Benjamin17cf2cb2016-12-13 01:07:13 -050036#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070037
38
39int bio_ip_and_port_to_socket_and_addr(int *out_sock,
40 struct sockaddr_storage *out_addr,
David Benjamine77dff62014-07-17 17:23:22 -040041 socklen_t *out_addr_length,
Adam Langley95c29f32014-06-20 12:00:00 -070042 const char *hostname,
43 const char *port_str) {
44 struct addrinfo hint, *result, *cur;
45 int ret;
46
47 *out_sock = -1;
48
David Benjamin17cf2cb2016-12-13 01:07:13 -050049 OPENSSL_memset(&hint, 0, sizeof(hint));
Adam Langley95c29f32014-06-20 12:00:00 -070050 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 Benjamin3570d732015-06-29 00:28:17 -040055 OPENSSL_PUT_ERROR(SYS, 0);
David Benjamin3fa65f02015-05-15 19:11:57 -040056 ERR_add_error_data(1, gai_strerror(ret));
Adam Langley95c29f32014-06-20 12:00:00 -070057 return 0;
58 }
59
60 ret = 0;
61
62 for (cur = result; cur; cur = cur->ai_next) {
Adam Langleyd9e81732015-10-30 13:43:49 -070063 if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
Adam Langley95c29f32014-06-20 12:00:00 -070064 continue;
65 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050066 OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage));
67 OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
David Benjamine77dff62014-07-17 17:23:22 -040068 *out_addr_length = cur->ai_addrlen;
Adam Langley95c29f32014-06-20 12:00:00 -070069
70 *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
71 if (*out_sock < 0) {
David Benjamin3fc138e2015-10-28 18:03:21 -040072 OPENSSL_PUT_SYSTEM_ERROR();
Adam Langley95c29f32014-06-20 12:00:00 -070073 goto out;
74 }
75
76 ret = 1;
77 break;
78 }
79
80out:
81 freeaddrinfo(result);
82 return ret;
83}
84
85int 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 Benjaminc44d2f42014-08-20 16:24:00 -0400104void bio_clear_socket_error(void) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700105
106int 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}