1Lite(3)               User Contributed Perl Documentation              Lite(3)
2
3
4

NAME

6       NetAddr::IP::Lite - Manages IPv4 and IPv6 addresses and subnets
7

SYNOPSIS

9         use NetAddr::IP::Lite qw(
10               Zeros
11               Ones
12               V4mask
13               V4net
14               :aton
15               :old_nth
16         );
17
18         my $ip = new NetAddr::IP::Lite '127.0.0.1';
19
20         print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ;
21
22         if ($ip->within(new NetAddr::IP::Lite "127.0.0.0", "255.0.0.0")) {
23             print "Is a loopback address\n";
24         }
25
26                                       # This prints 127.0.0.1/32
27         print "You can also say $ip...\n";
28
29         The following four functions return ipV6 representations of:
30
31         ::                                       = Zeros();
32         FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF: = Ones();
33         FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::          = V4mask();
34         ::FFFF:FFFF                              = V4net();
35

INSTALLATION

37       Un-tar the distribution in an appropriate directory and type:
38
39               perl Makefile.PL
40               make
41               make test
42               make install
43
44       NetAddr::IP::Lite depends on NetAddr::IP::Util which installs by
45       default with its primary functions compiled using Perl's XS extensions
46       to build a 'C' library. If you do not have a 'C' complier available or
47       would like the slower Pure Perl version for some other reason, then
48       type:
49
50               perl Makefile.PL -noxs
51               make
52               make test
53               make install
54

DESCRIPTION

56       This module provides an object-oriented abstraction on top of IP
57       addresses or IP subnets, that allows for easy manipulations. Most of
58       the operations of NetAddr::IP are supported. This module will work
59       older versions of Perl and does not use Math::BigInt.
60
61       The internal representation of all IP objects is in 128 bit IPv6 nota‐
62       tion.  IPv4 and IPv6 objects may be freely mixed.
63
64       The supported operations are described below:
65
66       Overloaded Operators
67
68       Assignment ("=")
69           Has been optimized to copy one NetAddr::IP::Lite object to another
70           very quickly.
71
72       "->copy()"
73           The assignment ("=") operation is only put in to operation when the
74           copied object is further mutated by another overloaded operation.
75           See overload SPECIAL SYMBOLS FOR "use overload" for details.
76
77           "->copy()" actually creates a new object when called.
78
79       Stringification
80           An object can be used just as a string. For instance, the following
81           code
82
83                   my $ip = new NetAddr::IP::Lite '192.168.1.123';
84                   print "$ip\n";
85
86           Will print the string 192.168.1.123/32.
87
88                   my $ip = new6 NetAddr::IP::Lite '192.168.1.123';
89                   print "$ip\n";
90
91           Will print the string
92
93       Equality
94           You can test for equality with either "eq" or "==". "eq" allows the
95           comparison with arbitrary strings as well as NetAddr::IP::Lite
96           objects. The following example:
97
98               if (NetAddr::IP::Lite->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8')
99                  { print "Yes\n"; }
100
101           Will print out "Yes".
102
103           Comparison with "==" requires both operands to be NetAddr::IP::Lite
104           objects.
105
106           In both cases, a true value is returned if the CIDR representation
107           of the operands is equal.
108
109       Comparison via >, <, >=, <=, <=> and "cmp"
110           Internally, all network objects are represented in 128 bit format.
111           The numeric representation of the network is compared through the
112           corresponding operation. Comparisons are tried first on the address
113           portion of the object and if that is equal then the cidr portion of
114           the masks are compared.
115
116       Addition of a constant
117           Adding a constant to a NetAddr::IP::Lite object changes its address
118           part to point to the one so many hosts above the start address. For
119           instance, this code:
120
121               print NetAddr::IP::Lite->new('127.0.0.1') + 5;
122
123           will output 127.0.0.6/8. The address will wrap around at the broad‐
124           cast back to the network address. This code:
125
126               print NetAddr::IP::Lite->new('10.0.0.1/24') + 255;
127
128           outputs 10.0.0.0/24.
129
130       Substraction of a constant
131           The complement of the addition of a constant.
132
133       Auto-increment
134           Auto-incrementing a NetAddr::IP::Lite object causes the address
135           part to be adjusted to the next host address within the subnet. It
136           will wrap at the broadcast address and start again from the network
137           address.
138
139       Auto-decrement
140           Auto-decrementing a NetAddr::IP::Lite object performs exactly the
141           opposite of auto-incrementing it, as you would expect.
142
143       Methods
144
145       "->new([$addr, [ $mask⎪IPv6 ]])"
146       "->new6([$addr, [ $mask]])"
147           These methods creates a new address with the supplied address in
148           $addr and an optional netmask $mask, which can be omitted to get a
149           /32 or /128 netmask for IPv4 / IPv6 addresses respectively
150
151           "->new6" marks the address as being in ipV6 address space even if
152           the format would suggest otherwise.
153
154             i.e.  ->new6('1.2.3.4') will result in ::102:304
155
156             addresses submitted to ->new in ipV6 notation will
157             remain in that notation permanently. i.e.
158                   ->new('::1.2.3.4') will result in ::102:304
159             whereas new('1.2.3.4') would print out as 1.2.3.4
160
161             See "STRINGIFICATION" below.
162
163           $addr can be almost anything that can be resolved to an IP address
164           in all the notations I have seen over time. It can optionally con‐
165           tain the mask in CIDR notation.
166
167           prefix notation is understood, with the limitation that the range
168           speficied by the prefix must match with a valid subnet.
169
170           Addresses in the same format returned by "inet_aton" or "gethostby‐
171           name" can also be understood, although no mask can be specified for
172           them. The default is to not attempt to recognize this format, as it
173           seems to be seldom used.
174
175           To accept addresses in that format, invoke the module as in
176
177             use NetAddr::IP::Lite ':aton'
178
179           If called with no arguments, 'default' is assumed.
180
181           $addr can be any of the following and possibly more...
182
183             n.n
184             n.n/mm
185             n.n.n
186             n.n.n/mm
187             n.n.n.n
188             n.n.n.n/mm            32 bit cidr notation
189             n.n.n.n/m.m.m.m
190             loopback, localhost, broadcast, any, default
191             x.x.x.x/host
192             0xABCDEF, 0b111111000101011110, (a bcd number)
193             a netaddr as returned by 'inet_aton'
194
195           Any RFC1884 notation
196
197             ::n.n.n.n
198             ::n.n.n.n/mmm         128 bit cidr notation
199             ::n.n.n.n/::m.m.m.m
200             ::x:x
201             ::x:x/mmm
202             x:x:x:x:x:x:x:x
203             x:x:x:x:x:x:x:x/mmm
204             x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation
205             loopback, localhost, unspecified, any, default
206             ::x:x/host
207             0xABCDEF, 0b111111000101011110 within the limits
208             of perl's number resolution
209             123456789012  a 'big' bcd number i.e. Math::BigInt
210
211           If called with no arguments, 'default' is assumed.
212
213       "->broadcast()"
214           Returns a new object refering to the broadcast address of a given
215           subnet. The broadcast address has all ones in all the bit positions
216           where the netmask has zero bits. This is normally used to address
217           all the hosts in a given subnet.
218
219       "->network()"
220           Returns a new object refering to the network address of a given
221           subnet. A network address has all zero bits where the bits of the
222           netmask are zero. Normally this is used to refer to a subnet.
223
224       "->addr()"
225           Returns a scalar with the address part of the object as an IPv4 or
226           IPv6 text string as appropriate. This is useful for printing or for
227           passing the address part of the NetAddr::IP::Lite object to other
228           components that expect an IP address. If the object is an ipV6
229           address or was created using ->new6($ip) it will be reported in
230           ipV6 hex format otherwise it will be reported in dot quad format
231           only if it resides in ipV4 address space.
232
233       "->mask()"
234           Returns a scalar with the mask as an IPv4 or IPv6 text string as
235           described above.
236
237       "->masklen()"
238           Returns a scalar the number of one bits in the mask.
239
240       "->bits()"
241           Returns the width of the address in bits. Normally 32 for v4 and
242           128 for v6.
243
244       "->version()"
245           Returns the version of the address or subnet. Currently this can be
246           either 4 or 6.
247
248       "->cidr()"
249           Returns a scalar with the address and mask in CIDR notation. A
250           NetAddr::IP::Lite object stringifies to the result of this func‐
251           tion.  (see comments about ->new6() and ->addr() for output for‐
252           mats)
253
254       "->aton()"
255           Returns the address part of the NetAddr::IP::Lite object in the
256           same format as the "inet_aton()" or "ipv6_aton" function respec‐
257           tively. If the object was created using ->new6($ip), the address
258           returned will always be in ipV6 format, even for addresses in ipV4
259           address space.
260
261       "->range()"
262           Returns a scalar with the base address and the broadcast address
263           separated by a dash and spaces. This is called range notation.
264
265       "->numeric()"
266           When called in a scalar context, will return a numeric representa‐
267           tion of the address part of the IP address. When called in an array
268           contest, it returns a list of two elements. The first element is as
269           described, the second element is the numeric representation of the
270           netmask.
271
272           This method is essential for serializing the representation of a
273           subnet.
274
275       "$me->contains($other)"
276           Returns true when $me completely contains $other. False is returned
277           otherwise and "undef" is returned if $me and $other are not both
278           "NetAddr::IP::Lite" objects.
279
280       "$me->within($other)"
281           The complement of "->contains()". Returns true when $me is com‐
282           pletely contained within $other, undef if $me and $other are not
283           both "NetAddr::IP::Lite" objects.
284
285       "->first()"
286           Returns a new object representing the first usable IP address
287           within the subnet (ie, the first host address).
288
289       "->last()"
290           Returns a new object representing the last usable IP address within
291           the subnet (ie, one less than the broadcast address).
292
293       "->nth($index)"
294           Returns a new object representing the n-th usable IP address within
295           the subnet (ie, the n-th host address).  If no address is available
296           (for example, when the network is too small for $index hosts),
297           "undef" is returned.
298
299           Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
300           implements "->nth($index)" and "->num()" exactly as the documenta‐
301           tion states.  Previous versions behaved slightly differently and
302           not in a consistent manner.
303
304           To use the old behavior for "->nth($index)" and "->num()":
305
306             use NetAddr::IP::Lite qw(:old_nth);
307
308             old behavior:
309             NetAddr::IP->new('10/32')->nth(0) == undef
310             NetAddr::IP->new('10/32')->nth(1) == undef
311             NetAddr::IP->new('10/31')->nth(0) == undef
312             NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31
313             NetAddr::IP->new('10/30')->nth(0) == undef
314             NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30
315             NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30
316             NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30
317
318           Note that in each case, the broadcast address is represented in the
319           output set and that the 'zero'th index is alway undef.
320
321             new behavior:
322             NetAddr::IP->new('10/32')->nth(0)  == 10.0.0.0/32
323             NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32
324             NetAddr::IP->new('10/31')->nth(0)  == undef
325             NetAddr::IP->new('10/31')->nth(1)  == undef
326             NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30
327             NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30
328             NetAddr::IP->new('10/30')->nth(2) == undef
329
330           Note that a /32 net always has 1 usable address while a /31 has
331           none since it has a network and broadcast address, but no host
332           addresses. The first index (0) returns the address immediately fol‐
333           lowing the network address.
334
335       "->num()"
336           Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
337           Returns the number of usable addresses IP addresses within the sub‐
338           net, not counting the broadcast or network address. Previous ver‐
339           sions returned th number of IP addresses not counting the broadcast
340           address.
341
342           To use the old behavior for "->nth($index)" and "->num()":
343
344             use NetAddr::IP::Lite qw(:old_nth);
345

EXPORT_OK

347               Zero
348               Ones
349               V4mask
350               V4net
351               :aton
352               :old_nth
353

AUTHOR

355       Luis E. Muñoz <luismunoz@cpan.org>, Michael Robinton <michael@bizsys‐
356       tems.com>
357

WARRANTY

359       This software comes with the  same warranty as perl itself (ie, none),
360       so by using it you accept any and all the liability.
361

LICENSE

363       This software is (c) Luis E. Muñoz, 1999 - 2005, and (c) Michael Robin‐
364       ton, 2006.  It can be used under the terms of the perl artistic license
365       provided that proper credit for the work of the author is preserved in
366       the form of this copyright notice and license for this module.
367

SEE ALSO

369       perl(1), NetAddr::IP(3), NetAddr::IP::Util(3)
370
371
372
373perl v5.8.8                       2007-07-11                           Lite(3)
Impressum