2018-12-12 02:52:51 +00:00
|
|
|
#include <bencode.hpp>
|
|
|
|
#include <messages/relay.hpp>
|
|
|
|
#include <router.hpp>
|
2018-06-22 00:25:30 +00:00
|
|
|
|
2018-06-08 13:12:17 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-12-20 16:49:05 +00:00
|
|
|
RelayUpstreamMessage::RelayUpstreamMessage() : ILinkMessage()
|
2018-06-08 13:12:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
RelayUpstreamMessage::~RelayUpstreamMessage()
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
void
|
|
|
|
RelayUpstreamMessage::Clear()
|
2018-06-08 13:12:17 +00:00
|
|
|
{
|
2019-01-06 15:20:17 +00:00
|
|
|
pathid.Zero();
|
2018-12-20 16:49:05 +00:00
|
|
|
X.Clear();
|
2019-01-06 15:20:17 +00:00
|
|
|
Y.Zero();
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RelayUpstreamMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
2018-06-26 16:23:43 +00:00
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "a", "u"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictEntry("p", pathid, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
2018-06-26 16:23:43 +00:00
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("x", X, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("y", Y, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RelayUpstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
|
|
|
{
|
2018-06-26 16:23:43 +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;
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-12-10 16:26:46 +00:00
|
|
|
RelayUpstreamMessage::HandleMessage(llarp::Router *r) const
|
2018-06-08 13:12:17 +00:00
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid);
|
2018-06-26 16:23:43 +00:00
|
|
|
if(path)
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
return path->HandleUpstream(X.Buffer(), Y, r);
|
2018-06-26 16:23:43 +00:00
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
return false;
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
RelayDownstreamMessage::RelayDownstreamMessage() : ILinkMessage()
|
2018-06-08 13:12:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
RelayDownstreamMessage::~RelayDownstreamMessage()
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
void
|
|
|
|
RelayDownstreamMessage::Clear()
|
2018-06-08 13:12:17 +00:00
|
|
|
{
|
2019-01-06 15:20:17 +00:00
|
|
|
pathid.Zero();
|
2018-12-20 16:49:05 +00:00
|
|
|
X.Clear();
|
2019-01-06 15:20:17 +00:00
|
|
|
Y.Zero();
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
2018-12-20 16:49:05 +00:00
|
|
|
|
2018-06-08 13:12:17 +00:00
|
|
|
bool
|
|
|
|
RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
2018-06-22 00:25:30 +00:00
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "a", "d"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictEntry("p", pathid, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
2018-06-22 00:25:30 +00:00
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("x", X, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("y", Y, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RelayDownstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
|
|
|
{
|
2018-06-22 00:25:30 +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;
|
2018-06-08 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-12-10 16:26:46 +00:00
|
|
|
RelayDownstreamMessage::HandleMessage(llarp::Router *r) const
|
2018-06-08 13:12:17 +00:00
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid);
|
2018-06-22 00:25:30 +00:00
|
|
|
if(path)
|
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
return path->HandleDownstream(X.Buffer(), Y, r);
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
2018-09-10 16:36:36 +00:00
|
|
|
llarp::LogWarn("unhandled downstream message");
|
2018-06-08 13:12:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
} // namespace llarp
|