2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.h>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.hpp>
|
2020-05-06 20:38:44 +00:00
|
|
|
#include <util/str.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2018-11-06 22:48:17 +00:00
|
|
|
|
2019-06-17 23:19:39 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <string_view>
|
2018-11-06 22:48:17 +00:00
|
|
|
|
2019-06-14 01:36:31 +00:00
|
|
|
// We libuv now
|
|
|
|
#include <ev/ev_libuv.hpp>
|
2019-12-11 21:05:40 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
2019-03-25 18:37:11 +00:00
|
|
|
#define SHUT_RDWR SD_BOTH
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev_win32.hpp>
|
2018-04-30 13:18:57 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-08 12:01:52 +00:00
|
|
|
llarp_ev_loop_ptr
|
2020-06-01 13:17:44 +00:00
|
|
|
llarp_make_ev_loop(size_t queueLength)
|
2019-02-11 14:43:48 +00:00
|
|
|
{
|
2020-06-01 13:17:44 +00:00
|
|
|
llarp_ev_loop_ptr r = std::make_shared<libuv::Loop>(queueLength);
|
2019-02-11 14:43:48 +00:00
|
|
|
r->init();
|
2019-03-15 14:31:54 +00:00
|
|
|
r->update_time();
|
2019-02-11 14:43:48 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev, std::shared_ptr<llarp::Logic> logic)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2020-05-21 14:20:47 +00:00
|
|
|
ev->run();
|
2019-11-23 04:47:08 +00:00
|
|
|
logic->clear_event_loop();
|
2019-06-02 21:17:05 +00:00
|
|
|
ev->stopped();
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp_ev_add_udp(struct llarp_ev_loop* ev, struct llarp_udp_io* udp, const llarp::SockAddr& src)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-16 16:41:20 +00:00
|
|
|
udp->parent = ev;
|
2020-04-07 18:38:56 +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
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_close_udp(struct llarp_udp_io* udp)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (udp->parent->udp_close(udp))
|
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-29 14:19:00 +00:00
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_time_now_ms(const llarp_ev_loop_ptr& loop)
|
2018-10-29 16:48:36 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (loop)
|
2019-03-15 14:31:54 +00:00
|
|
|
return loop->time_now();
|
2018-11-20 12:44:18 +00:00
|
|
|
return llarp::time_now_ms();
|
2018-10-29 16:48:36 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_stop(const llarp_ev_loop_ptr& 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
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp_ev_udp_sendto(struct llarp_udp_io* udp, const llarp::SockAddr& to, const llarp_buffer_t& buf)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-06-02 21:17:05 +00:00
|
|
|
return udp->sendto(udp, to, buf.base, buf.sz);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_add_tun(struct llarp_ev_loop* loop, struct llarp_tun_io* tun)
|
2018-08-15 15:36:34 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tun->ifaddr[0] == 0 || strcmp(tun->ifaddr, "auto") == 0)
|
2019-01-16 21:03:45 +00:00
|
|
|
{
|
2019-07-08 15:26:06 +00:00
|
|
|
LogError("invalid ifaddr on tun: ", tun->ifaddr);
|
|
|
|
return false;
|
2019-01-16 21:03:45 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tun->ifname[0] == 0 || strcmp(tun->ifname, "auto") == 0)
|
2019-01-16 21:03:45 +00:00
|
|
|
{
|
2019-07-08 15:26:06 +00:00
|
|
|
LogError("invalid ifname on tun: ", tun->ifname);
|
|
|
|
return false;
|
2019-01-16 21:03:45 +00:00
|
|
|
}
|
2019-09-05 01:25:50 +00:00
|
|
|
#if !defined(_WIN32)
|
2019-06-02 21:17:05 +00:00
|
|
|
return loop->tun_listen(tun);
|
2018-12-14 12:04:14 +00:00
|
|
|
#else
|
|
|
|
UNREFERENCED_PARAMETER(loop);
|
2020-04-07 18:38:56 +00:00
|
|
|
auto dev = new win32_tun_io(tun);
|
2018-12-14 12:04:14 +00:00
|
|
|
tun->impl = dev;
|
|
|
|
// We're not even going to add this to the socket event loop
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dev)
|
2018-12-14 12:04:14 +00:00
|
|
|
{
|
|
|
|
dev->setup();
|
2019-12-11 21:18:47 +00:00
|
|
|
return dev->add_ev(loop); // start up tun and add to event queue
|
2018-12-14 12:04:14 +00:00
|
|
|
}
|
2019-05-06 12:17:34 +00:00
|
|
|
llarp::LogWarn("Loop could not create tun");
|
2018-12-14 12:04:14 +00:00
|
|
|
return false;
|
2019-06-11 16:44:05 +00:00
|
|
|
#endif
|
2018-12-14 12:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_tun_async_write(struct llarp_tun_io* tun, const llarp_buffer_t& buf)
|
2018-12-14 12:04:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf.sz > EV_WRITE_BUF_SZ)
|
2018-12-14 12:04:14 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("packet too big, ", buf.sz, " > ", EV_WRITE_BUF_SZ);
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-20 12:19:33 +00:00
|
|
|
#ifndef _WIN32
|
2019-06-03 12:02:54 +00:00
|
|
|
return tun->writepkt(tun, buf.base, buf.sz);
|
2018-12-14 12:04:14 +00:00
|
|
|
#else
|
2020-04-07 18:38:56 +00:00
|
|
|
return static_cast<win32_tun_io*>(tun->impl)->queue_write(buf.base, buf.sz);
|
2018-12-14 12:04:14 +00:00
|
|
|
#endif
|
2019-05-06 12:17:34 +00:00
|
|
|
}
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_conn_async_write(struct llarp_tcp_conn* conn, const llarp_buffer_t& b)
|
2018-08-15 15:36:34 +00:00
|
|
|
{
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer buf{b};
|
2019-06-03 12:02:54 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
size_t sz = buf.underlying.sz;
|
2019-02-02 23:12:42 +00:00
|
|
|
buf.underlying.cur = buf.underlying.base;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (sz > EV_WRITE_BUF_SZ)
|
2018-10-25 12:09:29 +00:00
|
|
|
{
|
2019-06-03 12:02:54 +00:00
|
|
|
ssize_t amount = conn->write(conn, buf.underlying.cur, EV_WRITE_BUF_SZ);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (amount <= 0)
|
2019-06-03 14:03:59 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("write underrun");
|
2020-01-07 11:59:17 +00:00
|
|
|
llarp_tcp_conn_close(conn);
|
2018-10-25 12:09:29 +00:00
|
|
|
return false;
|
2019-06-03 14:03:59 +00:00
|
|
|
}
|
2019-06-03 12:02:54 +00:00
|
|
|
buf.underlying.cur += amount;
|
|
|
|
sz -= amount;
|
2018-10-25 12:09:29 +00:00
|
|
|
}
|
2019-06-03 14:03:59 +00:00
|
|
|
return conn->write(conn, buf.underlying.cur, sz) > 0;
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_async_try_connect(struct llarp_ev_loop* loop, struct llarp_tcp_connecter* tcp)
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
tcp->loop = loop;
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp::IpAddress address(tcp->remote);
|
|
|
|
|
|
|
|
if (not address.getPort())
|
|
|
|
throw std::runtime_error(llarp::stringify("Address with no port: ", address));
|
|
|
|
|
|
|
|
llarp::SockAddr addr = address.createSockAddr();
|
2018-11-01 12:47:14 +00:00
|
|
|
|
2020-04-07 18:38:56 +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");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tcp->error)
|
2018-11-02 12:35:20 +00:00
|
|
|
tcp->error(tcp);
|
2018-11-01 12:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_serve(
|
2020-05-08 17:23:21 +00:00
|
|
|
struct llarp_ev_loop* loop, struct llarp_tcp_acceptor* tcp, const llarp::SockAddr& bindaddr)
|
2018-10-09 12:06:30 +00:00
|
|
|
{
|
2019-06-02 21:17:05 +00:00
|
|
|
tcp->loop = loop;
|
|
|
|
return loop->tcp_listen(tcp, bindaddr);
|
2018-10-09 12:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:41:36 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor* tcp)
|
2018-10-19 11:41:36 +00:00
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
tcp->close(tcp);
|
2018-10-24 18:02:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_conn_close(struct llarp_tcp_conn* conn)
|
2018-10-09 12:06:30 +00:00
|
|
|
{
|
2019-06-02 21:17:05 +00:00
|
|
|
conn->close(conn);
|
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()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_shouldClose)
|
2018-10-25 12:39:32 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tcp.closed)
|
2018-11-01 12:47:14 +00:00
|
|
|
tcp.closed(&tcp);
|
2019-03-25 15:41:37 +00:00
|
|
|
::shutdown(fd, SHUT_RDWR);
|
2018-10-25 12:39:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tcp.tick)
|
2018-11-01 12:47:14 +00:00
|
|
|
tcp.tick(&tcp);
|
2018-10-25 12:39:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:48:17 +00:00
|
|
|
} // namespace llarp
|