lsquic_frame_writer.c revision 19f667fb
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_frame_writer.c -- write frames to HEADERS stream.
4 *
5 * The frame is first written to list of frame_buf's (frabs) and then
6 * out to the stream.  This is done because frame's size is written out
7 * to the stream and we may not have enough room in the stream to fit
8 * the whole frame.
9 */
10
11#ifndef WIN32
12#include <arpa/inet.h>
13#endif
14#include <assert.h>
15#include <errno.h>
16#include <inttypes.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/queue.h>
20
21#include "lshpack.h"
22#include "lsquic_mm.h"
23#include "lsquic.h"
24#include "lsquic_int_types.h"
25#include "lsquic_conn.h"
26
27#include "lsquic_frame_writer.h"
28#include "lsquic_frame_common.h"
29#include "lsquic_ev_log.h"
30
31#define LSQUIC_LOGGER_MODULE LSQLM_FRAME_WRITER
32#define LSQUIC_LOG_CONN_ID lsquic_conn_id(lsquic_stream_conn(fw->fw_stream))
33#include "lsquic_logger.h"
34
35#ifndef LSQUIC_FRAB_SZ
36#   define LSQUIC_FRAB_SZ 0x1000
37#endif
38
39struct frame_buf
40{
41    TAILQ_ENTRY(frame_buf)      frab_next;
42    unsigned short              frab_size,
43                                frab_off;
44    unsigned char               frab_buf[
45                                    LSQUIC_FRAB_SZ
46                                  - sizeof(TAILQ_ENTRY(frame_buf))
47                                  - sizeof(unsigned short) * 2
48                                ];
49};
50
51#define frab_left_to_read(f) ((f)->frab_size - (f)->frab_off)
52#define frab_left_to_write(f) ((unsigned short) sizeof((f)->frab_buf) - (f)->frab_size)
53#define frab_write_to(f) ((f)->frab_buf + (f)->frab_size)
54
55/* Make sure that frab_buf is at least five bytes long, otherwise a frame
56 * won't fit into two adjacent frabs.
57 */
58typedef char three_byte_frab_buf[(sizeof(((struct frame_buf *)0)->frab_buf) >= 5) ?1 : - 1];
59
60
61TAILQ_HEAD(frame_buf_head, frame_buf);
62
63
64struct lsquic_frame_writer
65{
66    struct lsquic_stream       *fw_stream;
67    fw_write_f                  fw_write;
68    struct lsquic_mm           *fw_mm;
69    struct lshpack_enc         *fw_henc;
70#if LSQUIC_CONN_STATS
71    struct conn_stats          *fw_conn_stats;
72#endif
73    struct frame_buf_head       fw_frabs;
74    unsigned                    fw_max_frame_sz;
75    uint32_t                    fw_max_header_list_sz;  /* 0 means unlimited */
76    enum {
77        FW_SERVER   = (1 << 0),
78    }                           fw_flags;
79};
80
81
82/* RFC 7540, Section 4.2 */
83#define MIN_MAX_FRAME_SIZE  (1 << 14)
84#define MAX_MAX_FRAME_SIZE ((1 << 24) - 1)
85
86#define MAX(a, b) ((a) > (b) ? (a) : (b))
87#define SETTINGS_FRAME_SZ 6
88#define ABS_MIN_FRAME_SIZE MAX(SETTINGS_FRAME_SZ, \
89                                            sizeof(struct http_prio_frame))
90
91struct lsquic_frame_writer *
92lsquic_frame_writer_new (struct lsquic_mm *mm, struct lsquic_stream *stream,
93     unsigned max_frame_sz, struct lshpack_enc *henc, fw_write_f write,
94#if LSQUIC_CONN_STATS
95     struct conn_stats *conn_stats,
96#endif
97     int is_server)
98{
99    struct lsquic_frame_writer *fw;
100
101    /* When frame writer is instantiated, limit the maximum size to
102     * MIN_MAX_FRAME_SIZE.  The reference implementation has this value
103     * hardcoded and QUIC does not provide a mechanism to advertise a
104     * different value.
105     */
106    if (0 == max_frame_sz)
107        max_frame_sz = MIN_MAX_FRAME_SIZE;
108    else
109        LSQ_LOG1(LSQ_LOG_WARN, "max frame size specified to be %u bytes "
110            "-- this better be test code!", max_frame_sz);
111
112    if (!is_server && max_frame_sz < ABS_MIN_FRAME_SIZE)
113    {
114        LSQ_LOG1(LSQ_LOG_ERROR, "max frame size must be at least %zd bytes, "
115            "which is the size of priority information that client always "
116            "writes", ABS_MIN_FRAME_SIZE);
117        return NULL;
118    }
119
120    fw = malloc(sizeof(*fw));
121    if (!fw)
122        return NULL;
123
124    fw->fw_mm           = mm;
125    fw->fw_henc         = henc;
126    fw->fw_stream       = stream;
127    fw->fw_write        = write;
128    fw->fw_max_frame_sz = max_frame_sz;
129    fw->fw_max_header_list_sz = 0;
130    if (is_server)
131        fw->fw_flags    = FW_SERVER;
132    else
133        fw->fw_flags    = 0;
134    TAILQ_INIT(&fw->fw_frabs);
135#if LSQUIC_CONN_STATS
136    fw->fw_conn_stats   = conn_stats;
137#endif
138    return fw;
139}
140
141
142void
143lsquic_frame_writer_destroy (struct lsquic_frame_writer *fw)
144{
145    struct frame_buf *frab;
146    while ((frab = TAILQ_FIRST(&fw->fw_frabs)))
147    {
148        TAILQ_REMOVE(&fw->fw_frabs, frab, frab_next);
149        lsquic_mm_put_4k(fw->fw_mm, frab);
150    }
151    free(fw);
152}
153
154
155static struct frame_buf *
156fw_get_frab (struct lsquic_frame_writer *fw)
157{
158    struct frame_buf *frab;
159    frab = lsquic_mm_get_4k(fw->fw_mm);
160    if (frab)
161        memset(frab, 0, offsetof(struct frame_buf, frab_buf));
162    return frab;
163}
164
165
166static void
167fw_put_frab (struct lsquic_frame_writer *fw, struct frame_buf *frab)
168{
169    TAILQ_REMOVE(&fw->fw_frabs, frab, frab_next);
170    lsquic_mm_put_4k(fw->fw_mm, frab);
171}
172
173
174static int
175fw_write_to_frab (struct lsquic_frame_writer *fw, const void *buf, size_t bufsz)
176{
177    const unsigned char *p = buf;
178    const unsigned char *const end = p + bufsz;
179    struct frame_buf *frab;
180    unsigned ntowrite;
181
182    while (p < end)
183    {
184        frab = TAILQ_LAST(&fw->fw_frabs, frame_buf_head);
185        if (!(frab && (ntowrite = frab_left_to_write(frab)) > 0))
186        {
187            frab = fw_get_frab(fw);
188            if (!frab)
189                return -1;
190            TAILQ_INSERT_TAIL(&fw->fw_frabs, frab, frab_next);
191            ntowrite = frab_left_to_write(frab);
192        }
193        if (ntowrite > bufsz)
194            ntowrite = bufsz;
195        memcpy(frab_write_to(frab), p, ntowrite);
196        p += ntowrite;
197        bufsz -= ntowrite;
198        frab->frab_size += ntowrite;
199    }
200
201    return 0;
202}
203
204
205int
206lsquic_frame_writer_have_leftovers (const struct lsquic_frame_writer *fw)
207{
208    return !TAILQ_EMPTY(&fw->fw_frabs);
209}
210
211
212int
213lsquic_frame_writer_flush (struct lsquic_frame_writer *fw)
214{
215    struct frame_buf *frab;
216
217    while ((frab = TAILQ_FIRST(&fw->fw_frabs)))
218    {
219        size_t ntowrite = frab_left_to_read(frab);
220        ssize_t nw = fw->fw_write(fw->fw_stream,
221                            frab->frab_buf + frab->frab_off, ntowrite);
222        if (nw > 0)
223        {
224            frab->frab_off += nw;
225            if (frab->frab_off == frab->frab_size)
226            {
227                TAILQ_REMOVE(&fw->fw_frabs, frab, frab_next);
228                fw_put_frab(fw, frab);
229            }
230        }
231        else if (nw == 0)
232            break;
233        else
234            return -1;
235    }
236
237    return 0;
238}
239
240
241struct header_framer_ctx
242{
243    struct lsquic_frame_writer
244               *hfc_fw;
245    struct {
246        struct frame_buf   *frab;
247        unsigned short      off;
248    }           hfc_header_ptr;     /* Points to byte *after* current frame header */
249    unsigned    hfc_max_frame_sz;   /* Maximum frame size.  We always fill it. */
250    unsigned    hfc_cur_sz;         /* Number of bytes in the current frame. */
251    unsigned    hfc_n_frames;       /* Number of frames written. */
252    uint32_t    hfc_stream_id;      /* Stream ID */
253    enum http_frame_header_flags
254                hfc_first_flags;
255    enum http_frame_type
256                hfc_frame_type;
257};
258
259
260static void
261hfc_init (struct header_framer_ctx *hfc, struct lsquic_frame_writer *fw,
262          unsigned max_frame_sz, enum http_frame_type frame_type,
263          uint32_t stream_id, enum http_frame_header_flags first_flags)
264{
265    memset(hfc, 0, sizeof(*hfc));
266    hfc->hfc_fw           = fw;
267    hfc->hfc_frame_type   = frame_type;
268    hfc->hfc_stream_id    = stream_id;
269    hfc->hfc_first_flags  = first_flags;
270    hfc->hfc_max_frame_sz = max_frame_sz;
271    hfc->hfc_cur_sz       = max_frame_sz;
272}
273
274
275static void
276hfc_save_ptr (struct header_framer_ctx *hfc)
277{
278    hfc->hfc_header_ptr.frab = TAILQ_LAST(&hfc->hfc_fw->fw_frabs, frame_buf_head);
279    hfc->hfc_header_ptr.off = hfc->hfc_header_ptr.frab->frab_size;
280}
281
282
283static void
284hfc_terminate_frame (struct header_framer_ctx *hfc,
285                     enum http_frame_header_flags flags)
286{
287    union {
288        struct http_frame_header fh;
289        unsigned char            buf[ sizeof(struct http_frame_header) ];
290    } u;
291    uint32_t stream_id;
292    struct frame_buf *frab;
293
294    /* Construct the frame */
295    u.fh.hfh_length[0] = hfc->hfc_cur_sz >> 16;
296    u.fh.hfh_length[1] = hfc->hfc_cur_sz >> 8;
297    u.fh.hfh_length[2] = hfc->hfc_cur_sz;
298    u.fh.hfh_flags     = flags;
299    if (1 == hfc->hfc_n_frames)
300    {
301        u.fh.hfh_type  = hfc->hfc_frame_type;
302        u.fh.hfh_flags |= hfc->hfc_first_flags;
303    }
304    else
305        u.fh.hfh_type  = HTTP_FRAME_CONTINUATION;
306    stream_id = htonl(hfc->hfc_stream_id);
307    memcpy(u.fh.hfh_stream_id, &stream_id, sizeof(stream_id));
308
309    if (hfc->hfc_header_ptr.off >= sizeof(u.fh))
310    {   /* Write in a single chunk */
311        assert(0 == memcmp("123456789", hfc->hfc_header_ptr.frab->frab_buf +
312                    hfc->hfc_header_ptr.off - sizeof(u.buf), sizeof(u.buf)));
313        memcpy(hfc->hfc_header_ptr.frab->frab_buf + hfc->hfc_header_ptr.off -
314                    sizeof(u.buf), u.buf, sizeof(u.buf));
315    }
316    else
317    {   /* Write across frab boundary */
318        memcpy(hfc->hfc_header_ptr.frab->frab_buf,
319            u.buf + sizeof(u.buf) - hfc->hfc_header_ptr.off,
320            hfc->hfc_header_ptr.off);
321        frab = TAILQ_PREV(hfc->hfc_header_ptr.frab, frame_buf_head, frab_next);
322        memcpy(frab->frab_buf + frab->frab_size - sizeof(u.buf) +
323            hfc->hfc_header_ptr.off, u.buf,
324            sizeof(u.buf) - hfc->hfc_header_ptr.off);
325    }
326}
327
328
329static int
330hfc_write (struct header_framer_ctx *hfc, const void *buf, size_t sz)
331{
332    const unsigned char *p = buf;
333    unsigned avail;
334    int s;
335
336    while (sz > 0)
337    {
338        if (hfc->hfc_max_frame_sz == hfc->hfc_cur_sz)
339        {
340            if (hfc->hfc_n_frames > 0)
341                hfc_terminate_frame(hfc, 0);
342            s = fw_write_to_frab(hfc->hfc_fw, "123456789",
343                                        sizeof(struct http_frame_header));
344            if (s < 0)
345                return s;
346            ++hfc->hfc_n_frames;
347            hfc_save_ptr(hfc);
348            hfc->hfc_cur_sz = 0;
349        }
350
351        avail = hfc->hfc_max_frame_sz - hfc->hfc_cur_sz;
352        if (sz < avail)
353            avail = sz;
354        if (avail)
355        {
356            s = fw_write_to_frab(hfc->hfc_fw, p, avail);
357            if (s < 0)
358                return s;
359            hfc->hfc_cur_sz += avail;
360            sz -= avail;
361            p += avail;
362        }
363    }
364
365    return 0;
366}
367
368
369static unsigned
370count_uppercase (const unsigned char *buf, size_t sz)
371{
372    static const unsigned char uppercase[0x100] = {
373        ['A'] = 1, ['B'] = 1, ['C'] = 1, ['D'] = 1, ['E'] = 1, ['F'] = 1,
374        ['G'] = 1, ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, ['L'] = 1,
375        ['M'] = 1, ['N'] = 1, ['O'] = 1, ['P'] = 1, ['Q'] = 1, ['R'] = 1,
376        ['S'] = 1, ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, ['X'] = 1,
377        ['Y'] = 1, ['Z'] = 1,
378    };
379    unsigned n_uppercase, i;
380    n_uppercase = 0;
381    for (i = 0; i < sz; ++i)
382        n_uppercase += uppercase[ buf[i] ];
383    return n_uppercase;
384}
385
386
387static uint32_t
388calc_headers_size (const struct lsquic_http_headers *headers)
389{
390    int i;
391    uint32_t size = 0;
392    for (i = 0; i < headers->count; ++i)
393        size += 32 + headers->headers[i].name.iov_len +
394                     headers->headers[i].value.iov_len;
395    return size;
396}
397
398
399static int
400have_oversize_strings (const struct lsquic_http_headers *headers)
401{
402    int i, have;
403    for (i = 0, have = 0; i < headers->count; ++i)
404    {
405        have |= headers->headers[i].name.iov_len  > LSHPACK_MAX_STRLEN;
406        have |= headers->headers[i].value.iov_len > LSHPACK_MAX_STRLEN;
407    }
408    return have;
409}
410
411
412static int
413check_headers_size (const struct lsquic_frame_writer *fw,
414                    const struct lsquic_http_headers *headers,
415                    const struct lsquic_http_headers *extra_headers)
416{
417    uint32_t headers_sz;
418    headers_sz = calc_headers_size(headers);
419    if (extra_headers)
420        headers_sz += calc_headers_size(extra_headers);
421    if (headers_sz > fw->fw_max_header_list_sz)
422    {
423        LSQ_INFO("Headers size %u is larger than max allowed (%u)",
424            headers_sz, fw->fw_max_header_list_sz);
425        errno = EMSGSIZE;
426        return -1;
427    }
428    return 0;
429}
430
431
432static int
433check_headers_case (const struct lsquic_frame_writer *fw,
434                    const struct lsquic_http_headers *headers)
435{
436    unsigned n_uppercase;
437    int i;
438    n_uppercase = 0;
439    for (i = 0; i < headers->count; ++i)
440        n_uppercase += count_uppercase(headers->headers[i].name.iov_base,
441                                        headers->headers[i].name.iov_len);
442    if (n_uppercase)
443    {
444        LSQ_INFO("Uppercase letters in header names");
445        errno = EINVAL;
446        return -1;
447    }
448    return 0;
449}
450
451
452static int
453write_headers (struct lsquic_frame_writer *fw,
454               const struct lsquic_http_headers *headers,
455               struct header_framer_ctx *hfc, unsigned char *buf,
456               const unsigned buf_sz)
457{
458    unsigned char *end;
459    int i, s;
460
461    for (i = 0; i < headers->count; ++i)
462    {
463        end = lshpack_enc_encode(fw->fw_henc, buf, buf + buf_sz,
464                                 LSHPACK_HDR_UNKNOWN,
465                                 (const lshpack_header_t *)&headers->headers[i],
466                                 0);
467        if (end > buf)
468        {
469            s = hfc_write(hfc, buf, end - buf);
470            if (s < 0)
471                return s;
472#if LSQUIC_CONN_STATS
473            fw->fw_conn_stats->out.headers_uncomp +=
474                headers->headers[i].name.iov_len
475                    + headers->headers[i].value.iov_len;
476            fw->fw_conn_stats->out.headers_comp += end - buf;
477#endif
478        }
479        else
480        {
481            LSQ_WARN("error encoding header");
482            errno = EBADMSG;
483            return -1;
484        }
485    }
486
487    return 0;
488}
489
490
491int
492lsquic_frame_writer_write_headers (struct lsquic_frame_writer *fw,
493                                   uint32_t stream_id,
494                                   const struct lsquic_http_headers *headers,
495                                   int eos, unsigned weight)
496{
497    struct header_framer_ctx hfc;
498    int s;
499    struct http_prio_frame prio_frame;
500    enum http_frame_header_flags flags;
501    unsigned char *buf;
502
503    /* Internal function: weight must be valid here */
504    assert(weight >= 1 && weight <= 256);
505
506    if (fw->fw_max_header_list_sz && 0 != check_headers_size(fw, headers, NULL))
507        return -1;
508
509    if (0 != check_headers_case(fw, headers))
510        return -1;
511
512    if (have_oversize_strings(headers))
513        return -1;
514
515    if (eos)
516        flags = HFHF_END_STREAM;
517    else
518        flags = 0;
519
520    if (!(fw->fw_flags & FW_SERVER))
521        flags |= HFHF_PRIORITY;
522
523    hfc_init(&hfc, fw, fw->fw_max_frame_sz, HTTP_FRAME_HEADERS, stream_id,
524                                                                        flags);
525
526    if (!(fw->fw_flags & FW_SERVER))
527    {
528        memset(&prio_frame.hpf_stream_id, 0, sizeof(prio_frame.hpf_stream_id));
529        prio_frame.hpf_weight = weight - 1;
530        s = hfc_write(&hfc, &prio_frame, sizeof(struct http_prio_frame));
531        if (s < 0)
532            return s;
533    }
534
535    buf = malloc(MAX_HEADERS_SIZE);
536    if (!buf)
537        return -1;
538    s = write_headers(fw, headers, &hfc, buf, MAX_HEADERS_SIZE);
539    free(buf);
540    if (0 == s)
541    {
542        EV_LOG_GENERATED_HTTP_HEADERS(LSQUIC_LOG_CONN_ID, stream_id,
543                            fw->fw_flags & FW_SERVER, &prio_frame, headers);
544        hfc_terminate_frame(&hfc, HFHF_END_HEADERS);
545        return lsquic_frame_writer_flush(fw);
546    }
547    else
548        return s;
549}
550
551
552int
553lsquic_frame_writer_write_promise (struct lsquic_frame_writer *fw,
554                           uint32_t stream_id, uint32_t promised_stream_id,
555                           const struct iovec *path, const struct iovec *host,
556                           const struct lsquic_http_headers *extra_headers)
557{
558    struct header_framer_ctx hfc;
559    struct http_push_promise_frame push_frame;
560    lsquic_http_header_t mpas_headers[4];
561    struct lsquic_http_headers mpas = {    /* method, path, authority, scheme */
562        .headers = mpas_headers,
563        .count   = 4,
564    };
565    unsigned char *buf;
566    int s;
567
568    mpas_headers[0].name. iov_base    = ":method";
569    mpas_headers[0].name. iov_len     = 7;
570    mpas_headers[0].value.iov_base    = "GET";
571    mpas_headers[0].value.iov_len     = 3;
572    mpas_headers[1].name .iov_base    = ":path";
573    mpas_headers[1].name .iov_len     = 5;
574    mpas_headers[1].value             = *path;
575    mpas_headers[2].name .iov_base    = ":authority";
576    mpas_headers[2].name .iov_len     = 10;
577    mpas_headers[2].value             = *host;
578    mpas_headers[3].name. iov_base    = ":scheme";
579    mpas_headers[3].name. iov_len     = 7;
580    mpas_headers[3].value.iov_base    = "https";
581    mpas_headers[3].value.iov_len     = 5;
582
583    if (fw->fw_max_header_list_sz &&
584                    0 != check_headers_size(fw, &mpas, extra_headers))
585        return -1;
586
587    if (extra_headers && 0 != check_headers_case(fw, extra_headers))
588        return -1;
589
590    if (have_oversize_strings(&mpas))
591        return -1;
592
593    if (extra_headers && have_oversize_strings(extra_headers))
594        return -1;
595
596    hfc_init(&hfc, fw, fw->fw_max_frame_sz, HTTP_FRAME_PUSH_PROMISE,
597                                                            stream_id, 0);
598
599    promised_stream_id = htonl(promised_stream_id);
600    memcpy(push_frame.hppf_promised_id, &promised_stream_id, 4);
601    s = hfc_write(&hfc, &push_frame, sizeof(struct http_push_promise_frame));
602    if (s < 0)
603        return s;
604
605    buf = malloc(MAX_HEADERS_SIZE);
606    if (!buf)
607        return -1;
608
609    s = write_headers(fw, &mpas, &hfc, buf, MAX_HEADERS_SIZE);
610    if (s != 0)
611    {
612        free(buf);
613        return -1;
614    }
615
616    if (extra_headers)
617        s = write_headers(fw, extra_headers, &hfc, buf, MAX_HEADERS_SIZE);
618
619    free(buf);
620
621    if (0 == s)
622    {
623        EV_LOG_GENERATED_HTTP_PUSH_PROMISE(LSQUIC_LOG_CONN_ID, stream_id,
624                            htonl(promised_stream_id), &mpas, extra_headers);
625        hfc_terminate_frame(&hfc, HFHF_END_HEADERS);
626        return lsquic_frame_writer_flush(fw);
627    }
628    else
629        return -1;
630}
631
632
633void
634lsquic_frame_writer_max_header_list_size (struct lsquic_frame_writer *fw,
635                                          uint32_t max_size)
636{
637    LSQ_DEBUG("set max_header_list_sz to %u", max_size);
638    fw->fw_max_header_list_sz = max_size;
639}
640
641
642static int
643write_settings (struct lsquic_frame_writer *fw,
644    const struct lsquic_http2_setting *settings, unsigned n_settings)
645{
646    struct http_frame_header fh;
647    unsigned payload_length;
648    uint32_t val;
649    uint16_t id;
650    int s;
651
652    payload_length = n_settings * 6;
653
654    memset(&fh, 0, sizeof(fh));
655    fh.hfh_type  = HTTP_FRAME_SETTINGS;
656    fh.hfh_length[0] = payload_length >> 16;
657    fh.hfh_length[1] = payload_length >> 8;
658    fh.hfh_length[2] = payload_length;
659
660    s = fw_write_to_frab(fw, &fh, sizeof(fh));
661    if (s != 0)
662        return s;
663
664    do
665    {
666        id  = htons(settings->id);
667        val = htonl(settings->value);
668        if (0 != (s = fw_write_to_frab(fw, &id, sizeof(id))) ||
669            0 != (s = fw_write_to_frab(fw, &val, sizeof(val))))
670            return s;
671        EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "wrote HTTP SETTINGS frame: "
672            "%s=%"PRIu32, lsquic_http_setting_id2str(settings->id),
673            settings->value);
674        ++settings;
675    }
676    while (--n_settings);
677
678    return 0;
679}
680
681
682int
683lsquic_frame_writer_write_settings (struct lsquic_frame_writer *fw,
684    const struct lsquic_http2_setting *settings, unsigned n_settings)
685{
686    unsigned settings_per_frame;
687    unsigned n;
688
689    if (0 == n_settings)
690    {
691        errno = EINVAL;
692        return -1;
693    }
694
695    settings_per_frame = fw->fw_max_frame_sz / SETTINGS_FRAME_SZ;
696    n = 0;
697
698    do {
699        if (settings_per_frame > n_settings - n)
700            settings_per_frame = n_settings - n;
701        if (0 != write_settings(fw, &settings[n], settings_per_frame))
702            return -1;
703        n += settings_per_frame;
704    } while (n < n_settings);
705
706    return lsquic_frame_writer_flush(fw);
707}
708
709
710int
711lsquic_frame_writer_write_priority (struct lsquic_frame_writer *fw,
712            uint32_t stream_id, int exclusive, uint32_t stream_dep_id,
713            unsigned weight)
714{
715    unsigned char buf[ sizeof(struct http_frame_header) +
716                                        sizeof(struct http_prio_frame) ];
717    struct http_frame_header *fh         = (void *) &buf[0];
718    struct http_prio_frame   *prio_frame = (void *) &buf[sizeof(*fh)];
719    int s;
720
721    if (stream_dep_id & (1UL << 31))
722    {
723        LSQ_WARN("stream ID too high (%u): cannot write PRIORITY frame",
724            stream_dep_id);
725        return -1;
726    }
727
728    if (weight < 1 || weight > 256)
729        return -1;
730
731    memset(fh, 0, sizeof(*fh));
732    fh->hfh_type      = HTTP_FRAME_PRIORITY;
733    fh->hfh_length[2] = sizeof(struct http_prio_frame);
734    stream_id = htonl(stream_id);
735    memcpy(fh->hfh_stream_id, &stream_id, 4);
736
737    stream_dep_id |= !!exclusive << 31;
738    stream_id = htonl(stream_dep_id);
739    memcpy(prio_frame->hpf_stream_id, &stream_id, 4);
740    prio_frame->hpf_weight = weight - 1;
741
742    s = fw_write_to_frab(fw, buf, sizeof(buf));
743    if (s != 0)
744        return s;
745
746    EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "wrote HTTP PRIORITY frame: "
747        "stream %"PRIu32"; weight: %u; exclusive: %d",
748        htonl(stream_id), weight, !!exclusive);
749
750    return lsquic_frame_writer_flush(fw);
751}
752
753
754size_t
755lsquic_frame_writer_mem_used (const struct lsquic_frame_writer *fw)
756{
757    const struct frame_buf *frab;
758    size_t size;
759
760    size = sizeof(*fw);
761    TAILQ_FOREACH(frab, &fw->fw_frabs, frab_next)
762        size += sizeof(*frab);
763
764    return size;
765}
766