Skip to content

Commit 66553fe

Browse files
cjihrigtargos
authored andcommitted
src: compare IPv4 addresses in host byte order
This commit updates compare_ipv4() to use the host byte order. PR-URL: #39096 Fixes: #39074 Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 1543497 commit 66553fe

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Diff for: src/node_sockaddr.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ SocketAddress::CompareResult compare_ipv4(
159159
reinterpret_cast<const sockaddr_in*>(one.data());
160160
const sockaddr_in* two_in =
161161
reinterpret_cast<const sockaddr_in*>(two.data());
162+
const uint32_t s_addr_one = ntohl(one_in->sin_addr.s_addr);
163+
const uint32_t s_addr_two = ntohl(two_in->sin_addr.s_addr);
162164

163-
if (one_in->sin_addr.s_addr < two_in->sin_addr.s_addr)
165+
if (s_addr_one < s_addr_two)
164166
return SocketAddress::CompareResult::LESS_THAN;
165-
else if (one_in->sin_addr.s_addr == two_in->sin_addr.s_addr)
167+
else if (s_addr_one == s_addr_two)
166168
return SocketAddress::CompareResult::SAME;
167169
else
168170
return SocketAddress::CompareResult::GREATER_THAN;

Diff for: test/parallel/test-blocklist.js

+21
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,27 @@ const util = require('util');
209209
assert(!blockList.check('8592:757c:efaf:2fff:ffff:ffff:ffff:ffff', 'ipv6'));
210210
}
211211

212+
{
213+
// Regression test for https://github.com/nodejs/node/issues/39074
214+
const blockList = new BlockList();
215+
216+
blockList.addRange('10.0.0.2', '10.0.0.10');
217+
218+
// IPv4 checks against IPv4 range.
219+
assert(blockList.check('10.0.0.2'));
220+
assert(blockList.check('10.0.0.10'));
221+
assert(!blockList.check('192.168.0.3'));
222+
assert(!blockList.check('2.2.2.2'));
223+
assert(!blockList.check('255.255.255.255'));
224+
225+
// IPv6 checks against IPv4 range.
226+
assert(blockList.check('::ffff:0a00:0002', 'ipv6'));
227+
assert(blockList.check('::ffff:0a00:000a', 'ipv6'));
228+
assert(!blockList.check('::ffff:c0a8:0003', 'ipv6'));
229+
assert(!blockList.check('::ffff:0202:0202', 'ipv6'));
230+
assert(!blockList.check('::ffff:ffff:ffff', 'ipv6'));
231+
}
232+
212233
{
213234
const blockList = new BlockList();
214235
assert.throws(() => blockList.addRange('1.1.1.2', '1.1.1.1'), /ERR_INVALID_ARG_VALUE/);

0 commit comments

Comments
 (0)