mirror of https://github.com/oxen-io/lokinet
clang-format
parent
a704a28f34
commit
de3ddbb93e
@ -0,0 +1,108 @@
|
||||
---
|
||||
Language: Cpp
|
||||
# BasedOnStyle: LLVM
|
||||
AccessModifierOffset: -2
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: true
|
||||
AlignTrailingComments: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
AlwaysBreakTemplateDeclarations: false
|
||||
BinPackArguments: true
|
||||
BinPackParameters: true
|
||||
BraceWrapping:
|
||||
AfterClass: false
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeBraces: Attach
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IncludeCategories:
|
||||
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||
Priority: 2
|
||||
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||
Priority: 3
|
||||
- Regex: '.*'
|
||||
Priority: 1
|
||||
IncludeIsMainRegex: '(Test)?$'
|
||||
IndentCaseLabels: false
|
||||
IndentWidth: 2
|
||||
IndentWrappedFunctionNames: false
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 19
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 60
|
||||
PointerAlignment: Right
|
||||
ReflowComments: true
|
||||
SortIncludes: true
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInAngles: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Cpp11
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
||||
...
|
||||
|
@ -1,22 +1,22 @@
|
||||
#ifndef LLARP_BUFFER_H_
|
||||
#define LLARP_BUFFER_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef struct llarp_buffer_t {
|
||||
char * base;
|
||||
size_t sz;
|
||||
char * cur;
|
||||
} llarp_buffer_t;
|
||||
typedef struct llarp_buffer_t {
|
||||
char *base;
|
||||
size_t sz;
|
||||
char *cur;
|
||||
} llarp_buffer_t;
|
||||
|
||||
size_t llarp_buffer_size_left(llarp_buffer_t *buff);
|
||||
bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz);
|
||||
|
||||
size_t llarp_buffer_size_left(llarp_buffer_t * buff);
|
||||
bool llarp_buffer_write(llarp_buffer_t * buff, const void * data, size_t sz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef SARP_NET_H
|
||||
#define SARP_NET_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#endif
|
||||
|
@ -1,70 +1,61 @@
|
||||
#ifndef LLARP_BENCODE_HPP
|
||||
#define LLARP_BENCODE_HPP
|
||||
#include <llarp/buffer.h>
|
||||
#include <list>
|
||||
#include <llarp/buffer.h>
|
||||
#include <string>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
template<typename ValType>
|
||||
bool BEncode(const ValType & t, llarp_buffer_t * buff);
|
||||
namespace llarp {
|
||||
template <typename ValType>
|
||||
bool BEncode(const ValType &t, llarp_buffer_t *buff);
|
||||
|
||||
template<typename ValType>
|
||||
bool BDecode(ValType & t, llarp_buffer_t * buff);
|
||||
template <typename ValType> bool BDecode(ValType &t, llarp_buffer_t *buff);
|
||||
|
||||
static bool bencodeDict(llarp_buffer_t * buff)
|
||||
{
|
||||
static uint8_t c = 'd';
|
||||
return llarp_buffer_write(buff, &c, 1);
|
||||
}
|
||||
|
||||
static bool bencodeList(llarp_buffer_t * buff)
|
||||
{
|
||||
static uint8_t c = 'l';
|
||||
return llarp_buffer_write(buff, &c, 1);
|
||||
}
|
||||
|
||||
static bool bencodeEnd(llarp_buffer_t * buff)
|
||||
{
|
||||
static char c = 'e';
|
||||
return llarp_buffer_write(buff, &c, 1);
|
||||
}
|
||||
static bool bencodeDict(llarp_buffer_t *buff) {
|
||||
static uint8_t c = 'd';
|
||||
return llarp_buffer_write(buff, &c, 1);
|
||||
}
|
||||
|
||||
static bool bencodeDictKey(llarp_buffer_t * buff, const std::string & key)
|
||||
{
|
||||
std::string kstr = std::to_string(key.size()) + ":" + key;
|
||||
return llarp_buffer_write(buff, kstr.c_str(), kstr.size());
|
||||
}
|
||||
|
||||
template<typename IntType>
|
||||
static bool bencodeDict_Int(llarp_buffer_t * buff, const std::string & key, IntType i)
|
||||
{
|
||||
std::string istr = "i" + std::to_string(i) + "e";
|
||||
return bencodeDictKey(buff, key) &&
|
||||
llarp_buffer_write(buff, istr.c_str(), istr.size());
|
||||
}
|
||||
static bool bencodeList(llarp_buffer_t *buff) {
|
||||
static uint8_t c = 'l';
|
||||
return llarp_buffer_write(buff, &c, 1);
|
||||
}
|
||||
|
||||
static bool bencodeDict_Bytes(llarp_buffer_t * buff, const std::string & key, const void * data, size_t sz)
|
||||
{
|
||||
std::string sz_str = std::to_string(sz) + ":";
|
||||
return bencodeDictKey(buff, key) &&
|
||||
llarp_buffer_write(buff, sz_str.c_str(), sz_str.size()) &&
|
||||
llarp_buffer_write(buff, data, sz);
|
||||
}
|
||||
static bool bencodeEnd(llarp_buffer_t *buff) {
|
||||
static char c = 'e';
|
||||
return llarp_buffer_write(buff, &c, 1);
|
||||
}
|
||||
|
||||
static bool bencodeDictKey(llarp_buffer_t *buff, const std::string &key) {
|
||||
std::string kstr = std::to_string(key.size()) + ":" + key;
|
||||
return llarp_buffer_write(buff, kstr.c_str(), kstr.size());
|
||||
}
|
||||
|
||||
template <typename IntType>
|
||||
static bool bencodeDict_Int(llarp_buffer_t *buff, const std::string &key,
|
||||
IntType i) {
|
||||
std::string istr = "i" + std::to_string(i) + "e";
|
||||
return bencodeDictKey(buff, key) &&
|
||||
llarp_buffer_write(buff, istr.c_str(), istr.size());
|
||||
}
|
||||
|
||||
static bool bencodeDict_Bytes(llarp_buffer_t *buff, const std::string &key,
|
||||
const void *data, size_t sz) {
|
||||
std::string sz_str = std::to_string(sz) + ":";
|
||||
return bencodeDictKey(buff, key) &&
|
||||
llarp_buffer_write(buff, sz_str.c_str(), sz_str.size()) &&
|
||||
llarp_buffer_write(buff, data, sz);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool BEncode(const std::list<T> & l, llarp_buffer_t * buff)
|
||||
{
|
||||
if(bencodeList(buff))
|
||||
{
|
||||
for(const auto & itr : l)
|
||||
if(!BEncode(itr, buff))
|
||||
return false;
|
||||
return bencodeEnd(buff);
|
||||
}
|
||||
return false;
|
||||
template <typename T>
|
||||
bool BEncode(const std::list<T> &l, llarp_buffer_t *buff) {
|
||||
if (bencodeList(buff)) {
|
||||
for (const auto &itr : l)
|
||||
if (!BEncode(itr, buff))
|
||||
return false;
|
||||
return bencodeEnd(buff);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
||||
|
@ -1,75 +1,65 @@
|
||||
#include <llarp/config.h>
|
||||
#include "config.hpp"
|
||||
#include "ini.hpp"
|
||||
#include "mem.hpp"
|
||||
#include <llarp/config.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
|
||||
template<typename Config, typename Section>
|
||||
static Section find_section(Config & c, const std::string & name, const Section & fallback)
|
||||
{
|
||||
if(c.sections.find(name) == c.sections.end())
|
||||
return fallback;
|
||||
return c.sections[name].values;
|
||||
}
|
||||
|
||||
|
||||
bool Config::Load(const char * fname)
|
||||
{
|
||||
std::ifstream f;
|
||||
f.open(fname);
|
||||
if(f.is_open())
|
||||
{
|
||||
ini::Parser parser(f);
|
||||
auto & top = parser.top();
|
||||
router = find_section(top, "router", section_t{});
|
||||
network = find_section(top, "network", section_t{});
|
||||
netdb = find_section(top, "netdb", section_t{});
|
||||
links = find_section(top, "links", section_t{});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
namespace llarp {
|
||||
|
||||
|
||||
template <typename Config, typename Section>
|
||||
static Section find_section(Config &c, const std::string &name,
|
||||
const Section &fallback) {
|
||||
if (c.sections.find(name) == c.sections.end())
|
||||
return fallback;
|
||||
return c.sections[name].values;
|
||||
}
|
||||
|
||||
bool Config::Load(const char *fname) {
|
||||
std::ifstream f;
|
||||
f.open(fname);
|
||||
if (f.is_open()) {
|
||||
ini::Parser parser(f);
|
||||
auto &top = parser.top();
|
||||
router = find_section(top, "router", section_t{});
|
||||
network = find_section(top, "network", section_t{});
|
||||
netdb = find_section(top, "netdb", section_t{});
|
||||
links = find_section(top, "links", section_t{});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
||||
void llarp_new_config(struct llarp_config ** conf)
|
||||
{
|
||||
llarp_config * c = new llarp_config;
|
||||
*conf = c;
|
||||
}
|
||||
void llarp_new_config(struct llarp_config **conf) {
|
||||
llarp_config *c = new llarp_config;
|
||||
*conf = c;
|
||||
}
|
||||
|
||||
void llarp_free_config(struct llarp_config ** conf)
|
||||
{
|
||||
if(*conf)
|
||||
delete *conf;
|
||||
*conf = nullptr;
|
||||
}
|
||||
void llarp_free_config(struct llarp_config **conf) {
|
||||
if (*conf)
|
||||
delete *conf;
|
||||
*conf = nullptr;
|
||||
}
|
||||
|
||||
int llarp_load_config(struct llarp_config * conf, const char * fname)
|
||||
{
|
||||
if(!conf->impl.Load(fname)) return -1;
|
||||
return 0;
|
||||
}
|
||||
int llarp_load_config(struct llarp_config *conf, const char *fname) {
|
||||
if (!conf->impl.Load(fname))
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llarp_config_iter(struct llarp_config * conf, struct llarp_config_iterator * iter)
|
||||
{
|
||||
iter->conf = conf;
|
||||
std::map<std::string, llarp::Config::section_t&> sections = {
|
||||
void llarp_config_iter(struct llarp_config *conf,
|
||||
struct llarp_config_iterator *iter) {
|
||||
iter->conf = conf;
|
||||
std::map<std::string, llarp::Config::section_t &> sections = {
|
||||
{"router", conf->impl.router},
|
||||
{"network", conf->impl.network},
|
||||
{"links", conf->impl.links},
|
||||
{"netdb", conf->impl.netdb}
|
||||
};
|
||||
for(const auto & section : sections)
|
||||
for(const auto & item : section.second)
|
||||
iter->visit(iter, section.first.c_str(), item.first.c_str(), item.second.c_str());
|
||||
}
|
||||
{"netdb", conf->impl.netdb}};
|
||||
for (const auto §ion : sections)
|
||||
for (const auto &item : section.second)
|
||||
iter->visit(iter, section.first.c_str(), item.first.c_str(),
|
||||
item.second.c_str());
|
||||
}
|
||||
}
|
||||
|
@ -1,90 +1,84 @@
|
||||
#include <llarp/crypto.h>
|
||||
#include <sodium/crypto_stream_xchacha20.h>
|
||||
#include <sodium/crypto_generichash.h>
|
||||
#include <sodium/crypto_scalarmult.h>
|
||||
#include <sodium/crypto_sign.h>
|
||||
#include <sodium/crypto_stream_xchacha20.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace sodium
|
||||
{
|
||||
bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n)
|
||||
{
|
||||
uint8_t * base = (uint8_t*)buff.base;
|
||||
return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
|
||||
}
|
||||
namespace llarp {
|
||||
namespace sodium {
|
||||
bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n) {
|
||||
uint8_t *base = (uint8_t *)buff.base;
|
||||
return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
|
||||
}
|
||||
|
||||
bool dh(llarp_sharedkey_t * shared, uint8_t * client_pk, uint8_t * server_pk, uint8_t * remote_key, uint8_t * local_key)
|
||||
{
|
||||
uint8_t * out = *shared;
|
||||
const size_t outsz = SHAREDKEYSIZE;
|
||||
crypto_generichash_state h;
|
||||
if(crypto_scalarmult(out, local_key, remote_key) == -1) return false;
|
||||
crypto_generichash_init(&h, NULL, 0U, outsz);
|
||||
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
|
||||
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
|
||||
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
|
||||
crypto_generichash_final(&h, out, outsz);
|
||||
return true;
|
||||
}
|
||||
bool dh(llarp_sharedkey_t *shared, uint8_t *client_pk, uint8_t *server_pk,
|
||||
uint8_t *remote_key, uint8_t *local_key) {
|
||||
uint8_t *out = *shared;
|
||||
const size_t outsz = SHAREDKEYSIZE;
|
||||
crypto_generichash_state h;
|
||||
if (crypto_scalarmult(out, local_key, remote_key) == -1)
|
||||
return false;
|
||||
crypto_generichash_init(&h, NULL, 0U, outsz);
|
||||
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
|
||||
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
|
||||
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
|
||||
crypto_generichash_final(&h, out, outsz);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dh_client(llarp_sharedkey_t * shared, llarp_pubkey_t pk, llarp_tunnel_nounce_t n, llarp_seckey_t sk)
|
||||
{
|
||||
llarp_pubkey_t local_pk;
|
||||
crypto_scalarmult_base(local_pk, sk);
|
||||
if(dh(shared, local_pk, pk, pk, sk))
|
||||
{
|
||||
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool dh_client(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
|
||||
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
|
||||
llarp_pubkey_t local_pk;
|
||||
crypto_scalarmult_base(local_pk, sk);
|
||||
if (dh(shared, local_pk, pk, pk, sk)) {
|
||||
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
|
||||
TUNNOUNCESIZE) != -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool dh_server(llarp_sharedkey_t * shared, llarp_pubkey_t pk, llarp_tunnel_nounce_t n, llarp_seckey_t sk)
|
||||
{
|
||||
llarp_pubkey_t local_pk;
|
||||
crypto_scalarmult_base(local_pk, sk);
|
||||
if(dh(shared, pk, local_pk, pk, sk))
|
||||
{
|
||||
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool dh_server(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
|
||||
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
|
||||
llarp_pubkey_t local_pk;
|
||||
crypto_scalarmult_base(local_pk, sk);
|
||||
if (dh(shared, pk, local_pk, pk, sk)) {
|
||||
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
|
||||
TUNNOUNCESIZE) != -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hash(llarp_hash_t * result, llarp_buffer_t buff)
|
||||
{
|
||||
const uint8_t * base = (const uint8_t *) buff.base;
|
||||
return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1;
|
||||
}
|
||||
bool hash(llarp_hash_t *result, llarp_buffer_t buff) {
|
||||
const uint8_t *base = (const uint8_t *)buff.base;
|
||||
return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1;
|
||||
}
|
||||
|
||||
bool hmac(llarp_hash_t * result, llarp_buffer_t buff, llarp_seckey_t secret)
|
||||
{
|
||||
const uint8_t * base = (const uint8_t *) buff.base;
|
||||
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz, secret, HMACSECSIZE) != -1;
|
||||
}
|
||||
bool hmac(llarp_hash_t *result, llarp_buffer_t buff, llarp_seckey_t secret) {
|
||||
const uint8_t *base = (const uint8_t *)buff.base;
|
||||
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz,
|
||||
secret, HMACSECSIZE) != -1;
|
||||
}
|
||||
|
||||
bool sign(llarp_sig_t * result, llarp_seckey_t secret, llarp_buffer_t buff)
|
||||
{
|
||||
const uint8_t * base = (const uint8_t *) buff.base;
|
||||
return crypto_sign_detached(*result, nullptr, base, buff.sz, secret) != -1;
|
||||
}
|
||||
bool sign(llarp_sig_t *result, llarp_seckey_t secret, llarp_buffer_t buff) {
|
||||
const uint8_t *base = (const uint8_t *)buff.base;
|
||||
return crypto_sign_detached(*result, nullptr, base, buff.sz, secret) != -1;
|
||||
}
|
||||
|
||||
bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig)
|
||||
{
|
||||
const uint8_t * base = (const uint8_t *) buff.base;
|
||||
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
|
||||
}
|
||||
}
|
||||
bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) {
|
||||
const uint8_t *base = (const uint8_t *)buff.base;
|
||||
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
|
||||
}
|
||||
} // namespace sodium
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
void llarp_crypto_libsodium_init(struct llarp_crypto * c)
|
||||
{
|
||||
c->xchacha20 = llarp::sodium::xchacha20;
|
||||
c->dh_client = llarp::sodium::dh_client;
|
||||
c->dh_server = llarp::sodium::dh_server;
|
||||
c->hash = llarp::sodium::hash;
|
||||
c->hmac = llarp::sodium::hmac;
|
||||
c->sign = llarp::sodium::sign;
|
||||
c->verify = llarp::sodium::verify;
|
||||
}
|
||||
void llarp_crypto_libsodium_init(struct llarp_crypto *c) {
|
||||
c->xchacha20 = llarp::sodium::xchacha20;
|
||||
c->dh_client = llarp::sodium::dh_client;
|
||||
c->dh_server = llarp::sodium::dh_server;
|
||||
c->hash = llarp::sodium::hash;
|
||||
c->hmac = llarp::sodium::hmac;
|
||||
c->sign = llarp::sodium::sign;
|
||||
c->verify = llarp::sodium::verify;
|
||||
}
|
||||
}
|
||||
|
@ -1,184 +1,154 @@
|
||||
#include "mem.hpp"
|
||||
#include <llarp/ev.h>
|
||||
#include <uv.h>
|
||||
#include "mem.hpp"
|
||||
|
||||
struct llarp_ev_loop
|
||||
{
|
||||
struct llarp_ev_loop {
|
||||
uv_loop_t _loop;
|
||||
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
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; }
|
||||
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
|
||||
|
||||
uv_loop_t *loop() { return &_loop; }
|
||||
};
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct udp_listener
|
||||
{
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
return llarp_g_mem.alloc(sz, alignment<udp_listener>());
|
||||
}
|
||||
namespace llarp {
|
||||
struct udp_listener {
|
||||
static void *operator new(size_t sz) {
|
||||
return llarp_g_mem.alloc(sz, alignment<udp_listener>());
|
||||
}
|
||||
|
||||
static void operator delete(void * ptr)
|
||||
{
|
||||
llarp_g_mem.free(ptr);
|
||||
}
|
||||
|
||||
uv_udp_t _handle;
|
||||
struct llarp_udp_listener * listener;
|
||||
|
||||
void recvfrom(const struct sockaddr * addr, char * buff, ssize_t sz)
|
||||
{
|
||||
if(listener->recvfrom)
|
||||
listener->recvfrom(listener, addr, buff, sz);
|
||||
}
|
||||
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
|
||||
|
||||
/** called after closed */
|
||||
void closed()
|
||||
{
|
||||
if(listener->closed)
|
||||
listener->closed(listener);
|
||||
listener->impl = nullptr;
|
||||
}
|
||||
|
||||
uv_udp_t * udp() { return &_handle; }
|
||||
};
|
||||
|
||||
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, 1024));
|
||||
buf->len = sz;
|
||||
}
|
||||
uv_udp_t _handle;
|
||||
struct llarp_udp_listener *listener;
|
||||
|
||||
static void udp_recv_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags)
|
||||
{
|
||||
udp_listener * l = static_cast<udp_listener *>(handle->data);
|
||||
l->recvfrom(addr, buf->base, nread);
|
||||
llarp_g_mem.free(buf->base);
|
||||
void recvfrom(const struct sockaddr *addr, char *buff, ssize_t sz) {
|
||||
if (listener->recvfrom)
|
||||
listener->recvfrom(listener, addr, buff, sz);
|
||||
}
|
||||
|
||||
static void udp_close_cb(uv_handle_t * handle)
|
||||
{
|
||||
udp_listener * l = static_cast<udp_listener *>(handle->data);
|
||||
l->closed();
|
||||
delete l;
|
||||
/** called after closed */
|
||||
void closed() {
|
||||
if (listener->closed)
|
||||
listener->closed(listener);
|
||||
listener->impl = nullptr;
|
||||
}
|
||||
|
||||
uv_udp_t *udp() { return &_handle; }
|
||||
};
|
||||
|
||||
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, 1024));
|
||||
buf->len = sz;
|
||||
}
|
||||
|
||||
static void udp_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
|
||||
const struct sockaddr *addr, unsigned flags) {
|
||||
udp_listener *l = static_cast<udp_listener *>(handle->data);
|
||||
l->recvfrom(addr, buf->base, nread);
|
||||
llarp_g_mem.free(buf->base);
|
||||
}
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
static void udp_close_cb(uv_handle_t *handle) {
|
||||
udp_listener *l = static_cast<udp_listener *>(handle->data);
|
||||
l->closed();
|
||||
delete l;
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
static void ev_handle_async_closed(uv_handle_t * handle)
|
||||
{
|
||||
struct llarp_ev_job * ev = static_cast<llarp_ev_job *>(handle->data);
|
||||
llarp_g_mem.free(ev);
|
||||
llarp_g_mem.free(handle);
|
||||
}
|
||||
|
||||
static void ev_handle_async(uv_async_t * handle)
|
||||
{
|
||||
struct llarp_ev_job * ev = static_cast<llarp_ev_job *>(handle->data);
|
||||
ev->work(ev);
|
||||
uv_close((uv_handle_t *)handle, ev_handle_async_closed);
|
||||
}
|
||||
namespace llarp {
|
||||
|
||||
static void ev_handle_async_closed(uv_handle_t *handle) {
|
||||
struct llarp_ev_job *ev = static_cast<llarp_ev_job *>(handle->data);
|
||||
llarp_g_mem.free(ev);
|
||||
llarp_g_mem.free(handle);
|
||||
}
|
||||
|
||||
static void ev_handle_async(uv_async_t *handle) {
|
||||
struct llarp_ev_job *ev = static_cast<llarp_ev_job *>(handle->data);
|
||||
ev->work(ev);
|
||||
uv_close((uv_handle_t *)handle, ev_handle_async_closed);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
void llarp_ev_loop_alloc(struct llarp_ev_loop ** ev)
|
||||
{
|
||||
*ev = new llarp_ev_loop;
|
||||
if (*ev)
|
||||
{
|
||||
uv_loop_init((*ev)->loop());
|
||||
}
|
||||
}
|
||||
|
||||
void llarp_ev_loop_free(struct llarp_ev_loop ** ev)
|
||||
{
|
||||
if(*ev)
|
||||
{
|
||||
uv_loop_close((*ev)->loop());
|
||||
llarp_g_mem.free(*ev);
|
||||
}
|
||||
*ev = nullptr;
|
||||
void llarp_ev_loop_alloc(struct llarp_ev_loop **ev) {
|
||||
*ev = new llarp_ev_loop;
|
||||
if (*ev) {
|
||||
uv_loop_init((*ev)->loop());
|
||||
}
|
||||
}
|
||||
|
||||
int llarp_ev_loop_run(struct llarp_ev_loop * ev)
|
||||
{
|
||||
return uv_run(ev->loop(), UV_RUN_DEFAULT);
|
||||
void llarp_ev_loop_free(struct llarp_ev_loop **ev) {
|
||||
if (*ev) {
|
||||
uv_loop_close((*ev)->loop());
|
||||
llarp_g_mem.free(*ev);
|
||||
}
|
||||
|
||||
int llarp_ev_add_udp_listener(struct llarp_ev_loop * ev, struct llarp_udp_listener * listener)
|
||||
{
|
||||
sockaddr_in6 addr;
|
||||
uv_ip6_addr(listener->host, listener->port, &addr);
|
||||
int ret = 0;
|
||||
llarp::udp_listener * l = new llarp::udp_listener;
|
||||
listener->impl = l;
|
||||
l->udp()->data = l;
|
||||
l->listener = listener;
|
||||
|
||||
ret = uv_udp_init(ev->loop(), l->udp());
|
||||
if (ret == 0)
|
||||
{
|
||||
ret = uv_udp_bind(l->udp(), (const sockaddr *)&addr, 0);
|
||||
if (ret == 0)
|
||||
{
|
||||
ret = uv_udp_recv_start(l->udp(), llarp::udp_alloc_cb, llarp::udp_recv_cb);
|
||||
}
|
||||
*ev = nullptr;
|
||||
}
|
||||
|
||||
int llarp_ev_loop_run(struct llarp_ev_loop *ev) {
|
||||
return uv_run(ev->loop(), UV_RUN_DEFAULT);
|
||||
}
|
||||
|
||||
int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
|
||||
struct llarp_udp_listener *listener) {
|
||||
sockaddr_in6 addr;
|
||||
uv_ip6_addr(listener->host, listener->port, &addr);
|
||||
int ret = 0;
|
||||
llarp::udp_listener *l = new llarp::udp_listener;
|
||||
listener->impl = l;
|
||||
l->udp()->data = l;
|
||||
l->listener = listener;
|
||||
|
||||
ret = uv_udp_init(ev->loop(), l->udp());
|
||||
if (ret == 0) {
|
||||
ret = uv_udp_bind(l->udp(), (const sockaddr *)&addr, 0);
|
||||
if (ret == 0) {
|
||||
ret =
|
||||
uv_udp_recv_start(l->udp(), llarp::udp_alloc_cb, llarp::udp_recv_cb);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int llarp_ev_close_udp_listener(struct llarp_udp_listener * listener)
|
||||
{
|
||||
int ret = -1;
|
||||
if(listener)
|
||||
{
|
||||
llarp::udp_listener * l = static_cast<llarp::udp_listener*>(listener->impl);
|
||||
if(l)
|
||||
{
|
||||
if(!uv_udp_recv_stop(l->udp()))
|
||||
{
|
||||
l->closed();
|
||||
delete l;
|
||||
ret = 0;
|
||||
}
|
||||
int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) {
|
||||
int ret = -1;
|
||||
if (listener) {
|
||||
llarp::udp_listener *l = static_cast<llarp::udp_listener *>(listener->impl);
|
||||
if (l) {
|
||||
if (!uv_udp_recv_stop(l->udp())) {
|
||||
l->closed();
|
||||
delete l;
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void llarp_ev_loop_stop(struct llarp_ev_loop * loop)
|
||||
{
|
||||
uv_stop(loop->loop());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool llarp_ev_async(struct llarp_ev_loop * loop, struct llarp_ev_job job)
|
||||
{
|
||||
struct llarp_ev_job * job_copy = static_cast<struct llarp_ev_job *>(llarp_g_mem.alloc(sizeof(struct llarp_ev_job), llarp::alignment<llarp_ev_job>()));
|
||||
job_copy->work = job.work;
|
||||
job_copy->loop = loop;
|
||||
job_copy->user = job.user;
|
||||
uv_async_t * async = static_cast<uv_async_t *>(llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment<uv_async_t>()));
|
||||
async->data = job_copy;
|
||||
if(uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 && uv_async_send(async))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
llarp_g_mem.free(job_copy);
|
||||
llarp_g_mem.free(async);
|
||||
return false;
|
||||
}
|
||||
void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { uv_stop(loop->loop()); }
|
||||
|
||||
bool llarp_ev_async(struct llarp_ev_loop *loop, struct llarp_ev_job job) {
|
||||
struct llarp_ev_job *job_copy =
|
||||
static_cast<struct llarp_ev_job *>(llarp_g_mem.alloc(
|
||||
sizeof(struct llarp_ev_job), llarp::alignment<llarp_ev_job>()));
|
||||
job_copy->work = job.work;
|
||||
job_copy->loop = loop;
|
||||
job_copy->user = job.user;
|
||||
uv_async_t *async = static_cast<uv_async_t *>(
|
||||
llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment<uv_async_t>()));
|
||||
async->data = job_copy;
|
||||
if (uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 &&
|
||||
uv_async_send(async))
|
||||
return true;
|
||||
else {
|
||||
llarp_g_mem.free(job_copy);
|
||||
llarp_g_mem.free(async);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,26 @@
|
||||
#ifndef LLARP_LINK_HPP
|
||||
#define LLARP_LINK_HPP
|
||||
#include <netinet/in.h>
|
||||
#include <llarp/crypto.h>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <llarp/crypto.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "mem.hpp"
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/router_contact.h>
|
||||
#include "mem.hpp"
|
||||
|
||||
|
||||
struct llarp_link
|
||||
{
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
struct llarp_link {
|
||||
static void *operator new(size_t sz) {
|
||||
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_link>());
|
||||
}
|
||||
|
||||
static void operator delete(void * ptr)
|
||||
{
|
||||
llarp_g_mem.free(ptr);
|
||||
}
|
||||
|
||||
|
||||
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
|
||||
|
||||
struct sockaddr_in6 localaddr;
|
||||
int af;
|
||||
llarp_udp_listener listener;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -1,8 +1,5 @@
|
||||
#include <llarp/mem.h>
|
||||
|
||||
extern "C" {
|
||||
struct llarp_alloc llarp_g_mem = {
|
||||
.alloc = nullptr,
|
||||
.free = nullptr
|
||||
};
|
||||
struct llarp_alloc llarp_g_mem = {.alloc = nullptr, .free = nullptr};
|
||||
}
|
||||
|
@ -1,14 +1,11 @@
|
||||
#ifndef LLARP_MEM_HPP
|
||||
#define LLARP_MEM_HPP
|
||||
#include <llarp/mem.h>
|
||||
#include <cmath>
|
||||
namespace llarp
|
||||
{
|
||||
template<typename T>
|
||||
static constexpr size_t alignment()
|
||||
{
|
||||
return std::exp2(1+std::floor(std::log2(sizeof(T))));
|
||||
}
|
||||
#include <llarp/mem.h>
|
||||
namespace llarp {
|
||||
template <typename T> static constexpr size_t alignment() {
|
||||
return std::exp2(1 + std::floor(std::log2(sizeof(T))));
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
||||
|
@ -1,23 +1,20 @@
|
||||
#include <llarp/mem.h>
|
||||
#include <jemalloc/jemalloc.h>
|
||||
#include <llarp/mem.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
static void * jem_malloc(size_t sz, size_t align)
|
||||
{
|
||||
return mallocx(sz, MALLOCX_ALIGN(align));
|
||||
}
|
||||
namespace llarp {
|
||||
static void *jem_malloc(size_t sz, size_t align) {
|
||||
return mallocx(sz, MALLOCX_ALIGN(align));
|
||||
}
|
||||
|
||||
static void jem_free(void * ptr)
|
||||
{
|
||||
if(ptr) free(ptr);
|
||||
}
|
||||
static void jem_free(void *ptr) {
|
||||
if (ptr)
|
||||
free(ptr);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
void llarp_mem_jemalloc()
|
||||
{
|
||||
llarp_g_mem.alloc = llarp::jem_malloc;
|
||||
llarp_g_mem.free = llarp::jem_free;
|
||||
}
|
||||
void llarp_mem_jemalloc() {
|
||||
llarp_g_mem.alloc = llarp::jem_malloc;
|
||||
llarp_g_mem.free = llarp::jem_free;
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,24 @@
|
||||
#include <llarp/mem.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
void * std_malloc(size_t sz, size_t align)
|
||||
{
|
||||
(void) align;
|
||||
void * ptr = malloc(sz);
|
||||
if(ptr) return ptr;
|
||||
abort();
|
||||
}
|
||||
namespace llarp {
|
||||
void *std_malloc(size_t sz, size_t align) {
|
||||
(void)align;
|
||||
void *ptr = malloc(sz);
|
||||
if (ptr)
|
||||
return ptr;
|
||||
abort();
|
||||
}
|
||||
|
||||
void std_free(void * ptr)
|
||||
{
|
||||
if(ptr) free(ptr);
|
||||
}
|
||||
|
||||
void std_free(void *ptr) {
|
||||
if (ptr)
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
void llarp_mem_std()
|
||||
{
|
||||
llarp_g_mem.alloc = llarp::std_malloc;
|
||||
llarp_g_mem.free = llarp::std_free;
|
||||
}
|
||||
void llarp_mem_std() {
|
||||
llarp_g_mem.alloc = llarp::std_malloc;
|
||||
llarp_g_mem.free = llarp::std_free;
|
||||
}
|
||||
}
|
||||
|
@ -1,133 +1,105 @@
|
||||
#include <llarp/router.h>
|
||||
#include <llarp/link.h>
|
||||
#include "link.hpp"
|
||||
#include "mem.hpp"
|
||||
#include "str.hpp"
|
||||
#include <llarp/link.h>
|
||||
#include <llarp/router.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val);
|
||||
struct router_links
|
||||
{
|
||||
struct llarp_link * link = nullptr;
|
||||
struct router_links * next = nullptr;
|
||||
};
|
||||
namespace llarp {
|
||||
void router_iter_config(llarp_config_iterator *iter, const char *section,
|
||||
const char *key, const char *val);
|
||||
struct router_links {
|
||||
struct llarp_link *link = nullptr;
|
||||
struct router_links *next = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
struct llarp_router
|
||||
{
|
||||
struct llarp_threadpool * tp;
|
||||
struct llarp_router {
|
||||
struct llarp_threadpool *tp;
|
||||
llarp::router_links links;
|
||||
llarp_crypto crypto;
|
||||
|
||||
|
||||
static void * operator new(size_t sz)
|
||||
{
|
||||
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);
|
||||
}
|
||||
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)
|
||||
void AddLink(struct llarp_link *link) {
|
||||
llarp::router_links *head = &links;
|
||||
while (head->next && head->link)
|
||||
head = head->next;
|
||||
|
||||
if(head->link)
|
||||
|
||||
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)
|
||||
|
||||
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);
|
||||
} while (cur);
|
||||
}
|
||||
};
|
||||
|
||||
void Close() { ForEachLink(llarp_link_stop); }
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct llarp_router * llarp_init_router(struct llarp_threadpool * tp)
|
||||
{
|
||||
llarp_router * router = new llarp_router;
|
||||
router->tp = tp;
|
||||
llarp_crypto_libsodium_init(&router->crypto);
|
||||
return router;
|
||||
}
|
||||
struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) {
|
||||
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)
|
||||
{
|
||||
llarp_config_iterator iter;
|
||||
iter.user = router;
|
||||
iter.visit = llarp::router_iter_config;
|
||||
llarp_config_iter(conf, &iter);
|
||||
return 0;
|
||||
}
|
||||
int llarp_configure_router(struct llarp_router *router,
|
||||
struct llarp_config *conf) {
|
||||
llarp_config_iterator iter;
|
||||
iter.user = router;
|
||||
iter.visit = llarp::router_iter_config;
|
||||
llarp_config_iter(conf, &iter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop)
|
||||
{
|
||||
router->ForEachLink([loop](llarp_link * link) {
|
||||
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
|
||||
});
|
||||
}
|
||||
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) {
|
||||
router->ForEachLink([loop](llarp_link *link) {
|
||||
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
|
||||
});
|
||||
}
|
||||
|
||||
void llarp_stop_router(struct llarp_router * router)
|
||||
{
|
||||
router->Close();
|
||||
}
|
||||
void llarp_stop_router(struct llarp_router *router) { router->Close(); }
|
||||
|
||||
void llarp_free_router(struct llarp_router ** router)
|
||||
{
|
||||
if(*router)
|
||||
{
|
||||
(*router)->ForEachLink([](llarp_link * link) { llarp_g_mem.free(link); });
|
||||
delete *router;
|
||||
}
|
||||
*router = nullptr;
|
||||
void llarp_free_router(struct llarp_router **router) {
|
||||
if (*router) {
|
||||
(*router)->ForEachLink([](llarp_link *link) { llarp_g_mem.free(link); });
|
||||
delete *router;
|
||||
}
|
||||
*router = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
|
||||
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val)
|
||||
{
|
||||
llarp_router * self = static_cast<llarp_router *>(iter->user);
|
||||
if (StrEq(section, "links"))
|
||||
{
|
||||
if(StrEq(val, "ip"))
|
||||
{
|
||||
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"))
|
||||
{
|
||||
/** todo: ethernet link */
|
||||
namespace llarp {
|
||||
|
||||
void router_iter_config(llarp_config_iterator *iter, const char *section,
|
||||
const char *key, const char *val) {
|
||||
llarp_router *self = static_cast<llarp_router *>(iter->user);
|
||||
if (StrEq(section, "links")) {
|
||||
if (StrEq(val, "ip")) {
|
||||
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")) {
|
||||
/** todo: ethernet link */
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace llarp
|
||||
|
@ -1,24 +1,20 @@
|
||||
#include "router_contact.hpp"
|
||||
#include "exit_info.hpp"
|
||||
#include "address_info.hpp"
|
||||
#include "bencode.hpp"
|
||||
#include "exit_info.hpp"
|
||||
|
||||
extern "C" {
|
||||
|
||||
}
|
||||
extern "C" {}
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
bool BEncode(const llarp_rc & a, llarp_buffer_t * buff)
|
||||
{
|
||||
std::list<llarp_ai> addresses = ai_list_to_std(a.addrs);
|
||||
std::list<llarp_xi> exits = xi_list_to_std(a.exits);
|
||||
return bencodeDict(buff) &&
|
||||
bencodeDictKey(buff, "a") && BEncode(addresses, buff) &&
|
||||
bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) &&
|
||||
bencodeDict_Int(buff, "v", 0) &&
|
||||
bencodeDictKey(buff, "x") && BEncode(exits, buff) &&
|
||||
bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
|
||||
bencodeEnd(buff);
|
||||
}
|
||||
namespace llarp {
|
||||
bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
|
||||
std::list<llarp_ai> addresses = ai_list_to_std(a.addrs);
|
||||
std::list<llarp_xi> exits = xi_list_to_std(a.exits);
|
||||
return bencodeDict(buff) && bencodeDictKey(buff, "a") &&
|
||||
BEncode(addresses, buff) &&
|
||||
bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) &&
|
||||
bencodeDict_Int(buff, "v", 0) && bencodeDictKey(buff, "x") &&
|
||||
BEncode(exits, buff) &&
|
||||
bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
|
||||
bencodeEnd(buff);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
@ -1,25 +1,22 @@
|
||||
#include <llarp/time.h>
|
||||
#include <chrono>
|
||||
#include <llarp/time.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
typedef std::chrono::steady_clock clock_t;
|
||||
namespace llarp {
|
||||
typedef std::chrono::steady_clock clock_t;
|
||||
|
||||
template<typename Res, typename IntType>
|
||||
static IntType time_since_epoch()
|
||||
{
|
||||
return std::chrono::duration_cast<Res>(llarp::clock_t::now().time_since_epoch()).count();
|
||||
}
|
||||
template <typename Res, typename IntType> static IntType time_since_epoch() {
|
||||
return std::chrono::duration_cast<Res>(
|
||||
llarp::clock_t::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
extern "C" {
|
||||
llarp_time_t llarp_time_now_ms()
|
||||
{
|
||||
return llarp::time_since_epoch<std::chrono::milliseconds, llarp_time_t>();
|
||||
}
|
||||
|
||||
llarp_seconds_t llarp_time_now_sec()
|
||||
{
|
||||
return llarp::time_since_epoch<std::chrono::seconds, llarp_seconds_t>();
|
||||
}
|
||||
llarp_time_t llarp_time_now_ms() {
|
||||
return llarp::time_since_epoch<std::chrono::milliseconds, llarp_time_t>();
|
||||
}
|
||||
|
||||
llarp_seconds_t llarp_time_now_sec() {
|
||||
return llarp::time_since_epoch<std::chrono::seconds, llarp_seconds_t>();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue