2018-04-30 13:18:57 +00:00
|
|
|
#ifndef EV_EPOLL_HPP
|
|
|
|
#define EV_EPOLL_HPP
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.h>
|
|
|
|
#include <net/net.hpp>
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/logger.hpp>
|
|
|
|
#include <util/mem.hpp>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <fcntl.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-10-24 18:02:42 +00:00
|
|
|
#include <sys/un.h>
|
2018-08-15 15:36:34 +00:00
|
|
|
#include <tuntap.h>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include <unistd.h>
|
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-10-25 12:39:32 +00:00
|
|
|
bool
|
2019-04-08 15:54:19 +00:00
|
|
|
tick() override;
|
2018-10-19 11:41:36 +00:00
|
|
|
|
2018-10-24 18:02:42 +00:00
|
|
|
int
|
2019-04-08 15:54:19 +00:00
|
|
|
read(byte_t* buf, size_t sz) override;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-10-24 18:02:42 +00:00
|
|
|
int
|
2019-04-08 15:54:19 +00:00
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz) override;
|
2018-05-22 15:54:19 +00:00
|
|
|
};
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
struct tun : public ev_io
|
|
|
|
{
|
|
|
|
llarp_tun_io* t;
|
2019-04-08 15:54:19 +00:00
|
|
|
int writefd;
|
2018-08-15 15:36:34 +00:00
|
|
|
device* tunif;
|
2019-04-08 12:01:52 +00:00
|
|
|
tun(llarp_tun_io* tio, llarp_ev_loop_ptr l)
|
2018-11-15 13:13:19 +00:00
|
|
|
: ev_io(-1, new LossyWriteQueue_t("tun_write_queue", l, l))
|
2018-08-15 15:36:34 +00:00
|
|
|
, t(tio)
|
2019-04-08 15:54:19 +00:00
|
|
|
, writefd(-1)
|
2018-08-15 15:36:34 +00:00
|
|
|
, tunif(tuntap_init())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2019-04-08 15:54:19 +00:00
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz) override;
|
2018-08-15 15:36:34 +00:00
|
|
|
|
2018-10-25 12:39:32 +00:00
|
|
|
bool
|
2019-04-08 15:54:19 +00:00
|
|
|
tick() override;
|
2018-10-19 11:41:36 +00:00
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
void
|
2019-04-08 15:54:19 +00:00
|
|
|
flush_write() override;
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
do_write(void* buf, size_t sz) override;
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
int
|
2019-04-08 15:54:19 +00:00
|
|
|
read(byte_t* buf, size_t sz) override;
|
2018-08-15 15:36:34 +00:00
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
static int
|
2019-02-01 19:41:17 +00:00
|
|
|
wait_for_fd_promise(struct device* dev);
|
2018-11-26 22:46:22 +00:00
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
bool
|
2019-02-01 19:41:17 +00:00
|
|
|
setup();
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
~tun()
|
|
|
|
{
|
2018-11-26 22:46:22 +00:00
|
|
|
if(tunif)
|
|
|
|
tuntap_destroy(tunif);
|
2018-08-15 15:36:34 +00:00
|
|
|
}
|
|
|
|
};
|
2018-04-30 16:14:20 +00:00
|
|
|
}; // namespace llarp
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2019-04-08 15:54:19 +00:00
|
|
|
struct llarp_epoll_loop
|
|
|
|
: public llarp_ev_loop,
|
|
|
|
public std::enable_shared_from_this< llarp_epoll_loop >
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
int epollfd;
|
2019-02-01 19:41:17 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_epoll_loop() : epollfd(-1)
|
|
|
|
{
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2019-02-01 19:41:17 +00:00
|
|
|
~llarp_epoll_loop()
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-01 19:41:17 +00:00
|
|
|
bool
|
|
|
|
tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr);
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
llarp::ev_io*
|
2019-02-01 19:41:17 +00:00
|
|
|
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr);
|
2018-10-29 23:56:05 +00:00
|
|
|
|
|
|
|
virtual bool
|
2019-02-01 19:41:17 +00:00
|
|
|
udp_listen(llarp_udp_io* l, const sockaddr* src);
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
bool
|
2019-02-01 19:41:17 +00:00
|
|
|
running() const;
|
2018-08-10 03:51:38 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2019-02-01 19:41:17 +00:00
|
|
|
init();
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
int
|
2019-02-01 19:41:17 +00:00
|
|
|
tick(int ms);
|
2018-06-06 12:46:26 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2019-02-01 19:41:17 +00:00
|
|
|
run();
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2019-02-01 19:41:17 +00:00
|
|
|
udp_bind(const sockaddr* addr);
|
2018-04-30 13:18:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2019-02-01 19:41:17 +00:00
|
|
|
close_ev(llarp::ev_io* ev);
|
2018-05-16 16:41:20 +00:00
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
llarp::ev_io*
|
2019-02-01 19:41:17 +00:00
|
|
|
create_tun(llarp_tun_io* tun);
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
llarp::ev_io*
|
2019-02-01 19:41:17 +00:00
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src);
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
bool
|
2019-02-01 19:41:17 +00:00
|
|
|
add_ev(llarp::ev_io* e, bool write);
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2019-02-01 19:41:17 +00:00
|
|
|
udp_close(llarp_udp_io* l);
|
2018-05-16 16:41:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2019-02-01 19:41:17 +00:00
|
|
|
stop();
|
2018-04-30 13:18:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|