Subversion Repositories vgwhois

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. package GWhoIs::IPv6;
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. # install with "cpan Net::IP" or "aptitude install libnet-ip-perl"
  7. use Net::IP;
  8.  
  9. # at Debian: install with "aptitude install libmath-bigint-gmp-perl"
  10. use Math::BigInt;
  11.  
  12. # %v6pattern = GWhoIs::IPv6::getpatternv6()
  13. sub GWhoIs::IPv6::getpatternv6 {
  14.         my (%pattern);
  15.         my ($method,$host,$additional,$cline,$line,$rehost,$readditional);
  16.  
  17.         foreach my $patternfile (GWhoIs::Core::getpatternfiles()) {
  18.                 open(PATTERN,"<$patternfile") || die "Cannot open $patternfile. STOP.\n";
  19.  
  20.                 while ( defined($line = <PATTERN>) ) {
  21.                         # chomp $line;
  22.                         $line = GWhoIs::Utils::trim($line);
  23.  
  24.                         if ( $line =~ /^#/ ) {                       # comment
  25.                         } elsif ( ($cline) = $line =~ /^:(.*)$/ ) {  # method declaration
  26.                                 ($method,$host,$additional) = split(/\|/,$cline,3);
  27.                                 $method=''     if !defined $method;
  28.                                 $host=''       if !defined $host;
  29.                                 $additional='' if !defined $additional;
  30.                         } elsif (( $line =~ /^=/ ) && ($line =~ /:/)) { # do not read IPv4 lines (not containing ':')
  31.                                 ($rehost,$readditional) = GWhoIs::Core::methodpatternregex('',$host,$additional,$line);
  32.  
  33.                                 # Store the IP inside the CIDR as integer-notation.
  34.                                 ($line) = $line =~ /^=(.*)$/;           # remove leading "="
  35.                                 my ($ipv6,$bits) = split(/\//,$line,2); # split into IP address and CIDR
  36.                                 $ipv6 = Net::IP::ip_expand_address($ipv6, 6);    # Expand the IP address in case it uses nested syntax or something
  37.                                 $bits = 128 if ($bits eq '');           # if no CIDR was found, assume it is a single IPv6 address
  38.                                 my ($ipa,$ipb,$ipc,$ipd,$ipe,$ipf,$ipg,$iph) = split(/:/,$ipv6,8);
  39.                                 $ipa = defined $ipa ? Math::BigInt->new(hex($ipa)) : 0;
  40.                                 $ipb = defined $ipb ? Math::BigInt->new(hex($ipb)) : 0;
  41.                                 $ipc = defined $ipc ? Math::BigInt->new(hex($ipc)) : 0;
  42.                                 $ipd = defined $ipd ? Math::BigInt->new(hex($ipd)) : 0;
  43.                                 $ipe = defined $ipe ? Math::BigInt->new(hex($ipe)) : 0;
  44.                                 $ipf = defined $ipf ? Math::BigInt->new(hex($ipf)) : 0;
  45.                                 $ipg = defined $ipg ? Math::BigInt->new(hex($ipg)) : 0;
  46.                                 $iph = defined $iph ? Math::BigInt->new(hex($iph)) : 0;
  47.                                 my $ip = $ipa<<112|$ipb<<96|$ipc<<80|$ipd<<64|$ipe<<48|$ipf<<32|$ipg<<16|$iph;
  48.  
  49.                                 my $cidr = "$ip/$bits";
  50.  
  51.                                 $pattern{$cidr}{'method'} = $method;
  52.                                 $pattern{$cidr}{'host'}   = $rehost;
  53.                                 $pattern{$cidr}{'add'}    = $readditional;
  54.                         }
  55.                 }
  56.         }
  57.         return (%pattern);
  58. }
  59.  
  60.  
  61. # ($method, $host, $additional) = GWhoIs::IPv6::getmethodv6($ipv6);
  62. sub GWhoIs::IPv6::getmethodv6 {
  63.         my ($ipv6) = @_;
  64.         $ipv6 = Net::IP::ip_expand_address($ipv6, 6);
  65.         my ($ipa,$ipb,$ipc,$ipd,$ipe,$ipf,$ipg,$iph) = split(/:/,$ipv6,8);
  66.  
  67.         $ipa = defined $ipa ? Math::BigInt->new(hex($ipa)) : 0;
  68.         $ipb = defined $ipb ? Math::BigInt->new(hex($ipb)) : 0;
  69.         $ipc = defined $ipc ? Math::BigInt->new(hex($ipc)) : 0;
  70.         $ipd = defined $ipd ? Math::BigInt->new(hex($ipd)) : 0;
  71.         $ipe = defined $ipe ? Math::BigInt->new(hex($ipe)) : 0;
  72.         $ipf = defined $ipf ? Math::BigInt->new(hex($ipf)) : 0;
  73.         $ipg = defined $ipg ? Math::BigInt->new(hex($ipg)) : 0;
  74.         $iph = defined $iph ? Math::BigInt->new(hex($iph)) : 0;
  75.  
  76.         my ($ip, $bits, $netmask, $method, $host, $additional, %pattern);
  77.  
  78.         $ip      = $ipa<<112|$ipb<<96|$ipc<<80|$ipd<<64|$ipe<<48|$ipf<<32|$ipg<<16|$iph;
  79.         $netmask = Math::BigInt->new(65536)**8-1;
  80.         %pattern = GWhoIs::IPv6::getpatternv6();
  81.         $method  = '';
  82.  
  83.         for ($bits=128; $bits>=0 && $method eq ''; $bits--) {
  84.                 $ip        = $ip & $netmask;
  85.                 $netmask <<= 1;
  86.  
  87.                 my $cidr = "$ip/$bits";
  88.  
  89.                 $method     = $pattern{$cidr}{'method'} if defined $pattern{$cidr}{'method'};
  90.                 $host       = $pattern{$cidr}{'host'}   if defined $pattern{$cidr}{'host'};
  91.                 $additional = $pattern{$cidr}{'add'}    if defined $pattern{$cidr}{'add'};
  92.         }
  93.  
  94.         $host = $GWhoIs::Core::mirror{$method.$host} if defined $GWhoIs::Core::mirror{$method.$host};
  95.         return ($method,$host,$additional);
  96. }
  97.  
  98. 1;
  99.  
  100.