From 2729237ae9435347035ba114dfda6958a4aaf4e8 Mon Sep 17 00:00:00 2001 From: Enrico Schiattarella Date: Mon, 23 Jan 2017 12:23:32 -0800 Subject: [PATCH] Add a comment regarding the fix for "0.0.0.0/0" subnet --- source/common/network/utility.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/common/network/utility.cc b/source/common/network/utility.cc index de16b7319b64..377f4159c9df 100644 --- a/source/common/network/utility.cc +++ b/source/common/network/utility.cc @@ -29,6 +29,8 @@ IpWhiteList::IpWhiteList(const Json::Object& config) { throw EnvoyException(fmt::format("invalid ipv4/mask combo '{}' (invalid IP address)", entry)); } + // "0.0.0.0/0" is a valid subnet that contains all possible IPv4 addresses, + // so mask can be equal to 0 uint64_t mask; if (!StringUtil::atoul(parts[1].c_str(), mask) || mask > 32) { throw EnvoyException( @@ -37,6 +39,9 @@ IpWhiteList::IpWhiteList(const Json::Object& config) { Ipv4Entry white_list_entry; white_list_entry.ipv4_address_ = ntohl(addr.s_addr); + // The 1ULL below makes sure that the RHS is computed as a 64-bit value, so that we do not + // over-shift to the left when mask = 0. The assignment to ipv4_mask_ then truncates + // the value back to 32 bits. white_list_entry.ipv4_mask_ = ~((1ULL << (32 - mask)) - 1); // Check to make sure applying the mask to the address equals the address. This can prevent