forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefs.h
More file actions
411 lines (355 loc) · 15.4 KB
/
Copy pathdefs.h
File metadata and controls
411 lines (355 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#pragma once
#include <aliased_struct.h>
#include <env.h>
#include <nghttp3/nghttp3.h>
#include <ngtcp2/ngtcp2.h>
#include <node_errors.h>
#include <uv.h>
#include <v8.h>
#include <limits>
namespace node::quic {
#define NGTCP2_SUCCESS 0
#define NGTCP2_ERR(V) ((V) != NGTCP2_SUCCESS)
#define NGTCP2_OK(V) ((V) == NGTCP2_SUCCESS)
#define IF_QUIC_DEBUG(env) \
if (env->enabled_debug_list()->enabled(DebugCategory::QUIC)) [[unlikely]]
#define DISALLOW_COPY(Name) \
Name(const Name&) = delete; \
Name& operator=(const Name&) = delete;
#define DISALLOW_MOVE(Name) \
Name(Name&&) = delete; \
Name& operator=(Name&&) = delete;
#define DISALLOW_COPY_AND_MOVE(Name) \
DISALLOW_COPY(Name) \
DISALLOW_MOVE(Name)
template <typename Opt, std::string Opt::*member>
bool SetOption(Environment* env,
Opt* options,
const v8::Local<v8::Object>& object,
const v8::Local<v8::String>& name) {
v8::Local<v8::Value> value;
if (!object->Get(env->context(), name).ToLocal(&value)) return false;
if (!value->IsUndefined()) {
Utf8Value utf8(env->isolate(), value);
options->*member = utf8.ToString();
}
return true;
}
template <typename Opt, bool Opt::*member>
bool SetOption(Environment* env,
Opt* options,
const v8::Local<v8::Object>& object,
const v8::Local<v8::String>& name) {
v8::Local<v8::Value> value;
if (!object->Get(env->context(), name).ToLocal(&value)) return false;
if (!value->IsUndefined()) {
options->*member = value->BooleanValue(env->isolate());
}
return true;
}
template <typename Opt, uint32_t Opt::*member>
bool SetOption(Environment* env,
Opt* options,
const v8::Local<v8::Object>& object,
const v8::Local<v8::String>& name) {
v8::Local<v8::Value> value;
if (!object->Get(env->context(), name).ToLocal(&value)) return false;
if (!value->IsUndefined()) {
if (!value->IsUint32()) {
Utf8Value nameStr(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(
env, "The %s option must be an uint32", nameStr);
return false;
}
v8::Local<v8::Uint32> num;
if (!value->ToUint32(env->context()).ToLocal(&num)) {
Utf8Value nameStr(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(
env, "The %s option must be an uint32", nameStr);
return false;
}
options->*member = num->Value();
}
return true;
}
template <typename Opt, uint16_t Opt::*member>
bool SetOption(Environment* env,
Opt* options,
const v8::Local<v8::Object>& object,
const v8::Local<v8::String>& name) {
v8::Local<v8::Value> value;
if (!object->Get(env->context(), name).ToLocal(&value)) return false;
if (!value->IsUndefined()) {
if (!value->IsUint32()) {
Utf8Value nameStr(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(
env, "The %s option must be an uint16", nameStr);
return false;
}
v8::Local<v8::Uint32> num;
if (!value->ToUint32(env->context()).ToLocal(&num)) {
Utf8Value nameStr(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(
env, "The %s option must be an uint16", nameStr);
return false;
}
uint32_t val = num->Value();
if (val > 0xFFFF) {
Utf8Value nameStr(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(
env, "The %s option must fit in a uint16", nameStr);
return false;
}
options->*member = static_cast<uint16_t>(val);
}
return true;
}
template <typename Opt, uint64_t Opt::*member>
bool SetOption(Environment* env,
Opt* options,
const v8::Local<v8::Object>& object,
const v8::Local<v8::String>& name) {
v8::Local<v8::Value> value;
if (!object->Get(env->context(), name).ToLocal(&value)) return false;
if (!value->IsUndefined()) {
if (!value->IsBigInt() && !value->IsNumber()) {
Utf8Value nameStr(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(
env, "option %s must be a bigint or number", nameStr);
return false;
}
DCHECK_IMPLIES(!value->IsBigInt(), value->IsNumber());
uint64_t val = 0;
if (value->IsBigInt()) {
bool lossless = true;
val = value.As<v8::BigInt>()->Uint64Value(&lossless);
if (!lossless) {
Utf8Value label(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(env, "option %s is out of range", label);
return false;
}
} else {
double dbl = value.As<v8::Number>()->Value();
if (dbl < 0) {
Utf8Value label(env->isolate(), name);
THROW_ERR_INVALID_ARG_VALUE(env, "option %s is out of range", label);
return false;
}
val = static_cast<uint64_t>(dbl);
}
options->*member = val;
}
return true;
}
// Utilities used to update the stats for Endpoint, Session, and Stream
// objects. The stats themselves are maintained in an AliasedStruct within
// each of the relevant classes.
template <typename Stats, uint64_t Stats::*member>
void IncrementStat(Stats* stats, uint64_t amt = 1) {
stats->*member += amt;
}
template <typename Stats, uint64_t Stats::*member>
void RecordTimestampStat(Stats* stats) {
stats->*member = uv_hrtime();
}
template <typename Stats, uint64_t Stats::*member>
void SetStat(Stats* stats, uint64_t val) {
stats->*member = val;
}
template <typename Stats, uint64_t Stats::*member>
uint64_t GetStat(Stats* stats) {
return stats->*member;
}
#define STAT_INCREMENT(Type, name) \
IncrementStat<Type, &Type::name>(stats_.Data());
#define STAT_INCREMENT_N(Type, name, amt) \
IncrementStat<Type, &Type::name>(stats_.Data(), amt);
#define STAT_RECORD_TIMESTAMP(Type, name) \
RecordTimestampStat<Type, &Type::name>(stats_.Data());
#define STAT_SET(Type, name, val) SetStat<Type, &Type::name>(stats_.Data(), val)
#define STAT_GET(Type, name) GetStat<Type, &Type::name>(stats_.Data())
#define STAT_FIELD(_, name) uint64_t name;
#define STAT_STRUCT(klass, name) \
struct klass::Stats final { \
name##_STATS(STAT_FIELD) \
};
#define JS_METHOD_IMPL(name) \
void name(const v8::FunctionCallbackInfo<v8::Value>& args)
#define JS_METHOD(name) static JS_METHOD_IMPL(name)
#define JS_CONSTRUCTOR(name) \
inline static bool HasInstance(Environment* env, \
v8::Local<v8::Value> value) { \
return GetConstructorTemplate(env)->HasInstance(value); \
} \
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( \
Environment* env)
#define JS_CONSTRUCTOR_IMPL(name, template, body) \
v8::Local<v8::FunctionTemplate> name::GetConstructorTemplate( \
Environment* env) { \
auto& state = BindingData::Get(env); \
auto tmpl = state.template(); \
if (tmpl.IsEmpty()) { \
body state.set_##template(tmpl); \
} \
return tmpl; \
}
#define JS_ILLEGAL_CONSTRUCTOR() \
tmpl = NewFunctionTemplate(env->isolate(), IllegalConstructor)
#define JS_NEW_CONSTRUCTOR() tmpl = NewFunctionTemplate(env->isolate(), New)
#define JS_INHERIT(name) tmpl->Inherit(name::GetConstructorTemplate(env));
#define JS_CLASS(name) \
tmpl->InstanceTemplate()->SetInternalFieldCount(kInternalFieldCount); \
tmpl->SetClassName(state.name##_string())
#define JS_CLASS_FIELDS(name, fields) \
tmpl->InstanceTemplate()->SetInternalFieldCount(fields); \
tmpl->SetClassName(state.name##_string())
#define JS_NEW_INSTANCE_OR_RETURN(env, name, ret) \
v8::Local<v8::Object> name; \
if (!GetConstructorTemplate(env) \
->InstanceTemplate() \
->NewInstance(env->context()) \
.ToLocal(&name)) { \
return ret; \
}
#define JS_NEW_INSTANCE(env, name) \
v8::Local<v8::Object> name; \
if (!GetConstructorTemplate(env) \
->InstanceTemplate() \
->NewInstance(env->context()) \
.ToLocal(&name)) { \
return; \
}
#define JS_BINDING_INIT_BOILERPLATE() \
static void InitPerIsolate(IsolateData* isolate_data, \
v8::Local<v8::ObjectTemplate> target); \
static void InitPerContext(Realm* realm, v8::Local<v8::Object> target); \
static void RegisterExternalReferences(ExternalReferenceRegistry* registry)
#define JS_TRY_ALLOCATE_BACKING(env, name, len) \
auto name = v8::ArrayBuffer::NewBackingStore( \
env->isolate(), \
len, \
v8::BackingStoreInitializationMode::kUninitialized, \
v8::BackingStoreOnFailureMode::kReturnNull); \
if (!name) { \
THROW_ERR_MEMORY_ALLOCATION_FAILED(env); \
return; \
}
#define JS_TRY_ALLOCATE_BACKING_OR_RETURN(env, name, len, ret) \
auto name = v8::ArrayBuffer::NewBackingStore( \
env->isolate(), \
len, \
v8::BackingStoreInitializationMode::kUninitialized, \
v8::BackingStoreOnFailureMode::kReturnNull); \
if (!name) { \
THROW_ERR_MEMORY_ALLOCATION_FAILED(env); \
return ret; \
}
#define JS_DEFINE_READONLY_PROPERTY(env, target, name, value) \
target \
->DefineOwnProperty( \
env->context(), name, value, v8::PropertyAttribute::ReadOnly) \
.Check();
enum class Side : uint8_t {
CLIENT,
SERVER,
};
enum class EndpointLabel : uint8_t {
LOCAL,
REMOTE,
};
enum class Direction : uint8_t {
BIDIRECTIONAL,
UNIDIRECTIONAL,
};
enum class HeadersKind : uint8_t {
HINTS,
INITIAL,
TRAILING,
};
enum class HeadersFlags : uint8_t {
NONE,
TERMINAL,
};
enum class StreamPriority : uint8_t {
DEFAULT = NGHTTP3_DEFAULT_URGENCY,
LOW = NGHTTP3_URGENCY_LOW,
HIGH = NGHTTP3_URGENCY_HIGH,
};
enum class StreamPriorityFlags : uint8_t {
NON_INCREMENTAL,
INCREMENTAL,
};
enum class HeadersSupportState : uint8_t {
UNKNOWN,
SUPPORTED,
UNSUPPORTED,
};
enum class PathValidationResult : uint8_t {
SUCCESS = NGTCP2_PATH_VALIDATION_RESULT_SUCCESS,
FAILURE = NGTCP2_PATH_VALIDATION_RESULT_FAILURE,
ABORTED = NGTCP2_PATH_VALIDATION_RESULT_ABORTED,
};
enum class DatagramStatus : uint8_t {
ACKNOWLEDGED,
LOST,
ABANDONED,
};
#define CC_ALGOS(V) \
V(RENO, reno) \
V(CUBIC, cubic) \
V(BBR, bbr)
#define V(name, _) static constexpr auto CC_ALGO_##name = NGTCP2_CC_ALGO_##name;
CC_ALGOS(V)
#undef V
using error_code = uint64_t;
using stream_id = int64_t;
using datagram_id = uint64_t;
constexpr size_t kDefaultMaxPacketLength = NGTCP2_MAX_UDP_PAYLOAD_SIZE;
constexpr uint64_t kMaxSizeT = std::numeric_limits<size_t>::max();
constexpr uint64_t kMaxSafeJsInteger = 9007199254740991;
constexpr auto kSocketAddressInfoTimeout = 60 * NGTCP2_SECONDS;
constexpr size_t kMaxVectorCount = 16;
constexpr stream_id kMaxStreamId = std::numeric_limits<stream_id>::max();
// A token bucket rate limiter using lazy refill. No timer needed — tokens
// are computed on demand from the elapsed time since the last check.
// Used to cap the total rate of stateless responses (retry, reset,
// version negotiation, immediate close) regardless of source address,
// preventing spoofed-source floods from bypassing per-host limits.
struct TokenBucket final {
double rate; // tokens per second (refill rate)
double burst; // maximum tokens (bucket capacity)
double tokens; // current token count
uint64_t last_ts; // last refill timestamp (nanoseconds, uv_hrtime)
TokenBucket() : rate(0), burst(0), tokens(0), last_ts(0) {}
TokenBucket(double rate, double burst);
// Reinitialize the bucket with new rate/burst parameters if it
// hasn't been initialized yet (last_ts == 0). Used for per-host
// buckets in the address LRU where the rate/burst aren't known
// at construction time.
void InitOnce(double r, double b, uint64_t now);
// Try to consume one token. Refills based on elapsed time, then
// attempts to consume. Returns true if the request is allowed.
// The caller provides the current timestamp to avoid redundant
// uv_hrtime() calls in hot paths.
bool consume(uint64_t now);
};
class DebugIndentScope final {
public:
inline DebugIndentScope() { ++indent_; }
DISALLOW_COPY_AND_MOVE(DebugIndentScope)
inline ~DebugIndentScope() { --indent_; }
inline std::string Prefix() const {
std::string res("\n");
res.append(indent_, '\t');
return res;
}
inline std::string Close() const {
std::string res("\n");
res.append(indent_ - 1, '\t');
res += "}";
return res;
}
private:
static thread_local int indent_;
};
} // namespace node::quic