2018-12-15 16:21:52 +00:00
|
|
|
#include <messages/relay.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2019-06-17 23:19:39 +00:00
|
|
|
#include <path/path_context.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <util/bencode.hpp>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
void
|
|
|
|
RelayUpstreamMessage::Clear()
|
|
|
|
{
|
|
|
|
pathid.Zero();
|
|
|
|
X.Clear();
|
|
|
|
Y.Zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RelayUpstreamMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "a", "u"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictEntry("p", pathid, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("x", X, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("y", Y, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-02 23:12:42 +00:00
|
|
|
RelayUpstreamMessage::DecodeKey(const llarp_buffer_t &key,
|
|
|
|
llarp_buffer_t *buf)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key,
|
|
|
|
buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("x", X, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("y", Y, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-11 19:45:42 +00:00
|
|
|
RelayUpstreamMessage::HandleMessage(AbstractRouter *r) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
auto path = r->pathContext().GetByDownstream(session->GetPubKey(), pathid);
|
2019-01-14 21:46:07 +00:00
|
|
|
if(path)
|
|
|
|
{
|
2019-02-03 00:31:10 +00:00
|
|
|
return path->HandleUpstream(llarp_buffer_t(X), Y, r);
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RelayDownstreamMessage::Clear()
|
|
|
|
{
|
|
|
|
pathid.Zero();
|
|
|
|
X.Clear();
|
|
|
|
Y.Zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "a", "d"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictEntry("p", pathid, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("x", X, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("y", Y, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-02 23:12:42 +00:00
|
|
|
RelayDownstreamMessage::DecodeKey(const llarp_buffer_t &key,
|
|
|
|
llarp_buffer_t *buf)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key,
|
|
|
|
buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("x", X, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("y", Y, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-11 19:45:42 +00:00
|
|
|
RelayDownstreamMessage::HandleMessage(AbstractRouter *r) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
auto path = r->pathContext().GetByUpstream(session->GetPubKey(), pathid);
|
2019-01-14 21:46:07 +00:00
|
|
|
if(path)
|
|
|
|
{
|
2019-02-03 00:31:10 +00:00
|
|
|
return path->HandleDownstream(llarp_buffer_t(X), Y, r);
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
llarp::LogWarn("unhandled downstream message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // namespace llarp
|