mirror of https://github.com/oxen-io/lokinet
start fleshing out dht subsystem a bit
parent
fe35b98abc
commit
5649d261ae
@ -0,0 +1,126 @@
|
|||||||
|
#ifndef LLARP_ALIGNED_HPP
|
||||||
|
#define LLARP_ALIGNED_HPP
|
||||||
|
|
||||||
|
#include <llarp/crypto.h>
|
||||||
|
#include <sodium.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
/// aligned buffer, sz must be multiple of 8 bytes
|
||||||
|
template < size_t sz >
|
||||||
|
struct AlignedBuffer
|
||||||
|
{
|
||||||
|
AlignedBuffer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
AlignedBuffer(const byte_t* data)
|
||||||
|
{
|
||||||
|
memcpy(buf.b, data, sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
AlignedBuffer(const AlignedBuffer& other)
|
||||||
|
{
|
||||||
|
memcpy(buf.b, other.data(), sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
AlignedBuffer&
|
||||||
|
operator=(const AlignedBuffer& other)
|
||||||
|
{
|
||||||
|
memcpy(buf.b, other.data(), sz);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte_t& operator[](size_t idx)
|
||||||
|
{
|
||||||
|
return buf.b[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream&
|
||||||
|
operator<<(std::ostream& out) const
|
||||||
|
{
|
||||||
|
char buf[(1 + sz) * 2] = {0};
|
||||||
|
size_t idx = 0;
|
||||||
|
char* ptr = buf;
|
||||||
|
char* end = ptr + (sz * 2);
|
||||||
|
while(idx < sz)
|
||||||
|
{
|
||||||
|
auto wrote = snprintf(ptr, end - ptr, "%.2x", buf.b[idx]);
|
||||||
|
if(wrote == -1)
|
||||||
|
break;
|
||||||
|
++idx;
|
||||||
|
ptr += wrote;
|
||||||
|
}
|
||||||
|
return out << std::string(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator==(const AlignedBuffer& other) const
|
||||||
|
{
|
||||||
|
return memcmp(data(), other.data(), sz) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator!=(const AlignedBuffer& other) const
|
||||||
|
{
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
size() const
|
||||||
|
{
|
||||||
|
return sz;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Zero()
|
||||||
|
{
|
||||||
|
for(size_t idx = 0; sz < idx / 8; ++idx)
|
||||||
|
buf.l[idx] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Randomize()
|
||||||
|
{
|
||||||
|
randombytes(buf.b, sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte_t*
|
||||||
|
data()
|
||||||
|
{
|
||||||
|
return buf.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
const byte_t*
|
||||||
|
data() const
|
||||||
|
{
|
||||||
|
return buf.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint64_t*
|
||||||
|
data_l() const
|
||||||
|
{
|
||||||
|
return buf.l;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t*
|
||||||
|
data_l()
|
||||||
|
{
|
||||||
|
return buf.l;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator const byte_t*() const
|
||||||
|
{
|
||||||
|
return buf.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
union {
|
||||||
|
byte_t b[sz];
|
||||||
|
uint64_t l[sz / 8];
|
||||||
|
} buf = {0};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,179 @@
|
|||||||
|
#ifndef LLARP_DHT_HPP_
|
||||||
|
#define LLARP_DHT_HPP_
|
||||||
|
#include <llarp/buffer.h>
|
||||||
|
#include <llarp/dht.h>
|
||||||
|
#include <llarp/router.h>
|
||||||
|
#include <llarp/router_contact.h>
|
||||||
|
#include <llarp/aligned.hpp>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace dht
|
||||||
|
{
|
||||||
|
const size_t MAX_MSG_SIZE = 2048;
|
||||||
|
|
||||||
|
struct SearchJob;
|
||||||
|
|
||||||
|
struct Node
|
||||||
|
{
|
||||||
|
llarp_rc rc;
|
||||||
|
|
||||||
|
const byte_t*
|
||||||
|
ID() const;
|
||||||
|
|
||||||
|
Node();
|
||||||
|
~Node();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Key_t : public llarp::AlignedBuffer< 32 >
|
||||||
|
{
|
||||||
|
Key_t(const byte_t* val) : llarp::AlignedBuffer< 32 >(val)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Key_t() : llarp::AlignedBuffer< 32 >()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Key_t
|
||||||
|
operator^(const Key_t& other) const
|
||||||
|
{
|
||||||
|
Key_t dist;
|
||||||
|
for(size_t idx = 0; idx < 8; ++idx)
|
||||||
|
dist.data_l()[idx] = data_l()[idx] ^ other.data_l()[idx];
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator<(const Key_t& other) const
|
||||||
|
{
|
||||||
|
return memcmp(data_l(), other.data_l(), 32) < 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct XorMetric
|
||||||
|
{
|
||||||
|
const Key_t& us;
|
||||||
|
|
||||||
|
XorMetric(const Key_t& ourKey) : us(ourKey){};
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator()(const Key_t& left, const Key_t& right) const
|
||||||
|
{
|
||||||
|
return (us ^ left) < right;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IMessage
|
||||||
|
{
|
||||||
|
virtual ~IMessage(){};
|
||||||
|
|
||||||
|
IMessage(const Key_t& from) : From(from)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool
|
||||||
|
BEncode(llarp_buffer_t* buf) const = 0;
|
||||||
|
|
||||||
|
virtual bool
|
||||||
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) = 0;
|
||||||
|
|
||||||
|
virtual bool
|
||||||
|
HandleMessage(llarp_router* router,
|
||||||
|
std::vector< IMessage* >& replies) const = 0;
|
||||||
|
|
||||||
|
Key_t From;
|
||||||
|
};
|
||||||
|
|
||||||
|
IMessage*
|
||||||
|
DecodeMessage(const Key_t& from, llarp_buffer_t* buf);
|
||||||
|
|
||||||
|
bool
|
||||||
|
DecodeMesssageList(const Key_t& from, llarp_buffer_t* buf,
|
||||||
|
std::vector< IMessage* >& dst);
|
||||||
|
|
||||||
|
struct Bucket
|
||||||
|
{
|
||||||
|
typedef std::map< Key_t, Node, XorMetric > BucketStorage_t;
|
||||||
|
|
||||||
|
Bucket(const Key_t& us) : nodes(XorMetric(us)){};
|
||||||
|
|
||||||
|
bool
|
||||||
|
FindClosest(const Key_t& target, Key_t& result) const;
|
||||||
|
|
||||||
|
BucketStorage_t nodes;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Context
|
||||||
|
{
|
||||||
|
Context();
|
||||||
|
~Context();
|
||||||
|
|
||||||
|
llarp_dht_msg_handler custom_handler = nullptr;
|
||||||
|
|
||||||
|
void
|
||||||
|
Init(const Key_t& us);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Bucket* nodes = nullptr;
|
||||||
|
Key_t ourKey;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GotRouterMessage : public IMessage
|
||||||
|
{
|
||||||
|
GotRouterMessage(const Key_t& from) : IMessage(from)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~GotRouterMessage();
|
||||||
|
|
||||||
|
bool
|
||||||
|
BEncode(llarp_buffer_t* buf) const;
|
||||||
|
|
||||||
|
bool
|
||||||
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val);
|
||||||
|
|
||||||
|
bool
|
||||||
|
HandleMessage(llarp_router* router,
|
||||||
|
std::vector< IMessage* >& replies) const;
|
||||||
|
|
||||||
|
std::vector< llarp_rc > R;
|
||||||
|
uint64_t txid = 0;
|
||||||
|
uint64_t version = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FindRouterMessage : public IMessage
|
||||||
|
{
|
||||||
|
FindRouterMessage(const Key_t& from) : IMessage(from)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~FindRouterMessage();
|
||||||
|
|
||||||
|
bool
|
||||||
|
BEncode(llarp_buffer_t* buf) const;
|
||||||
|
|
||||||
|
bool
|
||||||
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val);
|
||||||
|
|
||||||
|
bool
|
||||||
|
HandleMessage(llarp_router* router,
|
||||||
|
std::vector< IMessage* >& replies) const;
|
||||||
|
|
||||||
|
Key_t K;
|
||||||
|
uint64_t txid = 0;
|
||||||
|
uint64_t version = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct llarp_dht_context
|
||||||
|
{
|
||||||
|
llarp::dht::Context impl;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef LLARP_MESSAGES_DHT_IMMEDIATE_HPP
|
||||||
|
#define LLARP_MESSAGES_DHT_IMMEDIATE_HPP
|
||||||
|
#include <llarp/dht.hpp>
|
||||||
|
#include <llarp/link_message.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
struct DHTImmeidateMessage : public ILinkMessage
|
||||||
|
{
|
||||||
|
DHTImmeidateMessage(const RouterID& from) : ILinkMessage(from)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~DHTImmeidateMessage();
|
||||||
|
|
||||||
|
std::vector< llarp::dht::IMessage* > msgs;
|
||||||
|
|
||||||
|
bool
|
||||||
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf);
|
||||||
|
|
||||||
|
bool
|
||||||
|
BEncode(llarp_buffer_t* buf) const;
|
||||||
|
|
||||||
|
bool
|
||||||
|
HandleMessage(llarp_router* router) const;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef LLARP_MESSAGES_LINK_INTRO_HPP
|
||||||
|
#define LLARP_MESSAGES_LINK_INTRO_HPP
|
||||||
|
#include <llarp/link_message.hpp>
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
struct LinkIntroMessage : public ILinkMessage
|
||||||
|
{
|
||||||
|
LinkIntroMessage(llarp_rc* rc) : ILinkMessage({}), RC(rc)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~LinkIntroMessage();
|
||||||
|
|
||||||
|
llarp_rc* RC;
|
||||||
|
|
||||||
|
bool
|
||||||
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf);
|
||||||
|
|
||||||
|
bool
|
||||||
|
BEncode(llarp_buffer_t* buf) const;
|
||||||
|
|
||||||
|
bool
|
||||||
|
HandleMessage(llarp_router* router) const;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,303 @@
|
|||||||
|
#include <llarp/bencode.h>
|
||||||
|
#include <llarp/dht.hpp>
|
||||||
|
#include <llarp/messages/dht_immediate.hpp>
|
||||||
|
#include "router.hpp"
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
DHTImmeidateMessage::~DHTImmeidateMessage()
|
||||||
|
{
|
||||||
|
for(auto &msg : msgs)
|
||||||
|
delete msg;
|
||||||
|
msgs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
||||||
|
{
|
||||||
|
if(llarp_buffer_eq(key, "m"))
|
||||||
|
return llarp::dht::DecodeMesssageList(remote.data(), buf, msgs);
|
||||||
|
if(llarp_buffer_eq(key, "v"))
|
||||||
|
{
|
||||||
|
if(!bdecode_read_integer(buf, &version))
|
||||||
|
return false;
|
||||||
|
return version == LLARP_PROTO_VERSION;
|
||||||
|
}
|
||||||
|
// bad key
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
DHTImmeidateMessage::BEncode(llarp_buffer_t *buf) const
|
||||||
|
{
|
||||||
|
if(!bencode_start_dict(buf))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(!bencode_write_bytestring(buf, "a", 1))
|
||||||
|
return false;
|
||||||
|
if(!bencode_write_bytestring(buf, "m", 1))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// dht messages
|
||||||
|
if(!bencode_write_bytestring(buf, "m", 1))
|
||||||
|
return false;
|
||||||
|
// begin list
|
||||||
|
if(!bencode_start_list(buf))
|
||||||
|
return false;
|
||||||
|
for(const auto &msg : msgs)
|
||||||
|
{
|
||||||
|
if(!msg->BEncode(buf))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// end list
|
||||||
|
if(!bencode_end(buf))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(!bencode_write_version_entry(buf))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return bencode_end(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
DHTImmeidateMessage::HandleMessage(llarp_router *router) const
|
||||||
|
{
|
||||||
|
DHTImmeidateMessage *reply = new DHTImmeidateMessage(remote);
|
||||||
|
bool result = true;
|
||||||
|
for(auto &msg : msgs)
|
||||||
|
{
|
||||||
|
result &= msg->HandleMessage(router, reply->msgs);
|
||||||
|
}
|
||||||
|
return result && router->SendToOrQueue(remote.data(), {reply});
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace dht
|
||||||
|
{
|
||||||
|
GotRouterMessage::~GotRouterMessage()
|
||||||
|
{
|
||||||
|
for(auto &rc : R)
|
||||||
|
llarp_rc_free(&rc);
|
||||||
|
R.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
GotRouterMessage::BEncode(llarp_buffer_t *buf) const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
GotRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
GotRouterMessage::HandleMessage(llarp_router *router,
|
||||||
|
std::vector< IMessage * > &replies) const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
FindRouterMessage::~FindRouterMessage()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
FindRouterMessage::BEncode(llarp_buffer_t *buf) const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
FindRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
|
||||||
|
{
|
||||||
|
llarp_buffer_t strbuf;
|
||||||
|
if(llarp_buffer_eq(key, "K"))
|
||||||
|
{
|
||||||
|
if(!bdecode_read_string(val, &strbuf))
|
||||||
|
return false;
|
||||||
|
if(strbuf.sz != K.size())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
memcpy(K.data(), strbuf.base, K.size());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(llarp_buffer_eq(key, "T"))
|
||||||
|
{
|
||||||
|
return bdecode_read_integer(val, &txid);
|
||||||
|
}
|
||||||
|
if(llarp_buffer_eq(key, "V"))
|
||||||
|
{
|
||||||
|
if(!bdecode_read_integer(val, &version))
|
||||||
|
return false;
|
||||||
|
return version == LLARP_PROTO_VERSION;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
FindRouterMessage::HandleMessage(llarp_router *router,
|
||||||
|
std::vector< IMessage * > &replies) const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MessageDecoder
|
||||||
|
{
|
||||||
|
Key_t From;
|
||||||
|
bool firstKey = true;
|
||||||
|
IMessage *msg = nullptr;
|
||||||
|
|
||||||
|
MessageDecoder(const Key_t &from) : From(from)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
on_key(dict_reader *r, llarp_buffer_t *key)
|
||||||
|
{
|
||||||
|
llarp_buffer_t strbuf;
|
||||||
|
MessageDecoder *dec = static_cast< MessageDecoder * >(r->user);
|
||||||
|
// check for empty dict
|
||||||
|
if(!key)
|
||||||
|
return !dec->firstKey;
|
||||||
|
|
||||||
|
// first key
|
||||||
|
if(dec->firstKey)
|
||||||
|
{
|
||||||
|
if(!llarp_buffer_eq(*key, "A"))
|
||||||
|
return false;
|
||||||
|
if(!bdecode_read_string(r->buffer, &strbuf))
|
||||||
|
return false;
|
||||||
|
// bad msg size?
|
||||||
|
if(strbuf.sz != 1)
|
||||||
|
return false;
|
||||||
|
switch(*strbuf.base)
|
||||||
|
{
|
||||||
|
case 'R':
|
||||||
|
dec->msg = new FindRouterMessage(dec->From);
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
dec->msg = new GotRouterMessage(dec->From);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// bad msg type
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
dec->firstKey = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return dec->msg->DecodeKey(*key, r->buffer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
IMessage *
|
||||||
|
DecodeMesssage(const Key_t &from, llarp_buffer_t *buf)
|
||||||
|
{
|
||||||
|
MessageDecoder dec(from);
|
||||||
|
dict_reader r;
|
||||||
|
r.user = &dec;
|
||||||
|
r.on_key = &MessageDecoder::on_key;
|
||||||
|
if(bdecode_read_dict(buf, &r))
|
||||||
|
return dec.msg;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(dec.msg)
|
||||||
|
delete dec.msg;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ListDecoder
|
||||||
|
{
|
||||||
|
ListDecoder(const Key_t &from, std::vector< IMessage * > &list)
|
||||||
|
: From(from), l(list){};
|
||||||
|
Key_t From;
|
||||||
|
std::vector< IMessage * > &l;
|
||||||
|
|
||||||
|
static bool
|
||||||
|
on_item(list_reader *r, bool has)
|
||||||
|
{
|
||||||
|
ListDecoder *dec = static_cast< ListDecoder * >(r->user);
|
||||||
|
if(!has)
|
||||||
|
return true;
|
||||||
|
auto msg = DecodeMesssage(dec->From, r->buffer);
|
||||||
|
if(msg)
|
||||||
|
{
|
||||||
|
dec->l.push_back(msg);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool
|
||||||
|
DecodeMesssageList(const Key_t &from, llarp_buffer_t *buf,
|
||||||
|
std::vector< IMessage * > &list)
|
||||||
|
{
|
||||||
|
ListDecoder dec(from, list);
|
||||||
|
|
||||||
|
list_reader r;
|
||||||
|
r.user = &dec;
|
||||||
|
r.on_item = &ListDecoder::on_item;
|
||||||
|
return bdecode_read_list(buf, &r);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node::Node()
|
||||||
|
{
|
||||||
|
llarp_rc_clear(&rc);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node::~Node()
|
||||||
|
{
|
||||||
|
llarp_rc_free(&rc);
|
||||||
|
}
|
||||||
|
|
||||||
|
Context::Context()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Context::~Context()
|
||||||
|
{
|
||||||
|
if(nodes)
|
||||||
|
delete nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Context::Init(const Key_t &us)
|
||||||
|
{
|
||||||
|
ourKey = us;
|
||||||
|
nodes = new Bucket(ourKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
struct llarp_dht_context *
|
||||||
|
llarp_dht_context_new()
|
||||||
|
{
|
||||||
|
return new llarp_dht_context;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
llarp_dht_context_free(struct llarp_dht_context *ctx)
|
||||||
|
{
|
||||||
|
delete ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
llarp_dht_set_msg_handler(struct llarp_dht_context *ctx,
|
||||||
|
llarp_dht_msg_handler handler)
|
||||||
|
{
|
||||||
|
ctx->impl.custom_handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
llarp_dht_context_set_our_key(struct llarp_dht_context *ctx, const byte_t *key)
|
||||||
|
{
|
||||||
|
ctx->impl.Init(key);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
#include <llarp/bencode.h>
|
||||||
|
#include <llarp/router_contact.h>
|
||||||
|
#include <llarp/messages/link_intro.hpp>
|
||||||
|
#include "logger.hpp"
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
LinkIntroMessage::~LinkIntroMessage()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LinkIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
|
||||||
|
{
|
||||||
|
if(llarp_buffer_eq(key, "r"))
|
||||||
|
{
|
||||||
|
if(!llarp_rc_bdecode(RC, buf))
|
||||||
|
{
|
||||||
|
llarp::Warn(__FILE__, "failed to decode RC");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
remote = RC->pubkey;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(llarp_buffer_eq(key, "v"))
|
||||||
|
{
|
||||||
|
if(!bdecode_read_integer(buf, &version))
|
||||||
|
return false;
|
||||||
|
if(version != LLARP_PROTO_VERSION)
|
||||||
|
{
|
||||||
|
llarp::Warn(__FILE__, "llarp protocol version missmatch ", version,
|
||||||
|
" != ", LLARP_PROTO_VERSION);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
llarp::Warn(__FILE__, "invalid LIM key: ", *key.cur);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LinkIntroMessage::BEncode(llarp_buffer_t* buf) const
|
||||||
|
{
|
||||||
|
if(!bencode_start_dict(buf))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(!bencode_write_bytestring(buf, "a", 1))
|
||||||
|
return false;
|
||||||
|
if(!bencode_write_bytestring(buf, "i", 1))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(RC)
|
||||||
|
{
|
||||||
|
if(!bencode_write_bytestring(buf, "r", 1))
|
||||||
|
return false;
|
||||||
|
if(!llarp_rc_bencode(RC, buf))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!bencode_write_version_entry(buf))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return bencode_end(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LinkIntroMessage::HandleMessage(llarp_router* router) const
|
||||||
|
{
|
||||||
|
llarp::Info(__FILE__, "got LIM from ", remote);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
#include <llarp/messages/relay_commit.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
LR_CommitMessage::~LR_CommitMessage()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LR_CommitMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
|
||||||
|
{
|
||||||
|
// TODO: implement
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LR_CommitMessage::BEncode(llarp_buffer_t* buf) const
|
||||||
|
{
|
||||||
|
// TODO: implement
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
LR_CommitMessage::HandleMessage(llarp_router* router) const
|
||||||
|
{
|
||||||
|
// TODO: implement
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue