mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
more
This commit is contained in:
parent
dcc5e6e348
commit
8c2bbbb2eb
10
Makefile
10
Makefile
|
@ -40,13 +40,13 @@ ifneq ($(GIT_VERSION),"")
|
|||
VER_FLAGS=-DGIT_REV=\"$(GIT_VERSION)\"
|
||||
endif
|
||||
|
||||
ifeq ($(JEMALLOC),"1")
|
||||
MALLOC_LIB=-ljemalloc
|
||||
else
|
||||
MALLOC_LIB=
|
||||
ifeq ($(JEMALLOC),"1")
|
||||
MALLOC_LIB=-ljemalloc
|
||||
else
|
||||
MALLOC_LIB=
|
||||
endif
|
||||
|
||||
REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c99 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
|
||||
REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c11 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
|
||||
REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++14 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
|
||||
LIB_LDFLAGS = $(MALLOC_LIB) $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++
|
||||
REQUIRED_LDFLAGS = -L$(REPO) -lllarp
|
||||
|
|
|
@ -110,8 +110,8 @@ An exit info (XI) defines a exit address that can relay exit traffic to the
|
|||
internet.
|
||||
|
||||
{
|
||||
a: "<16 bytes big endian ipv6 address>",
|
||||
b: "<16 bytes big endian ipv6 netmask>",
|
||||
a: "<net address exit address>",
|
||||
b: "<net address exit netmask>",
|
||||
k: "<32 bytes public encryption/signing key>",
|
||||
v: 0
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ information required to access the internet via that exit address.
|
|||
a: "<16 bytes big endian ipv6 gateway address>",
|
||||
b: "<16 bytes big endian ipv6 netmask>",
|
||||
c: "<16 bytes big endian ipv6 source address>",
|
||||
e: lifetime_in_seconds_uint64,
|
||||
l: lifetime_in_seconds_uint64,
|
||||
v: 0
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef LLARP_BENCODE_H
|
||||
#define LLARP_BENCODE_H
|
||||
#include <llarp/common.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <llarp/buffer.h>
|
||||
|
@ -9,48 +10,53 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool inline bencode_write_bytestring(llarp_buffer_t * buff, const void * data, size_t sz)
|
||||
bool INLINE bencode_write_bytestring(llarp_buffer_t * buff, const void * data, size_t sz)
|
||||
{
|
||||
if(!llarp_buffer_writef(buff, "%ld:", sz)) return false;
|
||||
return llarp_buffer_write(buff, data, sz);
|
||||
}
|
||||
|
||||
bool inline bencode_write_int(llarp_buffer_t * buff, int i)
|
||||
bool INLINE bencode_write_int(llarp_buffer_t * buff, int i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%de", i);
|
||||
}
|
||||
|
||||
bool inline bencode_write_uint16(llarp_buffer_t * buff, uint16_t i)
|
||||
bool INLINE bencode_write_uint16(llarp_buffer_t * buff, uint16_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%de", i);
|
||||
}
|
||||
|
||||
bool inline bencode_write_int64(llarp_buffer_t * buff, int64_t i)
|
||||
bool INLINE bencode_write_int64(llarp_buffer_t * buff, int64_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%lde", i);
|
||||
}
|
||||
|
||||
bool INLINE bencode_write_uint64(llarp_buffer_t * buff, uint64_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%lde", i);
|
||||
}
|
||||
|
||||
bool inline bencode_write_sizeint(llarp_buffer_t * buff, size_t i)
|
||||
bool INLINE bencode_write_sizeint(llarp_buffer_t * buff, size_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%lde", i);
|
||||
}
|
||||
|
||||
bool inline bencode_start_list(llarp_buffer_t * buff)
|
||||
bool INLINE bencode_start_list(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_write(buff, "l", 1);
|
||||
}
|
||||
|
||||
bool inline bencode_start_dict(llarp_buffer_t * buff)
|
||||
bool INLINE bencode_start_dict(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_write(buff, "d", 1);
|
||||
}
|
||||
|
||||
bool inline bencode_end(llarp_buffer_t * buff)
|
||||
bool INLINE bencode_end(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_write(buff, "e", 1);
|
||||
}
|
||||
|
||||
bool inline bencode_write_version_entry(llarp_buffer_t * buff)
|
||||
bool INLINE bencode_write_version_entry(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef LLARP_BUFFER_H_
|
||||
#define LLARP_BUFFER_H_
|
||||
|
||||
#include <llarp/common.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -14,14 +14,14 @@ typedef struct llarp_buffer_t {
|
|||
char *cur;
|
||||
} llarp_buffer_t;
|
||||
|
||||
inline size_t llarp_buffer_size_left(llarp_buffer_t *buff)
|
||||
size_t INLINE llarp_buffer_size_left(llarp_buffer_t *buff)
|
||||
{
|
||||
size_t diff = buff->cur - buff->base;
|
||||
if ( diff > buff->sz) return 0;
|
||||
else return buff->sz - diff;
|
||||
}
|
||||
|
||||
inline bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz)
|
||||
bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz)
|
||||
{
|
||||
size_t left = llarp_buffer_size_left(buff);
|
||||
if (left >= sz)
|
||||
|
|
8
include/llarp/common.h
Normal file
8
include/llarp/common.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef LLARP_COMMON_H
|
||||
#define LLARP_COMMON_H
|
||||
#ifdef __STRICT_ANSI__
|
||||
#define INLINE __inline__
|
||||
#else
|
||||
#define INLINE inline
|
||||
#endif
|
||||
#endif
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef LLARP_CRYPTO_H_
|
||||
#define LLARP_CRYPTO_H_
|
||||
#include <llarp/common.h>
|
||||
#include <llarp/buffer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
@ -27,7 +28,7 @@ typedef uint8_t llarp_hmacsec_t[HMACSECSIZE];
|
|||
typedef uint8_t llarp_sig_t[SIGSIZE];
|
||||
typedef uint8_t llarp_tunnel_nounce_t[TUNNOUNCESIZE];
|
||||
|
||||
static inline uint8_t *llarp_seckey_topublic(llarp_seckey_t k) {
|
||||
static INLINE uint8_t *llarp_seckey_topublic(llarp_seckey_t k) {
|
||||
return k + 32;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef LLARP_XI_H
|
||||
#define LLARP_XI_H
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/crypto.h>
|
||||
#include <llarp/net.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -10,6 +11,7 @@ extern "C" {
|
|||
struct llarp_xi {
|
||||
struct in6_addr address;
|
||||
struct in6_addr netmask;
|
||||
llarp_pubkey_t pubkey;
|
||||
};
|
||||
|
||||
bool llarp_xi_bdecode(struct llarp_xi *xi, llarp_buffer_t *buf);
|
||||
|
|
25
include/llarp/exit_route.h
Normal file
25
include/llarp/exit_route.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef LLARP_XR_H
|
||||
#define LLARP_XR_H
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/net.h>
|
||||
#include <stdint.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_xr
|
||||
{
|
||||
struct in6_addr gateway;
|
||||
struct in6_addr netmask;
|
||||
struct in6_addr source;
|
||||
uint64_t lifetime;
|
||||
};
|
||||
|
||||
bool llarp_xr_bencode(struct llarp_xr * xr, llarp_buffer_t * buff);
|
||||
bool llarp_xr_bdecode(struct llarp_xr * xr, llarp_buffer_t * buff);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
|
@ -8,7 +8,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
// forward declair
|
||||
/* forward decl */
|
||||
struct llarp_link;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,26 +1,12 @@
|
|||
#include "address_info.hpp"
|
||||
#include <llarp/address_info.h>
|
||||
#include <llarp/bencode.h>
|
||||
#include <cstring>
|
||||
#include <llarp/mem.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
namespace llarp {
|
||||
/*
|
||||
template <>
|
||||
bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) {
|
||||
return bencodeDict(buff) && bencodeDict_Int(buff, "c", a.rank) &&
|
||||
bencodeDict_Bytes(buff, "d", a.dialect,
|
||||
UStrLen(a.dialect, sizeof(a.dialect))) &&
|
||||
bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) &&
|
||||
bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) &&
|
||||
bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) &&
|
||||
bencodeEnd(buff);
|
||||
}
|
||||
*/
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
|
||||
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
|
||||
char ipbuff [128] = {0};
|
||||
const char * ipstr;
|
||||
if(!bencode_start_dict(buff)) return false;
|
||||
/* rank */
|
||||
if(!bencode_write_bytestring(buff, "c", 1)) return false;
|
||||
|
@ -32,9 +18,8 @@ bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
|
|||
if(!bencode_write_bytestring(buff, "e", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t))) return false;
|
||||
/** ip */
|
||||
char ipbuff [128] = {0};
|
||||
const char * ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
|
||||
if(ipstr == nullptr) return false;
|
||||
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
|
||||
if(!ipstr) return false;
|
||||
if(!bencode_write_bytestring(buff, "i", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff)))) return false;
|
||||
/** port */
|
||||
|
@ -47,13 +32,35 @@ bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
|
|||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
|
||||
struct llarp_ai_list
|
||||
{
|
||||
struct llarp_ai_list * next;
|
||||
struct llarp_ai data;
|
||||
};
|
||||
|
||||
struct llarp_ai_list * llarp_ai_list_new()
|
||||
{
|
||||
return llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8);
|
||||
}
|
||||
|
||||
void llarp_ai_list_free(struct llarp_ai_list * l)
|
||||
{
|
||||
struct llarp_ai_list * cur = l;
|
||||
struct llarp_ai_list * tmp;
|
||||
do {
|
||||
tmp = cur->next;
|
||||
llarp_g_mem.free(cur);
|
||||
cur = tmp;
|
||||
}while(cur->next);
|
||||
}
|
||||
|
||||
void llarp_ai_list_iterate(struct llarp_ai_list *l,
|
||||
struct llarp_ai_list_iter *itr) {
|
||||
itr->list = l;
|
||||
struct llarp_ai_list *cur = l;
|
||||
itr->list = l;
|
||||
do {
|
||||
if (!itr->visit(itr, cur->data)) return;
|
||||
if (!itr->visit(itr, &cur->data)) return;
|
||||
cur = cur->next;
|
||||
} while (cur->next);
|
||||
}
|
||||
}
|
|
@ -36,16 +36,18 @@ static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func,
|
|||
llarp_dh_complete_hook result, void *user) {
|
||||
struct llarp_dh_internal *impl =
|
||||
llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32);
|
||||
struct llarp_thread_job job = {
|
||||
.caller = dh->caller,
|
||||
.data = impl,
|
||||
.user = impl,
|
||||
.work = &llarp_crypto_dh_work
|
||||
};
|
||||
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
|
||||
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
|
||||
impl->ourkey = dh->ourkey;
|
||||
impl->result.user = user;
|
||||
impl->result.hook = result;
|
||||
impl->func = func;
|
||||
struct llarp_thread_job job = {.caller = dh->caller,
|
||||
.data = impl,
|
||||
.user = impl,
|
||||
.work = &llarp_crypto_dh_work};
|
||||
llarp_threadpool_queue_job(dh->tp, job);
|
||||
}
|
||||
|
||||
|
|
33
llarp/exit_info.c
Normal file
33
llarp/exit_info.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <llarp/exit_info.h>
|
||||
#include <llarp/bencode.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
|
||||
char addr_buff[128] = {0};
|
||||
const char * addr;
|
||||
if(!bencode_start_dict(buff)) return false;
|
||||
|
||||
/** address */
|
||||
addr = inet_ntop(AF_INET6, &xi->address, addr_buff, sizeof(addr_buff));
|
||||
if(!addr) return false;
|
||||
if(!bencode_write_bytestring(buff, "a", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff)))) return false;
|
||||
|
||||
/** netmask */
|
||||
addr = inet_ntop(AF_INET6, &xi->netmask, addr_buff, sizeof(addr_buff));
|
||||
if(!addr) return false;
|
||||
if(!bencode_write_bytestring(buff, "b", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff)))) return false;
|
||||
|
||||
/** public key */
|
||||
if(!bencode_write_bytestring(buff, "k", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, xi->pubkey, sizeof(llarp_pubkey_t))) return false;
|
||||
|
||||
/** version */
|
||||
if(!bencode_write_version_entry(buff)) return false;
|
||||
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
#include "exit_info.hpp"
|
||||
#include "bencode.hpp"
|
||||
|
||||
namespace llarp {
|
||||
template <>
|
||||
bool BEncode(const struct llarp_xi &xi, llarp_buffer_t *buff) {
|
||||
return bencodeDict(buff) &&
|
||||
bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) &&
|
||||
bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) &&
|
||||
bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
|
||||
return llarp::BEncode(*xi, buff);
|
||||
}
|
||||
}
|
39
llarp/exit_route.c
Normal file
39
llarp/exit_route.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <llarp/exit_route.h>
|
||||
#include <llarp/bencode.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
bool llarp_xr_bencode(struct llarp_xr * xr, llarp_buffer_t * buff)
|
||||
{
|
||||
char addr_buff [128] = {0};
|
||||
const char * addr;
|
||||
|
||||
if(!bencode_start_dict(buff)) return false;
|
||||
|
||||
/** gateway */
|
||||
addr = inet_ntop(AF_INET6, &xr->gateway, addr_buff, sizeof(addr_buff));
|
||||
if(!addr) return false;
|
||||
if(!bencode_write_bytestring(buff, "a", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff)))) return false;
|
||||
|
||||
/** netmask */
|
||||
addr = inet_ntop(AF_INET6, &xr->netmask, addr_buff, sizeof(addr_buff));
|
||||
if(!addr) return false;
|
||||
if(!bencode_write_bytestring(buff, "b", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff)))) return false;
|
||||
|
||||
/** source */
|
||||
addr = inet_ntop(AF_INET6, &xr->source, addr_buff, sizeof(addr_buff));
|
||||
if(!addr) return false;
|
||||
if(!bencode_write_bytestring(buff, "c", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff)))) return false;
|
||||
|
||||
/** lifetime */
|
||||
if(!bencode_write_bytestring(buff, "l", 1)) return false;
|
||||
if(!bencode_write_uint64(buff, xr->lifetime)) return false;
|
||||
|
||||
/** version */
|
||||
if(!bencode_write_version_entry(buff)) return false;
|
||||
|
||||
/* end */
|
||||
return bencode_end(buff);
|
||||
}
|
|
@ -1,10 +1,15 @@
|
|||
#include <llarp/mem.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace llarp {
|
||||
void *std_malloc(size_t sz, size_t align) {
|
||||
(void)align;
|
||||
void *ptr = malloc(sz);
|
||||
if (ptr) return ptr;
|
||||
if (ptr)
|
||||
{
|
||||
std::memset(ptr, 0, sz);
|
||||
return ptr;
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ int main(int argc, char *argv[]) {
|
|||
struct bench_main b_main;
|
||||
struct llarp_threadpool *tp;
|
||||
|
||||
llarp_mem_std();
|
||||
llarp_mem_stdlib();
|
||||
llarp_crypto_libsodium_init(&b_main.crypto);
|
||||
llarp_ev_loop_alloc(&b_main.ev);
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ int main(int argc, char *argv[]) {
|
|||
struct llarp_crypto crypto;
|
||||
struct llarp_threadpool *tp;
|
||||
|
||||
llarp_mem_jemalloc();
|
||||
llarp_mem_stdlib();
|
||||
llarp_crypto_libsodium_init(&crypto);
|
||||
llarp_ev_loop_alloc(&dh_main.ev);
|
||||
|
||||
|
|
Loading…
Reference in a new issue