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,473 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # One path of a connection, with what loss recovery keeps for it: the RTT,
5
+ # the congestion controller made from the Config, the bytes and
6
+ # ack-eliciting packets in flight on it, and its Pacer, whose bucket
7
+ # holds RFC 9002's initial window whatever the Config's is, or what the
8
+ # pacing rate gives in one granularity when that is more, and its
9
+ # RateMeter. A new path starts them all over (RFC 9000 "Connection
10
+ # Migration"); packets sent on a path that is gone count for nothing.
11
+ #
12
+ # A path goes between a local and a remote address, and keeps what came
13
+ # on it and what went until the remote address is validated, which the
14
+ # amplification limit goes by (RFC 9000 "Address Validation").
15
+ class Path
16
+ # RFC 9000 "Address Validation": an endpoint sends no more than three
17
+ # times what it received to an address not validated.
18
+ AMPLIFICATION_FACTOR = 3
19
+ # RFC 9000 "Datagram Size": the least every path carries.
20
+ MIN_DATAGRAM_SIZE = 1200
21
+ # PTOs in a row with no ACK that may be a black hole: from the first,
22
+ # as long as persistent congestion takes, three PTOs.
23
+ BLACK_HOLE_PTOS = 2
24
+
25
+ attr_reader :rtt
26
+ attr_reader :bytes_in_flight
27
+ attr_reader :ack_eliciting_in_flight
28
+ attr_reader :packets_in_flight
29
+ attr_reader :max_datagram_size
30
+
31
+ # The addresses the path goes between, [host, port] each; nil while not
32
+ # known yet.
33
+ attr_reader :local
34
+ attr_reader :remote
35
+
36
+ def initialize(config:, local: nil, remote: nil, validated: false, destination_connection_id: nil,
37
+ max_datagram_size: config.initial_max_datagram_size, tracer: nil)
38
+ @config = config
39
+ @local = local
40
+ @remote = remote
41
+ @validated = validated
42
+ @received_unvalidated = 0
43
+ @sent_unvalidated = 0
44
+ @challenge = nil #: String?
45
+ @destination_connection_id = destination_connection_id
46
+ @challenge_due_at = nil #: Float?
47
+ @challenge_interval = nil #: Float?
48
+ @validation_deadline = nil #: Float?
49
+ @challenges_sent = {} #: Hash[String, bool]
50
+ @full_size_due = false
51
+ @validation_owed = false
52
+ @max_datagram_size = max_datagram_size
53
+ @mtu_search = nil #: MTUSearch?
54
+ @tracer = tracer
55
+ start_over
56
+ @abandoned = false
57
+ @ecn_state = ecn_family? ? :testing : :failed
58
+ @ecn_testing_sent = 0
59
+ @ecn_testing_lost = 0
60
+ # PTOs in a row with nothing of the path's acknowledged.
61
+ @timed_out = 0
62
+ end
63
+
64
+ # The addresses a datagram that came on the path names, `local` the one
65
+ # it came to and `remote` the one it came from, fill in those the path
66
+ # does not know yet: a server learns both from the client's first
67
+ # Initial, a client its own from the first datagram it takes.
68
+ def learn(local:, remote:)
69
+ @local ||= local
70
+ @remote ||= remote
71
+ @ecn_state = :failed unless ecn_family?
72
+ end
73
+
74
+ # Whether the remote address is validated: a client's is from the
75
+ # start, a server's once the client shows it receives there.
76
+ def validated? = @validated
77
+
78
+ # The remote address is validated: the amplification limit is off.
79
+ def validate = @validated = true
80
+
81
+ # A datagram of `bytes` came on the path, which counts until the
82
+ # address is validated, each datagram once.
83
+ def datagram_received(bytes)
84
+ @received_unvalidated += bytes unless @validated
85
+ end
86
+
87
+ # A datagram of `bytes` went on the path.
88
+ def datagram_sent(bytes)
89
+ @sent_unvalidated += bytes unless @validated
90
+ end
91
+
92
+ # How many bytes may go on the path before the address is validated;
93
+ # nil once it is.
94
+ def amplification_budget = @validated ? nil : AMPLIFICATION_FACTOR * @received_unvalidated - @sent_unvalidated
95
+
96
+ # A PATH_CHALLENGE with `data` came on the path. Only the last waits
97
+ # for an answer, a newer taking the place of one not answered yet, so
98
+ # that a peer that sends many piles up nothing (CVE-2023-49295,
99
+ # CVE-2023-6193).
100
+ def challenged(data) = @challenge = data
101
+
102
+ # The data of the challenge waiting for an answer; nil when none does.
103
+ attr_reader :challenge
104
+
105
+ # The challenge is answered: each is, once.
106
+ def answered = @challenge = nil
107
+
108
+ # The peer's connection ID this end sends with on the path, when it is
109
+ # not the one in use; nil on the path in use, whose ID the connection
110
+ # keeps.
111
+ attr_reader :destination_connection_id
112
+
113
+ # The path, left by the connection, sends with `connection_id`; nil
114
+ # when it has none to send with.
115
+ def send_with(connection_id) = @destination_connection_id = connection_id
116
+
117
+ # Validation of the path starts at `now` (RFC 9000 "Path Validation"):
118
+ # a PATH_CHALLENGE goes at once and again each `interval`, twice as
119
+ # long each time, until `deadline`, when the path fails.
120
+ def start_validation(now:, interval:, deadline:)
121
+ @validation_owed = false
122
+ @challenge_due_at = now
123
+ @challenge_interval = interval
124
+ @validation_deadline = deadline
125
+ @challenges_sent = {} #: Hash[String, bool]
126
+ end
127
+
128
+ # Whether the path is being validated.
129
+ def validating? = !@validation_deadline.nil?
130
+
131
+ # The validation stops where it is, neither failing nor succeeding, and
132
+ # is owed: the path is to be validated anew before it is used again.
133
+ def stop_validation
134
+ @challenge_due_at = @challenge_interval = @validation_deadline = nil
135
+ @challenges_sent = {}
136
+ @full_size_due = false
137
+ @validation_owed = true
138
+ end
139
+
140
+ # Whether a validation stopped is owed.
141
+ def validation_owed? = @validation_owed
142
+
143
+ # Whether a PATH_CHALLENGE is due on the path at `now`.
144
+ def challenge_due?(now)
145
+ due = @challenge_due_at
146
+ !due.nil? && due <= now
147
+ end
148
+
149
+ # A PATH_CHALLENGE with `data` went at `now` in a datagram that was
150
+ # `full_size` (MIN_INITIAL_DATAGRAM bytes or more) or not; the next is
151
+ # due an interval on, the interval doubled (RFC 9002 "Computing PTO").
152
+ def challenge_sent(data, full_size:, now:)
153
+ interval = @challenge_interval or return
154
+ @challenges_sent[data] = full_size
155
+ @challenge_due_at = now + interval
156
+ @challenge_interval = interval * 2
157
+ end
158
+
159
+ # A PATH_RESPONSE with `data` came: whether it answers a challenge sent
160
+ # on the path. The answer to one in a full-size datagram ends the
161
+ # validation; to a smaller one, it validates the address, and the
162
+ # next challenge, due at once, is to go in a full-size datagram
163
+ # (RFC 9000 "Initiating Path Validation").
164
+ def responded(data, now:)
165
+ full_size = @challenges_sent.fetch(data) { return false }
166
+ validate
167
+ if full_size
168
+ @challenge_due_at = @challenge_interval = @validation_deadline = nil
169
+ @full_size_due = false
170
+ @challenges_sent = {}
171
+ else
172
+ @full_size_due = true
173
+ @challenge_due_at = now
174
+ end
175
+ true
176
+ end
177
+
178
+ # Whether the next challenge must go in a full-size datagram, the
179
+ # address validated by an answer to a smaller one.
180
+ def full_size_due? = @full_size_due
181
+
182
+ # When something of the validation is due: the next challenge, or the
183
+ # deadline.
184
+ def validation_timeout = [@challenge_due_at, @validation_deadline].compact.min
185
+
186
+ # When the validation fails unless answered first; nil when there is
187
+ # none.
188
+ attr_reader :validation_deadline
189
+
190
+ # Whether the validation has failed by `now`.
191
+ def validation_failed?(now)
192
+ deadline = @validation_deadline
193
+ !deadline.nil? && deadline <= now
194
+ end
195
+
196
+ # Takes the RTT, the congestion controller and the counts in flight back
197
+ # to where a new path starts, as after a Retry (RFC 9002 "Handling Retry
198
+ # Packets").
199
+ def start_over
200
+ @bytes_in_flight = 0
201
+ @ack_eliciting_in_flight = 0
202
+ @packets_in_flight = 0
203
+ start_learning
204
+ end
205
+
206
+ # The path takes over what `path`, which only the peer's port tells
207
+ # apart, learned of the network: its RTT, congestion controller,
208
+ # pacer, rate meter, datagram size, the search for it, and ECN state
209
+ # (RFC 9000 "Loss Detection and Congestion Control", a NAT rebinding
210
+ # most likely). `path` starts learning over, so that what was sent on
211
+ # it counts for nothing here, and keeps what it has in flight.
212
+ def take_over(path) = path.hand_over_to(self)
213
+
214
+ # Gives `path` what this path learned, and starts learning over. A
215
+ # probe of the MTU out on this path is heard of here, where the search
216
+ # no longer is, so the search sends the next on `path`.
217
+ def hand_over_to(path)
218
+ @mtu_search&.abandon_probe
219
+ path.adopt(rtt: @rtt, congestion: @congestion, startup: @startup, pacer: @pacer, rate_meter: @rate_meter, rtt_heard: @rtt_heard,
220
+ max_datagram_size: @max_datagram_size, mtu_search: @mtu_search, ecn: [@ecn_state, @ecn_testing_sent, @ecn_testing_lost])
221
+ @mtu_search = nil
222
+ @ecn_state = ecn_family? ? :testing : :failed
223
+ @ecn_testing_sent = @ecn_testing_lost = 0
224
+ start_learning
225
+ end
226
+
227
+ # Takes what another path learned (hand_over_to).
228
+ def adopt(rtt:, congestion:, startup:, pacer:, rate_meter:, rtt_heard:, max_datagram_size:, mtu_search:, ecn:)
229
+ @rtt = rtt
230
+ @congestion = congestion
231
+ @startup = startup
232
+ @pacer = pacer
233
+ @rate_meter = rate_meter
234
+ @rtt_heard = rtt_heard
235
+ @max_datagram_size = max_datagram_size
236
+ @mtu_search = mtu_search
237
+ @ecn_state, @ecn_testing_sent, @ecn_testing_lost = ecn
238
+ end
239
+
240
+ # ECN is tested on the path again from the start, as for a new
241
+ # connection (RFC 9000 "ECN Validation").
242
+ def test_ecn_again
243
+ @ecn_state = ecn_family? ? :testing : :failed
244
+ @ecn_testing_sent = @ecn_testing_lost = 0
245
+ end
246
+
247
+ # A new RTT estimator, congestion controller, pacer and rate meter.
248
+ private def start_learning
249
+ @rtt = Recovery::RttEstimator.new
250
+ cc = @config.congestion_control.new(max_datagram_size: @max_datagram_size, initial_window: @config.initial_window, config: @config,
251
+ tracer: @tracer)
252
+ @startup = @config.startup&.new(cc, max_datagram_size: @max_datagram_size, initial_window: @config.initial_window, tracer: @tracer)
253
+ @congestion = @startup || cc
254
+ @app_limited = false
255
+ @rtt_heard = false
256
+ @pacer = Pacer.new(capacity: Config::DEFAULT_INITIAL_WINDOW.call(@max_datagram_size), granularity: @config.pacing_granularity)
257
+ @pacing_release_at = nil #: Float?
258
+ @rate_meter = Recovery::RateMeter.new
259
+ end
260
+
261
+ # The congestion controller, or the Config's startup wrapping it until
262
+ # the startup is done: then the path takes the controller back, so that
263
+ # what comes after goes to it with nothing in between.
264
+ def congestion
265
+ startup = @startup
266
+ if startup&.done?
267
+ @congestion = startup.handover
268
+ @startup = nil
269
+ end
270
+ @congestion
271
+ end
272
+
273
+ # :testing, :capable or :failed (loss recovery's design, "Marking").
274
+ attr_reader :ecn_state
275
+
276
+ # The codepoint to mark packets with: ECT(0) until the path fails.
277
+ def ecn = @ecn_state == :failed ? :not_ect : :ect0
278
+
279
+ # A packet marked ECT(0) was sent.
280
+ def ecn_sent
281
+ @ecn_testing_sent += 1 if @ecn_state == :testing
282
+ end
283
+
284
+ # ECT(0) packets were declared lost: while testing, all of them lost
285
+ # fails the path (RFC 9000 "ECN Validation").
286
+ def ecn_lost(count, now:)
287
+ return unless @ecn_state == :testing
288
+ @ecn_testing_lost += count
289
+ ecn_failed(now:) if @ecn_testing_lost >= @ecn_testing_sent
290
+ end
291
+
292
+ # An ACK of ECT(0) packets passed validation.
293
+ def ecn_validated(now:)
294
+ change_ecn_state(:capable, now) if @ecn_state == :testing
295
+ end
296
+
297
+ # The path fails ECN for good: no marks and no counts heeded.
298
+ def ecn_failed(now:)
299
+ change_ecn_state(:failed, now)
300
+ end
301
+
302
+ # Whether ECN goes on this path: the family of its remote address is
303
+ # one of Config#ecn_address_families, or the address is not known yet.
304
+ # An IPv4-mapped address is IPv4's on the wire.
305
+ private def ecn_family?
306
+ host = @remote&.first or return true
307
+ @config.ecn_address_families.include?(host.include?(".") ? :ipv4 : :ipv6)
308
+ end
309
+
310
+ # Counts a packet that went in flight on this path, and tells the
311
+ # congestion controller.
312
+ def add_in_flight(sent_bytes:, ack_eliciting:, now:)
313
+ @bytes_in_flight += sent_bytes
314
+ @packets_in_flight += 1
315
+ @ack_eliciting_in_flight += 1 if ack_eliciting
316
+ @pacer.sent(sent_bytes)
317
+ @congestion.on_packet_sent(bytes: sent_bytes, now:)
318
+ end
319
+
320
+ # Whether pacing lets a datagram of `size` bytes go in flight now, at
321
+ # the congestion controller's rate; when it does not, `pacing_release_at`
322
+ # is when it will.
323
+ def paced?(size, now)
324
+ @pacing_release_at = @pacer.release_at(size, @congestion.pacing_rate, now)
325
+ @pacing_release_at.nil?
326
+ end
327
+
328
+ # When pacing lets what is held back go, while sending is stopped for
329
+ # it.
330
+ attr_reader :pacing_release_at
331
+
332
+ # Bytes in flight of Application Data on this path were acknowledged at
333
+ # `now`, the largest packet newly acknowledged numbered `largest`.
334
+ def delivered(bytes, largest:, now:) = @rate_meter.on_ack(bytes, largest:, now:)
335
+
336
+ # The rate the path delivers at by its RateMeter, bytes a second; nil
337
+ # before a sample.
338
+ def delivery_rate = @rate_meter.rate
339
+
340
+ # Packets of `acked_bytes` in flight on this path were acknowledged,
341
+ # the latest of them sent at `largest_acked_sent_at`; the congestion
342
+ # controller hears of them with the path's RTT, and with its latest
343
+ # sample when the ACK's was the path's, `sampled`.
344
+ def acked(acked_bytes:, largest_acked_sent_at:, sampled:, now:)
345
+ @timed_out = 0
346
+ rtt = @rtt
347
+ min_rtt = rtt.min_rtt
348
+ @rtt_heard = true if min_rtt
349
+ congestion.on_ack(acked_bytes:, largest_acked_sent_at:, latest_rtt: (rtt.latest_rtt if sampled), smoothed_rtt: rtt.smoothed_rtt,
350
+ min_rtt:, app_limited: @app_limited, now:)
351
+ end
352
+
353
+ # No longer counts a packet acknowledged, lost or discarded.
354
+ def remove_in_flight(sent_bytes:, ack_eliciting:)
355
+ @bytes_in_flight -= sent_bytes
356
+ @packets_in_flight -= 1
357
+ @ack_eliciting_in_flight -= 1 if ack_eliciting
358
+ end
359
+
360
+ # Whether the window went unused when sending last stopped, which keeps
361
+ # the congestion controller from growing it (RFC 9002 "Underutilizing
362
+ # the Congestion Window"). false until the first stop.
363
+ def app_limited? = @app_limited
364
+
365
+ # Sending stopped, for `reason`: :drained (nothing left to send),
366
+ # :flow_control, :amplification (the server's limit before the client's
367
+ # address is validated), :congestion (the window is full) or :pacing.
368
+ # Only the window filled up, or pacing holding back what would have
369
+ # filled it, leaves the window used; pacing with no stream data or
370
+ # DATAGRAMs `waiting` holds back nothing, and is as nothing left to
371
+ # send. With them waiting and the window or pacing holding them back,
372
+ # the rate meter counts what goes from `next_packet_number`, the least
373
+ # the next 1-RTT packet will be numbered.
374
+ def transmission_stopped(reason, waiting:, next_packet_number:)
375
+ @pacing_release_at = nil unless reason == :pacing
376
+ @rate_meter.sending_stopped(limited: waiting && (reason == :congestion || reason == :pacing), next_packet_number:)
377
+ @app_limited = case reason
378
+ when :drained then @bytes_in_flight < window
379
+ when :pacing then !waiting && @bytes_in_flight < window
380
+ when :flow_control, :amplification then true
381
+ when :congestion then false
382
+ else raise ArgumentError, "unknown reason to stop sending: #{reason.inspect}"
383
+ end
384
+ end
385
+
386
+ # The path's MTU changed (DPLPMTUD): the congestion controller hears,
387
+ # and the pacer's burst is RFC 9002's initial window of the new size.
388
+ def update_max_datagram_size(max_datagram_size, now:)
389
+ old = @max_datagram_size
390
+ @max_datagram_size = max_datagram_size
391
+ @congestion.on_mtu_updated(max_datagram_size:, now:)
392
+ @pacer.capacity = Config::DEFAULT_INITIAL_WINDOW.call(max_datagram_size)
393
+ @tracer.mtu_updated(old:, new: max_datagram_size, done: @mtu_search&.done? || false, now:) if @tracer # steep:ignore
394
+ end
395
+
396
+ # The path searches for the largest datagram it carries, up to `up_to`
397
+ # bytes, from what it carries now (MTUSearch); a search under way or
398
+ # done goes on as it is.
399
+ def search_mtu(up_to:)
400
+ @mtu_search ||= MTUSearch.new(from: @max_datagram_size, up_to:)
401
+ end
402
+
403
+ # The size of the probe of the path's MTU to send next; nil when none
404
+ # is due.
405
+ def mtu_probe_size = @mtu_search&.next_size
406
+
407
+ # A probe of `size` bytes went on the path.
408
+ def mtu_probe_sent(size) = @mtu_search&.sent(size)
409
+
410
+ # A probe of `size` bytes got through: the path carries datagrams that
411
+ # large from now on, when they are larger than before.
412
+ def mtu_probe_acked(size, now:)
413
+ through = @mtu_search&.acked(size) or return
414
+ update_max_datagram_size(through, now:)
415
+ end
416
+
417
+ # A probe of `size` bytes was lost at `now`, which may end the search.
418
+ def mtu_probe_lost(size, now:)
419
+ search = @mtu_search or return
420
+ done = search.done?
421
+ search.lost(size)
422
+ return if done || !search.done?
423
+ @tracer.mtu_updated(old: @max_datagram_size, new: @max_datagram_size, done: true, now:) if @tracer # steep:ignore
424
+ end
425
+
426
+ # Persistent congestion on the path, which may be a black hole.
427
+ def persistent_congestion(now:) = black_hole(now)
428
+
429
+ # A PTO ran out while the path was in use: BLACK_HOLE_PTOS in a row,
430
+ # with nothing of the path's acknowledged in between, may be a black
431
+ # hole, which persistent congestion, judged on an ACK, never tells of
432
+ # when nothing at all gets through. Those that ran out on another path
433
+ # before the connection moved here count for nothing.
434
+ def probe_timed_out(now:)
435
+ @timed_out += 1
436
+ black_hole(now) if @timed_out >= BLACK_HOLE_PTOS
437
+ end
438
+
439
+ # The window sending goes by: the congestion controller's, but until
440
+ # an ACK tells the controller an RTT, when nothing is paced, no more
441
+ # than RFC 9002's initial window, however large the Config's, as RFC
442
+ # 9002 "Pacing" keeps a burst to it. A Retry's RTT sample tells the
443
+ # controller nothing.
444
+ def window
445
+ window = @congestion.window
446
+ return window if @rtt_heard
447
+ [window, Config::DEFAULT_INITIAL_WINDOW.call(@max_datagram_size)].min
448
+ end
449
+
450
+ # The congestion window in datagrams.
451
+ def window_packets = @congestion.window / @max_datagram_size
452
+
453
+ # The connection left this path; what was sent on it no longer counts.
454
+ def abandon = @abandoned = true
455
+
456
+ def abandoned? = @abandoned
457
+
458
+ # On a path that carries more than MIN_DATAGRAM_SIZE, the larger
459
+ # datagrams may be lost in a black hole (RFC 9000 "Datagram Size"): the
460
+ # path goes back to MIN_DATAGRAM_SIZE, and its search starts over.
461
+ private def black_hole(now)
462
+ return unless @max_datagram_size > MIN_DATAGRAM_SIZE
463
+ @mtu_search = nil
464
+ update_max_datagram_size(MIN_DATAGRAM_SIZE, now:)
465
+ end
466
+
467
+ private def change_ecn_state(state, now)
468
+ return if state == @ecn_state
469
+ @tracer.ecn_state_updated(old: @ecn_state, new: state, now:) if @tracer # steep:ignore
470
+ @ecn_state = state
471
+ end
472
+ end
473
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The connection IDs the peer issued for this end to send to (RFC 9000
5
+ # "Issuing Connection IDs" and "Consuming and Retiring Connection IDs"):
6
+ # the one of the handshake, sequence number 0, that of a server's
7
+ # preferred address, sequence number 1, and those NEW_CONNECTION_ID
8
+ # frames bring, with their stateless reset tokens.
9
+ #
10
+ # Retire Prior To retires the IDs numbered below it, the one in use too,
11
+ # which then gives way to the lowest still active, and each goes back in
12
+ # a RETIRE_CONNECTION_ID frame until acknowledged; so does an ID that
13
+ # comes numbered below it. More active IDs than this end's
14
+ # active_connection_id_limit is CONNECTION_ID_LIMIT_ERROR, and so is
15
+ # more than twice that waiting to be retired, as the peer could make
16
+ # them pile up by not acknowledging them (CVE-2026-84784). An ID or a
17
+ # sequence number that comes again with something else is
18
+ # PROTOCOL_VIOLATION.
19
+ #
20
+ # Each path this end sends on has an ID of its own (RFC 9000 "Privacy
21
+ # Implications of Connection Migration"): a path other than the one in
22
+ # use claims an ID no path has sent with, and gives it back retired when
23
+ # the path goes. Zero-length IDs are the same on every path.
24
+ class PeerConnectionIDs
25
+ FRAME_TYPE = Wire::Frame::NewConnectionID::TYPES.fetch(0)
26
+
27
+ Entry = Data.define(:sequence_number, :connection_id, :stateless_reset_token)
28
+
29
+ # The ID in use.
30
+ attr_reader :current
31
+
32
+ # The peer's first ID, sequence number 0, with the stateless reset token
33
+ # its transport parameters gave, if any, and the ID of the preferred
34
+ # address they gave, if any; `limit` is this end's
35
+ # active_connection_id_limit.
36
+ def initialize(connection_id, stateless_reset_token:, limit:, preferred_address: nil)
37
+ @limit = limit
38
+ @active = [Entry.new(sequence_number: 0, connection_id:, stateless_reset_token:)]
39
+ if preferred_address
40
+ @active << Entry.new(sequence_number: 1, connection_id: preferred_address.connection_id,
41
+ stateless_reset_token: preferred_address.stateless_reset_token)
42
+ end
43
+ @current = connection_id
44
+ @retire_prior_to = 0
45
+ # The sequence numbers to retire, and those retired and not yet
46
+ # acknowledged.
47
+ @to_retire = [] #: Array[Integer]
48
+ @retiring = [] #: Array[Integer]
49
+ # The IDs paths other than the one in use claimed.
50
+ @claimed = [] #: Array[String]
51
+ end
52
+
53
+ # The stateless reset tokens of `connection_ids`, the IDs this end
54
+ # sends with on the paths to the address a datagram that no packet of
55
+ # the connection opens came from; not of those active and not yet
56
+ # used, nor of those retired (RFC 9000 "Detecting a Stateless Reset").
57
+ def reset_tokens(connection_ids) = @active.select { |entry| connection_ids.include?(entry.connection_id) }.filter_map(&:stateless_reset_token)
58
+
59
+ # Whether there is an ID no path has sent with, for `claim`; with
60
+ # zero-length IDs there always is.
61
+ def spare? = @current.empty? || @active.any? { |entry| !in_use?(entry.connection_id) }
62
+
63
+ # An ID for a path to send with that no path has sent with, the lowest
64
+ # first; nil when there is none. Zero-length IDs are always there.
65
+ def claim
66
+ return @current if @current.empty?
67
+ entry = @active.find { |each| !in_use?(each.connection_id) } or return nil
68
+ @claimed << entry.connection_id
69
+ entry.connection_id
70
+ end
71
+
72
+ # Whether a path besides the one in use still sends with
73
+ # `connection_id`: claimed by it, and not retired by Retire Prior To.
74
+ def claimed?(connection_id) = @claimed.include?(connection_id) && @active.any? { |entry| entry.connection_id == connection_id }
75
+
76
+ # The path that claimed `connection_id` is now the one in use: the ID
77
+ # in use before is claimed by the path that leaves it.
78
+ def switch(connection_id)
79
+ return if connection_id == @current || !@claimed.delete(connection_id)
80
+ @claimed << @current
81
+ @current = connection_id
82
+ end
83
+
84
+ # A path that claimed `connection_id` went: the ID is retired, as no
85
+ # other path sends with it.
86
+ def release(connection_id)
87
+ return unless @claimed.delete(connection_id)
88
+ entry = @active.find { |each| each.connection_id == connection_id } or return
89
+ @active.delete(entry)
90
+ retire(entry.sequence_number)
91
+ end
92
+
93
+ # Takes a NEW_CONNECTION_ID frame.
94
+ def receive(frame)
95
+ raise Violation.new("NEW_CONNECTION_ID to an end that sends no connection ID", frame_type: FRAME_TYPE) if @current.empty?
96
+ return if known?(frame)
97
+ if frame.sequence_number < @retire_prior_to
98
+ retire(frame.sequence_number)
99
+ else
100
+ @active << Entry.new(sequence_number: frame.sequence_number, connection_id: frame.connection_id,
101
+ stateless_reset_token: frame.stateless_reset_token)
102
+ end
103
+ retire_prior_to(frame.retire_prior_to) if frame.retire_prior_to > @retire_prior_to
104
+ if @active.size > @limit
105
+ raise Violation.new("#{@active.size} connection IDs, over the limit of #{@limit}", code: ErrorCode::CONNECTION_ID_LIMIT_ERROR,
106
+ frame_type: FRAME_TYPE)
107
+ end
108
+ return unless @to_retire.size + @retiring.size > 2 * @limit
109
+ raise Violation.new("#{@to_retire.size + @retiring.size} connection IDs waiting to be retired", code: ErrorCode::CONNECTION_ID_LIMIT_ERROR,
110
+ frame_type: FRAME_TYPE)
111
+ end
112
+
113
+ # The next RETIRE_CONNECTION_ID due and its duty, nil when none is or
114
+ # it does not fit in `room` bytes.
115
+ def next_frame(room)
116
+ sequence_number = @to_retire.first or return nil
117
+ frame = Wire::Frame::RetireConnectionID.new(sequence_number:)
118
+ return nil if Wire::Frame.encode([frame], ack_delay_exponent: 0).bytesize > room
119
+ @retiring << @to_retire.shift
120
+ [frame, Recovery::Duty::RetireConnectionID.new(sequence_number:)]
121
+ end
122
+
123
+ def retire_acked(duty) = @retiring.delete(duty.sequence_number)
124
+
125
+ # A lost RETIRE_CONNECTION_ID goes again.
126
+ def retire_lost(duty)
127
+ return unless @retiring.delete(duty.sequence_number)
128
+ @to_retire << duty.sequence_number
129
+ end
130
+
131
+ # Whether the frame repeats an active ID; one that comes again with
132
+ # another ID, sequence number or token is PROTOCOL_VIOLATION.
133
+ private def known?(frame)
134
+ entry = @active.find { |each| each.sequence_number == frame.sequence_number || each.connection_id == frame.connection_id }
135
+ return false unless entry
136
+ same = entry.sequence_number == frame.sequence_number && entry.connection_id == frame.connection_id &&
137
+ entry.stateless_reset_token == frame.stateless_reset_token
138
+ raise Violation.new("connection ID #{frame.sequence_number} again with something else", frame_type: FRAME_TYPE) unless same
139
+ true
140
+ end
141
+
142
+ # The IDs numbered below `limit` are retired, and the one in use gives
143
+ # way to the lowest still active that no path claimed. There is one:
144
+ # the frame's own ID, which its Retire Prior To never reaches.
145
+ private def retire_prior_to(limit)
146
+ @retire_prior_to = limit
147
+ retired, @active = @active.partition { |entry| entry.sequence_number < limit }
148
+ retired.each { |entry| retire(entry.sequence_number) }
149
+ return if @active.any? { |entry| entry.connection_id == @current }
150
+ free = @active.reject { |entry| @claimed.include?(entry.connection_id) }
151
+ @current = free.min_by(&:sequence_number)&.connection_id || @current
152
+ end
153
+
154
+ private def in_use?(connection_id) = connection_id == @current || @claimed.include?(connection_id)
155
+
156
+ private def retire(sequence_number)
157
+ @to_retire << sequence_number unless @to_retire.include?(sequence_number) || @retiring.include?(sequence_number)
158
+ end
159
+ end
160
+ end