]>
Commit | Line | Data |
---|---|---|
aecfa1fd | 1 | package SrSv::IPv6; |
2 | ||
3 | use Exporter qw( import ); | |
4 | use SrSv::Conf2Consts qw( main ); | |
5 | ||
6 | use SrSv::64bit; | |
7 | BEGIN { | |
8 | our @EXPORT = qw( is_ipv6 get_ipv6_net get_ipv6_64 ); | |
9 | if(main_conf_ipv6) { | |
10 | require Socket; import Socket; | |
11 | require Socket6; import Socket6; | |
12 | if(!HAS_64BIT_INT) { | |
6e49d28e | 13 | eval { |
14 | require Math::BigInt; | |
15 | import Math::BigInt try => 'GMP'; | |
16 | }; | |
17 | if($@) { | |
18 | print STDERR "Running old version of perl/Math::BigInt.\n", $@, "Trying again.\n"; | |
19 | require Math::BigInt; | |
20 | import Math::BigInt; | |
21 | } | |
aecfa1fd | 22 | } |
23 | push @EXPORT, qw( AF_INET6 ); | |
24 | } | |
25 | } | |
26 | ||
27 | sub is_ipv6($) { | |
28 | my ($addr) = @_; | |
29 | if($addr =~ /^((?:\d{1,3}\.){3}\d{1,3})$/) { | |
30 | return 0 unless wantarray; | |
31 | return (0, $addr); | |
32 | } | |
33 | elsif($addr =~ /:ffff:((?:\d{1,3}\.){3}\d{1,3})$/) { | |
34 | return 0 unless wantarray; | |
35 | return (0, $1); | |
36 | } else { | |
37 | return 1 unless wantarray; | |
38 | return (1, $addr); | |
39 | } | |
40 | } | |
41 | ||
42 | ||
43 | sub get_ipv6_net($) { | |
44 | # grabs the top 64bits of the IPv6 addr. | |
45 | my ($addr) = @_; | |
46 | my $str = Socket6::inet_pton(AF_INET6, $addr); | |
47 | my (@words) = unpack('H4H4H4H4H4H4H4H4', $str); | |
48 | my $int = ( !HAS_64BIT_INT ? Math::BigInt->bzero() : 0 ); | |
49 | for(0..3) { | |
50 | $int <<= 16; | |
51 | $int |= hex($words[$_]); | |
52 | } | |
53 | return $int; | |
54 | } | |
55 | ||
56 | sub get_ipv6_64($) { | |
57 | my ($addr) = @_; | |
58 | my $str = Socket6::inet_pton(AF_INET6, $addr); | |
59 | return join(":", unpack("H4H4H4H4", $str))."::/64"; | |
60 | } | |
61 | ||
62 | 1; |