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