Subversion Repositories vgwhois

Rev

Blame | Last modification | View Log | RSS feed

  1. #
  2. #  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
  3. #  Main program
  4. #
  5. #  (c) 2010-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
  6. #  based on the code (c) 1998-2010 by Juliane Holzt <debian@kju.de>
  7. #  Some early parts by Lutz Donnerhacke <Lutz.Donnerhacke@Jena.Thur.de>
  8. #
  9. #  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
  10. #
  11.  
  12. package VGWhoIs::IPv4;
  13.  
  14. use warnings;
  15. use strict;
  16.  
  17. # install with "cpan Net::IP" or "aptitude install libnet-ip-perl"
  18. use Net::IP;
  19.  
  20. # %v4pattern = VGWhoIs::IPv4::getpatternv4()
  21. sub VGWhoIs::IPv4::getpatternv4 {
  22.         my (%pattern);
  23.         my ($method,$host,$additional,$cline,$line,$rehost,$readditional);
  24.  
  25.         foreach my $patternfile (VGWhoIs::Core::getpatternfiles()) {
  26.                 open(PATTERN,"<$patternfile") || die "Cannot open $patternfile. STOP.\n";
  27.  
  28.                 while ( defined($line = <PATTERN>) ) {
  29.                         # chomp $line;
  30.                         $line = VGWhoIs::Utils::trim($line);
  31.  
  32.                         if ( $line =~ /^#/ ) {                       # comment
  33.                         } elsif ( ($cline) = $line =~ /^:(.*)$/ ) {  # method declaration
  34.                                 ($method,$host,$additional) = split(/\|/,$cline,3);
  35.                                 $method=''     if !defined $method;
  36.                                 $host=''       if !defined $host;
  37.                                 $additional='' if !defined $additional;
  38.                         } elsif (( $line =~ /^=/ ) && ($line !~ /:/)) { # do not read IPv6 lines (containing ':')
  39.                                 ($rehost,$readditional) = VGWhoIs::Core::methodpatternregex('',$host,$additional,$line);
  40.  
  41.                                 # Store the IP inside the CIDR as integer-notation. So, the pattern "001.2.3.4" will be recognized as "1.2.3.4" too.
  42.                                 ($line) = $line =~ /^=(.*)$/;           # remove leading "="
  43.                                 my ($ipv4,$bits) = split(/\//,$line,2); # split into IP address and CIDR
  44.                                 $ipv4 = Net::IP::ip_expand_address($ipv4, 4);    # Expand the IP address in case it uses shortened syntax or something
  45.                                 $bits = 32 if ($bits eq '');            # if no CIDR was found, assume it is a single IPv4 address
  46.                                 my ($ipa,$ipb,$ipc,$ipd) = split(/\./,$ipv4,4);
  47.                                 $ipa=0 if (!defined $ipa);
  48.                                 $ipb=0 if (!defined $ipb);
  49.                                 $ipc=0 if (!defined $ipc);
  50.                                 $ipd=0 if (!defined $ipd);
  51.                                 my $ip = $ipa<<24|$ipb<<16|$ipc<<8|$ipd;
  52.  
  53.                                 my $cidr = "$ip/$bits";
  54.  
  55.                                 $pattern{$cidr}{'method'} = $method;
  56.                                 $pattern{$cidr}{'host'}   = $rehost;
  57.                                 $pattern{$cidr}{'add'}    = $readditional;
  58.                         }
  59.                 }
  60.         }
  61.         return (%pattern); # TODO: might be undefined
  62. }
  63.  
  64. # ($method, $host, $additional) = VGWhoIs::IPv4::getmethodv4($ipa, $ipb, $ipc, $ipd);
  65. sub VGWhoIs::IPv4::getmethodv4 {
  66.         my ($ipa, $ipb, $ipc, $ipd) = @_;
  67.         my ($ip, $bits, $netmask, $method, $host, $additional, %pattern);
  68.  
  69.         $ip      = $ipa<<24 | $ipb<<16 | $ipc<<8 | $ipd;
  70.         $netmask = 256**4-1;
  71.         %pattern = VGWhoIs::IPv4::getpatternv4();
  72.         $method  = '';
  73.  
  74.         for ($bits=32; $bits>=0 && $method eq ''; $bits--) {
  75.                 $ip        = $ip & $netmask;
  76.                 $netmask <<= 1;
  77.  
  78.                 my $cidr = "$ip/$bits";
  79.  
  80.                 $method     = $pattern{$cidr}{'method'} if defined $pattern{$cidr}{'method'};
  81.                 $host       = $pattern{$cidr}{'host'}   if defined $pattern{$cidr}{'host'};
  82.                 $additional = $pattern{$cidr}{'add'}    if defined $pattern{$cidr}{'add'};
  83.         }
  84.  
  85.         $host = $VGWhoIs::Core::mirror{$method.$host} if defined $VGWhoIs::Core::mirror{$method.$host};
  86.         return ($method,$host,$additional); # TODO: might be undefined (+ everywhere else)
  87. }
  88.  
  89. 1;
  90.  
  91.