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

quic: additional cleanups #217

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/node_quic_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,20 @@ bool DeriveTokenKey(
secret.size()));
}

bool MessageDigest(
std::array<uint8_t, 32>* dest,
const std::array<uint8_t, 16>& rand) {
void GenerateRandData(uint8_t* buf, size_t len) {
std::array<uint8_t, 16> rand;
std::array<uint8_t, 32> md;

const EVP_MD* meth = EVP_sha256();
unsigned int mdlen = EVP_MD_size(meth);
DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free> ctx;
ctx.reset(EVP_MD_CTX_new());
CHECK(ctx);

if (EVP_DigestInit_ex(ctx.get(), meth, nullptr) != 1 ||
EVP_DigestUpdate(ctx.get(), rand.data(), rand.size()) != 1) {
return false;
}

unsigned int mdlen = EVP_MD_size(meth);

return EVP_DigestFinal_ex(ctx.get(), dest->data(), &mdlen) == 1;
}

void GenerateRandData(uint8_t* buf, size_t len) {
std::array<uint8_t, 16> rand;
std::array<uint8_t, 32> md;
EntropySource(rand.data(), rand.size());
CHECK(MessageDigest(&md, rand));
CHECK_EQ(EVP_DigestInit_ex(ctx.get(), meth, nullptr), 1);
CHECK_EQ(EVP_DigestUpdate(ctx.get(), rand.data(), rand.size()), 1);
CHECK_EQ(EVP_DigestFinal_ex(ctx.get(), rand.data(), &mdlen), 1);
CHECK_LE(len, md.size());
std::copy_n(std::begin(md), len, buf);
}
Expand Down
9 changes: 5 additions & 4 deletions src/node_quic_default_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ bool DefaultApplication::ReceiveStreamData(
// empty stream frames to commit resources. Check that
// here. Essentially, we only want to create a new stream
// if the datalen is greater than 0, otherwise, we ignore
// the packet.
if (datalen == 0)
// the packet. ngtcp2 should be handling this for us,
// but we handle it just to be safe.
if (UNLIKELY(datalen == 0))
return true;

stream = Session()->CreateStream(stream_id);
Expand All @@ -62,7 +63,7 @@ void DefaultApplication::AcknowledgeStreamData(
Debug(Session(), "Default QUIC Application acknowledging stream data");
// It's possible that the stream has already been destroyed and
// removed. If so, just silently ignore the ack
if (stream)
if (stream != nullptr)
stream->AckedDataOffset(offset, datalen);
}

Expand Down Expand Up @@ -126,7 +127,7 @@ bool DefaultApplication::SendStreamData(QuicStream* stream) {
size_t remaining = stream->DrainInto(&vec);
Debug(stream, "Sending %d bytes of stream data. Still writable? %s",
remaining,
stream->IsWritable()?"yes":"no");
stream->IsWritable() ? "yes" : "no");

// c and v are used to track the current serialization position
// for each iteration of the for(;;) loop below.
Expand Down
30 changes: 30 additions & 0 deletions src/node_quic_http3_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <nghttp3/nghttp3.h>
#include <algorithm>
#include <string>

namespace node {

Expand Down Expand Up @@ -59,6 +60,13 @@ Http3Header::Http3Header(
value_.reset(value);
}

Http3Header::Http3Header(Http3Header&& other) noexcept :
token_(other.token_),
name_(std::move(other.name_)),
value_(std::move(other.value_)) {
other.token_ = -1;
}

MaybeLocal<String> Http3Header::GetName(QuicApplication* app) const {
const char* header_name = to_http_header_name(token_);
Environment* env = app->env();
Expand Down Expand Up @@ -97,6 +105,28 @@ MaybeLocal<String> Http3Header::GetValue(QuicApplication* app) const {
value_);
}

std::string Http3Header::GetName() const {
const char* header_name = to_http_header_name(token_);
if (header_name != nullptr)
return std::string(header_name);

if (UNLIKELY(!name_))
return std::string(); // Empty String

return std::string(
reinterpret_cast<const char*>(name_.data()),
name_.len());
}

std::string Http3Header::GetValue() const {
if (UNLIKELY(!value_))
return std::string(); // Empty String

return std::string(
reinterpret_cast<const char*>(value_.data()),
value_.len());
}

namespace {
template <typename t>
inline void SetConfig(Environment* env, int idx, t* val) {
Expand Down
11 changes: 5 additions & 6 deletions src/node_quic_http3_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ngtcp2/ngtcp2.h>
#include <nghttp3/nghttp3.h>

#include <string>
namespace node {

namespace quic {
Expand Down Expand Up @@ -52,16 +53,14 @@ using Http3ConnectionPointer = DeleteFnPtr<nghttp3_conn, nghttp3_conn_del>;
class Http3Header : public QuicHeader {
public:
Http3Header(int32_t token, nghttp3_rcbuf* name, nghttp3_rcbuf* value);
Http3Header(Http3Header&& other) noexcept :
token_(other.token_),
name_(std::move(other.name_)),
value_(std::move(other.value_)) {
other.token_ = -1;
}
Http3Header(Http3Header&& other) noexcept;

v8::MaybeLocal<v8::String> GetName(QuicApplication* app) const override;
v8::MaybeLocal<v8::String> GetValue(QuicApplication* app) const override;

std::string GetName() const override;
std::string GetValue() const override;

private:
int32_t token_ = -1;
Http3RcBufferPointer name_;
Expand Down
3 changes: 3 additions & 0 deletions src/node_quic_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "stream_base-inl.h"
#include "v8.h"

#include <string>
#include <vector>

namespace node {
Expand Down Expand Up @@ -60,6 +61,8 @@ class QuicHeader {
virtual ~QuicHeader() {}
virtual v8::MaybeLocal<v8::String> GetName(QuicApplication* app) const = 0;
virtual v8::MaybeLocal<v8::String> GetValue(QuicApplication* app) const = 0;
virtual std::string GetName() const = 0;
virtual std::string GetValue() const = 0;
};

// QuicStream's are simple data flows that, fortunately, do not
Expand Down