#15MediumNetwork Security· Stateless Packet Filtering
Network Packet Filter Simulation
Input format
rules: array of rule strings, packets: array of packet strings
Output format
Array of strings (`"allow"` or `"deny"`), one per packet
Examples
Input: rules = ["allow tcp 10.0.0.0/8 22-22", "deny any 0.0.0.0/0 0-65535"], packets = ["tcp 10.4.5.6 22", "tcp 10.4.5.6 80", "udp 10.4.5.6 22", "tcp 192.168.1.1 22"]
Output: ["allow", "deny", "deny", "deny"]
Only TCP traffic to port 22 from 10.0.0.0/8 matches the first rule. Everything else falls through to the catch-all deny.
Input: rules = ["deny tcp 192.168.1.0/24 0-65535", "allow tcp 192.168.0.0/16 80-443"], packets = ["tcp 192.168.1.20 443", "tcp 192.168.2.20 443", "tcp 192.168.2.20 8080", "udp 192.168.2.20 80"]
Output: ["deny", "allow", "deny", "deny"]
192.168.1.20 matches the deny rule first, even though the later allow rule would also match. 192.168.2.20:8080 matches neither rule and is denied by default.
Constraints
- 0 <= rules.length <= 100
- 1 <= packets.length <= 10^4
- 0 <= low <= high <= 65535, 0 <= port <= 65535
- 0 <= prefix length <= 32
- All IP addresses are valid dotted-quad IPv4 addresses.
Study first
- #networking
- #bit-manipulation
- #simulation
- #firewall
- #strings
