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();
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
RelayUpstreamMessage::BEncode(llarp_buffer_t* buf) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "a", "u"))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("p", pathid, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("x", X, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("y", Y, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
RelayUpstreamMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("v", version, LLARP_PROTO_VERSION, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("x", X, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("y", Y, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +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);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (path)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
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();
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
RelayDownstreamMessage::BEncode(llarp_buffer_t* buf) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "a", "d"))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("p", pathid, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("x", X, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("y", Y, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
RelayDownstreamMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("v", version, LLARP_PROTO_VERSION, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("x", X, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("y", Y, read, key, buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +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);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (path)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
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
|
|
|
}
|
2019-11-03 20:52:00 +00:00
|
|
|
llarp::LogWarn("unhandled downstream message id=", pathid);
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // namespace llarp
|