| 1 | use v5.40; | ||
| 2 | use feature 'class'; | ||
| 3 | no warnings 'experimental::class'; | ||
| 4 | # | ||
| 5 | use Net::BitTorrent::Emitter; | ||
| 6 | class Net::BitTorrent::Protocol::BEP03 v2.1.0 : isa(Net::BitTorrent::Emitter) { | ||
| 7 | # | ||
| 8 | field $infohash : param = undef; | ||
| 9 | field $peer_id : param : reader; | ||
| 10 | field $reserved : param : reader : writer = "\0" x 8; | ||
| 11 | field $debug : param : reader = 0; | ||
| 12 | field $state : reader = 'HANDSHAKE'; # HANDSHAKE, OPEN, CLOSED | ||
| 13 | field $buffer_in = ''; | ||
| 14 | field $buffer_out = ''; | ||
| 15 | field $handshake_sent = 0; | ||
| 16 | field $detected_ih : reader = undef; | ||
| 17 | |||
| 18 | # Message IDs | ||
| 19 | use constant { | ||
| 20 | CHOKE => 0, | ||
| 21 | UNCHOKE => 1, | ||
| 22 | INTERESTED => 2, | ||
| 23 | NOT_INTERESTED => 3, | ||
| 24 | HAVE => 4, | ||
| 25 | BITFIELD => 5, | ||
| 26 | REQUEST => 6, | ||
| 27 | PIECE => 7, | ||
| 28 | CANCEL => 8, | ||
| 29 | |||
| 30 | # BEP 52 | ||
| 31 | HASH_REQUEST => 21, | ||
| 32 | HASHES => 22, | ||
| 33 | HASH_REJECT => 23, | ||
| 34 | |||
| 35 | # Security limits | ||
| 36 | MAX_MESSAGE_SIZE => 16 * 1024 * 1024, # 16 MB | ||
| 37 | }; | ||
| 38 | |||
| 39 | method set_reserved_bit ( $byte, $mask ) { | ||
| 40 | no warnings 'numeric'; | ||
| 41 | my $val = ord( substr( $reserved, $byte, 1 ) ); | ||
| 42 | $val |= $mask; | ||
| 43 | $reserved = substr( $reserved, 0, $byte ) . chr($val) . substr( $reserved, $byte + 1 ); | ||
| 44 | } | ||
| 45 | |||
| 46 | method send_handshake () { | ||
| 47 | if ( !defined $infohash ) { | ||
| 48 | $self->_emit_log( 'fatal', 'infohash required to send handshake' ); | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | my $ih_len = CORE::length($infohash); | ||
| 52 | if ( $ih_len != 20 && $ih_len != 32 ) { | ||
| 53 | $self->_emit_log( 'fatal', 'Info hash must be 20 or 32 bytes' ); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | $self->_emit_log( 'debug', 'Sending handshake (' . unpack( 'H*', $infohash ) . ')' ) if $debug; | ||
| 57 | my $raw = pack( 'C A19 a8', 19, 'BitTorrent protocol', $reserved ) . $infohash . $peer_id; | ||
| 58 | $self->_emit_log( 'debug', 'Handshake hex: ' . unpack( 'H*', $raw ) ) if $debug; | ||
| 59 | $buffer_out .= $raw; | ||
| 60 | $handshake_sent = 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | method write_buffer () { | ||
| 64 | my $tmp = $buffer_out; | ||
| 65 | $buffer_out = ''; | ||
| 66 | return $tmp; | ||
| 67 | } | ||
| 68 | field $processing = 0; | ||
| 69 | |||
| 70 | method receive_data ($data) { | ||
| 71 | $buffer_in .= $data; | ||
| 72 | return if $processing; | ||
| 73 | $processing = 1; | ||
| 74 | try { $self->_process_buffer(); } | ||
| 75 | catch ($e) { | ||
| 76 | $self->_emit_log( 'error', "Protocol processing error: $e" ); | ||
| 77 | $state = 'CLOSED'; | ||
| 78 | } | ||
| 79 | $processing = 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | method _process_buffer () { | ||
| 83 | if ( $state eq 'OPEN' ) { | ||
| 84 | $self->_process_messages(); | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | my $old_state; | ||
| 88 | while ( $state ne 'CLOSED' && ( !$old_state || $old_state ne $state ) ) { | ||
| 89 | $old_state = $state; | ||
| 90 | if ( $state eq 'HANDSHAKE' ) { | ||
| 91 | $self->_process_handshake(); | ||
| 92 | } | ||
| 93 | if ( $state eq 'OPEN' ) { | ||
| 94 | $self->_process_messages(); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | method _process_handshake () { | ||
| 100 | return if length($buffer_in) < 1; | ||
| 101 | my $pstrlen = ord( substr( $buffer_in, 0, 1 ) ); | ||
| 102 | if ( $pstrlen != 19 ) { | ||
| 103 | $state = 'CLOSED'; | ||
| 104 | $self->_emit_log( 'fatal', | ||
| 105 | 'Invalid protocol string (expected 19, got ' . $pstrlen . ') hex: ' . unpack( 'H*', substr( $buffer_in, 0, 20 ) ) ); | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | return if length($buffer_in) < 1 + $pstrlen + 8 + 20 + 20; # Min v1 handshake (68 bytes) | ||
| 109 | my $pstr = substr( $buffer_in, 1, $pstrlen ); | ||
| 110 | if ( $pstr ne 'BitTorrent protocol' ) { | ||
| 111 | $state = 'CLOSED'; | ||
| 112 | $self->_emit_log( 'fatal', 'Invalid protocol string' ); | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | # We have at least 68 bytes. | ||
| 117 | my $ih_len; | ||
| 118 | if ( defined $infohash ) { | ||
| 119 | $ih_len = length($infohash); | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | # If we have exactly 80 bytes, or more than 80, it MIGHT be a v2 handshake. | ||
| 123 | # But it could also be a v1 handshake (68) followed by a small message. | ||
| 124 | # A v2 handshake MUST be exactly 80 bytes if nothing else follows it. | ||
| 125 | # For now, let's look at the actual length of the buffer. | ||
| 126 | # If it's 68-79, it must be v1. | ||
| 127 | # If it's exactly 80, we assume v2 if we don't know yet? | ||
| 128 | # Actually, most swarms are still v1. | ||
| 129 | if ( length($buffer_in) == 80 ) { | ||
| 130 | $ih_len = 32; | ||
| 131 | } | ||
| 132 | elsif ( length($buffer_in) > 80 ) { | ||
| 133 | |||
| 134 | # Could be v2 + messages OR v1 + messages. | ||
| 135 | # This is ambiguous without knowing which IH the peer is using. | ||
| 136 | # Standard practice: if we didn't specify, assume v1 first. | ||
| 137 | $ih_len = 20; | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | $ih_len = 20; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | my $handshake_len = 1 + $pstrlen + 8 + $ih_len + 20; | ||
| 144 | return if length($buffer_in) < $handshake_len; | ||
| 145 | my $remote_res = substr( $buffer_in, 1 + $pstrlen, 8 ); | ||
| 146 | my $remote_ih = substr( $buffer_in, 1 + $pstrlen + 8, $ih_len ); | ||
| 147 | my $remote_id = substr( $buffer_in, 1 + $pstrlen + 8 + $ih_len, 20 ); | ||
| 148 | if ( defined $infohash && $remote_ih ne $infohash ) { | ||
| 149 | |||
| 150 | # If we were expecting v2 but got v1, it might fail here. | ||
| 151 | $state = 'CLOSED'; | ||
| 152 | $self->_emit_log( 'fatal', 'Info hash mismatch' ); | ||
| 153 | return; | ||
| 154 | } | ||
| 155 | substr( $buffer_in, 0, $handshake_len, '' ); | ||
| 156 | $state = 'OPEN'; | ||
| 157 | $detected_ih = $remote_ih; | ||
| 158 | $reserved = $remote_res; | ||
| 159 | $self->_emit_log( 'debug', "Received handshake from " . unpack( 'H*', $remote_id ) ) if $debug; | ||
| 160 | $self->_emit( handshake => $remote_ih, $remote_id ); | ||
| 161 | } | ||
| 162 | |||
| 163 | method _process_messages () { | ||
| 164 | while ( length($buffer_in) >= 4 ) { | ||
| 165 | my $msg_len = unpack( 'N', substr( $buffer_in, 0, 4 ) ); | ||
| 166 | if ( $msg_len == 0 ) { | ||
| 167 | substr( $buffer_in, 0, 4, '' ); # Keep-alive | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | if ( $msg_len > MAX_MESSAGE_SIZE ) { | ||
| 171 | $state = 'CLOSED'; | ||
| 172 | $self->_emit_log( 'fatal', "Message too large ($msg_len bytes, max " . MAX_MESSAGE_SIZE . ')' ); | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | if ( length($buffer_in) < 4 + $msg_len ) { | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | my $raw_msg = substr( $buffer_in, 0, 4 + $msg_len, '' ); | ||
| 179 | my $id = unpack( 'C', substr( $raw_msg, 4, 1 ) ); | ||
| 180 | my $payload = substr( $raw_msg, 5 ); | ||
| 181 | $self->_emit_log( 'debug', "Received message ID $id (len " . length($payload) . ')' ) if $debug; | ||
| 182 | $self->_handle_message( $id, $payload ); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | method _handle_message ( $id, $payload ) { } | ||
| 186 | |||
| 187 | method send_message ( $id, $payload = '' ) { | ||
| 188 | $self->_emit_log( 'debug', "Sending message ID $id (len " . length($payload) . ')' ) if $debug; | ||
| 189 | $buffer_out .= pack( 'N C a*', 1 + length($payload), $id, $payload ); | ||
| 190 | } | ||
| 191 | method send_keepalive () { $buffer_out .= pack( 'N', 0 ) } | ||
| 192 | method send_choke () { $self->send_message(CHOKE) } | ||
| 193 | method send_unchoke () { $self->send_message(UNCHOKE) } | ||
| 194 | method send_interested () { $self->send_message(INTERESTED) } | ||
| 195 | method send_not_interested () { $self->send_message(NOT_INTERESTED) } | ||
| 196 | method send_have ($index) { $self->send_message( HAVE, pack( 'N', $index ) ) } | ||
| 197 | |||
| 198 | method send_bitfield ($data) { | ||
| 199 | $self->send_message( BITFIELD, $data ); | ||
| 200 | } | ||
| 201 | }; | ||
| 202 | 1; |