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

125 lines
3 KiB
C++
Raw Normal View History

2018-06-22 02:25:30 +02:00
#include <llarp/bencode.hpp>
2018-06-08 15:12:17 +02:00
#include <llarp/messages/relay.hpp>
2018-06-22 02:25:30 +02:00
#include "router.hpp"
2018-06-08 15:12:17 +02:00
namespace llarp
{
RelayUpstreamMessage::RelayUpstreamMessage(ILinkSession *from)
2018-06-08 15:12:17 +02:00
: ILinkMessage(from)
{
}
2018-06-22 02:25:30 +02:00
RelayUpstreamMessage::RelayUpstreamMessage() : ILinkMessage()
{
}
2018-06-08 15:12:17 +02:00
RelayUpstreamMessage::~RelayUpstreamMessage()
{
}
bool
RelayUpstreamMessage::BEncode(llarp_buffer_t *buf) const
{
2018-06-26 18:23:43 +02:00
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))
2018-06-26 18:23:43 +02:00
return false;
if(!BEncodeWriteDictEntry("x", X, buf))
return false;
if(!BEncodeWriteDictEntry("y", Y, buf))
return false;
return bencode_end(buf);
2018-06-08 15:12:17 +02:00
}
bool
RelayUpstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{
2018-06-26 18:23:43 +02: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 15:12:17 +02:00
}
bool
RelayUpstreamMessage::HandleMessage(llarp_router *r) const
2018-06-08 15:12:17 +02:00
{
auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid);
2018-06-26 18:23:43 +02:00
if(path)
{
return path->HandleUpstream(X.Buffer(), Y, r);
2018-06-26 18:23:43 +02:00
}
return false;
2018-06-08 15:12:17 +02:00
}
RelayDownstreamMessage::RelayDownstreamMessage(ILinkSession *from)
2018-06-08 15:12:17 +02:00
: ILinkMessage(from)
{
}
2018-06-22 02:25:30 +02:00
RelayDownstreamMessage::RelayDownstreamMessage() : ILinkMessage()
{
}
2018-06-08 15:12:17 +02:00
RelayDownstreamMessage::~RelayDownstreamMessage()
{
}
bool
RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const
{
2018-06-22 02:25:30 +02:00
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))
2018-06-22 02:25:30 +02:00
return false;
if(!BEncodeWriteDictEntry("x", X, buf))
return false;
if(!BEncodeWriteDictEntry("y", Y, buf))
return false;
return bencode_end(buf);
2018-06-08 15:12:17 +02:00
}
bool
RelayDownstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{
2018-06-22 02:25:30 +02: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 15:12:17 +02:00
}
bool
RelayDownstreamMessage::HandleMessage(llarp_router *r) const
2018-06-08 15:12:17 +02:00
{
auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid);
2018-06-22 02:25:30 +02:00
if(path)
{
return path->HandleDownstream(X.Buffer(), Y, r);
2018-06-22 02:25:30 +02:00
}
2018-06-08 15:12:17 +02:00
return false;
}
2018-06-22 02:25:30 +02:00
} // namespace llarp