2019-01-11 02:19:36 +01:00
|
|
|
#include <ev/ev.h>
|
|
|
|
#include <util/logic.hpp>
|
2019-01-10 20:41:51 +01:00
|
|
|
#include <util/mem.hpp>
|
|
|
|
#include <util/string_view.hpp>
|
2018-11-06 23:48:17 +01:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2018-08-15 04:42:33 +02:00
|
|
|
// apparently current Solaris will emulate epoll.
|
2018-08-17 00:36:15 +02:00
|
|
|
#if __linux__ || __sun__
|
2019-01-11 02:19:36 +01:00
|
|
|
#include <ev/ev_epoll.hpp>
|
2018-10-30 00:56:05 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|
2018-08-08 19:43:46 +02:00
|
|
|
|| (__APPLE__ && __MACH__)
|
2019-01-11 02:19:36 +01:00
|
|
|
#include <ev/ev_kqueue.hpp>
|
2018-10-30 00:56:05 +01:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
2019-01-11 02:19:36 +01:00
|
|
|
#include <ev/ev_win32.hpp>
|
2018-10-30 00:56:05 +01:00
|
|
|
#else
|
|
|
|
#error No async event loop for your platform, subclass llarp_ev_loop
|
2018-04-30 15:18:57 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_ev_loop_alloc(struct llarp_ev_loop **ev)
|
|
|
|
{
|
2018-08-17 00:36:15 +02:00
|
|
|
#if __linux__ || __sun__
|
2018-04-30 15:18:57 +02:00
|
|
|
*ev = new llarp_epoll_loop;
|
2018-10-30 00:56:05 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|
2018-08-08 19:43:46 +02:00
|
|
|
|| (__APPLE__ && __MACH__)
|
2018-05-29 14:14:50 +02:00
|
|
|
*ev = new llarp_kqueue_loop;
|
2018-10-30 00:56:05 +01:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
2018-07-30 06:38:14 +02:00
|
|
|
*ev = new llarp_win32_loop;
|
2018-10-30 00:56:05 +01:00
|
|
|
#else
|
|
|
|
#error no event loop subclass
|
2018-04-30 15:18:57 +02:00
|
|
|
#endif
|
2018-05-18 15:17:58 +02:00
|
|
|
(*ev)->init();
|
2018-11-19 23:45:37 +01:00
|
|
|
(*ev)->_now = llarp::time_now_ms();
|
2018-01-29 15:27:24 +01:00
|
|
|
}
|
2018-01-08 14:49:05 +01:00
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_ev_loop_free(struct llarp_ev_loop **ev)
|
|
|
|
{
|
2018-04-30 15:18:57 +02:00
|
|
|
delete *ev;
|
2018-01-29 15:27:24 +01:00
|
|
|
*ev = nullptr;
|
2018-12-14 13:04:14 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
exit_tun_loop();
|
|
|
|
#endif
|
2018-01-29 15:27:24 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
int
|
2018-12-10 15:14:55 +01:00
|
|
|
llarp_ev_loop_run(struct llarp_ev_loop *ev, llarp::Logic *logic)
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2018-08-29 22:40:26 +02:00
|
|
|
while(ev->running())
|
2018-07-30 00:20:31 +02:00
|
|
|
{
|
2018-11-19 23:45:37 +01:00
|
|
|
ev->_now = llarp::time_now_ms();
|
2018-09-21 14:30:57 +02:00
|
|
|
ev->tick(EV_TICK_INTERVAL);
|
|
|
|
if(ev->running())
|
2018-12-10 15:14:55 +01:00
|
|
|
logic->tick(ev->_now);
|
2018-07-30 00:20:31 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2018-05-22 17:54:19 +02:00
|
|
|
}
|
2018-01-29 15:27:24 +01:00
|
|
|
|
2018-12-02 19:07:07 +01:00
|
|
|
int
|
|
|
|
llarp_fd_promise_wait_for_value(struct llarp_fd_promise *p)
|
2018-11-26 23:46:22 +01:00
|
|
|
{
|
|
|
|
return p->Get();
|
|
|
|
}
|
|
|
|
|
2018-06-06 14:46:26 +02:00
|
|
|
void
|
|
|
|
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,
|
|
|
|
struct llarp_threadpool *tp,
|
2018-12-10 15:14:55 +01:00
|
|
|
llarp::Logic *logic)
|
2018-06-06 14:46:26 +02:00
|
|
|
{
|
2018-08-10 05:51:38 +02:00
|
|
|
while(ev->running())
|
2018-06-06 14:46:26 +02:00
|
|
|
{
|
2018-11-19 23:45:37 +01:00
|
|
|
ev->_now = llarp::time_now_ms();
|
2018-08-10 05:51:38 +02:00
|
|
|
ev->tick(EV_TICK_INTERVAL);
|
2018-09-21 14:30:57 +02:00
|
|
|
if(ev->running())
|
|
|
|
{
|
2019-01-24 16:13:41 +01:00
|
|
|
ev->_now = llarp::time_now_ms();
|
2018-12-10 15:14:55 +01:00
|
|
|
logic->tick_async(ev->_now);
|
2018-09-21 14:30:57 +02:00
|
|
|
llarp_threadpool_tick(tp);
|
|
|
|
}
|
2018-06-06 14:46:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
int
|
2018-05-23 15:49:00 +02:00
|
|
|
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
|
|
|
|
const struct sockaddr *src)
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2018-05-16 18:41:20 +02:00
|
|
|
udp->parent = ev;
|
2018-05-23 15:49:00 +02:00
|
|
|
if(ev->udp_listen(udp, src))
|
2018-05-22 17:54:19 +02:00
|
|
|
return 0;
|
2018-05-16 18:41:20 +02:00
|
|
|
return -1;
|
2018-01-29 15:27:24 +01:00
|
|
|
}
|
2018-01-19 17:51:27 +01:00
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
int
|
2018-05-22 21:19:06 +02:00
|
|
|
llarp_ev_close_udp(struct llarp_udp_io *udp)
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
|
|
|
if(udp->parent->udp_close(udp))
|
|
|
|
return 0;
|
2018-05-16 18:41:20 +02:00
|
|
|
return -1;
|
2018-01-29 15:27:24 +01:00
|
|
|
}
|
2018-01-29 15:19:00 +01:00
|
|
|
|
2018-10-29 17:48:36 +01:00
|
|
|
llarp_time_t
|
|
|
|
llarp_ev_loop_time_now_ms(struct llarp_ev_loop *loop)
|
|
|
|
{
|
2018-11-15 22:47:05 +01:00
|
|
|
if(loop)
|
|
|
|
return loop->_now;
|
2018-11-20 13:44:18 +01:00
|
|
|
return llarp::time_now_ms();
|
2018-10-29 17:48:36 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
llarp_ev_loop_stop(struct llarp_ev_loop *loop)
|
2018-05-18 19:10:48 +02:00
|
|
|
{
|
2018-05-22 17:54:19 +02:00
|
|
|
loop->stop();
|
2018-05-18 19:10:48 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
int
|
|
|
|
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
|
2018-11-23 15:37:26 +01:00
|
|
|
llarp_buffer_t buf)
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2018-11-26 14:30:03 +01:00
|
|
|
auto ret =
|
|
|
|
static_cast< llarp::ev_io * >(udp->impl)->sendto(to, buf.base, buf.sz);
|
2018-11-20 07:12:56 +01:00
|
|
|
#ifndef _WIN32
|
2018-10-26 05:56:48 +02:00
|
|
|
if(ret == -1 && errno != 0)
|
2018-08-02 06:34:46 +02:00
|
|
|
{
|
2018-11-20 07:12:56 +01:00
|
|
|
#else
|
|
|
|
if(ret == -1 && WSAGetLastError())
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2018-08-02 06:34:46 +02:00
|
|
|
llarp::LogWarn("sendto failed ", strerror(errno));
|
|
|
|
errno = 0;
|
|
|
|
}
|
2018-11-20 07:12:56 +01:00
|
|
|
#else
|
2018-11-20 09:18:29 +01:00
|
|
|
char ebuf[1024];
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf,
|
|
|
|
1024, nullptr);
|
2018-12-03 19:24:56 +01:00
|
|
|
llarp::LogWarn("sendto failed: ", ebuf);
|
2018-11-20 07:12:56 +01:00
|
|
|
WSASetLastError(0);
|
2018-11-20 09:18:29 +01:00
|
|
|
}
|
2018-11-20 07:12:56 +01:00
|
|
|
#endif
|
2018-08-02 06:34:46 +02:00
|
|
|
return ret;
|
2018-05-22 17:54:19 +02:00
|
|
|
}
|
2018-08-15 17:36:34 +02:00
|
|
|
|
2018-12-14 13:04:14 +01:00
|
|
|
#ifndef _WIN32
|
2019-01-16 22:03:45 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-08-15 17:36:34 +02:00
|
|
|
bool
|
|
|
|
llarp_ev_add_tun(struct llarp_ev_loop *loop, struct llarp_tun_io *tun)
|
|
|
|
{
|
2019-01-29 12:12:21 +01:00
|
|
|
// llarp::LogInfo("ev creating tunnel ", tun->ifaddr, " on ", tun->ifname);
|
|
|
|
if(strcmp(tun->ifaddr, "") == 0 || strcmp(tun->ifaddr, "auto") == 0)
|
2019-01-16 22:03:45 +01:00
|
|
|
{
|
|
|
|
std::string ifaddr = llarp::findFreePrivateRange();
|
2019-01-24 16:14:54 +01:00
|
|
|
auto pos = ifaddr.find("/");
|
2019-01-16 22:03:45 +01:00
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("Auto ifaddr didn't return a netmask: ", ifaddr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int num;
|
|
|
|
std::string part = ifaddr.substr(pos + 1);
|
|
|
|
#if defined(ANDROID) || defined(RPI)
|
|
|
|
num = atoi(part.c_str());
|
|
|
|
#else
|
|
|
|
num = std::stoi(part);
|
|
|
|
#endif
|
|
|
|
if(num <= 0)
|
|
|
|
{
|
|
|
|
llarp::LogError("bad ifaddr netmask value: ", ifaddr);
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-24 16:14:54 +01:00
|
|
|
tun->netmask = num;
|
2019-01-18 03:33:55 +01:00
|
|
|
const std::string addr = ifaddr.substr(0, pos);
|
2019-01-24 16:14:54 +01:00
|
|
|
std::copy_n(addr.begin(), std::min(sizeof(tun->ifaddr), addr.size()),
|
|
|
|
tun->ifaddr);
|
2019-01-16 22:03:45 +01:00
|
|
|
llarp::LogInfo("IfAddr autodetect: ", tun->ifaddr, "/", tun->netmask);
|
|
|
|
}
|
2019-01-29 12:12:21 +01:00
|
|
|
if(strcmp(tun->ifname, "") == 0 || strcmp(tun->ifname, "auto") == 0)
|
2019-01-16 22:03:45 +01:00
|
|
|
{
|
2019-01-18 03:33:55 +01:00
|
|
|
std::string ifname = llarp::findFreeLokiTunIfName();
|
2019-01-24 16:14:54 +01:00
|
|
|
std::copy_n(ifname.begin(), std::min(sizeof(tun->ifname), ifname.size()),
|
|
|
|
tun->ifname);
|
2019-01-16 22:03:45 +01:00
|
|
|
llarp::LogInfo("IfName autodetect: ", tun->ifname);
|
|
|
|
}
|
|
|
|
llarp::LogDebug("Tun Interface will use the following settings:");
|
|
|
|
llarp::LogDebug("IfAddr: ", tun->ifaddr);
|
|
|
|
llarp::LogDebug("IfName: ", tun->ifname);
|
|
|
|
llarp::LogDebug("IfNMsk: ", tun->netmask);
|
2018-08-21 20:17:16 +02:00
|
|
|
auto dev = loop->create_tun(tun);
|
|
|
|
tun->impl = dev;
|
2018-08-20 21:12:12 +02:00
|
|
|
if(dev)
|
2019-01-16 22:03:45 +01:00
|
|
|
{
|
2018-11-02 13:35:20 +01:00
|
|
|
return loop->add_ev(dev, false);
|
2019-01-16 22:03:45 +01:00
|
|
|
}
|
|
|
|
llarp::LogWarn("Loop could not create tun");
|
2018-08-20 21:12:12 +02:00
|
|
|
return false;
|
2018-08-15 17:36:34 +02:00
|
|
|
}
|
2018-12-14 13:04:14 +01:00
|
|
|
#else
|
|
|
|
// OK, now it's time to do it my way.
|
|
|
|
// we're not even going to use the existing llarp::tun
|
|
|
|
// we still use the llarp_tun_io struct
|
|
|
|
// since we still need to branch to the
|
|
|
|
// packet processing functions
|
|
|
|
bool
|
|
|
|
llarp_ev_add_tun(llarp_ev_loop *loop, llarp_tun_io *tun)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(loop);
|
|
|
|
auto dev = new win32_tun_io(tun);
|
|
|
|
tun->impl = dev;
|
|
|
|
// We're not even going to add this to the socket event loop
|
2018-12-23 14:29:11 +01:00
|
|
|
if(dev)
|
2018-12-14 13:04:14 +01:00
|
|
|
{
|
|
|
|
dev->setup();
|
|
|
|
return dev->add_ev(); // start up tun and add to event queue
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
bool
|
|
|
|
llarp_ev_tun_async_write(struct llarp_tun_io *tun, llarp_buffer_t buf)
|
|
|
|
{
|
|
|
|
if(buf.sz > EV_WRITE_BUF_SZ)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("packet too big, ", buf.sz, " > ", EV_WRITE_BUF_SZ);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return static_cast< llarp::tun * >(tun->impl)->queue_write(buf.base, buf.sz);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
bool
|
|
|
|
llarp_ev_tun_async_write(struct llarp_tun_io *tun, llarp_buffer_t buf)
|
|
|
|
{
|
|
|
|
if(buf.sz > EV_WRITE_BUF_SZ)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("packet too big, ", buf.sz, " > ", EV_WRITE_BUF_SZ);
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-23 14:29:11 +01:00
|
|
|
return static_cast< win32_tun_io * >(tun->impl)->queue_write(buf.base,
|
|
|
|
buf.sz);
|
2018-12-14 13:04:14 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-08-15 17:36:34 +02:00
|
|
|
|
|
|
|
bool
|
2018-11-23 15:37:26 +01:00
|
|
|
llarp_tcp_conn_async_write(struct llarp_tcp_conn *conn, llarp_buffer_t buf)
|
2018-08-15 17:36:34 +02:00
|
|
|
{
|
2018-10-29 17:48:36 +01:00
|
|
|
llarp::tcp_conn *impl = static_cast< llarp::tcp_conn * >(conn->impl);
|
2018-10-25 14:39:32 +02:00
|
|
|
if(impl->_shouldClose)
|
2018-11-02 13:35:20 +01:00
|
|
|
{
|
|
|
|
llarp::LogError("write on closed connection");
|
2018-10-25 14:39:32 +02:00
|
|
|
return false;
|
2018-11-02 13:35:20 +01:00
|
|
|
}
|
2018-11-23 15:37:26 +01:00
|
|
|
size_t sz = buf.sz;
|
2018-11-26 14:30:03 +01:00
|
|
|
buf.cur = buf.base;
|
2018-10-27 20:26:08 +02:00
|
|
|
while(sz > EV_WRITE_BUF_SZ)
|
2018-10-25 14:09:29 +02:00
|
|
|
{
|
2018-11-23 15:37:26 +01:00
|
|
|
if(!impl->queue_write(buf.cur, EV_WRITE_BUF_SZ))
|
2018-10-25 14:09:29 +02:00
|
|
|
return false;
|
2018-11-23 15:37:26 +01:00
|
|
|
buf.cur += EV_WRITE_BUF_SZ;
|
2018-10-27 20:26:08 +02:00
|
|
|
sz -= EV_WRITE_BUF_SZ;
|
2018-10-25 14:09:29 +02:00
|
|
|
}
|
2018-11-23 15:37:26 +01:00
|
|
|
return impl->queue_write(buf.cur, sz);
|
2018-08-17 00:36:15 +02:00
|
|
|
}
|
2018-10-09 14:06:30 +02:00
|
|
|
|
2018-11-01 13:47:14 +01:00
|
|
|
void
|
|
|
|
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;
|
|
|
|
// try parsing address
|
|
|
|
const char *begin = tcp->remote;
|
|
|
|
const char *ptr = strstr(tcp->remote, ":");
|
|
|
|
// get end of address
|
|
|
|
|
|
|
|
if(ptr == nullptr)
|
|
|
|
{
|
|
|
|
llarp::LogError("bad address: ", tcp->remote);
|
|
|
|
if(tcp->error)
|
|
|
|
tcp->error(tcp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *end = ptr;
|
2018-11-08 13:31:50 +01:00
|
|
|
while(*end && ((end - begin) < static_cast< ptrdiff_t >(sizeof tcp->remote)))
|
2018-11-01 13:47:14 +01:00
|
|
|
{
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
addr_str = llarp::string_view(begin, ptr - begin);
|
|
|
|
++ptr;
|
|
|
|
port_str = llarp::string_view(ptr, end - ptr);
|
|
|
|
// actually parse address
|
|
|
|
llarp::Addr addr(addr_str, port_str);
|
|
|
|
|
2018-11-02 13:35:20 +01:00
|
|
|
if(!loop->tcp_connect(tcp, addr))
|
2018-11-01 13:47:14 +01:00
|
|
|
{
|
2018-11-02 13:35:20 +01:00
|
|
|
llarp::LogError("async connect failed");
|
|
|
|
if(tcp->error)
|
|
|
|
tcp->error(tcp);
|
2018-11-01 13:47:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 14:06:30 +02:00
|
|
|
bool
|
2018-10-23 13:29:37 +02:00
|
|
|
llarp_tcp_serve(struct llarp_ev_loop *loop, struct llarp_tcp_acceptor *tcp,
|
|
|
|
const struct sockaddr *bindaddr)
|
2018-10-09 14:06:30 +02:00
|
|
|
{
|
2018-10-24 20:02:42 +02:00
|
|
|
tcp->loop = loop;
|
|
|
|
llarp::ev_io *impl = loop->bind_tcp(tcp, bindaddr);
|
|
|
|
if(impl)
|
|
|
|
{
|
2018-11-02 13:35:20 +01:00
|
|
|
return loop->add_ev(impl, false);
|
2018-10-24 20:02:42 +02:00
|
|
|
}
|
2018-10-09 14:06:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-19 13:41:36 +02:00
|
|
|
void
|
|
|
|
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor *tcp)
|
|
|
|
{
|
2018-10-24 20:02:42 +02:00
|
|
|
llarp::ev_io *impl = static_cast< llarp::ev_io * >(tcp->user);
|
|
|
|
tcp->impl = nullptr;
|
|
|
|
tcp->loop->close_ev(impl);
|
|
|
|
if(tcp->closed)
|
|
|
|
tcp->closed(tcp);
|
|
|
|
// dont free acceptor because it may be stack allocated
|
|
|
|
}
|
|
|
|
|
2018-10-09 14:06:30 +02:00
|
|
|
void
|
|
|
|
llarp_tcp_conn_close(struct llarp_tcp_conn *conn)
|
|
|
|
{
|
2018-10-25 14:39:32 +02:00
|
|
|
static_cast< llarp::tcp_conn * >(conn->impl)->_shouldClose = true;
|
2018-10-09 14:06:30 +02:00
|
|
|
}
|
2018-10-25 14:00:29 +02:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-10-25 14:39:32 +02:00
|
|
|
bool
|
|
|
|
tcp_conn::tick()
|
|
|
|
{
|
|
|
|
if(_shouldClose)
|
|
|
|
{
|
2018-11-01 13:47:14 +01:00
|
|
|
if(tcp.closed)
|
|
|
|
tcp.closed(&tcp);
|
2018-10-25 14:39:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-01 13:47:14 +01:00
|
|
|
else if(tcp.tick)
|
|
|
|
tcp.tick(&tcp);
|
2018-10-25 14:39:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-06 23:48:17 +01:00
|
|
|
} // namespace llarp
|