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_
|
#ifndef LLARP_BUFFER_H_
|
||||||
#define LLARP_BUFFER_H_
|
#define LLARP_BUFFER_H_
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
typedef struct llarp_buffer_t {
|
typedef struct llarp_buffer_t {
|
||||||
char * base;
|
char *base;
|
||||||
size_t sz;
|
size_t sz;
|
||||||
char * cur;
|
char *cur;
|
||||||
} llarp_buffer_t;
|
} 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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#ifndef SARP_NET_H
|
#ifndef SARP_NET_H
|
||||||
#define SARP_NET_H
|
#define SARP_NET_H
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,70 +1,61 @@
|
|||||||
#ifndef LLARP_BENCODE_HPP
|
#ifndef LLARP_BENCODE_HPP
|
||||||
#define LLARP_BENCODE_HPP
|
#define LLARP_BENCODE_HPP
|
||||||
#include <llarp/buffer.h>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <llarp/buffer.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
template <typename ValType>
|
||||||
template<typename ValType>
|
bool BEncode(const ValType &t, llarp_buffer_t *buff);
|
||||||
bool BEncode(const ValType & t, llarp_buffer_t * buff);
|
|
||||||
|
|
||||||
template<typename ValType>
|
template <typename ValType> bool BDecode(ValType &t, llarp_buffer_t *buff);
|
||||||
bool BDecode(ValType & t, llarp_buffer_t * buff);
|
|
||||||
|
|
||||||
static bool bencodeDict(llarp_buffer_t * buff)
|
static bool bencodeDict(llarp_buffer_t *buff) {
|
||||||
{
|
static uint8_t c = 'd';
|
||||||
static uint8_t c = 'd';
|
return llarp_buffer_write(buff, &c, 1);
|
||||||
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 bencodeDictKey(llarp_buffer_t * buff, const std::string & key)
|
static bool bencodeList(llarp_buffer_t *buff) {
|
||||||
{
|
static uint8_t c = 'l';
|
||||||
std::string kstr = std::to_string(key.size()) + ":" + key;
|
return llarp_buffer_write(buff, &c, 1);
|
||||||
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)
|
static bool bencodeEnd(llarp_buffer_t *buff) {
|
||||||
{
|
static char c = 'e';
|
||||||
std::string sz_str = std::to_string(sz) + ":";
|
return llarp_buffer_write(buff, &c, 1);
|
||||||
return bencodeDictKey(buff, key) &&
|
}
|
||||||
llarp_buffer_write(buff, sz_str.c_str(), sz_str.size()) &&
|
|
||||||
llarp_buffer_write(buff, data, sz);
|
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>
|
template <typename T>
|
||||||
bool BEncode(const std::list<T> & l, llarp_buffer_t * buff)
|
bool BEncode(const std::list<T> &l, llarp_buffer_t *buff) {
|
||||||
{
|
if (bencodeList(buff)) {
|
||||||
if(bencodeList(buff))
|
for (const auto &itr : l)
|
||||||
{
|
if (!BEncode(itr, buff))
|
||||||
for(const auto & itr : l)
|
return false;
|
||||||
if(!BEncode(itr, buff))
|
return bencodeEnd(buff);
|
||||||
return false;
|
|
||||||
return bencodeEnd(buff);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,75 +1,65 @@
|
|||||||
#include <llarp/config.h>
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "ini.hpp"
|
#include "ini.hpp"
|
||||||
#include "mem.hpp"
|
#include "mem.hpp"
|
||||||
|
#include <llarp/config.h>
|
||||||
|
|
||||||
namespace llarp
|
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
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) {
|
||||||
void llarp_new_config(struct llarp_config ** conf)
|
llarp_config *c = new llarp_config;
|
||||||
{
|
*conf = c;
|
||||||
llarp_config * c = new llarp_config;
|
}
|
||||||
*conf = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
void llarp_free_config(struct llarp_config ** conf)
|
void llarp_free_config(struct llarp_config **conf) {
|
||||||
{
|
if (*conf)
|
||||||
if(*conf)
|
delete *conf;
|
||||||
delete *conf;
|
*conf = nullptr;
|
||||||
*conf = nullptr;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int llarp_load_config(struct llarp_config * conf, const char * fname)
|
int llarp_load_config(struct llarp_config *conf, const char *fname) {
|
||||||
{
|
if (!conf->impl.Load(fname))
|
||||||
if(!conf->impl.Load(fname)) return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void llarp_config_iter(struct llarp_config * conf, struct llarp_config_iterator * iter)
|
void llarp_config_iter(struct llarp_config *conf,
|
||||||
{
|
struct llarp_config_iterator *iter) {
|
||||||
iter->conf = conf;
|
iter->conf = conf;
|
||||||
std::map<std::string, llarp::Config::section_t&> sections = {
|
std::map<std::string, llarp::Config::section_t &> sections = {
|
||||||
{"router", conf->impl.router},
|
{"router", conf->impl.router},
|
||||||
{"network", conf->impl.network},
|
{"network", conf->impl.network},
|
||||||
{"links", conf->impl.links},
|
{"links", conf->impl.links},
|
||||||
{"netdb", conf->impl.netdb}
|
{"netdb", conf->impl.netdb}};
|
||||||
};
|
for (const auto §ion : sections)
|
||||||
for(const auto & section : sections)
|
for (const auto &item : section.second)
|
||||||
for(const auto & item : section.second)
|
iter->visit(iter, section.first.c_str(), item.first.c_str(),
|
||||||
iter->visit(iter, section.first.c_str(), item.first.c_str(), item.second.c_str());
|
item.second.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,90 +1,84 @@
|
|||||||
#include <llarp/crypto.h>
|
#include <llarp/crypto.h>
|
||||||
#include <sodium/crypto_stream_xchacha20.h>
|
|
||||||
#include <sodium/crypto_generichash.h>
|
#include <sodium/crypto_generichash.h>
|
||||||
#include <sodium/crypto_scalarmult.h>
|
#include <sodium/crypto_scalarmult.h>
|
||||||
#include <sodium/crypto_sign.h>
|
#include <sodium/crypto_sign.h>
|
||||||
|
#include <sodium/crypto_stream_xchacha20.h>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
namespace sodium {
|
||||||
namespace sodium
|
bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n) {
|
||||||
{
|
uint8_t *base = (uint8_t *)buff.base;
|
||||||
bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n)
|
return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
|
||||||
{
|
}
|
||||||
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)
|
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;
|
uint8_t *out = *shared;
|
||||||
const size_t outsz = SHAREDKEYSIZE;
|
const size_t outsz = SHAREDKEYSIZE;
|
||||||
crypto_generichash_state h;
|
crypto_generichash_state h;
|
||||||
if(crypto_scalarmult(out, local_key, remote_key) == -1) return false;
|
if (crypto_scalarmult(out, local_key, remote_key) == -1)
|
||||||
crypto_generichash_init(&h, NULL, 0U, outsz);
|
return false;
|
||||||
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
|
crypto_generichash_init(&h, NULL, 0U, outsz);
|
||||||
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
|
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
|
||||||
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
|
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
|
||||||
crypto_generichash_final(&h, out, outsz);
|
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
|
||||||
return true;
|
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)
|
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;
|
llarp_pubkey_t local_pk;
|
||||||
crypto_scalarmult_base(local_pk, sk);
|
crypto_scalarmult_base(local_pk, sk);
|
||||||
if(dh(shared, local_pk, pk, pk, sk))
|
if (dh(shared, local_pk, pk, pk, sk)) {
|
||||||
{
|
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
|
||||||
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1;
|
TUNNOUNCESIZE) != -1;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool dh_server(llarp_sharedkey_t * shared, llarp_pubkey_t pk, llarp_tunnel_nounce_t n, llarp_seckey_t sk)
|
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;
|
llarp_pubkey_t local_pk;
|
||||||
crypto_scalarmult_base(local_pk, sk);
|
crypto_scalarmult_base(local_pk, sk);
|
||||||
if(dh(shared, pk, local_pk, pk, sk))
|
if (dh(shared, pk, local_pk, pk, sk)) {
|
||||||
{
|
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
|
||||||
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1;
|
TUNNOUNCESIZE) != -1;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hash(llarp_hash_t * result, llarp_buffer_t buff)
|
bool hash(llarp_hash_t *result, llarp_buffer_t buff) {
|
||||||
{
|
const uint8_t *base = (const uint8_t *)buff.base;
|
||||||
const uint8_t * base = (const uint8_t *) buff.base;
|
return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1;
|
||||||
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)
|
bool hmac(llarp_hash_t *result, llarp_buffer_t buff, llarp_seckey_t secret) {
|
||||||
{
|
const uint8_t *base = (const uint8_t *)buff.base;
|
||||||
const uint8_t * base = (const uint8_t *) buff.base;
|
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz,
|
||||||
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz, secret, HMACSECSIZE) != -1;
|
secret, HMACSECSIZE) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sign(llarp_sig_t * result, llarp_seckey_t secret, llarp_buffer_t buff)
|
bool sign(llarp_sig_t *result, llarp_seckey_t secret, llarp_buffer_t buff) {
|
||||||
{
|
const uint8_t *base = (const uint8_t *)buff.base;
|
||||||
const uint8_t * base = (const uint8_t *) buff.base;
|
return crypto_sign_detached(*result, nullptr, base, buff.sz, secret) != -1;
|
||||||
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)
|
bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) {
|
||||||
{
|
const uint8_t *base = (const uint8_t *)buff.base;
|
||||||
const uint8_t * base = (const uint8_t *) buff.base;
|
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
|
||||||
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} // namespace sodium
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void llarp_crypto_libsodium_init(struct llarp_crypto * c)
|
void llarp_crypto_libsodium_init(struct llarp_crypto *c) {
|
||||||
{
|
c->xchacha20 = llarp::sodium::xchacha20;
|
||||||
c->xchacha20 = llarp::sodium::xchacha20;
|
c->dh_client = llarp::sodium::dh_client;
|
||||||
c->dh_client = llarp::sodium::dh_client;
|
c->dh_server = llarp::sodium::dh_server;
|
||||||
c->dh_server = llarp::sodium::dh_server;
|
c->hash = llarp::sodium::hash;
|
||||||
c->hash = llarp::sodium::hash;
|
c->hmac = llarp::sodium::hmac;
|
||||||
c->hmac = llarp::sodium::hmac;
|
c->sign = llarp::sodium::sign;
|
||||||
c->sign = llarp::sodium::sign;
|
c->verify = llarp::sodium::verify;
|
||||||
c->verify = llarp::sodium::verify;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,184 +1,154 @@
|
|||||||
|
#include "mem.hpp"
|
||||||
#include <llarp/ev.h>
|
#include <llarp/ev.h>
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
#include "mem.hpp"
|
|
||||||
|
|
||||||
struct llarp_ev_loop
|
struct llarp_ev_loop {
|
||||||
{
|
|
||||||
uv_loop_t _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>());
|
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_ev_loop>());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void operator delete(void * ptr)
|
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
|
||||||
{
|
|
||||||
llarp_g_mem.free(ptr);
|
uv_loop_t *loop() { return &_loop; }
|
||||||
}
|
|
||||||
|
|
||||||
uv_loop_t * loop() { return &_loop; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
struct udp_listener {
|
||||||
struct udp_listener
|
static void *operator new(size_t sz) {
|
||||||
{
|
return llarp_g_mem.alloc(sz, alignment<udp_listener>());
|
||||||
static void * operator new(size_t sz)
|
}
|
||||||
{
|
|
||||||
return llarp_g_mem.alloc(sz, alignment<udp_listener>());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void operator delete(void * ptr)
|
static void operator delete(void *ptr) { llarp_g_mem.free(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** called after closed */
|
uv_udp_t _handle;
|
||||||
void closed()
|
struct llarp_udp_listener *listener;
|
||||||
{
|
|
||||||
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)
|
void recvfrom(const struct sockaddr *addr, char *buff, ssize_t sz) {
|
||||||
{
|
if (listener->recvfrom)
|
||||||
udp_listener * l = static_cast<udp_listener *>(handle->data);
|
listener->recvfrom(listener, addr, buff, sz);
|
||||||
l->recvfrom(addr, buf->base, nread);
|
|
||||||
llarp_g_mem.free(buf->base);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void udp_close_cb(uv_handle_t * handle)
|
/** called after closed */
|
||||||
{
|
void closed() {
|
||||||
udp_listener * l = static_cast<udp_listener *>(handle->data);
|
if (listener->closed)
|
||||||
l->closed();
|
listener->closed(listener);
|
||||||
delete l;
|
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)
|
namespace llarp {
|
||||||
{
|
|
||||||
struct llarp_ev_job * ev = static_cast<llarp_ev_job *>(handle->data);
|
static void ev_handle_async_closed(uv_handle_t *handle) {
|
||||||
llarp_g_mem.free(ev);
|
struct llarp_ev_job *ev = static_cast<llarp_ev_job *>(handle->data);
|
||||||
llarp_g_mem.free(handle);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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" {
|
extern "C" {
|
||||||
void llarp_ev_loop_alloc(struct llarp_ev_loop ** ev)
|
void llarp_ev_loop_alloc(struct llarp_ev_loop **ev) {
|
||||||
{
|
*ev = new llarp_ev_loop;
|
||||||
*ev = new llarp_ev_loop;
|
if (*ev) {
|
||||||
if (*ev)
|
uv_loop_init((*ev)->loop());
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int llarp_ev_loop_run(struct llarp_ev_loop * ev)
|
void llarp_ev_loop_free(struct llarp_ev_loop **ev) {
|
||||||
{
|
if (*ev) {
|
||||||
return uv_run(ev->loop(), UV_RUN_DEFAULT);
|
uv_loop_close((*ev)->loop());
|
||||||
|
llarp_g_mem.free(*ev);
|
||||||
}
|
}
|
||||||
|
*ev = nullptr;
|
||||||
int llarp_ev_add_udp_listener(struct llarp_ev_loop * ev, struct llarp_udp_listener * listener)
|
}
|
||||||
{
|
|
||||||
sockaddr_in6 addr;
|
int llarp_ev_loop_run(struct llarp_ev_loop *ev) {
|
||||||
uv_ip6_addr(listener->host, listener->port, &addr);
|
return uv_run(ev->loop(), UV_RUN_DEFAULT);
|
||||||
int ret = 0;
|
}
|
||||||
llarp::udp_listener * l = new llarp::udp_listener;
|
|
||||||
listener->impl = l;
|
int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
|
||||||
l->udp()->data = l;
|
struct llarp_udp_listener *listener) {
|
||||||
l->listener = listener;
|
sockaddr_in6 addr;
|
||||||
|
uv_ip6_addr(listener->host, listener->port, &addr);
|
||||||
ret = uv_udp_init(ev->loop(), l->udp());
|
int ret = 0;
|
||||||
if (ret == 0)
|
llarp::udp_listener *l = new llarp::udp_listener;
|
||||||
{
|
listener->impl = l;
|
||||||
ret = uv_udp_bind(l->udp(), (const sockaddr *)&addr, 0);
|
l->udp()->data = l;
|
||||||
if (ret == 0)
|
l->listener = listener;
|
||||||
{
|
|
||||||
ret = uv_udp_recv_start(l->udp(), llarp::udp_alloc_cb, llarp::udp_recv_cb);
|
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 llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) {
|
||||||
{
|
int ret = -1;
|
||||||
int ret = -1;
|
if (listener) {
|
||||||
if(listener)
|
llarp::udp_listener *l = static_cast<llarp::udp_listener *>(listener->impl);
|
||||||
{
|
if (l) {
|
||||||
llarp::udp_listener * l = static_cast<llarp::udp_listener*>(listener->impl);
|
if (!uv_udp_recv_stop(l->udp())) {
|
||||||
if(l)
|
l->closed();
|
||||||
{
|
delete l;
|
||||||
if(!uv_udp_recv_stop(l->udp()))
|
ret = 0;
|
||||||
{
|
|
||||||
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)
|
void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { uv_stop(loop->loop()); }
|
||||||
{
|
|
||||||
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>()));
|
bool llarp_ev_async(struct llarp_ev_loop *loop, struct llarp_ev_job job) {
|
||||||
job_copy->work = job.work;
|
struct llarp_ev_job *job_copy =
|
||||||
job_copy->loop = loop;
|
static_cast<struct llarp_ev_job *>(llarp_g_mem.alloc(
|
||||||
job_copy->user = job.user;
|
sizeof(struct llarp_ev_job), llarp::alignment<llarp_ev_job>()));
|
||||||
uv_async_t * async = static_cast<uv_async_t *>(llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment<uv_async_t>()));
|
job_copy->work = job.work;
|
||||||
async->data = job_copy;
|
job_copy->loop = loop;
|
||||||
if(uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 && uv_async_send(async))
|
job_copy->user = job.user;
|
||||||
return true;
|
uv_async_t *async = static_cast<uv_async_t *>(
|
||||||
else
|
llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment<uv_async_t>()));
|
||||||
{
|
async->data = job_copy;
|
||||||
llarp_g_mem.free(job_copy);
|
if (uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 &&
|
||||||
llarp_g_mem.free(async);
|
uv_async_send(async))
|
||||||
return false;
|
return true;
|
||||||
}
|
else {
|
||||||
|
llarp_g_mem.free(job_copy);
|
||||||
|
llarp_g_mem.free(async);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -1,33 +1,26 @@
|
|||||||
#ifndef LLARP_LINK_HPP
|
#ifndef LLARP_LINK_HPP
|
||||||
#define LLARP_LINK_HPP
|
#define LLARP_LINK_HPP
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <llarp/crypto.h>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
#include <llarp/crypto.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <functional>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#include "mem.hpp"
|
||||||
#include <llarp/ev.h>
|
#include <llarp/ev.h>
|
||||||
#include <llarp/router_contact.h>
|
#include <llarp/router_contact.h>
|
||||||
#include "mem.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
struct llarp_link
|
struct llarp_link {
|
||||||
{
|
static void *operator new(size_t sz) {
|
||||||
static void * operator new(size_t sz)
|
|
||||||
{
|
|
||||||
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_link>());
|
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_link>());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void operator delete(void * ptr)
|
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
|
||||||
{
|
|
||||||
llarp_g_mem.free(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sockaddr_in6 localaddr;
|
struct sockaddr_in6 localaddr;
|
||||||
int af;
|
int af;
|
||||||
llarp_udp_listener listener;
|
llarp_udp_listener listener;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
#include <llarp/mem.h>
|
#include <llarp/mem.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
struct llarp_alloc llarp_g_mem = {
|
struct llarp_alloc llarp_g_mem = {.alloc = nullptr, .free = nullptr};
|
||||||
.alloc = nullptr,
|
|
||||||
.free = nullptr
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
#ifndef LLARP_MEM_HPP
|
#ifndef LLARP_MEM_HPP
|
||||||
#define LLARP_MEM_HPP
|
#define LLARP_MEM_HPP
|
||||||
#include <llarp/mem.h>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
namespace llarp
|
#include <llarp/mem.h>
|
||||||
{
|
namespace llarp {
|
||||||
template<typename T>
|
template <typename T> static constexpr size_t alignment() {
|
||||||
static constexpr size_t alignment()
|
return std::exp2(1 + std::floor(std::log2(sizeof(T))));
|
||||||
{
|
|
||||||
return std::exp2(1+std::floor(std::log2(sizeof(T))));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,23 +1,20 @@
|
|||||||
#include <llarp/mem.h>
|
|
||||||
#include <jemalloc/jemalloc.h>
|
#include <jemalloc/jemalloc.h>
|
||||||
|
#include <llarp/mem.h>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
static void *jem_malloc(size_t sz, size_t align) {
|
||||||
static void * jem_malloc(size_t sz, size_t align)
|
return mallocx(sz, MALLOCX_ALIGN(align));
|
||||||
{
|
}
|
||||||
return mallocx(sz, MALLOCX_ALIGN(align));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void jem_free(void * ptr)
|
static void jem_free(void *ptr) {
|
||||||
{
|
if (ptr)
|
||||||
if(ptr) free(ptr);
|
free(ptr);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void llarp_mem_jemalloc()
|
void llarp_mem_jemalloc() {
|
||||||
{
|
llarp_g_mem.alloc = llarp::jem_malloc;
|
||||||
llarp_g_mem.alloc = llarp::jem_malloc;
|
llarp_g_mem.free = llarp::jem_free;
|
||||||
llarp_g_mem.free = llarp::jem_free;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,24 @@
|
|||||||
#include <llarp/mem.h>
|
#include <llarp/mem.h>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
void *std_malloc(size_t sz, size_t align) {
|
||||||
void * std_malloc(size_t sz, size_t align)
|
(void)align;
|
||||||
{
|
void *ptr = malloc(sz);
|
||||||
(void) align;
|
if (ptr)
|
||||||
void * ptr = malloc(sz);
|
return ptr;
|
||||||
if(ptr) return ptr;
|
abort();
|
||||||
abort();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void std_free(void * ptr)
|
void std_free(void *ptr) {
|
||||||
{
|
if (ptr)
|
||||||
if(ptr) free(ptr);
|
free(ptr);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void llarp_mem_std()
|
void llarp_mem_std() {
|
||||||
{
|
llarp_g_mem.alloc = llarp::std_malloc;
|
||||||
llarp_g_mem.alloc = llarp::std_malloc;
|
llarp_g_mem.free = llarp::std_free;
|
||||||
llarp_g_mem.free = llarp::std_free;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,133 +1,105 @@
|
|||||||
#include <llarp/router.h>
|
|
||||||
#include <llarp/link.h>
|
|
||||||
#include "link.hpp"
|
#include "link.hpp"
|
||||||
#include "mem.hpp"
|
#include "mem.hpp"
|
||||||
#include "str.hpp"
|
#include "str.hpp"
|
||||||
|
#include <llarp/link.h>
|
||||||
|
#include <llarp/router.h>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
void router_iter_config(llarp_config_iterator *iter, const char *section,
|
||||||
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val);
|
const char *key, const char *val);
|
||||||
struct router_links
|
struct router_links {
|
||||||
{
|
struct llarp_link *link = nullptr;
|
||||||
struct llarp_link * link = nullptr;
|
struct router_links *next = nullptr;
|
||||||
struct router_links * next = nullptr;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
}
|
} // namespace llarp
|
||||||
|
|
||||||
struct llarp_router
|
struct llarp_router {
|
||||||
{
|
struct llarp_threadpool *tp;
|
||||||
struct llarp_threadpool * tp;
|
|
||||||
llarp::router_links links;
|
llarp::router_links links;
|
||||||
llarp_crypto crypto;
|
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>());
|
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void operator delete(void * ptr)
|
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
|
||||||
{
|
|
||||||
llarp_g_mem.free(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddLink(struct llarp_link * link)
|
void AddLink(struct llarp_link *link) {
|
||||||
{
|
llarp::router_links *head = &links;
|
||||||
llarp::router_links * head = &links;
|
while (head->next && head->link)
|
||||||
while(head->next && head->link)
|
|
||||||
head = head->next;
|
head = head->next;
|
||||||
|
|
||||||
if(head->link)
|
if (head->link)
|
||||||
head->next = new llarp::router_links{link, nullptr};
|
head->next = new llarp::router_links{link, nullptr};
|
||||||
else
|
else
|
||||||
head->link = link;
|
head->link = link;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ForEachLink(std::function<void(llarp_link *)> visitor)
|
void ForEachLink(std::function<void(llarp_link *)> visitor) {
|
||||||
{
|
llarp::router_links *cur = &links;
|
||||||
llarp::router_links * cur = &links;
|
do {
|
||||||
do
|
if (cur->link)
|
||||||
{
|
|
||||||
if(cur->link)
|
|
||||||
visitor(cur->link);
|
visitor(cur->link);
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
} while (cur);
|
||||||
while(cur);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Close()
|
|
||||||
{
|
|
||||||
ForEachLink(llarp_link_stop);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
void Close() { ForEachLink(llarp_link_stop); }
|
||||||
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
struct llarp_router * llarp_init_router(struct llarp_threadpool * tp)
|
struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) {
|
||||||
{
|
llarp_router *router = new llarp_router;
|
||||||
llarp_router * router = new llarp_router;
|
router->tp = tp;
|
||||||
router->tp = tp;
|
llarp_crypto_libsodium_init(&router->crypto);
|
||||||
llarp_crypto_libsodium_init(&router->crypto);
|
return router;
|
||||||
return router;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf)
|
int llarp_configure_router(struct llarp_router *router,
|
||||||
{
|
struct llarp_config *conf) {
|
||||||
llarp_config_iterator iter;
|
llarp_config_iterator iter;
|
||||||
iter.user = router;
|
iter.user = router;
|
||||||
iter.visit = llarp::router_iter_config;
|
iter.visit = llarp::router_iter_config;
|
||||||
llarp_config_iter(conf, &iter);
|
llarp_config_iter(conf, &iter);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop)
|
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) {
|
||||||
{
|
router->ForEachLink([loop](llarp_link *link) {
|
||||||
router->ForEachLink([loop](llarp_link * link) {
|
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
|
||||||
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void llarp_stop_router(struct llarp_router * router)
|
void llarp_stop_router(struct llarp_router *router) { router->Close(); }
|
||||||
{
|
|
||||||
router->Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void llarp_free_router(struct llarp_router ** router)
|
void llarp_free_router(struct llarp_router **router) {
|
||||||
{
|
if (*router) {
|
||||||
if(*router)
|
(*router)->ForEachLink([](llarp_link *link) { llarp_g_mem.free(link); });
|
||||||
{
|
delete *router;
|
||||||
(*router)->ForEachLink([](llarp_link * link) { llarp_g_mem.free(link); });
|
|
||||||
delete *router;
|
|
||||||
}
|
|
||||||
*router = nullptr;
|
|
||||||
}
|
}
|
||||||
|
*router = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
|
||||||
|
void router_iter_config(llarp_config_iterator *iter, const char *section,
|
||||||
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val)
|
const char *key, const char *val) {
|
||||||
{
|
llarp_router *self = static_cast<llarp_router *>(iter->user);
|
||||||
llarp_router * self = static_cast<llarp_router *>(iter->user);
|
if (StrEq(section, "links")) {
|
||||||
if (StrEq(section, "links"))
|
if (StrEq(val, "ip")) {
|
||||||
{
|
struct llarp_link *link = llarp_link_alloc();
|
||||||
if(StrEq(val, "ip"))
|
if (llarp_link_configure(link, key, AF_INET6))
|
||||||
{
|
self->AddLink(link);
|
||||||
struct llarp_link * link = llarp_link_alloc();
|
else {
|
||||||
if(llarp_link_configure(link, key, AF_INET6))
|
llarp_link_free(&link);
|
||||||
self->AddLink(link);
|
printf("failed to configure %s link for %s\n", val, key);
|
||||||
else
|
|
||||||
{
|
|
||||||
llarp_link_free(&link);
|
|
||||||
printf("failed to configure %s link for %s\n", val, key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (StrEq(val, "eth"))
|
|
||||||
{
|
|
||||||
/** todo: ethernet link */
|
|
||||||
}
|
}
|
||||||
|
} else if (StrEq(val, "eth")) {
|
||||||
|
/** todo: ethernet link */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} // namespace llarp
|
||||||
|
@ -1,24 +1,20 @@
|
|||||||
#include "router_contact.hpp"
|
#include "router_contact.hpp"
|
||||||
#include "exit_info.hpp"
|
|
||||||
#include "address_info.hpp"
|
#include "address_info.hpp"
|
||||||
#include "bencode.hpp"
|
#include "bencode.hpp"
|
||||||
|
#include "exit_info.hpp"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
|
||||||
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);
|
||||||
std::list<llarp_ai> addresses = ai_list_to_std(a.addrs);
|
return bencodeDict(buff) && bencodeDictKey(buff, "a") &&
|
||||||
std::list<llarp_xi> exits = xi_list_to_std(a.exits);
|
BEncode(addresses, buff) &&
|
||||||
return bencodeDict(buff) &&
|
bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) &&
|
||||||
bencodeDictKey(buff, "a") && BEncode(addresses, buff) &&
|
bencodeDict_Int(buff, "v", 0) && bencodeDictKey(buff, "x") &&
|
||||||
bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) &&
|
BEncode(exits, buff) &&
|
||||||
bencodeDict_Int(buff, "v", 0) &&
|
bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
|
||||||
bencodeDictKey(buff, "x") && BEncode(exits, buff) &&
|
bencodeEnd(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 <chrono>
|
||||||
|
#include <llarp/time.h>
|
||||||
|
|
||||||
namespace llarp
|
namespace llarp {
|
||||||
{
|
typedef std::chrono::steady_clock clock_t;
|
||||||
typedef std::chrono::steady_clock clock_t;
|
|
||||||
|
|
||||||
template<typename Res, typename IntType>
|
template <typename Res, typename IntType> static IntType time_since_epoch() {
|
||||||
static IntType time_since_epoch()
|
return std::chrono::duration_cast<Res>(
|
||||||
{
|
llarp::clock_t::now().time_since_epoch())
|
||||||
return std::chrono::duration_cast<Res>(llarp::clock_t::now().time_since_epoch()).count();
|
.count();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
llarp_time_t llarp_time_now_ms()
|
llarp_time_t llarp_time_now_ms() {
|
||||||
{
|
return llarp::time_since_epoch<std::chrono::milliseconds, llarp_time_t>();
|
||||||
return llarp::time_since_epoch<std::chrono::milliseconds, llarp_time_t>();
|
}
|
||||||
}
|
|
||||||
|
llarp_seconds_t llarp_time_now_sec() {
|
||||||
llarp_seconds_t llarp_time_now_sec()
|
return llarp::time_since_epoch<std::chrono::seconds, llarp_seconds_t>();
|
||||||
{
|
}
|
||||||
return llarp::time_since_epoch<std::chrono::seconds, llarp_seconds_t>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue