Add IPv6 support

new cli options (RPC ones also apply to wallet):
  --p2p-bind-ipv6-address (default = "::")
  --p2p-bind-port-ipv6    (default same as ipv4 port for given nettype)
  --rpc-bind-ipv6-address (default = "::1")

  --p2p-use-ipv6          (default false)
  --rpc-use-ipv6          (default false)

  --p2p-require-ipv4      (default true, if ipv4 bind fails and this is
                           true, will not continue even if ipv6 bind
                           successful)
  --rpc-require-ipv4      (default true, description as above)

ipv6 addresses are to be specified as "[xx:xx:xx::xx:xx]:port" except
in the cases of the cli args for bind address.  For those the square
braces can be omitted.
This commit is contained in:
Thomas Winget 2019-04-10 18:34:30 -04:00
parent 8adde33e01
commit 155475d971
No known key found for this signature in database
GPG key ID: 58131A160789E630
20 changed files with 805 additions and 113 deletions

View file

@ -34,28 +34,69 @@
namespace net
{
void get_network_address_host_and_port(const std::string& address, std::string& host, std::string& port)
{
// require ipv6 address format "[addr:addr:addr:...:addr]:port"
if (address.find(']') != std::string::npos)
{
host = address.substr(1, address.rfind(']') - 1);
if ((host.size() + 2) < address.size())
{
port = address.substr(address.rfind(':') + 1);
}
}
else
{
host = address.substr(0, address.rfind(':'));
if (host.size() < address.size())
{
port = address.substr(host.size() + 1);
}
}
}
expect<epee::net_utils::network_address>
get_network_address(const boost::string_ref address, const std::uint16_t default_port)
{
const boost::string_ref host = address.substr(0, address.rfind(':'));
std::string host_str = "";
std::string port_str = "";
if (host.empty())
bool ipv6 = false;
get_network_address_host_and_port(std::string(address), host_str, port_str);
boost::string_ref host_str_ref(host_str);
boost::string_ref port_str_ref(port_str);
if (host_str.empty())
return make_error_code(net::error::invalid_host);
if (host.ends_with(".onion"))
if (host_str_ref.ends_with(".onion"))
return tor_address::make(address, default_port);
if (host.ends_with(".i2p"))
if (host_str_ref.ends_with(".i2p"))
return i2p_address::make(address, default_port);
boost::system::error_code ec;
boost::asio::ip::address_v6 v6 = boost::asio::ip::make_address_v6(host_str, ec);
ipv6 = !ec;
std::uint16_t port = default_port;
if (host.size() < address.size())
if (port_str.size())
{
if (!epee::string_tools::get_xtype_from_string(port, std::string{address.substr(host.size() + 1)}))
if (!epee::string_tools::get_xtype_from_string(port, port_str))
return make_error_code(net::error::invalid_port);
}
std::uint32_t ip = 0;
if (epee::string_tools::get_ip_int32_from_string(ip, std::string{host}))
return {epee::net_utils::ipv4_network_address{ip, port}};
if (ipv6)
{
return {epee::net_utils::ipv6_network_address{v6, port}};
}
else
{
std::uint32_t ip = 0;
if (epee::string_tools::get_ip_int32_from_string(ip, host_str))
return {epee::net_utils::ipv4_network_address{ip, port}};
}
return make_error_code(net::error::unsupported_address);
}