Skip to content

Commit

Permalink
quic: fixups after ngtcp2/nghttp3 update
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jasnell committed Aug 21, 2020
1 parent c6e1edc commit 2a80737
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
22 changes: 20 additions & 2 deletions src/quic/node_quic_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,22 @@ bool GenerateRetryToken(
return false;
}

ngtcp2_crypto_aead_ctx aead_ctx;
if (NGTCP2_ERR(ngtcp2_crypto_aead_ctx_encrypt_init(
&aead_ctx,
&ctx.aead,
token_key,
ivlen))) {
return false;
}

size_t plaintextlen = std::distance(std::begin(plaintext), p);
if (NGTCP2_ERR(ngtcp2_crypto_encrypt(
token,
&ctx.aead,
&aead_ctx,
plaintext.data(),
plaintextlen,
token_key,
token_iv,
ivlen,
addr.raw(),
Expand Down Expand Up @@ -291,12 +300,21 @@ bool InvalidRetryToken(

uint8_t plaintext[4096];

ngtcp2_crypto_aead_ctx aead_ctx;
if (NGTCP2_ERR(ngtcp2_crypto_aead_ctx_decrypt_init(
&aead_ctx,
&ctx.aead,
token_key,
ivlen))) {
return true;
}

if (NGTCP2_ERR(ngtcp2_crypto_decrypt(
plaintext,
&ctx.aead,
&aead_ctx,
ciphertext,
ciphertextlen,
token_key,
token_iv,
ivlen,
addr.raw(),
Expand Down
2 changes: 2 additions & 0 deletions src/quic/node_quic_session-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void QuicSessionConfig::set_original_connection_id(
transport_params.original_dcid = *ocid;
transport_params.retry_scid = *scid;
transport_params.retry_scid_present = 1;
} else {
transport_params.original_dcid = *scid;
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/quic/node_quic_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ bool QuicApplication::SendPendingData() {
continue;
case NGTCP2_ERR_STREAM_NOT_FOUND:
continue;
case NGTCP2_ERR_WRITE_STREAM_MORE:
case NGTCP2_ERR_WRITE_MORE:
CHECK_GT(ndatalen, 0);
CHECK(StreamCommit(&stream_data, ndatalen));
pos += ndatalen;
Expand Down Expand Up @@ -2096,7 +2096,6 @@ bool QuicSession::Receive(

if (!is_destroyed())
UpdateIdleTimer();

SendPendingData();
Debug(this, "Successfully processed received packet");
return true;
Expand Down Expand Up @@ -2239,8 +2238,11 @@ void QuicSession::RemoveStream(int64_t stream_id) {
void QuicSession::ScheduleRetransmit() {
uint64_t now = uv_hrtime();
uint64_t expiry = ngtcp2_conn_get_expiry(connection());
uint64_t interval = (expiry - now) / 1000000UL;
if (expiry < now || interval == 0) interval = 1;
// now and expiry are in nanoseconds, interval is milliseconds
uint64_t interval = (expiry < now) ? 1 : (expiry - now) / 1000000UL;
// If interval ends up being 0, the repeating timer won't be
// scheduled, so set it to 1 instead.
if (interval == 0) interval = 1;
Debug(this, "Scheduling the retransmit timer for %" PRIu64, interval);
UpdateRetransmitTimer(interval);
}
Expand Down Expand Up @@ -2440,7 +2442,7 @@ bool QuicSession::SendPacket(std::unique_ptr<QuicPacket> packet) {

IncrementStat(&QuicSessionStats::bytes_sent, packet->length());
RecordTimestamp(&QuicSessionStats::sent_at);
ScheduleRetransmit();
// ScheduleRetransmit();

Debug(this, "Sending %" PRIu64 " bytes to %s from %s",
packet->length(),
Expand Down Expand Up @@ -2471,6 +2473,7 @@ void QuicSession::SendPendingData() {
Debug(this, "Error sending QUIC application data");
HandleError();
}
ScheduleRetransmit();
}

// When completing the TLS handshake, the TLS session information
Expand Down Expand Up @@ -3392,11 +3395,9 @@ int QuicSession::OnStreamReset(
// Currently, there is only one use. In the future, we'll want to
// explore whether we want to handle the different cases uses.
int QuicSession::OnRand(
ngtcp2_conn* conn,
uint8_t* dest,
size_t destlen,
ngtcp2_rand_ctx ctx,
void* user_data) {
ngtcp2_rand_ctx ctx) {
EntropySource(dest, destlen);
return 0;
}
Expand Down Expand Up @@ -3541,6 +3542,8 @@ const ngtcp2_conn_callbacks QuicSession::callbacks[2] = {
OnConnectionIDStatus,
OnHandshakeConfirmed,
nullptr, // recv_new_token
ngtcp2_crypto_delete_crypto_aead_ctx_cb,
ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
},
// NGTCP2_CRYPTO_SIDE_SERVER
{
Expand Down Expand Up @@ -3574,6 +3577,8 @@ const ngtcp2_conn_callbacks QuicSession::callbacks[2] = {
OnConnectionIDStatus,
nullptr, // handshake_confirmed
nullptr, // recv_new_token
ngtcp2_crypto_delete_crypto_aead_ctx_cb,
ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
}
};

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

void QuicSession::OnQlogWrite(void* user_data, const void* data, size_t len) {
void QuicSession::OnQlogWrite(
void* user_data,
uint32_t flags,
const void* data,
size_t len) {
QuicSession* session = static_cast<QuicSession*>(user_data);
Environment* env = session->env();

Expand Down Expand Up @@ -3888,7 +3897,6 @@ void NewQuicClientSession(const FunctionCallbackInfo<Value>& args) {
args[ARG_IDX::QLOG]->IsTrue() ?
QlogMode::kEnabled :
QlogMode::kDisabled);

session->SendPendingData();
if (session->is_destroyed())
return args.GetReturnValue().Set(ERR_FAILED_TO_CREATE_SESSION);
Expand Down
11 changes: 7 additions & 4 deletions src/quic/node_quic_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class QuicSessionConfig final : public ngtcp2_settings {

QuicSessionConfig(const QuicSessionConfig& config) {
initial_ts = uv_hrtime();
initial_rtt = config.initial_rtt;
transport_params = config.transport_params;
max_udp_payload_size = config.max_udp_payload_size;
cc_algo = config.cc_algo;
Expand Down Expand Up @@ -1366,11 +1367,9 @@ class QuicSession final : public AsyncWrap,
void* stream_user_data);

static int OnRand(
ngtcp2_conn* conn,
uint8_t* dest,
size_t destlen,
ngtcp2_rand_ctx ctx,
void* user_data);
ngtcp2_rand_ctx ctx);

static int OnGetNewConnectionID(
ngtcp2_conn* conn,
Expand Down Expand Up @@ -1437,7 +1436,11 @@ class QuicSession final : public AsyncWrap,
const uint8_t* token,
void* user_data);

static void OnQlogWrite(void* user_data, const void* data, size_t len);
static void OnQlogWrite(
void* user_data,
uint32_t flags,
const void* data,
size_t len);

#define V(id, _) QUICSESSION_FLAG_##id,
enum QuicSessionFlags : uint32_t {
Expand Down

0 comments on commit 2a80737

Please sign in to comment.