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