File: README.md

package info (click to toggle)
libdata-validate-ip-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 1,069; sh: 23; makefile: 2
file content (288 lines) | stat: -rw-r--r-- 9,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# NAME

Data::Validate::IP - IPv4 and IPv6 validation methods

# VERSION

version 0.31

# SYNOPSIS

    use Data::Validate::IP qw(is_ipv4 is_ipv6);

    my $suspect = '1.2.3.4';
    if (is_ipv4($suspect)) {
        print "Looks like an IPv4 address";
    }
    else {
        print "Not an IPv4 address\n";
    }

    $suspect = '::1234';
    if (is_ipv6($suspect)) {
        print "Looks like an IPv6 address";
    }
    else {
        print "Not an IPv6 address\n";
    }

# DESCRIPTION

This module provides a number IP address validation subs that both validate and
untaint their input. This includes both basic validation (`is_ipv4()` and
`is_ipv6()`) and special cases like checking whether an address belongs to a
specific network or whether an address is public or private (reserved).

# USAGE AND SECURITY RECOMMENDATIONS

It's important to understand that if `is_ipv4($ip)`, `is_ipv6($ip)`, or
`is_ip($ip)` return false, then all other validation functions for that IP
address family will _also_ return false. So for example, if `is_ipv4($ip)`
returns false, then `is_private_ipv4($ip)` _and_ `is_public_ipv4($ip)` will
both also return false.

This means that simply calling `is_private_ipv4($ip)` by itself is not
sufficient if you are dealing with untrusted input. You should always check
`is_ipv4($ip)` as well. This applies as well when using IPv6 functions or
generic functions like `is_private_ip($ip)`.

There are security implications to this around certain oddly formed addresses.
Notably, an address like "010.0.0.1" is technically valid, but the operating
system will treat "010" as an octal number. That means that "010.0.0.1" is
equivalent to "8.0.0.1", _not_ "10.0.0.1".

However, this module's `is_ipv4($ip)` and `is_ip($ip)` functions will return
false for addresses like "010.0.0.1" which have octal components. And of course
that means that it also returns false for `is_private_ipv4($ip)` _and_
`is_public_ipv4($ip)`.

# FUNCTIONS

All of the functions below are exported by default.

All functions return an untainted value if the test passes and undef if it
fails. In theory, this means that you should always check for a defined status
explicitly but in practice there are no valid IP addresses where the string
form evaluates to false in Perl.

Note that none of these functions actually attempt to test whether the given IP
address is routable from your device; they are purely semantic checks.

## is\_ipv4($ip), is\_ipv6($ip), is\_ip($ip)

These functions simply check whether the address is a valid IPv4 or IPv6
address.

## is\_innet\_ipv4($ip, $network)

This subroutine checks whether the address belongs to the given IPv4 network.
The `$network` argument can either be a string in CIDR notation like
"15.0.15.0/24" or a [NetAddr::IP](https://metacpan.org/pod/NetAddr%3A%3AIP) object.

This subroutine used to accept many more forms of network specifications
(anything [Net::Netmask](https://metacpan.org/pod/Net%3A%3ANetmask) accepts) but this has been deprecated.

## is\_unroutable\_ipv4($ip)

This subroutine checks whether the address belongs to any of several special
use IPv4 networks - `0.0.0.0/8`, `100.64.0.0/10`, `192.0.0.0/29`,
`198.18.0.0/15`, `240.0.0.0/4` - as defined by [RFC
5735](http://tools.ietf.org/html/rfc5735), [RFC
6333](http://tools.ietf.org/html/rfc6333), and [RFC
6958](http://tools.ietf.org/html/rfc6598).

Arguably, these should be broken down further but this subroutine will always
exist for backwards compatibility.

## is\_private\_ipv4($ip)

This subroutine checks whether the address belongs to any of the private IPv4
networks - `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` - as defined by
[RFC 5735](http://tools.ietf.org/html/rfc5735).

## is\_loopback\_ipv4($ip)

This subroutine checks whether the address belongs to the IPv4 loopback network
\- `127.0.0.0/8` - as defined by [RFC
5735](http://tools.ietf.org/html/rfc5735).

## is\_linklocal\_ipv4($ip)

This subroutine checks whether the address belongs to the IPv4 link local
network - `169.254.0.0/16` - as defined by [RFC
5735](http://tools.ietf.org/html/rfc5735).

## is\_testnet\_ipv4($ip)

This subroutine checks whether the address belongs to any of the IPv4 TEST-NET
networks for use in documentation and example code - `192.0.2.0/24`,
`198.51.100.0/24`, and `203.0.113.0/24` - as defined by [RFC
5735](http://tools.ietf.org/html/rfc5735).

## is\_anycast\_ipv4($ip)

This subroutine checks whether the address belongs to the 6to4 relay anycast
network - `192.88.99.0/24` - as defined by [RFC
5735](http://tools.ietf.org/html/rfc5735).

## is\_multicast\_ipv4($ip)

This subroutine checks whether the address belongs to the IPv4 multicast
network - `224.0.0.0/4` - as defined by [RFC
5735](http://tools.ietf.org/html/rfc5735).

## is\_loopback\_ipv6($ip)

This subroutine checks whether the address is the IPv6 loopback address -
`::1/128` - as defined by [RFC 4291](http://tools.ietf.org/html/rfc4291).

## is\_ipv4\_mapped\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 IPv4-mapped
address network - `::ffff:0:0/96` - as defined by [RFC
4291](http://tools.ietf.org/html/rfc4291).

## is\_discard\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 discard prefix
network - `100::/64` - as defined by [RFC
6666](http://tools.ietf.org/html/rfc6666).

## is\_special\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 special network
\- `2001::/23` - as defined by [RFC 2928](http://tools.ietf.org/html/rfc2928).

## is\_teredo\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 TEREDO network -
`2001::/32` - as defined by [RFC 4380](http://tools.ietf.org/html/rfc4380).

Note that this network is a subnet of the larger special network at
`2001::/23`.

## is\_orchid\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 ORCHID network -
`2001::/32` - as defined by [RFC 4380](http://tools.ietf.org/html/rfc4380).

Note that this network is a subnet of the larger special network at
`2001::/23`.

This network is currently scheduled to be returned to the special pool in March
of 2014 unless the IETF extends its use. If that happens this subroutine will
continue to exist but will always return false.

## is\_documentation\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 documentation
network - `2001:DB8::/32` - as defined by [RFC
3849](http://tools.ietf.org/html/rfc3849).

## is\_unspecified\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 unspecified
network - `::0/128` - as defined by [RFC
4291](http://tools.ietf.org/html/rfc4291).. Note that the address in this subnet
is neither private nor public.

## is\_private\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 private network
\- `FC00::/7` - as defined by [RFC 4193](http://tools.ietf.org/html/rfc4193).

## is\_linklocal\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 link-local
unicast network - `FE80::/10` - as defined by [RFC
4291](http://tools.ietf.org/html/rfc4291).

## is\_multicast\_ipv6($ip)

This subroutine checks whether the address belongs to the IPv6 multicast
network - `FF00::/8` - as defined by [RFC
4291](http://tools.ietf.org/html/rfc4291).

## is\_public\_ipv4($ip), is\_public\_ipv6($ip), is\_public\_ip($ip)

These subroutines check whether the given IP address belongs to any of the
special case networks defined previously. Note that this is **not** simply the
opposite of checking `is_private_ipv4()` or `is_private_ipv6()`. The private
networks are a subset of all the special case networks.

## is\_linklocal\_ip($ip)

This subroutine checks whether the address belongs to the IPv4 or IPv6
link-local unicast network.

## is\_loopback\_ip($ip)

This subroutine checks whether the address is the IPv4 or IPv6 loopback
address.

## is\_multicast\_ip($ip)

This subroutine checks whether the address belongs to the IPv4 or IPv6
multicast network.

## is\_private\_ip($ip)

This subroutine checks whether the address belongs to the IPv4 or IPv6 private
network.

# OBJECT-ORIENTED INTERFACE

This module can also be used as a class. You can call `Data::Validate::IP->new()` to get an object and then call any of the
validation subroutines as methods on that object. This is somewhat pointless
since the object will never contain any state but this interface is kept for
backwards compatibility.

# SEE ALSO

IPv4

**\[RFC 5735\] \[RFC 1918\]**

IPv6

**\[RFC 2460\] \[RFC 4193\] \[RFC 4291\] \[RFC 6434\]**

# ACKNOWLEDGEMENTS

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.

# BUGS

Please report any bugs or feature requests to
`bug-data-validate-ip@rt.cpan.org`, or through the web interface at
[http://rt.cpan.org](http://rt.cpan.org). I will be notified, and then you'll automatically be
notified of progress on your bug as I make changes.

Bugs may be submitted at [https://github.com/houseabsolute/Data-Validate-IP/issues](https://github.com/houseabsolute/Data-Validate-IP/issues).

# SOURCE

The source code repository for Data-Validate-IP can be found at [https://github.com/houseabsolute/Data-Validate-IP](https://github.com/houseabsolute/Data-Validate-IP).

# AUTHORS

- Neil Neely <neil@neely.cx>
- Dave Rolsky <autarch@urth.org>

# CONTRIBUTOR

Gregory Oschwald <goschwald@maxmind.com>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2022 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.

The full text of the license can be found in the
`LICENSE` file included with this distribution.