Skip to content

Commit ff5d4ff

Browse files
committedFeb 9, 2019
Fix Address::isLocalhost algorithm
1 parent 7796a31 commit ff5d4ff

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed
 

Diff for: ‎src/network/address.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ bool Address::isLocalhost() const {
277277
static const unsigned char localhost_bytes[] = {
278278
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
279279
static const unsigned char mapped_ipv4_localhost[] = {
280-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1};
280+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 0};
281281

282282
auto addr = m_address.ipv6.sin6_addr.s6_addr;
283283

284284
return memcmp(addr, localhost_bytes, 16) == 0 ||
285-
memcmp(addr, mapped_ipv4_localhost, 16) == 0;
286-
} else {
287-
return m_address.ipv4.sin_addr.s_addr == 0x0100007F;
285+
memcmp(addr, mapped_ipv4_localhost, 13) == 0;
288286
}
287+
288+
return (m_address.ipv4.sin_addr.s_addr & 0xFF) == 0x7f;
289289
}

Diff for: ‎src/unittest/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set (UNITTEST_SRCS
22
${CMAKE_CURRENT_SOURCE_DIR}/test.cpp
3+
${CMAKE_CURRENT_SOURCE_DIR}/test_address.cpp
34
${CMAKE_CURRENT_SOURCE_DIR}/test_authdatabase.cpp
45
${CMAKE_CURRENT_SOURCE_DIR}/test_activeobject.cpp
56
${CMAKE_CURRENT_SOURCE_DIR}/test_areastore.cpp

Diff for: ‎src/unittest/test_address.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#include "test.h"
21+
22+
#include "log.h"
23+
#include "settings.h"
24+
#include "network/socket.h"
25+
26+
class TestAddress : public TestBase {
27+
public:
28+
TestAddress()
29+
{
30+
TestManager::registerTestModule(this);
31+
}
32+
33+
const char *getName() { return "TestAddress"; }
34+
35+
void runTests(IGameDef *gamedef);
36+
37+
void testIsLocalhost();
38+
};
39+
40+
static TestAddress g_test_instance;
41+
42+
void TestAddress::runTests(IGameDef *gamedef)
43+
{
44+
TEST(testIsLocalhost);
45+
}
46+
47+
void TestAddress::testIsLocalhost()
48+
{
49+
// v4
50+
UASSERT(Address(127, 0, 0, 1, 0).isLocalhost());
51+
UASSERT(Address(127, 254, 12, 99, 0).isLocalhost());
52+
UASSERT(Address(127, 188, 255, 247, 0).isLocalhost());
53+
UASSERT(!Address(126, 255, 255, 255, 0).isLocalhost());
54+
UASSERT(!Address(128, 0, 0, 0, 0).isLocalhost());
55+
UASSERT(!Address(1, 0, 0, 0, 0).isLocalhost());
56+
UASSERT(!Address(255, 255, 255, 255, 0).isLocalhost());
57+
UASSERT(!Address(36, 45, 99, 158, 0).isLocalhost());
58+
UASSERT(!Address(172, 45, 37, 68, 0).isLocalhost());
59+
60+
// v6
61+
std::unique_ptr<IPv6AddressBytes> ipv6Bytes(new IPv6AddressBytes());
62+
std::vector<u8> ipv6RawAddr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
63+
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
64+
UASSERT(Address(ipv6Bytes.get(), 0).isLocalhost())
65+
66+
ipv6RawAddr = {16, 34, 0, 0, 0, 0, 29, 0, 0, 0, 188, 0, 0, 0, 0, 14};
67+
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
68+
UASSERT(!Address(ipv6Bytes.get(), 0).isLocalhost())
69+
}

0 commit comments

Comments
 (0)
Please sign in to comment.