@encharm/cws 4.5.1 → 4.5.2

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 (237) hide show
  1. package/binding.gyp +1 -1
  2. package/dist/bindings/cws_darwin_arm64_node137.node +0 -0
  3. package/dist/bindings/cws_linux_x64_node137.node +0 -0
  4. package/package.json +1 -1
  5. package/src/Addon.h +6 -1
  6. package/src/headers/24/acorn_version.h +6 -0
  7. package/src/headers/24/addon_permission.h +31 -0
  8. package/src/headers/24/agent.h +195 -0
  9. package/src/headers/24/aliased_buffer-inl.h +244 -0
  10. package/src/headers/24/aliased_buffer.h +206 -0
  11. package/src/headers/24/aliased_struct-inl.h +54 -0
  12. package/src/headers/24/aliased_struct.h +63 -0
  13. package/src/headers/24/amaro_version.h +6 -0
  14. package/src/headers/24/application.h +165 -0
  15. package/src/headers/24/async_context_frame.h +33 -0
  16. package/src/headers/24/async_wrap-inl.h +96 -0
  17. package/src/headers/24/async_wrap.h +244 -0
  18. package/src/headers/24/base_object-inl.h +330 -0
  19. package/src/headers/24/base_object.h +338 -0
  20. package/src/headers/24/base_object_types.h +74 -0
  21. package/src/headers/24/bindingdata.h +272 -0
  22. package/src/headers/24/blob_serializer_deserializer-inl.h +385 -0
  23. package/src/headers/24/blob_serializer_deserializer.h +132 -0
  24. package/src/headers/24/callback_queue-inl.h +97 -0
  25. package/src/headers/24/callback_queue.h +79 -0
  26. package/src/headers/24/cares_wrap.h +447 -0
  27. package/src/headers/24/child_process_permission.h +31 -0
  28. package/src/headers/24/cid.h +127 -0
  29. package/src/headers/24/cjs_module_lexer_version.h +6 -0
  30. package/src/headers/24/cleanup_queue-inl.h +36 -0
  31. package/src/headers/24/cleanup_queue.h +79 -0
  32. package/src/headers/24/compile_cache.h +113 -0
  33. package/src/headers/24/connect_wrap.h +26 -0
  34. package/src/headers/24/connection_wrap.h +30 -0
  35. package/src/headers/24/cppgc_helpers-inl.h +65 -0
  36. package/src/headers/24/cppgc_helpers.h +162 -0
  37. package/src/headers/24/crypto/crypto_aes.h +97 -0
  38. package/src/headers/24/crypto/crypto_argon2.h +86 -0
  39. package/src/headers/24/crypto/crypto_bio.h +193 -0
  40. package/src/headers/24/crypto/crypto_chacha20_poly1305.h +64 -0
  41. package/src/headers/24/crypto/crypto_cipher.h +281 -0
  42. package/src/headers/24/crypto/crypto_clienthello-inl.h +90 -0
  43. package/src/headers/24/crypto/crypto_clienthello.h +131 -0
  44. package/src/headers/24/crypto/crypto_common.h +48 -0
  45. package/src/headers/24/crypto/crypto_context.h +171 -0
  46. package/src/headers/24/crypto/crypto_dh.h +126 -0
  47. package/src/headers/24/crypto/crypto_dsa.h +50 -0
  48. package/src/headers/24/crypto/crypto_ec.h +163 -0
  49. package/src/headers/24/crypto/crypto_hash.h +89 -0
  50. package/src/headers/24/crypto/crypto_hkdf.h +61 -0
  51. package/src/headers/24/crypto/crypto_hmac.h +92 -0
  52. package/src/headers/24/crypto/crypto_kem.h +113 -0
  53. package/src/headers/24/crypto/crypto_keygen.h +290 -0
  54. package/src/headers/24/crypto/crypto_keys.h +392 -0
  55. package/src/headers/24/crypto/crypto_ml_dsa.h +21 -0
  56. package/src/headers/24/crypto/crypto_pbkdf2.h +74 -0
  57. package/src/headers/24/crypto/crypto_random.h +124 -0
  58. package/src/headers/24/crypto/crypto_rsa.h +136 -0
  59. package/src/headers/24/crypto/crypto_scrypt.h +85 -0
  60. package/src/headers/24/crypto/crypto_sig.h +156 -0
  61. package/src/headers/24/crypto/crypto_spkac.h +21 -0
  62. package/src/headers/24/crypto/crypto_timing.h +20 -0
  63. package/src/headers/24/crypto/crypto_tls.h +309 -0
  64. package/src/headers/24/crypto/crypto_util.h +598 -0
  65. package/src/headers/24/crypto/crypto_x509.h +140 -0
  66. package/src/headers/24/data.h +148 -0
  67. package/src/headers/24/debug_utils-inl.h +232 -0
  68. package/src/headers/24/debug_utils.h +193 -0
  69. package/src/headers/24/defs.h +254 -0
  70. package/src/headers/24/diagnosticfilename-inl.h +33 -0
  71. package/src/headers/24/embedded_data.h +17 -0
  72. package/src/headers/24/encoding_binding.h +60 -0
  73. package/src/headers/24/endpoint.h +421 -0
  74. package/src/headers/24/env-inl.h +935 -0
  75. package/src/headers/24/env.h +1257 -0
  76. package/src/headers/24/env_properties.h +561 -0
  77. package/src/headers/24/fs_permission.h +167 -0
  78. package/src/headers/24/handle_wrap.h +123 -0
  79. package/src/headers/24/histogram-inl.h +107 -0
  80. package/src/headers/24/histogram.h +279 -0
  81. package/src/headers/24/http3.h +44 -0
  82. package/src/headers/24/inspector_agent.h +165 -0
  83. package/src/headers/24/inspector_io.h +78 -0
  84. package/src/headers/24/inspector_permission.h +31 -0
  85. package/src/headers/24/inspector_profiler.h +149 -0
  86. package/src/headers/24/inspector_socket.h +60 -0
  87. package/src/headers/24/inspector_socket_server.h +110 -0
  88. package/src/headers/24/io_agent.h +30 -0
  89. package/src/headers/24/js_native_api.h +615 -0
  90. package/src/headers/24/js_native_api_types.h +211 -0
  91. package/src/headers/24/js_native_api_v8.h +485 -0
  92. package/src/headers/24/js_native_api_v8_internals.h +45 -0
  93. package/src/headers/24/js_stream.h +52 -0
  94. package/src/headers/24/json_parser.h +40 -0
  95. package/src/headers/24/json_utils.h +172 -0
  96. package/src/headers/24/logstream.h +81 -0
  97. package/src/headers/24/main_thread_interface.h +116 -0
  98. package/src/headers/24/memory_tracker-inl.h +454 -0
  99. package/src/headers/24/memory_tracker.h +333 -0
  100. package/src/headers/24/module_wrap.h +229 -0
  101. package/src/headers/24/ncrypto.h +1625 -0
  102. package/src/headers/24/network_agent.h +118 -0
  103. package/src/headers/24/network_inspector.h +45 -0
  104. package/src/headers/24/network_resource_manager.h +29 -0
  105. package/src/headers/24/node.h +1620 -0
  106. package/src/headers/24/node_api.h +269 -0
  107. package/src/headers/24/node_api_internals.h +46 -0
  108. package/src/headers/24/node_api_types.h +52 -0
  109. package/src/headers/24/node_binding.h +163 -0
  110. package/src/headers/24/node_blob.h +159 -0
  111. package/src/headers/24/node_bob-inl.h +36 -0
  112. package/src/headers/24/node_bob.h +107 -0
  113. package/src/headers/24/node_buffer.h +92 -0
  114. package/src/headers/24/node_builtins.h +223 -0
  115. package/src/headers/24/node_config_file.h +65 -0
  116. package/src/headers/24/node_constants.h +82 -0
  117. package/src/headers/24/node_context_data.h +168 -0
  118. package/src/headers/24/node_contextify.h +261 -0
  119. package/src/headers/24/node_crypto.h +62 -0
  120. package/src/headers/24/node_debug.h +25 -0
  121. package/src/headers/24/node_dir.h +52 -0
  122. package/src/headers/24/node_dotenv.h +45 -0
  123. package/src/headers/24/node_errors.h +391 -0
  124. package/src/headers/24/node_exit_code.h +54 -0
  125. package/src/headers/24/node_external_reference.h +167 -0
  126. package/src/headers/24/node_file-inl.h +412 -0
  127. package/src/headers/24/node_file.h +540 -0
  128. package/src/headers/24/node_http2.h +1175 -0
  129. package/src/headers/24/node_http2_state.h +159 -0
  130. package/src/headers/24/node_http_common-inl.h +198 -0
  131. package/src/headers/24/node_http_common.h +535 -0
  132. package/src/headers/24/node_i18n.h +132 -0
  133. package/src/headers/24/node_internals.h +476 -0
  134. package/src/headers/24/node_json.h +24 -0
  135. package/src/headers/24/node_large_page.h +34 -0
  136. package/src/headers/24/node_locks.h +180 -0
  137. package/src/headers/24/node_main_instance.h +67 -0
  138. package/src/headers/24/node_mem-inl.h +112 -0
  139. package/src/headers/24/node_mem.h +45 -0
  140. package/src/headers/24/node_messaging.h +389 -0
  141. package/src/headers/24/node_metadata.h +164 -0
  142. package/src/headers/24/node_modules.h +93 -0
  143. package/src/headers/24/node_mutex.h +323 -0
  144. package/src/headers/24/node_object_wrap.h +132 -0
  145. package/src/headers/24/node_options-inl.h +524 -0
  146. package/src/headers/24/node_options.h +682 -0
  147. package/src/headers/24/node_perf.h +166 -0
  148. package/src/headers/24/node_perf_common.h +102 -0
  149. package/src/headers/24/node_platform.h +286 -0
  150. package/src/headers/24/node_process-inl.h +26 -0
  151. package/src/headers/24/node_process.h +111 -0
  152. package/src/headers/24/node_realm-inl.h +153 -0
  153. package/src/headers/24/node_realm.h +236 -0
  154. package/src/headers/24/node_report.h +43 -0
  155. package/src/headers/24/node_revert.h +82 -0
  156. package/src/headers/24/node_root_certs.h +3546 -0
  157. package/src/headers/24/node_sea.h +74 -0
  158. package/src/headers/24/node_shadow_realm.h +46 -0
  159. package/src/headers/24/node_snapshot_builder.h +57 -0
  160. package/src/headers/24/node_snapshotable.h +174 -0
  161. package/src/headers/24/node_sockaddr-inl.h +257 -0
  162. package/src/headers/24/node_sockaddr.h +394 -0
  163. package/src/headers/24/node_sqlite.h +270 -0
  164. package/src/headers/24/node_stat_watcher.h +73 -0
  165. package/src/headers/24/node_string.h +101 -0
  166. package/src/headers/24/node_task_runner.h +92 -0
  167. package/src/headers/24/node_threadsafe_cow-inl.h +54 -0
  168. package/src/headers/24/node_threadsafe_cow.h +105 -0
  169. package/src/headers/24/node_trace_buffer.h +83 -0
  170. package/src/headers/24/node_trace_writer.h +75 -0
  171. package/src/headers/24/node_union_bytes.h +81 -0
  172. package/src/headers/24/node_url.h +101 -0
  173. package/src/headers/24/node_url_pattern.h +118 -0
  174. package/src/headers/24/node_v8.h +77 -0
  175. package/src/headers/24/node_v8_platform-inl.h +185 -0
  176. package/src/headers/24/node_version.h +110 -0
  177. package/src/headers/24/node_wasi.h +185 -0
  178. package/src/headers/24/node_wasm_web_api.h +55 -0
  179. package/src/headers/24/node_watchdog.h +154 -0
  180. package/src/headers/24/node_webstorage.h +60 -0
  181. package/src/headers/24/node_worker.h +159 -0
  182. package/src/headers/24/packet.h +155 -0
  183. package/src/headers/24/path.h +33 -0
  184. package/src/headers/24/permission/addon_permission.h +31 -0
  185. package/src/headers/24/permission/child_process_permission.h +31 -0
  186. package/src/headers/24/permission/fs_permission.h +167 -0
  187. package/src/headers/24/permission/inspector_permission.h +31 -0
  188. package/src/headers/24/permission/permission.h +100 -0
  189. package/src/headers/24/permission/permission_base.h +65 -0
  190. package/src/headers/24/permission/wasi_permission.h +31 -0
  191. package/src/headers/24/permission/worker_permission.h +31 -0
  192. package/src/headers/24/permission.h +100 -0
  193. package/src/headers/24/permission_base.h +65 -0
  194. package/src/headers/24/pipe_wrap.h +80 -0
  195. package/src/headers/24/preferredaddress.h +72 -0
  196. package/src/headers/24/protocol_helper.h +27 -0
  197. package/src/headers/24/queue.h +306 -0
  198. package/src/headers/24/req_wrap-inl.h +176 -0
  199. package/src/headers/24/req_wrap.h +79 -0
  200. package/src/headers/24/runtime_agent.h +41 -0
  201. package/src/headers/24/session.h +528 -0
  202. package/src/headers/24/sessionticket.h +108 -0
  203. package/src/headers/24/spawn_sync.h +243 -0
  204. package/src/headers/24/stream_base-inl.h +173 -0
  205. package/src/headers/24/stream_base.h +478 -0
  206. package/src/headers/24/stream_pipe.h +76 -0
  207. package/src/headers/24/stream_wrap.h +132 -0
  208. package/src/headers/24/streams.h +370 -0
  209. package/src/headers/24/string_bytes.h +109 -0
  210. package/src/headers/24/string_decoder-inl.h +31 -0
  211. package/src/headers/24/string_decoder.h +49 -0
  212. package/src/headers/24/target_agent.h +75 -0
  213. package/src/headers/24/tcp_wrap.h +105 -0
  214. package/src/headers/24/threadpoolwork-inl.h +70 -0
  215. package/src/headers/24/timer_wrap-inl.h +32 -0
  216. package/src/headers/24/timer_wrap.h +87 -0
  217. package/src/headers/24/timers.h +75 -0
  218. package/src/headers/24/tlscontext.h +228 -0
  219. package/src/headers/24/tokens.h +256 -0
  220. package/src/headers/24/trace_event.h +722 -0
  221. package/src/headers/24/trace_event_common.h +1109 -0
  222. package/src/headers/24/traced_value.h +129 -0
  223. package/src/headers/24/tracing_agent.h +43 -0
  224. package/src/headers/24/transportparams.h +166 -0
  225. package/src/headers/24/tty_wrap.h +65 -0
  226. package/src/headers/24/udp_wrap.h +230 -0
  227. package/src/headers/24/undici_version.h +6 -0
  228. package/src/headers/24/util-inl.h +711 -0
  229. package/src/headers/24/util.h +1016 -0
  230. package/src/headers/24/v8-external-memory-accounter.h +60 -0
  231. package/src/headers/24/v8-fast-api-calls.h +819 -0
  232. package/src/headers/24/v8-inspector.h +426 -0
  233. package/src/headers/24/wasi_permission.h +31 -0
  234. package/src/headers/24/worker_agent.h +40 -0
  235. package/src/headers/24/worker_inspector.h +124 -0
  236. package/src/headers/24/worker_permission.h +31 -0
  237. package/src/headers/24/zlib_version.h +6 -0
@@ -0,0 +1,421 @@
1
+ #pragma once
2
+
3
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
4
+ #if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
5
+
6
+ #include <aliased_struct.h>
7
+ #include <async_wrap.h>
8
+ #include <env.h>
9
+ #include <node_sockaddr.h>
10
+ #include <uv.h>
11
+ #include <v8.h>
12
+ #include <algorithm>
13
+ #include <optional>
14
+ #include "bindingdata.h"
15
+ #include "packet.h"
16
+ #include "session.h"
17
+ #include "sessionticket.h"
18
+ #include "tokens.h"
19
+
20
+ namespace node::quic {
21
+
22
+ // An Endpoint encapsulates the UDP local port binding and is responsible for
23
+ // sending and receiving QUIC packets. A single endpoint can act as both a QUIC
24
+ // client and server simultaneously.
25
+ class Endpoint final : public AsyncWrap, public Packet::Listener {
26
+ public:
27
+ static constexpr uint64_t DEFAULT_MAX_CONNECTIONS =
28
+ std::min<uint64_t>(kMaxSizeT, static_cast<uint64_t>(kMaxSafeJsInteger));
29
+ static constexpr uint64_t DEFAULT_MAX_CONNECTIONS_PER_HOST = 100;
30
+ static constexpr uint64_t DEFAULT_MAX_SOCKETADDRESS_LRU_SIZE =
31
+ (DEFAULT_MAX_CONNECTIONS_PER_HOST * 10);
32
+ static constexpr uint64_t DEFAULT_MAX_STATELESS_RESETS = 10;
33
+ static constexpr uint64_t DEFAULT_MAX_RETRY_LIMIT = 10;
34
+
35
+ // Endpoint configuration options
36
+ struct Options final : public MemoryRetainer {
37
+ // The local socket address to which the UDP port will be bound. The port
38
+ // may be 0 to have Node.js select an available port. IPv6 or IPv4 addresses
39
+ // may be used. When using IPv6, dual mode will be supported by default.
40
+ std::shared_ptr<SocketAddress> local_address;
41
+
42
+ // Retry tokens issued by the Endpoint are time-limited. By default, retry
43
+ // tokens expire after DEFAULT_RETRYTOKEN_EXPIRATION *seconds*. This is an
44
+ // arbitrary choice that is not mandated by the QUIC specification; so we
45
+ // can choose any value that makes sense here. Retry tokens are sent to the
46
+ // client, which echoes them back to the server in a subsequent set of
47
+ // packets, which means the expiration must be set high enough to allow a
48
+ // reasonable round-trip time for the session TLS handshake to complete.
49
+ uint64_t retry_token_expiration =
50
+ RetryToken::QUIC_DEFAULT_RETRYTOKEN_EXPIRATION / NGTCP2_SECONDS;
51
+
52
+ // Tokens issued using NEW_TOKEN are time-limited. By default, tokens expire
53
+ // after DEFAULT_TOKEN_EXPIRATION *seconds*.
54
+ uint64_t token_expiration =
55
+ RegularToken::QUIC_DEFAULT_REGULARTOKEN_EXPIRATION / NGTCP2_SECONDS;
56
+
57
+ // Each Endpoint places limits on the number of concurrent connections from
58
+ // a single host, and the total number of concurrent connections allowed as
59
+ // a whole. These are set to fairly modest, and arbitrary defaults. We can
60
+ // set these to whatever we'd like.
61
+ uint64_t max_connections_per_host = DEFAULT_MAX_CONNECTIONS_PER_HOST;
62
+ uint64_t max_connections_total = DEFAULT_MAX_CONNECTIONS;
63
+
64
+ // A stateless reset in QUIC is a discrete mechanism that one endpoint can
65
+ // use to communicate to a peer that it has lost whatever state it
66
+ // previously held about a session. Because generating a stateless reset
67
+ // consumes resources (even very modestly), they can be a DOS vector in
68
+ // which a malicious peer intentionally sends a large number of stateless
69
+ // reset eliciting packets. To protect against that risk, we limit the
70
+ // number of stateless resets that may be generated for a given remote host
71
+ // within a window of time. This is not mandated by QUIC, and the limit is
72
+ // arbitrary. We can set it to whatever we'd like.
73
+ uint64_t max_stateless_resets = DEFAULT_MAX_STATELESS_RESETS;
74
+
75
+ // For tracking the number of connections per host, the number of stateless
76
+ // resets that have been sent, and tracking the path verification status of
77
+ // a remote host, we maintain an LRU cache of the most recently seen hosts.
78
+ // The address_lru_size parameter determines the size of that cache. The
79
+ // default is set modestly at 10 times the default max connections per host.
80
+ uint64_t address_lru_size = DEFAULT_MAX_SOCKETADDRESS_LRU_SIZE;
81
+
82
+ // Similar to stateless resets, we enforce a limit on the number of retry
83
+ // packets that can be generated and sent for a remote host. Generating
84
+ // retry packets consumes a modest amount of resources and it's fairly
85
+ // trivial for a malicious peer to trigger generation of a large number of
86
+ // retries, so limiting them helps prevent a DOS vector.
87
+ uint64_t max_retries = DEFAULT_MAX_RETRY_LIMIT;
88
+
89
+ // The validate_address parameter instructs the Endpoint to perform explicit
90
+ // address validation using retry tokens. This is strongly recommended and
91
+ // should only be disabled in trusted, closed environments as a performance
92
+ // optimization.
93
+ bool validate_address = true;
94
+
95
+ // The stateless reset mechanism can be disabled. This should rarely ever be
96
+ // needed, and should only ever be done in trusted, closed environments as a
97
+ // performance optimization.
98
+ bool disable_stateless_reset = false;
99
+
100
+ #ifdef DEBUG
101
+ // The rx_loss and tx_loss parameters are debugging tools that allow the
102
+ // Endpoint to simulate random packet loss. The value for each parameter is
103
+ // a value between 0.0 and 1.0 indicating a probability of packet loss. Each
104
+ // time a packet is sent or received, the packet loss bit is calculated and
105
+ // if true, the packet is silently dropped. This should only ever be used
106
+ // for testing and debugging. There is never a reason why rx_loss and
107
+ // tx_loss should ever be used in a production system.
108
+ double rx_loss = 0.0;
109
+ double tx_loss = 0.0;
110
+ #endif // DEBUG
111
+
112
+ // By default, when the endpoint is created, it will generate a
113
+ // reset_token_secret at random. This is a secret used in generating
114
+ // stateless reset tokens. In order for stateless reset to be effective,
115
+ // however, it is necessary to use a deterministic secret that persists
116
+ // across ngtcp2 endpoints and sessions. This means that the endpoint
117
+ // configuration really should have a reset token secret passed in.
118
+ TokenSecret reset_token_secret;
119
+
120
+ // The secret used for generating new regular tokens.
121
+ TokenSecret token_secret;
122
+
123
+ // When the local_address specifies an IPv6 local address to bind to, the
124
+ // ipv6_only parameter determines whether dual stack mode (supporting both
125
+ // IPv6 and IPv4) transparently is supported. This sets the UV_UDP_IPV6ONLY
126
+ // flag on the underlying uv_udp_t.
127
+ bool ipv6_only = false;
128
+
129
+ uint32_t udp_receive_buffer_size = 0;
130
+ uint32_t udp_send_buffer_size = 0;
131
+
132
+ // The UDP TTL configuration is the number of network hops a packet will be
133
+ // forwarded through. The default is 64. The value is in the range 1 to 255.
134
+ // Setting to 0 uses the default.
135
+ uint8_t udp_ttl = 0;
136
+
137
+ void MemoryInfo(MemoryTracker* tracker) const override;
138
+ SET_MEMORY_INFO_NAME(Endpoint::Config)
139
+ SET_SELF_SIZE(Options)
140
+
141
+ static v8::Maybe<Options> From(Environment* env,
142
+ v8::Local<v8::Value> value);
143
+
144
+ std::string ToString() const;
145
+ };
146
+
147
+ bool HasInstance(Environment* env, v8::Local<v8::Value> value);
148
+ static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
149
+ Environment* env);
150
+ static void InitPerIsolate(IsolateData* data,
151
+ v8::Local<v8::ObjectTemplate> target);
152
+ static void InitPerContext(Realm* realm, v8::Local<v8::Object> target);
153
+ static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
154
+
155
+ Endpoint(Environment* env,
156
+ v8::Local<v8::Object> object,
157
+ const Endpoint::Options& options);
158
+
159
+ inline operator Packet::Listener*() {
160
+ return this;
161
+ }
162
+
163
+ inline const Options& options() const {
164
+ return options_;
165
+ }
166
+
167
+ // While the busy flag is set, the Endpoint will reject all initial packets
168
+ // with a SERVER_BUSY response. This allows us to build a circuit breaker
169
+ // directly into the implementation, explicitly signaling that the server is
170
+ // blocked when activity is too high.
171
+ void MarkAsBusy(bool on = true);
172
+
173
+ // Use the endpoint's token secret to generate a new token.
174
+ RegularToken GenerateNewToken(uint32_t version,
175
+ const SocketAddress& remote_address);
176
+
177
+ // Use the endpoint's reset token secret to generate a new stateless reset.
178
+ StatelessResetToken GenerateNewStatelessResetToken(uint8_t* token,
179
+ const CID& cid) const;
180
+
181
+ void AddSession(const CID& cid, BaseObjectPtr<Session> session);
182
+ void RemoveSession(const CID& cid, const SocketAddress& remote_address);
183
+ BaseObjectPtr<Session> FindSession(const CID& cid);
184
+
185
+ // A single session may be associated with multiple CIDs.
186
+ // AssociateCID registers the mapping both in the Endpoint and the inner
187
+ // Endpoint.
188
+ void AssociateCID(const CID& cid, const CID& scid);
189
+ void DisassociateCID(const CID& cid);
190
+
191
+ // Associates a given stateless reset token with the session. This allows
192
+ // stateless reset tokens to be recognized and dispatched to the proper
193
+ // Endpoint and Session for processing.
194
+ void AssociateStatelessResetToken(const StatelessResetToken& token,
195
+ Session* session);
196
+ void DisassociateStatelessResetToken(const StatelessResetToken& token);
197
+
198
+ void Send(const BaseObjectPtr<Packet>& packet);
199
+
200
+ // Generates and sends a retry packet. This is terminal for the connection.
201
+ // Retry packets are used to force explicit path validation by issuing a token
202
+ // to the peer that it must thereafter include in all subsequent initial
203
+ // packets. Upon receiving a retry packet, the peer must termination it's
204
+ // initial attempt to establish a connection and start a new attempt.
205
+ //
206
+ // Retry packets will only ever be generated by QUIC servers, and only if the
207
+ // QuicSocket is configured for explicit path validation. There is no way for
208
+ // a client to force a retry packet to be created. However, once a client
209
+ // determines that explicit path validation is enabled, it could attempt to
210
+ // DOS by sending a large number of malicious initial packets to intentionally
211
+ // ellicit retry packets (It can do so by intentionally sending initial
212
+ // packets that ignore the retry token). To help mitigate that risk, we limit
213
+ // the number of retries we send to a given remote endpoint.
214
+ void SendRetry(const PathDescriptor& options);
215
+
216
+ // Sends a version negotiation packet. This is terminal for the connection and
217
+ // is sent only when a QUIC packet is received for an unsupported QUIC
218
+ // version. It is possible that a malicious packet triggered this so we need
219
+ // to be careful not to commit too many resources.
220
+ void SendVersionNegotiation(const PathDescriptor& options);
221
+
222
+ // Possibly generates and sends a stateless reset packet. This is terminal for
223
+ // the connection. It is possible that a malicious packet triggered this so we
224
+ // need to be careful not to commit too many resources.
225
+ bool SendStatelessReset(const PathDescriptor& options, size_t source_len);
226
+
227
+ // Shutdown a connection prematurely, before a Session is created. This should
228
+ // only be called at the start of a session before the crypto keys have been
229
+ // established.
230
+ void SendImmediateConnectionClose(const PathDescriptor& options,
231
+ QuicError error);
232
+
233
+ // Listen for connections (act as a server).
234
+ void Listen(const Session::Options& options);
235
+
236
+ // Create a new client-side Session.
237
+ BaseObjectPtr<Session> Connect(
238
+ const SocketAddress& remote_address,
239
+ const Session::Options& options,
240
+ std::optional<SessionTicket> sessionTicket = std::nullopt);
241
+
242
+ // Returns the local address only if the endpoint has been bound. Before
243
+ // the endpoint is bound, or after it is closed, this will abort due to
244
+ // a failed check so it is important to check `is_closed()` before calling.
245
+ SocketAddress local_address() const;
246
+
247
+ void MemoryInfo(MemoryTracker* tracker) const override;
248
+ SET_MEMORY_INFO_NAME(Endpoint)
249
+ SET_SELF_SIZE(Endpoint)
250
+
251
+ struct Stats;
252
+ struct State;
253
+
254
+ private:
255
+ class UDP final : public MemoryRetainer {
256
+ public:
257
+ explicit UDP(Endpoint* endpoint);
258
+ ~UDP() override;
259
+
260
+ int Bind(const Endpoint::Options& config);
261
+ int Start();
262
+ void Stop();
263
+ void Close();
264
+ int Send(const BaseObjectPtr<Packet>& packet);
265
+
266
+ // Returns the local UDP socket address to which we are bound,
267
+ // or fail with an assert if we are not bound.
268
+ SocketAddress local_address() const;
269
+
270
+ bool is_bound() const;
271
+ bool is_closed() const;
272
+ bool is_closed_or_closing() const;
273
+ operator bool() const;
274
+
275
+ void Ref();
276
+ void Unref();
277
+
278
+ void MemoryInfo(node::MemoryTracker* tracker) const override;
279
+ SET_MEMORY_INFO_NAME(Endpoint::UDP)
280
+ SET_SELF_SIZE(UDP)
281
+
282
+ private:
283
+ class Impl;
284
+
285
+ BaseObjectWeakPtr<Impl> impl_;
286
+ bool is_bound_ = false;
287
+ bool is_started_ = false;
288
+ bool is_closed_ = false;
289
+ };
290
+
291
+ bool is_closed() const;
292
+ bool is_closing() const;
293
+ bool is_listening() const;
294
+
295
+ bool Start();
296
+
297
+ // Destroy the endpoint if...
298
+ // * There are no sessions,
299
+ // * There are no sent packets with pending done callbacks, and
300
+ // * We're not listening for new initial packets.
301
+ void MaybeDestroy();
302
+
303
+ // Specifies the general reason the endpoint is being destroyed.
304
+ enum class CloseContext {
305
+ CLOSE,
306
+ BIND_FAILURE,
307
+ START_FAILURE,
308
+ RECEIVE_FAILURE,
309
+ SEND_FAILURE,
310
+ LISTEN_FAILURE,
311
+ };
312
+
313
+ void Destroy(CloseContext context = CloseContext::CLOSE, int status = 0);
314
+
315
+ // A graceful close will destroy the endpoint once all existing sessions
316
+ // have ended normally. Creating new sessions (inbound or outbound) will
317
+ // be prevented.
318
+ void CloseGracefully();
319
+
320
+ void Release();
321
+
322
+ void PacketDone(int status) override;
323
+
324
+ void EmitNewSession(const BaseObjectPtr<Session>& session);
325
+ void EmitClose(CloseContext context, int status);
326
+
327
+ void IncrementSocketAddressCounter(const SocketAddress& address);
328
+ void DecrementSocketAddressCounter(const SocketAddress& address);
329
+
330
+ // JavaScript API
331
+
332
+ // Create a new Endpoint.
333
+ // @param Endpoint::Options options - Options to configure the Endpoint.
334
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
335
+
336
+ // Methods on the Endpoint instance:
337
+
338
+ // Create a new client Session on this endpoint.
339
+ // @param node::SocketAddress local_address - The local address to bind to.
340
+ // @param Session::Options options - Options to configure the Session.
341
+ // @param v8::ArrayBufferView session_ticket - The session ticket to use for
342
+ // the Session.
343
+ // @param v8::ArrayBufferView remote_transport_params - The remote transport
344
+ // params.
345
+ static void DoConnect(const v8::FunctionCallbackInfo<v8::Value>& args);
346
+
347
+ // Start listening as a QUIC server
348
+ // @param Session::Options options - Options to configure the Session.
349
+ static void DoListen(const v8::FunctionCallbackInfo<v8::Value>& args);
350
+
351
+ // Mark the Endpoint as busy, temporarily pausing handling of new initial
352
+ // packets.
353
+ // @param bool on - If true, mark the Endpoint as busy.
354
+ static void MarkBusy(const v8::FunctionCallbackInfo<v8::Value>& args);
355
+ static void FastMarkBusy(v8::Local<v8::Object> receiver, bool on);
356
+
357
+ // DoCloseGracefully is the signal that endpoint should close. Any packets
358
+ // that are already in the queue or in flight will be allowed to finish, but
359
+ // the EndpoingWrap will be otherwise no longer able to receive or send
360
+ // packets.
361
+ static void DoCloseGracefully(
362
+ const v8::FunctionCallbackInfo<v8::Value>& args);
363
+
364
+ // Get the local address of the Endpoint.
365
+ // @return node::SocketAddress - The local address of the Endpoint.
366
+ static void LocalAddress(const v8::FunctionCallbackInfo<v8::Value>& args);
367
+
368
+ // Ref() causes a listening Endpoint to keep the event loop active.
369
+ static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
370
+ static void FastRef(v8::Local<v8::Object> receiver, bool on);
371
+
372
+ void Receive(const uv_buf_t& buf, const SocketAddress& from);
373
+
374
+ AliasedStruct<Stats> stats_;
375
+ AliasedStruct<State> state_;
376
+ const Options options_;
377
+ UDP udp_;
378
+
379
+ struct ServerState {
380
+ Session::Options options;
381
+ std::shared_ptr<TLSContext> tls_context;
382
+ };
383
+ // Set if/when the endpoint is configured to listen.
384
+ std::optional<ServerState> server_state_ = std::nullopt;
385
+
386
+ // A Session is generally identified by one or more CIDs. We use two
387
+ // maps for this rather than one to avoid creating a whole bunch of
388
+ // BaseObjectPtr references. The primary map (sessions_) just maps
389
+ // the original CID to the Session, the second map (dcid_to_scid_)
390
+ // maps the additional CIDs to the primary.
391
+ CID::Map<BaseObjectPtr<Session>> sessions_;
392
+ CID::Map<CID> dcid_to_scid_;
393
+ StatelessResetToken::Map<Session*> token_map_;
394
+
395
+ struct SocketAddressInfoTraits final {
396
+ struct Type final {
397
+ size_t active_connections;
398
+ size_t reset_count;
399
+ size_t retry_count;
400
+ uint64_t timestamp;
401
+ bool validated;
402
+ };
403
+
404
+ static bool CheckExpired(const SocketAddress& address, const Type& type);
405
+ static void Touch(const SocketAddress& address, Type* type);
406
+ };
407
+
408
+ SocketAddressLRU<SocketAddressInfoTraits> addrLRU_;
409
+
410
+ CloseContext close_context_ = CloseContext::CLOSE;
411
+ int close_status_ = 0;
412
+
413
+ friend class UDP;
414
+ friend class Packet;
415
+ friend class Session;
416
+ };
417
+
418
+ } // namespace node::quic
419
+
420
+ #endif // HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC
421
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS