2018-07-11 13:20:14 +00:00
|
|
|
#ifndef LLARP_SERVICE_ADDRESS_HPP
|
|
|
|
#define LLARP_SERVICE_ADDRESS_HPP
|
2018-12-12 02:52:51 +00:00
|
|
|
|
|
|
|
#include <aligned.hpp>
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/key.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <router_id.hpp>
|
|
|
|
|
2018-12-20 14:18:03 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <numeric>
|
2018-07-11 13:20:14 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2018-12-03 22:22:59 +00:00
|
|
|
/// Snapp/Snode Address
|
2018-08-10 21:34:11 +00:00
|
|
|
struct Address
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
static constexpr size_t SIZE = 32;
|
|
|
|
|
|
|
|
using Data = std::array< byte_t, SIZE >;
|
|
|
|
|
2018-07-12 13:43:37 +00:00
|
|
|
std::string
|
2018-12-03 22:22:59 +00:00
|
|
|
ToString(const char* tld = ".loki") const;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
2018-12-03 22:22:59 +00:00
|
|
|
FromString(const std::string& str, const char* tld = ".loki");
|
2018-08-02 00:48:43 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
Address()
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
Zero();
|
2018-07-12 13:43:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
Address(const byte_t* buf)
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
std::copy(buf, buf + SIZE, b.begin());
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
Address(const Address& other)
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
b = other.b;
|
2018-07-12 13:43:37 +00:00
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
|
|
|
|
byte_t& operator[](size_t idx)
|
2018-07-12 18:21:44 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
return b[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte_t& operator[](size_t idx) const
|
|
|
|
{
|
|
|
|
return b[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return bencode_write_bytestring(buf, b.data(), SIZE);
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
|
|
|
return false;
|
2018-12-20 14:18:03 +00:00
|
|
|
if(strbuf.sz != SIZE)
|
2018-07-12 18:21:44 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
llarp::LogErrorTag("Address::BDecode",
|
|
|
|
"bdecode buffer size missmatch ", strbuf.sz,
|
|
|
|
"!=32");
|
|
|
|
return false;
|
2018-07-12 18:21:44 +00:00
|
|
|
}
|
2018-12-20 14:18:03 +00:00
|
|
|
std::copy(strbuf.base, strbuf.base + SIZE, b.begin());
|
2018-08-10 21:34:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
|
2018-12-20 14:18:03 +00:00
|
|
|
static constexpr size_t
|
|
|
|
size()
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return SIZE;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsZero() const
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return b == Data{};
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Zero()
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
b.fill(0);
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const Address& other) const
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return data() < other.data();
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const Address& self)
|
|
|
|
{
|
|
|
|
return out << self.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Address& other) const
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return data() == other.data();
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Address& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
Address&
|
2018-12-20 14:18:03 +00:00
|
|
|
operator=(const Address& other) = default;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
|
|
|
const dht::Key_t
|
|
|
|
ToKey() const
|
2018-07-17 04:37:50 +00:00
|
|
|
{
|
|
|
|
return dht::Key_t(data());
|
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
|
|
|
|
const RouterID
|
|
|
|
ToRouter() const
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return RouterID(data().data());
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 14:18:03 +00:00
|
|
|
const Data&
|
2018-08-10 21:34:11 +00:00
|
|
|
data() const
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2018-12-20 14:18:03 +00:00
|
|
|
Data&
|
2018-08-10 21:34:11 +00:00
|
|
|
data()
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const Address& buf) const
|
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return std::accumulate(buf.data().begin(), buf.data().end(), 0,
|
|
|
|
std::bit_xor< size_t >());
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2018-12-20 14:18:03 +00:00
|
|
|
Data b;
|
2018-07-12 13:43:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-09-19 16:20:34 +00:00
|
|
|
#endif
|