Newer
Older
NetAddr-IP / Lite / Util / lib / NetAddr / IP / InetBase.pm
@Michael Robinton Michael Robinton on 21 Oct 2014 18 KB Import of MIKER/NetAddr-IP-4.065 from CPAN.
  1. #!/usr/bin/perl
  2. package NetAddr::IP::InetBase;
  3.  
  4. use strict;
  5. #use diagnostics;
  6. #use lib qw(blib lib);
  7.  
  8. use vars qw($VERSION @EXPORT_OK @ISA %EXPORT_TAGS $Mode);
  9. use AutoLoader qw(AUTOLOAD);
  10. require Exporter;
  11.  
  12. @ISA = qw(Exporter);
  13.  
  14. $VERSION = do { my @r = (q$Revision: 0.08 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
  15.  
  16. @EXPORT_OK = qw(
  17. inet_aton
  18. inet_ntoa
  19. ipv6_aton
  20. ipv6_ntoa
  21. ipv6_n2x
  22. ipv6_n2d
  23. inet_any2n
  24. inet_n2dx
  25. inet_n2ad
  26. inet_ntop
  27. inet_pton
  28. packzeros
  29. isIPv4
  30. isNewIPv4
  31. isAnyIPv4
  32. AF_INET
  33. AF_INET6
  34. fake_AF_INET6
  35. fillIPv4
  36. );
  37.  
  38. %EXPORT_TAGS = (
  39. all => [@EXPORT_OK],
  40. ipv4 => [qw(
  41. inet_aton
  42. inet_ntoa
  43. fillIPv4
  44. )],
  45. ipv6 => [qw(
  46. ipv6_aton
  47. ipv6_ntoa
  48. ipv6_n2x
  49. ipv6_n2d
  50. inet_any2n
  51. inet_n2dx
  52. inet_n2ad
  53. inet_pton
  54. inet_ntop
  55. packzeros
  56. )],
  57. );
  58.  
  59. # prototypes
  60. sub inet_ntoa;
  61. sub ipv6_aton;
  62. sub ipv6_ntoa;
  63. sub inet_any2n($);
  64. sub inet_n2dx($);
  65. sub inet_n2ad($);
  66. sub _inet_ntop;
  67. sub _inet_pton;
  68.  
  69. my $emulateAF_INET6 = 0;
  70.  
  71. { no warnings 'once';
  72.  
  73. *packzeros = \&_packzeros;
  74.  
  75. ## dynamic configuraton for IPv6
  76.  
  77. require Socket;
  78.  
  79. *AF_INET = \&Socket::AF_INET;
  80.  
  81. if (eval { AF_INET6() } ) {
  82. *AF_INET6 = \&Socket::AF_INET6;
  83. $emulateAF_INET6 = -1; # have it, remind below
  84. }
  85. if (eval{ require Socket6 } ) {
  86. import Socket6 qw(
  87. inet_pton
  88. inet_ntop
  89. );
  90. unless ($emulateAF_INET6) {
  91. *AF_INET6 = \&Socket6::AF_INET6;
  92. }
  93. $emulateAF_INET6 = 0; # clear, have it from elsewhere or here
  94. } else {
  95. unless ($emulateAF_INET6) { # unlikely at this point
  96. if ($^O =~ /(?:free|dragon.+)bsd/i) { # FreeBSD, DragonFlyBSD
  97. $emulateAF_INET6 = 28;
  98. } elsif ($^O =~ /bsd/i) { # other BSD flavors like NetBDS, OpenBSD, BSD
  99. $emulateAF_INET6 = 24;
  100. } elsif ($^O =~ /(?:darwin|mac)/i) { # Mac OS X
  101. $emulateAF_INET6 = 30;
  102. } elsif ($^O =~ /win/i) { # Windows
  103. $emulateAF_INET6 = 23;
  104. } elsif ($^O =~ /(?:solaris|sun)/i) { # Sun box
  105. $emulateAF_INET6 = 26;
  106. } else { # use linux default
  107. $emulateAF_INET6 = 10;
  108. }
  109. *AF_INET6 = sub { $emulateAF_INET6; };
  110. } else {
  111. $emulateAF_INET6 = 0; # clear, have it from elsewhere
  112. }
  113. *inet_pton = \&_inet_pton;
  114. *inet_ntop = \&_inet_ntop;
  115. }
  116.  
  117. } # end no warnings 'once'
  118.  
  119. sub fake_AF_INET6 {
  120. return $emulateAF_INET6;
  121. }
  122.  
  123. # allow user to choose upper or lower case
  124. BEGIN {
  125. use vars qw($n2x_format $n2d_format);
  126. $n2x_format = "%x:%x:%x:%x:%x:%x:%x:%x";
  127. $n2d_format = "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d";
  128. }
  129.  
  130. my $case = 0; # default lower case
  131.  
  132. sub upper { $n2x_format = uc($n2x_format); $n2d_format = uc($n2d_format); $case = 1; }
  133. sub lower { $n2x_format = lc($n2x_format); $n2d_format = lc($n2d_format); $case = 0; }
  134.  
  135. sub ipv6_n2x {
  136. die "Bad arg length for 'ipv6_n2x', length is ". length($_[0]) ." should be 16"
  137. unless length($_[0]) == 16;
  138. return sprintf($n2x_format,unpack("n8",$_[0]));
  139. }
  140.  
  141. sub ipv6_n2d {
  142. die "Bad arg length for 'ipv6_n2d', length is ". length($_[0]) ." should be 16"
  143. unless length($_[0]) == 16;
  144. my @hex = (unpack("n8",$_[0]));
  145. $hex[9] = $hex[7] & 0xff;
  146. $hex[8] = $hex[7] >> 8;
  147. $hex[7] = $hex[6] & 0xff;
  148. $hex[6] >>= 8;
  149. return sprintf($n2d_format,@hex);
  150. }
  151.  
  152. # if Socket lib is broken in some way, check for overange values
  153. #
  154. #my $overange = yinet_aton('256.1') ? 1:0;
  155. #my $overange = gethostbyname('256.1') ? 1:0;
  156.  
  157. #sub inet_aton {
  158. # unless (! $overange || $_[0] =~ /[^0-9\.]/) { # hostname
  159. # my @dq = split(/\./,$_[0]);
  160. # foreach (@dq) {
  161. # return undef if $_ > 255;
  162. # }
  163. # }
  164. # scalar gethostbyname($_[0]);
  165. #}
  166.  
  167. sub fillIPv4 {
  168. my $host = $_[0];
  169. return undef unless defined $host;
  170. if ($host =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/) {
  171. if (defined $4) {
  172. return undef unless
  173. $1 >= 0 && $1 < 256 &&
  174. $2 >= 0 && $2 < 256 &&
  175. $3 >= 0 && $3 < 256 &&
  176. $4 >= 0 && $4 < 256;
  177. $host = $1.'.'.$2.'.'.$3.'.'.$4;
  178. # return pack('C4',$1,$2,$3,$4);
  179. # $host = ($1 << 24) + ($2 << 16) + ($3 << 8) + $4;
  180. } elsif (defined $3) {
  181. return undef unless
  182. $1 >= 0 && $1 < 256 &&
  183. $2 >= 0 && $2 < 256 &&
  184. $3 >= 0 && $3 < 256;
  185. $host = $1.'.'.$2.'.0.'.$3
  186. # return pack('C4',$1,$2,0,$3);
  187. # $host = ($1 << 24) + ($2 << 16) + $3;
  188. } elsif (defined $2) {
  189. return undef unless
  190. $1 >= 0 && $1 < 256 &&
  191. $2 >= 0 && $2 < 256;
  192. $host = $1.'.0.0.'.$2;
  193. # return pack('C4',$1,0,0,$2);
  194. # $host = ($1 << 24) + $2;
  195. } else {
  196. $host = '0.0.0.'.$1;
  197. # return pack('C4',0,0,0,$1);
  198. # $host = $1;
  199. }
  200. # return pack('N',$host);
  201. }
  202. $host;
  203. }
  204.  
  205. sub inet_aton {
  206. my $host = fillIPv4($_[0]);
  207. return $host ? scalar gethostbyname($host) : undef;
  208. }
  209.  
  210. #sub inet_aton {
  211. # my $host = $_[0];
  212. # return undef unless defined $host;
  213. # if ($host =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/) {
  214. # if (defined $4) {
  215. # return undef unless
  216. # $1 >= 0 && $1 < 256 &&
  217. # $2 >= 0 && $2 < 256 &&
  218. # $3 >= 0 && $3 < 256 &&
  219. # $4 >= 0 && $4 < 256;
  220. # return pack('C4',$1,$2,$3,$4);
  221. ## $host = ($1 << 24) + ($2 << 16) + ($3 << 8) + $4;
  222. # } elsif (defined $3) {
  223. # return undef unless
  224. # $1 >= 0 && $1 < 256 &&
  225. # $2 >= 0 && $2 < 256 &&
  226. # $3 >= 0 && $3 < 256;
  227. # return pack('C4',$1,$2,0,$3);
  228. ## $host = ($1 << 24) + ($2 << 16) + $3;
  229. # } elsif (defined $2) {
  230. # return undef unless
  231. # $1 >= 0 && $1 < 256 &&
  232. # $2 >= 0 && $2 < 256;
  233. # return pack('C4',$1,0,0,$2);
  234. ## $host = ($1 << 24) + $2;
  235. # } else {
  236. # return pack('C4',0,0,0,$1);
  237. ## $host = $1;
  238. # }
  239. ## return pack('N',$host);
  240. # }
  241. # scalar gethostbyname($host);
  242. #}
  243.  
  244. my $_zero = pack('L4',0,0,0,0);
  245. my $_ipv4mask = pack('L4',0xffffffff,0xffffffff,0xffffffff,0);
  246.  
  247. sub isIPv4 {
  248. if (length($_[0]) != 16) {
  249. my $sub = (caller(1))[3] || (caller(0))[3];
  250. die "Bad arg length for $sub, length is ". (length($_[0]) *8) .", should be 128";
  251. }
  252. return ($_[0] & $_ipv4mask) eq $_zero
  253. ? 1 : 0;
  254. }
  255.  
  256. my $_newV4compat = pack('N4',0,0,0xffff,0);
  257.  
  258. sub isNewIPv4 {
  259. my $naddr = $_[0] ^ $_newV4compat;
  260. return isIPv4($naddr);
  261. }
  262.  
  263. sub isAnyIPv4 {
  264. my $naddr = $_[0];
  265. my $rv = isIPv4($_[0]);
  266. return $rv if $rv;
  267. return isNewIPv4($naddr);
  268. }
  269.  
  270. sub DESTROY {};
  271.  
  272. sub import {
  273. if (grep { $_ eq ':upper' } @_) {
  274. upper();
  275. @_ = grep { $_ ne ':upper' } @_;
  276. }
  277. NetAddr::IP::InetBase->export_to_level(1,@_);
  278. }
  279.  
  280. 1;
  281.  
  282. __END__
  283.  
  284. =head1 NAME
  285.  
  286. NetAddr::IP::InetBase -- IPv4 and IPV6 utilities
  287.  
  288. =head1 SYNOPSIS
  289.  
  290. use NetAddr::IP::Base qw(
  291. :upper
  292. inet_aton
  293. inet_ntoa
  294. ipv6_aton
  295. ipv6_ntoa
  296. ipv6_n2x
  297. ipv6_n2d
  298. inet_any2n
  299. inet_n2dx
  300. inet_n2ad
  301. inet_pton
  302. inet_ntop
  303. packzeros
  304. isIPv4
  305. isNewIPv4
  306. isAnyIPv4
  307. AF_INET
  308. AF_INET6
  309. fake_AF_INET6
  310. fillIPv4
  311. );
  312.  
  313. use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math)
  314.  
  315. :ipv4 => inet_aton, inet_ntoa, fillIPv4
  316.  
  317. :ipv6 => ipv6_aton, ipv6_ntoa,ipv6_n2x, ipv6_n2d,
  318. inet_any2n, inet_n2dx, inet_n2ad
  319. inet_pton, inet_ntop, packzeros
  320.  
  321. $dotquad = inet_ntoa($netaddr);
  322. $netaddr = inet_aton($dotquad);
  323. $ipv6naddr = ipv6_aton($ipv6_text);
  324. $ipv6_text = ipv6_ntoa($ipv6naddr);
  325. $hex_text = ipv6_n2x($ipv6naddr);
  326. $dec_text = ipv6_n2d($ipv6naddr);
  327. $ipv6naddr = inet_any2n($dotquad or $ipv6_text);
  328. $dotquad or $hex_text = inet_n2dx($ipv6naddr);
  329. $dotquad or $dec_text = inet_n2ad($ipv6naddr);
  330. $netaddr = inet_pton($AF_family,$text_addr);
  331. $text_addr = inet_ntop($AF_family,$netaddr);
  332. $hex_text = packzeros($hex_text);
  333. $rv = isIPv4($bits128);
  334. $rv = isNewIPv4($bits128);
  335. $rv = isAnyIPv4($bits128);
  336. $constant = AF_INET();
  337. $constant = AF_INET6();
  338. $trueif = fake_AF_INET6();
  339. $ip_filled = fillIPv4($shortIP);
  340.  
  341. NetAddr::IP::InetBase::lower();
  342. NetAddr::IP::InetBase::upper();
  343.  
  344. =head1 INSTALLATION
  345.  
  346. Un-tar the distribution in an appropriate directory and type:
  347.  
  348. perl Makefile.PL
  349. make
  350. make test
  351. make install
  352.  
  353. =head1 DESCRIPTION
  354.  
  355. B<NetAddr::IP::InetBase> provides a suite network of conversion functions
  356. written in pure Perl for converting both IPv4 and IPv6 addresses to
  357. and from network address format and text format.
  358.  
  359. The IPv6 functions support all rfc1884 formats.
  360.  
  361. i.e. x:x:x:x:x:x:x:x:x
  362. x:x:x:x:x:x:x:d.d.d.d
  363. ::x:x:x
  364. ::x:d.d.d.d
  365. and so on...
  366.  
  367. =over 4
  368.  
  369. =item * $dotquad = inet_ntoa($netaddr);
  370.  
  371. Convert a packed IPv4 network address to a dot-quad IP address.
  372.  
  373. input: packed network address
  374. returns: IP address i.e. 10.4.12.123
  375.  
  376. =cut
  377.  
  378. sub inet_ntoa {
  379. die 'Bad arg length for '. __PACKAGE__ ."::inet_ntoa, length is ". length($_[0]) ." should be 4"
  380. unless length($_[0]) == 4;
  381. my @hex = (unpack("n2",$_[0]));
  382. $hex[3] = $hex[1] & 0xff;
  383. $hex[2] = $hex[1] >> 8;
  384. $hex[1] = $hex[0] & 0xff;
  385. $hex[0] >>= 8;
  386. return sprintf("%d.%d.%d.%d",@hex);
  387. }
  388.  
  389. =item * $netaddr = inet_aton($dotquad);
  390.  
  391. Convert a dot-quad IP address into an IPv4 packed network address.
  392.  
  393. input: IP address i.e. 192.5.16.32
  394. returns: packed network address
  395.  
  396. =item * $ipv6addr = ipv6_aton($ipv6_text);
  397.  
  398. Takes an IPv6 address of the form described in rfc1884
  399. and returns a 128 bit binary RDATA string.
  400.  
  401. input: ipv6 text
  402. returns: 128 bit RDATA string
  403.  
  404. =cut
  405.  
  406. sub ipv6_aton {
  407. my($ipv6) = @_;
  408. return undef unless $ipv6;
  409. local($1,$2,$3,$4,$5);
  410. if ($ipv6 =~ /^(.*:)(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) { # mixed hex, dot-quad
  411. return undef if $2 > 255 || $3 > 255 || $4 > 255 || $5 > 255;
  412. $ipv6 = sprintf("%s%X%02X:%X%02X",$1,$2,$3,$4,$5); # convert to pure hex
  413. }
  414. my $c;
  415. return undef if
  416. $ipv6 =~ /[^:0-9a-fA-F]/ || # non-hex character
  417. (($c = $ipv6) =~ s/::/x/ && $c =~ /(?:x|:):/) || # double :: ::?
  418. $ipv6 =~ /[0-9a-fA-F]{5,}/; # more than 4 digits
  419. $c = $ipv6 =~ tr/:/:/; # count the colons
  420. return undef if $c < 7 && $ipv6 !~ /::/;
  421. if ($c > 7) { # strip leading or trailing ::
  422. return undef unless
  423. $ipv6 =~ s/^::/:/ ||
  424. $ipv6 =~ s/::$/:/;
  425. return undef if --$c > 7;
  426. }
  427. while ($c++ < 7) { # expand compressed fields
  428. $ipv6 =~ s/::/:::/;
  429. }
  430. $ipv6 .= 0 if $ipv6 =~ /:$/;
  431. my @hex = split(/:/,$ipv6);
  432. foreach(0..$#hex) {
  433. $hex[$_] = hex($hex[$_] || 0);
  434. }
  435. pack("n8",@hex);
  436. }
  437.  
  438. =item * $ipv6text = ipv6_ntoa($ipv6naddr);
  439.  
  440. Convert a 128 bit binary IPv6 address to compressed rfc 1884
  441. text representation.
  442.  
  443. input: 128 bit RDATA string
  444. returns: ipv6 text
  445.  
  446. =cut
  447.  
  448. sub ipv6_ntoa {
  449. return inet_ntop(AF_INET6(),$_[0]);
  450. }
  451.  
  452. =item * $hex_text = ipv6_n2x($ipv6addr);
  453.  
  454. Takes an IPv6 RDATA string and returns an 8 segment IPv6 hex address
  455.  
  456. input: 128 bit RDATA string
  457. returns: x:x:x:x:x:x:x:x
  458.  
  459. Note: this function does NOT compress adjacent
  460. strings of 0:0:0:0 into the :: format
  461.  
  462. =item * $dec_text = ipv6_n2d($ipv6addr);
  463.  
  464. Takes an IPv6 RDATA string and returns a mixed hex - decimal IPv6 address
  465. with the 6 uppermost chunks in hex and the lower 32 bits in dot-quad
  466. representation.
  467.  
  468. input: 128 bit RDATA string
  469. returns: x:x:x:x:x:x:d.d.d.d
  470.  
  471. Note: this function does NOT compress adjacent
  472. strings of 0:0:0:0 into the :: format
  473.  
  474. =item * $ipv6naddr = inet_any2n($dotquad or $ipv6_text);
  475.  
  476. This function converts a text IPv4 or IPv6 address in text format in any
  477. standard notation into a 128 bit IPv6 string address. It prefixes any
  478. dot-quad address (if found) with '::' and passes it to B<ipv6_aton>.
  479.  
  480. input: dot-quad or rfc1844 address
  481. returns: 128 bit IPv6 string
  482.  
  483. =cut
  484.  
  485. sub inet_any2n($) {
  486. my($addr) = @_;
  487. $addr = '' unless $addr;
  488. $addr = '::' . $addr
  489. unless $addr =~ /:/;
  490. return ipv6_aton($addr);
  491. }
  492.  
  493. =item * $dotquad or $hex_text = inet_n2dx($ipv6naddr);
  494.  
  495. This function B<does the right thing> and returns the text for either a
  496. dot-quad IPv4 or a hex notation IPv6 address.
  497.  
  498. input: 128 bit IPv6 string
  499. returns: ddd.ddd.ddd.ddd
  500. or x:x:x:x:x:x:x:x
  501.  
  502. Note: this function does NOT compress adjacent
  503. strings of 0:0:0:0 into the :: format
  504.  
  505. =cut
  506.  
  507. sub inet_n2dx($) {
  508. my($nadr) = @_;
  509. if (isAnyIPv4($nadr)) {
  510. local $1;
  511. ipv6_n2d($nadr) =~ /([^:]+)$/;
  512. return $1;
  513. }
  514. return ipv6_n2x($nadr);
  515. }
  516.  
  517. =item * $dotquad or $dec_text = inet_n2ad($ipv6naddr);
  518.  
  519. This function B<does the right thing> and returns the text for either a
  520. dot-quad IPv4 or a hex::decimal notation IPv6 address.
  521.  
  522. input: 128 bit IPv6 string
  523. returns: ddd.ddd.ddd.ddd
  524. or x:x:x:x:x:x:ddd.ddd.ddd.dd
  525.  
  526. Note: this function does NOT compress adjacent
  527. strings of 0:0:0:0 into the :: format
  528.  
  529. =cut
  530.  
  531. sub inet_n2ad($) {
  532. my($nadr) = @_;
  533. my $addr = ipv6_n2d($nadr);
  534. return $addr unless isAnyIPv4($nadr);
  535. local $1;
  536. $addr =~ /([^:]+)$/;
  537. return $1;
  538. }
  539.  
  540. =item * $netaddr = inet_pton($AF_family,$text_addr);
  541.  
  542. This function takes an IP address in IPv4 or IPv6 text format and converts it into
  543. binary format. The type of IP address conversion is controlled by the FAMILY
  544. argument.
  545.  
  546. NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it
  547. is present on this host.
  548.  
  549. =cut
  550.  
  551. sub _inet_pton {
  552. my($af,$ip) = @_;
  553. die 'Bad address family for '. __PACKAGE__ ."::inet_pton, got $af"
  554. unless $af == AF_INET6() || $af == AF_INET();
  555. if ($af == AF_INET()) {
  556. inet_aton($ip);
  557. } else {
  558. ipv6_aton($ip);
  559. }
  560. }
  561.  
  562. =item * $text_addr = inet_ntop($AF_family,$netaddr);
  563.  
  564. This function takes and IP address in binary format and converts it into
  565. text format. The type of IP address conversion is controlled by the FAMILY
  566. argument.
  567.  
  568. NOTE: inet_ntop ALWAYS returns lowercase characters.
  569.  
  570. NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it
  571. is present on this host.
  572.  
  573. =cut
  574.  
  575. sub _inet_ntop {
  576. my($af,$naddr) = @_;
  577. die 'Unsupported address family for '. __PACKAGE__ ."::inet_ntop, af is $af"
  578. unless $af == AF_INET6() || $af == AF_INET();
  579. if ($af == AF_INET()) {
  580. inet_ntoa($naddr);
  581. } else {
  582. return ($case)
  583. ? lc packzeros(ipv6_n2x($naddr))
  584. : _packzeros(ipv6_n2x($naddr));
  585. }
  586. }
  587.  
  588. =item * $hex_text = packzeros($hex_text);
  589.  
  590. This function optimizes and rfc 1884 IPv6 hex address to reduce the number of
  591. long strings of zero bits as specified in rfc 1884, 2.2 (2) by substituting
  592. B<::> for the first occurence of the longest string of zeros in the address.
  593.  
  594. =cut
  595.  
  596. sub _packzeros {
  597. my $x6 = shift;
  598. if ($x6 =~ /\:\:/) { # already contains ::
  599. # then re-optimize
  600. $x6 = ($x6 =~ /\:\d+\.\d+\.\d+\.\d+/) # ipv4 notation ?
  601. ? ipv6_n2d(ipv6_aton($x6))
  602. : ipv6_n2x(ipv6_aton($x6));
  603. }
  604. $x6 = ':'. lc $x6; # prefix : & always lower case
  605. my $d = '';
  606. if ($x6 =~ /(.+\:)(\d+\.\d+\.\d+\.\d+)/) { # if contains dot quad
  607. $x6 = $1; # save hex piece
  608. $d = $2; # and dot quad piece
  609. }
  610. $x6 .= ':'; # suffix :
  611. $x6 =~ s/\:0+/\:0/g; # compress strings of 0's to single '0'
  612. $x6 =~ s/\:0([1-9a-f]+)/\:$1/g; # eliminate leading 0's in hex strings
  613. my @x = $x6 =~ /(?:\:0)*/g; # split only strings of :0:0..."
  614.  
  615. my $m = 0;
  616. my $i = 0;
  617.  
  618. for (0..$#x) { # find next longest pattern :0:0:0...
  619. my $len = length($x[$_]);
  620. next unless $len > $m;
  621. $m = $len;
  622. $i = $_; # index to first longest pattern
  623. }
  624.  
  625. if ($m > 2) { # there was a string of 2 or more zeros
  626. $x6 =~ s/$x[$i]/\:/; # replace first longest :0:0:0... with "::"
  627. unless ($i) { # if it is the first match, $i = 0
  628. $x6 = substr($x6,0,-1); # keep the leading ::, remove trailing ':'
  629. } else {
  630. $x6 = substr($x6,1,-1); # else remove leading & trailing ':'
  631. }
  632. $x6 .= ':' unless $x6 =~ /\:\:/; # restore ':' if match and we can't see it, implies trailing '::'
  633. } else { # there was no match
  634. $x6 = substr($x6,1,-1); # remove leading & trailing ':'
  635. }
  636. $x6 .= $d; # append digits if any
  637. return $case
  638. ? uc $x6
  639. : $x6;
  640. }
  641.  
  642. =item * $ipv6naddr = ipv4to6($netaddr);
  643.  
  644. Convert an ipv4 network address into an ipv6 network address.
  645.  
  646. input: 32 bit network address
  647. returns: 128 bit network address
  648.  
  649. =item * $rv = isIPv4($bits128);
  650.  
  651. This function returns true if there are no on bits present in the IPv6
  652. portion of the 128 bit string and false otherwise.
  653.  
  654. i.e. the address must be of the form - ::d.d.d.d
  655.  
  656. Note: this is an old and deprecated ipV4 compatible ipV6 address
  657. =item * $rv = isNewIPv4($bits128);
  658.  
  659. This function return true if the IPv6 128 bit string is of the form
  660.  
  661. ::ffff:d.d.d.d
  662.  
  663. =item * $rv = isAnyIPv4($bits128);
  664.  
  665. This function return true if the IPv6 bit string is of the form
  666.  
  667. ::d.d.d.d or ::ffff:d.d.d.d
  668.  
  669. =item * NetAddr::IP::InetBase::lower();
  670.  
  671. Return IPv6 strings in lowercase. This is the default.
  672.  
  673. =item * NetAddr::IP::InetBase::upper();
  674.  
  675. Return IPv6 strings in uppercase.
  676.  
  677. The default may be set to uppercase when the module is loaded by invoking
  678. the TAG :upper. i.e.
  679.  
  680. use NetAddr::IP::InetBase qw( :upper );
  681.  
  682. =item * $constant = AF_INET;
  683.  
  684. This function returns the system value for AF_INET.
  685.  
  686. =item * $constant = AF_INET6;
  687.  
  688. AF_INET6 is sometimes present in the Socket library and always present in the Socket6 library. When the Socket
  689. library does not contain AF_INET6 and when Socket6 is not present, a place holder value is C<guessed> based on
  690. the underlying host operating system. See B<fake_AF_INET6> below.
  691.  
  692. NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it
  693. is present on this host.
  694.  
  695. =item * $trueif = fake_AF_INET6;
  696.  
  697. This function return FALSE if AF_INET6 is provided by Socket or Socket6. Otherwise, it returns the best guess
  698. value based on name of the host operating system.
  699.  
  700. =item * $ip_filled = fillIPv4($shortIP);
  701.  
  702. This function converts IPv4 addresses of the form 127.1 to the long form
  703. 127.0.0.1
  704.  
  705. If the function is passed an argument that does not match the form of an IP
  706. address, the original argument is returned. i.e. pass it a hostname or a
  707. short IP and it will return a hostname or a filled IP.
  708.  
  709. =back
  710.  
  711. =head1 EXPORT_OK
  712.  
  713. :upper
  714. inet_aton
  715. inet_ntoa
  716. ipv6_aton
  717. ipv6_ntoa
  718. ipv6_n2x
  719. ipv6_n2d
  720. inet_any2n
  721. inet_n2dx
  722. inet_n2ad
  723. inet_pton
  724. inet_ntop
  725. packzeros
  726. isIPv4
  727. isNewIPv4
  728. isAnyIPv4
  729. AF_INET
  730. AF_INET6
  731. fake_AF_INET6
  732. fillIPv4
  733.  
  734. =head1 %EXPORT_TAGS
  735.  
  736. :all
  737. :ipv4
  738. :ipv6
  739. :upper
  740.  
  741. =head1 AUTHOR
  742.  
  743. Michael Robinton <michael@bizsystems.com>
  744.  
  745. =head1 COPYRIGHT
  746.  
  747. Copyright 2003 - 2012, Michael Robinton E<lt>michael@bizsystems.comE<gt>
  748.  
  749. All rights reserved.
  750.  
  751. This program is free software; you can redistribute it and/or modify
  752. it under the terms of either:
  753.  
  754. a) the GNU General Public License as published by the Free
  755. Software Foundation; either version 2, or (at your option) any
  756. later version, or
  757.  
  758. b) the "Artistic License" which comes with this distribution.
  759.  
  760. This program is distributed in the hope that it will be useful,
  761. but WITHOUT ANY WARRANTY; without even the implied warranty of
  762. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
  763. the GNU General Public License or the Artistic License for more details.
  764.  
  765. You should have received a copy of the Artistic License with this
  766. distribution, in the file named "Artistic". If not, I'll be glad to provide
  767. one.
  768.  
  769. You should also have received a copy of the GNU General Public License
  770. along with this program in the file named "Copying". If not, write to the
  771.  
  772. Free Software Foundation, Inc.,
  773. 51 Franklin Street, Fifth Floor
  774. Boston, MA 02110-1301 USA
  775.  
  776. or visit their web page on the internet at:
  777.  
  778. http://www.gnu.org/copyleft/gpl.html.
  779.  
  780. =head1 AUTHOR
  781.  
  782. Michael Robinton <michael@bizsystems.com>
  783.  
  784. =head1 SEE ALSO
  785.  
  786. NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::Util(3)
  787.  
  788. =cut
  789.  
  790. 1;