Skip to content

Commit

Permalink
Merge pull request #1181 from loki-project/dev
Browse files Browse the repository at this point in the history
rc3
  • Loading branch information
majestrate committed Mar 12, 2020
2 parents 0a1b016 + ec2f691 commit 50514d5
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 191 deletions.
1 change: 1 addition & 0 deletions libabyss/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ namespace abyss
llarp::LogDebug("connection closed");
ConnImpl* self = static_cast< ConnImpl* >(conn->user);
self->_conn = nullptr;
self->m_State = eCloseMe;
}

static void
Expand Down
89 changes: 41 additions & 48 deletions llarp/config/key_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,68 +240,61 @@ namespace llarp
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curl_RecvIdentKey);
do

resp.clear();
LogInfo("Getting Identity Keys from lokid...");
if(curl_easy_perform(curl) == CURLE_OK)
{
resp.clear();
LogInfo("Getting Identity Keys from lokid...");
if(curl_easy_perform(curl) == CURLE_OK)
try
{
try
auto j = nlohmann::json::parse(resp);
if(not j.is_object())
return false;

const auto itr = j.find("result");
if(itr == j.end())
return false;
if(not itr->is_object())
return false;
const auto k =
(*itr)["service_node_ed25519_privkey"].get< std::string >();
if(k.size() != (identityKey.size() * 2))
{
auto j = nlohmann::json::parse(resp);
if(not j.is_object())
continue;

const auto itr = j.find("result");
if(itr == j.end())
continue;
if(not itr->is_object())
continue;
const auto k =
(*itr)["service_node_ed25519_privkey"].get< std::string >();
if(k.size() != (identityKey.size() * 2))
{
if(k.empty())
{
LogError("lokid gave no identity key");
}
else
{
LogError("lokid gave invalid identity key");
}
return false;
}
if(not HexDecode(k.c_str(), identityKey.data(), identityKey.size()))
continue;
if(CryptoManager::instance()->check_identity_privkey(identityKey))
if(k.empty())
{
ret = true;
LogError("lokid gave no identity key");
}
else
{
LogError("lokid gave bogus identity key");
LogError("lokid gave invalid identity key");
}
return false;
}
catch(nlohmann::json::exception& ex)
if(not HexDecode(k.c_str(), identityKey.data(), identityKey.size()))
return false;
if(CryptoManager::instance()->check_identity_privkey(identityKey))
{
LogError("Bad response from lokid: ", ex.what());
ret = true;
}
else
{
LogError("lokid gave bogus identity key");
}
}
else
{
LogError("failed to get identity keys");
}
if(ret)
{
LogInfo("Got Identity Keys from lokid: ",
RouterID(seckey_topublic(identityKey)));
break;
}
else
catch(nlohmann::json::exception& ex)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
LogError("Bad response from lokid: ", ex.what());
}
} while(true);
}
else
{
LogError("failed to get identity keys");
}
if(ret)
{
LogInfo("Got Identity Keys from lokid: ",
RouterID(seckey_topublic(identityKey)));
}
curl_easy_cleanup(curl);
curl_slist_free_all(list);
return ret;
Expand Down
76 changes: 42 additions & 34 deletions llarp/dns/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ namespace llarp
hdr.fields = hdr_fields;
hdr.qd_count = questions.size();
hdr.an_count = answers.size();
hdr.ns_count = authorities.size();
hdr.ar_count = additional.size();
hdr.ns_count = 0;
hdr.ar_count = 0;

if(!hdr.Encode(buf))
return false;
Expand All @@ -98,14 +98,6 @@ namespace llarp
if(!answer.Encode(buf))
return false;

for(const auto& auth : authorities)
if(!auth.Encode(buf))
return false;

for(const auto& rr : additional)
if(!rr.Encode(buf))
return false;

return true;
}

Expand All @@ -125,28 +117,10 @@ namespace llarp
{
if(not an.Decode(buf))
{
llarp::LogError("failed to decode answer");
return false;
}
}
/*
for(auto& auth : authorities)
{
if(!auth.Decode(buf))
{
llarp::LogError("failed to decode auth");
llarp::LogDebug("failed to decode answer");
return false;
}
}
for(auto& rr : additional)
{
if(!rr.Decode(buf))
{
llarp::LogError("failed to decode additional");
return false;
}
}
*/
return true;
}

Expand All @@ -162,12 +136,18 @@ namespace llarp
}
}

static constexpr uint16_t
reply_flags(uint16_t setbits)
{
return setbits | flags_QR | flags_AA | flags_RA;
}

void
Message::AddINReply(llarp::huint128_t ip, bool isV6, RR_TTL_t ttl)
{
if(questions.size())
{
hdr_fields |= flags_QR | flags_AA | flags_RA;
hdr_fields = reply_flags(hdr_fields);
ResourceRecord rec;
rec.rr_name = questions[0].qname;
rec.rr_class = qClassIN;
Expand All @@ -193,7 +173,8 @@ namespace llarp
{
if(questions.size())
{
hdr_fields |= flags_QR | flags_AA | flags_RA;
hdr_fields = reply_flags(hdr_fields);

const auto& question = questions[0];
answers.emplace_back();
auto& rec = answers.back();
Expand All @@ -212,12 +193,38 @@ namespace llarp
}
}

void
Message::AddNSReply(std::string name, RR_TTL_t ttl)
{
if(not questions.empty())
{
hdr_fields = reply_flags(hdr_fields);

const auto& question = questions[0];
answers.emplace_back();
auto& rec = answers.back();
rec.rr_name = question.qname;
rec.rr_type = qTypeNS;
rec.rr_class = qClassIN;
rec.ttl = ttl;
std::array< byte_t, 512 > tmp = {{0}};
llarp_buffer_t buf(tmp);
if(EncodeName(&buf, name))
{
buf.sz = buf.cur - buf.base;
rec.rData.resize(buf.sz);
memcpy(rec.rData.data(), buf.base, buf.sz);
}
}
}

void
Message::AddCNAMEReply(std::string name, RR_TTL_t ttl)
{
if(questions.size())
{
hdr_fields |= flags_QR | flags_AA | flags_RA;
hdr_fields = reply_flags(hdr_fields);

const auto& question = questions[0];
answers.emplace_back();
auto& rec = answers.back();
Expand All @@ -241,7 +248,8 @@ namespace llarp
{
if(questions.size())
{
hdr_fields |= flags_QR | flags_AA;
hdr_fields = reply_flags(hdr_fields);

const auto& question = questions[0];
answers.emplace_back();
auto& rec = answers.back();
Expand All @@ -266,7 +274,7 @@ namespace llarp
if(questions.size())
{
// authorative response with recursion available
hdr_fields |= flags_QR | flags_AA | flags_RA;
hdr_fields = reply_flags(hdr_fields);
// don't allow recursion on this request
hdr_fields &= ~flags_RD;
hdr_fields |= flags_RCODENameError;
Expand Down
3 changes: 3 additions & 0 deletions llarp/dns/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ namespace llarp
void
AddAReply(std::string name, RR_TTL_t ttl = 1);

void
AddNSReply(std::string name, RR_TTL_t ttl = 1);

bool
Encode(llarp_buffer_t* buf) const override;

Expand Down
8 changes: 4 additions & 4 deletions llarp/dns/rr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ namespace llarp
return false;
if(!buf->read_uint16(rr_type))
{
llarp::LogError("failed to decode rr type");
llarp::LogDebug("failed to decode rr type");
return false;
}
if(!buf->read_uint16(rr_class))
{
llarp::LogError("failed to decode rr class");
llarp::LogDebug("failed to decode rr class");
return false;
}
if(!buf->read_uint32(ttl))
{
llarp::LogError("failed to decode ttl");
llarp::LogDebug("failed to decode ttl");
return false;
}
if(!DecodeRData(buf, rData))
{
llarp::LogError("failed to decode rr rdata ", *this);
llarp::LogDebug("failed to decode rr rdata ", *this);
return false;
}
return true;
Expand Down
6 changes: 5 additions & 1 deletion llarp/exit/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ namespace llarp
bool
BaseSession::ShouldBuildMore(llarp_time_t now) const
{
if(BuildCooldownHit(now))
return false;
const size_t expect = (1 + (numPaths / 2));
// check 30 seconds into the future and see if we need more paths
const llarp_time_t future = now + 30s + buildIntervalLimit;
return NumPathsExistingAt(future) < expect && !BuildCooldownHit(now);
return NumPathsExistingAt(future) < expect;
}

void
Expand Down Expand Up @@ -259,6 +261,8 @@ namespace llarp
bool
BaseSession::UrgentBuild(llarp_time_t now) const
{
if(BuildCooldownHit(now))
return false;
if(!IsReady())
return NumInStatus(path::ePathBuilding) < numPaths;
return path::Builder::UrgentBuild(now);
Expand Down
13 changes: 8 additions & 5 deletions llarp/handlers/tun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ namespace llarp
}
else
{
auto replyMsg = std::make_shared< dns::Message >(msg);
auto replyMsg = std::make_shared< dns::Message >(std::move(msg));
using service::Address;
using service::OutboundContext;
return EnsurePathToService(
Expand Down Expand Up @@ -509,12 +509,15 @@ namespace llarp
else if(msg.questions[0].qtype == dns::qTypeA
|| msg.questions[0].qtype == dns::qTypeAAAA)
{
const bool isV6 =
msg.questions[0].qtype == dns::qTypeAAAA && SupportsV6();
const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA;
const bool isV4 = msg.questions[0].qtype == dns::qTypeA;
llarp::service::Address addr;
if(isV6 && !SupportsV6())
{ // empty reply but not a NXDOMAIN so that client can retry IPv4
msg.AddNSReply("localhost.loki.");
}
// on MacOS this is a typeA query
if(is_random_snode(msg))
else if(is_random_snode(msg))
{
RouterID random;
if(Router()->GetRandomGoodRouter(random))
Expand Down Expand Up @@ -554,7 +557,7 @@ namespace llarp
}
else
{
auto replyMsg = std::make_shared< dns::Message >(std::move(msg));
auto replyMsg = std::make_shared< dns::Message >(msg);
using service::Address;
using service::OutboundContext;
return EnsurePathToService(
Expand Down
1 change: 1 addition & 0 deletions llarp/handlers/tun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ namespace llarp
if(ctx)
{
huint128_t ip = ObtainIPForAddr(addr, snode);
query->answers.clear();
query->AddINReply(ip, sendIPv6);
}
else
Expand Down
10 changes: 4 additions & 6 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ namespace llarp
return;

util::Lock l(_mutex);

m_PersistingSessions[remote] =
std::max(until, m_PersistingSessions[remote]);
LogDebug("persist session to ", remote, " until ",
m_PersistingSessions[remote].count());
auto &curr = m_PersistingSessions[remote];
if(until > curr)
curr = until;
LogDebug("persist session to ", remote, " until ", curr - time_now_ms());
}

void
Expand Down Expand Up @@ -307,7 +306,6 @@ namespace llarp
auto link = GetLinkWithSessionTo(itr->first);
if(link)
{
LogDebug("keepalive to ", itr->first);
link->KeepAliveSessionTo(itr->first);
}
else
Expand Down
Loading

0 comments on commit 50514d5

Please sign in to comment.