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

99 lines
2.4 KiB
C++
Raw Normal View History

2018-08-30 20:48:43 +02:00
#include <llarp/bencode.hpp>
#include <llarp/router_contact.hpp>
2018-05-11 01:32:46 +02:00
#include <llarp/version.h>
#include <llarp/crypto.hpp>
#include "buffer.hpp"
#include "logger.hpp"
#include "mem.hpp"
2018-08-30 20:48:43 +02:00
namespace llarp
{
2018-08-30 20:48:43 +02:00
bool
RouterContact::BEncode(llarp_buffer_t *buf) const
2018-05-13 20:07:36 +02:00
{
2018-08-30 20:48:43 +02:00
/* write dict begin */
if(!bencode_start_dict(buf))
2018-06-10 16:05:48 +02:00
return false;
2018-08-30 20:48:43 +02:00
/* write ai if they exist */
if(!bencode_write_bytestring(buf, "a", 1))
2018-05-13 20:07:36 +02:00
return false;
2018-08-30 20:48:43 +02:00
if(!BEncodeWriteList(addrs.begin(), addrs.end(), buf))
2018-05-13 20:07:36 +02:00
return false;
2018-08-30 20:48:43 +02:00
/* write signing pubkey */
if(!bencode_write_bytestring(buf, "k", 1))
return false;
2018-08-30 20:48:43 +02:00
if(!pubkey.BEncode(buf))
2018-05-13 20:07:36 +02:00
return false;
2018-08-30 20:48:43 +02:00
std::string nick = Nick();
if(nick.size())
{
2018-08-30 20:48:43 +02:00
/* write nickname */
if(!bencode_write_bytestring(buf, "n", 1))
return false;
if(!bencode_write_bytestring(buf, nick.c_str(), nick.size()))
return false;
}
2018-05-13 20:07:36 +02:00
2018-08-30 20:48:43 +02:00
/* write encryption pubkey */
if(!bencode_write_bytestring(buf, "p", 1))
2018-05-13 20:07:36 +02:00
return false;
2018-08-30 20:48:43 +02:00
if(!enckey.BEncode(buf))
2018-05-13 20:07:36 +02:00
return false;
2018-08-30 20:48:43 +02:00
/* write last updated */
if(!bencode_write_bytestring(buf, "u", 1))
return false;
if(!bencode_write_uint64(buf, last_updated))
return false;
2018-05-13 20:07:36 +02:00
2018-08-30 20:48:43 +02:00
/* write version */
if(!bencode_write_version_entry(buf))
return false;
2018-05-11 01:32:46 +02:00
/* write ai if they exist */
2018-08-30 20:48:43 +02:00
if(!bencode_write_bytestring(buf, "x", 1))
return false;
2018-08-30 20:48:43 +02:00
if(!BEncodeWriteList(exits.begin(), exits.end(), buf))
return false;
2018-06-10 16:05:48 +02:00
2018-08-30 20:48:43 +02:00
/* write signature */
if(!bencode_write_bytestring(buf, "z", 1))
return false;
if(!signature.BEncode(buf))
return false;
return bencode_end(buf);
}
2018-05-11 01:32:46 +02:00
2018-08-30 20:48:43 +02:00
bool
RouterContact::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
2018-08-09 17:36:58 +02:00
{
2018-08-30 20:48:43 +02:00
bool read = false;
if(!BEncodeMaybeReadDictList("a", addrs, read, key, buf))
2018-08-09 17:36:58 +02:00
return false;
2018-08-30 20:48:43 +02:00
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
2018-08-09 17:36:58 +02:00
return false;
2018-08-30 20:48:43 +02:00
if(!BEncodeMaybeReadDictEntry("n", nickname, read, key, buf))
return false;
2018-06-12 14:49:23 +02:00
2018-08-30 20:48:43 +02:00
if(!BEncodeMaybeReadDictEntry("p", enckey, read, key, buf))
return false;
2018-05-21 14:44:50 +02:00
2018-08-30 20:48:43 +02:00
if(!BEncodeMaybeReadDictInt("u", last_updated, read, key, buf))
return false;
2018-05-11 01:32:46 +02:00
2018-08-30 20:48:43 +02:00
if(!BEncodeMaybeReadDictList("x", exits, read, key, buf))
return false;
2018-08-30 20:48:43 +02:00
if(!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
return false;
2018-08-30 20:48:43 +02:00
return read;
2018-05-11 01:32:46 +02:00
}
2018-08-30 20:48:43 +02:00
} // namespace llarp