Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/socket_helper.c

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #if defined(__linux__)
5 #undef _POSIX_C_SOURCE
6 #define _POSIX_C_SOURCE 200112L
7 #endif
8
9 #include <openssl/bio.h>
10 #include <openssl/err.h>
11
12 #if !defined(OPENSSL_NO_SOCK)
13
14 #include <time.h>
15 #include <fcntl.h>
16 #include <string.h>
17 #include <sys/types.h>
18
19 #if !defined(OPENSSL_WINDOWS)
20 #include <netdb.h>
21 #include <unistd.h>
22 #else
23 OPENSSL_MSVC_PRAGMA(warning(push, 3))
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 OPENSSL_MSVC_PRAGMA(warning(pop))
27 #endif
28
29 #include "internal.h"
30 #include "../internal.h"
31
32
33 int bio_ip_and_port_to_socket_and_addr(int *out_sock,
34 struct sockaddr_storage *out_addr,
35 socklen_t *out_addr_length,
36 const char *hostname,
37 const char *port_str) {
38 struct addrinfo hint, *result, *cur;
39 int ret;
40
41 *out_sock = -1;
42
43 OPENSSL_cleanse(&hint,sizeof(hint));
44 hint.ai_family = AF_UNSPEC;
45 hint.ai_socktype = SOCK_STREAM;
46
47 ret = getaddrinfo(hostname, port_str, &hint, &result);
48 if (ret != 0) {
49 OPENSSL_PUT_ERROR(SYS, 0);
50 #if defined(OPENSSL_WINDOWS)
51 ERR_add_error_data(1, gai_strerrorA(ret));
52 #else
53 ERR_add_error_data(1, gai_strerror(ret));
54 #endif
55 return 0;
56 }
57
58 ret = 0;
59
60 for (cur = result; cur; cur = cur->ai_next) {
61 if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
62 continue;
63 }
64 OPENSSL_cleanse(out_addr, sizeof(struct sockaddr_storage));
65 OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
66 *out_addr_length = cur->ai_addrlen;
67
68 *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
69 if (*out_sock < 0) {
70 OPENSSL_PUT_SYSTEM_ERROR();
71 goto out;
72 }
73
74 ret = 1;
75 break;
76 }
77
78 out:
79 freeaddrinfo(result);
80 return ret;
81 }
82
83 int bio_socket_nbio(int sock, int on) {
84 #if defined(OPENSSL_WINDOWS)
85 u_long arg = on;
86
87 return 0 == ioctlsocket(sock, FIONBIO, &arg);
88 #else
89 int flags = fcntl(sock, F_GETFL, 0);
90 if (flags < 0) {
91 return 0;
92 }
93 if (!on) {
94 flags &= ~O_NONBLOCK;
95 } else {
96 flags |= O_NONBLOCK;
97 }
98 return fcntl(sock, F_SETFL, flags) == 0;
99 #endif
100 }
101
102 void bio_clear_socket_error(int sock) {
103 bio_sock_error_get_and_clear(sock);
104 }
105
106 int bio_sock_error_get_and_clear(int sock) {
107 int error;
108 socklen_t error_size = sizeof(error);
109 // Get and clear the pending socket error. The SO_ERROR option is read-only.
110 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
111 return 1;
112 }
113 return error;
114 }
115
116 int bio_socket_should_retry(int return_value) {
117 #if defined(OPENSSL_WINDOWS)
118 return return_value == -1 && (WSAGetLastError() == WSAEWOULDBLOCK);
119 #else
120 // On POSIX platforms, sockets and fds are the same.
121 return bio_errno_should_retry(return_value);
122 #endif
123 }
124
125 #endif // OPENSSL_NO_SOCK