2018-07-11 13:20:14 +00:00
|
|
|
#ifndef LLARP_SERVICE_ADDRESS_HPP
|
|
|
|
#define LLARP_SERVICE_ADDRESS_HPP
|
|
|
|
#include <llarp/aligned.hpp>
|
2018-07-12 13:43:37 +00:00
|
|
|
#include <llarp/dht/key.hpp>
|
2018-08-10 21:34:11 +00:00
|
|
|
#include <llarp/router_id.hpp>
|
2018-07-11 13:20:14 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
struct Address
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
|
|
|
FromString(const std::string& str);
|
|
|
|
|
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-08-10 21:34:11 +00:00
|
|
|
Address(const Address& other)
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
memcpy(b, other.b, 32);
|
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
|
|
|
|
{
|
|
|
|
return bencode_write_bytestring(buf, b, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
|
|
|
return false;
|
|
|
|
if(strbuf.sz != 32)
|
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-08-10 21:34:11 +00:00
|
|
|
memcpy(b, strbuf.base, 32);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
size_t
|
|
|
|
size() const
|
|
|
|
{
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsZero() const
|
|
|
|
{
|
|
|
|
size_t sz = 4;
|
|
|
|
while(sz)
|
|
|
|
{
|
|
|
|
if(l[--sz])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Zero()
|
|
|
|
{
|
|
|
|
size_t sz = 4;
|
|
|
|
while(sz)
|
|
|
|
{
|
|
|
|
l[--sz] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const Address& other) const
|
|
|
|
{
|
|
|
|
return memcmp(data(), other.data(), 32) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const Address& self)
|
|
|
|
{
|
|
|
|
return out << self.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Address& other) const
|
|
|
|
{
|
|
|
|
return memcmp(data(), other.data(), 32) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Address& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
Address&
|
|
|
|
operator=(const Address& other)
|
|
|
|
{
|
|
|
|
memcpy(data(), other.data(), 32);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return RouterID(data());
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte_t*
|
|
|
|
data() const
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte_t*
|
|
|
|
data()
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint64_t*
|
|
|
|
data_l() const
|
|
|
|
{
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const Address& buf) const
|
|
|
|
{
|
|
|
|
return *buf.data_l();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
union {
|
|
|
|
byte_t b[32];
|
|
|
|
uint64_t l[4];
|
|
|
|
};
|
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
|