1Data::Validate::IP(3) User Contributed Perl DocumentationData::Validate::IP(3)
2
3
4
6 Data::Validate::IP - IPv4 and IPv6 validation methods
7
9 version 0.27
10
12 use Data::Validate::IP qw(is_ipv4 is_ipv6);
13
14 my $suspect = '1.2.3.4';
15 if (is_ipv4($suspect)) {
16 print "Looks like an IPv4 address";
17 }
18 else {
19 print "Not an IPv4 address\n";
20 }
21
22 $suspect = '::1234';
23 if (is_ipv6($suspect)) {
24 print "Looks like an IPv6 address";
25 }
26 else {
27 print "Not an IPv6 address\n";
28 }
29
31 This module provides a number IP address validation subs that both
32 validate and untaint their input. This includes both basic validation
33 ("is_ipv4()" and "is_ipv6()") and special cases like checking whether
34 an address belongs to a specific network or whether an address is
35 public or private (reserved).
36
38 All of the functions below are exported by default.
39
40 All functions return an untainted value if the test passes and undef if
41 it fails. In theory, this means that you should always check for a
42 defined status explicitly but in practice there are no valid IP
43 addresses where the string form evaluates to false in Perl.
44
45 Note that none of these functions actually attempt to test whether the
46 given IP address is routable from your device; they are purely semantic
47 checks.
48
49 is_ipv4($ip), is_ipv6($ip), is_ip($ip)
50 These functions simply check whether the address is a valid IPv4 or
51 IPv6 address.
52
53 is_innet_ipv4($ip, $network)
54 This subroutine checks whether the address belongs to the given IPv4
55 network. The $network argument can either be a string in CIDR notation
56 like "15.0.15.0/24" or a NetAddr::IP object.
57
58 This subroutine used to accept many more forms of network
59 specifications (anything Net::Netmask accepts) but this has been
60 deprecated.
61
62 is_unroutable_ipv4($ip)
63 This subroutine checks whether the address belongs to any of several
64 special use IPv4 networks - "0.0.0.0/8", "100.64.0.0/10",
65 "192.0.0.0/29", "198.18.0.0/15", "240.0.0.0/4" - as defined by RFC 5735
66 <http://tools.ietf.org/html/rfc5735>, RFC 6333
67 <http://tools.ietf.org/html/rfc6333>, and RFC 6958
68 <http://tools.ietf.org/html/rfc6598>.
69
70 Arguably, these should be broken down further but this subroutine will
71 always exist for backwards compatibility.
72
73 is_private_ipv4($ip)
74 This subroutine checks whether the address belongs to any of the
75 private IPv4 networks - "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"
76 - as defined by RFC 5735 <http://tools.ietf.org/html/rfc5735>.
77
78 is_loopback_ipv4($ip)
79 This subroutine checks whether the address belongs to the IPv4 loopback
80 network - "127.0.0.0/8" - as defined by RFC 5735
81 <http://tools.ietf.org/html/rfc5735>.
82
83 is_linklocal_ipv4($ip)
84 This subroutine checks whether the address belongs to the IPv4 link
85 local network - "169.254.0.0/16" - as defined by RFC 5735
86 <http://tools.ietf.org/html/rfc5735>.
87
88 is_testnet_ipv4($ip)
89 This subroutine checks whether the address belongs to any of the IPv4
90 TEST-NET networks for use in documentation and example code -
91 "192.0.2.0/24", "198.51.100.0/24", and "203.0.113.0/24" - as defined by
92 RFC 5735 <http://tools.ietf.org/html/rfc5735>.
93
94 is_anycast_ipv4($ip)
95 This subroutine checks whether the address belongs to the 6to4 relay
96 anycast network - "192.88.99.0/24" - as defined by RFC 5735
97 <http://tools.ietf.org/html/rfc5735>.
98
99 is_multicast_ipv4($ip)
100 This subroutine checks whether the address belongs to the IPv4
101 multicast network - "224.0.0.0/4" - as defined by RFC 5735
102 <http://tools.ietf.org/html/rfc5735>.
103
104 is_loopback_ipv6($ip)
105 This subroutine checks whether the address is the IPv6 loopback address
106 - "::1/128" - as defined by RFC 4291
107 <http://tools.ietf.org/html/rfc4291>.
108
109 is_ipv4_mapped_ipv6($ip)
110 This subroutine checks whether the address belongs to the IPv6
111 IPv4-mapped address network - "::ffff:0:0/96" - as defined by RFC 4291
112 <http://tools.ietf.org/html/rfc4291>.
113
114 is_discard_ipv6($ip)
115 This subroutine checks whether the address belongs to the IPv6 discard
116 prefix network - "100::/64" - as defined by RFC 6666
117 <http://tools.ietf.org/html/rfc6666>.
118
119 is_special_ipv6($ip)
120 This subroutine checks whether the address belongs to the IPv6 special
121 network - "2001::/23" - as defined by RFC 2928
122 <http://tools.ietf.org/html/rfc2928>.
123
124 is_teredo_ipv6($ip)
125 This subroutine checks whether the address belongs to the IPv6 TEREDO
126 network - "2001::/32" - as defined by RFC 4380
127 <http://tools.ietf.org/html/rfc4380>.
128
129 Note that this network is a subnet of the larger special network at
130 "2001::/23".
131
132 is_orchid_ipv6($ip)
133 This subroutine checks whether the address belongs to the IPv6 ORCHID
134 network - "2001::/32" - as defined by RFC 4380
135 <http://tools.ietf.org/html/rfc4380>.
136
137 Note that this network is a subnet of the larger special network at
138 "2001::/23".
139
140 This network is currently scheduled to be returned to the special pool
141 in March of 2014 unless the IETF extends its use. If that happens this
142 subroutine will continue to exist but will always return false.
143
144 is_documentation_ipv6($ip)
145 This subroutine checks whether the address belongs to the IPv6
146 documentation network - "2001:DB8::/32" - as defined by RFC 3849
147 <http://tools.ietf.org/html/rfc3849>.
148
149 is_private_ipv6($ip)
150 This subroutine checks whether the address belongs to the IPv6 private
151 network - "FC00::/7" - as defined by RFC 4193
152 <http://tools.ietf.org/html/rfc4193>.
153
154 is_linklocal_ipv6($ip)
155 This subroutine checks whether the address belongs to the IPv6 link-
156 local unicast network - "FE80::/10" - as defined by RFC 4291
157 <http://tools.ietf.org/html/rfc4291>.
158
159 is_multicast_ipv6($ip)
160 This subroutine checks whether the address belongs to the IPv6
161 multicast network - "FF00::/8" - as defined by RFC 4291
162 <http://tools.ietf.org/html/rfc4291>.
163
164 is_public_ipv4($ip), is_public_ipv6($ip), is_public_ip($ip)
165 These subroutines check whether the given IP address belongs to any of
166 the special case networks defined previously. Note that this is not
167 simply the opposite of checking "is_private_ipv4()" or
168 "is_private_ipv6()". The private networks are a subset of all the
169 special case networks.
170
171 is_linklocal_ip($ip)
172 This subroutine checks whether the address belongs to the IPv4 or IPv6
173 link-local unicast network.
174
175 is_loopback_ip($ip)
176 This subroutine checks whether the address is the IPv4 or IPv6 loopback
177 address.
178
179 is_multicast_ip($ip)
180 This subroutine checks whether the address belongs to the IPv4 or IPv6
181 multicast network.
182
183 is_private_ip($ip)
184 This subroutine checks whether the address belongs to the IPv4 or IPv6
185 private network.
186
188 This module can also be used as a class. You can call
189 "Data::Validate::IP->new()" to get an object and then call any of the
190 validation subroutines as methods on that object. This is somewhat
191 pointless since the object will never contain any state but this
192 interface is kept for backwards compatibility.
193
195 IPv4
196
197 [RFC 5735] [RFC 1918]
198
199 IPv6
200
201 [RFC 2460] [RFC 4193] [RFC 4291] [RFC 6434]
202
204 Thanks to Richard Sonnen <sonnen@richardsonnen.com> for writing the
205 Data::Validate module.
206
207 Thanks to Matt Dainty <matt@bodgit-n-scarper.com> for adding the
208 "is_multicast_ipv4()" and "is_linklocal_ipv4()" code.
209
211 Please report any bugs or feature requests to
212 "bug-data-validate-ip@rt.cpan.org", or through the web interface at
213 <http://rt.cpan.org>. I will be notified, and then you'll automatically
214 be notified of progress on your bug as I make changes.
215
216 Bugs may be submitted through
217 <https://github.com/houseabsolute/Data-Validate-IP/issues>.
218
220 · Neil Neely <neil@neely.cx>
221
222 · Dave Rolsky <autarch@urth.org>
223
225 Gregory Oschwald <goschwald@maxmind.com>
226
228 This software is copyright (c) 2016 by Neil Neely.
229
230 This is free software; you can redistribute it and/or modify it under
231 the same terms as the Perl 5 programming language system itself.
232
233
234
235perl v5.32.0 2020-07-28 Data::Validate::IP(3)