def handshake_in(sock, msg, torrent, peer_id, info_hash, addr_to_peer, sock_to_peer, peers, active_peers, ids_to_peers, size, handshook): #print('Handshake in') peer = None if (sock.getpeername()[0] in addr_to_peer): peer = addr_to_peer[sock.getpeername()[0]] else: peer = Peer(sock.getpeername()[0], sock.getpeername()[1], torrent.num_pieces, torrent.piece_len, sock) if (not "\x13BitTorrent protocol" == msg[0:20]): print("Invalid handshake received.") return False if (not msg[28:48] == info_hash): print("Invalid handshake info_hash.") return False addr_to_peer[peer.ip] = peer if (not peer in peers): peers.append(peer) if (not peer in active_peers): active_peers.append(peer) peer.set_id(msg[48:]) ids_to_peers[peer.id] = peer sock_to_peer[sock] = peer try: if not peer in handshook: handshake = "\x13BitTorrent protocol\x00\x00\x00\x00\x00\x00\x00\x00" handshake += info_hash + peer_id sock.sendall(handshake) handshook.append(peer) # Responded to handshake return True except: return False