2018-04-30 13:18:57 +00:00
|
|
|
#ifndef EV_EPOLL_HPP
|
|
|
|
#define EV_EPOLL_HPP
|
2018-05-18 13:17:58 +00:00
|
|
|
#include <llarp/net.h>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
2018-05-18 13:17:58 +00:00
|
|
|
#include <cstdio>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include "ev.hpp"
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
namespace llarp {
|
|
|
|
struct udp_listener : public ev_io {
|
|
|
|
llarp_udp_io* udp;
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
~udp_listener() {}
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-04-30 16:14:29 +00:00
|
|
|
virtual int read(void* buf, size_t sz) {
|
2018-04-30 16:14:20 +00:00
|
|
|
sockaddr src;
|
|
|
|
socklen_t slen;
|
|
|
|
int ret = ::recvfrom(fd, buf, sz, 0, &src, &slen);
|
|
|
|
if (ret == -1) return -1;
|
|
|
|
udp->recvfrom(udp, &src, buf, ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int sendto(const sockaddr* to, const void* data, size_t sz) {
|
|
|
|
socklen_t slen;
|
|
|
|
switch (to->sa_family) {
|
2018-04-30 13:18:57 +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 16:14:20 +00:00
|
|
|
return ::sendto(fd, data, sz, 0, to, slen);
|
|
|
|
}
|
2018-04-30 13:18:57 +00:00
|
|
|
};
|
2018-04-30 16:14:20 +00:00
|
|
|
}; // namespace llarp
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
struct llarp_epoll_loop : public llarp_ev_loop {
|
2018-04-30 13:18:57 +00:00
|
|
|
int epollfd;
|
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
llarp_epoll_loop() : epollfd(-1) {}
|
|
|
|
|
|
|
|
~llarp_epoll_loop() {}
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
bool init() {
|
|
|
|
if (epollfd == -1) epollfd = epoll_create1(EPOLL_CLOEXEC);
|
2018-04-30 13:18:57 +00:00
|
|
|
return epollfd != -1;
|
|
|
|
}
|
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
int run() {
|
2018-04-30 13:18:57 +00:00
|
|
|
epoll_event events[1024];
|
|
|
|
int result;
|
2018-04-30 16:14:20 +00:00
|
|
|
char readbuf[2048];
|
|
|
|
do {
|
2018-05-16 16:41:20 +00:00
|
|
|
result = epoll_wait(epollfd, events, 1024, -1);
|
2018-04-30 16:14:20 +00:00
|
|
|
if (result > 0) {
|
2018-04-30 13:18:57 +00:00
|
|
|
int idx = 0;
|
2018-04-30 16:14:20 +00:00
|
|
|
while (idx < result) {
|
|
|
|
llarp::ev_io* ev = static_cast<llarp::ev_io*>(events[idx].data.ptr);
|
|
|
|
if (events[idx].events & EPOLLIN) {
|
|
|
|
if (ev->read(readbuf, sizeof(readbuf)) == -1) {
|
2018-05-16 16:41:20 +00:00
|
|
|
close_ev(ev);
|
|
|
|
delete ev;
|
2018-04-30 13:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
} while (result != -1);
|
2018-04-30 13:18:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
int udp_bind(const sockaddr* addr) {
|
2018-04-30 13:18:57 +00:00
|
|
|
socklen_t slen;
|
2018-04-30 16:14:20 +00:00
|
|
|
switch (addr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
slen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
slen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
2018-05-18 13:17:58 +00:00
|
|
|
case AF_PACKET:
|
|
|
|
slen = sizeof(struct sockaddr_ll);
|
|
|
|
break;
|
2018-04-30 16:14:20 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
2018-04-30 13:18:57 +00:00
|
|
|
}
|
|
|
|
int fd = socket(addr->sa_family, SOCK_DGRAM, 0);
|
|
|
|
if (fd == -1) return -1;
|
2018-04-30 16:14:20 +00:00
|
|
|
if (bind(fd, addr, slen) == -1) {
|
2018-04-30 13:18:57 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-05-16 16:41:20 +00:00
|
|
|
bool close_ev(llarp::ev_io * ev)
|
|
|
|
{
|
|
|
|
return epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, nullptr) != -1;
|
|
|
|
}
|
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
bool udp_listen(llarp_udp_io* l) {
|
2018-05-17 20:00:58 +00:00
|
|
|
int fd = udp_bind(&l->addr);
|
2018-04-30 13:18:57 +00:00
|
|
|
if (fd == -1) return false;
|
2018-04-30 16:14:20 +00:00
|
|
|
llarp::udp_listener* listener = new llarp::udp_listener(fd, l);
|
2018-04-30 13:18:57 +00:00
|
|
|
epoll_event ev;
|
|
|
|
ev.data.ptr = listener;
|
|
|
|
ev.events = EPOLLIN;
|
2018-04-30 16:14:20 +00:00
|
|
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
|
2018-04-30 13:18:57 +00:00
|
|
|
delete listener;
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-16 16:41:20 +00:00
|
|
|
l->impl = listener;
|
2018-04-30 13:18:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-16 16:41:20 +00:00
|
|
|
bool udp_close(llarp_udp_io* l)
|
|
|
|
{
|
|
|
|
auto listener = static_cast<llarp::udp_listener*>(l->impl);
|
|
|
|
if(!listener) return false;
|
|
|
|
return close_ev(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
void stop() {
|
|
|
|
if (epollfd != -1) ::close(epollfd);
|
|
|
|
|
2018-04-30 13:18:57 +00:00
|
|
|
epollfd = -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|