2018-04-30 13:18:57 +00:00
|
|
|
#ifndef EV_EPOLL_HPP
|
|
|
|
#define EV_EPOLL_HPP
|
2018-08-21 18:17:16 +00:00
|
|
|
#include <fcntl.h>
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <llarp/buffer.h>
|
2018-05-18 13:17:58 +00:00
|
|
|
#include <llarp/net.h>
|
2018-05-28 20:51:15 +00:00
|
|
|
#include <signal.h>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include <sys/epoll.h>
|
2018-08-15 15:36:34 +00:00
|
|
|
#include <tuntap.h>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include <unistd.h>
|
2018-05-18 13:17:58 +00:00
|
|
|
#include <cstdio>
|
2018-08-22 15:52:10 +00:00
|
|
|
#include "buffer.hpp"
|
2018-04-30 16:14:20 +00:00
|
|
|
#include "ev.hpp"
|
2018-07-27 00:21:57 +00:00
|
|
|
#include "llarp/net.hpp"
|
2018-05-27 13:42:55 +00:00
|
|
|
#include "logger.hpp"
|
2018-08-22 15:52:10 +00:00
|
|
|
#include "mem.hpp"
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct udp_listener : public ev_io
|
|
|
|
{
|
|
|
|
llarp_udp_io* udp;
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
~udp_listener()
|
|
|
|
{
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
virtual int
|
|
|
|
read(void* buf, size_t sz)
|
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
sockaddr_in6 src;
|
|
|
|
socklen_t slen = sizeof(sockaddr_in6);
|
|
|
|
sockaddr* addr = (sockaddr*)&src;
|
|
|
|
int ret = ::recvfrom(fd, buf, sz, 0, addr, &slen);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(ret == -1)
|
2018-04-30 13:18:57 +00:00
|
|
|
return -1;
|
2018-05-23 20:37:43 +00:00
|
|
|
udp->recvfrom(udp, addr, buf, ret);
|
2018-05-22 15:54:19 +00:00
|
|
|
return 0;
|
2018-04-30 13:18:57 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
virtual 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)
|
2018-06-01 17:47:37 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn(strerror(errno));
|
2018-06-01 17:47:37 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
};
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
struct tun : public ev_io
|
|
|
|
{
|
|
|
|
llarp_tun_io* t;
|
|
|
|
device* tunif;
|
|
|
|
tun(llarp_tun_io* tio)
|
|
|
|
: ev_io(-1)
|
|
|
|
, t(tio)
|
|
|
|
, tunif(tuntap_init())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
void
|
|
|
|
flush_write()
|
|
|
|
{
|
|
|
|
if(t->before_write)
|
|
|
|
{
|
|
|
|
t->before_write(t);
|
|
|
|
}
|
2018-08-21 18:17:16 +00:00
|
|
|
ev_io::flush_write();
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
int
|
|
|
|
read(void* buf, size_t sz)
|
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
ssize_t ret = tuntap_read(tunif, buf, sz);
|
2018-10-01 17:16:15 +00:00
|
|
|
if(ret > 0 && t->recvpkt)
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2018-10-01 17:16:15 +00:00
|
|
|
// does not have pktinfo
|
|
|
|
t->recvpkt(t, buf, ret);
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
return ret;
|
2018-08-15 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
setup()
|
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
llarp::LogDebug("set ifname to ", t->ifname);
|
|
|
|
strncpy(tunif->if_name, t->ifname, sizeof(tunif->if_name));
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
if(tuntap_start(tunif, TUNTAP_MODE_TUNNEL, 0) == -1)
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to start interface");
|
2018-08-15 15:36:34 +00:00
|
|
|
return false;
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
|
|
|
if(tuntap_up(tunif) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to put interface up: ", strerror(errno));
|
2018-08-15 15:36:34 +00:00
|
|
|
return false;
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2018-09-20 22:38:45 +00:00
|
|
|
if(tuntap_set_ip(tunif, t->ifaddr, t->ifaddr, t->netmask) == -1)
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to set ip");
|
2018-08-15 15:36:34 +00:00
|
|
|
return false;
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2018-08-15 15:36:34 +00:00
|
|
|
fd = tunif->tun_fd;
|
2018-08-21 18:17:16 +00:00
|
|
|
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 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~tun()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2018-04-30 16:14:20 +00:00
|
|
|
}; // namespace llarp
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_epoll_loop : public llarp_ev_loop
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
int epollfd;
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_epoll_loop() : epollfd(-1)
|
|
|
|
{
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
~llarp_epoll_loop()
|
|
|
|
{
|
2018-05-28 20:51:15 +00:00
|
|
|
if(epollfd != -1)
|
|
|
|
close(epollfd);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
bool
|
|
|
|
running() const
|
|
|
|
{
|
|
|
|
return epollfd != -1;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
init()
|
|
|
|
{
|
|
|
|
if(epollfd == -1)
|
2018-07-24 01:44:01 +00:00
|
|
|
epollfd = epoll_create(1);
|
2018-05-28 20:51:15 +00:00
|
|
|
return false;
|
2018-04-30 13:18:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
int
|
2018-06-06 21:23:57 +00:00
|
|
|
tick(int ms)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
|
|
|
epoll_event events[1024];
|
|
|
|
int result;
|
2018-06-06 21:23:57 +00:00
|
|
|
result = epoll_wait(epollfd, events, 1024, ms);
|
2018-06-06 12:46:26 +00:00
|
|
|
if(result > 0)
|
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
while(idx < result)
|
|
|
|
{
|
|
|
|
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].data.ptr);
|
|
|
|
if(events[idx].events & EPOLLIN)
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->read(readbuf, sizeof(readbuf));
|
2018-08-15 15:36:34 +00:00
|
|
|
}
|
2018-06-06 12:46:26 +00:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 13:43:36 +00:00
|
|
|
if(result != -1)
|
|
|
|
tick_listeners();
|
2018-06-06 12:46:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
|
|
|
run()
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
epoll_event events[1024];
|
|
|
|
int result;
|
2018-05-22 15:54:19 +00:00
|
|
|
do
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
result = epoll_wait(epollfd, events, 1024, EV_TICK_INTERVAL);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(result > 0)
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
int idx = 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
while(idx < result)
|
|
|
|
{
|
|
|
|
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].data.ptr);
|
|
|
|
if(events[idx].events & EPOLLIN)
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->read(readbuf, sizeof(readbuf));
|
2018-08-15 15:36:34 +00:00
|
|
|
}
|
2018-04-30 13:18:57 +00:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 13:43:36 +00:00
|
|
|
if(result != -1)
|
|
|
|
tick_listeners();
|
2018-05-28 20:51:15 +00:00
|
|
|
} while(epollfd != -1);
|
2018-04-30 13:18:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
|
|
|
udp_bind(const sockaddr* addr)
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
socklen_t slen;
|
2018-05-22 15:54:19 +00:00
|
|
|
switch(addr->sa_family)
|
|
|
|
{
|
2018-04-30 16:14:20 +00: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 13:18:57 +00:00
|
|
|
}
|
|
|
|
int fd = socket(addr->sa_family, SOCK_DGRAM, 0);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(fd == -1)
|
2018-05-23 13:49:00 +00:00
|
|
|
{
|
|
|
|
perror("socket()");
|
2018-05-22 15:54:19 +00:00
|
|
|
return -1;
|
2018-05-23 13:49:00 +00: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);
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("bind to ", a);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(bind(fd, addr, slen) == -1)
|
|
|
|
{
|
2018-05-23 13:49:00 +00:00
|
|
|
perror("bind()");
|
2018-04-30 13:18:57 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-23 13:49:00 +00:00
|
|
|
|
2018-04-30 13:18:57 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
close_ev(llarp::ev_io* ev)
|
2018-05-16 16:41:20 +00:00
|
|
|
{
|
|
|
|
return epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, nullptr) != -1;
|
|
|
|
}
|
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
create_tun(llarp_tun_io* tun)
|
|
|
|
{
|
|
|
|
llarp::tun* t = new llarp::tun(tun);
|
|
|
|
if(t->setup())
|
|
|
|
return t;
|
|
|
|
delete t;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::ev_io*
|
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-23 13:49:00 +00:00
|
|
|
int fd = udp_bind(src);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(fd == -1)
|
2018-08-15 15:36:34 +00:00
|
|
|
return nullptr;
|
2018-04-30 16:14:20 +00:00
|
|
|
llarp::udp_listener* listener = new llarp::udp_listener(fd, l);
|
2018-08-15 15:36:34 +00:00
|
|
|
l->impl = listener;
|
|
|
|
udp_listeners.push_back(l);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-08-16 15:31:32 +00:00
|
|
|
add_ev(llarp::ev_io* e, bool write)
|
2018-08-15 15:36:34 +00:00
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
epoll_event ev;
|
2018-08-15 15:36:34 +00:00
|
|
|
ev.data.ptr = e;
|
2018-08-16 15:31:32 +00:00
|
|
|
ev.events = EPOLLIN;
|
2018-08-21 18:17:16 +00:00
|
|
|
// if(write)
|
|
|
|
// ev.events |= EPOLLOUT;
|
2018-08-15 15:36:34 +00:00
|
|
|
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, e->fd, &ev) == -1)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-08-15 15:36:34 +00:00
|
|
|
delete e;
|
2018-04-30 13:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
udp_close(llarp_udp_io* l)
|
2018-05-16 16:41:20 +00:00
|
|
|
{
|
2018-05-28 20:58:10 +00:00
|
|
|
bool ret = false;
|
|
|
|
llarp::udp_listener* listener =
|
|
|
|
static_cast< llarp::udp_listener* >(l->impl);
|
2018-05-22 19:19:06 +00:00
|
|
|
if(listener)
|
|
|
|
{
|
2018-05-28 20:58:10 +00:00
|
|
|
close_ev(listener);
|
2018-05-22 19:19:06 +00:00
|
|
|
l->impl = nullptr;
|
2018-05-28 20:58:10 +00:00
|
|
|
delete listener;
|
2018-06-27 13:14:07 +00:00
|
|
|
udp_listeners.remove(l);
|
2018-05-22 19:19:06 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2018-05-16 16:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
stop()
|
|
|
|
{
|
2018-09-10 13:43:36 +00:00
|
|
|
if(epollfd != -1)
|
|
|
|
close(epollfd);
|
|
|
|
epollfd = -1;
|
2018-04-30 13:18:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|