Data::Validate::IP - ipv4 and ipv6 validation methods
version 0.15
use Data::Validate::IP qw(is_ipv4 is_ipv6); if(is_ipv4($suspect)){ print "Looks like an ipv4 address"; } else { print "Not an ipv4 address\n"; } if(is_ipv6($suspect)){ print "Looks like an ipv6 address"; } else { print "Not an ipv6 address\n"; } # or as an object my $v = Data::Validate::IP->new(); die "not an ipv4 ip" unless ($v->is_ipv4('domain.com')); die "not an ipv6 ip" unless ($v->is_ipv6('domain.com'));
This module collects ip validation routines to make input validation, and untainting easier and more readable.
All functions return an untainted value if the test passes, and undef if it fails. This means that you should always check for a defined status explicitly. Don't assume the return will be true. (e.g. is_username('0'))
The value to test is always the first (and often only) argument.
All of the functions below are exported by default.
$obj = Data::Validate::IP->new();
Returns a Data::Validator::IP object. This lets you access all the validator function calls as methods without importing them into your namespace or using the clumsy Data::Validate::IP::function_name() format.
None
Returns a Data::Validate::IP object
is_ipv4($value); or $obj->is_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed ip address.
The potential ip to test.
Returns the untainted ip on success, undef on failure.
The function does not make any attempt to check whether an ip actually exists. It only looks to see that the format is appropriate.
is_ipv6($value);
is_innet_ipv4($value,$network); or $obj->is_innet_ipv4($value,$network);
Returns the untainted ip address if the test value appears to be a well-formed ip address inside of the network specified
The potential network the IP must be a part of. Functionality uses Net::Netmask and should be in the form:
'216.240.32.0/24' The preferred form. '216.240.32.0:255.255.255.0' '216.240.32.0-255.255.255.0' '216.240.32.0 - 216.240.32.255' '216.240.32.4' A /32 block. '216.240.32' Always a /24 block. '216.240' Always a /16 block. '140' Always a /8 block. '216.240.32/24' '216.240/16' 'default' 0.0.0.0/0 (the default route) '216.240.32.0#0.0.31.255' A hostmask (as used by Cisco access-lists).
Examples taken from Net::Netmask documentation. For more advanced network matching needs please see Net::Netmask.
The function does not make any attempt to check whether an ip actually exists.
is_private_ipv4($value); or $obj->is_private_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed private ip address.
10.0.0.0/8 - This block is set aside for use in private networks. Its intended use is documented in [RFC1918]. Addresses within this block should not appear on the public Internet. 172.16.0.0/12 - This block is set aside for use in private networks. Its intended use is documented in [RFC1918]. Addresses within this block should not appear on the public Internet. 192.168.0.0/16 - This block is set aside for use in private networks. Its intended use is documented in [RFC1918]. Addresses within this block should not appear on the public Internet.
is_loopback_ipv4($value); or $obj->is_loopback_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed loopback ip address.
127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher level protocol to an address anywhere within this block should loop back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback, but no addresses within this block should ever appear on any network anywhere [RFC1700, page 5].
is_testnet_ipv4($value); or $obj->is_testnet_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed testnet ip address.
192.0.2.0/24 - This block is assigned as "TEST-NET" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. Addresses within this block should not appear on the public Internet.
is_multicast_ipv4($value); or $obj->is_multicast_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed multicast ip address.
224.0.0.0/4 - This block, formerly known as the Class D address space, is allocated for use in IPv4 multicast address assignments. The IANA guidelines for assignments from this space are described in [RFC3171].
is_linklocal_ipv4($value); or $obj->is_linklocal_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed link-local ip address.
169.254.0.0/16 - This is the "link local" block. It is allocated for communication between hosts on a single link. Hosts obtain these addresses by auto-configuration, such as when a DHCP server may not be found.
is_unroutable_ipv4($value); or $obj->is_unroutable_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed unroutable ip address.
0.0.0.0/8 - Addresses in this block refer to source hosts on "this" network. Address 0.0.0.0/32 may be used as a source address for this host on this network; other addresses within 0.0.0.0/8 may be used to refer to specified hosts on this network ([RFC1122], Section 3.2.1.3). 192.0.0.0/24 - This block is reserved for IETF protocol assignments. At the time of writing this document, there are no current assignments. Allocation policy for future assignments is given in [RFC5736]. 198.18.0.0/15 - This block has been allocated for use in benchmark tests of network interconnect devices. [RFC2544] explains that this range was assigned to minimize the chance of conflict in case a testing device were to be accidentally connected to part of the Internet. Packets with source addresses from this range are not meant to be forwarded across the Internet. 198.51.100.0/24 - This block is assigned as "TEST-NET-2" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. As described in [RFC5737], addresses within this block do not legitimately appear on the public Internet and can be used without any coordination with IANA or an Internet registry. 203.0.113.0/24 - This block is assigned as "TEST-NET-3" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. As described in [RFC5737], addresses within this block do not legitimately appear on the public Internet and can be used without any coordination with IANA or an Internet registry. 240.0.0.0/4 - This block, formerly known as the Class E address space, is reserved for future use; see [RFC1112], Section 4.
is_public_ipv4($value); or $obj->is_public_ipv4($value);
Returns the untainted ip address if the test value appears to be a well-formed public ip address.
The function does not make any attempt to check whether an ip actually exists or could truly route. This is true for any non- private/testnet/loopback ip.
is_private_ipv6($value); or $obj->is_private_ipv6($value);
The default behavior of exterior routing protocol sessions between administrative routing regions must be to ignore receipt of and not advertise prefixes in the FC00::/7 block. A network operator may specifically configure prefixes longer than FC00::/7 for inter-site communication.
is_loopback_ipv6($value); or $obj->is_loopback_ipv6($value);
The unicast address 0:0:0:0:0:0:0:1 is called the loopback address. It may be used by a node to send an IPv6 packet to itself. It must not be assigned to any physical interface. It is treated as having Link-Local scope, and may be thought of as the Link-Local unicast address of a virtual interface (typically called the "loopback interface") to an imaginary link that goes nowhere.
is_multicast_ipv6($value); or $obj->is_multicast_ipv6($value);
An IPv6 multicast address is an identifier for a group of interfaces (typically on different nodes). An interface may belong to any number of multicast groups. Multicast addresses have the following format: | 8 | 4 | 4 | 112 bits | +------ -+----+----+---------------------------------------------+ |11111111|flgs|scop| group ID | +--------+----+----+---------------------------------------------+
is_linklocal_ipv6($value); or $obj->is_linklocal_ipv6($value);
Link-Local addresses are for use on a single link. Link-Local addresses have the following format: | 10 | | bits | 54 bits | 64 bits | +----------+-------------------------+----------------------------+ |1111111010| 0 | interface ID | +----------+-------------------------+----------------------------+ Link-Local addresses are designed to be used for addressing on a single link for purposes such as automatic address configuration, neighbor discovery, or when no routers are present.
is_special_ipv6($value); or $obj->is_special_ipv6($value);
Returns the untainted ip address if the test value appears to be a well-formed special purpose ip address.
The block of Sub-TLA IDs assigned to the IANA (i.e., 2001:0000::/29 - 2001:01F8::/29) is for assignment for testing and experimental usage to support activities such as the 6bone, and for new approaches like exchanges.
The whole block of special IPv6 addresses can be written simple as 2001::/23.
IPv4
[RFC 5735] [RFC 1918]
IPv6
[RFC 2460] [RFC 4193] [RFC 4291] [RFC 6434]
IPv6 Support is new, please test it thoroughly and report any bugs.
Thanks to Richard Sonnen <sonnen@richardsonnen.com> for writing the Data::Validate module.
Thanks to Matt Dainty <matt@bodgit-n-scarper.com> for adding the is_multicast_ipv4 and is_linklocal_ipv4 code.
Neil Neely <neil@neely.cx>
Dave Rolsky <autarch@urth.org>
This software is copyright (c) 2013 by Neil Neely.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Data::Validate::IP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Validate::IP
CPAN shell
perl -MCPAN -e shell install Data::Validate::IP
For more information on module installation, please visit the detailed CPAN module installation guide.