Ejemplo n.º 1
0
def _query_decoder(rpc_dict):
    """
    Decode the given KRPC dictionary into a valid Query
    
    @see decode
    @return krpc_types.Query

    """
    q = Query()
    q._from = basic_coder.decode_network_id(rpc_dict['a']['id'])
    q.rpctype = rpctype = rpc_dict['q']

    if rpctype == 'ping':
        pass
    elif rpctype == 'find_node':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['target'])
    elif rpctype == 'get_peers':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
    elif rpctype == 'announce_peer':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
        # Try encoding the port (to ensure it is within range)
        basic_coder.encode_port(rpc_dict['a']['port'])
        q.port = rpc_dict['a']['port']
        q.token = basic_coder.btol(rpc_dict['a']['token'])
    else:
        raise _ProtocolFormatError()
    return q
Ejemplo n.º 2
0
def _query_decoder(rpc_dict):
    """
    Decode the given KRPC dictionary into a valid Query
    
    @see decode
    @return krpc_types.Query

    """
    q = Query()
    q._from = basic_coder.decode_network_id(rpc_dict['a']['id'])
    q.rpctype = rpctype = rpc_dict['q']

    if rpctype == 'ping':
        pass
    elif rpctype == 'find_node':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['target'])
    elif rpctype == 'get_peers':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
    elif rpctype == 'announce_peer':
        q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash'])
        # Try encoding the port (to ensure it is within range)
        basic_coder.encode_port(rpc_dict['a']['port'])
        q.port = rpc_dict['a']['port']
        q.token = basic_coder.btol(rpc_dict['a']['token'])
    else:
        raise _ProtocolFormatError()
    return q
Ejemplo n.º 3
0
    def test_announce_peer_Received_sendsValidResponse(self):
        kresponder = self._patched_responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
Ejemplo n.º 4
0
    def test_announce_peer_Received_sendsValidResponse(self):
        kresponder = self._patched_responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
Ejemplo n.º 5
0
 def announce_peer(self, address, target_id, token, port, timeout=None):
     timeout = timeout or constants.rpctimeout
     query = Query()
     query.rpctype = "announce_peer"
     query.target_id = target_id
     query.token = token
     query.port = port
     return self.sendQuery(query, address, timeout)
Ejemplo n.º 6
0
 def announce_peer(self, address, target_id, token, port, timeout=None):
     timeout = timeout or constants.rpctimeout
     query = Query()
     query.rpctype = "announce_peer"
     query.target_id = target_id
     query.token = token
     query.port = port
     return self.sendQuery(query, address, timeout)
Ejemplo n.º 7
0
    def test_announce_peer_Received_validTokenAddsPeer(self):
        kresponder = self._patched_responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)

        # Check to see if another get_peers query will return us
        # as a peer
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 9809831
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        self.assertEquals(1, len(response.peers))
        test_ip, test_port = test_address
        returned_peer = response.peers[0]
        self.assertEquals((test_ip, incoming_query.port), returned_peer)
Ejemplo n.º 8
0
    def test_announce_peer_Received_validTokenAddsPeer(self):
        kresponder = self._patched_responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)

        # Check to see if another get_peers query will return us
        # as a peer
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 9809831
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        self.assertEquals(1, len(response.peers))
        test_ip, test_port = test_address
        returned_peer = response.peers[0]
        self.assertEquals((test_ip, incoming_query.port), returned_peer)