mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
progress (does not compile)
This commit is contained in:
parent
b7d4499f43
commit
f50a49b7cb
|
@ -1,2 +1,4 @@
|
|||
[router]
|
||||
|
||||
|
||||
[links]
|
||||
eth0=ip
|
|
@ -1,28 +1,78 @@
|
|||
#include <llarp.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
struct llarp_router * router;
|
||||
struct llarp_config * gconfig;
|
||||
struct llarp_ev_loop * mainloop;
|
||||
struct llarp_main
|
||||
{
|
||||
struct llarp_router * router;
|
||||
struct llarp_threadpool * tp;
|
||||
struct llarp_config * config;
|
||||
struct llarp_ev_loop * mainloop;
|
||||
};
|
||||
|
||||
void iter_main_config(struct llarp_config_iterator * itr, const char * section, const char * key, const char * val)
|
||||
{
|
||||
struct llarp_main * m = (struct llarp_main *) itr->user;
|
||||
if(!strcmp(section, "threadpool"))
|
||||
{
|
||||
if(!strcmp(key, "workers"))
|
||||
{
|
||||
int workers = atoi(val);
|
||||
if(!m->tp && workers > 0)
|
||||
{
|
||||
m->tp = llarp_init_threadpool(workers);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int shutdown_llarp(struct llarp_main * m)
|
||||
{
|
||||
printf("Shutting down .");
|
||||
llarp_stop_router(m->router);
|
||||
printf(".");
|
||||
llarp_ev_loop_stop(m->mainloop);
|
||||
printf(".");
|
||||
llarp_threadpool_join(m->tp);
|
||||
printf(".");
|
||||
llarp_free_router(&m->router);
|
||||
printf(".");
|
||||
llarp_free_config(&m->config);
|
||||
printf(".");
|
||||
llarp_ev_loop_free(&m->mainloop);
|
||||
printf(".");
|
||||
llarp_free_threadpool(&m->tp);
|
||||
printf(".\n");
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
struct llarp_main llarp = {
|
||||
NULL, NULL, NULL, NULL
|
||||
};
|
||||
const char * conffname = "daemon.ini";
|
||||
if (argc > 1)
|
||||
conffname = argv[1];
|
||||
llarp_mem_jemalloc();
|
||||
llarp_new_config(&gconfig);
|
||||
llarp_ev_loop_alloc(&mainloop);
|
||||
llarp_new_config(&llarp.config);
|
||||
llarp_ev_loop_alloc(&llarp.mainloop);
|
||||
printf("%s loaded\n", LLARP_VERSION);
|
||||
if(!llarp_load_config(gconfig, conffname))
|
||||
if(!llarp_load_config(llarp.config, conffname))
|
||||
{
|
||||
printf("Loaded config %s\n", conffname);
|
||||
llarp_init_router(&router);
|
||||
if(!llarp_configure_router(router, gconfig))
|
||||
struct llarp_config_iterator iter;
|
||||
iter.user = &llarp;
|
||||
iter.visit = iter_main_config;
|
||||
llarp_config_iter(llarp.config, &iter);
|
||||
if(!llarp.tp)
|
||||
llarp.tp = llarp_init_threadpool(2);
|
||||
llarp.router = llarp_init_router(llarp.tp);
|
||||
if(!llarp_configure_router(llarp.router, llarp.config))
|
||||
{
|
||||
printf("Running\n");
|
||||
llarp_run_router(router, mainloop);
|
||||
llarp_ev_loop_run(mainloop);
|
||||
llarp_run_router(llarp.router, llarp.mainloop);
|
||||
llarp_ev_loop_run(llarp.mainloop);
|
||||
}
|
||||
else
|
||||
printf("Failed to configure router\n");
|
||||
|
@ -30,12 +80,5 @@ int main(int argc, char * argv[])
|
|||
else
|
||||
printf("Failed to load config %s\n", conffname);
|
||||
|
||||
printf("Shutting down.");
|
||||
llarp_free_router(&router);
|
||||
printf(".");
|
||||
llarp_free_config(&gconfig);
|
||||
printf(".");
|
||||
llarp_ev_loop_free(&mainloop);
|
||||
printf(".\n");
|
||||
return 0;
|
||||
return shutdown_llarp(&llarp);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
#ifndef LLARP_BUFFER_H_
|
||||
#define LLARP_BUFFER_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct llarp_buffer_t {
|
||||
char * base;
|
||||
size_t sz;
|
||||
|
|
|
@ -15,6 +15,8 @@ extern "C" {
|
|||
void llarp_ev_loop_free(struct llarp_ev_loop ** ev);
|
||||
|
||||
int llarp_ev_loop_run(struct llarp_ev_loop * ev);
|
||||
/** stop event loop and wait for it to complete all jobs */
|
||||
void llarp_ev_loop_stop(struct llarp_ev_loop * ev);
|
||||
|
||||
struct llarp_udp_listener
|
||||
{
|
||||
|
@ -29,6 +31,18 @@ extern "C" {
|
|||
int llarp_ev_add_udp_listener(struct llarp_ev_loop * ev, struct llarp_udp_listener * listener);
|
||||
|
||||
int llarp_ev_close_udp_listener(struct llarp_udp_listener * listener);
|
||||
|
||||
|
||||
struct llarp_ev_job
|
||||
{
|
||||
struct llarp_ev_loop * loop;
|
||||
void * user;
|
||||
/** work is called async when ready in the event loop thread */
|
||||
void (*work)(struct llarp_ev_job *);
|
||||
};
|
||||
|
||||
/** call work async in event loop thread (thread safe) */
|
||||
void llarp_ev_async(struct llarp_ev_loop * ev, struct llarp_ev_job job);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
30
include/llarp/ibfq.h
Normal file
30
include/llarp/ibfq.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#ifndef LLARP_IBFQ_H_
|
||||
#define LLARP_IBFQ_H_
|
||||
#include <llarp/buffer.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// forward declare
|
||||
struct llarp_msg_muxer;
|
||||
|
||||
struct llarp_link_queue;
|
||||
|
||||
struct llarp_link_queue * llarp_init_link_queue();
|
||||
void llarp_free_link_queue(struct llarp_link_queue ** queue);
|
||||
/**
|
||||
offer a full frame to the inbound frame queue
|
||||
return true if successfully added
|
||||
return false if the queue is full
|
||||
*/
|
||||
bool llarp_link_offer_frame(struct llarp_link_queue * queue, llarp_buffer_t msg);
|
||||
/** return true if we have more messages to process */
|
||||
bool llarp_link_queue_process(struct llarp_link_queue * queue, struct llarp_msg_muxer * muxer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
29
include/llarp/ibmq.h
Normal file
29
include/llarp/ibmq.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef LLARP_IBMQ_H_
|
||||
#define LLARP_IBMQ_H_
|
||||
#include <llarp/buffer.h>
|
||||
#include <stdbool.h>
|
||||
#include <llarp/msg_handler.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_mq;
|
||||
|
||||
struct llarp_mq * llarp_init_mq();
|
||||
void llarp_free_mq(struct llarp_mq ** queue);
|
||||
/**
|
||||
offer a full message to the inbound message queue
|
||||
return true if successfully added
|
||||
return false if the queue is full
|
||||
*/
|
||||
bool llarp_mq_offer(struct llarp_mq * queue, llarp_buffer_t msg);
|
||||
size_t llarp_mq_peek(struct llarp_mq * queue);
|
||||
/** return true if we have more messages to process */
|
||||
bool llarp_mq_process(struct llarp_mq * queue, struct llarp_msg_muxer * muxer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,24 +1,58 @@
|
|||
#ifndef LLARP_LINK_H_
|
||||
#define LLARP_LINK_H_
|
||||
#include <llarp/config.h>
|
||||
#include <llarp/mem.h>
|
||||
#include <llarp/router.h>
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/ibmq.h>
|
||||
#include <llarp/obmd.h>
|
||||
#include <llarp/crypto.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_inet_link;
|
||||
struct llarp_link;
|
||||
|
||||
void llarp_inet_link_alloc(struct llarp_inet_link ** link, struct llarp_alloc * mem);
|
||||
void llarp_inet_link_free(struct llarp_inet_link ** link);
|
||||
struct llarp_link * llarp_link_alloc();
|
||||
void llarp_link_free(struct llarp_link ** link);
|
||||
|
||||
void llarp_inet_link_configure(struct llarp_inet_link * link, struct llarp_config * conf);
|
||||
bool llarp_link_configure(struct llarp_link * link, const char * ifname, int af);
|
||||
|
||||
void llarp_inet_link_start(struct llarp_inet_link * link, struct llarp_router * router);
|
||||
/** get link listener for events */
|
||||
struct llarp_udp_listener * llarp_link_udp_listener(struct llarp_link * link);
|
||||
|
||||
void llarp_inet_link_stop(struct llarp_inet_link * link);
|
||||
void llarp_link_stop(struct llarp_link * link);
|
||||
|
||||
struct llarp_link_session;
|
||||
|
||||
struct llarp_link_session_listener
|
||||
{
|
||||
void * user;
|
||||
/** set by llarp_try_establish_session */
|
||||
struct llarp_link * link;
|
||||
/** set by llarp_try_establish_session */
|
||||
struct llarp_rc * rc;
|
||||
void (*result)(struct llarp_link_session_listener *, struct llarp_link_session *);
|
||||
};
|
||||
|
||||
/** information for establishing an outbound session */
|
||||
struct llarp_link_establish_job
|
||||
{
|
||||
struct llarp_rc * rc;
|
||||
uint64_t timeout;
|
||||
};
|
||||
|
||||
void llarp_link_try_establish_session(struct llarp_link * link, struct llarp_link_establish_job * job, struct llarp_link_session_listener * l);
|
||||
|
||||
|
||||
struct llarp_link_session_iter
|
||||
{
|
||||
void * user;
|
||||
struct llarp_link * link;
|
||||
bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *);
|
||||
};
|
||||
|
||||
void llarp_link_iter_sessions(struct llarp_link * l, struct llarp_link_session_iter * i);
|
||||
|
||||
struct llarp_rc * llarp_link_session_rc(struct llarp_link_session * s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
38
include/llarp/msg_handler.h
Normal file
38
include/llarp/msg_handler.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
#ifndef LLARP_MSG_HANDLER_H_
|
||||
#define LLARP_MSG_HANDLER_H_
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/link.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_frame_handler
|
||||
{
|
||||
struct llarp_obmd * outbound;
|
||||
struct llarp_ibmq * inbound;
|
||||
bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *, llarp_buffer_t);
|
||||
};
|
||||
|
||||
struct llarp_msg_handler
|
||||
{
|
||||
struct llarp_path_context * paths;
|
||||
};
|
||||
|
||||
struct llarp_msg_muxer
|
||||
{
|
||||
/** get a message handler for a link level message given msg.a */
|
||||
struct llarp_frame_handler * (*link_handler_for)(const char *);
|
||||
/** get a message handler for a routing layer message given msg.A */
|
||||
struct llarp_msg_handler * (*routing_handler_for)(const char *);
|
||||
};
|
||||
|
||||
/** fill function pointers with default values */
|
||||
void llarp_msg_handler_mux_init(struct llarp_msg_muxer * muxer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
27
include/llarp/obmd.h
Normal file
27
include/llarp/obmd.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef LLARP_OBMD_H_
|
||||
#define LLARP_OBMD_H_
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/crypto.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// forward declair
|
||||
struct llarp_link;
|
||||
|
||||
struct llarp_link_dispatcher;
|
||||
|
||||
struct llarp_link_dispatcher * llarp_init_link_dispatcher();
|
||||
void llarp_free_link_dispatcher(struct llarp_link_dispatcher ** dispatcher);
|
||||
|
||||
void llarp_link_sendto(struct llarp_link_dispatcher * dispatcher, llarp_pubkey_t pubkey, llarp_buffer_t msg);
|
||||
|
||||
void llarp_link_register(struct llarp_link_dispatcher * dispatcher, struct llarp_link * link);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
19
include/llarp/path.h
Normal file
19
include/llarp/path.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#ifndef LLARP_PATH_H
|
||||
#define LLARP_PATH_H
|
||||
|
||||
#include <llarp/types.h>
|
||||
|
||||
typedef uint64_t llarp_path_id_t;
|
||||
|
||||
struct llarp_transit_hop
|
||||
{
|
||||
llarp_path_id_t id;
|
||||
llarp_sharedkey_t symkey;
|
||||
llarp_pubkey_t nextHop;
|
||||
uint64_t started;
|
||||
uint64_t lifetime;
|
||||
llarp_version_t version;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -2,6 +2,9 @@
|
|||
#define LLARP_ROUTER_H_
|
||||
#include <llarp/config.h>
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/ibmq.h>
|
||||
#include <llarp/obmd.h>
|
||||
#include <llarp/threadpool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -9,7 +12,7 @@ extern "C" {
|
|||
|
||||
struct llarp_router;
|
||||
|
||||
void llarp_init_router(struct llarp_router ** router);
|
||||
struct llarp_router * llarp_init_router(struct llarp_threadpool * tp);
|
||||
void llarp_free_router(struct llarp_router ** router);
|
||||
|
||||
int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf);
|
||||
|
@ -17,6 +20,12 @@ extern "C" {
|
|||
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop);
|
||||
void llarp_stop_router(struct llarp_router * router);
|
||||
|
||||
|
||||
/** get router's inbound link level frame queue */
|
||||
struct llarp_link_queue * llarp_router_link_queue(struct llarp_router * router);
|
||||
/** get router's outbound link level frame dispatcher */
|
||||
struct llarp_link_dispatcher * llarp_router_link_dispatcher(struct llarp_router * router);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
31
include/llarp/threadpool.h
Normal file
31
include/llarp/threadpool.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
#ifndef LLARP_THREADPOOL_H
|
||||
#define LLARP_THREADPOOL_H
|
||||
#include <llarp/ev.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_threadpool;
|
||||
|
||||
struct llarp_threadpool * llarp_init_threadpool(int workers);
|
||||
void llarp_free_threadpool(struct llarp_threadpool ** tp);
|
||||
|
||||
/** job to be done in worker thread */
|
||||
struct llarp_thread_job
|
||||
{
|
||||
/** calls result async after work is executed */
|
||||
struct llarp_ev_job * result;
|
||||
/** called in threadpool worker thread */
|
||||
void (*work)(struct llarp_thread_job *);
|
||||
};
|
||||
|
||||
void llarp_threadpool_queue_job(struct llarp_threadpool * tp, struct llarp_thread_job j);
|
||||
|
||||
void llarp_threadpool_start(struct llarp_threadpool * tp);
|
||||
void llarp_threadpool_join(struct llarp_threadpool * tp);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,12 +1,12 @@
|
|||
#ifndef LLARP_TIME_H
|
||||
#define LLARP_TIME_H
|
||||
#include <stdint.h>
|
||||
#include <llarp/types.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint64_t llarp_time_now_ms();
|
||||
uint64_t llarp_time_now_sec();
|
||||
llarp_time_t llarp_time_now_ms();
|
||||
llarp_seconds_t llarp_time_now_sec();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
16
include/llarp/types.h
Normal file
16
include/llarp/types.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef LLARP_TYPES_H
|
||||
#define LLARP_TYEPS_H
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef uint8_t llarp_proto_version_t;
|
||||
typedef uint64_t llarp_time_t;
|
||||
typedef uint32_t llarp_seconds_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
|
@ -13,7 +13,7 @@ struct llarp_ai_list
|
|||
static std::list<llarp_ai> ai_list_to_std(struct llarp_ai_list * l)
|
||||
{
|
||||
std::list<llarp_ai> list;
|
||||
if(l->data)
|
||||
if(l && l->data)
|
||||
{
|
||||
do
|
||||
{
|
||||
|
|
|
@ -42,14 +42,14 @@ extern "C" {
|
|||
|
||||
void llarp_new_config(struct llarp_config ** conf)
|
||||
{
|
||||
llarp_config * c = llarp::alloc<llarp_config>();
|
||||
llarp_config * c = new llarp_config;
|
||||
*conf = c;
|
||||
}
|
||||
|
||||
void llarp_free_config(struct llarp_config ** conf)
|
||||
{
|
||||
if(*conf)
|
||||
llarp_g_mem.free(*conf);
|
||||
delete *conf;
|
||||
*conf = nullptr;
|
||||
}
|
||||
|
||||
|
|
23
llarp/ev.cpp
23
llarp/ev.cpp
|
@ -6,6 +6,16 @@ struct llarp_ev_loop
|
|||
{
|
||||
uv_loop_t _loop;
|
||||
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_ev_loop>());
|
||||
}
|
||||
|
||||
static void operator delete(void * ptr)
|
||||
{
|
||||
llarp_g_mem.free(ptr);
|
||||
}
|
||||
|
||||
uv_loop_t * loop() { return &_loop; }
|
||||
};
|
||||
|
||||
|
@ -13,6 +23,11 @@ namespace llarp
|
|||
{
|
||||
struct udp_listener
|
||||
{
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
return llarp_g_mem.alloc(sz, alignment<udp_listener>());
|
||||
}
|
||||
|
||||
uv_udp_t _handle;
|
||||
struct llarp_udp_listener * listener;
|
||||
|
||||
|
@ -35,7 +50,7 @@ namespace llarp
|
|||
|
||||
static void udp_alloc_cb(uv_handle_t * h, size_t sz, uv_buf_t * buf)
|
||||
{
|
||||
buf->base = static_cast<char *>(llarp_g_mem.alloc(sz, 512));
|
||||
buf->base = static_cast<char *>(llarp_g_mem.alloc(sz, 1024));
|
||||
buf->len = sz;
|
||||
}
|
||||
|
||||
|
@ -58,7 +73,7 @@ namespace llarp
|
|||
extern "C" {
|
||||
void llarp_ev_loop_alloc(struct llarp_ev_loop ** ev)
|
||||
{
|
||||
*ev = llarp::alloc<llarp_ev_loop>();
|
||||
*ev = new llarp_ev_loop;
|
||||
if (*ev)
|
||||
{
|
||||
uv_loop_init((*ev)->loop());
|
||||
|
@ -85,7 +100,7 @@ extern "C" {
|
|||
sockaddr_in6 addr;
|
||||
uv_ip6_addr(listener->host, listener->port, &addr);
|
||||
int ret = 0;
|
||||
llarp::udp_listener * l = llarp::alloc<llarp::udp_listener>();
|
||||
llarp::udp_listener * l = new llarp::udp_listener;
|
||||
listener->impl = l;
|
||||
l->udp()->data = l;
|
||||
l->listener = listener;
|
||||
|
@ -113,7 +128,7 @@ extern "C" {
|
|||
if(!uv_udp_recv_stop(l->udp()))
|
||||
{
|
||||
l->closed();
|
||||
llarp_g_mem.free(l);
|
||||
delete l;
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace llarp
|
|||
auto itr = link->sessions.find(remote);
|
||||
if(itr == link->sessions.end())
|
||||
{
|
||||
link->sessions[remote] = std::make_unique<PeerSession>(link->_crypto, remote);
|
||||
link->sessions[remote] = std::make_shared<PeerSession>(link->_crypto, remote);
|
||||
}
|
||||
link->sessions[remote]->RecvFrom(buff, sz);
|
||||
}
|
||||
|
@ -32,9 +32,6 @@ namespace llarp
|
|||
_listener.recvfrom = link_recv_from;
|
||||
}
|
||||
|
||||
Link::~Link()
|
||||
{
|
||||
}
|
||||
|
||||
PeerSession::PeerSession(llarp_crypto * crypto, sockaddr_in6 remote) :
|
||||
lastRX(0),
|
||||
|
|
|
@ -43,22 +43,18 @@ namespace llarp
|
|||
PeerSession(llarp_crypto * crypto, sockaddr_in6 remote);
|
||||
/** outbound session */
|
||||
PeerSession(llarp_crypto * crypto, llarp_rc rc);
|
||||
|
||||
PeerSession & operator=(const PeerSession & other);
|
||||
|
||||
void SendTo(Link * link, const char * buff, std::size_t sz);
|
||||
|
||||
void RecvFrom(const char * buff, ssize_t sz);
|
||||
|
||||
|
||||
|
||||
typedef std::shared_ptr<PeerSession> Ptr;
|
||||
};
|
||||
|
||||
typedef std::unique_ptr<PeerSession> PeerSession_ptr;
|
||||
|
||||
typedef std::function<void(const PeerSession_ptr &)> PeerSessionVisitor;
|
||||
|
||||
struct Link
|
||||
{
|
||||
typedef std::map<sockaddr_in6, PeerSession_ptr> Sessions;
|
||||
typedef std::map<sockaddr_in6, PeerSession::Ptr> Sessions;
|
||||
|
||||
Sessions sessions;
|
||||
llarp_seckey_t transportSecKey;
|
||||
|
@ -67,13 +63,10 @@ namespace llarp
|
|||
llarp_crypto * _crypto;
|
||||
|
||||
Link(llarp_crypto * crypto);
|
||||
~Link();
|
||||
|
||||
llarp_udp_listener _listener;
|
||||
|
||||
llarp_udp_listener * Listener() { return &_listener; }
|
||||
|
||||
bool VisitPeerByIdent(PeerSessionVisitor v);
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -9,13 +9,6 @@ namespace llarp
|
|||
{
|
||||
return std::exp2(1+std::floor(std::log2(sizeof(T))));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T * alloc(llarp_alloc * mem=nullptr)
|
||||
{
|
||||
if(!mem) mem = &llarp_g_mem;
|
||||
return static_cast<T*>(mem->alloc(sizeof(T), alignment<T>()));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace llarp
|
|||
{
|
||||
static void * jem_malloc(size_t sz, size_t align)
|
||||
{
|
||||
return mallocx(sz, MALLOCX_ZERO | MALLOCX_ALIGN(align));
|
||||
return mallocx(sz, MALLOCX_ALIGN(align));
|
||||
}
|
||||
|
||||
static void jem_free(void * ptr)
|
||||
|
|
112
llarp/router.cpp
112
llarp/router.cpp
|
@ -2,55 +2,75 @@
|
|||
#include <llarp/link.h>
|
||||
#include "link.hpp"
|
||||
#include "mem.hpp"
|
||||
#include <list>
|
||||
#include "str.hpp"
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val);
|
||||
|
||||
|
||||
struct Router
|
||||
struct router_links
|
||||
{
|
||||
std::list<Link *> Links;
|
||||
llarp_crypto * crypto;
|
||||
void Close()
|
||||
{
|
||||
if(Links.size())
|
||||
{
|
||||
for(auto & itr : Links)
|
||||
{
|
||||
llarp_ev_close_udp_listener(itr->Listener());
|
||||
}
|
||||
Links.clear();
|
||||
}
|
||||
}
|
||||
|
||||
bool Configured()
|
||||
{
|
||||
if(Links.size()) return true;
|
||||
return false;
|
||||
}
|
||||
struct llarp_link * link = nullptr;
|
||||
struct router_links * next = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
struct llarp_router
|
||||
{
|
||||
struct llarp_threadpool * tp;
|
||||
llarp::router_links links;
|
||||
llarp_crypto crypto;
|
||||
|
||||
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
|
||||
}
|
||||
|
||||
static void operator delete(void * ptr)
|
||||
{
|
||||
llarp_g_mem.free(ptr);
|
||||
}
|
||||
|
||||
void AddLink(struct llarp_link * link)
|
||||
{
|
||||
llarp::router_links * head = &links;
|
||||
while(head->next && head->link)
|
||||
head = head->next;
|
||||
|
||||
if(head->link)
|
||||
head->next = new llarp::router_links{link, nullptr};
|
||||
else
|
||||
head->link = link;
|
||||
}
|
||||
|
||||
void ForEachLink(std::function<void(llarp_link *)> visitor)
|
||||
{
|
||||
llarp::router_links * cur = &links;
|
||||
do
|
||||
{
|
||||
if(cur->link)
|
||||
visitor(cur->link);
|
||||
cur = cur->next;
|
||||
}
|
||||
while(cur);
|
||||
}
|
||||
|
||||
void Close()
|
||||
{
|
||||
ForEachLink(llarp_link_stop);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct llarp_router
|
||||
struct llarp_router * llarp_init_router(struct llarp_threadpool * tp)
|
||||
{
|
||||
llarp::Router impl;
|
||||
llarp_crypto crypto;
|
||||
};
|
||||
|
||||
void llarp_init_router(struct llarp_router ** router)
|
||||
{
|
||||
*router = llarp::alloc<llarp_router>(&llarp_g_mem);
|
||||
if(*router)
|
||||
{
|
||||
llarp_crypto_libsodium_init(&(*router)->crypto);
|
||||
}
|
||||
llarp_router * router = new llarp_router;
|
||||
router->tp = tp;
|
||||
llarp_crypto_libsodium_init(&router->crypto);
|
||||
return router;
|
||||
}
|
||||
|
||||
int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf)
|
||||
|
@ -59,14 +79,14 @@ extern "C" {
|
|||
iter.user = router;
|
||||
iter.visit = llarp::router_iter_config;
|
||||
llarp_config_iter(conf, &iter);
|
||||
return router->impl.Configured() ? 0 : -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop)
|
||||
{
|
||||
if(router->impl.Links.size())
|
||||
for(auto & iter : router->impl.Links)
|
||||
llarp_ev_add_udp_listener(loop, iter->Listener());
|
||||
router->ForEachLink([loop](llarp_link * link) {
|
||||
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
|
||||
});
|
||||
}
|
||||
|
||||
void llarp_free_router(struct llarp_router ** router)
|
||||
|
@ -74,8 +94,9 @@ extern "C" {
|
|||
if(*router)
|
||||
{
|
||||
llarp_router * r = *router;
|
||||
r->impl.Close();
|
||||
llarp_g_mem.free(r);
|
||||
r->Close();
|
||||
r->ForEachLink([](llarp_link * link) { llarp_g_mem.free(link); });
|
||||
delete r;
|
||||
}
|
||||
*router = nullptr;
|
||||
}
|
||||
|
@ -91,7 +112,14 @@ namespace llarp
|
|||
{
|
||||
if(StrEq(val, "ip"))
|
||||
{
|
||||
self->impl.Links.push_back(new Link(&self->crypto));
|
||||
struct llarp_link * link = llarp_link_alloc();
|
||||
if(llarp_link_configure(link, key, AF_INET6))
|
||||
self->AddLink(link);
|
||||
else
|
||||
{
|
||||
llarp_link_free(&link);
|
||||
printf("failed to configure %s link for %s\n", val, key);
|
||||
}
|
||||
}
|
||||
else if (StrEq(val, "eth"))
|
||||
{
|
||||
|
|
|
@ -5,21 +5,21 @@ namespace llarp
|
|||
{
|
||||
typedef std::chrono::steady_clock clock_t;
|
||||
|
||||
template<typename Res>
|
||||
static uint64_t time_since_epoch()
|
||||
template<typename Res, typename IntType>
|
||||
static IntType time_since_epoch()
|
||||
{
|
||||
return std::chrono::duration_cast<Res>(llarp::clock_t::now().time_since_epoch()).count();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
uint64_t llarp_time_now_ms()
|
||||
llarp_time_t llarp_time_now_ms()
|
||||
{
|
||||
return llarp::time_since_epoch<std::chrono::milliseconds>();
|
||||
return llarp::time_since_epoch<std::chrono::milliseconds, llarp_time_t>();
|
||||
}
|
||||
|
||||
uint64_t llarp_time_now_sec()
|
||||
llarp_seconds_t llarp_time_now_sec()
|
||||
{
|
||||
return llarp::time_since_epoch<std::chrono::seconds>();
|
||||
return llarp::time_since_epoch<std::chrono::seconds, llarp_seconds_t>();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue