| 1 | package MaxMind::DB::Reader::PP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 21 | our $VERSION = '1.000014'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Carp qw( confess ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use Math::BigInt (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use MaxMind::DB::Types qw( Int ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 21 | ▶ | use Socket 1.87 qw( inet_pton AF_INET AF_INET6 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 21 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 21 | ▶ | use MooX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 21 | with 'MaxMind::DB::Reader::Role::Reader', | |||||||||||||||||||||||||||
| 19 | 'MaxMind::DB::Reader::Role::NodeReader', | ||||||||||||||||||||||||||||
| 20 | 'MaxMind::DB::Reader::Role::HasDecoder', | ||||||||||||||||||||||||||||
| 21 | 'MaxMind::DB::Role::Debugs'; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 21 | has _ipv4_start_node => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => Int, | ||||||||||||||||||||||||||||
| 26 | init_arg => undef, | ||||||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||||||
| 28 | builder => '_build_ipv4_start_node', | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 21 | ▶ | use constant DEBUG => $ENV{MAXMIND_DB_READER_DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 22 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 22 | my $file = $self->file; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 22 | ▶ | die qq{Error opening database file "$file": The file does not exist.} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | unless -e $file; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 21 | ▶ | die qq{Error opening database file "$file": The file cannot be read.} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | unless -r _; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Build the metadata right away to ensure file's validity | ||||||||||||||||||||||||||||
| 45 | 21 | $self->metadata; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 20 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 21 | ▶ | sub _build_data_source { | ||||||||||||||||||||||||||
|
Subroutine
_build_data_source: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 21 | my $file = $self->file; | |||||||||||||||||||||||||||
| 54 | 21 | open my $fh, '<:raw', $file; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 21 | return $fh; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 60 | 203 | ▶ | sub _data_for_address { | ||||||||||||||||||||||||||
|
Subroutine
_data_for_address: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 203 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 203 | my $addr = shift; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 203 | my $pointer = $self->_find_address_in_tree($addr); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ## no critic (Subroutines::ProhibitExplicitReturnUndef) | ||||||||||||||||||||||||||||
| 67 | 203 | ▶ | return undef unless $pointer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 168 | return $self->_get_entry_data($pointer); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | ## use critic | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 203 | ▶ | sub _find_address_in_tree { | ||||||||||||||||||||||||||
|
Subroutine
_find_address_in_tree: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 74 | 203 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 203 | my $addr = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 203 | my $is_ipv6_addr = $addr =~ /:/; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 203 | ▶ | my $packed_addr = inet_pton( $is_ipv6_addr ? AF_INET6 : AF_INET, $addr ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 203 | ▶ | die | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | "The IP address you provided ($addr) is not a valid IPv4 or IPv6 address" | ||||||||||||||||||||||||||||
| 83 | unless defined $packed_addr; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 203 | my @address_bytes = unpack( 'C*', $packed_addr ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # The first node of the tree is always node 0, at the beginning of the | ||||||||||||||||||||||||||||
| 88 | # value | ||||||||||||||||||||||||||||
| 89 | 203 | ▶ | my $node = $self->ip_version == 6 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->ip_version == 6 and not $is_ipv6_addr
MC/DC100%2/2
$self->ip_version == 6 and not $is_ipv6_addr
$self->ip_version == 6
not $is_ipv6_addr
Truth table100%3/3
A and not(B)
A= $self->ip_version == 6B= not $is_ipv6_addr
| |||||||||||||||||||||||||||||
| 90 | && !$is_ipv6_addr ? $self->_ipv4_start_node : 0; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 203 | my $bit_length = @address_bytes * 8; | |||||||||||||||||||||||||||
| 93 | 203 | for my $bit_num ( 0 .. $bit_length ) { | |||||||||||||||||||||||||||
| 94 | 12965 | ▶ | last if $node >= $self->node_count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 12762 | my $temp_bit = 0xFF & $address_bytes[ $bit_num >> 3 ]; | |||||||||||||||||||||||||||
| 97 | 12762 | my $bit = 1 & ( $temp_bit >> 7 - ( $bit_num % 8 ) ); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 12762 | my ( $left_record, $right_record ) = $self->_read_node($node); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 12762 | ▶ | $node = $bit ? $right_record : $left_record; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 12762 | if (DEBUG) { | |||||||||||||||||||||||||||
| 104 | $self->_debug_string( 'Bit #', $bit_length - $bit_num ); | ||||||||||||||||||||||||||||
| 105 | $self->_debug_string( 'Bit value', $bit ); | ||||||||||||||||||||||||||||
| 106 | $self->_debug_string( 'Record', $bit ? 'right' : 'left' ); | ||||||||||||||||||||||||||||
| 107 | $self->_debug_string( 'Record value', $node ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 203 | ▶ | if ( $node == $self->node_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 35 | $self->_debug_message('Record is empty') | |||||||||||||||||||||||||||
| 113 | if DEBUG; | ||||||||||||||||||||||||||||
| 114 | 35 | return; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 168 | ▶ | if ( $node >= $self->node_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 168 | $self->_debug_message('Record is a data pointer') | |||||||||||||||||||||||||||
| 119 | if DEBUG; | ||||||||||||||||||||||||||||
| 120 | 168 | return $node; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | sub iterate_search_tree { | ||||||||||||||||||||||||||
|
Subroutine
iterate_search_tree: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 1 | my $data_callback = shift; | |||||||||||||||||||||||||||
| 127 | 1 | my $node_callback = shift; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | my $node_num = 0; | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | my $ipnum = $self->ip_version() == 4 ? 0 : Math::BigInt->bzero(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | my $depth = 1; | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | my $max_depth = $self->ip_version() == 4 ? 32 : 128; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | $self->_iterate_search_tree( | |||||||||||||||||||||||||||
| 135 | $data_callback, | ||||||||||||||||||||||||||||
| 136 | $node_callback, | ||||||||||||||||||||||||||||
| 137 | $node_num, | ||||||||||||||||||||||||||||
| 138 | $ipnum, | ||||||||||||||||||||||||||||
| 139 | $depth, | ||||||||||||||||||||||||||||
| 140 | $max_depth, | ||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | ## no critic (Subroutines::ProhibitManyArgs) | ||||||||||||||||||||||||||||
| 145 | 937 | ▶ | sub _iterate_search_tree { | ||||||||||||||||||||||||||
|
Subroutine
_iterate_search_tree: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 146 | 937 | my $self = shift; | |||||||||||||||||||||||||||
| 147 | 937 | my $data_callback = shift; | |||||||||||||||||||||||||||
| 148 | 937 | my $node_callback = shift; | |||||||||||||||||||||||||||
| 149 | 937 | my $node_num = shift; | |||||||||||||||||||||||||||
| 150 | 937 | my $ipnum = shift; | |||||||||||||||||||||||||||
| 151 | 937 | my $depth = shift; | |||||||||||||||||||||||||||
| 152 | 937 | my $max_depth = shift; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | ## no critic (TestingAndDebugging::ProhibitNoWarnings) | ||||||||||||||||||||||||||||
| 155 | 21 | ▶ | no warnings 'recursion'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | ## use critic | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 937 | my @records = $self->_read_node($node_num); | |||||||||||||||||||||||||||
| 159 | 937 | ▶ | $node_callback->( $node_num, @records ) if $node_callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 937 | for my $idx ( 0 .. 1 ) { | |||||||||||||||||||||||||||
| 162 | 1874 | my $value = $records[$idx]; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # We ignore empty branches of the search tree | ||||||||||||||||||||||||||||
| 165 | 1874 | ▶ | next if $value == $self->node_count(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 965 | ▶ | my $one = $self->ip_version() == 4 ? 1 : Math::BigInt->bone(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 965 | ▶ | $ipnum = $ipnum | ( $one << ( $max_depth - $depth ) ) if $idx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 965 | ▶ | if ( $value <= $self->node_count() ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 171 | 936 | $self->_iterate_search_tree( | |||||||||||||||||||||||||||
| 172 | $data_callback, | ||||||||||||||||||||||||||||
| 173 | $node_callback, | ||||||||||||||||||||||||||||
| 174 | $value, | ||||||||||||||||||||||||||||
| 175 | $ipnum, | ||||||||||||||||||||||||||||
| 176 | $depth + 1, | ||||||||||||||||||||||||||||
| 177 | $max_depth, | ||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | elsif ($data_callback) { | ||||||||||||||||||||||||||||
| 181 | 0 | $data_callback->( | |||||||||||||||||||||||||||
| 182 | $ipnum, $depth, | ||||||||||||||||||||||||||||
| 183 | $self->_get_entry_data($value) | ||||||||||||||||||||||||||||
| 184 | ); | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | ## use critic | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 168 | ▶ | sub _get_entry_data { | ||||||||||||||||||||||||||
|
Subroutine
_get_entry_data: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 191 | 168 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | 168 | my $offset = shift; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 168 | my $resolved = ( $offset - $self->node_count ) + $self->_search_tree_size; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 168 | ▶ | confess q{The MaxMind DB file's search tree is corrupt} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | if $resolved > $self->_data_source_size; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 167 | if (DEBUG) { | |||||||||||||||||||||||||||
| 200 | my $node_count = $self->node_count; | ||||||||||||||||||||||||||||
| 201 | my $tree_size = $self->_search_tree_size; | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | $self->_debug_string( | ||||||||||||||||||||||||||||
| 204 | 'Resolved data pointer', | ||||||||||||||||||||||||||||
| 205 | "( $offset - $node_count ) + $tree_size = $resolved" | ||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # We only want the data from the decoder, not the offset where it was | ||||||||||||||||||||||||||||
| 210 | # found. | ||||||||||||||||||||||||||||
| 211 | 167 | return scalar $self->_decoder->decode($resolved); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 11 | ▶ | sub _build_ipv4_start_node { | ||||||||||||||||||||||||||
|
Subroutine
_build_ipv4_start_node: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 215 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 11 | ▶ | return 0 unless $self->ip_version == 6; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 11 | my $node_num = 0; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 11 | for ( 1 ... 96 ) { | |||||||||||||||||||||||||||
| 222 | 1024 | ($node_num) = $self->_read_node($node_num); | |||||||||||||||||||||||||||
| 223 | 1024 | ▶ | last if $node_num >= $self->node_count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 11 | return $node_num; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 21 | 1; | |||||||||||||||||||||||||||