From baf2e1fb3cd9d528655d0fe5ea579e16e9581e68 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 11 Sep 2018 09:21:35 -0400 Subject: [PATCH] client improvements for real do correct timestamp comparison for introsets --- include/llarp/ip.hpp | 4 +- include/llarp/service/Intro.hpp | 4 +- include/llarp/service/endpoint.hpp | 3 ++ llarp/handlers/tun.cpp | 2 +- llarp/path.cpp | 3 +- llarp/service.cpp | 2 +- llarp/service/endpoint.cpp | 76 ++++++++++++++++++------------ 7 files changed, 58 insertions(+), 36 deletions(-) diff --git a/include/llarp/ip.hpp b/include/llarp/ip.hpp index 160d0cc05..9bda9ac75 100644 --- a/include/llarp/ip.hpp +++ b/include/llarp/ip.hpp @@ -124,13 +124,13 @@ namespace llarp void src(uint32_t ip) { - Header()->saddr = htonl(ip); + Header()->saddr = ip; } void dst(uint32_t ip) { - Header()->daddr = htonl(ip); + Header()->daddr = ip; } // update ip packet checksum diff --git a/include/llarp/service/Intro.hpp b/include/llarp/service/Intro.hpp index d9720b66e..cafd18b38 100644 --- a/include/llarp/service/Intro.hpp +++ b/include/llarp/service/Intro.hpp @@ -31,14 +31,14 @@ namespace llarp bool IsExpired(llarp_time_t now) const { - return now <= expiresAt; + return now >= expiresAt; } bool ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 5000) const { if(dlt) - return now <= (expiresAt - (llarp_randint() % dlt)); + return now >= (expiresAt - (llarp_randint() % dlt)); return IsExpired(now); } diff --git a/include/llarp/service/endpoint.hpp b/include/llarp/service/endpoint.hpp index 7d2ff110a..7b55c542a 100644 --- a/include/llarp/service/endpoint.hpp +++ b/include/llarp/service/endpoint.hpp @@ -255,6 +255,9 @@ namespace llarp virtual void IntroSetPublished(); + void + RegenAndPublishIntroSet(llarp_time_t now); + IServiceLookup* GenerateLookupByTag(const Tag& tag); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 211169ff1..3fdd7f9a3 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -248,7 +248,7 @@ namespace llarp } if(m_NextIP < m_MaxIP) { - nextIP = htonl(++m_NextIP); + nextIP = ++m_NextIP; m_AddrToIP.insert(std::make_pair(addr, nextIP)); m_IPToAddr.insert(std::make_pair(nextIP, addr)); } diff --git a/llarp/path.cpp b/llarp/path.cpp index fd0212c0b..6f52df4ba 100644 --- a/llarp/path.cpp +++ b/llarp/path.cpp @@ -498,7 +498,8 @@ namespace llarp intro.expiresAt = buildStarted + hops[0].lifetime; // confirm that we build the path status = ePathEstablished; - llarp::LogInfo("path is confirmed tx=", TXID(), " rx=", RXID()); + llarp::LogInfo("path is confirmed tx=", TXID(), " rx=", RXID(), + " took ", llarp_time_now_ms() - buildStarted, " ms"); if(m_BuiltHook) m_BuiltHook(this); m_BuiltHook = nullptr; diff --git a/llarp/service.cpp b/llarp/service.cpp index 8d108b901..13b46fbf7 100644 --- a/llarp/service.cpp +++ b/llarp/service.cpp @@ -105,7 +105,7 @@ namespace llarp auto highest = now; for(const auto& i : I) highest = std::max(i.expiresAt, highest); - return highest >= now; + return highest <= now; } Introduction::~Introduction() diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index dd188fc7e..ab8bfcbb7 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -84,6 +84,48 @@ namespace llarp != m_PendingServiceLookups.end(); } + void + Endpoint::RegenAndPublishIntroSet(llarp_time_t now) + { + std::set< Introduction > I; + if(!GetCurrentIntroductions(I)) + { + llarp::LogWarn("could not publish descriptors for endpoint ", Name(), + " because we couldn't get any introductions"); + if(ShouldBuildMore()) + ManualRebuild(1); + return; + } + IntroSet introset = m_IntroSet; + introset.I.clear(); + for(const auto& intro : I) + { + llarp::LogInfo(intro); + if(!intro.ExpiresSoon(now)) + introset.I.push_back(intro); + } + if(introset.I.size() == 0) + { + llarp::LogWarn("not enough intros to publish introset for ", Name()); + return; + } + introset.topic = m_Tag; + if(!m_Identity.SignIntroSet(introset, &m_Router->crypto)) + { + llarp::LogWarn("failed to sign introset for endpoint ", Name()); + return; + } + m_IntroSet = introset; + if(PublishIntroSet(m_Router)) + { + llarp::LogInfo("publishing introset for endpoint ", Name()); + } + else + { + llarp::LogWarn("failed to publish intro set for endpoint ", Name()); + } + } + void Endpoint::Tick(llarp_time_t now) { @@ -93,32 +135,7 @@ namespace llarp // publish descriptors if(ShouldPublishDescriptors(now)) { - std::set< Introduction > I; - if(!GetCurrentIntroductions(I)) - { - llarp::LogWarn("could not publish descriptors for endpoint ", Name(), - " because we couldn't get any introductions"); - if(ShouldBuildMore()) - ManualRebuild(1); - return; - } - m_IntroSet.I.clear(); - for(const auto& intro : I) - m_IntroSet.I.push_back(intro); - m_IntroSet.topic = m_Tag; - if(!m_Identity.SignIntroSet(m_IntroSet, &m_Router->crypto)) - { - llarp::LogWarn("failed to sign introset for endpoint ", Name()); - return; - } - if(PublishIntroSet(m_Router)) - { - llarp::LogInfo("publishing introset for endpoint ", Name()); - } - else - { - llarp::LogWarn("failed to publish intro set for endpoint ", Name()); - } + RegenAndPublishIntroSet(now); } // expire pending tx { @@ -448,7 +465,7 @@ namespace llarp bool Endpoint::PublishIntroSet(llarp_router* r) { - auto path = GetEstablishedPathClosestTo(m_Identity.pub.Addr().ToRouter()); + auto path = PickRandomEstablishedPath(); if(path) { m_CurrentPublishTX = llarp_randint(); @@ -478,8 +495,8 @@ namespace llarp { if(m_IntroSet.HasExpiredIntros(now)) return now - m_LastPublishAttempt >= INTROSET_PUBLISH_RETRY_INTERVAL; - return m_CurrentPublishTX == 0 - && now - m_LastPublish >= INTROSET_PUBLISH_INTERVAL; + return now - m_LastPublish >= INTROSET_PUBLISH_INTERVAL + && m_CurrentPublishTX == 0; } void @@ -628,6 +645,7 @@ namespace llarp { p->SetDataHandler(std::bind(&Endpoint::HandleHiddenServiceFrame, this, std::placeholders::_1)); + RegenAndPublishIntroSet(llarp_time_now_ms()); } bool