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,58 @@
1
+ # A server and a client of the runtime on the loopback interface, each
2
+ # writing a qlog of its connection to tmp/raiha-example-qlog: the
3
+ # handshake and an echo on a stream, as in high_level_loopback.rb. Each
4
+ # trace is a JSON-SEQ file (.sqlog), which qvis and other qlog tools read:
5
+ # the server's named by the connection ID the client first chose, the
6
+ # client's by the time it connected.
7
+ #
8
+ # Usage:
9
+ # bundle exec ruby -Ilib examples/qlog_output.rb
10
+ #
11
+ # Expected output:
12
+ # qlog written: 1 client and 1 server trace in tmp/raiha-example-qlog
13
+
14
+ require "raiha/runtime"
15
+ # The scheduler of async reads with IO::Buffer, which Ruby 4.0 warns is
16
+ # experimental.
17
+ Warning[:experimental] = false
18
+ require "async"
19
+ require "fileutils"
20
+ require "securerandom"
21
+ require_relative "_certificate"
22
+
23
+ Runtime = Raiha::Runtime
24
+ QUIC = Raiha::QUIC
25
+ TLS = Raiha::TLS
26
+ REGISTRY = TLS::Extension::Registry::BUILTIN.with(QUIC::TransportParameters::Extension)
27
+ ALPN = ["hq-interop"].freeze
28
+ QLOG_DIR = "tmp/raiha-example-qlog"
29
+
30
+ FileUtils.rm_rf(QLOG_DIR)
31
+ FileUtils.mkdir_p(QLOG_DIR)
32
+ server_tls = TLS::Config.server(profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
33
+ server = Runtime::Server.new(config: QUIC::Config.new, tls_config: server_tls,
34
+ keyring: TLS::Keyring.server(config: server_tls, identities: [ExampleCertificate.identity]), host: "127.0.0.1", port: 0,
35
+ qlog_dir: QLOG_DIR)
36
+ client_tls = TLS::Config.client(trust_anchors: [ExampleCertificate.ca_der], profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
37
+ payload = SecureRandom.random_bytes(256)
38
+ echoed = nil
39
+
40
+ Async do
41
+ running = Async { server.run { |session| session.each_stream { |stream| (stream << stream.read).close_write } } }
42
+ sleep 0.01 until server.local_address
43
+ Runtime::Client.connect("127.0.0.1", server.local_address[1], server_name: "localhost", config: QUIC::Config.new, tls_config: client_tls,
44
+ keyring: TLS::Keyring.client(config: client_tls), qlog_dir: QLOG_DIR) do |session|
45
+ stream = session.open_stream
46
+ stream << payload
47
+ stream.close_write
48
+ echoed = stream.read
49
+ end
50
+ server.stop(timeout: 1)
51
+ running.wait
52
+ end
53
+
54
+ raise "Echo payload mismatch" unless echoed == payload
55
+
56
+ clients = Dir.glob(File.join(QLOG_DIR, "*client*.sqlog")).size
57
+ servers = Dir.glob(File.join(QLOG_DIR, "*server*.sqlog")).size
58
+ puts "qlog written: #{clients} client and #{servers} server trace in #{QLOG_DIR}"
@@ -0,0 +1,66 @@
1
+ # A richer qlog than qlog_output.rb: one connection of the runtime on the
2
+ # loopback interface carries ROUNDTRIPS echoes, each on a stream of its
3
+ # own, of 512 random bytes, so that the traces in
4
+ # tmp/raiha-example-qlog-roundtrips show every packet sent and received
5
+ # over many round trips, for qlog tools to draw. ROUNDTRIPS is 20 unless
6
+ # the environment says otherwise; the server lets a client open 100
7
+ # streams.
8
+ #
9
+ # Usage:
10
+ # bundle exec ruby -Ilib examples/qlog_roundtrips.rb
11
+ # ROUNDTRIPS=50 bundle exec ruby -Ilib examples/qlog_roundtrips.rb
12
+ #
13
+ # Expected output:
14
+ # 20 roundtrips OK (512 bytes each)
15
+ # qlog written: 1 client and 1 server trace in tmp/raiha-example-qlog-roundtrips
16
+
17
+ require "raiha/runtime"
18
+ # The scheduler of async reads with IO::Buffer, which Ruby 4.0 warns is
19
+ # experimental.
20
+ Warning[:experimental] = false
21
+ require "async"
22
+ require "fileutils"
23
+ require "securerandom"
24
+ require_relative "_certificate"
25
+
26
+ Runtime = Raiha::Runtime
27
+ QUIC = Raiha::QUIC
28
+ TLS = Raiha::TLS
29
+ REGISTRY = TLS::Extension::Registry::BUILTIN.with(QUIC::TransportParameters::Extension)
30
+ ALPN = ["hq-interop"].freeze
31
+ QLOG_DIR = "tmp/raiha-example-qlog-roundtrips"
32
+ ROUNDTRIPS = Integer(ENV.fetch("ROUNDTRIPS", 20))
33
+ PAYLOAD_SIZE = 512
34
+
35
+ FileUtils.rm_rf(QLOG_DIR)
36
+ FileUtils.mkdir_p(QLOG_DIR)
37
+ server_tls = TLS::Config.server(profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
38
+ server = Runtime::Server.new(config: QUIC::Config.new, tls_config: server_tls,
39
+ keyring: TLS::Keyring.server(config: server_tls, identities: [ExampleCertificate.identity]), host: "127.0.0.1", port: 0,
40
+ qlog_dir: QLOG_DIR)
41
+ client_tls = TLS::Config.client(trust_anchors: [ExampleCertificate.ca_der], profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
42
+ matched = 0
43
+
44
+ Async do
45
+ running = Async { server.run { |session| session.each_stream { |stream| (stream << stream.read).close_write } } }
46
+ sleep 0.01 until server.local_address
47
+ Runtime::Client.connect("127.0.0.1", server.local_address[1], server_name: "localhost", config: QUIC::Config.new, tls_config: client_tls,
48
+ keyring: TLS::Keyring.client(config: client_tls), qlog_dir: QLOG_DIR) do |session|
49
+ ROUNDTRIPS.times do
50
+ payload = SecureRandom.random_bytes(PAYLOAD_SIZE)
51
+ stream = session.open_stream
52
+ stream << payload
53
+ stream.close_write
54
+ matched += 1 if stream.read == payload
55
+ end
56
+ end
57
+ server.stop(timeout: 1)
58
+ running.wait
59
+ end
60
+
61
+ raise "#{ROUNDTRIPS - matched} of #{ROUNDTRIPS} echoes did not match" unless matched == ROUNDTRIPS
62
+
63
+ puts "#{ROUNDTRIPS} roundtrips OK (#{PAYLOAD_SIZE} bytes each)"
64
+ clients = Dir.glob(File.join(QLOG_DIR, "*client*.sqlog")).size
65
+ servers = Dir.glob(File.join(QLOG_DIR, "*server*.sqlog")).size
66
+ puts "qlog written: #{clients} client and #{servers} server trace in #{QLOG_DIR}"
@@ -0,0 +1,62 @@
1
+ # A client of the quic gem (github: unasuke/quic-ruby, ngtcp2 and picotls
2
+ # under a thin binding) against the runtime's server on the loopback
3
+ # interface: the client completes the handshake, sends a payload on a
4
+ # stream of its own, and the server answers it with "ECHO:" and the
5
+ # payload.
6
+ #
7
+ # The client runs in a process of its own (test/support/quic_gem_echo_client.rb):
8
+ # the quic gem carries a TLS library of its own, which crashes Ruby when
9
+ # loaded beside Ruby's openssl, which raiha needs.
10
+ #
11
+ # The quic gem is in the optional :interop group of the bundle, so no test
12
+ # runs this example. Run with:
13
+ # bundle install --with interop
14
+ # bundle exec ruby -Ilib examples/quic_gem_loopback.rb
15
+ #
16
+ # Expected output:
17
+ # The quic gem's client (ngtcp2) connected, stream echo OK
18
+
19
+ require "raiha/runtime"
20
+ # The scheduler of async reads with IO::Buffer, which Ruby 4.0 warns is
21
+ # experimental.
22
+ Warning[:experimental] = false
23
+ require "async"
24
+ require "securerandom"
25
+ require_relative "_certificate"
26
+
27
+ Runtime = Raiha::Runtime
28
+ QUIC = Raiha::QUIC
29
+ TLS = Raiha::TLS
30
+ REGISTRY = TLS::Extension::Registry::BUILTIN.with(QUIC::TransportParameters::Extension)
31
+ ALPN = "raiha-interop"
32
+ ECHO_CLIENT = File.expand_path("../test/support/quic_gem_echo_client.rb", __dir__)
33
+
34
+ server_tls = TLS::Config.server(profile: TLS::Profile::QUIC, alpn: [ALPN], registry: REGISTRY)
35
+ server = Runtime::Server.new(config: QUIC::Config.new, tls_config: server_tls,
36
+ keyring: TLS::Keyring.server(config: server_tls, identities: [ExampleCertificate.identity]), host: "127.0.0.1", port: 0)
37
+ payload = SecureRandom.hex(16)
38
+ output = nil
39
+ status = nil
40
+
41
+ Async do
42
+ running = Async { server.run { |session| session.each_stream { |stream| (stream << "ECHO:" << stream.read).close_write } } }
43
+ sleep 0.01 until server.local_address
44
+ # The client's process, waited for in a thread so that the server's
45
+ # fiber goes on.
46
+ output, status = Thread.new do
47
+ reader, writer = IO.pipe
48
+ pid = Process.spawn("bundle", "exec", "ruby", ECHO_CLIENT, "--host", "127.0.0.1", "--port", server.local_address[1].to_s, "--alpn", ALPN,
49
+ "--data", payload, out: writer, err: writer)
50
+ writer.close
51
+ text = reader.read
52
+ [text, Process.wait2(pid).last]
53
+ end.value
54
+ server.stop(timeout: 1)
55
+ running.wait
56
+ end
57
+
58
+ unless status.success? && output.include?("HANDSHAKE_COMPLETE") && output.include?("ECHO_OK")
59
+ raise "the quic gem's client failed (#{status.inspect}):\n#{output}"
60
+ end
61
+
62
+ puts "The quic gem's client (ngtcp2) connected, stream echo OK"
@@ -0,0 +1,68 @@
1
+ # A stream between a client and a server Connection in one process, with
2
+ # no socket: once the handshake is done, the client sends 256 random bytes
3
+ # on a stream of its own and ends it; the server reads them as they come
4
+ # (Event::StreamReadable) and sends the same bytes back on that stream;
5
+ # the client reads them back. Each side's datagrams are handed to the
6
+ # other by hand.
7
+ #
8
+ # Usage:
9
+ # bundle exec ruby -Ilib examples/stream_echo.rb
10
+ #
11
+ # Expected output:
12
+ # Echo matched: 256 bytes
13
+
14
+ require "raiha/quic"
15
+ require "securerandom"
16
+ require_relative "_certificate"
17
+
18
+ QUIC = Raiha::QUIC
19
+ TLS = Raiha::TLS
20
+ REGISTRY = TLS::Extension::Registry::BUILTIN.with(QUIC::TransportParameters::Extension)
21
+ ALPN = ["hq-interop"].freeze
22
+ CLIENT = ["192.0.2.1", 50_000].freeze
23
+ SERVER = ["192.0.2.2", 443].freeze
24
+
25
+ client_tls = TLS::Config.client(trust_anchors: [ExampleCertificate.ca_der], profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
26
+ server_tls = TLS::Config.server(profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
27
+ now = 0.0
28
+ client = QUIC::Connection.client(config: QUIC::Config.new, tls_config: client_tls, keyring: TLS::Keyring.client(config: client_tls),
29
+ server_name: "localhost", remote: SERVER, time: Time.now, now:)
30
+ server = QUIC::Connection.server(config: QUIC::Config.new, tls_config: server_tls,
31
+ keyring: TLS::Keyring.server(config: server_tls, identities: [ExampleCertificate.identity]), time: Time.now, now:)
32
+
33
+ # Passes what each side has to send to the other, and moves the time on.
34
+ exchange = lambda do
35
+ client.each_transmit(now:) { |transmit| server.receive(transmit.payload, from: CLIENT, now:) }
36
+ server.each_transmit(now:) { |transmit| client.receive(transmit.payload, from: SERVER, now:) }
37
+ now += 0.001
38
+ end
39
+
40
+ exchange.call until client.handshake_confirmed? && server.handshake_complete?
41
+
42
+ payload = SecureRandom.random_bytes(256)
43
+ stream_id = client.open_stream
44
+ client.stream_write(stream_id, payload, fin: true)
45
+
46
+ # The server echoes what it reads on each stream that becomes readable,
47
+ # and ends the stream once the client has ended it.
48
+ received = Hash.new { |all, id| all[id] = "".b }
49
+ echoed = "".b
50
+ 50.times do
51
+ exchange.call
52
+ server.each_event do |event|
53
+ next unless event.is_a?(QUIC::Event::StreamReadable)
54
+ while (chunk = server.stream_read(event.stream_id, 65_536)) && !chunk.empty?
55
+ received[event.stream_id] << chunk
56
+ end
57
+ server.stream_write(event.stream_id, received[event.stream_id], fin: true) if chunk.nil?
58
+ end
59
+ client.each_event { nil }
60
+ while (chunk = client.stream_read(stream_id, 65_536)) && !chunk.empty?
61
+ echoed << chunk
62
+ end
63
+ break if chunk.nil?
64
+ end
65
+
66
+ raise "Echo payload mismatch" unless echoed == payload
67
+
68
+ puts "Echo matched: #{echoed.bytesize} bytes"
@@ -0,0 +1,76 @@
1
+ # A QUIC handshake over UDP on the loopback interface, with the loop an
2
+ # application writes around a connection: send what there is to send, take
3
+ # the events, then wait for a datagram or the next timeout, whichever comes
4
+ # first. A server's Endpoint runs the same loop in a thread of its own,
5
+ # taking every client.
6
+ #
7
+ # Usage:
8
+ # bundle exec ruby -Ilib examples/udp_handshake.rb
9
+ #
10
+ # Expected output:
11
+ # Connected to localhost with hq-interop
12
+ # Handshake complete over UDP (client: true, server: true)
13
+
14
+ require "raiha/quic"
15
+ require "socket"
16
+ require_relative "_certificate"
17
+
18
+ QUIC = Raiha::QUIC
19
+ TLS = Raiha::TLS
20
+ REGISTRY = TLS::Extension::Registry::BUILTIN.with(QUIC::TransportParameters::Extension)
21
+ ALPN = ["hq-interop"].freeze
22
+
23
+ def mono = Process.clock_gettime(Process::CLOCK_MONOTONIC)
24
+
25
+ def handle(ev)
26
+ puts "Connected to #{ev.handshake.server_name} with #{ev.handshake.alpn}" if ev.is_a?(QUIC::Event::Connected)
27
+ end
28
+
29
+ server_tls = TLS::Config.server(profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
30
+ server_keyring = TLS::Keyring.server(config: server_tls, identities: [ExampleCertificate.identity])
31
+ server_socket = UDPSocket.new
32
+ server_socket.bind("127.0.0.1", 0)
33
+ endpoint = QUIC::Endpoint.server(config: QUIC::Config.new, random: SecureRandom, time: Time.now, now: mono)
34
+ accepted = Thread::Queue.new
35
+ Thread.new do
36
+ loop do
37
+ endpoint.each_transmit(now: mono) { |t| server_socket.send(t.payload, 0, *t.destination) }
38
+
39
+ deadline = endpoint.next_timeout
40
+ timeout = deadline ? [deadline - mono, 0].max : Float::INFINITY
41
+ if IO.select([server_socket], nil, nil, timeout)
42
+ data, addr = server_socket.recvfrom(65535)
43
+ route = endpoint.receive(data, from: [addr[3], addr[1]], now: mono)
44
+ route = endpoint.admit(route.handle, now: mono) if route.is_a?(QUIC::Route::Incoming)
45
+ accepted << endpoint.accept(route.handle, tls_config: server_tls, keyring: server_keyring, now: mono) if route.is_a?(QUIC::Route::ClientHello)
46
+ server_socket.send(route.transmit.payload, 0, *route.transmit.destination) if route.is_a?(QUIC::Route::Respond)
47
+ else
48
+ endpoint.handle_timeout(now: mono)
49
+ end
50
+ end
51
+ end
52
+
53
+ client_tls = TLS::Config.client(trust_anchors: [ExampleCertificate.ca_der], profile: TLS::Profile::QUIC, alpn: ALPN, registry: REGISTRY)
54
+ socket = UDPSocket.new
55
+ socket.bind("127.0.0.1", 0)
56
+ conn = QUIC::Connection.client(config: QUIC::Config.new, tls_config: client_tls, keyring: TLS::Keyring.client(config: client_tls),
57
+ server_name: "localhost", remote: ["127.0.0.1", server_socket.addr[1]], time: Time.now, now: mono)
58
+
59
+ until conn.handshake_confirmed? || conn.closed?
60
+ conn.each_transmit(now: mono) { |t| socket.send(t.payload, 0, *t.destination) }
61
+ conn.each_event { |ev| handle(ev) }
62
+
63
+ deadline = conn.next_timeout
64
+ timeout = deadline ? [deadline - mono, 0].max : Float::INFINITY
65
+ if IO.select([socket], nil, nil, timeout)
66
+ data, addr = socket.recvfrom(65535)
67
+ conn.receive(data, from: [addr[3], addr[1]], now: mono)
68
+ else
69
+ conn.handle_timeout(now: mono)
70
+ end
71
+ end
72
+ abort "Handshake failed: #{conn.close_reason&.reason}" if conn.closed?
73
+
74
+ conn.close(now: mono)
75
+ conn.each_transmit(now: mono) { |t| socket.send(t.payload, 0, *t.destination) }
76
+ puts "Handshake complete over UDP (client: #{conn.handshake_complete?}, server: #{accepted.pop.handshake_complete?})"
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # What the HTTP/3 layer asks of the transport below it besides writing
5
+ # to streams, for whatever joins the two to do (HTTP/3 does not know
6
+ # QUIC).
7
+ module Action
8
+ # Abandon sending on `stream_id`, with `error_code`.
9
+ class Reset < Data.define(:stream_id, :error_code); end
10
+
11
+ # Ask the peer to stop sending on `stream_id`, with `error_code`.
12
+ class StopSending < Data.define(:stream_id, :error_code); end
13
+
14
+ # Send `stream_id` with `urgency` and as `incremental` (RFC 9218).
15
+ class Priority < Data.define(:stream_id, :urgency, :incremental); end
16
+
17
+ # Close the connection with `error_code`, an application's, for
18
+ # `reason`. Nothing more comes from this layer after it.
19
+ class Close < Data.define(:error_code, :reason); end
20
+ end
21
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "varint"
4
+
5
+ module Raiha::HTTP3
6
+ # A Capsule of the Capsule Protocol (RFC 9297 "The Capsule Protocol"): a
7
+ # type and a value, which the request's data stream carries in place of
8
+ # content. This layer reads and writes any type; what a type means is
9
+ # for the layers above, except DATAGRAM, which is an HTTP Datagram. The
10
+ # value is frozen and binary, copied first when the caller's is not.
11
+ class Capsule < Data.define(:type, :value)
12
+ DATAGRAM = 0x00
13
+
14
+ def initialize(type:, value:)
15
+ raise ArgumentError, "a Capsule Type is 0..2^62-1, not #{type.inspect}" unless type.is_a?(Integer) && type.between?(0, Varint::MAX)
16
+ value = value.frozen? && value.encoding == Encoding::BINARY ? value : value.b.freeze
17
+ super
18
+ end
19
+
20
+ # The bytes of `capsule` on a data stream.
21
+ def self.encode(capsule) = Varint.encode(capsule.type) << Varint.encode(capsule.value.bytesize) << capsule.value
22
+ end
23
+
24
+ # Reads the Capsules of one data stream from its bytes as they come.
25
+ # One whose value is longer than `max_value` is passed over without its
26
+ # bytes held, as RFC 9297 asks of one too long to use.
27
+ class CapsuleReader
28
+ # The longest a type and a length take together.
29
+ MAX_HEADER = 16
30
+
31
+ def initialize(max_value:)
32
+ @max_value = max_value
33
+ @header = "".b
34
+ @type = nil #: Integer?
35
+ @left = 0
36
+ @value = nil #: String?
37
+ end
38
+
39
+ # Whether the bytes so far end inside a Capsule, which makes the
40
+ # message malformed if the stream ends there (RFC 9297 "Error
41
+ # Handling").
42
+ def mid_capsule? = !@header.empty? || !@type.nil?
43
+
44
+ # Reads `bytes`, yielding each Capsule whole.
45
+ def feed(bytes, &)
46
+ offset = 0
47
+ while offset < bytes.bytesize
48
+ offset = @type ? read_value(bytes, offset, &) : read_header(bytes, offset, &)
49
+ end
50
+ end
51
+
52
+ private def read_header(bytes, offset, &)
53
+ held = @header.bytesize
54
+ source = held.zero? ? bytes : @header << bytes.byteslice(offset, MAX_HEADER - held) # steep:ignore
55
+ start = held.zero? ? offset : 0
56
+ type, after = Varint.decode(source, start)
57
+ length, after = Varint.decode(source, after) if type && after
58
+ unless length && after
59
+ @header = source.byteslice(start..) if held.zero? # steep:ignore
60
+ return bytes.bytesize
61
+ end
62
+ @header = "".b
63
+ @type = type
64
+ @left = length
65
+ @value = length <= @max_value ? "".b : nil
66
+ finish(&) if length.zero?
67
+ held.zero? ? after : offset + after - held
68
+ end
69
+
70
+ private def read_value(bytes, offset, &)
71
+ taken = [@left, bytes.bytesize - offset].min
72
+ @value&.<<(bytes.byteslice(offset, taken)) # steep:ignore
73
+ @left -= taken
74
+ finish(&) if @left.zero?
75
+ offset + taken
76
+ end
77
+
78
+ private def finish
79
+ type = @type or return
80
+ value = @value
81
+ @type = nil
82
+ @value = nil
83
+ yield Capsule.new(type:, value: value.freeze) if value
84
+ end
85
+ end
86
+ end