more dht stuff

pull/1/head
Jeff Becker 6 years ago
parent 9d66b3a1a6
commit d5fe7623fc
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -15,6 +15,8 @@ namespace llarp
template < size_t sz >
struct AlignedBuffer
{
static_assert(sz % 8 == 0, "aligned buffer size is not a multiple of 8");
AlignedBuffer() = default;
AlignedBuffer(const byte_t* data)

@ -5,7 +5,7 @@
namespace llarp
{
bool
inline bool
BEncodeWriteDictMsgType(llarp_buffer_t* buf, const char* k, const char* t)
{
return bencode_write_bytestring(buf, k, 1)
@ -19,6 +19,13 @@ namespace llarp
return bencode_write_bytestring(buf, k, 1) && o.BEncode(buf);
}
template < typename Int_t >
bool
BEncodeWriteDictInt(llarp_buffer_t* buf, const char* k, const Int_t& i)
{
return bencode_write_bytestring(buf, k, 1) && bencode_write_uint64(buf, i);
}
template < typename Item_t >
bool
BEncodeRead(Item_t& item, llarp_buffer_t* buf);

@ -55,6 +55,10 @@ struct llarp_router_lookup_job
struct llarp_rc result;
};
/// start allowing dht participation on a context
void
llarp_dht_allow_transit(struct llarp_dht_context* ctx);
// shallow copy
void
llarp_dht_put_local_router(struct llarp_dht_context* ctx, struct llarp_rc* rc);

@ -181,6 +181,7 @@ namespace llarp
llarp_router* router = nullptr;
Bucket* nodes = nullptr;
bool allowTransit = false;
private:
void

@ -9,9 +9,15 @@ extern "C" {
// forward declare
struct llarp_alloc;
struct llarp_rc;
#define MAX_RC_SIZE (1024)
bool
llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buf);
bool
llarp_rc_bencode(const struct llarp_rc *rc, llarp_buffer_t *buf);
struct llarp_rc
{
struct llarp_ai_list *addrs;
@ -22,12 +28,21 @@ struct llarp_rc
struct llarp_xi_list *exits;
byte_t signature[SIGSIZE];
uint64_t last_updated;
};
bool
llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buf);
bool
llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buf);
#ifdef __cplusplus
bool
BEncode(llarp_buffer_t *buf) const
{
return llarp_rc_bencode(this, buf);
}
bool
BDecode(llarp_buffer_t *buf)
{
return llarp_rc_bdecode(this, buf);
}
#endif
};
void
llarp_rc_free(struct llarp_rc *rc);

@ -1,12 +1,13 @@
#include <llarp/bencode.h>
#include <llarp/bencode.hpp>
#include <llarp/dht.hpp>
#include <llarp/messages/dht_immediate.hpp>
#include "router.hpp"
#include "router_contact.hpp"
#include <sodium.h>
#include <algorithm> // std::find
#include <set>
#include <algorithm> // std::find
namespace llarp
{
@ -90,7 +91,25 @@ namespace llarp
bool
GotRouterMessage::BEncode(llarp_buffer_t *buf) const
{
return false;
if(!bencode_start_dict(buf))
return false;
// message type
if(!BEncodeWriteDictMsgType(buf, "A", "S"))
return false;
if(!BEncodeWriteDictList("R", R, buf))
return false;
// txid
if(!BEncodeWriteDictInt(buf, "T", txid))
return false;
// version
if(!BEncodeWriteDictInt(buf, "V", version))
return false;
return bencode_end(buf);
}
bool
@ -186,7 +205,13 @@ namespace llarp
FindRouterMessage::HandleMessage(llarp_router *router,
std::vector< IMessage * > &replies) const
{
auto &dht = router->dht->impl;
auto &dht = router->dht->impl;
if(!dht.allowTransit)
{
llarp::Warn("Got DHT lookup from ", From,
" when we are not allowing dht transit");
return false;
}
auto pending = dht.FindPendingTX(From, txid);
if(pending)
{
@ -417,8 +442,8 @@ namespace llarp
{
TXOwner search;
search.requester = owner;
search.txid = id;
auto itr = pendingTX.find(search);
search.txid = id;
auto itr = pendingTX.find(search);
if(itr == pendingTX.end())
return;
pendingTX.erase(itr);
@ -429,8 +454,8 @@ namespace llarp
{
TXOwner search;
search.requester = owner;
search.txid = id;
auto itr = pendingTX.find(search);
search.txid = id;
auto itr = pendingTX.find(search);
if(itr == pendingTX.end())
return nullptr;
else
@ -452,7 +477,7 @@ namespace llarp
{
pendingTX[e].Completed(nullptr, true);
RemovePendingLookup(e.requester, e.txid);
if(e.requester != ourKey)
if(e.requester != ourKey && allowTransit)
{
// inform not found
llarp::DHTImmeidateMessage msg(e.requester);
@ -486,11 +511,11 @@ namespace llarp
Context::LookupRouter(const Key_t &target, const Key_t &whoasked,
const Key_t &askpeer, llarp_router_lookup_job *job)
{
auto id = ++ids;
auto id = ++ids;
TXOwner ownerKey;
ownerKey.requester = whoasked;
ownerKey.txid = id;
ownerKey.txid = id;
pendingTX[ownerKey] = SearchJob(whoasked, target, job);
@ -569,6 +594,12 @@ llarp_dht_set_msg_handler(struct llarp_dht_context *ctx,
ctx->impl.custom_handler = handler;
}
void
llarp_dht_allow_transit(llarp_dht_context *ctx)
{
ctx->impl.allowTransit = true;
}
void
llarp_dht_context_start(struct llarp_dht_context *ctx, const byte_t *key)
{

@ -29,7 +29,10 @@ namespace llarp
} // namespace llarp
llarp_router::llarp_router()
: ready(false), paths(this), inbound_msg_parser(this)
: ready(false)
, paths(this)
, dht(llarp_dht_context_new(this))
, inbound_msg_parser(this)
{
llarp_rc_clear(&rc);
@ -350,9 +353,9 @@ llarp_router::send_padded_message(llarp_link_session_iter *itr,
llarp_router *self = static_cast< llarp_router * >(itr->user);
llarp::RouterID remote;
remote = &peer->get_remote_router(peer)->pubkey[0];
for(size_t idx = 0; idx < 50; ++idx)
for(size_t idx = 0; idx < 5; ++idx)
{
llarp::DiscardMessage msg(9000);
llarp::DiscardMessage msg(2000);
self->SendTo(remote, &msg);
}
return true;
@ -401,7 +404,9 @@ llarp_router::SessionClosed(const llarp::RouterID &remote)
auto itr = validRouters.find(remote);
if(itr == validRouters.end())
return;
llarp_dht_remove_local_router(dht, remote);
if(dht)
llarp_dht_remove_local_router(dht, remote);
llarp_rc_free(&itr->second);
validRouters.erase(itr);
}
@ -461,7 +466,7 @@ llarp_router::on_try_connect_result(llarp_link_establish_job *job)
{
llarp::PubKey pk = job->pubkey;
llarp::Warn("failed to connect to ", pk, " dropping all pending messages");
router->DiscardOutboundFor(job->pubkey);
router->DiscardOutboundFor(pk);
}
}
@ -553,6 +558,10 @@ llarp_router::Run()
InitServiceNode();
}
llarp::PubKey ourPubkey = pubkey();
llarp::Info("starting dht context as ", ourPubkey);
llarp_dht_context_start(dht, ourPubkey);
llarp_logic_call_later(logic, {1000, this, &ConnectAll});
ScheduleTicker(500);
@ -561,13 +570,9 @@ llarp_router::Run()
void
llarp_router::InitServiceNode()
{
llarp::PubKey ourPubkey = pubkey();
llarp::Info("starting dht context as ", ourPubkey);
dht = llarp_dht_context_new(this);
llarp_dht_context_start(dht, ourPubkey);
llarp::Info("accepting transit traffic");
paths.AllowTransit();
llarp_dht_allow_transit(dht);
}
void

@ -164,7 +164,7 @@ llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
}
bool
llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff)
llarp_rc_bencode(const struct llarp_rc *rc, llarp_buffer_t *buff)
{
/* write dict begin */
if(!bencode_start_dict(buff))

Loading…
Cancel
Save