lokinet/llarp/ev/ev_epoll.hpp

145 lines
2.3 KiB
C++
Raw Normal View History

2018-04-30 13:18:57 +00:00
#ifndef EV_EPOLL_HPP
#define EV_EPOLL_HPP
2019-01-11 01:19:36 +00:00
#include <ev/ev.hpp>
#include <net/net.h>
#include <net/net.hpp>
2019-02-02 23:12:42 +00:00
#include <util/buffer.hpp>
#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>
#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
namespace llarp
{
struct udp_listener : public ev_io
{
llarp_udp_io* udp;
2018-04-30 13:18:57 +00:00
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
2018-04-30 13:18:57 +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
int
2019-04-08 15:54:19 +00:00
read(byte_t* buf, size_t sz) override;
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
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)
: 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
void
2019-04-08 15:54:19 +00:00
flush_write() override;
ssize_t
do_write(void* buf, size_t sz) override;
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
static int
wait_for_fd_promise(struct device* dev);
2018-08-15 15:36:34 +00:00
bool
setup();
2018-08-15 15:36:34 +00:00
~tun()
{
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-04-30 13:18:57 +00:00
int epollfd;
llarp_epoll_loop() : epollfd(-1)
{
}
2018-04-30 16:14:20 +00:00
~llarp_epoll_loop()
2018-11-01 12:47:14 +00:00
{
}
bool
tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr);
llarp::ev_io*
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr);
virtual bool
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
running() const;
2018-08-10 03:51:38 +00:00
bool
init();
2018-04-30 13:18:57 +00:00
2018-06-06 12:46:26 +00:00
int
tick(int ms);
2018-06-06 12:46:26 +00:00
int
run();
2018-04-30 13:18:57 +00:00
int
udp_bind(const sockaddr* addr);
2018-04-30 13:18:57 +00:00
bool
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*
create_tun(llarp_tun_io* tun);
2018-08-15 15:36:34 +00:00
llarp::ev_io*
create_udp(llarp_udp_io* l, const sockaddr* src);
2018-08-15 15:36:34 +00:00
bool
add_ev(llarp::ev_io* e, bool write);
2018-04-30 16:14:20 +00:00
bool
udp_close(llarp_udp_io* l);
2018-05-16 16:41:20 +00:00
void
stop();
2018-04-30 13:18:57 +00:00
};
#endif