Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 5d9fbb2

Browse files
jasnelladdaleax
authored andcommitted
quic: refine QuicError
PR-URL: #207 Reviewed-By: #207
1 parent be206cf commit 5d9fbb2

File tree

6 files changed

+91
-75
lines changed

6 files changed

+91
-75
lines changed

src/node_quic_session-inl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ void QuicSession::SetLastError(QuicError error) {
3939
last_error_ = error;
4040
}
4141

42-
void QuicSession::SetLastError(QuicErrorFamily family, uint64_t code) {
42+
void QuicSession::SetLastError(int32_t family, uint64_t code) {
4343
SetLastError({ family, code });
4444
}
4545

46-
void QuicSession::SetLastError(QuicErrorFamily family, int code) {
47-
SetLastError(family, ngtcp2_err_infer_quic_transport_error_code(code));
46+
void QuicSession::SetLastError(int32_t family, int code) {
47+
SetLastError({ family, code });
4848
}
4949

5050
bool QuicSession::IsInClosingPeriod() {

src/node_quic_session.cc

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ bool QuicCryptoContext::SetSession(const unsigned char* data, size_t length) {
612612

613613
void QuicCryptoContext::SetTLSAlert(int err) {
614614
Debug(session_, "TLS Alert [%d]: %s", err, SSL_alert_type_string_long(err));
615-
session_->SetLastError(InitQuicError(QUIC_ERROR_CRYPTO, err));
615+
session_->SetLastError(QuicError(QUIC_ERROR_CRYPTO, err));
616616
}
617617

618618
bool QuicCryptoContext::SetupInitialKey(const ngtcp2_cid* dcid) {
@@ -1048,7 +1048,7 @@ void QuicSession::ImmediateClose() {
10481048
QuicError last_error = GetLastError();
10491049
Debug(this, "Immediate close with code %" PRIu64 " (%s)",
10501050
last_error.code,
1051-
ErrorFamilyName(last_error.family));
1051+
last_error.GetFamilyName());
10521052

10531053
HandleScope scope(env()->isolate());
10541054
Context::Scope context_scope(env()->context());
@@ -2040,7 +2040,7 @@ void QuicSession::SilentClose(bool stateless_reset) {
20402040
QuicError last_error = GetLastError();
20412041
Debug(this,
20422042
"Silent close with %s code %" PRIu64 " (stateless reset? %s)",
2043-
ErrorFamilyName(last_error.family),
2043+
last_error.GetFamilyName(),
20442044
last_error.code,
20452045
stateless_reset ? "yes" : "no");
20462046

@@ -3021,10 +3021,7 @@ void QuicSessionClose(const FunctionCallbackInfo<Value>& args) {
30213021
Environment* env = Environment::GetCurrent(args);
30223022
QuicSession* session;
30233023
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());
3024-
int family = QUIC_ERROR_SESSION;
3025-
uint64_t code = ExtractErrorCode(env, args[0]);
3026-
if (!args[1]->Int32Value(env->context()).To(&family)) return;
3027-
session->SetLastError(static_cast<QuicErrorFamily>(family), code);
3024+
session->SetLastError(QuicError(env, args[0], args[1]));
30283025
session->SendConnectionClose();
30293026
}
30303027

@@ -3044,11 +3041,7 @@ void QuicSessionDestroy(const FunctionCallbackInfo<Value>& args) {
30443041
Environment* env = Environment::GetCurrent(args);
30453042
QuicSession* session;
30463043
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());
3047-
int code = 0;
3048-
int family = QUIC_ERROR_SESSION;
3049-
if (!args[0]->Int32Value(env->context()).To(&code)) return;
3050-
if (!args[1]->Int32Value(env->context()).To(&family)) return;
3051-
session->SetLastError(static_cast<QuicErrorFamily>(family), code);
3044+
session->SetLastError(QuicError(env, args[0], args[1]));
30523045
session->Destroy();
30533046
}
30543047

src/node_quic_session.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,11 @@ class QuicSession : public AsyncWrap,
666666

667667
inline void SetLastError(
668668
QuicError error = {
669-
QUIC_ERROR_SESSION,
670-
NGTCP2_NO_ERROR
669+
uint32_t{QUIC_ERROR_SESSION},
670+
uint64_t{NGTCP2_NO_ERROR}
671671
});
672-
inline void SetLastError(QuicErrorFamily family, uint64_t error_code);
673-
inline void SetLastError(QuicErrorFamily family, int error_code);
672+
inline void SetLastError(int32_t family, uint64_t error_code);
673+
inline void SetLastError(int32_t family, int error_code);
674674

675675
inline uint64_t GetMaxLocalStreamsUni();
676676

@@ -1108,7 +1108,7 @@ class QuicSession : public AsyncWrap,
11081108
uint64_t error_code,
11091109
ngtcp2_tstamp ts);
11101110

1111-
static inline ngtcp2_close_fn SelectCloseFn(QuicErrorFamily family) {
1111+
static inline ngtcp2_close_fn SelectCloseFn(uint32_t family) {
11121112
if (family == QUIC_ERROR_APPLICATION)
11131113
return ngtcp2_conn_write_application_close;
11141114
return ngtcp2_conn_write_connection_close;
@@ -1123,7 +1123,10 @@ class QuicSession : public AsyncWrap,
11231123
BaseObjectWeakPtr<QuicSocket> socket_;
11241124
std::string alpn_;
11251125
std::string hostname_;
1126-
QuicError last_error_ = { QUIC_ERROR_SESSION, NGTCP2_NO_ERROR };
1126+
QuicError last_error_ = {
1127+
uint32_t{QUIC_ERROR_SESSION},
1128+
uint64_t{NGTCP2_NO_ERROR}
1129+
};
11271130
ConnectionPointer connection_;
11281131
SocketAddress remote_address_;
11291132
uint32_t flags_ = 0;

src/node_quic_stream.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,11 @@ void QuicStreamReset(const FunctionCallbackInfo<Value>& args) {
491491
QuicStream* stream;
492492
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder());
493493

494-
uint32_t family = QUIC_ERROR_APPLICATION;
495-
uint64_t code = ExtractErrorCode(env, args[0]);
496-
if (!args[1]->Uint32Value(env->context()).To(&family)) return;
494+
QuicError error(env, args[0], args[1], QUIC_ERROR_APPLICATION);
497495

498496
stream->ResetStream(
499-
family == QUIC_ERROR_APPLICATION ?
500-
code : static_cast<uint64_t>(NGTCP2_NO_ERROR));
497+
error.family == QUIC_ERROR_APPLICATION ?
498+
error.code : static_cast<uint64_t>(NGTCP2_NO_ERROR));
501499
}
502500

503501
// Requests transmission of a block of informational headers. Not all

src/node_quic_util.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "node_internals.h"
12
#include "node_quic_util.h"
23
#include "env-inl.h"
34
#include "util-inl.h"
@@ -53,5 +54,61 @@ const char* crypto_level_name(ngtcp2_crypto_level level) {
5354
}
5455
}
5556

57+
QuicError::QuicError(
58+
int32_t family_,
59+
uint64_t code_) :
60+
family(family_),
61+
code(code_) {}
62+
63+
QuicError::QuicError(
64+
int32_t family_,
65+
int code_) :
66+
family(family_) {
67+
switch (family) {
68+
case QUIC_ERROR_CRYPTO:
69+
code_ |= NGTCP2_CRYPTO_ERROR;
70+
// Fall-through...
71+
case QUIC_ERROR_SESSION:
72+
code = ngtcp2_err_infer_quic_transport_error_code(code_);
73+
break;
74+
case QUIC_ERROR_APPLICATION:
75+
code = code_;
76+
default:
77+
UNREACHABLE();
78+
}
79+
}
80+
81+
QuicError::QuicError(
82+
Environment* env,
83+
v8::Local<v8::Value> codeArg,
84+
v8::Local<v8::Value> familyArg,
85+
int32_t family_) :
86+
code(NGTCP2_NO_ERROR),
87+
family(family_) {
88+
if (codeArg->IsBigInt()) {
89+
code = codeArg.As<v8::BigInt>()->Int64Value();
90+
} else if (codeArg->IsNumber()) {
91+
double num = 0;
92+
CHECK(codeArg->NumberValue(env->context()).To(&num));
93+
code = static_cast<uint64_t>(num);
94+
}
95+
if (familyArg->IsNumber()) {
96+
CHECK(familyArg->Int32Value(env->context()).To(&family));
97+
}
98+
}
99+
100+
const char* QuicError::GetFamilyName() {
101+
switch (family) {
102+
case QUIC_ERROR_SESSION:
103+
return "Session";
104+
case QUIC_ERROR_APPLICATION:
105+
return "Application";
106+
case QUIC_ERROR_CRYPTO:
107+
return "Crypto";
108+
default:
109+
UNREACHABLE();
110+
}
111+
}
112+
56113
} // namespace quic
57114
} // namespace node

src/node_quic_util.h

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -71,64 +71,29 @@ inline void hash_combine(size_t* seed, const T& value, Args... rest) {
7171
// look at the ALPN identifier to determine exactly what it
7272
// means. Connection (Session) and Crypto errors, on the other
7373
// hand, share the same meaning regardless of the ALPN.
74-
enum QuicErrorFamily : int {
74+
enum QuicErrorFamily : int32_t {
7575
QUIC_ERROR_SESSION,
7676
QUIC_ERROR_CRYPTO,
7777
QUIC_ERROR_APPLICATION
7878
};
7979

8080
struct QuicError {
81-
QuicErrorFamily family;
81+
int32_t family;
8282
uint64_t code;
83-
inline QuicError(
84-
QuicErrorFamily family_ = QUIC_ERROR_SESSION,
85-
uint64_t code_ = NGTCP2_NO_ERROR) :
86-
family(family_), code(code_) {}
83+
QuicError(
84+
int32_t family_ = QUIC_ERROR_SESSION,
85+
int code_ = NGTCP2_NO_ERROR);
86+
QuicError(
87+
int32_t family_ = QUIC_ERROR_SESSION,
88+
uint64_t code_ = NGTCP2_NO_ERROR);
89+
QuicError(
90+
Environment* env,
91+
v8::Local<v8::Value> codeArg,
92+
v8::Local<v8::Value> familyArg = v8::Local<v8::Object>(),
93+
int32_t family_ = QUIC_ERROR_SESSION);
94+
const char* GetFamilyName();
8795
};
8896

89-
inline QuicError InitQuicError(
90-
QuicErrorFamily family = QUIC_ERROR_SESSION,
91-
int code_ = NGTCP2_NO_ERROR) {
92-
QuicError error;
93-
error.family = family;
94-
switch (family) {
95-
case QUIC_ERROR_CRYPTO:
96-
code_ |= NGTCP2_CRYPTO_ERROR;
97-
// Fall-through...
98-
case QUIC_ERROR_SESSION:
99-
error.code = ngtcp2_err_infer_quic_transport_error_code(code_);
100-
break;
101-
case QUIC_ERROR_APPLICATION:
102-
error.code = code_;
103-
}
104-
return error;
105-
}
106-
107-
inline uint64_t ExtractErrorCode(Environment* env, v8::Local<v8::Value> arg) {
108-
uint64_t code = NGTCP2_APP_NOERROR;
109-
if (arg->IsBigInt()) {
110-
code = arg.As<v8::BigInt>()->Int64Value();
111-
} else if (arg->IsNumber()) {
112-
double num = 0;
113-
USE(arg->NumberValue(env->context()).To(&num));
114-
code = static_cast<uint64_t>(num);
115-
}
116-
return code;
117-
}
118-
119-
inline const char* ErrorFamilyName(QuicErrorFamily family) {
120-
switch (family) {
121-
case QUIC_ERROR_SESSION:
122-
return "Session";
123-
case QUIC_ERROR_APPLICATION:
124-
return "Application";
125-
case QUIC_ERROR_CRYPTO:
126-
return "Crypto";
127-
default:
128-
return "<unknown>";
129-
}
130-
}
131-
13297
class SocketAddress {
13398
public:
13499
// std::hash specialization for sockaddr instances (ipv4 or ipv6) used

0 commit comments

Comments
 (0)