make format

pull/1307/head
Thomas Winget 4 years ago
parent f58c7df54d
commit 221e9ff5de

@ -98,14 +98,13 @@ namespace llarp
bool bool
Proxy::SetupUnboundResolver(const std::vector<IpAddress>& resolvers) Proxy::SetupUnboundResolver(const std::vector<IpAddress>& resolvers)
{ {
auto replyFunc = [self=weak_from_this()](const SockAddr& to, Message msg) auto replyFunc = [self = weak_from_this()](const SockAddr& to, Message msg) {
{ auto this_ptr = self.lock();
auto this_ptr = self.lock(); if (this_ptr)
if (this_ptr) {
{ this_ptr->SendServerMessageTo(to, msg);
this_ptr->SendServerMessageTo(to, msg); }
} };
};
m_UnboundResolver = std::make_shared<UnboundResolver>(m_ServerLoop, std::move(replyFunc)); m_UnboundResolver = std::make_shared<UnboundResolver>(m_ServerLoop, std::move(replyFunc));
if (not m_UnboundResolver->Init()) if (not m_UnboundResolver->Init())

@ -12,7 +12,8 @@ namespace llarp::dns
SockAddr source; SockAddr source;
}; };
void UnboundResolver::Reset() void
UnboundResolver::Reset()
{ {
started = false; started = false;
if (unboundContext) if (unboundContext)
@ -23,30 +24,33 @@ namespace llarp::dns
unboundContext = nullptr; unboundContext = nullptr;
} }
void UnboundResolver::DeregisterPollFD() void
UnboundResolver::DeregisterPollFD()
{ {
eventLoop->deregister_poll_fd_readable(ub_fd(unboundContext)); eventLoop->deregister_poll_fd_readable(ub_fd(unboundContext));
} }
void UnboundResolver::RegisterPollFD() void
UnboundResolver::RegisterPollFD()
{ {
eventLoop->register_poll_fd_readable(ub_fd(unboundContext), [=](){ ub_process(unboundContext); }); eventLoop->register_poll_fd_readable(
ub_fd(unboundContext), [=]() { ub_process(unboundContext); });
} }
UnboundResolver::UnboundResolver( UnboundResolver::UnboundResolver(llarp_ev_loop_ptr eventLoop, ReplyFunction replyFunc)
llarp_ev_loop_ptr eventLoop, : unboundContext(nullptr), started(false), eventLoop(eventLoop), replyFunc(replyFunc)
ReplyFunction replyFunc)
: unboundContext(nullptr), started(false), eventLoop(eventLoop), replyFunc(replyFunc)
{ {
} }
// static callback // static callback
void UnboundResolver::Callback(void* data, int err, ub_result* result) void
UnboundResolver::Callback(void* data, int err, ub_result* result)
{ {
std::unique_ptr<PendingUnboundLookup> lookup{static_cast<PendingUnboundLookup*>(data)}; std::unique_ptr<PendingUnboundLookup> lookup{static_cast<PendingUnboundLookup*>(data)};
auto this_ptr = lookup->resolver.lock(); auto this_ptr = lookup->resolver.lock();
if (not this_ptr) return; // resolver is gone, so we don't reply. if (not this_ptr)
return; // resolver is gone, so we don't reply.
if (err != 0) if (err != 0)
{ {
@ -73,7 +77,8 @@ namespace llarp::dns
ub_resolve_free(result); ub_resolve_free(result);
} }
bool UnboundResolver::Init() bool
UnboundResolver::Init()
{ {
if (started) if (started)
{ {
@ -92,7 +97,8 @@ namespace llarp::dns
return true; return true;
} }
bool UnboundResolver::AddUpstreamResolver(const std::string& upstreamResolverIP) bool
UnboundResolver::AddUpstreamResolver(const std::string& upstreamResolverIP)
{ {
if (ub_ctx_set_fwd(unboundContext, upstreamResolverIP.c_str()) != 0) if (ub_ctx_set_fwd(unboundContext, upstreamResolverIP.c_str()) != 0)
{ {
@ -102,7 +108,8 @@ namespace llarp::dns
return true; return true;
} }
void UnboundResolver::Lookup(const SockAddr& source, Message& msg) void
UnboundResolver::Lookup(const SockAddr& source, Message& msg)
{ {
if (not unboundContext) if (not unboundContext)
{ {
@ -115,7 +122,14 @@ namespace llarp::dns
const auto& q = msg.questions[0]; const auto& q = msg.questions[0];
auto* lookup = new PendingUnboundLookup{weak_from_this(), msg, source}; auto* lookup = new PendingUnboundLookup{weak_from_this(), msg, source};
int err = ub_resolve_async(unboundContext, q.Name().c_str(), q.qtype, q.qclass, (void*)lookup, &UnboundResolver::Callback, nullptr); int err = ub_resolve_async(
unboundContext,
q.Name().c_str(),
q.qtype,
q.qclass,
(void*)lookup,
&UnboundResolver::Callback,
nullptr);
if (err != 0) if (err != 0)
{ {
@ -125,4 +139,4 @@ namespace llarp::dns
} }
} }
} // namespace llarp::dns } // namespace llarp::dns

@ -17,8 +17,7 @@ namespace llarp::dns
class UnboundResolver : public std::enable_shared_from_this<UnboundResolver> class UnboundResolver : public std::enable_shared_from_this<UnboundResolver>
{ {
private:
private:
ub_ctx* unboundContext; ub_ctx* unboundContext;
bool started; bool started;
@ -26,27 +25,29 @@ namespace llarp::dns
llarp_ev_loop_ptr eventLoop; llarp_ev_loop_ptr eventLoop;
ReplyFunction replyFunc; ReplyFunction replyFunc;
void Reset(); void
Reset();
void DeregisterPollFD();
void RegisterPollFD();
public: void
DeregisterPollFD();
void
RegisterPollFD();
public:
UnboundResolver(llarp_ev_loop_ptr eventLoop, ReplyFunction replyFunc);
UnboundResolver( static void
llarp_ev_loop_ptr eventLoop, Callback(void* data, int err, ub_result* result);
ReplyFunction replyFunc);
static void Callback(void* data, int err, ub_result* result);
// upstream resolver IP can be IPv4 or IPv6 // upstream resolver IP can be IPv4 or IPv6
bool Init(); bool
Init();
bool AddUpstreamResolver(const std::string& upstreamResolverIP); bool
AddUpstreamResolver(const std::string& upstreamResolverIP);
void Lookup(const SockAddr& source, Message& msg); void
Lookup(const SockAddr& source, Message& msg);
}; };
} // namespace llarp::dns } // namespace llarp::dns

@ -816,7 +816,6 @@ struct llarp_ev_loop
virtual void virtual void
deregister_poll_fd_readable(int fd) = 0; deregister_poll_fd_readable(int fd) = 0;
}; };
#endif #endif

@ -1059,9 +1059,10 @@ namespace libuv
void void
OnUVPollFDReadable(uv_poll_t* handle, int status, [[maybe_unused]] int events) OnUVPollFDReadable(uv_poll_t* handle, int status, [[maybe_unused]] int events)
{ {
if (status < 0) return; // probably fd was closed if (status < 0)
return; // probably fd was closed
auto func = static_cast<libuv::Loop::Callback* >(handle->data); auto func = static_cast<libuv::Loop::Callback*>(handle->data);
(*func)(); (*func)();
} }
@ -1071,7 +1072,10 @@ namespace libuv
{ {
if (m_Polls.count(fd)) if (m_Polls.count(fd))
{ {
llarp::LogError("Attempting to create event loop poll on fd ", fd, ", but an event loop poll for that fd already exists."); llarp::LogError(
"Attempting to create event loop poll on fd ",
fd,
", but an event loop poll for that fd already exists.");
return; return;
} }
@ -1081,9 +1085,8 @@ namespace libuv
auto& new_poll = m_Polls[fd]; auto& new_poll = m_Polls[fd];
uv_poll_init(&m_Impl, &new_poll, fd); uv_poll_init(&m_Impl, &new_poll, fd);
new_poll.data = (void *) function_ptr; new_poll.data = (void*)function_ptr;
uv_poll_start(&new_poll, UV_READABLE, &OnUVPollFDReadable); uv_poll_start(&new_poll, UV_READABLE, &OnUVPollFDReadable);
} }
void void
@ -1094,7 +1097,7 @@ namespace libuv
if (itr != m_Polls.end()) if (itr != m_Polls.end())
{ {
uv_poll_stop(&(itr->second)); uv_poll_stop(&(itr->second));
auto func = static_cast<Callback* >(itr->second.data); auto func = static_cast<Callback*>(itr->second.data);
delete func; delete func;
m_Polls.erase(itr); m_Polls.erase(itr);
} }

Loading…
Cancel
Save