raiha 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE +202 -0
- data/README.md +94 -22
- data/docs/configuration.md +227 -0
- data/docs/development.md +99 -0
- data/docs/fiber-scheduler.md +591 -0
- data/docs/getting-started.md +190 -0
- data/docs/http3-client.md +303 -0
- data/docs/http3-server.md +301 -0
- data/docs/platforms.md +98 -0
- data/docs/qlog.md +86 -0
- data/docs/quic.md +268 -0
- data/docs/threads.md +127 -0
- data/examples/_certificate.rb +55 -0
- data/examples/high_level_loopback.rb +59 -0
- data/examples/http3_get_public.rb +68 -0
- data/examples/in_memory_handshake.rb +38 -0
- data/examples/qlog_output.rb +58 -0
- data/examples/qlog_roundtrips.rb +66 -0
- data/examples/quic_gem_loopback.rb +62 -0
- data/examples/stream_echo.rb +68 -0
- data/examples/udp_handshake.rb +76 -0
- data/lib/raiha/http3/action.rb +21 -0
- data/lib/raiha/http3/capsule.rb +86 -0
- data/lib/raiha/http3/connection.rb +983 -0
- data/lib/raiha/http3/error.rb +24 -0
- data/lib/raiha/http3/error_code.rb +27 -0
- data/lib/raiha/http3/event.rb +63 -0
- data/lib/raiha/http3/frame.rb +148 -0
- data/lib/raiha/http3/frame_reader.rb +114 -0
- data/lib/raiha/http3/message.rb +137 -0
- data/lib/raiha/http3/output.rb +61 -0
- data/lib/raiha/http3/priority.rb +52 -0
- data/lib/raiha/http3/qlog.rb +145 -0
- data/lib/raiha/http3/qpack/decoder.rb +302 -0
- data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
- data/lib/raiha/http3/qpack/encoder.rb +94 -0
- data/lib/raiha/http3/qpack/error.rb +39 -0
- data/lib/raiha/http3/qpack/error_code.rb +13 -0
- data/lib/raiha/http3/qpack/field.rb +15 -0
- data/lib/raiha/http3/qpack/huffman.rb +397 -0
- data/lib/raiha/http3/qpack/reader.rb +82 -0
- data/lib/raiha/http3/qpack/result.rb +26 -0
- data/lib/raiha/http3/qpack/static_table.rb +139 -0
- data/lib/raiha/http3/qpack/writer.rb +44 -0
- data/lib/raiha/http3/qpack.rb +26 -0
- data/lib/raiha/http3/receiving.rb +46 -0
- data/lib/raiha/http3/sending.rb +23 -0
- data/lib/raiha/http3/session.rb +31 -0
- data/lib/raiha/http3/settings.rb +81 -0
- data/lib/raiha/http3/structured_field.rb +120 -0
- data/lib/raiha/http3/tracer.rb +26 -0
- data/lib/raiha/http3/varint.rb +45 -0
- data/lib/raiha/http3.rb +19 -0
- data/lib/raiha/http3_binding.rb +139 -0
- data/lib/raiha/namespace.rb +11 -0
- data/lib/raiha/quic/config.rb +229 -0
- data/lib/raiha/quic/congestion/algorithms.rb +17 -0
- data/lib/raiha/quic/congestion/cubic.rb +258 -0
- data/lib/raiha/quic/congestion/new_reno.rb +161 -0
- data/lib/raiha/quic/congestion/pacing.rb +21 -0
- data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
- data/lib/raiha/quic/congestion/startup.rb +13 -0
- data/lib/raiha/quic/congestion.rb +15 -0
- data/lib/raiha/quic/connection.rb +2022 -0
- data/lib/raiha/quic/connection_ids.rb +62 -0
- data/lib/raiha/quic/crypto/aead.rb +68 -0
- data/lib/raiha/quic/crypto/header_protection.rb +42 -0
- data/lib/raiha/quic/crypto/initial.rb +23 -0
- data/lib/raiha/quic/crypto/keys.rb +32 -0
- data/lib/raiha/quic/crypto/mac.rb +15 -0
- data/lib/raiha/quic/crypto/random.rb +13 -0
- data/lib/raiha/quic/crypto.rb +18 -0
- data/lib/raiha/quic/crypto_stream.rb +161 -0
- data/lib/raiha/quic/datagrams.rb +91 -0
- data/lib/raiha/quic/endpoint.rb +785 -0
- data/lib/raiha/quic/error.rb +61 -0
- data/lib/raiha/quic/error_code.rb +34 -0
- data/lib/raiha/quic/event.rb +59 -0
- data/lib/raiha/quic/issued_connection_ids.rb +137 -0
- data/lib/raiha/quic/kept_packets.rb +41 -0
- data/lib/raiha/quic/mtu_search.rb +75 -0
- data/lib/raiha/quic/negotiation.rb +143 -0
- data/lib/raiha/quic/new_token.rb +55 -0
- data/lib/raiha/quic/pacer.rb +52 -0
- data/lib/raiha/quic/packet_protection.rb +310 -0
- data/lib/raiha/quic/path.rb +473 -0
- data/lib/raiha/quic/peer_connection_ids.rb +160 -0
- data/lib/raiha/quic/qlog.rb +260 -0
- data/lib/raiha/quic/ranges.rb +32 -0
- data/lib/raiha/quic/recovery/duty.rb +102 -0
- data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
- data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
- data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
- data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
- data/lib/raiha/quic/recovery.rb +641 -0
- data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
- data/lib/raiha/quic/retry_token.rb +54 -0
- data/lib/raiha/quic/route.rb +37 -0
- data/lib/raiha/quic/scheduler.rb +112 -0
- data/lib/raiha/quic/session.rb +47 -0
- data/lib/raiha/quic/stateless_reset.rb +38 -0
- data/lib/raiha/quic/stats.rb +24 -0
- data/lib/raiha/quic/streams/receive_window.rb +119 -0
- data/lib/raiha/quic/streams/receiver.rb +217 -0
- data/lib/raiha/quic/streams/sender.rb +200 -0
- data/lib/raiha/quic/streams.rb +687 -0
- data/lib/raiha/quic/test/bottleneck.rb +55 -0
- data/lib/raiha/quic/test/congestion_contract.rb +106 -0
- data/lib/raiha/quic/test/network.rb +244 -0
- data/lib/raiha/quic/test/random_loss.rb +34 -0
- data/lib/raiha/quic/test/reorder.rb +51 -0
- data/lib/raiha/quic/test/server.rb +54 -0
- data/lib/raiha/quic/test/vectors.rb +337 -0
- data/lib/raiha/quic/test/virtual_clock.rb +21 -0
- data/lib/raiha/quic/test.rb +17 -0
- data/lib/raiha/quic/tls_binding.rb +179 -0
- data/lib/raiha/quic/token.rb +28 -0
- data/lib/raiha/quic/tracer.rb +127 -0
- data/lib/raiha/quic/transmit.rb +14 -0
- data/lib/raiha/quic/transport_parameters.rb +244 -0
- data/lib/raiha/quic/version.rb +63 -0
- data/lib/raiha/quic/wire/codec.rb +136 -0
- data/lib/raiha/quic/wire/frame.rb +521 -0
- data/lib/raiha/quic/wire/header.rb +90 -0
- data/lib/raiha/quic/wire/packet.rb +130 -0
- data/lib/raiha/quic/wire/packet_number.rb +47 -0
- data/lib/raiha/quic/wire/retry.rb +62 -0
- data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
- data/lib/raiha/quic/wire.rb +9 -0
- data/lib/raiha/quic.rb +46 -0
- data/lib/raiha/runtime/addresses.rb +35 -0
- data/lib/raiha/runtime/admission.rb +37 -0
- data/lib/raiha/runtime/client.rb +190 -0
- data/lib/raiha/runtime/driver.rb +165 -0
- data/lib/raiha/runtime/error.rb +16 -0
- data/lib/raiha/runtime/http3.rb +363 -0
- data/lib/raiha/runtime/http3_fields.rb +38 -0
- data/lib/raiha/runtime/http3_server.rb +416 -0
- data/lib/raiha/runtime/kept.rb +36 -0
- data/lib/raiha/runtime/qlog.rb +204 -0
- data/lib/raiha/runtime/rack.rb +180 -0
- data/lib/raiha/runtime/server.rb +334 -0
- data/lib/raiha/runtime/session.rb +447 -0
- data/lib/raiha/runtime/threaded/client.rb +161 -0
- data/lib/raiha/runtime/threaded/driver.rb +230 -0
- data/lib/raiha/runtime/threaded/http3.rb +287 -0
- data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
- data/lib/raiha/runtime/threaded/server.rb +290 -0
- data/lib/raiha/runtime/threaded/session.rb +604 -0
- data/lib/raiha/runtime/threaded.rb +45 -0
- data/lib/raiha/runtime/udp.rb +410 -0
- data/lib/raiha/runtime/worker.rb +138 -0
- data/lib/raiha/runtime/workers.rb +248 -0
- data/lib/raiha/runtime.rb +37 -0
- data/lib/raiha/tls/anti_replay.rb +139 -0
- data/lib/raiha/tls/cipher_suite.rb +62 -0
- data/lib/raiha/tls/codec/error.rb +43 -0
- data/lib/raiha/tls/codec/reader.rb +95 -0
- data/lib/raiha/tls/codec/writer.rb +81 -0
- data/lib/raiha/tls/codec.rb +5 -0
- data/lib/raiha/tls/config.rb +318 -0
- data/lib/raiha/tls/crypto/aead.rb +64 -0
- data/lib/raiha/tls/crypto/certificate.rb +28 -0
- data/lib/raiha/tls/crypto/digest.rb +30 -0
- data/lib/raiha/tls/crypto/ecdh.rb +74 -0
- data/lib/raiha/tls/crypto/hash.rb +22 -0
- data/lib/raiha/tls/crypto/hkdf.rb +28 -0
- data/lib/raiha/tls/crypto/signature.rb +87 -0
- data/lib/raiha/tls/crypto.rb +19 -0
- data/lib/raiha/tls/error.rb +36 -0
- data/lib/raiha/tls/event.rb +45 -0
- data/lib/raiha/tls/extension/alpn.rb +35 -0
- data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
- data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
- data/lib/raiha/tls/extension/cookie.rb +27 -0
- data/lib/raiha/tls/extension/early_data.rb +54 -0
- data/lib/raiha/tls/extension/key_share.rb +90 -0
- data/lib/raiha/tls/extension/list.rb +49 -0
- data/lib/raiha/tls/extension/opaque.rb +21 -0
- data/lib/raiha/tls/extension/placement.rb +55 -0
- data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
- data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
- data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
- data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
- data/lib/raiha/tls/extension/registry.rb +50 -0
- data/lib/raiha/tls/extension/server_name.rb +63 -0
- data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
- data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
- data/lib/raiha/tls/extension/supported_groups.rb +32 -0
- data/lib/raiha/tls/extension/supported_versions.rb +53 -0
- data/lib/raiha/tls/extension.rb +32 -0
- data/lib/raiha/tls/handshake/acceptor.rb +164 -0
- data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
- data/lib/raiha/tls/handshake/certificate.rb +56 -0
- data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
- data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
- data/lib/raiha/tls/handshake/client_hello.rb +208 -0
- data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
- data/lib/raiha/tls/handshake/client_side.rb +578 -0
- data/lib/raiha/tls/handshake/conductor.rb +390 -0
- data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
- data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
- data/lib/raiha/tls/handshake/finished.rb +36 -0
- data/lib/raiha/tls/handshake/key_update.rb +38 -0
- data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
- data/lib/raiha/tls/handshake/machine.rb +238 -0
- data/lib/raiha/tls/handshake/message.rb +25 -0
- data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
- data/lib/raiha/tls/handshake/reassembler.rb +88 -0
- data/lib/raiha/tls/handshake/server_hello.rb +98 -0
- data/lib/raiha/tls/handshake/server_side.rb +672 -0
- data/lib/raiha/tls/handshake/violation.rb +12 -0
- data/lib/raiha/tls/handshake.rb +62 -0
- data/lib/raiha/tls/iana.rb +113 -0
- data/lib/raiha/tls/key_exchange.rb +58 -0
- data/lib/raiha/tls/key_schedule.rb +269 -0
- data/lib/raiha/tls/keylog.rb +40 -0
- data/lib/raiha/tls/keyring.rb +180 -0
- data/lib/raiha/tls/masked_inspect.rb +33 -0
- data/lib/raiha/tls/output.rb +32 -0
- data/lib/raiha/tls/profile.rb +82 -0
- data/lib/raiha/tls/record/layer.rb +228 -0
- data/lib/raiha/tls/record/protection.rb +67 -0
- data/lib/raiha/tls/record.rb +11 -0
- data/lib/raiha/tls/server_name.rb +42 -0
- data/lib/raiha/tls/session.rb +106 -0
- data/lib/raiha/tls/signature_scheme.rb +35 -0
- data/lib/raiha/tls/tcp/acceptor.rb +121 -0
- data/lib/raiha/tls/tcp/connection.rb +371 -0
- data/lib/raiha/tls/tcp/event.rb +15 -0
- data/lib/raiha/tls/tcp/socket.rb +161 -0
- data/lib/raiha/tls/tcp.rb +14 -0
- data/lib/raiha/tls/test/fixed_kem.rb +30 -0
- data/lib/raiha/tls/test/fixed_random.rb +18 -0
- data/lib/raiha/tls/test/rfc8448.rb +923 -0
- data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
- data/lib/raiha/tls/test.rb +13 -0
- data/lib/raiha/tls/ticket.rb +184 -0
- data/lib/raiha/tls/ticket_keys.rb +206 -0
- data/lib/raiha/tls/trace.rb +58 -0
- data/lib/raiha/tls/transcript.rb +94 -0
- data/lib/raiha/tls/verifier/default.rb +72 -0
- data/lib/raiha/tls/verifier/host_name.rb +75 -0
- data/lib/raiha/tls/verifier/none.rb +14 -0
- data/lib/raiha/tls/verifier.rb +24 -0
- data/lib/raiha/tls.rb +34 -0
- data/lib/raiha/version.rb +3 -1
- data/lib/raiha.rb +8 -5
- data/sig/patches/array.rbs +9 -0
- data/sig/patches/io_buffer.rbs +13 -0
- data/sig/patches/openssl.rbs +25 -0
- data/sig/patches/socket.rbs +29 -0
- data/sig/patches/string.rbs +26 -0
- data/sig/patches/strscan.rbs +12 -0
- data/sig/raiha/http3/capsule.rbs +32 -0
- data/sig/raiha/http3/connection.rbs +324 -0
- data/sig/raiha/http3/frame.rbs +138 -0
- data/sig/raiha/http3/frame_reader.rbs +39 -0
- data/sig/raiha/http3/message.rbs +23 -0
- data/sig/raiha/http3/priority.rbs +40 -0
- data/sig/raiha/http3/qlog.rbs +54 -0
- data/sig/raiha/http3/qpack.rbs +246 -0
- data/sig/raiha/http3/session.rbs +11 -0
- data/sig/raiha/http3/settings.rbs +26 -0
- data/sig/raiha/http3.rbs +2 -0
- data/sig/raiha/http3_binding.rbs +26 -0
- data/sig/raiha/namespace.rbs +2 -0
- data/sig/raiha/quic/config.rbs +89 -0
- data/sig/raiha/quic/congestion.rbs +213 -0
- data/sig/raiha/quic/connection.rbs +249 -0
- data/sig/raiha/quic/connection_ids.rbs +34 -0
- data/sig/raiha/quic/crypto.rbs +78 -0
- data/sig/raiha/quic/crypto_stream.rbs +34 -0
- data/sig/raiha/quic/datagrams.rbs +32 -0
- data/sig/raiha/quic/endpoint.rbs +179 -0
- data/sig/raiha/quic/frame.rbs +387 -0
- data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
- data/sig/raiha/quic/kept_packets.rbs +25 -0
- data/sig/raiha/quic/mtu_search.rbs +21 -0
- data/sig/raiha/quic/negotiation.rbs +33 -0
- data/sig/raiha/quic/new_token.rbs +14 -0
- data/sig/raiha/quic/pacer.rbs +16 -0
- data/sig/raiha/quic/packet_protection.rbs +63 -0
- data/sig/raiha/quic/path.rbs +106 -0
- data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
- data/sig/raiha/quic/qlog.rbs +74 -0
- data/sig/raiha/quic/ranges.rbs +7 -0
- data/sig/raiha/quic/recovery.rbs +376 -0
- data/sig/raiha/quic/retry_token.rbs +21 -0
- data/sig/raiha/quic/route.rbs +51 -0
- data/sig/raiha/quic/scheduler.rbs +36 -0
- data/sig/raiha/quic/session.rbs +17 -0
- data/sig/raiha/quic/stateless_reset.rbs +10 -0
- data/sig/raiha/quic/streams.rbs +244 -0
- data/sig/raiha/quic/test.rbs +354 -0
- data/sig/raiha/quic/tls_binding.rbs +58 -0
- data/sig/raiha/quic/token.rbs +11 -0
- data/sig/raiha/quic/tracer.rbs +71 -0
- data/sig/raiha/quic/transport_parameters.rbs +113 -0
- data/sig/raiha/quic/values.rbs +200 -0
- data/sig/raiha/quic/version.rbs +30 -0
- data/sig/raiha/quic/wire.rbs +199 -0
- data/sig/raiha/quic.rbs +2 -0
- data/sig/raiha/runtime/threaded.rbs +616 -0
- data/sig/raiha/runtime.rbs +982 -0
- data/sig/raiha/tls/anti_replay.rbs +41 -0
- data/sig/raiha/tls/cipher_suite.rbs +41 -0
- data/sig/raiha/tls/codec.rbs +66 -0
- data/sig/raiha/tls/config.rbs +122 -0
- data/sig/raiha/tls/crypto.rbs +86 -0
- data/sig/raiha/tls/error.rbs +21 -0
- data/sig/raiha/tls/event.rbs +61 -0
- data/sig/raiha/tls/extension.rbs +331 -0
- data/sig/raiha/tls/handshake.rbs +311 -0
- data/sig/raiha/tls/iana.rbs +13 -0
- data/sig/raiha/tls/key_exchange.rbs +33 -0
- data/sig/raiha/tls/key_schedule.rbs +58 -0
- data/sig/raiha/tls/keylog.rbs +20 -0
- data/sig/raiha/tls/keyring.rbs +64 -0
- data/sig/raiha/tls/machine.rbs +429 -0
- data/sig/raiha/tls/masked_inspect.rbs +12 -0
- data/sig/raiha/tls/output.rbs +37 -0
- data/sig/raiha/tls/profile.rbs +26 -0
- data/sig/raiha/tls/record.rbs +79 -0
- data/sig/raiha/tls/server_name.rbs +10 -0
- data/sig/raiha/tls/session.rbs +48 -0
- data/sig/raiha/tls/signature_scheme.rbs +20 -0
- data/sig/raiha/tls/tcp.rbs +134 -0
- data/sig/raiha/tls/test.rbs +143 -0
- data/sig/raiha/tls/ticket.rbs +68 -0
- data/sig/raiha/tls/ticket_keys.rbs +67 -0
- data/sig/raiha/tls/trace.rbs +99 -0
- data/sig/raiha/tls/transcript.rbs +22 -0
- data/sig/raiha/tls/verifier.rbs +54 -0
- data/sig/raiha/tls.rbs +5 -0
- data/sig/raiha.rbs +3 -0
- metadata +355 -57
- data/.gitignore +0 -10
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -22
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/raiha.gemspec +0 -40
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
module Wire
|
|
5
|
+
# The frames of RFC 9000 "Frame Types and Formats" and the DATAGRAM frame
|
|
6
|
+
# of RFC 9221, as values. Each knows its frame types, reads its fields
|
|
7
|
+
# after the type, writes itself, and tells what RFC 9000 Table 3 says of
|
|
8
|
+
# it. ACK Delay is in seconds as a Float; `ack_delay_exponent` converts
|
|
9
|
+
# it, the peer's when reading and our own when writing.
|
|
10
|
+
module Frame
|
|
11
|
+
# The packet types of RFC 9000 Table 3, "Pkts": Initial, Handshake,
|
|
12
|
+
# 0-RTT and 1-RTT.
|
|
13
|
+
ALL_PACKETS = %i[initial handshake zero_rtt one_rtt].freeze
|
|
14
|
+
NOT_ZERO_RTT = %i[initial handshake one_rtt].freeze
|
|
15
|
+
APPLICATION_PACKETS = %i[zero_rtt one_rtt].freeze
|
|
16
|
+
ONE_RTT = %i[one_rtt].freeze
|
|
17
|
+
|
|
18
|
+
# What RFC 9000 Table 3 says of most frames: they travel in 0-RTT and
|
|
19
|
+
# 1-RTT packets, elicit an ACK, and are no probe. Frames for which it
|
|
20
|
+
# says otherwise override these.
|
|
21
|
+
module Defaults
|
|
22
|
+
def permitted_in?(packet_type) = APPLICATION_PACKETS.include?(packet_type)
|
|
23
|
+
|
|
24
|
+
def ack_eliciting? = true
|
|
25
|
+
|
|
26
|
+
# Whether the frame may go in a packet that probes a new path.
|
|
27
|
+
def probing? = false
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# A run of PADDING frames, one zero byte each, read as one value.
|
|
31
|
+
class Padding < Data.define(:length)
|
|
32
|
+
include Defaults
|
|
33
|
+
|
|
34
|
+
TYPES = [0x00].freeze
|
|
35
|
+
|
|
36
|
+
def permitted_in?(packet_type) = ALL_PACKETS.include?(packet_type)
|
|
37
|
+
|
|
38
|
+
def ack_eliciting? = false
|
|
39
|
+
|
|
40
|
+
def probing? = true
|
|
41
|
+
|
|
42
|
+
def initialize(length:)
|
|
43
|
+
raise ArgumentError, "padding is 1 byte at least, not #{length}" unless length.positive?
|
|
44
|
+
super
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(length: 1 + reader.zero_run)
|
|
48
|
+
|
|
49
|
+
def write(writer, ack_delay_exponent:) = writer.bytes("\0".b * length)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class Ping < Data.define
|
|
53
|
+
include Defaults
|
|
54
|
+
|
|
55
|
+
TYPES = [0x01].freeze
|
|
56
|
+
|
|
57
|
+
def permitted_in?(packet_type) = ALL_PACKETS.include?(packet_type)
|
|
58
|
+
|
|
59
|
+
def self.read(reader, type:, ack_delay_exponent:) = new
|
|
60
|
+
|
|
61
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x01)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
class HandshakeDone < Data.define
|
|
65
|
+
include Defaults
|
|
66
|
+
|
|
67
|
+
TYPES = [0x1e].freeze
|
|
68
|
+
|
|
69
|
+
def permitted_in?(packet_type) = ONE_RTT.include?(packet_type)
|
|
70
|
+
|
|
71
|
+
def self.read(reader, type:, ack_delay_exponent:) = new
|
|
72
|
+
|
|
73
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x1e)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# ACK, and ACK with ECN counts (RFC 9000 "ACK Frames"). `ranges` are
|
|
77
|
+
# the packet numbers acknowledged, as inclusive Ranges from the
|
|
78
|
+
# largest down with a gap between each two; `delay` is the ACK Delay in
|
|
79
|
+
# seconds; `ecn` the ECN counts, or nil.
|
|
80
|
+
class Ack < Data.define(:ranges, :delay, :ecn)
|
|
81
|
+
include Defaults
|
|
82
|
+
|
|
83
|
+
TYPES = [0x02, 0x03].freeze
|
|
84
|
+
|
|
85
|
+
def permitted_in?(packet_type) = NOT_ZERO_RTT.include?(packet_type)
|
|
86
|
+
|
|
87
|
+
def ack_eliciting? = false
|
|
88
|
+
|
|
89
|
+
# The packets received with ECT(0), ECT(1) and CE.
|
|
90
|
+
class ECNCounts < Data.define(:ect0, :ect1, :ce); end
|
|
91
|
+
|
|
92
|
+
def initialize(ranges:, delay:, ecn: nil)
|
|
93
|
+
raise ArgumentError, "an ACK acknowledges one packet at least" if ranges.empty?
|
|
94
|
+
raise ArgumentError, "packet numbers are not negative: #{ranges.last}" if ranges.last.begin.negative?
|
|
95
|
+
ranges.each { |range| raise ArgumentError, "an empty range #{range}" if range.begin > range.end }
|
|
96
|
+
ranges.inject do |above, below|
|
|
97
|
+
raise ArgumentError, "#{below} is not below #{above} with a gap" unless below.end < above.begin - 1
|
|
98
|
+
below
|
|
99
|
+
end
|
|
100
|
+
raise ArgumentError, "a negative ACK Delay" if delay.negative?
|
|
101
|
+
super
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def largest = ranges.first.end
|
|
105
|
+
|
|
106
|
+
# The Range Count field is not trusted to allocate anything: each
|
|
107
|
+
# range takes two fields, so the bytes run out first when it lies.
|
|
108
|
+
# A range below packet 0 is FRAME_ENCODING_ERROR.
|
|
109
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
110
|
+
largest = reader.varint
|
|
111
|
+
delay = reader.varint * (1 << ack_delay_exponent) / 1_000_000.0
|
|
112
|
+
count = reader.varint
|
|
113
|
+
smallest = largest - reader.varint
|
|
114
|
+
raise Malformed, "the first ACK range goes below packet 0" if smallest.negative?
|
|
115
|
+
ranges = [smallest..largest]
|
|
116
|
+
count.times do
|
|
117
|
+
range_largest = smallest - reader.varint - 2
|
|
118
|
+
smallest = range_largest - reader.varint
|
|
119
|
+
raise Malformed, "an ACK range goes below packet 0" if smallest.negative?
|
|
120
|
+
ranges << (smallest..range_largest)
|
|
121
|
+
end
|
|
122
|
+
ecn = type == 0x03 ? ECNCounts.new(ect0: reader.varint, ect1: reader.varint, ce: reader.varint) : nil
|
|
123
|
+
new(ranges:, delay:, ecn:)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# The delay goes in whole microseconds, divided by 2 to the power of
|
|
127
|
+
# `ack_delay_exponent` (RFC 9000 "ACK Frames").
|
|
128
|
+
def write(writer, ack_delay_exponent:)
|
|
129
|
+
writer.u8(ecn ? 0x03 : 0x02).varint(largest).varint((delay * 1_000_000).round >> ack_delay_exponent)
|
|
130
|
+
writer.varint(ranges.size - 1).varint(largest - ranges.first.begin)
|
|
131
|
+
ranges.inject do |above, below|
|
|
132
|
+
writer.varint(above.begin - below.end - 2).varint(below.end - below.begin)
|
|
133
|
+
below
|
|
134
|
+
end
|
|
135
|
+
writer.varint(ecn.ect0).varint(ecn.ect1).varint(ecn.ce) if ecn
|
|
136
|
+
writer
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Bytes of the TLS handshake at `offset` in the crypto stream of the
|
|
141
|
+
# packet's encryption level (RFC 9000 "CRYPTO Frames").
|
|
142
|
+
class Crypto < Data.define(:offset, :data)
|
|
143
|
+
include Defaults
|
|
144
|
+
|
|
145
|
+
TYPES = [0x06].freeze
|
|
146
|
+
|
|
147
|
+
def permitted_in?(packet_type) = NOT_ZERO_RTT.include?(packet_type)
|
|
148
|
+
|
|
149
|
+
def initialize(offset:, data:)
|
|
150
|
+
raise ArgumentError, "CRYPTO data ends past 2^62 - 1" unless offset >= 0 && offset + data.bytesize <= Varint::MAX
|
|
151
|
+
super
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Data that would end past 2^62 - 1 is FRAME_ENCODING_ERROR.
|
|
155
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
156
|
+
offset = reader.varint
|
|
157
|
+
length = reader.varint
|
|
158
|
+
raise Malformed, "CRYPTO data ends past 2^62 - 1" if offset + length > Varint::MAX
|
|
159
|
+
new(offset:, data: reader.bytes(length))
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x06).varint(offset).varint(data.bytesize).bytes(data)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# A token for the address validation of a later connection (RFC 9000
|
|
166
|
+
# "NEW_TOKEN Frames"). An empty one is FRAME_ENCODING_ERROR.
|
|
167
|
+
class NewToken < Data.define(:token)
|
|
168
|
+
include Defaults
|
|
169
|
+
|
|
170
|
+
TYPES = [0x07].freeze
|
|
171
|
+
|
|
172
|
+
def permitted_in?(packet_type) = ONE_RTT.include?(packet_type)
|
|
173
|
+
|
|
174
|
+
def initialize(token:)
|
|
175
|
+
raise ArgumentError, "a NEW_TOKEN frame carries a token" if token.empty?
|
|
176
|
+
super
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
180
|
+
token = reader.bytes(reader.varint)
|
|
181
|
+
raise Malformed, "an empty NEW_TOKEN" if token.empty?
|
|
182
|
+
new(token:)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x07).varint(token.bytesize).bytes(token)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Bytes of a stream at `offset`, and whether they end it (RFC 9000
|
|
189
|
+
# "STREAM Frames"). The low three bits of the frame type say whether
|
|
190
|
+
# an Offset and a Length field follow and whether FIN is set; without
|
|
191
|
+
# a Length the data runs to the end of the packet. Written, the frame
|
|
192
|
+
# always has a Length, and an Offset unless it is 0.
|
|
193
|
+
class Stream < Data.define(:stream_id, :offset, :data, :fin)
|
|
194
|
+
include Defaults
|
|
195
|
+
|
|
196
|
+
TYPES = (0x08..0x0f).to_a.freeze
|
|
197
|
+
OFF = 0x04
|
|
198
|
+
LEN = 0x02
|
|
199
|
+
FIN = 0x01
|
|
200
|
+
|
|
201
|
+
def initialize(stream_id:, offset:, data:, fin: false)
|
|
202
|
+
raise ArgumentError, "stream data ends past 2^62 - 1" unless offset >= 0 && offset + data.bytesize <= Varint::MAX
|
|
203
|
+
super
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Data that would end past 2^62 - 1 is FRAME_ENCODING_ERROR.
|
|
207
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
208
|
+
stream_id = reader.varint
|
|
209
|
+
offset = type & OFF == 0 ? 0 : reader.varint
|
|
210
|
+
length = type & LEN == 0 ? reader.remaining : reader.varint
|
|
211
|
+
raise Malformed, "stream data ends past 2^62 - 1" if offset + length > Varint::MAX
|
|
212
|
+
new(stream_id:, offset:, data: reader.bytes(length), fin: type & FIN != 0)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def write(writer, ack_delay_exponent:)
|
|
216
|
+
type = 0x08 | LEN | (offset.zero? ? 0 : OFF) | (fin ? FIN : 0)
|
|
217
|
+
writer.u8(type).varint(stream_id)
|
|
218
|
+
writer.varint(offset) unless offset.zero?
|
|
219
|
+
writer.varint(data.bytesize).bytes(data)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# The sender abandons the sending part of a stream (RFC 9000
|
|
224
|
+
# "RESET_STREAM Frames"); `final_size` is how far it got.
|
|
225
|
+
class ResetStream < Data.define(:stream_id, :error_code, :final_size)
|
|
226
|
+
include Defaults
|
|
227
|
+
|
|
228
|
+
TYPES = [0x04].freeze
|
|
229
|
+
|
|
230
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(stream_id: reader.varint, error_code: reader.varint, final_size: reader.varint)
|
|
231
|
+
|
|
232
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x04).varint(stream_id).varint(error_code).varint(final_size)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# The receiver asks the sender to stop sending on a stream (RFC 9000
|
|
236
|
+
# "STOP_SENDING Frames").
|
|
237
|
+
class StopSending < Data.define(:stream_id, :error_code)
|
|
238
|
+
include Defaults
|
|
239
|
+
|
|
240
|
+
TYPES = [0x05].freeze
|
|
241
|
+
|
|
242
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(stream_id: reader.varint, error_code: reader.varint)
|
|
243
|
+
|
|
244
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x05).varint(stream_id).varint(error_code)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# The most data the peer may send on the connection (RFC 9000
|
|
248
|
+
# "MAX_DATA Frames").
|
|
249
|
+
class MaxData < Data.define(:maximum)
|
|
250
|
+
include Defaults
|
|
251
|
+
|
|
252
|
+
TYPES = [0x10].freeze
|
|
253
|
+
|
|
254
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(maximum: reader.varint)
|
|
255
|
+
|
|
256
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x10).varint(maximum)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# The most data the peer may send on a stream (RFC 9000
|
|
260
|
+
# "MAX_STREAM_DATA Frames").
|
|
261
|
+
class MaxStreamData < Data.define(:stream_id, :maximum)
|
|
262
|
+
include Defaults
|
|
263
|
+
|
|
264
|
+
TYPES = [0x11].freeze
|
|
265
|
+
|
|
266
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(stream_id: reader.varint, maximum: reader.varint)
|
|
267
|
+
|
|
268
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x11).varint(stream_id).varint(maximum)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# A count of streams of one direction no stream ID can go past: 2^60.
|
|
272
|
+
MAX_STREAM_COUNT = 1 << 60
|
|
273
|
+
|
|
274
|
+
# The most streams of one kind the peer may open (RFC 9000
|
|
275
|
+
# "MAX_STREAMS Frames"). A count past 2^60 is FRAME_ENCODING_ERROR.
|
|
276
|
+
class MaxStreams < Data.define(:bidirectional, :maximum)
|
|
277
|
+
include Defaults
|
|
278
|
+
|
|
279
|
+
TYPES = [0x12, 0x13].freeze
|
|
280
|
+
|
|
281
|
+
def initialize(bidirectional:, maximum:)
|
|
282
|
+
raise ArgumentError, "#{maximum} streams is past 2^60" if maximum > MAX_STREAM_COUNT
|
|
283
|
+
super
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
287
|
+
maximum = reader.varint
|
|
288
|
+
raise Malformed, "#{maximum} streams is past 2^60" if maximum > MAX_STREAM_COUNT
|
|
289
|
+
new(bidirectional: type == 0x12, maximum:)
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def write(writer, ack_delay_exponent:) = writer.u8(bidirectional ? 0x12 : 0x13).varint(maximum)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# The sender has data to send but the connection's limit, `maximum`,
|
|
296
|
+
# stops it (RFC 9000 "DATA_BLOCKED Frames").
|
|
297
|
+
class DataBlocked < Data.define(:maximum)
|
|
298
|
+
include Defaults
|
|
299
|
+
|
|
300
|
+
TYPES = [0x14].freeze
|
|
301
|
+
|
|
302
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(maximum: reader.varint)
|
|
303
|
+
|
|
304
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x14).varint(maximum)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# The same for a stream (RFC 9000 "STREAM_DATA_BLOCKED Frames").
|
|
308
|
+
class StreamDataBlocked < Data.define(:stream_id, :maximum)
|
|
309
|
+
include Defaults
|
|
310
|
+
|
|
311
|
+
TYPES = [0x15].freeze
|
|
312
|
+
|
|
313
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(stream_id: reader.varint, maximum: reader.varint)
|
|
314
|
+
|
|
315
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x15).varint(stream_id).varint(maximum)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# The sender would open a stream but the count `maximum` stops it (RFC
|
|
319
|
+
# 9000 "STREAMS_BLOCKED Frames"). A count past 2^60 is
|
|
320
|
+
# FRAME_ENCODING_ERROR.
|
|
321
|
+
class StreamsBlocked < Data.define(:bidirectional, :maximum)
|
|
322
|
+
include Defaults
|
|
323
|
+
|
|
324
|
+
TYPES = [0x16, 0x17].freeze
|
|
325
|
+
|
|
326
|
+
def initialize(bidirectional:, maximum:)
|
|
327
|
+
raise ArgumentError, "#{maximum} streams is past 2^60" if maximum > MAX_STREAM_COUNT
|
|
328
|
+
super
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
332
|
+
maximum = reader.varint
|
|
333
|
+
raise Malformed, "#{maximum} streams is past 2^60" if maximum > MAX_STREAM_COUNT
|
|
334
|
+
new(bidirectional: type == 0x16, maximum:)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def write(writer, ack_delay_exponent:) = writer.u8(bidirectional ? 0x16 : 0x17).varint(maximum)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# A connection ID the peer may use, with its sequence number and the
|
|
341
|
+
# token of a stateless reset for it (RFC 9000 "NEW_CONNECTION_ID
|
|
342
|
+
# Frames"). A length outside 1 to 20 and Retire Prior To past the
|
|
343
|
+
# sequence number are FRAME_ENCODING_ERROR.
|
|
344
|
+
class NewConnectionID < Data.define(:sequence_number, :retire_prior_to, :connection_id, :stateless_reset_token)
|
|
345
|
+
include Defaults
|
|
346
|
+
|
|
347
|
+
TYPES = [0x18].freeze
|
|
348
|
+
|
|
349
|
+
def probing? = true
|
|
350
|
+
STATELESS_RESET_TOKEN_LENGTH = 16
|
|
351
|
+
|
|
352
|
+
def initialize(sequence_number:, retire_prior_to:, connection_id:, stateless_reset_token:)
|
|
353
|
+
raise ArgumentError, "Retire Prior To #{retire_prior_to} is past #{sequence_number}" if retire_prior_to > sequence_number
|
|
354
|
+
raise ArgumentError, "a connection ID of #{connection_id.bytesize} bytes" unless connection_id.bytesize.between?(1, CONNECTION_ID_MAX_LENGTH)
|
|
355
|
+
raise ArgumentError, "a stateless reset token has 16 bytes" unless stateless_reset_token.bytesize == STATELESS_RESET_TOKEN_LENGTH
|
|
356
|
+
super
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
360
|
+
sequence_number = reader.varint
|
|
361
|
+
retire_prior_to = reader.varint
|
|
362
|
+
raise Malformed, "Retire Prior To #{retire_prior_to} is past #{sequence_number}" if retire_prior_to > sequence_number
|
|
363
|
+
length = reader.u8
|
|
364
|
+
raise Malformed, "a connection ID of #{length} bytes" unless length.between?(1, CONNECTION_ID_MAX_LENGTH)
|
|
365
|
+
new(sequence_number:, retire_prior_to:, connection_id: reader.bytes(length), stateless_reset_token: reader.bytes(STATELESS_RESET_TOKEN_LENGTH))
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def write(writer, ack_delay_exponent:)
|
|
369
|
+
writer.u8(0x18).varint(sequence_number).varint(retire_prior_to)
|
|
370
|
+
.u8(connection_id.bytesize).bytes(connection_id).bytes(stateless_reset_token)
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# The connection ID of `sequence_number` is no longer used (RFC 9000
|
|
375
|
+
# "RETIRE_CONNECTION_ID Frames").
|
|
376
|
+
class RetireConnectionID < Data.define(:sequence_number)
|
|
377
|
+
include Defaults
|
|
378
|
+
|
|
379
|
+
TYPES = [0x19].freeze
|
|
380
|
+
|
|
381
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(sequence_number: reader.varint)
|
|
382
|
+
|
|
383
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x19).varint(sequence_number)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
PATH_DATA_LENGTH = 8
|
|
387
|
+
|
|
388
|
+
# Eight bytes for the peer to echo, to validate a path (RFC 9000
|
|
389
|
+
# "PATH_CHALLENGE Frames").
|
|
390
|
+
class PathChallenge < Data.define(:data)
|
|
391
|
+
include Defaults
|
|
392
|
+
|
|
393
|
+
TYPES = [0x1a].freeze
|
|
394
|
+
|
|
395
|
+
def probing? = true
|
|
396
|
+
|
|
397
|
+
def initialize(data:)
|
|
398
|
+
raise ArgumentError, "PATH_CHALLENGE data has 8 bytes" unless data.bytesize == PATH_DATA_LENGTH
|
|
399
|
+
super
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(data: reader.bytes(PATH_DATA_LENGTH))
|
|
403
|
+
|
|
404
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x1a).bytes(data)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
# The echo of a PATH_CHALLENGE (RFC 9000 "PATH_RESPONSE Frames").
|
|
408
|
+
class PathResponse < Data.define(:data)
|
|
409
|
+
include Defaults
|
|
410
|
+
|
|
411
|
+
TYPES = [0x1b].freeze
|
|
412
|
+
|
|
413
|
+
def permitted_in?(packet_type) = ONE_RTT.include?(packet_type)
|
|
414
|
+
|
|
415
|
+
def probing? = true
|
|
416
|
+
|
|
417
|
+
def initialize(data:)
|
|
418
|
+
raise ArgumentError, "PATH_RESPONSE data has 8 bytes" unless data.bytesize == PATH_DATA_LENGTH
|
|
419
|
+
super
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(data: reader.bytes(PATH_DATA_LENGTH))
|
|
423
|
+
|
|
424
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x1b).bytes(data)
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# The end of the connection (RFC 9000 "CONNECTION_CLOSE Frames"), with
|
|
428
|
+
# the fields of ConnectionError. A transport error (type 0x1c) names
|
|
429
|
+
# the frame type that caused it, 0 when none did; an application error
|
|
430
|
+
# (type 0x1d) has no frame type. `reason` is the bytes as they came.
|
|
431
|
+
class ConnectionClose < Data.define(:code, :frame_type, :reason, :application)
|
|
432
|
+
include Defaults
|
|
433
|
+
|
|
434
|
+
TYPES = [0x1c, 0x1d].freeze
|
|
435
|
+
|
|
436
|
+
# Only a transport error may close a connection in Initial and Handshake
|
|
437
|
+
# packets.
|
|
438
|
+
def permitted_in?(packet_type) = (application ? APPLICATION_PACKETS : ALL_PACKETS).include?(packet_type)
|
|
439
|
+
|
|
440
|
+
def ack_eliciting? = false
|
|
441
|
+
|
|
442
|
+
def initialize(code:, frame_type:, reason:, application:)
|
|
443
|
+
raise ArgumentError, "an application error names no frame type" if application && frame_type
|
|
444
|
+
raise ArgumentError, "a transport error names a frame type, 0 for none" unless application || frame_type
|
|
445
|
+
super
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
def self.read(reader, type:, ack_delay_exponent:)
|
|
449
|
+
application = type == 0x1d
|
|
450
|
+
code = reader.varint
|
|
451
|
+
frame_type = application ? nil : reader.varint
|
|
452
|
+
new(code:, frame_type:, reason: reader.bytes(reader.varint), application:)
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
def write(writer, ack_delay_exponent:)
|
|
456
|
+
writer.u8(application ? 0x1d : 0x1c).varint(code)
|
|
457
|
+
writer.varint(frame_type) if frame_type
|
|
458
|
+
writer.varint(reason.bytesize).bytes(reason)
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
# Application data outside any stream (RFC 9221 "Datagram Frame
|
|
463
|
+
# Types"). Type 0x30 runs to the end of the packet, 0x31 has a Length;
|
|
464
|
+
# written, it always has the Length.
|
|
465
|
+
class Datagram < Data.define(:data)
|
|
466
|
+
include Defaults
|
|
467
|
+
|
|
468
|
+
TYPES = [0x30, 0x31].freeze
|
|
469
|
+
|
|
470
|
+
def self.read(reader, type:, ack_delay_exponent:) = new(data: type == 0x31 ? reader.bytes(reader.varint) : reader.rest)
|
|
471
|
+
|
|
472
|
+
def write(writer, ack_delay_exponent:) = writer.u8(0x31).varint(data.bytesize).bytes(data)
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
KINDS = [
|
|
476
|
+
Padding, Ping, HandshakeDone, Ack, Crypto, NewToken, Stream, ResetStream, StopSending,
|
|
477
|
+
MaxData, MaxStreamData, MaxStreams, DataBlocked, StreamDataBlocked, StreamsBlocked,
|
|
478
|
+
NewConnectionID, RetireConnectionID, PathChallenge, PathResponse, ConnectionClose, Datagram
|
|
479
|
+
].freeze
|
|
480
|
+
BY_TYPE = KINDS.flat_map { |kind| kind::TYPES.map { |type| [type, kind] } }.to_h.freeze # steep:ignore
|
|
481
|
+
|
|
482
|
+
# The frames of the payload of a packet of `packet_type` (:initial,
|
|
483
|
+
# :handshake, :zero_rtt or :one_rtt, as the header's `type` says). A
|
|
484
|
+
# payload without frames, a frame type of an unknown frame or not in its
|
|
485
|
+
# shortest encoding, fields that do not decode, and a frame the packet
|
|
486
|
+
# type may not carry raise Malformed with the frame type and the error
|
|
487
|
+
# to close the connection with (RFC 9000 "Frames and Frame Types").
|
|
488
|
+
def self.parse(payload, packet_type:, ack_delay_exponent:)
|
|
489
|
+
reader = Reader.new(payload)
|
|
490
|
+
raise Malformed.new("a packet without frames", code: ErrorCode::PROTOCOL_VIOLATION) if reader.eof?
|
|
491
|
+
frames = [] #: Array[t]
|
|
492
|
+
frames << read(reader, packet_type:, ack_delay_exponent:) until reader.eof?
|
|
493
|
+
frames
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
# The bytes of `frames`, one after another.
|
|
497
|
+
def self.encode(frames, ack_delay_exponent:)
|
|
498
|
+
writer = Writer.new
|
|
499
|
+
frames.each { |frame| frame.write(writer, ack_delay_exponent:) }
|
|
500
|
+
writer.to_s
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
private_class_method def self.read(reader, packet_type:, ack_delay_exponent:)
|
|
504
|
+
type, size = reader.varint_with_size
|
|
505
|
+
unless size == Varint.size(type)
|
|
506
|
+
raise Malformed.new("frame type 0x#{type.to_s(16)} in #{size} bytes", code: ErrorCode::PROTOCOL_VIOLATION, frame_type: type)
|
|
507
|
+
end
|
|
508
|
+
kind = BY_TYPE.fetch(type) { raise Malformed.new("unknown frame type 0x#{type.to_s(16)}", frame_type: type) }
|
|
509
|
+
frame = begin
|
|
510
|
+
kind.read(reader, type:, ack_delay_exponent:)
|
|
511
|
+
rescue Malformed => error
|
|
512
|
+
raise Malformed.new(error.message, code: error.code, frame_type: type)
|
|
513
|
+
end
|
|
514
|
+
unless frame.permitted_in?(packet_type)
|
|
515
|
+
raise Malformed.new("frame type 0x#{type.to_s(16)} in a #{packet_type} packet", code: ErrorCode::PROTOCOL_VIOLATION, frame_type: type)
|
|
516
|
+
end
|
|
517
|
+
frame
|
|
518
|
+
end
|
|
519
|
+
end
|
|
520
|
+
end
|
|
521
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
module Wire
|
|
5
|
+
# In QUIC version 1 a connection ID has 20 bytes at most (RFC 9000 "Long
|
|
6
|
+
# Header Packets").
|
|
7
|
+
CONNECTION_ID_MAX_LENGTH = 20
|
|
8
|
+
EMPTY = "".b.freeze
|
|
9
|
+
|
|
10
|
+
# The header of an Initial, 0-RTT or Handshake packet (RFC 9000 "Long
|
|
11
|
+
# Header Packets"), apart from the packet number. `token` is empty
|
|
12
|
+
# except in an Initial packet. Retry has a header of its own.
|
|
13
|
+
class LongHeader < Data.define(:type, :version, :dcid, :scid, :token)
|
|
14
|
+
TYPES = %i[initial zero_rtt handshake].freeze
|
|
15
|
+
# The bits of the first byte under header protection: the two reserved
|
|
16
|
+
# bits and the packet number length.
|
|
17
|
+
PROTECTED_BITS = 0x0f
|
|
18
|
+
RESERVED_BITS = 0x0c
|
|
19
|
+
|
|
20
|
+
def initialize(type:, version:, dcid:, scid:, token: EMPTY)
|
|
21
|
+
raise ArgumentError, "a long header with a packet number is one of #{TYPES}, not #{type.inspect}" unless TYPES.include?(type)
|
|
22
|
+
raise ArgumentError, "only an Initial packet carries a token" unless type == :initial || token.empty?
|
|
23
|
+
[dcid, scid].each do |cid|
|
|
24
|
+
raise ArgumentError, "a connection ID has #{CONNECTION_ID_MAX_LENGTH} bytes at most, not #{cid.bytesize}" if cid.bytesize > CONNECTION_ID_MAX_LENGTH
|
|
25
|
+
end
|
|
26
|
+
super
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def protected_bits = PROTECTED_BITS
|
|
30
|
+
|
|
31
|
+
def reserved_bits = RESERVED_BITS
|
|
32
|
+
|
|
33
|
+
# The first byte before header protection.
|
|
34
|
+
def first_byte(packet_number_length) = 0xc0 | (version.type_bits(type) << 4) | (packet_number_length - 1)
|
|
35
|
+
|
|
36
|
+
# Writes what comes between the first byte and the packet number.
|
|
37
|
+
# `length` is the value of the Length field: the bytes of the packet
|
|
38
|
+
# number, the payload and the tag.
|
|
39
|
+
def write_fields(writer, length:)
|
|
40
|
+
writer.u32(version.number).u8(dcid.bytesize).bytes(dcid).u8(scid.bytesize).bytes(scid)
|
|
41
|
+
writer.varint(token.bytesize).bytes(token) if type == :initial
|
|
42
|
+
writer.varint(length)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# This header with header protection off: none of its fields was
|
|
46
|
+
# under it.
|
|
47
|
+
def unmasked(_first_byte) = self
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The header of a 1-RTT packet (RFC 9000 "Short Header Packets"), apart
|
|
51
|
+
# from the packet number. The Destination Connection ID goes without its
|
|
52
|
+
# length: the receiver knows the length of the IDs it issued. The key
|
|
53
|
+
# phase is under header protection, so a parsed header has nil there
|
|
54
|
+
# until the protection is off.
|
|
55
|
+
class ShortHeader < Data.define(:dcid, :spin, :key_phase)
|
|
56
|
+
# The two reserved bits, the key phase and the packet number length.
|
|
57
|
+
PROTECTED_BITS = 0x1f
|
|
58
|
+
RESERVED_BITS = 0x18
|
|
59
|
+
|
|
60
|
+
def initialize(dcid:, spin: false, key_phase: 0)
|
|
61
|
+
raise ArgumentError, "a connection ID has #{CONNECTION_ID_MAX_LENGTH} bytes at most, not #{dcid.bytesize}" if dcid.bytesize > CONNECTION_ID_MAX_LENGTH
|
|
62
|
+
super
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The packet type, as `LongHeader#type` tells it for a long header.
|
|
66
|
+
def type = :one_rtt
|
|
67
|
+
|
|
68
|
+
def protected_bits = PROTECTED_BITS
|
|
69
|
+
|
|
70
|
+
def reserved_bits = RESERVED_BITS
|
|
71
|
+
|
|
72
|
+
# The first byte before header protection.
|
|
73
|
+
def first_byte(packet_number_length)
|
|
74
|
+
key_phase_bit = case key_phase
|
|
75
|
+
when 0 then 0x00
|
|
76
|
+
when 1 then 0x04
|
|
77
|
+
else raise ArgumentError, "the key phase is 0 or 1, not #{key_phase.inspect}"
|
|
78
|
+
end
|
|
79
|
+
0x40 | (spin ? 0x20 : 0) | key_phase_bit | (packet_number_length - 1)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Writes what comes between the first byte and the packet number.
|
|
83
|
+
def write_fields(writer, length:) = writer.bytes(dcid)
|
|
84
|
+
|
|
85
|
+
# This header with header protection off, which reveals the key phase
|
|
86
|
+
# in `first_byte`.
|
|
87
|
+
def unmasked(first_byte) = with(key_phase: (first_byte >> 2) & 1)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|