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