mirror of https://github.com/oxen-io/lokinet
more
parent
a449dffc09
commit
dcc5e6e348
@ -0,0 +1,61 @@
|
||||
#ifndef LLARP_BENCODE_H
|
||||
#define LLARP_BENCODE_H
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/version.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool inline bencode_write_bytestring(llarp_buffer_t * buff, const void * data, size_t sz)
|
||||
{
|
||||
if(!llarp_buffer_writef(buff, "%ld:", sz)) return false;
|
||||
return llarp_buffer_write(buff, data, sz);
|
||||
}
|
||||
|
||||
bool inline bencode_write_int(llarp_buffer_t * buff, int i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%de", i);
|
||||
}
|
||||
|
||||
bool inline bencode_write_uint16(llarp_buffer_t * buff, uint16_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%de", i);
|
||||
}
|
||||
|
||||
bool inline bencode_write_int64(llarp_buffer_t * buff, int64_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%lde", i);
|
||||
}
|
||||
|
||||
bool inline bencode_write_sizeint(llarp_buffer_t * buff, size_t i)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "i%lde", i);
|
||||
}
|
||||
|
||||
bool inline bencode_start_list(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_write(buff, "l", 1);
|
||||
}
|
||||
|
||||
bool inline bencode_start_dict(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_write(buff, "d", 1);
|
||||
}
|
||||
|
||||
bool inline bencode_end(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_write(buff, "e", 1);
|
||||
}
|
||||
|
||||
bool inline bencode_write_version_entry(llarp_buffer_t * buff)
|
||||
{
|
||||
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
@ -1,25 +1,18 @@
|
||||
#include <llarp/buffer.h>
|
||||
#include <cstring>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
size_t llarp_buffer_size_left(llarp_buffer_t *buff) {
|
||||
auto diff = buff->cur - buff->base;
|
||||
if (diff < 0) {
|
||||
return 0;
|
||||
} else if (diff > buff->sz)
|
||||
return 0;
|
||||
else
|
||||
return buff->sz - diff;
|
||||
}
|
||||
|
||||
bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz) {
|
||||
size_t left = llarp_buffer_size_left(buff);
|
||||
if (sz > left) {
|
||||
return false;
|
||||
bool llarp_buffer_writef(llarp_buffer_t * buff, const char * fmt, ...)
|
||||
{
|
||||
int written;
|
||||
size_t sz = llarp_buffer_size_left(buff);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
written = snprintf(buff->cur, sz, fmt, args);
|
||||
va_end(args);
|
||||
if (written == -1) return false;
|
||||
buff->sz += written;
|
||||
return true;
|
||||
}
|
||||
std::memcpy(buff->cur, data, sz);
|
||||
buff->cur += sz;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,74 @@
|
||||
#include "router_contact.hpp"
|
||||
#include "address_info.hpp"
|
||||
#include "bencode.hpp"
|
||||
#include "exit_info.hpp"
|
||||
#include <llarp/bencode.h>
|
||||
#include <llarp/version.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static bool bencode_rc_ai_write(struct llarp_ai_list_iter * i, struct llarp_ai * ai)
|
||||
{
|
||||
llarp_buffer_t * buff = static_cast<llarp_buffer_t *>(i->user);
|
||||
return llarp_ai_bencode(ai, buff);
|
||||
}
|
||||
|
||||
static bool bencode_rc_xi_write(struct llarp_xi_list_iter * i, struct llarp_xi * xi)
|
||||
{
|
||||
llarp_buffer_t * buff = static_cast<llarp_buffer_t *>(i->user);
|
||||
return llarp_xi_bencode(xi, buff);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
|
||||
|
||||
extern "C" {
|
||||
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
|
||||
return llarp::BEncode(*rc, buff);
|
||||
/* write dict begin */
|
||||
if(!bencode_start_dict(buff))
|
||||
return false;
|
||||
if(rc->addrs)
|
||||
{
|
||||
/* write ai if they exist */
|
||||
if(!bencode_write_bytestring(buff, "a", 1))
|
||||
return false;
|
||||
if(!bencode_start_list(buff))
|
||||
return false;
|
||||
struct llarp_ai_list_iter ai_itr = {
|
||||
.user = buff,
|
||||
.list = nullptr,
|
||||
.visit = &llarp::bencode_rc_ai_write
|
||||
};
|
||||
llarp_ai_list_iterate(rc->addrs, &ai_itr);
|
||||
if(!bencode_end(buff))
|
||||
return false;
|
||||
}
|
||||
/* write pubkey */
|
||||
if(!bencode_write_bytestring(buff, "k", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t))) return false;
|
||||
|
||||
/* write version */
|
||||
if(!bencode_write_version_entry(buff)) return false;
|
||||
|
||||
if(rc->exits)
|
||||
{
|
||||
/* write ai if they exist */
|
||||
if(!bencode_write_bytestring(buff, "x", 1))
|
||||
return false;
|
||||
if(!bencode_start_list(buff))
|
||||
return false;
|
||||
struct llarp_xi_list_iter xi_itr = {
|
||||
.user = buff,
|
||||
.list = nullptr,
|
||||
.visit = &llarp::bencode_rc_xi_write
|
||||
};
|
||||
llarp_xi_list_iterate(rc->exits, &xi_itr);
|
||||
if(!bencode_end(buff))
|
||||
return false;
|
||||
}
|
||||
|
||||
/* write signature */
|
||||
if(!bencode_write_bytestring(buff, "z", 1)) return false;
|
||||
if(!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t))) return false;
|
||||
return bencode_end(buff);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue