1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

onion routing downstream traffic relaying works on testnet

This commit is contained in:
Jeff Becker 2018-06-22 09:59:28 -04:00
parent 2f8cde8e07
commit f2c42e4e5a
No known key found for this signature in database
GPG key ID: F357B3B42F6F9B05
15 changed files with 291 additions and 156 deletions

View file

@ -124,7 +124,6 @@ set(LIB_SRC
llarp/nodedb.cpp
llarp/path.cpp
llarp/pathbuilder.cpp
llarp/path_confirm.cpp
llarp/proofofwork.cpp
llarp/relay_ack.cpp
llarp/relay_commit.cpp
@ -136,6 +135,8 @@ set(LIB_SRC
llarp/testnet.c
llarp/time.cpp
llarp/timer.cpp
llarp/routing/message_parser.cpp
llarp/routing/path_confirm.cpp
vendor/cppbackport-master/lib/fs/rename.cpp
vendor/cppbackport-master/lib/fs/filestatus.cpp
vendor/cppbackport-master/lib/fs/filetype.cpp

View file

@ -66,7 +66,7 @@ testnet-build: testnet-configure
testnet: testnet-build
mkdir -p $(TESTNET_ROOT)
python3 contrib/testnet/genconf.py --bin=$(REPO)/llarpd --svc=30 --clients=300 --dir=$(TESTNET_ROOT) --out $(TESTNET_CONF)
python3 contrib/testnet/genconf.py --bin=$(REPO)/llarpd --svc=30 --clients=1 --dir=$(TESTNET_ROOT) --out $(TESTNET_CONF)
supervisord -n -d $(TESTNET_ROOT) -l $(TESTNET_LOG) -c $(TESTNET_CONF)
test: debug-configure

View file

@ -48,6 +48,23 @@ namespace llarp
return true;
}
template < typename Int_t >
bool
BEncodeMaybeReadDictInt(const char* k, Int_t& i, bool& read,
llarp_buffer_t key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
{
if(!bencode_read_integer(buf, &i))
{
llarp::Warn("failed to decode key ", k);
return false;
}
read = true;
}
return true;
}
template < typename Item_t >
bool
BEncodeMaybeReadVersion(const char* k, Item_t& item, uint64_t expect,

View file

@ -46,7 +46,7 @@ typedef byte_t llarp_tunnel_nonce_t[TUNNONCESIZE];
/// PKE(result, publickey, secretkey, nonce)
typedef bool (*llarp_path_dh_func)(byte_t *, byte_t *, byte_t *, byte_t *);
/// TKE(result publickey, secretkey, nonce)
/// TKE(result, publickey, secretkey, nonce)
typedef bool (*llarp_transport_dh_func)(byte_t *, byte_t *, byte_t *, byte_t *);
/// SD/SE(buffer, key, nonce)

View file

@ -1,7 +1,7 @@
#ifndef LLARP_MESSAGE_PATH_CONFIRM_HPP
#define LLARP_MESSAGE_PATH_CONFIRM_HPP
#include <llarp/routing_message.hpp>
#include <llarp/routing/message.hpp>
namespace llarp
{
@ -11,18 +11,21 @@ namespace llarp
{
uint64_t pathLifetime;
uint64_t pathCreated;
PathConfirmMessage();
PathConfirmMessage(uint64_t lifetime);
~PathConfirmMessage(){};
bool
BEncode(llarp_buffer_t* buf) const;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val);
bool
BDecode(llarp_buffer_t* buf);
bool
HandleMessage(llarp_router* r) const;
HandleMessage(IMessageHandler* h) const;
};
} // namespace routing
} // namespace llarp

View file

@ -10,7 +10,8 @@
#include <llarp/messages/relay_commit.hpp>
#include <llarp/path_types.hpp>
#include <llarp/router_id.hpp>
#include <llarp/routing_message.hpp>
#include <llarp/routing/handler.hpp>
#include <llarp/routing/message.hpp>
#include <list>
#include <map>
@ -173,7 +174,7 @@ namespace llarp
};
/// A path we made
struct Path : public IHopHandler
struct Path : public IHopHandler, public llarp::routing::IMessageHandler
{
typedef std::vector< PathHopConfig > HopList;
HopList hops;
@ -207,6 +208,9 @@ namespace llarp
RouterID
Upstream() const;
protected:
llarp::routing::InboundMessageParser m_InboundMessageParser;
};
enum PathBuildStatus

View file

@ -0,0 +1,14 @@
#ifndef LLARP_ROUTING_HANDLER_HPP
#define LLARP_ROUTING_HANDLER_HPP
namespace llarp
{
namespace routing
{
struct IMessageHandler
{
};
} // namespace routing
} // namespace llarp
#endif

View file

@ -0,0 +1,46 @@
#ifndef LLARP_ROUTING_MESSAGE_HPP
#define LLARP_ROUTING_MESSAGE_HPP
#include <llarp/buffer.h>
#include <llarp/router.h>
#include <llarp/path_types.hpp>
namespace llarp
{
namespace routing
{
struct IMessageHandler;
struct IMessage
{
llarp::PathID_t from;
virtual ~IMessage(){};
virtual bool
BEncode(llarp_buffer_t* buf) const = 0;
virtual bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) = 0;
virtual bool
HandleMessage(IMessageHandler* r) const = 0;
};
struct InboundMessageParser
{
InboundMessageParser();
bool
ParseMessageBuffer(llarp_buffer_t buf, IMessageHandler* handler);
private:
static bool
OnKey(dict_reader* r, llarp_buffer_t* key);
bool firstKey;
dict_reader reader;
IMessage* msg;
};
} // namespace routing
} // namespace llarp
#endif

View file

@ -1,30 +0,0 @@
#ifndef LLARP_ROUTING_MESSAGE_HPP
#define LLARP_ROUTING_MESSAGE_HPP
#include <llarp/buffer.h>
#include <llarp/router.h>
#include <llarp/path_types.hpp>
namespace llarp
{
namespace routing
{
struct IMessage
{
llarp::PathID_t from;
virtual ~IMessage(){};
virtual bool
BEncode(llarp_buffer_t* buf) const = 0;
virtual bool
BDecode(llarp_buffer_t* buf) = 0;
virtual bool
HandleMessage(llarp_router* r) const = 0;
};
} // namespace routing
} // namespace llarp
#endif

View file

@ -1397,7 +1397,7 @@ namespace iwp
UnmapAddr(addr);
session *s = static_cast< session * >(itr->second.impl);
s->done();
m_sessions.erase(itr);
m_sessions.erase(addr);
if(s->frames)
{
llarp::Warn("session has ", s->frames,

View file

@ -291,7 +291,8 @@ nodedb_async_load_rc(void *user)
llarp_logic_queue_job(job->logic, {job, &nodedb_inform_load_rc});
}
extern "C" {
extern "C"
{
struct llarp_nodedb *
llarp_nodedb_new(struct llarp_crypto *crypto)
{
@ -393,7 +394,7 @@ llarp_nodedb_select_random_hop(struct llarp_nodedb *n, struct llarp_rc *prev,
auto itr = n->entries.begin();
if(sz > 1)
{
auto idx = rand() % (sz - 1);
auto idx = rand() % sz;
std::advance(itr, idx);
}
if(memcmp(prev->pubkey, itr->second.pubkey, PUBKEYSIZE) == 0)
@ -407,7 +408,7 @@ llarp_nodedb_select_random_hop(struct llarp_nodedb *n, struct llarp_rc *prev,
auto itr = n->entries.begin();
if(sz > 1)
{
auto idx = rand() % (sz - 1);
auto idx = rand() % sz;
std::advance(itr, idx);
}
llarp_rc_copy(result, &itr->second);

View file

@ -269,14 +269,9 @@ namespace llarp
Path::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
llarp_router* r)
{
size_t idx = hops.size() - 1;
while(idx >= 0)
for(const auto& hop : hops)
{
r->crypto.xchacha20(buf, hops[idx].shared, Y);
if(idx)
idx--;
else
break;
r->crypto.xchacha20(buf, hop.shared, Y);
}
return HandleRoutingMessage(buf, r);
}
@ -284,7 +279,11 @@ namespace llarp
bool
Path::HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r)
{
// TODO: implement me
if(!m_InboundMessageParser.ParseMessageBuffer(buf, this))
{
llarp::Warn("Failed to parse inbound routing message");
return false;
}
return true;
}

View file

@ -47,10 +47,9 @@ namespace llarp
abort();
return;
}
++ctx->idx;
bool isFarthestHop = ctx->idx == ctx->path->hops.size() - 1;
bool isFarthestHop = ctx->idx == ctx->path->hops.size();
if(isFarthestHop)
{

View file

@ -0,0 +1,69 @@
#include <llarp/messages/path_confirm.hpp>
#include <llarp/routing/message.hpp>
namespace llarp
{
namespace routing
{
InboundMessageParser::InboundMessageParser()
{
reader.user = this;
reader.on_key = &OnKey;
firstKey = false;
}
bool
InboundMessageParser::OnKey(dict_reader* r, llarp_buffer_t* key)
{
InboundMessageParser* self =
static_cast< InboundMessageParser* >(r->user);
if(key == nullptr && self->firstKey)
{
// empty dict
return false;
}
if(!key)
return true;
if(self->firstKey)
{
llarp_buffer_t strbuf;
if(!llarp_buffer_eq(*key, "A"))
return false;
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != 1)
return false;
switch(*strbuf.cur)
{
case 'P':
self->msg = new PathConfirmMessage;
break;
default:
llarp::Error("invalid routing message id: ", *strbuf.cur);
}
self->firstKey = false;
return self->msg != nullptr;
}
else
{
return self->msg->DecodeKey(*key, r->buffer);
}
}
bool
InboundMessageParser::ParseMessageBuffer(llarp_buffer_t buf,
IMessageHandler* h)
{
bool result = false;
msg = nullptr;
firstKey = true;
if(bencode_read_dict(&buf, &reader))
{
result = msg->HandleMessage(h);
delete msg;
}
return result;
}
} // namespace routing
} // namespace llarp

View file

@ -1,16 +1,32 @@
#include <llarp/time.h>
#include <llarp/bencode.hpp>
#include <llarp/messages/path_confirm.hpp>
#include <llarp/routing/handler.hpp>
namespace llarp
{
namespace routing
{
PathConfirmMessage::PathConfirmMessage() : pathLifetime(0), pathCreated(0)
{
}
PathConfirmMessage::PathConfirmMessage(uint64_t lifetime)
: pathLifetime(lifetime), pathCreated(llarp_time_now_ms())
{
}
bool
PathConfirmMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("L", pathLifetime, read, key, val))
return false;
if(!BEncodeMaybeReadDictInt("S", pathCreated, read, key, val))
return false;
return read;
}
bool
PathConfirmMessage::BEncode(llarp_buffer_t* buf) const
{
@ -26,14 +42,10 @@ namespace llarp
}
bool
PathConfirmMessage::BDecode(llarp_buffer_t* buf)
{
return false;
}
bool
PathConfirmMessage::HandleMessage(llarp_router* r) const
PathConfirmMessage::HandleMessage(IMessageHandler* h) const
{
llarp::Info("got path confirm created=", pathCreated,
" lifetime=", pathLifetime);
return true;
}