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