2021-03-09 22:24:35 +00:00
|
|
|
#include "transfer_traffic_message.hpp"
|
2019-06-15 14:55:13 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "handler.hpp"
|
|
|
|
#include <llarp/util/bencode.hpp>
|
|
|
|
#include <llarp/util/endian.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace routing
|
|
|
|
{
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TransferTrafficMessage::PutBuffer(const llarp_buffer_t& buf, uint64_t counter)
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf.sz > MaxExitMTU)
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
X.emplace_back(buf.sz + 8);
|
|
|
|
byte_t* ptr = X.back().data();
|
|
|
|
htobe64buf(ptr, counter);
|
|
|
|
ptr += 8;
|
|
|
|
memcpy(ptr, buf.base, buf.sz);
|
|
|
|
// 8 bytes encoding overhead and 8 bytes counter
|
|
|
|
_size += buf.sz + 16;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TransferTrafficMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "A", "I"))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2021-03-16 11:56:27 +00:00
|
|
|
if (!BEncodeWriteDictInt("P", protocol, buf))
|
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("S", S, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("V", version, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictList("X", X, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TransferTrafficMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("S", S, read, key, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2021-03-16 11:56:27 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("P", protocol, read, key, buf))
|
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("V", version, read, key, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictList("X", X, read, key, buf))
|
2019-06-15 14:55:13 +00:00
|
|
|
return false;
|
2021-04-06 14:17:59 +00:00
|
|
|
return read or bencode_discard(buf);
|
2019-06-15 14:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TransferTrafficMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
return h->HandleTransferTrafficMessage(*this, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace routing
|
|
|
|
} // namespace llarp
|