【问题标题】:How to find nodes with exact info_hash in Bittorrent DHT?如何在 Bittorrent DHT 中找到具有精确 info_hash 的节点?
【发布时间】:2017-08-28 18:51:04
【问题描述】:

在bittorrent的DHT协议的文档中,给出了get_peers方法用于查找具有给定info_hash的节点。它说如果响应包含“values”键,则被查询的节点返回了包含精确info_hash的节点的信息.如果节点返回“nodes”键,它返回了最接近结果的K个节点。我们是否应该在返回的节点(最近)上递归调用get_peers以到达精确的节点(具有相同的info_hash)?

【问题讨论】:

  • 感谢您在@the8472 那里指导我。所以我们必须执行递归 get_peers 操作,直到我们得到“值”作为响应的键?如果我们得到与返回值相同的节点并以“nodes”作为响应的关键,则停止这样做?请指导我。
  • 你试过了吗?你遇到过特殊的问题吗?有什么可以“指导”的,您只是要求重述已经在多个地方制定的相同内容。
  • @PratikShinde 你看我的回答了吗?

标签: p2p bittorrent dht


【解决方案1】:

我们是否应该在返回的节点(最近的)上递归调用 get_peers 以到达确切的节点(具有相同的 info_hash)?

是和不是。如果您是 LISP 类型,则可以使用递归函数。也就是说,一个while循环将完成这项工作。下面是一些用一些 cmets 实现 FIND_VALUE 算法的 python 代码:

async def _get(self, key):
    """Fetch the value associated with KEY from the network"""
    uid = key
    queried = set()
    while True:
        # retrieve the k nearest peers and remove already queried peers
        peers = nearest(k, self.peers)
        # no more peer to query, the key is not found in the dht
        if not peers:
            raise KeyError(uid)
        # query selected peers
        responses = dict()
        for address in peers:
            response = self._protocol.rpc(address, "value", uid)
            responses[address] = response
        # check responses: look for the value or add peers more near KEY
        for (address, response) in responses.items():
            queried.add(address)
            if isinstance(response, Exception):
                continue
            elif response[0] == b"VALUE":
                # value is found, return it
                value = response[1]
                if hash(value) == unpack(uid):
                    # at last!
                    return value
                else:
                    log.warning(
                        "[%r] bad value returned from %r", self._uid, address
                    )
                    await self.blacklist(address)
                    continue
            elif response[0] == b"PEERS":
                # value not found but we got peers that are more near KEY
                await self._welcome_peers(response[1])

此代码基于qadom's peer.py

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多