that's dumb af, use std::unique_ptr

pull/66/head
Jeff Becker 6 years ago
parent d6c42c4a30
commit 17297837d9
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -6,7 +6,7 @@
{
"label": "build",
"type": "shell",
"command": "make -j8 JSONRPC=ON",
"command": "make -j8 JSONRPC=ON test",
"group": "build",
"problemMatcher": [
"$gcc"

@ -505,9 +505,11 @@ set(DNS_SRC
set(TEST_SRC
test/main.cpp
test/alignedbuffer_unittest.cpp
test/base32_unittest.cpp
test/dht_unittest.cpp
test/encrypted_frame_unittest.cpp
test/exit_unittest.cpp
test/hiddenservice_unittest.cpp
test/traffic_transfer_unittest.cpp
test/obtain_exit_unittest.cpp

@ -21,6 +21,9 @@ namespace llarp
void
Tick(llarp_time_t now);
void
ClearAllEndpoints();
bool
AddExitEndpoint(const std::string &name, const Config_t &config);

@ -80,6 +80,12 @@ namespace llarp
return m_RxRate;
}
huint32_t
LocalIP() const
{
return m_IP;
}
private:
llarp::handlers::ExitEndpoint* m_Parent;
llarp::PubKey m_remoteSignKey;

@ -54,8 +54,8 @@ namespace llarp
auto itr = m_ActiveExits.begin();
while(itr != m_ActiveExits.end())
{
stats[itr->first].first += itr->second.TxRate();
stats[itr->first].second += itr->second.RxRate();
stats[itr->first].first += itr->second->TxRate();
stats[itr->first].second += itr->second->RxRate();
++itr;
}
}
@ -99,16 +99,19 @@ namespace llarp
KickIdentOffExit(const llarp::PubKey& pk);
llarp_router* m_Router;
bool m_ShouldInitTun;
std::string m_Name;
bool m_PermitExit;
std::unordered_map< llarp::PathID_t, llarp::PubKey,
llarp::PathID_t::Hash >
m_Paths;
std::unordered_multimap< llarp::PubKey, llarp::exit::Endpoint,
std::unordered_multimap< llarp::PubKey,
std::unique_ptr< llarp::exit::Endpoint >,
llarp::PubKey::Hash >
m_ActiveExits;
using KeyMap_t = std::map< llarp::PubKey, llarp::huint32_t >;
using KeyMap_t = std::unordered_map< llarp::PubKey, llarp::huint32_t,
llarp::PubKey::Hash >;
KeyMap_t m_KeyToIP;

@ -95,7 +95,9 @@ llarp_ev_close_udp(struct llarp_udp_io *udp)
llarp_time_t
llarp_ev_loop_time_now_ms(struct llarp_ev_loop *loop)
{
return loop->_now;
if(loop)
return loop->_now;
return llarp_time_now_ms();
}
void

@ -2,6 +2,7 @@
#include "../str.hpp"
#include "../router.hpp"
#include <llarp/net.hpp>
#include <cassert>
namespace llarp
{
@ -26,9 +27,10 @@ namespace llarp
, m_InetToNetwork(name + "_exit_rx", r->netloop, r->netloop)
{
m_Tun.user = this;
m_Tun.recvpkt = &ExitHandlerRecvPkt;
m_Tun.tick = &ExitHandlerFlushInbound;
m_Tun.user = this;
m_Tun.recvpkt = &ExitHandlerRecvPkt;
m_Tun.tick = &ExitHandlerFlushInbound;
m_ShouldInitTun = true;
}
ExitEndpoint::~ExitEndpoint()
@ -60,11 +62,11 @@ namespace llarp
while(itr != range.second)
{
if(ep == nullptr)
ep = &itr->second;
else if(itr->second.RxRate() < min && !itr->second.ExpiresSoon(now))
ep = itr->second.get();
else if(itr->second->RxRate() < min && !itr->second->ExpiresSoon(now))
{
min = ep->RxRate();
ep = &itr->second;
ep = itr->second.get();
}
++itr;
}
@ -78,7 +80,9 @@ namespace llarp
bool
ExitEndpoint::Start()
{
return llarp_ev_add_tun(Router()->netloop, &m_Tun);
if(m_ShouldInitTun)
return llarp_ev_add_tun(Router()->netloop, &m_Tun);
return true;
}
llarp_router *
@ -102,7 +106,7 @@ namespace llarp
bool
ExitEndpoint::HasLocalMappedAddrFor(const llarp::PubKey &pk) const
{
return m_KeyToIP.count(pk) > 0;
return m_KeyToIP.find(pk) != m_KeyToIP.end();
}
huint32_t
@ -112,7 +116,7 @@ namespace llarp
if(!HasLocalMappedAddrFor(pk))
{
// allocate and map
found = AllocateNewAddress();
found.h = AllocateNewAddress().h;
if(!m_KeyToIP.emplace(pk, found).second)
{
llarp::LogError(Name(), "failed to map ", pk, " to ", found);
@ -132,7 +136,8 @@ namespace llarp
found.h = m_KeyToIP[pk].h;
MarkIPActive(found);
m_KeyToIP.rehash(0);
assert(HasLocalMappedAddrFor(pk));
return found;
}
@ -210,8 +215,8 @@ namespace llarp
auto itr = m_ActiveExits.find(pk);
if(itr != m_ActiveExits.end())
{
if(itr->second.PubKey() == pk)
endpoint = &itr->second;
if(itr->second->PubKey() == pk)
endpoint = itr->second.get();
}
}
return endpoint;
@ -232,6 +237,11 @@ namespace llarp
bool
ExitEndpoint::SetOption(const std::string &k, const std::string &v)
{
if(k == "type" && v == "null")
{
m_ShouldInitTun = false;
return true;
}
if(k == "exit")
{
m_PermitExit = IsTrueValue(v.c_str());
@ -287,10 +297,9 @@ namespace llarp
return false;
huint32_t ip = GetIPForIdent(pk);
m_ActiveExits.insert(std::make_pair(
pk, llarp::exit::Endpoint(pk, path, !wantInternet, ip, this)));
pk, new llarp::exit::Endpoint(pk, path, !wantInternet, ip, this)));
m_Paths[path] = pk;
llarp::LogInfo(Name(), " exit for ", pk, " has address ", ip);
return true;
return HasLocalMappedAddrFor(pk);
}
std::string
@ -315,7 +324,7 @@ namespace llarp
auto itr = range.first;
while(itr != range.second)
{
if(itr->second.LocalPath() == ep->LocalPath())
if(itr->second->LocalPath() == ep->LocalPath())
{
itr = m_ActiveExits.erase(itr);
// now ep is gone af
@ -331,13 +340,13 @@ namespace llarp
auto itr = m_ActiveExits.begin();
while(itr != m_ActiveExits.end())
{
if(itr->second.IsExpired(now))
if(itr->second->IsExpired(now))
{
itr = m_ActiveExits.erase(itr);
}
else
{
itr->second.Tick(now);
itr->second->Tick(now);
++itr;
}
}

@ -0,0 +1,33 @@
#include <gtest/gtest.h>
#include <llarp/aligned.hpp>
using Buffer_t = llarp::AlignedBuffer< 32 >;
using Map_t = std::unordered_map< Buffer_t, int, Buffer_t::Hash >;
struct AlignedBufferTest : public ::testing::Test
{
AlignedBufferTest()
{
llarp_crypto_init(&crypto);
}
llarp_crypto crypto;
};
TEST_F(AlignedBufferTest, TestHash)
{
Buffer_t k, other_k;
k.Randomize();
other_k.Randomize();
Map_t m;
ASSERT_TRUE(m.empty());
ASSERT_TRUE(m.emplace(k, 1).second);
ASSERT_TRUE(m.find(k) != m.end());
ASSERT_TRUE(m[k] == 1);
ASSERT_FALSE(m.find(other_k) != m.end());
ASSERT_TRUE(m.size() == 1);
Buffer_t k_copy = k;
ASSERT_FALSE(m.emplace(k_copy, 2).second);
ASSERT_FALSE(m[k_copy] == 2);
ASSERT_TRUE(m[k_copy] == 1);
};

@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include <llarp/exit.hpp>
#include "router.hpp"
struct ExitTest : public ::testing::Test
{
ExitTest()
{
llarp_crypto_init(&r.crypto);
}
llarp_router r;
};
TEST_F(ExitTest, AddMultipleIP)
{
llarp::PubKey pk;
pk.Randomize();
llarp::PathID_t firstPath, secondPath;
firstPath.Randomize();
secondPath.Randomize();
llarp::exit::Context::Config_t conf;
conf.emplace("exit", "true");
conf.emplace("type", "null");
conf.emplace("ifaddr", "10.0.0.1/24");
ASSERT_TRUE(r.exitContext.AddExitEndpoint("test-exit", conf));
ASSERT_TRUE(r.exitContext.ObtainNewExit(pk, firstPath, true));
ASSERT_TRUE(r.exitContext.ObtainNewExit(pk, secondPath, true));
ASSERT_TRUE(r.exitContext.FindEndpointForPath(firstPath)->LocalIP()
== r.exitContext.FindEndpointForPath(secondPath)->LocalIP());
};
Loading…
Cancel
Save