The Great Wall of Blame

This commit reflects changes to clang-format rules. Unfortunately,
these rule changes create a massive change to the codebase, which
causes an apparent rewrite of git history.

Git blame's --ignore-rev flag can be used to ignore this commit when
attempting to `git blame` some code.
pull/1215/head
Stephen Shelton 4 years ago
parent 30e7c7f828
commit 273270916e
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -71,18 +71,15 @@ namespace
std::string address = config.api.rpcBindAddr() + "/jsonrpc";
const nlohmann::json request{{"method", command},
{"params", nlohmann::json::object()},
{"id", "foo"}};
const nlohmann::json request{
{"method", command}, {"params", nlohmann::json::object()}, {"id", "foo"}};
const std::string requestStr = request.dump();
std::unique_ptr<curl_slist, void (*)(curl_slist*)> chunk(
curl_slist_append(nullptr, "content-type: application/json"),
&curl_slist_free_all);
curl_slist_append(nullptr, "content-type: application/json"), &curl_slist_free_all);
std::unique_ptr< CURL, void (*)(CURL*) > curl(curl_easy_init(),
&curl_easy_cleanup);
std::unique_ptr<CURL, void (*)(CURL*)> curl(curl_easy_init(), &curl_easy_cleanup);
curl_easy_setopt(curl.get(), CURLOPT_URL, address.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, requestStr.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDSIZE, requestStr.size());
@ -109,21 +106,21 @@ namespace
int
main(int argc, char* argv[])
{
cxxopts::Options options("lokinetctl",
cxxopts::Options options(
"lokinetctl",
"LokiNET is a free, open source, private, "
"decentralized, \"market based sybil resistant\" "
"and IP based onion routing network");
options.add_options()("v,verbose", "Verbose", cxxopts::value<bool>())(
"h,help", "help", cxxopts::value<bool>())(
"c,config", "config file",
cxxopts::value< std::string >()->default_value(
llarp::GetDefaultConfigPath().string()))
"c,config",
"config file",
cxxopts::value<std::string>()->default_value(llarp::GetDefaultConfigPath().string()))
#ifdef WITH_CURL
("j,jsonrpc", "hit json rpc endpoint", cxxopts::value<std::string>())
#endif
("dump", "dump rc file",
cxxopts::value< std::vector< std::string > >(), "FILE");
("dump", "dump rc file", cxxopts::value<std::vector<std::string>>(), "FILE");
try
{
@ -160,8 +157,7 @@ main(int argc, char* argv[])
#ifdef WITH_CURL
if (result.count("jsonrpc") > 0)
{
if(!executeJsonRpc(result["jsonrpc"].as< std::string >(),
result["config"].as< std::string >()))
if (!executeJsonRpc(result["jsonrpc"].as<std::string>(), result["config"].as<std::string>()))
{
return 1;
}

@ -103,22 +103,21 @@ main(int argc, char *argv[])
// SetUnhandledExceptionFilter(win32_signal_handler);
#endif
cxxopts::Options options("lokinet",
cxxopts::Options options(
"lokinet",
"LokiNET is a free, open source, private, "
"decentralized, \"market based sybil resistant\" "
"and IP based onion routing network");
options.add_options()("v,verbose", "Verbose", cxxopts::value<bool>())(
"h,help", "help", cxxopts::value< bool >())("version", "version",
cxxopts::value< bool >())(
"h,help", "help", cxxopts::value<bool>())("version", "version", cxxopts::value<bool>())(
"g,generate", "generate client config", cxxopts::value<bool>())(
"r,router", "generate router config", cxxopts::value<bool>())(
"f,force", "overwrite", cxxopts::value<bool>())(
"c,colour", "colour output",
cxxopts::value< bool >()->default_value("true"))(
"c,colour", "colour output", cxxopts::value<bool>()->default_value("true"))(
"b,background",
"background mode (start, but do not connect to the network)",
cxxopts::value< bool >())("config", "path to configuration file",
cxxopts::value< std::string >());
cxxopts::value<bool>())(
"config", "path to configuration file", cxxopts::value<std::string>());
options.parse_positional("config");
@ -206,8 +205,7 @@ main(int argc, char *argv[])
{
if (ec)
{
llarp::LogError("failed to create '", basedir.string(),
"': ", ec.message());
llarp::LogError("failed to create '", basedir.string(), "': ", ec.message());
return 1;
}
}
@ -215,8 +213,7 @@ main(int argc, char *argv[])
if (genconfigOnly)
{
if(!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(),
overWrite, asRouter))
if (!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(), overWrite, asRouter))
return 1;
}
else
@ -241,8 +238,7 @@ main(int argc, char *argv[])
{
if (ec)
{
llarp::LogError("failed to create '", basepath.string(),
"': ", ec.message());
llarp::LogError("failed to create '", basepath.string(), "': ", ec.message());
return 1;
}
}
@ -250,8 +246,8 @@ main(int argc, char *argv[])
auto fpath = llarp::GetDefaultConfigPath();
// if using default INI file, we're create it even if you don't ask us too
if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(),
overWrite, asRouter))
if (!llarp_ensure_config(
fpath.string().c_str(), basepath.string().c_str(), overWrite, asRouter))
return 1;
conffname = fpath.string();
}

@ -15,8 +15,7 @@ extern "C"
/// if basedir is not nullptr then use basedir as an absolute
/// base path for all files in config
bool
llarp_ensure_config(const char *, const char *, bool overwrite,
bool asrouter);
llarp_ensure_config(const char*, const char*, bool overwrite, bool asrouter);
/// llarp application context for C api
struct llarp_main;
@ -96,16 +95,14 @@ extern "C"
/// returns -1 on error, returns size of packet read
/// thread safe
ssize_t
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader *r, unsigned char *dst,
size_t dstlen);
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader* r, unsigned char* dst, size_t dstlen);
/// blocking write on packet writer to lokinet internals
/// returns false if we can't write this packet
/// return true if we wrote this packet
/// thread safe
bool
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer *w, unsigned char *pktbuf,
size_t pktlen);
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer* w, unsigned char* pktbuf, size_t pktlen);
/// close vpn io and free private implementation after done
/// operation is async and calls llarp_vpn_io.closed after fully closed
@ -123,17 +120,18 @@ extern "C"
/// deferred call to llarp_vpn_io.injected is queued unconditionally
/// thread safe
bool
llarp_main_inject_vpn_by_name(struct llarp_main *m, const char *epName,
llarp_main_inject_vpn_by_name(
struct llarp_main* m,
const char* epName,
struct llarp_vpn_io* io,
struct llarp_vpn_ifaddr_info info);
/// give main context a vpn io on its default endpoint
static bool
llarp_main_inject_default_vpn(struct llarp_main *m, struct llarp_vpn_io *io,
struct llarp_vpn_ifaddr_info info)
llarp_main_inject_default_vpn(
struct llarp_main* m, struct llarp_vpn_io* io, struct llarp_vpn_ifaddr_info info)
{
return llarp_main_inject_vpn_by_name(
m, llarp_main_get_default_endpoint_name(m), io, info);
return llarp_main_inject_vpn_by_name(m, llarp_main_get_default_endpoint_name(m), io, info);
}
/// load config from file by name

@ -206,8 +206,7 @@ extern "C"
*/
TUNTAP_EXPORT int
tuntap_set_ip(struct device *, const char *srcaddr, const char *dstaddr,
int netmask);
tuntap_set_ip(struct device*, const char* srcaddr, const char* dstaddr, int netmask);
// TUNTAP_EXPORT int tuntap_set_ip_old(struct device *, const char
// *, int);
/*TUNTAP_EXPORT int tuntap_set_ip_old(struct device *, const char
@ -250,8 +249,8 @@ extern "C"
int
tuntap_sys_set_ipv4_tap(struct device*, t_tun_in_addr*, uint32_t);
int
tuntap_sys_set_ipv4_tun(struct device *dev, t_tun_in_addr *s4,
t_tun_in_addr *s4dest, uint32_t bits, int netmask);
tuntap_sys_set_ipv4_tun(
struct device* dev, t_tun_in_addr* s4, t_tun_in_addr* s4dest, uint32_t bits, int netmask);
#endif
int

@ -20,8 +20,7 @@ extern "C"
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self,
jstring fname)
Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self, jstring fname)
{
llarp_config* conf = GetImpl<llarp_config>(env, self);
if (conf == nullptr)

@ -22,8 +22,7 @@ extern "C"
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv *env, jobject self,
jobject conf)
Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv* env, jobject self, jobject conf)
{
llarp_main* ptr = GetImpl<llarp_main>(env, self);
llarp_config* config = GetImpl<llarp_config>(env, conf);
@ -48,8 +47,7 @@ extern "C"
Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv* env, jobject self)
{
llarp_main* ptr = GetImpl<llarp_main>(env, self);
return (ptr != nullptr && llarp_main_is_running(ptr)) ? JNI_TRUE
: JNI_FALSE;
return (ptr != nullptr && llarp_main_is_running(ptr)) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL
@ -65,8 +63,7 @@ extern "C"
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv *env, jobject self,
jobject vpn)
Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv* env, jobject self, jobject vpn)
{
llarp_main* ptr = GetImpl<llarp_main>(env, self);
lokinet_jni_vpnio* impl = GetImpl<lokinet_jni_vpnio>(env, vpn);
@ -76,8 +73,6 @@ extern "C"
return JNI_FALSE;
if (not impl->Init(ptr))
return JNI_FALSE;
return llarp_main_inject_default_vpn(ptr, &impl->io, impl->info)
? JNI_TRUE
: JNI_FALSE;
return llarp_main_inject_default_vpn(ptr, &impl->io, impl->info) ? JNI_TRUE : JNI_FALSE;
}
}

@ -12,12 +12,10 @@ static T
VisitStringAsStringView(JNIEnv* env, jobject str, V visit)
{
const jclass stringClass = env->GetObjectClass(str);
const jmethodID getBytes =
env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jstring charsetName = env->NewStringUTF("UTF-8");
const jbyteArray stringJbytes =
(jbyteArray)env->CallObjectMethod(str, getBytes, charsetName);
const jbyteArray stringJbytes = (jbyteArray)env->CallObjectMethod(str, getBytes, charsetName);
env->DeleteLocalRef(charsetName);
const size_t length = env->GetArrayLength(stringJbytes);
@ -55,8 +53,7 @@ FromObjectMember(JNIEnv* env, jobject self, const char* membername)
/// visit object string member called membername as bytes
template <typename T, typename V>
static T
VisitObjectMemberStringAsStringView(JNIEnv* env, jobject self,
const char* membername, V v)
VisitObjectMemberStringAsStringView(JNIEnv* env, jobject self, const char* membername, V v)
{
jclass cl = env->GetObjectClass(self);
jfieldID name = env->GetFieldID(cl, membername, "Ljava/lang/String;");

@ -37,8 +37,7 @@ extern "C"
}
JNIEXPORT jint JNICALL
Java_network_loki_lokinet_LokinetVPN_ReadPkt(JNIEnv *env, jobject self,
jobject pkt)
Java_network_loki_lokinet_LokinetVPN_ReadPkt(JNIEnv* env, jobject self, jobject pkt)
{
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
if (vpn == nullptr)
@ -51,8 +50,7 @@ extern "C"
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetVPN_WritePkt(JNIEnv *env, jobject self,
jobject pkt)
Java_network_loki_lokinet_LokinetVPN_WritePkt(JNIEnv* env, jobject self, jobject pkt)
{
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
if (vpn == nullptr)
@ -65,8 +63,7 @@ extern "C"
}
JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetVPN_SetInfo(JNIEnv *env, jobject self,
jobject info)
Java_network_loki_lokinet_LokinetVPN_SetInfo(JNIEnv* env, jobject self, jobject info)
{
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
if (vpn == nullptr)

@ -22,8 +22,7 @@ extern "C"
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_startLokinet(JNIEnv *, jclass,
jstring);
Java_network_loki_lokinet_Lokinet_1JNI_startLokinet(JNIEnv*, jclass, jstring);
JNIEXPORT jstring JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_getIfAddr(JNIEnv*, jclass);
@ -40,8 +39,7 @@ extern "C"
Java_network_loki_lokinet_Lokinet_1JNI_stopLokinet(JNIEnv*, jclass);
JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_setVPNFileDescriptor(JNIEnv *, jclass,
jint, jint);
Java_network_loki_lokinet_Lokinet_1JNI_setVPNFileDescriptor(JNIEnv*, jclass, jint, jint);
/*
* Class: network_loki_lokinet_Lokinet_JNI
@ -49,8 +47,7 @@ extern "C"
* Signature: (Z)V
*/
JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_onNetworkStateChanged(JNIEnv *, jclass,
jboolean);
Java_network_loki_lokinet_Lokinet_1JNI_onNetworkStateChanged(JNIEnv*, jclass, jboolean);
#ifdef __cplusplus
}

@ -73,8 +73,7 @@ namespace abyss
/// must be called after RunAsync returns true
/// queue a call for rpc
void
QueueRPC(RPC_Method_t method, RPC_Params params,
HandlerFactory createHandler);
QueueRPC(RPC_Method_t method, RPC_Params params, HandlerFactory createHandler);
/// drop all pending calls on the floor
void
@ -99,9 +98,7 @@ namespace abyss
struct Call
{
Call(RPC_Method_t&& m, RPC_Params&& p, HandlerFactory&& f)
: method(std::move(m))
, params(std::move(p))
, createHandler(std::move(f))
: method(std::move(m)), params(std::move(p)), createHandler(std::move(f))
{
}
RPC_Method_t method;

@ -27,8 +27,7 @@ struct MD5
m.Update((const unsigned char*)str.c_str(), dist);
m.Final(digest.data());
std::string hex;
std::for_each(digest.begin(), digest.end(),
[&hex](const unsigned char& ch) {
std::for_each(digest.begin(), digest.end(), [&hex](const unsigned char& ch) {
char tmpbuf[4] = {0};
std::snprintf(tmpbuf, sizeof(tmpbuf), "%.2x", ch);
hex += std::string(tmpbuf);

@ -51,8 +51,8 @@ namespace abyss
virtual ~BaseReqHandler();
bool
ServeAsync(llarp_ev_loop_ptr loop, std::shared_ptr< llarp::Logic > logic,
const sockaddr* bindaddr);
ServeAsync(
llarp_ev_loop_ptr loop, std::shared_ptr<llarp::Logic> logic, const sockaddr* bindaddr);
void
RemoveConn(IRPCHandler* handler);

@ -26,11 +26,11 @@ struct DemoCall : public abyss::http::IRPCClientHandler
std::function<void(void)> m_Callback;
std::shared_ptr<llarp::Logic> m_Logic;
DemoCall(abyss::http::ConnImpl* impl, std::shared_ptr< llarp::Logic > logic,
DemoCall(
abyss::http::ConnImpl* impl,
std::shared_ptr<llarp::Logic> logic,
std::function<void(void)> callback)
: abyss::http::IRPCClientHandler(impl)
, m_Callback(callback)
, m_Logic(logic)
: abyss::http::IRPCClientHandler(impl), m_Callback(callback), m_Logic(logic)
{
llarp::LogInfo("new call");
}
@ -73,7 +73,9 @@ struct DemoClient : public abyss::http::JSONRPC
void
DoDemoRequest()
{
QueueRPC("test", nlohmann::json::object(),
QueueRPC(
"test",
nlohmann::json::object(),
std::bind(&DemoClient::NewConn, this, std::placeholders::_1));
Flush();
}

@ -32,8 +32,11 @@ namespace abyss
State state;
ConnImpl(llarp_tcp_conn* conn, JSONRPC* parent,
const RPC_Method_t& method, const RPC_Params& params,
ConnImpl(
llarp_tcp_conn* conn,
JSONRPC* parent,
const RPC_Method_t& method,
const RPC_Params& params,
JSONRPC::HandlerFactory factory)
: m_Conn(conn)
, m_Parent(parent)
@ -153,8 +156,7 @@ namespace abyss
auto strip = [&opts](const std::string& name) -> std::string {
std::string val;
std::for_each(opts[name].begin(), opts[name].end(),
[&val](const char& ch) {
std::for_each(opts[name].begin(), opts[name].end(), [&val](const char& ch) {
if (ch != '"')
val += ch;
});
@ -166,28 +168,24 @@ namespace abyss
const auto qop = strip("qop");
std::string nonceCount = "0000000" + std::to_string(m_AuthTries);
std::string str =
m_Parent->username + ":" + realm + ":" + m_Parent->password;
std::string str = m_Parent->username + ":" + realm + ":" + m_Parent->password;
std::string h1 = MD5::SumHex(str);
str = "POST:/json_rpc";
std::string h2 = MD5::SumHex(str);
llarp::AlignedBuffer<8> n;
n.Randomize();
std::string cnonce = n.ToHex();
str = h1 + ":" + nonce + ":" + nonceCount + ":" + cnonce + ":" + qop
+ ":" + h2;
str = h1 + ":" + nonce + ":" + nonceCount + ":" + cnonce + ":" + qop + ":" + h2;
auto responseH = MD5::SumHex(str);
authgen << "Digest username=\"" << m_Parent->username + "\", realm=\""
<< realm
<< "\", uri=\"/json_rpc\", algorithm=MD5, qop=auth, nonce=\""
<< nonce << "\", response=\"" << responseH
<< "\", nc=" << nonceCount << ", cnonce=\"" << cnonce << "\"";
authgen << "Digest username=\"" << m_Parent->username + "\", realm=\"" << realm
<< "\", uri=\"/json_rpc\", algorithm=MD5, qop=auth, nonce=\"" << nonce
<< "\", response=\"" << responseH << "\", nc=" << nonceCount << ", cnonce=\""
<< cnonce << "\"";
for (const auto& opt : opts)
{
if(opt.first == "algorithm" || opt.first == "realm"
|| opt.first == "qop" || opt.first == "nonce"
|| opt.first == "stale")
if (opt.first == "algorithm" || opt.first == "realm" || opt.first == "qop"
|| opt.first == "nonce" || opt.first == "stale")
continue;
authgen << ", " << opt.first << "=" << opt.second;
}
@ -332,8 +330,7 @@ namespace abyss
request << "\r\n" << body;
std::string buf = request.str();
if(!llarp_tcp_conn_async_write(m_Conn,
llarp_buffer_t(buf.c_str(), buf.size())))
if (!llarp_tcp_conn_async_write(m_Conn, llarp_buffer_t(buf.c_str(), buf.size())))
{
CloseError("failed to write request");
return;
@ -400,12 +397,10 @@ namespace abyss
}
void
JSONRPC::QueueRPC(RPC_Method_t method, RPC_Params params,
HandlerFactory createHandler)
JSONRPC::QueueRPC(RPC_Method_t method, RPC_Params params, HandlerFactory createHandler)
{
if (m_Run)
m_PendingCalls.emplace_back(std::move(method), std::move(params),
std::move(createHandler));
m_PendingCalls.emplace_back(std::move(method), std::move(params), std::move(createHandler));
}
bool
@ -446,8 +441,8 @@ namespace abyss
return;
}
auto& front = m_PendingCalls.front();
ConnImpl* connimpl = new ConnImpl(conn, this, front.method, front.params,
front.createHandler);
ConnImpl* connimpl =
new ConnImpl(conn, this, front.method, front.params, front.createHandler);
m_PendingCalls.pop_front();
m_Conns.emplace_back(connimpl->handler);
connimpl->SendRequest();

@ -7,12 +7,10 @@ void
Transform(uint32_t* buf, uint32_t* in);
static unsigned char PADDING[64] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/* F, G and H are basic MD5 functions: selection, majority, parity */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))

@ -95,21 +95,25 @@ namespace abyss
ShouldProcessHeader(const string_view& name) const
{
// TODO: header whitelist
return name == string_view("content-type")
|| name == string_view("content-length")
return name == string_view("content-type") || name == string_view("content-length")
|| name == string_view("host");
}
bool
WriteResponseSimple(int code, const std::string& msg,
const char* contentType, const char* content)
WriteResponseSimple(
int code, const std::string& msg, const char* contentType, const char* content)
{
char buf[512] = {0};
size_t contentLength = strlen(content);
int sz = snprintf(buf, sizeof(buf),
int sz = snprintf(
buf,
sizeof(buf),
"HTTP/1.0 %d %s\r\nContent-Type: "
"%s\r\nContent-Length: %zu\r\n\r\n",
code, msg.c_str(), contentType, contentLength);
code,
msg.c_str(),
contentType,
contentLength);
if (sz <= 0)
return false;
if (!llarp_tcp_conn_async_write(_conn, llarp_buffer_t(buf, sz)))
@ -117,8 +121,7 @@ namespace abyss
m_State = eWriteHTTPBody;
return llarp_tcp_conn_async_write(
_conn, llarp_buffer_t(content, contentLength));
return llarp_tcp_conn_async_write(_conn, llarp_buffer_t(content, contentLength));
}
bool
@ -126,22 +129,19 @@ namespace abyss
{
if (Header.Method != "POST")
{
return WriteResponseSimple(405, "Method Not Allowed", "text/plain",
"nope");
return WriteResponseSimple(405, "Method Not Allowed", "text/plain", "nope");
}
{
auto itr = Header.Headers.find("content-type");
if (itr == Header.Headers.end())
{
return WriteResponseSimple(415, "Unsupported Media Type",
"text/plain",
"no content type provided");
return WriteResponseSimple(
415, "Unsupported Media Type", "text/plain", "no content type provided");
}
else if (itr->second != string_view("application/json"))
{
return WriteResponseSimple(415, "Unsupported Media Type",
"text/plain",
"this does not look like jsonrpc 2.0");
return WriteResponseSimple(
415, "Unsupported Media Type", "text/plain", "this does not look like jsonrpc 2.0");
}
}
// initialize body parser
@ -150,14 +150,12 @@ namespace abyss
auto itr = Header.Headers.find("content-length");
if (itr == Header.Headers.end())
{
return WriteResponseSimple(400, "Bad Request", "text/plain",
"no content length");
return WriteResponseSimple(400, "Bad Request", "text/plain", "no content length");
}
ssize_t contentLength = std::stoll(itr->second);
if (contentLength <= 0)
{
return WriteResponseSimple(400, "Bad Request", "text/plain",
"bad content length");
return WriteResponseSimple(400, "Bad Request", "text/plain", "bad content length");
}
else
{
@ -166,19 +164,16 @@ namespace abyss
itr = Header.Headers.find("host");
if (itr == Header.Headers.end())
{
return WriteResponseSimple(400, "Bad Request", "text/plain",
"no host header provided");
return WriteResponseSimple(400, "Bad Request", "text/plain", "no host header provided");
}
if (not handler->ValidateHost(itr->second))
{
return WriteResponseSimple(400, "Bad Request", "text/plain",
"invalid host header");
return WriteResponseSimple(400, "Bad Request", "text/plain", "invalid host header");
}
}
if (!m_BodyParser->FeedData(buf, sz))
{
return WriteResponseSimple(400, "Bad Request", "text/plain",
"invalid body size");
return WriteResponseSimple(400, "Bad Request", "text/plain", "invalid body size");
}
switch (m_BodyParser->Parse(m_Request))
@ -186,28 +181,24 @@ namespace abyss
case json::IParser::eNeedData:
return true;
case json::IParser::eParseError:
return WriteResponseSimple(400, "Bad Request", "text/plain",
"bad json object");
return WriteResponseSimple(400, "Bad Request", "text/plain", "bad json object");
case json::IParser::eDone:
if(m_Request.is_object() && m_Request.count("params")
&& m_Request.count("method") && m_Request.count("id")
&& m_Request["id"].is_string() && m_Request["method"].is_string()
&& m_Request["params"].is_object())
if (m_Request.is_object() && m_Request.count("params") && m_Request.count("method")
&& m_Request.count("id") && m_Request["id"].is_string()
&& m_Request["method"].is_string() && m_Request["params"].is_object())
{
nlohmann::json response;
response["jsonrpc"] = "2.0";
response["id"] = m_Request["id"].get<std::string>();
auto value = handler->HandleJSONRPC(
m_Request["method"].get< std::string >(),
m_Request["params"]);
m_Request["method"].get<std::string>(), m_Request["params"]);
if (!value.is_null())
response["result"] = std::move(value);
return WriteResponseJSON(response);
}
return WriteResponseSimple(500, "internal error", "text/plain",
"nope");
return WriteResponseSimple(500, "internal error", "text/plain", "nope");
default:
return false;
}
@ -217,8 +208,7 @@ namespace abyss
WriteResponseJSON(const nlohmann::json& response)
{
std::string responseStr = response.dump();
return WriteResponseSimple(200, "OK", "application/json",
responseStr.c_str());
return WriteResponseSimple(200, "OK", "application/json", responseStr.c_str());
}
bool
@ -309,8 +299,7 @@ namespace abyss
bool
ShouldClose(llarp_time_t now) const
{
return now - m_LastActive > m_ReadTimeout || m_Bad
|| m_State == eCloseMe;
return now - m_LastActive > m_ReadTimeout || m_Bad || m_State == eCloseMe;
}
void
@ -340,8 +329,7 @@ namespace abyss
return m_Impl->ShouldClose(now);
}
BaseReqHandler::BaseReqHandler(llarp_time_t reqtimeout)
: m_ReqTimeout(reqtimeout)
BaseReqHandler::BaseReqHandler(llarp_time_t reqtimeout) : m_ReqTimeout(reqtimeout)
{
m_acceptor.accepted = &BaseReqHandler::OnAccept;
m_acceptor.user = this;
@ -350,9 +338,8 @@ namespace abyss
}
bool
BaseReqHandler::ServeAsync(llarp_ev_loop_ptr loop,
std::shared_ptr< llarp::Logic > logic,
const sockaddr* bindaddr)
BaseReqHandler::ServeAsync(
llarp_ev_loop_ptr loop, std::shared_ptr<llarp::Logic> logic, const sockaddr* bindaddr)
{
m_loop = loop;
m_Logic = logic;

@ -85,8 +85,13 @@ netlink_send(int p_socket, int p_request)
struct sockaddr_nl l_addr;
memset(&l_addr, 0, sizeof(l_addr));
l_addr.nl_family = AF_NETLINK;
return (sendto(p_socket, &l_data.m_hdr, l_data.m_hdr.nlmsg_len, 0,
(struct sockaddr *)&l_addr, sizeof(l_addr)));
return (sendto(
p_socket,
&l_data.m_hdr,
l_data.m_hdr.nlmsg_len,
0,
(struct sockaddr*)&l_addr,
sizeof(l_addr)));
}
static int
@ -150,12 +155,10 @@ getNetlinkResponse(int p_socket, int *p_size, int *p_done)
{
pid_t l_pid = getpid();
struct nlmsghdr* l_hdr;
for(l_hdr = (struct nlmsghdr *)l_buffer;
NLMSG_OK(l_hdr, (unsigned int)l_read);
for (l_hdr = (struct nlmsghdr*)l_buffer; NLMSG_OK(l_hdr, (unsigned int)l_read);
l_hdr = (struct nlmsghdr*)NLMSG_NEXT(l_hdr, l_read))
{
if((pid_t)l_hdr->nlmsg_pid != l_pid
|| (int)l_hdr->nlmsg_seq != p_socket)
if ((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
{
continue;
}
@ -263,17 +266,15 @@ calcAddrLen(sa_family_t p_family, int p_dataSize)
case AF_INET6:
return sizeof(struct sockaddr_in6);
case AF_PACKET:
return maxSize(sizeof(struct sockaddr_ll),
offsetof(struct sockaddr_ll, sll_addr) + p_dataSize);
return maxSize(
sizeof(struct sockaddr_ll), offsetof(struct sockaddr_ll, sll_addr) + p_dataSize);
default:
return maxSize(sizeof(struct sockaddr),
offsetof(struct sockaddr, sa_data) + p_dataSize);
return maxSize(sizeof(struct sockaddr), offsetof(struct sockaddr, sa_data) + p_dataSize);
}
}
static void
makeSockaddr(sa_family_t p_family, struct sockaddr *p_dest, void *p_data,
size_t p_size)
makeSockaddr(sa_family_t p_family, struct sockaddr* p_dest, void* p_data, size_t p_size)
{
switch (p_family)
{
@ -323,8 +324,7 @@ interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
struct rtattr* l_rta;
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void* l_rtaData = RTA_DATA(l_rta);
(void)l_rtaData;
@ -346,8 +346,8 @@ interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
}
}
struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + sizeof(int)
+ l_nameSize + l_addrSize + l_dataSize);
struct ifaddrs* l_entry =
malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
if (l_entry == NULL)
{
return -1;
@ -366,8 +366,7 @@ interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
l_entry->ifa_flags = l_info->ifi_flags;
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void* l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
@ -377,8 +376,7 @@ interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
case IFLA_BROADCAST:
{
size_t l_addrLen = calcAddrLen(AF_PACKET, l_rtaDataSize);
makeSockaddr(AF_PACKET, (struct sockaddr *)l_addr, l_rtaData,
l_rtaDataSize);
makeSockaddr(AF_PACKET, (struct sockaddr*)l_addr, l_rtaData, l_rtaDataSize);
((struct sockaddr_ll*)l_addr)->sll_ifindex = l_info->ifi_index;
((struct sockaddr_ll*)l_addr)->sll_hatype = l_info->ifi_type;
if (l_rta->rta_type == IFLA_ADDRESS)
@ -432,12 +430,10 @@ findInterface(int p_index, struct ifaddrs **p_links, int p_numLinks)
}
static int
interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
int p_numLinks)
interpretAddr(struct nlmsghdr* p_hdr, struct ifaddrs** p_resultList, int p_numLinks)
{
struct ifaddrmsg* l_info = (struct ifaddrmsg*)NLMSG_DATA(p_hdr);
struct ifaddrs *l_interface =
findInterface(l_info->ifa_index, p_resultList, p_numLinks);
struct ifaddrs* l_interface = findInterface(l_info->ifa_index, p_resultList, p_numLinks);
if (l_info->ifa_family == AF_PACKET)
{
@ -451,8 +447,7 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
struct rtattr* l_rta;
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void* l_rtaData = RTA_DATA(l_rta);
(void)l_rtaData;
@ -462,16 +457,13 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
{
case IFA_ADDRESS:
case IFA_LOCAL:
if((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6)
&& !l_addedNetmask)
if ((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
{ // make room for netmask
l_addrSize +=
NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
l_addedNetmask = 1;
}
case IFA_BROADCAST:
l_addrSize +=
NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
break;
case IFA_LABEL:
l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
@ -481,8 +473,7 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
}
}
struct ifaddrs *l_entry =
malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
struct ifaddrs* l_entry = malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
if (l_entry == NULL)
{
return -1;
@ -500,8 +491,7 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
}
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void* l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
@ -512,8 +502,7 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
case IFA_LOCAL:
{
size_t l_addrLen = calcAddrLen(l_info->ifa_family, l_rtaDataSize);
makeSockaddr(l_info->ifa_family, (struct sockaddr *)l_addr, l_rtaData,
l_rtaDataSize);
makeSockaddr(l_info->ifa_family, (struct sockaddr*)l_addr, l_rtaData, l_rtaDataSize);
if (l_info->ifa_family == AF_INET6)
{
if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr*)l_rtaData)
@ -561,13 +550,10 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
}
if (l_entry->ifa_addr
&& (l_entry->ifa_addr->sa_family == AF_INET
|| l_entry->ifa_addr->sa_family == AF_INET6))
&& (l_entry->ifa_addr->sa_family == AF_INET || l_entry->ifa_addr->sa_family == AF_INET6))
{
unsigned l_maxPrefix = (l_entry->ifa_addr->sa_family == AF_INET ? 32 : 128);
unsigned l_prefix =
(l_info->ifa_prefixlen > l_maxPrefix ? l_maxPrefix
: l_info->ifa_prefixlen);
unsigned l_prefix = (l_info->ifa_prefixlen > l_maxPrefix ? l_maxPrefix : l_info->ifa_prefixlen);
char l_mask[16] = {0};
unsigned i;
for (i = 0; i < (l_prefix / 8); ++i)
@ -579,8 +565,7 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
l_mask[i] = 0xff << (8 - (l_prefix % 8));
}
makeSockaddr(l_entry->ifa_addr->sa_family, (struct sockaddr *)l_addr,
l_mask, l_maxPrefix / 8);
makeSockaddr(l_entry->ifa_addr->sa_family, (struct sockaddr*)l_addr, l_mask, l_maxPrefix / 8);
l_entry->ifa_netmask = (struct sockaddr*)l_addr;
}
@ -589,8 +574,7 @@ interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
}
static int
interpretLinks(int p_socket, NetlinkList *p_netlinkList,
struct ifaddrs **p_resultList)
interpretLinks(int p_socket, NetlinkList* p_netlinkList, struct ifaddrs** p_resultList)
{
int l_numLinks = 0;
pid_t l_pid = getpid();
@ -625,8 +609,8 @@ interpretLinks(int p_socket, NetlinkList *p_netlinkList,
}
static int
interpretAddrs(int p_socket, NetlinkList *p_netlinkList,
struct ifaddrs **p_resultList, int p_numLinks)
interpretAddrs(
int p_socket, NetlinkList* p_netlinkList, struct ifaddrs** p_resultList, int p_numLinks)
{
pid_t l_pid = getpid();
for (; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
@ -690,8 +674,7 @@ getifaddrs(struct ifaddrs **ifap)
int l_result = 0;
int l_numLinks = interpretLinks(l_socket, l_linkResults, ifap);
if(l_numLinks == -1
|| interpretAddrs(l_socket, l_addrResults, ifap, l_numLinks) == -1)
if (l_numLinks == -1 || interpretAddrs(l_socket, l_addrResults, ifap, l_numLinks) == -1)
{
l_result = -1;
}

@ -309,8 +309,8 @@ namespace llarp
if (key == "*")
{
m_OutboundLink = std::make_tuple(
"*", AF_INET, fromEnv(proto, "OUTBOUND_PORT"), std::move(opts));
m_OutboundLink =
std::make_tuple("*", AF_INET, fromEnv(proto, "OUTBOUND_PORT"), std::move(opts));
}
else
{
@ -438,8 +438,8 @@ namespace llarp
}
else
{
LogError("failed to open log file at '", val,
"' for an unknown reason, bailing tf out kbai");
LogError(
"failed to open log file at '", val, "' for an unknown reason, bailing tf out kbai");
::abort();
}
}
@ -533,8 +533,7 @@ namespace llarp
/// fname should be a relative path (from CWD) or absolute path to the config
/// file
extern "C" bool
llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
bool asRouter)
llarp_ensure_config(const char* fname, const char* basedir, bool overwrite, bool asRouter)
{
if (Lokinet_INIT())
return false;
@ -578,8 +577,7 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
}
// write fname ini
auto optional_f =
llarp::util::OpenFileStream< std::ofstream >(fname, std::ios::binary);
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fname, std::ios::binary);
if (!optional_f || !optional_f.value().is_open())
{
llarp::LogError("failed to open ", fname, " for writing");
@ -600,8 +598,7 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
}
void
llarp_generic_ensure_config(std::ofstream &f, std::string basepath,
bool isRouter)
llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter)
{
f << "# this configuration was auto generated with 'sane' defaults\n";
f << "# change these values as desired\n";
@ -618,8 +615,7 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath,
f << "# encryption key for onion routing\n";
f << "encryption-privkey=" << basepath << "encryption.private\n";
f << std::endl;
f << "# uncomment following line to set router nickname to 'lokinet'"
<< std::endl;
f << "# uncomment following line to set router nickname to 'lokinet'" << std::endl;
f << "#nickname=lokinet\n";
const auto limits = isRouter ? llarp::limits::snode : llarp::limits::client;
@ -739,8 +735,7 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath)
// write snapp-example.ini
const std::string snappExample_fpath = basepath + "snapp-example.ini";
{
auto stream = llarp::util::OpenFileStream< std::ofstream >(
snappExample_fpath, std::ios::binary);
auto stream = llarp::util::OpenFileStream<std::ofstream>(snappExample_fpath, std::ios::binary);
if (!stream)
{
return false;

@ -271,8 +271,7 @@ namespace llarp
} // namespace llarp
void
llarp_generic_ensure_config(std::ofstream& f, std::string basepath,
bool isRouter);
llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter);
void
llarp_ensure_router_config(std::ofstream& f, std::string basepath);

@ -143,8 +143,7 @@ namespace llarp
}
void
ConfigParser::IterAll(
std::function< void(string_view, const Section_t&) > visit)
ConfigParser::IterAll(std::function<void(string_view, const Section_t&)> visit)
{
for (const auto& item : m_Config)
visit(item.first, item.second);
@ -152,8 +151,7 @@ namespace llarp
bool
ConfigParser::VisitSection(
const char* name,
std::function< bool(const Section_t& sect) > visit) const
const char* name, std::function<bool(const Section_t& sect)> visit) const
{
auto itr = m_Config.find(name);
if (itr == m_Config.end())

@ -37,8 +37,7 @@ namespace llarp
/// visit a section in config read only by name
/// return false if no section or value propagated from visitor
bool
VisitSection(const char* name,
std::function< bool(const Section_t&) > visit) const;
VisitSection(const char* name, std::function<bool(const Section_t&)> visit) const;
private:
bool

@ -65,7 +65,9 @@ namespace llarp
}
else
{
LogWarn("Our RouterContact ", m_rcPath,
LogWarn(
"Our RouterContact ",
m_rcPath,
" seems out of date, backing up and regenerating private keys");
if (!backupKeyFilesByMoving())
@ -133,8 +135,7 @@ namespace llarp
bool exists = fs::exists(filepath, ec);
if (ec)
{
LogError("Could not determine status of file ", filepath, ": ",
ec.message());
LogError("Could not determine status of file ", filepath, ": ", ec.message());
return false;
}
@ -151,8 +152,7 @@ namespace llarp
return false;
}
LogInfo("Backing up (moving) key file ", filepath, " to ", newFilepath,
"...");
LogInfo("Backing up (moving) key file ", filepath, " to ", newFilepath, "...");
fs::rename(filepath, newFilepath, ec);
if (ec)
@ -167,8 +167,7 @@ namespace llarp
bool
KeyManager::backupKeyFilesByMoving() const
{
std::vector< std::string > files = {m_rcPath, m_idKeyPath, m_encKeyPath,
m_transportKeyPath};
std::vector<std::string> files = {m_rcPath, m_idKeyPath, m_encKeyPath, m_transportKeyPath};
for (auto& filepath : files)
{
@ -181,7 +180,8 @@ namespace llarp
bool
KeyManager::loadOrCreateKey(
const std::string& filepath, llarp::SecretKey& key,
const std::string& filepath,
llarp::SecretKey& key,
std::function<void(llarp::SecretKey& key)> keygen)
{
fs::path path(filepath);
@ -230,9 +230,8 @@ namespace llarp
list = curl_slist_append(list, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
nlohmann::json request = {{"id", "0"},
{"jsonrpc", "2.0"},
{"method", "get_service_node_privkey"}};
nlohmann::json request = {
{"id", "0"}, {"jsonrpc", "2.0"}, {"method", "get_service_node_privkey"}};
const auto data = request.dump();
std::vector<char> resp;
@ -256,8 +255,7 @@ namespace llarp
return false;
if (not itr->is_object())
return false;
const auto k =
(*itr)["service_node_ed25519_privkey"].get< std::string >();
const auto k = (*itr)["service_node_ed25519_privkey"].get<std::string>();
if (k.size() != (identityKey.size() * 2))
{
if (k.empty())
@ -292,8 +290,7 @@ namespace llarp
}
if (ret)
{
LogInfo("Got Identity Keys from lokid: ",
RouterID(seckey_topublic(identityKey)));
LogInfo("Got Identity Keys from lokid: ", RouterID(seckey_topublic(identityKey)));
}
curl_easy_cleanup(curl);
curl_slist_free_all(list);

@ -87,7 +87,9 @@ namespace llarp
///
/// @param keygen is a function that will generate the key if needed
static bool
loadOrCreateKey(const std::string& filepath, llarp::SecretKey& key,
loadOrCreateKey(
const std::string& filepath,
llarp::SecretKey& key,
std::function<void(llarp::SecretKey& key)> keygen);
/// Requests the identity key from lokid via HTTP (curl)

@ -20,11 +20,9 @@ namespace llarp
/// default path lifetime in ms
constexpr std::chrono::milliseconds default_lifetime = 20min;
/// minimum into lifetime we will advertise
constexpr std::chrono::milliseconds min_intro_lifetime =
default_lifetime / 2;
constexpr std::chrono::milliseconds min_intro_lifetime = default_lifetime / 2;
/// spacing frequency at which we try to build paths for introductions
constexpr std::chrono::milliseconds intro_path_spread =
default_lifetime / 5;
constexpr std::chrono::milliseconds intro_path_spread = default_lifetime / 5;
/// Minimum paths to keep around for intros; mainly used at startup (the
/// spread, above, should be able to maintain more than this number of paths
/// normally once things are going).

@ -13,8 +13,7 @@
#endif
#if defined(_WIN32) && defined(RC_INVOKED)
#define LLARP_VERSION \
LLARP_VERSION_MAJOR, LLARP_VERSION_MINOR, LLARP_VERSION_PATCH, 0
#define LLARP_VERSION LLARP_VERSION_MAJOR, LLARP_VERSION_MINOR, LLARP_VERSION_PATCH, 0
#define MAKE_TRIPLET(X, Y, Z) TRIPLET_CAT(X, ., Y, ., Z)
#define TRIPLET_CAT(X, D1, Y, D2, Z) X##D1##Y##D2##Z

@ -49,8 +49,7 @@ namespace llarp
auto threads = config->router.workerThreads();
if (threads <= 0)
threads = 1;
worker = std::make_shared< llarp::thread::ThreadPool >(threads, 1024,
"llarp-worker");
worker = std::make_shared<llarp::thread::ThreadPool>(threads, 1024, "llarp-worker");
auto jobQueueSize = config->router.jobQueueSize();
if (jobQueueSize < 1024)
jobQueueSize = 1024;
@ -219,8 +218,7 @@ namespace llarp
if (router)
{
router->hiddenServiceContext().ForEachService(
[](const std::string &name,
const llarp::service::Endpoint_ptr &ep) -> bool {
[](const std::string& name, const llarp::service::Endpoint_ptr& ep) -> bool {
ep->ResetInternalState();
llarp::LogInfo("Reset internal state for ", name);
return true;
@ -409,8 +407,7 @@ extern "C"
void
llarp_main_signal(struct llarp_main* ptr, int sig)
{
LogicCall(ptr->ctx->logic,
std::bind(&llarp::Context::HandleSignal, ptr->ctx.get(), sig));
LogicCall(ptr->ctx->logic, std::bind(&llarp::Context::HandleSignal, ptr->ctx.get(), sig));
}
int
@ -432,8 +429,7 @@ extern "C"
}
ssize_t
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader *r, unsigned char *dst,
size_t dstlen)
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader* r, unsigned char* dst, size_t dstlen)
{
if (r == nullptr)
return -1;
@ -449,8 +445,7 @@ extern "C"
}
bool
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer *w, unsigned char *pktbuf,
size_t pktlen)
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer* w, unsigned char* pktbuf, size_t pktlen)
{
if (pktlen == 0 || pktbuf == nullptr)
return false;
@ -460,12 +455,13 @@ extern "C"
llarp_buffer_t buf(pktbuf, pktlen);
if (not pkt.Load(buf))
return false;
return w->queue.pushBack(std::move(pkt))
== llarp::thread::QueueReturn::Success;
return w->queue.pushBack(std::move(pkt)) == llarp::thread::QueueReturn::Success;
}
bool
llarp_main_inject_vpn_by_name(struct llarp_main *ptr, const char *name,
llarp_main_inject_vpn_by_name(
struct llarp_main* ptr,
const char* name,
struct llarp_vpn_io* io,
struct llarp_vpn_ifaddr_info info)
{

@ -26,30 +26,25 @@ namespace llarp
/// xchacha symmetric cipher
virtual bool
xchacha20(const llarp_buffer_t &, const SharedSecret &,
const TunnelNonce &) = 0;
xchacha20(const llarp_buffer_t&, const SharedSecret&, const TunnelNonce&) = 0;
/// xchacha symmetric cipher (multibuffer)
virtual bool
xchacha20_alt(const llarp_buffer_t &, const llarp_buffer_t &,
const SharedSecret &, const byte_t *) = 0;
xchacha20_alt(
const llarp_buffer_t&, const llarp_buffer_t&, const SharedSecret&, const byte_t*) = 0;
/// path dh creator's side
virtual bool
dh_client(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
dh_client(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) = 0;
/// path dh relay side
virtual bool
dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
dh_server(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) = 0;
/// transport dh client side
virtual bool
transport_dh_client(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
transport_dh_client(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) = 0;
/// transport dh server side
virtual bool
transport_dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
transport_dh_server(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) = 0;
/// blake2b 256 bit
virtual bool
shorthash(ShortHash&, const llarp_buffer_t&) = 0;
@ -68,13 +63,12 @@ namespace llarp
/// derive sub keys for public keys
virtual bool
derive_subkey(PubKey &, const PubKey &, uint64_t,
const AlignedBuffer< 32 > * = nullptr) = 0;
derive_subkey(PubKey&, const PubKey&, uint64_t, const AlignedBuffer<32>* = nullptr) = 0;
/// derive sub keys for private keys
virtual bool
derive_subkey_private(PrivateKey &, const SecretKey &, uint64_t,
const AlignedBuffer< 32 > * = nullptr) = 0;
derive_subkey_private(
PrivateKey&, const SecretKey&, uint64_t, const AlignedBuffer<32>* = nullptr) = 0;
/// seed to secretkey
virtual bool

@ -23,8 +23,11 @@ namespace llarp
namespace sodium
{
static bool
dh(llarp::SharedSecret &out, const PubKey &client_pk,
const PubKey &server_pk, const uint8_t *themPub, const SecretKey &usSec)
dh(llarp::SharedSecret& out,
const PubKey& client_pk,
const PubKey& server_pk,
const uint8_t* themPub,
const SecretKey& usSec)
{
llarp::SharedSecret shared;
crypto_generichash_state h;
@ -42,15 +45,14 @@ namespace llarp
}
static bool
dh_client_priv(llarp::SharedSecret &shared, const PubKey &pk,
const SecretKey &sk, const TunnelNonce &n)
dh_client_priv(
llarp::SharedSecret& shared, const PubKey& pk, const SecretKey& sk, const TunnelNonce& n)
{
llarp::SharedSecret dh_result;
if (dh(dh_result, sk.toPublic(), pk, pk.data(), sk))
{
return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32,
dh_result.data(), 32)
return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32, dh_result.data(), 32)
!= -1;
}
llarp::LogWarn("crypto::dh_client - dh failed");
@ -58,14 +60,13 @@ namespace llarp
}
static bool
dh_server_priv(llarp::SharedSecret &shared, const PubKey &pk,
const SecretKey &sk, const TunnelNonce &n)
dh_server_priv(
llarp::SharedSecret& shared, const PubKey& pk, const SecretKey& sk, const TunnelNonce& n)
{
llarp::SharedSecret dh_result;
if (dh(dh_result, pk, sk.toPublic(), pk.data(), sk))
{
return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32,
dh_result.data(), 32)
return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32, dh_result.data(), 32)
!= -1;
}
llarp::LogWarn("crypto::dh_server - dh failed");
@ -93,51 +94,45 @@ namespace llarp
}
bool
CryptoLibSodium::xchacha20(const llarp_buffer_t &buff,
const SharedSecret &k, const TunnelNonce &n)
CryptoLibSodium::xchacha20(
const llarp_buffer_t& buff, const SharedSecret& k, const TunnelNonce& n)
{
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz,
n.data(), k.data())
== 0;
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz, n.data(), k.data()) == 0;
}
bool
CryptoLibSodium::xchacha20_alt(const llarp_buffer_t &out,
const llarp_buffer_t &in,
const SharedSecret &k, const byte_t *n)
CryptoLibSodium::xchacha20_alt(
const llarp_buffer_t& out, const llarp_buffer_t& in, const SharedSecret& k, const byte_t* n)
{
if (in.sz > out.sz)
return false;
return crypto_stream_xchacha20_xor(out.base, in.base, in.sz, n, k.data())
== 0;
return crypto_stream_xchacha20_xor(out.base, in.base, in.sz, n, k.data()) == 0;
}
bool
CryptoLibSodium::dh_client(llarp::SharedSecret &shared, const PubKey &pk,
const SecretKey &sk, const TunnelNonce &n)
CryptoLibSodium::dh_client(
llarp::SharedSecret& shared, const PubKey& pk, const SecretKey& sk, const TunnelNonce& n)
{
return dh_client_priv(shared, pk, sk, n);
}
/// path dh relay side
bool
CryptoLibSodium::dh_server(llarp::SharedSecret &shared, const PubKey &pk,
const SecretKey &sk, const TunnelNonce &n)
CryptoLibSodium::dh_server(
llarp::SharedSecret& shared, const PubKey& pk, const SecretKey& sk, const TunnelNonce& n)
{
return dh_server_priv(shared, pk, sk, n);
}
/// transport dh client side
bool
CryptoLibSodium::transport_dh_client(llarp::SharedSecret &shared,
const PubKey &pk, const SecretKey &sk,
const TunnelNonce &n)
CryptoLibSodium::transport_dh_client(
llarp::SharedSecret& shared, const PubKey& pk, const SecretKey& sk, const TunnelNonce& n)
{
return dh_client_priv(shared, pk, sk, n);
}
/// transport dh server side
bool
CryptoLibSodium::transport_dh_server(llarp::SharedSecret &shared,
const PubKey &pk, const SecretKey &sk,
const TunnelNonce &n)
CryptoLibSodium::transport_dh_server(
llarp::SharedSecret& shared, const PubKey& pk, const SecretKey& sk, const TunnelNonce& n)
{
return dh_server_priv(shared, pk, sk, n);
}
@ -145,40 +140,33 @@ namespace llarp
bool
CryptoLibSodium::shorthash(ShortHash& result, const llarp_buffer_t& buff)
{
return crypto_generichash_blake2b(result.data(), ShortHash::SIZE,
buff.base, buff.sz, nullptr, 0)
return crypto_generichash_blake2b(
result.data(), ShortHash::SIZE, buff.base, buff.sz, nullptr, 0)
!= -1;
}
bool
CryptoLibSodium::hmac(byte_t *result, const llarp_buffer_t &buff,
const SharedSecret &secret)
CryptoLibSodium::hmac(byte_t* result, const llarp_buffer_t& buff, const SharedSecret& secret)
{
return crypto_generichash_blake2b(result, HMACSIZE, buff.base, buff.sz,
secret.data(), HMACSECSIZE)
return crypto_generichash_blake2b(
result, HMACSIZE, buff.base, buff.sz, secret.data(), HMACSECSIZE)
!= -1;
}
static bool
hash(uint8_t* result, const llarp_buffer_t& buff)
{
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz,
nullptr, 0)
!= -1;
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz, nullptr, 0) != -1;
}
bool
CryptoLibSodium::sign(Signature &sig, const SecretKey &secret,
const llarp_buffer_t &buf)
CryptoLibSodium::sign(Signature& sig, const SecretKey& secret, const llarp_buffer_t& buf)
{
return crypto_sign_detached(sig.data(), nullptr, buf.base, buf.sz,
secret.data())
!= -1;
return crypto_sign_detached(sig.data(), nullptr, buf.base, buf.sz, secret.data()) != -1;
}
bool
CryptoLibSodium::sign(Signature &sig, const PrivateKey &privkey,
const llarp_buffer_t &buf)
CryptoLibSodium::sign(Signature& sig, const PrivateKey& privkey, const llarp_buffer_t& buf)
{
PubKey pubkey;
@ -222,12 +210,9 @@ namespace llarp
}
bool
CryptoLibSodium::verify(const PubKey &pub, const llarp_buffer_t &buf,
const Signature &sig)
CryptoLibSodium::verify(const PubKey& pub, const llarp_buffer_t& buf, const Signature& sig)
{
return crypto_sign_verify_detached(sig.data(), buf.base, buf.sz,
pub.data())
!= -1;
return crypto_sign_verify_detached(sig.data(), buf.base, buf.sz, pub.data()) != -1;
}
/// clamp a 32 byte ec point
@ -263,7 +248,8 @@ namespace llarp
"to read? lokinet yolo!";
template <typename K>
static bool make_scalar(AlignedBuffer< 32 > &out, const K &k, uint64_t i)
static bool
make_scalar(AlignedBuffer<32>& out, const K& k, uint64_t i)
{
// b = BLIND-STRING || k || i
std::array<byte_t, 160 + K::SIZE + sizeof(uint64_t)> buf;
@ -274,16 +260,18 @@ namespace llarp
// h = make_point(n)
ShortHash n;
return -1
!= crypto_generichash_blake2b(n.data(), ShortHash::SIZE, buf.data(),
buf.size(), nullptr, 0)
!= crypto_generichash_blake2b(
n.data(), ShortHash::SIZE, buf.data(), buf.size(), nullptr, 0)
&& -1 != crypto_core_ed25519_from_uniform(out.data(), n.data());
}
static AlignedBuffer<32> zero;
bool
CryptoLibSodium::derive_subkey(PubKey &out_pubkey,
const PubKey &root_pubkey, uint64_t key_n,
CryptoLibSodium::derive_subkey(
PubKey& out_pubkey,
const PubKey& root_pubkey,
uint64_t key_n,
const AlignedBuffer<32>* hash)
{
// scalar h = H( BLIND-STRING || root_pubkey || key_n )
@ -296,13 +284,12 @@ namespace llarp
return false;
}
return 0
== crypto_scalarmult_ed25519(out_pubkey.data(), h.data(),
root_pubkey.data());
return 0 == crypto_scalarmult_ed25519(out_pubkey.data(), h.data(), root_pubkey.data());
}
bool
CryptoLibSodium::derive_subkey_private(PrivateKey &out_key,
CryptoLibSodium::derive_subkey_private(
PrivateKey& out_key,
const SecretKey& root_key,
uint64_t key_n,
const AlignedBuffer<32>* hash)
@ -364,19 +351,16 @@ namespace llarp
std::copy(h.begin(), h.end(), buf.begin());
std::copy(a.signingHash(), a.signingHash() + 32, buf.begin() + 32);
return -1
!= crypto_generichash_blake2b(out_key.signingHash(), 32, buf.data(),
buf.size(), nullptr, 0);
!= crypto_generichash_blake2b(
out_key.signingHash(), 32, buf.data(), buf.size(), nullptr, 0);
return true;
}
bool
CryptoLibSodium::seed_to_secretkey(llarp::SecretKey &secret,
const llarp::IdentitySecret &seed)
CryptoLibSodium::seed_to_secretkey(llarp::SecretKey& secret, const llarp::IdentitySecret& seed)
{
return crypto_sign_ed25519_seed_keypair(secret.data() + 32, secret.data(),
seed.data())
!= -1;
return crypto_sign_ed25519_seed_keypair(secret.data() + 32, secret.data(), seed.data()) != -1;
}
void
CryptoLibSodium::randomize(const llarp_buffer_t& buff)
@ -426,20 +410,16 @@ namespace llarp
}
bool
CryptoLibSodium::pqe_encrypt(PQCipherBlock &ciphertext,
SharedSecret &sharedkey,
const PQPubKey &pubkey)
CryptoLibSodium::pqe_encrypt(
PQCipherBlock& ciphertext, SharedSecret& sharedkey, const PQPubKey& pubkey)
{
return crypto_kem_enc(ciphertext.data(), sharedkey.data(), pubkey.data())
!= -1;
return crypto_kem_enc(ciphertext.data(), sharedkey.data(), pubkey.data()) != -1;
}
bool
CryptoLibSodium::pqe_decrypt(const PQCipherBlock &ciphertext,
SharedSecret &sharedkey,
const byte_t *secretkey)
CryptoLibSodium::pqe_decrypt(
const PQCipherBlock& ciphertext, SharedSecret& sharedkey, const byte_t* secretkey)
{
return crypto_kem_dec(sharedkey.data(), ciphertext.data(), secretkey)
!= -1;
return crypto_kem_dec(sharedkey.data(), ciphertext.data(), secretkey) != -1;
}
void

@ -15,30 +15,30 @@ namespace llarp
/// xchacha symmetric cipher
bool
xchacha20(const llarp_buffer_t &, const SharedSecret &,
const TunnelNonce &) override;
xchacha20(const llarp_buffer_t&, const SharedSecret&, const TunnelNonce&) override;
/// xchacha symmetric cipher (multibuffer)
bool
xchacha20_alt(const llarp_buffer_t &, const llarp_buffer_t &,
const SharedSecret &, const byte_t *) override;
xchacha20_alt(
const llarp_buffer_t&,
const llarp_buffer_t&,
const SharedSecret&,
const byte_t*) override;
/// path dh creator's side
bool
dh_client(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) override;
dh_client(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) override;
/// path dh relay side
bool
dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) override;
dh_server(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) override;
/// transport dh client side
bool
transport_dh_client(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) override;
transport_dh_client(
SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) override;
/// transport dh server side
bool
transport_dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) override;
transport_dh_server(
SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&) override;
/// blake2b 256 bit
bool
shorthash(ShortHash&, const llarp_buffer_t&) override;
@ -53,26 +53,29 @@ namespace llarp
sign(Signature&, const PrivateKey&, const llarp_buffer_t&) override;
/// ed25519 verify
bool
verify(const PubKey &, const llarp_buffer_t &,
const Signature &) override;
verify(const PubKey&, const llarp_buffer_t&, const Signature&) override;
/// derive sub keys for public keys. hash is really only intended for
/// testing and overrides key_n if given.
bool
derive_subkey(PubKey &derived, const PubKey &root, uint64_t key_n,
derive_subkey(
PubKey& derived,
const PubKey& root,
uint64_t key_n,
const AlignedBuffer<32>* hash = nullptr) override;
/// derive sub keys for private keys. hash is really only intended for
/// testing and overrides key_n if given.
bool
derive_subkey_private(PrivateKey &derived, const SecretKey &root,
derive_subkey_private(
PrivateKey& derived,
const SecretKey& root,
uint64_t key_n,
const AlignedBuffer<32>* hash = nullptr) override;
/// seed to secretkey
bool
seed_to_secretkey(llarp::SecretKey &,
const llarp::IdentitySecret &) override;
seed_to_secretkey(llarp::SecretKey&, const llarp::IdentitySecret&) override;
/// randomize buffer
void
randomize(const llarp_buffer_t&) override;
@ -90,8 +93,7 @@ namespace llarp
pqe_keygen(PQKeyPair&) override;
/// post quantum decrypt (buffer, sharedkey_dst, sec)
bool
pqe_decrypt(const PQCipherBlock &, SharedSecret &,
const byte_t *) override;
pqe_decrypt(const PQCipherBlock&, SharedSecret&, const byte_t*) override;
/// post quantum encrypt (buffer, sharedkey_dst, pub)
bool
pqe_encrypt(PQCipherBlock&, SharedSecret&, const PQPubKey&) override;

@ -23,15 +23,17 @@ namespace llarp
~NoOpCrypto() override = default;
bool
xchacha20(const llarp_buffer_t &, const SharedSecret &,
const TunnelNonce &) override
xchacha20(const llarp_buffer_t&, const SharedSecret&, const TunnelNonce&) override
{
return true;
}
bool
xchacha20_alt(const llarp_buffer_t &out, const llarp_buffer_t &in,
const SharedSecret &, const byte_t *) override
xchacha20_alt(
const llarp_buffer_t& out,
const llarp_buffer_t& in,
const SharedSecret&,
const byte_t*) override
{
if (in.sz > out.sz)
{
@ -43,32 +45,30 @@ namespace llarp
}
bool
dh_client(SharedSecret &shared, const PubKey &pk, const SecretKey &,
const TunnelNonce &) override
dh_client(SharedSecret& shared, const PubKey& pk, const SecretKey&, const TunnelNonce&) override
{
std::copy_n(pk.begin(), pk.size(), shared.begin());
return true;
}
bool
dh_server(SharedSecret &shared, const PubKey &pk, const SecretKey &,
const TunnelNonce &) override
dh_server(SharedSecret& shared, const PubKey& pk, const SecretKey&, const TunnelNonce&) override
{
std::copy_n(pk.begin(), pk.size(), shared.begin());
return true;
}
bool
transport_dh_client(SharedSecret &shared, const PubKey &pk,
const SecretKey &, const TunnelNonce &) override
transport_dh_client(
SharedSecret& shared, const PubKey& pk, const SecretKey&, const TunnelNonce&) override
{
std::copy_n(pk.begin(), pk.size(), shared.begin());
return true;
}
bool
transport_dh_server(SharedSecret &shared, const PubKey &pk,
const SecretKey &, const TunnelNonce &) override
transport_dh_server(
SharedSecret& shared, const PubKey& pk, const SecretKey&, const TunnelNonce&) override
{
std::copy_n(pk.begin(), pk.size(), shared.begin());
return true;
@ -130,8 +130,7 @@ namespace llarp
{
static_assert(SecretKey::SIZE == (2 * IdentitySecret::SIZE), "");
std::copy(secret.begin(), secret.end(), key.begin());
std::copy(secret.begin(), secret.end(),
key.begin() + IdentitySecret::SIZE);
std::copy(secret.begin(), secret.end(), key.begin() + IdentitySecret::SIZE);
return true;
}
@ -172,16 +171,14 @@ namespace llarp
}
bool
pqe_decrypt(const PQCipherBlock &block, SharedSecret &secret,
const byte_t *) override
pqe_decrypt(const PQCipherBlock& block, SharedSecret& secret, const byte_t*) override
{
std::copy_n(block.begin(), SharedSecret::SIZE, secret.begin());
return true;
}
bool
pqe_encrypt(PQCipherBlock &block, SharedSecret &secret,
const PQPubKey &) override
pqe_encrypt(PQCipherBlock& block, SharedSecret& secret, const PQPubKey&) override
{
std::copy_n(secret.begin(), SharedSecret::SIZE, block.begin());
return true;

@ -54,8 +54,7 @@ namespace llarp
}
bool
EncryptedFrame::EncryptInPlace(const SecretKey& ourSecretKey,
const PubKey& otherPubkey)
EncryptedFrame::EncryptInPlace(const SecretKey& ourSecretKey, const PubKey& otherPubkey)
{
// format of frame is
// <32 bytes keyed hash of following data>

@ -10,11 +10,9 @@
namespace llarp
{
static constexpr size_t EncryptedFrameOverheadSize =
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
static constexpr size_t EncryptedFrameOverheadSize = PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
static constexpr size_t EncryptedFrameBodySize = 128 * 6;
static constexpr size_t EncryptedFrameSize =
EncryptedFrameOverheadSize + EncryptedFrameBodySize;
static constexpr size_t EncryptedFrameSize = EncryptedFrameOverheadSize + EncryptedFrameBodySize;
struct EncryptedFrame : public Encrypted<EncryptedFrameSize>
{
@ -23,8 +21,8 @@ namespace llarp
}
EncryptedFrame(size_t sz)
: Encrypted< EncryptedFrameSize >(std::min(sz, EncryptedFrameBodySize)
+ EncryptedFrameOverheadSize)
: Encrypted<EncryptedFrameSize>(
std::min(sz, EncryptedFrameBodySize) + EncryptedFrameOverheadSize)
{
}
@ -81,12 +79,11 @@ namespace llarp
EncryptedFrame target;
void
AsyncDecrypt(const std::shared_ptr< thread::ThreadPool >& worker,
const EncryptedFrame& frame, User_ptr u)
AsyncDecrypt(
const std::shared_ptr<thread::ThreadPool>& worker, const EncryptedFrame& frame, User_ptr u)
{
target = frame;
worker->addJob(
std::bind(&AsyncFrameDecrypter< User >::Decrypt, this, std::move(u)));
worker->addJob(std::bind(&AsyncFrameDecrypter<User>::Decrypt, this, std::move(u)));
}
};
} // namespace llarp

@ -98,8 +98,7 @@ namespace llarp
return false;
}
const fs::path fpath = std::string(fname);
auto optional_f =
llarp::util::OpenFileStream< std::ofstream >(fpath, std::ios::binary);
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f)
return false;
auto& f = optional_f.value();
@ -113,8 +112,7 @@ namespace llarp
IdentitySecret::LoadFromFile(const char* fname)
{
const fs::path fpath = std::string(fname);
auto optional = util::OpenFileStream< std::ifstream >(
fpath, std::ios::binary | std::ios::in);
auto optional = util::OpenFileStream<std::ifstream>(fpath, std::ios::binary | std::ios::in);
if (!optional)
return false;
auto& f = optional.value();

@ -26,8 +26,7 @@ namespace llarp
{
}
explicit PubKey(const AlignedBuffer< SIZE > &other)
: AlignedBuffer< SIZE >(other)
explicit PubKey(const AlignedBuffer<SIZE>& other) : AlignedBuffer<SIZE>(other)
{
}
@ -88,8 +87,7 @@ namespace llarp
}
// The full data
explicit SecretKey(const AlignedBuffer< SECKEYSIZE > &seed)
: AlignedBuffer< SECKEYSIZE >(seed)
explicit SecretKey(const AlignedBuffer<SECKEYSIZE>& seed) : AlignedBuffer<SECKEYSIZE>(seed)
{
}
@ -151,8 +149,7 @@ namespace llarp
{
}
explicit PrivateKey(const AlignedBuffer< 64 > &key_and_hash)
: AlignedBuffer< 64 >(key_and_hash)
explicit PrivateKey(const AlignedBuffer<64>& key_and_hash) : AlignedBuffer<64>(key_and_hash)
{
}
@ -244,16 +241,15 @@ namespace llarp
using PQKeyPair = AlignedBuffer<PQ_KEYPAIRSIZE>;
/// PKE(result, publickey, secretkey, nonce)
using path_dh_func = std::function< bool(
SharedSecret &, const PubKey &, const SecretKey &, const TunnelNonce &) >;
using path_dh_func =
std::function<bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)>;
/// TKE(result, publickey, secretkey, nonce)
using transport_dh_func = std::function< bool(
SharedSecret &, const PubKey &, const SecretKey &, const TunnelNonce &) >;
using transport_dh_func =
std::function<bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)>;
/// SH(result, body)
using shorthash_func =
std::function< bool(ShortHash &, const llarp_buffer_t &) >;
using shorthash_func = std::function<bool(ShortHash&, const llarp_buffer_t&)>;
} // namespace llarp
#endif

@ -19,8 +19,7 @@ namespace llarp
using BucketStorage_t = std::map<Key_t, Val_t, XorMetric>;
using Random_t = std::function<uint64_t()>;
Bucket(const Key_t& us, Random_t r)
: nodes(XorMetric(us)), random(std::move(r))
Bucket(const Key_t& us, Random_t r) : nodes(XorMetric(us)), random(std::move(r))
{
}
@ -44,27 +43,28 @@ namespace llarp
struct SetIntersector
{
bool
operator()(const typename BucketStorage_t::value_type& lhs,
const Key_t& rhs)
operator()(const typename BucketStorage_t::value_type& lhs, const Key_t& rhs)
{
return lhs.first < rhs;
}
bool
operator()(const Key_t& lhs,
const typename BucketStorage_t::value_type& rhs)
operator()(const Key_t& lhs, const typename BucketStorage_t::value_type& rhs)
{
return lhs < rhs.first;
}
};
bool
GetRandomNodeExcluding(Key_t& result,
const std::set< Key_t >& exclude) const
GetRandomNodeExcluding(Key_t& result, const std::set<Key_t>& exclude) const
{
std::vector<typename BucketStorage_t::value_type> candidates;
std::set_difference(nodes.begin(), nodes.end(), exclude.begin(),
exclude.end(), std::back_inserter(candidates),
std::set_difference(
nodes.begin(),
nodes.end(),
exclude.begin(),
exclude.end(),
std::back_inserter(candidates),
SetIntersector());
if (candidates.empty())
@ -97,15 +97,15 @@ namespace llarp
{
if (nodes.size() < N || nodes.empty())
{
llarp::LogWarn("Not enough dht nodes, have ", nodes.size(), " want ",
N);
llarp::LogWarn("Not enough dht nodes, have ", nodes.size(), " want ", N);
return false;
}
if (nodes.size() == N)
{
std::transform(nodes.begin(), nodes.end(),
std::inserter(result, result.end()),
[](const auto& a) { return a.first; });
std::transform(
nodes.begin(), nodes.end(), std::inserter(result, result.end()), [](const auto& a) {
return a.first;
});
return true;
}
@ -124,8 +124,7 @@ namespace llarp
}
bool
FindCloseExcluding(const Key_t& target, Key_t& result,
const std::set< Key_t >& exclude) const
FindCloseExcluding(const Key_t& target, Key_t& result, const std::set<Key_t>& exclude) const
{
Key_t maxdist;
maxdist.Fill(0xff);
@ -149,8 +148,11 @@ namespace llarp
}
bool
GetManyNearExcluding(const Key_t& target, std::set< Key_t >& result,
size_t N, const std::set< Key_t >& exclude) const
GetManyNearExcluding(
const Key_t& target,
std::set<Key_t>& result,
size_t N,
const std::set<Key_t>& exclude) const
{
std::set<Key_t> s(exclude.begin(), exclude.end());

@ -47,21 +47,29 @@ namespace llarp
void
LookupIntroSetRelayed(
const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX,
const Key_t& askpeer, uint64_t relayOrder,
const Key_t& target,
const Key_t& whoasked,
uint64_t whoaskedTX,
const Key_t& askpeer,
uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler result = nullptr) override;
void
LookupIntroSetDirect(
const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX,
const Key_t& target,
const Key_t& whoasked,
uint64_t whoaskedTX,
const Key_t& askpeer,
service::EncryptedIntroSetLookupHandler result = nullptr) override;
/// on behalf of whoasked request router with public key target from dht
/// router with key askpeer
void
LookupRouterRecursive(const RouterID& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
LookupRouterRecursive(
const RouterID& target,
const Key_t& whoasked,
uint64_t whoaskedTX,
const Key_t& askpeer,
RouterLookupHandler result = nullptr) override;
bool
@ -84,51 +92,65 @@ namespace llarp
/// issue dht lookup for router via askpeer and send reply to local path
void
LookupRouterForPath(const RouterID& target, uint64_t txid,
const PathID_t& path, const Key_t& askpeer) override;
LookupRouterForPath(
const RouterID& target,
uint64_t txid,
const PathID_t& path,
const Key_t& askpeer) override;
/// issue dht lookup for introset for addr via askpeer and send reply to
/// local path
void
LookupIntroSetForPath(const Key_t& addr, uint64_t txid,
const llarp::PathID_t& path, const Key_t& askpeer,
LookupIntroSetForPath(
const Key_t& addr,
uint64_t txid,
const llarp::PathID_t& path,
const Key_t& askpeer,
uint64_t relayOrder) override;
/// send a dht message to peer, if keepalive is true then keep the session
/// with that peer alive for 10 seconds
void
DHTSendTo(const RouterID& peer, IMessage* msg,
bool keepalive = true) override;
DHTSendTo(const RouterID& peer, IMessage* msg, bool keepalive = true) override;
/// get routers closest to target excluding requester
bool
HandleExploritoryRouterLookup(
const Key_t& requester, uint64_t txid, const RouterID& target,
const Key_t& requester,
uint64_t txid,
const RouterID& target,
std::vector<std::unique_ptr<IMessage>>& reply) override;
/// handle rc lookup from requester for target
void
LookupRouterRelayed(
const Key_t& requester, uint64_t txid, const Key_t& target,
const Key_t& requester,
uint64_t txid,
const Key_t& target,
bool recursive,
std::vector<std::unique_ptr<IMessage>>& replies) override;
/// relay a dht message from a local path to the main network
bool
RelayRequestForPath(const llarp::PathID_t& localPath,
const IMessage& msg) override;
RelayRequestForPath(const llarp::PathID_t& localPath, const IMessage& msg) override;
/// send introset to peer as R/S
void
PropagateLocalIntroSet(const PathID_t& from, uint64_t txid,
PropagateLocalIntroSet(
const PathID_t& from,
uint64_t txid,
const service::EncryptedIntroSet& introset,
const Key_t& tellpeer, uint64_t relayOrder);
const Key_t& tellpeer,
uint64_t relayOrder);
/// send introset to peer from source with S counter and excluding peers
void
PropagateIntroSetTo(const Key_t& from, uint64_t txid,
PropagateIntroSetTo(
const Key_t& from,
uint64_t txid,
const service::EncryptedIntroSet& introset,
const Key_t& tellpeer, uint64_t relayOrder);
const Key_t& tellpeer,
uint64_t relayOrder);
/// initialize dht context and explore every exploreInterval milliseconds
void
@ -291,8 +313,7 @@ namespace llarp
ExploreNetworkVia(peer);
}
else
llarp::LogError("failed to select ", N,
" random nodes for exploration");
llarp::LogError("failed to select ", N, " random nodes for exploration");
}
void
@ -333,14 +354,16 @@ namespace llarp
void
Context::LookupRouterRelayed(
const Key_t& requester, uint64_t txid, const Key_t& target,
bool recursive, std::vector< std::unique_ptr< IMessage > >& replies)
const Key_t& requester,
uint64_t txid,
const Key_t& target,
bool recursive,
std::vector<std::unique_ptr<IMessage>>& replies)
{
if (target == ourKey)
{
// we are the target, give them our RC
replies.emplace_back(
new GotRouterMessage(requester, txid, {router->rc()}, false));
replies.emplace_back(new GotRouterMessage(requester, txid, {router->rc()}, false));
return;
}
if (not GetRouter()->ConnectionToRouterAllowed(target.as_array()))
@ -363,8 +386,7 @@ namespace llarp
else
{
// send reply with rc we know of
replies.emplace_back(
new GotRouterMessage(requester, txid, {rc}, false));
replies.emplace_back(new GotRouterMessage(requester, txid, {rc}, false));
}
}
else if (recursive) // are we doing a recursive lookup?
@ -379,15 +401,13 @@ namespace llarp
{
// no we are closer to the target so tell requester it's not there
// so they switch to iterative lookup
replies.emplace_back(
new GotRouterMessage(requester, txid, {}, false));
replies.emplace_back(new GotRouterMessage(requester, txid, {}, false));
}
}
else
{
// iterative lookup and we don't have it tell them who is closer
replies.emplace_back(
new GotRouterMessage(requester, next, txid, false));
replies.emplace_back(new GotRouterMessage(requester, next, txid, false));
}
}
}
@ -415,8 +435,7 @@ namespace llarp
util::StatusObject
Context::ExtractStatus() const
{
util::StatusObject obj{
{"pendingRouterLookups", pendingRouterLookups().ExtractStatus()},
util::StatusObject obj{{"pendingRouterLookups", pendingRouterLookups().ExtractStatus()},
{"pendingIntrosetLookups", _pendingIntrosetLookups.ExtractStatus()},
{"pendingExploreLookups", pendingExploreLookups().ExtractStatus()},
{"nodes", _nodes->ExtractStatus()},
@ -441,8 +460,7 @@ namespace llarp
Context::ScheduleCleanupTimer()
{
router->logic()->call_later(
1s,
std::bind(&llarp::dht::Context::handle_cleaner_timer, this, 1000));
1s, std::bind(&llarp::dht::Context::handle_cleaner_timer, this, 1000));
}
void
@ -477,71 +495,87 @@ namespace llarp
}
void
Context::LookupIntroSetForPath(const Key_t& addr, uint64_t txid,
Context::LookupIntroSetForPath(
const Key_t& addr,
uint64_t txid,
const llarp::PathID_t& path,
const Key_t& askpeer, uint64_t relayOrder)
const Key_t& askpeer,
uint64_t relayOrder)
{
const TXOwner asker(OurKey(), txid);
const TXOwner peer(askpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, asker,
new LocalServiceAddressLookup(path, txid, relayOrder, addr, this,
askpeer));
peer,
asker,
asker,
new LocalServiceAddressLookup(path, txid, relayOrder, addr, this, askpeer));
}
void
Context::PropagateIntroSetTo(const Key_t& from, uint64_t txid,
Context::PropagateIntroSetTo(
const Key_t& from,
uint64_t txid,
const service::EncryptedIntroSet& introset,
const Key_t& tellpeer, uint64_t relayOrder)
const Key_t& tellpeer,
uint64_t relayOrder)
{
const TXOwner asker(from, txid);
const TXOwner peer(tellpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, asker,
new PublishServiceJob(asker, introset, this, relayOrder));
peer, asker, asker, new PublishServiceJob(asker, introset, this, relayOrder));
}
void
Context::PropagateLocalIntroSet(const PathID_t& from, uint64_t txid,
Context::PropagateLocalIntroSet(
const PathID_t& from,
uint64_t txid,
const service::EncryptedIntroSet& introset,
const Key_t& tellpeer, uint64_t relayOrder)
const Key_t& tellpeer,
uint64_t relayOrder)
{
const TXOwner asker(OurKey(), txid);
const TXOwner peer(tellpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, peer,
new LocalPublishServiceJob(peer, from, txid, introset, this,
relayOrder));
peer,
asker,
peer,
new LocalPublishServiceJob(peer, from, txid, introset, this, relayOrder));
}
void
Context::LookupIntroSetRelayed(
const Key_t& addr, const Key_t& whoasked, uint64_t txid,
const Key_t& askpeer, uint64_t relayOrder,
const Key_t& addr,
const Key_t& whoasked,
uint64_t txid,
const Key_t& askpeer,
uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler handler)
{
const TXOwner asker(whoasked, txid);
const TXOwner peer(askpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, asker,
new ServiceAddressLookup(asker, addr, this, relayOrder, handler));
peer, asker, asker, new ServiceAddressLookup(asker, addr, this, relayOrder, handler));
}
void
Context::LookupIntroSetDirect(
const Key_t& addr, const Key_t& whoasked, uint64_t txid,
const Key_t& askpeer, service::EncryptedIntroSetLookupHandler handler)
const Key_t& addr,
const Key_t& whoasked,
uint64_t txid,
const Key_t& askpeer,
service::EncryptedIntroSetLookupHandler handler)
{
const TXOwner asker(whoasked, txid);
const TXOwner peer(askpeer, ++ids);
_pendingIntrosetLookups.NewTX(
peer, asker, asker,
new ServiceAddressLookup(asker, addr, this, 0, handler), 1s);
peer, asker, asker, new ServiceAddressLookup(asker, addr, this, 0, handler), 1s);
}
bool
Context::HandleExploritoryRouterLookup(
const Key_t& requester, uint64_t txid, const RouterID& target,
const Key_t& requester,
uint64_t txid,
const RouterID& target,
std::vector<std::unique_ptr<IMessage>>& reply)
{
std::vector<RouterID> closer;
@ -553,23 +587,21 @@ namespace llarp
const size_t nodeCount = _nodes->size();
if (nodeCount == 0)
{
llarp::LogError(
"cannot handle exploritory router lookup, no dht peers");
llarp::LogError("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
if(!_nodes->GetManyNearExcluding(t, foundRouters,
std::min(nodeCount, size_t{4}),
std::set< Key_t >{ourKey, requester}))
if (!_nodes->GetManyNearExcluding(
t, foundRouters, std::min(nodeCount, size_t{4}), std::set<Key_t>{ourKey, requester}))
{
llarp::LogError(
"not enough dht nodes to handle exploritory router lookup, "
"have ",
nodeCount, " dht peers");
nodeCount,
" dht peers");
return false;
}
for (const auto& f : foundRouters)
@ -586,29 +618,28 @@ namespace llarp
}
void
Context::LookupRouterForPath(const RouterID& target, uint64_t txid,
const llarp::PathID_t& path,
const Key_t& askpeer)
Context::LookupRouterForPath(
const RouterID& target, uint64_t txid, const llarp::PathID_t& path, const Key_t& askpeer)
{
const TXOwner peer(askpeer, ++ids);
const TXOwner whoasked(OurKey(), txid);
_pendingRouterLookups.NewTX(
peer, whoasked, target,
new LocalRouterLookup(path, txid, target, this));
peer, whoasked, target, new LocalRouterLookup(path, txid, target, this));
}
void
Context::LookupRouterRecursive(const RouterID& target,
const Key_t& whoasked, uint64_t txid,
Context::LookupRouterRecursive(
const RouterID& target,
const Key_t& whoasked,
uint64_t txid,
const Key_t& askpeer,
RouterLookupHandler handler)
{
const TXOwner asker(whoasked, txid);
const TXOwner peer(askpeer, ++ids);
_pendingRouterLookups.NewTX(
peer, asker, target,
new RecursiveRouterLookup(asker, target, this, handler));
peer, asker, target, new RecursiveRouterLookup(asker, target, this, handler));
}
llarp_time_t

@ -34,12 +34,9 @@ namespace llarp
struct AbstractContext
{
using PendingIntrosetLookups =
TXHolder< TXOwner, service::EncryptedIntroSet, TXOwner::Hash >;
using PendingRouterLookups =
TXHolder< RouterID, RouterContact, RouterID::Hash >;
using PendingExploreLookups =
TXHolder< RouterID, RouterID, RouterID::Hash >;
using PendingIntrosetLookups = TXHolder<TXOwner, service::EncryptedIntroSet, TXOwner::Hash>;
using PendingRouterLookups = TXHolder<RouterID, RouterContact, RouterID::Hash>;
using PendingExploreLookups = TXHolder<RouterID, RouterID, RouterID::Hash>;
virtual ~AbstractContext() = 0;
@ -47,22 +44,31 @@ namespace llarp
LookupRouter(const RouterID& target, RouterLookupHandler result) = 0;
virtual void
LookupRouterRecursive(const RouterID& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
LookupRouterRecursive(
const RouterID& target,
const Key_t& whoasked,
uint64_t whoaskedTX,
const Key_t& askpeer,
RouterLookupHandler result = nullptr) = 0;
/// Ask a Service Node to perform an Introset lookup for us
virtual void
LookupIntroSetRelayed(const Key_t& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
LookupIntroSetRelayed(
const Key_t& target,
const Key_t& whoasked,
uint64_t whoaskedTX,
const Key_t& askpeer,
uint64_t relayOrder,
service::EncryptedIntroSetLookupHandler result =
service::EncryptedIntroSetLookupHandler()) = 0;
/// Directly as a Service Node for an Introset
virtual void
LookupIntroSetDirect(const Key_t& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
LookupIntroSetDirect(
const Key_t& target,
const Key_t& whoasked,
uint64_t whoaskedTX,
const Key_t& askpeer,
service::EncryptedIntroSetLookupHandler result =
service::EncryptedIntroSetLookupHandler()) = 0;
@ -71,12 +77,15 @@ namespace llarp
/// issue dht lookup for router via askpeer and send reply to local path
virtual void
LookupRouterForPath(const RouterID& target, uint64_t txid,
const PathID_t& path, const Key_t& askpeer) = 0;
LookupRouterForPath(
const RouterID& target, uint64_t txid, const PathID_t& path, const Key_t& askpeer) = 0;
virtual void
LookupIntroSetForPath(const Key_t& addr, uint64_t txid,
const PathID_t& path, const Key_t& askpeer,
LookupIntroSetForPath(
const Key_t& addr,
uint64_t txid,
const PathID_t& path,
const Key_t& askpeer,
uint64_t relayOrder) = 0;
virtual void
@ -85,13 +94,17 @@ namespace llarp
/// get routers closest to target excluding requester
virtual bool
HandleExploritoryRouterLookup(
const Key_t& requester, uint64_t txid, const RouterID& target,
const Key_t& requester,
uint64_t txid,
const RouterID& target,
std::vector<std::unique_ptr<IMessage>>& reply) = 0;
/// handle rc lookup from requester for target
virtual void
LookupRouterRelayed(
const Key_t& requester, uint64_t txid, const Key_t& target,
const Key_t& requester,
uint64_t txid,
const Key_t& target,
bool recursive,
std::vector<std::unique_ptr<IMessage>>& replies) = 0;
@ -100,15 +113,21 @@ namespace llarp
/// send introset to peer from source with S counter and excluding peers
virtual void
PropagateLocalIntroSet(const PathID_t& path, uint64_t sourceTX,
PropagateLocalIntroSet(
const PathID_t& path,
uint64_t sourceTX,
const service::EncryptedIntroSet& introset,
const Key_t& peer, uint64_t relayOrder) = 0;
const Key_t& peer,
uint64_t relayOrder) = 0;
/// send introset to peer from source with S counter and excluding peers
virtual void
PropagateIntroSetTo(const Key_t& source, uint64_t sourceTX,
PropagateIntroSetTo(
const Key_t& source,
uint64_t sourceTX,
const service::EncryptedIntroSet& introset,
const Key_t& peer, uint64_t relayOrder) = 0;
const Key_t& peer,
uint64_t relayOrder) = 0;
virtual void
Init(const Key_t& us, AbstractRouter* router) = 0;

@ -39,8 +39,7 @@ llarp_dht_context_start(struct llarp_dht_context *ctx, const byte_t *key)
}
void
llarp_dht_lookup_router(struct llarp_dht_context *ctx,
struct llarp_router_lookup_job *job)
llarp_dht_lookup_router(struct llarp_dht_context* ctx, struct llarp_router_lookup_job* job)
{
job->dht = ctx;
job->found = false;

@ -59,7 +59,6 @@ void
__llarp_dht_remove_peer(struct llarp_dht_context* ctx, const byte_t* id);
void
llarp_dht_lookup_router(struct llarp_dht_context* ctx,
struct llarp_router_lookup_job* job);
llarp_dht_lookup_router(struct llarp_dht_context* ctx, struct llarp_router_lookup_job* job);
#endif

@ -15,12 +15,9 @@ namespace llarp
void
ExploreNetworkJob::Start(const TXOwner& peer)
{
auto msg = new FindRouterMessage(peer.txid);
auto router = parent->GetRouter();
router->NotifyRouterEvent< tooling::FindRouterSentEvent >(
router->pubkey(),
msg);
router->NotifyRouterEvent<tooling::FindRouterSentEvent>(router->pubkey(), msg);
parent->DHTSendTo(peer.node.as_array(), msg);
}
@ -38,9 +35,7 @@ namespace llarp
if (router and router->nodedb()->Has(pk))
continue;
parent->LookupRouter(
pk,
std::bind(&AbstractRouter::HandleDHTLookupForExplore, router, pk,
_1));
pk, std::bind(&AbstractRouter::HandleDHTLookupForExplore, router, pk, _1));
}
}
} // namespace dht

@ -20,8 +20,7 @@ namespace llarp
{
}
explicit Key_t(const AlignedBuffer< SIZE >& data)
: AlignedBuffer< SIZE >(data)
explicit Key_t(const AlignedBuffer<SIZE>& data) : AlignedBuffer<SIZE>(data)
{
}
@ -50,8 +49,7 @@ namespace llarp
operator^(const Key_t& other) const
{
Key_t dist;
std::transform(begin(), end(), other.begin(), dist.begin(),
std::bit_xor< byte_t >());
std::transform(begin(), end(), other.begin(), dist.begin(), std::bit_xor<byte_t>());
return dist;
}

@ -12,11 +12,9 @@ namespace llarp
{
namespace dht
{
LocalRouterLookup::LocalRouterLookup(const PathID_t &path, uint64_t txid,
const RouterID &_target,
AbstractContext *ctx)
: RecursiveRouterLookup(TXOwner{ctx->OurKey(), txid}, _target, ctx,
nullptr)
LocalRouterLookup::LocalRouterLookup(
const PathID_t& path, uint64_t txid, const RouterID& _target, AbstractContext* ctx)
: RecursiveRouterLookup(TXOwner{ctx->OurKey(), txid}, _target, ctx, nullptr)
, localPath(path)
{
}
@ -24,8 +22,8 @@ namespace llarp
void
LocalRouterLookup::SendReply()
{
auto path = parent->GetRouter()->pathContext().GetByUpstream(
parent->OurKey().as_array(), localPath);
auto path =
parent->GetRouter()->pathContext().GetByUpstream(parent->OurKey().as_array(), localPath);
if (!path)
{
llarp::LogWarn(
@ -54,8 +52,7 @@ namespace llarp
}
}
routing::DHTMessage msg;
msg.M.emplace_back(new GotRouterMessage(parent->OurKey(), whoasked.txid,
valuesFound, true));
msg.M.emplace_back(new GotRouterMessage(parent->OurKey(), whoasked.txid, valuesFound, true));
if (!path->SendRoutingMessage(msg, parent->GetRouter()))
{
llarp::LogWarn(

@ -15,8 +15,8 @@ namespace llarp
{
PathID_t localPath;
LocalRouterLookup(const PathID_t &path, uint64_t txid,
const RouterID &target, AbstractContext *ctx);
LocalRouterLookup(
const PathID_t& path, uint64_t txid, const RouterID& target, AbstractContext* ctx);
void
SendReply() override;

@ -12,11 +12,13 @@ namespace llarp
namespace dht
{
LocalServiceAddressLookup::LocalServiceAddressLookup(
const PathID_t &pathid, uint64_t txid, uint64_t relayOrder,
const Key_t &addr, AbstractContext *ctx,
const PathID_t& pathid,
uint64_t txid,
uint64_t relayOrder,
const Key_t& addr,
AbstractContext* ctx,
__attribute__((unused)) const Key_t& askpeer)
: ServiceAddressLookup(TXOwner{ctx->OurKey(), txid}, addr, ctx,
relayOrder, nullptr)
: ServiceAddressLookup(TXOwner{ctx->OurKey(), txid}, addr, ctx, relayOrder, nullptr)
, localPath(pathid)
{
}
@ -24,8 +26,8 @@ namespace llarp
void
LocalServiceAddressLookup::SendReply()
{
auto path = parent->GetRouter()->pathContext().GetByUpstream(
parent->OurKey().as_array(), localPath);
auto path =
parent->GetRouter()->pathContext().GetByUpstream(parent->OurKey().as_array(), localPath);
if (!path)
{
llarp::LogWarn(

@ -13,8 +13,11 @@ namespace llarp
{
PathID_t localPath;
LocalServiceAddressLookup(const PathID_t &pathid, uint64_t txid,
uint64_t relayOrder, const Key_t &addr,
LocalServiceAddressLookup(
const PathID_t& pathid,
uint64_t txid,
uint64_t relayOrder,
const Key_t& addr,
AbstractContext* ctx,
__attribute__((unused)) const Key_t& askpeer);

@ -10,19 +10,17 @@ namespace llarp
{
namespace dht
{
LocalTagLookup::LocalTagLookup(const PathID_t &path, uint64_t txid,
const service::Tag &_target,
AbstractContext *ctx)
: TagLookup(TXOwner{ctx->OurKey(), txid}, _target, ctx, 0)
, localPath(path)
LocalTagLookup::LocalTagLookup(
const PathID_t& path, uint64_t txid, const service::Tag& _target, AbstractContext* ctx)
: TagLookup(TXOwner{ctx->OurKey(), txid}, _target, ctx, 0), localPath(path)
{
}
void
LocalTagLookup::SendReply()
{
auto path = parent->GetRouter()->pathContext().GetByUpstream(
parent->OurKey().as_array(), localPath);
auto path =
parent->GetRouter()->pathContext().GetByUpstream(parent->OurKey().as_array(), localPath);
if (!path)
{
llarp::LogWarn(

@ -11,8 +11,8 @@ namespace llarp
{
PathID_t localPath;
LocalTagLookup(const PathID_t &path, uint64_t txid,
const service::Tag &target, AbstractContext *ctx);
LocalTagLookup(
const PathID_t& path, uint64_t txid, const service::Tag& target, AbstractContext* ctx);
void
SendReply() override;

@ -19,8 +19,7 @@ namespace llarp
bool firstKey = true;
bool relayed = false;
MessageDecoder(const Key_t &from, bool wasRelayed)
: From(from), relayed(wasRelayed)
MessageDecoder(const Key_t& from, bool wasRelayed) : From(from), relayed(wasRelayed)
{
}
@ -42,8 +41,7 @@ namespace llarp
// bad msg size?
if (strbuf.sz != 1)
return false;
llarp::LogDebug("Handle DHT message ", *strbuf.base,
" relayed=", relayed);
llarp::LogDebug("Handle DHT message ", *strbuf.base, " relayed=", relayed);
switch (*strbuf.base)
{
case 'F':
@ -97,8 +95,7 @@ namespace llarp
struct ListDecoder
{
ListDecoder(bool hasRelayed, const Key_t &from,
std::vector< IMessage::Ptr_t > &list)
ListDecoder(bool hasRelayed, const Key_t& from, std::vector<IMessage::Ptr_t>& list)
: relayed(hasRelayed), From(from), l(list)
{
}
@ -124,8 +121,8 @@ namespace llarp
};
bool
DecodeMesssageList(Key_t from, llarp_buffer_t *buf,
std::vector< IMessage::Ptr_t > &list, bool relayed)
DecodeMesssageList(
Key_t from, llarp_buffer_t* buf, std::vector<IMessage::Ptr_t>& list, bool relayed)
{
ListDecoder dec(relayed, from, list);
return bencode_read_list(dec, buf);

@ -26,8 +26,7 @@ namespace llarp
using Ptr_t = std::unique_ptr<IMessage>;
virtual bool
HandleMessage(struct llarp_dht_context* dht,
std::vector< Ptr_t >& replies) const = 0;
HandleMessage(struct llarp_dht_context* dht, std::vector<Ptr_t>& replies) const = 0;
virtual bool
BEncode(llarp_buffer_t* buf) const = 0;
@ -44,9 +43,8 @@ namespace llarp
DecodeMessage(const Key_t& from, llarp_buffer_t* buf, bool relayed = false);
bool
DecodeMesssageList(Key_t from, llarp_buffer_t* buf,
std::vector< IMessage::Ptr_t >& dst,
bool relayed = false);
DecodeMesssageList(
Key_t from, llarp_buffer_t* buf, std::vector<IMessage::Ptr_t>& dst, bool relayed = false);
} // namespace dht
} // namespace llarp

@ -28,8 +28,7 @@ namespace llarp
if (!BEncodeMaybeReadDictInt("T", txID, read, k, val))
return false;
if(!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read, k,
val))
if (!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read, k, val))
return false;
return read;
@ -105,13 +104,12 @@ namespace llarp
return true;
}
auto closestRCs = dht.GetRouter()->nodedb()->FindClosestTo(
location, IntroSetStorageRedundancy);
auto closestRCs =
dht.GetRouter()->nodedb()->FindClosestTo(location, IntroSetStorageRedundancy);
if (closestRCs.size() <= relayOrder)
{
llarp::LogWarn("Can't fulfill FindIntro for relayOrder: ",
relayOrder);
llarp::LogWarn("Can't fulfill FindIntro for relayOrder: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}

@ -18,8 +18,7 @@ namespace llarp
bool relayed = false;
uint64_t relayOrder = 0;
FindIntroMessage(const Key_t& from, bool relay, uint64_t order)
: IMessage(from)
FindIntroMessage(const Key_t& from, bool relay, uint64_t order) : IMessage(from)
{
relayed = relay;
relayOrder = order;
@ -30,8 +29,7 @@ namespace llarp
{
}
explicit FindIntroMessage(uint64_t txid, const Key_t& addr,
uint64_t order)
explicit FindIntroMessage(uint64_t txid, const Key_t& addr, uint64_t order)
: IMessage({}), location(addr), txID(txid), relayOrder(order)
{
tagName.Zero();
@ -46,8 +44,7 @@ namespace llarp
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override;
bool
HandleMessage(llarp_dht_context* ctx,
std::vector< IMessage::Ptr_t >& replies) const override;
HandleMessage(llarp_dht_context* ctx, std::vector<IMessage::Ptr_t>& replies) const override;
};
} // namespace dht
} // namespace llarp

@ -15,8 +15,7 @@ namespace llarp
{
bool
RelayedFindRouterMessage::HandleMessage(
llarp_dht_context *ctx,
std::vector< std::unique_ptr< IMessage > > &replies) const
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const
{
auto& dht = *ctx->impl;
/// lookup for us, send an immeidate reply
@ -24,12 +23,10 @@ namespace llarp
const Key_t k{targetKey};
if (k == us)
{
auto path =
dht.GetRouter()->pathContext().GetByUpstream(targetKey, pathID);
auto path = dht.GetRouter()->pathContext().GetByUpstream(targetKey, pathID);
if (path)
{
replies.emplace_back(
new GotRouterMessage(k, txid, {dht.GetRouter()->rc()}, false));
replies.emplace_back(new GotRouterMessage(k, txid, {dht.GetRouter()->rc()}, false));
return true;
}
return false;
@ -150,20 +147,16 @@ namespace llarp
bool
FindRouterMessage::HandleMessage(
llarp_dht_context *ctx,
std::vector< std::unique_ptr< IMessage > > &replies) const
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const
{
auto& dht = *ctx->impl;
auto router = dht.GetRouter();
router->NotifyRouterEvent< tooling::FindRouterReceivedEvent >(
router->pubkey(),
this);
router->NotifyRouterEvent<tooling::FindRouterReceivedEvent>(router->pubkey(), this);
if (!dht.AllowTransit())
{
llarp::LogWarn("Got DHT lookup from ", From,
" when we are not allowing dht transit");
llarp::LogWarn("Got DHT lookup from ", From, " when we are not allowing dht transit");
return false;
}
if (dht.pendingRouterLookups().HasPendingLookupFrom({From, txid}))
@ -179,8 +172,7 @@ namespace llarp
}
const Key_t k(targetKey);
if (exploritory)
return dht.HandleExploritoryRouterLookup(From, txid, targetKey,
replies);
return dht.HandleExploritoryRouterLookup(From, txid, targetKey, replies);
dht.LookupRouterRelayed(From, txid, k, !iterative, replies);
return true;
}

@ -35,8 +35,7 @@ namespace llarp
bool
HandleMessage(
llarp_dht_context* ctx,
std::vector< std::unique_ptr< IMessage > >& replies) const override;
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const override;
RouterID targetKey;
bool iterative = false;
@ -56,8 +55,7 @@ namespace llarp
/// the path of the result
/// TODO: smart path expiration logic needs to be implemented
bool
HandleMessage(llarp_dht_context* ctx,
std::vector< IMessage::Ptr_t >& replies) const override;
HandleMessage(llarp_dht_context* ctx, std::vector<IMessage::Ptr_t>& replies) const override;
};
} // namespace dht
} // namespace llarp

@ -13,22 +13,21 @@ namespace llarp
{
namespace dht
{
GotIntroMessage::GotIntroMessage(
std::vector< service::EncryptedIntroSet > results, uint64_t tx)
GotIntroMessage::GotIntroMessage(std::vector<service::EncryptedIntroSet> results, uint64_t tx)
: IMessage({}), found(std::move(results)), txid(tx)
{
}
bool
GotIntroMessage::HandleMessage(
llarp_dht_context *ctx,
std::vector< std::unique_ptr< IMessage > > & /*replies*/) const
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& /*replies*/) const
{
auto& dht = *ctx->impl;
auto* router = dht.GetRouter();
router->NotifyRouterEvent<tooling::GotIntroReceivedEvent>(
router->pubkey(), Key_t(From.data()),
router->pubkey(),
Key_t(From.data()),
(found.size() > 0 ? found[0] : llarp::service::EncryptedIntroSet{}),
txid);
@ -45,14 +44,12 @@ namespace llarp
}
TXOwner owner(From, txid);
auto serviceLookup =
dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
auto serviceLookup = dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
if (serviceLookup)
{
if (not found.empty())
{
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target,
found);
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target, found);
}
else
{
@ -67,12 +64,10 @@ namespace llarp
bool
RelayedGotIntroMessage::HandleMessage(
llarp_dht_context* ctx,
__attribute__((unused))
std::vector< std::unique_ptr< IMessage > > &replies) const
__attribute__((unused)) std::vector<std::unique_ptr<IMessage>>& replies) const
{
// TODO: implement me better?
auto pathset =
ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
if (pathset)
{
auto copy = std::make_shared<const RelayedGotIntroMessage>(*this);

@ -27,10 +27,7 @@ namespace llarp
}
GotIntroMessage(const GotIntroMessage& other)
: IMessage(other.From)
, found(other.found)
, txid(other.txid)
, closer(other.closer)
: IMessage(other.From), found(other.found), txid(other.txid), closer(other.closer)
{
version = other.version;
}
@ -42,8 +39,7 @@ namespace llarp
}
/// for recursive reply
GotIntroMessage(std::vector< service::EncryptedIntroSet > results,
uint64_t txid);
GotIntroMessage(std::vector<service::EncryptedIntroSet> results, uint64_t txid);
~GotIntroMessage() override = default;
@ -54,8 +50,7 @@ namespace llarp
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override;
bool
HandleMessage(llarp_dht_context* ctx,
std::vector< IMessage::Ptr_t >& replies) const override;
HandleMessage(llarp_dht_context* ctx, std::vector<IMessage::Ptr_t>& replies) const override;
};
struct RelayedGotIntroMessage final : public GotIntroMessage
@ -65,8 +60,7 @@ namespace llarp
}
bool
HandleMessage(llarp_dht_context* ctx,
std::vector< IMessage::Ptr_t >& replies) const override;
HandleMessage(llarp_dht_context* ctx, std::vector<IMessage::Ptr_t>& replies) const override;
};
using GotIntroMessage_constptr = std::shared_ptr<const GotIntroMessage>;

@ -73,8 +73,7 @@ namespace llarp
return bencode_read_integer(val, &txid);
}
bool read = false;
if(!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read,
key, val))
if (!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read, key, val))
return false;
return read;
@ -83,14 +82,12 @@ namespace llarp
bool
GotRouterMessage::HandleMessage(
llarp_dht_context* ctx,
__attribute__((unused))
std::vector< std::unique_ptr< IMessage > > &replies) const
__attribute__((unused)) std::vector<std::unique_ptr<IMessage>>& replies) const
{
auto& dht = *ctx->impl;
if (relayed)
{
auto pathset =
ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
auto copy = std::make_shared<const GotRouterMessage>(*this);
return pathset && pathset->HandleGotRouterMessage(copy);
}
@ -129,8 +126,7 @@ namespace llarp
{
LogWarn("Received Gossiped RC, generating RCGossipReceivedEvent");
auto* router = dht.GetRouter();
router->NotifyRouterEvent< tooling::RCGossipReceivedEvent >(
router->pubkey(), rc);
router->NotifyRouterEvent<tooling::RCGossipReceivedEvent>(router->pubkey(), rc);
router->GossipRCIfNeeded(rc);
}
}

@ -13,38 +13,27 @@ namespace llarp
{
struct GotRouterMessage final : public IMessage
{
GotRouterMessage(const Key_t& from, bool tunneled)
: IMessage(from), relayed(tunneled)
GotRouterMessage(const Key_t& from, bool tunneled) : IMessage(from), relayed(tunneled)
{
}
GotRouterMessage(const Key_t& from, uint64_t id,
const std::vector< RouterContact >& results,
bool tunneled)
GotRouterMessage(
const Key_t& from, uint64_t id, const std::vector<RouterContact>& results, bool tunneled)
: IMessage(from), foundRCs(results), txid(id), relayed(tunneled)
{
}
GotRouterMessage(const Key_t& from, const Key_t& closer, uint64_t id,
bool tunneled)
: IMessage(from)
, closerTarget(new Key_t(closer))
, txid(id)
, relayed(tunneled)
GotRouterMessage(const Key_t& from, const Key_t& closer, uint64_t id, bool tunneled)
: IMessage(from), closerTarget(new Key_t(closer)), txid(id), relayed(tunneled)
{
}
GotRouterMessage(uint64_t id, std::vector< RouterID > _near,
bool tunneled)
: IMessage({})
, nearKeys(std::move(_near))
, txid(id)
, relayed(tunneled)
GotRouterMessage(uint64_t id, std::vector<RouterID> _near, bool tunneled)
: IMessage({}), nearKeys(std::move(_near)), txid(id), relayed(tunneled)
{
}
/// gossip message
GotRouterMessage(const RouterContact rc)
: IMessage({}), foundRCs({rc}), txid(0)
GotRouterMessage(const RouterContact rc) : IMessage({}), foundRCs({rc}), txid(0)
{
version = LLARP_PROTO_VERSION;
}
@ -70,8 +59,7 @@ namespace llarp
bool
HandleMessage(
llarp_dht_context* ctx,
std::vector< std::unique_ptr< IMessage > >& replies) const override;
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const override;
std::vector<RouterContact> foundRCs;
std::vector<RouterID> nearKeys;

@ -17,8 +17,7 @@ namespace llarp
PublishIntroMessage::~PublishIntroMessage() = default;
bool
PublishIntroMessage::DecodeKey(const llarp_buffer_t &key,
llarp_buffer_t *val)
PublishIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
{
bool read = false;
if (!BEncodeMaybeReadDictEntry("I", introset, read, key, val))
@ -55,8 +54,7 @@ namespace llarp
bool
PublishIntroMessage::HandleMessage(
llarp_dht_context *ctx,
std::vector< std::unique_ptr< IMessage > > &replies) const
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const
{
const auto now = ctx->impl->Now();
const llarp::dht::Key_t addr(introset.derivedSigningKey);
@ -64,14 +62,16 @@ namespace llarp
auto router = ctx->impl->GetRouter();
router->NotifyRouterEvent<tooling::PubIntroReceivedEvent>(
router->pubkey(), Key_t(relayed ? router->pubkey() : From.data()),
addr, txID, relayOrder);
router->pubkey(),
Key_t(relayed ? router->pubkey() : From.data()),
addr,
txID,
relayOrder);
auto& dht = *ctx->impl;
if (!introset.Verify(now))
{
llarp::LogWarn("Received PublishIntroMessage with invalid introset: ",
introset);
llarp::LogWarn("Received PublishIntroMessage with invalid introset: ", introset);
// don't propogate or store
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
@ -80,19 +80,16 @@ namespace llarp
if (introset.IsExpired(now + llarp::service::MAX_INTROSET_TIME_DELTA))
{
// don't propogate or store
llarp::LogWarn("Received PublishIntroMessage with expired Introset: ",
introset);
llarp::LogWarn("Received PublishIntroMessage with expired Introset: ", introset);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
// identify closest 4 routers
auto closestRCs = dht.GetRouter()->nodedb()->FindClosestTo(
addr, IntroSetStorageRedundancy);
auto closestRCs = dht.GetRouter()->nodedb()->FindClosestTo(addr, IntroSetStorageRedundancy);
if (closestRCs.size() != IntroSetStorageRedundancy)
{
llarp::LogWarn("Received PublishIntroMessage but only know ",
closestRCs.size(), " nodes");
llarp::LogWarn("Received PublishIntroMessage but only know ", closestRCs.size(), " nodes");
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
@ -108,8 +105,7 @@ namespace llarp
if (peer == us)
{
llarp::LogInfo("we are peer ", index,
" so storing instead of propagating");
llarp::LogInfo("we are peer ", index, " so storing instead of propagating");
dht.services()->PutNode(introset);
replies.emplace_back(new GotIntroMessage({introset}, txID));
@ -132,15 +128,12 @@ namespace llarp
{
if (relayOrder >= IntroSetStorageRedundancy)
{
llarp::LogWarn(
"Received PublishIntroMessage with invalid relayOrder: ",
relayOrder);
llarp::LogWarn("Received PublishIntroMessage with invalid relayOrder: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
llarp::LogInfo("Relaying PublishIntroMessage for ", keyStr,
", txid=", txID);
llarp::LogInfo("Relaying PublishIntroMessage for ", keyStr, ", txid=", txID);
propagateIfNotUs(relayOrder);
}
@ -160,8 +153,13 @@ namespace llarp
if (candidateNumber >= 0)
{
LogInfo("Received PubIntro for ", keyStr, ", txid=", txID,
" and we are candidate ", candidateNumber);
LogInfo(
"Received PubIntro for ",
keyStr,
", txid=",
txID,
" and we are candidate ",
candidateNumber);
dht.services()->PutNode(introset);
replies.emplace_back(new GotIntroMessage({introset}, txID));
}
@ -170,7 +168,11 @@ namespace llarp
LogWarn(
"!!! Received PubIntro with relayed==false but we aren't"
" candidate, intro derived key: ",
keyStr, ", txid=", txID, ", message from: ", From);
keyStr,
", txid=",
txID,
", message from: ",
From);
}
}

@ -17,18 +17,16 @@ namespace llarp
bool relayed = false;
uint64_t relayOrder = 0;
uint64_t txID = 0;
PublishIntroMessage(const Key_t& from, bool relayed_)
: IMessage(from), relayed(relayed_)
PublishIntroMessage(const Key_t& from, bool relayed_) : IMessage(from), relayed(relayed_)
{
}
PublishIntroMessage(const llarp::service::EncryptedIntroSet& introset_,
uint64_t tx, bool relayed_, uint64_t relayOrder_)
: IMessage({})
, introset(introset_)
, relayed(relayed_)
, relayOrder(relayOrder_)
, txID(tx)
PublishIntroMessage(
const llarp::service::EncryptedIntroSet& introset_,
uint64_t tx,
bool relayed_,
uint64_t relayOrder_)
: IMessage({}), introset(introset_), relayed(relayed_), relayOrder(relayOrder_), txID(tx)
{
}
@ -42,8 +40,7 @@ namespace llarp
bool
HandleMessage(
llarp_dht_context* ctx,
std::vector< std::unique_ptr< IMessage > >& replies) const override;
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const override;
};
} // namespace dht
} // namespace llarp

@ -13,8 +13,10 @@ namespace llarp
namespace dht
{
PublishServiceJob::PublishServiceJob(
const TXOwner &asker, const service::EncryptedIntroSet &introset_,
AbstractContext *ctx, uint64_t relayOrder_)
const TXOwner& asker,
const service::EncryptedIntroSet& introset_,
AbstractContext* ctx,
uint64_t relayOrder_)
: TX<TXOwner, service::EncryptedIntroSet>(asker, asker, ctx)
, relayOrder(relayOrder_)
, introset(introset_)
@ -26,8 +28,7 @@ namespace llarp
{
if (value.derivedSigningKey != introset.derivedSigningKey)
{
llarp::LogWarn(
"publish introset acknowledgement acked a different service");
llarp::LogWarn("publish introset acknowledgement acked a different service");
return false;
}
const llarp_time_t now = llarp::time_now_ms();
@ -38,32 +39,31 @@ namespace llarp
PublishServiceJob::Start(const TXOwner& peer)
{
parent->DHTSendTo(
peer.node.as_array(),
new PublishIntroMessage(introset, peer.txid, false, relayOrder));
peer.node.as_array(), new PublishIntroMessage(introset, peer.txid, false, relayOrder));
}
void
PublishServiceJob::SendReply()
{
parent->DHTSendTo(whoasked.node.as_array(),
new GotIntroMessage({introset}, whoasked.txid));
parent->DHTSendTo(whoasked.node.as_array(), new GotIntroMessage({introset}, whoasked.txid));
}
LocalPublishServiceJob::LocalPublishServiceJob(
const TXOwner &peer, const PathID_t &fromID, uint64_t _txid,
const service::EncryptedIntroSet &introset, AbstractContext *ctx,
const TXOwner& peer,
const PathID_t& fromID,
uint64_t _txid,
const service::EncryptedIntroSet& introset,
AbstractContext* ctx,
uint64_t relayOrder)
: PublishServiceJob(peer, introset, ctx, relayOrder)
, localPath(fromID)
, txid(_txid)
: PublishServiceJob(peer, introset, ctx, relayOrder), localPath(fromID), txid(_txid)
{
}
void
LocalPublishServiceJob::SendReply()
{
auto path = parent->GetRouter()->pathContext().GetByUpstream(
parent->OurKey().as_array(), localPath);
auto path =
parent->GetRouter()->pathContext().GetByUpstream(parent->OurKey().as_array(), localPath);
if (!path)
{
llarp::LogWarn(

@ -17,9 +17,11 @@ namespace llarp
uint64_t relayOrder;
service::EncryptedIntroSet introset;
PublishServiceJob(const TXOwner &asker,
PublishServiceJob(
const TXOwner& asker,
const service::EncryptedIntroSet& introset,
AbstractContext *ctx, uint64_t relayOrder);
AbstractContext* ctx,
uint64_t relayOrder);
bool
Validate(const service::EncryptedIntroSet& introset) const override;
@ -35,10 +37,13 @@ namespace llarp
{
PathID_t localPath;
uint64_t txid;
LocalPublishServiceJob(const TXOwner &peer, const PathID_t &fromID,
LocalPublishServiceJob(
const TXOwner& peer,
const PathID_t& fromID,
uint64_t txid,
const service::EncryptedIntroSet& introset,
AbstractContext *ctx, uint64_t relayOrder);
AbstractContext* ctx,
uint64_t relayOrder);
void
SendReply() override;

@ -13,12 +13,12 @@ namespace llarp
{
namespace dht
{
RecursiveRouterLookup::RecursiveRouterLookup(const TXOwner &_whoasked,
RecursiveRouterLookup::RecursiveRouterLookup(
const TXOwner& _whoasked,
const RouterID& _target,
AbstractContext* ctx,
RouterLookupHandler result)
: TX< RouterID, RouterContact >(_whoasked, _target, ctx)
, resultHandler(std::move(result))
: TX<RouterID, RouterContact>(_whoasked, _target, ctx), resultHandler(std::move(result))
{
peersAsked.insert(ctx->OurKey());
@ -38,8 +38,7 @@ namespace llarp
void
RecursiveRouterLookup::Start(const TXOwner& peer)
{
parent->DHTSendTo(peer.node.as_array(),
new FindRouterMessage(peer.txid, target));
parent->DHTSendTo(peer.node.as_array(), new FindRouterMessage(peer.txid, target));
}
void
@ -50,8 +49,7 @@ namespace llarp
RouterContact found;
for (const auto& rc : valuesFound)
{
if(found.OtherIsNewer(rc)
&& parent->GetRouter()->rcLookupHandler().CheckRC(rc))
if (found.OtherIsNewer(rc) && parent->GetRouter()->rcLookupHandler().CheckRC(rc))
found = rc;
}
valuesFound.clear();
@ -65,7 +63,8 @@ namespace llarp
{
parent->DHTSendTo(
whoasked.node.as_array(),
new GotRouterMessage({}, whoasked.txid, valuesFound, false), false);
new GotRouterMessage({}, whoasked.txid, valuesFound, false),
false);
}
}
} // namespace dht

@ -13,8 +13,11 @@ namespace llarp
struct RecursiveRouterLookup : public TX<RouterID, RouterContact>
{
RouterLookupHandler resultHandler;
RecursiveRouterLookup(const TXOwner &whoasked, const RouterID &target,
AbstractContext *ctx, RouterLookupHandler result);
RecursiveRouterLookup(
const TXOwner& whoasked,
const RouterID& target,
AbstractContext* ctx,
RouterLookupHandler result);
bool
Validate(const RouterContact& rc) const override;

@ -10,8 +10,11 @@ namespace llarp
namespace dht
{
ServiceAddressLookup::ServiceAddressLookup(
const TXOwner &asker, const Key_t &addr, AbstractContext *ctx,
uint32_t order, service::EncryptedIntroSetLookupHandler handler)
const TXOwner& asker,
const Key_t& addr,
AbstractContext* ctx,
uint32_t order,
service::EncryptedIntroSetLookupHandler handler)
: TX<TXOwner, service::EncryptedIntroSet>(asker, asker, ctx)
, location(addr)
, handleResult(std::move(handler))
@ -21,8 +24,7 @@ namespace llarp
}
bool
ServiceAddressLookup::Validate(
const service::EncryptedIntroSet &value) const
ServiceAddressLookup::Validate(const service::EncryptedIntroSet& value) const
{
if (!value.Verify(parent->Now()))
{
@ -40,8 +42,8 @@ namespace llarp
void
ServiceAddressLookup::Start(const TXOwner& peer)
{
parent->DHTSendTo(peer.node.as_array(),
new FindIntroMessage(peer.txid, location, relayOrder));
parent->DHTSendTo(
peer.node.as_array(), new FindIntroMessage(peer.txid, location, relayOrder));
}
void
@ -63,8 +65,7 @@ namespace llarp
{
handleResult(valuesFound);
}
parent->DHTSendTo(whoasked.node.as_array(),
new GotIntroMessage(valuesFound, whoasked.txid));
parent->DHTSendTo(whoasked.node.as_array(), new GotIntroMessage(valuesFound, whoasked.txid));
}
} // namespace dht
} // namespace llarp

@ -12,15 +12,17 @@ namespace llarp
{
struct TXOwner;
struct ServiceAddressLookup
: public TX< TXOwner, service::EncryptedIntroSet >
struct ServiceAddressLookup : public TX<TXOwner, service::EncryptedIntroSet>
{
Key_t location;
service::EncryptedIntroSetLookupHandler handleResult;
uint32_t relayOrder;
ServiceAddressLookup(const TXOwner &asker, const Key_t &addr,
AbstractContext *ctx, uint32_t relayOrder,
ServiceAddressLookup(
const TXOwner& asker,
const Key_t& addr,
AbstractContext* ctx,
uint32_t relayOrder,
service::EncryptedIntroSetLookupHandler handler);
bool

@ -28,15 +28,13 @@ namespace llarp
void
TagLookup::Start(const TXOwner& peer)
{
parent->DHTSendTo(peer.node.as_array(),
new FindIntroMessage(target, peer.txid));
parent->DHTSendTo(peer.node.as_array(), new FindIntroMessage(target, peer.txid));
}
void
TagLookup::SendReply()
{
parent->DHTSendTo(whoasked.node.as_array(),
new GotIntroMessage({}, whoasked.txid));
parent->DHTSendTo(whoasked.node.as_array(), new GotIntroMessage({}, whoasked.txid));
}
} // namespace dht
} // namespace llarp

@ -12,10 +12,9 @@ namespace llarp
struct TagLookup : public TX<service::Tag, service::EncryptedIntroSet>
{
uint64_t recursionDepth;
TagLookup(const TXOwner &asker, const service::Tag &tag,
AbstractContext *ctx, uint64_t recursion)
: TX< service::Tag, service::EncryptedIntroSet >(asker, tag, ctx)
, recursionDepth(recursion)
TagLookup(
const TXOwner& asker, const service::Tag& tag, AbstractContext* ctx, uint64_t recursion)
: TX<service::Tag, service::EncryptedIntroSet>(asker, tag, ctx), recursionDepth(recursion)
{
}

@ -40,16 +40,18 @@ namespace llarp
util::StatusObject obj{{"whoasked", whoasked.ExtractStatus()},
{"target", target.ExtractStatus()}};
std::vector<util::StatusObject> foundObjs;
std::transform(valuesFound.begin(), valuesFound.end(),
std::transform(
valuesFound.begin(),
valuesFound.end(),
std::back_inserter(foundObjs),
[](const auto& item) -> util::StatusObject {
return item.ExtractStatus();
});
[](const auto& item) -> util::StatusObject { return item.ExtractStatus(); });
obj["found"] = foundObjs;
std::vector<std::string> asked;
std::transform(
peersAsked.begin(), peersAsked.end(), std::back_inserter(asked),
peersAsked.begin(),
peersAsked.end(),
std::back_inserter(asked),
[](const auto& item) -> std::string { return item.ToString(); });
obj["asked"] = asked;
return obj;

@ -32,25 +32,30 @@ namespace llarp
{
util::StatusObject obj{};
std::vector<util::StatusObject> txObjs, timeoutsObjs, waitingObjs;
std::transform(tx.begin(), tx.end(), std::back_inserter(txObjs),
std::transform(
tx.begin(),
tx.end(),
std::back_inserter(txObjs),
[](const auto& item) -> util::StatusObject {
return util::StatusObject{
{"owner", item.first.ExtractStatus()},
return util::StatusObject{{"owner", item.first.ExtractStatus()},
{"tx", item.second->ExtractStatus()}};
});
obj["tx"] = txObjs;
std::transform(
timeouts.begin(), timeouts.end(), std::back_inserter(timeoutsObjs),
timeouts.begin(),
timeouts.end(),
std::back_inserter(timeoutsObjs),
[](const auto& item) -> util::StatusObject {
return util::StatusObject{{"time", to_json(item.second)},
{"target", item.first.ExtractStatus()}};
});
obj["timeouts"] = timeoutsObjs;
std::transform(waiting.begin(), waiting.end(),
std::transform(
waiting.begin(),
waiting.end(),
std::back_inserter(waitingObjs),
[](const auto& item) -> util::StatusObject {
return util::StatusObject{
{"target", item.first.ExtractStatus()},
return util::StatusObject{{"target", item.first.ExtractStatus()},
{"whoasked", item.second.ExtractStatus()}};
});
obj["waiting"] = waitingObjs;
@ -70,8 +75,12 @@ namespace llarp
}
void
NewTX(const TXOwner& askpeer, const TXOwner& whoasked, const K& k,
TX< K, V >* t, llarp_time_t requestTimeoutMS = 15s);
NewTX(
const TXOwner& askpeer,
const TXOwner& whoasked,
const K& k,
TX<K, V>* t,
llarp_time_t requestTimeoutMS = 15s);
/// mark tx as not fond
void
@ -85,8 +94,12 @@ namespace llarp
/// inform all watches for key of values found
void
Inform(TXOwner from, K key, std::vector< V > values,
bool sendreply = false, bool removeTimeouts = true);
Inform(
TXOwner from,
K key,
std::vector<V> values,
bool sendreply = false,
bool removeTimeouts = true);
void
Expire(llarp_time_t now);
@ -107,8 +120,10 @@ namespace llarp
template <typename K, typename V, typename K_Hash>
void
TXHolder< K, V, K_Hash >::NewTX(const TXOwner& askpeer,
const TXOwner& whoasked, const K& k,
TXHolder<K, V, K_Hash>::NewTX(
const TXOwner& askpeer,
const TXOwner& whoasked,
const K& k,
TX<K, V>* t,
llarp_time_t requestTimeoutMS)
{
@ -130,8 +145,7 @@ namespace llarp
template <typename K, typename V, typename K_Hash>
void
TXHolder< K, V, K_Hash >::NotFound(const TXOwner& from,
const std::unique_ptr< Key_t >&)
TXHolder<K, V, K_Hash>::NotFound(const TXOwner& from, const std::unique_ptr<Key_t>&)
{
auto txitr = tx.find(from);
if (txitr == tx.end())
@ -143,9 +157,8 @@ namespace llarp
template <typename K, typename V, typename K_Hash>
void
TXHolder< K, V, K_Hash >::Inform(TXOwner from, K key,
std::vector< V > values, bool sendreply,
bool removeTimeouts)
TXHolder<K, V, K_Hash>::Inform(
TXOwner from, K key, std::vector<V> values, bool sendreply, bool removeTimeouts)
{
auto range = waiting.equal_range(key);
auto itr = range.first;

@ -67,8 +67,7 @@ namespace llarp
{
}
Message::Message(const MessageHeader& hdr)
: hdr_id(hdr.id), hdr_fields(hdr.fields)
Message::Message(const MessageHeader& hdr) : hdr_id(hdr.id), hdr_fields(hdr.fields)
{
questions.resize(size_t(hdr.qd_count));
answers.resize(size_t(hdr.an_count));

@ -35,9 +35,9 @@ namespace llarp
bool
operator==(const MessageHeader& other) const
{
return id == other.id && fields == other.fields
&& qd_count == other.qd_count && an_count == other.an_count
&& ns_count == other.ns_count && ar_count == other.ar_count;
return id == other.id && fields == other.fields && qd_count == other.qd_count
&& an_count == other.an_count && ns_count == other.ns_count
&& ar_count == other.ar_count;
}
};

@ -29,8 +29,7 @@ namespace llarp
bool
operator==(const Question& other) const
{
return qname == other.qname && qtype == other.qtype
&& qclass == other.qclass;
return qname == other.qname && qtype == other.qtype && qclass == other.qclass;
}
Name_t qname;

@ -9,8 +9,11 @@ namespace llarp
{
namespace dns
{
Proxy::Proxy(llarp_ev_loop_ptr serverLoop, Logic_ptr serverLogic,
llarp_ev_loop_ptr clientLoop, Logic_ptr clientLogic,
Proxy::Proxy(
llarp_ev_loop_ptr serverLoop,
Logic_ptr serverLogic,
llarp_ev_loop_ptr clientLoop,
Logic_ptr clientLogic,
IQueryHandler* h)
: m_ServerLoop(std::move(serverLoop))
, m_ClientLoop(std::move(clientLoop))
@ -32,8 +35,7 @@ namespace llarp
}
bool
Proxy::Start(const llarp::Addr addr,
const std::vector< llarp::Addr >& resolvers)
Proxy::Start(const llarp::Addr addr, const std::vector<llarp::Addr>& resolvers)
{
m_Resolvers.clear();
m_Resolvers = resolvers;
@ -57,29 +59,25 @@ namespace llarp
}
void
Proxy::HandleUDPRecv_server(llarp_udp_io* u, const sockaddr* from,
ManagedBuffer buf)
Proxy::HandleUDPRecv_server(llarp_udp_io* u, const sockaddr* from, ManagedBuffer buf)
{
const llarp::Addr addr(*from);
Buffer_t msgbuf = CopyBuffer(buf.underlying);
auto self = static_cast<Proxy*>(u->user)->shared_from_this();
// yes we use the server loop here because if the server loop is not the
// client loop we'll crash again
LogicCall(self->m_ServerLogic, [self, addr, msgbuf]() {
self->HandlePktServer(addr, msgbuf);
});
LogicCall(
self->m_ServerLogic, [self, addr, msgbuf]() { self->HandlePktServer(addr, msgbuf); });
}
void
Proxy::HandleUDPRecv_client(llarp_udp_io* u, const sockaddr* from,
ManagedBuffer buf)
Proxy::HandleUDPRecv_client(llarp_udp_io* u, const sockaddr* from, ManagedBuffer buf)
{
const llarp::Addr addr(*from);
Buffer_t msgbuf = CopyBuffer(buf.underlying);
auto self = static_cast<Proxy*>(u->user)->shared_from_this();
LogicCall(self->m_ServerLogic, [self, addr, msgbuf]() {
self->HandlePktClient(addr, msgbuf);
});
LogicCall(
self->m_ServerLogic, [self, addr, msgbuf]() { self->HandlePktClient(addr, msgbuf); });
}
llarp::Addr
@ -158,8 +156,7 @@ namespace llarp
msg.hdr_id = itr->first.txid;
if (!m_QueryHandler->HandleHookedDNSMessage(
std::move(msg),
std::bind(&Proxy::SendServerMessageTo, self, requester,
std::placeholders::_1)))
std::bind(&Proxy::SendServerMessageTo, self, requester, std::placeholders::_1)))
{
llarp::LogWarn("failed to handle hooked dns");
}
@ -218,8 +215,7 @@ namespace llarp
{
if (!m_QueryHandler->HandleHookedDNSMessage(
std::move(msg),
std::bind(&Proxy::SendServerMessageTo, self, from,
std::placeholders::_1)))
std::bind(&Proxy::SendServerMessageTo, self, from, std::placeholders::_1)))
{
llarp::LogWarn("failed to handle hooked dns");
}

@ -24,20 +24,21 @@ namespace llarp
/// handle a hooked message
virtual bool
HandleHookedDNSMessage(Message query,
std::function< void(Message) > sendReply) = 0;
HandleHookedDNSMessage(Message query, std::function<void(Message)> sendReply) = 0;
};
struct Proxy : public std::enable_shared_from_this<Proxy>
{
using Logic_ptr = std::shared_ptr<Logic>;
Proxy(llarp_ev_loop_ptr serverLoop, Logic_ptr serverLogic,
llarp_ev_loop_ptr clientLoop, Logic_ptr clientLogic,
Proxy(
llarp_ev_loop_ptr serverLoop,
Logic_ptr serverLogic,
llarp_ev_loop_ptr clientLoop,
Logic_ptr clientLogic,
IQueryHandler* handler);
bool
Start(const llarp::Addr addr,
const std::vector< llarp::Addr >& resolvers);
Start(const llarp::Addr addr, const std::vector<llarp::Addr>& resolvers);
void
Stop();
@ -47,11 +48,9 @@ namespace llarp
private:
/// low level packet handler
static void
HandleUDPRecv_client(llarp_udp_io*, const struct sockaddr*,
ManagedBuffer);
HandleUDPRecv_client(llarp_udp_io*, const struct sockaddr*, ManagedBuffer);
static void
HandleUDPRecv_server(llarp_udp_io*, const struct sockaddr*,
ManagedBuffer);
HandleUDPRecv_server(llarp_udp_io*, const struct sockaddr*, ManagedBuffer);
/// low level ticker
static void

@ -24,8 +24,7 @@ llarp_make_ev_loop()
}
void
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev,
std::shared_ptr< llarp::Logic > logic)
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev, std::shared_ptr<llarp::Logic> logic)
{
while (ev->running())
{
@ -38,8 +37,7 @@ llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev,
}
int
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
const struct sockaddr *src)
llarp_ev_add_udp(struct llarp_ev_loop* ev, struct llarp_udp_io* udp, const struct sockaddr* src)
{
udp->parent = ev;
if (ev->udp_listen(udp, src))
@ -70,8 +68,7 @@ llarp_ev_loop_stop(const llarp_ev_loop_ptr &loop)
}
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
const llarp_buffer_t &buf)
llarp_ev_udp_sendto(struct llarp_udp_io* udp, const sockaddr* to, const llarp_buffer_t& buf)
{
return udp->sendto(udp, to, buf.base, buf.sz);
}
@ -117,8 +114,7 @@ llarp_ev_tun_async_write(struct llarp_tun_io *tun, const llarp_buffer_t &buf)
#ifndef _WIN32
return tun->writepkt(tun, buf.base, buf.sz);
#else
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
}
@ -145,8 +141,7 @@ llarp_tcp_conn_async_write(struct llarp_tcp_conn *conn, const llarp_buffer_t &b)
}
void
llarp_tcp_async_try_connect(struct llarp_ev_loop *loop,
struct llarp_tcp_connecter *tcp)
llarp_tcp_async_try_connect(struct llarp_ev_loop* loop, struct llarp_tcp_connecter* tcp)
{
tcp->loop = loop;
llarp::string_view addr_str, port_str;
@ -182,8 +177,8 @@ llarp_tcp_async_try_connect(struct llarp_ev_loop *loop,
}
bool
llarp_tcp_serve(struct llarp_ev_loop *loop, struct llarp_tcp_acceptor *tcp,
const struct sockaddr *bindaddr)
llarp_tcp_serve(
struct llarp_ev_loop* loop, struct llarp_tcp_acceptor* tcp, const struct sockaddr* bindaddr)
{
tcp->loop = loop;
return loop->tcp_listen(tcp, bindaddr);

@ -51,8 +51,7 @@ llarp_make_ev_loop();
// run mainloop
void
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev,
std::shared_ptr< llarp::Logic > logic);
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev, std::shared_ptr<llarp::Logic> logic);
/// get the current time on the event loop
llarp_time_t
@ -78,11 +77,9 @@ struct llarp_udp_io
/// called every event loop tick after reads
void (*tick)(struct llarp_udp_io*);
/// sockaddr * is the source address
void (*recvfrom)(struct llarp_udp_io *, const struct sockaddr *,
ManagedBuffer);
void (*recvfrom)(struct llarp_udp_io*, const struct sockaddr*, ManagedBuffer);
/// set by parent
int (*sendto)(struct llarp_udp_io *, const struct sockaddr *, const byte_t *,
size_t);
int (*sendto)(struct llarp_udp_io*, const struct sockaddr*, const byte_t*, size_t);
};
/// get all packets recvieved last tick
@ -92,13 +89,11 @@ llarp_ev_udp_recvmany(struct llarp_udp_io *udp, struct llarp_pkt_list *pkts);
/// add UDP handler
int
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
const struct sockaddr *src);
llarp_ev_add_udp(struct llarp_ev_loop* ev, struct llarp_udp_io* udp, const struct sockaddr* src);
/// send a UDP packet
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const struct sockaddr *to,
const llarp_buffer_t &pkt);
llarp_ev_udp_sendto(struct llarp_udp_io* udp, const struct sockaddr* to, const llarp_buffer_t& pkt);
/// close UDP handler
int
@ -160,8 +155,7 @@ struct llarp_tcp_connecter
/// async try connecting to a remote connection 1 time
void
llarp_tcp_async_try_connect(struct llarp_ev_loop *l,
struct llarp_tcp_connecter *tcp);
llarp_tcp_async_try_connect(struct llarp_ev_loop* l, struct llarp_tcp_connecter* tcp);
/// handles inbound connections
struct llarp_tcp_acceptor
@ -186,8 +180,7 @@ struct llarp_tcp_acceptor
/// return false if failed to bind
/// return true on success
bool
llarp_tcp_serve(struct llarp_ev_loop *loop, struct llarp_tcp_acceptor *t,
const sockaddr *bindaddr);
llarp_tcp_serve(struct llarp_ev_loop* loop, struct llarp_tcp_acceptor* t, const sockaddr* bindaddr);
/// close and stop accepting connections
void

@ -31,8 +31,7 @@ typedef struct sockaddr_un
} SOCKADDR_UN, *PSOCKADDR_UN;
#else
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|| (__APPLE__ && __MACH__)
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || (__APPLE__ && __MACH__)
#include <sys/event.h>
#endif
@ -137,15 +136,13 @@ 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;
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)
{
}
@ -154,8 +151,7 @@ namespace llarp
{
char ebuf[1024];
int err = WSAGetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL,
ebuf, 1024, nullptr);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf, 1024, nullptr);
llarp::LogError(ebuf);
}
@ -341,18 +337,20 @@ namespace llarp
};
};
using LossyWriteQueue_t =
llarp::util::CoDelQueue< WriteBuffer, WriteBuffer::GetTime,
WriteBuffer::PutTime, WriteBuffer::Compare,
WriteBuffer::GetNow, llarp::util::NullMutex,
using LossyWriteQueue_t = llarp::util::CoDelQueue<
WriteBuffer,
WriteBuffer::GetTime,
WriteBuffer::PutTime,
WriteBuffer::Compare,
WriteBuffer::GetNow,
llarp::util::NullMutex,
llarp::util::NullLock>;
using LosslessWriteQueue_t = std::deque<WriteBuffer>;
int fd;
int flags = 0;
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|| (__APPLE__ && __MACH__)
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || (__APPLE__ && __MACH__)
struct kevent change;
#endif
@ -380,7 +378,8 @@ namespace llarp
read(byte_t* buf, size_t sz) = 0;
virtual int
sendto(__attribute__((unused)) const sockaddr* dst,
sendto(
__attribute__((unused)) const sockaddr* dst,
__attribute__((unused)) const void* data,
__attribute__((unused)) size_t sz)
{
@ -540,8 +539,7 @@ namespace llarp
}
/// inbound
tcp_conn(llarp_ev_loop* loop, int _fd)
: ev_io(_fd, new LosslessWriteQueue_t{}), _conn(nullptr)
tcp_conn(llarp_ev_loop* loop, int _fd) : ev_io(_fd, new LosslessWriteQueue_t{}), _conn(nullptr)
{
tcp.impl = this;
tcp.loop = loop;
@ -553,8 +551,7 @@ namespace llarp
}
/// outbound
tcp_conn(llarp_ev_loop* loop, int _fd, const sockaddr* addr,
llarp_tcp_connecter* conn)
tcp_conn(llarp_ev_loop* loop, int _fd, const sockaddr* addr, llarp_tcp_connecter* conn)
: ev_io(_fd, new LosslessWriteQueue_t{}), _conn(conn)
{
socklen_t slen = sizeof(sockaddr_in);
@ -621,8 +618,7 @@ namespace llarp
#else
char ebuf[1024];
int err = WSAGetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL,
ebuf, 1024, nullptr);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf, 1024, nullptr);
llarp::LogError("tcp_conn error: ", ebuf);
#endif
if (_conn->error)
@ -645,8 +641,7 @@ namespace llarp
{
llarp_ev_loop* loop;
llarp_tcp_acceptor* tcp;
tcp_serv(llarp_ev_loop* l, int _fd, llarp_tcp_acceptor* t)
: ev_io(_fd), loop(l), tcp(t)
tcp_serv(llarp_ev_loop* l, int _fd, llarp_tcp_acceptor* t) : ev_io(_fd), loop(l), tcp(t)
{
tcp->impl = this;
}
@ -669,8 +664,7 @@ namespace llarp
#ifdef _WIN32
struct llarp_fd_promise
{
void
Set(std::pair< int, int >)
void Set(std::pair<int, int>)
{
}
@ -742,8 +736,7 @@ struct llarp_ev_loop
tick(int ms) = 0;
virtual uint32_t
call_after_delay(llarp_time_t delay_ms,
std::function< void(void) > callback) = 0;
call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback) = 0;
virtual void
cancel_delayed_call(uint32_t call_id) = 0;

@ -7,8 +7,7 @@
namespace libuv
{
#define LoopCall(h, ...) \
LogicCall(static_cast< Loop* >((h)->loop->data)->m_Logic, __VA_ARGS__)
#define LoopCall(h, ...) LogicCall(static_cast<Loop*>((h)->loop->data)->m_Logic, __VA_ARGS__)
struct glue
{
@ -106,8 +105,7 @@ namespace libuv
bool
ConnectAsync()
{
return uv_tcp_connect(&m_Connect, &m_Handle, m_Addr, &OnOutboundConnect)
!= -1;
return uv_tcp_connect(&m_Connect, &m_Handle, m_Addr, &OnOutboundConnect) != -1;
}
static void
@ -353,8 +351,7 @@ namespace libuv
{
uv_check_start(&m_Ticker, &OnTick);
m_Accept->close = &ExplicitCloseAccept;
return uv_tcp_bind(&m_Handle, m_Addr, 0) == 0
&& uv_listen(Stream(), 5, &OnAccept) == 0;
return uv_tcp_bind(&m_Handle, m_Addr, 0) == 0 && uv_listen(Stream(), 5, &OnAccept) == 0;
}
};
@ -404,8 +401,7 @@ namespace libuv
llarp_pkt_list m_LastPackets;
std::array<char, 1500> m_Buffer;
udp_glue(uv_loop_t* loop, llarp_udp_io* udp, const sockaddr* src)
: m_UDP(udp), m_Addr(*src)
udp_glue(uv_loop_t* loop, llarp_udp_io* udp, const sockaddr* src) : m_UDP(udp), m_Addr(*src)
{
m_Handle.data = this;
m_Ticker.data = this;
@ -422,14 +418,13 @@ namespace libuv
}
static void
OnRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf,
const struct sockaddr* addr, unsigned)
OnRecv(
uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned)
{
udp_glue* glue = static_cast<udp_glue*>(handle->data);
if (addr)
glue->RecvFrom(nread, buf, addr);
if(nread <= 0 || glue->m_UDP == nullptr
|| glue->m_UDP->recvfrom != nullptr)
if (nread <= 0 || glue->m_UDP == nullptr || glue->m_UDP->recvfrom != nullptr)
delete[] buf->base;
}
@ -711,8 +706,7 @@ namespace libuv
llarp::LogError("failed to start up ", m_Tun->ifname);
return false;
}
if(tuntap_set_ip(m_Device, m_Tun->ifaddr, m_Tun->ifaddr, m_Tun->netmask)
== -1)
if (tuntap_set_ip(m_Device, m_Tun->ifaddr, m_Tun->ifaddr, m_Tun->netmask) == -1)
{
llarp::LogError("failed to set address on ", m_Tun->ifname);
return false;
@ -724,8 +718,7 @@ namespace libuv
}
if (m_Device->tun_fd == -1)
{
llarp::LogError("tun interface ", m_Tun->ifname,
" has invalid fd: ", m_Device->tun_fd);
llarp::LogError("tun interface ", m_Tun->ifname, " has invalid fd: ", m_Device->tun_fd);
return false;
}
@ -741,11 +734,9 @@ namespace libuv
llarp::LogError("failed to start polling on ", m_Tun->ifname);
return false;
}
if(uv_check_init(loop, &m_Ticker) != 0
|| uv_check_start(&m_Ticker, &OnTick) != 0)
if (uv_check_init(loop, &m_Ticker) != 0 || uv_check_start(&m_Ticker, &OnTick) != 0)
{
llarp::LogError("failed to set up tun interface timer for ",
m_Tun->ifname);
llarp::LogError("failed to set up tun interface timer for ", m_Tun->ifname);
return false;
}
m_Tun->writepkt = &WritePkt;
@ -763,11 +754,7 @@ namespace libuv
loop->process_cancel_queue();
}
Loop::Loop()
: llarp_ev_loop()
, m_LogicCalls(1024)
, m_timerQueue(20)
, m_timerCancelQueue(20)
Loop::Loop() : llarp_ev_loop(), m_LogicCalls(1024), m_timerQueue(20), m_timerCancelQueue(20)
{
}
@ -789,7 +776,9 @@ namespace libuv
#endif
m_LogicCaller.data = this;
int err;
if((err = uv_async_init(&m_Impl, &m_LogicCaller,
if ((err = uv_async_init(
&m_Impl,
&m_LogicCaller,
[](uv_async_t* h) {
Loop* l = static_cast<Loop*>(h->data);
while (not l->m_LogicCalls.empty())
@ -870,8 +859,7 @@ namespace libuv
{
// have to delete timer handle this way because libuv.
uv_timer_stop(timer);
uv_close((uv_handle_t*)timer,
[](uv_handle_t* handle) { delete(uv_timer_t*)handle; });
uv_close((uv_handle_t*)timer, [](uv_handle_t* handle) { delete (uv_timer_t*)handle; });
}
static void
@ -886,8 +874,7 @@ namespace libuv
}
uint32_t
Loop::call_after_delay(llarp_time_t delay_ms,
std::function< void(void) > callback)
Loop::call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback)
{
#ifdef TESTNET_SPEED
delay_ms *= TESTNET_SPEED;

@ -49,8 +49,7 @@ namespace libuv
tick(int ms) override;
uint32_t
call_after_delay(llarp_time_t delay_ms,
std::function< void(void) > callback) override;
call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback) override;
void
cancel_delayed_call(uint32_t job_id) override;

@ -53,8 +53,7 @@ win32_tun_io::setup()
{
char ebuf[1024];
int err = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf,
1024, nullptr);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf, 1024, nullptr);
llarp::LogWarn("failed to put interface up: ", ebuf);
return false;
}
@ -83,8 +82,7 @@ win32_tun_io::add_ev(llarp_ev_loop* loop)
unsigned long numCPU = sys_info.dwNumberOfProcessors;
// let the system handle 2x the number of CPUs or hardware
// threads
tun_event_queue = CreateIoCompletionPort(tunif->tun_fd, nullptr,
(ULONG_PTR)this, numCPU * 2);
tun_event_queue = CreateIoCompletionPort(tunif->tun_fd, nullptr, (ULONG_PTR)this, numCPU * 2);
begin_tun_loop(numCPU * 2, loop);
}
else
@ -143,8 +141,7 @@ tun_ev_loop(void* u)
while (true)
{
alert = GetQueuedCompletionStatus(tun_event_queue, &size, &listener, &ovl,
EV_TICK_INTERVAL);
alert = GetQueuedCompletionStatus(tun_event_queue, &size, &listener, &ovl, EV_TICK_INTERVAL);
if (!alert)
{

@ -15,8 +15,7 @@
// io packet for TUN read/write
struct asio_evt_pkt
{
OVERLAPPED pkt = {
0, 0, 0, 0, nullptr}; // must be first, since this is part of the IO call
OVERLAPPED pkt = {0, 0, 0, 0, nullptr}; // must be first, since this is part of the IO call
bool write = false; // true, or false if read pkt
size_t sz; // should match the queued data size, if not try again?
void* buf; // must remain valid until we get notification; this is _supposed_

@ -19,8 +19,7 @@ llarp_ev_pkt_pipe::StartPipe()
return false;
#else
int _fds[2];
if(pipe(_fds) == -1
&& fcntl(_fds[0], F_SETFL, fcntl(_fds[0], F_GETFL) | O_NONBLOCK))
if (pipe(_fds) == -1 && fcntl(_fds[0], F_SETFL, fcntl(_fds[0], F_GETFL) | O_NONBLOCK))
{
return false;
}

@ -84,8 +84,7 @@ namespace llarp
}
bool
Context::ObtainNewExit(const PubKey& pk, const PathID_t& path,
bool permitInternet)
Context::ObtainNewExit(const PubKey& pk, const PathID_t& path, bool permitInternet)
{
auto itr = m_Exits.begin();
while (itr != m_Exits.end())

@ -35,25 +35,20 @@ namespace llarp
AddExitEndpoint(const std::string& name, const Config_t& config);
bool
ObtainNewExit(const PubKey &remote, const PathID_t &path,
bool permitInternet);
ObtainNewExit(const PubKey& remote, const PathID_t& path, bool permitInternet);
exit::Endpoint*
FindEndpointForPath(const PathID_t& path) const;
/// calculate (pk, tx, rx) for all exit traffic
using TrafficStats =
std::unordered_map< PubKey, std::pair< uint64_t, uint64_t >,
PubKey::Hash >;
using TrafficStats = std::unordered_map<PubKey, std::pair<uint64_t, uint64_t>, PubKey::Hash>;
void
CalculateExitTraffic(TrafficStats& stats);
private:
AbstractRouter* m_Router;
std::unordered_map< std::string,
std::shared_ptr< handlers::ExitEndpoint > >
m_Exits;
std::unordered_map<std::string, std::shared_ptr<handlers::ExitEndpoint>> m_Exits;
std::list<std::shared_ptr<handlers::ExitEndpoint>> m_Closed;
};
} // namespace exit

@ -8,9 +8,12 @@ namespace llarp
{
namespace exit
{
Endpoint::Endpoint(const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath, bool rewriteIP,
huint128_t ip, llarp::handlers::ExitEndpoint* parent)
Endpoint::Endpoint(
const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath,
bool rewriteIP,
huint128_t ip,
llarp::handlers::ExitEndpoint* parent)
: createdAt(parent->Now())
, m_Parent(parent)
, m_remoteSignKey(remoteIdent)
@ -129,8 +132,7 @@ namespace llarp
dst = net::IPPacket::TruncateV6(m_Parent->GetIfAddr());
else
dst = pkt.dstv4();
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(m_IP)),
xhtonl(dst));
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(m_IP)), xhtonl(dst));
}
else
{
@ -157,8 +159,8 @@ namespace llarp
if (pkt.IsV6())
pkt.UpdateIPv6Address(src, m_IP);
else
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(src)),
xhtonl(net::IPPacket::TruncateV6(m_IP)));
pkt.UpdateIPv4Address(
xhtonl(net::IPPacket::TruncateV6(src)), xhtonl(net::IPPacket::TruncateV6(m_IP)));
const auto _pktbuf = pkt.Buffer();
const llarp_buffer_t& pktbuf = _pktbuf.underlying;
@ -220,8 +222,7 @@ namespace llarp
Endpoint::GetCurrentPath() const
{
auto router = m_Parent->GetRouter();
return router->pathContext().GetByUpstream(router->pubkey(),
m_CurrentPath);
return router->pathContext().GetByUpstream(router->pubkey(), m_CurrentPath);
}
} // namespace exit
} // namespace llarp

@ -23,8 +23,11 @@ namespace llarp
{
static constexpr size_t MaxUpstreamQueueSize = 256;
Endpoint(const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath, bool rewriteIP, huint128_t ip,
Endpoint(
const llarp::PubKey& remoteIdent,
const llarp::PathID_t& beginPath,
bool rewriteIP,
huint128_t ip,
llarp::handlers::ExitEndpoint* parent);
~Endpoint();
@ -113,16 +116,14 @@ namespace llarp
uint64_t m_TxRate, m_RxRate;
llarp_time_t m_LastActive;
bool m_RewriteSource;
using InboundTrafficQueue_t =
std::deque< llarp::routing::TransferTrafficMessage >;
using InboundTrafficQueue_t = std::deque<llarp::routing::TransferTrafficMessage>;
using TieredQueue = std::map<uint8_t, InboundTrafficQueue_t>;
// maps number of fragments the message will fit in to the queue for it
TieredQueue m_DownstreamQueues;
struct UpstreamBuffer
{
UpstreamBuffer(const llarp::net::IPPacket& p, uint64_t c)
: pkt(p), counter(c)
UpstreamBuffer(const llarp::net::IPPacket& p, uint64_t c) : pkt(p), counter(c)
{
}

@ -93,8 +93,7 @@ namespace llarp
}
bool
ObtainExitMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
ObtainExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
{
return h->HandleObtainExitMessage(*this, r);
}
@ -242,8 +241,7 @@ namespace llarp
}
bool
RejectExitMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
RejectExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
{
return h->HandleRejectExitMessage(*this, r);
}
@ -313,8 +311,7 @@ namespace llarp
}
bool
UpdateExitMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
UpdateExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
{
return h->HandleUpdateExitMessage(*this, r);
}
@ -336,8 +333,7 @@ namespace llarp
}
bool
UpdateExitVerifyMessage::DecodeKey(const llarp_buffer_t& k,
llarp_buffer_t* buf)
UpdateExitVerifyMessage::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
{
bool read = false;
if (!BEncodeMaybeReadDictInt("S", S, read, k, buf))
@ -350,8 +346,7 @@ namespace llarp
}
bool
UpdateExitVerifyMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
UpdateExitVerifyMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
{
return h->HandleUpdateExitVerifyMessage(*this, r);
}

@ -15,7 +15,10 @@ namespace llarp
BaseSession::BaseSession(
const llarp::RouterID& routerId,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r, size_t numpaths, size_t hoplen, bool bundleRC)
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool bundleRC)
: llarp::path::Builder(r, numpaths, hoplen)
, m_ExitRouter(routerId)
, m_WritePacket(std::move(writepkt))
@ -68,8 +71,11 @@ namespace llarp
}
bool
BaseSession::SelectHop(llarp_nodedb* db, const std::set< RouterID >& prev,
RouterContact& cur, size_t hop,
BaseSession::SelectHop(
llarp_nodedb* db,
const std::set<RouterID>& prev,
RouterContact& cur,
size_t hop,
llarp::path::PathRole roles)
{
std::set<RouterID> exclude = prev;
@ -158,8 +164,7 @@ namespace llarp
BaseSession::ResetInternalState()
{
auto sendExitClose = [&](const llarp::path::Path_ptr p) {
const static auto roles =
llarp::path::ePathRoleExit | llarp::path::ePathRoleSVC;
const static auto roles = llarp::path::ePathRoleExit | llarp::path::ePathRoleSVC;
if (p->SupportsAnyRoles(roles))
{
llarp::LogInfo(p->Name(), " closing exit path");
@ -195,8 +200,7 @@ namespace llarp
}
bool
BaseSession::HandleTraffic(llarp::path::Path_ptr, const llarp_buffer_t& buf,
uint64_t counter)
BaseSession::HandleTraffic(llarp::path::Path_ptr, const llarp_buffer_t& buf, uint64_t counter)
{
if (m_WritePacket)
{
@ -211,11 +215,9 @@ namespace llarp
}
bool
BaseSession::HandleTrafficDrop(llarp::path::Path_ptr p,
const PathID_t& path, uint64_t s)
BaseSession::HandleTrafficDrop(llarp::path::Path_ptr p, const PathID_t& path, uint64_t s)
{
llarp::LogError("dropped traffic on exit ", m_ExitRouter, " S=", s,
" P=", path);
llarp::LogError("dropped traffic on exit ", m_ExitRouter, " S=", s, " P=", path);
p->EnterState(path::ePathIgnore, m_router->Now());
return true;
}
@ -308,8 +310,7 @@ namespace llarp
if (r->nodedb()->Get(m_ExitRouter, rc))
r->TryConnectAsync(rc, 5);
else
r->LookupRouter(m_ExitRouter,
[r](const std::vector< RouterContact >& results) {
r->LookupRouter(m_ExitRouter, [r](const std::vector<RouterContact>& results) {
if (results.size())
r->TryConnectAsync(results[0], 5);
});
@ -334,8 +335,11 @@ namespace llarp
SNodeSession::SNodeSession(
const llarp::RouterID& snodeRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r, size_t numpaths, size_t hoplen,
bool useRouterSNodeKey, bool bundleRC)
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool useRouterSNodeKey,
bool bundleRC)
: BaseSession(snodeRouter, writepkt, r, numpaths, hoplen, bundleRC)
{
if (useRouterSNodeKey)

@ -27,9 +27,12 @@ namespace llarp
{
static constexpr size_t MaxUpstreamQueueLength = 256;
BaseSession(const llarp::RouterID& exitRouter,
BaseSession(
const llarp::RouterID& exitRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r, size_t numpaths, size_t hoplen,
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool bundleRC);
~BaseSession() override;
@ -64,8 +67,11 @@ namespace llarp
CheckPathDead(path::Path_ptr p, llarp_time_t dlt);
bool
SelectHop(llarp_nodedb* db, const std::set< RouterID >& prev,
RouterContact& cur, size_t hop,
SelectHop(
llarp_nodedb* db,
const std::set<RouterID>& prev,
RouterContact& cur,
size_t hop,
llarp::path::PathRole roles) override;
bool
@ -122,21 +128,18 @@ namespace llarp
PopulateRequest(llarp::routing::ObtainExitMessage& msg) const = 0;
bool
HandleTrafficDrop(llarp::path::Path_ptr p, const llarp::PathID_t& path,
uint64_t s);
HandleTrafficDrop(llarp::path::Path_ptr p, const llarp::PathID_t& path, uint64_t s);
bool
HandleGotExit(llarp::path::Path_ptr p, llarp_time_t b);
bool
HandleTraffic(llarp::path::Path_ptr p, const llarp_buffer_t& buf,
uint64_t seqno);
HandleTraffic(llarp::path::Path_ptr p, const llarp_buffer_t& buf, uint64_t seqno);
private:
std::set<RouterID> m_SnodeBlacklist;
using UpstreamTrafficQueue_t =
std::deque< llarp::routing::TransferTrafficMessage >;
using UpstreamTrafficQueue_t = std::deque<llarp::routing::TransferTrafficMessage>;
using TieredQueue_t = std::map<uint8_t, UpstreamTrafficQueue_t>;
TieredQueue_t m_Upstream;
@ -152,8 +155,7 @@ namespace llarp
};
using DownstreamTrafficQueue_t =
std::priority_queue< DownstreamPkt, std::vector< DownstreamPkt >,
DownstreamPktSorter >;
std::priority_queue<DownstreamPkt, std::vector<DownstreamPkt>, DownstreamPktSorter>;
DownstreamTrafficQueue_t m_Downstream;
uint64_t m_Counter;
@ -168,9 +170,12 @@ namespace llarp
struct ExitSession final : public BaseSession
{
ExitSession(const llarp::RouterID& snodeRouter,
ExitSession(
const llarp::RouterID& snodeRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r, size_t numpaths, size_t hoplen,
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool bundleRC)
: BaseSession(snodeRouter, writepkt, r, numpaths, hoplen, bundleRC)
{
@ -193,10 +198,14 @@ namespace llarp
struct SNodeSession final : public BaseSession
{
SNodeSession(const llarp::RouterID& snodeRouter,
SNodeSession(
const llarp::RouterID& snodeRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r, size_t numpaths, size_t hoplen,
bool useRouterSNodeKey, bool bundleRC);
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool useRouterSNodeKey,
bool bundleRC);
~SNodeSession() override = default;

@ -37,8 +37,18 @@ namespace llarp
, m_Resolver(std::make_shared<dns::Proxy>(
r->netloop(), r->logic(), r->netloop(), r->logic(), this))
, m_Name(name)
, m_Tun{{0}, 0, 0, {0}, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}
, m_Tun{{0},
0,
0,
{0},
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
, m_LocalResolverAddr("127.0.0.1", 53)
, m_InetToNetwork(name + "_exit_rx", r->netloop(), r->netloop())
@ -54,8 +64,7 @@ namespace llarp
util::StatusObject
ExitEndpoint::ExtractStatus() const
{
util::StatusObject obj{{"permitExit", m_PermitExit},
{"ip", m_IfAddr.ToString()}};
util::StatusObject obj{{"permitExit", m_PermitExit}, {"ip", m_IfAddr.ToString()}};
util::StatusObject exitsObj{};
for (const auto& item : m_ActiveExits)
{
@ -84,8 +93,7 @@ namespace llarp
return false;
return m_OurRange.Contains(ip);
}
if(msg.questions[0].qtype == dns::qTypeA
|| msg.questions[0].qtype == dns::qTypeCNAME
if (msg.questions[0].qtype == dns::qTypeA || msg.questions[0].qtype == dns::qTypeCNAME
|| msg.questions[0].qtype == dns::qTypeAAAA)
{
if (msg.questions[0].IsName("localhost.loki"))
@ -97,8 +105,7 @@ namespace llarp
}
bool
ExitEndpoint::HandleHookedDNSMessage(
dns::Message msg, std::function< void(dns::Message) > reply)
ExitEndpoint::HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)> reply)
{
if (msg.questions[0].qtype == dns::qTypePTR)
{
@ -113,8 +120,7 @@ namespace llarp
else
{
auto itr = m_IPToKey.find(ip);
if(itr != m_IPToKey.end()
&& m_SNodeKeys.find(itr->second) != m_SNodeKeys.end())
if (itr != m_IPToKey.end() && m_SNodeKeys.find(itr->second) != m_SNodeKeys.end())
{
RouterID them = itr->second;
msg.AddAReply(them.ToString());
@ -141,8 +147,7 @@ namespace llarp
else
msg.AddNXReply();
}
else if(msg.questions[0].qtype == dns::qTypeA
|| msg.questions[0].qtype == dns::qTypeAAAA)
else if (msg.questions[0].qtype == dns::qTypeA || msg.questions[0].qtype == dns::qTypeAAAA)
{
const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA;
const bool isV4 = msg.questions[0].qtype == dns::qTypeA;
@ -179,8 +184,7 @@ namespace llarp
else if (m_SNodeKeys.find(pubKey) == m_SNodeKeys.end())
{
// we do not have it mapped, async obtain it
ObtainSNodeSession(
r, [&](std::shared_ptr< exit::BaseSession > session) {
ObtainSNodeSession(r, [&](std::shared_ptr<exit::BaseSession> session) {
if (session && session->IsReady())
{
msg.AddINReply(m_KeyToIP[pubKey], isV6);
@ -214,8 +218,7 @@ namespace llarp
}
void
ExitEndpoint::ObtainSNodeSession(const RouterID &router,
exit::SessionReadyFunc obtainCb)
ExitEndpoint::ObtainSNodeSession(const RouterID& router, exit::SessionReadyFunc obtainCb)
{
ObtainServiceNodeIP(router);
m_SNodeSessions[router]->AddReadyHook(obtainCb);
@ -253,8 +256,7 @@ namespace llarp
if (itr == m_IPToKey.end())
{
// drop
LogWarn(Name(), " dropping packet, has no session at ",
pkt.dstv6());
LogWarn(Name(), " dropping packet, has no session at ", pkt.dstv6());
return;
}
pk = itr->second;
@ -275,7 +277,10 @@ namespace llarp
auto tryFlushingTraffic = [&](exit::Endpoint* const ep) -> bool {
if (!ep->QueueInboundTraffic(ManagedBuffer{pkt.Buffer()}))
{
LogWarn(Name(), " dropped inbound traffic for session ", pk,
LogWarn(
Name(),
" dropped inbound traffic for session ",
pk,
" as we are overloaded (probably)");
// continue iteration
return true;
@ -286,7 +291,10 @@ namespace llarp
if (!VisitEndpointsFor(pk, tryFlushingTraffic))
{
// we may have all dead sessions, wtf now?
LogWarn(Name(), " dropped inbound traffic for session ", pk,
LogWarn(
Name(),
" dropped inbound traffic for session ",
pk,
" as we have no working endpoints");
}
});
@ -308,8 +316,7 @@ namespace llarp
// TODO: move flush upstream to router event loop
if (!itr->second->FlushUpstream())
{
LogWarn("failed to flush snode traffic to ", itr->first,
" via outbound session");
LogWarn("failed to flush snode traffic to ", itr->first, " via outbound session");
}
itr->second->FlushDownstream();
++itr;
@ -336,8 +343,7 @@ namespace llarp
llarp::LogWarn("Could not create tunnel for exit endpoint");
return false;
}
llarp::LogInfo("Trying to start resolver ",
m_LocalResolverAddr.ToString());
llarp::LogInfo("Trying to start resolver ", m_LocalResolverAddr.ToString());
return m_Resolver->Start(m_LocalResolverAddr, m_UpstreamResolvers);
}
return true;
@ -467,9 +473,7 @@ namespace llarp
{
const llarp_buffer_t buffer(buf);
m_InetToNetwork.EmplaceIf(
[b = ManagedBuffer(buffer)](Pkt_t &pkt) -> bool {
return pkt.Load(b);
});
[b = ManagedBuffer(buffer)](Pkt_t& pkt) -> bool { return pkt.Load(b); });
}
bool
@ -482,8 +486,8 @@ namespace llarp
if (m_UseV6)
pkt.UpdateIPv6Address(from, m_IfAddr);
else
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(from)),
xhtonl(net::IPPacket::TruncateV6(m_IfAddr)));
pkt.UpdateIPv4Address(
xhtonl(net::IPPacket::TruncateV6(from)), xhtonl(net::IPPacket::TruncateV6(m_IfAddr)));
return llarp_ev_tun_async_write(&m_Tun, pkt.Buffer());
}
@ -557,8 +561,7 @@ namespace llarp
dnsport = std::atoi(v.substr(pos + 1).c_str());
}
m_UpstreamResolvers.emplace_back(resolverAddr, dnsport);
LogInfo(Name(), " adding upstream dns set to ", resolverAddr, ":",
dnsport);
LogInfo(Name(), " adding upstream dns set to ", resolverAddr, ":", dnsport);
}
if (k == "ifaddr")
{
@ -581,8 +584,16 @@ namespace llarp
m_IfAddr = m_OurRange.addr;
m_NextAddr = m_IfAddr;
m_HigestAddr = m_OurRange.HighestAddr();
LogInfo(Name(), " set ifaddr range to ", m_Tun.ifaddr, "/",
m_Tun.netmask, " lo=", m_IfAddr, " hi=", m_HigestAddr);
LogInfo(
Name(),
" set ifaddr range to ",
m_Tun.ifaddr,
"/",
m_Tun.netmask,
" lo=",
m_IfAddr,
" hi=",
m_HigestAddr);
m_UseV6 = false;
}
if (k == "ifname")
@ -625,9 +636,12 @@ namespace llarp
{
auto session = std::make_shared<exit::SNodeSession>(
other,
std::bind(&ExitEndpoint::QueueSNodePacket, this,
std::placeholders::_1, ip),
GetRouter(), 2, 1, true, false);
std::bind(&ExitEndpoint::QueueSNodePacket, this, std::placeholders::_1, ip),
GetRouter(),
2,
1,
true,
false);
// this is a new service node make an outbound session to them
m_SNodeSessions.emplace(other, session);
}
@ -635,22 +649,19 @@ namespace llarp
}
bool
ExitEndpoint::AllocateNewExit(const PubKey pk, const PathID_t &path,
bool wantInternet)
ExitEndpoint::AllocateNewExit(const PubKey pk, const PathID_t& path, bool wantInternet)
{
if (wantInternet && !m_PermitExit)
return false;
auto ip = GetIPForIdent(pk);
if(GetRouter()->pathContext().TransitHopPreviousIsRouter(path,
pk.as_array()))
if (GetRouter()->pathContext().TransitHopPreviousIsRouter(path, pk.as_array()))
{
// we think this path belongs to a service node
// mark it as such so we don't make an outbound session to them
m_SNodeKeys.emplace(pk.as_array());
}
m_ActiveExits.emplace(pk,
std::make_unique< exit::Endpoint >(
pk, path, !wantInternet, ip, this));
m_ActiveExits.emplace(
pk, std::make_unique<exit::Endpoint>(pk, path, !wantInternet, ip, this));
m_Paths[path] = pk;
@ -729,9 +740,8 @@ namespace llarp
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
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);

@ -26,8 +26,7 @@ namespace llarp
Name() const;
bool
VisitEndpointsFor(const PubKey& pk,
std::function< bool(exit::Endpoint* const) > visit);
VisitEndpointsFor(const PubKey& pk, std::function<bool(exit::Endpoint* const)> visit);
util::StatusObject
ExtractStatus() const;
@ -39,12 +38,10 @@ namespace llarp
ShouldHookDNSMessage(const dns::Message& msg) const override;
bool
HandleHookedDNSMessage(dns::Message msg,
std::function< void(dns::Message) >) override;
HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)>) override;
bool
AllocateNewExit(const PubKey pk, const PathID_t& path,
bool permitInternet);
AllocateNewExit(const PubKey pk, const PathID_t& path, bool permitInternet);
exit::Endpoint*
FindEndpointByPath(const PathID_t& path);
@ -122,8 +119,7 @@ namespace llarp
/// async obtain snode session and call callback when it's ready to send
void
ObtainSNodeSession(const RouterID& router,
exit::SessionReadyFunc obtainCb);
ObtainSNodeSession(const RouterID& router, exit::SessionReadyFunc obtainCb);
bool
QueueSNodePacket(const llarp_buffer_t& buf, huint128_t from);
@ -143,9 +139,7 @@ namespace llarp
std::unordered_map<PubKey, exit::Endpoint*, PubKey::Hash> m_ChosenExits;
std::unordered_multimap< PubKey, std::unique_ptr< exit::Endpoint >,
PubKey::Hash >
m_ActiveExits;
std::unordered_multimap<PubKey, std::unique_ptr<exit::Endpoint>, PubKey::Hash> m_ActiveExits;
using KeyMap_t = std::unordered_map<PubKey, huint128_t, PubKey::Hash>;
@ -156,8 +150,7 @@ namespace llarp
SNodes_t m_SNodeKeys;
using SNodeSessions_t =
std::unordered_map< RouterID, std::shared_ptr< exit::SNodeSession >,
RouterID::Hash >;
std::unordered_map<RouterID, std::shared_ptr<exit::SNodeSession>, RouterID::Hash>;
/// snode sessions we are talking to directly
SNodeSessions_t m_SNodeSessions;
@ -177,9 +170,13 @@ namespace llarp
std::vector<Addr> m_UpstreamResolvers;
using Pkt_t = net::IPPacket;
using PacketQueue_t =
util::CoDelQueue< Pkt_t, Pkt_t::GetTime, Pkt_t::PutTime,
Pkt_t::CompareOrder, Pkt_t::GetNow, util::NullMutex,
using PacketQueue_t = util::CoDelQueue<
Pkt_t,
Pkt_t::GetTime,
Pkt_t::PutTime,
Pkt_t::CompareOrder,
Pkt_t::GetNow,
util::NullMutex,
util::NullLock>;
/// internet to llarp packet queue

@ -7,19 +7,17 @@ namespace llarp
{
namespace handlers
{
struct NullEndpoint final
: public llarp::service::Endpoint,
struct NullEndpoint final : public llarp::service::Endpoint,
public std::enable_shared_from_this<NullEndpoint>
{
NullEndpoint(const std::string &name, AbstractRouter *r,
llarp::service::Context *parent)
NullEndpoint(const std::string& name, AbstractRouter* r, llarp::service::Context* parent)
: llarp::service::Endpoint(name, r, parent)
{
}
virtual bool
HandleInboundPacket(const service::ConvoTag, const llarp_buffer_t &,
service::ProtocolType) override
HandleInboundPacket(
const service::ConvoTag, const llarp_buffer_t&, service::ProtocolType) override
{
return true;
}

@ -49,13 +49,11 @@ namespace llarp
}
}
TunEndpoint::TunEndpoint(const std::string &nickname, AbstractRouter *r,
service::Context *parent, bool lazyVPN)
TunEndpoint::TunEndpoint(
const std::string& nickname, AbstractRouter* r, service::Context* parent, bool lazyVPN)
: service::Endpoint(nickname, r, parent)
, m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop(),
r->netloop())
, m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop(),
r->netloop())
, m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop(), r->netloop())
, m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop(), r->netloop())
, m_Resolver(std::make_shared<dns::Proxy>(
r->netloop(), r->logic(), r->netloop(), r->logic(), this))
{
@ -123,8 +121,7 @@ namespace llarp
}
else
{
LogError(Name(), " config option reachable = '", v,
"' does not make sense");
LogError(Name(), " config option reachable = '", v, "' does not make sense");
return false;
}
}
@ -140,8 +137,7 @@ namespace llarp
LogInfo(Name(), " booyeah network isolation succeeded");
return true;
#else
LogError(Name(),
" network isolation is not supported on your platform");
LogError(Name(), " network isolation is not supported on your platform");
return false;
#endif
}
@ -157,15 +153,14 @@ namespace llarp
RouterContact rc;
if (!m_router->nodedb()->Get(connect, rc))
{
LogError(Name(), " we don't have the RC for ", v,
" so we can't use it in strict-connect");
LogError(
Name(), " we don't have the RC for ", v, " so we can't use it in strict-connect");
return false;
}
for (const auto& ai : rc.addrs)
{
m_StrictConnectAddrs.emplace_back(ai);
LogInfo(Name(), " added ", m_StrictConnectAddrs.back(),
" to strict connect");
LogInfo(Name(), " added ", m_StrictConnectAddrs.back(), " to strict connect");
}
return true;
}
@ -193,8 +188,7 @@ namespace llarp
}
routerStr = str(TrimWhitespace(routerStr));
if (!(exitRouter.FromString(routerStr)
|| HexDecode(routerStr.c_str(), exitRouter.begin(),
exitRouter.size())))
|| HexDecode(routerStr.c_str(), exitRouter.begin(), exitRouter.size())))
{
llarp::LogError(Name(), " bad exit router key: ", routerStr);
return false;
@ -202,10 +196,12 @@ namespace llarp
auto exit = std::make_shared<llarp::exit::ExitSession>(
exitRouter,
util::memFn(&TunEndpoint::QueueInboundPacketForExit, this),
m_router, numPaths, numHops, ShouldBundleRC());
m_router,
numPaths,
numHops,
ShouldBundleRC());
m_ExitMap.Insert(exitRange, exit);
llarp::LogInfo(Name(), " using exit at ", exitRouter, " for ",
exitRange);
llarp::LogInfo(Name(), " using exit at ", exitRouter, " for ", exitRange);
}
if (k == "local-dns")
{
@ -233,15 +229,16 @@ namespace llarp
dnsport = std::atoi(v.substr(pos + 1).c_str());
}
m_UpstreamResolvers.emplace_back(resolverAddr, dnsport);
llarp::LogInfo(Name(), " adding upstream DNS server ", resolverAddr,
":", dnsport);
llarp::LogInfo(Name(), " adding upstream DNS server ", resolverAddr, ":", dnsport);
}
if (k == "mapaddr")
{
auto pos = v.find(":");
if (pos == std::string::npos)
{
llarp::LogError("Cannot map address ", v,
llarp::LogError(
"Cannot map address ",
v,
" invalid format, missing colon (:), expects "
"address.loki:ip.address.goes.here");
return false;
@ -265,8 +262,7 @@ namespace llarp
}
else
{
llarp::LogError(Name(), "failed to map ", ip_str,
" failed to parse IP");
llarp::LogError(Name(), "failed to map ", ip_str, " failed to parse IP");
return false;
}
return MapAddress(addr, ipv6, false);
@ -315,8 +311,7 @@ namespace llarp
tunif->netmask = 32;
addr = v;
}
llarp::LogInfo(Name() + " set ifaddr to ", addr, " with netmask ",
tunif->netmask);
llarp::LogInfo(Name() + " set ifaddr to ", addr, " with netmask ", tunif->netmask);
strncpy(tunif->ifaddr, addr.c_str(), sizeof(tunif->ifaddr) - 1);
return true;
}
@ -341,8 +336,7 @@ namespace llarp
{
static const auto func = [](auto self) {
self->FlushSend();
self->m_ExitMap.ForEachValue(
[](const auto &exit) { exit->FlushUpstream(); });
self->m_ExitMap.ForEachValue([](const auto& exit) { exit->FlushUpstream(); });
self->Pump(self->Now());
};
if (NetworkIsIsolated())
@ -404,20 +398,16 @@ namespace llarp
}
bool
TunEndpoint::HandleHookedDNSMessage(
dns::Message msg, std::function< void(dns::Message) > reply)
TunEndpoint::HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)> reply)
{
auto ReplyToSNodeDNSWhenReady = [self = this, reply = reply](
RouterID snode, auto msg,
bool isV6) -> bool {
return self->EnsurePathToSNode(
snode, [=](const RouterID &, exit::BaseSession_ptr s) {
RouterID snode, auto msg, bool isV6) -> bool {
return self->EnsurePathToSNode(snode, [=](const RouterID&, exit::BaseSession_ptr s) {
self->SendDNSReply(snode, s, msg, reply, true, isV6);
});
};
auto ReplyToLokiDNSWhenReady = [self = this, reply = reply](
service::Address addr, auto msg,
bool isV6) -> bool {
service::Address addr, auto msg, bool isV6) -> bool {
using service::Address;
using service::OutboundContext;
if (self->HasAddress(addr))
@ -447,8 +437,7 @@ namespace llarp
RouterID addr;
if (not addr.FromString(qname))
return false;
auto replyMsg =
std::make_shared< dns::Message >(clear_dns_message(msg));
auto replyMsg = std::make_shared<dns::Message>(clear_dns_message(msg));
return ReplyToSNodeDNSWhenReady(addr, std::move(replyMsg), false);
}
else if (answer.HasCNameForTLD(".loki"))
@ -460,8 +449,7 @@ namespace llarp
service::Address addr;
if (not addr.FromString(qname))
return false;
auto replyMsg =
std::make_shared< dns::Message >(clear_dns_message(msg));
auto replyMsg = std::make_shared<dns::Message>(clear_dns_message(msg));
return ReplyToLokiDNSWhenReady(addr, replyMsg, false);
}
}
@ -499,8 +487,7 @@ namespace llarp
{
size_t counter = 0;
context->ForEachService(
[&](const std::string &,
const std::shared_ptr< service::Endpoint > &service) -> bool {
[&](const std::string&, const std::shared_ptr<service::Endpoint>& service) -> bool {
const service::Address addr = service->GetIdentity().pub.Addr();
msg.AddCNAMEReply(addr.ToString(), 1);
++counter;
@ -513,8 +500,7 @@ namespace llarp
msg.AddNXReply();
reply(msg);
}
else if(msg.questions[0].qtype == dns::qTypeA
|| msg.questions[0].qtype == dns::qTypeAAAA)
else if (msg.questions[0].qtype == dns::qTypeA || msg.questions[0].qtype == dns::qTypeAAAA)
{
const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA;
const bool isV4 = msg.questions[0].qtype == dns::qTypeA;
@ -530,8 +516,7 @@ namespace llarp
if (Router()->GetRandomGoodRouter(random))
{
msg.AddCNAMEReply(random.ToString(), 1);
return ReplyToSNodeDNSWhenReady(
random, std::make_shared< dns::Message >(msg), isV6);
return ReplyToSNodeDNSWhenReady(random, std::make_shared<dns::Message>(msg), isV6);
}
else
msg.AddNXReply();
@ -540,8 +525,7 @@ namespace llarp
{
size_t counter = 0;
context->ForEachService(
[&](const std::string &,
const std::shared_ptr< service::Endpoint > &service) -> bool {
[&](const std::string&, const std::shared_ptr<service::Endpoint>& service) -> bool {
if (!service->HasIfAddr())
return true;
huint128_t ip = service->GetIfAddr();
@ -563,8 +547,7 @@ namespace llarp
}
else
{
return ReplyToLokiDNSWhenReady(
addr, std::make_shared< dns::Message >(msg), isV6);
return ReplyToLokiDNSWhenReady(addr, std::make_shared<dns::Message>(msg), isV6);
}
}
else if (addr.FromString(qname, ".snode"))
@ -624,8 +607,7 @@ namespace llarp
TunEndpoint::ResetInternalState()
{
service::Endpoint::ResetInternalState();
m_ExitMap.ForEachValue(
[](const auto &exit) { exit->ResetInternalState(); });
m_ExitMap.ForEachValue([](const auto& exit) { exit->ResetInternalState(); });
}
bool
@ -667,14 +649,13 @@ namespace llarp
}
bool
TunEndpoint::MapAddress(const service::Address &addr, huint128_t ip,
bool SNode)
TunEndpoint::MapAddress(const service::Address& addr, huint128_t ip, bool SNode)
{
auto itr = m_IPToAddr.find(ip);
if (itr != m_IPToAddr.end())
{
llarp::LogWarn(ip, " already mapped to ",
service::Address(itr->second.as_array()).ToString());
llarp::LogWarn(
ip, " already mapped to ", service::Address(itr->second.as_array()).ToString());
return false;
}
llarp::LogInfo(Name() + " map ", addr.ToString(), " to ", ip);
@ -783,9 +764,8 @@ namespace llarp
{
if (!llarp_ev_add_tun(loop.get(), tunif.get()))
{
llarp::LogError(Name(),
" failed to set up tun interface: ", tunif->ifaddr,
" on ", tunif->ifname);
llarp::LogError(
Name(), " failed to set up tun interface: ", tunif->ifaddr, " on ", tunif->ifname);
return false;
}
}
@ -826,8 +806,7 @@ namespace llarp
m_OurRange.addr = m_OurIP;
m_MaxIP = m_OurRange.HighestAddr();
llarp::LogInfo(Name(), " set ", ifname, " to have address ", m_OurIP);
llarp::LogInfo(Name(), " allocated up to ", m_MaxIP, " on range ",
m_OurRange);
llarp::LogInfo(Name(), " allocated up to ", m_MaxIP, " on range ", m_OurRange);
const service::Address ourAddr = m_Identity.pub.Addr();
@ -919,28 +898,32 @@ namespace llarp
if (pkt.IsV4() && !llarp::IsIPv4Bogon(pkt.dstv4()))
{
pkt.UpdateIPv4Address({0}, xhtonl(pkt.dstv4()));
exit->QueueUpstreamTraffic(std::move(pkt),
llarp::routing::ExitPadSize);
exit->QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize);
}
else if (pkt.IsV6())
{
pkt.UpdateIPv6Address({0}, pkt.dstv6());
exit->QueueUpstreamTraffic(std::move(pkt),
llarp::routing::ExitPadSize);
exit->QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize);
}
}
return;
}
if (m_SNodes.at(itr->second))
{
sendFunc = std::bind(&TunEndpoint::SendToSNodeOrQueue, this,
itr->second.as_array(), std::placeholders::_1);
sendFunc = std::bind(
&TunEndpoint::SendToSNodeOrQueue,
this,
itr->second.as_array(),
std::placeholders::_1);
}
else
{
sendFunc = std::bind(&TunEndpoint::SendToServiceOrQueue, this,
sendFunc = std::bind(
&TunEndpoint::SendToServiceOrQueue,
this,
service::Address(itr->second.as_array()),
std::placeholders::_1, pkt.ServiceProtocol());
std::placeholders::_1,
pkt.ServiceProtocol());
}
// prepare packet for insertion into network
// this includes clearing IP addresses, recalculating checksums, etc
@ -967,8 +950,7 @@ namespace llarp
// llarp::LogInfo("themIP ", themIP);
auto usIP = m_OurIP;
ManagedBuffer buf(b);
return m_NetworkToUserPktQueue.EmplaceIf(
[buf, themIP, usIP](net::IPPacket &pkt) -> bool {
return m_NetworkToUserPktQueue.EmplaceIf([buf, themIP, usIP](net::IPPacket& pkt) -> bool {
// load
if (!pkt.Load(buf))
return false;
@ -981,15 +963,14 @@ namespace llarp
if (pkt.IsV4())
{
auto hdr = pkt.Header();
if(pkt.sz < sizeof(*hdr)
|| (hdr->saddr != 0 && *(byte_t *)&(hdr->saddr) == 0)
if (pkt.sz < sizeof(*hdr) || (hdr->saddr != 0 && *(byte_t*)&(hdr->saddr) == 0)
|| (hdr->daddr != 0 && *(byte_t*)&(hdr->daddr) == 0)
|| ((hdr->saddr == 0) != (hdr->daddr == 0)))
{
return false;
}
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(themIP)),
xhtonl(net::IPPacket::TruncateV6(usIP)));
pkt.UpdateIPv4Address(
xhtonl(net::IPPacket::TruncateV6(themIP)), xhtonl(net::IPPacket::TruncateV6(usIP)));
}
else if (pkt.IsV6())
{
@ -1026,8 +1007,7 @@ namespace llarp
do
{
nextIP = ++m_NextIP;
} while(m_IPToAddr.find(nextIP) != m_IPToAddr.end()
&& m_NextIP < m_MaxIP);
} while (m_IPToAddr.find(nextIP) != m_IPToAddr.end() && m_NextIP < m_MaxIP);
if (nextIP < m_MaxIP)
{
m_AddrToIP[ident] = nextIP;

@ -20,8 +20,11 @@ namespace llarp
public dns::IQueryHandler,
public std::enable_shared_from_this<TunEndpoint>
{
TunEndpoint(const std::string& nickname, AbstractRouter* r,
llarp::service::Context* parent, bool lazyVPN = false);
TunEndpoint(
const std::string& nickname,
AbstractRouter* r,
llarp::service::Context* parent,
bool lazyVPN = false);
~TunEndpoint() override;
path::PathSet_ptr
@ -50,8 +53,7 @@ namespace llarp
bool
HandleHookedDNSMessage(
dns::Message query,
std::function< void(dns::Message) > sendreply) override;
dns::Message query, std::function<void(dns::Message)> sendreply) override;
void
TickTun(llarp_time_t now);
@ -81,9 +83,8 @@ namespace llarp
/// overrides Endpoint
bool
HandleInboundPacket(const service::ConvoTag tag,
const llarp_buffer_t& pkt,
service::ProtocolType t) override
HandleInboundPacket(
const service::ConvoTag tag, const llarp_buffer_t& pkt, service::ProtocolType t) override
{
if (t != service::eProtocolTrafficV4 && t != service::eProtocolTrafficV6)
return false;
@ -97,8 +98,7 @@ namespace llarp
/// handle inbound traffic
bool
HandleWriteIPPacket(const llarp_buffer_t& buf,
std::function< huint128_t(void) > getFromIP);
HandleWriteIPPacket(const llarp_buffer_t& buf, std::function<huint128_t(void)> getFromIP);
/// queue outbound packet to the world
bool
@ -189,8 +189,11 @@ namespace llarp
llarp_time_t m_LastFlushAt = 0s;
using PacketQueue_t = llarp::util::CoDelQueue<
net::IPPacket, net::IPPacket::GetTime, net::IPPacket::PutTime,
net::IPPacket::CompareOrder, net::IPPacket::GetNow >;
net::IPPacket,
net::IPPacket::GetTime,
net::IPPacket::PutTime,
net::IPPacket::CompareOrder,
net::IPPacket::GetNow>;
/// queue packet for send on net thread from user
std::vector<net::IPPacket> m_TunPkts;
@ -217,14 +220,11 @@ namespace llarp
/// maps ip to key (host byte order)
std::unordered_map<huint128_t, AlignedBuffer<32>> m_IPToAddr;
/// maps key to ip (host byte order)
std::unordered_map< AlignedBuffer< 32 >, huint128_t,
AlignedBuffer< 32 >::Hash >
m_AddrToIP;
std::unordered_map<AlignedBuffer<32>, huint128_t, AlignedBuffer<32>::Hash> m_AddrToIP;
/// maps key to true if key is a service node, maps key to false if key is
/// a hidden service
std::unordered_map< AlignedBuffer< 32 >, bool, AlignedBuffer< 32 >::Hash >
m_SNodes;
std::unordered_map<AlignedBuffer<32>, bool, AlignedBuffer<32>::Hash> m_SNodes;
private:
llarp_vpn_io_impl*
@ -239,16 +239,14 @@ namespace llarp
QueueInboundPacketForExit(const llarp_buffer_t& buf)
{
ManagedBuffer copy{buf};
return m_NetworkToUserPktQueue.EmplaceIf(
[&](llarp::net::IPPacket& pkt) -> bool {
return m_NetworkToUserPktQueue.EmplaceIf([&](llarp::net::IPPacket& pkt) -> bool {
if (!pkt.Load(copy.underlying))
return false;
if (SupportsV6())
{
if (pkt.IsV4())
{
pkt.UpdateIPv6Address(net::IPPacket::ExpandV4(pkt.srcv4()),
m_OurIP);
pkt.UpdateIPv6Address(net::IPPacket::ExpandV4(pkt.srcv4()), m_OurIP);
}
else
{
@ -259,8 +257,7 @@ namespace llarp
{
if (pkt.IsV4())
pkt.UpdateIPv4Address(
xhtonl(pkt.srcv4()),
xhtonl(net::IPPacket::TruncateV6(m_OurIP)));
xhtonl(pkt.srcv4()), xhtonl(net::IPPacket::TruncateV6(m_OurIP)));
else
return false;
}
@ -270,9 +267,12 @@ namespace llarp
template <typename Addr_t, typename Endpoint_t>
void
SendDNSReply(Addr_t addr, Endpoint_t ctx,
SendDNSReply(
Addr_t addr,
Endpoint_t ctx,
std::shared_ptr<dns::Message> query,
std::function< void(dns::Message) > reply, bool snode,
std::function<void(dns::Message)> reply,
bool snode,
bool sendIPv6)
{
if (ctx)

@ -19,8 +19,7 @@ namespace llarp
namespace hooks
{
#if defined(ENABLE_SHELLHOOKS)
struct ExecShellHookBackend
: public IBackend,
struct ExecShellHookBackend : public IBackend,
public std::enable_shared_from_this<ExecShellHookBackend>
{
thread::ThreadPool m_ThreadPool;
@ -28,8 +27,7 @@ namespace llarp
std::vector<std::string> _args;
std::vector<char*> args;
ExecShellHookBackend(std::string script)
: m_ThreadPool(1, 1000, "exechook")
ExecShellHookBackend(std::string script) : m_ThreadPool(1, 1000, "exechook")
{
do
{
@ -79,8 +77,7 @@ namespace llarp
}
void
NotifyAsync(
std::unordered_map< std::string, std::string > params) override;
NotifyAsync(std::unordered_map<std::string, std::string> params) override;
};
struct ExecShellHookJob
@ -89,7 +86,8 @@ namespace llarp
std::vector<char*> _m_env;
std::shared_ptr<ExecShellHookBackend> m_Parent;
ExecShellHookJob(std::shared_ptr< ExecShellHookBackend > b,
ExecShellHookJob(
std::shared_ptr<ExecShellHookBackend> b,
const std::unordered_map<std::string, std::string> env)
: m_Parent(b)
{
@ -134,11 +132,9 @@ namespace llarp
};
void
ExecShellHookBackend::NotifyAsync(
std::unordered_map< std::string, std::string > params)
ExecShellHookBackend::NotifyAsync(std::unordered_map<std::string, std::string> params)
{
auto job = std::make_shared< ExecShellHookJob >(shared_from_this(),
std::move(params));
auto job = std::make_shared<ExecShellHookJob>(shared_from_this(), std::move(params));
m_ThreadPool.addJob(std::bind(&ExecShellHookJob::Exec, job));
}

@ -8,27 +8,35 @@ namespace llarp
namespace iwp
{
LinkLayer_ptr
NewInboundLink(std::shared_ptr< KeyManager > keyManager, GetRCFunc getrc,
LinkMessageHandler h, SignBufferFunc sign,
NewInboundLink(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
LinkMessageHandler h,
SignBufferFunc sign,
SessionEstablishedHandler est,
SessionRenegotiateHandler reneg, TimeoutHandler timeout,
SessionClosedHandler closed, PumpDoneHandler pumpDone)
SessionRenegotiateHandler reneg,
TimeoutHandler timeout,
SessionClosedHandler closed,
PumpDoneHandler pumpDone)
{
return std::make_shared< LinkLayer >(keyManager, getrc, h, sign, est,
reneg, timeout, closed, pumpDone,
true);
return std::make_shared<LinkLayer>(
keyManager, getrc, h, sign, est, reneg, timeout, closed, pumpDone, true);
}
LinkLayer_ptr
NewOutboundLink(std::shared_ptr< KeyManager > keyManager, GetRCFunc getrc,
LinkMessageHandler h, SignBufferFunc sign,
NewOutboundLink(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
LinkMessageHandler h,
SignBufferFunc sign,
SessionEstablishedHandler est,
SessionRenegotiateHandler reneg, TimeoutHandler timeout,
SessionClosedHandler closed, PumpDoneHandler pumpDone)
SessionRenegotiateHandler reneg,
TimeoutHandler timeout,
SessionClosedHandler closed,
PumpDoneHandler pumpDone)
{
return std::make_shared< LinkLayer >(keyManager, getrc, h, sign, est,
reneg, timeout, closed, pumpDone,
false);
return std::make_shared<LinkLayer>(
keyManager, getrc, h, sign, est, reneg, timeout, closed, pumpDone, false);
}
} // namespace iwp
} // namespace llarp

@ -11,17 +11,27 @@ namespace llarp
namespace iwp
{
LinkLayer_ptr
NewInboundLink(std::shared_ptr< KeyManager > keyManager, GetRCFunc getrc,
LinkMessageHandler h, SignBufferFunc sign,
NewInboundLink(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
LinkMessageHandler h,
SignBufferFunc sign,
SessionEstablishedHandler est,
SessionRenegotiateHandler reneg, TimeoutHandler timeout,
SessionClosedHandler closed, PumpDoneHandler pumpDone);
SessionRenegotiateHandler reneg,
TimeoutHandler timeout,
SessionClosedHandler closed,
PumpDoneHandler pumpDone);
LinkLayer_ptr
NewOutboundLink(std::shared_ptr< KeyManager > keyManager, GetRCFunc getrc,
LinkMessageHandler h, SignBufferFunc sign,
NewOutboundLink(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
LinkMessageHandler h,
SignBufferFunc sign,
SessionEstablishedHandler est,
SessionRenegotiateHandler reneg, TimeoutHandler timeout,
SessionClosedHandler closed, PumpDoneHandler pumpDone);
SessionRenegotiateHandler reneg,
TimeoutHandler timeout,
SessionClosedHandler closed,
PumpDoneHandler pumpDone);
} // namespace iwp
} // namespace llarp

@ -8,14 +8,18 @@ namespace llarp
{
namespace iwp
{
LinkLayer::LinkLayer(std::shared_ptr< KeyManager > keyManager,
GetRCFunc getrc, LinkMessageHandler h,
SignBufferFunc sign, SessionEstablishedHandler est,
LinkLayer::LinkLayer(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
LinkMessageHandler h,
SignBufferFunc sign,
SessionEstablishedHandler est,
SessionRenegotiateHandler reneg,
TimeoutHandler timeout, SessionClosedHandler closed,
PumpDoneHandler pumpDone, bool allowInbound)
: ILinkLayer(keyManager, getrc, h, sign, est, reneg, timeout, closed,
pumpDone)
TimeoutHandler timeout,
SessionClosedHandler closed,
PumpDoneHandler pumpDone,
bool allowInbound)
: ILinkLayer(keyManager, getrc, h, sign, est, reneg, timeout, closed, pumpDone)
, permitInbound{allowInbound}
{
}
@ -69,8 +73,7 @@ namespace llarp
bool success = session->Recv_LL(std::move(pkt));
if (!success and isNewSession)
{
LogWarn(
"Brand new session failed; removing from pending sessions list");
LogWarn("Brand new session failed; removing from pending sessions list");
m_Pending.erase(m_Pending.find(from));
}
}
@ -92,8 +95,7 @@ namespace llarp
}
std::shared_ptr<ILinkSession>
LinkLayer::NewOutboundSession(const RouterContact& rc,
const AddressInfo& ai)
LinkLayer::NewOutboundSession(const RouterContact& rc, const AddressInfo& ai)
{
return std::make_shared<Session>(this, rc, ai);
}

@ -17,17 +17,22 @@ namespace llarp
{
struct LinkLayer final : public ILinkLayer
{
LinkLayer(std::shared_ptr< KeyManager > keyManager, GetRCFunc getrc,
LinkMessageHandler h, SignBufferFunc sign,
SessionEstablishedHandler est, SessionRenegotiateHandler reneg,
TimeoutHandler timeout, SessionClosedHandler closed,
PumpDoneHandler pumpDone, bool permitInbound);
LinkLayer(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
LinkMessageHandler h,
SignBufferFunc sign,
SessionEstablishedHandler est,
SessionRenegotiateHandler reneg,
TimeoutHandler timeout,
SessionClosedHandler closed,
PumpDoneHandler pumpDone,
bool permitInbound);
~LinkLayer() override;
std::shared_ptr<ILinkSession>
NewOutboundSession(const RouterContact &rc,
const AddressInfo &ai) override;
NewOutboundSession(const RouterContact& rc, const AddressInfo& ai) override;
const char*
Name() const override;

@ -6,7 +6,8 @@ namespace llarp
{
namespace iwp
{
OutboundMessage::OutboundMessage(uint64_t msgid,
OutboundMessage::OutboundMessage(
uint64_t msgid,
ILinkSession::Message_t msg,
llarp_time_t now,
ILinkSession::CompletionHandler handler)
@ -27,10 +28,9 @@ namespace llarp
auto xmit = CreatePacket(Command::eXMIT, 10 + 32 + extra, 0, 0);
htobe16buf(xmit.data() + CommandOverhead + PacketOverhead, m_Data.size());
htobe64buf(xmit.data() + 2 + CommandOverhead + PacketOverhead, m_MsgID);
std::copy_n(m_Digest.begin(), m_Digest.size(),
xmit.data() + 10 + CommandOverhead + PacketOverhead);
std::copy_n(m_Data.data(), extra,
xmit.data() + 10 + CommandOverhead + PacketOverhead + 32);
std::copy_n(
m_Digest.begin(), m_Digest.size(), xmit.data() + 10 + CommandOverhead + PacketOverhead);
std::copy_n(m_Data.data(), extra, xmit.data() + 10 + CommandOverhead + PacketOverhead + 32);
return xmit;
}
@ -68,12 +68,13 @@ namespace llarp
{
if (not m_Acks[idx / FragmentSize])
{
const size_t fragsz =
idx + FragmentSize < datasz ? FragmentSize : datasz - idx;
const size_t fragsz = idx + FragmentSize < datasz ? FragmentSize : datasz - idx;
auto frag = CreatePacket(Command::eDATA, fragsz + Overhead, 0, 0);
htobe16buf(frag.data() + 2 + PacketOverhead, idx);
htobe64buf(frag.data() + 4 + PacketOverhead, m_MsgID);
std::copy(m_Data.begin() + idx, m_Data.begin() + idx + fragsz,
std::copy(
m_Data.begin() + idx,
m_Data.begin() + idx + fragsz,
frag.data() + PacketOverhead + Overhead + 2);
sendpkt(std::move(frag));
}
@ -111,18 +112,13 @@ namespace llarp
m_Completed = nullptr;
}
InboundMessage::InboundMessage(uint64_t msgid, uint16_t sz, ShortHash h,
llarp_time_t now)
: m_Data(size_t{sz})
, m_Digset{std::move(h)}
, m_MsgID(msgid)
, m_LastActiveAt{now}
InboundMessage::InboundMessage(uint64_t msgid, uint16_t sz, ShortHash h, llarp_time_t now)
: m_Data(size_t{sz}), m_Digset{std::move(h)}, m_MsgID(msgid), m_LastActiveAt{now}
{
}
void
InboundMessage::HandleData(uint16_t idx, const llarp_buffer_t &buf,
llarp_time_t now)
InboundMessage::HandleData(uint16_t idx, const llarp_buffer_t& buf, llarp_time_t now)
{
if (idx + buf.sz > m_Data.size())
{
@ -176,8 +172,7 @@ namespace llarp
}
void
InboundMessage::SendACKS(
std::function< void(ILinkSession::Packet_t) > sendpkt, llarp_time_t now)
InboundMessage::SendACKS(std::function<void(ILinkSession::Packet_t)> sendpkt, llarp_time_t now)
{
sendpkt(ACKS());
m_LastACKSent = now;

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save