From 3d76e3d4bdd8f3e077042c462d30276d4620d520 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 8 Apr 2021 09:23:45 -0400 Subject: [PATCH] split up liblokinet headers --- include/lokinet.h | 135 ++---------------------------- include/lokinet/lokinet_addr.h | 15 ++++ include/lokinet/lokinet_context.h | 55 ++++++++++++ include/lokinet/lokinet_misc.h | 27 ++++++ include/lokinet/lokinet_srv.h | 61 ++++++++++++++ include/lokinet/lokinet_stream.h | 62 ++++++++++++++ 6 files changed, 226 insertions(+), 129 deletions(-) create mode 100644 include/lokinet/lokinet_addr.h create mode 100644 include/lokinet/lokinet_context.h create mode 100644 include/lokinet/lokinet_misc.h create mode 100644 include/lokinet/lokinet_srv.h create mode 100644 include/lokinet/lokinet_stream.h diff --git a/include/lokinet.h b/include/lokinet.h index 96f8b88a1..6a920924f 100644 --- a/include/lokinet.h +++ b/include/lokinet.h @@ -1,131 +1,8 @@ -#ifndef LOKINET_H -#define LOKINET_H +#pragma once -#include -#include -#include +#include "lokinet/lokinet_context.h" +#include "lokinet/lokinet_srv.h" +#include "lokinet/lokinet_misc.h" +#include "lokinet/lokinet_addr.h" +#include "lokinet/lokinet_stream.h" -#ifdef __cplusplus -extern "C" -{ -#endif - - struct lokinet_context; - - /// allocate a new lokinet context - struct lokinet_context* - lokinet_context_new(); - - /// change our network id globally across all contexts - void - lokinet_set_netid(const char*); - - /// get our current netid - /// must be free()'d after use - const char* - lokinet_get_netid(); - - /// load a bootstrap RC from memory - /// return 0 on success - /// return non zero on fail - int - lokinet_add_bootstrap_rc(const char*, size_t, struct lokinet_context*); - - /// set log level - /// possible values: trace, debug, info, warn, error, none - /// return 0 on success - /// return non zero on fail - int - lokinet_log_level(const char*); - - /// free a context allocated by lokinet_context_new - void - lokinet_context_free(struct lokinet_context*); - - /// spawn all the threads needed for operation and start running - /// return 0 on success - /// return non zero on fail - int - lokinet_context_start(struct lokinet_context*); - - /// return 0 if we our endpoint has published on the network and is ready to send - /// return -1 if we don't have enough paths ready - /// retrun -2 if we look deadlocked - /// retrun -3 if context was null or not started yet - int - lokinet_status(struct lokinet_context*); - - /// wait at most N milliseconds for lokinet to build paths and get ready - /// return 0 if we are ready - /// return nonzero if we are not ready - int - lokinet_wait_for_ready(int N, struct lokinet_context*); - - /// stop all operations on this lokinet context - void - lokinet_context_stop(struct lokinet_context*); - - /// get default lokinet context - /// not to be freed by lokinet_context_free - struct lokinet_context* - lokinet_default(); - - /// get a free()-able null terminated string that holds our .loki address - /// returns NULL if we dont have one right now - char* - lokinet_address(struct lokinet_context*); - - /// the result of a lokinet stream mapping attempt -#pragma pack(1) - struct lokinet_stream_result - { - /// set to zero on success otherwise the error that happened - /// use strerror(3) to get printable string of this error - int error; - - /// the local ip address we mapped the remote endpoint to - /// null terminated - char local_address[256]; - /// the local port we mapped the remote endpoint to - int local_port; - /// the id of the stream we created - int stream_id; - }; -#pragma pack() - - /// connect out to a remote endpoint - /// remoteAddr is in the form of "name:port" - /// localAddr is either NULL for any or in the form of "ip:port" to bind to an explicit address - void - lokinet_outbound_stream( - struct lokinet_stream_result* result, - const char* remoteAddr, - const char* localAddr, - struct lokinet_context* context); - - /// stream accept filter determines if we should accept a stream or not - /// return 0 to accept - /// return -1 to explicitly reject - /// return -2 to silently drop - typedef int (*lokinet_stream_filter)(const char* remote, uint16_t port, void*); - - /// set stream accepter filter - /// passes user parameter into stream filter as void * - /// returns stream id - int - lokinet_inbound_stream_filter( - lokinet_stream_filter acceptFilter, void* user, struct lokinet_context* context); - - /// simple stream acceptor - /// simple variant of lokinet_inbound_stream_filter that maps port to localhost:port - int - lokinet_inbound_stream(uint16_t port, struct lokinet_context* context); - - /// close a stream by id - void - lokinet_close_stream(int stream_id, struct lokinet_context* context); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/include/lokinet/lokinet_addr.h b/include/lokinet/lokinet_addr.h new file mode 100644 index 000000000..d67771f75 --- /dev/null +++ b/include/lokinet/lokinet_addr.h @@ -0,0 +1,15 @@ +#pragma once +#include "lokinet_context.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/// get a free()-able null terminated string that holds our .loki address + /// returns NULL if we dont have one right now + char* + lokinet_address(struct lokinet_context*); +#ifdef __cplusplus +} +#endif diff --git a/include/lokinet/lokinet_context.h b/include/lokinet/lokinet_context.h new file mode 100644 index 000000000..e6fd2f408 --- /dev/null +++ b/include/lokinet/lokinet_context.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include + + +#ifdef __cplusplus +extern "C" +{ +#endif + + struct lokinet_context; + + /// allocate a new lokinet context + struct lokinet_context* + lokinet_context_new(); + + /// free a context allocated by lokinet_context_new + void + lokinet_context_free(struct lokinet_context*); + + /// spawn all the threads needed for operation and start running + /// return 0 on success + /// return non zero on fail + int + lokinet_context_start(struct lokinet_context*); + + /// return 0 if we our endpoint has published on the network and is ready to send + /// return -1 if we don't have enough paths ready + /// retrun -2 if we look deadlocked + /// retrun -3 if context was null or not started yet + int + lokinet_status(struct lokinet_context*); + + /// wait at most N milliseconds for lokinet to build paths and get ready + /// return 0 if we are ready + /// return nonzero if we are not ready + int + lokinet_wait_for_ready(int N, struct lokinet_context*); + + /// stop all operations on this lokinet context + void + lokinet_context_stop(struct lokinet_context*); + + /// load a bootstrap RC from memory + /// return 0 on success + /// return non zero on fail + int + lokinet_add_bootstrap_rc(const char*, size_t, struct lokinet_context*); + + +#ifdef __cplusplus +} +#endif diff --git a/include/lokinet/lokinet_misc.h b/include/lokinet/lokinet_misc.h new file mode 100644 index 000000000..7d5ad04ca --- /dev/null +++ b/include/lokinet/lokinet_misc.h @@ -0,0 +1,27 @@ +#pragma once + + +#ifdef __cplusplus +extern "C" +{ +#endif + + /// change our network id globally across all contexts + void + lokinet_set_netid(const char*); + + /// get our current netid + /// must be free()'d after use + const char* + lokinet_get_netid(); + + /// set log level + /// possible values: trace, debug, info, warn, error, none + /// return 0 on success + /// return non zero on fail + int + lokinet_log_level(const char*); + +#ifdef __cplusplus +} +#endif diff --git a/include/lokinet/lokinet_srv.h b/include/lokinet/lokinet_srv.h new file mode 100644 index 000000000..654a322f2 --- /dev/null +++ b/include/lokinet/lokinet_srv.h @@ -0,0 +1,61 @@ +#pragma once + +#include "lokinet_context.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + // a single srv record + struct lokinet_srv_record + { + /// the srv priority of the record + uint16_t priority; + /// the weight of this record + uint16_t weight; + /// null terminated string of the hostname + char * target; + /// the port to use + int port; + }; + + /// private members of a srv lookup + struct lokinet_srv_lookup_private; + + /// the result of an srv lookup + struct lokinet_srv_lookup_result + { + /// pointer to internal members + /// dont touch me + struct lokinet_srv_lookup_private * internal; + /// set to zero on success otherwise is the error code + /// + int error; + }; + + /// do a srv lookup on host for service + /// caller MUST call lokinet_srv_lookup_done when they are done handling the result + void + lokinet_srv_lookup(char * host, char * service, struct lokinet_srv_lookup_result * result, struct lokinet_context * ctx); + + + /// a hook function to handle each srv record in a srv lookup result + /// passes in NULL when we are at the end of iteration + /// passes in void * user data + /// hook should NOT free the record + typedef void (*lokinet_srv_record_iterator)(struct lokinet_srv_record *, void *); + + /// iterate over each srv record in a lookup result + /// user is passes into hook and called for each result and then with NULL as the result on the end of iteration + void + lokinet_for_each_srv_record(struct lokinet_srv_lookup_result * result, lokinet_srv_record_iterator iter, void * user); + + + /// free internal members of a srv lookup result after use of the result + void + lokinet_srv_lookup_done(struct lokinet_srv_lookup_result * result); + +#ifdef __cplusplus +} +#endif diff --git a/include/lokinet/lokinet_stream.h b/include/lokinet/lokinet_stream.h new file mode 100644 index 000000000..3d42344e7 --- /dev/null +++ b/include/lokinet/lokinet_stream.h @@ -0,0 +1,62 @@ +#pragma once + +#include "lokinet_context.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + /// the result of a lokinet stream mapping attempt +#pragma pack(1) + struct lokinet_stream_result + { + /// set to zero on success otherwise the error that happened + /// use strerror(3) to get printable string of this error + int error; + + /// the local ip address we mapped the remote endpoint to + /// null terminated + char local_address[256]; + /// the local port we mapped the remote endpoint to + int local_port; + /// the id of the stream we created + int stream_id; + }; +#pragma pack() + + /// connect out to a remote endpoint + /// remoteAddr is in the form of "name:port" + /// localAddr is either NULL for any or in the form of "ip:port" to bind to an explicit address + void + lokinet_outbound_stream( + struct lokinet_stream_result* result, + const char* remoteAddr, + const char* localAddr, + struct lokinet_context* context); + + /// stream accept filter determines if we should accept a stream or not + /// return 0 to accept + /// return -1 to explicitly reject + /// return -2 to silently drop + typedef int (*lokinet_stream_filter)(const char* remote, uint16_t port, void*); + + /// set stream accepter filter + /// passes user parameter into stream filter as void * + /// returns stream id + int + lokinet_inbound_stream_filter( + lokinet_stream_filter acceptFilter, void* user, struct lokinet_context* context); + + /// simple stream acceptor + /// simple variant of lokinet_inbound_stream_filter that maps port to localhost:port + int + lokinet_inbound_stream(uint16_t port, struct lokinet_context* context); + + /// close a stream by id + void + lokinet_close_stream(int stream_id, struct lokinet_context* context); + +#ifdef __cplusplus +} +#endif