Subversion Repositories vgwhois

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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