aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/include
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/epee/include')
-rw-r--r--contrib/epee/include/net/abstract_tcp_server.h5
-rw-r--r--contrib/epee/include/net/abstract_tcp_server2.h2
-rw-r--r--contrib/epee/include/net/abstract_tcp_server2.inl7
-rw-r--r--contrib/epee/include/net/abstract_tcp_server_cp.inl5
-rw-r--r--contrib/epee/include/net/http_server_handlers_map2.h2
-rw-r--r--contrib/epee/include/net/levin_client_async.h3
-rw-r--r--contrib/epee/include/net/net_utils_base.h122
-rw-r--r--contrib/epee/include/string_tools.h2
8 files changed, 122 insertions, 26 deletions
diff --git a/contrib/epee/include/net/abstract_tcp_server.h b/contrib/epee/include/net/abstract_tcp_server.h
index 1efd5091c..000305cfa 100644
--- a/contrib/epee/include/net/abstract_tcp_server.h
+++ b/contrib/epee/include/net/abstract_tcp_server.h
@@ -296,7 +296,7 @@ namespace net_utils
}
//----------------------------------------------------------------------------------------
template<class THandler>
- bool abstract_tcp_server<THandler>::invoke_connection(SOCKET hnew_sock, long ip_from, int post_from)
+ bool abstract_tcp_server<THandler>::invoke_connection(SOCKET hnew_sock, const network_address &remote_address)
{
m_connections_lock.lock();
m_connections.push_back(thread_context());
@@ -304,8 +304,7 @@ namespace net_utils
m_connections.back().m_socket = hnew_sock;
m_connections.back().powner = this;
m_connections.back().m_self_it = --m_connections.end();
- m_connections.back().m_context.m_remote_ip = ip_from;
- m_connections.back().m_context.m_remote_port = post_from;
+ m_connections.back().m_context.m_remote_address = remote_address;
m_connections.back().m_htread = threads_helper::create_thread(ConnectionHandlerProc, &m_connections.back());
return true;
diff --git a/contrib/epee/include/net/abstract_tcp_server2.h b/contrib/epee/include/net/abstract_tcp_server2.h
index 506949dbd..ca58d5467 100644
--- a/contrib/epee/include/net/abstract_tcp_server2.h
+++ b/contrib/epee/include/net/abstract_tcp_server2.h
@@ -69,7 +69,7 @@ namespace net_utils
struct i_connection_filter
{
- virtual bool is_remote_ip_allowed(uint32_t adress)=0;
+ virtual bool is_remote_host_allowed(const epee::net_utils::network_address &address)=0;
protected:
virtual ~i_connection_filter(){}
};
diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl
index 75a9c5be9..0fbd9ed28 100644
--- a/contrib/epee/include/net/abstract_tcp_server2.inl
+++ b/contrib/epee/include/net/abstract_tcp_server2.inl
@@ -133,6 +133,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
boost::system::error_code ec;
auto remote_ep = socket_.remote_endpoint(ec);
CHECK_AND_NO_ASSERT_MES(!ec, false, "Failed to get remote endpoint: " << ec.message() << ':' << ec.value());
+ CHECK_AND_NO_ASSERT_MES(remote_ep.address().is_v4(), false, "IPv6 not supported here");
auto local_ep = socket_.local_endpoint(ec);
CHECK_AND_NO_ASSERT_MES(!ec, false, "Failed to get local endpoint: " << ec.message() << ':' << ec.value());
@@ -145,14 +146,14 @@ PRAGMA_WARNING_DISABLE_VS(4355)
// that stuff turns out to be included, even though it's from src... Taking advantage
random_uuid = crypto::rand<boost::uuids::uuid>();
- context.set_details(random_uuid, ip_, remote_ep.port(), is_income);
+ context.set_details(random_uuid, new epee::net_utils::ipv4_network_address(ip_, remote_ep.port()), is_income);
_dbg3("[sock " << socket_.native_handle() << "] new connection from " << print_connection_context_short(context) <<
" to " << local_ep.address().to_string() << ':' << local_ep.port() <<
", total sockets objects " << m_ref_sock_count);
- if(m_pfilter && !m_pfilter->is_remote_ip_allowed(context.m_remote_ip))
+ if(m_pfilter && !m_pfilter->is_remote_host_allowed(context.m_remote_address))
{
- _dbg2("[sock " << socket_.native_handle() << "] ip denied " << string_tools::get_ip_string_from_int32(context.m_remote_ip) << ", shutdowning connection");
+ _dbg2("[sock " << socket_.native_handle() << "] host denied " << context.m_remote_address.host_str() << ", shutdowning connection");
close();
return false;
}
diff --git a/contrib/epee/include/net/abstract_tcp_server_cp.inl b/contrib/epee/include/net/abstract_tcp_server_cp.inl
index ba201e3bf..e0ef6470e 100644
--- a/contrib/epee/include/net/abstract_tcp_server_cp.inl
+++ b/contrib/epee/include/net/abstract_tcp_server_cp.inl
@@ -471,7 +471,7 @@ bool cp_server_impl<TProtocol>::run_server(int threads_count = 0)
}
//-------------------------------------------------------------
template<class TProtocol>
-bool cp_server_impl<TProtocol>::add_new_connection(SOCKET new_sock, long ip_from, int port_from)
+bool cp_server_impl<TProtocol>::add_new_connection(SOCKET new_sock, const network_address &address_from)
{
PROFILE_FUNC("[add_new_connection]");
@@ -487,8 +487,7 @@ bool cp_server_impl<TProtocol>::add_new_connection(SOCKET new_sock, long ip_from
m_connections_lock.unlock();
conn.init_buffers();
conn.m_sock = new_sock;
- conn.context.m_remote_ip = ip_from;
- conn.context.m_remote_port = port_from;
+ conn.context.m_remote_address = address_from;
conn.m_completion_port = m_completion_port;
{
PROFILE_FUNC("[add_new_connection] CreateIoCompletionPort");
diff --git a/contrib/epee/include/net/http_server_handlers_map2.h b/contrib/epee/include/net/http_server_handlers_map2.h
index 8e39b4104..429e3e1af 100644
--- a/contrib/epee/include/net/http_server_handlers_map2.h
+++ b/contrib/epee/include/net/http_server_handlers_map2.h
@@ -39,7 +39,7 @@
epee::net_utils::http::http_response_info& response, \
context_type& m_conn_context) \
{\
- LOG_PRINT_L2("HTTP [" << epee::string_tools::get_ip_string_from_int32(m_conn_context.m_remote_ip ) << "] " << query_info.m_http_method_str << " " << query_info.m_URI); \
+ LOG_PRINT_L2("HTTP [" << m_conn_context.m_remote_address.host_str() << "] " << query_info.m_http_method_str << " " << query_info.m_URI); \
response.m_response_code = 200; \
response.m_response_comment = "Ok"; \
if(!handle_http_request_map(query_info, response, m_conn_context)) \
diff --git a/contrib/epee/include/net/levin_client_async.h b/contrib/epee/include/net/levin_client_async.h
index 2cbdb4587..4b48070d6 100644
--- a/contrib/epee/include/net/levin_client_async.h
+++ b/contrib/epee/include/net/levin_client_async.h
@@ -492,8 +492,7 @@ namespace levin
{
net_utils::connection_context_base conn_context;
- conn_context.m_remote_ip = m_ip;
- conn_context.m_remote_port = m_port;
+ conn_context.m_remote_address = m_address;
if(head.m_have_to_return_data)
{
std::string return_buff;
diff --git a/contrib/epee/include/net/net_utils_base.h b/contrib/epee/include/net/net_utils_base.h
index 4334029f7..ba59daf4f 100644
--- a/contrib/epee/include/net/net_utils_base.h
+++ b/contrib/epee/include/net/net_utils_base.h
@@ -29,8 +29,11 @@
#ifndef _NET_UTILS_BASE_H_
#define _NET_UTILS_BASE_H_
+#include <typeinfo>
#include <boost/asio/io_service.hpp>
#include <boost/uuid/uuid.hpp>
+#include "serialization/keyvalue_serialization.h"
+#include "net/local_ip.h"
#include "string_tools.h"
#include "misc_log_ex.h"
@@ -46,14 +49,111 @@ namespace epee
{
namespace net_utils
{
+ struct network_address_base
+ {
+ public:
+ bool operator==(const network_address_base &other) const { return m_full_id == other.m_full_id; }
+ bool operator!=(const network_address_base &other) const { return !operator==(other); }
+ bool operator<(const network_address_base &other) const { return m_full_id < other.m_full_id; }
+ bool is_same_host(const network_address_base &other) const { return m_host_id == other.m_host_id; }
+ virtual std::string str() const = 0;
+ virtual std::string host_str() const = 0;
+ virtual bool is_loopback() const = 0;
+ virtual bool is_local() const = 0;
+ virtual uint8_t get_type_id() const = 0;
+ protected:
+ // A very simple non cryptographic hash function by Fowler, Noll, Vo
+ uint64_t fnv1a(const uint8_t *data, size_t len) const {
+ uint64_t h = 0xcbf29ce484222325;
+ while (len--)
+ h = (h ^ *data++) * 0x100000001b3;
+ return h;
+ }
+ uint64_t m_host_id;
+ uint64_t m_full_id;
+ };
+ struct ipv4_network_address: public network_address_base
+ {
+ void init_ids()
+ {
+ m_host_id = fnv1a((const uint8_t*)&m_ip, sizeof(m_ip));
+ m_full_id = fnv1a((const uint8_t*)&m_ip, sizeof(m_ip) + sizeof(m_port));
+ }
+ public:
+ ipv4_network_address(uint32_t ip, uint16_t port): network_address_base(), m_ip(ip), m_port(port) { init_ids(); }
+ uint32_t ip() const { return m_ip; }
+ uint16_t port() const { return m_port; }
+ virtual std::string str() const { return epee::string_tools::get_ip_string_from_int32(m_ip) + ":" + std::to_string(m_port); }
+ virtual std::string host_str() const { return epee::string_tools::get_ip_string_from_int32(m_ip); }
+ virtual bool is_loopback() const { return epee::net_utils::is_ip_loopback(m_ip); }
+ virtual bool is_local() const { return epee::net_utils::is_ip_local(m_ip); }
+ virtual uint8_t get_type_id() const { return ID; }
+ public: // serialization
+ static const uint8_t ID = 1;
+#pragma pack(push)
+#pragma pack(1)
+ uint32_t m_ip;
+ uint16_t m_port;
+#pragma pack(pop)
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(m_ip)
+ KV_SERIALIZE(m_port)
+ if (!is_store)
+ const_cast<ipv4_network_address&>(this_ref).init_ids();
+ END_KV_SERIALIZE_MAP()
+ };
+ class network_address: public boost::shared_ptr<network_address_base>
+ {
+ public:
+ network_address() {}
+ network_address(ipv4_network_address *address): boost::shared_ptr<network_address_base>(address) {}
+ bool operator==(const network_address &other) const { return (*this)->operator==(*other); }
+ bool operator!=(const network_address &other) const { return (*this)->operator!=(*other); }
+ bool operator<(const network_address &other) const { return (*this)->operator<(*other); }
+ bool is_same_host(const network_address &other) const { return (*this)->is_same_host(*other); }
+ std::string str() const { return (*this) ? (*this)->str() : "<none>"; }
+ std::string host_str() const { return (*this) ? (*this)->host_str() : "<none>"; }
+ bool is_loopback() const { return (*this)->is_loopback(); }
+ bool is_local() const { return (*this)->is_local(); }
+ const std::type_info &type() const { return typeid(**this); }
+ uint8_t get_type_id() const { return (*this)->get_type_id(); }
+ template<typename Type> Type &as() { if (type() != typeid(Type)) throw std::runtime_error("Bad type"); return *(Type*)get(); }
+ template<typename Type> const Type &as() const { if (type() != typeid(Type)) throw std::runtime_error("Bad type"); return *(const Type*)get(); }
+
+ BEGIN_KV_SERIALIZE_MAP()
+ uint8_t type = is_store ? this_ref.get_type_id() : 0;
+ epee::serialization::selector<is_store>::serialize(type, stg, hparent_section, "type");
+ switch (type)
+ {
+ case ipv4_network_address::ID:
+ if (!is_store)
+ const_cast<network_address&>(this_ref).reset(new ipv4_network_address(0, 0));
+ KV_SERIALIZE(as<ipv4_network_address>());
+ break;
+ default: MERROR("Unsupported network address type: " << type); return false;
+ }
+ END_KV_SERIALIZE_MAP()
+ };
+ inline bool create_network_address(network_address &address, const std::string &string, uint16_t default_port = 0)
+ {
+ uint32_t ip;
+ uint16_t port;
+ if (epee::string_tools::parse_peer_from_string(ip, port, string))
+ {
+ if (default_port && !port)
+ port = default_port;
+ address.reset(new ipv4_network_address(ip, port));
+ return true;
+ }
+ return false;
+ }
/************************************************************************/
/* */
/************************************************************************/
struct connection_context_base
{
const boost::uuids::uuid m_connection_id;
- const uint32_t m_remote_ip;
- const uint32_t m_remote_port;
+ const network_address m_remote_address;
const bool m_is_income;
const time_t m_started;
time_t m_last_recv;
@@ -64,12 +164,11 @@ namespace net_utils
double m_current_speed_up;
connection_context_base(boost::uuids::uuid connection_id,
- long remote_ip, int remote_port, bool is_income,
+ const network_address &remote_address, bool is_income,
time_t last_recv = 0, time_t last_send = 0,
uint64_t recv_cnt = 0, uint64_t send_cnt = 0):
m_connection_id(connection_id),
- m_remote_ip(remote_ip),
- m_remote_port(remote_port),
+ m_remote_address(remote_address),
m_is_income(is_income),
m_started(time(NULL)),
m_last_recv(last_recv),
@@ -81,8 +180,7 @@ namespace net_utils
{}
connection_context_base(): m_connection_id(),
- m_remote_ip(0),
- m_remote_port(0),
+ m_remote_address(new ipv4_network_address(0,0)),
m_is_income(false),
m_started(time(NULL)),
m_last_recv(0),
@@ -95,17 +193,17 @@ namespace net_utils
connection_context_base& operator=(const connection_context_base& a)
{
- set_details(a.m_connection_id, a.m_remote_ip, a.m_remote_port, a.m_is_income);
+ set_details(a.m_connection_id, a.m_remote_address, a.m_is_income);
return *this;
}
private:
template<class t_protocol_handler>
friend class connection;
- void set_details(boost::uuids::uuid connection_id, long remote_ip, int remote_port, bool is_income)
+ void set_details(boost::uuids::uuid connection_id, const network_address &remote_address, bool is_income)
{
this->~connection_context_base();
- new(this) connection_context_base(connection_id, remote_ip, remote_port, is_income);
+ new(this) connection_context_base(connection_id, remote_address, is_income);
}
};
@@ -135,7 +233,7 @@ namespace net_utils
std::string print_connection_context(const connection_context_base& ctx)
{
std::stringstream ss;
- ss << epee::string_tools::get_ip_string_from_int32(ctx.m_remote_ip) << ":" << ctx.m_remote_port << " " << epee::string_tools::get_str_from_guid_a(ctx.m_connection_id) << (ctx.m_is_income ? " INC":" OUT");
+ ss << ctx.m_remote_address->str() << " " << epee::string_tools::get_str_from_guid_a(ctx.m_connection_id) << (ctx.m_is_income ? " INC":" OUT");
return ss.str();
}
@@ -143,7 +241,7 @@ namespace net_utils
std::string print_connection_context_short(const connection_context_base& ctx)
{
std::stringstream ss;
- ss << epee::string_tools::get_ip_string_from_int32(ctx.m_remote_ip) << ":" << ctx.m_remote_port << (ctx.m_is_income ? " INC":" OUT");
+ ss << ctx.m_remote_address->str() << (ctx.m_is_income ? " INC":" OUT");
return ss.str();
}
diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h
index 16e60e99a..258caa49e 100644
--- a/contrib/epee/include/string_tools.h
+++ b/contrib/epee/include/string_tools.h
@@ -193,7 +193,7 @@ POP_WARNINGS
//----------------------------------------------------------------------------
bool get_ip_int32_from_string(uint32_t& ip, const std::string& ip_str);
//----------------------------------------------------------------------------
- inline bool parse_peer_from_string(uint32_t& ip, uint32_t& port, const std::string& addres)
+ inline bool parse_peer_from_string(uint32_t& ip, uint16_t& port, const std::string& addres)
{
//parse ip and address
std::string::size_type p = addres.find(':');