lokinet/include/llarp/ev.h

162 lines
3.8 KiB
C
Raw Normal View History

2018-01-25 16:24:33 +00:00
#ifndef LLARP_EV_H
#define LLARP_EV_H
#if defined(__MINGW32__) || defined(_WIN32)
2018-07-25 00:39:52 +00:00
#include <winsock2.h>
#include <ws2tcpip.h>
#include <wspiapi.h>
#ifndef ssize_t
#define ssize_t long
#endif
2018-07-25 00:39:52 +00:00
#else
2018-08-15 15:36:34 +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
2018-01-29 14:19:00 +00:00
#include <stdbool.h>
2018-01-29 14:27:24 +00:00
#include <stdint.h>
#include <stdlib.h>
2018-08-15 15:36:34 +00:00
#include <tuntap.h>
2018-05-25 09:17:08 +00:00
/**
* ev.h
*
* event handler (cross platform high performance event system for IO)
*/
2018-05-25 09:17:08 +00:00
2018-06-06 12:46:26 +00:00
// forward declare
struct llarp_threadpool;
struct llarp_logic;
2018-01-29 14:27:24 +00:00
struct llarp_ev_loop;
2018-05-25 09:17:08 +00:00
/// allocator
void
llarp_ev_loop_alloc(struct llarp_ev_loop **ev);
2018-05-25 09:17:08 +00:00
// deallocator
void
llarp_ev_loop_free(struct llarp_ev_loop **ev);
2018-01-29 14:27:24 +00:00
2018-05-25 09:17:08 +00:00
/// run main loop
int
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic);
2018-05-25 09:17:08 +00:00
2018-06-06 12:46:26 +00:00
void
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,
struct llarp_threadpool *tp,
struct llarp_logic *logic);
2018-05-25 09:17:08 +00:00
/// stop event loop and wait for it to complete all jobs
void
llarp_ev_loop_stop(struct llarp_ev_loop *ev);
2018-01-29 14:27:24 +00:00
2018-05-25 09:17:08 +00:00
/// UDP handling configuration
struct llarp_udp_io
{
2018-09-06 20:31:58 +00:00
/// set after added
int fd;
2018-01-29 14:27:24 +00:00
void *user;
void *impl;
struct llarp_ev_loop *parent;
/// called every event loop tick after reads
void (*tick)(struct llarp_udp_io *);
2018-09-29 10:24:10 +00:00
// sockaddr * is the source
2018-05-18 17:10:48 +00:00
void (*recvfrom)(struct llarp_udp_io *, const struct sockaddr *, const void *,
2018-01-29 14:27:24 +00:00
ssize_t);
};
2018-05-25 09:17:08 +00:00
/// add UDP handler
int
2018-05-23 13:49:00 +00:00
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
const struct sockaddr *src);
2018-01-29 14:27:24 +00:00
2018-05-25 09:17:08 +00:00
/// schedule UDP packet
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const struct sockaddr *to,
const void *data, size_t sz);
2018-04-30 16:14:20 +00:00
2018-05-25 09:17:08 +00:00
/// close UDP handler
int
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
void *user;
/// private implementation
void *impl;
/// parent loop (dont set me)
struct llarp_ev_loop *loop;
/// handle read event
void (*read)(struct llarp_tcp_conn *, const void *, size_t);
/// handle close event (free-ing is handled by event loop)
void (*closed)(struct llarp_tcp_conn *);
};
/// queue async write a buffer in full
void
llarp_tcp_conn_async_write(struct llarp_tcp_conn *, const void *, size_t);
/// close a tcp connection
void
llarp_tcp_conn_close(struct llarp_tcp_conn *);
struct llarp_tcp_acceptor
{
/// userdata pointer
void *user;
/// internal implementation
void *impl;
/// parent event loop (dont set me)
struct llarp_ev_loop *loop;
/// handle inbound connection
void (*accepted)(struct llarp_tcp_acceptor *, struct llarp_tcp_conn *);
};
/// bind to an address and start serving async
/// return false if failed to bind
/// return true on successs
bool
llarp_tcp_serve(struct llarp_tcp_acceptor *t, const sockaddr *bindaddr);
/// close and stop accepting connections
void
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor *);
2018-08-15 16:12:41 +00:00
#ifdef _WIN32
#define IFNAMSIZ (16)
#endif
2018-08-15 15:36:34 +00:00
struct llarp_tun_io
{
// TODO: more info?
char ifaddr[128];
int netmask;
char ifname[IFNAMSIZ + 1];
void *user;
void *impl;
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 15:36:34 +00:00
/// called every event loop tick after reads
void (*tick)(struct llarp_tun_io *);
void (*recvpkt)(struct llarp_tun_io *, const void *, ssize_t);
};
/// 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
llarp_ev_tun_async_write(struct llarp_tun_io *tun, const void *pkt, size_t sz);
2018-01-25 16:24:33 +00:00
#endif