From 4446f2fc16306229d771567657a1fa2c80651f31 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 12 Mar 2021 08:50:21 -0500 Subject: [PATCH] fix and format. * start moving quic code to use lokinet internals --- llarp/ev/ev_libuv.cpp | 8 ++ llarp/ev/ev_libuv.hpp | 3 + llarp/quic/address.cpp | 45 +++--- llarp/quic/address.hpp | 59 ++++---- llarp/quic/client.cpp | 16 +-- llarp/quic/client.hpp | 6 +- llarp/quic/connection.cpp | 51 ++----- llarp/quic/connection.hpp | 7 +- llarp/quic/endpoint.cpp | 190 +------------------------ llarp/quic/endpoint.hpp | 53 +++---- llarp/quic/server.cpp | 6 +- llarp/quic/server.hpp | 3 +- llarp/quic/stream.cpp | 3 +- llarp/quic/tunnel.cpp | 5 +- llarp/quic/tunnel_client.cpp | 2 + llarp/quic/tunnel_server.cpp | 19 +-- llarp/quic/tunnel_server.hpp | 8 +- llarp/service/convotag.cpp | 30 ++++ llarp/service/convotag.hpp | 35 +++++ llarp/service/handler.hpp | 18 +-- llarp/util/aligned.hpp | 2 +- llarp/util/logging/buffer.cpp | 5 +- llarp/util/logging/buffer.hpp | 8 +- llarp/util/logging/logger.hpp | 3 +- llarp/util/logging/logger_internal.hpp | 6 +- 25 files changed, 212 insertions(+), 379 deletions(-) create mode 100644 llarp/service/convotag.cpp create mode 100644 llarp/service/convotag.hpp diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index 65f5d4734..a00cc5848 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -12,6 +12,14 @@ namespace llarp::uv { + std::shared_ptr + Loop::MaybeGetLoop(const EventLoop_ptr& ptr) + { + if (auto* uv = dynamic_cast(ptr.get())) + return uv->m_Impl; + return nullptr; + } + class UVWakeup final : public EventLoopWakeup { std::shared_ptr async; diff --git a/llarp/ev/ev_libuv.hpp b/llarp/ev/ev_libuv.hpp index 926ec164a..53c01ad8a 100644 --- a/llarp/ev/ev_libuv.hpp +++ b/llarp/ev/ev_libuv.hpp @@ -71,6 +71,9 @@ namespace llarp::uv std::function PumpLL; + static std::shared_ptr + MaybeGetLoop(const EventLoop_ptr&); + bool inEventLoop() const override; diff --git a/llarp/quic/address.cpp b/llarp/quic/address.cpp index fbf793244..4b985e04a 100644 --- a/llarp/quic/address.cpp +++ b/llarp/quic/address.cpp @@ -1,46 +1,39 @@ #include "address.hpp" - -extern "C" -{ -#include -} - +#include +#include #include namespace llarp::quic { using namespace std::literals; - Address::Address(std::array ip, uint16_t port) - { - s.in.sin_family = AF_INET; - std::memcpy(&s.in.sin_addr.s_addr, ip.data(), ip.size()); - s.in.sin_port = htons(port); - a.addrlen = sizeof(s.in); - } + Address::Address(service::ConvoTag tag) : saddr{tag.ToV6()} + {} - Address::Address(const sockaddr_any* addr, size_t addrlen) - { - assert(addrlen == sizeof(sockaddr_in)); // FIXME: IPv6 support - std::memmove(&s, addr, addrlen); - a.addrlen = addrlen; - } Address& - Address::operator=(const Address& addr) + Address::operator=(const Address& other) { - std::memmove(&s, &addr.s, sizeof(s)); - a.addrlen = addr.a.addrlen; + std::memmove(&saddr, &other.saddr, sizeof(saddr)); + a.addrlen = other.a.addrlen; return *this; } + service::ConvoTag + Address::Tag() const + { + service::ConvoTag tag{}; + tag.FromV6(saddr); + return tag; + } + std::string Address::to_string() const { - if (a.addrlen != sizeof(sockaddr_in)) + if (a.addrlen != sizeof(sockaddr_in6)) return "(unknown-addr)"; - char buf[INET_ADDRSTRLEN] = {0}; - inet_ntop(AF_INET, &s.in.sin_addr, buf, INET_ADDRSTRLEN); - return buf + ":"s + std::to_string(ntohs(s.in.sin_port)); + char buf[INET6_ADDRSTRLEN] = {0}; + inet_ntop(AF_INET6, &saddr.sin6_addr, buf, INET6_ADDRSTRLEN); + return buf; } std::ostream& diff --git a/llarp/quic/address.hpp b/llarp/quic/address.hpp index 1ca8c55b0..910c95e19 100644 --- a/llarp/quic/address.hpp +++ b/llarp/quic/address.hpp @@ -3,18 +3,14 @@ #include #include #include +#include #include #include #include -extern "C" -{ -#include -#include -} - -// FIXME: replace use of this with a llarp::SockAddr +#include +#include namespace llarp::quic { @@ -28,44 +24,50 @@ namespace llarp::quic class Address { - sockaddr_any s{}; - ngtcp2_addr a{0, &s.sa, nullptr}; + sockaddr_in6 saddr{}; + ngtcp2_addr a{0, reinterpret_cast(&saddr), nullptr}; public: Address() = default; - Address(std::array ip, uint16_t port); - Address(const sockaddr_any* addr, size_t addrlen); - Address(const Address& addr) + Address(service::ConvoTag tag); + + Address(const Address& other) { - *this = addr; + *this = other; } + + service::ConvoTag + Tag() const; + Address& - operator=(const Address& addr); + operator=(const Address&); - // Implicit conversion to sockaddr* and ngtcp2_addr& so that an Address can be passed wherever - // one of those is expected. operator sockaddr*() { - return a.addr; + return reinterpret_cast(&saddr); } + operator const sockaddr*() const { - return a.addr; - } - constexpr socklen_t - sockaddr_size() const - { - return a.addrlen; + return reinterpret_cast(&saddr); } + operator ngtcp2_addr&() { return a; } + operator const ngtcp2_addr&() const { return a; } + size_t + sockaddr_size() const + { + return sizeof(sockaddr_in6); + } + std::string to_string() const; }; @@ -76,7 +78,7 @@ namespace llarp::quic struct Path { private: - Address local_{}, remote_{}; + Address local_, remote_; public: ngtcp2_path path{ @@ -87,12 +89,10 @@ namespace llarp::quic const Address& remote = remote_; Path() = default; - Path(const Address& local, const Address& remote) : local_{local}, remote_{remote} - {} - Path(const Address& local, const sockaddr_any* remote_addr, size_t remote_len) - : local_{local}, remote_{remote_addr, remote_len} + Path(const Address& laddr, const Address& raddr) : local_{laddr}, remote_{raddr} {} - Path(const Path& p) : local_{p.local_}, remote_{p.remote_} + + Path(const Path& p) : Path{p.local, p.remote} {} Path& @@ -120,6 +120,7 @@ namespace llarp::quic std::ostream& operator<<(std::ostream& o, const Address& a); + std::ostream& operator<<(std::ostream& o, const Path& p); diff --git a/llarp/quic/client.cpp b/llarp/quic/client.cpp index cf5e2006a..933774591 100644 --- a/llarp/quic/client.cpp +++ b/llarp/quic/client.cpp @@ -4,17 +4,18 @@ #include #include +#include +#include +#include namespace llarp::quic { - Client::Client( - Address remote, - std::shared_ptr loop_, - uint16_t tunnel_port, - std::optional
local_) - : Endpoint{std::move(local_), std::move(loop_)} + Client::Client(service::ConvoTag tag, service::Endpoint* parent, uint16_t tunnel_port) + : Endpoint{parent, uv::Loop::MaybeGetLoop(parent->Loop())} { // Our UDP socket is now set up, so now we initiate contact with the remote QUIC + Address remote{std::move(tag)}; + Path path{local, remote}; llarp::LogDebug("Connecting to ", remote); @@ -32,8 +33,7 @@ namespace llarp::quic // // - delay_stream_timer - auto connptr = - std::make_shared(*this, ConnectionID::random(), path, tunnel_port); + auto connptr = std::make_shared(*this, ConnectionID::random(), path, tunnel_port); auto& conn = *connptr; conns.emplace(conn.base_cid, connptr); diff --git a/llarp/quic/client.hpp b/llarp/quic/client.hpp index a7e44b36a..6cca4a55d 100644 --- a/llarp/quic/client.hpp +++ b/llarp/quic/client.hpp @@ -12,11 +12,7 @@ namespace llarp::quic // Constructs a client that establishes an outgoing connection to `remote` to tunnel packets to // `tunnel_port` on the remote's lokinet address. `local` can be used to optionally bind to a // local IP and/or port for the connection. - Client( - Address remote, - std::shared_ptr loop, - uint16_t tunnel_port, - std::optional
local = std::nullopt); + Client(service::ConvoTag remote, service::Endpoint* parent, uint16_t tunnel_port); // Returns a reference to the client's connection to the server. Returns a nullptr if there is // no connection. diff --git a/llarp/quic/connection.cpp b/llarp/quic/connection.cpp index cba7edcfd..d8d40504f 100644 --- a/llarp/quic/connection.cpp +++ b/llarp/quic/connection.cpp @@ -17,7 +17,8 @@ #include #include -extern "C" { +extern "C" +{ #include } @@ -36,11 +37,13 @@ namespace llarp::quic return o << oxenmq::to_hex(c.data, c.data + c.datalen); } - ConnectionID ConnectionID::random(size_t size) { - ConnectionID r; - r.datalen = std::min(size, ConnectionID::max_size()); - randombytes_buf(r.data, r.datalen); - return r; + ConnectionID + ConnectionID::random(size_t size) + { + ConnectionID r; + r.datalen = std::min(size, ConnectionID::max_size()); + randombytes_buf(r.data, r.datalen); + return r; } namespace @@ -322,7 +325,8 @@ namespace llarp::quic { va_list ap; va_start(ap, fmt); - if (char* msg; vasprintf(&msg, sizeof(ngtcp_debug_out), fmt, ap) >= 0) { + if (char* msg; vasprintf(&msg, sizeof(ngtcp_debug_out), fmt, ap) >= 0) + { LogTraceExplicit("external/ngtcp2/*.c", 0, msg); std::free(msg); } @@ -341,33 +345,8 @@ namespace llarp::quic { LogDebug("Sending packet: ", buffer_printer{send_data}); rv = endpoint.send_packet(path.remote, send_data, send_pkt_info.ecn); - if (rv.blocked()) - { - if (!wpoll) - { - wpoll = endpoint.loop->resource(endpoint.socket_fd()); - wpoll->on([this](const auto&, auto&) { send(); }); - } - if (!wpoll_active) - { - wpoll->start(uvw::PollHandle::Event::WRITABLE); - wpoll_active = true; - } - } - else if (!rv) - { - // FIXME: disconnect here? - LogWarn("packet send failed: ", rv.str()); - LogError("FIXME - should disconnect"); - } - else if (wpoll_active) - { - wpoll->stop(); - wpoll_active = false; - } } return rv; - // We succeeded // // FIXME2: probably don't want to do these things *here*, because this is called from the stream @@ -546,8 +525,6 @@ namespace llarp::quic Connection::~Connection() { - if (wpoll) - wpoll->close(); if (io_trigger) io_trigger->close(); } @@ -581,7 +558,8 @@ namespace llarp::quic if (!ts) ts = get_timestamp(); - LogTrace("send_buffer size=", send_buffer.size(), ", datalen=", datalen, ", flags=", flags); + LogTrace( + "send_buffer size=", send_buffer.size(), ", datalen=", datalen, ", flags=", flags); nwrite = ngtcp2_conn_writev_stream( conn.get(), &path.path, @@ -1134,7 +1112,8 @@ namespace llarp::quic if (rv == 0) { rv = ngtcp2_conn_set_remote_transport_params(*this, ¶ms); - LogDebug("Set remote transport params ", rv == 0 ? "success" : "fail: "s + ngtcp2_strerror(rv)); + LogDebug( + "Set remote transport params ", rv == 0 ? "success" : "fail: "s + ngtcp2_strerror(rv)); } if (rv != 0) diff --git a/llarp/quic/connection.hpp b/llarp/quic/connection.hpp index 480f9f2d8..7f414c8f8 100644 --- a/llarp/quic/connection.hpp +++ b/llarp/quic/connection.hpp @@ -12,7 +12,8 @@ #include #include -extern "C" { +extern "C" +{ #include #include } @@ -133,10 +134,6 @@ namespace llarp::quic io_result send(); - // Poll for writability; activated if we block while trying to send a packet. - std::shared_ptr wpoll; - bool wpoll_active = false; - // Internal base method called invoked during construction to set up common client/server // settings. dest_cid and path must already be set. std::tuple diff --git a/llarp/quic/endpoint.cpp b/llarp/quic/endpoint.cpp index 4f2f83b85..43ea0ad4a 100644 --- a/llarp/quic/endpoint.cpp +++ b/llarp/quic/endpoint.cpp @@ -1,8 +1,10 @@ #include "endpoint.hpp" #include "client.hpp" #include "server.hpp" +#include "uvw/async.h" #include #include +#include #include #include @@ -19,69 +21,11 @@ extern "C" namespace llarp::quic { - Endpoint::Endpoint(std::optional
addr, std::shared_ptr loop_) - : loop{std::move(loop_)} + Endpoint::Endpoint(service::Endpoint* parent_, std::shared_ptr loop_) + : parent{parent_}, loop{std::move(loop_)} { randombytes_buf(static_secret.data(), static_secret.size()); - // Create and bind the UDP socket. We can't use libuv's UDP socket here because it doesn't - // give us the ability to set up the ECN field as QUIC requires. - auto fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); - if (fd == -1) - throw std::runtime_error{"Failed to open socket: "s + strerror(errno)}; - - if (addr) - { - assert(addr->sockaddr_size() == sizeof(sockaddr_in)); // FIXME: IPv4-only for now - auto rv = bind(fd, *addr, addr->sockaddr_size()); - if (rv == -1) - throw std::runtime_error{ - "Failed to bind UDP socket to " + addr->to_string() + ": " + strerror(errno)}; - } - - // Get our address via the socket in case `addr` is using anyaddr/anyport. - sockaddr_any sa; - socklen_t salen = sizeof(sa); - // FIXME: if I didn't call bind above then do I need to call bind() before this (with - // anyaddr/anyport)? - getsockname(fd, &sa.sa, &salen); - assert(salen == sizeof(sockaddr_in)); // FIXME: IPv4-only for now - local = {&sa, salen}; - LogDebug("Bound to ", local, addr ? "" : " (auto-selected)"); - - // Set up the socket to provide us with incoming ECN (IP_TOS) info - // NB: This is for IPv4; on AF_INET6 this would be IPPROTO_IPV6, IPV6_RECVTCLASS - if (uint8_t want_tos = 1; - - 1 - == setsockopt( - fd, IPPROTO_IP, IP_RECVTOS, &want_tos, static_cast(sizeof(want_tos)))) - throw std::runtime_error{"Failed to set ECN on socket: "s + strerror(errno)}; - - // Wire up our recv buffer structures into what recvmmsg() wants - buf.resize(max_buf_size * msgs.size()); - for (size_t i = 0; i < msgs.size(); i++) - { - auto& iov = msgs_iov[i]; - iov.iov_base = buf.data() + max_buf_size * i; - iov.iov_len = max_buf_size; -#ifdef LOKINET_HAVE_RECVMMSG - auto& mh = msgs[i].msg_hdr; -#else - auto& mh = msgs[i]; -#endif - mh.msg_name = &msgs_addr[i]; - mh.msg_namelen = sizeof(msgs_addr[i]); - mh.msg_iov = &iov; - mh.msg_iovlen = 1; - mh.msg_control = msgs_cmsg[i].data(); - mh.msg_controllen = msgs_cmsg[i].size(); - } - - // Let uv do its stuff - poll = loop->resource(fd); - poll->on([this](const auto&, auto&) { on_readable(); }); - poll->start(uvw::PollHandle::Event::READABLE); - // Set up a callback every 250ms to clean up stale sockets, etc. expiry_timer = loop->resource(); expiry_timer->on([this](const auto&, auto&) { check_timeouts(); }); @@ -92,101 +36,10 @@ namespace llarp::quic Endpoint::~Endpoint() { - if (poll) - poll->close(); if (expiry_timer) expiry_timer->close(); } - int - Endpoint::socket_fd() const - { - return poll->fd(); - } - - void - Endpoint::on_readable() - { - LogDebug("poll callback on readable"); - -#ifdef LOKINET_HAVE_RECVMMSG - // NB: recvmmsg is linux-specific but ought to offer some performance benefits - int n_msg = recvmmsg(socket_fd(), msgs.data(), msgs.size(), 0, nullptr); - if (n_msg == -1) - { - if (errno != EAGAIN && errno != ENOTCONN) - LogWarn("Error recv'ing from ", local.to_string(), ": ", strerror(errno)); - return; - } - - LogDebug("Recv'd ", n_msg, " messages"); - for (int i = 0; i < n_msg; i++) - { - auto& [msg_hdr, msg_len] = msgs[i]; - bstring_view data{buf.data() + i * max_buf_size, msg_len}; -#else - for (size_t i = 0; i < N_msgs; i++) - { - auto& msg_hdr = msgs[0]; - auto n_bytes = recvmsg(socket_fd(), &msg_hdr, 0); - if (n_bytes == -1 && errno != EAGAIN && errno != ENOTCONN) - LogWarn("Error recv'ing from ", local.to_string(), ": ", strerror(errno)); - if (n_bytes <= 0) - return; - auto msg_len = static_cast(n_bytes); - bstring_view data{buf.data(), msg_len}; -#endif - - LogDebug( - "header [", - msg_hdr.msg_namelen, - "]: ", - buffer_printer{reinterpret_cast(msg_hdr.msg_name), msg_hdr.msg_namelen}); - - if (!msg_hdr.msg_name || msg_hdr.msg_namelen != sizeof(sockaddr_in)) - { // FIXME: IPv6 support? - LogWarn("Invalid/unknown source address, dropping packet"); - continue; - } - - Packet pkt{ - Path{local, reinterpret_cast(msg_hdr.msg_name), msg_hdr.msg_namelen}, - data, - ngtcp2_pkt_info{.ecn = 0}}; - - // Go look for the ECN header field on the incoming packet - for (auto cmsg = CMSG_FIRSTHDR(&msg_hdr); cmsg; cmsg = CMSG_NXTHDR(&msg_hdr, cmsg)) - { - // IPv4; for IPv6 these would be IPPROTO_IPV6 and IPV6_TCLASS - if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_TOS && cmsg->cmsg_len) - { - pkt.info.ecn = *reinterpret_cast(CMSG_DATA(cmsg)); - } - } - - LogDebug( - i, - "[", - pkt.path, - ",ecn=0x", - std::hex, - +pkt.info.ecn, - std::dec, - "]: received ", - msg_len, - " bytes"); - - handle_packet(pkt); - - LogDebug("Done handling packet"); - -#ifdef LOKINET_HAVE_RECVMMSG // Help editor's { } matching: - } -#else - } -#endif - } - std::optional Endpoint::handle_packet_init(const Packet& p) { @@ -272,15 +125,6 @@ namespace llarp::quic assert(ecn <= std::numeric_limits::max()); if (ecn_curr != ecn) { - if (-1 - == setsockopt(socket_fd(), IPPROTO_IP, IP_TOS, &ecn, static_cast(sizeof(ecn)))) - LogWarn("setsockopt failed to set IP_TOS: ", strerror(errno)); - - // IPv6 version: - // int tclass = this->ecn; - // setsockopt(socket_fd(), IPPROTO_IPV6, IPV6_TCLASS, &tclass, - // static_cast(sizeof(tclass))); - ecn_curr = ecn; } } @@ -288,31 +132,9 @@ namespace llarp::quic io_result Endpoint::send_packet(const Address& to, bstring_view data, uint32_t ecn) { - iovec msg_iov; - msg_iov.iov_base = const_cast(data.data()); - msg_iov.iov_len = data.size(); - - msghdr msg{}; - msg.msg_name = &const_cast(reinterpret_cast(to)); - msg.msg_namelen = sizeof(sockaddr_in); - msg.msg_iov = &msg_iov; - msg.msg_iovlen = 1; - - auto fd = socket_fd(); - update_ecn(ecn); - ssize_t nwrite = 0; - do - { - nwrite = sendmsg(fd, &msg, 0); - } while (nwrite == -1 && errno == EINTR); - - if (nwrite == -1) - { - LogWarn("sendmsg failed: ", strerror(errno)); - return {errno}; - } + parent->SendTo(to.Tag(), data, service::ProtocolType::QUIC); LogDebug( "[", to.to_string(), @@ -321,7 +143,7 @@ namespace llarp::quic +ecn_curr, std::dec, "]: sent ", - nwrite, + data.size(), " bytes"); return {}; } diff --git a/llarp/quic/endpoint.hpp b/llarp/quic/endpoint.hpp index ba9174a4d..5e82d9782 100644 --- a/llarp/quic/endpoint.hpp +++ b/llarp/quic/endpoint.hpp @@ -6,23 +6,31 @@ #include "null_crypto.hpp" #include "packet.hpp" #include "stream.hpp" +#include "uvw/async.h" #include #include #include #include #include -#include + #include #include #include #include -// True if we support recvmmsg/sendmmsg -#if defined(__linux__) && !defined(LOKINET_NO_RECVMMSG) -#define LOKINET_HAVE_RECVMMSG -#endif +#include + +namespace llarp::service +{ + struct Endpoint; +} // namespace llarp::service + +namespace llarp::net +{ + struct IPPacket; +} namespace llarp::quic { @@ -33,29 +41,17 @@ namespace llarp::quic class Endpoint { protected: - // Address we are listening on - Address local; + /// the service endpoint we are owned by + service::Endpoint* const parent; // The current outgoing IP ecn value for the socket uint8_t ecn_curr = 0; - std::shared_ptr poll; + /// local "address" just a blank + Address local{}; + std::shared_ptr expiry_timer; std::shared_ptr loop; - // How many messages (at most) we recv per callback: - static constexpr int N_msgs = 8; -#ifdef LOKINET_HAVE_RECVMMSG - static constexpr int N_mmsg = N_msgs; - std::array msgs; -#else - static constexpr int N_mmsg = 1; - std::array msgs; -#endif - - std::array msgs_iov; - std::array msgs_addr; - std::array, N_mmsg> msgs_cmsg; - std::vector buf; // Max theoretical size of a UDP packet is 2^16-1 minus IP/UDP header overhead static constexpr size_t max_buf_size = 64 * 1024; // Max size of a UDP packet that we'll send @@ -101,21 +97,10 @@ namespace llarp::quic friend class Connection; // Wires up an endpoint connection. - // - // `bind` - address we should bind to. Required for a server, optional for a client. If - // omitted, no explicit bind is performed (which means the socket will be implicitly bound to - // some OS-determined random high bind port). - // `loop` - the uv loop pointer managing polling of this endpoint - Endpoint(std::optional
bind, std::shared_ptr loop); + Endpoint(service::Endpoint* ep, std::shared_ptr loop); virtual ~Endpoint(); - int - socket_fd() const; - - void - on_readable(); - // Version & connection id info that we can potentially extract when decoding a packet struct version_info { diff --git a/llarp/quic/server.cpp b/llarp/quic/server.cpp index 198f89e7b..cad32790c 100644 --- a/llarp/quic/server.cpp +++ b/llarp/quic/server.cpp @@ -13,8 +13,10 @@ namespace llarp::quic { Server::Server( - Address listen, std::shared_ptr loop, stream_open_callback_t stream_open) - : Endpoint{std::move(listen), std::move(loop)}, stream_open_callback{std::move(stream_open)} + service::Endpoint* parent, + std::shared_ptr loop, + stream_open_callback_t stream_open) + : Endpoint{parent, std::move(loop)}, stream_open_callback{std::move(stream_open)} {} void diff --git a/llarp/quic/server.hpp b/llarp/quic/server.hpp index e6973ed55..192809420 100644 --- a/llarp/quic/server.hpp +++ b/llarp/quic/server.hpp @@ -12,7 +12,8 @@ namespace llarp::quic using stream_open_callback_t = std::function; - Server(Address listen, std::shared_ptr loop, stream_open_callback_t stream_opened); + Server( + service::Endpoint*, std::shared_ptr loop, stream_open_callback_t stream_opened); // Stream callback: takes the server, the (just-created) stream, and the connection port. // Returns true if the stream should be allowed or false to reject the stream. The callback diff --git a/llarp/quic/stream.cpp b/llarp/quic/stream.cpp index 221df9f16..166f38dc4 100644 --- a/llarp/quic/stream.cpp +++ b/llarp/quic/stream.cpp @@ -125,7 +125,8 @@ namespace llarp::quic { // No wrap needs, it fits before the end: std::copy(data.begin(), data.end(), buffer.begin() + wpos); - LogTrace("Wrote ", data.size(), " bytes to buffer range [", wpos, ",", wpos + data.size(), ")"); + LogTrace( + "Wrote ", data.size(), " bytes to buffer range [", wpos, ",", wpos + data.size(), ")"); } size += data.size(); LogTrace("New stream buffer: ", size, "/", buffer.size(), " bytes beginning at ", start); diff --git a/llarp/quic/tunnel.cpp b/llarp/quic/tunnel.cpp index 579a91ce7..d41bb21c1 100644 --- a/llarp/quic/tunnel.cpp +++ b/llarp/quic/tunnel.cpp @@ -84,8 +84,7 @@ namespace llarp::quic::tunnel }); tcp.on([](auto&, uvw::TCPHandle& c) { // This fires on eof, most likely because the other side of the TCP connection closed it. - LogError( - "EOF on connection with ", c.peer().ip, ":", c.peer().port, ", closing quic stream"); + LogError("EOF on connection with ", c.peer().ip, ":", c.peer().port, ", closing quic stream"); c.data()->close(); }); tcp.on([](const uvw::ErrorEvent& e, uvw::TCPHandle& tcp) { @@ -109,4 +108,4 @@ namespace llarp::quic::tunnel stream.data_callback = on_incoming_data; } -} // namespace tunnel +} // namespace llarp::quic::tunnel diff --git a/llarp/quic/tunnel_client.cpp b/llarp/quic/tunnel_client.cpp index 5b3d053cb..4ae894e75 100644 --- a/llarp/quic/tunnel_client.cpp +++ b/llarp/quic/tunnel_client.cpp @@ -13,6 +13,7 @@ #include +/* using namespace std::literals; namespace llarp::quic::tunnel @@ -139,3 +140,4 @@ namespace llarp::quic::tunnel } } // namespace llarp::quic::tunnel +*/ diff --git a/llarp/quic/tunnel_server.cpp b/llarp/quic/tunnel_server.cpp index c750391d4..f8c03e16f 100644 --- a/llarp/quic/tunnel_server.cpp +++ b/llarp/quic/tunnel_server.cpp @@ -8,6 +8,7 @@ #include +/* using namespace std::literals; namespace llarp::quic::tunnel @@ -75,13 +76,7 @@ namespace llarp::quic::tunnel loop, [loop, localhost, allowed_ports]( llarp::quic::Server&, llarp::quic::Stream& stream, uint16_t port) { - LogDebug( - "New incoming quic stream ", - stream.id(), - " to reach ", - localhost, - ":", - port); + LogDebug("New incoming quic stream ", stream.id(), " to reach ", localhost, ":", port); if (port == 0 || !(allowed_ports.empty() || allowed_ports.count(port))) { LogWarn( @@ -129,13 +124,7 @@ namespace llarp::quic::tunnel tcp.closeReset(); return; } - LogDebug( - "Connected to ", - peer.ip, - ":", - peer.port, - " for quic ", - stream->id()); + LogDebug("Connected to ", peer.ip, ":", peer.port, " for quic ", stream->id()); tcp.erase(error_handler); tunnel::install_stream_forwarding(tcp, *stream); assert(stream->used() == 0); @@ -165,3 +154,5 @@ namespace llarp::quic::tunnel } } // namespace llarp::quic::tunnel + +*/ diff --git a/llarp/quic/tunnel_server.hpp b/llarp/quic/tunnel_server.hpp index 2a2ab78d4..67dac4146 100644 --- a/llarp/quic/tunnel_server.hpp +++ b/llarp/quic/tunnel_server.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include "address.hpp" +#include +#include #include @@ -21,7 +21,7 @@ namespace llarp::quic::tunnel { public: using AcceptCallback = std::function; + const Address& remote, uint16_t port, llarp::SockAddr& connect_to)>; private: AcceptCallback accept; diff --git a/llarp/service/convotag.cpp b/llarp/service/convotag.cpp new file mode 100644 index 000000000..1da3cecc1 --- /dev/null +++ b/llarp/service/convotag.cpp @@ -0,0 +1,30 @@ +#include "convotag.hpp" +#include "net/ip.hpp" + +namespace llarp::service +{ + void + ConvoTag::Randomize() + { + llarp::AlignedBuffer<16>::Randomize(); + /// ensure we are in the fc00 range + llarp::AlignedBuffer<16>::operator[](0) = 0xfc; + } + + sockaddr_in6 + ConvoTag::ToV6() const + { + const auto* ptr = reinterpret_cast(m_data.data()); + sockaddr_in6 saddr{}; + saddr.sin6_family = AF_INET6; + saddr.sin6_addr = net::HUIntToIn6(huint128_t{ptr[0], ptr[1]}); + return saddr; + } + + void + ConvoTag::FromV6(sockaddr_in6 saddr) + { + std::copy_n(saddr.sin6_addr.s6_addr, m_data.size(), m_data.data()); + } + +} // namespace llarp::service diff --git a/llarp/service/convotag.hpp b/llarp/service/convotag.hpp new file mode 100644 index 000000000..3c77cecb6 --- /dev/null +++ b/llarp/service/convotag.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include + +namespace llarp::service +{ + struct ConvoTag final : AlignedBuffer<16> + { + void + Randomize() override; + + sockaddr_in6 + ToV6() const; + + void + FromV6(sockaddr_in6 saddr); + }; +} // namespace llarp::service + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::service::ConvoTag& tag) const + { + std::hash h{}; + return h(std::string_view{reinterpret_cast(tag.data()), tag.size()}); + } + }; +} // namespace std diff --git a/llarp/service/handler.hpp b/llarp/service/handler.hpp index d4d39370d..b2683940e 100644 --- a/llarp/service/handler.hpp +++ b/llarp/service/handler.hpp @@ -3,7 +3,7 @@ #include #include #include "intro_set.hpp" -#include +#include "convotag.hpp" #include #include @@ -12,8 +12,6 @@ namespace llarp { namespace service { - struct ConvoTag final : AlignedBuffer<16> - {}; struct ProtocolMessage; struct RecvDataEvent @@ -80,17 +78,3 @@ namespace llarp }; } // namespace service } // namespace llarp - -namespace std -{ - template <> - struct hash - { - size_t - operator()(const llarp::service::ConvoTag& tag) const - { - std::hash h{}; - return h(std::string_view{reinterpret_cast(tag.data()), tag.size()}); - } - }; -} // namespace std diff --git a/llarp/util/aligned.hpp b/llarp/util/aligned.hpp index f2507e1ba..fd61879b7 100644 --- a/llarp/util/aligned.hpp +++ b/llarp/util/aligned.hpp @@ -200,7 +200,7 @@ namespace llarp m_data.fill(0); } - void + virtual void Randomize() { randombytes(data(), SIZE); diff --git a/llarp/util/logging/buffer.cpp b/llarp/util/logging/buffer.cpp index 59a75d771..284bf8633 100644 --- a/llarp/util/logging/buffer.cpp +++ b/llarp/util/logging/buffer.cpp @@ -2,7 +2,8 @@ #include #include -namespace llarp { +namespace llarp +{ std::ostream& operator<<(std::ostream& o, const buffer_printer& bp) { @@ -42,4 +43,4 @@ namespace llarp { o.fill(oldfill); return o; } -} +} // namespace llarp diff --git a/llarp/util/logging/buffer.hpp b/llarp/util/logging/buffer.hpp index 362714b7e..e9cc2db75 100644 --- a/llarp/util/logging/buffer.hpp +++ b/llarp/util/logging/buffer.hpp @@ -26,13 +26,15 @@ namespace llarp : buf{reinterpret_cast(buf.data()), buf.size()} {} - // Constructed from any type of lvalue string for a single-byte T (char, std::byte, uint8_t, etc.) + // Constructed from any type of lvalue string for a single-byte T (char, std::byte, uint8_t, + // etc.) template > explicit buffer_printer(const std::basic_string& buf) : buffer_printer(std::basic_string_view{buf}) {} - // *Not* constructable from a string rvalue (because we only hold a view and do not take ownership). + // *Not* constructable from a string rvalue (because we only hold a view and do not take + // ownership). template > explicit buffer_printer(std::basic_string&& buf) = delete; @@ -49,4 +51,4 @@ namespace llarp }; std::ostream& operator<<(std::ostream& o, const buffer_printer& bp); -} +} // namespace llarp diff --git a/llarp/util/logging/logger.hpp b/llarp/util/logging/logger.hpp index 41ea618d3..6effe12cf 100644 --- a/llarp/util/logging/logger.hpp +++ b/llarp/util/logging/logger.hpp @@ -93,7 +93,8 @@ namespace llarp log.logStream->AppendLog(lvl, fname, lineno, log.nodeName, ss.str()); } - inline void _log_noop() noexcept + inline void + _log_noop() noexcept {} } // namespace llarp diff --git a/llarp/util/logging/logger_internal.hpp b/llarp/util/logging/logger_internal.hpp index 002f8d4b5..64684f8be 100644 --- a/llarp/util/logging/logger_internal.hpp +++ b/llarp/util/logging/logger_internal.hpp @@ -23,11 +23,11 @@ namespace llarp // print numeric values rather than std::ostream's default of printing it as a raw char. using PlainT = std::remove_reference_t; if constexpr (is_same_any_v) - ss << +std::forward(arg); // Promote to int + ss << +std::forward(arg); // Promote to int else - ss << std::forward(arg); + ss << std::forward(arg); if constexpr (sizeof...(TArgs)) - LogAppend(ss, std::forward(args)...); + LogAppend(ss, std::forward(args)...); } inline std::string