Skip to content

Commit 05ca05a

Browse files
committed
More work on tunnel code
1 parent 6cb5666 commit 05ca05a

12 files changed

+664
-286
lines changed

CMakeLists.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ add_library(quictest
2727
client.cpp
2828
connection.cpp
2929
endpoint.cpp
30+
log.cpp
31+
null-crypto.cpp
3032
server.cpp
3133
stream.cpp
3234
)
@@ -36,10 +38,11 @@ set_target_properties(ngtcp2 PROPERTIES IMPORTED_LOCATION ${NGTCP2_DIR}/lib/libn
3638
target_include_directories(ngtcp2 INTERFACE ${NGTCP2_DIR}/lib/includes ${NGTCP2_DIR}/../lib/includes)
3739

3840
find_package(PkgConfig REQUIRED)
39-
pkg_check_modules(LIBUV libuv>=1.18.0 IMPORTED_TARGET)
40-
pkg_check_modules(OXENMQ liboxenmq>=1.2.3 IMPORTED_TARGET)
41+
pkg_check_modules(LIBUV REQUIRED libuv>=1.18.0 IMPORTED_TARGET)
42+
pkg_check_modules(OXENMQ REQUIRED liboxenmq>=1.2.3 IMPORTED_TARGET)
43+
pkg_check_modules(SODIUM REQUIRED libsodium>=1.0.17 IMPORTED_TARGET)
4144

42-
target_link_libraries(quictest PUBLIC ngtcp2 PkgConfig::LIBUV PkgConfig::OXENMQ)
45+
target_link_libraries(quictest PUBLIC ngtcp2 PkgConfig::LIBUV PkgConfig::OXENMQ PkgConfig::SODIUM)
4346

4447
add_executable(echo-server echo-server.cpp)
4548
add_executable(echo-client echo-client.cpp)

client.cpp

+26-45
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
#include "client.h"
33
#include "log.h"
44

5-
namespace quic {
6-
7-
// Cranks a value to 11, i.e. set it to its maximum
8-
template <typename T>
9-
void crank_to_eleven(T& val) { val = std::numeric_limits<T>::max(); }
5+
#include <oxenmq/variant.h>
106

11-
static std::array<uint8_t, 32> null_secret{};
12-
static std::array<uint8_t, 16> null_iv{};
13-
static std::array<uint8_t, 4096> null_data{};
7+
namespace quic {
148

159
Client::Client(Address remote, uv_loop_t* loop_, std::optional<Address> local_)
1610
: Endpoint{std::move(local_), loop_} {
@@ -30,38 +24,21 @@ Client::Client(Address remote, uv_loop_t* loop_, std::optional<Address> local_)
3024
// - delay_stream_timer
3125

3226

33-
auto local_cid = ConnectionID::random(rng);
34-
auto [it, ins] = conns.emplace(std::piecewise_construct,
35-
std::forward_as_tuple(local_cid),
36-
std::forward_as_tuple(*this, local_cid, path));
37-
assert(ins);
38-
auto& conn = it->second;
39-
40-
// FIXME: likely need to move this crap info connection.cpp, or maybe a "null_crypto.cpp"?
41-
ngtcp2_crypto_ctx null_crypto{};
42-
crank_to_eleven(null_crypto.max_encryption);
43-
crank_to_eleven(null_crypto.max_decryption_failure);
27+
auto connptr = std::make_shared<Connection>(*this, ConnectionID::random(rng), path);
28+
auto& conn = *connptr;
29+
conns.emplace(conn.base_cid, connptr);
4430

45-
Debug("set crypto ctx");
31+
/* Debug("set crypto ctx");
4632
47-
ngtcp2_crypto_aead_ctx null_aead_ctx{};
48-
ngtcp2_crypto_aead retry_aead{0, 16}; // FIXME: 16 overhead is for AES-128-GCM AEAD, but do we need it?
49-
ngtcp2_crypto_cipher_ctx null_cipher_ctx{};
50-
51-
ngtcp2_conn_set_initial_crypto_ctx(conn, &null_crypto);
52-
ngtcp2_conn_install_initial_key(conn, &null_aead_ctx, null_iv.data(), &null_cipher_ctx, &null_aead_ctx, null_iv.data(), &null_cipher_ctx, null_iv.size());
53-
ngtcp2_conn_set_retry_aead(conn, &retry_aead, &null_aead_ctx);
54-
ngtcp2_conn_set_crypto_ctx(conn, &null_crypto);
55-
ngtcp2_conn_install_rx_handshake_key(conn, &null_aead_ctx, null_iv.data(), null_iv.size(), &null_cipher_ctx);
56-
ngtcp2_conn_install_tx_handshake_key(conn, &null_aead_ctx, null_iv.data(), null_iv.size(), &null_cipher_ctx);
57-
ngtcp2_conn_install_rx_key(conn, null_secret.data(), null_secret.size(), &null_aead_ctx, null_iv.data(), null_iv.size(), &null_cipher_ctx);
58-
ngtcp2_conn_install_tx_key(conn, null_secret.data(), null_secret.size(), &null_aead_ctx, null_iv.data(), null_iv.size(), &null_cipher_ctx);
33+
null_crypto.client_initial(conn);
5934
6035
auto x = ngtcp2_conn_get_max_data_left(conn);
6136
Debug("mdl = ", x);
37+
*/
6238

63-
conn.flush_streams();
39+
conn.io_ready();
6440

41+
/*
6542
Debug("Opening bidi stream");
6643
int64_t stream_id;
6744
if (auto rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, nullptr);
@@ -70,24 +47,28 @@ Client::Client(Address remote, uv_loop_t* loop_, std::optional<Address> local_)
7047
assert(rv == NGTCP2_ERR_STREAM_ID_BLOCKED);
7148
}
7249
else { Debug("Opening bidi stream good"); }
50+
*/
7351
}
7452

7553
void Client::handle_packet(const Packet& p) {
76-
version_info vi;
77-
auto rv = ngtcp2_pkt_decode_version_cid(&vi.version, &vi.dcid, &vi.dcid_len, &vi.scid, &vi.scid_len,
78-
u8data(p.data), p.data.size(), NGTCP2_MAX_CIDLEN);
79-
if (rv == 1) // 1 means Version Negotiation should be sent
80-
return send_version_negotiation(vi, p.path.remote);
81-
else if (rv != 0) {
82-
Warn("QUIC packet header decode failed: ", ngtcp2_strerror(rv));
83-
return;
84-
}
85-
86-
if (vi.dcid_len > ConnectionID::max_size()) {
87-
Warn("Internal error: destination ID is longer than should be allowed");
54+
Debug("Handling incoming client packet: ", buffer_printer{p.data});
55+
auto maybe_dcid = handle_packet_init(p);
56+
if (!maybe_dcid) return;
57+
auto& dcid = *maybe_dcid;
58+
59+
Debug("Incoming connection id ", dcid);
60+
auto [connptr, alias] = get_conn(dcid);
61+
if (!connptr) {
62+
Debug("CID is ", alias ? "expired alias" : "unknown/expired", "; dropping");
8863
return;
8964
}
65+
auto& conn = *connptr;
66+
if (alias)
67+
Debug("CID is alias for primary CID ", conn.base_cid);
68+
else
69+
Debug("CID is primary CID");
9070

71+
handle_conn_packet(conn, p);
9172
}
9273

9374
}

0 commit comments

Comments
 (0)