2019-08-02 09:27:27 +00:00
|
|
|
#include <ev/ev_libuv.hpp>
|
2021-01-11 23:13:22 +00:00
|
|
|
#include <ev/vpn.hpp>
|
2019-09-16 10:21:12 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-10-28 14:15:36 +00:00
|
|
|
#include <util/thread/queue.hpp>
|
2019-08-02 09:27:27 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstring>
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
namespace libuv
|
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
#define LoopCall(h, ...) \
|
|
|
|
{ \
|
|
|
|
auto __f = __VA_ARGS__; \
|
|
|
|
__f(); \
|
|
|
|
}
|
2019-09-16 10:21:12 +00:00
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
struct glue
|
|
|
|
{
|
2019-06-21 13:00:17 +00:00
|
|
|
virtual ~glue() = default;
|
2019-06-20 18:28:26 +00:00
|
|
|
virtual void
|
|
|
|
Close() = 0;
|
|
|
|
};
|
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
class UVWakeup final : public llarp::EventLoopWakeup, public glue
|
|
|
|
{
|
|
|
|
uv_async_t m_Impl;
|
|
|
|
const int m_Idx;
|
|
|
|
static void
|
|
|
|
OnWake(uv_async_t* self)
|
|
|
|
{
|
|
|
|
static_cast<UVWakeup*>(self->data)->callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
UVWakeup(uv_loop_t* loop, std::function<void()> hook, int idx)
|
|
|
|
: llarp::EventLoopWakeup{hook}, m_Idx{idx}
|
|
|
|
{
|
|
|
|
uv_async_init(loop, &m_Impl, OnWake);
|
|
|
|
m_Impl.data = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~UVWakeup() = default;
|
|
|
|
|
|
|
|
void
|
|
|
|
Close() override
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t*)&m_Impl, [](uv_handle_t* h) {
|
|
|
|
auto loop = static_cast<libuv::Loop*>(h->loop->data);
|
|
|
|
loop->delete_waker(static_cast<UVWakeup*>(h->data)->m_Idx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
End() override
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Wakeup() override
|
|
|
|
{
|
|
|
|
uv_async_send(&m_Impl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
struct ticker_glue : public glue
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::function<void(void)> func;
|
2019-09-05 17:39:09 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
ticker_glue(uv_loop_t* loop, std::function<void(void)> tick) : func(tick)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
|
|
|
m_Ticker.data = this;
|
|
|
|
uv_check_init(loop, &m_Ticker);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnTick(uv_check_t* t)
|
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("ticker_glue::OnTick() start");
|
2020-04-07 18:38:56 +00:00
|
|
|
ticker_glue* ticker = static_cast<ticker_glue*>(t->data);
|
2020-06-16 11:28:20 +00:00
|
|
|
ticker->func();
|
2021-01-11 23:13:22 +00:00
|
|
|
// Loop* loop = static_cast<Loop*>(t->loop->data);
|
|
|
|
// loop->FlushLogic();
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("ticker_glue::OnTick() end");
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Start()
|
|
|
|
{
|
|
|
|
return uv_check_start(&m_Ticker, &OnTick) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Close() override
|
|
|
|
{
|
|
|
|
uv_check_stop(&m_Ticker);
|
2019-11-20 19:45:23 +00:00
|
|
|
uv_close((uv_handle_t*)&m_Ticker, [](auto h) {
|
|
|
|
ticker_glue* self = (ticker_glue*)h->data;
|
2020-04-07 18:38:56 +00:00
|
|
|
h->data = nullptr;
|
2019-11-20 19:45:23 +00:00
|
|
|
delete self;
|
|
|
|
});
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uv_check_t m_Ticker;
|
|
|
|
};
|
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
struct udp_glue : public glue
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
uv_udp_t m_Handle;
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_t m_Ticker;
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp_udp_io* const m_UDP;
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp::SockAddr m_Addr;
|
2020-05-22 16:06:34 +00:00
|
|
|
std::vector<char> m_Buffer;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
udp_glue(uv_loop_t* loop, llarp_udp_io* udp, const llarp::SockAddr& src)
|
|
|
|
: m_UDP(udp), m_Addr(src)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
m_Handle.data = this;
|
|
|
|
m_Ticker.data = this;
|
|
|
|
uv_udp_init(loop, &m_Handle);
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_init(loop, &m_Ticker);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-22 17:13:58 +00:00
|
|
|
Alloc(uv_handle_t* h, size_t suggested_size, uv_buf_t* buf)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2020-05-22 17:13:58 +00:00
|
|
|
udp_glue* self = static_cast<udp_glue*>(h->data);
|
|
|
|
if (self->m_Buffer.empty())
|
2020-05-22 16:06:34 +00:00
|
|
|
self->m_Buffer.resize(suggested_size);
|
|
|
|
buf->base = self->m_Buffer.data();
|
|
|
|
buf->len = self->m_Buffer.size();
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
/// callback for libuv
|
2019-06-02 21:17:05 +00:00
|
|
|
static void
|
2020-05-06 20:38:44 +00:00
|
|
|
OnRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const sockaddr* addr, unsigned)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
udp_glue* glue = static_cast<udp_glue*>(handle->data);
|
|
|
|
if (addr)
|
2020-05-08 17:23:21 +00:00
|
|
|
glue->RecvFrom(nread, buf, llarp::SockAddr(*addr));
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-06 20:38:44 +00:00
|
|
|
RecvFrom(ssize_t sz, const uv_buf_t* buf, const llarp::SockAddr& fromaddr)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz > 0 && m_UDP)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
const size_t pktsz = sz;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_UDP->recvfrom)
|
2019-11-03 19:38:34 +00:00
|
|
|
{
|
|
|
|
const llarp_buffer_t pkt((const byte_t*)buf->base, pktsz);
|
2019-10-02 13:06:14 +00:00
|
|
|
m_UDP->recvfrom(m_UDP, fromaddr, ManagedBuffer{pkt});
|
2019-11-03 19:38:34 +00:00
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-04 14:36:54 +00:00
|
|
|
OnTick(uv_check_t* t)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("udp_glue::OnTick() start");
|
2020-04-07 18:38:56 +00:00
|
|
|
udp_glue* udp = static_cast<udp_glue*>(t->data);
|
2019-09-16 10:21:12 +00:00
|
|
|
udp->Tick();
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("udp_glue::OnTick() end");
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_UDP && m_UDP->tick)
|
2019-06-03 12:02:54 +00:00
|
|
|
m_UDP->tick(m_UDP);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-05-06 20:38:44 +00:00
|
|
|
SendTo(llarp_udp_io* udp, const llarp::SockAddr& to, const byte_t* ptr, size_t sz)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* self = static_cast<udp_glue*>(udp->impl);
|
|
|
|
if (self == nullptr)
|
2019-06-20 18:28:26 +00:00
|
|
|
return -1;
|
2021-01-11 23:13:22 +00:00
|
|
|
const auto buf = uv_buf_init((char*)ptr, sz);
|
|
|
|
return uv_udp_try_send(
|
2020-10-27 21:34:09 +00:00
|
|
|
&self->m_Handle, &buf, 1, (const sockaddr*)static_cast<const sockaddr_in*>(to));
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Bind()
|
|
|
|
{
|
2020-10-27 21:34:09 +00:00
|
|
|
auto ret =
|
|
|
|
uv_udp_bind(&m_Handle, (const sockaddr*)static_cast<const sockaddr_in*>(m_Addr), 0);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ret)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to bind to ", m_Addr, " ", uv_strerror(ret));
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (uv_udp_recv_start(&m_Handle, &Alloc, &OnRecv))
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to start recving packets via ", m_Addr);
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (uv_check_start(&m_Ticker, &OnTick))
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to start ticker");
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-11 21:18:47 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#else
|
2020-04-07 18:38:56 +00:00
|
|
|
if (uv_fileno((const uv_handle_t*)&m_Handle, &m_UDP->fd))
|
2019-06-02 21:17:05 +00:00
|
|
|
return false;
|
2019-12-11 21:18:47 +00:00
|
|
|
#endif
|
2019-06-02 21:17:05 +00:00
|
|
|
m_UDP->sendto = &SendTo;
|
2020-04-07 18:38:56 +00:00
|
|
|
m_UDP->impl = this;
|
2019-06-02 21:17:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnClosed(uv_handle_t* h)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* glue = static_cast<udp_glue*>(h->data);
|
|
|
|
if (glue)
|
2019-06-20 18:28:26 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
h->data = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
delete glue;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-20 18:28:26 +00:00
|
|
|
Close() override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
m_UDP->impl = nullptr;
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_stop(&m_Ticker);
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_close((uv_handle_t*)&m_Handle, &OnClosed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
struct tun_glue : public glue
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
uv_poll_t m_Handle;
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_t m_Ticker;
|
2021-01-11 23:13:22 +00:00
|
|
|
std::shared_ptr<llarp::vpn::NetworkInterface> m_NetIf;
|
|
|
|
std::function<void(llarp::net::IPPacket)> m_Handler;
|
2019-06-02 21:37:29 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
tun_glue(
|
|
|
|
std::shared_ptr<llarp::vpn::NetworkInterface> netif,
|
|
|
|
std::function<void(llarp::net::IPPacket)> handler)
|
|
|
|
: m_NetIf{std::move(netif)}, m_Handler{std::move(handler)}
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
m_Handle.data = this;
|
|
|
|
m_Ticker.data = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-01-11 23:13:22 +00:00
|
|
|
OnTick(uv_check_t* h)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
auto self = static_cast<tun_glue*>(h->data);
|
|
|
|
while (self->m_NetIf->HasNextPacket())
|
|
|
|
self->Read();
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:37:29 +00:00
|
|
|
static void
|
2019-06-03 12:02:54 +00:00
|
|
|
OnPoll(uv_poll_t* h, int, int events)
|
2019-06-02 21:37:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (events & UV_READABLE)
|
2019-06-02 21:37:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
static_cast<tun_glue*>(h->data)->Read();
|
2019-06-02 21:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Read()
|
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
auto pkt = m_NetIf->ReadNextPacket();
|
|
|
|
LogDebug("got packet ", pkt.sz);
|
|
|
|
if (m_Handler)
|
|
|
|
m_Handler(std::move(pkt));
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnClosed(uv_handle_t* h)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* self = static_cast<tun_glue*>(h->data);
|
|
|
|
if (self)
|
2019-06-20 18:28:26 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
h->data = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
delete self;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-20 18:28:26 +00:00
|
|
|
Close() override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_stop(&m_Ticker);
|
2021-01-11 23:13:22 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
uv_close((uv_handle_t*)&m_Handle, &OnClosed);
|
|
|
|
#endif
|
2019-06-03 12:02:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
|
|
|
Init(uv_loop_t* loop)
|
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
if (uv_check_init(loop, &m_Ticker) == -1)
|
2019-06-03 12:02:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
if (uv_check_start(&m_Ticker, &OnTick) == -1)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (uv_poll_init(loop, &m_Handle, m_NetIf->PollFD()) == -1)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
llarp::LogError("failed to initialize polling on ", m_NetIf->IfName());
|
2019-06-02 21:17:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (uv_poll_start(&m_Handle, UV_READABLE, &OnPoll))
|
2019-06-02 21:37:29 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
llarp::LogError("failed to start polling on ", m_NetIf->IfName());
|
2019-06-02 21:37:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
#endif
|
2019-06-02 21:17:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2020-05-15 12:36:38 +00:00
|
|
|
void
|
|
|
|
Loop::FlushLogic()
|
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("Loop::FlushLogic() start");
|
2020-05-15 12:36:38 +00:00
|
|
|
while (not m_LogicCalls.empty())
|
|
|
|
{
|
2020-05-26 18:04:43 +00:00
|
|
|
auto f = m_LogicCalls.popFront();
|
2020-05-15 12:36:38 +00:00
|
|
|
f();
|
|
|
|
}
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("Loop::FlushLogic() end");
|
2020-05-15 12:36:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:47:08 +00:00
|
|
|
static void
|
|
|
|
OnAsyncWake(uv_async_t* async_handle)
|
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("OnAsyncWake, ticking event loop.");
|
2020-04-07 18:38:56 +00:00
|
|
|
Loop* loop = static_cast<Loop*>(async_handle->data);
|
2020-05-21 14:20:47 +00:00
|
|
|
loop->update_time();
|
2019-11-23 04:47:08 +00:00
|
|
|
loop->process_timer_queue();
|
|
|
|
loop->process_cancel_queue();
|
2020-05-15 12:36:38 +00:00
|
|
|
loop->FlushLogic();
|
2021-01-11 23:13:22 +00:00
|
|
|
loop->PumpLL();
|
2020-06-08 13:07:49 +00:00
|
|
|
auto& log = llarp::LogContext::Instance();
|
|
|
|
if (log.logStream)
|
|
|
|
log.logStream->Tick(loop->time_now());
|
2019-11-23 04:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 13:37:45 +00:00
|
|
|
constexpr size_t TimerQueueSize = 20;
|
2021-01-11 23:13:22 +00:00
|
|
|
|
2020-06-01 17:31:12 +00:00
|
|
|
Loop::Loop(size_t queue_size)
|
2021-01-11 23:13:22 +00:00
|
|
|
: llarp::EventLoop{}
|
|
|
|
, PumpLL{[]() {}}
|
|
|
|
, m_LogicCalls{queue_size}
|
|
|
|
, m_timerQueue{TimerQueueSize}
|
|
|
|
, m_timerCancelQueue{TimerQueueSize}
|
Config file improvements (#1397)
* Config file API/comment improvements
API improvements:
=================
Make the config API use position-independent tag parameters (Required,
Default{123}, MultiValue) rather than a sequence of bools with
overloads. For example, instead of:
conf.defineOption<int>("a", "b", false, true, 123, [] { ... });
you now write:
conf.defineOption<int>("a", "b", MultiValue, Default{123}, [] { ... });
The tags are:
- Required
- MultiValue
- Default{value}
plus new abilities (see below):
- Hidden
- RelayOnly
- ClientOnly
- Comment{"line1", "line2", "line3"}
Made option definition more powerful:
=====================================
- `Hidden` allows you to define an option that won't show up in the
generated config file if it isn't set.
- `RelayOnly`/`ClientOnly` sets up an option that is only accepted and
only shows up for relay or client configs. (If neither is specified
the option shows up in both modes).
- `Comment{...}` lets the option comments be specified as part of the
defineOption.
Comment improvements
====================
- Rewrote comments for various options to expand on details.
- Inlined all the comments with the option definitions.
- Several options that were missing comments got comments added.
- Made various options for deprecated and or internal options hidden by
default so that they don't show up in a default config file.
- show the section comment (but not option comments) *after* the
[section] tag instead of before it as it makes more sense that way
(particularly for the [bind] section which has a new long comment to
describe how it works).
Disable profiling by default
============================
We had this weird state where we use and store profiling by default but
never *load* it when starting up. This commit makes us just not use
profiling at all unless explicitly enabled.
Other misc changes:
===================
- change default worker threads to 0 (= num cpus) instead of 1, and fix
it to allow 0.
- Actually apply worker-threads option
- fixed default data-dir value erroneously having quotes around it
- reordered ifname/ifaddr/mapaddr (was previously mapaddr/ifaddr/ifname)
as mapaddr is a sort of specialization of ifaddr and so makes more
sense to come after it (particularly because it now references ifaddr
in its help message).
- removed peer-stats option (since we always require it for relays and
never use it for clients)
- removed router profiles filename option (this doesn't need to be
configurable)
- removed defunct `service-node-seed` option
- Change default logging output file to "" (which means stdout), and
also made "-" work for stdout.
* Router hive compilation fixes
* Comments for SNApp SRV settings in ini file
* Add extra blank line after section comments
* Better deprecated option handling
Allow {client,relay}-only options in {relay,client} configs to be
specified as implicitly deprecated options: they warn, and don't set
anything.
Add an explicit `Deprecated` tag and move deprecated option handling
into definition.cpp.
* Move backwards compat options into section definitions
Keep the "addBackwardsCompatibleConfigOptions" only for options in
sections that no longer exist.
* Fix INI parsing issues & C++17-ify
- don't allow inline comments because it seems they aren't allowed in
ini formats in general, and is going to cause problems if there is a
comment character in a value (e.g. an exit auth string). Additionally
it was breaking on a line such as:
# some comment; see?
because it was treating only `; see?` as the comment and then producing
an error message about the rest of the line being invalid.
- make section parsing stricter: the `[` and `]` have to be at the
beginning at end of the line now (after stripping whitespace).
- Move whitespace stripping to the top since everything in here does it.
- chop off string_view suffix/prefix rather than maintaining position
values
- fix potential infinite loop/segfault when given a line such as `]foo[`
* Make config parsing failure fatal
Load() LogError's and returns false on failure, so we weren't aborting
on config file errors.
* Formatting: allow `{}` for empty functions/structs
Instead of using two lines when empty:
{
}
* Make default dns bind 127.0.0.1 on non-Linux
* Don't show empty section; fix tests
We can conceivably have sections that only make sense for clients or
relays, and so want to completely omit that section if we have no
options for the type of config being generated.
Also fixes missing empty lines between tests.
Co-authored-by: Thomas Winget <tewinget@gmail.com>
2020-10-07 22:22:58 +00:00
|
|
|
{}
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
|
|
|
Loop::init()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (uv_loop_init(&m_Impl) == -1)
|
2019-06-02 21:17:05 +00:00
|
|
|
return false;
|
2019-11-28 02:35:59 +00:00
|
|
|
|
|
|
|
#ifdef LOKINET_DEBUG
|
2020-04-07 18:38:56 +00:00
|
|
|
last_time = 0;
|
2019-11-28 02:35:59 +00:00
|
|
|
loop_run_count = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-10-16 11:45:52 +00:00
|
|
|
m_Impl.data = this;
|
2019-12-11 21:18:47 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#else
|
2019-10-16 11:45:52 +00:00
|
|
|
uv_loop_configure(&m_Impl, UV_LOOP_BLOCK_SIGNAL, SIGPIPE);
|
2019-12-11 21:18:47 +00:00
|
|
|
#endif
|
2020-04-07 18:38:56 +00:00
|
|
|
m_TickTimer = new uv_timer_t;
|
2019-11-23 04:47:08 +00:00
|
|
|
m_TickTimer->data = this;
|
2020-05-21 14:20:47 +00:00
|
|
|
if (uv_timer_init(&m_Impl, m_TickTimer) == -1)
|
|
|
|
return false;
|
2019-06-02 21:17:05 +00:00
|
|
|
m_Run.store(true);
|
2019-11-23 04:47:08 +00:00
|
|
|
m_nextID.store(0);
|
|
|
|
m_WakeUp.data = this;
|
|
|
|
uv_async_init(&m_Impl, &m_WakeUp, &OnAsyncWake);
|
2020-05-21 14:20:47 +00:00
|
|
|
return true;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::update_time()
|
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
llarp::EventLoop::update_time();
|
2019-10-16 11:45:52 +00:00
|
|
|
uv_update_time(&m_Impl);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::running() const
|
|
|
|
{
|
|
|
|
return m_Run.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnTickTimeout(uv_timer_t* timer)
|
|
|
|
{
|
|
|
|
uv_stop(timer->loop);
|
|
|
|
}
|
|
|
|
|
2020-05-15 12:36:38 +00:00
|
|
|
int
|
|
|
|
Loop::run()
|
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("Loop::run()");
|
2020-06-16 11:28:20 +00:00
|
|
|
m_EventLoopThreadID = std::this_thread::get_id();
|
2020-05-15 12:36:38 +00:00
|
|
|
return uv_run(&m_Impl, UV_RUN_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
void
|
|
|
|
Loop::set_pump_function(std::function<void(void)> pump)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
PumpLL = std::move(pump);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:47:08 +00:00
|
|
|
struct TimerData
|
|
|
|
{
|
|
|
|
Loop* loop;
|
|
|
|
uint64_t job_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseUVTimer(uv_timer_t* timer)
|
|
|
|
{
|
|
|
|
// have to delete timer handle this way because libuv.
|
|
|
|
uv_timer_stop(timer);
|
2020-04-07 18:38:56 +00:00
|
|
|
uv_close((uv_handle_t*)timer, [](uv_handle_t* handle) { delete (uv_timer_t*)handle; });
|
2019-11-23 04:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnUVTimer(uv_timer_t* timer)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
TimerData* timer_data = static_cast<TimerData*>(timer->data);
|
|
|
|
Loop* loop = timer_data->loop;
|
2019-11-23 04:47:08 +00:00
|
|
|
loop->do_timer_job(timer_data->job_id);
|
|
|
|
|
|
|
|
delete timer_data;
|
|
|
|
CloseUVTimer(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2020-04-07 18:38:56 +00:00
|
|
|
Loop::call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback)
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogTrace("Loop::call_after_delay()");
|
2020-01-23 12:59:51 +00:00
|
|
|
#ifdef TESTNET_SPEED
|
|
|
|
delay_ms *= TESTNET_SPEED;
|
|
|
|
#endif
|
2019-11-23 04:47:08 +00:00
|
|
|
PendingTimer timer;
|
2020-04-07 18:38:56 +00:00
|
|
|
timer.delay_ms = delay_ms;
|
|
|
|
timer.callback = callback;
|
|
|
|
timer.job_id = m_nextID++;
|
2019-11-23 04:47:08 +00:00
|
|
|
uint64_t job_id = timer.job_id;
|
|
|
|
|
|
|
|
m_timerQueue.pushBack(std::move(timer));
|
|
|
|
uv_async_send(&m_WakeUp);
|
|
|
|
|
|
|
|
return job_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::cancel_delayed_call(uint32_t job_id)
|
|
|
|
{
|
|
|
|
m_timerCancelQueue.pushBack(job_id);
|
|
|
|
uv_async_send(&m_WakeUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::process_timer_queue()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (not m_timerQueue.empty())
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
|
|
|
PendingTimer job = m_timerQueue.popFront();
|
2020-04-07 18:38:56 +00:00
|
|
|
uint64_t job_id = job.job_id;
|
2019-11-23 04:47:08 +00:00
|
|
|
m_pendingCalls.emplace(job_id, std::move(job.callback));
|
|
|
|
|
|
|
|
TimerData* timer_data = new TimerData;
|
2020-04-07 18:38:56 +00:00
|
|
|
timer_data->loop = this;
|
|
|
|
timer_data->job_id = job_id;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
|
|
|
uv_timer_t* newTimer = new uv_timer_t;
|
2020-04-07 18:38:56 +00:00
|
|
|
newTimer->data = (void*)timer_data;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
|
|
|
uv_timer_init(&m_Impl, newTimer);
|
2020-02-24 19:40:45 +00:00
|
|
|
uv_timer_start(newTimer, &OnUVTimer, job.delay_ms.count(), 0);
|
2019-11-23 04:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::process_cancel_queue()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (not m_timerCancelQueue.empty())
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
|
|
|
uint64_t job_id = m_timerCancelQueue.popFront();
|
2020-05-21 14:20:47 +00:00
|
|
|
m_pendingCalls.erase(job_id);
|
2019-11-23 04:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::do_timer_job(uint64_t job_id)
|
|
|
|
{
|
|
|
|
auto itr = m_pendingCalls.find(job_id);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_pendingCalls.end())
|
2019-11-23 04:47:08 +00:00
|
|
|
{
|
2020-05-21 14:20:47 +00:00
|
|
|
if (itr->second)
|
|
|
|
itr->second();
|
|
|
|
m_pendingCalls.erase(itr->first);
|
2019-11-23 04:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
|
|
|
Loop::stop()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_Run)
|
2019-11-20 19:45:23 +00:00
|
|
|
{
|
|
|
|
llarp::LogInfo("stopping event loop");
|
|
|
|
CloseAll();
|
2020-05-15 12:36:38 +00:00
|
|
|
uv_stop(&m_Impl);
|
2019-11-20 19:45:23 +00:00
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
m_Run.store(false);
|
2019-06-20 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::CloseAll()
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Closing all handles");
|
2019-08-13 21:38:00 +00:00
|
|
|
uv_walk(
|
2019-10-16 11:45:52 +00:00
|
|
|
&m_Impl,
|
2019-08-13 21:38:00 +00:00
|
|
|
[](uv_handle_t* h, void*) {
|
2020-04-07 18:38:56 +00:00
|
|
|
if (uv_is_closing(h))
|
2019-08-13 21:38:00 +00:00
|
|
|
return;
|
2020-06-09 16:59:49 +00:00
|
|
|
if (h->data && uv_is_active(h) && h->type != UV_TIMER && h->type != UV_POLL)
|
2019-08-13 21:38:00 +00:00
|
|
|
{
|
2020-06-17 13:07:05 +00:00
|
|
|
auto glue = reinterpret_cast<libuv::glue*>(h->data);
|
|
|
|
if (glue)
|
|
|
|
glue->Close();
|
2019-08-13 21:38:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
nullptr);
|
2019-06-20 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::stopped()
|
|
|
|
{
|
|
|
|
llarp::LogInfo("we have stopped");
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-05-06 20:38:44 +00:00
|
|
|
Loop::udp_listen(llarp_udp_io* udp, const llarp::SockAddr& src)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* impl = new udp_glue(&m_Impl, udp, src);
|
2020-04-07 18:38:56 +00:00
|
|
|
udp->impl = impl;
|
|
|
|
if (impl->Bind())
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-21 13:06:43 +00:00
|
|
|
llarp::LogError("Loop::udp_listen failed to bind");
|
2019-06-20 18:28:26 +00:00
|
|
|
delete impl;
|
2019-06-02 21:17:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Loop::add_ticker(std::function<void(void)> func)
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* ticker = new ticker_glue(&m_Impl, func);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ticker->Start())
|
2019-09-05 17:39:09 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
delete ticker;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
2021-01-11 23:13:22 +00:00
|
|
|
Loop::add_network_interface(
|
|
|
|
std::shared_ptr<llarp::vpn::NetworkInterface> netif,
|
|
|
|
std::function<void(llarp::net::IPPacket)> handler)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
auto* glue = new tun_glue(netif, handler);
|
|
|
|
// call to Init gives ownership of glue to event loop
|
2020-04-07 18:38:56 +00:00
|
|
|
if (glue->Init(&m_Impl))
|
2019-06-03 11:14:31 +00:00
|
|
|
return true;
|
|
|
|
delete glue;
|
|
|
|
return false;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:18:05 +00:00
|
|
|
bool
|
2021-01-11 23:13:22 +00:00
|
|
|
Loop::udp_close(llarp_udp_io* udp)
|
2019-08-22 11:18:05 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
if (udp == nullptr)
|
|
|
|
return false;
|
|
|
|
auto* glue = static_cast<udp_glue*>(udp->impl);
|
|
|
|
if (glue == nullptr)
|
|
|
|
return false;
|
|
|
|
glue->Close();
|
|
|
|
return true;
|
2019-08-22 11:18:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:21:52 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Loop::call_soon(std::function<void(void)> f)
|
2019-12-10 15:21:52 +00:00
|
|
|
{
|
2020-06-16 11:28:20 +00:00
|
|
|
if (not m_EventLoopThreadID.has_value())
|
|
|
|
{
|
|
|
|
m_LogicCalls.tryPushBack(f);
|
|
|
|
uv_async_send(&m_WakeUp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto inEventLoop = *m_EventLoopThreadID == std::this_thread::get_id();
|
|
|
|
|
2020-10-21 21:31:57 +00:00
|
|
|
if (inEventLoop and m_LogicCalls.full())
|
2020-06-16 11:28:20 +00:00
|
|
|
{
|
2020-10-21 21:31:57 +00:00
|
|
|
FlushLogic();
|
2020-06-16 11:28:20 +00:00
|
|
|
}
|
2020-10-21 21:31:57 +00:00
|
|
|
m_LogicCalls.pushBack(f);
|
2020-05-15 12:36:38 +00:00
|
|
|
uv_async_send(&m_WakeUp);
|
2019-12-10 15:21:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 16:59:49 +00:00
|
|
|
void
|
|
|
|
OnUVPollFDReadable(uv_poll_t* handle, int status, [[maybe_unused]] int events)
|
|
|
|
{
|
2020-06-11 19:36:19 +00:00
|
|
|
if (status < 0)
|
|
|
|
return; // probably fd was closed
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2020-06-11 19:36:19 +00:00
|
|
|
auto func = static_cast<libuv::Loop::Callback*>(handle->data);
|
2020-06-09 16:59:49 +00:00
|
|
|
|
|
|
|
(*func)();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::register_poll_fd_readable(int fd, Callback callback)
|
|
|
|
{
|
|
|
|
if (m_Polls.count(fd))
|
|
|
|
{
|
2020-06-11 19:36:19 +00:00
|
|
|
llarp::LogError(
|
|
|
|
"Attempting to create event loop poll on fd ",
|
|
|
|
fd,
|
|
|
|
", but an event loop poll for that fd already exists.");
|
2020-06-09 16:59:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new a copy as the one passed in here will go out of scope
|
|
|
|
auto function_ptr = new Callback(callback);
|
|
|
|
|
|
|
|
auto& new_poll = m_Polls[fd];
|
|
|
|
|
|
|
|
uv_poll_init(&m_Impl, &new_poll, fd);
|
2020-06-11 19:36:19 +00:00
|
|
|
new_poll.data = (void*)function_ptr;
|
2020-06-09 16:59:49 +00:00
|
|
|
uv_poll_start(&new_poll, UV_READABLE, &OnUVPollFDReadable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::deregister_poll_fd_readable(int fd)
|
|
|
|
{
|
|
|
|
auto itr = m_Polls.find(fd);
|
|
|
|
|
|
|
|
if (itr != m_Polls.end())
|
|
|
|
{
|
|
|
|
uv_poll_stop(&(itr->second));
|
2020-06-11 19:36:19 +00:00
|
|
|
auto func = static_cast<Callback*>(itr->second.data);
|
2020-06-09 16:59:49 +00:00
|
|
|
delete func;
|
|
|
|
m_Polls.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
llarp::EventLoopWakeup*
|
|
|
|
Loop::make_event_loop_waker(std::function<void()> callback)
|
|
|
|
{
|
|
|
|
auto wake_idx = m_NumWakers++;
|
|
|
|
auto wake = new UVWakeup{&m_Impl, callback, wake_idx};
|
|
|
|
m_Wakers[wake_idx] = wake;
|
|
|
|
return wake;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::delete_waker(int idx)
|
|
|
|
{
|
|
|
|
delete m_Wakers[idx];
|
|
|
|
m_Wakers.erase(idx);
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
} // namespace libuv
|