2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/uint_set.h"
11 #include "internal/common.h"
12 #include "internal/quic_sf_list.h"
14 struct stream_frame_st {
15 struct stream_frame_st *prev, *next;
18 const unsigned char *data;
21 static void stream_frame_free(SFRAME_LIST *fl, STREAM_FRAME *sf)
23 if (fl->cleanse && sf->data != NULL)
24 OPENSSL_cleanse((unsigned char *)sf->data,
25 (size_t)(sf->range.end - sf->range.start));
26 ossl_qrx_pkt_release(sf->pkt);
30 static STREAM_FRAME *stream_frame_new(UINT_RANGE *range, OSSL_QRX_PKT *pkt,
31 const unsigned char *data)
33 STREAM_FRAME *sf = OPENSSL_zalloc(sizeof(*sf));
39 ossl_qrx_pkt_up_ref(pkt);
48 void ossl_sframe_list_init(SFRAME_LIST *fl)
50 memset(fl, 0, sizeof(*fl));
53 void ossl_sframe_list_destroy(SFRAME_LIST *fl)
55 STREAM_FRAME *sf, *next_frame;
57 for (sf = fl->head; sf != NULL; sf = next_frame) {
58 next_frame = sf->next;
59 stream_frame_free(fl, sf);
63 static int append_frame(SFRAME_LIST *fl, UINT_RANGE *range,
65 const unsigned char *data)
67 STREAM_FRAME *new_frame;
69 if ((new_frame = stream_frame_new(range, pkt, data)) == NULL)
71 new_frame->prev = fl->tail;
73 fl->tail->next = new_frame;
79 int ossl_sframe_list_insert(SFRAME_LIST *fl, UINT_RANGE *range,
81 const unsigned char *data, int fin)
83 STREAM_FRAME *sf, *new_frame, *prev_frame, *next_frame;
85 uint64_t curr_end = fl->tail != NULL ? fl->tail->range.end
88 /* This check for FINAL_SIZE_ERROR is handled by QUIC FC already */
89 assert((!fin || curr_end <= range->end)
90 && (!fl->fin || curr_end >= range->end));
93 if (fl->offset >= range->end)
96 /* nothing there yet */
97 if (fl->tail == NULL) {
98 fl->tail = fl->head = stream_frame_new(range, pkt, data);
106 /* optimize insertion at the end */
107 if (fl->tail->range.start < range->start) {
108 if (fl->tail->range.end >= range->end)
111 if (!append_frame(fl, range, pkt, data))
117 for (sf = fl->head; sf != NULL && sf->range.start < range->start;
121 if (!ossl_assert(sf != NULL))
122 /* frame list invariant broken */
125 if (prev_frame != NULL && prev_frame->range.end >= range->end)
129 * Now we must create a new frame although in the end we might drop it,
130 * because we will be potentially dropping existing overlapping frames.
132 new_frame = stream_frame_new(range, pkt, data);
133 if (new_frame == NULL)
136 for (next_frame = sf;
137 next_frame != NULL && next_frame->range.end <= range->end;) {
138 STREAM_FRAME *drop_frame = next_frame;
140 next_frame = next_frame->next;
141 if (next_frame != NULL)
142 next_frame->prev = drop_frame->prev;
143 if (prev_frame != NULL)
144 prev_frame->next = drop_frame->next;
145 if (fl->head == drop_frame)
146 fl->head = next_frame;
147 if (fl->tail == drop_frame)
148 fl->tail = prev_frame;
150 stream_frame_free(fl, drop_frame);
153 if (next_frame != NULL) {
154 /* check whether the new_frame is redundant because there is no gap */
155 if (prev_frame != NULL
156 && next_frame->range.start <= prev_frame->range.end) {
157 stream_frame_free(fl, new_frame);
160 next_frame->prev = new_frame;
162 fl->tail = new_frame;
165 new_frame->next = next_frame;
166 new_frame->prev = prev_frame;
168 if (prev_frame != NULL)
169 prev_frame->next = new_frame;
171 fl->head = new_frame;
176 fl->fin = fin || fl->fin;
181 int ossl_sframe_list_peek(const SFRAME_LIST *fl, void **iter,
182 UINT_RANGE *range, const unsigned char **data,
185 STREAM_FRAME *sf = *iter;
192 start = sf->range.end;
196 range->start = start;
198 if (sf == NULL || sf->range.start > start
199 || !ossl_assert(start < sf->range.end)) {
203 /* set fin only if we are at the end */
204 *fin = sf == NULL ? fl->fin : 0;
208 range->end = sf->range.end;
209 if (sf->data != NULL)
210 *data = sf->data + (start - sf->range.start);
213 *fin = sf->next == NULL ? fl->fin : 0;
218 int ossl_sframe_list_drop_frames(SFRAME_LIST *fl, uint64_t limit)
222 /* offset cannot move back or past the data received */
223 if (!ossl_assert(limit >= fl->offset)
224 || !ossl_assert(fl->tail == NULL
225 || limit <= fl->tail->range.end)
226 || !ossl_assert(fl->tail != NULL
227 || limit == fl->offset))
232 for (sf = fl->head; sf != NULL && sf->range.end <= limit;) {
233 STREAM_FRAME *drop_frame = sf;
237 stream_frame_free(fl, drop_frame);
251 int ossl_sframe_list_lock_head(SFRAME_LIST *fl, UINT_RANGE *range,
252 const unsigned char **data,
261 ret = ossl_sframe_list_peek(fl, &iter, range, data, fin);
267 int ossl_sframe_list_is_head_locked(SFRAME_LIST *fl)
269 return fl->head_locked;
272 int ossl_sframe_list_move_data(SFRAME_LIST *fl,
273 sframe_list_write_at_cb *write_at_cb,
276 STREAM_FRAME *sf = fl->head, *prev_frame = NULL;
277 uint64_t limit = fl->offset;
285 for (; sf != NULL; sf = sf->next) {
287 const unsigned char *data = sf->data;
289 if (limit < sf->range.start)
290 limit = sf->range.start;
293 if (limit > sf->range.start)
294 data += (size_t)(limit - sf->range.start);
295 len = (size_t)(sf->range.end - limit);
297 if (!write_at_cb(limit, data, len, cb_arg))
298 /* data did not fit */
302 OPENSSL_cleanse((unsigned char *)sf->data,
303 (size_t)(sf->range.end - sf->range.start));
305 /* release the packet */
307 ossl_qrx_pkt_release(sf->pkt);
311 limit = sf->range.end;
313 /* merge contiguous frames */
314 if (prev_frame != NULL
315 && prev_frame->range.end >= sf->range.start) {
316 prev_frame->range.end = sf->range.end;
317 prev_frame->next = sf->next;
319 if (sf->next != NULL)
320 sf->next->prev = prev_frame;
322 fl->tail = prev_frame;
325 stream_frame_free(fl, sf);