lokinet/llarp/routing/transfer_traffic_message.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

2019-06-19 22:30:07 +00:00
#include <routing/transfer_traffic_message.hpp>
2019-06-15 14:55:13 +00:00
#include <routing/handler.hpp>
#include <util/bencode.hpp>
#include <util/endian.hpp>
namespace llarp
{
namespace routing
{
bool
TransferTrafficMessage::PutBuffer(const llarp_buffer_t& buf, uint64_t counter)
2019-06-15 14:55:13 +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
{
if (!bencode_start_dict(buf))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeWriteDictMsgType(buf, "A", "I"))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeWriteDictInt("S", S, buf))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeWriteDictInt("V", version, buf))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeWriteDictList("X", X, buf))
2019-06-15 14:55:13 +00:00
return false;
return bencode_end(buf);
}
bool
TransferTrafficMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
2019-06-15 14:55:13 +00:00
{
bool read = false;
if (!BEncodeMaybeReadDictInt("S", S, read, key, buf))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeMaybeReadDictInt("V", version, read, key, buf))
2019-06-15 14:55:13 +00:00
return false;
if (!BEncodeMaybeReadDictList("X", X, read, key, buf))
2019-06-15 14:55:13 +00:00
return false;
return read;
}
bool
TransferTrafficMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const
2019-06-15 14:55:13 +00:00
{
return h->HandleTransferTrafficMessage(*this, r);
}
} // namespace routing
} // namespace llarp