lokinet/include/llarp.h

232 lines
6.9 KiB
C
Raw Normal View History

2018-01-25 16:24:33 +00:00
#ifndef LLARP_H_
#define LLARP_H_
#include <stdint.h>
#include <unistd.h>
2019-11-05 13:19:27 +00:00
2018-12-13 21:46:55 +00:00
#ifdef __cplusplus
2018-07-17 04:37:50 +00:00
extern "C"
{
2018-05-27 18:03:10 +00:00
#endif
2018-11-06 14:06:09 +00:00
/// ensure configuration exists
/// populate with defaults
/// return if this succeeded
/// if overwrite is true then overwrite old config file
/// if basedir is not nullptr then use basedir as an absolute
/// base path for all files in config
bool
llarp_ensure_config(const char *, const char *, bool overwrite,
bool asrouter);
2018-11-06 14:06:09 +00:00
2018-07-17 04:37:50 +00:00
/// llarp application context for C api
struct llarp_main;
2018-05-27 18:03:10 +00:00
/// runtime options for main context from cli
struct llarp_main_runtime_opts
{
bool background = false;
bool debug = false;
bool singleThreaded = false;
};
2018-05-27 18:03:10 +00:00
2019-10-03 13:54:21 +00:00
/// llarp_application config
struct llarp_config;
2018-05-27 18:03:10 +00:00
2019-10-03 13:54:21 +00:00
/// get default config for current platform
struct llarp_config *
llarp_default_config();
2018-05-27 18:03:10 +00:00
2019-10-08 14:52:01 +00:00
/// free previously allocated configuration
void
llarp_config_free(struct llarp_config *);
2019-10-03 13:54:21 +00:00
/// packet writer to send packets to lokinet internals
struct llarp_vpn_writer_pipe;
/// packet reader to recv packets from lokinet internals
struct llarp_vpn_reader_pipe;
2019-10-03 13:54:21 +00:00
/// vpn io api
/// all hooks called in native code
/// for use with a vpn interface managed by external code
/// the external vpn interface MUST be up and have addresses set
struct llarp_vpn_io
{
/// private implementation
void *impl;
/// user data
void *user;
/// hook set by user called by lokinet core when lokinet is done with the
/// vpn io
void (*closed)(struct llarp_vpn_io *);
/// hook set by user called from lokinet core after attempting to inject
/// into endpoint passed a bool set to true if we were injected otherwise
/// set to false
void (*injected)(struct llarp_vpn_io *, bool);
/// hook set by user called every event loop tick
void (*tick)(struct llarp_vpn_io *);
};
2019-10-03 13:54:21 +00:00
/// info about the network interface that we give to lokinet core
struct llarp_vpn_ifaddr_info
{
/// name of the network interface
char ifname[64];
/// interface's address as string
char ifaddr[128];
2019-10-08 14:52:01 +00:00
/// netmask number of bits set
uint8_t netmask;
2019-10-03 13:54:21 +00:00
};
2018-05-27 18:03:10 +00:00
2019-10-03 13:54:21 +00:00
/// initialize llarp_vpn_io private implementation
/// returns false if either parameter is nullptr
bool
llarp_vpn_io_init(struct llarp_main *m, struct llarp_vpn_io *io);
2019-10-03 13:54:21 +00:00
/// get the packet pipe for writing IP packets to lokinet internals
/// returns nullptr if llarp_vpn_io is nullptr or not initialized
2019-10-03 13:54:21 +00:00
struct llarp_vpn_pkt_writer *
llarp_vpn_io_packet_writer(struct llarp_vpn_io *io);
/// get the packet pipe for reading IP packets from lokinet internals
/// returns nullptr if llarp_vpn_io is nullptr or not initialized
2019-10-03 13:54:21 +00:00
struct llarp_vpn_pkt_reader *
llarp_vpn_io_packet_reader(struct llarp_vpn_io *io);
/// blocking read on packet reader from lokinet internals
/// returns -1 on error, returns size of packet read
/// thread safe
ssize_t
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader *r, unsigned char *dst,
size_t dstlen);
/// blocking write on packet writer to lokinet internals
/// returns false if we can't write this packet
/// return true if we wrote this packet
/// thread safe
bool
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer *w, unsigned char *pktbuf,
size_t pktlen);
2018-12-13 21:46:55 +00:00
2019-10-03 13:54:21 +00:00
/// close vpn io and free private implementation after done
/// operation is async and calls llarp_vpn_io.closed after fully closed
/// after fully closed the llarp_vpn_io MUST be re-initialized by
/// llarp_vpn_io_init if it is to be used again
2018-07-17 04:37:50 +00:00
void
2019-10-03 13:54:21 +00:00
llarp_vpn_io_close_async(struct llarp_vpn_io *io);
2019-10-03 13:54:21 +00:00
/// get the default endpoint's name for injection
2018-12-13 21:46:55 +00:00
const char *
2019-10-03 13:54:21 +00:00
llarp_main_get_default_endpoint_name(struct llarp_main *m);
2019-10-03 13:54:21 +00:00
/// give main context a vpn io for mobile when it is reader to do io with
/// associated info tries to give the vpn io to endpoint with name epName a
/// deferred call to llarp_vpn_io.injected is queued unconditionally
/// thread safe
2018-07-17 04:37:50 +00:00
bool
2019-10-03 13:54:21 +00:00
llarp_main_inject_vpn_by_name(struct llarp_main *m, const char *epName,
struct llarp_vpn_io *io,
struct llarp_vpn_ifaddr_info info);
/// give main context a vpn io on its default endpoint
static bool
2019-10-03 13:54:21 +00:00
llarp_main_inject_default_vpn(struct llarp_main *m, struct llarp_vpn_io *io,
struct llarp_vpn_ifaddr_info info)
{
return llarp_main_inject_vpn_by_name(
m, llarp_main_get_default_endpoint_name(m), io, info);
2019-10-03 13:54:21 +00:00
}
2019-10-03 13:54:21 +00:00
/// load config from file by name
2019-10-08 14:52:01 +00:00
/// allocates new config and puts it into c
/// return false on failure
bool
llarp_config_load_file(const char *fname, struct llarp_config **c);
/// loads config from file by name
/// uses already allocated config
/// return false on failure
2018-07-17 04:37:50 +00:00
bool
2019-10-08 14:52:01 +00:00
llarp_config_read_file(struct llarp_config *c, const char *f);
2019-10-03 13:54:21 +00:00
/// make a main context from configuration
2019-10-08 14:52:01 +00:00
/// copies config contents
2019-10-03 13:54:21 +00:00
struct llarp_main *
llarp_main_init_from_config(struct llarp_config *conf);
2019-10-03 13:54:21 +00:00
/// initialize application context and load config
static struct llarp_main *
llarp_main_init(const char *fname)
2018-07-17 04:37:50 +00:00
{
2019-10-08 14:52:01 +00:00
struct llarp_main *m = 0;
struct llarp_config *conf = 0;
2019-10-03 13:54:21 +00:00
if(!llarp_config_load_file(fname, &conf))
return 0;
2019-10-03 13:54:21 +00:00
if(conf == NULL)
return 0;
2019-10-08 14:52:01 +00:00
m = llarp_main_init_from_config(conf);
llarp_config_free(conf);
return m;
2019-10-03 13:54:21 +00:00
}
/// initialize applicatin context with all defaults
static struct llarp_main *
llarp_main_default_init()
{
2019-10-08 14:52:01 +00:00
struct llarp_main *m;
2019-10-03 13:54:21 +00:00
struct llarp_config *conf;
conf = llarp_default_config();
if(conf == 0)
return 0;
2019-10-08 14:52:01 +00:00
m = llarp_main_init_from_config(conf);
llarp_config_free(conf);
return m;
2019-10-03 13:54:21 +00:00
}
2018-07-17 04:37:50 +00:00
2019-10-08 14:52:01 +00:00
/// (re)configure main context
/// return true if (re)configuration was successful
bool
llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf);
/// return true if this main context is running
/// return false otherwise
bool
llarp_main_is_running(struct llarp_main *ptr);
2019-10-03 13:54:21 +00:00
/// handle signal for main context
void
2019-10-03 13:54:21 +00:00
llarp_main_signal(struct llarp_main *ptr, int sig);
2019-10-03 13:54:21 +00:00
/// setup main context, returns 0 on success
int
llarp_main_setup(struct llarp_main *ptr);
2018-12-13 21:37:30 +00:00
2019-10-03 13:54:21 +00:00
/// run main context, returns 0 on success, blocks until program end
int
llarp_main_run(struct llarp_main *ptr, struct llarp_main_runtime_opts opts);
2019-10-08 14:52:01 +00:00
/// tell main context to stop and wait for complete stop
/// after calling this you can call llarp_main_free safely
void
llarp_main_stop(struct llarp_main *ptr);
2019-10-03 13:54:21 +00:00
/// free main context and end all operations
void
llarp_main_free(struct llarp_main *ptr);
2018-09-20 10:05:42 +00:00
/// get version string
const char *
llarp_version();
2018-09-20 10:05:42 +00:00
2019-10-08 14:52:01 +00:00
/// return sizeof(llarp_main); for jni
size_t
llarp_main_size();
/// return sizeof(llarp_config); for jni
size_t
llarp_config_size();
2019-10-03 13:54:21 +00:00
#ifdef __cplusplus
2018-05-27 18:03:10 +00:00
}
#endif
2018-01-25 16:24:33 +00:00
#endif