diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-01-04 18:37:52 +0100 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2021-01-10 15:51:15 +0100 |
commit | 94d335da7f8232bc653c9b08b0a33b517b4c98ad (patch) | |
tree | 6a41720c136a83ad30c1a8dadde611629cb3f1d0 /src/netaddress.h | |
parent | bc8ada1c15345d14e324aee68488c8aa8a75cae0 (diff) |
net: allow CSubNet of non-IP networks
Allow creation of valid `CSubNet` objects of non-IP networks and only
match the single address they were created from (like /32 for IPv4 or
/128 for IPv6).
This fixes a deficiency in `CConnman::DisconnectNode(const CNetAddr& addr)`
and in `BanMan` which assume that creating a subnet from any address
using the `CSubNet(CNetAddr)` constructor would later match that address
only. Before this change a non-IP subnet would be invalid and would not
match any address.
Diffstat (limited to 'src/netaddress.h')
-rw-r--r-- | src/netaddress.h | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/netaddress.h b/src/netaddress.h index 29b2eaafeb..b9beb1e358 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -462,11 +462,33 @@ class CSubNet bool SanityCheck() const; public: + /** + * Construct an invalid subnet (empty, `Match()` always returns false). + */ CSubNet(); + + /** + * Construct from a given network start and number of bits (CIDR mask). + * @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is + * created. + * @param[in] mask CIDR mask, must be in [0, 32] for IPv4 addresses and in [0, 128] for + * IPv6 addresses. Otherwise an invalid subnet is created. + */ CSubNet(const CNetAddr& addr, uint8_t mask); + + /** + * Construct from a given network start and mask. + * @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is + * created. + * @param[in] mask Network mask, must be of the same type as `addr` and not contain 0-bits + * followed by 1-bits. Otherwise an invalid subnet is created. + */ CSubNet(const CNetAddr& addr, const CNetAddr& mask); - //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128) + /** + * Construct a single-host subnet. + * @param[in] addr The sole address to be contained in the subnet, can also be non-IPv[46]. + */ explicit CSubNet(const CNetAddr& addr); bool Match(const CNetAddr &addr) const; |