1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/ev/ev.h

252 lines
6.2 KiB
C
Raw Normal View History

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