delay dns resolution for snode until we have a session with it

pull/367/head
Jeff Becker 5 years ago
parent f034bfc184
commit 159415c363
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -560,7 +560,7 @@ namespace llarp
{ {
sockaddr_storage st; sockaddr_storage st;
socklen_t sl; socklen_t sl;
if (getpeername(fd, (sockaddr*)&st, &sl) == 0) if(getpeername(fd, (sockaddr*)&st, &sl) == 0)
{ {
// we are connected yeh boi // we are connected yeh boi
if(_conn) if(_conn)

@ -322,7 +322,7 @@ llarp_epoll_loop::tick(int ms)
{ {
epoll_event events[1024]; epoll_event events[1024];
int result; int result;
result = epoll_wait(epollfd, events, 1024, ms); result = epoll_wait(epollfd, events, 1024, ms);
bool didIO = false; bool didIO = false;
if(result > 0) if(result > 0)
{ {
@ -339,7 +339,7 @@ llarp_epoll_loop::tick(int ms)
llarp::LogDebug("epoll error"); llarp::LogDebug("epoll error");
ev->error(); ev->error();
} }
else else
{ {
// write THEN READ don't revert me // write THEN READ don't revert me
if(events[idx].events & EPOLLOUT) if(events[idx].events & EPOLLOUT)

@ -101,19 +101,34 @@ namespace llarp
llarp::LogError("failed to send exit request"); llarp::LogError("failed to send exit request");
} }
void
BaseSession::AddReadyHook(SessionReadyFunc func)
{
m_PendingCallbacks.emplace_back(func);
}
bool bool
BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b) BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b)
{ {
m_LastUse = router->Now(); BaseSession* self = this;
m_LastUse = router->Now();
if(b == 0) if(b == 0)
llarp::LogInfo("obtained an exit via ", p->Endpoint()); llarp::LogInfo("obtained an exit via ", p->Endpoint());
else
self = nullptr;
for(auto& f : m_PendingCallbacks)
f(self);
m_PendingCallbacks.clear();
return true; return true;
} }
bool bool
BaseSession::Stop() BaseSession::Stop()
{ {
for(auto& f : m_PendingCallbacks)
f(nullptr);
m_PendingCallbacks.clear();
auto sendExitClose = [&](llarp::path::Path* p) { auto sendExitClose = [&](llarp::path::Path* p) {
if(p->SupportsAnyRoles(llarp::path::ePathRoleExit)) if(p->SupportsAnyRoles(llarp::path::ePathRoleExit))
{ {

@ -13,6 +13,10 @@ namespace llarp
{ {
namespace exit namespace exit
{ {
struct BaseSession;
using SessionReadyFunc = std::function< void(BaseSession*) >;
/// a persisting exit session with an exit router /// a persisting exit session with an exit router
struct BaseSession : public llarp::path::Builder struct BaseSession : public llarp::path::Builder
{ {
@ -64,6 +68,9 @@ namespace llarp
bool bool
LoadIdentityFromFile(const char* fname); LoadIdentityFromFile(const char* fname);
void
AddReadyHook(SessionReadyFunc func);
protected: protected:
llarp::RouterID m_ExitRouter; llarp::RouterID m_ExitRouter;
llarp::SecretKey m_ExitIdentity; llarp::SecretKey m_ExitIdentity;
@ -107,6 +114,8 @@ namespace llarp
uint64_t m_Counter; uint64_t m_Counter;
llarp_time_t m_LastUse; llarp_time_t m_LastUse;
std::vector< SessionReadyFunc > m_PendingCallbacks;
}; };
struct ExitSession final : public BaseSession struct ExitSession final : public BaseSession

@ -333,24 +333,28 @@ namespace llarp
else else
{ {
dns::Message *replyMsg = new dns::Message(std::move(msg)); dns::Message *replyMsg = new dns::Message(std::move(msg));
service::Endpoint::PathEnsureHook hook = std::bind( return EnsurePathToService(
&TunEndpoint::SendDNSReply, this, std::placeholders::_1, addr,
std::placeholders::_2, replyMsg, reply); [=](const service::Address &remote, OutboundContext *ctx) {
return EnsurePathToService(addr, hook, 2000); SendDNSReply(remote, ctx, replyMsg, reply, false);
},
2000);
} }
} }
else if(addr.FromString(qname, ".snode")) else if(addr.FromString(qname, ".snode"))
{ {
// TODO: add hook to EnsurePathToSNode if(HasPathToSNode(addr.as_array()))
EnsurePathToSNode(addr.as_array());
huint32_t ip = ObtainIPForAddr(addr, true);
if(ip.h)
msg.AddINReply(ip);
else
{ {
llarp::LogWarn("no ip found for ", addr); msg.AddINReply(ObtainIPForAddr(addr, true));
msg.AddNXReply(); reply(msg);
return true;
} }
dns::Message *replyMsg = new dns::Message(std::move(msg));
EnsurePathToSNode(addr.as_array(),
[=](const RouterID &remote, exit::BaseSession *s) {
SendDNSReply(remote, s, replyMsg, reply, true);
});
return true;
} }
else else
// forward dns // forward dns
@ -430,24 +434,6 @@ namespace llarp
return false; return false;
} }
void
TunEndpoint::SendDNSReply(service::Address addr,
service::Endpoint::OutboundContext *ctx,
dns::Message *request,
std::function< void(dns::Message) > reply)
{
if(ctx)
{
huint32_t ip = ObtainIPForAddr(addr, false);
request->AddINReply(ip);
}
else
request->AddNXReply();
reply(*request);
delete request;
}
bool bool
TunEndpoint::MapAddress(const service::Address &addr, huint32_t ip, TunEndpoint::MapAddress(const service::Address &addr, huint32_t ip,
bool SNode) bool SNode)

@ -192,10 +192,21 @@ namespace llarp
}); });
} }
template < typename Addr_t, typename Endpoint_t >
void void
SendDNSReply(service::Address addr, SendDNSReply(Addr_t addr, Endpoint_t* ctx, dns::Message* query,
service::Endpoint::OutboundContext* ctx, dns::Message* query, std::function< void(dns::Message) > reply, bool snode)
std::function< void(dns::Message) > reply); {
if(ctx)
{
huint32_t ip = ObtainIPForAddr(addr, snode);
query->AddINReply(ip);
}
else
query->AddNXReply();
reply(*query);
delete query;
}
#ifndef WIN32 #ifndef WIN32
/// handles fd injection force android /// handles fd injection force android

@ -193,7 +193,7 @@ namespace llarp
} }
bool bool
Router::OnSessionEstablished(ILinkSession * s) Router::OnSessionEstablished(ILinkSession *s)
{ {
return async_verify_RC(s->GetRemoteRC()); return async_verify_RC(s->GetRemoteRC());
} }
@ -1228,7 +1228,6 @@ namespace llarp
bool bool
Router::async_verify_RC(const RouterContact &rc) Router::async_verify_RC(const RouterContact &rc)
{ {
if(rc.IsPublicRouter() && whitelistRouters && IsServiceNode()) if(rc.IsPublicRouter() && whitelistRouters && IsServiceNode())
{ {
if(lokinetRouters.find(rc.pubkey) == lokinetRouters.end()) if(lokinetRouters.find(rc.pubkey) == lokinetRouters.end())

@ -130,9 +130,9 @@ namespace llarp
&& nickname == other.nickname && last_updated == other.last_updated && nickname == other.nickname && last_updated == other.last_updated
&& netID == other.netID; && netID == other.netID;
} }
bool bool
operator!=(const RouterContact & other) const operator!=(const RouterContact &other) const
{ {
return !(*this == other); return !(*this == other);
} }

@ -1205,7 +1205,7 @@ namespace llarp
} }
void void
Endpoint::EnsurePathToSNode(const RouterID& snode) Endpoint::EnsurePathToSNode(const RouterID& snode, SNodeEnsureHook h)
{ {
if(m_SNodeSessions.count(snode) == 0) if(m_SNodeSessions.count(snode) == 0)
{ {
@ -1219,6 +1219,13 @@ namespace llarp
[themIP]() -> huint32_t { return themIP; }), [themIP]() -> huint32_t { return themIP; }),
m_Router, 2, numHops)); m_Router, 2, numHops));
} }
auto range = m_SNodeSessions.equal_range(snode);
auto itr = range.first;
while(itr != range.second)
{
itr->second->AddReadyHook(std::bind(h, snode, std::placeholders::_1));
++itr;
}
} }
bool bool

@ -366,7 +366,7 @@ namespace llarp
/// ensure a path to a service node by public key /// ensure a path to a service node by public key
void void
EnsurePathToSNode(const RouterID& remote); EnsurePathToSNode(const RouterID& remote, SNodeEnsureHook h);
bool bool
HasPathToSNode(const RouterID& remote) const; HasPathToSNode(const RouterID& remote) const;

Loading…
Cancel
Save