1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/ev/ev_epoll.hpp

598 lines
12 KiB
C++
Raw Normal View History

2018-04-30 15:18:57 +02:00
#ifndef EV_EPOLL_HPP
#define EV_EPOLL_HPP
2019-01-11 02:19:36 +01:00
#include <ev/ev.hpp>
#include <net.h>
#include <net.hpp>
#include <util/buffer.h>
#include <util/buffer.hpp>
#include <util/logger.hpp>
#include <util/mem.hpp>
#include <cassert>
#include <cstdio>
#include <fcntl.h>
2018-05-28 22:51:15 +02:00
#include <signal.h>
2018-04-30 18:14:20 +02:00
#include <sys/epoll.h>
#include <sys/un.h>
2018-08-15 17:36:34 +02:00
#include <tuntap.h>
2018-04-30 18:14:20 +02:00
#include <unistd.h>
2018-04-30 15:18:57 +02:00
2018-11-06 15:06:09 +01:00
#ifdef ANDROID
/** TODO: correct this value */
#define SOCK_NONBLOCK (0)
#endif
namespace llarp
{
int
2018-12-01 15:35:11 +01:00
tcp_conn::read(byte_t* buf, size_t sz)
{
if(_shouldClose)
return -1;
ssize_t amount = ::read(fd, buf, sz);
if(amount > 0)
{
2018-11-01 13:47:14 +01:00
if(tcp.read)
2018-11-23 15:37:26 +01:00
tcp.read(&tcp, llarp::InitBuffer(buf, amount));
}
else
{
// error
_shouldClose = true;
return -1;
}
return 0;
}
2018-11-01 13:47:14 +01:00
void
tcp_conn::flush_write()
{
connected();
ev_io::flush_write();
}
ssize_t
tcp_conn::do_write(void* buf, size_t sz)
{
if(_shouldClose)
return -1;
// pretty much every UNIX system still extant, _including_ solaris
// (on both sides of the fork) can ignore SIGPIPE....except
// the other vendored systems... -rick
return ::send(fd, buf, sz, MSG_NOSIGNAL); // ignore sigpipe
}
2018-11-01 13:47:14 +01:00
void
tcp_conn::connect()
{
socklen_t slen = sizeof(sockaddr_in);
if(_addr.ss_family == AF_UNIX)
slen = sizeof(sockaddr_un);
else if(_addr.ss_family == AF_INET6)
slen = sizeof(sockaddr_in6);
int result = ::connect(fd, (const sockaddr*)&_addr, slen);
if(result == 0)
{
llarp::LogDebug("connected immedidately");
connected();
}
else if(errno == EINPROGRESS)
{
// in progress
llarp::LogDebug("connect in progress");
errno = 0;
return;
}
else if(_conn->error)
{
// wtf?
llarp::LogError("error connecting ", strerror(errno));
_conn->error(_conn);
}
}
int
2018-12-01 15:35:11 +01:00
tcp_serv::read(byte_t*, size_t)
{
int new_fd = ::accept(fd, nullptr, nullptr);
if(new_fd == -1)
{
llarp::LogError("failed to accept on ", fd, ":", strerror(errno));
return -1;
}
// build handler
2018-11-01 13:47:14 +01:00
llarp::tcp_conn* connimpl = new tcp_conn(loop, new_fd);
if(loop->add_ev(connimpl, true))
{
// call callback
if(tcp->accepted)
2018-11-01 13:47:14 +01:00
tcp->accepted(tcp, &connimpl->tcp);
return 0;
}
// cleanup error
delete connimpl;
return -1;
}
struct udp_listener : public ev_io
{
llarp_udp_io* udp;
2018-04-30 15:18:57 +02:00
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
2018-04-30 15:18:57 +02:00
~udp_listener()
{
}
2018-04-30 18:14:20 +02:00
2018-10-25 14:39:32 +02:00
bool
2018-10-19 13:41:36 +02:00
tick()
{
if(udp->tick)
udp->tick(udp);
2018-10-25 14:39:32 +02:00
return true;
2018-10-19 13:41:36 +02:00
}
int
2018-12-01 15:35:11 +01:00
read(byte_t* buf, size_t sz)
{
2018-12-01 15:35:11 +01:00
llarp_buffer_t b;
b.base = buf;
b.cur = b.base;
2018-05-23 22:37:43 +02:00
sockaddr_in6 src;
socklen_t slen = sizeof(sockaddr_in6);
sockaddr* addr = (sockaddr*)&src;
2018-12-01 15:35:11 +01:00
ssize_t ret = ::recvfrom(fd, b.base, sz, 0, addr, &slen);
if(ret < 0)
2018-04-30 15:18:57 +02:00
return -1;
if(static_cast< size_t >(ret) > sz)
2018-10-15 18:20:45 +02:00
return -1;
2018-12-01 15:35:11 +01:00
b.sz = ret;
udp->recvfrom(udp, addr, b);
return 0;
2018-04-30 15:18:57 +02:00
}
int
sendto(const sockaddr* to, const void* data, size_t sz)
{
socklen_t slen;
switch(to->sa_family)
{
case AF_INET:
slen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
slen = sizeof(struct sockaddr_in6);
break;
default:
return -1;
}
ssize_t sent = ::sendto(fd, data, sz, SOCK_NONBLOCK, to, slen);
if(sent == -1)
{
llarp::LogWarn(strerror(errno));
}
return sent;
}
};
2018-08-15 17:36:34 +02:00
struct tun : public ev_io
{
llarp_tun_io* t;
device* tunif;
2018-10-29 17:48:36 +01:00
tun(llarp_tun_io* tio, llarp_ev_loop* l)
: ev_io(-1, new LossyWriteQueue_t("tun_write_queue", l, l))
2018-08-15 17:36:34 +02:00
, t(tio)
, tunif(tuntap_init())
{
};
int
sendto(__attribute__((unused)) const sockaddr* to,
__attribute__((unused)) const void* data,
__attribute__((unused)) size_t sz)
2018-08-15 17:36:34 +02:00
{
return -1;
}
2018-10-25 14:39:32 +02:00
bool
2018-10-19 13:41:36 +02:00
tick()
{
if(t->tick)
t->tick(t);
2018-10-25 21:06:16 +02:00
flush_write();
2018-10-25 14:39:32 +02:00
return true;
2018-10-19 13:41:36 +02:00
}
void
flush_write()
{
if(t->before_write)
t->before_write(t);
ev_io::flush_write();
}
2018-08-15 17:36:34 +02:00
int
read(byte_t* buf, size_t sz)
2018-08-15 17:36:34 +02:00
{
ssize_t ret = tuntap_read(tunif, buf, sz);
2018-10-01 19:16:15 +02:00
if(ret > 0 && t->recvpkt)
2018-08-22 17:52:10 +02:00
{
2018-10-01 19:16:15 +02:00
// does not have pktinfo
2018-11-26 14:30:03 +01:00
t->recvpkt(t, llarp::InitBuffer(buf, ret));
2018-08-22 17:52:10 +02:00
}
return ret;
2018-08-15 17:36:34 +02:00
}
static int
wait_for_fd_promise(struct device* dev)
{
llarp::tun* t = static_cast< llarp::tun* >(dev->user);
if(t->t->get_fd_promise)
{
struct llarp_fd_promise* promise = t->t->get_fd_promise(t->t);
if(promise)
return llarp_fd_promise_wait_for_value(promise);
}
return -1;
}
2018-08-15 17:36:34 +02:00
bool
setup()
{
// for android
if(t->get_fd_promise)
{
tunif->obtain_fd = &wait_for_fd_promise;
tunif->user = this;
}
2018-08-22 17:52:10 +02:00
llarp::LogDebug("set ifname to ", t->ifname);
strncpy(tunif->if_name, t->ifname, sizeof(tunif->if_name));
2018-08-20 21:12:12 +02:00
if(tuntap_start(tunif, TUNTAP_MODE_TUNNEL, 0) == -1)
2018-08-22 17:52:10 +02:00
{
llarp::LogWarn("failed to start interface");
2018-08-15 17:36:34 +02:00
return false;
2018-08-22 17:52:10 +02:00
}
if(t->get_fd_promise == nullptr)
2018-08-22 17:52:10 +02:00
{
if(tuntap_up(tunif) == -1)
{
llarp::LogWarn("failed to put interface up: ", strerror(errno));
return false;
}
if(tuntap_set_ip(tunif, t->ifaddr, t->ifaddr, t->netmask) == -1)
{
llarp::LogWarn("failed to set ip");
return false;
}
2018-08-22 17:52:10 +02:00
}
2018-08-15 17:36:34 +02:00
fd = tunif->tun_fd;
if(fd == -1)
return false;
// set non blocking
int flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
return false;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1;
2018-08-15 17:36:34 +02:00
}
~tun()
{
if(tunif)
tuntap_destroy(tunif);
2018-08-15 17:36:34 +02:00
}
};
2018-04-30 18:14:20 +02:00
}; // namespace llarp
2018-04-30 15:18:57 +02:00
struct llarp_epoll_loop : public llarp_ev_loop
{
2018-04-30 15:18:57 +02:00
int epollfd;
llarp_epoll_loop() : epollfd(-1)
{
}
2018-04-30 18:14:20 +02:00
bool
2018-11-01 13:47:14 +01:00
tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr)
{
// create socket
int fd = ::socket(remoteaddr->sa_family, SOCK_STREAM, 0);
if(fd == -1)
return false;
2018-11-01 13:47:14 +01:00
// set non blocking
int flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
{
::close(fd);
return false;
2018-11-01 13:47:14 +01:00
}
if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
{
::close(fd);
return false;
2018-11-01 13:47:14 +01:00
}
llarp::tcp_conn* conn = new llarp::tcp_conn(this, fd, remoteaddr, tcp);
add_ev(conn, true);
2018-11-01 13:47:14 +01:00
conn->connect();
return true;
2018-11-01 13:47:14 +01:00
}
llarp::ev_io*
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr)
{
int fd = ::socket(bindaddr->sa_family, SOCK_STREAM, 0);
if(fd == -1)
return nullptr;
socklen_t sz = sizeof(sockaddr_in);
if(bindaddr->sa_family == AF_INET6)
{
sz = sizeof(sockaddr_in6);
}
else if(bindaddr->sa_family == AF_UNIX)
{
sz = sizeof(sockaddr_un);
}
if(::bind(fd, bindaddr, sz) == -1)
{
::close(fd);
return nullptr;
}
if(::listen(fd, 5) == -1)
{
::close(fd);
return nullptr;
}
2018-11-01 13:47:14 +01:00
return new llarp::tcp_serv(this, fd, tcp);
}
virtual bool
udp_listen(llarp_udp_io* l, const sockaddr* src)
{
auto ev = create_udp(l, src);
if(ev)
l->fd = ev->fd;
return ev && add_ev(ev, false);
}
~llarp_epoll_loop()
{
2018-05-28 22:51:15 +02:00
if(epollfd != -1)
close(epollfd);
}
2018-04-30 15:18:57 +02:00
2018-08-10 05:51:38 +02:00
bool
running() const
{
return epollfd != -1;
}
bool
init()
{
if(epollfd == -1)
2018-07-24 03:44:01 +02:00
epollfd = epoll_create(1);
2018-05-28 22:51:15 +02:00
return false;
2018-04-30 15:18:57 +02:00
}
2018-06-06 14:46:26 +02:00
int
2018-06-06 23:23:57 +02:00
tick(int ms)
2018-06-06 14:46:26 +02:00
{
epoll_event events[1024];
int result;
2018-06-06 23:23:57 +02:00
result = epoll_wait(epollfd, events, 1024, ms);
2018-06-06 14:46:26 +02:00
if(result > 0)
{
int idx = 0;
while(idx < result)
{
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].data.ptr);
if(ev)
2018-06-06 14:46:26 +02:00
{
llarp::LogDebug(idx, " of ", result,
" events=", std::to_string(events[idx].events));
2018-11-01 13:47:14 +01:00
if(events[idx].events & EPOLLERR)
{
2018-11-01 13:47:14 +01:00
ev->error();
}
2018-11-01 13:47:14 +01:00
else
2018-10-25 14:39:32 +02:00
{
2018-11-01 13:47:14 +01:00
if(events[idx].events & EPOLLIN)
{
ev->read(readbuf, sizeof(readbuf));
}
if(events[idx].events & EPOLLOUT)
{
ev->flush_write();
}
2018-10-25 14:39:32 +02:00
}
2018-08-15 17:36:34 +02:00
}
2018-06-06 14:46:26 +02:00
++idx;
}
}
2018-09-10 15:43:36 +02:00
if(result != -1)
tick_listeners();
2018-06-06 14:46:26 +02:00
return result;
}
int
run()
{
2018-04-30 15:18:57 +02:00
epoll_event events[1024];
int result;
do
{
2018-08-10 05:51:38 +02:00
result = epoll_wait(epollfd, events, 1024, EV_TICK_INTERVAL);
if(result > 0)
{
2018-04-30 15:18:57 +02:00
int idx = 0;
while(idx < result)
{
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].data.ptr);
if(ev)
{
2018-11-01 13:47:14 +01:00
if(events[idx].events & EPOLLERR)
{
2018-11-01 13:47:14 +01:00
ev->error();
}
2018-11-01 13:47:14 +01:00
else
2018-10-25 14:39:32 +02:00
{
2018-11-01 13:47:14 +01:00
if(events[idx].events & EPOLLIN)
{
ev->read(readbuf, sizeof(readbuf));
}
if(events[idx].events & EPOLLOUT)
{
ev->flush_write();
}
2018-10-25 14:39:32 +02:00
}
2018-08-15 17:36:34 +02:00
}
2018-04-30 15:18:57 +02:00
++idx;
}
}
2018-09-10 15:43:36 +02:00
if(result != -1)
tick_listeners();
2018-05-28 22:51:15 +02:00
} while(epollfd != -1);
2018-04-30 15:18:57 +02:00
return result;
}
int
udp_bind(const sockaddr* addr)
{
2018-04-30 15:18:57 +02:00
socklen_t slen;
switch(addr->sa_family)
{
2018-04-30 18:14:20 +02:00
case AF_INET:
slen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
slen = sizeof(struct sockaddr_in6);
break;
default:
return -1;
2018-04-30 15:18:57 +02:00
}
int fd = socket(addr->sa_family, SOCK_DGRAM, 0);
if(fd == -1)
2018-05-23 15:49:00 +02:00
{
perror("socket()");
return -1;
2018-05-23 15:49:00 +02:00
}
if(addr->sa_family == AF_INET6)
{
// enable dual stack explicitly
int dual = 1;
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &dual, sizeof(dual)) == -1)
{
// failed
perror("setsockopt()");
close(fd);
return -1;
}
}
llarp::Addr a(*addr);
llarp::LogDebug("bind to ", a);
if(bind(fd, addr, slen) == -1)
{
2018-05-23 15:49:00 +02:00
perror("bind()");
2018-04-30 15:18:57 +02:00
close(fd);
return -1;
}
2018-05-23 15:49:00 +02:00
2018-04-30 15:18:57 +02:00
return fd;
}
bool
close_ev(llarp::ev_io* ev)
2018-05-16 18:41:20 +02:00
{
2018-10-25 14:39:32 +02:00
return epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, nullptr) != -1;
2018-05-16 18:41:20 +02:00
}
2018-08-15 17:36:34 +02:00
llarp::ev_io*
create_tun(llarp_tun_io* tun)
{
2018-10-29 17:48:36 +01:00
llarp::tun* t = new llarp::tun(tun, this);
if(tun->get_fd_promise)
{
}
else if(t->setup())
2018-10-19 13:41:36 +02:00
{
2018-08-15 17:36:34 +02:00
return t;
2018-10-19 13:41:36 +02:00
}
2018-08-15 17:36:34 +02:00
delete t;
return nullptr;
}
llarp::ev_io*
create_udp(llarp_udp_io* l, const sockaddr* src)
{
2018-05-23 15:49:00 +02:00
int fd = udp_bind(src);
if(fd == -1)
2018-08-15 17:36:34 +02:00
return nullptr;
2018-10-21 19:07:17 +02:00
llarp::ev_io* listener = new llarp::udp_listener(fd, l);
2018-10-19 13:41:36 +02:00
l->impl = listener;
2018-08-15 17:36:34 +02:00
return listener;
}
bool
2018-08-16 17:31:32 +02:00
add_ev(llarp::ev_io* e, bool write)
2018-08-15 17:36:34 +02:00
{
2018-04-30 15:18:57 +02:00
epoll_event ev;
2018-08-15 17:36:34 +02:00
ev.data.ptr = e;
2018-11-01 13:47:14 +01:00
ev.events = EPOLLIN | EPOLLERR;
if(write)
ev.events |= EPOLLOUT;
2018-08-15 17:36:34 +02:00
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, e->fd, &ev) == -1)
{
2018-08-15 17:36:34 +02:00
delete e;
2018-04-30 15:18:57 +02:00
return false;
}
2018-10-21 19:07:17 +02:00
handlers.emplace_back(e);
2018-04-30 15:18:57 +02:00
return true;
}
2018-04-30 18:14:20 +02:00
bool
udp_close(llarp_udp_io* l)
2018-05-16 18:41:20 +02:00
{
2018-05-28 22:58:10 +02:00
bool ret = false;
llarp::udp_listener* listener =
static_cast< llarp::udp_listener* >(l->impl);
2018-05-22 21:19:06 +02:00
if(listener)
{
2018-05-28 22:58:10 +02:00
close_ev(listener);
2018-10-25 14:39:32 +02:00
// remove handler
auto itr = handlers.begin();
while(itr != handlers.end())
{
if(itr->get() == listener)
itr = handlers.erase(itr);
else
++itr;
}
2018-05-22 21:19:06 +02:00
l->impl = nullptr;
2018-10-19 13:41:36 +02:00
ret = true;
2018-05-22 21:19:06 +02:00
}
return ret;
2018-05-16 18:41:20 +02:00
}
void
stop()
{
// close all handlers before closing the epoll fd
auto itr = handlers.begin();
2018-12-03 02:55:59 +01:00
while(itr != handlers.end())
{
close_ev(itr->get());
itr = handlers.erase(itr);
}
2018-09-10 15:43:36 +02:00
if(epollfd != -1)
close(epollfd);
epollfd = -1;
2018-04-30 15:18:57 +02:00
}
};
#endif