| 1 | package IP::Authority; | ||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use Socket qw ( inet_aton ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use vars qw ( $VERSION ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | $VERSION = '1305.001'; # MAY 2013, version 0.01 | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | my $singleton = undef; | |||||||||||||||||||
| 10 | 2 | my $ip_db; | |||||||||||||||||||
| 11 | 2 | my $null = substr(pack('N',0),0,1); | |||||||||||||||||||
| 12 | 2 | my $nullnullnull = $null . $null . $null; | |||||||||||||||||||
| 13 | 2 | my %auth; | |||||||||||||||||||
| 14 | 2 | my $ip_match = qr/^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])$/o; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | my $bit0; | |||||||||||||||||||
| 17 | my $bit1; | ||||||||||||||||||||
| 18 | 2 | my @mask; | |||||||||||||||||||
| 19 | 2 | my @dtoc; | |||||||||||||||||||
| 20 | { | ||||||||||||||||||||
| 21 | 2 | $bit0 = substr(pack('N',2 ** 31),0,1); | |||||||||||||||||||
| 22 | 2 | $bit1 = substr(pack('N',2 ** 30),0,1); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | ▶ | for (my $i = 0; $i <= 31; $i++){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 64 | $mask[$i] = pack('N',2 ** (31 - $i)); | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | ▶ | for (my $i = 0; $i <= 255; $i++){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 512 | $dtoc[$i] = substr(pack('N',$i),3,1); | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | 2 | (my $module_dir = __FILE__) =~ s/\.pm$//; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | local $/; # set it so <> reads all the file at once | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 2 | ▶ | open (AUTH, "< $module_dir/auth.gif") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | or die ("couldn't read authority database: $!"); | ||||||||||||||||||||
| 37 | 2 | binmode AUTH; | |||||||||||||||||||
| 38 | 2 | my $auth_ultra = <AUTH>; # read in the file | |||||||||||||||||||
| 39 | 2 | close AUTH; | |||||||||||||||||||
| 40 | 2 | my $auth_num = (length $auth_ultra) / 3; | |||||||||||||||||||
| 41 | 2 | ▶ | for (my $i = 0; $i < $auth_num; $i++){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 14 | my $auth = substr($auth_ultra,3 * $i + 1,2); | |||||||||||||||||||
| 43 | 14 | ▶ | $auth = undef if ($auth eq '--'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 14 | $auth{substr($auth_ultra,3 * $i,1)} = $auth; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 2 | ▶ | open (IP, "< $module_dir/ipauth.gif") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | or die ("couldn't read IP database: $!"); | ||||||||||||||||||||
| 49 | 2 | binmode IP; | |||||||||||||||||||
| 50 | 2 | $ip_db = <IP>; | |||||||||||||||||||
| 51 | 2 | close IP; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub new () | ||||||||||||||||||||
| 55 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 56 | 2 | my $caller = shift; | |||||||||||||||||||
| 57 | 2 | ▶ | unless (defined $singleton){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 2 | ▶ | my $class = ref($caller) || $caller; | ||||||||||||||||||
|
Condition33%1/3
ref $caller || $caller
MC/DC0%0/2
ref $caller || $caller
ref $caller
$caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
| |||||||||||||||||||||
| 59 | 2 | $singleton = bless {}, $class; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 2 | return $singleton; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | sub inet_atoauth | ||||||||||||||||||||
| 65 | 29 | ▶ | { | ||||||||||||||||||
|
Subroutine
inet_atoauth: called CC=3 SCAR=16.0
Pod
documented
| |||||||||||||||||||||
| 66 | 29 | my $inet_a = $_[1]; | |||||||||||||||||||
| 67 | 29 | ▶ | if ($inet_a =~ $ip_match){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 29 | return inet_ntoauth($dtoc[$1].$dtoc[$2].$dtoc[$3].$dtoc[$4]); | |||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||
| 70 | 0 | ▶ | if (defined (my $n = inet_aton($inet_a))){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | return inet_ntoauth($n); | |||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||
| 73 | 0 | return undef; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub db_time | ||||||||||||||||||||
| 79 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
db_time: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 80 | 0 | return unpack("N",substr($ip_db,0,4)); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | sub inet_ntoauth | ||||||||||||||||||||
| 84 | 29 | ▶ | { | ||||||||||||||||||
|
Subroutine
inet_ntoauth: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||
| 85 | 29 | ▶ | my $inet_n = $_[1] || $_[0]; | ||||||||||||||||||
|
Condition33%1/3
$_[1] || $_[0]
MC/DC0%0/2
$_[1] || $_[0]
$_[1]
$_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 29 | my $pos = 4; | |||||||||||||||||||
| 88 | 29 | my $byte_zero = substr($ip_db,$pos,1); | |||||||||||||||||||
| 89 | # loop through bits of IP address | ||||||||||||||||||||
| 90 | 29 | ▶ | for (my $i = 0; $i <= 31; $i++){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 411 | ▶ | if (($inet_n & $mask[$i]) eq $mask[$i]){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | # bit[$i] is set [binary one] | ||||||||||||||||||||
| 94 | # - jump to next node | ||||||||||||||||||||
| 95 | # (start of child[1] node) | ||||||||||||||||||||
| 96 | 198 | ▶ | if (($byte_zero & $bit1) eq $bit1){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 61 | $pos = $pos + 1 + unpack('N', $nullnullnull . ($byte_zero ^ $bit1)); | |||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||
| 99 | 137 | $pos = $pos + 3 + unpack('N', $null . substr($ip_db,$pos,3)); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||
| 102 | # bit[$i] is unset [binary zero] | ||||||||||||||||||||
| 103 | # jump to end of this node | ||||||||||||||||||||
| 104 | # (start of child[0] node) | ||||||||||||||||||||
| 105 | 213 | ▶ | if (($byte_zero & $bit1) eq $bit1){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 31 | $pos = $pos + 1; | |||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||
| 108 | 182 | $pos = $pos + 3; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | # all terminal nodes of the tree start with zeroth bit | ||||||||||||||||||||
| 113 | # set to zero. the first bit can then be used to indicate | ||||||||||||||||||||
| 114 | # whether we're using the first or second byte to store the | ||||||||||||||||||||
| 115 | # country code | ||||||||||||||||||||
| 116 | 411 | $byte_zero = substr($ip_db,$pos,1); | |||||||||||||||||||
| 117 | 411 | ▶ | if (($byte_zero & $bit0) eq $bit0){ # country code | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 29 | ▶ | if (($byte_zero & $bit1) eq $bit1){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | # unpopular country code - stored in second byte | ||||||||||||||||||||
| 120 | 0 | return $auth{substr($ip_db,$pos+1,1)}; | |||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||
| 122 | # popular country code - stored in bits 2-7 | ||||||||||||||||||||
| 123 | # (we already know that bit 1 is not set, so | ||||||||||||||||||||
| 124 | # just need to unset bit 1) | ||||||||||||||||||||
| 125 | 29 | return $auth{$byte_zero ^ $bit0}; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||