Newer
Older
NetAddr-IP / Lite / README
@Michael Robinton Michael Robinton on 21 Oct 2014 19 KB Import of MIKER/NetAddr-IP-4.069 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 with
  62. older versions of Perl and 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. If called with an empty string as the argument, returns 'undef'
  256.  
  257. "$addr" can be any of the following and possibly more...
  258.  
  259. n.n
  260. n.n/mm
  261. n.n mm
  262. n.n.n
  263. n.n.n/mm
  264. n.n.n mm
  265. n.n.n.n
  266. n.n.n.n/mm 32 bit cidr notation
  267. n.n.n.n mm
  268. n.n.n.n/m.m.m.m
  269. n.n.n.n m.m.m.m
  270. loopback, localhost, broadcast, any, default
  271. x.x.x.x/host
  272. 0xABCDEF, 0b111111000101011110, (or a bcd number)
  273. a netaddr as returned by 'inet_aton'
  274.  
  275. Any RFC1884 notation
  276.  
  277. ::n.n.n.n
  278. ::n.n.n.n/mmm 128 bit cidr notation
  279. ::n.n.n.n/::m.m.m.m
  280. ::x:x
  281. ::x:x/mmm
  282. x:x:x:x:x:x:x:x
  283. x:x:x:x:x:x:x:x/mmm
  284. x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation
  285. loopback, localhost, unspecified, any, default
  286. ::x:x/host
  287. 0xABCDEF, 0b111111000101011110 within the limits
  288. of perl's number resolution
  289. 123456789012 a 'big' bcd number (bigger than perl likes)
  290. and Math::BigInt
  291.  
  292. If called with no arguments, 'default' is assumed.
  293.  
  294. If called with and empty string as the argument, 'undef' is
  295. returned;
  296.  
  297. "->broadcast()"
  298. Returns a new object referring to the broadcast address of a given
  299. subnet. The broadcast address has all ones in all the bit positions
  300. where the netmask has zero bits. This is normally used to address
  301. all the hosts in a given subnet.
  302.  
  303. "->network()"
  304. Returns a new object referring to the network address of a given
  305. subnet. A network address has all zero bits where the bits of the
  306. netmask are zero. Normally this is used to refer to a subnet.
  307.  
  308. "->addr()"
  309. Returns a scalar with the address part of the object as an IPv4 or
  310. IPv6 text string as appropriate. This is useful for printing or for
  311. passing the address part of the NetAddr::IP::Lite object to other
  312. components that expect an IP address. If the object is an ipV6
  313. address or was created using ->new6($ip) it will be reported in ipV6
  314. hex format otherwise it will be reported in dot quad format only if
  315. it resides in ipV4 address space.
  316.  
  317. "->mask()"
  318. Returns a scalar with the mask as an IPv4 or IPv6 text string as
  319. described above.
  320.  
  321. "->masklen()"
  322. Returns a scalar the number of one bits in the mask.
  323.  
  324. "->bits()"
  325. Returns the width of the address in bits. Normally 32 for v4 and 128
  326. for v6.
  327.  
  328. "->version()"
  329. Returns the version of the address or subnet. Currently this can be
  330. either 4 or 6.
  331.  
  332. "->cidr()"
  333. Returns a scalar with the address and mask in CIDR notation. A
  334. NetAddr::IP::Lite object *stringifies* to the result of this
  335. function. (see comments about ->new6() and ->addr() for output
  336. formats)
  337.  
  338. "->aton()"
  339. Returns the address part of the NetAddr::IP::Lite object in the same
  340. format as the "inet_aton()" or "ipv6_aton" function respectively. If
  341. the object was created using ->new6($ip), the address returned will
  342. always be in ipV6 format, even for addresses in ipV4 address space.
  343.  
  344. "->range()"
  345. Returns a scalar with the base address and the broadcast address
  346. separated by a dash and spaces. This is called range notation.
  347.  
  348. "->numeric()"
  349. When called in a scalar context, will return a numeric
  350. representation of the address part of the IP address. When called in
  351. an array context, it returns a list of two elements. The first
  352. element is as described, the second element is the numeric
  353. representation of the netmask.
  354.  
  355. This method is essential for serializing the representation of a
  356. subnet.
  357.  
  358. "->bigint()"
  359. When called in a scalar context, will return a Math::BigInt
  360. representation of the address part of the IP address. When called in
  361. an array contest, it returns a list of two elements. The first
  362. element is as described, the second element is the Math::BigInt
  363. representation of the netmask.
  364.  
  365. "$me->contains($other)"
  366. Returns true when "$me" completely contains "$other". False is
  367. returned otherwise and "undef" is returned if "$me" and "$other" are
  368. not both "NetAddr::IP::Lite" objects.
  369.  
  370. "$me->within($other)"
  371. The complement of "->contains()". Returns true when "$me" is
  372. completely contained within "$other", undef if "$me" and "$other"
  373. are not both "NetAddr::IP::Lite" objects.
  374.  
  375. C->is_rfc1918()>
  376. Returns true when "$me" is an RFC 1918 address.
  377.  
  378. 10.0.0.0 - 10.255.255.255 (10/8 prefix)
  379. 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
  380. 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
  381.  
  382. "->first()"
  383. Returns a new object representing the first usable IP address within
  384. the subnet (ie, the first host address).
  385.  
  386. "->last()"
  387. Returns a new object representing the last usable IP address within
  388. the subnet (ie, one less than the broadcast address).
  389.  
  390. "->nth($index)"
  391. Returns a new object representing the *n*-th usable IP address
  392. within the subnet (ie, the *n*-th host address). If no address is
  393. available (for example, when the network is too small for "$index"
  394. hosts), "undef" is returned.
  395.  
  396. Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
  397. implements "->nth($index)" and "->num()" exactly as the
  398. documentation states. Previous versions behaved slightly differently
  399. and not in a consistent manner.
  400.  
  401. To use the old behavior for "->nth($index)" and "->num()":
  402.  
  403. use NetAddr::IP::Lite qw(:old_nth);
  404.  
  405. old behavior:
  406. NetAddr::IP->new('10/32')->nth(0) == undef
  407. NetAddr::IP->new('10/32')->nth(1) == undef
  408. NetAddr::IP->new('10/31')->nth(0) == undef
  409. NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31
  410. NetAddr::IP->new('10/30')->nth(0) == undef
  411. NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30
  412. NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30
  413. NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30
  414.  
  415. Note that in each case, the broadcast address is represented in the
  416. output set and that the 'zero'th index is alway undef except for a
  417. point-to-point /31 or /127 network where there are exactly two
  418. addresses in the network.
  419.  
  420. new behavior:
  421. NetAddr::IP->new('10/32')->nth(0) == 10.0.0.0/32
  422. NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32
  423. NetAddr::IP->new('10/31')->nth(0) == 10.0.0.0/32
  424. NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/32
  425. NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30
  426. NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30
  427. NetAddr::IP->new('10/30')->nth(2) == undef
  428.  
  429. Note that a /32 net always has 1 usable address while a /31 has
  430. exactly two usable addresses for point-to-point addressing. The
  431. first index (0) returns the address immediately following the
  432. network address except for a /31 or /127 when it return the network
  433. address.
  434.  
  435. "->num()"
  436. As of version 4.42 of NetAddr::IP and version 1.27 of
  437. NetAddr::IP::Lite a /31 and /127 with return a net num value of 2
  438. instead of 0 (zero) for point-to-point networks.
  439.  
  440. Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
  441. return the number of usable IP addresses within the subnet, not
  442. counting the broadcast or network address.
  443.  
  444. Previous versions worked only for ipV4 addresses, returned a maximum
  445. span of 2**32 and returned the number of IP addresses not counting
  446. the broadcast address. (one greater than the new behavior)
  447.  
  448. To use the old behavior for "->nth($index)" and "->num()":
  449.  
  450. use NetAddr::IP::Lite qw(:old_nth);
  451.  
  452. WARNING:
  453.  
  454. NetAddr::IP will calculate and return a numeric string for network
  455. ranges as large as 2**128. These values are TEXT strings and perl
  456. can treat them as integers for numeric calculations.
  457.  
  458. Perl on 32 bit platforms only handles integer numbers up to 2**32
  459. and on 64 bit platforms to 2**64.
  460.  
  461. If you wish to manipulate numeric strings returned by NetAddr::IP
  462. that are larger than 2**32 or 2**64, respectively, you must load
  463. additional modules such as Math::BigInt, bignum or some similar
  464. package to do the integer math.
  465.  
  466. EXPORT_OK
  467. Zeros
  468. Ones
  469. V4mask
  470. V4net
  471. :aton DEPRECATED
  472. :old_nth
  473. :upper
  474. :lower
  475.  
  476. AUTHORS
  477. Luis E. Muñoz <luismunoz@cpan.org>, Michael Robinton
  478. <michael@bizsystems.com>
  479.  
  480. WARRANTY
  481. This software comes with the same warranty as perl itself (ie, none), so
  482. by using it you accept any and all the liability.
  483.  
  484. COPYRIGHT
  485. This software is (c) Luis E. Muñoz, 1999 - 2005
  486. and (c) Michael Robinton, 2006 - 2012.
  487.  
  488. All rights reserved.
  489.  
  490. This program is free software; you can redistribute it and/or modify it
  491. under the terms of either:
  492.  
  493. a) the GNU General Public License as published by the Free
  494. Software Foundation; either version 2, or (at your option) any
  495. later version, or
  496.  
  497. b) the "Artistic License" which comes with this distribution.
  498.  
  499. This program is distributed in the hope that it will be useful, but
  500. WITHOUT ANY WARRANTY; without even the implied warranty of
  501. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU
  502. General Public License or the Artistic License for more details.
  503.  
  504. You should have received a copy of the Artistic License with this
  505. distribution, in the file named "Artistic". If not, I'll be glad to
  506. provide one.
  507.  
  508. You should also have received a copy of the GNU General Public License
  509. along with this program in the file named "Copying". If not, write to
  510. the
  511.  
  512. Free Software Foundation, Inc.,
  513. 51 Franklin Street, Fifth Floor
  514. Boston, MA 02110-1301 USA
  515.  
  516. or visit their web page on the internet at:
  517.  
  518. http://www.gnu.org/copyleft/gpl.html.
  519.  
  520. SEE ALSO
  521. NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3)
  522.