BitwiseIp.Block.contains-question-mark
contains-question-mark
, go back to BitwiseIp.Block module for more information.
Specs
Efficiently checks if block2
is a subset of block1
.
Thanks to BitwiseIp.Mask
, we encode masks as integers. So if mask A is less
than mask B, that means A had fewer leading bits, meaning the block will
contain more addresses than the block for B. Therefore, as a prerequisite,
we first check that block1
's mask is <=
block2
's mask. If not, then
there's no chance that block2
could be wholly contained in block1
.
Then, if block1
's range is wide enough, we can test an arbitrary IP from
block2
for membership in block1
. Its inclusion would imply that
everything else in block2
is also included, since block1
is wider. We
have a suitable address to test in the form of the :addr
field from
block2
. The membership check involves the same bitwise AND
+ integer
comparison as member?/2
.
If the blocks don't have matching protocols, this function returns false
.
Examples
iex> BitwiseIp.Block.parse!("1.0.0.0/8")
...> |> BitwiseIp.Block.contains?(BitwiseIp.Block.parse!("1.2.0.0/16"))
true
iex> BitwiseIp.Block.parse!("1.2.0.0/16")
...> |> BitwiseIp.Block.contains?(BitwiseIp.Block.parse!("1.0.0.0/8"))
false
iex> BitwiseIp.Block.parse!("1.2.0.0/16")
...> |> BitwiseIp.Block.contains?(BitwiseIp.Block.parse!("1.2.0.0/16"))
true
iex> BitwiseIp.Block.parse!("1.2.0.0/16")
...> |> BitwiseIp.Block.contains?(BitwiseIp.Block.parse!("2.3.0.0/16"))
false