Newer
Older
NetAddr-IP / Lite / Util / README
@Michael Robinton Michael Robinton on 21 Oct 2014 14 KB Import of MIKER/NetAddr-IP-4.032 from CPAN.
  1. NAME
  2. NetAddr::IP::Util -- IPv4/6 and 128 bit number utilities
  3.  
  4. SYNOPSIS
  5. use NetAddr::IP::Util qw(
  6. inet_aton
  7. inet_ntoa
  8. ipv6_aton
  9. ipv6_n2x
  10. ipv6_n2d
  11. inet_any2n
  12. hasbits
  13. isIPv4
  14. inet_n2dx
  15. inet_n2ad
  16. ipv4to6
  17. mask4to6
  18. ipanyto6
  19. maskanyto6
  20. ipv6to4
  21. shiftleft
  22. addconst
  23. add128
  24. sub128
  25. notcontiguous
  26. bin2bcd
  27. bcd2bin
  28. mode
  29. naip_gethostbyname
  30. );
  31.  
  32. use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math)
  33.  
  34. :inet => inet_aton, inet_ntoa, ipv6_aton,
  35. ipv6_n2x, ipv6_n2d, inet_any2n,
  36. inet_n2dx, inet_n2ad, ipv4to6,
  37. mask4to6, ipanyto6, maskanyto6,
  38. ipv6to4, naip_gethostbyname
  39.  
  40. :ipv4 => inet_aton, inet_ntoa
  41.  
  42. :ipv6 => ipv6_aton, ipv6_n2x, ipv6_n2d,
  43. inet_any2n, inet_n2dx, inet_n2ad
  44. ipv4to6, mask4to6, ipanyto6,
  45. maskanyto6, ipv6to4, naip_gethostbyname
  46.  
  47. :math => hasbits, isIPv4, addconst,
  48. add128, sub128, notcontiguous,
  49. bin2bcd, bcd2bin, shiftleft
  50.  
  51. $dotquad = inet_ntoa($netaddr);
  52. $netaddr = inet_aton($dotquad);
  53. $ipv6naddr = ipv6_aton($ipv6_text);
  54. $hex_text = ipv6_n2x($ipv6naddr);
  55. $dec_text = ipv6_n2d($ipv6naddr);
  56. $ipv6naddr = inet_any2n($dotquad or $ipv6_text);
  57. $rv = hasbits($bits128);
  58. $rv = isIPv4($bits128);
  59. $dotquad or $hex_text = inet_n2dx($ipv6naddr);
  60. $dotquad or $dec_text = inet_n2ad($ipv6naddr);
  61. $ipv6naddr = ipv4to6($netaddr);
  62. $ipv6naddr = mask4to6($netaddr);
  63. $ipv6naddr = ipanyto6($netaddr);
  64. $ipv6naddr = maskanyto6($netaddr);
  65. $netaddr = ipv6to4($pv6naddr);
  66. $bitsX2 = shiftleft($bits128,$n);
  67. $carry = addconst($ipv6naddr,$signed_32con);
  68. ($carry,$ipv6naddr)=addconst($ipv6naddr,$signed_32con);
  69. $carry = add128($ipv6naddr1,$ipv6naddr2);
  70. ($carry,$ipv6naddr)=add128($ipv6naddr1,$ipv6naddr2);
  71. $carry = sub128($ipv6naddr1,$ipv6naddr2);
  72. ($carry,$ipv6naddr)=sub128($ipv6naddr1,$ipv6naddr2);
  73. ($spurious,$cidr) = notcontiguous($mask128);
  74. $bcdtext = bin2bcd($bits128);
  75. $bits128 = bcd2bin($bcdtxt);
  76. $modetext = mode;
  77. ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME);
  78.  
  79. NetAddr::IP::Util::lower();
  80. NetAddr::IP::Util::upper();
  81.  
  82. INSTALLATION
  83. Un-tar the distribution in an appropriate directory and type:
  84.  
  85. perl Makefile.PL
  86. make
  87. make test
  88. make install
  89.  
  90. NetAddr::IP::Util installs by default with its primary functions
  91. compiled using Perl's XS extensions to build a 'C' library. If you do
  92. not have a 'C' complier available or would like the slower Pure Perl
  93. version for some other reason, then type:
  94.  
  95. perl Makefile.PL -noxs
  96. make
  97. make test
  98. make install
  99.  
  100. DESCRIPTION
  101. NetAddr::IP::Util provides a suite of tools for manipulating and
  102. converting IPv4 and IPv6 addresses into 128 bit string context and back
  103. to text. The strings can be manipulated with Perl's logical operators:
  104.  
  105. and &
  106. or |
  107. xor ^
  108. ~ compliment
  109.  
  110. in the same manner as 'vec' strings.
  111.  
  112. The IPv6 functions support all rfc1884 formats.
  113.  
  114. i.e. x:x:x:x:x:x:x:x:x
  115. x:x:x:x:x:x:x:d.d.d.d
  116. ::x:x:x
  117. ::x:d.d.d.d
  118. and so on...
  119.  
  120. * $dotquad = inet_ntoa($netaddr);
  121. Convert a packed IPv4 network address to a dot-quad IP address.
  122.  
  123. input: packed network address
  124. returns: IP address i.e. 10.4.12.123
  125.  
  126. * $netaddr = inet_aton($dotquad);
  127. Convert a dot-quad IP address into an IPv4 packed network address.
  128.  
  129. input: IP address i.e. 192.5.16.32
  130. returns: packed network address
  131.  
  132. * $ipv6addr = ipv6_aton($ipv6_text);
  133. Takes an IPv6 address of the form described in rfc1884 and returns a
  134. 128 bit binary RDATA string.
  135.  
  136. input: ipv6 text
  137. returns: 128 bit RDATA string
  138.  
  139. * $hex_text = ipv6_n2x($ipv6addr);
  140. Takes an IPv6 RDATA string and returns an 8 segment IPv6 hex address
  141.  
  142. input: 128 bit RDATA string
  143. returns: x:x:x:x:x:x:x:x
  144.  
  145. * $dec_text = ipv6_n2d($ipv6addr);
  146. Takes an IPv6 RDATA string and returns a mixed hex - decimal IPv6
  147. address with the 6 uppermost chunks in hex and the lower 32 bits in
  148. dot-quad representation.
  149.  
  150. input: 128 bit RDATA string
  151. returns: x:x:x:x:x:x:d.d.d.d
  152.  
  153. * $ipv6naddr = inet_any2n($dotquad or $ipv6_text);
  154. This function converts a text IPv4 or IPv6 address in text format in
  155. any standard notation into a 128 bit IPv6 string address. It
  156. prefixes any dot-quad address (if found) with '::' and passes it to
  157. ipv6_aton.
  158.  
  159. input: dot-quad or rfc1844 address
  160. returns: 128 bit IPv6 string
  161.  
  162. * $rv = hasbits($bits128);
  163. This function returns true if there are one's present in the 128 bit
  164. string and false if all the bits are zero.
  165.  
  166. i.e. if (hasbits($bits128)) {
  167. &do_something;
  168. }
  169.  
  170. or if (hasbits($bits128 & $mask128) {
  171. &do_something;
  172. }
  173.  
  174. This allows the implementation of logical functions of the form of:
  175.  
  176. if ($bits128 & $mask128) {
  177. ...
  178.  
  179. input: 128 bit IPv6 string
  180. returns: true if any bits are present
  181.  
  182. * $rv = isIPv4($bits128);
  183. This function returns true if there are no on bits present in the
  184. IPv6 portion of the 128 bit string and false otherwise.
  185.  
  186. * $dotquad or $hex_text = inet_n2dx($ipv6naddr);
  187. This function does the right thing and returns the text for either a
  188. dot-quad IPv4 or a hex notation IPv6 address.
  189.  
  190. input: 128 bit IPv6 string
  191. returns: ddd.ddd.ddd.ddd
  192. or x:x:x:x:x:x:x:x
  193.  
  194. * $dotquad or $dec_text = inet_n2ad($ipv6naddr);
  195. This function does the right thing and returns the text for either a
  196. dot-quad IPv4 or a hex::decimal notation IPv6 address.
  197.  
  198. input: 128 bit IPv6 string
  199. returns: ddd.ddd.ddd.ddd
  200. or x:x:x:x:x:x:ddd.ddd.ddd.dd
  201.  
  202. * $ipv6naddr = ipv4to6($netaddr);
  203. Convert an ipv4 network address into an ipv6 network address.
  204.  
  205. input: 32 bit network address
  206. returns: 128 bit network address
  207.  
  208. * $ipv6naddr = mask4to6($netaddr);
  209. Convert an ipv4 network address/mask into an ipv6 network mask.
  210.  
  211. input: 32 bit network/mask address
  212. returns: 128 bit network/mask address
  213.  
  214. NOTE: returns the high 96 bits as one's
  215.  
  216. * $ipv6naddr = ipanyto6($netaddr);
  217. Similar to ipv4to6 except that this function takes either an IPv4 or
  218. IPv6 input and always returns a 128 bit IPv6 network address.
  219.  
  220. input: 32 or 128 bit network address
  221. returns: 128 bit network address
  222.  
  223. * $ipv6naddr = maskanyto6($netaddr);
  224. Similar to mask4to6 except that this function takes either an IPv4
  225. or IPv6 netmask and always returns a 128 bit IPv6 netmask.
  226.  
  227. input: 32 or 128 bit network mask
  228. returns: 128 bit network mask
  229.  
  230. * $netaddr = ipv6to4($pv6naddr);
  231. Truncate the upper 96 bits of a 128 bit address and return the lower
  232. 32 bits. Returns an IPv4 address as returned by inet_aton.
  233.  
  234. input: 128 bit network address
  235. returns: 32 bit inet_aton network address
  236.  
  237. * $bitsXn = shiftleft($bits128,$n);
  238. input: 128 bit string variable,
  239. number of shifts [optional]
  240. returns: bits X n shifts
  241.  
  242. NOTE: a single shift is performed
  243. if $n is not specified
  244.  
  245. * addconst($ipv6naddr,$signed_32con);
  246. Add a signed constant to a 128 bit string variable.
  247.  
  248. input: 128 bit IPv6 string,
  249. signed 32 bit integer
  250. returns: scalar carry
  251. array (carry, result)
  252.  
  253. * add128($ipv6naddr1,$ipv6naddr2);
  254. Add two 128 bit string variables.
  255.  
  256. input: 128 bit string var1,
  257. 128 bit string var2
  258. returns: scalar carry
  259. array (carry, result)
  260.  
  261. * sub128($ipv6naddr1,$ipv6naddr2);
  262. Subtract two 128 bit string variables.
  263.  
  264. input: 128 bit string var1,
  265. 128 bit string var2
  266. returns: scalar carry
  267. array (carry, result)
  268.  
  269. Note: The carry from this operation is the result of adding the
  270. one's complement of ARG2 +1 to the ARG1. It is logically NOT borrow.
  271.  
  272. i.e. if ARG1 >= ARG2 then carry = 1
  273. or if ARG1 < ARG2 then carry = 0
  274.  
  275. * ($spurious,$cidr) = notcontiguous($mask128);
  276. This function counts the bit positions remaining in the mask when
  277. the rightmost '0's are removed.
  278.  
  279. input: 128 bit netmask
  280. returns true if there are spurious
  281. zero bits remaining in the
  282. mask, false if the mask is
  283. contiguous one's,
  284. 128 bit cidr number
  285.  
  286. * $bcdtext = bin2bcd($bits128);
  287. Convert a 128 bit binary string into binary coded decimal text
  288. digits.
  289.  
  290. input: 128 bit string variable
  291. returns: string of bcd text digits
  292.  
  293. * $bits128 = bcd2bin($bcdtxt);
  294. Convert a bcd text string to 128 bit string variable
  295.  
  296. input: string of bcd text digits
  297. returns: 128 bit string variable
  298.  
  299. * $modetext = mode;
  300. Returns the operating mode of this module.
  301.  
  302. input: none
  303. returns: "Pure Perl"
  304. or "CC XS"
  305.  
  306. * ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME);
  307. Replacement for Perl's gethostbyname if Socket6 is available
  308.  
  309. * NetAddr::IP::Util::lower();
  310. Return IPv6 strings in lowercase.
  311.  
  312. * NetAddr::IP::Util::upper();
  313. Return IPv6 strings in uppercase. This is the default.
  314.  
  315. EXAMPLES
  316. # convert any textual IP address into a 128 bit vector
  317. #
  318. sub text2vec {
  319. my($anyIP,$anyMask) = @_;
  320.  
  321. # not IPv4 bit mask
  322. my $notiv4 = ipv6_aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::');
  323.  
  324. my $vecip = inet_any2n($anyIP);
  325. my $mask = inet_any2n($anyMask);
  326.  
  327. # extend mask bits for IPv4
  328. my $bits = 128; # default
  329. unless (hasbits($mask & $notiv4)) {
  330. $mask |= $notiv4;
  331. $bits = 32;
  332. }
  333. return ($vecip, $mask, $bits);
  334. }
  335.  
  336. ... alternate implementation, a little faster
  337.  
  338. sub text2vec {
  339. my($anyIP,$anyMask) = @_;
  340.  
  341. # not IPv4 bit mask
  342. my $notiv4 = ipv6_aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::');
  343.  
  344. my $vecip = inet_any2n($anyIP);
  345. my $mask = inet_any2n($anyMask);
  346.  
  347. # extend mask bits for IPv4
  348. my $bits = 128; # default
  349. if (isIPv4($mask)) {
  350. $mask |= $notiv4;
  351. $bits = 32;
  352. }
  353. return ($vecip, $mask, $bits);
  354. }
  355.  
  356. ... elsewhere
  357. $nip = {
  358. addr => $vecip,
  359. mask => $mask,
  360. bits => $bits,
  361. };
  362.  
  363. # return network and broadcast addresses from IP and Mask
  364. #
  365. sub netbroad {
  366. my($nip) = shift;
  367. my $notmask = ~ $nip->{mask};
  368. my $bcast = $nip->{addr} | $notmask;
  369. my $network = $nip->{addr} & $nip->{mask};
  370. return ($network, $broadcast);
  371. }
  372.  
  373. # check if address is within a network
  374. #
  375. sub within {
  376. my($nip,$net) = @_;
  377. my $addr = $nip->{addr}
  378. my($nw,$bc) = netbroad($net);
  379. # arg1 >= arg2, sub128 returns true
  380. return (sub128($addr,$nw) && sub128($bc,$addr))
  381. ? 1 : 0;
  382. }
  383.  
  384. # add a constant, wrapping at netblock boundaries
  385. # to subtract the constant, negate it before calling
  386. # 'addwrap' since 'addconst' will extend the sign bits
  387. #
  388. sub addwrap {
  389. my($nip,$const) = @_;
  390. my $mask = $nip->{addr};
  391. my $bits = $nip->{bits};
  392. my $notmask = ~ $mask;
  393. my $hibits = $addr & $mask;
  394. my $addr = addconst($addr,$const);
  395. my $wraponly = $addr & $notmask;
  396. my $newip = {
  397. addr => $hibits | $wraponly,
  398. mask => $mask,
  399. bits => $bits,
  400. };
  401. # bless $newip as appropriate
  402. return $newip;
  403. }
  404.  
  405. EXPORT_OK
  406. inet_aton
  407. inet_ntoa
  408. ipv6_aton
  409. ipv6_n2x
  410. ipv6_n2d
  411. inet_any2n
  412. hasbits
  413. isIPv4
  414. inet_n2dx
  415. inet_n2ad
  416. ipv4to6
  417. mask4to6
  418. ipanyto6
  419. maskanyto6
  420. ipv6to4
  421. shiftleft
  422. addconst
  423. add128
  424. sub128
  425. notcontiguous
  426. bin2bcd
  427. bcd2bin
  428. mode
  429. naip_gethostbyname
  430.  
  431. AUTHOR
  432. Michael Robinton <michael@bizsystems.com>
  433.  
  434. ACKNOWLEDGMENTS
  435. The following functions are used in whole or in part as include files to
  436. Util.xs. The copyright is include in the file.
  437.  
  438. file: function:
  439.  
  440. miniSocket.inc inet_aton, inet_ntoa
  441.  
  442. inet_aton, inet_ntoa are from the perl-5.8.0 release by Larry Wall,
  443. copyright 1989-2002. inet_aton, inet_ntoa code is current through
  444. perl-5.9.3 release. Thank you Larry for making PERL possible for all of
  445. us.
  446.  
  447. COPYRIGHT
  448. Copyright 2003 - 2010, Michael Robinton <michael@bizsystems.com>
  449.  
  450. LICENSE AND WARRANTY
  451.  
  452. This software is (c) Michael Robinton. It can be used under the terms of
  453. the perl artistic license provided that proper credit for the work of
  454. the author is preserved in the form of this copyright notice and license
  455. for this module.
  456.  
  457. No warranty of any kind is expressed or implied, by using it you accept
  458. any and all the liability.
  459.  
  460. AUTHOR
  461. Michael Robinton <michael@bizsystems.com>
  462.