mirror of https://github.com/oxen-io/lokinet
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.
178 lines
3.0 KiB
C++
178 lines
3.0 KiB
C++
6 years ago
|
#ifndef LLARP_SERVICE_ADDRESS_HPP
|
||
|
#define LLARP_SERVICE_ADDRESS_HPP
|
||
|
#include <llarp/aligned.hpp>
|
||
6 years ago
|
#include <dht/key.hpp>
|
||
6 years ago
|
#include <llarp/router_id.hpp>
|
||
6 years ago
|
#include <string>
|
||
|
|
||
|
namespace llarp
|
||
|
{
|
||
|
namespace service
|
||
|
{
|
||
6 years ago
|
/// Snapp/Snode Address
|
||
6 years ago
|
struct Address
|
||
6 years ago
|
{
|
||
|
std::string
|
||
6 years ago
|
ToString(const char* tld = ".loki") const;
|
||
6 years ago
|
|
||
6 years ago
|
bool
|
||
6 years ago
|
FromString(const std::string& str, const char* tld = ".loki");
|
||
6 years ago
|
|
||
6 years ago
|
Address()
|
||
6 years ago
|
{
|
||
6 years ago
|
Zero();
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
Address(const byte_t* buf)
|
||
|
{
|
||
|
memcpy(b, buf, 32);
|
||
|
}
|
||
|
|
||
6 years ago
|
Address(const Address& other)
|
||
6 years ago
|
{
|
||
6 years ago
|
memcpy(b, other.b, 32);
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
byte_t& operator[](size_t idx)
|
||
6 years ago
|
{
|
||
6 years ago
|
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)
|
||
6 years ago
|
{
|
||
6 years ago
|
llarp::LogErrorTag("Address::BDecode",
|
||
|
"bdecode buffer size missmatch ", strbuf.sz,
|
||
|
"!=32");
|
||
|
return false;
|
||
6 years ago
|
}
|
||
6 years ago
|
memcpy(b, strbuf.base, 32);
|
||
|
return true;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
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
|
||
6 years ago
|
{
|
||
|
return dht::Key_t(data());
|
||
|
}
|
||
6 years ago
|
|
||
|
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];
|
||
|
};
|
||
6 years ago
|
};
|
||
|
|
||
|
} // namespace service
|
||
|
} // namespace llarp
|
||
6 years ago
|
|
||
6 years ago
|
#endif
|