29#ifndef _NET_UTILS_BASE_H_
30#define _NET_UTILS_BASE_H_
32#include <boost/uuid/uuid.hpp>
33#include <boost/asio/io_service.hpp>
40#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
41#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net"
44#define MAKE_IP( a1, a2, a3, a4 ) (a1|(a2<<8)|(a3<<16)|(a4<<24))
47#if BOOST_VERSION >= 107000
48#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
50#define GET_IO_SERVICE(s) ((s).get_io_service())
79 {
return ip() == other.ip(); }
83 std::string
str()
const;
98 {
return lhs.equal(rhs); }
100 {
return !lhs.equal(rhs); }
102 {
return lhs.less(rhs); }
104 {
return !rhs.less(lhs); }
106 {
return rhs.less(lhs); }
108 {
return !lhs.less(rhs); }
114 virtual ~interface() {};
116 virtual bool equal(
const interface&)
const = 0;
117 virtual bool less(
const interface&)
const = 0;
120 virtual std::string
str()
const = 0;
121 virtual std::string
host_str()
const = 0;
130 struct implementation final : interface
134 implementation(
const T& src) :
value(src) {}
135 ~implementation() =
default;
138 static const T& cast(
const interface& src)
noexcept
139 {
return static_cast<const implementation<T>&
>(src).
value; }
141 virtual bool equal(
const interface& other)
const override
142 {
return value.equal(cast(other)); }
144 virtual bool less(
const interface& other)
const override
145 {
return value.less(cast(other)); }
147 virtual bool is_same_host(
const interface& other)
const override
148 {
return value.is_same_host(cast(other)); }
150 virtual std::string
str()
const override {
return value.str(); }
151 virtual std::string
host_str()
const override {
return value.host_str(); }
153 virtual bool is_local()
const override {
return value.is_local(); }
159 std::shared_ptr<interface> self;
161 template<
typename Type>
162 Type& as_mutable()
const
165 using Type_ =
typename std::remove_const<Type>::type;
166 network_address::interface*
const self_ = self.get();
167 if (!self_ ||
typeid(implementation<Type_>) !=
typeid(*self_))
168 throw std::bad_cast{};
169 return static_cast<implementation<Type_>*
>(self_)->
value;
172 template<
typename T,
typename t_storage>
173 bool serialize_addr(std::false_type, t_storage& stg,
typename t_storage::hsection hparent)
178 *
this = std::move(addr);
182 template<
typename T,
typename t_storage>
183 bool serialize_addr(std::true_type, t_storage& stg,
typename t_storage::hsection hparent)
const
192 : self(
std::make_shared<implementation<
T>>(src)) {}
196 std::string
str()
const {
return self ? self->str() :
"<none>"; }
197 std::string
host_str()
const {
return self ? self->host_str() :
"<none>"; }
198 bool is_loopback()
const {
return self ? self->is_loopback() :
false; }
199 bool is_local()
const {
return self ? self->is_local() :
false; }
202 bool is_blockable()
const {
return self ? self->is_blockable() :
false; }
203 template<
typename Type>
const Type &
as()
const {
return as_mutable<const Type>(); }
216 return this_ref.template serialize_addr<ipv4_network_address>(
is_store_, stg, hparent_section);
218 return this_ref.template serialize_addr<net::tor_address>(
is_store_, stg, hparent_section);
220 return this_ref.template serialize_addr<net::i2p_address>(
is_store_, stg, hparent_section);
226 MERROR(
"Unsupported network address type: " << (
unsigned)
type);
232 {
return lhs.
equal(rhs); }
234 {
return !lhs.
equal(rhs); }
236 {
return lhs.
less(rhs); }
238 {
return !rhs.
less(lhs); }
240 {
return rhs.
less(lhs); }
242 {
return !lhs.
less(rhs); }
265 time_t last_recv = 0, time_t last_send = 0,
299 set_details(
a.m_connection_id,
a.m_remote_address,
a.m_is_income,
a.m_ssl);
304 set_details(
a.m_connection_id,
a.m_remote_address,
a.m_is_income,
a.m_ssl);
309 template<
class t_protocol_handler>
311 void set_details(boost::uuids::uuid connection_id,
const network_address &remote_address,
bool is_income,
bool ssl)
324 virtual bool do_send(
const void* ptr,
size_t cb)=0;
350#define LOG_ERROR_CC(ct, message) MERROR(ct << message)
351#define LOG_WARNING_CC(ct, message) MWARNING(ct << message)
352#define LOG_INFO_CC(ct, message) MINFO(ct << message)
353#define LOG_DEBUG_CC(ct, message) MDEBUG(ct << message)
354#define LOG_TRACE_CC(ct, message) MTRACE(ct << message)
355#define LOG_CC(level, ct, message) MLOG(level, ct << message)
357#define LOG_PRINT_CC_L0(ct, message) LOG_PRINT_L0(ct << message)
358#define LOG_PRINT_CC_L1(ct, message) LOG_PRINT_L1(ct << message)
359#define LOG_PRINT_CC_L2(ct, message) LOG_PRINT_L2(ct << message)
360#define LOG_PRINT_CC_L3(ct, message) LOG_PRINT_L3(ct << message)
361#define LOG_PRINT_CC_L4(ct, message) LOG_PRINT_L4(ct << message)
363#define LOG_PRINT_CCONTEXT_L0(message) LOG_PRINT_CC_L0(context, message)
364#define LOG_PRINT_CCONTEXT_L1(message) LOG_PRINT_CC_L1(context, message)
365#define LOG_PRINT_CCONTEXT_L2(message) LOG_PRINT_CC_L2(context, message)
366#define LOG_PRINT_CCONTEXT_L3(message) LOG_PRINT_CC_L3(context, message)
367#define LOG_ERROR_CCONTEXT(message) LOG_ERROR_CC(context, message)
369#define CHECK_AND_ASSERT_MES_CC(condition, return_val, err_message) CHECK_AND_ASSERT_MES(condition, return_val, "[" << epee::net_utils::print_connection_context_short(context) << "]" << err_message)
static constexpr zone get_zone() noexcept
constexpr bool is_same_host(const ipv4_network_address &other) const noexcept
bool less(const ipv4_network_address &other) const noexcept
static constexpr address_type get_type_id() noexcept
static constexpr bool is_blockable() noexcept
constexpr uint16_t port() const noexcept
constexpr uint32_t ip() const noexcept
bool equal(const ipv4_network_address &other) const noexcept
std::string host_str() const
constexpr ipv4_network_address(uint32_t ip, uint16_t port) noexcept
constexpr ipv4_network_address() noexcept
bool equal(const network_address &other) const
MERROR("Unsupported network address type: "<<(unsigned) type)
network_address(const T &src)
address_type get_type_id() const
bool less(const network_address &other) const
bool is_same_host(const network_address &other) const
static constexpr std::integral_constant< bool, is_store > is_store_
std::string host_str() const
bool is_blockable() const
b32 i2p address; internal format not condensed/decoded.
Tor onion address; internal format not condensed/decoded.
#define MAKE_LOGGABLE(ClassType, ClassInstance, OutputStreamInstance)
#define KV_SERIALIZE(varialble)
#define END_KV_SERIALIZE_MAP()
#define BEGIN_KV_SERIALIZE_MAP()
bool operator<=(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
bool operator==(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
std::string print_connection_context_short(const connection_context_base &ctx)
std::string print_connection_context(const connection_context_base &ctx)
bool operator!=(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
bool operator>=(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
bool operator>(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
bool operator<(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
const GenericPointer< typename T::ValueType > T2 value
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
unsigned __int64 uint64_t
connection_context_base(boost::uuids::uuid connection_id, const network_address &remote_address, bool is_income, bool ssl, time_t last_recv=0, time_t last_send=0, uint64_t recv_cnt=0, uint64_t send_cnt=0)
connection_context_base()
connection_context_base(const connection_context_base &a)
connection_context_base & operator=(const connection_context_base &a)
const boost::uuids::uuid m_connection_id
const network_address m_remote_address
double m_current_speed_down
double m_current_speed_up
virtual bool send_done()=0
virtual bool request_callback()=0
virtual ~i_service_endpoint() noexcept(false)
virtual bool call_run_once_service_io()=0
virtual boost::asio::io_service & get_io_service()=0
virtual bool do_send(const void *ptr, size_t cb)=0