Mercurial > hg > nginx-quic
comparison src/event/quic/ngx_event_quic_transport.c @ 8683:de7b9af30fc6 quic
QUIC: refactored packet creation.
The "min" and "max" arguments refer to UDP datagram size. Generating payload
requires to account properly for header size, which is variable and depends on
payload size and packet number.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Thu, 07 Oct 2021 13:48:29 +0300 |
parents | 126a15530136 |
children | cab3b7a070ef |
comparison
equal
deleted
inserted
replaced
8682:126a15530136 | 8683:de7b9af30fc6 |
---|---|
92 static ngx_int_t ngx_quic_parse_long_header(ngx_quic_header_t *pkt); | 92 static ngx_int_t ngx_quic_parse_long_header(ngx_quic_header_t *pkt); |
93 static ngx_int_t ngx_quic_supported_version(uint32_t version); | 93 static ngx_int_t ngx_quic_supported_version(uint32_t version); |
94 static ngx_int_t ngx_quic_parse_long_header_v1(ngx_quic_header_t *pkt); | 94 static ngx_int_t ngx_quic_parse_long_header_v1(ngx_quic_header_t *pkt); |
95 | 95 |
96 static size_t ngx_quic_create_long_header(ngx_quic_header_t *pkt, u_char *out, | 96 static size_t ngx_quic_create_long_header(ngx_quic_header_t *pkt, u_char *out, |
97 size_t pkt_len, u_char **pnp); | 97 u_char **pnp); |
98 static size_t ngx_quic_create_short_header(ngx_quic_header_t *pkt, u_char *out, | 98 static size_t ngx_quic_create_short_header(ngx_quic_header_t *pkt, u_char *out, |
99 u_char **pnp); | 99 u_char **pnp); |
100 | 100 |
101 static ngx_int_t ngx_quic_frame_allowed(ngx_quic_header_t *pkt, | 101 static ngx_int_t ngx_quic_frame_allowed(ngx_quic_header_t *pkt, |
102 ngx_uint_t frame_type); | 102 ngx_uint_t frame_type); |
611 | 611 |
612 return p - start; | 612 return p - start; |
613 } | 613 } |
614 | 614 |
615 | 615 |
616 /* returns the amount of payload quic packet of "pkt_len" size may fit or 0 */ | |
616 size_t | 617 size_t |
617 ngx_quic_create_header(ngx_quic_header_t *pkt, u_char *out, size_t pkt_len, | 618 ngx_quic_payload_size(ngx_quic_header_t *pkt, size_t pkt_len) |
618 u_char **pnp) | 619 { |
620 size_t len; | |
621 | |
622 if ngx_quic_short_pkt(pkt->flags) { | |
623 | |
624 len = 1 + pkt->dcid.len + pkt->num_len + EVP_GCM_TLS_TAG_LEN; | |
625 if (len > pkt_len) { | |
626 return 0; | |
627 } | |
628 | |
629 return pkt_len - len; | |
630 } | |
631 | |
632 /* flags, version, dcid and scid with lengths and zero-length token */ | |
633 len = 5 + 2 + pkt->dcid.len + pkt->scid.len | |
634 + (pkt->level == ssl_encryption_initial ? 1 : 0); | |
635 | |
636 if (len > pkt_len) { | |
637 return 0; | |
638 } | |
639 | |
640 /* (pkt_len - len) is 'remainder' packet length (see RFC 9000, 17.2) */ | |
641 len += ngx_quic_varint_len(pkt_len - len) | |
642 + pkt->num_len + EVP_GCM_TLS_TAG_LEN; | |
643 | |
644 if (len > pkt_len) { | |
645 return 0; | |
646 } | |
647 | |
648 return pkt_len - len; | |
649 } | |
650 | |
651 | |
652 size_t | |
653 ngx_quic_create_header(ngx_quic_header_t *pkt, u_char *out, u_char **pnp) | |
619 { | 654 { |
620 return ngx_quic_short_pkt(pkt->flags) | 655 return ngx_quic_short_pkt(pkt->flags) |
621 ? ngx_quic_create_short_header(pkt, out, pnp) | 656 ? ngx_quic_create_short_header(pkt, out, pnp) |
622 : ngx_quic_create_long_header(pkt, out, pkt_len, pnp); | 657 : ngx_quic_create_long_header(pkt, out, pnp); |
623 } | 658 } |
624 | 659 |
625 | 660 |
626 static size_t | 661 static size_t |
627 ngx_quic_create_long_header(ngx_quic_header_t *pkt, u_char *out, | 662 ngx_quic_create_long_header(ngx_quic_header_t *pkt, u_char *out, |
628 size_t pkt_len, u_char **pnp) | 663 u_char **pnp) |
629 { | 664 { |
665 size_t rem_len; | |
630 u_char *p, *start; | 666 u_char *p, *start; |
667 | |
668 rem_len = pkt->num_len + pkt->payload.len + EVP_GCM_TLS_TAG_LEN; | |
631 | 669 |
632 if (out == NULL) { | 670 if (out == NULL) { |
633 return 5 + 2 + pkt->dcid.len + pkt->scid.len | 671 return 5 + 2 + pkt->dcid.len + pkt->scid.len |
634 + ngx_quic_varint_len(pkt_len + pkt->num_len) + pkt->num_len | 672 + ngx_quic_varint_len(rem_len) + pkt->num_len |
635 + (pkt->level == ssl_encryption_initial ? 1 : 0); | 673 + (pkt->level == ssl_encryption_initial ? 1 : 0); |
636 } | 674 } |
637 | 675 |
638 p = start = out; | 676 p = start = out; |
639 | 677 |
649 | 687 |
650 if (pkt->level == ssl_encryption_initial) { | 688 if (pkt->level == ssl_encryption_initial) { |
651 ngx_quic_build_int(&p, 0); | 689 ngx_quic_build_int(&p, 0); |
652 } | 690 } |
653 | 691 |
654 ngx_quic_build_int(&p, pkt_len + pkt->num_len); | 692 ngx_quic_build_int(&p, rem_len); |
655 | 693 |
656 *pnp = p; | 694 *pnp = p; |
657 | 695 |
658 switch (pkt->num_len) { | 696 switch (pkt->num_len) { |
659 case 1: | 697 case 1: |