Newer
Older
NetAddr-IP / Lite / README
@Michael Robinton Michael Robinton on 21 Oct 2014 18 KB Import of MIKER/NetAddr-IP-4.050 from CPAN.
  1. NAME
  2. NetAddr::IP::Lite - Manages IPv4 and IPv6 addresses and subnets
  3.  
  4. SYNOPSIS
  5. use NetAddr::IP::Lite qw(
  6. Zeros
  7. Ones
  8. V4mask
  9. V4net
  10. :aton DEPRECATED !
  11. :old_nth
  12. :upper
  13. :lower
  14. );
  15.  
  16. my $ip = new NetAddr::IP::Lite '127.0.0.1';
  17. or if your prefer
  18. my $ip = NetAddr::IP::Lite->new('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.  
  40. INSTALLATION
  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 default
  49. with its primary functions compiled using Perl's XS extensions to build
  50. a 'C' library. If you do not have a 'C' complier available or would like
  51. the slower Pure Perl version for some other reason, then type:
  52.  
  53. perl Makefile.PL -noxs
  54. make
  55. make test
  56. make install
  57.  
  58. DESCRIPTION
  59. This module provides an object-oriented abstraction on top of IP
  60. addresses or IP subnets, that allows for easy manipulations. Most of the
  61. operations of NetAddr::IP are supported. This module will work older
  62. versions of Perl and does not use but is compatible with Math::BigInt.
  63.  
  64. * By default NetAddr::IP functions and methods return string IPv6
  65. addresses in uppercase. To change that to lowercase:
  66.  
  67. NOTE: the AUGUST 2010 RFC5952 states:
  68.  
  69. 4.3. Lowercase
  70.  
  71. The characters "a", "b", "c", "d", "e", and "f" in an IPv6
  72. address MUST be represented in lowercase.
  73.  
  74. It is recommended that all NEW applications using NetAddr::IP::Lite be
  75. invoked as shown on the next line.
  76.  
  77. use NetAddr::IP::Lite qw(:lower);
  78.  
  79. * To ensure the current IPv6 string case behavior even if the default
  80. changes:
  81.  
  82. use NetAddr::IP::Lite qw(:upper);
  83.  
  84. The internal representation of all IP objects is in 128 bit IPv6
  85. notation. IPv4 and IPv6 objects may be freely mixed.
  86.  
  87. The supported operations are described below:
  88.  
  89. Overloaded Operators
  90.  
  91. Assignment ("=")
  92. Has been optimized to copy one NetAddr::IP::Lite object to another
  93. very quickly.
  94.  
  95. "->copy()"
  96. The assignment ("=") operation is only put in to operation when the
  97. copied object is further mutated by another overloaded operation.
  98. See the overload manpage SPECIAL SYMBOLS FOR "use overload" for
  99. details.
  100.  
  101. "->copy()" actually creates a new object when called.
  102.  
  103. Stringification
  104. An object can be used just as a string. For instance, the following
  105. code
  106.  
  107. my $ip = new NetAddr::IP::Lite '192.168.1.123';
  108. print "$ip\n";
  109.  
  110. Will print the string 192.168.1.123/32.
  111.  
  112. my $ip = new6 NetAddr::IP::Lite '192.168.1.123';
  113. print "$ip\n";
  114.  
  115. Will print the string 0:0:0:0:0:0:C0A8:17B/128
  116.  
  117. Equality
  118. You can test for equality with either "eq", "ne", "==" or "!=".
  119. "eq", "ne" allows the comparison with arbitrary strings as well as
  120. NetAddr::IP::Lite objects. The following example:
  121.  
  122. if (NetAddr::IP::Lite->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8')
  123. { print "Yes\n"; }
  124.  
  125. Will print out "Yes".
  126.  
  127. Comparison with "==" and "!=" requires both operands to be
  128. NetAddr::IP::Lite objects.
  129.  
  130. Comparison via >, <, >=, <=, <=> and "cmp"
  131. Internally, all network objects are represented in 128 bit format.
  132. The numeric representation of the network is compared through the
  133. corresponding operation. Comparisons are tried first on the address
  134. portion of the object and if that is equal then the NUMERIC cidr
  135. portion of the masks are compared. This leads to the
  136. counterintuitive result that
  137.  
  138. /24 > /16
  139.  
  140. Comparison should not be done on netaddr objects with different CIDR
  141. as this may produce indeterminate - unexpected results, rather the
  142. determination of which netblock is larger or smaller should be done
  143. by comparing
  144.  
  145. $ip1->masklen <=> $ip2->masklen
  146.  
  147. Addition of a constant ("+")
  148. Add a 32 bit signed constant to the address part of a NetAddr
  149. object. This operation changes the address part to point so many
  150. hosts above the current objects start address. For instance, this
  151. code:
  152.  
  153. print NetAddr::IP::Lite->new('127.0.0.1/8') + 5;
  154.  
  155. will output 127.0.0.6/8. The address will wrap around at the
  156. broadcast back to the network address. This code:
  157.  
  158. print NetAddr::IP::Lite->new('10.0.0.1/24') + 255;
  159.  
  160. outputs 10.0.0.0/24.
  161.  
  162. Returns the the unchanged object when the constant is missing or out
  163. of range.
  164.  
  165. 2147483647 <= constant >= -2147483648
  166.  
  167. Subtraction of a constant ("-")
  168. The complement of the addition of a constant.
  169.  
  170. Difference ("-")
  171. Returns the difference between the address parts of two
  172. NetAddr::IP::Lite objects address parts as a 32 bit signed number.
  173.  
  174. Returns undef if the difference is out of range.
  175.  
  176. Auto-increment
  177. Auto-incrementing a NetAddr::IP::Lite object causes the address part
  178. to be adjusted to the next host address within the subnet. It will
  179. wrap at the broadcast address and start again from the network
  180. address.
  181.  
  182. Auto-decrement
  183. Auto-decrementing a NetAddr::IP::Lite object performs exactly the
  184. opposite of auto-incrementing it, as you would expect.
  185.  
  186. Methods
  187.  
  188. "->new([$addr, [ $mask|IPv6 ]])"
  189. "->new6([$addr, [ $mask]])"
  190. "->new_no([$addr, [ $mask]])"
  191. "->new_from_aton($netaddr)"
  192. new_cis and new_cis6 are DEPRECATED
  193. "->new_cis("$addr $mask)"
  194. "->new_cis6("$addr $mask)"
  195. The first two methods create a new address with the supplied address
  196. in "$addr" and an optional netmask "$mask", which can be omitted to
  197. get a /32 or /128 netmask for IPv4 / IPv6 addresses respectively.
  198.  
  199. The third method "new_no" is exclusively for IPv4 addresses and
  200. filters improperly formatted dot quad strings for leading 0's that
  201. would normally be interpreted as octal format by NetAddr per the
  202. specifications for inet_aton.
  203.  
  204. new_from_aton takes a packed IPv4 address and assumes a /32 mask.
  205. This function replaces the DEPRECATED :aton functionality which is
  206. fundamentally broken.
  207.  
  208. The last two methods new_cis and new_cis6 differ from new and new6
  209. only in that they except the common Cisco address notation for
  210. address/mask pairs with a space as a separator instead of a slash
  211. (/)
  212.  
  213. These methods are DEPRECATED because the functionality is now
  214. included in the other "new" methods
  215.  
  216. i.e. ->new_cis('1.2.3.0 24')
  217. or
  218. ->new_cis6('::1.2.3.0 120')
  219.  
  220. "->new6" and "->new_cis6" mark the address as being in ipV6 address
  221. space even if the format would suggest otherwise.
  222.  
  223. i.e. ->new6('1.2.3.4') will result in ::102:304
  224.  
  225. addresses submitted to ->new in ipV6 notation will
  226. remain in that notation permanently. i.e.
  227. ->new('::1.2.3.4') will result in ::102:304
  228. whereas new('1.2.3.4') would print out as 1.2.3.4
  229.  
  230. See "STRINGIFICATION" below.
  231.  
  232. "$addr" can be almost anything that can be resolved to an IP address
  233. in all the notations I have seen over time. It can optionally
  234. contain the mask in CIDR notation. If the OPTIONAL perl module
  235. Socket6 is available in the local library it will autoload and ipV6
  236. host6 names will be resolved as well as ipV4 hostnames.
  237.  
  238. prefix notation is understood, with the limitation that the range
  239. specified by the prefix must match with a valid subnet.
  240.  
  241. Addresses in the same format returned by "inet_aton" or
  242. "gethostbyname" can also be understood, although no mask can be
  243. specified for them. The default is to not attempt to recognize this
  244. format, as it seems to be seldom used.
  245.  
  246. ###### DEPRECATED, will be remove in version 5 ############ To
  247. accept addresses in that format, invoke the module as in
  248.  
  249. use NetAddr::IP::Lite ':aton'
  250.  
  251. ###### USE new_from_aton instead ##########################
  252.  
  253. If called with no arguments, 'default' is assumed.
  254.  
  255. "$addr" can be any of the following and possibly more...
  256.  
  257. n.n
  258. n.n/mm
  259. n.n mm
  260. n.n.n
  261. n.n.n/mm
  262. n.n.n mm
  263. n.n.n.n
  264. n.n.n.n/mm 32 bit cidr notation
  265. n.n.n.n mm
  266. n.n.n.n/m.m.m.m
  267. n.n.n.n m.m.m.m
  268. loopback, localhost, broadcast, any, default
  269. x.x.x.x/host
  270. 0xABCDEF, 0b111111000101011110, (or a bcd number)
  271. a netaddr as returned by 'inet_aton'
  272.  
  273. Any RFC1884 notation
  274.  
  275. ::n.n.n.n
  276. ::n.n.n.n/mmm 128 bit cidr notation
  277. ::n.n.n.n/::m.m.m.m
  278. ::x:x
  279. ::x:x/mmm
  280. x:x:x:x:x:x:x:x
  281. x:x:x:x:x:x:x:x/mmm
  282. x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation
  283. loopback, localhost, unspecified, any, default
  284. ::x:x/host
  285. 0xABCDEF, 0b111111000101011110 within the limits
  286. of perl's number resolution
  287. 123456789012 a 'big' bcd number (bigger than perl likes)
  288. and Math::BigInt
  289.  
  290. If called with no arguments, 'default' is assumed.
  291.  
  292. "->broadcast()"
  293. Returns a new object referring to the broadcast address of a given
  294. subnet. The broadcast address has all ones in all the bit positions
  295. where the netmask has zero bits. This is normally used to address
  296. all the hosts in a given subnet.
  297.  
  298. "->network()"
  299. Returns a new object referring to the network address of a given
  300. subnet. A network address has all zero bits where the bits of the
  301. netmask are zero. Normally this is used to refer to a subnet.
  302.  
  303. "->addr()"
  304. Returns a scalar with the address part of the object as an IPv4 or
  305. IPv6 text string as appropriate. This is useful for printing or for
  306. passing the address part of the NetAddr::IP::Lite object to other
  307. components that expect an IP address. If the object is an ipV6
  308. address or was created using ->new6($ip) it will be reported in ipV6
  309. hex format otherwise it will be reported in dot quad format only if
  310. it resides in ipV4 address space.
  311.  
  312. "->mask()"
  313. Returns a scalar with the mask as an IPv4 or IPv6 text string as
  314. described above.
  315.  
  316. "->masklen()"
  317. Returns a scalar the number of one bits in the mask.
  318.  
  319. "->bits()"
  320. Returns the width of the address in bits. Normally 32 for v4 and 128
  321. for v6.
  322.  
  323. "->version()"
  324. Returns the version of the address or subnet. Currently this can be
  325. either 4 or 6.
  326.  
  327. "->cidr()"
  328. Returns a scalar with the address and mask in CIDR notation. A
  329. NetAddr::IP::Lite object *stringifies* to the result of this
  330. function. (see comments about ->new6() and ->addr() for output
  331. formats)
  332.  
  333. "->aton()"
  334. Returns the address part of the NetAddr::IP::Lite object in the same
  335. format as the "inet_aton()" or "ipv6_aton" function respectively. If
  336. the object was created using ->new6($ip), the address returned will
  337. always be in ipV6 format, even for addresses in ipV4 address space.
  338.  
  339. "->range()"
  340. Returns a scalar with the base address and the broadcast address
  341. separated by a dash and spaces. This is called range notation.
  342.  
  343. "->numeric()"
  344. When called in a scalar context, will return a numeric
  345. representation of the address part of the IP address. When called in
  346. an array contest, it returns a list of two elements. The first
  347. element is as described, the second element is the numeric
  348. representation of the netmask.
  349.  
  350. This method is essential for serializing the representation of a
  351. subnet.
  352.  
  353. "->bigint()"
  354. When called in a scalar context, will return a Math::BigInt
  355. representation of the address part of the IP address. When called in
  356. an array contest, it returns a list of two elements. The first
  357. element is as described, the second element is the Math::BigInt
  358. representation of the netmask.
  359.  
  360. "$me->contains($other)"
  361. Returns true when "$me" completely contains "$other". False is
  362. returned otherwise and "undef" is returned if "$me" and "$other" are
  363. not both "NetAddr::IP::Lite" objects.
  364.  
  365. "$me->within($other)"
  366. The complement of "->contains()". Returns true when "$me" is
  367. completely contained within "$other", undef if "$me" and "$other"
  368. are not both "NetAddr::IP::Lite" objects.
  369.  
  370. "->first()"
  371. Returns a new object representing the first usable IP address within
  372. the subnet (ie, the first host address).
  373.  
  374. "->last()"
  375. Returns a new object representing the last usable IP address within
  376. the subnet (ie, one less than the broadcast address).
  377.  
  378. "->nth($index)"
  379. Returns a new object representing the *n*-th usable IP address
  380. within the subnet (ie, the *n*-th host address). If no address is
  381. available (for example, when the network is too small for "$index"
  382. hosts), "undef" is returned.
  383.  
  384. Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
  385. implements "->nth($index)" and "->num()" exactly as the
  386. documentation states. Previous versions behaved slightly differently
  387. and not in a consistent manner.
  388.  
  389. To use the old behavior for "->nth($index)" and "->num()":
  390.  
  391. use NetAddr::IP::Lite qw(:old_nth);
  392.  
  393. old behavior:
  394. NetAddr::IP->new('10/32')->nth(0) == undef
  395. NetAddr::IP->new('10/32')->nth(1) == undef
  396. NetAddr::IP->new('10/31')->nth(0) == undef
  397. NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31
  398. NetAddr::IP->new('10/30')->nth(0) == undef
  399. NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30
  400. NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30
  401. NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30
  402.  
  403. Note that in each case, the broadcast address is represented in the
  404. output set and that the 'zero'th index is alway undef except for a
  405. point-to-point /31 or /127 network where there are exactly two
  406. addresses in the network.
  407.  
  408. new behavior:
  409. NetAddr::IP->new('10/32')->nth(0) == 10.0.0.0/32
  410. NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32
  411. NetAddr::IP->new('10/31')->nth(0) == 10.0.0.0/32
  412. NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/32
  413. NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30
  414. NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30
  415. NetAddr::IP->new('10/30')->nth(2) == undef
  416.  
  417. Note that a /32 net always has 1 usable address while a /31 has
  418. exactly two usable addresses for point-to-point addressing. The
  419. first index (0) returns the address immediately following the
  420. network address except for a /31 or /127 when it return the network
  421. address.
  422.  
  423. "->num()"
  424. As of version 4.42 of NetAddr::IP and version 1.27 of
  425. NetAddr::IP::Lite a /31 and /127 with return a net num value of 2
  426. instead of 0 (zero) for point-to-point networks.
  427.  
  428. Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
  429. return the number of usable IP addresses within the subnet, not
  430. counting the broadcast or network address.
  431.  
  432. Previous versions worked only for ipV4 addresses, returned a maximum
  433. span of 2**32 and returned the number of IP addresses not counting
  434. the broadcast address. (one greater than the new behavior)
  435.  
  436. To use the old behavior for "->nth($index)" and "->num()":
  437.  
  438. use NetAddr::IP::Lite qw(:old_nth);
  439.  
  440. WARNING:
  441.  
  442. NetAddr::IP will calculate and return a numeric string for network
  443. ranges as large as 2**128. These values are TEXT strings and perl
  444. can treat them as integers for numeric calculations.
  445.  
  446. Perl on 32 bit platforms only handles integer numbers up to 2**32
  447. and on 64 bit platforms to 2**64.
  448.  
  449. If you wish to manipulate numeric strings returned by NetAddr::IP
  450. that are larger than 2**32 or 2**64, respectively, you must load
  451. additional modules such as Math::BigInt, bignum or some similar
  452. package to do the integer math.
  453.  
  454. EXPORT_OK
  455. Zeros
  456. Ones
  457. V4mask
  458. V4net
  459. :aton DEPRECATED
  460. :old_nth
  461. :upper
  462. :lower
  463.  
  464. AUTHORS
  465. Luis E. Muñoz <luismunoz@cpan.org>, Michael Robinton
  466. <michael@bizsystems.com>
  467.  
  468. WARRANTY
  469. This software comes with the same warranty as perl itself (ie, none), so
  470. by using it you accept any and all the liability.
  471.  
  472. COPYRIGHT
  473. This software is (c) Luis E. Muñoz, 1999 - 2005
  474. and (c) Michael Robinton, 2006 - 2011.
  475.  
  476. All rights reserved.
  477.  
  478. This program is free software; you can redistribute it and/or modify it
  479. under the terms of either:
  480.  
  481. a) the GNU General Public License as published by the Free
  482. Software Foundation; either version 2, or (at your option) any
  483. later version, or
  484.  
  485. b) the "Artistic License" which comes with this distribution.
  486.  
  487. This program is distributed in the hope that it will be useful, but
  488. WITHOUT ANY WARRANTY; without even the implied warranty of
  489. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU
  490. General Public License or the Artistic License for more details.
  491.  
  492. You should have received a copy of the Artistic License with this
  493. distribution, in the file named "Artistic". If not, I'll be glad to
  494. provide one.
  495.  
  496. You should also have received a copy of the GNU General Public License
  497. along with this program in the file named "Copying". If not, write to
  498. the
  499.  
  500. Free Software Foundation, Inc.
  501. 59 Temple Place, Suite 330
  502. Boston, MA 02111-1307, USA
  503.  
  504. or visit their web page on the internet at:
  505.  
  506. http://www.gnu.org/copyleft/gpl.html.
  507.  
  508. SEE ALSO
  509. NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3)
  510.