Skip to content

Commit

Permalink
src: compare IPv4 addresses in host byte order
Browse files Browse the repository at this point in the history
This commit updates compare_ipv4() to use the host byte
order.

PR-URL: #39096
Fixes: #39074
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
cjihrig authored and nodejs-github-bot committed Jun 22, 2021
1 parent c5cc3d4 commit b82a1a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/node_sockaddr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ SocketAddress::CompareResult compare_ipv4(
reinterpret_cast<const sockaddr_in*>(one.data());
const sockaddr_in* two_in =
reinterpret_cast<const sockaddr_in*>(two.data());
const uint32_t s_addr_one = ntohl(one_in->sin_addr.s_addr);
const uint32_t s_addr_two = ntohl(two_in->sin_addr.s_addr);

if (one_in->sin_addr.s_addr < two_in->sin_addr.s_addr)
if (s_addr_one < s_addr_two)
return SocketAddress::CompareResult::LESS_THAN;
else if (one_in->sin_addr.s_addr == two_in->sin_addr.s_addr)
else if (s_addr_one == s_addr_two)
return SocketAddress::CompareResult::SAME;
else
return SocketAddress::CompareResult::GREATER_THAN;
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ const util = require('util');
assert(!blockList.check('8592:757c:efaf:2fff:ffff:ffff:ffff:ffff', 'ipv6'));
}

{
// Regression test for https://github.com/nodejs/node/issues/39074
const blockList = new BlockList();

blockList.addRange('10.0.0.2', '10.0.0.10');

// IPv4 checks against IPv4 range.
assert(blockList.check('10.0.0.2'));
assert(blockList.check('10.0.0.10'));
assert(!blockList.check('192.168.0.3'));
assert(!blockList.check('2.2.2.2'));
assert(!blockList.check('255.255.255.255'));

// IPv6 checks against IPv4 range.
assert(blockList.check('::ffff:0a00:0002', 'ipv6'));
assert(blockList.check('::ffff:0a00:000a', 'ipv6'));
assert(!blockList.check('::ffff:c0a8:0003', 'ipv6'));
assert(!blockList.check('::ffff:0202:0202', 'ipv6'));
assert(!blockList.check('::ffff:ffff:ffff', 'ipv6'));
}

{
const blockList = new BlockList();
assert.throws(() => blockList.addRange('1.1.1.2', '1.1.1.1'), /ERR_INVALID_ARG_VALUE/);
Expand Down

0 comments on commit b82a1a1

Please sign in to comment.