/src/openssl/ssl/dgram_demux.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/e_os.h" |
11 | | #include "internal/dgram_demux.h" |
12 | | #include "internal/thread_arch.h" |
13 | | #include "internal/common.h" |
14 | | #include <openssl/err.h> |
15 | | #include <string.h> |
16 | | |
17 | | #if !defined(OPENSSL_NO_QUIC) || !defined(OPENSSL_NO_DTLS) |
18 | | |
19 | | /* URXE_DEMUX_STATE_* are defined in dgram_demux.h */ |
20 | | |
21 | 0 | #define DEMUX_MAX_MSGS_PER_CALL 32 |
22 | 0 | #define DEMUX_DEFAULT_MTU 1500 |
23 | 0 | #define DEMUX_MIN_INITIAL_DGRAM_LEN 1200 |
24 | | |
25 | | struct dgram_demux_st { |
26 | | /* The underlying transport BIO with datagram semantics. */ |
27 | | BIO *net_bio; |
28 | | |
29 | | /* |
30 | | * Our current understanding of the upper bound on an incoming datagram size |
31 | | * in bytes. |
32 | | */ |
33 | | size_t mtu; |
34 | | |
35 | | /* The datagram_id to use for the next datagram we receive. */ |
36 | | uint64_t next_datagram_id; |
37 | | |
38 | | /* Time retrieval callback. */ |
39 | | OSSL_TIME (*now)(void *arg); |
40 | | void *now_arg; |
41 | | |
42 | | /* The default packet handler, if any. */ |
43 | | ossl_dgram_demux_cb_fn *default_cb; |
44 | | void *default_cb_arg; |
45 | | |
46 | | /* |
47 | | * List of URXEs which are not currently in use (i.e., not filled with |
48 | | * unconsumed data). These are moved to the pending list as they are filled. |
49 | | */ |
50 | | DGRAM_URXE_LIST urx_free; |
51 | | |
52 | | /* |
53 | | * List of URXEs which are filled with received encrypted data. These are |
54 | | * removed from this list as we invoke the callbacks for each of them. They |
55 | | * are then not on any list managed by us; we forget about them until our |
56 | | * user calls ossl_dgram_demux_release_urxe to return the URXE to us, at |
57 | | * which point we add it to the free list. |
58 | | */ |
59 | | DGRAM_URXE_LIST urx_pending; |
60 | | |
61 | | /* Whether to use local address support. */ |
62 | | char use_local_addr; |
63 | | |
64 | | /* Whether internal locking is required */ |
65 | | char require_mutex; |
66 | | |
67 | | /* |
68 | | * Mutex protecting the URXE lists (urx_free and urx_pending). |
69 | | */ |
70 | | CRYPTO_MUTEX *mutex; |
71 | | }; |
72 | | |
73 | | /* List management helpers */ |
74 | | void ossl_dgram_urxe_remove(DGRAM_URXE_LIST *l, DGRAM_URXE *e) |
75 | 0 | { |
76 | 0 | ossl_list_urxe_remove(l, e); |
77 | 0 | } |
78 | | |
79 | | void ossl_dgram_urxe_insert_head(DGRAM_URXE_LIST *l, DGRAM_URXE *e) |
80 | 0 | { |
81 | 0 | ossl_list_urxe_insert_head(l, e); |
82 | 0 | } |
83 | | |
84 | | void ossl_dgram_urxe_insert_tail(DGRAM_URXE_LIST *l, DGRAM_URXE *e) |
85 | 0 | { |
86 | 0 | ossl_list_urxe_insert_tail(l, e); |
87 | 0 | } |
88 | | |
89 | | DGRAM_DEMUX *ossl_dgram_demux_new(BIO *net_bio, |
90 | | int threadsafe, |
91 | | OSSL_TIME (*now)(void *arg), |
92 | | void *now_arg) |
93 | 0 | { |
94 | 0 | DGRAM_DEMUX *demux; |
95 | |
|
96 | 0 | demux = OPENSSL_zalloc(sizeof(DGRAM_DEMUX)); |
97 | 0 | if (demux == NULL) |
98 | 0 | return NULL; |
99 | | |
100 | 0 | if (threadsafe) { |
101 | 0 | demux->require_mutex = 1; |
102 | 0 | demux->mutex = ossl_crypto_mutex_new(); |
103 | 0 | #ifdef OPENSSL_THREADS |
104 | 0 | if (demux->mutex == NULL) { |
105 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
106 | 0 | OPENSSL_free(demux); |
107 | 0 | return NULL; |
108 | 0 | } |
109 | 0 | #endif |
110 | 0 | } |
111 | | |
112 | | /* We update this if possible when we get a BIO. */ |
113 | 0 | demux->mtu = DEMUX_DEFAULT_MTU; |
114 | 0 | demux->now = now; |
115 | 0 | demux->now_arg = now_arg; |
116 | |
|
117 | 0 | ossl_dgram_demux_set_bio(demux, net_bio); |
118 | |
|
119 | 0 | return demux; |
120 | 0 | } |
121 | | |
122 | | static void dgram_demux_free_urxl(DGRAM_URXE_LIST *l) |
123 | 0 | { |
124 | 0 | DGRAM_URXE *e, *enext; |
125 | |
|
126 | 0 | for (e = ossl_list_urxe_head(l); e != NULL; e = enext) { |
127 | 0 | enext = ossl_list_urxe_next(e); |
128 | 0 | ossl_list_urxe_remove(l, e); |
129 | 0 | OPENSSL_free(e); |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | void ossl_dgram_demux_free(DGRAM_DEMUX *demux) |
134 | 0 | { |
135 | 0 | if (demux == NULL) |
136 | 0 | return; |
137 | | |
138 | | /* Free all URXEs we are holding. */ |
139 | 0 | dgram_demux_free_urxl(&demux->urx_free); |
140 | 0 | dgram_demux_free_urxl(&demux->urx_pending); |
141 | |
|
142 | 0 | if (demux->require_mutex) |
143 | 0 | ossl_crypto_mutex_free(&demux->mutex); |
144 | |
|
145 | 0 | OPENSSL_free(demux); |
146 | 0 | } |
147 | | |
148 | | void ossl_dgram_demux_set_bio(DGRAM_DEMUX *demux, BIO *net_bio) |
149 | 0 | { |
150 | 0 | unsigned int mtu; |
151 | |
|
152 | 0 | demux->net_bio = net_bio; |
153 | |
|
154 | 0 | if (net_bio != NULL) { |
155 | | /* |
156 | | * Try to determine our MTU if possible. The BIO is not required to |
157 | | * support this, in which case we remain at the last known MTU, or our |
158 | | * initial default. |
159 | | */ |
160 | 0 | mtu = BIO_dgram_get_mtu(net_bio); |
161 | 0 | if (mtu >= DEMUX_MIN_INITIAL_DGRAM_LEN) |
162 | 0 | ossl_dgram_demux_set_mtu(demux, mtu); |
163 | |
|
164 | 0 | if (BIO_dgram_get_local_addr_cap(net_bio) |
165 | 0 | && BIO_dgram_set_local_addr_enable(net_bio, 1)) |
166 | 0 | demux->use_local_addr = 1; |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | int ossl_dgram_demux_set_mtu(DGRAM_DEMUX *demux, unsigned int mtu) |
171 | 0 | { |
172 | 0 | if (mtu < DEMUX_MIN_INITIAL_DGRAM_LEN) |
173 | 0 | return 0; |
174 | | |
175 | | /* |
176 | | * mtu is read under demux->mutex by the receive path |
177 | | * so take the lock here. |
178 | | */ |
179 | 0 | if (demux->require_mutex) |
180 | 0 | ossl_crypto_mutex_lock(demux->mutex); |
181 | 0 | demux->mtu = mtu; |
182 | 0 | if (demux->require_mutex) |
183 | 0 | ossl_crypto_mutex_unlock(demux->mutex); |
184 | 0 | return 1; |
185 | 0 | } |
186 | | |
187 | | void ossl_dgram_demux_set_default_handler(DGRAM_DEMUX *demux, |
188 | | ossl_dgram_demux_cb_fn *cb, |
189 | | void *cb_arg) |
190 | 0 | { |
191 | 0 | demux->default_cb = cb; |
192 | 0 | demux->default_cb_arg = cb_arg; |
193 | 0 | } |
194 | | |
195 | | static DGRAM_URXE *dgram_demux_alloc_urxe(size_t alloc_len) |
196 | 0 | { |
197 | 0 | DGRAM_URXE *e; |
198 | |
|
199 | 0 | if (alloc_len >= SIZE_MAX - sizeof(DGRAM_URXE)) |
200 | 0 | return NULL; |
201 | | |
202 | 0 | e = OPENSSL_zalloc(sizeof(DGRAM_URXE) + alloc_len); |
203 | 0 | if (e == NULL) |
204 | 0 | return NULL; |
205 | | |
206 | 0 | ossl_list_urxe_init_elem(e); |
207 | 0 | e->alloc_len = alloc_len; |
208 | 0 | e->data_len = 0; |
209 | 0 | return e; |
210 | 0 | } |
211 | | |
212 | | static DGRAM_URXE *dgram_demux_resize_urxe(DGRAM_DEMUX *demux, DGRAM_URXE *e, |
213 | | size_t new_alloc_len) |
214 | 0 | { |
215 | 0 | DGRAM_URXE *e2, *prev; |
216 | |
|
217 | 0 | if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE)) |
218 | | /* Never attempt to resize a URXE which is not on the free list. */ |
219 | 0 | return NULL; |
220 | | |
221 | 0 | prev = ossl_list_urxe_prev(e); |
222 | 0 | ossl_list_urxe_remove(&demux->urx_free, e); |
223 | |
|
224 | 0 | if (new_alloc_len >= SIZE_MAX - sizeof(DGRAM_URXE)) |
225 | 0 | goto rollback; |
226 | | |
227 | 0 | e2 = OPENSSL_realloc(e, sizeof(DGRAM_URXE) + new_alloc_len); |
228 | | |
229 | | /* Failed to resize, abort. */ |
230 | 0 | if (e2 == NULL) |
231 | 0 | goto rollback; |
232 | | |
233 | 0 | if (prev == NULL) |
234 | 0 | ossl_list_urxe_insert_head(&demux->urx_free, e2); |
235 | 0 | else |
236 | 0 | ossl_list_urxe_insert_after(&demux->urx_free, prev, e2); |
237 | |
|
238 | 0 | e2->alloc_len = new_alloc_len; |
239 | 0 | return e2; |
240 | | |
241 | 0 | rollback: |
242 | | /* Reinsert e back into the list on failures */ |
243 | 0 | if (prev == NULL) |
244 | 0 | ossl_list_urxe_insert_head(&demux->urx_free, e); |
245 | 0 | else |
246 | 0 | ossl_list_urxe_insert_after(&demux->urx_free, prev, e); |
247 | |
|
248 | 0 | return NULL; |
249 | 0 | } |
250 | | |
251 | | static DGRAM_URXE *dgram_demux_reserve_urxe(DGRAM_DEMUX *demux, DGRAM_URXE *e, |
252 | | size_t alloc_len) |
253 | 0 | { |
254 | 0 | return e->alloc_len < alloc_len |
255 | 0 | ? dgram_demux_resize_urxe(demux, e, alloc_len) |
256 | 0 | : e; |
257 | 0 | } |
258 | | |
259 | | static int dgram_demux_ensure_free_urxe(DGRAM_DEMUX *demux, size_t min_num_free) |
260 | 0 | { |
261 | 0 | DGRAM_URXE *e; |
262 | | |
263 | | /* Caller must hold the lock */ |
264 | 0 | while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) { |
265 | 0 | e = dgram_demux_alloc_urxe(demux->mtu); |
266 | 0 | if (e == NULL) |
267 | 0 | return 0; |
268 | | |
269 | 0 | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
270 | 0 | e->demux_state = URXE_DEMUX_STATE_FREE; |
271 | 0 | } |
272 | | |
273 | 0 | return 1; |
274 | 0 | } |
275 | | |
276 | | /* |
277 | | * Receive datagrams from network, placing them into URXEs. |
278 | | * |
279 | | * Returns DGRAM_DEMUX_PUMP_RES_* value. |
280 | | * |
281 | | * Precondition: at least one URXE is free |
282 | | * Precondition: there are no pending URXEs |
283 | | * Precondition: Caller holds the demux lock |
284 | | */ |
285 | | static int dgram_demux_recv(DGRAM_DEMUX *demux) |
286 | 0 | { |
287 | 0 | BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL]; |
288 | 0 | size_t rd, i; |
289 | 0 | DGRAM_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext; |
290 | 0 | OSSL_TIME now; |
291 | | |
292 | | /* This should never be called when we have any pending URXE. */ |
293 | 0 | assert(ossl_list_urxe_head(&demux->urx_pending) == NULL); |
294 | 0 | assert(urxe->demux_state == URXE_DEMUX_STATE_FREE); |
295 | |
|
296 | 0 | if (demux->net_bio == NULL) |
297 | | /* |
298 | | * If no BIO is plugged in, treat this as no datagram being available. |
299 | | */ |
300 | 0 | return DGRAM_DEMUX_PUMP_RES_TRANSIENT_FAIL; |
301 | | |
302 | | /* |
303 | | * Opportunistically receive as many messages as possible in a single |
304 | | * syscall, determined by how many free URXEs are available. |
305 | | */ |
306 | 0 | for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg); |
307 | 0 | ++i, urxe = ossl_list_urxe_next(urxe)) { |
308 | 0 | if (urxe == NULL) { |
309 | | /* We need at least one URXE to receive into. */ |
310 | 0 | if (!ossl_assert(i > 0)) |
311 | 0 | return DGRAM_DEMUX_PUMP_RES_PERMANENT_FAIL; |
312 | | |
313 | 0 | break; |
314 | 0 | } |
315 | | |
316 | | /* Ensure the URXE is big enough. */ |
317 | 0 | urxe = dgram_demux_reserve_urxe(demux, urxe, demux->mtu); |
318 | 0 | if (urxe == NULL) |
319 | | /* Allocation error, fail. */ |
320 | 0 | return DGRAM_DEMUX_PUMP_RES_PERMANENT_FAIL; |
321 | | |
322 | | /* Ensure we zero any fields added to BIO_MSG at a later date. */ |
323 | 0 | memset(&msg[i], 0, sizeof(BIO_MSG)); |
324 | 0 | msg[i].data = ossl_dgram_urxe_data(urxe); |
325 | 0 | msg[i].data_len = urxe->alloc_len; |
326 | 0 | msg[i].peer = &urxe->peer; |
327 | 0 | BIO_ADDR_clear(&urxe->peer); |
328 | 0 | if (demux->use_local_addr) |
329 | 0 | msg[i].local = &urxe->local; |
330 | 0 | else |
331 | 0 | BIO_ADDR_clear(&urxe->local); |
332 | 0 | } |
333 | | |
334 | 0 | ERR_set_mark(); |
335 | 0 | if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) { |
336 | 0 | if (BIO_err_is_non_fatal(ERR_peek_last_error())) { |
337 | | /* Transient error, clear the error and stop. */ |
338 | 0 | ERR_pop_to_mark(); |
339 | 0 | return DGRAM_DEMUX_PUMP_RES_TRANSIENT_FAIL; |
340 | 0 | } else { |
341 | | /* Non-transient error, do not clear the error. */ |
342 | 0 | ERR_clear_last_mark(); |
343 | 0 | return DGRAM_DEMUX_PUMP_RES_PERMANENT_FAIL; |
344 | 0 | } |
345 | 0 | } |
346 | | |
347 | 0 | ERR_clear_last_mark(); |
348 | 0 | now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero(); |
349 | |
|
350 | 0 | urxe = ossl_list_urxe_head(&demux->urx_free); |
351 | 0 | for (i = 0; i < rd; ++i, urxe = unext) { |
352 | 0 | unext = ossl_list_urxe_next(urxe); |
353 | | /* Set URXE with actual length of received datagram. */ |
354 | 0 | urxe->data_len = msg[i].data_len; |
355 | | /* Time we received datagram. */ |
356 | 0 | urxe->time = now; |
357 | 0 | urxe->datagram_id = demux->next_datagram_id++; |
358 | | /* Move from free list to pending list. */ |
359 | 0 | ossl_list_urxe_remove(&demux->urx_free, urxe); |
360 | 0 | ossl_list_urxe_insert_tail(&demux->urx_pending, urxe); |
361 | 0 | urxe->demux_state = URXE_DEMUX_STATE_PENDING; |
362 | 0 | } |
363 | |
|
364 | 0 | return DGRAM_DEMUX_PUMP_RES_OK; |
365 | 0 | } |
366 | | |
367 | | /* |
368 | | * Process a single pending URXE. |
369 | | * Returning 1 on success, 0 on failure. |
370 | | * |
371 | | * Precondition: Caller holds the demux lock |
372 | | * Note: Lock is released before callback and reacquired after. |
373 | | */ |
374 | | static int dgram_demux_process_pending_urxe(DGRAM_DEMUX *demux, DGRAM_URXE *e) |
375 | 0 | { |
376 | | /* The next URXE we process should be at the head of the pending list. */ |
377 | 0 | if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending))) |
378 | 0 | return 0; |
379 | | |
380 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_PENDING); |
381 | |
|
382 | 0 | ossl_list_urxe_remove(&demux->urx_pending, e); |
383 | 0 | if (demux->default_cb != NULL) { |
384 | | /* |
385 | | * Pass to handler for routing. The URXE now belongs to the callback. |
386 | | * Release lock before callback to avoid deadlock if callback calls |
387 | | * release_urxe or reinject_urxe. |
388 | | */ |
389 | 0 | e->demux_state = URXE_DEMUX_STATE_ISSUED; |
390 | 0 | if (demux->require_mutex) |
391 | 0 | ossl_crypto_mutex_unlock(demux->mutex); |
392 | 0 | demux->default_cb(e, demux->default_cb_arg); |
393 | 0 | if (demux->require_mutex) |
394 | 0 | ossl_crypto_mutex_lock(demux->mutex); |
395 | 0 | } else { |
396 | | /* No handler, discard. */ |
397 | 0 | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
398 | 0 | e->demux_state = URXE_DEMUX_STATE_FREE; |
399 | 0 | } |
400 | |
|
401 | 0 | return 1; /* keep processing pending URXEs */ |
402 | 0 | } |
403 | | |
404 | | /* |
405 | | * Process pending URXEs to generate callbacks. |
406 | | * Precondition: Caller holds the demux lock |
407 | | */ |
408 | | static int dgram_demux_process_pending_urxl(DGRAM_DEMUX *demux) |
409 | 0 | { |
410 | 0 | DGRAM_URXE *e; |
411 | 0 | int ret; |
412 | |
|
413 | 0 | while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL) |
414 | 0 | if ((ret = dgram_demux_process_pending_urxe(demux, e)) <= 0) |
415 | 0 | return ret; |
416 | | |
417 | 0 | return 1; |
418 | 0 | } |
419 | | |
420 | | /* |
421 | | * Drain the pending URXE list, processing any pending URXEs by making their |
422 | | * callbacks. If no URXEs are pending, a network read is attempted first. |
423 | | */ |
424 | | int ossl_dgram_demux_pump(DGRAM_DEMUX *demux) |
425 | 0 | { |
426 | 0 | int ret; |
427 | |
|
428 | 0 | if (demux->require_mutex) |
429 | 0 | ossl_crypto_mutex_lock(demux->mutex); |
430 | |
|
431 | 0 | if (ossl_list_urxe_head(&demux->urx_pending) == NULL) { |
432 | 0 | if (!dgram_demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL)) { |
433 | 0 | ret = DGRAM_DEMUX_PUMP_RES_PERMANENT_FAIL; |
434 | 0 | goto end; |
435 | 0 | } |
436 | | |
437 | 0 | ret = dgram_demux_recv(demux); |
438 | 0 | if (ret != DGRAM_DEMUX_PUMP_RES_OK) |
439 | 0 | goto end; |
440 | | |
441 | | /* |
442 | | * If dgram_demux_recv returned successfully, we should always have |
443 | | * something. |
444 | | */ |
445 | 0 | assert(ossl_list_urxe_head(&demux->urx_pending) != NULL); |
446 | 0 | } |
447 | | |
448 | 0 | if (dgram_demux_process_pending_urxl(demux) <= 0) { |
449 | 0 | ret = DGRAM_DEMUX_PUMP_RES_PERMANENT_FAIL; |
450 | 0 | goto end; |
451 | 0 | } |
452 | | |
453 | 0 | ret = DGRAM_DEMUX_PUMP_RES_OK; |
454 | |
|
455 | 0 | end: |
456 | 0 | if (demux->require_mutex) |
457 | 0 | ossl_crypto_mutex_unlock(demux->mutex); |
458 | 0 | return ret; |
459 | 0 | } |
460 | | |
461 | | /* Artificially inject a packet into the demuxer for testing purposes. */ |
462 | | int ossl_dgram_demux_inject(DGRAM_DEMUX *demux, |
463 | | const unsigned char *buf, |
464 | | size_t buf_len, |
465 | | const BIO_ADDR *peer, |
466 | | const BIO_ADDR *local) |
467 | 0 | { |
468 | 0 | int ret = 0; |
469 | 0 | DGRAM_URXE *urxe; |
470 | |
|
471 | 0 | if (demux->require_mutex) |
472 | 0 | ossl_crypto_mutex_lock(demux->mutex); |
473 | |
|
474 | 0 | if (!dgram_demux_ensure_free_urxe(demux, 1)) |
475 | 0 | goto end; |
476 | | |
477 | 0 | urxe = ossl_list_urxe_head(&demux->urx_free); |
478 | |
|
479 | 0 | assert(urxe->demux_state == URXE_DEMUX_STATE_FREE); |
480 | |
|
481 | 0 | urxe = dgram_demux_reserve_urxe(demux, urxe, buf_len); |
482 | 0 | if (urxe == NULL) |
483 | 0 | goto end; |
484 | | |
485 | 0 | memcpy(ossl_dgram_urxe_data(urxe), buf, buf_len); |
486 | 0 | urxe->data_len = buf_len; |
487 | |
|
488 | 0 | if (peer != NULL) |
489 | 0 | BIO_ADDR_copy(&urxe->peer, peer); |
490 | 0 | else |
491 | 0 | BIO_ADDR_clear(&urxe->peer); |
492 | |
|
493 | 0 | if (local != NULL) |
494 | 0 | BIO_ADDR_copy(&urxe->local, local); |
495 | 0 | else |
496 | 0 | BIO_ADDR_clear(&urxe->local); |
497 | |
|
498 | 0 | urxe->time |
499 | 0 | = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero(); |
500 | | |
501 | | /* Move from free list to pending list. */ |
502 | 0 | ossl_list_urxe_remove(&demux->urx_free, urxe); |
503 | 0 | urxe->datagram_id = demux->next_datagram_id++; |
504 | 0 | ossl_list_urxe_insert_tail(&demux->urx_pending, urxe); |
505 | 0 | urxe->demux_state = URXE_DEMUX_STATE_PENDING; |
506 | |
|
507 | 0 | ret = dgram_demux_process_pending_urxl(demux) > 0; |
508 | |
|
509 | 0 | end: |
510 | 0 | if (demux->require_mutex) |
511 | 0 | ossl_crypto_mutex_unlock(demux->mutex); |
512 | 0 | return ret; |
513 | 0 | } |
514 | | |
515 | | /* Called by our user to return a URXE to the free list. */ |
516 | | void ossl_dgram_demux_release_urxe(DGRAM_DEMUX *demux, DGRAM_URXE *e) |
517 | 0 | { |
518 | 0 | assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL); |
519 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_ISSUED); |
520 | |
|
521 | 0 | if (demux->require_mutex) |
522 | 0 | ossl_crypto_mutex_lock(demux->mutex); |
523 | 0 | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
524 | 0 | e->demux_state = URXE_DEMUX_STATE_FREE; |
525 | 0 | if (demux->require_mutex) |
526 | 0 | ossl_crypto_mutex_unlock(demux->mutex); |
527 | 0 | } |
528 | | |
529 | | void ossl_dgram_demux_reinject_urxe(DGRAM_DEMUX *demux, DGRAM_URXE *e) |
530 | 0 | { |
531 | 0 | assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL); |
532 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_ISSUED); |
533 | |
|
534 | 0 | if (demux->require_mutex) |
535 | 0 | ossl_crypto_mutex_lock(demux->mutex); |
536 | 0 | ossl_list_urxe_insert_head(&demux->urx_pending, e); |
537 | 0 | e->demux_state = URXE_DEMUX_STATE_PENDING; |
538 | 0 | if (demux->require_mutex) |
539 | 0 | ossl_crypto_mutex_unlock(demux->mutex); |
540 | 0 | } |
541 | | |
542 | | int ossl_dgram_demux_has_pending(const DGRAM_DEMUX *demux) |
543 | 0 | { |
544 | | return ossl_list_urxe_head(&demux->urx_pending) != NULL; |
545 | 0 | } |
546 | | |
547 | | #endif /* !OPENSSL_NO_QUIC || !OPENSSL_NO_DTLS */ |