Exemplo n.º 1
0
 def __init__(self,
              node_id=None,
              address=None,
              last_updated=None,
              totalrtt=None,
              successcount=None,
              failcount=None):
     # TODO make check interface.Don't use the encoding funcs directlly
     # So we don't crash here.
     basic_coder.encode_address(address)
     basic_coder.encode_network_id(node_id)
     # Network information
     self.node_id = node_id
     self.address = address
     # Statistical information
     if last_updated is None:
         self.last_updated = time.time()
         self.totalrtt = 0
         self.successcount = 0
         self.failcount = 0
     else:
         self.last_updated = last_updated
         self.totalrtt = totalrtt
         self.successcount = successcount
         self.failcount = failcount
Exemplo n.º 2
0
 def test_encode_address_EdgeAddresses(self):
     zero_address = ("0.0.0.0", 0)
     full_address = ("255.255.255.255", 65535)
     port_three_address = ("0.0.0.0", 3)
     self.assertEquals("\x00\x00\x00\x00\x00\x00",
                         encode_address(zero_address))
     self.assertEquals("\xff\xff\xff\xff\xff\xff",
                         encode_address(full_address))
     self.assertEquals("\x00\x00\x00\x00\x00\x03",
                         encode_address(port_three_address))
Exemplo n.º 3
0
 def test_encode_address_EdgeAddresses(self):
     zero_address = ("0.0.0.0", 0)
     full_address = ("255.255.255.255", 65535)
     port_three_address = ("0.0.0.0", 3)
     self.assertEquals("\x00\x00\x00\x00\x00\x00",
                       encode_address(zero_address))
     self.assertEquals("\xff\xff\xff\xff\xff\xff",
                       encode_address(full_address))
     self.assertEquals("\x00\x00\x00\x00\x00\x03",
                       encode_address(port_three_address))
Exemplo n.º 4
0
 def __init__(self, node_id, address):
     # Verify the node_id and address are in the proper format
     basic_coder.encode_address(address)
     basic_coder.encode_network_id(node_id)
     # Network information
     self.node_id = node_id
     self.address = address
     # Statistical information
     self.last_updated = time.time()
     self.totalrtt = 0
     self.successcount = 0
     self.failcount = 0
Exemplo n.º 5
0
 def __init__(self, node_id, address):
     # Verify the node_id and address are in the proper format
     basic_coder.encode_address(address)
     basic_coder.encode_network_id(node_id)
     # Network information
     self.node_id = node_id
     self.address = address
     # Statistical information
     # TODO make this time format human readable
     self.last_updated = time.time()
     self.totalrtt = 0
     self.successcount = 0
     self.failcount = 0
Exemplo n.º 6
0
 def test_encode_and_decode_address_validAddresses(self):
     valid_addresses = [("127.0.0.1", 80), ("4.2.2.1", 53),
                  ("8.8.8.8", 52), ("12.12.42.42", 1385),
                  ("0.0.0.0", 0), ("255.255.255.255", 65535)]
     for address in valid_addresses:
         self.assertEquals(address,
                           decode_address(encode_address(address)))
Exemplo n.º 7
0
 def __init__(self, node_id=None, address=None, last_updated=None, totalrtt=None, successcount=None, failcount=None):
     # TODO make check interface.Don't use the encoding funcs directlly
     # So we don't crash here.
     basic_coder.encode_address(address)
     basic_coder.encode_network_id(node_id)
     # Network information
     self.node_id = node_id
     self.address = address
     # Statistical information
     if last_updated is None:
         self.last_updated = time.time()
         self.totalrtt = 0
         self.successcount = 0
         self.failcount = 0
     else:
         self.last_updated = last_updated
         self.totalrtt = totalrtt
         self.successcount = successcount
         self.failcount = failcount
Exemplo n.º 8
0
def encode_node(node):
    """
    Encodes the given node into a network string

    The format of the network string is specified in the
    BitTorrent DHT extension specification

    @see DHTBot/references/README for the DHT BEP

    """
    return "%s%s" % (basic_coder.encode_network_id(
        node.node_id), basic_coder.encode_address(node.address))
Exemplo n.º 9
0
def encode_node(node):
    """
    Encodes the given node into a network string

    The format of the network string is specified in the
    BitTorrent DHT extension specification

    @see DHTBot/references/README for the DHT BEP

    """
    return "%s%s" % (basic_coder.encode_network_id(node.node_id),
                     basic_coder.encode_address(node.address))
Exemplo n.º 10
0
def _response_encoder(response):
    """@see encode"""
    resp_dict = {"r": {"id": basic_coder.encode_network_id(response._from)}}
    if response.nodes is not None:
        encoded_nodes = [contact.encode_node(node) for node in response.nodes]
        resp_dict['r']['nodes'] = "".join(encoded_nodes)
    if response.peers is not None:
        encoded_peers = [basic_coder.encode_address(peer)
                         for peer in response.peers]
        resp_dict['r']['values'] = "".join(encoded_peers)
    if response.token is not None:
        resp_dict['r']['token'] = basic_coder.ltob(response.token)
    return resp_dict
Exemplo n.º 11
0
 def _get_hash(self, query, address, secret):
     """
     Create the hash code for the given query/address/secret combination
     """
     node_id = query._from
     infohash = query.target_id
     hash = self.hash_constructor()
     # The hash code relies on the querying node's ID,
     # the target infohash of the query, the address of
     # the querier, and a secret that changes every
     # constants._secret_timeout seconds
     hash.update(basic_coder.encode_network_id(node_id))
     hash.update(basic_coder.encode_network_id(infohash))
     hash.update(basic_coder.encode_address(address))
     hash.update(secret)
     # Return the hash as a number rather than a string
     numeric_hash_value = basic_coder.btol(hash.digest())
     return numeric_hash_value
Exemplo n.º 12
0
 def test_encode_and_decode_address_validAddresses(self):
     valid_addresses = [("127.0.0.1", 80), ("4.2.2.1", 53), ("8.8.8.8", 52),
                        ("12.12.42.42", 1385), ("0.0.0.0", 0),
                        ("255.255.255.255", 65535)]
     for address in valid_addresses:
         self.assertEquals(address, decode_address(encode_address(address)))