Debian Codegraph @

crt/s2n/utils/s2n_socket.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 /* On Windows, s2n-tls does not provide built-in socket I/O.
17 * This file compiles to an empty translation unit on Windows.
18 */
19 #ifndef _WIN32
20
21 #include "utils/s2n_socket.h"
22
23 #include <netinet/in.h>
24 #include <netinet/tcp.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27
28 #include "tls/s2n_connection.h"
29 #include "utils/s2n_safety.h"
30
31 #if TCP_CORK
32 #define S2N_CORK TCP_CORK
33 #define S2N_CORK_ON 1
34 #define S2N_CORK_OFF 0
35 #elif TCP_NOPUSH
36 #define S2N_CORK TCP_NOPUSH
37 #define S2N_CORK_ON 1
38 #define S2N_CORK_OFF 0
39 #elif TCP_NODELAY
40 #define S2N_CORK TCP_NODELAY
41 #define S2N_CORK_ON 0
42 #define S2N_CORK_OFF 1
43 #endif
44
45 int s2n_socket_quickack(struct s2n_connection *conn)
46 {
47 #ifdef TCP_QUICKACK
48 POSIX_ENSURE_REF(conn);
49 if (!conn->managed_recv_io) {
50 return 0;
51 }
52
53 struct s2n_socket_read_io_context *r_io_ctx = (struct s2n_socket_read_io_context *) conn->recv_io_context;
54 POSIX_ENSURE_REF(r_io_ctx);
55 if (r_io_ctx->tcp_quickack_set) {
56 return 0;
57 }
58
59 /* Ignore the return value, if it fails it fails */
60 int optval = 1;
61 if (setsockopt(r_io_ctx->fd, IPPROTO_TCP, TCP_QUICKACK, &optval, sizeof(optval)) == 0) {
62 r_io_ctx->tcp_quickack_set = 1;
63 }
64 #endif
65
66 return 0;
67 }
68
69 int s2n_socket_write_snapshot(struct s2n_connection *conn)
70 {
71 #ifdef S2N_CORK
72 socklen_t corklen = sizeof(int);
73 POSIX_ENSURE_REF(conn);
74 struct s2n_socket_write_io_context *w_io_ctx = (struct s2n_socket_write_io_context *) conn->send_io_context;
75 POSIX_ENSURE_REF(w_io_ctx);
76
77 getsockopt(w_io_ctx->fd, IPPROTO_TCP, S2N_CORK, &w_io_ctx->original_cork_val, &corklen);
78 POSIX_ENSURE_EQ(corklen, sizeof(int));
79 w_io_ctx->original_cork_is_set = 1;
80 #endif
81
82 return 0;
83 }
84
85 int s2n_socket_read_snapshot(struct s2n_connection *conn)
86 {
87 #ifdef SO_RCVLOWAT
88 socklen_t watlen = sizeof(int);
89 POSIX_ENSURE_REF(conn);
90 struct s2n_socket_read_io_context *r_io_ctx = (struct s2n_socket_read_io_context *) conn->recv_io_context;
91 POSIX_ENSURE_REF(r_io_ctx);
92
93 getsockopt(r_io_ctx->fd, SOL_SOCKET, SO_RCVLOWAT, &r_io_ctx->original_rcvlowat_val, &watlen);
94 POSIX_ENSURE_EQ(watlen, sizeof(int));
95 r_io_ctx->original_rcvlowat_is_set = 1;
96 #endif
97
98 return 0;
99 }
100
101 int s2n_socket_write_restore(struct s2n_connection *conn)
102 {
103 #ifdef S2N_CORK
104 POSIX_ENSURE_REF(conn);
105 struct s2n_socket_write_io_context *w_io_ctx = (struct s2n_socket_write_io_context *) conn->send_io_context;
106 POSIX_ENSURE_REF(w_io_ctx);
107
108 if (!w_io_ctx->original_cork_is_set) {
109 return 0;
110 }
111 setsockopt(w_io_ctx->fd, IPPROTO_TCP, S2N_CORK, &w_io_ctx->original_cork_val, sizeof(w_io_ctx->original_cork_val));
112 w_io_ctx->original_cork_is_set = 0;
113 #endif
114
115 return 0;
116 }
117
118 int s2n_socket_read_restore(struct s2n_connection *conn)
119 {
120 #ifdef SO_RCVLOWAT
121 POSIX_ENSURE_REF(conn);
122 struct s2n_socket_read_io_context *r_io_ctx = (struct s2n_socket_read_io_context *) conn->recv_io_context;
123 POSIX_ENSURE_REF(r_io_ctx);
124
125 if (!r_io_ctx->original_rcvlowat_is_set) {
126 return 0;
127 }
128 setsockopt(r_io_ctx->fd, SOL_SOCKET, SO_RCVLOWAT, &r_io_ctx->original_rcvlowat_val, sizeof(r_io_ctx->original_rcvlowat_val));
129 r_io_ctx->original_rcvlowat_is_set = 0;
130 #endif
131
132 return 0;
133 }
134
135 int s2n_socket_was_corked(struct s2n_connection *conn)
136 {
137 POSIX_ENSURE_REF(conn);
138 /* If we're not using custom I/O and a send fd has not been set yet, return false*/
139 if (!conn->managed_send_io || !conn->send) {
140 return 0;
141 }
142
143 struct s2n_socket_write_io_context *io_ctx = (struct s2n_socket_write_io_context *) conn->send_io_context;
144 POSIX_ENSURE_REF(io_ctx);
145
146 return io_ctx->original_cork_val;
147 }
148
149 int s2n_socket_write_cork(struct s2n_connection *conn)
150 {
151 #ifdef S2N_CORK
152 POSIX_ENSURE_REF(conn);
153 int optval = S2N_CORK_ON;
154
155 struct s2n_socket_write_io_context *w_io_ctx = (struct s2n_socket_write_io_context *) conn->send_io_context;
156 POSIX_ENSURE_REF(w_io_ctx);
157
158 /* Ignore the return value, if it fails it fails */
159 setsockopt(w_io_ctx->fd, IPPROTO_TCP, S2N_CORK, &optval, sizeof(optval));
160 #endif
161
162 return 0;
163 }
164
165 int s2n_socket_write_uncork(struct s2n_connection *conn)
166 {
167 #ifdef S2N_CORK
168 POSIX_ENSURE_REF(conn);
169 int optval = S2N_CORK_OFF;
170
171 struct s2n_socket_write_io_context *w_io_ctx = (struct s2n_socket_write_io_context *) conn->send_io_context;
172 POSIX_ENSURE_REF(w_io_ctx);
173
174 /* Ignore the return value, if it fails it fails */
175 setsockopt(w_io_ctx->fd, IPPROTO_TCP, S2N_CORK, &optval, sizeof(optval));
176 #endif
177
178 return 0;
179 }
180
181 int s2n_socket_read(void *io_context, uint8_t *buf, uint32_t len)
182 {
183 POSIX_ENSURE_REF(io_context);
184 POSIX_ENSURE_REF(buf);
185 int rfd = ((struct s2n_socket_read_io_context *) io_context)->fd;
186 if (rfd < 0) {
187 errno = EBADF;
188 POSIX_BAIL(S2N_ERR_BAD_FD);
189 }
190
191 /* Clear the quickack flag so we know to reset it */
192 ((struct s2n_socket_read_io_context *) io_context)->tcp_quickack_set = 0;
193
194 /* On success, the number of bytes read is returned. On failure, -1 is
195 * returned and errno is set appropriately. */
196 ssize_t result = read(rfd, buf, len);
197 POSIX_ENSURE_INCLUSIVE_RANGE(INT_MIN, result, INT_MAX);
198 return result;
199 }
200
201 int s2n_socket_write(void *io_context, const uint8_t *buf, uint32_t len)
202 {
203 POSIX_ENSURE_REF(io_context);
204 POSIX_ENSURE_REF(buf);
205 int wfd = ((struct s2n_socket_write_io_context *) io_context)->fd;
206 if (wfd < 0) {
207 errno = EBADF;
208 POSIX_BAIL(S2N_ERR_BAD_FD);
209 }
210
211 /* On success, the number of bytes written is returned. On failure, -1 is
212 * returned and errno is set appropriately. */
213 ssize_t result = write(wfd, buf, len);
214 POSIX_ENSURE_INCLUSIVE_RANGE(INT_MIN, result, INT_MAX);
215 return result;
216 }
217
218 #endif
219
220 /* Suppress empty translation unit warning when compiled on Windows. */
221 #pragma clang diagnostic ignored "-Wempty-translation-unit"