mirror of https://github.com/oxen-io/lokinet
more
parent
781637bbf4
commit
1e86edd321
@ -1,6 +1,10 @@
|
||||
|
||||
#include <sarp.h>
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
struct sarp_router * router = NULL;
|
||||
struct sarp_alloc mem;
|
||||
sarp_mem_jemalloc(&mem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
#ifndef SARP_BUFFER_H_
|
||||
#define SARP_BUFFER_H_
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t * ptr;
|
||||
size_t sz;
|
||||
} sarp_buffer_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,19 @@
|
||||
#ifndef SARP_CONFIG_H_
|
||||
#define SARP_CONFIG_H_
|
||||
#include <sarp/mem.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct sarp_config;
|
||||
|
||||
void sarp_new_config(struct sarp_config ** conf, struct sarp_alloc * mem);
|
||||
void sarp_free_config(struct sarp_config ** conf);
|
||||
|
||||
int sarp_load_config(struct sarp_config * conf, const char * fname);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
@ -0,0 +1,44 @@
|
||||
#ifndef SARP_CRYPTO_H_
|
||||
#define SARP_CRYPTO_H_
|
||||
#include <sarp/buffer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define PUBKEYSIZE 32
|
||||
#define SECKEYSIZE 32
|
||||
#define SYMKEYSIZE 32
|
||||
#define NOUNCESIZE 24
|
||||
#define SHAREDKEYSIZE 64
|
||||
#define HASHSIZE 64
|
||||
#define HMACSECSIZE 32
|
||||
#define SIGSIZE 64
|
||||
|
||||
typedef uint8_t sarp_pubkey_t[PUBKEYSIZE];
|
||||
typedef uint8_t sarp_seckey_t[SECKEYSIZE];
|
||||
typedef uint8_t sarp_symkey_t[SYMKEYSIZE];
|
||||
typedef uint8_t sarp_nounce_t[NOUNCESIZE];
|
||||
typedef uint8_t sarp_sharedkey_t[SHAREDKEYSIZE];
|
||||
typedef uint8_t sarp_hash_t[HASHSIZE];
|
||||
typedef uint8_t sarp_hmacsec_t[HMACSECSIZE];
|
||||
typedef uint8_t sarp_sig_t[SIGSIZE];
|
||||
|
||||
struct sarp_crypto
|
||||
{
|
||||
int (*xchacha20)(sarp_buffer_t, sarp_symkey_t, sarp_nounce_t);
|
||||
int (*dh_client)(sarp_sharedkey_t *, sarp_pubkey_t, sarp_seckey_t);
|
||||
int (*dh_server)(sarp_sharedkey_t *, sarp_pubkey_t, sarp_seckey_t);
|
||||
int (*hash)(sarp_hash_t *, sarp_buffer_t);
|
||||
int (*mhac)(sarp_hash_t *, sarp_buffer_t, sarp_hmacsec_t);
|
||||
int (*sign)(sarp_sig_t *, sarp_seckey_t, sarp_buffer_t);
|
||||
int (*verify)(sarp_pubkey_t, sarp_buffer_t, sarp_sig_t);
|
||||
};
|
||||
|
||||
void sarp_crypto_libsodium_init(struct sarp_crypto * c);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,26 @@
|
||||
#ifndef SARP_MEM_H_
|
||||
#define SARP_MEM_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
struct sarp_alloc
|
||||
{
|
||||
void * (*malloc)(size_t sz);
|
||||
void * (*realloc)(void * ptr, size_t sz);
|
||||
void * (*calloc)(size_t n, size_t sz);
|
||||
void (*free)(void * ptr);
|
||||
};
|
||||
void sarp_mem_jemalloc(struct sarp_alloc * mem);
|
||||
void sarp_mem_libc(struct sarp_alloc * mem);
|
||||
void sarp_mem_dmalloc(struct sarp_alloc * mem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -1,5 +1,24 @@
|
||||
#ifndef SARP_ROUTER_H_
|
||||
#define SARP_ROUTER_H_
|
||||
#include <sarp/config.h>
|
||||
#include <sarp/mem.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct sarp_router;
|
||||
|
||||
void sarp_init_router(struct sarp_router ** router, struct sarp_alloc * mem);
|
||||
void sarp_free_router(struct sarp_router ** router);
|
||||
|
||||
int sarp_configure_router(struct sarp_router * router, struct sarp_config * conf);
|
||||
|
||||
void sarp_run_router(struct sarp_router * router);
|
||||
void sarp_stop_router(struct sarp_router * router);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,57 @@
|
||||
#include <sarp/config.h>
|
||||
#include "ini.hpp"
|
||||
|
||||
namespace sarp
|
||||
{
|
||||
struct config
|
||||
{
|
||||
typedef std::map<std::string, std::string> section_t;
|
||||
section_t router;
|
||||
section_t network;
|
||||
section_t netdb;
|
||||
|
||||
bool Load(const char * fname)
|
||||
{
|
||||
std::ifstream f;
|
||||
f.open(fname);
|
||||
if(f.is_open())
|
||||
{
|
||||
ini::Parser parser(f);
|
||||
auto & top = parser.top();
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
extern "C" {
|
||||
struct sarp_config
|
||||
{
|
||||
sarp::config impl;
|
||||
sarp_alloc * mem;
|
||||
};
|
||||
|
||||
void sarp_new_config(struct sarp_config ** conf, struct sarp_alloc * mem)
|
||||
{
|
||||
sarp_config * c = static_cast<sarp_config*>(mem->malloc(sizeof(struct sarp_config)));
|
||||
c->mem = mem;
|
||||
*conf = c;
|
||||
}
|
||||
|
||||
void sarp_free_config(struct sarp_config ** conf)
|
||||
{
|
||||
sarp_alloc * mem = (*conf)->mem;
|
||||
mem->free(*conf);
|
||||
*conf = nullptr;
|
||||
}
|
||||
|
||||
int sarp_load_config(struct sarp_config * conf, const char * fname)
|
||||
{
|
||||
if(!conf->impl.Load(fname)) return -1;
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
#include <sarp/crypto.h>
|
||||
#include <sodium/crypto_stream_xchacha20.h>
|
||||
#include <sodium/crypto_generichash.h>
|
||||
#include <sodium/crypto_scalarmult.h>
|
||||
|
||||
namespace sarp
|
||||
{
|
||||
namespace sodium
|
||||
{
|
||||
int xchacha20(sarp_buffer_t buff, sarp_symkey_t k, sarp_nounce_t n)
|
||||
{
|
||||
return crypto_stream_xchacha20_xor(buff.ptr, buff.ptr, buff.sz, n, k);
|
||||
}
|
||||
|
||||
int dh(sarp_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 = sizeof(sarp_sharedkey_t);
|
||||
crypto_generichash_state h;
|
||||
if(crypto_scalarmult(out, local_key, remote_key) == -1) return -1;
|
||||
crypto_generichash_init(&h, NULL, 0U, outsz);
|
||||
crypto_generichash_update(&h, client_pk, sizeof(sarp_pubkey_t));
|
||||
crypto_generichash_update(&h, server_pk, sizeof(sarp_pubkey_t));
|
||||
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
|
||||
crypto_generichash_final(&h, out, outsz);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dh_client(sarp_sharedkey_t * shared, sarp_pubkey_t pk, sarp_seckey_t sk)
|
||||
{
|
||||
sarp_pubkey_t local_pk;
|
||||
crypto_scalarmult_base(local_pk, sk);
|
||||
return dh(shared, local_pk, pk, pk, sk);
|
||||
}
|
||||
|
||||
int dh_server(sarp_sharedkey_t * shared, sarp_pubkey_t pk, sarp_seckey_t sk)
|
||||
{
|
||||
sarp_pubkey_t local_pk;
|
||||
crypto_scalarmult_base(local_pk, sk);
|
||||
return dh(shared, pk, local_pk, pk, sk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void sarp_crypto_libsodium_init(struct sarp_crypto * c)
|
||||
{
|
||||
c->xchacha20 = sarp::sodium::xchacha20;
|
||||
c->dh_client = sarp::sodium::dh_client;
|
||||
c->dh_server = sarp::sodium::dh_server;
|
||||
}
|
||||
}
|
@ -0,0 +1,186 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) <2015> <carriez.md@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef INI_HPP
|
||||
#define INI_HPP
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace ini {
|
||||
|
||||
struct Level
|
||||
{
|
||||
Level() : parent(NULL), depth(0) {}
|
||||
Level(Level* p) : parent(p), depth(0) {}
|
||||
|
||||
typedef std::map<std::string, std::string> value_map_t;
|
||||
typedef std::map<std::string, Level> section_map_t;
|
||||
typedef std::list<value_map_t::const_iterator> values_t;
|
||||
typedef std::list<section_map_t::const_iterator> sections_t;
|
||||
value_map_t values;
|
||||
section_map_t sections;
|
||||
values_t ordered_values; // original order in the ini file
|
||||
sections_t ordered_sections;
|
||||
Level* parent;
|
||||
size_t depth;
|
||||
|
||||
const std::string& operator[](const std::string& name) { return values[name]; }
|
||||
Level& operator()(const std::string& name) { return sections[name]; }
|
||||
};
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
Parser(const std::string & fname) : Parser(fname.c_str()) {}
|
||||
Parser(const char* fn);
|
||||
Parser(std::istream& f) : f_(&f), ln_(0) { parse(top_); }
|
||||
Level& top() { return top_; }
|
||||
void dump(std::ostream& s) { dump(s, top(), ""); }
|
||||
|
||||
private:
|
||||
void dump(std::ostream& s, const Level& l, const std::string& sname);
|
||||
void parse(Level& l);
|
||||
void parseSLine(std::string& sname, size_t& depth);
|
||||
void err(const char* s);
|
||||
|
||||
private:
|
||||
Level top_;
|
||||
std::ifstream f0_;
|
||||
std::istream* f_;
|
||||
std::string line_;
|
||||
size_t ln_;
|
||||
};
|
||||
|
||||
inline void
|
||||
Parser::err(const char* s)
|
||||
{
|
||||
char buf[256];
|
||||
sprintf(buf, "%s on line #%ld", s, ln_);
|
||||
throw std::runtime_error(buf);
|
||||
}
|
||||
|
||||
inline std::string trim(const std::string& s)
|
||||
{
|
||||
char p[] = " \t\r\n";
|
||||
long sp = 0;
|
||||
long ep = s.length() - 1;
|
||||
for (; sp <= ep; ++sp)
|
||||
if (!strchr(p, s[sp])) break;
|
||||
for (; ep >= 0; --ep)
|
||||
if (!strchr(p, s[ep])) break;
|
||||
return s.substr(sp, ep-sp+1);
|
||||
}
|
||||
|
||||
inline
|
||||
Parser::Parser(const char* fn) : f0_(fn), f_(&f0_), ln_(0)
|
||||
{
|
||||
if (!f0_)
|
||||
throw std::runtime_error(std::string("failed to open file: ") + fn);
|
||||
|
||||
parse(top_);
|
||||
}
|
||||
|
||||
inline void
|
||||
Parser::parseSLine(std::string& sname, size_t& depth)
|
||||
{
|
||||
depth = 0;
|
||||
for (; depth < line_.length(); ++depth)
|
||||
if (line_[depth] != '[') break;
|
||||
|
||||
sname = line_.substr(depth, line_.length() - 2*depth);
|
||||
}
|
||||
|
||||
inline void
|
||||
Parser::parse(Level& l)
|
||||
{
|
||||
while (std::getline(*f_, line_)) {
|
||||
++ln_;
|
||||
if (line_[0] == '#' || line_[0] == ';') continue;
|
||||
line_ = trim(line_);
|
||||
if (line_.empty()) continue;
|
||||
if (line_[0] == '[') {
|
||||
size_t depth;
|
||||
std::string sname;
|
||||
parseSLine(sname, depth);
|
||||
Level* lp = NULL;
|
||||
Level* parent = &l;
|
||||
if (depth > l.depth + 1)
|
||||
err("section with wrong depth");
|
||||
if (l.depth == depth-1)
|
||||
lp = &l.sections[sname];
|
||||
else {
|
||||
lp = l.parent;
|
||||
size_t n = l.depth - depth;
|
||||
for (size_t i = 0; i < n; ++i) lp = lp->parent;
|
||||
parent = lp;
|
||||
lp = &lp->sections[sname];
|
||||
}
|
||||
if (lp->depth != 0)
|
||||
err("duplicate section name on the same level");
|
||||
if (!lp->parent) {
|
||||
lp->depth = depth;
|
||||
lp->parent = parent;
|
||||
}
|
||||
parent->ordered_sections.push_back(parent->sections.find(sname));
|
||||
parse(*lp);
|
||||
} else {
|
||||
size_t n = line_.find('=');
|
||||
if (n == std::string::npos)
|
||||
err("no '=' found");
|
||||
std::pair<Level::value_map_t::const_iterator, bool> res =
|
||||
l.values.insert(std::make_pair(trim(line_.substr(0, n)),
|
||||
trim(line_.substr(n+1, line_.length()-n-1))));
|
||||
if (!res.second)
|
||||
err("duplicated key found");
|
||||
l.ordered_values.push_back(res.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
Parser::dump(std::ostream& s, const Level& l, const std::string& sname)
|
||||
{
|
||||
if (!sname.empty()) s << '\n';
|
||||
for (size_t i = 0; i < l.depth; ++i) s << '[';
|
||||
if (!sname.empty()) s << sname;
|
||||
for (size_t i = 0; i < l.depth; ++i) s << ']';
|
||||
if (!sname.empty()) s << std::endl;
|
||||
for (Level::values_t::const_iterator it = l.ordered_values.begin(); it != l.ordered_values.end(); ++it)
|
||||
s << (*it)->first << '=' << (*it)->second << std::endl;
|
||||
for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); it != l.ordered_sections.end(); ++it) {
|
||||
assert((*it)->second.depth == l.depth+1);
|
||||
dump(s, (*it)->second, (*it)->first);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // INI_HPP
|
@ -0,0 +1,35 @@
|
||||
#include <sarp/mem.h>
|
||||
#include <jemalloc/jemalloc.h>
|
||||
|
||||
namespace sarp
|
||||
{
|
||||
static void * jem_malloc(size_t sz)
|
||||
{
|
||||
return mallocx(sz, MALLOCX_ZERO);
|
||||
}
|
||||
|
||||
static void jem_free(void * ptr)
|
||||
{
|
||||
if(ptr) free(ptr);
|
||||
}
|
||||
|
||||
static void * jem_calloc(size_t n, size_t sz)
|
||||
{
|
||||
return mallocx(n * sz, MALLOCX_ZERO);
|
||||
}
|
||||
|
||||
static void * jem_realloc(void * ptr, size_t sz)
|
||||
{
|
||||
return rallocx(ptr, sz, MALLOCX_ZERO);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void sarp_mem_jemalloc(struct sarp_alloc * mem)
|
||||
{
|
||||
mem->malloc = sarp::jem_malloc;
|
||||
mem->free = sarp::jem_free;
|
||||
mem->calloc = sarp::jem_calloc;
|
||||
mem->realloc = sarp::jem_realloc;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#include <sarp/mem.h>
|
||||
|
||||
namespace sarp
|
||||
{
|
||||
void * std_malloc(size_t sz)
|
||||
{
|
||||
void * ptr = malloc(sz);
|
||||
if(ptr) return ptr;
|
||||
abort();
|
||||
}
|
||||
|
||||
void std_free(void * ptr)
|
||||
{
|
||||
if(ptr) free(ptr);
|
||||
}
|
||||
|
||||
void * std_calloc(size_t n, size_t sz)
|
||||
{
|
||||
void * ptr = calloc(n, sz);
|
||||
if (ptr) return ptr;
|
||||
abort();
|
||||
}
|
||||
|
||||
void * std_realloc(void * ptr, size_t sz)
|
||||
{
|
||||
ptr = realloc(ptr, sz);
|
||||
if (ptr) return ptr;
|
||||
abort();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void sarp_mem_std(struct sarp_alloc * mem)
|
||||
{
|
||||
mem->malloc = sarp::std_malloc;
|
||||
mem->free = sarp::std_free;
|
||||
mem->calloc = sarp::std_calloc;
|
||||
mem->realloc = sarp::std_realloc;
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
|
||||
|
Loading…
Reference in New Issue