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>
|
2018-06-12 13:57:14 +02:00
|
|
|
#include <llarp/crypto.hpp>
|
2018-05-27 15:42:55 +02:00
|
|
|
#include "buffer.hpp"
|
|
|
|
#include "logger.hpp"
|
2018-08-02 02:48:43 +02:00
|
|
|
#include "mem.hpp"
|
2018-05-27 15:42:55 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
namespace llarp
|
2018-07-08 15:26:24 +02:00
|
|
|
{
|
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))
|
2018-05-22 20:41:38 +02:00
|
|
|
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-05-22 17:54:19 +02:00
|
|
|
{
|
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-22 17:54:19 +02:00
|
|
|
}
|
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-05-27 15:42:55 +02:00
|
|
|
|
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-25 19:52:10 +02:00
|
|
|
|
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))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!BEncodeWriteList(exits.begin(), exits.end(), buf))
|
2018-05-22 17:54:19 +02:00
|
|
|
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-02 02:48:43 +02:00
|
|
|
|
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))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
|
2018-05-22 17:54:19 +02:00
|
|
|
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
|