2018-01-25 16:24:33 +00:00
|
|
|
#include <llarp/ev.h>
|
2018-06-06 12:46:26 +00:00
|
|
|
#include <llarp/logic.h>
|
2018-11-01 12:47:14 +00:00
|
|
|
#include <llarp/string_view.hpp>
|
2018-11-06 22:48:17 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2018-02-01 13:21:00 +00:00
|
|
|
#include "mem.hpp"
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
#define EV_TICK_INTERVAL 100
|
|
|
|
|
2018-08-15 02:42:33 +00:00
|
|
|
// apparently current Solaris will emulate epoll.
|
2018-08-16 22:36:15 +00:00
|
|
|
#if __linux__ || __sun__
|
2018-06-06 12:46:26 +00:00
|
|
|
#include "ev_epoll.hpp"
|
2018-10-29 23:56:05 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|
2018-08-08 17:43:46 +00:00
|
|
|
|| (__APPLE__ && __MACH__)
|
2018-06-06 12:46:26 +00:00
|
|
|
#include "ev_kqueue.hpp"
|
2018-10-29 23:56:05 +00:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
2018-07-30 04:38:14 +00:00
|
|
|
#include "ev_win32.hpp"
|
2018-10-29 23:56:05 +00:00
|
|
|
#else
|
|
|
|
#error No async event loop for your platform, subclass llarp_ev_loop
|
2018-04-30 13:18:57 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_alloc(struct llarp_ev_loop **ev)
|
|
|
|
{
|
2018-08-16 22:36:15 +00:00
|
|
|
#if __linux__ || __sun__
|
2018-04-30 13:18:57 +00:00
|
|
|
*ev = new llarp_epoll_loop;
|
2018-10-29 23:56:05 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|
2018-08-08 17:43:46 +00:00
|
|
|
|| (__APPLE__ && __MACH__)
|
2018-05-29 12:14:50 +00:00
|
|
|
*ev = new llarp_kqueue_loop;
|
2018-10-29 23:56:05 +00:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
2018-07-30 04:38:14 +00:00
|
|
|
*ev = new llarp_win32_loop;
|
2018-10-29 23:56:05 +00:00
|
|
|
#else
|
|
|
|
#error no event loop subclass
|
2018-04-30 13:18:57 +00:00
|
|
|
#endif
|
2018-05-18 13:17:58 +00:00
|
|
|
(*ev)->init();
|
2018-10-29 16:48:36 +00:00
|
|
|
(*ev)->_now = llarp_time_now_ms();
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_free(struct llarp_ev_loop **ev)
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
delete *ev;
|
2018-01-29 14:27:24 +00:00
|
|
|
*ev = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2018-07-29 22:20:31 +00:00
|
|
|
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-08-29 20:40:26 +00:00
|
|
|
while(ev->running())
|
2018-07-29 22:20:31 +00:00
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
ev->_now = llarp_time_now_ms();
|
2018-09-21 12:30:57 +00:00
|
|
|
ev->tick(EV_TICK_INTERVAL);
|
|
|
|
if(ev->running())
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_logic_tick(logic, ev->_now);
|
2018-07-29 22:20:31 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,
|
|
|
|
struct llarp_threadpool *tp,
|
|
|
|
struct llarp_logic *logic)
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
while(ev->running())
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
ev->_now = llarp_time_now_ms();
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->tick(EV_TICK_INTERVAL);
|
2018-09-21 12:30:57 +00:00
|
|
|
if(ev->running())
|
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_logic_tick_async(logic, ev->_now);
|
2018-09-21 12:30:57 +00:00
|
|
|
llarp_threadpool_tick(tp);
|
|
|
|
}
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2018-05-23 13:49:00 +00:00
|
|
|
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
|
|
|
|
const struct sockaddr *src)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-16 16:41:20 +00:00
|
|
|
udp->parent = ev;
|
2018-05-23 13:49:00 +00:00
|
|
|
if(ev->udp_listen(udp, src))
|
2018-05-22 15:54:19 +00:00
|
|
|
return 0;
|
2018-05-16 16:41:20 +00:00
|
|
|
return -1;
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2018-05-22 19:19:06 +00:00
|
|
|
llarp_ev_close_udp(struct llarp_udp_io *udp)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
if(udp->parent->udp_close(udp))
|
|
|
|
return 0;
|
2018-05-16 16:41:20 +00:00
|
|
|
return -1;
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
|
|
|
llarp_ev_loop_time_now_ms(struct llarp_ev_loop *loop)
|
|
|
|
{
|
|
|
|
return loop->_now;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_stop(struct llarp_ev_loop *loop)
|
2018-05-18 17:10:48 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
loop->stop();
|
2018-05-18 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
|
|
|
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
|
|
|
|
const void *buf, size_t sz)
|
|
|
|
{
|
2018-08-02 04:34:46 +00:00
|
|
|
auto ret = static_cast< llarp::ev_io * >(udp->impl)->sendto(to, buf, sz);
|
2018-10-26 03:56:48 +00:00
|
|
|
if(ret == -1 && errno != 0)
|
2018-08-02 04:34:46 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("sendto failed ", strerror(errno));
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_ev_add_tun(struct llarp_ev_loop *loop, struct llarp_tun_io *tun)
|
|
|
|
{
|
2018-08-21 18:17:16 +00:00
|
|
|
auto dev = loop->create_tun(tun);
|
|
|
|
tun->impl = dev;
|
2018-08-20 19:12:12 +00:00
|
|
|
if(dev)
|
2018-08-21 18:17:16 +00:00
|
|
|
{
|
2018-11-02 12:35:20 +00:00
|
|
|
return loop->add_ev(dev, false);
|
2018-08-21 18:17:16 +00:00
|
|
|
}
|
2018-08-20 19:12:12 +00:00
|
|
|
return false;
|
2018-08-15 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-10-25 12:09:29 +00:00
|
|
|
llarp_tcp_conn_async_write(struct llarp_tcp_conn *conn, const void *pkt,
|
|
|
|
size_t sz)
|
2018-08-15 15:36:34 +00:00
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
const byte_t *ptr = (const byte_t *)pkt;
|
|
|
|
llarp::tcp_conn *impl = static_cast< llarp::tcp_conn * >(conn->impl);
|
2018-10-25 12:39:32 +00:00
|
|
|
if(impl->_shouldClose)
|
2018-11-02 12:35:20 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("write on closed connection");
|
2018-10-25 12:39:32 +00:00
|
|
|
return false;
|
2018-11-02 12:35:20 +00:00
|
|
|
}
|
2018-10-27 18:26:08 +00:00
|
|
|
while(sz > EV_WRITE_BUF_SZ)
|
2018-10-25 12:09:29 +00:00
|
|
|
{
|
2018-10-27 18:26:08 +00:00
|
|
|
if(!impl->queue_write((const byte_t *)ptr, EV_WRITE_BUF_SZ))
|
2018-10-25 12:09:29 +00:00
|
|
|
return false;
|
2018-10-27 18:26:08 +00:00
|
|
|
ptr += EV_WRITE_BUF_SZ;
|
|
|
|
sz -= EV_WRITE_BUF_SZ;
|
2018-10-25 12:09:29 +00:00
|
|
|
}
|
|
|
|
return impl->queue_write(ptr, sz);
|
2018-08-16 22:36:15 +00:00
|
|
|
}
|
2018-10-09 12:06:30 +00:00
|
|
|
|
2018-11-01 12:47:14 +00: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 12:31:50 +00:00
|
|
|
while(*end && ((end - begin) < static_cast< ptrdiff_t >(sizeof tcp->remote)))
|
2018-11-01 12:47:14 +00: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 12:35:20 +00:00
|
|
|
if(!loop->tcp_connect(tcp, addr))
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
2018-11-02 12:35:20 +00:00
|
|
|
llarp::LogError("async connect failed");
|
|
|
|
if(tcp->error)
|
|
|
|
tcp->error(tcp);
|
2018-11-01 12:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
bool
|
2018-10-23 11:29:37 +00:00
|
|
|
llarp_tcp_serve(struct llarp_ev_loop *loop, struct llarp_tcp_acceptor *tcp,
|
|
|
|
const struct sockaddr *bindaddr)
|
2018-10-09 12:06:30 +00:00
|
|
|
{
|
2018-10-24 18:02:42 +00:00
|
|
|
tcp->loop = loop;
|
|
|
|
llarp::ev_io *impl = loop->bind_tcp(tcp, bindaddr);
|
|
|
|
if(impl)
|
|
|
|
{
|
2018-11-02 12:35:20 +00:00
|
|
|
return loop->add_ev(impl, false);
|
2018-10-24 18:02:42 +00:00
|
|
|
}
|
2018-10-09 12:06:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:41:36 +00:00
|
|
|
void
|
|
|
|
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor *tcp)
|
|
|
|
{
|
2018-10-24 18:02:42 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-10-25 12:09:29 +00:00
|
|
|
llarp_ev_tun_async_write(struct llarp_tun_io *tun, const void *buf, size_t sz)
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
2018-10-27 18:26:08 +00:00
|
|
|
if(sz > EV_WRITE_BUF_SZ)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("packet too big, ", sz, " > ", EV_WRITE_BUF_SZ);
|
2018-10-25 12:09:29 +00:00
|
|
|
return false;
|
2018-10-27 18:26:08 +00:00
|
|
|
}
|
2018-10-27 19:03:23 +00:00
|
|
|
return static_cast< llarp::tun * >(tun->impl)->queue_write(
|
2018-10-25 12:09:29 +00:00
|
|
|
(const byte_t *)buf, sz);
|
2018-10-19 11:41:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
void
|
|
|
|
llarp_tcp_conn_close(struct llarp_tcp_conn *conn)
|
|
|
|
{
|
2018-10-25 12:39:32 +00:00
|
|
|
static_cast< llarp::tcp_conn * >(conn->impl)->_shouldClose = true;
|
2018-10-09 12:06:30 +00:00
|
|
|
}
|
2018-10-25 12:00:29 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-10-25 12:39:32 +00:00
|
|
|
bool
|
|
|
|
tcp_conn::tick()
|
|
|
|
{
|
|
|
|
if(_shouldClose)
|
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
if(tcp.closed)
|
|
|
|
tcp.closed(&tcp);
|
2018-10-25 12:39:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-01 12:47:14 +00:00
|
|
|
else if(tcp.tick)
|
|
|
|
tcp.tick(&tcp);
|
2018-10-25 12:39:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:48:17 +00:00
|
|
|
} // namespace llarp
|