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>
OPENSSL_MSVC_PRAGMA(warning (pop))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 #if defined(OPENSSL_WINDOWS)
59 ERR_add_error_data(1, gai_strerrorA(ret));
60 #else
61 ERR_add_error_data(1, gai_strerror(ret));
62 #endif
63 return 0;
64 }
65
66 ret = 0;
67
68 for (cur = result; cur; cur = cur->ai_next) {
69 if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
70 continue;
71 }
72 OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage));
73 OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
74 *out_addr_length = cur->ai_addrlen;
75
76 *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
77 if (*out_sock < 0) {
78 OPENSSL_PUT_SYSTEM_ERROR();
79 goto out;
80 }
81
82 ret = 1;
83 break;
84 }
85
86 out:
87 freeaddrinfo(result);
88 return ret;
89 }
90
bio_socket_nbio(int sock,int on)91 int bio_socket_nbio(int sock, int on) {
92 #if defined(OPENSSL_WINDOWS)
93 u_long arg = on;
94
95 return 0 == ioctlsocket(sock, FIONBIO, &arg);
96 #else
97 int flags = fcntl(sock, F_GETFL, 0);
98 if (flags < 0) {
99 return 0;
100 }
101 if (!on) {
102 flags &= ~O_NONBLOCK;
103 } else {
104 flags |= O_NONBLOCK;
105 }
106 return fcntl(sock, F_SETFL, flags) == 0;
107 #endif
108 }
109
bio_clear_socket_error(void)110 void bio_clear_socket_error(void) {}
111
bio_sock_error(int sock)112 int bio_sock_error(int sock) {
113 int error;
114 socklen_t error_size = sizeof(error);
115
116 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
117 return 1;
118 }
119 return error;
120 }
121
122 #endif // OPENSSL_TRUSTY
123