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,785 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
# The connections of one UDP socket. It finds the
|
|
5
|
+
# connection a datagram belongs to by its Destination Connection ID and
|
|
6
|
+
# decides for one that belongs to none: the offer of a new connection,
|
|
7
|
+
# which the application takes or turns down, a Version Negotiation, a
|
|
8
|
+
# Stateless Reset, a CONNECTION_CLOSE for a Retry token that does not
|
|
9
|
+
# verify, or nothing. A Connection never refers to its Endpoint.
|
|
10
|
+
#
|
|
11
|
+
# Which offers to take is the application's: the Endpoint bounds what
|
|
12
|
+
# each keeps and how long, but not how many connections there are, nor
|
|
13
|
+
# how many have addresses not validated. An application should count the
|
|
14
|
+
# offers it admitted and has not taken, and its connections not
|
|
15
|
+
# `address_validated?`, and `retry` the offers of addresses not validated
|
|
16
|
+
# (Route::Incoming#address_validated) once they come to a bound of its
|
|
17
|
+
# own: a connection from a spoofed address is never validated, and
|
|
18
|
+
# Initials from such addresses would otherwise take every connection
|
|
19
|
+
# there is room for. Runtime::Server does so with `max_unvalidated`.
|
|
20
|
+
#
|
|
21
|
+
# A client's Endpoint makes its connections with `connect` and takes no
|
|
22
|
+
# offers; a Version Negotiation or a Retry goes to the connection it is
|
|
23
|
+
# for. Either finds a Stateless Reset of its peers among the datagrams of
|
|
24
|
+
# no connection.
|
|
25
|
+
#
|
|
26
|
+
# Like a Connection it never reads a clock: `now:` is the monotonic clock,
|
|
27
|
+
# and the wall clock its tokens expire by is `time:`, given once when it is
|
|
28
|
+
# made.
|
|
29
|
+
class Endpoint
|
|
30
|
+
# RFC 9000 "Stateless Reset": a reset is shorter than the packet that
|
|
31
|
+
# triggered it, so that two endpoints cannot keep one going, and no
|
|
32
|
+
# longer than this, one byte short of the 43 bytes RFC 9000 has resets
|
|
33
|
+
# match, so that a reset reflects little.
|
|
34
|
+
MAX_RESET_LENGTH = 42
|
|
35
|
+
# RFC 9000 "Looping": each address is sent resets from a token bucket of
|
|
36
|
+
# RESET_BURST, refilled at RESET_RATE a second. At most RESET_ADDRESSES
|
|
37
|
+
# buckets are kept, the least recently used going first, so that no
|
|
38
|
+
# address is shut out. An address whose bucket went starts again with a
|
|
39
|
+
# full one, so all resets together come from one more bucket, of
|
|
40
|
+
# RESET_TOTAL_BURST refilled at RESET_TOTAL_RATE a second.
|
|
41
|
+
RESET_BURST = 10.0
|
|
42
|
+
RESET_RATE = 1.0
|
|
43
|
+
RESET_ADDRESSES = 1024
|
|
44
|
+
RESET_TOTAL_BURST = 1000.0
|
|
45
|
+
RESET_TOTAL_RATE = 100.0
|
|
46
|
+
# How long an offer lasts unless the application decides on it, from
|
|
47
|
+
# its first datagram, so that one the application forgets goes too.
|
|
48
|
+
# It is long enough for a client to send the rest of a ClientHello
|
|
49
|
+
# again a few times after losses, as an offer that sent an ACK is
|
|
50
|
+
# refused when it goes, not forgotten.
|
|
51
|
+
HANDLE_LIFETIME = 10.0
|
|
52
|
+
# The bytes of datagrams an offer keeps until it becomes a connection,
|
|
53
|
+
# and all offers together, so that what an attacker can make an
|
|
54
|
+
# Endpoint keep is bounded whatever the application does.
|
|
55
|
+
HANDLE_BUFFER = 64 * 1024
|
|
56
|
+
HANDLES_BUFFER = 16 * 1024 * 1024
|
|
57
|
+
# How many connection IDs an Endpoint draws for a new connection before
|
|
58
|
+
# it gives up on finding one that leads to nothing yet.
|
|
59
|
+
CONNECTION_ID_ATTEMPTS = 8
|
|
60
|
+
|
|
61
|
+
# An offer of a new connection, until the application decides on it:
|
|
62
|
+
# what the headers of the client's first Initial said, and the
|
|
63
|
+
# datagrams that came for it. `destination_connection_id` is the one the
|
|
64
|
+
# client chose, which its Initial keys come from; the client's first, the
|
|
65
|
+
# original, differs from it after a Retry, as does
|
|
66
|
+
# `retry_source_connection_id`, nil without one.
|
|
67
|
+
#
|
|
68
|
+
# Once admitted, the offer reads the ClientHello out of the Initial
|
|
69
|
+
# packets with the client's Initial keys, into the TLS layer's Acceptor,
|
|
70
|
+
# and acknowledges them while the ClientHello is not whole, as RFC 9000
|
|
71
|
+
# "Sending ACK Frames" asks of every Initial packet that elicits one
|
|
72
|
+
# (acknowledgment). The ACKs come from `server_connection_id`, which
|
|
73
|
+
# the client sends its Initial packets to from then, and which the
|
|
74
|
+
# connection takes; the connection takes the ACKs as well
|
|
75
|
+
# (`early_acks`, Connection.server).
|
|
76
|
+
class Handle
|
|
77
|
+
attr_reader :remote, :version, :destination_connection_id, :source_connection_id, :original_destination_connection_id,
|
|
78
|
+
:retry_source_connection_id, :expires_at, :datagrams, :server_connection_id, :early_acks
|
|
79
|
+
# Whether the client's address is validated, by a Retry or by a token
|
|
80
|
+
# of NEW_TOKEN.
|
|
81
|
+
attr_reader :address_validated
|
|
82
|
+
# The bytes of the datagrams kept.
|
|
83
|
+
attr_reader :buffered
|
|
84
|
+
|
|
85
|
+
def initialize(remote:, version:, destination_connection_id:, source_connection_id:, original_destination_connection_id:,
|
|
86
|
+
retry_source_connection_id:, expires_at:, address_validated:)
|
|
87
|
+
@remote = remote
|
|
88
|
+
@address_validated = address_validated
|
|
89
|
+
@version = version
|
|
90
|
+
@destination_connection_id = destination_connection_id
|
|
91
|
+
@source_connection_id = source_connection_id
|
|
92
|
+
@original_destination_connection_id = original_destination_connection_id
|
|
93
|
+
@retry_source_connection_id = retry_source_connection_id
|
|
94
|
+
@expires_at = expires_at
|
|
95
|
+
@datagrams = [] #: Array[[String, address, address?, ecn | :ce]]
|
|
96
|
+
@buffered = 0
|
|
97
|
+
@acceptor = nil #: Raiha::TLS::Handshake::Acceptor?
|
|
98
|
+
@keys = nil #: Crypto::Keys?
|
|
99
|
+
@crypto = CryptoStream.new(:initial)
|
|
100
|
+
@largest = nil #: Integer?
|
|
101
|
+
@violation = nil #: Violation?
|
|
102
|
+
@offered = false
|
|
103
|
+
# What the second stage received and sent in the Initial space: the
|
|
104
|
+
# packets to acknowledge, the server's keys, and the ACKs that went.
|
|
105
|
+
@space = nil #: Recovery::PacketNumberSpace?
|
|
106
|
+
@server_keys = nil #: Crypto::Keys?
|
|
107
|
+
@server_connection_id = nil #: String?
|
|
108
|
+
@early_acks = [] #: Array[Connection::EarlyAck]
|
|
109
|
+
@sent = 0
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Keeps a datagram for the connection to take.
|
|
113
|
+
def keep(datagram, from:, to:, ecn:)
|
|
114
|
+
@buffered += datagram.bytesize
|
|
115
|
+
@datagrams << [datagram, from, to, ecn]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def expired?(now) = now >= @expires_at
|
|
119
|
+
|
|
120
|
+
def admitted? = !@acceptor.nil?
|
|
121
|
+
|
|
122
|
+
# Starts reading the ClientHello, from the datagrams kept so far.
|
|
123
|
+
# `time` is the wall clock; short header packets have connection IDs
|
|
124
|
+
# of `dcid_length` bytes, and long header ones are read in `versions`.
|
|
125
|
+
def admit(time:, dcid_length:, versions:, now:)
|
|
126
|
+
@acceptor = Raiha::TLS::Handshake::Acceptor.new
|
|
127
|
+
keys = Crypto::Initial.derive(dcid: @destination_connection_id, version: @version)
|
|
128
|
+
@keys = keys.client
|
|
129
|
+
@server_keys = keys.server
|
|
130
|
+
@space = Recovery::PacketNumberSpace.new(:initial)
|
|
131
|
+
@datagrams.each { |datagram, _, _, ecn| read(datagram, time:, dcid_length:, versions:, ecn:, now:) }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The server's connection ID is `connection_id` from now on, which the
|
|
135
|
+
# ACKs come from.
|
|
136
|
+
def choose_connection_id(connection_id) = @server_connection_id = connection_id
|
|
137
|
+
|
|
138
|
+
# Whether a packet received waits for an ACK: none once the ClientHello
|
|
139
|
+
# is whole, as the connection's first packets carry one, nor once it
|
|
140
|
+
# cannot be read, as the offer ends with a CONNECTION_CLOSE.
|
|
141
|
+
def ack_owed? = !accepted && !failed? && !!@space&.ack_owed?
|
|
142
|
+
|
|
143
|
+
# The server's Initial packet of only an ACK of the Initial packets
|
|
144
|
+
# received, which goes now; nil when none is owed, or the packet would
|
|
145
|
+
# take the server past three times what came from an address not
|
|
146
|
+
# validated (RFC 9000 "Address Validation"). A packet of only an ACK
|
|
147
|
+
# elicits none, and needs no padding (RFC 9000 "Datagram Size"). Its
|
|
148
|
+
# number takes two bytes, as the client acknowledges none of them and
|
|
149
|
+
# an offer may send far more than a byte tells apart.
|
|
150
|
+
def acknowledgment(now:)
|
|
151
|
+
space = @space
|
|
152
|
+
keys = @server_keys
|
|
153
|
+
scid = @server_connection_id
|
|
154
|
+
return nil unless space && keys && scid && ack_owed?
|
|
155
|
+
frame = space.ack_frame(now:) or return nil
|
|
156
|
+
header = Wire::LongHeader.new(type: :initial, version: @version, dcid: @source_connection_id, scid:)
|
|
157
|
+
payload = Wire::Frame.encode([frame.with(delay: 0.0)], ack_delay_exponent: Connection::DEFAULT_ACK_DELAY_EXPONENT)
|
|
158
|
+
number = space.upcoming_packet_number
|
|
159
|
+
packet = Wire::Packet.seal(header, packet_number: number, packet_number_length: 2, payload:, keys:)
|
|
160
|
+
return nil if !@address_validated && @sent + packet.bytesize > Path::AMPLIFICATION_FACTOR * @buffered
|
|
161
|
+
space.next_packet_number
|
|
162
|
+
space.ack_sent
|
|
163
|
+
@sent += packet.bytesize
|
|
164
|
+
@early_acks << Connection::EarlyAck.new(packet_number: number, bytes: packet.bytesize, time: now)
|
|
165
|
+
packet
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# The number of the server's next Initial packet.
|
|
169
|
+
def next_packet_number = @space&.upcoming_packet_number || 0
|
|
170
|
+
|
|
171
|
+
# Reads the CRYPTO frames of the Initial packets of `datagram` that
|
|
172
|
+
# open into the Acceptor. What breaks a rule once a packet has opened
|
|
173
|
+
# ends the ClientHello. Once it is whole, nothing more is read: anyone
|
|
174
|
+
# who saw the client's Initial can make Initial packets, and the
|
|
175
|
+
# connection, which reads them all again, is to judge them. The
|
|
176
|
+
# packets are those to the client's first connection ID, or to the
|
|
177
|
+
# server's, once an ACK has gone.
|
|
178
|
+
def read(datagram, time:, dcid_length:, versions:, ecn:, now:)
|
|
179
|
+
acceptor = @acceptor or return
|
|
180
|
+
keys = @keys or return
|
|
181
|
+
space = @space or return
|
|
182
|
+
return if acceptor.accepted
|
|
183
|
+
offset = 0
|
|
184
|
+
while offset < datagram.bytesize && !failed?
|
|
185
|
+
packet = parse(datagram, offset, dcid_length, versions) or break
|
|
186
|
+
offset = packet.end_offset
|
|
187
|
+
header = packet.header
|
|
188
|
+
next unless header.is_a?(Wire::LongHeader) && header.type == :initial && header.version == @version
|
|
189
|
+
next unless header.dcid == @destination_connection_id || header.dcid == @server_connection_id
|
|
190
|
+
unmasked = packet.unmask(keys.header_protection, largest: @largest)
|
|
191
|
+
payload = unmasked.open(keys.aead) or next
|
|
192
|
+
@largest = unmasked.packet_number if @largest.nil? || unmasked.packet_number > @largest # steep:ignore
|
|
193
|
+
frames = Wire::Frame.parse(payload, packet_type: :initial, ack_delay_exponent: Connection::DEFAULT_ACK_DELAY_EXPONENT)
|
|
194
|
+
space.receive(unmasked.packet_number, ecn:, ack_eliciting: frames.any?(&:ack_eliciting?), now:)
|
|
195
|
+
frames.each do |frame|
|
|
196
|
+
@crypto.receive(frame.offset, frame.data, limit: Raiha::TLS::Handshake::Acceptor::MAX_CLIENT_HELLO_SIZE) if frame.is_a?(Wire::Frame::Crypto)
|
|
197
|
+
end
|
|
198
|
+
bytes = @crypto.read
|
|
199
|
+
acceptor.receive(bytes, epoch: :initial, now: time) unless bytes.empty?
|
|
200
|
+
end
|
|
201
|
+
rescue Violation => error
|
|
202
|
+
@violation = error
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Why the ClientHello could not be read, as the error to close with:
|
|
206
|
+
# its CRYPTO frames broke a rule, or the Acceptor failed on what they
|
|
207
|
+
# carried (RFC 9001 "TLS Errors"). nil while it can be.
|
|
208
|
+
def error
|
|
209
|
+
if (violation = @violation)
|
|
210
|
+
ConnectionError.new(code: violation.code, frame_type: violation.frame_type || 0, reason: violation.message, application: false)
|
|
211
|
+
elsif (acceptor = @acceptor)&.failed?
|
|
212
|
+
code = TLSBinding.error_code(Raiha::TLS::Profile::QUIC, acceptor.alert, acceptor.reason)
|
|
213
|
+
ConnectionError.new(code:, frame_type: CryptoStream::FRAME_TYPE, reason: acceptor.reason.to_s, application: false)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def failed? = !error.nil?
|
|
218
|
+
|
|
219
|
+
# Turns the whole ClientHello down with the TLS alert `description`,
|
|
220
|
+
# one of the Acceptor's REJECT_ALERTS (another raises ArgumentError),
|
|
221
|
+
# and gives the error to close with.
|
|
222
|
+
def reject(description)
|
|
223
|
+
acceptor = @acceptor
|
|
224
|
+
raise Error::StateError, "the ClientHello of the offer is not whole yet" unless acceptor&.accepted
|
|
225
|
+
acceptor.reject(description)
|
|
226
|
+
error or raise Error::StateError, "the Acceptor took no alert"
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# The address the first datagram came to, which an answer goes from.
|
|
230
|
+
def local = @datagrams.dig(0, 2)
|
|
231
|
+
|
|
232
|
+
# The ClientHello once whole, nil before.
|
|
233
|
+
def accepted = @acceptor&.accepted
|
|
234
|
+
|
|
235
|
+
# The version for the connection, by compatible version negotiation
|
|
236
|
+
# (RFC 9368 "Compatible Version Negotiation"), from the version
|
|
237
|
+
# information of the client's transport parameters: the first of
|
|
238
|
+
# `config.versions` fully deployed that the client offered and that is
|
|
239
|
+
# the offer's version or compatible with it. The offer's version when
|
|
240
|
+
# none is, or the client's Chosen Version is not the offer's, or its
|
|
241
|
+
# transport parameters do not read, which the connection closes on.
|
|
242
|
+
def negotiate(config)
|
|
243
|
+
data = accepted&.extension_data(TransportParameters::Extension::TYPE) or return @version
|
|
244
|
+
information = begin
|
|
245
|
+
TransportParameters.decode(data, sender: :client).version_information
|
|
246
|
+
rescue Wire::Malformed
|
|
247
|
+
nil
|
|
248
|
+
end
|
|
249
|
+
return @version unless information && information.chosen == @version.number
|
|
250
|
+
config.versions.find do |version|
|
|
251
|
+
config.deployed.include?(version) && information.available.include?(version.number) &&
|
|
252
|
+
(version == @version || @version.compatible_with?(version))
|
|
253
|
+
end || @version
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The ClientHello, the first time it is whole and asked for.
|
|
257
|
+
def take_client_hello
|
|
258
|
+
return nil if @offered
|
|
259
|
+
accepted = self.accepted or return nil
|
|
260
|
+
@offered = true
|
|
261
|
+
accepted
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# The packet at `offset` in `datagram`; nil when there is none, or its
|
|
265
|
+
# header does not read, which ends the datagram as anyone could have
|
|
266
|
+
# sent it.
|
|
267
|
+
private def parse(datagram, offset, dcid_length, versions)
|
|
268
|
+
packet = Wire::Packet.parse(datagram, offset:, dcid_length:, versions:)
|
|
269
|
+
packet if packet.is_a?(Wire::Packet::Parsed)
|
|
270
|
+
rescue Wire::Malformed
|
|
271
|
+
nil
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
private def instance_variables_to_inspect = %i[@remote @expires_at]
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# The connections by the peer addresses of their paths, which a
|
|
278
|
+
# datagram no connection ID leads anywhere is looked up by, for a
|
|
279
|
+
# stateless reset.
|
|
280
|
+
class Peers
|
|
281
|
+
def initialize
|
|
282
|
+
@connections = {} #: Hash[address, Array[Connection]]
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# `connection` has a path to `address`.
|
|
286
|
+
def add(connection, address)
|
|
287
|
+
connections = @connections[address] ||= []
|
|
288
|
+
connections << connection unless connections.include?(connection)
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# `connection` has no path to `address` any more.
|
|
292
|
+
def remove(connection, address)
|
|
293
|
+
connections = @connections[address] or return
|
|
294
|
+
connections.delete(connection)
|
|
295
|
+
@connections.delete(address) if connections.empty?
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# The connections with a path to `address`.
|
|
299
|
+
def of(address) = @connections[address] || []
|
|
300
|
+
|
|
301
|
+
# The connections closed are let go.
|
|
302
|
+
def prune
|
|
303
|
+
@connections.each_value { |connections| connections.delete_if(&:closed?) }
|
|
304
|
+
@connections.delete_if { |_, connections| connections.empty? }
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# How a server's connection has its Endpoint seal a NEW_TOKEN token for
|
|
309
|
+
# a new address of the client it validated.
|
|
310
|
+
class Tokens
|
|
311
|
+
def initialize(mint)
|
|
312
|
+
@mint = mint
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def mint(address, version:, now:) = @mint.call(address, version, now)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# How a connection has its Endpoint make connection IDs that lead to
|
|
319
|
+
# it, each with its stateless reset token, for NEW_CONNECTION_ID frames,
|
|
320
|
+
# and forget those the peer retired.
|
|
321
|
+
class Issuer
|
|
322
|
+
def initialize(issue, forget)
|
|
323
|
+
@issue = issue
|
|
324
|
+
@forget = forget
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
def issue(connection) = @issue.call(connection)
|
|
328
|
+
|
|
329
|
+
def forget(connection_id) = @forget.call(connection_id)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# An Endpoint of a server. `random` is what it draws its own random
|
|
333
|
+
# bytes from, a seeded Random in tests, and its keys when `config` has
|
|
334
|
+
# none. `connection_ids` makes the connection IDs it issues
|
|
335
|
+
# (RandomConnectionIDs).
|
|
336
|
+
def self.server(config:, random:, time:, now:, connection_ids: RandomConnectionIDs.new(random))
|
|
337
|
+
new(role: :server, config:, random:, time:, now:, connection_ids:)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# An Endpoint of a client.
|
|
341
|
+
def self.client(config:, random:, time:, now:, connection_ids: RandomConnectionIDs.new(random))
|
|
342
|
+
new(role: :client, config:, random:, time:, now:, connection_ids:)
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
private_class_method :new
|
|
346
|
+
|
|
347
|
+
def initialize(role:, config:, random:, time:, now:, connection_ids:)
|
|
348
|
+
@role = role
|
|
349
|
+
@config = config
|
|
350
|
+
@random = random
|
|
351
|
+
@connection_ids = connection_ids
|
|
352
|
+
# Zero-length connection IDs lead to no connection of their own, and
|
|
353
|
+
# are never issued.
|
|
354
|
+
@issuer = config.connection_id_length.zero? ? nil : Issuer.new(->(connection) { issue_connection_id(connection) }, ->(id) { @connections.delete(id) })
|
|
355
|
+
@tokens = Tokens.new(->(address, version, now) { NewToken.mint(key: @new_token_key, address:, version:, time: wall_clock(now), random: @random) })
|
|
356
|
+
@time = time
|
|
357
|
+
@created_at = now
|
|
358
|
+
@retry_token_key = config.retry_token_key || random.bytes(Config::MIN_KEY_LENGTH).b
|
|
359
|
+
@new_token_key = NewToken.key(@retry_token_key)
|
|
360
|
+
@stateless_reset_key = config.stateless_reset_key || random.bytes(Config::MIN_KEY_LENGTH).b
|
|
361
|
+
# The connections by each connection ID that leads to them, and by the
|
|
362
|
+
# peer's address.
|
|
363
|
+
@connections = {} #: Hash[String, Connection]
|
|
364
|
+
@peers = Peers.new
|
|
365
|
+
# The offers by the Destination Connection ID of their Initials, and
|
|
366
|
+
# the bytes they keep.
|
|
367
|
+
@handles = {} #: Hash[String, Handle]
|
|
368
|
+
@buffered = 0
|
|
369
|
+
# What the Endpoint sends itself, for the offers.
|
|
370
|
+
@transmits = [] #: Array[Transmit]
|
|
371
|
+
# What is left of each address's stateless resets and of all of them,
|
|
372
|
+
# and when.
|
|
373
|
+
@resets = {} #: Hash[address, [Float, Float]]
|
|
374
|
+
@reset_total = RESET_TOTAL_BURST
|
|
375
|
+
@reset_total_at = now
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# What to do with `datagram`, which came from `from` to `to` with the
|
|
379
|
+
# ECN codepoint `ecn`: a Route, or nil when an offer keeps it. Nothing
|
|
380
|
+
# the peer sends raises. A datagram another worker handed on is
|
|
381
|
+
# `forwarded`, and is not handed on again.
|
|
382
|
+
def receive(datagram, from:, to: nil, ecn: :not_ect, now:, forwarded: false)
|
|
383
|
+
packet = Wire::Packet.parse(datagram, dcid_length: @config.connection_id_length, versions: @config.versions)
|
|
384
|
+
case packet
|
|
385
|
+
when Wire::Packet::Parsed then route(packet.header, datagram, from:, to:, ecn:, now:, forwarded:)
|
|
386
|
+
when Wire::UnsupportedVersion
|
|
387
|
+
@role == :server ? negotiate(packet, datagram, from, to) : Route::Drop.new(reason: :unsupported_version)
|
|
388
|
+
else
|
|
389
|
+
@role == :client ? deliver(packet.dcid, datagram, from:, to:, ecn:, now:) : Route::Drop.new(reason: :unexpected_packet)
|
|
390
|
+
end
|
|
391
|
+
rescue Wire::Malformed
|
|
392
|
+
(connection = reset_of(datagram, from, now)) ? Route::Deliver.new(connection:) : Route::Drop.new(reason: :malformed)
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# A new client connection to `remote` with the TLS settings given, its
|
|
396
|
+
# first Initial waiting to be sent, found by the Source Connection ID
|
|
397
|
+
# the Endpoint chooses for it. A server's Endpoint connects to no one:
|
|
398
|
+
# Error::Usage.
|
|
399
|
+
def connect(tls_config:, keyring:, server_name:, remote:, now:, tracer: nil, keylog: nil, token: nil, session: nil, early_data: false)
|
|
400
|
+
raise Error::Usage, "a server's Endpoint does not connect" unless @role == :client
|
|
401
|
+
scid = new_connection_id
|
|
402
|
+
connection = Connection.client(config: @config, tls_config:, keyring:, server_name:, remote:, time: wall_clock(now), now:, tracer:,
|
|
403
|
+
keylog:, source_connection_id: scid, token:, session:, early_data:, issuer: @issuer, peers: @peers)
|
|
404
|
+
add(connection, [scid], remote)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
# Each datagram to send now: the Endpoint's own, then the connections'.
|
|
408
|
+
def each_transmit(now:, &)
|
|
409
|
+
while (transmit = @transmits.shift)
|
|
410
|
+
yield transmit
|
|
411
|
+
end
|
|
412
|
+
each_connection { |connection| connection.each_transmit(now:, &) }
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
# When something is to be done next: a connection's timer or the end of
|
|
416
|
+
# an offer.
|
|
417
|
+
def next_timeout
|
|
418
|
+
[*connections.filter_map(&:next_timeout), *@handles.each_value.map(&:expires_at)].min
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# Runs out what is due at `now`: the timers of the connections, and the
|
|
422
|
+
# offers no one decided on. A connection that has closed is let go.
|
|
423
|
+
def handle_timeout(now:)
|
|
424
|
+
connections.each { |connection| connection.handle_timeout(now:) }
|
|
425
|
+
@handles.each_value.uniq.select { |handle| handle.expired?(now) }.each { |handle| expire(handle) }
|
|
426
|
+
@connections.delete_if { |_, connection| connection.closed? }
|
|
427
|
+
@peers.prune
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# The second stage of the offer of `handle`: the Endpoint reads the
|
|
431
|
+
# ClientHello out of its Initial packets, and acknowledges them while
|
|
432
|
+
# it is not whole. Route::ClientHello when it is whole by now, else nil;
|
|
433
|
+
# the datagrams to come complete it. A ClientHello that cannot be read
|
|
434
|
+
# ends the offer with a CONNECTION_CLOSE.
|
|
435
|
+
def admit(handle, now:)
|
|
436
|
+
pending!(handle)
|
|
437
|
+
raise Error::StateError, "the offer is admitted already" if handle.admitted?
|
|
438
|
+
handle.admit(time: wall_clock(now), dcid_length: @config.connection_id_length, versions: @config.versions, now:)
|
|
439
|
+
acknowledge(handle, now)
|
|
440
|
+
client_hello(handle)
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
# Takes the offer of `handle`, whose ClientHello is whole, as a new
|
|
444
|
+
# connection with the TLS settings the application chose by it, and
|
|
445
|
+
# returns the Connection. It is given the datagrams the offer kept, and
|
|
446
|
+
# is found from now on by its own connection ID, which the Endpoint
|
|
447
|
+
# chooses and gives the stateless reset token of, and by the one the
|
|
448
|
+
# client chose. It sends the client a token of NEW_TOKEN for its later
|
|
449
|
+
# connections, once it confirms the handshake. Its version is the one
|
|
450
|
+
# the offer negotiates (Handle#negotiate), which the token belongs to
|
|
451
|
+
# (RFC 9369 "TLS Resumption and NEW_TOKEN Tokens"). `anti_replay` and
|
|
452
|
+
# `app_early_data_context` go to Connection.server, for 0-RTT.
|
|
453
|
+
def accept(handle, tls_config:, keyring:, now:, tracer: nil, keylog: nil, anti_replay: nil, app_early_data_context: "".b)
|
|
454
|
+
pending!(handle)
|
|
455
|
+
accepted = handle.accepted or raise Error::StateError, "the ClientHello of the offer is not whole yet"
|
|
456
|
+
scid = handle.server_connection_id || new_connection_id
|
|
457
|
+
token = StatelessReset.token(key: @stateless_reset_key, connection_id: scid) unless scid.empty?
|
|
458
|
+
negotiated = handle.negotiate(@config)
|
|
459
|
+
new_token = NewToken.mint(key: @new_token_key, address: handle.remote, version: negotiated.number, time: wall_clock(now),
|
|
460
|
+
random: @random)
|
|
461
|
+
connection = Connection.server(config: @config, tls_config:, keyring:, time: wall_clock(now), now:, tracer:, keylog:,
|
|
462
|
+
client_hello: accepted, original_destination_connection_id: handle.original_destination_connection_id,
|
|
463
|
+
retry_source_connection_id: handle.retry_source_connection_id, address_validated: handle.address_validated,
|
|
464
|
+
source_connection_id: scid, stateless_reset_token: token, new_token:, anti_replay:, app_early_data_context:, issuer: @issuer,
|
|
465
|
+
peers: @peers, tokens: @tokens, early_acks: handle.early_acks, original_version: handle.version, negotiated_version: negotiated)
|
|
466
|
+
forget(handle)
|
|
467
|
+
add(connection, [scid, handle.destination_connection_id], handle.remote)
|
|
468
|
+
handle.datagrams.each { |datagram, from, to, ecn| connection.receive(datagram, from:, to:, ecn:, now:) }
|
|
469
|
+
connection
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# Turns down the offer of `handle`, whose ClientHello is whole, with the
|
|
473
|
+
# TLS alert `description` as CRYPTO_ERROR, as the application chose by
|
|
474
|
+
# the ClientHello. It is one of those the Acceptor rejects with;
|
|
475
|
+
# another raises ArgumentError.
|
|
476
|
+
def reject(handle, description)
|
|
477
|
+
pending!(handle)
|
|
478
|
+
close(handle, handle.reject(description))
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
# Turns down the offer of `handle`, at either stage, with the transport
|
|
482
|
+
# error `error_code` in a CONNECTION_CLOSE: CONNECTION_REFUSED, say,
|
|
483
|
+
# for a server that takes no more connections.
|
|
484
|
+
def refuse(handle, error_code: ErrorCode::CONNECTION_REFUSED)
|
|
485
|
+
pending!(handle)
|
|
486
|
+
close(handle, ConnectionError.new(code: error_code, frame_type: 0, reason: "", application: false))
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
# Answers the offer of `handle`, at its first stage, with a Retry (RFC
|
|
490
|
+
# 9000 "Address Validation Using Retry Packets"): a token of the
|
|
491
|
+
# client's first connection ID and address, which the Initial it sends
|
|
492
|
+
# next brings back to show it receives there, and a connection ID it
|
|
493
|
+
# sends that Initial to. An offer whose Initial brought a Retry token
|
|
494
|
+
# already cannot have another, nor can one admitted, which may have
|
|
495
|
+
# answered the client's Initials, after which the client drops a Retry
|
|
496
|
+
# (RFC 9000 "Handling a Retry Packet"): Error::Usage.
|
|
497
|
+
def retry(handle, now:)
|
|
498
|
+
pending!(handle)
|
|
499
|
+
raise Error::Usage, "an offer after a Retry cannot be retried" if handle.retry_source_connection_id
|
|
500
|
+
raise Error::Usage, "an admitted offer cannot be retried" if handle.admitted?
|
|
501
|
+
token = RetryToken.mint(key: @retry_token_key, original_destination_connection_id: handle.original_destination_connection_id,
|
|
502
|
+
version: handle.version.number, address: handle.remote, time: wall_clock(now))
|
|
503
|
+
packet = Wire::Retry.new(version: handle.version, dcid: handle.source_connection_id, scid: new_connection_id, token:)
|
|
504
|
+
@transmits << transmit(packet.seal(handle.destination_connection_id), handle.remote, handle.local)
|
|
505
|
+
forget(handle)
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
def each_connection(&) = connections.each(&)
|
|
509
|
+
|
|
510
|
+
def connection_count = connections.size
|
|
511
|
+
|
|
512
|
+
private def connections = @connections.values.uniq
|
|
513
|
+
|
|
514
|
+
# Where a packet with a header that reads goes: to the connection of its
|
|
515
|
+
# Destination Connection ID, else, for a short header, to a connection
|
|
516
|
+
# it is a Stateless Reset of, and at a server, for a client's Initial,
|
|
517
|
+
# to the offer of a new connection, and for a short header, a Stateless
|
|
518
|
+
# Reset. What none of those take but another worker issued the
|
|
519
|
+
# Destination Connection ID of goes to that worker.
|
|
520
|
+
private def route(header, datagram, from:, to:, ecn:, now:, forwarded:)
|
|
521
|
+
return deliver(header.dcid, datagram, from:, to:, ecn:, now:) if @connections.key?(header.dcid)
|
|
522
|
+
if header.is_a?(Wire::ShortHeader)
|
|
523
|
+
if (connection = reset_of(datagram, from, now))
|
|
524
|
+
return Route::Deliver.new(connection:)
|
|
525
|
+
end
|
|
526
|
+
if (foreign = foreign(header, datagram, forwarded))
|
|
527
|
+
return foreign
|
|
528
|
+
end
|
|
529
|
+
return @role == :server ? reset(header.dcid, datagram, from, to, now) : Route::Drop.new(reason: :unknown_connection_id)
|
|
530
|
+
end
|
|
531
|
+
return Route::Drop.new(reason: :unknown_connection_id) unless @role == :server
|
|
532
|
+
if (handle = @handles[header.dcid])
|
|
533
|
+
# Before the server answers, the client sends only Initial and 0-RTT
|
|
534
|
+
# packets, from where its first came.
|
|
535
|
+
return Route::Drop.new(reason: :address_mismatch) unless from == handle.remote
|
|
536
|
+
return Route::Drop.new(reason: :unexpected_packet) if header.type == :handshake
|
|
537
|
+
# RFC 9000 "Datagram Size": a server drops an Initial in a datagram
|
|
538
|
+
# under 1200 bytes, the offer's later ones too.
|
|
539
|
+
return Route::Drop.new(reason: :too_short) if header.type == :initial && datagram.bytesize < Connection::MIN_INITIAL_DATAGRAM
|
|
540
|
+
if (dropped = keep(handle, datagram, from:, to:, ecn:))
|
|
541
|
+
return dropped
|
|
542
|
+
end
|
|
543
|
+
return nil unless handle.admitted?
|
|
544
|
+
handle.read(datagram, time: wall_clock(now), dcid_length: @config.connection_id_length, versions: @config.versions, ecn:, now:)
|
|
545
|
+
acknowledge(handle, now)
|
|
546
|
+
return client_hello(handle)
|
|
547
|
+
end
|
|
548
|
+
if (foreign = foreign(header, datagram, forwarded))
|
|
549
|
+
return foreign
|
|
550
|
+
end
|
|
551
|
+
return Route::Drop.new(reason: :unknown_connection_id) unless header.type == :initial
|
|
552
|
+
# RFC 9000 "Datagram Size": a server drops an Initial in a datagram
|
|
553
|
+
# under 1200 bytes.
|
|
554
|
+
return Route::Drop.new(reason: :too_short) if datagram.bytesize < Connection::MIN_INITIAL_DATAGRAM
|
|
555
|
+
offer(header, datagram, from:, to:, ecn:, now:)
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
# The first stage of a new connection, from the headers alone: no key
|
|
559
|
+
# is derived and nothing is decrypted, so that a Retry or a refusal
|
|
560
|
+
# costs little under attack. A Retry token
|
|
561
|
+
# shows the client receives at its address and carries its first
|
|
562
|
+
# Destination Connection ID (RFC 9000 "Address Validation Using Retry
|
|
563
|
+
# Packets"); one that does not verify is answered with a CONNECTION_CLOSE
|
|
564
|
+
# of INVALID_TOKEN, as the client would not take another Retry (the same
|
|
565
|
+
# section). A token of NEW_TOKEN that is valid shows the same of the
|
|
566
|
+
# address; one that is not is as none, and the application may still send
|
|
567
|
+
# a Retry (RFC 9000 "Address Validation for Future Connections"). A
|
|
568
|
+
# token of another kind is as none.
|
|
569
|
+
#
|
|
570
|
+
# Without a Retry, the connection ID is the client's first, which RFC
|
|
571
|
+
# 9000 "Negotiating Connection IDs" has 8 bytes at least, and an
|
|
572
|
+
# Initial with a shorter one is dropped; after one, it is the server's.
|
|
573
|
+
private def offer(header, datagram, from:, to:, ecn:, now:)
|
|
574
|
+
odcid = header.dcid
|
|
575
|
+
retry_scid = nil #: String?
|
|
576
|
+
unless header.token.empty? || header.token.getbyte(0) != RetryToken::KIND
|
|
577
|
+
verified = RetryToken.verify(header.token, key: @retry_token_key, address: from, time: wall_clock(now))
|
|
578
|
+
unless verified
|
|
579
|
+
error = ConnectionError.new(code: ErrorCode::INVALID_TOKEN, frame_type: 0, reason: "", application: false)
|
|
580
|
+
packet = close_packet(header.version, client_dcid: header.dcid, client_scid: header.scid, error:)
|
|
581
|
+
return Route::Respond.new(transmit: transmit(packet, from, to))
|
|
582
|
+
end
|
|
583
|
+
# RFC 9369 "Compatible Negotiation Requirements": a client changes
|
|
584
|
+
# no version after a Retry, and a token of another version's is
|
|
585
|
+
# dropped.
|
|
586
|
+
return Route::Drop.new(reason: :version_mismatch) if verified.version != header.version.number
|
|
587
|
+
odcid = verified.original_destination_connection_id
|
|
588
|
+
retry_scid = header.dcid
|
|
589
|
+
end
|
|
590
|
+
return Route::Drop.new(reason: :short_connection_id) if retry_scid.nil? && header.dcid.bytesize < Connection::INITIAL_DCID_LENGTH
|
|
591
|
+
validated = !retry_scid.nil? || (header.token.getbyte(0) == NewToken::KIND &&
|
|
592
|
+
NewToken.valid?(header.token, key: @new_token_key, address: from, version: header.version.number, time: wall_clock(now)))
|
|
593
|
+
handle = Handle.new(remote: from, version: header.version, destination_connection_id: header.dcid, source_connection_id: header.scid,
|
|
594
|
+
original_destination_connection_id: odcid, retry_source_connection_id: retry_scid, expires_at: now + HANDLE_LIFETIME,
|
|
595
|
+
address_validated: validated)
|
|
596
|
+
if (dropped = keep(handle, datagram, from:, to:, ecn:))
|
|
597
|
+
return dropped
|
|
598
|
+
end
|
|
599
|
+
@handles[header.dcid] = handle
|
|
600
|
+
Route::Incoming.new(handle:, remote: from, address_validated: validated)
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
# Keeps `datagram` with the offer `handle`; nil, or a Route::Drop when
|
|
604
|
+
# the offer or all of them keep as much as they may.
|
|
605
|
+
private def keep(handle, datagram, from:, to:, ecn:)
|
|
606
|
+
size = datagram.bytesize
|
|
607
|
+
return Route::Drop.new(reason: :buffer_full) if handle.buffered + size > HANDLE_BUFFER || @buffered + size > HANDLES_BUFFER
|
|
608
|
+
handle.keep(datagram, from:, to:, ecn:)
|
|
609
|
+
@buffered += size
|
|
610
|
+
nil
|
|
611
|
+
end
|
|
612
|
+
|
|
613
|
+
# Route::Foreign for a short header, Handshake or 0-RTT packet, as the
|
|
614
|
+
# first of `datagram`, whose Destination Connection ID, of the length
|
|
615
|
+
# this Endpoint issues, another worker issued; nil for any other, and
|
|
616
|
+
# for one that a worker `forwarded` already. An Initial is never handed
|
|
617
|
+
# on: a new connection's comes with the client's own choice of ID.
|
|
618
|
+
private def foreign(header, datagram, forwarded)
|
|
619
|
+
return nil if forwarded || @role == :client
|
|
620
|
+
return nil if header.is_a?(Wire::LongHeader) && header.type == :initial
|
|
621
|
+
return nil unless header.dcid.bytesize == @config.connection_id_length
|
|
622
|
+
worker = @connection_ids.worker_of(header.dcid) or return nil
|
|
623
|
+
Route::Foreign.new(worker:, datagram:)
|
|
624
|
+
end
|
|
625
|
+
|
|
626
|
+
# What the second stage of `handle` has come to: Route::ClientHello the
|
|
627
|
+
# first time the ClientHello is whole, nil before and when it could not
|
|
628
|
+
# be read, which closes the offer.
|
|
629
|
+
private def client_hello(handle)
|
|
630
|
+
if (error = handle.error)
|
|
631
|
+
close(handle, error)
|
|
632
|
+
return nil
|
|
633
|
+
end
|
|
634
|
+
accepted = handle.take_client_hello or return nil
|
|
635
|
+
Route::ClientHello.new(handle:, accepted:)
|
|
636
|
+
end
|
|
637
|
+
|
|
638
|
+
# Sends the ACK the offer of `handle` owes, if any, from a connection ID
|
|
639
|
+
# chosen for the server the first time. With connection IDs of zero
|
|
640
|
+
# length, which one offer alone could have, no ACK goes.
|
|
641
|
+
private def acknowledge(handle, now)
|
|
642
|
+
return unless handle.ack_owed? && @config.connection_id_length.positive?
|
|
643
|
+
unless handle.server_connection_id
|
|
644
|
+
id = new_connection_id
|
|
645
|
+
handle.choose_connection_id(id)
|
|
646
|
+
@handles[id] = handle
|
|
647
|
+
end
|
|
648
|
+
packet = handle.acknowledgment(now:) or return
|
|
649
|
+
@transmits << transmit(packet, handle.remote, handle.local)
|
|
650
|
+
end
|
|
651
|
+
|
|
652
|
+
# Gives `datagram` to the connection of `dcid`.
|
|
653
|
+
private def deliver(dcid, datagram, from:, to:, ecn:, now:)
|
|
654
|
+
connection = @connections[dcid] or return Route::Drop.new(reason: :unknown_connection_id)
|
|
655
|
+
connection.receive(datagram, from:, to:, ecn:, now:)
|
|
656
|
+
Route::Deliver.new(connection:)
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
# RFC 9000 "Detecting a Stateless Reset": the connection to the address
|
|
660
|
+
# `datagram` came from that takes it as a Stateless Reset of its peer,
|
|
661
|
+
# which then drains; nil when none does. A reset has a connection ID of
|
|
662
|
+
# no connection, or none that reads.
|
|
663
|
+
private def reset_of(datagram, from, now) = @peers.of(from).find { |connection| connection.take_stateless_reset(datagram, from:, now:) }
|
|
664
|
+
|
|
665
|
+
# Keeps `connection`, found by `ids` and by the address of its peer.
|
|
666
|
+
private def add(connection, ids, peer)
|
|
667
|
+
ids.each { |id| @connections[id] = connection }
|
|
668
|
+
@peers.add(connection, peer)
|
|
669
|
+
connection
|
|
670
|
+
end
|
|
671
|
+
|
|
672
|
+
# Ends the offer of `handle` with a CONNECTION_CLOSE of `error` in an
|
|
673
|
+
# Initial packet, the server's only, sealed with the server's Initial
|
|
674
|
+
# keys of the client's connection ID (RFC 9000 "Immediate Close during
|
|
675
|
+
# the Handshake"). It is far smaller than the Initial that came, as the
|
|
676
|
+
# anti-amplification limit asks. Its Source Connection ID is the one an
|
|
677
|
+
# ACK of the offer came from, and else leads nowhere, as no connection
|
|
678
|
+
# follows, so it is drawn with nothing to avoid; its packet number
|
|
679
|
+
# follows the ACKs'.
|
|
680
|
+
private def close(handle, error)
|
|
681
|
+
packet = close_packet(handle.version, client_dcid: handle.destination_connection_id, client_scid: handle.source_connection_id, error:,
|
|
682
|
+
scid: handle.server_connection_id, packet_number: handle.next_packet_number)
|
|
683
|
+
@transmits << transmit(packet, handle.remote, handle.local)
|
|
684
|
+
forget(handle)
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
# The server's Initial packet `packet_number` with a CONNECTION_CLOSE of
|
|
688
|
+
# `error` to the client whose Initial had the connection IDs
|
|
689
|
+
# `client_dcid` and `client_scid`, from `scid`, or one drawn.
|
|
690
|
+
private def close_packet(version, client_dcid:, client_scid:, error:, scid: nil, packet_number: 0)
|
|
691
|
+
reason = error.reason.b.byteslice(0, Connection::MAX_REASON_LENGTH) #: String
|
|
692
|
+
frame = Wire::Frame::ConnectionClose.new(code: error.code, frame_type: error.frame_type, reason:, application: false)
|
|
693
|
+
scid ||= @random.bytes(@config.connection_id_length).b
|
|
694
|
+
header = Wire::LongHeader.new(type: :initial, version:, dcid: client_scid, scid:)
|
|
695
|
+
keys = Crypto::Initial.derive(dcid: client_dcid, version:).server
|
|
696
|
+
payload = Wire::Frame.encode([frame], ack_delay_exponent: Connection::DEFAULT_ACK_DELAY_EXPONENT)
|
|
697
|
+
Wire::Packet.seal(header, packet_number:, packet_number_length: 2, payload:, keys:)
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
# A connection ID of `config.connection_id_length` bytes, from the
|
|
701
|
+
# generator, that leads to nothing yet. With none found in
|
|
702
|
+
# CONNECTION_ID_ATTEMPTS draws, as with zero-length connection IDs,
|
|
703
|
+
# which only one connection can have, the Endpoint can take no more
|
|
704
|
+
# connections: Error::StateError. A generator that gives another length
|
|
705
|
+
# is misused: Error::Usage.
|
|
706
|
+
private def new_connection_id
|
|
707
|
+
length = @config.connection_id_length
|
|
708
|
+
CONNECTION_ID_ATTEMPTS.times do
|
|
709
|
+
id = @connection_ids.generate(length)
|
|
710
|
+
raise Error::Usage, "the connection ID generator gave #{id.bytesize} bytes, not #{length}" unless id.bytesize == length
|
|
711
|
+
return id unless @connections.key?(id) || @handles.key?(id)
|
|
712
|
+
end
|
|
713
|
+
raise Error::StateError, "no connection ID of #{@config.connection_id_length} bytes is free for another connection"
|
|
714
|
+
end
|
|
715
|
+
|
|
716
|
+
# A connection ID that leads to `connection`, and its stateless reset
|
|
717
|
+
# token.
|
|
718
|
+
private def issue_connection_id(connection)
|
|
719
|
+
id = new_connection_id
|
|
720
|
+
@connections[id] = connection
|
|
721
|
+
[id, StatelessReset.token(key: @stateless_reset_key, connection_id: id)]
|
|
722
|
+
end
|
|
723
|
+
|
|
724
|
+
private def pending!(handle)
|
|
725
|
+
return if @handles[handle.destination_connection_id].equal?(handle)
|
|
726
|
+
raise Error::StateError, "the offer has been decided on or has expired"
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
# An offer no one decided on is forgotten; one that acknowledged the
|
|
730
|
+
# client's Initials is refused, so that the client, which knows the
|
|
731
|
+
# server's connection ID, does not wait on it.
|
|
732
|
+
private def expire(handle)
|
|
733
|
+
return forget(handle) unless handle.server_connection_id
|
|
734
|
+
close(handle, ConnectionError.new(code: ErrorCode::CONNECTION_REFUSED, frame_type: 0, reason: "", application: false))
|
|
735
|
+
end
|
|
736
|
+
|
|
737
|
+
private def forget(handle)
|
|
738
|
+
@handles.delete(handle.destination_connection_id)
|
|
739
|
+
(id = handle.server_connection_id) and @handles.delete(id)
|
|
740
|
+
@buffered -= handle.buffered
|
|
741
|
+
end
|
|
742
|
+
|
|
743
|
+
# RFC 9000 "Version Negotiation": a packet of a version not supported,
|
|
744
|
+
# in a datagram large enough to start a connection, is answered with
|
|
745
|
+
# the versions offered, and a reserved one (RFC 9368 "Incompatible
|
|
746
|
+
# Version Negotiation").
|
|
747
|
+
private def negotiate(packet, datagram, from, to)
|
|
748
|
+
return Route::Drop.new(reason: :too_short) if datagram.bytesize < Connection::MIN_INITIAL_DATAGRAM
|
|
749
|
+
versions = [*@config.offered.map(&:number), Version.reserved(@random)]
|
|
750
|
+
Route::Respond.new(transmit: transmit(packet.negotiation(versions).encode, from, to))
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
# RFC 9000 "Stateless Reset": a short header packet of no connection is
|
|
754
|
+
# answered with a reset whose token comes from its connection ID, when
|
|
755
|
+
# the address has resets left.
|
|
756
|
+
private def reset(dcid, datagram, from, to, now)
|
|
757
|
+
length = [datagram.bytesize - 1, MAX_RESET_LENGTH].min
|
|
758
|
+
return Route::Drop.new(reason: :unknown_connection_id) if length < StatelessReset::MIN_LENGTH
|
|
759
|
+
return Route::Drop.new(reason: :reset_rate_limited) unless take_reset(from, now)
|
|
760
|
+
token = StatelessReset.token(key: @stateless_reset_key, connection_id: dcid)
|
|
761
|
+
Route::Respond.new(transmit: transmit(StatelessReset.packet(token:, length:, random: @random), from, to))
|
|
762
|
+
end
|
|
763
|
+
|
|
764
|
+
# Takes a reset from the bucket of `address`, which goes to the end of
|
|
765
|
+
# the table as the most recently used, and from the bucket of all.
|
|
766
|
+
private def take_reset(address, now)
|
|
767
|
+
tokens, at = @resets.delete(address) || [RESET_BURST, now]
|
|
768
|
+
@resets.shift if @resets.size >= RESET_ADDRESSES
|
|
769
|
+
tokens = [tokens + (now - at) * RESET_RATE, RESET_BURST].min
|
|
770
|
+
total = [@reset_total + (now - @reset_total_at) * RESET_TOTAL_RATE, RESET_TOTAL_BURST].min
|
|
771
|
+
taken = tokens >= 1 && total >= 1
|
|
772
|
+
@resets[address] = [taken ? tokens - 1 : tokens, now]
|
|
773
|
+
@reset_total = taken ? total - 1 : total
|
|
774
|
+
@reset_total_at = now
|
|
775
|
+
taken
|
|
776
|
+
end
|
|
777
|
+
|
|
778
|
+
private def transmit(payload, destination, source) = Transmit.new(payload:, destination:, source:, ecn: :not_ect, segment_size: nil)
|
|
779
|
+
|
|
780
|
+
# The wall clock at `now`.
|
|
781
|
+
private def wall_clock(now) = @time + (now - @created_at)
|
|
782
|
+
|
|
783
|
+
private def instance_variables_to_inspect = %i[@config]
|
|
784
|
+
end
|
|
785
|
+
end
|