| 1 | 39 | ▶ | use v5.40; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 39 | ▶ | use feature 'class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 39 | ▶ | no warnings 'experimental::class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | 39 | my @CRC32C_TABLE; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 39 | ▶ | sub _init_table { | ||||||||||||||||||||||||||
|
Subroutine
_init_table: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 8 | 39 | ▶ | return if @CRC32C_TABLE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 9 | 39 | for my $i ( 0 .. 255 ) { | |||||||||||||||||||||||||||
| 10 | 9984 | my $res = $i; | |||||||||||||||||||||||||||
| 11 | 9984 | ▶ | $res = ( $res & 1 ) ? ( $res >> 1 ) ^ 0x82F63B78 : ( $res >> 1 ) for 1 .. 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 9984 | $CRC32C_TABLE[$i] = $res & 0xFFFFFFFF; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | 39 | _init_table(); | |||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | 39 | class Net::BitTorrent::DHT::Security v2.1.1 { | |||||||||||||||||||||||||||
| 19 | 39 | ▶ | use Socket qw[inet_aton inet_pton AF_INET AF_INET6]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 39 | ▶ | use Crypt::URandom qw[urandom]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 353 | ▶ | method _crc32c ($data) { | ||||||||||||||||||||||||||
|
Subroutine
_crc32c: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 353 | my $crc = 0xFFFFFFFF; | |||||||||||||||||||||||||||
| 24 | 353 | $crc = ( $crc >> 8 ) ^ $CRC32C_TABLE[ ( $crc ^ $_ ) & 0xFF ] for unpack 'C*', $data; | |||||||||||||||||||||||||||
| 25 | 353 | return ( $crc ^ 0xFFFFFFFF ) & 0xFFFFFFFF; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 349 | ▶ | method generate_node_id ( $ip, $seed //= undef ) { | ||||||||||||||||||||||||||
|
Subroutine
generate_node_id: called CC=11 SCAR=24.5
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 349 | ▶ | $seed //= unpack( 'C', urandom(1) ); | ||||||||||||||||||||||||||
|
Condition66%2/3
$seed //= unpack("C", urandom(1))
MC/DC0%0/2
$seed //= unpack("C", urandom(1))
$seed
unpack("C", urandom(1))
Truth table66%2/3
A //= B
A= $seedB= unpack("C", urandom(1))
| |||||||||||||||||||||||||||||
| 30 | 349 | my $ip_bin; | |||||||||||||||||||||||||||
| 31 | 349 | my @v4_mask = ( 0x03, 0x0f, 0x3f, 0xff ); | |||||||||||||||||||||||||||
| 32 | 349 | my @v6_mask = ( 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff ); | |||||||||||||||||||||||||||
| 33 | 349 | my $ip_masked = ''; | |||||||||||||||||||||||||||
| 34 | 349 | ▶ | if ( $ip !~ /:/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 345 | $ip_bin = inet_aton($ip); | |||||||||||||||||||||||||||
| 36 | 345 | ▶ | return undef unless defined $ip_bin && length($ip_bin) == 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ip_bin and length $ip_bin == 4
MC/DC0%0/2
defined $ip_bin and length $ip_bin == 4
defined $ip_bin
length $ip_bin == 4
Truth table33%1/3
A and B
A= defined $ip_binB= length $ip_bin == 4
| |||||||||||||||||||||||||||||
| 37 | 345 | my @bytes = unpack( 'C*', $ip_bin ); | |||||||||||||||||||||||||||
| 38 | 345 | $bytes[$_] &= $v4_mask[$_] for 0 .. 3; | |||||||||||||||||||||||||||
| 39 | 345 | $ip_masked = pack( 'C*', @bytes ); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||
| 42 | 4 | $ip_bin = inet_pton( AF_INET6, $ip ); | |||||||||||||||||||||||||||
| 43 | 4 | ▶ | return undef unless defined $ip_bin && length($ip_bin) == 16; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ip_bin and length $ip_bin == 16
MC/DC0%0/2
defined $ip_bin and length $ip_bin == 16
defined $ip_bin
length $ip_bin == 16
Truth table33%1/3
A and B
A= defined $ip_binB= length $ip_bin == 16
| |||||||||||||||||||||||||||||
| 44 | 4 | my @bytes = unpack( 'C*', $ip_bin ); | |||||||||||||||||||||||||||
| 45 | 4 | $bytes[$_] &= $v6_mask[$_] for 0 .. 7; | |||||||||||||||||||||||||||
| 46 | 4 | $ip_masked = pack 'C*', @bytes[ 0 .. 7 ]; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 349 | my $input = $ip_masked . chr( $seed & 0x07 ); | |||||||||||||||||||||||||||
| 49 | 349 | my $crc = $self->_crc32c($input); | |||||||||||||||||||||||||||
| 50 | 349 | my @id; | |||||||||||||||||||||||||||
| 51 | 349 | $id[0] = ( $crc >> 24 ) & 0xFF; | |||||||||||||||||||||||||||
| 52 | 349 | $id[1] = ( $crc >> 16 ) & 0xFF; | |||||||||||||||||||||||||||
| 53 | 349 | $id[2] = ( ( $crc >> 8 ) & 0xF8 ) | ( unpack( 'C', urandom(1) ) & 0x07 ); | |||||||||||||||||||||||||||
| 54 | 349 | $id[$_] = unpack( 'C', urandom(1) ) for 3 .. 18; | |||||||||||||||||||||||||||
| 55 | 349 | $id[19] = $seed & 0xFF; | |||||||||||||||||||||||||||
| 56 | 349 | pack 'C*', @id; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 259 | ▶ | method validate_node_id ( $id_bin, $ip ) { | ||||||||||||||||||||||||||
|
Subroutine
validate_node_id: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 259 | ▶ | return 1 if !$ip; # Can't validate without IP | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 259 | my $seed = unpack 'C', substr $id_bin, 19, 1; | |||||||||||||||||||||||||||
| 62 | 259 | my $expected_id = $self->generate_node_id( $ip, $seed ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Compare first 21 bits | ||||||||||||||||||||||||||||
| 65 | 259 | my @id = unpack( 'C*', $id_bin ); | |||||||||||||||||||||||||||
| 66 | 259 | my @exp = unpack( 'C*', $expected_id ); | |||||||||||||||||||||||||||
| 67 | 259 | ▶ | return 0 if $id[0] != $exp[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 140 | ▶ | return 0 if $id[1] != $exp[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 140 | ▶ | return 0 if ( $id[2] & 0xF8 ) != ( $exp[2] & 0xF8 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 140 | return 1; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||
| 73 | # | ||||||||||||||||||||||||||||
| 74 | 39 | 1; | |||||||||||||||||||||||||||