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,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error_code"
4
+
5
+ module Raiha::QUIC
6
+ # Exceptions are for misuse of the API only. What the peer does never
7
+ # raises: a protocol error is a ConnectionError value, sent as
8
+ # CONNECTION_CLOSE and reported in Event::Closed.
9
+ module Error
10
+ class Base < StandardError; end
11
+
12
+ # A call the API does not allow, as writing to a closed connection.
13
+ class Usage < Base; end
14
+
15
+ # A call in a state that does not allow it.
16
+ class StateError < Usage; end
17
+
18
+ # A call on a stream that does not exist or does not allow it.
19
+ class StreamError < Usage; end
20
+
21
+ # A client's 0-RTT was rejected and the application has not said it
22
+ # knows (Connection#acknowledge_zero_rtt_rejection): the streams it
23
+ # opened are gone, and an ID of one could name a new stream.
24
+ class ZeroRttRejected < Usage; end
25
+ end
26
+
27
+ # The peer broke a rule of the protocol. `code` is the transport error to
28
+ # close the connection with, and `frame_type` the type of the frame that
29
+ # broke it, when one did. Raised inside the layer and turned into a
30
+ # ConnectionError where the input came in; the application never sees it.
31
+ class Violation < StandardError
32
+ attr_reader :code
33
+ attr_reader :frame_type
34
+
35
+ def initialize(message = nil, code: ErrorCode::PROTOCOL_VIOLATION, frame_type: nil)
36
+ super(message)
37
+ @code = code
38
+ @frame_type = frame_type
39
+ end
40
+ end
41
+
42
+ # Why a connection closed: a transport error code (ErrorCode) or an
43
+ # application's, the type of the frame that caused it (nil when none or an
44
+ # application error), and a reason phrase.
45
+ class ConnectionError < Data.define(:code, :frame_type, :reason, :application)
46
+ end
47
+
48
+ # The connection closed under a call that waited on it or came after, as
49
+ # an I/O layer that blocks for the application raises it; `error` is the
50
+ # ConnectionError it closed with. It is no misuse of the API: the peer
51
+ # or the network can close a connection at any time.
52
+ class ClosedError < StandardError
53
+ attr_reader :error
54
+
55
+ def initialize(error)
56
+ @error = error
57
+ kind = error.application ? "application" : "transport"
58
+ super(format("closed with %s error 0x%x%s", kind, error.code, error.reason.empty? ? "" : ": #{error.reason}"))
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The transport error codes of RFC 9000 "Transport Error Codes", and
5
+ # that of RFC 9368 "Version Downgrade Prevention".
6
+ module ErrorCode
7
+ NO_ERROR = 0x00
8
+ INTERNAL_ERROR = 0x01
9
+ CONNECTION_REFUSED = 0x02
10
+ FLOW_CONTROL_ERROR = 0x03
11
+ STREAM_LIMIT_ERROR = 0x04
12
+ STREAM_STATE_ERROR = 0x05
13
+ FINAL_SIZE_ERROR = 0x06
14
+ FRAME_ENCODING_ERROR = 0x07
15
+ TRANSPORT_PARAMETER_ERROR = 0x08
16
+ CONNECTION_ID_LIMIT_ERROR = 0x09
17
+ PROTOCOL_VIOLATION = 0x0a
18
+ INVALID_TOKEN = 0x0b
19
+ APPLICATION_ERROR = 0x0c
20
+ CRYPTO_BUFFER_EXCEEDED = 0x0d
21
+ KEY_UPDATE_ERROR = 0x0e
22
+ AEAD_LIMIT_REACHED = 0x0f
23
+ NO_VIABLE_PATH = 0x10
24
+ VERSION_NEGOTIATION_ERROR = 0x11
25
+
26
+ # CRYPTO_ERROR is a range: 0x0100 plus the TLS alert.
27
+ CRYPTO_ERROR_BASE = 0x0100
28
+
29
+ def self.crypto_error(alert_code)
30
+ raise ArgumentError, "a TLS alert is 0..255, not #{alert_code.inspect}" unless alert_code.is_a?(Integer) && alert_code.between?(0, 0xff)
31
+ CRYPTO_ERROR_BASE + alert_code
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # What a connection tells the application. An event says that something
5
+ # can be done (a stream has data to read) rather than carrying the data,
6
+ # so flow control credit goes back as the application reads.
7
+ module Event
8
+ # The handshake completed. `handshake` is the TLS layer's
9
+ # Event::HandshakeComplete, not a copy of its fields.
10
+ class Connected < Data.define(:handshake)
11
+ end
12
+
13
+ class StreamReadable < Data.define(:stream_id)
14
+ end
15
+
16
+ # Once, when a stream that took part of a write can take more.
17
+ class StreamWritable < Data.define(:stream_id)
18
+ end
19
+
20
+ class StreamReset < Data.define(:stream_id, :error_code, :final_size)
21
+ end
22
+
23
+ class StopSending < Data.define(:stream_id, :error_code)
24
+ end
25
+
26
+ class DatagramReceived < Data.define(:data)
27
+ end
28
+
29
+ class KeyUpdated < Data.define(:phase)
30
+ end
31
+
32
+ # A ticket the client may resume later, as a QUIC::Session; keeping it
33
+ # is the application's.
34
+ class SessionTicket < Data.define(:session)
35
+ end
36
+
37
+ # The server took no 0-RTT, for `reason` as TLS has it, or
38
+ # :version_negotiation when a Version Negotiation packet came and the
39
+ # client started again in another version: the streams the client
40
+ # opened are gone, and what to send again is the application's
41
+ # (Connection#zero_rtt_rejected).
42
+ class ZeroRttRejected < Data.define(:reason)
43
+ end
44
+
45
+ # A token the server gave in a NEW_TOKEN frame, a QUIC::Token of the
46
+ # version the connection is in, for the Initials of a later connection
47
+ # to it (Endpoint#connect(token:)); keeping it is the application's.
48
+ class NewToken < Data.define(:token)
49
+ end
50
+
51
+ # A TLS event the binding does not consume itself, passed on as it came.
52
+ class TLSEvent < Data.define(:event)
53
+ end
54
+
55
+ # The connection ended; `error` is a ConnectionError.
56
+ class Closed < Data.define(:error, :by_peer)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The connection IDs this end issued for the peer to send to (RFC 9000
5
+ # "Issuing Connection IDs"): the one of the handshake, sequence number 0,
6
+ # that of a server's preferred address, sequence number 1, and those
7
+ # NEW_CONNECTION_ID frames carry, which the Endpoint makes
8
+ # (Endpoint::Issuer) with their stateless reset tokens and leads to the
9
+ # connection. Once the handshake is confirmed, as many are issued as the
10
+ # peer's active_connection_id_limit takes, MAX at most; Retire Prior To
11
+ # is always 0, as this end never retires its own. A NEW_CONNECTION_ID
12
+ # frame lost goes again as it was. With no issuer, or with zero-length
13
+ # connection IDs, none is issued.
14
+ #
15
+ # The peer retires IDs with RETIRE_CONNECTION_ID frames, and each ID it
16
+ # retires is replaced by a new one. An ID retired still leads to the
17
+ # connection until the time the connection gives, three PTOs on, so
18
+ # that packets late on the way get there; then the Endpoint forgets it.
19
+ # MAX_RETIRED wait at most, the oldest forgotten at once past them, so
20
+ # that a peer that retires each new ID straight away, with a PTO it
21
+ # makes long, piles up nothing (CVE-2024-1410). Whether a sequence
22
+ # number was issued goes by the highest issued, and no set of those
23
+ # retired is kept.
24
+ class IssuedConnectionIDs
25
+ # The most IDs this end keeps active, sequence number 0 among them:
26
+ # enough that a server, which moves on a NAT rebinding only with an ID
27
+ # to validate the path left with, follows a few rebindings in a row.
28
+ MAX = 8
29
+ # The most IDs retired that wait to be forgotten.
30
+ MAX_RETIRED = 2 * MAX
31
+ FRAME_TYPE = Wire::Frame::RetireConnectionID::TYPES.fetch(0)
32
+
33
+ # An ID issued, with the NEW_CONNECTION_ID frame that carries it, which
34
+ # the ID of the handshake has none of.
35
+ Entry = Data.define(:sequence_number, :connection_id, :frame)
36
+
37
+ # An ID retired, and when the Endpoint forgets it.
38
+ Retired = Data.define(:connection_id, :forget_at)
39
+
40
+ # The ID of the handshake, `first`, of `connection`, and what makes
41
+ # the others.
42
+ def initialize(first, issuer:, connection:)
43
+ @issuer = first.empty? ? nil : issuer
44
+ @connection = connection
45
+ @active = [Entry.new(sequence_number: 0, connection_id: first, frame: nil)]
46
+ @next_sequence_number = 1
47
+ # The NEW_CONNECTION_ID frames to go.
48
+ @to_send = [] #: Array[Wire::Frame::NewConnectionID]
49
+ # The peer's active_connection_id_limit, once IDs are issued.
50
+ @limit = 0
51
+ # The IDs retired that wait to be forgotten, the oldest first.
52
+ @retired = [] #: Array[Retired]
53
+ end
54
+
55
+ # The preferred address the transport parameters offer at `addresses`
56
+ # (Config#preferred_address), with an ID issued for it, sequence number
57
+ # 1, that no frame carries; nil with no addresses or no issuer, when
58
+ # there is none.
59
+ def preferred_address(addresses)
60
+ return nil unless addresses && (issuer = @issuer)
61
+ connection_id, stateless_reset_token = issuer.issue(@connection)
62
+ @active << Entry.new(sequence_number: @next_sequence_number, connection_id:, frame: nil)
63
+ @next_sequence_number += 1
64
+ TransportParameters::PreferredAddress.new(ipv4: addresses[:ipv4], ipv6: addresses[:ipv6], connection_id:, stateless_reset_token:)
65
+ end
66
+
67
+ # Issues IDs until min(`limit`, MAX) are active; `limit` is the peer's
68
+ # active_connection_id_limit.
69
+ def issue(limit)
70
+ @limit = limit
71
+ issuer = @issuer or return
72
+ while @active.size < [limit, MAX].min
73
+ connection_id, stateless_reset_token = issuer.issue(@connection)
74
+ frame = Wire::Frame::NewConnectionID.new(sequence_number: @next_sequence_number, retire_prior_to: 0, connection_id:,
75
+ stateless_reset_token:)
76
+ @active << Entry.new(sequence_number: @next_sequence_number, connection_id:, frame:)
77
+ @to_send << frame
78
+ @next_sequence_number += 1
79
+ end
80
+ end
81
+
82
+ # The next NEW_CONNECTION_ID frame due and its duty, nil when none is
83
+ # or it does not fit in `room` bytes.
84
+ def next_frame(room)
85
+ frame = @to_send.first or return nil
86
+ return nil if Wire::Frame.encode([frame], ack_delay_exponent: 0).bytesize > room
87
+ @to_send.shift
88
+ [frame, Recovery::Duty::NewConnectionID.new(sequence_number: frame.sequence_number)]
89
+ end
90
+
91
+ # Takes a RETIRE_CONNECTION_ID frame that came in a packet to `dcid`
92
+ # (RFC 9000 "RETIRE_CONNECTION_ID Frames"): a sequence number never
93
+ # issued, or that of the packet's own ID, is PROTOCOL_VIOLATION, the
94
+ # latter so that the two ends do not retire and replace for ever
95
+ # (CVE-2025-7054); one retired already is nothing. The ID retired is
96
+ # forgotten at `forget_at`, and a new one replaces it.
97
+ def retire(frame, dcid:, forget_at:)
98
+ sequence_number = frame.sequence_number
99
+ if sequence_number >= @next_sequence_number
100
+ raise Violation.new("RETIRE_CONNECTION_ID of #{sequence_number}, which was never issued", frame_type: FRAME_TYPE)
101
+ end
102
+ entry = @active.find { |each| each.sequence_number == sequence_number } or return
103
+ if entry.connection_id == dcid
104
+ raise Violation.new("RETIRE_CONNECTION_ID of #{sequence_number}, the ID of the packet it came in", frame_type: FRAME_TYPE)
105
+ end
106
+ @active.delete(entry)
107
+ issued = entry.frame
108
+ @to_send.delete(issued) if issued
109
+ @retired << Retired.new(connection_id: entry.connection_id, forget_at:)
110
+ while @retired.size > MAX_RETIRED
111
+ oldest = @retired.shift or break
112
+ forget(oldest.connection_id)
113
+ end
114
+ issue(@limit)
115
+ end
116
+
117
+ # When the next ID retired is to be forgotten; nil when none waits.
118
+ def next_timeout = @retired.first&.forget_at
119
+
120
+ # The IDs retired whose time has come are forgotten.
121
+ def handle_timeout(now)
122
+ while (oldest = @retired.first) && oldest.forget_at <= now
123
+ @retired.shift
124
+ forget(oldest.connection_id)
125
+ end
126
+ end
127
+
128
+ # A NEW_CONNECTION_ID frame lost goes again, while its ID is active.
129
+ def lost(duty)
130
+ entry = @active.find { |each| each.sequence_number == duty.sequence_number }
131
+ frame = entry&.frame or return
132
+ @to_send << frame unless @to_send.include?(frame)
133
+ end
134
+
135
+ private def forget(connection_id) = @issuer&.forget(connection_id)
136
+ end
137
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The packets a connection cannot open yet, as their keys have not come
5
+ # (RFC 9001 "Receiving Out-of-Order Protected Packets"): no more than
6
+ # PACKETS of them, nor BYTES in all, so that what a peer can make a
7
+ # connection keep is bounded. Each is kept as a String of its own, never
8
+ # as a part of the datagram it came in, which a substring to its end
9
+ # would keep whole.
10
+ class KeptPackets
11
+ PACKETS = 10
12
+ BYTES = 16 * 1024
13
+
14
+ # A packet kept: its bytes and level, the addresses and ECN codepoint
15
+ # of its datagram, and when it came.
16
+ Kept = Data.define(:bytes, :level, :from, :to, :ecn, :received_at)
17
+
18
+ def initialize
19
+ @kept = [] #: Array[Kept]
20
+ @bytes = 0
21
+ end
22
+
23
+ def empty? = @kept.empty?
24
+
25
+ # Keeps the packet `bytes` of `level`: whether there was room for it.
26
+ def keep(bytes, level:, from:, to:, ecn:, received_at:)
27
+ return false if @kept.size >= PACKETS || @bytes + bytes.bytesize > BYTES
28
+ @kept << Kept.new(bytes: "".b << bytes, level:, from:, to:, ecn:, received_at:)
29
+ @bytes += bytes.bytesize
30
+ true
31
+ end
32
+
33
+ # Takes out the packets whose level the block is true of, in the order
34
+ # they came.
35
+ def take
36
+ taken, @kept = @kept.partition { |kept| yield kept.level }
37
+ @bytes -= taken.sum { |kept| kept.bytes.bytesize }
38
+ taken
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The search of a path for the largest datagram it carries (RFC 8899
5
+ # "Datagram Packetization Layer Path MTU Discovery"), one probe at a
6
+ # time: the largest it may carry first, then halfway between the largest
7
+ # that got through and the smallest that did not. A size is given up on
8
+ # once MAX_LOSSES of its probes in a row are lost; a probe that gets
9
+ # through, late included, counts as through and starts the losses over.
10
+ # The search is done once the largest gets through, or once what got
11
+ # through and what did not are CLOSE_ENOUGH; it does not start again,
12
+ # as RFC 8899's PMTU_RAISE_TIMER would.
13
+ class MTUSearch
14
+ # Bytes between what got through and what did not that end the search.
15
+ CLOSE_ENOUGH = 20
16
+ # Probes of a size lost in a row that give the size up.
17
+ MAX_LOSSES = 3
18
+
19
+ # From `from` bytes, which the path carries, up to `up_to`.
20
+ def initialize(from:, up_to:)
21
+ @through = from
22
+ @failed = nil #: Integer?
23
+ @max = up_to
24
+ @probing = nil #: Integer?
25
+ # The size whose probes were lost last, and how many in a row.
26
+ @losing = nil #: Integer?
27
+ @losses = 0
28
+ end
29
+
30
+ # The size of the next probe; nil while one is out, or once the search
31
+ # is done.
32
+ def next_size
33
+ return nil if @probing || done?
34
+ failed = @failed
35
+ failed ? (@through + failed) / 2 : @max
36
+ end
37
+
38
+ def done?
39
+ failed = @failed
40
+ @through >= @max || (!failed.nil? && failed - @through <= CLOSE_ENOUGH)
41
+ end
42
+
43
+ # A probe of `size` went.
44
+ def sent(size) = @probing = size
45
+
46
+ # The probe out will not be heard of, as it went on a path the search
47
+ # left: the next is due.
48
+ def abandon_probe = @probing = nil
49
+
50
+ # A probe of `size` got through: the size the path carries now, when
51
+ # it is larger than before; else nil.
52
+ def acked(size)
53
+ @probing = nil if @probing == size
54
+ @losses = 0
55
+ return nil if size <= @through
56
+ @through = size
57
+ failed = @failed
58
+ @failed = nil if failed && failed <= size
59
+ size
60
+ end
61
+
62
+ # A probe of `size` was lost; one no larger than what got through
63
+ # tells nothing.
64
+ def lost(size)
65
+ @probing = nil if @probing == size
66
+ return if size <= @through
67
+ @losses = @losing == size ? @losses + 1 : 1
68
+ @losing = size
69
+ return if @losses < MAX_LOSSES
70
+ @losses = 0
71
+ failed = @failed
72
+ @failed = size if failed.nil? || size < failed
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # Where the negotiation of a connection's version stands (RFC 9368), as a
5
+ # value the connection replaces as it changes. `versions` are those the
6
+ # endpoint is configured with, in its order of preference. `original` is
7
+ # the Original Version: a client's first attempt, or the version of the
8
+ # client's Initial at a server. `chosen` is the version the attempt
9
+ # started in, and `negotiated` the Negotiated Version once known, nil
10
+ # until a client learns it. `available` are the numbers of the Available
11
+ # Versions this end sends, reserved ones among them. `reacted` is whether
12
+ # a client reacted to a Version Negotiation packet. Each end's Available
13
+ # Versions end with a reserved version from `random`, but for a server's
14
+ # with none fully deployed: an empty list tells a client that reacted to
15
+ # a Version Negotiation packet of a downgrade (RFC 9368 "Version
16
+ # Downgrade Prevention"), and a reserved version would hide it.
17
+ class Negotiation < Data.define(:role, :versions, :original, :chosen, :negotiated, :available, :reacted)
18
+ # A client's first attempt, in `chosen`. It offers `chosen` and the
19
+ # versions of `versions` compatible with it, in the order of `versions`
20
+ # (RFC 9368 "Version Information").
21
+ def self.client(versions:, chosen:, random:)
22
+ offered = versions.select { |version| version == chosen || chosen.compatible_with?(version) }
23
+ available = [*offered.map(&:number), Version.reserved(random)].freeze
24
+ new(role: :client, versions:, original: chosen, chosen:, negotiated: nil, available:, reacted: false)
25
+ end
26
+
27
+ # A server's connection, which the client started in `original` and the
28
+ # server chose `negotiated` for. Its Available Versions are those fully
29
+ # `deployed`.
30
+ def self.server(versions:, deployed:, original:, negotiated:, random:)
31
+ available = deployed.map(&:number)
32
+ available << Version.reserved(random) unless deployed.empty?
33
+ new(role: :server, versions:, original:, chosen: original, negotiated:, available: available.freeze, reacted: false)
34
+ end
35
+
36
+ # The version in use.
37
+ def current = negotiated || chosen
38
+
39
+ # The versions a client may get the server's Initials in, whose Initial
40
+ # keys it makes before the server's first opens: the one it started in,
41
+ # then those it offered compatible with it. A server's are the client's
42
+ # version and the one it chose.
43
+ def initial_versions
44
+ return [original, current].uniq if role == :server
45
+ [chosen, *versions.select { |version| available.include?(version.number) && chosen.compatible_with?(version) }]
46
+ end
47
+
48
+ # A client learns the version negotiated: the server's first Initial
49
+ # that opens in another version than the one it started in, or one in
50
+ # that version that carries a CRYPTO frame (RFC 9369 "Compatible
51
+ # Negotiation Requirements").
52
+ def learn(version) = with(negotiated: version)
53
+
54
+ # A client reacts to a Version Negotiation packet with a new attempt in
55
+ # `version` (RFC 9368 "Incompatible Version Negotiation"), offering
56
+ # again; the Original Version stays that of the first attempt.
57
+ def react(version, random:) = Negotiation.client(versions:, chosen: version, random:).with(original:, reacted: true)
58
+
59
+ # The version information this end sends: a client's Chosen Version is
60
+ # the one its attempt started in, a server's the Negotiated Version.
61
+ def information
62
+ TransportParameters::VersionInformation.new(chosen: role == :client ? chosen.number : current.number, available:)
63
+ end
64
+
65
+ # Whether a long header packet of `type` (:initial, :zero_rtt or
66
+ # :handshake) in `version` from the peer is one to take (RFC 9369
67
+ # "Compatible Negotiation Requirements"). A server takes Initials in the
68
+ # client's version and the one it chose, 0-RTT in the client's alone,
69
+ # and Handshake packets in the one it chose. A client takes, until it
70
+ # knows the version, those of the version it started in or of one it
71
+ # offered that is compatible with it, and then only of that version.
72
+ def accepts?(type, version)
73
+ if role == :server
74
+ case type
75
+ when :initial then version == original || version == negotiated
76
+ when :zero_rtt then version == original
77
+ else version == negotiated
78
+ end
79
+ elsif (known = negotiated)
80
+ version == known
81
+ else
82
+ version == chosen || (available.include?(version.number) && chosen.compatible_with?(version))
83
+ end
84
+ end
85
+
86
+ # A Violation of VERSION_NEGOTIATION_ERROR when the peer's version
87
+ # information `information` (nil when it sent none) shows a downgrade
88
+ # (RFC 9368 "Version Downgrade Prevention"), nil when it does not.
89
+ def check(information)
90
+ reason = role == :server ? server_check(information) : client_check(information)
91
+ Violation.new(reason, code: ErrorCode::VERSION_NEGOTIATION_ERROR, frame_type: CryptoStream::FRAME_TYPE) if reason
92
+ end
93
+
94
+ # A server closes when the client's Chosen Version is not the version
95
+ # its Initial came in. A client that sends none may be one of version 1
96
+ # alone; one of another version breaks RFC 9369, which requires it.
97
+ private def server_check(information)
98
+ if information.nil?
99
+ format("no version_information in version 0x%08x", original.number) unless original == Version::V1
100
+ elsif information.chosen != original.number
101
+ format("the client's chosen version 0x%08x is not 0x%08x, the version its Initial came in", information.chosen, original.number)
102
+ end
103
+ end
104
+
105
+ # A client closes when the server's Chosen Version is not the version in
106
+ # use, or not one it offered. A server that switched versions supports
107
+ # RFC 9368, and one that sends no version information did not.
108
+ #
109
+ # After it reacted to a Version Negotiation packet, it closes on none,
110
+ # but that a server of version 1 alone sends none, which stands for
111
+ # version 1 offered alone (RFC 9368 "Special Handling for QUIC Version
112
+ # 1"), and on Available Versions that would not have led it to the
113
+ # version it started again in, had a Version Negotiation packet listed
114
+ # them and the version negotiated, none at all among them (RFC 9368
115
+ # "Version Downgrade Prevention").
116
+ private def client_check(information)
117
+ if information.nil?
118
+ return "no version_information after a Version Negotiation" if reacted && chosen != Version::V1
119
+ return format("no version_information from a server that switched to 0x%08x", current.number) if !reacted && current != chosen
120
+ return nil unless reacted
121
+ information = TransportParameters::VersionInformation.new(chosen: Version::V1.number, available: [Version::V1.number])
122
+ end
123
+ if information.chosen != current.number
124
+ format("the server's chosen version 0x%08x is not 0x%08x, the version in use", information.chosen, current.number)
125
+ elsif !available.include?(information.chosen)
126
+ format("the server's chosen version 0x%08x is none of those offered", information.chosen)
127
+ elsif reacted
128
+ downgrade(information.available)
129
+ end
130
+ end
131
+
132
+ # Why the server's Available Versions show a downgrade to a client that
133
+ # reacted to a Version Negotiation packet, nil when they do not.
134
+ private def downgrade(server_versions)
135
+ return "no available versions after a Version Negotiation" if server_versions.empty?
136
+ listed = [*server_versions, current.number]
137
+ selected = versions.find { |version| listed.include?(version.number) }
138
+ return nil if selected.nil? || selected == chosen
139
+ format("a Version Negotiation of %s would have led to 0x%08x, not 0x%08x",
140
+ listed.map { |number| format("0x%08x", number) }.join(", "), selected.number, chosen.number)
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipaddr"
4
+
5
+ module Raiha::QUIC
6
+ # The token of a NEW_TOKEN frame (RFC 9000 "Address Validation for Future
7
+ # Connections"), which a client puts in the Initial of a later connection
8
+ # to show it receives at the address it sends from:
9
+ #
10
+ # kind (1, a NEW_TOKEN token) || nonce (12, random)
11
+ # || AES-256-GCM(QUIC version (4) || expiry (8, seconds since the epoch))
12
+ #
13
+ # with the kind and the client's IP address, its bytes without the port,
14
+ # as the associated data. The nonce makes every token one of its own;
15
+ # the version, which RFC 9369 "TLS Resumption and NEW_TOKEN Tokens" asks
16
+ # to check, and the expiry are sealed, so that nothing of the connection
17
+ # it came on shows. The key comes from the Endpoint's retry_token_key, by
18
+ # a label of its own. No token is recorded as used: the address and the
19
+ # expiry limit replays, and a token from the same address may be taken
20
+ # again until it expires.
21
+ module NewToken
22
+ KIND = 1
23
+ LIFETIME = 86_400
24
+ NONCE_LENGTH = 12
25
+ # kind, nonce, version, expiry and the tag.
26
+ LENGTH = 1 + NONCE_LENGTH + 4 + 8 + Raiha::TLS::Crypto::AEAD::TAG_LENGTH
27
+
28
+ # The key a server seals its NEW_TOKEN tokens with.
29
+ def self.key(retry_token_key)
30
+ Raiha::TLS::KeySchedule.expand_label(secret: retry_token_key, label: "new token", length: 32, hash: :sha256, prefix: "raiha ")
31
+ end
32
+
33
+ def self.mint(key:, address:, version:, time:, random:)
34
+ nonce = random.bytes(NONCE_LENGTH).b
35
+ header = [KIND].pack("C")
36
+ sealed = Raiha::TLS::Crypto::AEAD.new(:aes_256_gcm, key).seal(nonce:, aad: header + bound(address),
37
+ plaintext: [version, time.to_i + LIFETIME].pack("NQ>"))
38
+ header + nonce + sealed
39
+ end
40
+
41
+ # Whether `token` is one made for `address` and `version` that has not
42
+ # expired.
43
+ def self.valid?(token, key:, address:, version:, time:)
44
+ return false unless token.bytesize == LENGTH && token.getbyte(0) == KIND
45
+ header = token.byteslice(0, 1).to_s
46
+ nonce = token.byteslice(1, NONCE_LENGTH).to_s
47
+ opened = Raiha::TLS::Crypto::AEAD.new(:aes_256_gcm, key).open(nonce:, aad: header + bound(address),
48
+ ciphertext: token.byteslice(1 + NONCE_LENGTH..).to_s) or return false
49
+ sealed_version, expiry = opened.unpack("NQ>")
50
+ sealed_version == version && time.to_i <= expiry.to_i
51
+ end
52
+
53
+ private_class_method def self.bound(address) = IPAddr.new(address.fetch(0)).hton
54
+ end
55
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The pacer of a path (RFC 9002 "Pacing"): a token bucket that fills at
5
+ # the congestion controller's pacing rate, up to `capacity`, the most RFC
6
+ # 9002 lets go in a burst, or up to what the rate gives in `granularity`
7
+ # seconds when that is more, as quicly's burst window is, so that waking
8
+ # no more often than that does not slow the rate down. A datagram goes
9
+ # while the bucket holds its worth, and takes what it sends from it; an
10
+ # idle path saves up no more than one burst. When a datagram has to wait,
11
+ # `release_at` is when it may go, rounded up to `granularity`, so that the
12
+ # loop that sends wakes no more often than that. No rate, before an RTT
13
+ # sample or on a path of no delay, is no pacing.
14
+ class Pacer
15
+ def initialize(capacity:, granularity:)
16
+ @capacity = capacity
17
+ @granularity = granularity
18
+ @tokens = capacity.to_f
19
+ @filled_at = nil #: Float?
20
+ end
21
+
22
+ # Whether a datagram of `size` bytes may go at `now`, at `rate` bytes a
23
+ # second.
24
+ def ready?(size, rate, now)
25
+ fill(rate, now)
26
+ @tokens >= size
27
+ end
28
+
29
+ # The burst the bucket holds from now on, as the datagrams of the path
30
+ # grow; what it holds now stays.
31
+ attr_writer :capacity
32
+
33
+ # A datagram of `bytes` went; one that went without waiting, a probe
34
+ # say, still takes its bytes, and the next waits the longer.
35
+ def sent(bytes) = @tokens -= bytes
36
+
37
+ # When a datagram of `size` bytes may go, at `rate`: nil when it may go
38
+ # at once.
39
+ def release_at(size, rate, now)
40
+ return nil if ready?(size, rate, now) || rate.nil?
41
+ wait = (size - @tokens) / rate
42
+ now + (wait / @granularity).ceil * @granularity
43
+ end
44
+
45
+ private def fill(rate, now)
46
+ filled_at = @filled_at
47
+ @filled_at = now
48
+ return @tokens = @capacity.to_f if rate.nil?
49
+ @tokens = [@tokens + rate * (now - filled_at), [@capacity.to_f, rate * @granularity].max].min if filled_at && now > filled_at
50
+ end
51
+ end
52
+ end