mirror of https://github.com/oxen-io/lokinet
more
parent
464188b67a
commit
b7d4499f43
@ -1,39 +0,0 @@
|
||||
#include <llarp/address_info.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
bool llarp_address_info_bencode(struct llarp_address_info * ai, llarp_buffer_t * buff)
|
||||
{
|
||||
char * ptr = buff->cur;
|
||||
size_t sz = llarp_buffer_size_left(buff);
|
||||
char * end = ptr + sz;
|
||||
int r = 0;
|
||||
r = snprintf(ptr, (end - ptr), "d1:ci%de1:e32:", ai->rank);
|
||||
if (r == -1) return false;
|
||||
ptr += r;
|
||||
if( ( end - ptr ) <= 0 ) return false;
|
||||
|
||||
memcpy(ptr, ai->enc_key, sizeof(ai->enc_key));
|
||||
ptr += sizeof(ai->enc_key);
|
||||
if( ( end - ptr ) <= 0 ) return false;
|
||||
|
||||
r = snprintf(ptr, (end - ptr), "1:d%ld:%s", strlen(ai->dialect), ai->dialect);
|
||||
if (r == -1) return false;
|
||||
ptr += r;
|
||||
if( ( end - ptr ) <= 0 ) return false;
|
||||
|
||||
r = snprintf(ptr, (end - ptr), "1:i%ld:", sizeof(ai->ip));
|
||||
if(r == -1) return false;
|
||||
ptr += r;
|
||||
if( ( end - ptr ) <= 0) return false;
|
||||
|
||||
memcpy(ptr, &ai->ip, sizeof(ai->ip));
|
||||
ptr += sizeof(ai->ip);
|
||||
if( ( end - ptr ) <= 0) return false;
|
||||
|
||||
r = snprintf(ptr, (end - ptr), "1:pi%dee", ai->port);
|
||||
if( r == -1) return false;
|
||||
ptr += r;
|
||||
buff->cur = ptr;
|
||||
return end - ptr <= sz;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
#include "address_info.hpp"
|
||||
#include "bencode.hpp"
|
||||
#include "str.hpp"
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
static void * ai_alloc(size_t sz, size_t align)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct llarp_alloc * llarp_ai_alloc = &llarp_g_mem;
|
||||
|
||||
bool llarp_ai_bencode(struct llarp_ai * ai, llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp::BEncode(*ai, buff);
|
||||
}
|
||||
|
||||
void llarp_ai_list_iterate(struct llarp_ai_list * l, struct llarp_ai_list_iter * itr)
|
||||
{
|
||||
itr->list = l;
|
||||
struct llarp_ai_list * cur = l;
|
||||
do
|
||||
{
|
||||
if(!itr->visit(itr, cur->data))
|
||||
return;
|
||||
cur = cur->next;
|
||||
}
|
||||
while(cur->next);
|
||||
}
|
||||
}
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
bool BEncode(const llarp_ai & a, llarp_buffer_t * buff)
|
||||
{
|
||||
return bencodeDict(buff) &&
|
||||
bencodeDict_Int(buff, "c", a.rank) &&
|
||||
bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) &&
|
||||
bencodeDict_Bytes(buff, "d", a.dialect, UStrLen(a.dialect, sizeof(a.dialect))) &&
|
||||
bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) &&
|
||||
bencodeDict_Int(buff, "p", a.port) &&
|
||||
bencodeDict_Int(buff, "v", 0) &&
|
||||
bencodeEnd(buff);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef LLARP_AI_HPP
|
||||
#define LLARP_AI_HPP
|
||||
#include <llarp/address_info.h>
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
|
||||
struct llarp_ai_list
|
||||
{
|
||||
llarp_ai * data;
|
||||
llarp_ai_list * next = nullptr;
|
||||
};
|
||||
|
||||
static std::list<llarp_ai> ai_list_to_std(struct llarp_ai_list * l)
|
||||
{
|
||||
std::list<llarp_ai> list;
|
||||
if(l->data)
|
||||
{
|
||||
do
|
||||
{
|
||||
llarp_ai copy;
|
||||
memcpy(©, l->data, sizeof(llarp_ai));
|
||||
list.push_back(copy);
|
||||
l = l->next;
|
||||
}
|
||||
while(l->next);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,70 @@
|
||||
#ifndef LLARP_BENCODE_HPP
|
||||
#define LLARP_BENCODE_HPP
|
||||
#include <llarp/buffer.h>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,21 @@
|
||||
#include "exit_info.hpp"
|
||||
#include "bencode.hpp"
|
||||
|
||||
extern "C" {
|
||||
bool llarp_xi_bencode(struct llarp_xi * xi, llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp::BEncode(*xi, buff);
|
||||
}
|
||||
}
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
bool BEncode(const llarp_xi & xi, llarp_buffer_t * buff)
|
||||
{
|
||||
return bencodeDict(buff) &&
|
||||
bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) &&
|
||||
bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) &&
|
||||
bencodeDict_Int(buff, "v", 0) &&
|
||||
bencodeEnd(buff);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
#ifndef LLARP_XI_HPP
|
||||
#define LLARP_XI_HPP
|
||||
#include <llarp/exit_info.h>
|
||||
#include <list>
|
||||
|
||||
struct llarp_xi_list
|
||||
{
|
||||
std::list<llarp_xi> list;
|
||||
};
|
||||
|
||||
static std::list<llarp_xi> xi_list_to_std(struct llarp_xi_list * l)
|
||||
{
|
||||
std::list<llarp_xi> list;
|
||||
if(l->list.size())
|
||||
for(const auto & xi : l->list)
|
||||
list.push_back(xi);
|
||||
return list;
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,21 @@
|
||||
#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))));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T * alloc(llarp_alloc * mem=nullptr)
|
||||
{
|
||||
if(!mem) mem = &llarp_g_mem;
|
||||
return static_cast<T*>(mem->alloc(sizeof(T), alignment<T>()));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
#include "router_contact.hpp"
|
||||
#include "exit_info.hpp"
|
||||
#include "address_info.hpp"
|
||||
#include "bencode.hpp"
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#ifndef LLARP_RC_HPP
|
||||
#define LLARP_RC_HPP
|
||||
#include <llarp/router_contact.h>
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue