diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 4fe024a44..e6deec451 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -415,6 +415,22 @@ namespace llarp "on the server and may pose liability concerns. Enable at your own risk.", }); + conf.defineOption( + "network", + "owned-range", + MultiValue, + Comment{ + "When in exit mode announce we allow a private range in our introset" + "exmaple:", + "owned-range=10.0.0.0/24", + }, + [this](std::string arg) { + IPRange range; + if (not range.FromString(arg)) + throw std::invalid_argument{"bad ip range: '" + arg + "'"}; + m_OwnedRanges.insert(range); + }); + conf.defineOption( "network", "traffic-whitelist", diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 37d67b4f8..399fac8a2 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -122,7 +122,7 @@ namespace llarp std::optional m_baseV6Address; - std::set m_AdvertisedRanges; + std::set m_OwnedRanges; std::optional m_TrafficPolicy; // TODO: diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 753c4dd36..596141d96 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -158,6 +158,9 @@ namespace llarp m_AuthPolicy = std::move(auth); } + m_TrafficPolicy = conf.m_TrafficPolicy; + m_OwnedRanges = conf.m_OwnedRanges; + m_LocalResolverAddr = dnsConf.m_bind; m_UpstreamResolvers = dnsConf.m_upstreamDNS; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index ba803d2d4..77054fb6f 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -126,7 +126,13 @@ namespace llarp std::optional GetExitPolicy() const override { - return m_ExitPolicy; + return m_TrafficPolicy; + } + + std::set + GetOwnedRanges() const override + { + return m_OwnedRanges; } /// ip packet against any exit policies we have @@ -258,7 +264,9 @@ namespace llarp std::unique_ptr m_PacketRouter; - std::optional m_ExitPolicy; + std::optional m_TrafficPolicy; + /// ranges we advetise as reachable + std::set m_OwnedRanges; }; } // namespace handlers diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 33ee7ea60..32e9bfd76 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -114,6 +114,8 @@ namespace llarp return; } + introSet().supportedProtocols.clear(); + // add supported ethertypes if (HasIfAddr()) { @@ -215,14 +217,17 @@ namespace llarp std::function)> resultHandler) { auto fail = [resultHandler]() { resultHandler({}); }; - auto lookupByAddress = [resultHandler](auto address) { + + auto lookupByAddress = [service, fail, resultHandler](auto address) { + // TODO: remove me after implementing the rest + fail(); if (auto* ptr = std::get_if(&address)) {} else if (auto* ptr = std::get_if
(&address)) {} else { - resultHandler({}); + fail(); } }; if (auto maybe = ParseAddress(name)) @@ -1096,15 +1101,6 @@ namespace llarp return m_Identity.pub.Addr(); } - inline void - AccumulateStats(const Session& session, EndpointBase::SendStat& stats) - {} - - inline void - AccumulateStats( - const std::shared_ptr& session, EndpointBase::SendStat& stats) - {} - std::optional Endpoint::GetStatFor(AddressVariant_t) const { // TODO: implement me diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 850bdb79e..6f17bec07 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -123,7 +123,7 @@ namespace llarp /// get the ip ranges we claim to own /// override me - virtual std::vector + virtual std::set GetOwnedRanges() const { return {}; diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 9ad78f86c..8bd8ae66d 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -219,7 +219,7 @@ namespace llarp::service if (key == "r") { - return BEncodeReadList(ownedRanges, buf); + return BEncodeReadSet(ownedRanges, buf); } if (key == "s") @@ -308,6 +308,16 @@ namespace llarp::service return false; } + // owned ranges + if (not ownedRanges.empty()) + { + if (not bencode_write_bytestring(buf, "r", 1)) + return false; + + if (not BEncodeWriteSet(ownedRanges, buf)) + return false; + } + // srv records if (not SRVs.empty()) { @@ -318,13 +328,6 @@ namespace llarp::service return false; } - // owned ranges - if (not ownedRanges.empty()) - { - if (not BEncodeWriteDictArray("r", ownedRanges, buf)) - return false; - } - // timestamp if (!BEncodeWriteDictInt("t", timestampSignedAt.count(), buf)) return false; diff --git a/llarp/service/intro_set.hpp b/llarp/service/intro_set.hpp index 06557d4d9..d0fbca60a 100644 --- a/llarp/service/intro_set.hpp +++ b/llarp/service/intro_set.hpp @@ -41,7 +41,7 @@ namespace llarp std::vector supportedProtocols; /// aonnuce that these ranges are reachable via our endpoint /// only set when we support exit traffic ethertype is supported - std::vector ownedRanges; + std::set ownedRanges; /// policies about traffic that we are willing to carry /// a protocol/range whitelist or blacklist diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index 6f8160244..cfb49c849 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -291,31 +291,28 @@ namespace llarp if (not item.BDecode(buf)) return false; // deny duplicates - return set.emplace(std::move(item)).second; + return set.emplace(item).second; } return true; }, buffer); } - /// read a std::set of decodable entities and deny duplicates + /// write an iterable container as a list template bool - BEncodeWriteSet(Set_t& set, llarp_buffer_t* buffer) + BEncodeWriteSet(const Set_t& set, llarp_buffer_t* buffer) { - return bencode_read_list( - [&set](llarp_buffer_t* buf, bool more) { - if (more) - { - typename Set_t::value_type item; - if (not item.BDecode(buf)) - return false; - // deny duplicates - return set.emplace(std::move(item)).second; - } - return true; - }, - buffer); + if (not bencode_start_list(buffer)) + return false; + + for (const auto& item : set) + { + if (not item.BEncode(buffer)) + return false; + } + + return bencode_end(buffer); } template