Merge remote-tracking branch 'origin/master'

pull/617/head
Jeff Becker 5 years ago
commit b86e834e1b
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -70,6 +70,33 @@ if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW)
message( FATAL_ERROR "shadow-framework is Linux only" )
endif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW)
if(NOT DEBIAN AND NOT MSVC_VERSION)
set(OPTIMIZE_FLAGS -O3)
set(DEBUG_FLAGS -O0 -g3)
endif()
if(ASAN)
set(DEBUG_FLAGS ${DEBUG_FLAGS} -fsanitize=address -fno-omit-frame-pointer)
set(OPTIMIZE_FLAGS "-O0")
endif(ASAN)
if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
set(OPTIMIZE_FLAGS "")
add_definitions(-DLOKINET_DEBUG=1)
set(CRYPTO_FLAGS "")
add_compile_options( ${DEBUG_FLAGS} )
link_libraries( ${DEBUG_FLAGS} )
endif(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
# Add non-386 target-specific options here
if(NON_PC_TARGET)
add_definitions(-DRPI)
set(WITH_STATIC ON)
endif(NON_PC_TARGET)
add_compile_options(${OPTIMIZE_FLAGS} ${CRYPTO_FLAGS})
set(ABSEIL_DIR vendor/abseil-cpp)
add_subdirectory(${ABSEIL_DIR})
include_directories(SYSTEM ${ABSEIL_DIR})
@ -156,36 +183,10 @@ if(TESTNET)
add_definitions(-DTESTNET=1)
endif(TESTNET)
if(NOT DEBIAN AND NOT MSVC_VERSION)
set(OPTIMIZE_FLAGS -O3)
set(DEBUG_FLAGS -O0 -g3)
endif()
if(ASAN)
set(DEBUG_FLAGS ${DEBUG_FLAGS} -fsanitize=address -fno-omit-frame-pointer)
set(OPTIMIZE_FLAGS "-O0")
endif(ASAN)
if(SHADOW)
include(cmake/shadow.cmake)
endif(SHADOW)
if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
set(OPTIMIZE_FLAGS "")
add_definitions(-DLOKINET_DEBUG=1)
set(CRYPTO_FLAGS "")
add_compile_options( ${DEBUG_FLAGS} )
link_libraries( ${DEBUG_FLAGS} )
endif(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
# Add non-386 target-specific options here
if(NON_PC_TARGET)
add_definitions(-DRPI)
set(WITH_STATIC ON)
endif(NON_PC_TARGET)
add_compile_options(${OPTIMIZE_FLAGS} ${CRYPTO_FLAGS})
if(NOT GIT_VERSION)
exec_program("git" ${CMAKE_CURRENT_SOURCE_DIR} ARGS "rev-parse --short HEAD" OUTPUT_VARIABLE GIT_VERSION_UNSTRIP)
string(STRIP "${GIT_VERSION_UNSTRIP}" GIT_VERSION)

@ -42,7 +42,7 @@ namespace abyss
struct BaseReqHandler
{
BaseReqHandler(llarp_time_t req_timeout);
~BaseReqHandler();
virtual ~BaseReqHandler();
bool
ServeAsync(llarp_ev_loop_ptr loop, llarp::Logic* logic,

@ -54,9 +54,7 @@ namespace abyss
m_State = eReadHTTPMethodLine;
}
~ConnImpl()
{
}
~ConnImpl() = default;
bool
FeedLine(std::string& line)

@ -3167,7 +3167,8 @@ utp_connect(utp_socket *conn, const struct sockaddr *to, socklen_t tolen)
"UTP_Connect conn_seed:%u packet_size:%u (B) "
"target_delay:%u (ms) delay_history:%u "
"delay_base_history:%u (minutes)",
conn->conn_seed, PACKET_SIZE, conn->target_delay / 1000,
conn->conn_seed, PACKET_SIZE,
static_cast< unsigned int >(conn->target_delay / 1000),
CUR_DELAY_SIZE, DELAY_BASE_HISTORY);
// Setup initial timeout timer.

@ -59,10 +59,6 @@ namespace llarp
{
}
~Encrypted()
{
}
bool
BEncode(llarp_buffer_t* buf) const
{

@ -27,16 +27,6 @@ namespace llarp
: Encrypted< EncryptedFrameSize >(std::min(sz, EncryptedFrameBodySize)
+ EncryptedFrameOverheadSize)
{
UpdateBuffer();
}
EncryptedFrame&
operator=(const EncryptedFrame& other)
{
_sz = other._sz;
memcpy(data(), other.data(), size());
UpdateBuffer();
return *this;
}
void

@ -39,12 +39,6 @@ namespace llarp
bool
FromString(const std::string &str);
friend std::ostream &
operator<<(std::ostream &out, const PubKey &k)
{
return out << k.ToString();
}
operator RouterID() const
{
return RouterID(as_array());
@ -58,6 +52,12 @@ namespace llarp
}
};
inline std::ostream &
operator<<(std::ostream &out, const PubKey &k)
{
return out << k.ToString();
}
inline bool
operator==(const PubKey &lhs, const PubKey &rhs)
{
@ -82,22 +82,10 @@ namespace llarp
{
}
explicit SecretKey(const SecretKey &k) : AlignedBuffer< SECKEYSIZE >(k)
{
}
explicit SecretKey(const byte_t *ptr) : AlignedBuffer< SECKEYSIZE >(ptr)
{
}
friend std::ostream &
operator<<(std::ostream &out, const SecretKey &)
{
// return out << k.ToHex();
// make sure we never print out secret keys
return out << "[secretkey]";
}
std::ostream &
print(std::ostream &stream, int level, int spaces) const
{
@ -117,15 +105,16 @@ namespace llarp
bool
SaveToFile(const char *fname) const;
SecretKey &
operator=(const byte_t *ptr)
{
std::copy(ptr, ptr + SIZE, begin());
return *this;
}
};
inline std::ostream &
operator<<(std::ostream &out, const SecretKey &)
{
// return out << k.ToHex();
// make sure we never print out secret keys
return out << "[secretkey]";
}
/// IdentitySecret is a secret key from a service node secret seed
struct IdentitySecret final : public AlignedBuffer< 32 >
{
@ -133,14 +122,6 @@ namespace llarp
{
}
friend std::ostream &
operator<<(std::ostream &out, const IdentitySecret &)
{
// return out << k.ToHex();
// make sure we never print out secret keys
return out << "[secretkey]";
}
/// no copy constructor
explicit IdentitySecret(const IdentitySecret &) = delete;
// no byte data constructor
@ -151,6 +132,13 @@ namespace llarp
LoadFromFile(const char *fname);
};
inline std::ostream &
operator<<(std::ostream &out, const IdentitySecret &)
{
// make sure we never print out secret keys
return out << "[IdentitySecret]";
}
using ShortHash = AlignedBuffer< SHORTHASHSIZE >;
using LongHash = AlignedBuffer< HASHSIZE >;

@ -66,16 +66,6 @@ namespace llarp
return c->sign(Z, sk, buf);
}
CloseExitMessage&
CloseExitMessage::operator=(const CloseExitMessage& other)
{
S = other.S;
version = other.version;
Y = other.Y;
Z = other.Z;
return *this;
}
bool
CloseExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
{

@ -71,17 +71,6 @@ namespace llarp
return c->sign(Z, sk, buf);
}
GrantExitMessage&
GrantExitMessage::operator=(const GrantExitMessage& other)
{
S = other.S;
T = other.T;
version = other.version;
Y = other.Y;
Z = other.Z;
return *this;
}
bool
GrantExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
{

@ -52,19 +52,6 @@ namespace llarp
return read;
}
RejectExitMessage&
RejectExitMessage::operator=(const RejectExitMessage& other)
{
B = other.B;
R = other.R;
S = other.S;
T = other.T;
version = other.version;
Y = other.Y;
Z = other.Z;
return *this;
}
bool
RejectExitMessage::Sign(llarp::Crypto* c, const llarp::SecretKey& sk)
{

@ -7,15 +7,6 @@ namespace llarp
{
namespace routing
{
TransferTrafficMessage&
TransferTrafficMessage::operator=(const TransferTrafficMessage& other)
{
S = other.S;
version = other.version;
X = other.X;
return *this;
}
bool
TransferTrafficMessage::PutBuffer(const llarp_buffer_t& buf,
uint64_t counter)

@ -59,18 +59,6 @@ namespace llarp
return c->verify(pk, buf, Z);
}
UpdateExitMessage&
UpdateExitMessage::operator=(const UpdateExitMessage& other)
{
P = other.P;
S = other.S;
T = other.T;
version = other.version;
Y = other.Y;
Z = other.Z;
return *this;
}
bool
UpdateExitMessage::Sign(llarp::Crypto* c, const llarp::SecretKey& sk)
{

@ -2,7 +2,7 @@
#if defined(_WIN32)
/** put win32 stuff here */
#else
#include <util/threadpool.h>
#include <util/thread_pool.hpp>
#include <util/logger.hpp>
#include <sys/wait.h>
#include <unistd.h>
@ -29,13 +29,12 @@ namespace llarp
: public IBackend,
public std::enable_shared_from_this< ExecShellHookBackend >
{
llarp_threadpool *m_ThreadPool;
thread::ThreadPool m_ThreadPool;
std::vector< std::string > _args;
std::vector< char * > args;
ExecShellHookBackend(std::string script)
: m_ThreadPool(llarp_init_threadpool(1, script.c_str()))
ExecShellHookBackend(std::string script) : m_ThreadPool(1, 1000)
{
do
{
@ -55,21 +54,20 @@ namespace llarp
~ExecShellHookBackend()
{
llarp_threadpool_stop(m_ThreadPool);
llarp_free_threadpool(&m_ThreadPool);
m_ThreadPool.shutdown();
}
bool
Start() override
{
llarp_threadpool_start(m_ThreadPool);
m_ThreadPool.start();
return true;
}
bool
Stop() override
{
llarp_threadpool_stop(m_ThreadPool);
m_ThreadPool.stop();
return true;
}
@ -123,8 +121,8 @@ namespace llarp
return _m_env.data();
}
static void
Exec(std::shared_ptr< ExecShellHookJob > self)
void
Exec()
{
std::thread t([&]() {
int result = 0;
@ -134,8 +132,7 @@ namespace llarp
if(child)
::waitpid(child, &result, 0);
else
::execve(self->m_Parent->Exe(), self->m_Parent->Args(),
self->Env());
::execve(m_Parent->Exe(), m_Parent->Args(), Env());
});
t.join();
}
@ -147,7 +144,8 @@ namespace llarp
{
auto job = std::make_shared< ExecShellHookJob >(shared_from_this(),
std::move(params));
m_ThreadPool->QueueFunc([=]() { ExecShellHookJob::Exec(job); });
m_ThreadPool.addJob(std::bind(&ExecShellHookJob::Exec, job));
}
Backend_ptr

@ -62,19 +62,10 @@ namespace llarp
struct GrantExitMessage final : public IMessage
{
using Nonce_t = llarp::AlignedBuffer< 16 >;
uint64_t T;
Nonce_t Y;
llarp::Signature Z;
GrantExitMessage() : IMessage()
{
}
~GrantExitMessage()
{
}
GrantExitMessage&
operator=(const GrantExitMessage& other);
bool
BEncode(llarp_buffer_t* buf) const override;
@ -109,14 +100,6 @@ namespace llarp
Nonce_t Y;
llarp::Signature Z;
RejectExitMessage() : IMessage()
{
}
~RejectExitMessage()
{
}
void
Clear() override
{
@ -127,9 +110,6 @@ namespace llarp
Z.Zero();
}
RejectExitMessage&
operator=(const RejectExitMessage& other);
bool
Sign(llarp::Crypto* c, const llarp::SecretKey& sk);
@ -196,17 +176,6 @@ namespace llarp
Nonce_t Y;
llarp::Signature Z;
UpdateExitMessage() : IMessage()
{
}
~UpdateExitMessage()
{
}
UpdateExitMessage&
operator=(const UpdateExitMessage& other);
bool
Sign(llarp::Crypto* c, const llarp::SecretKey& sk);
@ -239,17 +208,6 @@ namespace llarp
Nonce_t Y;
llarp::Signature Z;
CloseExitMessage() : IMessage()
{
}
~CloseExitMessage()
{
}
CloseExitMessage&
operator=(const CloseExitMessage& other);
bool
BEncode(llarp_buffer_t* buf) const override;

@ -8,10 +8,6 @@
namespace llarp
{
LinkIntroMessage::~LinkIntroMessage()
{
}
bool
LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
@ -104,17 +100,6 @@ namespace llarp
return bencode_end(buf);
}
LinkIntroMessage&
LinkIntroMessage::operator=(const LinkIntroMessage& msg)
{
version = msg.version;
Z = msg.Z;
rc = msg.rc;
N = msg.N;
P = msg.P;
return *this;
}
bool
LinkIntroMessage::HandleMessage(AbstractRouter* router) const
{

@ -17,16 +17,11 @@ namespace llarp
{
}
~LinkIntroMessage();
RouterContact rc;
KeyExchangeNonce N;
Signature Z;
uint64_t P;
LinkIntroMessage&
operator=(const LinkIntroMessage& msg);
bool
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override;

@ -31,9 +31,6 @@ namespace llarp
return _size;
}
TransferTrafficMessage&
operator=(const TransferTrafficMessage& other);
/// append buffer to X
bool
PutBuffer(const llarp_buffer_t& buf, uint64_t counter);

@ -91,6 +91,7 @@ namespace llarp
break;
}
assert(false && "Invalid publication type");
return {};
}
std::string

@ -14,42 +14,6 @@
#define inet_aton(x, y) inet_pton(AF_INET, x, y)
#endif
llarp::Addr::operator const sockaddr*() const
{
if(af() == AF_INET)
return (const sockaddr*)&_addr4;
else
return (const sockaddr*)&_addr;
}
llarp::Addr::operator sockaddr*() const
{
if(af() == AF_INET)
return (sockaddr*)&_addr4;
else
return (sockaddr*)&_addr;
}
bool
llarp::Addr::operator<(const Addr& other) const
{
if(af() == AF_INET && other.af() == AF_INET)
return port() < other.port() || addr4()->s_addr < other.addr4()->s_addr;
else
return port() < other.port() || *addr6() < *other.addr6()
|| af() < other.af();
}
bool
llarp::Addr::operator==(const Addr& other) const
{
if(af() == AF_INET && other.af() == AF_INET)
return port() == other.port() && addr4()->s_addr == other.addr4()->s_addr;
else
return af() == other.af() && memcmp(addr6(), other.addr6(), 16) == 0
&& port() == other.port();
}
namespace llarp
{
Addr::Addr()
@ -112,9 +76,8 @@ namespace llarp
Addr::Addr(string_view addr_str, string_view port_str)
{
this->from_char_array(llarp::string_view_string(addr_str).c_str());
this->port(
std::strtoul(llarp::string_view_string(port_str).c_str(), nullptr, 10));
this->from_char_array(string_view_string(addr_str).c_str());
this->port(std::strtoul(string_view_string(port_str).c_str(), nullptr, 10));
}
bool
@ -129,16 +92,16 @@ namespace llarp
char buf[6];
snprintf(buf, 6, "%s", pPosition + 1);
uint16_t port = std::atoi(buf);
llarp::LogDebug("Setting port ", std::to_string(port));
LogDebug("Setting port ", std::to_string(port));
this->port(port);
// trim str
// can't VLA
str = strdup(in); // copy it
str[pPosition - in] = '\0'; // nul terminate it early
llarp::LogDebug("Truncating to ", str);
LogDebug("Truncating to ", str);
freeStr = true;
}
llarp::Zero(&_addr, sizeof(sockaddr_in6));
Zero(&_addr, sizeof(sockaddr_in6));
struct addrinfo hint, *res = NULL;
int ret;
@ -150,7 +113,7 @@ namespace llarp
ret = getaddrinfo(str, NULL, &hint, &res);
if(ret)
{
llarp::LogError("failed to determine address family: ", str);
LogError("failed to determine address family: ", str);
if(freeStr)
free(str);
return false;
@ -158,14 +121,14 @@ namespace llarp
if(res->ai_family == AF_INET6)
{
llarp::LogError("IPv6 address not supported yet", str);
LogError("IPv6 address not supported yet", str);
if(freeStr)
free(str);
return false;
}
else if(res->ai_family != AF_INET)
{
llarp::LogError("Address family not supported yet", str);
LogError("Address family not supported yet", str);
if(freeStr)
free(str);
return false;
@ -175,7 +138,7 @@ namespace llarp
struct in_addr* addr = &_addr4.sin_addr;
if(inet_aton(str, addr) == 0)
{
llarp::LogError("failed to parse ", str);
LogError("failed to parse ", str);
if(freeStr)
free(str);
return false;
@ -208,7 +171,7 @@ namespace llarp
Addr::from_4int(const uint8_t one, const uint8_t two, const uint8_t three,
const uint8_t four)
{
llarp::Zero(&_addr, sizeof(sockaddr_in6));
Zero(&_addr, sizeof(sockaddr_in6));
struct in_addr* addr = &_addr4.sin_addr;
unsigned char* ip = (unsigned char*)&(addr->s_addr);
@ -264,7 +227,7 @@ namespace llarp
Addr::Addr(const sockaddr_in& other)
{
llarp::Zero(&_addr, sizeof(sockaddr_in6));
Zero(&_addr, sizeof(sockaddr_in6));
_addr.sin6_family = AF_INET;
uint8_t* addrptr = _addr.sin6_addr.s6_addr;
uint16_t* port = &_addr.sin6_port;
@ -300,7 +263,7 @@ namespace llarp
Addr::Addr(const sockaddr& other)
{
llarp::Zero(&_addr, sizeof(sockaddr_in6));
Zero(&_addr, sizeof(sockaddr_in6));
_addr.sin6_family = other.sa_family;
uint8_t* addrptr = _addr.sin6_addr.s6_addr;
uint16_t* port = &_addr.sin6_port;
@ -406,10 +369,46 @@ namespace llarp
return ntohs(_addr.sin6_port);
}
Addr::operator const sockaddr*() const
{
if(af() == AF_INET)
return (const sockaddr*)&_addr4;
else
return (const sockaddr*)&_addr;
}
Addr::operator sockaddr*() const
{
if(af() == AF_INET)
return (sockaddr*)&_addr4;
else
return (sockaddr*)&_addr;
}
bool
Addr::operator<(const Addr& other) const
{
if(af() == AF_INET && other.af() == AF_INET)
return port() < other.port() || addr4()->s_addr < other.addr4()->s_addr;
else
return port() < other.port() || *addr6() < *other.addr6()
|| af() < other.af();
}
bool
Addr::operator==(const Addr& other) const
{
if(af() == AF_INET && other.af() == AF_INET)
return port() == other.port() && addr4()->s_addr == other.addr4()->s_addr;
else
return af() == other.af() && memcmp(addr6(), other.addr6(), 16) == 0
&& port() == other.port();
}
Addr&
Addr::operator=(const sockaddr& other)
{
llarp::Zero(&_addr, sizeof(sockaddr_in6));
Zero(&_addr, sizeof(sockaddr_in6));
_addr.sin6_family = other.sa_family;
uint8_t* addrptr = _addr.sin6_addr.s6_addr;
uint16_t* port = &_addr.sin6_port;

@ -8,6 +8,7 @@
#include <util/logger.hpp>
#include <util/logic.hpp>
#include <util/mem.hpp>
#include <util/thread_pool.hpp>
#include <fstream>
#include <unordered_map>
@ -48,31 +49,18 @@ llarp_nodedb::Get(const llarp::RouterID &pk, llarp::RouterContact &result)
return true;
}
// kill rcs from disk async
struct AsyncKillRCJobs
void
KillRCJobs(const std::set< std::string > &files)
{
std::set< std::string > files;
static void
Work(void *u)
{
static_cast< AsyncKillRCJobs * >(u)->Kill();
}
void
Kill()
{
for(const auto &file : files)
fs::remove(file);
delete this;
}
};
for(const auto &file : files)
fs::remove(file);
}
void
llarp_nodedb::RemoveIf(
std::function< bool(const llarp::RouterContact &rc) > filter)
{
AsyncKillRCJobs *job = new AsyncKillRCJobs();
std::set< std::string > files;
{
llarp::util::Lock l(&access);
auto itr = entries.begin();
@ -80,14 +68,15 @@ llarp_nodedb::RemoveIf(
{
if(filter(itr->second))
{
job->files.insert(getRCFilePath(itr->second.pubkey));
files.insert(getRCFilePath(itr->second.pubkey));
itr = entries.erase(itr);
}
else
++itr;
}
}
llarp_threadpool_queue_job(disk, {job, &AsyncKillRCJobs::Work});
disk->addJob(std::bind(&KillRCJobs, files));
}
bool
@ -118,38 +107,24 @@ llarp_nodedb::getRCFilePath(const llarp::RouterID &pubkey) const
return filepath.string();
}
struct async_insert_rc
{
llarp_nodedb *nodedb;
llarp::RouterContact rc;
llarp::Logic *logic;
std::function< void(void) > completedHook;
async_insert_rc(llarp_nodedb *n, const llarp::RouterContact &r)
: nodedb(n), rc(r)
{
}
};
static void
handle_async_insert_rc(void *u)
handle_async_insert_rc(llarp_nodedb *nodedb, const llarp::RouterContact &rc,
llarp::Logic *logic,
const std::function< void(void) > &completedHook)
{
async_insert_rc *job = static_cast< async_insert_rc * >(u);
job->nodedb->Insert(job->rc);
if(job->logic && job->completedHook)
nodedb->Insert(rc);
if(logic && completedHook)
{
job->logic->queue_func(job->completedHook);
logic->queue_func(completedHook);
}
delete job;
}
void
llarp_nodedb::InsertAsync(llarp::RouterContact rc, llarp::Logic *logic,
std::function< void(void) > completionHandler)
{
async_insert_rc *ctx = new async_insert_rc(this, rc);
ctx->completedHook = completionHandler;
ctx->logic = logic;
llarp_threadpool_queue_job(disk, {ctx, &handle_async_insert_rc});
disk->addJob(
std::bind(&handle_async_insert_rc, this, rc, logic, completionHandler));
}
/// insert and write to disk
@ -304,10 +279,8 @@ logic_threadworker_callback(void *user)
// write it to disk
void
disk_threadworker_setRC(void *user)
disk_threadworker_setRC(llarp_async_verify_rc *verify_request)
{
llarp_async_verify_rc *verify_request =
static_cast< llarp_async_verify_rc * >(user);
verify_request->valid = verify_request->nodedb->Insert(verify_request->rc);
if(verify_request->logic)
verify_request->logic->queue_job(
@ -327,8 +300,8 @@ crypto_threadworker_verifyrc(void *user)
if(verify_request->valid && rc.IsPublicRouter())
{
llarp::LogDebug("RC is valid, saving to disk");
llarp_threadpool_queue_job(verify_request->diskworker,
{verify_request, &disk_threadworker_setRC});
verify_request->diskworker->addJob(
std::bind(&disk_threadworker_setRC, verify_request));
}
else
{

@ -28,6 +28,11 @@ namespace llarp
{
struct Crypto;
class Logic;
namespace thread
{
class ThreadPool;
}
} // namespace llarp
struct llarp_nodedb_iter
@ -40,7 +45,7 @@ struct llarp_nodedb_iter
struct llarp_nodedb
{
llarp_nodedb(llarp::Crypto *c, llarp_threadpool *diskworker)
llarp_nodedb(llarp::Crypto *c, llarp::thread::ThreadPool *diskworker)
: crypto(c), disk(diskworker)
{
}
@ -51,7 +56,7 @@ struct llarp_nodedb
}
llarp::Crypto *crypto;
llarp_threadpool *disk;
llarp::thread::ThreadPool *disk;
mutable llarp::util::Mutex access; // protects entries
std::unordered_map< llarp::RouterID, llarp::RouterContact,
llarp::RouterID::Hash >
@ -146,13 +151,11 @@ struct llarp_async_verify_rc
/// async_verify_context
void *user;
/// nodedb storage
struct llarp_nodedb *nodedb;
llarp_nodedb *nodedb;
// llarp::Logic for queue_job
llarp::Logic *logic; // includes a llarp_threadpool
// struct llarp::Crypto *crypto; // probably don't need this because we have
// it in the nodedb
struct llarp_threadpool *cryptoworker;
struct llarp_threadpool *diskworker;
llarp_threadpool *cryptoworker;
llarp::thread::ThreadPool *diskworker;
/// router contact
llarp::RouterContact rc;
@ -181,11 +184,11 @@ struct llarp_async_load_rc
/// async_verify_context
void *user;
/// nodedb storage
struct llarp_nodedb *nodedb;
llarp_nodedb *nodedb;
/// llarp::Logic for calling hook
llarp::Logic *logic;
/// disk worker threadpool
struct llarp_threadpool *diskworker;
llarp::thread::ThreadPool *diskworker;
/// target pubkey
llarp::PubKey pubkey;
/// router contact result

@ -53,7 +53,6 @@ namespace llarp
struct TransitHopInfo
{
TransitHopInfo() = default;
TransitHopInfo(const TransitHopInfo& other);
TransitHopInfo(const RouterID& down, const LR_CommitRecord& record);
PathID_t txID, rxID;
@ -158,8 +157,6 @@ namespace llarp
{
TransitHop();
TransitHop(const TransitHop& other);
TransitHopInfo info;
SharedSecret pathKey;
ShortHash nonceXOR;

@ -253,7 +253,7 @@ namespace llarp
return true;
}
} while(tries > 0);
return tries > 0;
return false;
}
bool

@ -32,14 +32,6 @@ namespace llarp
return started + lifetime;
}
TransitHopInfo::TransitHopInfo(const TransitHopInfo& other)
: txID(other.txID)
, rxID(other.rxID)
, upstream(other.upstream)
, downstream(other.downstream)
{
}
TransitHopInfo::TransitHopInfo(const RouterID& down,
const LR_CommitRecord& record)
: txID(record.txid)
@ -49,15 +41,6 @@ namespace llarp
{
}
TransitHop::TransitHop(const TransitHop& other)
: info(other.info)
, pathKey(other.pathKey)
, started(other.started)
, lifetime(other.lifetime)
, version(other.version)
{
}
bool
TransitHop::SendRoutingMessage(const routing::IMessage& msg,
AbstractRouter* r)

@ -46,6 +46,11 @@ namespace llarp
struct Context;
}
namespace thread
{
class ThreadPool;
}
struct AbstractRouter
{
virtual ~AbstractRouter() = 0;
@ -96,7 +101,7 @@ namespace llarp
virtual llarp_threadpool *
threadpool() = 0;
virtual llarp_threadpool *
virtual thread::ThreadPool *
diskworker() = 0;
virtual service::Context &

@ -223,6 +223,7 @@ namespace llarp
, _crypto(std::make_unique< sodium::CryptoLibSodium >())
, paths(this)
, _exitContext(this)
, disk(1, 1000)
, _dht(llarp_dht_context_new(this))
, inbound_link_msg_parser(this)
, _hiddenServiceContext(this)
@ -231,11 +232,6 @@ namespace llarp
this->ip4addr.sin_family = AF_INET;
this->ip4addr.sin_port = htons(1090);
#ifdef TESTNET
disk = tp;
#else
disk = llarp_init_threadpool(1, "llarp-diskio");
#endif
_stopping.store(false);
_running.store(false);
}
@ -325,8 +321,6 @@ namespace llarp
}
}
constexpr size_t MaxPendingSendQueueSize = 8;
bool
Router::SendToOrQueue(const RouterID &remote, const ILinkMessage *msg)
{
@ -499,12 +493,11 @@ namespace llarp
}
/// called in disk worker thread
static void
HandleSaveRC(void *u)
void
Router::HandleSaveRC() const
{
Router *self = static_cast< Router * >(u);
std::string fname = self->our_rc_file.string();
self->_rc.Write(fname.c_str());
std::string fname = our_rc_file.string();
_rc.Write(fname.c_str());
}
bool
@ -517,7 +510,7 @@ namespace llarp
LogError("RC is invalid, not saving");
return false;
}
llarp_threadpool_queue_job(diskworker(), {this, &HandleSaveRC});
diskworker()->addJob(std::bind(&Router::HandleSaveRC, this));
return true;
}
@ -534,6 +527,8 @@ namespace llarp
llarp_ev_loop_stop(_netloop.get());
inboundLinks.clear();
outboundLinks.clear();
disk.stop();
disk.shutdown();
}
void
@ -1335,14 +1330,10 @@ namespace llarp
// save profiles async
if(routerProfiling().ShouldSave(now))
{
llarp_threadpool_queue_job(
diskworker(),
{this, [](void *u) {
Router *self = static_cast< Router * >(u);
self->routerProfiling().Save(self->routerProfilesFile.c_str());
}});
diskworker()->addJob(
[&]() { routerProfiling().Save(routerProfilesFile.c_str()); });
}
}
} // namespace llarp
bool
Router::Sign(Signature &sig, const llarp_buffer_t &buf) const
@ -1506,7 +1497,7 @@ namespace llarp
job->nodedb = _nodedb;
job->logic = _logic;
job->cryptoworker = tp;
job->diskworker = disk;
job->diskworker = &disk;
if(rc.IsPublicRouter())
job->hook = &Router::on_verify_server_rc;
else
@ -1569,7 +1560,7 @@ namespace llarp
}
llarp_threadpool_start(tp);
llarp_threadpool_start(disk);
disk.start();
for(const auto &rc : bootstrapRCList)
{

@ -170,10 +170,10 @@ namespace llarp
return tp;
}
llarp_threadpool *
thread::ThreadPool *
diskworker() override
{
return disk;
return &disk;
}
// our ipv4 public setting
@ -189,7 +189,7 @@ namespace llarp
exit::Context _exitContext;
SecretKey _identity;
SecretKey _encryption;
llarp_threadpool *disk;
thread::ThreadPool disk;
llarp_dht_context *_dht = nullptr;
llarp_nodedb *_nodedb;
llarp_time_t _startedAt;
@ -385,6 +385,9 @@ namespace llarp
bool
ConnectionToRouterAllowed(const RouterID &router) const override;
void
HandleSaveRC() const;
bool
SaveRC();

@ -10,6 +10,41 @@ namespace llarp
{
namespace service
{
namespace
{
using EndpointConstructor = std::function< service::Endpoint_ptr(
const std::string &, AbstractRouter *, service::Context *) >;
using EndpointConstructors = std::map< std::string, EndpointConstructor >;
static EndpointConstructors endpointConstructors = {
{"tun",
[](const std::string &nick, AbstractRouter *r,
service::Context *c) -> service::Endpoint_ptr {
return std::make_shared< handlers::TunEndpoint >(nick, r, c);
}},
{"android-tun",
[](const std::string &, AbstractRouter *,
service::Context *) -> service::Endpoint_ptr {
return nullptr;
/// SOOOOOOON (tm)
// return std::make_shared<handlers::AndroidTunEndpoint>(nick,
// r, c);
}},
{"ios-tun",
[](const std::string &, AbstractRouter *,
service::Context *) -> service::Endpoint_ptr {
return nullptr;
/// SOOOOOOON (tm)
// return std::make_shared<handlers::IOSTunEndpoint>(nick, r,
// c);
}},
{"null",
[](const std::string &nick, AbstractRouter *r,
service::Context *c) -> service::Endpoint_ptr {
return std::make_shared< handlers::NullEndpoint >(nick, r, c);
}}};
} // namespace
Context::Context(AbstractRouter *r) : m_Router(r)
{
}
@ -190,7 +225,7 @@ namespace llarp
}
// extract type
std::string endpointType = "tun";
std::string keyfile = "";
std::string keyfile;
for(const auto &option : conf.second)
{
if(option.first == "type")
@ -201,41 +236,9 @@ namespace llarp
service::Endpoint_ptr service;
static std::map<
std::string,
std::function< service::Endpoint_ptr(
const std::string &, AbstractRouter *, service::Context *) > >
endpointConstructors = {
{"tun",
[](const std::string &nick, AbstractRouter *r,
service::Context *c) -> service::Endpoint_ptr {
return std::make_shared< handlers::TunEndpoint >(nick, r, c);
}},
{"android-tun",
[](const std::string &, AbstractRouter *,
service::Context *) -> service::Endpoint_ptr {
return nullptr;
/// SOOOOOOON (tm)
// return std::make_shared<handlers::AndroidTunEndpoint>(nick,
// r, c);
}},
{"ios-tun",
[](const std::string &, AbstractRouter *,
service::Context *) -> service::Endpoint_ptr {
return nullptr;
/// SOOOOOOON (tm)
// return std::make_shared<handlers::IOSTunEndpoint>(nick, r,
// c);
}},
{"null",
[](const std::string &nick, AbstractRouter *r,
service::Context *c) -> service::Endpoint_ptr {
return std::make_shared< handlers::NullEndpoint >(nick, r, c);
}}};
{
// detect type
auto itr = endpointConstructors.find(endpointType);
const auto itr = endpointConstructors.find(endpointType);
if(itr == endpointConstructors.end())
{
LogError("no such endpoint type: ", endpointType);
@ -248,7 +251,7 @@ namespace llarp
{
// if ephemeral, then we need to regen key
// if privkey file, then set it and load it
if(keyfile != "")
if(!keyfile.empty())
{
service->SetOption("keyfile", keyfile);
// load keyfile, so we have the correct name for logging

@ -495,11 +495,11 @@ namespace llarp
Endpoint::LoadKeyFile()
{
auto crypto = m_Router->crypto();
if(m_Keyfile.size())
if(!m_Keyfile.empty())
{
if(!m_Identity.EnsureKeys(m_Keyfile, crypto))
{
LogWarn("Can't ensure keyfile [", m_Keyfile, "]");
LogError("Can't ensure keyfile [", m_Keyfile, "]");
return false;
}
}
@ -514,7 +514,6 @@ namespace llarp
Endpoint::Start()
{
// how can I tell if a m_Identity isn't loaded?
// this->LoadKeyFile();
if(!m_DataHandler)
{
m_DataHandler = this;

@ -6,10 +6,6 @@ namespace llarp
{
namespace service
{
Identity::~Identity()
{
}
bool
Identity::BEncode(llarp_buffer_t* buf) const
{
@ -107,6 +103,13 @@ namespace llarp
return false;
f.write((char*)buf.cur, buf.sz);
}
if(!fs::is_regular_file(fname))
{
LogError("keyfile ", fname, " is not a regular file");
return false;
}
// read file
std::ifstream inf(fname, std::ios::binary);
inf.seekg(0, std::ios::end);

@ -7,6 +7,8 @@
#include <service/vanity.hpp>
#include <util/bencode.hpp>
#include <tuple>
namespace llarp
{
struct Crypto;
@ -25,16 +27,10 @@ namespace llarp
// public service info
ServiceInfo pub;
~Identity();
// regenerate secret keys
void
RegenerateKeys(Crypto* c);
// load from file
bool
LoadFromFile(const std::string& fpath);
bool
BEncode(llarp_buffer_t* buf) const override;
@ -54,6 +50,13 @@ namespace llarp
bool
Sign(Crypto*, Signature& sig, const llarp_buffer_t& buf) const;
};
inline bool
operator==(const Identity& lhs, const Identity& rhs)
{
return std::tie(lhs.enckey, lhs.signkey, lhs.pq, lhs.version, lhs.vanity)
== std::tie(rhs.enckey, rhs.signkey, rhs.pq, rhs.version, rhs.vanity);
}
} // namespace service
} // namespace llarp

@ -17,18 +17,9 @@ namespace llarp
PubKey router;
PathID_t pathID;
uint64_t latency = 0;
uint64_t version = 0;
uint64_t expiresAt = 0;
Introduction() = default;
Introduction(const Introduction& other) : IBEncodeMessage(other.version)
{
router = other.router;
pathID = other.pathID;
latency = other.latency;
version = other.version;
expiresAt = other.expiresAt;
}
util::StatusObject
ExtractStatus() const;

@ -36,13 +36,6 @@ namespace llarp
begin());
}
Tag&
operator=(const Tag& other)
{
as_array() = other.as_array();
return *this;
}
Tag&
operator=(const std::string& str)
{

@ -3,7 +3,17 @@
namespace llarp
{
FileLogStream::FileLogStream(llarp_threadpool *disk, FILE *f,
namespace
{
static void
Flush(const std::deque< std::string > &lines, FILE *const f)
{
for(const auto &line : lines)
fprintf(f, "%s\n", line.c_str());
fflush(f);
}
} // namespace
FileLogStream::FileLogStream(thread::ThreadPool *disk, FILE *f,
llarp_time_t flushInterval)
: m_Disk(disk), m_File(f), m_FlushInterval(flushInterval)
{
@ -66,24 +76,7 @@ namespace llarp
void
FileLogStream::FlushLinesToDisk(llarp_time_t now)
{
FlushEvent *ev = new FlushEvent(std::move(m_Lines), m_File);
llarp_threadpool_queue_job(m_Disk, {ev, &FlushEvent::HandleFlush});
m_Disk->addJob(std::bind(&Flush, std::move(m_Lines), m_File));
m_LastFlush = now;
}
void
FileLogStream::FlushEvent::HandleFlush(void *user)
{
static_cast< FileLogStream::FlushEvent * >(user)->Flush();
}
void
FileLogStream::FlushEvent::Flush()
{
for(const auto &line : lines)
fprintf(f, "%s\n", line.c_str());
fflush(f);
delete this;
}
} // namespace llarp
} // namespace llarp

@ -2,15 +2,18 @@
#define LLARP_UTIL_FILE_LOGGER_HPP
#include <util/logstream.hpp>
#include <util/threadpool.h>
#include <util/thread_pool.hpp>
#include <util/time.hpp>
#include <deque>
namespace llarp
{
/// flushable file based log stream
struct FileLogStream : public ILogStream
{
FileLogStream(llarp_threadpool* disk, FILE* f, llarp_time_t flushInterval);
FileLogStream(thread::ThreadPool* disk, FILE* f,
llarp_time_t flushInterval);
~FileLogStream();
@ -30,29 +33,13 @@ namespace llarp
}
private:
struct FlushEvent
{
FlushEvent(std::deque< std::string > l, FILE* file)
: lines(std::move(l)), f(file)
{
}
const std::deque< std::string > lines;
FILE* const f;
void
Flush();
static void
HandleFlush(void*);
};
bool
ShouldFlush(llarp_time_t now) const;
void
FlushLinesToDisk(llarp_time_t now);
llarp_threadpool* m_Disk;
thread::ThreadPool* m_Disk;
FILE* m_File;
const llarp_time_t m_FlushInterval;
llarp_time_t m_LastFlush = 0;

@ -46,22 +46,26 @@ namespace llarp
{
const char* format;
log_timestamp(const char* fmt = "%c %Z") : format(fmt)
log_timestamp() : format("%c %Z")
{
}
friend std::ostream&
operator<<(std::ostream& out, const log_timestamp& ts)
explicit log_timestamp(const char* fmt) : format(fmt)
{
}
};
inline std::ostream&
operator<<(std::ostream& out, const log_timestamp& ts)
{
#if defined(ANDROID) || defined(RPI)
(void)ts;
return out << time_now_ms();
(void)ts;
return out << time_now_ms();
#else
return out << absl::FormatTime(ts.format, absl::Now(),
absl::LocalTimeZone());
absl::TimeZone tz = absl::LocalTimeZone();
return out << absl::FormatTime(ts.format, absl::Now(), tz);
#endif
}
};
}
} // namespace llarp

@ -178,7 +178,7 @@ namespace llarp
QueueManager::~QueueManager()
{
delete m_states;
delete[] m_states;
}
QueueReturn

@ -6,12 +6,19 @@
#include <service/intro_set.hpp>
#include <util/time.hpp>
#include <crypto/mock_crypto.hpp>
#include <test_util.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace llarp;
using namespace testing;
struct HiddenServiceTest : public ::testing::Test
{
llarp::sodium::CryptoLibSodium crypto;
llarp::service::Identity ident;
sodium::CryptoLibSodium crypto;
service::Identity ident;
HiddenServiceTest()
{
@ -34,15 +41,15 @@ struct HiddenServiceTest : public ::testing::Test
TEST_F(HiddenServiceTest, TestGenerateIntroSet)
{
llarp::service::Address addr;
service::Address addr;
ASSERT_TRUE(ident.pub.CalculateAddress(addr.as_array()));
llarp::service::IntroSet I;
auto now = llarp::time_now_ms();
service::IntroSet I;
auto now = time_now_ms();
I.T = now;
while(I.I.size() < 10)
{
llarp::service::Introduction intro;
intro.expiresAt = now + (llarp::path::default_lifetime / 2);
service::Introduction intro;
intro.expiresAt = now + (path::default_lifetime / 2);
intro.router.Randomize();
intro.pathID.Randomize();
I.I.emplace_back(std::move(intro));
@ -54,7 +61,76 @@ TEST_F(HiddenServiceTest, TestGenerateIntroSet)
TEST_F(HiddenServiceTest, TestAddressToFromString)
{
auto str = ident.pub.Addr().ToString();
llarp::service::Address addr;
service::Address addr;
ASSERT_TRUE(addr.FromString(str));
ASSERT_TRUE(addr == ident.pub.Addr());
}
struct ServiceIdentityTest : public ::testing::Test
{
test::MockCrypto crypto;
};
template < typename Arg >
std::function< void(Arg&) >
FillArg(byte_t val)
{
return [=](Arg& arg) { arg.Fill(val); };
}
TEST_F(ServiceIdentityTest, EnsureKeys)
{
fs::path p = test::randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
test::FileGuard guard(p);
EXPECT_CALL(crypto, encryption_keygen(_))
.WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x01)));
EXPECT_CALL(crypto, identity_keygen(_))
.WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x02)));
EXPECT_CALL(crypto, pqe_keygen(_))
.WillOnce(WithArg< 0 >(FillArg< PQKeyPair >(0x03)));
service::Identity identity;
ASSERT_TRUE(identity.EnsureKeys(p, &crypto));
ASSERT_TRUE(fs::exists(fs::status(p)));
// Verify what is on disk is what is what was generated
service::Identity other;
// No need to set more mocks, as we shouldn't need to re-keygen
ASSERT_TRUE(other.EnsureKeys(p, &crypto));
ASSERT_EQ(identity, other);
}
TEST_F(ServiceIdentityTest, EnsureKeysDir)
{
fs::path p = test::randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
test::FileGuard guard(p);
std::error_code code;
ASSERT_TRUE(fs::create_directory(p, code)) << code;
service::Identity identity;
ASSERT_FALSE(identity.EnsureKeys(p, &crypto));
}
TEST_F(ServiceIdentityTest, EnsureKeysBrokenFile)
{
fs::path p = test::randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
test::FileGuard guard(p);
std::error_code code;
std::fstream file;
file.open(p.string(), std::ios::out);
ASSERT_TRUE(file.is_open()) << p;
file.close();
service::Identity identity;
ASSERT_FALSE(identity.EnsureKeys(p, &crypto));
}

@ -35,7 +35,7 @@ namespace llarp
{
if(fs::exists(fs::status(p)))
{
fs::remove(p);
fs::remove_all(p);
}
}
};

@ -120,6 +120,7 @@
<Content Include="lokinet.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!--
<PropertyGroup>
<PreBuildEvent>$(SolutionDir)versioning\NetRevisionTool /info /patch "$(ProjectDir)"
$(SolutionDir)versioning\release.bat</PreBuildEvent>
@ -128,4 +129,5 @@ $(SolutionDir)versioning\release.bat</PreBuildEvent>
<PostBuildEvent>$(SolutionDir)versioning\NetRevisionTool /restore "$(ProjectDir)"
$(SolutionDir)versioning\unpatch.bat</PostBuildEvent>
</PropertyGroup>
</Project>
-->
</Project>

Loading…
Cancel
Save