1Lite(3) User Contributed Perl Documentation Lite(3)
2
3
4
6 NetAddr::IP::Lite - Manages IPv4 and IPv6 addresses and subnets
7
9 use NetAddr::IP::Lite qw(
10 Zeros
11 Ones
12 V4mask
13 V4net
14 :aton DEPRECATED !
15 :old_nth
16 );
17
18 my $ip = new NetAddr::IP::Lite '127.0.0.1';
19 or from a packed IPv4 address
20 my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1'));
21 or from an octal filtered IPv4 address
22 my $ip = new_no NetAddr::IP::Lite '127.012.0.0';
23
24 print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ;
25
26 if ($ip->within(new NetAddr::IP::Lite "127.0.0.0", "255.0.0.0")) {
27 print "Is a loopback address\n";
28 }
29
30 # This prints 127.0.0.1/32
31 print "You can also say $ip...\n";
32
33 The following four functions return ipV6 representations of:
34
35 :: = Zeros();
36 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF = Ones();
37 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:: = V4mask();
38 ::FFFF:FFFF = V4net();
39
41 Un-tar the distribution in an appropriate directory and type:
42
43 perl Makefile.PL
44 make
45 make test
46 make install
47
48 NetAddr::IP::Lite depends on NetAddr::IP::Util which installs by
49 default with its primary functions compiled using Perl's XS extensions
50 to build a 'C' library. If you do not have a 'C' complier available or
51 would like the slower Pure Perl version for some other reason, then
52 type:
53
54 perl Makefile.PL -noxs
55 make
56 make test
57 make install
58
60 This module provides an object-oriented abstraction on top of IP
61 addresses or IP subnets, that allows for easy manipulations. Most of
62 the operations of NetAddr::IP are supported. This module will work
63 older versions of Perl and does not use Math::BigInt.
64
65 The internal representation of all IP objects is in 128 bit IPv6
66 notation. IPv4 and IPv6 objects may be freely mixed.
67
68 The supported operations are described below:
69
70 Overloaded Operators
71 Assignment ("=")
72 Has been optimized to copy one NetAddr::IP::Lite object to another
73 very quickly.
74
75 "->copy()"
76 The assignment ("=") operation is only put in to operation when the
77 copied object is further mutated by another overloaded operation.
78 See overload SPECIAL SYMBOLS FOR "use overload" for details.
79
80 "->copy()" actually creates a new object when called.
81
82 Stringification
83 An object can be used just as a string. For instance, the following
84 code
85
86 my $ip = new NetAddr::IP::Lite '192.168.1.123';
87 print "$ip\n";
88
89 Will print the string 192.168.1.123/32.
90
91 my $ip = new6 NetAddr::IP::Lite '192.168.1.123';
92 print "$ip\n";
93
94 Will print the string
95
96 Equality
97 You can test for equality with either "eq" or "==". "eq" allows the
98 comparison with arbitrary strings as well as NetAddr::IP::Lite
99 objects. The following example:
100
101 if (NetAddr::IP::Lite->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8')
102 { print "Yes\n"; }
103
104 Will print out "Yes".
105
106 Comparison with "==" requires both operands to be NetAddr::IP::Lite
107 objects.
108
109 In both cases, a true value is returned if the CIDR representation
110 of the operands is equal.
111
112 Comparison via >, <, >=, <=, <=> and "cmp"
113 Internally, all network objects are represented in 128 bit format.
114 The numeric representation of the network is compared through the
115 corresponding operation. Comparisons are tried first on the address
116 portion of the object and if that is equal then the NUMERIC cidr
117 portion of the masks are compared. This leads to the
118 counterintuitive result that
119
120 /24 > /16
121
122 Comparison should not be done on netaddr objects with different
123 CIDR as this may produce indeterminate - unexpected results, rather
124 the determination of which netblock is larger or smaller should be
125 done by comparing
126
127 $ip1->masklen <=> $ip2->masklen
128
129 Addition of a constant ("+")
130 Add a 32 bit signed constant to the address part of a NetAddr
131 object. This operation changes the address part to point so many
132 hosts above the current objects start address. For instance, this
133 code:
134
135 print NetAddr::IP::Lite->new('127.0.0.1') + 5;
136
137 will output 127.0.0.6/8. The address will wrap around at the
138 broadcast back to the network address. This code:
139
140 print NetAddr::IP::Lite->new('10.0.0.1/24') + 255;
141
142 outputs 10.0.0.0/24.
143
144 Returns the the unchanged object when the constant is missing or
145 out of range.
146
147 2147483647 <= constant >= -2147483648
148
149 Subtraction of a constant ("-")
150 The complement of the addition of a constant.
151
152 Difference ("-")
153 Returns the difference between the address parts of two
154 NetAddr::IP::Lite objects address parts as a 32 bit signed number.
155
156 Returns undef if the difference is out of range.
157
158 Auto-increment
159 Auto-incrementing a NetAddr::IP::Lite object causes the address
160 part to be adjusted to the next host address within the subnet. It
161 will wrap at the broadcast address and start again from the network
162 address.
163
164 Auto-decrement
165 Auto-decrementing a NetAddr::IP::Lite object performs exactly the
166 opposite of auto-incrementing it, as you would expect.
167
168 Methods
169 "->new([$addr, [ $mask|IPv6 ]])"
170 "->new6([$addr, [ $mask]])"
171 "->new_no([$addr, [ $mask]])"
172 "->new_from_aton($netaddr)"
173 The first two methods create a new address with the supplied
174 address in $addr and an optional netmask $mask, which can be
175 omitted to get a /32 or /128 netmask for IPv4 / IPv6 addresses
176 respectively.
177
178 The third method "new_no" is exclusively for IPv4 addresses and
179 filters improperly formatted dot quad strings for leading 0's that
180 would normally be interpreted as octal format by NetAddr per the
181 specifications for inet_aton.
182
183 new_from_aton takes a packed IPv4 address and assumes a /32 mask.
184 This function replaces the DEPRECATED :aton functionality which is
185 fundamentally broken.
186
187 "->new6" marks the address as being in ipV6 address space even if
188 the format would suggest otherwise.
189
190 i.e. ->new6('1.2.3.4') will result in ::102:304
191
192 addresses submitted to ->new in ipV6 notation will
193 remain in that notation permanently. i.e.
194 ->new('::1.2.3.4') will result in ::102:304
195 whereas new('1.2.3.4') would print out as 1.2.3.4
196
197 See "STRINGIFICATION" below.
198
199 $addr can be almost anything that can be resolved to an IP address
200 in all the notations I have seen over time. It can optionally
201 contain the mask in CIDR notation.
202
203 prefix notation is understood, with the limitation that the range
204 specified by the prefix must match with a valid subnet.
205
206 Addresses in the same format returned by "inet_aton" or
207 "gethostbyname" can also be understood, although no mask can be
208 specified for them. The default is to not attempt to recognize this
209 format, as it seems to be seldom used.
210
211 ###### DEPRECATED, will be remove in version 5 ############ To
212 accept addresses in that format, invoke the module as in
213
214 use NetAddr::IP::Lite ':aton'
215
216 ###### USE new_from_aton instead ##########################
217
218 If called with no arguments, 'default' is assumed.
219
220 $addr can be any of the following and possibly more...
221
222 n.n
223 n.n/mm
224 n.n.n
225 n.n.n/mm
226 n.n.n.n
227 n.n.n.n/mm 32 bit cidr notation
228 n.n.n.n/m.m.m.m
229 loopback, localhost, broadcast, any, default
230 x.x.x.x/host
231 0xABCDEF, 0b111111000101011110, (or a bcd number)
232 a netaddr as returned by 'inet_aton'
233
234 Any RFC1884 notation
235
236 ::n.n.n.n
237 ::n.n.n.n/mmm 128 bit cidr notation
238 ::n.n.n.n/::m.m.m.m
239 ::x:x
240 ::x:x/mmm
241 x:x:x:x:x:x:x:x
242 x:x:x:x:x:x:x:x/mmm
243 x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation
244 loopback, localhost, unspecified, any, default
245 ::x:x/host
246 0xABCDEF, 0b111111000101011110 within the limits
247 of perl's number resolution
248 123456789012 a 'big' bcd number i.e. Math::BigInt
249
250 If called with no arguments, 'default' is assumed.
251
252 "->broadcast()"
253 Returns a new object referring to the broadcast address of a given
254 subnet. The broadcast address has all ones in all the bit positions
255 where the netmask has zero bits. This is normally used to address
256 all the hosts in a given subnet.
257
258 "->network()"
259 Returns a new object referring to the network address of a given
260 subnet. A network address has all zero bits where the bits of the
261 netmask are zero. Normally this is used to refer to a subnet.
262
263 "->addr()"
264 Returns a scalar with the address part of the object as an IPv4 or
265 IPv6 text string as appropriate. This is useful for printing or for
266 passing the address part of the NetAddr::IP::Lite object to other
267 components that expect an IP address. If the object is an ipV6
268 address or was created using ->new6($ip) it will be reported in
269 ipV6 hex format otherwise it will be reported in dot quad format
270 only if it resides in ipV4 address space.
271
272 "->mask()"
273 Returns a scalar with the mask as an IPv4 or IPv6 text string as
274 described above.
275
276 "->masklen()"
277 Returns a scalar the number of one bits in the mask.
278
279 "->bits()"
280 Returns the width of the address in bits. Normally 32 for v4 and
281 128 for v6.
282
283 "->version()"
284 Returns the version of the address or subnet. Currently this can be
285 either 4 or 6.
286
287 "->cidr()"
288 Returns a scalar with the address and mask in CIDR notation. A
289 NetAddr::IP::Lite object stringifies to the result of this
290 function. (see comments about ->new6() and ->addr() for output
291 formats)
292
293 "->aton()"
294 Returns the address part of the NetAddr::IP::Lite object in the
295 same format as the "inet_aton()" or "ipv6_aton" function
296 respectively. If the object was created using ->new6($ip), the
297 address returned will always be in ipV6 format, even for addresses
298 in ipV4 address space.
299
300 "->range()"
301 Returns a scalar with the base address and the broadcast address
302 separated by a dash and spaces. This is called range notation.
303
304 "->numeric()"
305 When called in a scalar context, will return a numeric
306 representation of the address part of the IP address. When called
307 in an array contest, it returns a list of two elements. The first
308 element is as described, the second element is the numeric
309 representation of the netmask.
310
311 This method is essential for serializing the representation of a
312 subnet.
313
314 "$me->contains($other)"
315 Returns true when $me completely contains $other. False is returned
316 otherwise and "undef" is returned if $me and $other are not both
317 "NetAddr::IP::Lite" objects.
318
319 "$me->within($other)"
320 The complement of "->contains()". Returns true when $me is
321 completely contained within $other, undef if $me and $other are not
322 both "NetAddr::IP::Lite" objects.
323
324 "->first()"
325 Returns a new object representing the first usable IP address
326 within the subnet (ie, the first host address).
327
328 "->last()"
329 Returns a new object representing the last usable IP address within
330 the subnet (ie, one less than the broadcast address).
331
332 "->nth($index)"
333 Returns a new object representing the n-th usable IP address within
334 the subnet (ie, the n-th host address). If no address is available
335 (for example, when the network is too small for $index hosts),
336 "undef" is returned.
337
338 Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
339 implements "->nth($index)" and "->num()" exactly as the
340 documentation states. Previous versions behaved slightly
341 differently and not in a consistent manner.
342
343 To use the old behavior for "->nth($index)" and "->num()":
344
345 use NetAddr::IP::Lite qw(:old_nth);
346
347 old behavior:
348 NetAddr::IP->new('10/32')->nth(0) == undef
349 NetAddr::IP->new('10/32')->nth(1) == undef
350 NetAddr::IP->new('10/31')->nth(0) == undef
351 NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31
352 NetAddr::IP->new('10/30')->nth(0) == undef
353 NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30
354 NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30
355 NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30
356
357 Note that in each case, the broadcast address is represented in the
358 output set and that the 'zero'th index is alway undef.
359
360 new behavior:
361 NetAddr::IP->new('10/32')->nth(0) == 10.0.0.0/32
362 NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32
363 NetAddr::IP->new('10/31')->nth(0) == undef
364 NetAddr::IP->new('10/31')->nth(1) == undef
365 NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30
366 NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30
367 NetAddr::IP->new('10/30')->nth(2) == undef
368
369 Note that a /32 net always has 1 usable address while a /31 has
370 none since it has a network and broadcast address, but no host
371 addresses. The first index (0) returns the address immediately
372 following the network address.
373
374 "->num()"
375 Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
376 Returns the number of usable addresses IP addresses within the
377 subnet, not counting the broadcast or network address. Previous
378 versions returned th number of IP addresses not counting the
379 broadcast address.
380
381 To use the old behavior for "->nth($index)" and "->num()":
382
383 use NetAddr::IP::Lite qw(:old_nth);
384
386 Zeros
387 Ones
388 V4mask
389 V4net
390 :aton DEPRECATED
391 :old_nth
392
394 Luis E. MuA~Xoz <luismunoz@cpan.org>, Michael Robinton
395 <michael@bizsystems.com>
396
398 This software comes with the same warranty as perl itself (ie, none),
399 so by using it you accept any and all the liability.
400
402 This software is (c) Luis E. MuA~Xoz, 1999 - 2005
403 and (c) Michael Robinton, 2006 - 2008.
404
405 It can be used under the terms of the perl artistic license provided
406 that proper credit for the work of the author is preserved in the form
407 of this copyright notice and license for this module.
408
410 perl(1), NetAddr::IP(3), NetAddr::IP::Util(3)
411
412
413
414perl v5.10.1 2011-10-04 Lite(3)