use Net::IP; my $ip = new Net::IP ('193.0.1/24') or die (Net::IP::Error()); print ("IP : ".$ip->ip()."\n"); print ("Sho : ".$ip->short()."\n"); print ("Bin : ".$ip->binip()."\n"); print ("Int : ".$ip->intip()."\n"); print ("Mask: ".$ip->mask()."\n"); print ("Last: ".$ip->last_ip()."\n"); print ("Len : ".$ip->prefixlen()."\n"); print ("Size: ".$ip->size()."\n"); print ("Type: ".$ip->iptype()."\n"); print ("Rev: ".$ip->reverse_ip()."\n");
$ip = new Net::IP ('193.0.1.46') || die ...
Or from a Classless Prefix (a /24 prefix is equivalent to a C class):
$ip = new Net::IP ('195.114.80/24') || die ...
Or from a range of addresses:
$ip = new Net::IP ('20.34.101.207 - 201.3.9.99') || die ...
Or from a address plus a number:
$ip = new Net::IP ('20.34.10.0 + 255') || die ...
The new() function accepts IPv4 and IPv6 addresses:
$ip = new Net::IP ('dead:beef::/32') || die ...
Optionnaly, the function can be passed the version of the IP. Otherwise, it tries to guess what the version is (see _is_ipv4() and _is_ipv6()).
$ip = new Net::IP ('195/8',4); # Class A
"$ip->set('130.23.1/24',4);"
Like new(), set() takes two arguments - a string used to build an IP address, prefix, or range, and optionally, the IP version of the considered address.
It returns an IP object on success, and undef on failure.
"@list = $ip->find_prefixes($other_ip));"
The operation can be one of: lt: less than (smaller than) le: smaller or equal to gt: greater than ge: greater or equal to
"if ($ip->bincomp('lt',$ip2) {...}"
"my $sum = $ip->binadd($ip2);"
"my $total = $ip->aggregate($ip2);"
"if ($ip->overlaps($ip2)==$IP_A_IN_B_OVERLAP) {...};"
my $ip = new Net::IP ('195.45.6.7 - 195.45.6.19') || die; # Loop do { print $ip->ip(), "\n"; } while (++$ip);
The ++ operator returns undef when the last address of the range is reached.
"$auth = ip-"auth ();>
The functions of the procedural interface are not exported by default. In order to import these functions, you need to modify the use statement for the module:
Params : IP address, IP version Returns : binary IP string on success, undef otherwise
"$binip = ip_iptobin ($ip,6);"
Params : binary IP, IP version Returns : IP address on success, undef otherwise
"$ip = ip_bintoip ($binip,6);"
Params : binary IP Returns : BigInt
"$bigint = new Math::BigInt (ip_bintoint($binip));"
Params : BigInt, IP version Returns : binary IP
"$binip = ip_inttobin ($bigint);"
Params : IP address Returns : 4, 6, undef(unable to determine)
"$version = ip_get_version ($ip)"
Params : IP address Returns : 1 (yes) or 0 (no)
"ip_is_ipv4($ip) and print "$ip is IPv4";"
Params : IP address Returns : 1 (yes) or 0 (no)
"ip_is_ipv6($ip) and print "$ip is IPv6";"
Params : IP address, IP version Returns : expanded IP address or undef on failure
"$ip = ip_expand_address ($ip,4);"
Params : Prefix length, IP version Returns : Binary Mask
"$mask = ip_get_mask ($len,6);"
Params : First binary IP, prefix length, IP version Returns : Binary IP
"$lastbin = ip_last_address_bin ($ip,$len,6);"
Params : Prefix Returns : IP, optionnaly length of prefix
"($ip,$len) = ip_splitprefix ($prefix)"
Params : Prefix, IP version Returns : First IP, last IP
"($ip1,$ip2) = ip_prefix_to_range ($prefix,6);"
Params : First binary IP, operator, Last binary IP Returns : 1 (yes), 0 (no), or undef (problem)
"ip_bincomp ($ip1,'lt',$ip2) == 1 or do {}"
Params : First binary IP, Last binary IP Returns : Binary sum or undef (problem)
"$binip = ip_binadd ($bin1,$bin2);"
Params : First binary IP, Last binary IP Returns : Length of prefix or undef (problem)
"$len = ip_get_prefix_length ($ip1,$ip2);"
Params : First IP (binary format), Last IP (binary format), IP version Returns : List of Prefixes or undef (problem)
The prefixes returned have the form q.q.q.q/nn.
"@prefix = ip_range_to_prefix ($ip1,$ip2,6);"
Params : IP, Prefix length Returns : Compressed Prefix
"$ip = ip_compress_v4_prefix ($ip, $len);"
Params : IP, IP version Returns : Compressed IP or undef (problem)
"$ip = ip_compress_adress ($ip, $version);"
Params : Four binary IPs (begin of range 1,end1,begin2,end2), IP version $IP_PARTIAL_OVERLAP (ranges overlap) $IP_NO_OVERLAP (no overlap) $IP_A_IN_B_OVERLAP (range2 contains range1) $IP_B_IN_A_OVERLAP (range1 contains range2) $IP_IDENTICAL (ranges are identical) undef (problem)
"(ip_is_overlap($rb1,$re1,$rb2,$re2,4) eq $IP_A_IN_B_OVERLAP) and do {};"
Params : IPv6 Returns : IPv4 string or undef (not found)
"$ip4 = ip_get_embedded($ip6);"
Params : Mask Returns : 1 or undef (invalid)
"ip_check_mask($binmask) or do {};"
Checks if mask has only 1s followed by 0s.
Params : 1st range (1st IP, Last IP), last range (1st IP, last IP), IP version Returns : prefix or undef (invalid)
"$prefix = ip_aggregate ($bip1,$eip1,$bip2,$eip2) || die ..."
Params: binary IP Returns: type as of the following table or undef (invalid ip)
See RFC 5735 and RFC 6598
Address Block Present Use Reference ------------------------------------------------------------------- 0.0.0.0/8 ``This'' Network RFC 1122 PRIVATE 10.0.0.0/8 Private-Use Networks RFC 1918 PRIVATE 100.64.0.0/10 CGN Shared Address Space RFC 6598 SHARED 127.0.0.0/8 Loopback RFC 1122 LOOPBACK 169.254.0.0/16 Link Local RFC 3927 LINK-LOCAL 172.16.0.0/12 Private-Use Networks RFC 1918 PRIVATE 192.0.0.0/24 IETF Protocol Assignments RFC 5736 RESERVED 192.0.2.0/24 TEST-NET-1 RFC 5737 TEST-NET 192.88.99.0/24 6to4 Relay Anycast RFC 3068 6TO4-RELAY 192.168.0.0/16 Private-Use Networks RFC 1918 PRIVATE 198.18.0.0/15 Network Interconnect Device Benchmark Testing RFC 2544 RESERVED 198.51.100.0/24 TEST-NET-2 RFC 5737 TEST-NET 203.0.113.0/24 TEST-NET-3 RFC 5737 TEST-NET 224.0.0.0/4 Multicast RFC 3171 MULTICAST 240.0.0.0/4 Reserved for Future Use RFC 1112 RESERVED 255.255.255.255/32 Limited Broadcast RFC 919 BROADCAST RFC 922
Params: binary ip Returns: type as of the following table or undef (invalid)
See IANA Internet Protocol Version 6 Address Space <http://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.txt> and IANA IPv6 Special Purpose Address Registry <http://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.txt>
Prefix Allocation Reference ------------------------------------------------------------- 0000::/8 Reserved by IETF [RFC4291] RESERVED 0100::/8 Reserved by IETF [RFC4291] RESERVED 0200::/7 Reserved by IETF [RFC4048] RESERVED 0400::/6 Reserved by IETF [RFC4291] RESERVED 0800::/5 Reserved by IETF [RFC4291] RESERVED 1000::/4 Reserved by IETF [RFC4291] RESERVED 2000::/3 Global Unicast [RFC4291] GLOBAL-UNICAST 4000::/3 Reserved by IETF [RFC4291] RESERVED 6000::/3 Reserved by IETF [RFC4291] RESERVED 8000::/3 Reserved by IETF [RFC4291] RESERVED A000::/3 Reserved by IETF [RFC4291] RESERVED C000::/3 Reserved by IETF [RFC4291] RESERVED E000::/4 Reserved by IETF [RFC4291] RESERVED F000::/5 Reserved by IETF [RFC4291] RESERVED F800::/6 Reserved by IETF [RFC4291] RESERVED FC00::/7 Unique Local Unicast [RFC4193] UNIQUE-LOCAL-UNICAST FE00::/9 Reserved by IETF [RFC4291] RESERVED FE80::/10 Link Local Unicast [RFC4291] LINK-LOCAL-UNICAST FEC0::/10 Reserved by IETF [RFC3879] RESERVED FF00::/8 Multicast [RFC4291] MULTICAST
Prefix Assignment Reference --------------------------------------------------------------------- ::1/128 Loopback Address [RFC4291] UNSPECIFIED ::/128 Unspecified Address [RFC4291] LOOPBACK ::FFFF:0:0/96 IPv4-mapped Address [RFC4291] IPV4MAP 0100::/64 Discard-Only Prefix [RFC6666] DISCARD 2001:0000::/32 TEREDO [RFC4380] TEREDO 2001:0002::/48 BMWG [RFC5180] BMWG 2001:db8::/32 Documentation Prefix [RFC3849] DOCUMENTATION 2001:10::/28 ORCHID [RFC4843] ORCHID 2002::/16 6to4 [RFC3056] 6TO4 FC00::/7 Unique-Local [RFC4193] UNIQUE-LOCAL-UNICAST FE80::/10 Linked-Scoped Unicast [RFC4291] LINK-LOCAL-UNICAST FF00::/8 Multicast [RFC4291] MULTICAST
Params : Binary IP to test, IP version (defaults to 6) Returns : type (see ip_iptypev4 and ip_iptypev6 for details) or undef (invalid)
Params : binary IP, length of prefix, IP version Returns : 1 or undef (invalid)
Checks if the variant part of a prefix only has 0s, and the length is correct.
"ip_check_prefix ($ip,$len,$ipv) or do {};"
Params : IP, length of prefix, IP version Returns : Reverse name or undef (error)
"$reverse = ip_reverse ($ip);"
Params : Data String (Single IP, Range, Prefix) Returns : ip1, ip2 (if range/prefix) or undef (error)
"($ip1,$ip2) = ip_normalize ($data);"
Params : IP, version Returns : Auth info (RI for RIPE, AR for ARIN, etc)
"$auth = ip_auth ($ip,4);"
Original IPv4 code by Monica Cortes Sack <mcortes@ripe.net>.
Original IPv6 code by Lee Wilmot <lee@ripe.net>.