def decode_handshake(self, torrent_obj): #raise exception if info-hash from handshake doesn't match our files's info-hash handshake = messages.Handshake(self.message_buffer) expected_info_hash = torrent_obj.info_hash if (expected_info_hash != handshake.info_hash): raise Exception( 'info_hash does not match expected. Info hash expected: ' + repr(expected_info_hash) + '. Info hash found: ' + repr(handshake.info_hash))
def handshake(self, torrent_obj): '''Input: ip:port of a peer with the torrent files of interest Output: <fill this in> <fill this in> ''' info_hash = torrent_obj.info_hash peer_id = torrent_obj.peer_id handshake = messages.Handshake(info_hash, peer_id) return handshake
def decode_handshake(self, torrent_obj): handshake = messages.Handshake(self.message_buffer) expected_info_hash = torrent_obj.info_hash if (expected_info_hash != handshake.info_hash): #TODO: instead of throwing exception, we should send a cancel message or add deffered and errback raise Exception( 'info_hash does not match expected. Info hash expected: ' + repr(expected_info_hash) + '. Info hash found: ' + repr(handshake.info_hash))
def handshake(self): try: # Send handshakeString for BitTorrent Protocol handshake_msg = messages.Handshake( self.tManager.infoHash, self.tManager.local_peer_id).encode() self.send_msg(handshake_msg) # Receive handshake message self.read_buffer += self.read_from_socket() # print("Raw Handshake response:") # print(self.read_buffer) handshake_recv = messages.Handshake.decode(self.read_buffer) #print("Decoded response:") #print("\033[92mInfo Hash: {}, Length: {}\033[0m".format(handshake_recv.infoHash, len(handshake_recv.infoHash))) #print("\033[92mPeer ID: {}, Length: {}\033[0m".format(handshake_recv.peer_id, len(handshake_recv.peer_id))) self.changeStatus("Got handshake response") # Drop connection if hashes don't match if handshake_recv.infoHash != self.tManager.infoHash: #print("\033[91m]Info Hashes don't match. Dropping Connection\033[0m") self.changeStatus( "\033[91m]Info Hashes don't match. Dropping Connection\033[0m" ) return -1 # Update read_buffer with the next message to be read self.read_buffer = self.read_buffer[handshake_recv.total_length:] # print("Updated read_buffer: {}".format(self.read_buffer)) # Set isGoodPeer = 1 indicating completion of handshake self.isGoodPeer = 1 self.send_interested() self.send_unchoke() except Exception: self.isGoodPeer = 0 self.readyToBeChecked = 1 print( "\033[91mError sending or receiving Handshake message.\033[0m") # self.sock.close() return -1
def emit(): print("Establishing handshake with server!") return messages.Handshake()
def handshake(self, torrent_obj): info_hash = torrent_obj.info_hash peer_id = torrent_obj.peer_id handshake = messages.Handshake(info_hash, peer_id) return handshake