1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "internal/quic_reactor.h"
#include "internal/common.h"
#include "internal/thread_arch.h"
/*
* Core I/O Reactor Framework
* ==========================
*/
void ossl_quic_reactor_init(QUIC_REACTOR *rtor,
void (*tick_cb)(QUIC_TICK_RESULT *res, void *arg,
uint32_t flags),
void *tick_cb_arg,
OSSL_TIME initial_tick_deadline)
{
rtor->poll_r.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
rtor->poll_w.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
rtor->net_read_desired = 0;
rtor->net_write_desired = 0;
rtor->can_poll_r = 0;
rtor->can_poll_w = 0;
rtor->tick_deadline = initial_tick_deadline;
rtor->tick_cb = tick_cb;
rtor->tick_cb_arg = tick_cb_arg;
}
void ossl_quic_reactor_set_poll_r(QUIC_REACTOR *rtor, const BIO_POLL_DESCRIPTOR *r)
{
if (r == NULL)
rtor->poll_r.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
else
rtor->poll_r = *r;
rtor->can_poll_r
= ossl_quic_reactor_can_support_poll_descriptor(rtor, &rtor->poll_r);
}
void ossl_quic_reactor_set_poll_w(QUIC_REACTOR *rtor, const BIO_POLL_DESCRIPTOR *w)
{
if (w == NULL)
rtor->poll_w.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
else
rtor->poll_w = *w;
rtor->can_poll_w
= ossl_quic_reactor_can_support_poll_descriptor(rtor, &rtor->poll_w);
}
const BIO_POLL_DESCRIPTOR *ossl_quic_reactor_get_poll_r(const QUIC_REACTOR *rtor)
{
return &rtor->poll_r;
}
const BIO_POLL_DESCRIPTOR *ossl_quic_reactor_get_poll_w(const QUIC_REACTOR *rtor)
{
return &rtor->poll_w;
}
int ossl_quic_reactor_can_support_poll_descriptor(const QUIC_REACTOR *rtor,
const BIO_POLL_DESCRIPTOR *d)
{
return d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
}
int ossl_quic_reactor_can_poll_r(const QUIC_REACTOR *rtor)
{
return rtor->can_poll_r;
}
int ossl_quic_reactor_can_poll_w(const QUIC_REACTOR *rtor)
{
return rtor->can_poll_w;
}
int ossl_quic_reactor_net_read_desired(QUIC_REACTOR *rtor)
{
return rtor->net_read_desired;
}
int ossl_quic_reactor_net_write_desired(QUIC_REACTOR *rtor)
{
return rtor->net_write_desired;
}
OSSL_TIME ossl_quic_reactor_get_tick_deadline(QUIC_REACTOR *rtor)
{
return rtor->tick_deadline;
}
int ossl_quic_reactor_tick(QUIC_REACTOR *rtor, uint32_t flags)
{
QUIC_TICK_RESULT res = {0};
/*
* Note that the tick callback cannot fail; this is intentional. Arguably it
* does not make that much sense for ticking to 'fail' (in the sense of an
* explicit error indicated to the user) because ticking is by its nature
* best effort. If something fatal happens with a connection we can report
* it on the next actual application I/O call.
*/
rtor->tick_cb(&res, rtor->tick_cb_arg, flags);
rtor->net_read_desired = res.net_read_desired;
rtor->net_write_desired = res.net_write_desired;
rtor->tick_deadline = res.tick_deadline;
return 1;
}
/*
* Blocking I/O Adaptation Layer
* =============================
*/
/*
* Utility which can be used to poll on up to two FDs. This is designed to
* support use of split FDs (e.g. with SSL_set_rfd and SSL_set_wfd where
* different FDs are used for read and write).
*
* Generally use of poll(2) is preferred where available. Windows, however,
* hasn't traditionally offered poll(2), only select(2). WSAPoll() was
* introduced in Vista but has seemingly been buggy until relatively recent
* versions of Windows 10. Moreover we support XP so this is not a suitable
* target anyway. However, the traditional issues with select(2) turn out not to
* be an issue on Windows; whereas traditional *NIX select(2) uses a bitmap of
* FDs (and thus is limited in the magnitude of the FDs expressible), Windows
* select(2) is very different. In Windows, socket handles are not allocated
* contiguously from zero and thus this bitmap approach was infeasible. Thus in
* adapting the Berkeley sockets API to Windows a different approach was taken
* whereby the fd_set contains a fixed length array of socket handles and an
* integer indicating how many entries are valid; thus Windows select()
* ironically is actually much more like *NIX poll(2) than *NIX select(2). In
* any case, this means that the relevant limit for Windows select() is the
* number of FDs being polled, not the magnitude of those FDs. Since we only
* poll for two FDs here, this limit does not concern us.
*
* Usage: rfd and wfd may be the same or different. Either or both may also be
* -1. If rfd_want_read is 1, rfd is polled for readability, and if
* wfd_want_write is 1, wfd is polled for writability. Note that since any
* passed FD is always polled for error conditions, setting rfd_want_read=0 and
* wfd_want_write=0 is not the same as passing -1 for both FDs.
*
* deadline is a timestamp to return at. If it is ossl_time_infinite(), the call
* never times out.
*
* Returns 0 on error and 1 on success. Timeout expiry is considered a success
* condition. We don't elaborate our return values here because the way we are
* actually using this doesn't currently care.
*
* If mutex is non-NULL, it is assumed to be held for write and is unlocked for
* the duration of the call.
*
* Precondition: mutex is NULL or is held for write (unchecked)
* Postcondition: mutex is NULL or is held for write (unless
* CRYPTO_THREAD_write_lock fails)
*/
static int poll_two_fds(int rfd, int rfd_want_read,
int wfd, int wfd_want_write,
OSSL_TIME deadline,
CRYPTO_MUTEX *mutex)
{
#if defined(OPENSSL_SYS_WINDOWS) || !defined(POLLIN)
fd_set rfd_set, wfd_set, efd_set;
OSSL_TIME now, timeout;
struct timeval tv, *ptv;
int maxfd, pres;
# ifndef OPENSSL_SYS_WINDOWS
/*
* On Windows there is no relevant limit to the magnitude of a fd value (see
* above). On *NIX the fd_set uses a bitmap and we must check the limit.
*/
if (rfd >= FD_SETSIZE || wfd >= FD_SETSIZE)
return 0;
# endif
FD_ZERO(&rfd_set);
FD_ZERO(&wfd_set);
FD_ZERO(&efd_set);
if (rfd != -1 && rfd_want_read)
openssl_fdset(rfd, &rfd_set);
if (wfd != -1 && wfd_want_write)
openssl_fdset(wfd, &wfd_set);
/* Always check for error conditions. */
if (rfd != -1)
openssl_fdset(rfd, &efd_set);
if (wfd != -1)
openssl_fdset(wfd, &efd_set);
maxfd = rfd;
if (wfd > maxfd)
maxfd = wfd;
if (!ossl_assert(rfd != -1 || wfd != -1
|| !ossl_time_is_infinite(deadline)))
/* Do not block forever; should not happen. */
return 0;
# if defined(OPENSSL_THREADS)
if (mutex != NULL)
ossl_crypto_mutex_unlock(mutex);
# endif
do {
/*
* select expects a timeout, not a deadline, so do the conversion.
* Update for each call to ensure the correct value is used if we repeat
* due to EINTR.
*/
if (ossl_time_is_infinite(deadline)) {
ptv = NULL;
} else {
now = ossl_time_now();
/*
* ossl_time_subtract saturates to zero so we don't need to check if
* now > deadline.
*/
timeout = ossl_time_subtract(deadline, now);
tv = ossl_time_to_timeval(timeout);
ptv = &tv;
}
pres = select(maxfd + 1, &rfd_set, &wfd_set, &efd_set, ptv);
} while (pres == -1 && get_last_socket_error_is_eintr());
# if defined(OPENSSL_THREADS)
if (mutex != NULL)
ossl_crypto_mutex_lock(mutex);
# endif
return pres < 0 ? 0 : 1;
#else
int pres, timeout_ms;
OSSL_TIME now, timeout;
struct pollfd pfds[2] = {0};
size_t npfd = 0;
if (rfd == wfd) {
pfds[npfd].fd = rfd;
pfds[npfd].events = (rfd_want_read ? POLLIN : 0)
| (wfd_want_write ? POLLOUT : 0);
if (rfd >= 0 && pfds[npfd].events != 0)
++npfd;
} else {
pfds[npfd].fd = rfd;
pfds[npfd].events = (rfd_want_read ? POLLIN : 0);
if (rfd >= 0 && pfds[npfd].events != 0)
++npfd;
pfds[npfd].fd = wfd;
pfds[npfd].events = (wfd_want_write ? POLLOUT : 0);
if (wfd >= 0 && pfds[npfd].events != 0)
++npfd;
}
if (!ossl_assert(npfd != 0 || !ossl_time_is_infinite(deadline)))
/* Do not block forever; should not happen. */
return 0;
# if defined(OPENSSL_THREADS)
if (mutex != NULL)
ossl_crypto_mutex_unlock(mutex);
# endif
do {
if (ossl_time_is_infinite(deadline)) {
timeout_ms = -1;
} else {
now = ossl_time_now();
timeout = ossl_time_subtract(deadline, now);
timeout_ms = ossl_time2ms(timeout);
}
pres = poll(
|