2018-01-25 16:24:33 +00:00
|
|
|
#ifndef LLARP_EV_H
|
|
|
|
#define LLARP_EV_H
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
#include <net/ip_address.hpp>
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <tuntap.h>
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
#ifdef _WIN32
|
2018-07-25 00:39:52 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2018-11-19 07:55:19 +00:00
|
|
|
#include <wspiapi.h>
|
2018-07-25 00:39:52 +00:00
|
|
|
#else
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <netinet/in.h>
|
2018-07-25 00:42:14 +00:00
|
|
|
#include <sys/socket.h>
|
2018-07-25 00:39:52 +00:00
|
|
|
#endif
|
2020-06-24 14:25:36 +00:00
|
|
|
#include <net/net_if.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2019-02-11 14:43:48 +00:00
|
|
|
#include <memory>
|
2019-07-30 23:42:13 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2019-06-12 19:35:02 +00:00
|
|
|
#if !defined(WIN32)
|
2019-06-02 21:17:05 +00:00
|
|
|
#include <uv.h>
|
2019-06-12 19:35:02 +00:00
|
|
|
#endif
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2020-06-01 13:17:44 +00:00
|
|
|
#include <constants/evloop.hpp>
|
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/**
|
2018-08-08 17:43:46 +00:00
|
|
|
* ev.h
|
|
|
|
*
|
|
|
|
* event handler (cross platform high performance event system for IO)
|
|
|
|
*/
|
2018-05-25 09:17:08 +00:00
|
|
|
|
2019-11-22 21:05:51 +00:00
|
|
|
#define EV_TICK_INTERVAL 10
|
2018-12-15 16:21:52 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
// forward declare
|
|
|
|
struct llarp_threadpool;
|
|
|
|
|
2018-01-29 14:27:24 +00:00
|
|
|
struct llarp_ev_loop;
|
|
|
|
|
2018-12-10 14:14:55 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
class Logic;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using llarp_ev_loop_ptr = std::shared_ptr<llarp_ev_loop>;
|
2018-05-25 09:17:08 +00:00
|
|
|
|
2019-06-18 04:18:53 +00:00
|
|
|
/// make an event loop using our baked in event loop on Windows
|
|
|
|
/// make an event loop using libuv otherwise.
|
2020-06-01 13:17:44 +00:00
|
|
|
/// @param queue_size how big the logic job queue is
|
2019-04-08 12:01:52 +00:00
|
|
|
llarp_ev_loop_ptr
|
2020-06-01 13:17:44 +00:00
|
|
|
llarp_make_ev_loop(std::size_t queue_size = llarp::event_loop_queue_size);
|
2019-02-11 14:43:48 +00:00
|
|
|
|
2019-04-08 12:01:52 +00:00
|
|
|
// run mainloop
|
2018-06-06 12:46:26 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev, std::shared_ptr<llarp::Logic> logic);
|
2018-06-06 12:46:26 +00:00
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
/// get the current time on the event loop
|
|
|
|
llarp_time_t
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_time_now_ms(const llarp_ev_loop_ptr& ev);
|
2018-10-29 16:48:36 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// stop event loop and wait for it to complete all jobs
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_stop(const llarp_ev_loop_ptr& ev);
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// UDP handling configuration
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_udp_io
|
|
|
|
{
|
2018-09-06 20:31:58 +00:00
|
|
|
/// set after added
|
|
|
|
int fd;
|
2020-04-07 18:38:56 +00:00
|
|
|
void* user;
|
|
|
|
void* impl;
|
|
|
|
struct llarp_ev_loop* parent;
|
2018-10-29 16:48:36 +00:00
|
|
|
|
2018-06-27 13:14:07 +00:00
|
|
|
/// called every event loop tick after reads
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*tick)(struct llarp_udp_io*);
|
2020-05-06 20:38:44 +00:00
|
|
|
|
|
|
|
void (*recvfrom)(struct llarp_udp_io*, const llarp::SockAddr& source, ManagedBuffer);
|
2019-06-02 21:17:05 +00:00
|
|
|
/// set by parent
|
2020-05-06 20:38:44 +00:00
|
|
|
int (*sendto)(struct llarp_udp_io*, const llarp::SockAddr&, const byte_t*, size_t);
|
2018-01-29 14:27:24 +00:00
|
|
|
};
|
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// add UDP handler
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp_ev_add_udp(struct llarp_ev_loop* ev, struct llarp_udp_io* udp, const llarp::SockAddr& src);
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-11-23 14:37:26 +00:00
|
|
|
/// send a UDP packet
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp_ev_udp_sendto(struct llarp_udp_io* udp, const llarp::SockAddr& to, const llarp_buffer_t& pkt);
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// close UDP handler
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_close_udp(struct llarp_udp_io* udp);
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
// forward declare
|
|
|
|
struct llarp_tcp_acceptor;
|
|
|
|
|
|
|
|
/// a single tcp connection
|
|
|
|
struct llarp_tcp_conn
|
|
|
|
{
|
|
|
|
/// user data
|
2020-04-07 18:38:56 +00:00
|
|
|
void* user;
|
2018-10-09 12:06:30 +00:00
|
|
|
/// private implementation
|
2020-04-07 18:38:56 +00:00
|
|
|
void* impl;
|
2018-10-09 12:06:30 +00:00
|
|
|
/// parent loop (dont set me)
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_ev_loop* loop;
|
2018-10-09 12:06:30 +00:00
|
|
|
/// handle read event
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*read)(struct llarp_tcp_conn*, const llarp_buffer_t&);
|
2019-06-03 12:02:54 +00:00
|
|
|
//// set by parent
|
2020-04-07 18:38:56 +00:00
|
|
|
ssize_t (*write)(struct llarp_tcp_conn*, const byte_t*, size_t sz);
|
2019-06-03 12:02:54 +00:00
|
|
|
/// set by parent
|
2020-04-07 18:38:56 +00:00
|
|
|
bool (*is_open)(struct llarp_tcp_conn*);
|
2018-10-09 12:06:30 +00:00
|
|
|
/// handle close event (free-ing is handled by event loop)
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*closed)(struct llarp_tcp_conn*);
|
2019-06-02 21:17:05 +00:00
|
|
|
/// explict close by user (set by parent)
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*close)(struct llarp_tcp_conn*);
|
2018-10-19 11:41:36 +00:00
|
|
|
/// handle event loop tick
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*tick)(struct llarp_tcp_conn*);
|
2018-10-09 12:06:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// queue async write a buffer in full
|
2019-02-11 14:43:48 +00:00
|
|
|
/// return if we queued it or not
|
2018-10-24 18:02:42 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_conn_async_write(struct llarp_tcp_conn*, const llarp_buffer_t&);
|
2018-10-09 12:06:30 +00:00
|
|
|
|
|
|
|
/// close a tcp connection
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_conn_close(struct llarp_tcp_conn*);
|
2018-10-09 12:06:30 +00:00
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
/// handles outbound connections to 1 endpoint
|
|
|
|
struct llarp_tcp_connecter
|
|
|
|
{
|
|
|
|
/// remote address family
|
|
|
|
int af;
|
|
|
|
/// remote address string
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp::IpAddress remote;
|
2018-11-01 12:47:14 +00:00
|
|
|
/// userdata pointer
|
2020-04-07 18:38:56 +00:00
|
|
|
void* user;
|
2019-06-02 21:17:05 +00:00
|
|
|
/// private implementation (dont set me)
|
2020-04-07 18:38:56 +00:00
|
|
|
void* impl;
|
2018-11-01 12:47:14 +00:00
|
|
|
/// parent event loop (dont set me)
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_ev_loop* loop;
|
2018-11-01 12:47:14 +00:00
|
|
|
/// handle outbound connection made
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*connected)(struct llarp_tcp_connecter*, struct llarp_tcp_conn*);
|
2018-11-01 12:47:14 +00:00
|
|
|
/// handle outbound connection error
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*error)(struct llarp_tcp_connecter*);
|
2018-11-01 12:47:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// async try connecting to a remote connection 1 time
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_async_try_connect(struct llarp_ev_loop* l, struct llarp_tcp_connecter* tcp);
|
2018-11-01 12:47:14 +00:00
|
|
|
|
|
|
|
/// handles inbound connections
|
2018-10-09 12:06:30 +00:00
|
|
|
struct llarp_tcp_acceptor
|
|
|
|
{
|
|
|
|
/// userdata pointer
|
2020-04-07 18:38:56 +00:00
|
|
|
void* user;
|
2018-10-09 12:06:30 +00:00
|
|
|
/// internal implementation
|
2020-04-07 18:38:56 +00:00
|
|
|
void* impl;
|
2018-10-09 12:06:30 +00:00
|
|
|
/// parent event loop (dont set me)
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_ev_loop* loop;
|
2018-11-01 12:47:14 +00:00
|
|
|
/// handle event loop tick
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*tick)(struct llarp_tcp_acceptor*);
|
2018-10-09 12:06:30 +00:00
|
|
|
/// handle inbound connection
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*accepted)(struct llarp_tcp_acceptor*, struct llarp_tcp_conn*);
|
2018-10-24 18:02:42 +00:00
|
|
|
/// handle after server socket closed (free-ing is handled by event loop)
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*closed)(struct llarp_tcp_acceptor*);
|
2019-06-03 14:03:59 +00:00
|
|
|
/// set by impl
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*close)(struct llarp_tcp_acceptor*);
|
2018-10-09 12:06:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// bind to an address and start serving async
|
|
|
|
/// return false if failed to bind
|
2019-02-11 14:43:48 +00:00
|
|
|
/// return true on success
|
2018-10-09 12:06:30 +00:00
|
|
|
bool
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp_tcp_serve(
|
|
|
|
struct llarp_ev_loop* loop, struct llarp_tcp_acceptor* t, const llarp::SockAddr& bindaddr);
|
2018-10-09 12:06:30 +00:00
|
|
|
|
|
|
|
/// close and stop accepting connections
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor*);
|
2018-10-09 12:06:30 +00:00
|
|
|
|
2018-08-15 16:12:41 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define IFNAMSIZ (16)
|
|
|
|
#endif
|
|
|
|
|
2018-11-26 22:46:22 +00:00
|
|
|
struct llarp_fd_promise;
|
|
|
|
|
|
|
|
/// wait until the fd promise is set
|
|
|
|
int
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_fd_promise_wait_for_value(struct llarp_fd_promise* promise);
|
2018-11-26 22:46:22 +00:00
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
struct llarp_tun_io
|
|
|
|
{
|
|
|
|
// TODO: more info?
|
2020-01-31 21:51:43 +00:00
|
|
|
char ifaddr[128];
|
2020-01-31 19:47:16 +00:00
|
|
|
// windows only
|
|
|
|
uint32_t dnsaddr;
|
2018-08-15 15:36:34 +00:00
|
|
|
int netmask;
|
|
|
|
char ifname[IFNAMSIZ + 1];
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
void* user;
|
|
|
|
void* impl;
|
2018-11-26 22:46:22 +00:00
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
/// functor for getting a promise that returns the vpn fd
|
2018-11-26 22:46:22 +00:00
|
|
|
/// dont set me if you don't know how to use this
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_fd_promise* (*get_fd_promise)(struct llarp_tun_io*);
|
2018-11-26 22:46:22 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_ev_loop* parent;
|
2018-08-17 19:49:58 +00:00
|
|
|
/// called when we are able to write right before we write
|
|
|
|
/// this happens after reading packets
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*before_write)(struct llarp_tun_io*);
|
2018-08-15 15:36:34 +00:00
|
|
|
/// called every event loop tick after reads
|
2020-04-07 18:38:56 +00:00
|
|
|
void (*tick)(struct llarp_tun_io*);
|
|
|
|
void (*recvpkt)(struct llarp_tun_io*, const llarp_buffer_t&);
|
2019-06-03 12:02:54 +00:00
|
|
|
/// set by parent
|
2020-04-07 18:38:56 +00:00
|
|
|
bool (*writepkt)(struct llarp_tun_io*, const byte_t*, size_t);
|
2018-08-15 15:36:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// create tun interface with network interface name ifname
|
|
|
|
/// returns true on success otherwise returns false
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_add_tun(struct llarp_ev_loop* ev, struct llarp_tun_io* tun);
|
2018-08-15 15:36:34 +00:00
|
|
|
|
|
|
|
/// async write a packet on tun interface
|
|
|
|
/// returns true if queued, returns false on drop
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_tun_async_write(struct llarp_tun_io* tun, const llarp_buffer_t&);
|
2018-08-15 15:36:34 +00:00
|
|
|
|
2018-01-25 16:24:33 +00:00
|
|
|
#endif
|