diff --git a/daemon/main.cpp b/daemon/main.cpp index e3e393581..ff70cb3e9 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -51,7 +51,7 @@ handle_signal_win32(DWORD fdwCtrlType) { UNREFERENCED_PARAMETER(fdwCtrlType); handle_signal(SIGINT); - return TRUE; // probably unreachable + return TRUE; // probably unreachable } #endif diff --git a/include/tl/optional.hpp b/include/tl/optional.hpp index ddde2317f..8cce87d3d 100644 --- a/include/tl/optional.hpp +++ b/include/tl/optional.hpp @@ -343,8 +343,8 @@ namespace tl using enable_assign_forward = detail::enable_if_t< !std::is_same< optional< T >, detail::decay_t< U > >::value && !detail::conjunction< - std::is_scalar< T >, - std::is_same< T, detail::decay_t< U > > >::value + std::is_scalar< T >, + std::is_same< T, detail::decay_t< U > > >::value && std::is_constructible< T, U >::value && std::is_assignable< T &, U >::value >; @@ -465,7 +465,7 @@ namespace tl 0))::value &&detail::swap_adl_tests:: is_std_swap_noexcept< T >::value) || (!decltype(detail::swap_adl_tests::uses_std< T, U >( - 0))::value &&detail::swap_adl_tests:: + 0))::value &&detail::swap_adl_tests:: is_adl_swap_noexcept< T, U >::value)) > { }; diff --git a/llarp/aligned.hpp b/llarp/aligned.hpp index 2a5c8f56b..968ad9664 100644 --- a/llarp/aligned.hpp +++ b/llarp/aligned.hpp @@ -257,9 +257,9 @@ namespace llarp }; private: - using AlignedStorage = - typename std::aligned_storage< sizeof(Data), - alignof(uint64_t) >::type; // why did we align to the nearest double-precision float + using AlignedStorage = typename std::aligned_storage< sizeof(Data), + alignof(uint64_t) >:: + type; // why did we align to the nearest double-precision float AlignedStorage val; }; diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index c711a123f..cec9c3985 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -669,7 +669,8 @@ namespace llarp "cannot handle exploritory router lookup, no dht peers"); return false; } - llarp::LogDebug("We have ", nodes->Size(), " connected nodes into the DHT"); + llarp::LogDebug("We have ", nodes->Size(), + " connected nodes into the DHT"); // ourKey should never be in the connected list // requester is likely in the connected list // 4 or connection nodes (minus a potential requestor), whatever is less diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 47750bbe9..b941b4782 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -165,7 +165,7 @@ llarp_ev_add_tun(llarp_ev_loop *loop, llarp_tun_io *tun) auto dev = new win32_tun_io(tun); tun->impl = dev; // We're not even going to add this to the socket event loop - if (dev) + if(dev) { dev->setup(); return dev->add_ev(); // start up tun and add to event queue @@ -194,7 +194,8 @@ llarp_ev_tun_async_write(struct llarp_tun_io *tun, llarp_buffer_t buf) llarp::LogWarn("packet too big, ", buf.sz, " > ", EV_WRITE_BUF_SZ); return false; } - return static_cast< win32_tun_io * >(tun->impl)->queue_write(buf.base, buf.sz); + return static_cast< win32_tun_io * >(tun->impl)->queue_write(buf.base, + buf.sz); } #endif diff --git a/llarp/ev.hpp b/llarp/ev.hpp index 969651777..772a15fa4 100644 --- a/llarp/ev.hpp +++ b/llarp/ev.hpp @@ -121,13 +121,17 @@ namespace llarp using LosslessWriteQueue_t = std::deque< WriteBuffer >; - intptr_t fd; // Sockets only, fuck UNIX-style reactive IO with a rusty knife + intptr_t + fd; // Sockets only, fuck UNIX-style reactive IO with a rusty knife - int flags = 0; + int flags = 0; win32_ev_io(intptr_t f) : fd(f){}; /// for tcp - win32_ev_io(intptr_t f, LosslessWriteQueue_t* q) : fd(f), m_BlockingWriteQueue(q){} + win32_ev_io(intptr_t f, LosslessWriteQueue_t* q) + : fd(f), m_BlockingWriteQueue(q) + { + } virtual void error() @@ -189,7 +193,7 @@ namespace llarp virtual void flush_write_buffers(size_t amount) { - if(m_BlockingWriteQueue) + if(m_BlockingWriteQueue) { if(amount) { diff --git a/llarp/ev_win32.hpp b/llarp/ev_win32.hpp index d4bd89b63..2a6b068f1 100644 --- a/llarp/ev_win32.hpp +++ b/llarp/ev_win32.hpp @@ -57,7 +57,7 @@ struct win32_tun_io device* tunif; byte_t readbuf[EV_READ_BUF_SZ] = {0}; -struct WriteBuffer + struct WriteBuffer { llarp_time_t timestamp = 0; size_t bufsz; @@ -128,12 +128,14 @@ struct WriteBuffer WriteBuffer::GetNow, llarp::util::NullMutex, llarp::util::NullLock, 5, 100, 128 >; - std::unique_ptr m_LossyWriteQueue; + std::unique_ptr< LossyWriteQueue_t > m_LossyWriteQueue; win32_tun_io(llarp_tun_io* tio) : t(tio), tunif(tuntap_init()) { - // This is not your normal everyday event loop, this is _advanced_ event handling :> - m_LossyWriteQueue = std::make_unique("win32_tun_queue", nullptr, nullptr); + // This is not your normal everyday event loop, this is _advanced_ event + // handling :> + m_LossyWriteQueue = std::make_unique< LossyWriteQueue_t >("win32_tun_queue", + nullptr, nullptr); }; bool @@ -276,7 +278,7 @@ tun_ev_loop(void* unused) win32_tun_io* ev = reinterpret_cast< win32_tun_io* >(listener); if(!pkt->write) { - //llarp::LogInfo("read tun ", size, " bytes, pass to handler"); + // llarp::LogInfo("read tun ", size, " bytes, pass to handler"); if(ev->t->recvpkt) ev->t->recvpkt(ev->t, llarp::InitBuffer(pkt->buf, size)); ev->flush_write(); @@ -765,7 +767,7 @@ struct llarp_win32_loop : public llarp_ev_loop void stop() { - // do nothing + // do nothing } }; diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index f9922cafd..62071ec90 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -9,7 +9,8 @@ namespace llarp Endpoint::Endpoint(const llarp::PubKey& remoteIdent, const llarp::PathID_t& beginPath, bool rewriteIP, huint32_t ip, llarp::handlers::ExitEndpoint* parent) - : m_Parent(parent) + : createdAt(parent->Now()) + , m_Parent(parent) , m_remoteSignKey(remoteIdent) , m_CurrentPath(beginPath) , m_IP(ip) @@ -73,9 +74,7 @@ namespace llarp { if(ExpiresSoon(now, timeout)) return true; - if(now > m_LastActive) - return now - m_LastActive > timeout; - return true; + return now > m_LastActive && now - m_LastActive > timeout; } bool diff --git a/llarp/exit/endpoint.hpp b/llarp/exit/endpoint.hpp index 10822bfec..aa26a4da1 100644 --- a/llarp/exit/endpoint.hpp +++ b/llarp/exit/endpoint.hpp @@ -97,6 +97,8 @@ namespace llarp return m_IP; } + const llarp_time_t createdAt; + private: llarp::handlers::ExitEndpoint* m_Parent; llarp::PubKey m_remoteSignKey; diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 3f56264d7..0adb514eb 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -132,7 +132,6 @@ namespace llarp void ExitEndpoint::Flush() { - auto now = Now(); m_InetToNetwork.Process([&](Pkt_t &pkt) { llarp::PubKey pk; { @@ -160,20 +159,7 @@ namespace llarp return; } } - llarp::exit::Endpoint *ep = nullptr; - auto range = m_ActiveExits.equal_range(pk); - auto itr = range.first; - uint64_t min = std::numeric_limits< uint64_t >::max(); - /// pick non dead looking path with lowest tx rate - while(itr != range.second) - { - if(itr->second->TxRate() < min && !itr->second->LooksDead(now)) - { - ep = itr->second.get(); - min = ep->TxRate(); - } - ++itr; - } + llarp::exit::Endpoint *ep = m_ChosenExits[pk]; if(ep == nullptr) { @@ -427,8 +413,8 @@ namespace llarp dnsport = std::atoi(v.substr(pos + 1).c_str()); } m_UpstreamResolvers.emplace_back(resolverAddr, dnsport); - llarp::LogInfo(Name(), " adding upstream dns set to ", resolverAddr, ":", - dnsport); + llarp::LogInfo(Name(), " adding upstream dns set to ", resolverAddr, + ":", dnsport); } if(k == "ifaddr") { @@ -559,18 +545,38 @@ namespace llarp } } { + // expire auto itr = m_ActiveExits.begin(); while(itr != m_ActiveExits.end()) { if(itr->second->IsExpired(now)) - { itr = m_ActiveExits.erase(itr); - } else - { - itr->second->Tick(now); ++itr; + } + // pick chosen exits and tick + m_ChosenExits.clear(); + itr = m_ActiveExits.begin(); + while(itr != m_ActiveExits.end()) + { + // do we have an exit set for this key? + if(m_ChosenExits.find(itr->first) != m_ChosenExits.end()) + { + // yes + if(m_ChosenExits[itr->first]->createdAt < itr->second->createdAt) + { + // if the iterators's exit is newer use it for the chosen exit for + // key + m_ChosenExits[itr->first] = itr->second.get(); + } } + else if(!itr->second->LooksDead( + now)) // set chosen exit if not dead for key that doesn't + // have one yet + m_ChosenExits[itr->first] = itr->second.get(); + // tick which clears the tx rx counters + itr->second->Tick(now); + ++itr; } } } diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index e0ae142c5..a780d55de 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -125,6 +125,11 @@ namespace llarp std::unordered_map< llarp::PathID_t, llarp::PubKey, llarp::PathID_t::Hash > m_Paths; + + std::unordered_map< llarp::PubKey, llarp::exit::Endpoint*, + llarp::PubKey::Hash > + m_ChosenExits; + std::unordered_multimap< llarp::PubKey, std::unique_ptr< llarp::exit::Endpoint >, llarp::PubKey::Hash > diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 2ef02d605..75df0acc5 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -37,7 +37,7 @@ namespace llarp tunif.user = this; tunif.netmask = DefaultTunNetmask; - // eh this shouldn't do anything on windows anyway + // eh this shouldn't do anything on windows anyway strncpy(tunif.ifaddr, DefaultTunSrcAddr, sizeof(tunif.ifaddr) - 1); strncpy(tunif.ifname, DefaultTunIfname, sizeof(tunif.ifname) - 1); diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 713d00af5..70c1d625b 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -18,13 +18,13 @@ namespace llarp std::vector< std::unique_ptr< llarp::dht::IMessage > > msgs; bool - DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf); + DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override; bool - BEncode(llarp_buffer_t* buf) const; + BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const; + HandleMessage(llarp::Router* router) const override; void Clear() override; diff --git a/llarp/messages/link_intro.hpp b/llarp/messages/link_intro.hpp index 4a45f1387..a9c4cc5f5 100644 --- a/llarp/messages/link_intro.hpp +++ b/llarp/messages/link_intro.hpp @@ -27,13 +27,13 @@ namespace llarp operator=(const LinkIntroMessage& msg); bool - DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf); + DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override; bool - BEncode(llarp_buffer_t* buf) const; + BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const; + HandleMessage(llarp::Router* router) const override; bool Sign(llarp::Crypto* c, const SecretKey& signKeySecret); diff --git a/llarp/net.cpp b/llarp/net.cpp index 5bb6cf036..e6aa6d37c 100644 --- a/llarp/net.cpp +++ b/llarp/net.cpp @@ -753,7 +753,7 @@ llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr) #ifndef _WIN32 if(getifaddrs(&ifa) == -1) #else - if (!strcmp(ifname, "lo") || !strcmp(ifname, "lo0")) + if(!strcmp(ifname, "lo") || !strcmp(ifname, "lo0")) { sockaddr_in* lo = (sockaddr_in*)addr; lo->sin_family = af; diff --git a/llarp/router.cpp b/llarp/router.cpp index 2e58806e0..adb114d9c 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -664,7 +664,8 @@ namespace llarp if(N < minRequiredRouters) { llarp::LogInfo("We need at least ", minRequiredRouters, - " service nodes to build paths but we have ", N, " in nodedb"); + " service nodes to build paths but we have ", N, + " in nodedb"); // TODO: only connect to random subset if(bootstrapRCList.size()) { diff --git a/llarp/win32_upoll.c b/llarp/win32_upoll.c index 7dd3a3e98..86b2c1260 100644 --- a/llarp/win32_upoll.c +++ b/llarp/win32_upoll.c @@ -274,7 +274,7 @@ int upoll_wait(upoll_t* upq, upoll_event_t* evs, int nev, int timeout) { int r = 0; - r = upoll_wait_select(upq, evs, nev, timeout); + r = upoll_wait_select(upq, evs, nev, timeout); return r; }