Skip to content

Commit 2a80737

Browse files
committed
quic: fixups after ngtcp2/nghttp3 update
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #34752 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent c6e1edc commit 2a80737

4 files changed

+47
-16
lines changed

src/quic/node_quic_crypto.cc

+20-2
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,22 @@ bool GenerateRetryToken(
164164
return false;
165165
}
166166

167+
ngtcp2_crypto_aead_ctx aead_ctx;
168+
if (NGTCP2_ERR(ngtcp2_crypto_aead_ctx_encrypt_init(
169+
&aead_ctx,
170+
&ctx.aead,
171+
token_key,
172+
ivlen))) {
173+
return false;
174+
}
175+
167176
size_t plaintextlen = std::distance(std::begin(plaintext), p);
168177
if (NGTCP2_ERR(ngtcp2_crypto_encrypt(
169178
token,
170179
&ctx.aead,
180+
&aead_ctx,
171181
plaintext.data(),
172182
plaintextlen,
173-
token_key,
174183
token_iv,
175184
ivlen,
176185
addr.raw(),
@@ -291,12 +300,21 @@ bool InvalidRetryToken(
291300

292301
uint8_t plaintext[4096];
293302

303+
ngtcp2_crypto_aead_ctx aead_ctx;
304+
if (NGTCP2_ERR(ngtcp2_crypto_aead_ctx_decrypt_init(
305+
&aead_ctx,
306+
&ctx.aead,
307+
token_key,
308+
ivlen))) {
309+
return true;
310+
}
311+
294312
if (NGTCP2_ERR(ngtcp2_crypto_decrypt(
295313
plaintext,
296314
&ctx.aead,
315+
&aead_ctx,
297316
ciphertext,
298317
ciphertextlen,
299-
token_key,
300318
token_iv,
301319
ivlen,
302320
addr.raw(),

src/quic/node_quic_session-inl.h

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void QuicSessionConfig::set_original_connection_id(
5050
transport_params.original_dcid = *ocid;
5151
transport_params.retry_scid = *scid;
5252
transport_params.retry_scid_present = 1;
53+
} else {
54+
transport_params.original_dcid = *scid;
5355
}
5456
}
5557

src/quic/node_quic_session.cc

+18-10
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ bool QuicApplication::SendPendingData() {
14221422
continue;
14231423
case NGTCP2_ERR_STREAM_NOT_FOUND:
14241424
continue;
1425-
case NGTCP2_ERR_WRITE_STREAM_MORE:
1425+
case NGTCP2_ERR_WRITE_MORE:
14261426
CHECK_GT(ndatalen, 0);
14271427
CHECK(StreamCommit(&stream_data, ndatalen));
14281428
pos += ndatalen;
@@ -2096,7 +2096,6 @@ bool QuicSession::Receive(
20962096

20972097
if (!is_destroyed())
20982098
UpdateIdleTimer();
2099-
21002099
SendPendingData();
21012100
Debug(this, "Successfully processed received packet");
21022101
return true;
@@ -2239,8 +2238,11 @@ void QuicSession::RemoveStream(int64_t stream_id) {
22392238
void QuicSession::ScheduleRetransmit() {
22402239
uint64_t now = uv_hrtime();
22412240
uint64_t expiry = ngtcp2_conn_get_expiry(connection());
2242-
uint64_t interval = (expiry - now) / 1000000UL;
2243-
if (expiry < now || interval == 0) interval = 1;
2241+
// now and expiry are in nanoseconds, interval is milliseconds
2242+
uint64_t interval = (expiry < now) ? 1 : (expiry - now) / 1000000UL;
2243+
// If interval ends up being 0, the repeating timer won't be
2244+
// scheduled, so set it to 1 instead.
2245+
if (interval == 0) interval = 1;
22442246
Debug(this, "Scheduling the retransmit timer for %" PRIu64, interval);
22452247
UpdateRetransmitTimer(interval);
22462248
}
@@ -2440,7 +2442,7 @@ bool QuicSession::SendPacket(std::unique_ptr<QuicPacket> packet) {
24402442

24412443
IncrementStat(&QuicSessionStats::bytes_sent, packet->length());
24422444
RecordTimestamp(&QuicSessionStats::sent_at);
2443-
ScheduleRetransmit();
2445+
// ScheduleRetransmit();
24442446

24452447
Debug(this, "Sending %" PRIu64 " bytes to %s from %s",
24462448
packet->length(),
@@ -2471,6 +2473,7 @@ void QuicSession::SendPendingData() {
24712473
Debug(this, "Error sending QUIC application data");
24722474
HandleError();
24732475
}
2476+
ScheduleRetransmit();
24742477
}
24752478

24762479
// When completing the TLS handshake, the TLS session information
@@ -3392,11 +3395,9 @@ int QuicSession::OnStreamReset(
33923395
// Currently, there is only one use. In the future, we'll want to
33933396
// explore whether we want to handle the different cases uses.
33943397
int QuicSession::OnRand(
3395-
ngtcp2_conn* conn,
33963398
uint8_t* dest,
33973399
size_t destlen,
3398-
ngtcp2_rand_ctx ctx,
3399-
void* user_data) {
3400+
ngtcp2_rand_ctx ctx) {
34003401
EntropySource(dest, destlen);
34013402
return 0;
34023403
}
@@ -3541,6 +3542,8 @@ const ngtcp2_conn_callbacks QuicSession::callbacks[2] = {
35413542
OnConnectionIDStatus,
35423543
OnHandshakeConfirmed,
35433544
nullptr, // recv_new_token
3545+
ngtcp2_crypto_delete_crypto_aead_ctx_cb,
3546+
ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
35443547
},
35453548
// NGTCP2_CRYPTO_SIDE_SERVER
35463549
{
@@ -3574,6 +3577,8 @@ const ngtcp2_conn_callbacks QuicSession::callbacks[2] = {
35743577
OnConnectionIDStatus,
35753578
nullptr, // handshake_confirmed
35763579
nullptr, // recv_new_token
3580+
ngtcp2_crypto_delete_crypto_aead_ctx_cb,
3581+
ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
35773582
}
35783583
};
35793584

@@ -3585,7 +3590,11 @@ BaseObjectPtr<QLogStream> QuicSession::qlog_stream() {
35853590
return qlog_stream_;
35863591
}
35873592

3588-
void QuicSession::OnQlogWrite(void* user_data, const void* data, size_t len) {
3593+
void QuicSession::OnQlogWrite(
3594+
void* user_data,
3595+
uint32_t flags,
3596+
const void* data,
3597+
size_t len) {
35893598
QuicSession* session = static_cast<QuicSession*>(user_data);
35903599
Environment* env = session->env();
35913600

@@ -3888,7 +3897,6 @@ void NewQuicClientSession(const FunctionCallbackInfo<Value>& args) {
38883897
args[ARG_IDX::QLOG]->IsTrue() ?
38893898
QlogMode::kEnabled :
38903899
QlogMode::kDisabled);
3891-
38923900
session->SendPendingData();
38933901
if (session->is_destroyed())
38943902
return args.GetReturnValue().Set(ERR_FAILED_TO_CREATE_SESSION);

src/quic/node_quic_session.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class QuicSessionConfig final : public ngtcp2_settings {
7575

7676
QuicSessionConfig(const QuicSessionConfig& config) {
7777
initial_ts = uv_hrtime();
78+
initial_rtt = config.initial_rtt;
7879
transport_params = config.transport_params;
7980
max_udp_payload_size = config.max_udp_payload_size;
8081
cc_algo = config.cc_algo;
@@ -1366,11 +1367,9 @@ class QuicSession final : public AsyncWrap,
13661367
void* stream_user_data);
13671368

13681369
static int OnRand(
1369-
ngtcp2_conn* conn,
13701370
uint8_t* dest,
13711371
size_t destlen,
1372-
ngtcp2_rand_ctx ctx,
1373-
void* user_data);
1372+
ngtcp2_rand_ctx ctx);
13741373

13751374
static int OnGetNewConnectionID(
13761375
ngtcp2_conn* conn,
@@ -1437,7 +1436,11 @@ class QuicSession final : public AsyncWrap,
14371436
const uint8_t* token,
14381437
void* user_data);
14391438

1440-
static void OnQlogWrite(void* user_data, const void* data, size_t len);
1439+
static void OnQlogWrite(
1440+
void* user_data,
1441+
uint32_t flags,
1442+
const void* data,
1443+
size_t len);
14411444

14421445
#define V(id, _) QUICSESSION_FLAG_##id,
14431446
enum QuicSessionFlags : uint32_t {

0 commit comments

Comments
 (0)