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> 35 #include <type_traits> 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(); }
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;
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)); }
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>
182 template<
typename T,
typename t_storage>
192 : self(
std::make_shared<implementation<
T>>(src)) {}
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>(); }
207 static constexpr
std::integral_constant<
bool, is_store> is_store_{};
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,
267 m_connection_id(connection_id),
268 m_remote_address(remote_address),
269 m_is_income(is_income),
270 m_started(
time(NULL)),
272 m_last_recv(last_recv),
273 m_last_send(last_send),
274 m_recv_cnt(recv_cnt),
275 m_send_cnt(send_cnt),
276 m_current_speed_down(0),
277 m_current_speed_up(0),
285 m_started(
time(NULL)),
291 m_current_speed_down(0),
292 m_current_speed_up(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>
324 virtual bool do_send(
const void* ptr,
size_t cb)=0;
325 virtual bool close()=0;
326 virtual bool send_done()=0;
327 virtual bool call_run_once_service_io()=0;
328 virtual bool request_callback()=0;
329 virtual boost::asio::io_service& get_io_service()=0;
331 virtual bool add_ref()=0;
332 virtual bool release()=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) 374 #endif //_NET_UTILS_BASE_H_
bool equal(const ipv4_network_address &other) const noexcept
std::string print_connection_context_short(const connection_context_base &ctx)
constexpr uint16_t port() const noexcept
bool operator!=(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
b32 i2p address; internal format not condensed/decoded.
bool operator==(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
std::string host_str() const
virtual ~i_service_endpoint() noexcept(false)
std::string host_str() const
#define KV_SERIALIZE(varialble)
double m_current_speed_up
network_address(const T &src)
bool_constant< true > true_type
address_type get_type_id() const
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)
constexpr uint32_t ip() const noexcept
bool operator<=(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
void serialize(Archive &a, unsigned_tx_set &x, const boost::serialization::version_type ver)
Represents a single connection from a client.
bool less(const network_address &other) const
static constexpr address_type get_type_id() noexcept
unsigned __int64 uint64_t
connection_context_base()
static constexpr bool is_blockable() noexcept
constexpr ipv4_network_address(uint32_t ip, uint16_t port) noexcept
const network_address m_remote_address
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
MAKE_LOGGABLE(connection_context_base, ct, os)
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 T & move(const T &t)
Tor onion address; internal format not condensed/decoded.
connection_context_base(const connection_context_base &a)
constexpr bool is_same_host(const ipv4_network_address &other) const noexcept
double m_current_speed_down
bool is_blockable() const
constexpr ipv4_network_address() noexcept
const GenericPointer< typename T::ValueType > T2 value
static constexpr zone get_zone() noexcept
bool_constant< false > false_type
std::string print_connection_context(const connection_context_base &ctx)
connection_context_base & operator=(const connection_context_base &a)
bool operator>=(const ipv4_network_address &lhs, const ipv4_network_address &rhs) noexcept
#define END_KV_SERIALIZE_MAP()
bool less(const ipv4_network_address &other) const noexcept
bool equal(const network_address &other) const
const boost::uuids::uuid m_connection_id
#define BEGIN_KV_SERIALIZE_MAP()