lokinet/llarp/exit/transfer_traffic.cpp

66 lines
1.7 KiB
C++
Raw Normal View History

#include <messages/transfer_traffic.hpp>
2018-12-12 02:04:32 +00:00
#include <routing/handler.hpp>
2019-05-24 02:01:36 +00:00
#include <util/bencode.hpp>
#include <util/endian.hpp>
namespace llarp
{
namespace routing
{
bool
TransferTrafficMessage::PutBuffer(const llarp_buffer_t& buf,
uint64_t counter)
{
if(buf.sz > MaxExitMTU)
return false;
2018-11-29 21:19:20 +00:00
X.emplace_back(buf.sz + 8);
byte_t* ptr = X.back().data();
2018-11-29 21:19:20 +00:00
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))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "I"))
return false;
if(!BEncodeWriteDictInt("S", S, buf))
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
2018-11-28 16:38:20 +00:00
if(!BEncodeWriteDictList("X", X, buf))
return false;
return bencode_end(buf);
}
bool
TransferTrafficMessage::DecodeKey(const llarp_buffer_t& key,
llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("V", version, read, key, buf))
return false;
2018-11-28 16:38:20 +00:00
if(!BEncodeMaybeReadDictList("X", X, read, key, buf))
return false;
return read;
}
bool
TransferTrafficMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
{
return h->HandleTransferTrafficMessage(*this, r);
}
} // namespace routing
} // namespace llarp