2019-01-19 00:41:50 +00:00
|
|
|
#ifndef EV_WIN32_H
|
|
|
|
#define EV_WIN32_H
|
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-01-13 22:39:10 +00:00
|
|
|
#include <util/buffer.h>
|
|
|
|
#include <util/logger.hpp>
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#include <windows.h>
|
2018-12-14 12:04:14 +00:00
|
|
|
#include <process.h>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
2018-12-14 12:04:14 +00:00
|
|
|
// io packet for TUN read/write
|
|
|
|
struct asio_evt_pkt
|
|
|
|
{
|
|
|
|
OVERLAPPED pkt = {
|
|
|
|
0, 0, 0, 0, nullptr}; // must be first, since this is part of the IO call
|
|
|
|
bool write = false; // true, or false if read pkt
|
|
|
|
size_t sz; // should match the queued data size, if not try again?
|
|
|
|
void* buf; // must remain valid until we get notification; this is _supposed_
|
|
|
|
// to be zero-copy
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" DWORD FAR PASCAL
|
|
|
|
tun_ev_loop(void* unused);
|
2019-01-04 03:00:07 +00:00
|
|
|
|
2019-01-19 00:41:50 +00:00
|
|
|
void
|
|
|
|
exit_tun_loop();
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
void
|
2019-01-19 00:41:50 +00:00
|
|
|
begin_tun_loop(int nThreads);
|
|
|
|
|
|
|
|
namespace llarp
|
2018-12-14 12:04:14 +00:00
|
|
|
{
|
2019-01-19 00:41:50 +00:00
|
|
|
struct udp_listener : public ev_io
|
2018-12-14 12:04:14 +00:00
|
|
|
{
|
2019-01-19 00:41:50 +00:00
|
|
|
llarp_udp_io* udp;
|
|
|
|
|
|
|
|
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
|
|
|
|
|
|
|
|
~udp_listener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
tick();
|
|
|
|
|
|
|
|
int
|
|
|
|
read(byte_t* buf, size_t sz);
|
|
|
|
|
|
|
|
int
|
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz);
|
|
|
|
};
|
|
|
|
} // namespace llarp
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
// A different kind of event loop,
|
|
|
|
// more suited for the native Windows NT
|
|
|
|
// event model
|
|
|
|
struct win32_tun_io
|
|
|
|
{
|
|
|
|
llarp_tun_io* t;
|
|
|
|
device* tunif;
|
|
|
|
byte_t readbuf[EV_READ_BUF_SZ] = {0};
|
|
|
|
|
2019-01-02 04:33:03 +00:00
|
|
|
win32_tun_io(llarp_tun_io* tio) : t(tio), tunif(tuntap_init()){};
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
queue_write(const byte_t* buf, size_t sz);
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
setup();
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
// first TUN device gets to set up the event port
|
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
add_ev();
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
// places data in event queue for kernel to process
|
|
|
|
void
|
2019-01-19 00:41:50 +00:00
|
|
|
do_write(void* data, size_t sz);
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
// we call this one when we get a packet in the event port
|
|
|
|
// which then kicks off another write
|
|
|
|
void
|
2019-01-19 00:41:50 +00:00
|
|
|
flush_write();
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
void
|
2019-01-19 00:41:50 +00:00
|
|
|
read(byte_t* buf, size_t sz);
|
2018-12-14 12:04:14 +00:00
|
|
|
|
|
|
|
~win32_tun_io()
|
|
|
|
{
|
|
|
|
CancelIo(tunif->tun_fd);
|
|
|
|
if(tunif->tun_fd)
|
|
|
|
tuntap_destroy(tunif);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-19 00:41:50 +00:00
|
|
|
// UDP event loop (we use select(3SOCKET) because linux)
|
2018-07-30 04:38:14 +00:00
|
|
|
struct llarp_win32_loop : public llarp_ev_loop
|
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
upoll_t* upollfd;
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-12-14 12:04:14 +00:00
|
|
|
llarp_win32_loop() : upollfd(nullptr)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr);
|
2018-11-02 20:43:53 +00:00
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
llarp::ev_io*
|
2019-01-19 00:41:50 +00:00
|
|
|
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr);
|
2018-10-30 12:30:21 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
virtual bool
|
2019-01-19 00:41:50 +00:00
|
|
|
udp_listen(llarp_udp_io* l, const sockaddr* src);
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
~llarp_win32_loop()
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
if(upollfd)
|
|
|
|
upoll_destroy(upollfd);
|
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
running() const;
|
2018-08-11 09:48:24 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
init();
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
int
|
2019-01-19 00:41:50 +00:00
|
|
|
tick(int ms);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
int
|
2019-01-19 00:41:50 +00:00
|
|
|
run();
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
int
|
2019-01-19 00:41:50 +00:00
|
|
|
udp_bind(const sockaddr* addr);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
close_ev(llarp::ev_io* ev);
|
2018-08-15 16:19:22 +00:00
|
|
|
|
2018-12-14 12:04:14 +00:00
|
|
|
// no tunnels here
|
2018-08-15 16:19:22 +00:00
|
|
|
llarp::ev_io*
|
2019-01-19 00:41:50 +00:00
|
|
|
create_tun(llarp_tun_io* tun);
|
2018-08-15 16:19:22 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
llarp::ev_io*
|
2019-01-19 00:41:50 +00:00
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src);
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
add_ev(llarp::ev_io* e, bool write);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
udp_close(llarp_udp_io* l);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
void
|
2019-01-19 00:41:50 +00:00
|
|
|
stop();
|
2018-07-30 04:38:14 +00:00
|
|
|
};
|
|
|
|
|
2019-01-19 00:41:50 +00:00
|
|
|
#endif
|