32 #include <boost/spirit/include/karma_generate.hpp> 33 #include <boost/spirit/include/karma_uint.hpp> 48 constexpr
const char tld[] =
u8".b32.i2p";
49 constexpr
const char unknown_host[] =
"<unknown i2p host>";
51 constexpr
const unsigned b32_length = 52;
53 constexpr
const char base32_alphabet[] =
54 u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz234567";
58 if (!host.ends_with(tld))
61 host.remove_suffix(
sizeof(tld) - 1);
63 if (host.size() != b32_length)
65 if (host.find_first_not_of(base32_alphabet) != boost::string_ref::npos)
87 assert(host.size() <
sizeof(host_));
89 const std::size_t length = std::min(
sizeof(host_) - 1, host.size());
91 std::memset(host_ + length, 0,
sizeof(host_) - length);
102 static_assert(
sizeof(unknown_host) <=
sizeof(host_),
"bad buffer size");
103 std::memcpy(host_, unknown_host,
sizeof(unknown_host));
104 std::memset(host_ +
sizeof(unknown_host), 0,
sizeof(host_) -
sizeof(unknown_host));
110 const boost::string_ref
port =
111 address.substr(host.size() + (host.size() ==
address.size() ? 0 : 1));
119 static_assert(b32_length +
sizeof(tld) ==
sizeof(i2p_address::host_),
"bad internal host size");
126 if (in._load(src, hparent) && in.host.size() <
sizeof(host_) && (in.host == unknown_host || !host_check(in.host).has_error()))
128 std::memcpy(host_, in.host.data(), in.host.size());
129 std::memset(host_ + in.host.size(), 0,
sizeof(host_) - in.host.size());
133 static_assert(
sizeof(unknown_host) <=
sizeof(host_),
"bad buffer size");
134 std::memcpy(host_, unknown_host,
sizeof(unknown_host));
141 const i2p_serialized out{
std::string{host_}, port_};
142 return out.store(
dest, hparent);
153 if (
this != std::addressof(rhs))
163 static_assert(1 <=
sizeof(host_),
"host size too small");
164 return host_[0] ==
'<';
169 return port_ == rhs.port_ && is_same_host(rhs);
174 return std::strcmp(host_str(), rhs.host_str()) < 0 ||
port() < rhs.port();
179 return std::strcmp(host_str(), rhs.host_str()) == 0;
184 const std::size_t host_length = std::strlen(host_str());
185 const std::size_t port_length =
186 port_ == 0 ? 0 : std::numeric_limits<std::uint16_t>::digits10 + 2;
189 out.reserve(host_length + port_length);
190 out.assign(host_str(), host_length);
195 namespace karma = boost::spirit::karma;
196 karma::generate(std::back_inserter(out), karma::ushort_,
port());
i2p_address & operator=(const i2p_address &rhs) noexcept
b32 i2p address; internal format not condensed/decoded.
CXA_THROW_INFO_T void(* dest)(void *))
bool equal(const i2p_address &rhs) const noexcept
static const char * unknown_str() noexcept
bool _load(epee::serialization::portable_storage &src, epee::serialization::section *hparent)
Load from epee p2p format, and.
#define KV_SERIALIZE(varialble)
bool is_same_host(const i2p_address &rhs) const noexcept
#define ELECTRONEUM_CHECK(...)
Check expect<void> and return errors in current scope.
bool is_unknown() const noexcept
boost::endian::big_uint16_t port
i2p_address() noexcept
An object with port() == 0 and host_str() == unknown_str().
expect< void > success() noexcept
void * memcpy(void *a, const void *b, size_t c)
static expect< i2p_address > make(boost::string_ref address, std::uint16_t default_port=0)
#define END_KV_SERIALIZE_MAP()
Outside of 0-65535 range.
bool less(const i2p_address &rhs) const noexcept
bool store(epee::serialization::portable_storage &dest, epee::serialization::section *hparent) const
Store in epee p2p format.
#define BEGIN_KV_SERIALIZE_MAP()