From f8dd43311078b92f4601565ae45441cfd01c82ad Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Fri, 1 Jun 2018 21:09:46 +0100 Subject: [PATCH 1/2] epee: fix detection of 172.16.0.0/172.31.255.255 local IP range --- contrib/epee/include/net/local_ip.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/epee/include/net/local_ip.h b/contrib/epee/include/net/local_ip.h index 0d458963c..52c5855b9 100644 --- a/contrib/epee/include/net/local_ip.h +++ b/contrib/epee/include/net/local_ip.h @@ -48,7 +48,7 @@ namespace epee if( (ip | 0xffffff00) == 0xffffffac) { - uint32_t second_num = (ip << 8) & 0xff000000; + uint32_t second_num = (ip >> 8) & 0xff; if(second_num >= 16 && second_num <= 31 ) return true; } From 5db9e3c28e6aa96b673dbde6cf0e4eb5de8771de Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Fri, 1 Jun 2018 21:10:20 +0100 Subject: [PATCH 2/2] unit_tests: add tests for local IP range detection --- tests/unit_tests/epee_utils.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 3969f50f6..3474000d8 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -45,6 +45,7 @@ #include "boost/archive/portable_binary_oarchive.hpp" #include "hex.h" #include "net/net_utils_base.h" +#include "net/local_ip.h" #include "p2p/net_peerlist_boost_serialization.h" #include "span.h" #include "string_tools.h" @@ -648,3 +649,34 @@ TEST(NetUtils, NetworkAddress) EXPECT_THROW(address1.as(), std::bad_cast); EXPECT_NO_THROW(address1.as()); } + +static bool is_local(const char *s) +{ + uint32_t ip; + CHECK_AND_ASSERT_THROW_MES(epee::string_tools::get_ip_int32_from_string(ip, s), std::string("Invalid IP address: ") + s); + return epee::net_utils::is_ip_local(ip); +} + +TEST(NetUtils, PrivateRanges) +{ + ASSERT_EQ(is_local("10.0.0.0"), true); + ASSERT_EQ(is_local("10.255.0.0"), true); + ASSERT_EQ(is_local("127.0.0.0"), false); // loopback is not considered local + ASSERT_EQ(is_local("192.167.255.255"), false); + ASSERT_EQ(is_local("192.168.0.0"), true); + ASSERT_EQ(is_local("192.168.255.255"), true); + ASSERT_EQ(is_local("192.169.0.0"), false); + ASSERT_EQ(is_local("172.0.0.0"), false); + ASSERT_EQ(is_local("172.15.255.255"), false); + ASSERT_EQ(is_local("172.16.0.0"), true); + ASSERT_EQ(is_local("172.16.255.255"), true); + ASSERT_EQ(is_local("172.31.255.255"), true); + ASSERT_EQ(is_local("172.32.0.0"), false); + ASSERT_EQ(is_local("0.0.0.0"), false); + ASSERT_EQ(is_local("255.255.255.254"), false); + ASSERT_EQ(is_local("11.255.255.255"), false); + ASSERT_EQ(is_local("0.0.0.10"), false); + ASSERT_EQ(is_local("0.0.168.192"), false); + ASSERT_EQ(is_local("0.0.30.172"), false); + ASSERT_EQ(is_local("0.0.30.127"), false); +}