Subversion Repositories vgwhois

Rev

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

  1. package GWhoIs::IPv4;
  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. # %v4pattern = GWhoIs::IPv4::getpatternv4()
  10. sub GWhoIs::IPv4::getpatternv4 {
  11.         my (%pattern);
  12.         my ($method,$host,$additional,$cline,$line,$rehost,$readditional);
  13.  
  14.         foreach my $patternfile (GWhoIs::Core::getpatternfiles()) {
  15.                 open(PATTERN,"<$patternfile") || die "Cannot open $patternfile. STOP.\n";
  16.  
  17.                 while ( defined($line = <PATTERN>) ) {
  18.                         # chomp $line;
  19.                         $line = GWhoIs::Utils::trim($line);
  20.  
  21.                         if ( $line =~ /^#/ ) {                       # comment
  22.                         } elsif ( ($cline) = $line =~ /^:(.*)$/ ) {  # method declaration
  23.                                 ($method,$host,$additional) = split(/\|/,$cline,3);
  24.                                 $method=''     if !defined $method;
  25.                                 $host=''       if !defined $host;
  26.                                 $additional='' if !defined $additional;
  27.                         } elsif (( $line =~ /^=/ ) && ($line !~ /:/)) { # do not read IPv6 lines (containing ':')
  28.                                 ($rehost,$readditional) = GWhoIs::Core::methodpatternregex('',$host,$additional,$line);
  29.  
  30.                                 # 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.
  31.                                 ($line) = $line =~ /^=(.*)$/;           # remove leading "="
  32.                                 my ($ipv4,$bits) = split(/\//,$line,2); # split into IP address and CIDR
  33.                                 $ipv4 = Net::IP::ip_expand_address($ipv4, 4);    # Expand the IP address in case it uses shortened syntax or something
  34.                                 $bits = 32 if ($bits eq '');            # if no CIDR was found, assume it is a single IPv4 address
  35.                                 my ($ipa,$ipb,$ipc,$ipd) = split(/\./,$ipv4,4);
  36.                                 $ipa=0 if (!defined $ipa);
  37.                                 $ipb=0 if (!defined $ipb);
  38.                                 $ipc=0 if (!defined $ipc);
  39.                                 $ipd=0 if (!defined $ipd);
  40.                                 my $ip = $ipa<<24|$ipb<<16|$ipc<<8|$ipd;
  41.  
  42.                                 my $cidr = "$ip/$bits";
  43.  
  44.                                 $pattern{$cidr}{'method'} = $method;
  45.                                 $pattern{$cidr}{'host'}   = $rehost;
  46.                                 $pattern{$cidr}{'add'}    = $readditional;
  47.                         }
  48.                 }
  49.         }
  50.         return (%pattern); # TODO: might be undefined
  51. }
  52.  
  53. # ($method, $host, $additional) = GWhoIs::IPv4::getmethodv4($ipa, $ipb, $ipc, $ipd);
  54. sub GWhoIs::IPv4::getmethodv4 {
  55.         my ($ipa, $ipb, $ipc, $ipd) = @_;
  56.         my ($ip, $bits, $netmask, $method, $host, $additional, %pattern);
  57.  
  58.         $ip      = $ipa<<24 | $ipb<<16 | $ipc<<8 | $ipd;
  59.         $netmask = 256**4-1;
  60.         %pattern = GWhoIs::IPv4::getpatternv4();
  61.         $method  = '';
  62.  
  63.         for ($bits=32; $bits>=0 && $method eq ''; $bits--) {
  64.                 $ip        = $ip & $netmask;
  65.                 $netmask <<= 1;
  66.  
  67.                 my $cidr = "$ip/$bits";
  68.  
  69.                 $method     = $pattern{$cidr}{'method'} if defined $pattern{$cidr}{'method'};
  70.                 $host       = $pattern{$cidr}{'host'}   if defined $pattern{$cidr}{'host'};
  71.                 $additional = $pattern{$cidr}{'add'}    if defined $pattern{$cidr}{'add'};
  72.         }
  73.  
  74.         $host = $GWhoIs::Core::mirror{$method.$host} if defined $GWhoIs::Core::mirror{$method.$host};
  75.         return ($method,$host,$additional); # TODO: might be undefined (+ everywhere else)
  76. }
  77.  
  78. 1;
  79.  
  80.