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.
Files changed (347) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/LICENSE +202 -0
  4. data/README.md +94 -22
  5. data/docs/configuration.md +227 -0
  6. data/docs/development.md +99 -0
  7. data/docs/fiber-scheduler.md +591 -0
  8. data/docs/getting-started.md +190 -0
  9. data/docs/http3-client.md +303 -0
  10. data/docs/http3-server.md +301 -0
  11. data/docs/platforms.md +98 -0
  12. data/docs/qlog.md +86 -0
  13. data/docs/quic.md +268 -0
  14. data/docs/threads.md +127 -0
  15. data/examples/_certificate.rb +55 -0
  16. data/examples/high_level_loopback.rb +59 -0
  17. data/examples/http3_get_public.rb +68 -0
  18. data/examples/in_memory_handshake.rb +38 -0
  19. data/examples/qlog_output.rb +58 -0
  20. data/examples/qlog_roundtrips.rb +66 -0
  21. data/examples/quic_gem_loopback.rb +62 -0
  22. data/examples/stream_echo.rb +68 -0
  23. data/examples/udp_handshake.rb +76 -0
  24. data/lib/raiha/http3/action.rb +21 -0
  25. data/lib/raiha/http3/capsule.rb +86 -0
  26. data/lib/raiha/http3/connection.rb +983 -0
  27. data/lib/raiha/http3/error.rb +24 -0
  28. data/lib/raiha/http3/error_code.rb +27 -0
  29. data/lib/raiha/http3/event.rb +63 -0
  30. data/lib/raiha/http3/frame.rb +148 -0
  31. data/lib/raiha/http3/frame_reader.rb +114 -0
  32. data/lib/raiha/http3/message.rb +137 -0
  33. data/lib/raiha/http3/output.rb +61 -0
  34. data/lib/raiha/http3/priority.rb +52 -0
  35. data/lib/raiha/http3/qlog.rb +145 -0
  36. data/lib/raiha/http3/qpack/decoder.rb +302 -0
  37. data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
  38. data/lib/raiha/http3/qpack/encoder.rb +94 -0
  39. data/lib/raiha/http3/qpack/error.rb +39 -0
  40. data/lib/raiha/http3/qpack/error_code.rb +13 -0
  41. data/lib/raiha/http3/qpack/field.rb +15 -0
  42. data/lib/raiha/http3/qpack/huffman.rb +397 -0
  43. data/lib/raiha/http3/qpack/reader.rb +82 -0
  44. data/lib/raiha/http3/qpack/result.rb +26 -0
  45. data/lib/raiha/http3/qpack/static_table.rb +139 -0
  46. data/lib/raiha/http3/qpack/writer.rb +44 -0
  47. data/lib/raiha/http3/qpack.rb +26 -0
  48. data/lib/raiha/http3/receiving.rb +46 -0
  49. data/lib/raiha/http3/sending.rb +23 -0
  50. data/lib/raiha/http3/session.rb +31 -0
  51. data/lib/raiha/http3/settings.rb +81 -0
  52. data/lib/raiha/http3/structured_field.rb +120 -0
  53. data/lib/raiha/http3/tracer.rb +26 -0
  54. data/lib/raiha/http3/varint.rb +45 -0
  55. data/lib/raiha/http3.rb +19 -0
  56. data/lib/raiha/http3_binding.rb +139 -0
  57. data/lib/raiha/namespace.rb +11 -0
  58. data/lib/raiha/quic/config.rb +229 -0
  59. data/lib/raiha/quic/congestion/algorithms.rb +17 -0
  60. data/lib/raiha/quic/congestion/cubic.rb +258 -0
  61. data/lib/raiha/quic/congestion/new_reno.rb +161 -0
  62. data/lib/raiha/quic/congestion/pacing.rb +21 -0
  63. data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
  64. data/lib/raiha/quic/congestion/startup.rb +13 -0
  65. data/lib/raiha/quic/congestion.rb +15 -0
  66. data/lib/raiha/quic/connection.rb +2022 -0
  67. data/lib/raiha/quic/connection_ids.rb +62 -0
  68. data/lib/raiha/quic/crypto/aead.rb +68 -0
  69. data/lib/raiha/quic/crypto/header_protection.rb +42 -0
  70. data/lib/raiha/quic/crypto/initial.rb +23 -0
  71. data/lib/raiha/quic/crypto/keys.rb +32 -0
  72. data/lib/raiha/quic/crypto/mac.rb +15 -0
  73. data/lib/raiha/quic/crypto/random.rb +13 -0
  74. data/lib/raiha/quic/crypto.rb +18 -0
  75. data/lib/raiha/quic/crypto_stream.rb +161 -0
  76. data/lib/raiha/quic/datagrams.rb +91 -0
  77. data/lib/raiha/quic/endpoint.rb +785 -0
  78. data/lib/raiha/quic/error.rb +61 -0
  79. data/lib/raiha/quic/error_code.rb +34 -0
  80. data/lib/raiha/quic/event.rb +59 -0
  81. data/lib/raiha/quic/issued_connection_ids.rb +137 -0
  82. data/lib/raiha/quic/kept_packets.rb +41 -0
  83. data/lib/raiha/quic/mtu_search.rb +75 -0
  84. data/lib/raiha/quic/negotiation.rb +143 -0
  85. data/lib/raiha/quic/new_token.rb +55 -0
  86. data/lib/raiha/quic/pacer.rb +52 -0
  87. data/lib/raiha/quic/packet_protection.rb +310 -0
  88. data/lib/raiha/quic/path.rb +473 -0
  89. data/lib/raiha/quic/peer_connection_ids.rb +160 -0
  90. data/lib/raiha/quic/qlog.rb +260 -0
  91. data/lib/raiha/quic/ranges.rb +32 -0
  92. data/lib/raiha/quic/recovery/duty.rb +102 -0
  93. data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
  94. data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
  95. data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
  96. data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
  97. data/lib/raiha/quic/recovery.rb +641 -0
  98. data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
  99. data/lib/raiha/quic/retry_token.rb +54 -0
  100. data/lib/raiha/quic/route.rb +37 -0
  101. data/lib/raiha/quic/scheduler.rb +112 -0
  102. data/lib/raiha/quic/session.rb +47 -0
  103. data/lib/raiha/quic/stateless_reset.rb +38 -0
  104. data/lib/raiha/quic/stats.rb +24 -0
  105. data/lib/raiha/quic/streams/receive_window.rb +119 -0
  106. data/lib/raiha/quic/streams/receiver.rb +217 -0
  107. data/lib/raiha/quic/streams/sender.rb +200 -0
  108. data/lib/raiha/quic/streams.rb +687 -0
  109. data/lib/raiha/quic/test/bottleneck.rb +55 -0
  110. data/lib/raiha/quic/test/congestion_contract.rb +106 -0
  111. data/lib/raiha/quic/test/network.rb +244 -0
  112. data/lib/raiha/quic/test/random_loss.rb +34 -0
  113. data/lib/raiha/quic/test/reorder.rb +51 -0
  114. data/lib/raiha/quic/test/server.rb +54 -0
  115. data/lib/raiha/quic/test/vectors.rb +337 -0
  116. data/lib/raiha/quic/test/virtual_clock.rb +21 -0
  117. data/lib/raiha/quic/test.rb +17 -0
  118. data/lib/raiha/quic/tls_binding.rb +179 -0
  119. data/lib/raiha/quic/token.rb +28 -0
  120. data/lib/raiha/quic/tracer.rb +127 -0
  121. data/lib/raiha/quic/transmit.rb +14 -0
  122. data/lib/raiha/quic/transport_parameters.rb +244 -0
  123. data/lib/raiha/quic/version.rb +63 -0
  124. data/lib/raiha/quic/wire/codec.rb +136 -0
  125. data/lib/raiha/quic/wire/frame.rb +521 -0
  126. data/lib/raiha/quic/wire/header.rb +90 -0
  127. data/lib/raiha/quic/wire/packet.rb +130 -0
  128. data/lib/raiha/quic/wire/packet_number.rb +47 -0
  129. data/lib/raiha/quic/wire/retry.rb +62 -0
  130. data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
  131. data/lib/raiha/quic/wire.rb +9 -0
  132. data/lib/raiha/quic.rb +46 -0
  133. data/lib/raiha/runtime/addresses.rb +35 -0
  134. data/lib/raiha/runtime/admission.rb +37 -0
  135. data/lib/raiha/runtime/client.rb +190 -0
  136. data/lib/raiha/runtime/driver.rb +165 -0
  137. data/lib/raiha/runtime/error.rb +16 -0
  138. data/lib/raiha/runtime/http3.rb +363 -0
  139. data/lib/raiha/runtime/http3_fields.rb +38 -0
  140. data/lib/raiha/runtime/http3_server.rb +416 -0
  141. data/lib/raiha/runtime/kept.rb +36 -0
  142. data/lib/raiha/runtime/qlog.rb +204 -0
  143. data/lib/raiha/runtime/rack.rb +180 -0
  144. data/lib/raiha/runtime/server.rb +334 -0
  145. data/lib/raiha/runtime/session.rb +447 -0
  146. data/lib/raiha/runtime/threaded/client.rb +161 -0
  147. data/lib/raiha/runtime/threaded/driver.rb +230 -0
  148. data/lib/raiha/runtime/threaded/http3.rb +287 -0
  149. data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
  150. data/lib/raiha/runtime/threaded/server.rb +290 -0
  151. data/lib/raiha/runtime/threaded/session.rb +604 -0
  152. data/lib/raiha/runtime/threaded.rb +45 -0
  153. data/lib/raiha/runtime/udp.rb +410 -0
  154. data/lib/raiha/runtime/worker.rb +138 -0
  155. data/lib/raiha/runtime/workers.rb +248 -0
  156. data/lib/raiha/runtime.rb +37 -0
  157. data/lib/raiha/tls/anti_replay.rb +139 -0
  158. data/lib/raiha/tls/cipher_suite.rb +62 -0
  159. data/lib/raiha/tls/codec/error.rb +43 -0
  160. data/lib/raiha/tls/codec/reader.rb +95 -0
  161. data/lib/raiha/tls/codec/writer.rb +81 -0
  162. data/lib/raiha/tls/codec.rb +5 -0
  163. data/lib/raiha/tls/config.rb +318 -0
  164. data/lib/raiha/tls/crypto/aead.rb +64 -0
  165. data/lib/raiha/tls/crypto/certificate.rb +28 -0
  166. data/lib/raiha/tls/crypto/digest.rb +30 -0
  167. data/lib/raiha/tls/crypto/ecdh.rb +74 -0
  168. data/lib/raiha/tls/crypto/hash.rb +22 -0
  169. data/lib/raiha/tls/crypto/hkdf.rb +28 -0
  170. data/lib/raiha/tls/crypto/signature.rb +87 -0
  171. data/lib/raiha/tls/crypto.rb +19 -0
  172. data/lib/raiha/tls/error.rb +36 -0
  173. data/lib/raiha/tls/event.rb +45 -0
  174. data/lib/raiha/tls/extension/alpn.rb +35 -0
  175. data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
  176. data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
  177. data/lib/raiha/tls/extension/cookie.rb +27 -0
  178. data/lib/raiha/tls/extension/early_data.rb +54 -0
  179. data/lib/raiha/tls/extension/key_share.rb +90 -0
  180. data/lib/raiha/tls/extension/list.rb +49 -0
  181. data/lib/raiha/tls/extension/opaque.rb +21 -0
  182. data/lib/raiha/tls/extension/placement.rb +55 -0
  183. data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
  184. data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
  185. data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
  186. data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
  187. data/lib/raiha/tls/extension/registry.rb +50 -0
  188. data/lib/raiha/tls/extension/server_name.rb +63 -0
  189. data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
  190. data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
  191. data/lib/raiha/tls/extension/supported_groups.rb +32 -0
  192. data/lib/raiha/tls/extension/supported_versions.rb +53 -0
  193. data/lib/raiha/tls/extension.rb +32 -0
  194. data/lib/raiha/tls/handshake/acceptor.rb +164 -0
  195. data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
  196. data/lib/raiha/tls/handshake/certificate.rb +56 -0
  197. data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
  198. data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
  199. data/lib/raiha/tls/handshake/client_hello.rb +208 -0
  200. data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
  201. data/lib/raiha/tls/handshake/client_side.rb +578 -0
  202. data/lib/raiha/tls/handshake/conductor.rb +390 -0
  203. data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
  204. data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
  205. data/lib/raiha/tls/handshake/finished.rb +36 -0
  206. data/lib/raiha/tls/handshake/key_update.rb +38 -0
  207. data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
  208. data/lib/raiha/tls/handshake/machine.rb +238 -0
  209. data/lib/raiha/tls/handshake/message.rb +25 -0
  210. data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
  211. data/lib/raiha/tls/handshake/reassembler.rb +88 -0
  212. data/lib/raiha/tls/handshake/server_hello.rb +98 -0
  213. data/lib/raiha/tls/handshake/server_side.rb +672 -0
  214. data/lib/raiha/tls/handshake/violation.rb +12 -0
  215. data/lib/raiha/tls/handshake.rb +62 -0
  216. data/lib/raiha/tls/iana.rb +113 -0
  217. data/lib/raiha/tls/key_exchange.rb +58 -0
  218. data/lib/raiha/tls/key_schedule.rb +269 -0
  219. data/lib/raiha/tls/keylog.rb +40 -0
  220. data/lib/raiha/tls/keyring.rb +180 -0
  221. data/lib/raiha/tls/masked_inspect.rb +33 -0
  222. data/lib/raiha/tls/output.rb +32 -0
  223. data/lib/raiha/tls/profile.rb +82 -0
  224. data/lib/raiha/tls/record/layer.rb +228 -0
  225. data/lib/raiha/tls/record/protection.rb +67 -0
  226. data/lib/raiha/tls/record.rb +11 -0
  227. data/lib/raiha/tls/server_name.rb +42 -0
  228. data/lib/raiha/tls/session.rb +106 -0
  229. data/lib/raiha/tls/signature_scheme.rb +35 -0
  230. data/lib/raiha/tls/tcp/acceptor.rb +121 -0
  231. data/lib/raiha/tls/tcp/connection.rb +371 -0
  232. data/lib/raiha/tls/tcp/event.rb +15 -0
  233. data/lib/raiha/tls/tcp/socket.rb +161 -0
  234. data/lib/raiha/tls/tcp.rb +14 -0
  235. data/lib/raiha/tls/test/fixed_kem.rb +30 -0
  236. data/lib/raiha/tls/test/fixed_random.rb +18 -0
  237. data/lib/raiha/tls/test/rfc8448.rb +923 -0
  238. data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
  239. data/lib/raiha/tls/test.rb +13 -0
  240. data/lib/raiha/tls/ticket.rb +184 -0
  241. data/lib/raiha/tls/ticket_keys.rb +206 -0
  242. data/lib/raiha/tls/trace.rb +58 -0
  243. data/lib/raiha/tls/transcript.rb +94 -0
  244. data/lib/raiha/tls/verifier/default.rb +72 -0
  245. data/lib/raiha/tls/verifier/host_name.rb +75 -0
  246. data/lib/raiha/tls/verifier/none.rb +14 -0
  247. data/lib/raiha/tls/verifier.rb +24 -0
  248. data/lib/raiha/tls.rb +34 -0
  249. data/lib/raiha/version.rb +3 -1
  250. data/lib/raiha.rb +8 -5
  251. data/sig/patches/array.rbs +9 -0
  252. data/sig/patches/io_buffer.rbs +13 -0
  253. data/sig/patches/openssl.rbs +25 -0
  254. data/sig/patches/socket.rbs +29 -0
  255. data/sig/patches/string.rbs +26 -0
  256. data/sig/patches/strscan.rbs +12 -0
  257. data/sig/raiha/http3/capsule.rbs +32 -0
  258. data/sig/raiha/http3/connection.rbs +324 -0
  259. data/sig/raiha/http3/frame.rbs +138 -0
  260. data/sig/raiha/http3/frame_reader.rbs +39 -0
  261. data/sig/raiha/http3/message.rbs +23 -0
  262. data/sig/raiha/http3/priority.rbs +40 -0
  263. data/sig/raiha/http3/qlog.rbs +54 -0
  264. data/sig/raiha/http3/qpack.rbs +246 -0
  265. data/sig/raiha/http3/session.rbs +11 -0
  266. data/sig/raiha/http3/settings.rbs +26 -0
  267. data/sig/raiha/http3.rbs +2 -0
  268. data/sig/raiha/http3_binding.rbs +26 -0
  269. data/sig/raiha/namespace.rbs +2 -0
  270. data/sig/raiha/quic/config.rbs +89 -0
  271. data/sig/raiha/quic/congestion.rbs +213 -0
  272. data/sig/raiha/quic/connection.rbs +249 -0
  273. data/sig/raiha/quic/connection_ids.rbs +34 -0
  274. data/sig/raiha/quic/crypto.rbs +78 -0
  275. data/sig/raiha/quic/crypto_stream.rbs +34 -0
  276. data/sig/raiha/quic/datagrams.rbs +32 -0
  277. data/sig/raiha/quic/endpoint.rbs +179 -0
  278. data/sig/raiha/quic/frame.rbs +387 -0
  279. data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
  280. data/sig/raiha/quic/kept_packets.rbs +25 -0
  281. data/sig/raiha/quic/mtu_search.rbs +21 -0
  282. data/sig/raiha/quic/negotiation.rbs +33 -0
  283. data/sig/raiha/quic/new_token.rbs +14 -0
  284. data/sig/raiha/quic/pacer.rbs +16 -0
  285. data/sig/raiha/quic/packet_protection.rbs +63 -0
  286. data/sig/raiha/quic/path.rbs +106 -0
  287. data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
  288. data/sig/raiha/quic/qlog.rbs +74 -0
  289. data/sig/raiha/quic/ranges.rbs +7 -0
  290. data/sig/raiha/quic/recovery.rbs +376 -0
  291. data/sig/raiha/quic/retry_token.rbs +21 -0
  292. data/sig/raiha/quic/route.rbs +51 -0
  293. data/sig/raiha/quic/scheduler.rbs +36 -0
  294. data/sig/raiha/quic/session.rbs +17 -0
  295. data/sig/raiha/quic/stateless_reset.rbs +10 -0
  296. data/sig/raiha/quic/streams.rbs +244 -0
  297. data/sig/raiha/quic/test.rbs +354 -0
  298. data/sig/raiha/quic/tls_binding.rbs +58 -0
  299. data/sig/raiha/quic/token.rbs +11 -0
  300. data/sig/raiha/quic/tracer.rbs +71 -0
  301. data/sig/raiha/quic/transport_parameters.rbs +113 -0
  302. data/sig/raiha/quic/values.rbs +200 -0
  303. data/sig/raiha/quic/version.rbs +30 -0
  304. data/sig/raiha/quic/wire.rbs +199 -0
  305. data/sig/raiha/quic.rbs +2 -0
  306. data/sig/raiha/runtime/threaded.rbs +616 -0
  307. data/sig/raiha/runtime.rbs +982 -0
  308. data/sig/raiha/tls/anti_replay.rbs +41 -0
  309. data/sig/raiha/tls/cipher_suite.rbs +41 -0
  310. data/sig/raiha/tls/codec.rbs +66 -0
  311. data/sig/raiha/tls/config.rbs +122 -0
  312. data/sig/raiha/tls/crypto.rbs +86 -0
  313. data/sig/raiha/tls/error.rbs +21 -0
  314. data/sig/raiha/tls/event.rbs +61 -0
  315. data/sig/raiha/tls/extension.rbs +331 -0
  316. data/sig/raiha/tls/handshake.rbs +311 -0
  317. data/sig/raiha/tls/iana.rbs +13 -0
  318. data/sig/raiha/tls/key_exchange.rbs +33 -0
  319. data/sig/raiha/tls/key_schedule.rbs +58 -0
  320. data/sig/raiha/tls/keylog.rbs +20 -0
  321. data/sig/raiha/tls/keyring.rbs +64 -0
  322. data/sig/raiha/tls/machine.rbs +429 -0
  323. data/sig/raiha/tls/masked_inspect.rbs +12 -0
  324. data/sig/raiha/tls/output.rbs +37 -0
  325. data/sig/raiha/tls/profile.rbs +26 -0
  326. data/sig/raiha/tls/record.rbs +79 -0
  327. data/sig/raiha/tls/server_name.rbs +10 -0
  328. data/sig/raiha/tls/session.rbs +48 -0
  329. data/sig/raiha/tls/signature_scheme.rbs +20 -0
  330. data/sig/raiha/tls/tcp.rbs +134 -0
  331. data/sig/raiha/tls/test.rbs +143 -0
  332. data/sig/raiha/tls/ticket.rbs +68 -0
  333. data/sig/raiha/tls/ticket_keys.rbs +67 -0
  334. data/sig/raiha/tls/trace.rbs +99 -0
  335. data/sig/raiha/tls/transcript.rbs +22 -0
  336. data/sig/raiha/tls/verifier.rbs +54 -0
  337. data/sig/raiha/tls.rbs +5 -0
  338. data/sig/raiha.rbs +3 -0
  339. metadata +355 -57
  340. data/.gitignore +0 -10
  341. data/CODE_OF_CONDUCT.md +0 -74
  342. data/Gemfile +0 -4
  343. data/Gemfile.lock +0 -22
  344. data/Rakefile +0 -10
  345. data/bin/console +0 -14
  346. data/bin/setup +0 -8
  347. data/raiha.gemspec +0 -40
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The token of a Retry packet (RFC 9000 "Address Validation Using Retry
5
+ # Packets"). It comes back on the client's next Initial and shows the
6
+ # server, which keeps nothing in between, that the client receives at the
7
+ # address it sends from, what the client's first Destination Connection
8
+ # ID was, and the version the Retry went in, which the token belongs to
9
+ # (RFC 9369 "TLS Resumption and NEW_TOKEN Tokens"):
10
+ #
11
+ # kind (0, a Retry token) || expiry (8 bytes, seconds since the epoch)
12
+ # || version (4) || ODCID length (1) || ODCID || MAC (32)
13
+ #
14
+ # The MAC is HMAC-SHA256 under the Endpoint's retry_token_key over all
15
+ # before it and the client's address, which the token does not carry. The
16
+ # kind tells it from the tokens of NEW_TOKEN frames, which the server must
17
+ # be able to (RFC 9000 "Address Validation for Future Connections"); it
18
+ # lives `LIFETIME` seconds of the wall clock, which every worker shares.
19
+ module RetryToken
20
+ KIND = 0
21
+ LIFETIME = 10
22
+ EXPIRY_LENGTH = 8
23
+ VERSION_LENGTH = 4
24
+ # kind, expiry, version, the ODCID's length and the MAC.
25
+ OVERHEAD = 1 + EXPIRY_LENGTH + VERSION_LENGTH + 1 + Crypto::Mac::LENGTH
26
+
27
+ # What a token tells: the client's first Destination Connection ID and
28
+ # the number of the version the Retry went in.
29
+ class Verified < Data.define(:original_destination_connection_id, :version)
30
+ end
31
+
32
+ # A token for `address` of the Retry in the version numbered `version`.
33
+ def self.mint(key:, original_destination_connection_id:, version:, address:, time:)
34
+ odcid = original_destination_connection_id
35
+ body = [KIND, time.to_i + LIFETIME, version, odcid.bytesize].pack("CQ>NC") + odcid
36
+ body + Crypto::Mac.digest(key, body + bound(address))
37
+ end
38
+
39
+ # What a token made for `address` that has not expired tells, a
40
+ # Verified; nil for any other.
41
+ def self.verify(token, key:, address:, time:)
42
+ return nil if token.bytesize < OVERHEAD || token.getbyte(0) != KIND
43
+ body = token.byteslice(0, token.bytesize - Crypto::Mac::LENGTH) #: String
44
+ mac = token.byteslice(body.bytesize, Crypto::Mac::LENGTH) #: String
45
+ return nil unless Crypto::Mac.same?(mac, Crypto::Mac.digest(key, body + bound(address)))
46
+ expiry, version, odcid_length = body.unpack("Q>NC", offset: 1) #: [Integer, Integer, Integer]
47
+ return nil if time.to_i > expiry || body.bytesize != OVERHEAD - Crypto::Mac::LENGTH + odcid_length
48
+ odcid = body.byteslice(OVERHEAD - Crypto::Mac::LENGTH, odcid_length) #: String
49
+ Verified.new(original_destination_connection_id: odcid, version:)
50
+ end
51
+
52
+ private_class_method def self.bound(address) = "#{address.fetch(0)}\0".b + [address.fetch(1)].pack("n")
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # What an Endpoint did with a datagram it received.
5
+ module Route
6
+ # Given to the connection it belongs to.
7
+ class Deliver < Data.define(:connection)
8
+ end
9
+
10
+ # The first stage of a new connection, from what the headers of a
11
+ # client's first Initial alone say: the application `retry`s, `refuse`s
12
+ # or `admit`s `handle`, and can retry it at this stage alone. `address_validated` is whether a token showed
13
+ # the client receives at `remote`.
14
+ class Incoming < Data.define(:handle, :remote, :address_validated)
15
+ end
16
+
17
+ # The second stage: the ClientHello of `handle` is whole. `accepted` is
18
+ # the TLS layer's Handshake::Accepted, unauthenticated, which the
19
+ # application picks a TLS Config by before it `accept`s.
20
+ class ClientHello < Data.define(:handle, :accepted)
21
+ end
22
+
23
+ # A datagram the Endpoint answers itself, with nothing kept: a Version
24
+ # Negotiation, a Stateless Reset, or a CONNECTION_CLOSE for a Retry
25
+ # token that does not verify.
26
+ class Respond < Data.define(:transmit)
27
+ end
28
+
29
+ # For another worker, whose connection ID it has.
30
+ class Foreign < Data.define(:worker, :datagram)
31
+ end
32
+
33
+ # Dropped, for `reason`.
34
+ class Drop < Data.define(:reason)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The order the streams with something to send take turns in, a frame
5
+ # a turn. Kept apart from Streams, which knows what each stream has to
6
+ # send.
7
+ #
8
+ # The order follows the priorities of RFC 9218: an urgency from 0, the
9
+ # most urgent, to 7, and whether the stream is incremental. A stream
10
+ # takes turns only while no more urgent one has something to send. Of
11
+ # those of one urgency, the incremental streams take turns together
12
+ # with the one not incremental of the lowest ID, so that those not
13
+ # incremental go one by one in the order of their IDs ("Server
14
+ # Scheduling"), and neither kind starves the other, which the same
15
+ # section leaves to the implementation. A stream nobody gave a priority
16
+ # has DEFAULT_URGENCY and is incremental, so that streams take turns
17
+ # alike unless told otherwise.
18
+ class Scheduler
19
+ URGENCIES = 0..7
20
+ DEFAULT_URGENCY = 3
21
+ # The place in a level's turns of the streams not incremental, which
22
+ # the one of the lowest ID takes.
23
+ SEQUENTIAL = -1
24
+
25
+ # The streams of one urgency with something to send: the turns, and
26
+ # those not incremental, by ID.
27
+ class Level
28
+ attr_reader :turns, :sequential
29
+
30
+ def initialize
31
+ @turns = [] #: Array[Integer]
32
+ @sequential = [] #: Array[Integer]
33
+ end
34
+
35
+ def empty? = @turns.empty?
36
+
37
+ def add(stream_id, incremental:)
38
+ return @turns << stream_id if incremental
39
+ @turns << SEQUENTIAL if @sequential.empty?
40
+ index = @sequential.bsearch_index { |queued| queued > stream_id } || @sequential.size
41
+ @sequential.insert(index, stream_id)
42
+ end
43
+
44
+ def remove(stream_id, incremental:)
45
+ return @turns.delete(stream_id) if incremental
46
+ @sequential.delete(stream_id)
47
+ @turns.delete(SEQUENTIAL) if @sequential.empty?
48
+ end
49
+
50
+ def current
51
+ turn = @turns.first
52
+ turn == SEQUENTIAL ? @sequential.first : turn
53
+ end
54
+ end
55
+
56
+ def initialize
57
+ @levels = URGENCIES.map { Level.new }
58
+ # The priorities given, as [urgency, incremental], and the streams
59
+ # taking turns.
60
+ @priorities = {} #: Hash[Integer, [Integer, bool]]
61
+ @ready = {} #: Hash[Integer, true]
62
+ end
63
+
64
+ # `stream_id` goes with `urgency` and as `incremental` from now on,
65
+ # behind those already taking turns at that urgency.
66
+ def prioritize(stream_id, urgency:, incremental:)
67
+ priority = [urgency, incremental] #: [Integer, bool]
68
+ return if priority_of(stream_id) == priority
69
+ ready = @ready.key?(stream_id)
70
+ idle(stream_id)
71
+ @priorities[stream_id] = priority
72
+ ready(stream_id) if ready
73
+ end
74
+
75
+ # `stream_id` has something to send: it takes turns from now on.
76
+ def ready(stream_id)
77
+ return if @ready.key?(stream_id)
78
+ @ready[stream_id] = true
79
+ urgency, incremental = priority_of(stream_id)
80
+ @levels.fetch(urgency).add(stream_id, incremental:)
81
+ end
82
+
83
+ # `stream_id` has nothing to send for now; its priority stays.
84
+ def idle(stream_id)
85
+ return unless @ready.delete(stream_id)
86
+ urgency, incremental = priority_of(stream_id)
87
+ @levels.fetch(urgency).remove(stream_id, incremental:)
88
+ end
89
+
90
+ # `stream_id` is gone.
91
+ def forget(stream_id)
92
+ idle(stream_id)
93
+ @priorities.delete(stream_id)
94
+ end
95
+
96
+ # The stream whose turn it is, and its urgency; nil when none has a
97
+ # turn.
98
+ def current = level&.current
99
+
100
+ def urgency = @levels.index { |level| !level.empty? }
101
+
102
+ # The stream whose turn it was took it: the next one's comes.
103
+ def advance
104
+ level&.turns&.rotate!
105
+ end
106
+
107
+ # The most urgent level with streams taking turns.
108
+ private def level = @levels.find { |level| !level.empty? }
109
+
110
+ private def priority_of(stream_id) = @priorities.fetch(stream_id) { [DEFAULT_URGENCY, true] }
111
+ end
112
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # A session a QUIC client may resume, as a value: the TLS layer's, the
5
+ # server's transport parameters that 0-RTT may use (RFC 9000 "Values of
6
+ # Transport Parameters for 0-RTT"), and the ALPN protocol and QUIC
7
+ # version of the connection the ticket came on, which 0-RTT keeps. The
8
+ # application keeps it where it likes, `to_h` and `from_h` turning it into
9
+ # plain values and back.
10
+ class Session < Data.define(:tls, :transport_parameters, :alpn, :version)
11
+ def to_h = { tls: tls.to_h, transport_parameters: transport_parameters.to_h, alpn:, version: }
12
+
13
+ def self.from_h(hash)
14
+ raise ArgumentError, "a session is a Hash" unless hash.is_a?(Hash)
15
+ parameters = hash.fetch(:transport_parameters)
16
+ unless parameters.is_a?(Hash) && parameters.keys.sort == RememberedTransportParameters.members.sort
17
+ raise ArgumentError, "transport_parameters must name every remembered parameter"
18
+ end
19
+ alpn = hash.fetch(:alpn)
20
+ raise ArgumentError, "alpn must be a String or nil" unless alpn.nil? || alpn.is_a?(String)
21
+ version = hash.fetch(:version)
22
+ raise ArgumentError, "version must be an Integer" unless version.is_a?(Integer)
23
+ tls = hash.fetch(:tls)
24
+ raise ArgumentError, "tls must be a Hash" unless tls.is_a?(Hash)
25
+ new(tls: Raiha::TLS::Session.from_h(tls), transport_parameters: remembered(parameters), alpn: alpn&.b&.freeze, version:)
26
+ rescue KeyError => e
27
+ raise ArgumentError, "a session lacks #{e.key}"
28
+ end
29
+
30
+ private_class_method def self.remembered(parameters)
31
+ values = parameters.to_h do |name, value|
32
+ case name
33
+ when :max_idle_timeout
34
+ raise ArgumentError, "max_idle_timeout must be a Numeric of 0 or more" unless value.is_a?(Numeric) && !value.negative?
35
+ [name, Float(value)]
36
+ when :disable_active_migration
37
+ raise ArgumentError, "disable_active_migration must be true or false" unless value == true || value == false
38
+ [name, value]
39
+ else
40
+ raise ArgumentError, "#{name} must be an Integer of 0 or more" unless value.is_a?(Integer) && !value.negative?
41
+ [name, value]
42
+ end
43
+ end
44
+ RememberedTransportParameters.new(**values) # steep:ignore
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # RFC 9000 "Stateless Reset": what an endpoint that has lost a
5
+ # connection sends to end it, which looks like a short header packet but
6
+ # ends in the token the endpoint gave out with the connection ID.
7
+ module StatelessReset
8
+ TOKEN_LENGTH = 16
9
+ # The shortest a reset is: a short header packet with a 1-byte packet
10
+ # number and an AEAD tag, which RFC 9000 asks a reset to be as long as.
11
+ MIN_LENGTH = 21
12
+
13
+ # The token of `connection_id`, which only the holder of `key` can make
14
+ # and which no state is kept for (RFC 9000 "Calculating a Stateless Reset
15
+ # Token"): HMAC-SHA256 of the connection ID, cut to 16 bytes.
16
+ def self.token(key:, connection_id:)
17
+ Crypto::Mac.digest(key, connection_id).byteslice(0, TOKEN_LENGTH) #: String
18
+ end
19
+
20
+ # A reset of `length` bytes ending in `token`: random bytes from `random`
21
+ # under a first byte with the form bit clear and the fixed bit set.
22
+ def self.packet(token:, length:, random:)
23
+ raise ArgumentError, "a stateless reset is #{MIN_LENGTH} bytes at least, not #{length}" if length < MIN_LENGTH
24
+ unpredictable = random.bytes(length - TOKEN_LENGTH).b
25
+ first = unpredictable.getbyte(0) #: Integer
26
+ unpredictable.setbyte(0, (first & 0x3f) | 0x40)
27
+ unpredictable + token
28
+ end
29
+
30
+ # Whether `datagram` ends in one of `tokens`, each compared in constant
31
+ # time.
32
+ def self.match?(datagram, tokens)
33
+ return false if datagram.bytesize < MIN_LENGTH
34
+ tail = datagram.byteslice(-TOKEN_LENGTH, TOKEN_LENGTH) #: String
35
+ tokens.any? { |token| Crypto::Mac.same?(tail, token) }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # What a connection has done, as `Connection#stats` gives it. `rtt` is
5
+ # the smoothed RTT, and it, `rtt_var`, `min_rtt` (nil before the first
6
+ # sample), `congestion_window` and `bytes_in_flight` are the current
7
+ # path's; `packets_spuriously_lost` counts the packets declared lost and
8
+ # acknowledged after. `handshake_duration` is the seconds from the
9
+ # connection's start to the handshake's completion, nil before it.
10
+ # `receive_window` is the connection's receive window, which grows as the
11
+ # application reads fast, and `send_buffer_bytes` the stream data not
12
+ # sent or not acknowledged; `blocked_sent` counts the BLOCKED frames the
13
+ # peer's limits made this end send, not those sent again, and
14
+ # `blocked_received` those the peer sent, so that the slow end shows
15
+ # without a qlog.
16
+ class Stats < Data.define(
17
+ :rtt, :rtt_var, :min_rtt, :congestion_window, :bytes_in_flight,
18
+ :packets_sent, :packets_received, :packets_lost, :packets_spuriously_lost,
19
+ :bytes_sent, :bytes_received,
20
+ :path_mtu, :handshake_duration,
21
+ :receive_window, :send_buffer_bytes, :blocked_sent, :blocked_received
22
+ )
23
+ end
24
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ class Streams
5
+ # The receive side of flow control, of one stream or of the whole
6
+ # connection: the limit told to the peer, how far the peer has sent,
7
+ # how much has been consumed, and the window. Credit goes back as the
8
+ # data is consumed, read by the application or abandoned, so that a
9
+ # peer whose data is not read stops.
10
+ #
11
+ # A new limit, what is consumed plus the window, is due once what is
12
+ # left of the old one is three quarters of the window or less; the
13
+ # peer then still has three quarters of a window while the update
14
+ # travels. Its value is made when the frame is put together, and the
15
+ # window grows then too: when what was read since the last time it was
16
+ # judged is over half the window and took less than four round trips'
17
+ # share of it, the window doubles, up to its maximum. It stops about
18
+ # four bandwidth-delay products wide, and never shrinks (RFC 9000
19
+ # "Increasing Flow Control Limits").
20
+ class ReceiveWindow
21
+ # The share of the window left that makes a new limit due.
22
+ UPDATE_THRESHOLD = Rational(3, 4)
23
+ # Round trips to read a window in, faster than which it doubles.
24
+ ROUND_TRIPS = 4
25
+
26
+ attr_reader :size
27
+ # The limit last told to the peer.
28
+ attr_reader :limit
29
+ # The end of the furthest data received.
30
+ attr_reader :received
31
+ attr_reader :consumed
32
+
33
+ # `size` is the window, first told as the limit, and `max` the most it
34
+ # grows to.
35
+ def initialize(size:, max:, now:)
36
+ @size = size
37
+ @max = max
38
+ @limit = size
39
+ @received = 0
40
+ @consumed = 0
41
+ @blocked = false
42
+ @resend = false
43
+ start_period(now)
44
+ end
45
+
46
+ # The peer has sent up to `end_offset` in a frame of `frame_type`:
47
+ # FLOW_CONTROL_ERROR past the limit.
48
+ def receive(end_offset, frame_type:)
49
+ if end_offset > @limit
50
+ raise Violation.new("data up to #{end_offset} past the limit #{@limit}", code: ErrorCode::FLOW_CONTROL_ERROR, frame_type:)
51
+ end
52
+ @received = end_offset if end_offset > @received
53
+ end
54
+
55
+ # `bytes` more are read or abandoned.
56
+ def consume(bytes)
57
+ @consumed += bytes
58
+ end
59
+
60
+ # The peer is blocked by the limit: credit not yet told goes at once,
61
+ # whatever the threshold. A peer blocked by a limit that has nothing
62
+ # more to give is held back on purpose, and gets nothing.
63
+ def blocked!
64
+ @blocked = true
65
+ end
66
+
67
+ # A frame with limit `maximum` was lost: the limit goes again if it is
68
+ # still the latest (loss recovery's design, "Retransmission").
69
+ def lost(maximum)
70
+ @resend = true if maximum == @limit
71
+ end
72
+
73
+ def update_due? = @resend || credit_due?
74
+
75
+ # The limit to send now, made the one told: a new one when credit is
76
+ # due, the window having first grown when it was read fast enough,
77
+ # else the last one again. `rtt` is the smoothed RTT, nil before the
78
+ # path's first sample, until which the window does not grow, as the
79
+ # initial RTT is longer than most and would make it grow too soon.
80
+ def advance(now:, rtt:)
81
+ if credit_due?
82
+ tune(now, rtt)
83
+ @limit = next_limit
84
+ end
85
+ @blocked = false
86
+ @resend = false
87
+ @limit
88
+ end
89
+
90
+ # The window grows to `size` at least, as far as its maximum: the
91
+ # connection's follows a stream's.
92
+ def grow_to(size, now:)
93
+ return if size <= @size
94
+ @size = [size, @max].min
95
+ start_period(now)
96
+ end
97
+
98
+ private def credit_due?
99
+ return false if next_limit <= @limit
100
+ @blocked || @limit - @consumed <= @size * UPDATE_THRESHOLD
101
+ end
102
+
103
+ # What is consumed plus the window, short of what a varint holds.
104
+ private def next_limit = [@consumed + @size, Wire::Varint::MAX].min
105
+
106
+ private def tune(now, rtt)
107
+ read = @consumed - @period_consumed
108
+ return if read <= @size / 2 || rtt.nil?
109
+ @size = [2 * @size, @max].min if now - @period_start < ROUND_TRIPS * read.fdiv(@size) * rtt
110
+ start_period(now)
111
+ end
112
+
113
+ private def start_period(now)
114
+ @period_start = now
115
+ @period_consumed = @consumed
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,217 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ class Streams
5
+ # The receiving part of a stream (RFC 9000 "Receiving Stream States"):
6
+ # its receive window, the data received but not read, and the final
7
+ # size once known.
8
+ #
9
+ # Abandoned, when the application stops reading or the peer resets the
10
+ # stream, it keeps no data: what was received and not read is
11
+ # consumed, and so is what comes after, so that the credit goes back
12
+ # (flow control's design, "Accounting").
13
+ #
14
+ # Data waits as ranges that neither overlap nor touch, in order, each
15
+ # in a String of the stream's own, so that nothing of a datagram is
16
+ # kept; a range is found with `bsearch_index` and joined to those it
17
+ # touches. The holes before the furthest range, from where the
18
+ # application has read, are counted, so that the connection can keep
19
+ # their number down.
20
+ class Receiver
21
+ # Data received, from `offset` on.
22
+ class Chunk
23
+ attr_accessor :offset, :data
24
+
25
+ def initialize(offset, data)
26
+ @offset = offset
27
+ @data = data
28
+ end
29
+
30
+ def end_offset = offset + data.bytesize
31
+ end
32
+
33
+ attr_reader :window
34
+ # The final size, nil until a frame with FIN tells it.
35
+ attr_reader :final_size
36
+ # How far the application has read.
37
+ attr_reader :read_offset
38
+
39
+ def initialize(window:)
40
+ @window = window
41
+ @ranges = [] #: Array[Chunk]
42
+ @read_offset = 0
43
+ @final_size = nil #: Integer?
44
+ @stopped = false
45
+ @reset = false
46
+ # Whether a read gave the end.
47
+ @end_read = false
48
+ # The error code of the STOP_SENDING the application asked for, and
49
+ # whether it is due.
50
+ @stop_code = nil #: Integer?
51
+ @stop_due = false
52
+ end
53
+
54
+ # Takes the end of a frame of `frame_type` that ends at `end_offset`,
55
+ # with FIN or not, before any of its data is kept: FINAL_SIZE_ERROR
56
+ # when it does not keep to the final size, known or now told, then
57
+ # FLOW_CONTROL_ERROR past the stream's limit (RFC 9000 "Stream Final
58
+ # Size"). The bytes the stream has been received further by, which
59
+ # the connection's window takes.
60
+ def admit(end_offset, fin:, frame_type:)
61
+ if (final = @final_size)
62
+ if end_offset > final || (fin && end_offset != final)
63
+ raise Violation.new("data up to #{end_offset} of a stream of #{final} bytes", code: ErrorCode::FINAL_SIZE_ERROR, frame_type:)
64
+ end
65
+ elsif fin && end_offset < @window.received
66
+ raise Violation.new("a final size of #{end_offset} under #{@window.received} received", code: ErrorCode::FINAL_SIZE_ERROR,
67
+ frame_type:)
68
+ end
69
+ before = @window.received
70
+ @window.receive(end_offset, frame_type:)
71
+ @final_size = end_offset if fin
72
+ @window.received - before
73
+ end
74
+
75
+ # The holes there would be with `length` bytes at `offset` too.
76
+ def gaps_with(offset, length)
77
+ start, finish = clip(offset, length)
78
+ return gaps if finish <= start
79
+ first, last = touching(start, finish)
80
+ count = @ranges.size - (last - first) + 1
81
+ head = first.zero? ? [start, @ranges.first&.offset || start].min : @ranges.fetch(0).offset
82
+ count - (head == @read_offset ? 1 : 0)
83
+ end
84
+
85
+ def gaps
86
+ return 0 if @ranges.empty?
87
+ @ranges.size - (@ranges.fetch(0).offset == @read_offset ? 1 : 0)
88
+ end
89
+
90
+ # Keeps `data` at `offset`, admitted, as far as it was not read or
91
+ # received before; what was received before stays. The ranges it
92
+ # touches become one: the first goes on growing when the data starts
93
+ # in it, which is how data in order comes.
94
+ def insert(offset, data)
95
+ start, finish = clip(offset, data.bytesize)
96
+ return if finish <= start
97
+ first, last = touching(start, finish)
98
+ joined = @ranges[first, last - first] || []
99
+ head = joined.first
100
+ if head && head.offset <= start
101
+ joined = joined.drop(1)
102
+ chunk = head
103
+ else
104
+ chunk = Chunk.new(start, "".b)
105
+ end
106
+ # Ranges never touch, so data received fills what lies between.
107
+ joined.each do |range|
108
+ between = data.byteslice(chunk.end_offset - offset, range.offset - chunk.end_offset) #: String
109
+ chunk.data << between << range.data
110
+ end
111
+ if chunk.end_offset < finish
112
+ rest = data.byteslice(chunk.end_offset - offset, finish - chunk.end_offset) #: String
113
+ chunk.data << rest
114
+ end
115
+ @ranges[first, last - first] = [chunk]
116
+ end
117
+
118
+ # Up to `max_bytes` of the data that runs on from what was read, ""
119
+ # when none does yet, and nil once all of it up to the final size
120
+ # has been read, as IO#read has it at the end of a file. What is read
121
+ # is consumed in the receive window.
122
+ def read(max_bytes)
123
+ head = @ranges.first
124
+ if head && head.offset == @read_offset
125
+ chunk = head.data.byteslice(0, max_bytes) #: String
126
+ if chunk.bytesize == head.data.bytesize
127
+ @ranges.shift
128
+ else
129
+ head.data = head.data.byteslice(chunk.bytesize..) # steep:ignore
130
+ head.offset += chunk.bytesize
131
+ end
132
+ @read_offset += chunk.bytesize
133
+ @window.consume(chunk.bytesize)
134
+ return chunk
135
+ end
136
+ if @final_size == @read_offset
137
+ @end_read = true
138
+ return nil
139
+ end
140
+ "".b
141
+ end
142
+
143
+ # The application stops reading with `error_code`: STOP_SENDING is
144
+ # due, and what was received and not read is consumed. The bytes
145
+ # consumed.
146
+ def stop!(error_code)
147
+ return 0 if @stopped || @reset
148
+ @stopped = true
149
+ @stop_code = error_code
150
+ @stop_due = true
151
+ drop_to(@window.received)
152
+ end
153
+
154
+ # The peer reset the stream at `final_size`, admitted: what was not
155
+ # read up to it is consumed. The bytes consumed.
156
+ def reset!(final_size)
157
+ @reset = true
158
+ @final_size = final_size
159
+ @stop_due = false
160
+ drop_to(final_size)
161
+ end
162
+
163
+ def reset? = @reset
164
+
165
+ # Whether the stream is over here: read to its final size, or reset.
166
+ def finished? = @final_size == @read_offset
167
+
168
+ # Whether the stream keeps no data any more.
169
+ def abandoned? = @stopped || @reset
170
+
171
+ # Data admitted to an abandoned stream is consumed as it comes. The
172
+ # bytes consumed.
173
+ def discard = drop_to(@window.received)
174
+
175
+ # The error code of the STOP_SENDING due, nil when none is.
176
+ def stop_due = @stop_due ? @stop_code : nil
177
+
178
+ # STOP_SENDING went.
179
+ def stop_sent
180
+ @stop_due = false
181
+ end
182
+
183
+ # STOP_SENDING was lost: it goes again unless the peer has reset the
184
+ # stream since, or sent all of it.
185
+ def stop_lost
186
+ @stop_due = true unless @reset || (@final_size && @window.received == @final_size)
187
+ end
188
+
189
+ # Whether a read gives anything: data, or the end not read yet.
190
+ def readable?
191
+ head = @ranges.first
192
+ (head && head.offset == @read_offset) || (@final_size == @read_offset && !@end_read)
193
+ end
194
+
195
+ # The data up to `offset` is consumed, and none is kept: the bytes
196
+ # consumed.
197
+ private def drop_to(offset)
198
+ consumed = [offset - @read_offset, 0].max
199
+ @ranges.clear
200
+ @read_offset += consumed
201
+ @window.consume(consumed)
202
+ consumed
203
+ end
204
+
205
+ # `offset` and `length` cut to what is past the read data.
206
+ private def clip(offset, length) = [[offset, @read_offset].max, offset + length]
207
+
208
+ # The indices of the ranges that overlap or touch start...finish:
209
+ # first...last.
210
+ private def touching(start, finish)
211
+ first = @ranges.bsearch_index { |range| range.end_offset >= start } || @ranges.size
212
+ last = @ranges.bsearch_index { |range| range.offset > finish } || @ranges.size
213
+ [first, last]
214
+ end
215
+ end
216
+ end
217
+ end