diff --git a/src/overlay/PeerBareAddress.cpp b/src/overlay/PeerBareAddress.cpp index 674e8856c6..1c8884e8be 100644 --- a/src/overlay/PeerBareAddress.cpp +++ b/src/overlay/PeerBareAddress.cpp @@ -138,7 +138,8 @@ PeerBareAddress::isPrivate() const unsigned long val = addr.to_ulong(); if (((val >> 24) == 10) // 10.x.y.z || ((val >> 20) == 2753) // 172.[16-31].x.y - || ((val >> 16) == 49320)) // 192.168.x.y + || ((val >> 16) == 49320) // 192.168.x.y + || ((val >> 16) == 43518)) // 169.254.x.y (link-local) { return true; } @@ -148,7 +149,14 @@ PeerBareAddress::isPrivate() const bool PeerBareAddress::isLocalhost() const { - return mIP == "127.0.0.1"; + asio::error_code ec; + asio::ip::address_v4 addr = asio::ip::address_v4::from_string(mIP, ec); + if (ec) + { + return false; + } + unsigned long val = addr.to_ulong(); + return (val >> 24) == 127; // 127.x.y.z } bool diff --git a/src/overlay/test/PeerManagerTests.cpp b/src/overlay/test/PeerManagerTests.cpp index 86586e3512..6a9517d4ee 100644 --- a/src/overlay/test/PeerManagerTests.cpp +++ b/src/overlay/test/PeerManagerTests.cpp @@ -87,6 +87,18 @@ TEST_CASE("private addresses", "[overlay][PeerManager]") CHECK(pa.isPrivate()); pa = PeerBareAddress("192.168.1.2", 15); CHECK(pa.isPrivate()); + pa = PeerBareAddress("169.254.169.254", 15); + CHECK(pa.isPrivate()); +} + +TEST_CASE("localhost addresses", "[overlay][PeerManager]") +{ + PeerBareAddress pa("1.2.3.4", 15); + CHECK(!pa.isLocalhost()); + pa = PeerBareAddress("127.0.0.1", 15); + CHECK(pa.isLocalhost()); + pa = PeerBareAddress("127.255.255.255", 15); + CHECK(pa.isLocalhost()); } TEST_CASE("create peer record", "[overlay][PeerManager]")