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,301 @@
1
+ # HTTP/3 server
2
+
3
+ Raiha's HTTP/3 does no I/O of its own. A server is three pieces joined by a loop that you write:
4
+
5
+ - `Raiha::QUIC::Endpoint` takes the datagrams of a UDP socket and turns new clients into `Raiha::QUIC::Connection`s.
6
+ - `Raiha::HTTP3::Connection.server` is the HTTP/3 of one of those connections: it takes the bytes of streams and gives back requests as events.
7
+ - `Raiha::HTTP3Binding` joins one QUIC connection and its HTTP/3 connection: it carries QUIC's stream events and bytes to HTTP/3, and what HTTP/3 writes to QUIC.
8
+
9
+ The loop below blocks its thread on `IO.select` and needs no Fiber scheduler. Under a Fiber scheduler, `Raiha::Runtime::HTTP3::Server` runs this loop for you, with less code; see [fiber-scheduler.md](fiber-scheduler.md). The QUIC side of the loop (routes, Retry, timers) is described in [quic.md](quic.md), and the TLS and QUIC settings in [configuration.md](configuration.md).
10
+
11
+ ## A complete server
12
+
13
+ This server answers every request with `200` and `hello from <path>`. It loads `cert.pem` (the chain, leaf first) and `key.pem`, and stops on Ctrl-C.
14
+
15
+ ```ruby
16
+ require "raiha"
17
+ require "socket"
18
+
19
+ def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
20
+
21
+ chain = OpenSSL::X509::Certificate.load(File.read("cert.pem")).map(&:to_der)
22
+ key = OpenSSL::PKey.read(File.read("key.pem")).private_to_der
23
+ identity = Raiha::TLS::Keyring::Identity.new(cert_der: chain, key_der: key)
24
+
25
+ tls_config = Raiha::TLS::Config.server(
26
+ profile: Raiha::TLS::Profile::QUIC,
27
+ alpn: ["h3"],
28
+ registry: Raiha::TLS::Extension::Registry::BUILTIN.with(Raiha::QUIC::TransportParameters::Extension)
29
+ )
30
+ keyring = Raiha::TLS::Keyring.server(config: tls_config, identities: [identity])
31
+
32
+ socket = UDPSocket.new
33
+ socket.bind("127.0.0.1", 4433)
34
+ endpoint = Raiha::QUIC::Endpoint.server(config: Raiha::QUIC::Config.new(ecn_address_families: [], enable_pmtud: false), random: SecureRandom, time: Time.now, now:)
35
+ # Each QUIC connection with its HTTP/3 connection and the binding between them.
36
+ connections = {}
37
+
38
+ def respond(http3, event)
39
+ return unless event.is_a?(Raiha::HTTP3::Event::Headers)
40
+ path = event.headers.to_h[":path"]
41
+ http3.send_headers(event.stream_id, [[":status", "200"], ["content-type", "text/plain"]])
42
+ http3.send_data(event.stream_id, "hello from #{path}\n", fin: true)
43
+ end
44
+
45
+ begin
46
+ loop do
47
+ endpoint.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
48
+
49
+ deadline = endpoint.next_timeout
50
+ if IO.select([socket], nil, nil, deadline && [deadline - now, 0].max)
51
+ data, address = socket.recvfrom(65535)
52
+ route = endpoint.receive(data, from: [address[3], address[1]], now:)
53
+ route = endpoint.admit(route.handle, now:) if route.is_a?(Raiha::QUIC::Route::Incoming)
54
+ case route
55
+ when Raiha::QUIC::Route::ClientHello
56
+ quic = endpoint.accept(route.handle, tls_config:, keyring:, now:)
57
+ http3 = Raiha::HTTP3::Connection.server
58
+ connections[quic] = [http3, Raiha::HTTP3Binding.new(quic, http3)]
59
+ when Raiha::QUIC::Route::Respond
60
+ socket.send(route.transmit.payload, 0, *route.transmit.destination)
61
+ end
62
+ end
63
+ deadline = endpoint.next_timeout
64
+ endpoint.handle_timeout(now:) if deadline && now >= deadline
65
+
66
+ connections.each do |quic, (http3, binding)|
67
+ quic.each_event { |event| binding.receive(event, now:) }
68
+ binding.flush(now:)
69
+ http3.each_event { |event| respond(http3, event) }
70
+ binding.flush(now:)
71
+ end
72
+ connections.delete_if { |quic, _| quic.closed? }
73
+ end
74
+ rescue Interrupt
75
+ connections.each_key { |quic| quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:) }
76
+ endpoint.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
77
+ end
78
+ ```
79
+
80
+ The client in [http3-client.md](http3-client.md) talks to it, and so does a curl built with HTTP/3: `curl -k --http3-only https://127.0.0.1:4433/`.
81
+
82
+ `ecn_address_families: []` and `enable_pmtud: false` are for the plain `UDPSocket`, which neither marks ECN nor keeps IP from fragmenting path MTU probes; see [quic.md](quic.md#a-plain-socket).
83
+
84
+ ## The loop
85
+
86
+ Each pass of the loop does the same things in the same order:
87
+
88
+ 1. Send what is due: `endpoint.each_transmit` gives every datagram the endpoint and its connections have to send.
89
+ 2. Wait for a datagram, or until `endpoint.next_timeout`, the clock time the next timer is due (`nil` when none is). The clock is any monotonic clock in seconds, passed as `now:` to every call.
90
+ 3. Hand a datagram to `endpoint.receive`, and act on the route it returns. A new client comes as `Route::Incoming`, which `admit` turns into `Route::ClientHello` once its ClientHello is whole; `accept` makes it a connection. `Route::Respond` is a datagram the endpoint answers itself. [quic.md](quic.md) covers the routes, Retry and refusing clients.
91
+ 4. Once `next_timeout` has come, run the timers with `handle_timeout`, whether or not a datagram came, so that a socket that never falls quiet does not hold off loss recovery and the idle timeout.
92
+ 5. For each connection, give its QUIC events to `binding.receive`, then `binding.flush`, take the HTTP/3 events, and `binding.flush` again so that what you wrote goes to QUIC.
93
+
94
+ `binding.receive(event, now:)` returns whether the event was HTTP/3's alone (stream data and stream state). The others are for you as well: `Raiha::QUIC::Event::Connected` when the handshake completes, at which point the binding opens HTTP/3's control and QPACK streams, and `Raiha::QUIC::Event::Closed` when the connection is over.
95
+
96
+ `binding.flush(now:)` reads every stream with data into HTTP/3, carries out what HTTP/3 asks of QUIC (resetting streams, stopping them, priorities, closing the connection on a protocol error), and writes what HTTP/3 has for each stream. Call it after anything you give HTTP/3, before the next `each_transmit`.
97
+
98
+ A flush can bring HTTP/3 events of its own: `Raiha::HTTP3::Event::Writable` comes out of the second flush, after the events were taken. A server that writes bodies larger than the stream takes at once has to go round until a pass brings no event:
99
+
100
+ ```ruby
101
+ connections.each do |quic, (http3, binding, handler)|
102
+ quic.each_event { |event| binding.receive(event, now:) }
103
+ loop do
104
+ binding.flush(now:)
105
+ taken = 0
106
+ http3.each_event { |event| handler.call(event); taken += 1 }
107
+ handler.write_more
108
+ break if taken.zero?
109
+ end
110
+ end
111
+ ```
112
+
113
+ ## Requests
114
+
115
+ Each request comes on a bidirectional stream the client opened (IDs 0, 4, 8, and so on, for each connection) as a series of events:
116
+
117
+ | Event | Fields | What it means |
118
+ | --- | --- | --- |
119
+ | `Raiha::HTTP3::Event::Headers` | `stream_id`, `headers`, `fin` | The header section of a request: `[name, value]` pairs, the pseudo-header fields (`:method`, `:scheme`, `:authority`, `:path`) first. `fin` when the request has no body. |
120
+ | `Raiha::HTTP3::Event::Data` | `stream_id`, `data`, `fin` | Some of the body, as it came. `fin` when the body ends here, which may come with empty `data`. |
121
+ | `Raiha::HTTP3::Event::Trailers` | `stream_id`, `headers` | The trailer section, which ends the request. |
122
+ | `Raiha::HTTP3::Event::Reset` | `stream_id`, `error_code` | The client reset the request; nothing more of it comes. |
123
+ | `Raiha::HTTP3::Event::Stopped` | `stream_id`, `error_code` | The client asked the server to stop sending the response; nothing more goes. |
124
+ | `Raiha::HTTP3::Event::Writable` | `stream_id` | `send_data` takes more on the stream, after it took less than given. |
125
+ | `Raiha::HTTP3::Event::Settings` | `settings` | The client's SETTINGS came; also `http3.peer_settings`. |
126
+ | `Raiha::HTTP3::Event::GoAway` | `stream_id` | The client goes away. |
127
+ | `Raiha::HTTP3::Event::Blocked` | `stream_id` | A header section waits for the QPACK encoder stream, which can happen only when `qpack_max_table_capacity` is above 0. Its `Headers` or `Trailers` follow once it can be decoded; there is nothing to do. |
128
+ | `Raiha::HTTP3::Event::Datagram` | `stream_id`, `payload` | An HTTP Datagram (RFC 9297) for the request, with `h3_datagram` in the SETTINGS. |
129
+ | `Raiha::HTTP3::Event::Capsule` | `stream_id`, `capsule` | A capsule other than DATAGRAM on the stream of an Extended CONNECT request (RFC 9297). |
130
+
131
+ A malformed request never reaches you: HTTP/3 resets its stream with `H3_MESSAGE_ERROR`. A request over `max_field_section_size` is answered with 431 by HTTP/3 itself.
132
+
133
+ Events of different requests interleave, so keep what has come of each request by stream ID, per connection. This handler reads each request whole, body and trailers included, then answers it:
134
+
135
+ ```ruby
136
+ # The requests of one HTTP/3 connection by stream ID, and the response
137
+ # bodies their streams have not taken all of yet.
138
+ class Handler
139
+ def initialize(http3)
140
+ @http3 = http3
141
+ @requests = {}
142
+ @unsent = {}
143
+ @writable = []
144
+ end
145
+
146
+ def busy? = !@requests.empty? || !@unsent.empty?
147
+
148
+ def call(event)
149
+ case event
150
+ when Raiha::HTTP3::Event::Headers
151
+ @requests[event.stream_id] = { headers: event.headers, body: +"", trailers: [] }
152
+ answer(event.stream_id) if event.fin
153
+ when Raiha::HTTP3::Event::Data
154
+ request = @requests[event.stream_id] or return
155
+ request[:body] << event.data
156
+ answer(event.stream_id) if event.fin
157
+ when Raiha::HTTP3::Event::Trailers
158
+ request = @requests[event.stream_id] or return
159
+ request[:trailers] = event.headers
160
+ answer(event.stream_id)
161
+ when Raiha::HTTP3::Event::Writable
162
+ @writable << event.stream_id
163
+ when Raiha::HTTP3::Event::Reset, Raiha::HTTP3::Event::Stopped
164
+ @requests.delete(event.stream_id)
165
+ @unsent.delete(event.stream_id)
166
+ end
167
+ end
168
+
169
+ # Writes more of each body whose stream takes more, once a pass's events
170
+ # are all taken: an event after a Writable may have stopped its stream.
171
+ def write_more
172
+ @writable.each { |stream_id| (data = @unsent.delete(stream_id)) && write(stream_id, data) }
173
+ @writable.clear
174
+ end
175
+
176
+ private def answer(stream_id)
177
+ request = @requests.delete(stream_id)
178
+ method, path = request[:headers].to_h.values_at(":method", ":path")
179
+ @http3.send_headers(stream_id, [[":status", "200"], ["content-type", "text/plain"]])
180
+ if path == "/big"
181
+ write(stream_id, "x" * 1_000_000)
182
+ else
183
+ write(stream_id, "#{method} #{path}: #{request[:body].bytesize} bytes, trailers #{request[:trailers].inspect}\n")
184
+ end
185
+ end
186
+
187
+ # Writes `data` and ends the stream; what the stream does not take now
188
+ # waits for Raiha::HTTP3::Event::Writable.
189
+ private def write(stream_id, data)
190
+ taken = @http3.send_data(stream_id, data, fin: true)
191
+ @unsent[stream_id] = data.byteslice(taken..) if taken < data.bytesize
192
+ end
193
+ end
194
+ ```
195
+
196
+ Make one per connection, `connections[quic] = [http3, Raiha::HTTP3Binding.new(quic, http3), Handler.new(http3)]`, and drive it with the loop of the previous section.
197
+
198
+ Two orderings to keep in mind. A `Writable` can come before a `Stopped` of the same stream in one pass, and `send_data` on a stopped stream raises `Raiha::HTTP3::Error::Usage`; that is why the handler writes only once the pass's events are all taken. And after you let a request go (below), `Data` events that were already queued for it still come, so look a request up before using it.
199
+
200
+ ## Responses
201
+
202
+ - `http3.send_headers(stream_id, headers, fin: false)` sends the response's header section. `headers` is an Array of `[name, value]` Strings, lowercase names, `[":status", "200"]` first. A malformed section raises `ArgumentError`. A status of 1xx is an interim response, which may come before the final one and cannot end the stream: `send_headers(stream_id, [[":status", "103"], ["link", "</style.css>; rel=preload"]])`.
203
+ - `http3.send_data(stream_id, data, fin: false)` sends content and returns how many bytes it took. It holds at most `max_send_buffer` bytes for the stream (64 KiB unless given to `Connection.server`); with less taken than given, `Writable` tells when to give the rest, and `fin` applies only once all of it is taken.
204
+ - `send_headers` once the body has begun sends the trailer section, which always ends the stream. Send it only after `send_data` took all of the body:
205
+
206
+ ```ruby
207
+ http3.send_headers(stream_id, [[":status", "200"]])
208
+ http3.send_data(stream_id, "abc")
209
+ http3.send_headers(stream_id, [["x-sum", "3"]])
210
+ ```
211
+
212
+ To answer without reading the rest of the body, `http3.stop_reading(stream_id)` asks the client to stop sending it with `H3_NO_ERROR`; what of it still comes is dropped, and the response goes on:
213
+
214
+ ```ruby
215
+ http3.stop_reading(stream_id)
216
+ http3.send_headers(stream_id, [[":status", "413"]], fin: true)
217
+ ```
218
+
219
+ `http3.cancel(stream_id, error_code: Raiha::HTTP3::ErrorCode::REQUEST_CANCELLED)` drops a request both ways, resetting the stream.
220
+
221
+ ### Priority
222
+
223
+ A request's `priority` header field (RFC 9218) sets the urgency and incrementality its response is sent with, and a `priority` field in the response's header section overrides what it gives. PRIORITY_UPDATE frames from the client are applied as they come. `http3.update_priority(stream_id, urgency: 1)` changes it from the server's side while the request is open; it raises `Raiha::HTTP3::Error::Usage` for a stream with no request. The binding passes priorities on to QUIC, which sends the streams of lower urgency first.
224
+
225
+ ## Stopping
226
+
227
+ To stop gracefully (RFC 9114 "Connection Shutdown"), send each connection a GOAWAY, keep serving the requests already taken, then close. `http3.goaway` gives the lowest ID of a request not seen yet; HTTP/3 rejects requests from there on with `H3_REQUEST_REJECTED`. A connection whose handshake has not completed has no control stream yet, and `goaway` raises `Raiha::HTTP3::Error::Usage`: close it at once.
228
+
229
+ The GOAWAY has to be sent before the connection closes, since a closing QUIC connection sends nothing but its CONNECTION_CLOSE. With one pass of the loop as a lambda that waits no longer than `longest` seconds, the end of the server becomes:
230
+
231
+ ```ruby
232
+ serve = lambda do |longest = nil|
233
+ endpoint.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
234
+ deadline = endpoint.next_timeout
235
+ wait = [deadline && [deadline - now, 0].max, longest].compact.min
236
+ if IO.select([socket], nil, nil, wait)
237
+ # ... as in the complete server, with Handler.new(http3) for each new connection
238
+ end
239
+ deadline = endpoint.next_timeout
240
+ endpoint.handle_timeout(now:) if deadline && now >= deadline
241
+ # ... each connection's events, as in "The loop"
242
+ connections.delete_if { |quic, _| quic.closed? }
243
+ end
244
+
245
+ begin
246
+ loop { serve.call }
247
+ rescue Interrupt
248
+ connections.each do |quic, (http3, binding, _)|
249
+ http3.goaway
250
+ binding.flush(now:)
251
+ rescue Raiha::HTTP3::Error::Usage
252
+ # No control stream yet: the handshake has not completed.
253
+ quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:)
254
+ end
255
+ # A second for the GOAWAY to go, and up to five for the requests taken.
256
+ stopping = now
257
+ serve.call(0.1) while now - stopping < 1 || (now - stopping < 5 && connections.each_value.any? { |_, _, handler| handler.busy? })
258
+ connections.each_key { |quic| quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:) }
259
+ endpoint.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
260
+ end
261
+ ```
262
+
263
+ Close with `Raiha::HTTP3::ErrorCode::NO_ERROR` (`H3_NO_ERROR`), the application error code of HTTP/3; QUIC's own `NO_ERROR` is not one.
264
+
265
+ ## Settings and 0-RTT
266
+
267
+ `Raiha::HTTP3::Connection.server(settings:)` takes the SETTINGS the server sends, a `Raiha::HTTP3::Settings`:
268
+
269
+ | Keyword | Default | Meaning |
270
+ | --- | --- | --- |
271
+ | `qpack_max_table_capacity` | `0` | The QPACK dynamic table the client may use; 0 is none. With a table, a request's header section may wait for the encoder stream (`Raiha::HTTP3::Event::Blocked`). |
272
+ | `qpack_blocked_streams` | `0` | How many streams may wait so. |
273
+ | `max_field_section_size` | `65536` | The longest header or trailer section taken. |
274
+ | `h3_datagram` | `false` | HTTP Datagrams (RFC 9297), which also need QUIC's `max_datagram_frame_size`. |
275
+ | `enable_connect_protocol` | `false` | Extended CONNECT (RFC 9220). |
276
+
277
+ `Connection.server` without `settings:` uses `Raiha::HTTP3::Settings.new(enable_connect_protocol: true)`, which takes Extended CONNECT.
278
+
279
+ 0-RTT needs a TLS config that issues tickets and allows early data, a record of the 0-RTT taken, and the same SETTINGS for the tickets and the connections (RFC 9114 "Initialization"): HTTP/3 takes 0-RTT only from a ticket issued with the SETTINGS the connection has. The ticket keys and the record are described in [configuration.md](configuration.md).
280
+
281
+ ```ruby
282
+ settings = Raiha::HTTP3::Settings.new
283
+ tls_config = Raiha::TLS::Config.server(
284
+ profile: Raiha::TLS::Profile::QUIC,
285
+ alpn: ["h3"],
286
+ registry: Raiha::TLS::Extension::Registry::BUILTIN.with(Raiha::QUIC::TransportParameters::Extension),
287
+ ticket_key: SecureRandom.bytes(32),
288
+ max_early_data_size: 1
289
+ )
290
+ anti_replay = Raiha::TLS::AntiReplay::Memory.new(now: Time.now)
291
+
292
+ # For each new connection:
293
+ quic = endpoint.accept(route.handle, tls_config:, keyring:, now:, anti_replay:, app_early_data_context: settings.early_data_context)
294
+ http3 = Raiha::HTTP3::Connection.server(settings:)
295
+ ```
296
+
297
+ The record takes no 0-RTT for its first 11 seconds (`Raiha::TLS::AntiReplay::WINDOW`), so that a ClientHello taken before a restart is not taken again. `quic.stream_early_data?(stream_id)` tells whether any of a request came in 0-RTT, which an attacker can replay; answering 425 (Too Early) to one that is not safe to repeat is up to you.
298
+
299
+ ## qlog
300
+
301
+ `Raiha::HTTP3::Connection.server(tracer:)` takes an HTTP/3 tracer. `Raiha::HTTP3::Qlog::Tracer.new(quic_tracer)` writes HTTP/3's events into the qlog of the QUIC connection; see [qlog.md](qlog.md).
data/docs/platforms.md ADDED
@@ -0,0 +1,98 @@
1
+ # Platforms
2
+
3
+ Raiha is pure Ruby. It needs:
4
+
5
+ - Ruby 4.0 or later.
6
+ - The `openssl` gem 4.0 or later, which the gemspec depends on, built against OpenSSL or LibreSSL. Raiha implements TLS 1.3 itself and takes only primitives from it: AEAD ciphers, HKDF and HMAC, X25519 and ECDH, signatures, and X.509 parsing.
7
+
8
+ The protocol layers (`Raiha::TLS`, `Raiha::QUIC`, `Raiha::HTTP3`) do no I/O and run anywhere Ruby does. Everything below is about `Raiha::Runtime`, whose UDP sockets differ by operating system. It is written for Linux, and runs on OpenBSD as well. It chooses what to do from `RUBY_PLATFORM` (GSO and the PMTU discovery options are used on Linux alone) and from the `Socket` constants Ruby defines (the control messages), so on another system it takes whichever of these paths that system's constants allow; only Linux and OpenBSD have been tried.
9
+
10
+ The runtime needs a Fiber scheduler and blocks only the calling fiber ([fiber-scheduler.md](fiber-scheduler.md) uses the `async` gem, which is not a dependency of Raiha). `Raiha::Runtime::Threaded` runs on threads with no scheduler ([threads.md](threads.md)). `Raiha::Runtime::Workers` runs each worker in a Ractor of its own.
11
+
12
+ ## How the runtime adapts
13
+
14
+ `Raiha::Runtime::Server`, `Raiha::Runtime::HTTP3::Server` and `Raiha::Runtime::Client` pass the `Raiha::QUIC::Config` you give them through three adjustments, so the same configuration works on every system:
15
+
16
+ - `UDP.batching`: a config without `max_transmit_size` gets `UDP::MAX_TRANSMIT` (65,507 bytes), so that one `Transmit` carries many datagrams at once.
17
+ - `UDP.pmtud`: `enable_pmtud` becomes false where a socket cannot keep IP from fragmenting (anywhere but Linux), so that no path MTU probe goes out in pieces.
18
+ - `UDP.ecn`: `:ipv4` is taken out of `ecn_address_families` where Ruby has no `IP_RECVTOS` (OpenBSD), so that IPv4 paths send Not-ECT. Such a system also drops an `IP_TOS` control message silently, and QUIC would otherwise count packets it never marked as ECT(0).
19
+
20
+ ## Sending and receiving
21
+
22
+ | | Linux | OpenBSD |
23
+ | --- | --- | --- |
24
+ | Datagrams of one `Transmit` | One `sendmsg` with `UDP_SEGMENT` (GSO); the kernel cuts them | One `sendmsg` each |
25
+ | Fragmentation | Never; `IP_PMTUDISC_PROBE` / `IPV6_PMTUDISC_PROBE`, so a probe that is too large is lost or refused with `EMSGSIZE` | Left to the OS; the runtime sends no PMTU probe |
26
+ | ECN over IPv4 | Marked with `IP_TOS`, read with `IP_RECVTOS` | Not used |
27
+ | ECN over IPv6 | Marked with `IPV6_TCLASS`, read with `IPV6_RECVTCLASS` | Same as Linux |
28
+ | Address a datagram came to | `IP_PKTINFO`, `IPV6_RECVPKTINFO` | `IP_RECVDSTADDR`, `IPV6_RECVPKTINFO` |
29
+ | Address a datagram goes from | `IP_PKTINFO`, `IPV6_PKTINFO` | `IP_SENDSRCADDR`, `IPV6_PKTINFO` |
30
+
31
+ Datagrams are read with `recvmsg`, up to 64 at a time (`UDP::RECEIVE_BATCH`).
32
+
33
+ GSO falls back to one datagram per `sendmsg` when the kernel refuses it. `EIO` (a path without checksum offload, as in the ns-3 simulator) and `ENOPROTOOPT` (a kernel without `UDP_SEGMENT`) turn GSO off for the socket for good. `EINVAL` and `EMSGSIZE` turn it off only if the datagrams then go one by one, since otherwise the destination was at fault.
34
+
35
+ A control message that `sendmsg` refuses with `ENOPROTOOPT` or `EOPNOTSUPP` is left out from then on. The datagram goes again without the ECN mark, then without the source address, then without both. `EINVAL` may be the source address alone, as when Linux no longer has an IPv6 address, so the datagram goes again without the mark and nothing is left out for good.
36
+
37
+ Some errors drop one datagram and do not stop the socket:
38
+
39
+ - `ECONNREFUSED`, `EMSGSIZE`, `ENETUNREACH`, `EHOSTUNREACH` and `EADDRNOTAVAIL`
40
+ - `EINVAL` (such as port 0), `EACCES` (a broadcast address) and `EAFNOSUPPORT` (a family the socket does not send to)
41
+ - `EPERM` (a firewall) and `ENOBUFS`
42
+
43
+ A peer that spoofs its source chooses where responses go, so none of these is a reason to stop.
44
+
45
+ ## Socket buffers
46
+
47
+ Each socket asks for 8 MiB of receive and send buffer (`UDP::BUFFER`), since the Linux default of about 212 KB drops a burst before it is read. Linux cuts the request to `net.core.rmem_max` / `net.core.wmem_max` and reports twice the size it keeps. Raise those limits for a busy server.
48
+
49
+ OpenBSD refuses a size over its limit (`SB_MAX`, 2 MiB) with `ENOBUFS`, so the socket asks for half as much, then half again, until the OS takes it. It never sets a buffer smaller than the one the socket already has.
50
+
51
+ ## IPv4 and IPv6
52
+
53
+ A server binds `"::"` by default (`host: "::"`, `port: 443`).
54
+
55
+ On Linux, the socket clears `IPV6_V6ONLY` and takes IPv4 as well. Addresses of IPv4 peers are reported as IPv4 (`"192.0.2.1"`, not the IPv4-mapped `"::ffff:192.0.2.1"`), and are mapped back when sending.
56
+
57
+ OpenBSD's IPv6 sockets take IPv6 only, and clearing `IPV6_V6ONLY` fails. The runtime handles that as follows:
58
+
59
+ - `Server` and `HTTP3::Server` bound to `"::"` also bind `"0.0.0.0"` on the same port, so they take both families.
60
+ - `Workers` does the same for each worker. All the sockets are bound with `SO_REUSEPORT`.
61
+ - `Client.connect` binds `"::"` for an IPv6 server and `"0.0.0.0"` for an IPv4 one.
62
+ - A connection on such a socket takes addresses of that family only (`Config#address_families`), so it moves to a preferred address of that family alone.
63
+ - Where IPv6 is off and `"::"` cannot be bound, an IPv6 address fails at once and the next address of the host is tried.
64
+
65
+ A server bound to a specific address binds only that address. A server's preferred addresses (`Config#preferred_address`) each get a socket of their own unless a socket already bound takes them. A preferred address that cannot be bound makes `run` raise, so that no client is offered an address the server does not listen at.
66
+
67
+ ## Workers and SO_REUSEPORT
68
+
69
+ `Raiha::Runtime::Workers` binds one socket per worker to the same address with `SO_REUSEPORT`. On Linux the kernel spreads datagrams over those sockets by 4-tuple. Elsewhere, as on OpenBSD, the kernel may give them all to one socket. The worker that has that socket then takes every new connection, and hands on what comes for the others' connections. More than one worker runs, but the load is not spread.
70
+
71
+ On OpenBSD with Ruby 4.0 (4.0.10 included), a worker can crash the whole process with `[BUG] Segmentation fault`, while it serves as well as when it stops. The bug is in Ruby, not in Raiha ([Bug #22453](https://bugs.ruby-lang.org/issues/22453)). Every Ractor but the main one runs its threads on Ruby's M:N scheduler. There, a thread that blocks may resume on another native thread, and on OpenBSD Ruby may then read the previous native thread's state after a fiber switch. Each worker has a Fiber scheduler and a thread of its own that waits on its ports, which is enough to hit it. Ruby's master branch has the fix. Until a Ruby 4.0 release has it, use `Raiha::Runtime::Server` rather than `Raiha::Runtime::Workers` on OpenBSD; with the load not spread there, one server loses little.
72
+
73
+ ## Looking at a socket
74
+
75
+ `Raiha::Runtime::UDP#features` tells what a socket got on this system: its buffer sizes as the OS reports them, and whether it takes IPv4. It also tells which control messages it reads (`ecn_in`, `destination`) and sends (`source`, `ecn_out`), as they are now: a control message refused later turns its flag off. The runtime logs none of this.
76
+
77
+ ```ruby
78
+ require "raiha"
79
+
80
+ ["::", "0.0.0.0"].each do |host|
81
+ udp = Raiha::Runtime::UDP.bind(host, 0)
82
+ p [host, udp.features]
83
+ udp.close
84
+ end
85
+ ```
86
+
87
+ On Linux with `net.core.rmem_max` at 4 MiB, both sockets report:
88
+
89
+ ```
90
+ #<data Raiha::Runtime::UDP::Features receive_buffer=8388608, send_buffer=8388608, ipv4=true, ecn_in=true, destination=true, source=true, ecn_out=true>
91
+ ```
92
+
93
+ On OpenBSD 8.0 the following were seen:
94
+
95
+ - The buffers were 2097152.
96
+ - An IPv4 socket had `ecn_in=false` and `ecn_out=false`, with `destination` and `source` true.
97
+ - An IPv6 socket had everything true except `ipv4`.
98
+ - The servers' `Config#ecn_address_families` became `[:ipv6]`.
data/docs/qlog.md ADDED
@@ -0,0 +1,86 @@
1
+ # qlog
2
+
3
+ Raiha writes [qlog](https://datatracker.ietf.org/doc/draft-ietf-quic-qlog-main-schema/) traces of its QUIC connections, and of HTTP/3 over them, so that a connection can be looked at after the fact with qvis or another qlog tool. The runtime writes one file per connection into a directory you name; the sans-I/O layers take a tracer object of your own instead.
4
+
5
+ ## Turning it on in the runtime
6
+
7
+ `Raiha::Runtime::Server`, `Raiha::Runtime::HTTP3::Server` and `Raiha::Runtime::Client.connect` take `qlog_dir:`. Without it (the default, `nil`) nothing is traced. The directory is created if it is not there.
8
+
9
+ ```ruby
10
+ server = Raiha::Runtime::HTTP3::Server.new(config:, tls_config:, keyring:, host: "::", port: 443, qlog_dir: "log/qlog")
11
+
12
+ Raiha::Runtime::Client.connect("example.com", 443, config:, tls_config:, keyring:, qlog_dir: "log/qlog") do |session|
13
+ # ...
14
+ end
15
+ ```
16
+
17
+ The servers of `Raiha::Runtime::Workers` are made in the block each worker runs, so pass `qlog_dir:` there; every worker may share one directory. See [fiber-scheduler.md](fiber-scheduler.md) for the rest of their arguments, and `examples/qlog_output.rb` and `examples/qlog_roundtrips.rb` for complete programs.
18
+
19
+ ### File names
20
+
21
+ - A server's trace is `<connection ID>_server.sqlog`, named by the Destination Connection ID the client first chose, in hex.
22
+ - A client's trace is `<time>_client.sqlog`, named by the UTC time the connection was tried, as `%Y%m%dT%H%M%S.%6N` (for example `20261116T112340.256879_client.sqlog`). Each address `Client.connect` tries has a trace of its own.
23
+
24
+ Files are created readable by their owner only (mode 0600), as they hold what the peers said. A file is opened without following symbolic links and used only when it is a regular file, and it is held under an exclusive `flock` while its trace writes; a trace whose file cannot be had so is dropped.
25
+
26
+ ### Limits
27
+
28
+ A busy server must not fill its disk with qlog, so the runtime bounds what it writes:
29
+
30
+ | Limit | Size | What happens |
31
+ | --- | --- | --- |
32
+ | One trace | 64 MiB (`Raiha::QUIC::Qlog::Tracer::DEFAULT_MAX_BYTES`) | The trace ends with a `raiha:qlog_stopped` event naming the limit. |
33
+ | All traces of one Ractor | 1 GiB (`Raiha::Runtime::Qlog::BUDGET`) | Spent bytes never come back: once the budget is gone, every trace of that Ractor stops, and traces begun later write nothing until the process restarts. Each worker of `Workers` has its own budget. |
34
+ | The directory | 1 GiB and 4,096 files (`Raiha::Runtime::Qlog::MAX_FILES`) | Before a new trace begins, the oldest `.sqlog` files no trace is writing are deleted, files from earlier runs included. |
35
+
36
+ The directory is listed once, when the server or client is made; after that it counts only the files it has closed itself. A file another process holds the lock of is neither deleted nor counted.
37
+
38
+ Traces are written to memory first, since a call into the protocol core never waits on I/O, and go to their files after each turn of the runtime's driver.
39
+
40
+ ## What is in a trace
41
+
42
+ Each file is a JSON Text Sequence (RFC 7464): records separated by the byte `0x1e`, each followed by a line feed. The first record is the header:
43
+
44
+ - `file_schema` is `urn:ietf:params:qlog:file:sequential` and `serialization_format` is `application/qlog+json-seq`. The header also carries `qlog_version` `"0.3"` and `qlog_format` `"JSON-SEQ"` of the earlier drafts, which qvis looks for.
45
+ - `event_schemas` lists `urn:ietf:params:qlog:events:quic` (the events follow draft-ietf-quic-qlog-main-schema-13 and quic-events-12), and also `urn:ietf:params:qlog:events:http3-12` (draft-ietf-quic-qlog-h3-events-12) when the ALPN of the TLS config includes `h3`.
46
+ - Times are milliseconds of the monotonic clock the connection is given; the reference time's epoch is `unknown`.
47
+
48
+ QUIC events:
49
+
50
+ - `quic:packet_sent`, `quic:packet_received` (with the frames of each packet), `quic:packet_dropped`, `quic:packet_lost`
51
+ - `quic:parameters_set`, `quic:recovery_parameters_set`, `quic:recovery_metrics_updated`, `quic:congestion_state_updated`, `quic:timer_updated`
52
+ - `quic:version_information`: the versions the client offers, those the server takes or lists in a Version Negotiation packet, and the version chosen. `version_information` in `quic:parameters_set` and the versions are hex, as in a packet header.
53
+ - `quic:ecn_state_updated`, `quic:mtu_updated`, `quic:connection_id_updated`, `quic:migration_state_updated`, `quic:key_updated`
54
+ - `quic:connection_data_blocked_updated`, `quic:stream_data_blocked_updated`
55
+
56
+ HTTP/3 events: `http3:parameters_set`, `http3:stream_type_set`, `http3:frame_created`, `http3:frame_parsed`, `http3:priority_updated`.
57
+
58
+ Events qlog has no name for are under `raiha:`: `raiha:spurious_loss`, the traces of the TLS handshake as `raiha:tls_<name>` (for example `raiha:tls_transcript_updated`), and `raiha:qlog_stopped`.
59
+
60
+ What a trace leaves out:
61
+
62
+ - Stream and datagram payloads appear only as their length; connection IDs and other bytes are hex. The data of PATH_CHALLENGE and PATH_RESPONSE is written in full.
63
+ - No key is written, a key update included.
64
+ - In HTTP/3 field sections, a name or value is cut to 256 bytes and a section to 64 lines, the rest counted. The values of `authorization`, `proxy-authorization`, `cookie` and `set-cookie` are written as their length only. A name or value that is not printable text is written in hex.
65
+
66
+ ## Opening the files
67
+
68
+ [qvis](https://qvis.quictools.info/) opens `.sqlog` files directly: load them through its file picker. Since each record is one line of JSON after the `0x1e` separator, the files can also be read with ordinary tools, for example:
69
+
70
+ ```sh
71
+ tr -d '\036' < log/qlog/c93f63f45e397579_server.sqlog | jq -c 'select(.name == "quic:packet_lost")'
72
+ ```
73
+
74
+ ## Tracers in the sans-I/O layers
75
+
76
+ Below the runtime, the protocol layers do no I/O and write nothing themselves; they call a tracer you pass.
77
+
78
+ - `Raiha::QUIC::Tracer` is the interface of a QUIC connection's tracer: one method per qlog event (`packet_sent`, `packet_lost`, `metrics_updated`, and so on). Every method is optional; include the module and override the ones you want. `Raiha::QUIC::Connection.client` / `.server` and `Raiha::QUIC::Endpoint#connect` / `#accept` take it as `tracer:`. Without a tracer, not even the arguments of an event are built.
79
+ - `Raiha::QUIC::Qlog::Tracer.new(io, vantage_point:, title: nil, max_bytes: 64 MiB, budget: nil, event_schemas: [])` is the tracer the runtime uses: it writes JSON-SEQ to any `io` that answers `write`. `vantage_point` is `:client` or `:server`; `budget` is a `Raiha::QUIC::Qlog::Budget.new(max_bytes:)` shared by several traces.
80
+ - `Raiha::HTTP3::Tracer` is the interface of an HTTP/3 connection's tracer, taken by `Raiha::HTTP3::Connection.client` / `.server` as `tracer:`. `Raiha::HTTP3::Qlog::Tracer.new(quic_tracer)` writes the HTTP/3 events into the same file as the QUIC qlog tracer of that connection, through its `record(name, data)`; name the HTTP/3 schema in that tracer's `event_schemas` (`Raiha::HTTP3::Qlog::EVENT_SCHEMA`).
81
+
82
+ ## TLS secrets
83
+
84
+ To decrypt a packet capture in Wireshark, the TLS traffic secrets can be written in the SSLKEYLOGFILE format (RFC 9850). Nothing writes them unless code asks: the environment variable `SSLKEYLOGFILE` is not read by the library, and the runtime's `Server` and `Client` take no keylog. At the sans-I/O level, `Raiha::QUIC::Endpoint#connect` / `#accept` and `Raiha::QUIC::Connection.client` / `.server` take `keylog:`, anything that answers `write(line)`, such as `Raiha::TLS::Keylog::File.new(path)` (opened for appending, mode 0600). Only the traffic secrets are written, not the exporter secret. RFC 9850 forbids this in production.
85
+
86
+ The quic-interop-runner endpoint under `interop/` writes a keylog when `SSLKEYLOGFILE` is set, and qlog into `QLOGDIR`; see [interop/README.md](../interop/README.md).