NAME
Net::Subnet - Fast IP-in-subnet matcher for IPv4 and IPv6, CIDR or mask.
SYNOPSIS
use Net::Subnet;
# CIDR notation
my $is_rfc1918 = subnet_matcher(
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16"
);
# Subnet mask notation
my $is_rfc1918 = subnet_matcher(
"10.0.0.0/255.0.0.0",
"172.16.0.0/255.240.0.0",
"192.168.0.0/255.255.0.0",
);
print $is_rfc1918->("192.168.1.1") ? "yes" : "no"; # prints "yes"
print $is_rfc1918->("8.8.8.8") ? "yes" : "no"; # prints "no"
# Mixed IPv4 and IPv6
my $in_office_network = subnet_matcher(
"192.168.1.0/24", "2001:db8:1337::/48"
);
print $in_office_network->("192.168.1.1"); # prints 1
print $in_office_network->("2001:db8:dead:beef::5"); # prints nothing
my $classifier = subnet_classifier(
"192.168.1.0/24",
"2001:db8:1337::/48",
"10.0.0.0/255.0.0.0",
);
$x = $classifier->("192.168.1.250"); # $x is "192.168.1.0/24"
$x = $classifier->("2001:db8:1337::babe"); # $x is "2001:db8:1337::/48"
$x = $classifier->("10.2.127.1"); # $x is "10.0.0.0/255.0.0.0"
$x = $classifier->("8.8.8.8"); # $x is undef
# More specific subnets (smaller subnets) must be listed first
my @subnets = sort_subnets(
"192.168.0.0/24", # second
"192.168.0.1/32", # first
"192.168.0.0/16", # third
);
my $classifier = subnet_classifier(@subnets);
DESCRIPTION
This is a simple but fast pure Perl module for determining whether a given IP address is in a given set of IP subnets. It's iterative, and it doesn't use any fancy tries, but because it uses simple bitwise operations on strings it's still very fast.
All documented functions are exported by default.
Subnets have to be given in "address/mask" or "address/length" (CIDR) format. The Socket and Socket6 modules are used to normalise addresses, which means that any of the address formats supported by inet_aton and inet_pton can be used with Net::Subnet.
FUNCTIONS
subnet_matcher(@subnets)
Returns a reference to a function that returns true if the given IP address is in @subnets, false it it's not.
subnet_classifier(@subnets)
Returns a reference to a function that returns the element from @subnets that matches the given IP address, or undef if none matched.
sort_subnets(@subnets)
Returns @subnets in reverse order of prefix length and prefix; use this with subnet_matcher or subnet_classifier if your subnet list has overlapping ranges and it's not already sorted most-specific-first.
CAVEATS
No argument verification is done; garbage in, garbage out. If you give it hostnames, DNS may be used to resolve them, courtesy of the Socket and Socket6 modules.
AUTHOR
Juerd Waalboer <#####@juerd.nl>
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.