|
|
|
@ -1,15 +1,7 @@
|
|
|
|
|
#ifndef LLARP_H_
|
|
|
|
|
#define LLARP_H_
|
|
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
#include <constants/version.hpp>
|
|
|
|
|
#include <ev/ev.h>
|
|
|
|
|
#include <handlers/tun.hpp> // for handlers
|
|
|
|
|
#include <service/address.hpp> // for service::address
|
|
|
|
|
#include <service/endpoint.hpp>
|
|
|
|
|
#include <util/thread/logic.hpp>
|
|
|
|
|
#include <util/mem.h>
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#endif
|
|
|
|
@ -27,106 +19,156 @@ extern "C"
|
|
|
|
|
/// llarp application context for C api
|
|
|
|
|
struct llarp_main;
|
|
|
|
|
|
|
|
|
|
/// initialize application context and load config
|
|
|
|
|
struct llarp_main *
|
|
|
|
|
llarp_main_init(const char *fname, bool multiProcess);
|
|
|
|
|
/// llarp_application config
|
|
|
|
|
struct llarp_config;
|
|
|
|
|
|
|
|
|
|
/// handle signal for main context
|
|
|
|
|
void
|
|
|
|
|
llarp_main_signal(struct llarp_main *ptr, int sig);
|
|
|
|
|
/// get default config for current platform
|
|
|
|
|
struct llarp_config *
|
|
|
|
|
llarp_default_config();
|
|
|
|
|
|
|
|
|
|
/// give main context a vpn file descriptor pair (android/ios)
|
|
|
|
|
void
|
|
|
|
|
llarp_main_inject_vpn_fd(struct llarp_main *m, int rfd, int wfd);
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
|
|
/// setup main context, returns 0 on success
|
|
|
|
|
int
|
|
|
|
|
llarp_main_setup(struct llarp_main *ptr, bool debugMode);
|
|
|
|
|
/// 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 *);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// run main context, returns 0 on success, blocks until program end
|
|
|
|
|
int
|
|
|
|
|
llarp_main_run(struct llarp_main *ptr);
|
|
|
|
|
/// 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
|
|
|
|
|
struct sockaddr_storage ifaddr;
|
|
|
|
|
/// interface's netmask
|
|
|
|
|
struct sockaddr_storage netmask;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// free main context and end all operations
|
|
|
|
|
/// initialize llarp_vpn_io private implementation
|
|
|
|
|
void
|
|
|
|
|
llarp_main_free(struct llarp_main *ptr);
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
llarp_vpn_io_init(struct llarp_vpn_io *io);
|
|
|
|
|
|
|
|
|
|
/// get the packet pipe for writing IP packets to lokinet internals
|
|
|
|
|
/// can return nullptr
|
|
|
|
|
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
|
|
|
|
|
/// can return nullptr
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
void
|
|
|
|
|
llarp_main_abort(struct llarp_main *ptr);
|
|
|
|
|
llarp_vpn_io_close_async(struct llarp_vpn_io *io);
|
|
|
|
|
|
|
|
|
|
/// get the default endpoint's name for injection
|
|
|
|
|
const char *
|
|
|
|
|
handleBaseCmdLineArgs(int argc, char *argv[]);
|
|
|
|
|
|
|
|
|
|
/// load nodeDB into memory
|
|
|
|
|
int
|
|
|
|
|
llarp_main_loadDatabase(struct llarp_main *ptr);
|
|
|
|
|
llarp_main_get_default_endpoint_name(struct llarp_main *m);
|
|
|
|
|
|
|
|
|
|
/// iterator on nodedb entries
|
|
|
|
|
int
|
|
|
|
|
llarp_main_iterateDatabase(struct llarp_main *ptr,
|
|
|
|
|
struct llarp_nodedb_iter i);
|
|
|
|
|
/// 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
|
|
|
|
|
void
|
|
|
|
|
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 void
|
|
|
|
|
llarp_main_inject_default_vpn(struct llarp_main *m, struct llarp_vpn_io *io,
|
|
|
|
|
struct llarp_vpn_ifaddr_info info)
|
|
|
|
|
{
|
|
|
|
|
llarp_main_inject_vpn_by_name(m, llarp_main_get_default_endpoint_name(m),
|
|
|
|
|
io, info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// put RC into nodeDB
|
|
|
|
|
/// load config from file by name
|
|
|
|
|
bool
|
|
|
|
|
llarp_main_putDatabase(struct llarp_main *ptr,
|
|
|
|
|
struct llarp::RouterContact &rc);
|
|
|
|
|
|
|
|
|
|
// fwd declr
|
|
|
|
|
struct check_online_request;
|
|
|
|
|
llarp_config_load_file(const char *fname, struct llarp_config **config);
|
|
|
|
|
|
|
|
|
|
/// check_online_request hook definition
|
|
|
|
|
typedef void (*check_online_request_hook_func)(struct check_online_request *);
|
|
|
|
|
/// make a main context from configuration
|
|
|
|
|
struct llarp_main *
|
|
|
|
|
llarp_main_init_from_config(struct llarp_config *conf);
|
|
|
|
|
|
|
|
|
|
struct check_online_request
|
|
|
|
|
/// initialize application context and load config
|
|
|
|
|
static struct llarp_main *
|
|
|
|
|
llarp_main_init(const char *fname, bool)
|
|
|
|
|
{
|
|
|
|
|
struct llarp_main *ptr;
|
|
|
|
|
struct llarp_router_lookup_job *job;
|
|
|
|
|
bool online;
|
|
|
|
|
size_t nodes;
|
|
|
|
|
bool first;
|
|
|
|
|
check_online_request_hook_func hook;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// get RC from DHT but wait until online
|
|
|
|
|
void
|
|
|
|
|
llarp_main_queryDHT(struct check_online_request *request);
|
|
|
|
|
struct llarp_config *conf = NULL;
|
|
|
|
|
if(!llarp_config_load_file(fname, &conf))
|
|
|
|
|
return NULL;
|
|
|
|
|
if(conf == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
return llarp_main_init_from_config(conf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// initialize applicatin context with all defaults
|
|
|
|
|
static struct llarp_main *
|
|
|
|
|
llarp_main_default_init()
|
|
|
|
|
{
|
|
|
|
|
struct llarp_config *conf;
|
|
|
|
|
conf = llarp_default_config();
|
|
|
|
|
if(conf == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
return llarp_main_init_from_config(conf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// get RC from DHT
|
|
|
|
|
/// handle signal for main context
|
|
|
|
|
void
|
|
|
|
|
llarp_main_queryDHT_RC(struct llarp_main *ptr,
|
|
|
|
|
struct llarp_router_lookup_job *job);
|
|
|
|
|
|
|
|
|
|
/// set up DNS libs with a context
|
|
|
|
|
bool
|
|
|
|
|
llarp_main_init_dnsd(struct llarp_main *ptr, struct dnsd_context *dnsd,
|
|
|
|
|
const llarp::Addr &dnsd_sockaddr,
|
|
|
|
|
const llarp::Addr &dnsc_sockaddr);
|
|
|
|
|
|
|
|
|
|
/// set up dotLokiLookup with logic for setting timers
|
|
|
|
|
bool
|
|
|
|
|
llarp_main_init_dotLokiLookup(struct llarp_main *ptr,
|
|
|
|
|
struct dotLokiLookup *dll);
|
|
|
|
|
llarp_main_signal(struct llarp_main *ptr, int sig);
|
|
|
|
|
|
|
|
|
|
llarp::RouterContact *
|
|
|
|
|
llarp_main_getLocalRC(struct llarp_main *ptr);
|
|
|
|
|
/// get RC from nodeDB
|
|
|
|
|
llarp::RouterContact *
|
|
|
|
|
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk);
|
|
|
|
|
/// setup main context, returns 0 on success
|
|
|
|
|
int
|
|
|
|
|
llarp_main_setup(struct llarp_main *ptr, bool debugMode);
|
|
|
|
|
|
|
|
|
|
llarp_tun_io *
|
|
|
|
|
main_router_getRange(struct llarp_main *ptr);
|
|
|
|
|
/// run main context, returns 0 on success, blocks until program end
|
|
|
|
|
int
|
|
|
|
|
llarp_main_run(struct llarp_main *ptr);
|
|
|
|
|
|
|
|
|
|
/// map an (host byte order) ip to a hidden service address
|
|
|
|
|
bool
|
|
|
|
|
main_router_mapAddress(struct llarp_main *ptr,
|
|
|
|
|
const llarp::service::Address &addr, uint32_t ip);
|
|
|
|
|
/// free main context and end all operations
|
|
|
|
|
void
|
|
|
|
|
llarp_main_free(struct llarp_main *ptr);
|
|
|
|
|
|
|
|
|
|
/// info of possible path usage
|
|
|
|
|
bool
|
|
|
|
|
main_router_prefetch(struct llarp_main *ptr,
|
|
|
|
|
const llarp::service::Address &addr);
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|