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,290 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module Raiha::Runtime
|
|
6
|
+
module Threaded
|
|
7
|
+
# The server of the runtime on threads, as the Server of fibers is: an
|
|
8
|
+
# Endpoint on a UDP socket of its own that takes clients up to
|
|
9
|
+
# `max_connections` at a time, refusing more with CONNECTION_REFUSED,
|
|
10
|
+
# and gives the block of `run` the Session of each connection once its
|
|
11
|
+
# handshake completes, each in a thread of its own ("raiha-conn"). No
|
|
12
|
+
# Fiber scheduler is needed. The arguments are those of the Server of
|
|
13
|
+
# fibers, without `worker:`, as there are no Workers of threads, and
|
|
14
|
+
# with `max_threads:`.
|
|
15
|
+
#
|
|
16
|
+
# A connection's handler counts toward `max_connections` until its
|
|
17
|
+
# thread ends, though its connection has closed: a handler held up in
|
|
18
|
+
# a library that blocks keeps its place, so that no more of them run
|
|
19
|
+
# than `max_connections`. The threads of the streams of `each_stream`
|
|
20
|
+
# and of the requests of HTTP/3, the application's besides, are
|
|
21
|
+
# `max_threads` at most; one more waits for a place. So the threads
|
|
22
|
+
# the server starts for the application are `max_connections +
|
|
23
|
+
# max_threads` at most, and those of the driver, its readers and its
|
|
24
|
+
# runner, and the one that waits for `stop` ("raiha-wake") besides.
|
|
25
|
+
#
|
|
26
|
+
# When the block is over, what the streams sent is let be acknowledged,
|
|
27
|
+
# until three PTOs go with no more of it acknowledged or 30 seconds in
|
|
28
|
+
# all, and the connection closes with the application's error 0; when
|
|
29
|
+
# it raises, the connection closes with INTERNAL_ERROR, and the
|
|
30
|
+
# exception goes on, for its thread to report. Other connections are
|
|
31
|
+
# not touched.
|
|
32
|
+
#
|
|
33
|
+
# `stop` asks the server to stop, from a signal handler as well, as the
|
|
34
|
+
# Server of fibers has it. Once the driver has stopped, `run` waits for
|
|
35
|
+
# the threads it started to end, until the time `stop` gave the
|
|
36
|
+
# sessions is out, or STOP_TIMEOUT from when `run` was left by an
|
|
37
|
+
# exception; those still running, held up in a library say, are left
|
|
38
|
+
# to run, and `run` returns all the same. Their connections have
|
|
39
|
+
# closed, and what they call of them raises QUIC::ClosedError.
|
|
40
|
+
#
|
|
41
|
+
# With `qlog_dir`, each connection has a qlog there, as the Server of
|
|
42
|
+
# fibers has it, written outside the core lock.
|
|
43
|
+
class Server
|
|
44
|
+
MAX_CONNECTIONS = 64
|
|
45
|
+
MAX_THREADS = 256
|
|
46
|
+
STOP_TIMEOUT = Raiha::Runtime::Server::STOP_TIMEOUT
|
|
47
|
+
|
|
48
|
+
def initialize(config:, tls_config:, keyring:, host: nil, port: nil, max_connections: MAX_CONNECTIONS, max_unvalidated: max_connections / 2,
|
|
49
|
+
max_threads: MAX_THREADS, qlog_dir: nil, anti_replay: nil, app_early_data_context: "".b, stop_error_code: 0)
|
|
50
|
+
unless max_unvalidated.is_a?(Integer) && (0..max_connections).cover?(max_unvalidated)
|
|
51
|
+
raise ArgumentError, "max_unvalidated #{max_unvalidated.inspect} is not 0 to max_connections (#{max_connections})"
|
|
52
|
+
end
|
|
53
|
+
@threads = Threads.new(max_threads)
|
|
54
|
+
@handlers = Threads.new(max_connections)
|
|
55
|
+
@stop_error_code = stop_error_code
|
|
56
|
+
@config = UDP.ecn(UDP.pmtud(UDP.batching(config)))
|
|
57
|
+
@tls_config = tls_config
|
|
58
|
+
@keyring = keyring
|
|
59
|
+
@anti_replay = anti_replay || Raiha::TLS::AntiReplay::Memory.new(now: Time.now)
|
|
60
|
+
@app_early_data_context = app_early_data_context
|
|
61
|
+
@host = host || "::"
|
|
62
|
+
@port = port || 443
|
|
63
|
+
@max_connections = max_connections
|
|
64
|
+
@max_unvalidated = max_unvalidated
|
|
65
|
+
@admitted = [] #: Array[Raiha::QUIC::Endpoint::Handle]
|
|
66
|
+
@qlog = qlog_dir && Qlog.new(qlog_dir, budget: Budget.current)
|
|
67
|
+
# Who writes the qlog files: the runner after its turn, and whoever
|
|
68
|
+
# stops the driver.
|
|
69
|
+
@tracing = Thread::Mutex.new
|
|
70
|
+
@traces = {} #: Hash[Session, Qlog::Pending]
|
|
71
|
+
@traces.compare_by_identity
|
|
72
|
+
@sessions = [] #: Array[Session]
|
|
73
|
+
@started = {} #: Hash[Session, true]
|
|
74
|
+
@started.compare_by_identity
|
|
75
|
+
@handler = nil #: ^(Session) -> void | nil
|
|
76
|
+
@driver = nil #: Driver?
|
|
77
|
+
@done = nil #: MonitorMixin::ConditionVariable?
|
|
78
|
+
@udp = nil #: UDP?
|
|
79
|
+
@stop = nil #: [Float | Integer, Integer]?
|
|
80
|
+
@stop_deadline = nil #: Float?
|
|
81
|
+
@wake = nil #: IO?
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Binds the socket and serves until stopped, giving the block each
|
|
85
|
+
# Session; raises what failed in the driver, if anything did.
|
|
86
|
+
def run(&handler)
|
|
87
|
+
@handler = handler
|
|
88
|
+
udp = @udp = UDP.bind(@host, @port)
|
|
89
|
+
others = preferred_sockets(udp)
|
|
90
|
+
endpoint = Raiha::QUIC::Endpoint.server(config: @config, random: SecureRandom, time: Time.now, now: Driver.clock,
|
|
91
|
+
connection_ids: Raiha::QUIC::RandomConnectionIDs.new(SecureRandom))
|
|
92
|
+
driver = @driver = Driver.new(endpoint, udp, self, others:)
|
|
93
|
+
done = @done = driver.new_cond
|
|
94
|
+
reader, @wake = IO.pipe
|
|
95
|
+
driver.start
|
|
96
|
+
waker = Thread.new { wake_on(reader, driver) }.tap { |thread| thread.name = "raiha-wake" }
|
|
97
|
+
driver.synchronize { done.wait until driver.stopped? }
|
|
98
|
+
failure = driver.failure and raise failure
|
|
99
|
+
nil
|
|
100
|
+
ensure
|
|
101
|
+
# Left by an exception as well, Interrupt say.
|
|
102
|
+
@driver&.stop
|
|
103
|
+
@driver&.join
|
|
104
|
+
reader&.close
|
|
105
|
+
@wake&.close
|
|
106
|
+
waker&.join
|
|
107
|
+
deadline = @stop_deadline || Driver.clock + STOP_TIMEOUT
|
|
108
|
+
@handlers.join(deadline)
|
|
109
|
+
@threads.join(deadline)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# [address, port] the socket is bound to, once `run` has bound it.
|
|
113
|
+
def local_address = @udp&.local_address
|
|
114
|
+
|
|
115
|
+
# Takes the connections to come with `tls_config` and `keyring`; those
|
|
116
|
+
# already taken keep theirs.
|
|
117
|
+
def reconfigure(tls_config:, keyring:)
|
|
118
|
+
configure = lambda do
|
|
119
|
+
@tls_config = tls_config
|
|
120
|
+
@keyring = keyring
|
|
121
|
+
end
|
|
122
|
+
(driver = @driver) ? driver.synchronize(&configure) : configure.call
|
|
123
|
+
nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Asks the server to stop, giving the sessions `timeout` seconds; the
|
|
127
|
+
# first call is the one that counts. It takes no lock and writes to a
|
|
128
|
+
# pipe without waiting, so a signal handler may call it.
|
|
129
|
+
def stop(timeout: STOP_TIMEOUT, error_code: 0)
|
|
130
|
+
@stop ||= [timeout, error_code]
|
|
131
|
+
@wake&.write_nonblock(".", exception: false)
|
|
132
|
+
nil
|
|
133
|
+
rescue IOError, SystemCallError
|
|
134
|
+
nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The driver's owner, under the core lock: a new connection is taken
|
|
138
|
+
# while there is room for it, a handler that has not ended taking room
|
|
139
|
+
# as its connection does.
|
|
140
|
+
def route(route, now)
|
|
141
|
+
endpoint = @driver&.endpoint or return
|
|
142
|
+
case route
|
|
143
|
+
when Raiha::QUIC::Route::Incoming
|
|
144
|
+
full = !@stop.nil? || @sessions.count { |session| !@started.key?(session) } + @handlers.taken >= @max_connections
|
|
145
|
+
case Admission.decide(route, full:, max_unvalidated: @max_unvalidated) { unvalidated(now) }
|
|
146
|
+
when :refuse then return endpoint.refuse(route.handle)
|
|
147
|
+
when :retry then return endpoint.retry(route.handle, now:)
|
|
148
|
+
end
|
|
149
|
+
hello = endpoint.admit(route.handle, now:)
|
|
150
|
+
return route(hello, now) if hello
|
|
151
|
+
@admitted << route.handle unless route.address_validated
|
|
152
|
+
when Raiha::QUIC::Route::ClientHello
|
|
153
|
+
@admitted.delete(route.handle)
|
|
154
|
+
advance_ticket_keys
|
|
155
|
+
tracer, pending = @qlog&.trace(:server, route.handle.original_destination_connection_id.unpack1("H*"), tls_config: @tls_config)
|
|
156
|
+
connection = endpoint.accept(route.handle, tls_config: @tls_config, keyring: @keyring, now:, tracer:, anti_replay: @anti_replay,
|
|
157
|
+
app_early_data_context: @app_early_data_context)
|
|
158
|
+
session = Session.new(connection, driver!, role: :server, threads: @threads, tracer:)
|
|
159
|
+
@sessions << session
|
|
160
|
+
@traces[session] = pending if pending
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# The driver's turn, under the core lock: each Session's, a handler for
|
|
165
|
+
# each whose handshake has completed, and the sessions of connections
|
|
166
|
+
# over let go.
|
|
167
|
+
def turn(now)
|
|
168
|
+
@admitted.reject! { |handle| handle.expired?(now) || handle.failed? }
|
|
169
|
+
@sessions.each do |session|
|
|
170
|
+
session.turn
|
|
171
|
+
start(session) if !@stop && !@started.key?(session) && session.connection.handshake_complete?
|
|
172
|
+
end
|
|
173
|
+
over = @sessions.select { |session| session.connection.closed? }
|
|
174
|
+
over.each do |session|
|
|
175
|
+
@started.delete(session)
|
|
176
|
+
@traces.delete(session)&.over!
|
|
177
|
+
end
|
|
178
|
+
@sessions -= over
|
|
179
|
+
stopping(now) if @stop
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def next_timeout = @stop_deadline
|
|
183
|
+
|
|
184
|
+
def stopped(failure)
|
|
185
|
+
@sessions.each { |session| session.abort(failure) }
|
|
186
|
+
@traces.each_value(&:over!)
|
|
187
|
+
ensure
|
|
188
|
+
@done&.broadcast
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Writes what the traces have taken to their files, outside the core
|
|
192
|
+
# lock, one writer at a time.
|
|
193
|
+
def flush_traces
|
|
194
|
+
qlog = @qlog or return
|
|
195
|
+
driver = @driver or return
|
|
196
|
+
@tracing.synchronize do
|
|
197
|
+
taken = driver.synchronize { qlog.take }
|
|
198
|
+
done = qlog.put(taken)
|
|
199
|
+
driver.synchronize { qlog.retire(done) }
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
private def unvalidated(now) = Admission.unvalidated(@admitted, @sessions.map(&:connection), now)
|
|
204
|
+
|
|
205
|
+
private def driver! = @driver || raise(Error::Usage, "the server is not running")
|
|
206
|
+
|
|
207
|
+
# A socket bound to "0.0.0.0" on the port of `udp` where `udp` is bound
|
|
208
|
+
# to "::" and takes no IPv4 (OpenBSD), and one bound to each preferred
|
|
209
|
+
# address that none bound so far sends from; none is left open when
|
|
210
|
+
# one cannot be bound, nor `udp`.
|
|
211
|
+
private def preferred_sockets(udp)
|
|
212
|
+
others = [] #: Array[UDP]
|
|
213
|
+
bound = udp.to_io.local_address
|
|
214
|
+
addresses = @config.preferred_address&.values || [] #: Array[UDP::address]
|
|
215
|
+
if bound.ipv6? && bound.ipv6_unspecified? && !udp.takes_ipv4?
|
|
216
|
+
addresses = [["0.0.0.0", bound.ip_port], *addresses] #: Array[UDP::address]
|
|
217
|
+
end
|
|
218
|
+
addresses.each do |host, port|
|
|
219
|
+
others << UDP.bind(host, port) unless [udp, *others].any? { |socket| socket.sends_from?([host, port]) }
|
|
220
|
+
rescue SystemCallError
|
|
221
|
+
[udp, *others].each(&:close)
|
|
222
|
+
raise
|
|
223
|
+
end
|
|
224
|
+
others
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
private def advance_ticket_keys
|
|
228
|
+
keyring = Admission.advance_ticket_keys(@tls_config, @keyring, Time.now) or return
|
|
229
|
+
@keyring = keyring
|
|
230
|
+
@tls_config = keyring.config
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Has the driver go round each time `stop` writes to the pipe.
|
|
234
|
+
private def wake_on(reader, driver)
|
|
235
|
+
until driver.stopped?
|
|
236
|
+
reader.wait_readable
|
|
237
|
+
reader.read_nonblock(64, exception: false)
|
|
238
|
+
driver.changed
|
|
239
|
+
end
|
|
240
|
+
rescue IOError
|
|
241
|
+
nil
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# A turn while stopping, as the Server of fibers has it; the last
|
|
245
|
+
# datagrams go, and the sockets close, once the turn is over.
|
|
246
|
+
private def stopping(now)
|
|
247
|
+
timeout, error_code = @stop || return
|
|
248
|
+
unless @stop_deadline
|
|
249
|
+
@stop_deadline = now + timeout
|
|
250
|
+
@sessions.each do |session|
|
|
251
|
+
session.stopping!(@stop_deadline)
|
|
252
|
+
session.connection.close(error_code:, application: true, now:) unless @started.key?(session)
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
deadline = @stop_deadline
|
|
256
|
+
return unless @sessions.empty? || (deadline && now >= deadline)
|
|
257
|
+
@sessions.each { |session| session.expire(error_code:) }
|
|
258
|
+
driver!.flush
|
|
259
|
+
driver!.stop
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# The handler of `session` in a thread of its own, counted until it
|
|
263
|
+
# ends. One that cannot be started closes the connection with
|
|
264
|
+
# INTERNAL_ERROR, as one that raises does.
|
|
265
|
+
private def start(session)
|
|
266
|
+
@started[session] = true
|
|
267
|
+
handler = @handler or return
|
|
268
|
+
driver = driver!
|
|
269
|
+
@handlers.count
|
|
270
|
+
@handlers.start("raiha-conn") do
|
|
271
|
+
raise Error::Usage, "the core lock of the driver is held where the application runs" if driver.mon_owned?
|
|
272
|
+
handler.call(session)
|
|
273
|
+
session.close_when_acknowledged
|
|
274
|
+
rescue Raiha::QUIC::ClosedError
|
|
275
|
+
nil
|
|
276
|
+
rescue StandardError
|
|
277
|
+
internal_error(session)
|
|
278
|
+
raise
|
|
279
|
+
end
|
|
280
|
+
rescue ThreadError
|
|
281
|
+
internal_error(session)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
private def internal_error(session)
|
|
285
|
+
session.connection.close(error_code: Raiha::QUIC::ErrorCode::INTERNAL_ERROR, application: false, now: Driver.clock)
|
|
286
|
+
session.changed
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|