You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/router_contact.cpp

99 lines
2.4 KiB
C++

#include <llarp/bencode.hpp>
#include <llarp/router_contact.hpp>
6 years ago
#include <llarp/version.h>
#include <llarp/crypto.hpp>
#include "buffer.hpp"
#include "logger.hpp"
#include "mem.hpp"
namespace llarp
{
bool
RouterContact::BEncode(llarp_buffer_t *buf) const
6 years ago
{
/* write dict begin */
if(!bencode_start_dict(buf))
return false;
/* write ai if they exist */
if(!bencode_write_bytestring(buf, "a", 1))
6 years ago
return false;
if(!BEncodeWriteList(addrs.begin(), addrs.end(), buf))
6 years ago
return false;
/* write signing pubkey */
if(!bencode_write_bytestring(buf, "k", 1))
return false;
if(!pubkey.BEncode(buf))
6 years ago
return false;
std::string nick = Nick();
if(nick.size())
{
/* write nickname */
if(!bencode_write_bytestring(buf, "n", 1))
return false;
if(!bencode_write_bytestring(buf, nick.c_str(), nick.size()))
return false;
}
6 years ago
/* write encryption pubkey */
if(!bencode_write_bytestring(buf, "p", 1))
6 years ago
return false;
if(!enckey.BEncode(buf))
6 years ago
return false;
/* write last updated */
if(!bencode_write_bytestring(buf, "u", 1))
return false;
if(!bencode_write_uint64(buf, last_updated))
return false;
6 years ago
/* write version */
if(!bencode_write_version_entry(buf))
return false;
6 years ago
/* write ai if they exist */
if(!bencode_write_bytestring(buf, "x", 1))
return false;
if(!BEncodeWriteList(exits.begin(), exits.end(), buf))
return false;
/* write signature */
if(!bencode_write_bytestring(buf, "z", 1))
return false;
if(!signature.BEncode(buf))
return false;
return bencode_end(buf);
}
6 years ago
bool
RouterContact::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{
bool read = false;
if(!BEncodeMaybeReadDictList("a", addrs, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("n", nickname, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("p", enckey, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("u", last_updated, read, key, buf))
return false;
6 years ago
if(!BEncodeMaybeReadDictList("x", exits, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
return false;
return read;
6 years ago
}
} // namespace llarp