You are given a list of IPv4 access-control rules. Each rule consists of:
-
an action:
allow
or
deny
-
a CIDR block such as
192.168.0.0/16
You are also given a target IPv4 address such as 192.168.1.25.
Determine whether the target IP should be allowed or denied.
Use the following rule semantics so the problem is fully specified:
-
A rule matches if the target IP belongs to that CIDR block.
-
If multiple rules match, the rule with the
longest prefix length
takes precedence.
-
If multiple matching rules have the same prefix length, the
later
rule in the input overrides the earlier one.
-
If no rule matches, return
deny
.
Implement a function that takes a list of rules and one IPv4 address and returns the final decision.
You may assume all inputs are valid IPv4 addresses and valid CIDR blocks, with prefix lengths from 0 to 32.