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