1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

addressing fix plus linting

This commit is contained in:
dan 2023-03-06 08:01:48 -08:00
parent 6d28d0d78c
commit 550486d216
7 changed files with 117 additions and 102 deletions

View file

@ -36,8 +36,11 @@ namespace llarp::quic
//
// - key_update_timer
Path path{local_addr, remote};
path.remote;
auto data = var::visit([](auto&& addr) { return addr.as_array(); }, ep.LocalAddress());
huint128_t ip{};
std::copy_n(data.begin(), sizeof(ip.h), &ip.h);
Path path{SockAddr{ip, huint16_t{pseudo_port}}, remote};
log::debug(logcat, "Connecting to {}", remote);
auto conn = std::make_shared<Connection>(*this, ConnectionID::random(), path, tunnel_port);

View file

@ -177,7 +177,7 @@ namespace llarp::quic
if (auto rv = conn.complete_handshake(); rv != 0)
return rv;
break;
case NGTCP2_CRYPTO_LEVEL_APPLICATION:
@ -596,9 +596,9 @@ namespace llarp::quic
cb.client_initial = client_initial;
cb.recv_retry = recv_retry;
//cb.handshake_confirmed = handshake_confirmed;
// cb.extend_max_local_streams_bidi = extend_max_local_streams_bidi;
// cb.recv_new_token = recv_new_token;
// cb.handshake_confirmed = handshake_confirmed;
// cb.extend_max_local_streams_bidi = extend_max_local_streams_bidi;
// cb.recv_new_token = recv_new_token;
ngtcp2_conn* connptr;
@ -668,7 +668,7 @@ namespace llarp::quic
if (sent.blocked())
{
log::debug(logcat, "Packet send blocked, scheduling retransmit");
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
schedule_retransmit();
return 0;
@ -679,7 +679,7 @@ namespace llarp::quic
{
log::warning(logcat, "I/O error while trying to send packet: {}", sent.str());
// FIXME: disconnect?
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
return 0;
}
@ -705,17 +705,22 @@ namespace llarp::quic
}
}
}
while (!strs.empty() && stream_packets < max_stream_packets)
{
for (auto it = strs.begin(); it != strs.end();)
{
log::debug(logcat, "Max stream packets: {}\nCurrent stream packets: {}", max_stream_packets, stream_packets);
log::debug(
logcat,
"Max stream packets: {}\nCurrent stream packets: {}",
max_stream_packets,
stream_packets);
auto& stream = **it;
auto bufs = stream.pending();
if (bufs.empty()) {
if (bufs.empty())
{
log::debug(logcat, "Stream buffer empty, have you considered moving on");
}
@ -725,7 +730,7 @@ namespace llarp::quic
return ngtcp2_vec{const_cast<uint8_t*>(u8data(buf)), buf.size()};
});
#ifndef NDEBUG
#ifndef NDEBUG
{
std::string buf_sizes;
for (auto& b : bufs)
@ -737,7 +742,7 @@ namespace llarp::quic
log::debug(
logcat, "Sending {} data for {}", buf_sizes.empty() ? "no" : buf_sizes, stream.id());
}
#endif
#endif
// debug
fprintf(
@ -755,29 +760,31 @@ namespace llarp::quic
}
auto nwrite = ngtcp2_conn_writev_stream(
conn.get(),
&path.path,
&send_pkt_info,
u8data(send_buffer),
send_buffer.size(),
&ndatalen,
flags,
stream.id().id,
reinterpret_cast<const ngtcp2_vec*>(vecs.data()),
vecs.size(),
conn.get(),
&path.path,
&send_pkt_info,
u8data(send_buffer),
send_buffer.size(),
&ndatalen,
flags,
stream.id().id,
reinterpret_cast<const ngtcp2_vec*>(vecs.data()),
vecs.size(),
(!ts) ? get_timestamp() : ts);
log::debug(logcat,
log::debug(
logcat,
"add_stream_data for stream {} returned [{},{}]",
stream.id(),
nwrite,
ndatalen);
if (nwrite < 0)
{
if (nwrite == -240) // NGTCP2_ERR_WRITE_MORE
{
log::debug(logcat,
log::debug(
logcat,
"Consumed {} bytes from stream {} and have space left",
ndatalen,
stream.id());
@ -819,16 +826,20 @@ namespace llarp::quic
if (nwrite == 0) // we are probably done, but maybe congested
{
log::debug(logcat,
log::debug(
logcat,
"Done stream writing to {} (either stream is congested or we have nothing else to "
"send right now)",
stream.id());
ngtcp2_conn_stat cstat;
ngtcp2_conn_get_conn_stat(conn.get(), &cstat);
log::debug(logcat, "Current unacked bytes in flight: {}, Congestion window: {}",
cstat.bytes_in_flight, cstat.cwnd);
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
log::debug(
logcat,
"Current unacked bytes in flight: {}, Congestion window: {}",
cstat.bytes_in_flight,
cstat.cwnd);
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
it = strs.erase(it);
continue;
@ -838,16 +849,16 @@ namespace llarp::quic
if (!send_packet(nwrite))
return;
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts); // so far always useful
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts); // so far always useful
//++stream_packets;
//std::advance(it, 1);
// std::advance(it, 1);
it = strs.erase(it);
if (++stream_packets == max_stream_packets)
{
log::debug(logcat, "Max stream packets ({}) reached", max_stream_packets);
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
return;
}
@ -862,17 +873,17 @@ namespace llarp::quic
fprintf(stderr, "Calling add_stream_data for empty stream\n");
auto nwrite = ngtcp2_conn_writev_stream(
conn.get(),
&path.path,
&send_pkt_info,
u8data(send_buffer),
send_buffer.size(),
&ndatalen,
flags,
-1,
nullptr,
0,
(!ts) ? get_timestamp() : ts);
conn.get(),
&path.path,
&send_pkt_info,
u8data(send_buffer),
send_buffer.size(),
&ndatalen,
flags,
-1,
nullptr,
0,
(!ts) ? get_timestamp() : ts);
log::debug(logcat, "add_stream_data for non-stream returned [{},{}]", nwrite, ndatalen);
// debug
@ -882,31 +893,34 @@ namespace llarp::quic
if (nwrite == 0)
{
log::debug(
logcat, "Nothing else to write for non-stream data for now (or we are congested)");
logcat, "Nothing else to write for non-stream data for now (or we are congested)");
ngtcp2_conn_stat cstat;
ngtcp2_conn_get_conn_stat(conn.get(), &cstat);
log::debug(logcat, "Current unacked bytes in flight: {}, Congestion window: {}",
cstat.bytes_in_flight, cstat.cwnd);
log::debug(
logcat,
"Current unacked bytes in flight: {}, Congestion window: {}",
cstat.bytes_in_flight,
cstat.cwnd);
break;
}
if (nwrite < 0)
{
if (nwrite == -240) // NGTCP2_ERR_WRITE_MORE
if (nwrite == -240) // NGTCP2_ERR_WRITE_MORE
{
log::debug(logcat, "Writing non-stream data frames, and have space left");
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
continue;
}
if (nwrite == -230) // NGTCP2_ERR_CLOSING
if (nwrite == -230) // NGTCP2_ERR_CLOSING
{
log::warning(logcat, "Error writing non-stream data: {}", ngtcp2_strerror(nwrite));
break;
}
if (nwrite == -210) // NGTCP2_ERR_STREAM_DATA_BLOCKED
if (nwrite == -210) // NGTCP2_ERR_STREAM_DATA_BLOCKED
{
log::debug(logcat, "cannot add to empty stream right now: stream is blocked");
break;
@ -919,13 +933,13 @@ namespace llarp::quic
log::debug(logcat, "Sending data packet with non-stream data frames");
if (auto rv = send_packet(nwrite); rv != 0)
return;
log::debug(logcat, "Updating pkt tx time at {}" ,__LINE__);
log::debug(logcat, "Updating pkt tx time at {}", __LINE__);
ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
}
log::debug(logcat, "Exiting flush_streams()");
//ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
//schedule_retransmit();
// ngtcp2_conn_update_pkt_tx_time(conn.get(), ts);
// schedule_retransmit();
}
void
@ -1157,7 +1171,8 @@ namespace llarp::quic
{
std::shared_ptr<Stream> stream{new Stream{
*this, std::move(data_cb), std::move(close_cb), endpoint.default_stream_buffer_size}};
if (int rv = ngtcp2_conn_open_bidi_stream(conn.get(), &stream->stream_id.id, stream.get()); rv != 0)
if (int rv = ngtcp2_conn_open_bidi_stream(conn.get(), &stream->stream_id.id, stream.get());
rv != 0)
throw std::runtime_error{"Stream creation failed: "s + ngtcp2_strerror(rv)};
auto& str = streams[stream->stream_id];
@ -1172,9 +1187,9 @@ namespace llarp::quic
{
std::shared_ptr<Stream> stream{new Stream{
*this, std::move(data_cb), std::move(close_cb), endpoint.default_stream_buffer_size}};
if (int rv = ngtcp2_conn_open_bidi_stream(conn.get(),
&stream->stream_id.id,
std::get<0>(stream->user_data).get());
if (int rv = ngtcp2_conn_open_bidi_stream(conn.get(),
&stream->stream_id.id,
std::get<0>(stream->user_data).get());
rv != 0)
throw std::runtime_error{"Stream creation failed: "s + ngtcp2_strerror(rv)};
@ -1247,7 +1262,6 @@ namespace llarp::quic
return 0;
}
// ngtcp2 doesn't expose the varint encoding, but it's fairly simple:
// 0bXXyyyyyy -- XX indicates the encoded size (00=1, 01=2, 10=4, 11=8) and the rest of the bits
// (6, 14, 30, or 62) are the number, with bytes in network order for >6-bit values.
@ -1459,7 +1473,8 @@ namespace llarp::quic
return nwrite;
}
log::debug(logcat, "encoded transport params: {}", buffer_printer{conn_buffer});
return ngtcp2_conn_submit_crypto_data(conn.get(), level, u8data(conn_buffer), conn_buffer.size());
return ngtcp2_conn_submit_crypto_data(
conn.get(), level, u8data(conn_buffer), conn_buffer.size());
}
} // namespace llarp::quic

View file

@ -245,7 +245,7 @@ namespace llarp::quic
int
setup_server_crypto_initial();
int
int
get_handshake_confirmed();
// Flush any streams with pending data. Note that, depending on available ngtcp2 state, we may
@ -330,8 +330,8 @@ namespace llarp::quic
send_magic(ngtcp2_crypto_level level);
int
send_transport_params(ngtcp2_crypto_level level);
//void
//complete_handshake();
// void
// complete_handshake();
};
} // namespace llarp::quic

View file

@ -722,7 +722,7 @@ namespace llarp::quic
}
}
}
while (!strs.empty() && stream_packets < max_stream_packets)
{
for (auto it = strs.begin(); it != strs.end();)
@ -730,10 +730,11 @@ namespace llarp::quic
auto& stream = **it;
auto bufs = stream.pending();
if (bufs.empty()) {
if (bufs.empty())
{
log::debug(logcat, "Stream empty, moving on");
//it = strs.erase(it);
//continue;
// it = strs.erase(it);
// continue;
}
std::vector<ngtcp2_vec> vecs;
@ -895,7 +896,7 @@ namespace llarp::quic
fprintf(stderr, "add_stream_data for non-stream returned [%ld,%ld]\n", nwrite, consumed);
assert(consumed <= 0);
if (nwrite == -240) // NGTCP2_ERR_WRITE_MORE
if (nwrite == -240) // NGTCP2_ERR_WRITE_MORE
{
log::trace(logcat, "Writing non-stream data frames, and have space left");
// debug
@ -903,16 +904,14 @@ namespace llarp::quic
ngtcp2_conn_update_pkt_tx_time(*this, *ts);
continue;
}
if (nwrite == -210) // NGTCP2_ERR_STREAM_DATA_BLOCKED
if (nwrite == -210) // NGTCP2_ERR_STREAM_DATA_BLOCKED
{
log::debug(logcat, "cannot add to empty stream right now: stream is blocked");
fprintf(
stderr,
"cannot add to empty stream right now: stream is blocked\n");
fprintf(stderr, "cannot add to empty stream right now: stream is blocked\n");
ngtcp2_conn_update_pkt_tx_time(*this, *ts);
break;
}
if (nwrite == -230) // NGTCP2_ERR_CLOSING
if (nwrite == -230) // NGTCP2_ERR_CLOSING
{
log::warning(logcat, "Error writing non-stream data: {}", ngtcp2_strerror(nwrite));
fprintf(stderr, "Error writing non-stream data: %s\n", ngtcp2_strerror(nwrite));
@ -922,7 +921,7 @@ namespace llarp::quic
if (nwrite == 0)
{
log::trace(
logcat, "Nothing else to write for non-stream data for now (or we are congested)");
logcat, "Nothing else to write for non-stream data for now (or we are congested)");
// debug
fprintf(
stderr, "Nothing else to write for non-stream data for now (or we are congested)\n");

View file

@ -185,7 +185,7 @@ namespace llarp::quic
"Immediate Close-ing connection {} due to error {}",
conn.base_cid,
ngtcp2_strerror(rv));
//close_connection(conn, rv, "ERR_PROTO"sv);
// close_connection(conn, rv, "ERR_PROTO"sv);
close_connection(conn, ngtcp2_err_infer_quic_transport_error_code(rv), "ERR_PROTO"sv);
}
else if (rv == NGTCP2_ERR_DROP_CONN)
@ -258,8 +258,8 @@ namespace llarp::quic
{
log::debug(logcat, "Closing connection {}", conn.base_cid);
if (!conn || conn.closing || conn.draining)
return;
if (!conn || conn.closing || conn.draining)
return;
ngtcp2_connection_close_error err;
ngtcp2_connection_close_error_set_transport_error_liberr(
@ -273,23 +273,14 @@ namespace llarp::quic
ngtcp2_pkt_info pi;
auto written = ngtcp2_conn_write_connection_close(
conn,
path,
&pi,
u8data(conn.conn_buffer),
conn.conn_buffer.size(),
&err,
get_timestamp());
conn, path, &pi, u8data(conn.conn_buffer), conn.conn_buffer.size(), &err, get_timestamp());
if (written <= 0)
{
log::warning(
logcat,
"Failed to write connection close packet: {}",
written < 0 ? ngtcp2_strerror(written) : "unknown error: closing is 0 bytes??");
log::warning(
logcat,
"Failed to write packet: removing connection {}",
conn.base_cid);
log::warning(logcat, "Failed to write packet: removing connection {}", conn.base_cid);
delete_conn(conn.base_cid);
return;
}
@ -298,7 +289,7 @@ namespace llarp::quic
conn.closing = true;
conn.path = path;
assert(conn.closing && !conn.conn_buffer.empty());
if (auto sent = send_packet(conn.path.remote, conn.conn_buffer, 0); not sent)
@ -355,7 +346,7 @@ namespace llarp::quic
if (cleanup)
clean_alias_conns();
for (auto & it : conns)
for (auto& it : conns)
{
if (auto* conn_ptr = std::get_if<primary_conn_ptr>(&it.second))
{

View file

@ -39,7 +39,7 @@ namespace llarp::quic
null_iv.data(),
&null_cipher_ctx,
null_iv.size());
if (rv != 0)
log::debug(logcat, "Call to ngtcp2_conn_set_initial_crypto_ctx unsuccessful at {}", __LINE__);
@ -61,7 +61,7 @@ namespace llarp::quic
null_iv.data(),
&null_cipher_ctx,
null_iv.size());
if (rv != 0)
log::debug(logcat, "Call to ngtcp2_conn_set_initial_crypto_ctx unsuccessful at {}", __LINE__);
@ -103,13 +103,7 @@ namespace llarp::quic
{
log::debug(logcat, "Calling {}", __PRETTY_FUNCTION__);
return ngtcp2_conn_install_rx_key(
conn,
nullptr,
0,
&null_aead_ctx,
null_iv.data(),
null_iv.size(),
&null_cipher_ctx)
conn, nullptr, 0, &null_aead_ctx, null_iv.data(), null_iv.size(), &null_cipher_ctx)
== 0;
}

View file

@ -272,6 +272,7 @@ namespace llarp::quic
}
auto lokinet_addr = var::visit([](auto&& remote) { return remote.ToString(); }, *remote);
auto tunnel_to = allow_connection(lokinet_addr, port);
if (not tunnel_to)
return false;
@ -694,8 +695,20 @@ namespace llarp::quic
auto ecn = static_cast<uint8_t>(buf.base[3]);
bstring_view data{reinterpret_cast<const std::byte*>(&buf.base[4]), buf.sz - 4};
SockAddr remote{tag.ToV6()};
auto maybe_addr = service_endpoint_.GetEndpointWithConvoTag(tag);
if (not maybe_addr)
{
log::error(logcat, "No remote endpoint found for packet addressing");
return;
}
auto addr_data = var::visit([](auto&& addr) { return addr.as_array(); }, *maybe_addr);
huint128_t ip{};
std::copy_n(addr_data.begin(), sizeof(ip.h), &ip.h);
SockAddr remote{ip};
quic::Endpoint* ep = nullptr;
if (type == CLIENT_TO_SERVER)
{
log::trace(logcat, "packet is client-to-server from client pport {}", pseudo_port);