lokinet/llarp/ev/ev_epoll.hpp

143 lines
2.1 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
2018-11-06 14:06:09 +00:00
#ifdef ANDROID
/** TODO: correct this value */
#define SOCK_NONBLOCK (0)
#endif
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
tick();
2018-10-19 11:41:36 +00:00
int
read(byte_t* buf, size_t sz);
int
sendto(const sockaddr* to, const void* data, size_t sz);
};
2018-08-15 15:36:34 +00:00
struct tun : public ev_io
{
llarp_tun_io* t;
device* tunif;
2018-10-29 16:48:36 +00:00
tun(llarp_tun_io* tio, llarp_ev_loop* l)
: ev_io(-1, new LossyWriteQueue_t("tun_write_queue", l, l))
2018-08-15 15:36:34 +00:00
, t(tio)
, tunif(tuntap_init())
{
};
int
sendto(const sockaddr* to, const void* data, size_t sz);
2018-08-15 15:36:34 +00:00
2018-10-25 12:39:32 +00:00
bool
tick();
2018-10-19 11:41:36 +00:00
void
flush_write();
2018-08-15 15:36:34 +00:00
int
read(byte_t* buf, size_t sz);
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
struct llarp_epoll_loop : public llarp_ev_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