Skip to content

Commit

Permalink
Move socket_functions impl
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Dec 6, 2024
1 parent c9b8bc8 commit 84e08f0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
39 changes: 0 additions & 39 deletions nano/node/transport/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,45 +494,6 @@ bool nano::transport::socket_queue::empty () const
});
}

/*
* socket_functions
*/

boost::asio::ip::network_v6 nano::transport::socket_functions::get_ipv6_subnet_address (boost::asio::ip::address_v6 const & ip_address, std::size_t network_prefix)
{
return boost::asio::ip::make_network_v6 (ip_address, static_cast<unsigned short> (network_prefix));
}

boost::asio::ip::address nano::transport::socket_functions::first_ipv6_subnet_address (boost::asio::ip::address_v6 const & ip_address, std::size_t network_prefix)
{
auto range = get_ipv6_subnet_address (ip_address, network_prefix).hosts ();
debug_assert (!range.empty ());
return *(range.begin ());
}

boost::asio::ip::address nano::transport::socket_functions::last_ipv6_subnet_address (boost::asio::ip::address_v6 const & ip_address, std::size_t network_prefix)
{
auto range = get_ipv6_subnet_address (ip_address, network_prefix).hosts ();
debug_assert (!range.empty ());
return *(--range.end ());
}

std::size_t nano::transport::socket_functions::count_subnetwork_connections (
nano::transport::address_socket_mmap const & per_address_connections,
boost::asio::ip::address_v6 const & remote_address,
std::size_t network_prefix)
{
auto range = get_ipv6_subnet_address (remote_address, network_prefix).hosts ();
if (range.empty ())
{
return 0;
}
auto const first_ip = first_ipv6_subnet_address (remote_address, network_prefix);
auto const last_ip = last_ipv6_subnet_address (remote_address, network_prefix);
auto const counted_connections = std::distance (per_address_connections.lower_bound (first_ip), per_address_connections.upper_bound (last_ip));
return counted_connections;
}

/*
*
*/
Expand Down
10 changes: 0 additions & 10 deletions nano/node/transport/tcp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,4 @@ class tcp_socket final : public std::enable_shared_from_this<tcp_socket>
public: // Logging
virtual void operator() (nano::object_stream &) const;
};

using address_socket_mmap = std::multimap<boost::asio::ip::address, std::weak_ptr<tcp_socket>>;

namespace socket_functions
{
boost::asio::ip::network_v6 get_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
boost::asio::ip::address first_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
boost::asio::ip::address last_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
std::size_t count_subnetwork_connections (nano::transport::address_socket_mmap const &, boost::asio::ip::address_v6 const &, std::size_t);
}
}
39 changes: 39 additions & 0 deletions nano/node/transport/transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,42 @@ nano::stat::detail nano::to_stat_detail (boost::system::error_code const & ec)
return nano::stat::detail::other;
}
}

/*
* socket_functions
*/

boost::asio::ip::network_v6 nano::transport::socket_functions::get_ipv6_subnet_address (boost::asio::ip::address_v6 const & ip_address, std::size_t network_prefix)
{
return boost::asio::ip::make_network_v6 (ip_address, static_cast<unsigned short> (network_prefix));
}

boost::asio::ip::address nano::transport::socket_functions::first_ipv6_subnet_address (boost::asio::ip::address_v6 const & ip_address, std::size_t network_prefix)
{
auto range = get_ipv6_subnet_address (ip_address, network_prefix).hosts ();
debug_assert (!range.empty ());
return *(range.begin ());
}

boost::asio::ip::address nano::transport::socket_functions::last_ipv6_subnet_address (boost::asio::ip::address_v6 const & ip_address, std::size_t network_prefix)
{
auto range = get_ipv6_subnet_address (ip_address, network_prefix).hosts ();
debug_assert (!range.empty ());
return *(--range.end ());
}

std::size_t nano::transport::socket_functions::count_subnetwork_connections (
nano::transport::address_socket_mmap const & per_address_connections,
boost::asio::ip::address_v6 const & remote_address,
std::size_t network_prefix)
{
auto range = get_ipv6_subnet_address (remote_address, network_prefix).hosts ();
if (range.empty ())
{
return 0;
}
auto const first_ip = first_ipv6_subnet_address (remote_address, network_prefix);
auto const last_ip = last_ipv6_subnet_address (remote_address, network_prefix);
auto const counted_connections = std::distance (per_address_connections.lower_bound (first_ip), per_address_connections.upper_bound (last_ip));
return counted_connections;
}
10 changes: 10 additions & 0 deletions nano/node/transport/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ bool is_same_subnetwork (boost::asio::ip::address const &, boost::asio::ip::addr

// Unassigned, reserved, self
bool reserved_address (nano::endpoint const &, bool allow_local_peers = false);

using address_socket_mmap = std::multimap<boost::asio::ip::address, std::weak_ptr<tcp_socket>>;

namespace socket_functions
{
boost::asio::ip::network_v6 get_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
boost::asio::ip::address first_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
boost::asio::ip::address last_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
std::size_t count_subnetwork_connections (nano::transport::address_socket_mmap const &, boost::asio::ip::address_v6 const &, std::size_t);
}
}

namespace nano
Expand Down

0 comments on commit 84e08f0

Please sign in to comment.