#!/usr/bin/perl -w use strict; use Socket; sub subnet_matcher { @_ > 1 and goto &multi_matcher; my ($net, $mask) = split m[/], shift; return ipv4_matcher($net, $mask); } sub ipv4_matcher { my ($net, $mask) = @_; $net = inet_aton($net); $mask = inet_aton($mask); my $masked_net = $net & $mask; return sub { ((inet_aton(shift) // return !1) & $mask) eq $masked_net }; } sub multi_matcher { my @nets = map subnet_matcher($_), @_; return sub { $_->($_[0]) and return 1 for @nets; return !!0; } } sub main { my $ip = "65.181.207.232"; my @true = qw(10.96.2.0/255.255.254.0 10.123.50.0/255.255.255.0 72.24.196.0/255.255.255.0 72.24.137.192/255.255.255.192 10.122.50.0/255.255.255.0 65.181.207.128/255.255.255.128); my @false = qw(10.96.2.0/255.255.254.0 10.123.50.0/255.255.255.0 72.24.196.0/255.255.255.0 72.24.137.192/255.255.255.192 10.122.50.0/255.255.255.0); my $matcherTrue = subnet_matcher(@true); my $matcherFalse = subnet_matcher(@false); print "matcherTrue = " . ($matcherTrue->($ip) ? "yes\n" : "no\n"); print "matcherFalse = " . ($matcherFalse->($ip) ? "yes\n" : "no\n"); } main;