2018-05-29 12:14:50 +00:00
|
|
|
#ifndef EV_KQUEUE_HPP
|
|
|
|
#define EV_KQUEUE_HPP
|
2018-12-12 02:52: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/logger.hpp>
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2018-10-27 12:41:04 +00:00
|
|
|
#include <sys/un.h>
|
|
|
|
|
2019-01-19 01:38:04 +00:00
|
|
|
// why did we need a macro here, kqueue(7) _only_ exists
|
|
|
|
// on BSD and Macintosh
|
2018-06-17 15:26:00 +00:00
|
|
|
#include <sys/event.h>
|
2018-11-02 17:41:55 +00:00
|
|
|
#include <fcntl.h>
|
2018-05-29 12:14:50 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct udp_listener : public ev_io
|
|
|
|
{
|
|
|
|
llarp_udp_io* udp;
|
|
|
|
|
2019-04-24 23:27:31 +00:00
|
|
|
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u)
|
|
|
|
{
|
|
|
|
}
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
~udp_listener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:39:32 +00:00
|
|
|
bool
|
2019-01-19 01:38:04 +00:00
|
|
|
tick();
|
2018-10-25 12:39:32 +00:00
|
|
|
|
2018-05-29 12:14:50 +00:00
|
|
|
virtual int
|
2019-01-19 01:38:04 +00:00
|
|
|
read(byte_t* buf, size_t sz);
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
virtual int
|
2019-01-19 01:38:04 +00:00
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz);
|
2018-05-29 12:14:50 +00:00
|
|
|
};
|
2018-08-21 13:33:33 +00:00
|
|
|
|
|
|
|
struct tun : public ev_io
|
|
|
|
{
|
|
|
|
llarp_tun_io* t;
|
|
|
|
device* tunif;
|
2019-04-09 12:29:10 +00:00
|
|
|
tun(llarp_tun_io* tio, llarp_ev_loop_ptr l)
|
2018-11-21 21:59:16 +00:00
|
|
|
: ev_io(-1, new LossyWriteQueue_t("kqueue_tun_write", l, l))
|
2018-10-29 16:48:36 +00:00
|
|
|
, t(tio)
|
2019-04-24 23:27:31 +00:00
|
|
|
, tunif(tuntap_init())
|
|
|
|
{
|
|
|
|
}
|
2018-08-21 13:33:33 +00:00
|
|
|
|
|
|
|
int
|
2018-11-07 15:30:22 +00:00
|
|
|
sendto(__attribute__((unused)) const sockaddr* to,
|
|
|
|
__attribute__((unused)) const void* data,
|
2019-01-19 01:38:04 +00:00
|
|
|
__attribute__((unused)) size_t sz) override;
|
2018-08-21 13:33:33 +00:00
|
|
|
|
2018-12-14 15:09:36 +00:00
|
|
|
#ifdef __APPLE__
|
2018-12-14 16:01:32 +00:00
|
|
|
ssize_t
|
2019-01-19 01:38:04 +00:00
|
|
|
do_write(void* buf, size_t sz) override;
|
2018-12-14 15:09:36 +00:00
|
|
|
#endif
|
2018-10-01 17:16:15 +00:00
|
|
|
|
2018-08-21 13:33:33 +00:00
|
|
|
void
|
2019-01-19 01:38:04 +00:00
|
|
|
before_flush_write() override;
|
2018-08-21 13:33:33 +00:00
|
|
|
|
2018-10-25 12:39:32 +00:00
|
|
|
bool
|
2019-01-19 01:38:04 +00:00
|
|
|
tick() override;
|
2018-08-21 13:33:33 +00:00
|
|
|
int
|
2019-01-19 01:38:04 +00:00
|
|
|
read(byte_t* buf, size_t) override;
|
2018-08-21 13:33:33 +00:00
|
|
|
|
|
|
|
bool
|
2019-01-19 01:38:04 +00:00
|
|
|
setup();
|
2018-08-21 13:33:33 +00:00
|
|
|
|
|
|
|
~tun()
|
|
|
|
{
|
2018-12-14 14:48:36 +00:00
|
|
|
if(tunif)
|
|
|
|
tuntap_destroy(tunif);
|
2018-08-21 13:33:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-19 01:38:04 +00:00
|
|
|
} // namespace llarp
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2019-04-09 12:29:10 +00:00
|
|
|
struct llarp_kqueue_loop final
|
|
|
|
: public llarp_ev_loop,
|
|
|
|
public std::enable_shared_from_this< llarp_kqueue_loop >
|
2018-05-29 12:14:50 +00:00
|
|
|
{
|
|
|
|
int kqueuefd;
|
|
|
|
|
|
|
|
llarp_kqueue_loop() : kqueuefd(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:16:16 +00:00
|
|
|
virtual ~llarp_kqueue_loop()
|
2018-05-29 12:14:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-23 22:16:16 +00:00
|
|
|
init() override;
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2019-01-23 22:16:16 +00:00
|
|
|
int
|
|
|
|
run() override;
|
2018-08-10 03:51:38 +00:00
|
|
|
|
2018-11-02 12:35:20 +00:00
|
|
|
bool
|
2019-01-23 22:16:16 +00:00
|
|
|
running() const override;
|
2018-11-02 12:35:20 +00:00
|
|
|
|
2019-01-23 22:16:16 +00:00
|
|
|
bool
|
|
|
|
tcp_connect(llarp_tcp_connecter* tcp, const sockaddr* addr) override;
|
2018-06-06 12:46:26 +00:00
|
|
|
|
2018-05-29 12:14:50 +00:00
|
|
|
int
|
2019-01-23 22:16:16 +00:00
|
|
|
tick(int ms) override;
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
int
|
2019-01-19 01:38:04 +00:00
|
|
|
udp_bind(const sockaddr* addr);
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2019-06-10 16:31:01 +00:00
|
|
|
bool
|
2019-01-23 22:16:16 +00:00
|
|
|
udp_listen(llarp_udp_io* l, const sockaddr* src) override;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2018-05-29 12:14:50 +00:00
|
|
|
bool
|
2019-01-23 22:16:16 +00:00
|
|
|
close_ev(llarp::ev_io* ev) override;
|
|
|
|
|
|
|
|
llarp::ev_io*
|
|
|
|
create_tun(llarp_tun_io* tun) override;
|
|
|
|
|
|
|
|
llarp::ev_io*
|
|
|
|
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr) override;
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2018-08-15 15:45:07 +00:00
|
|
|
llarp::ev_io*
|
2019-06-10 16:31:51 +00:00
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src);
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2018-08-15 15:45:07 +00:00
|
|
|
bool
|
2019-01-23 22:16:16 +00:00
|
|
|
add_ev(llarp::ev_io* ev, bool w) override;
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
bool
|
2019-01-23 22:16:16 +00:00
|
|
|
udp_close(llarp_udp_io* l) override;
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
void
|
2019-01-23 22:16:16 +00:00
|
|
|
stop() override;
|
2018-05-29 12:14:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|