【问题标题】:Correctly Parse a BitTorrent UDP Announce Response in Java在 Java 中正确解析 BitTorrent UDP 通告响应
【发布时间】:2011-03-10 03:14:31
【问题描述】:

我正在尝试解析来自通过 UDP 发送到 BitTorrent 服务器的 Announce 请求的响应。我取回了数据,但我认为我没有正确处理对等方的 IP 地址和端口,因为我无法与其中任何一个建立连接:

        Map<String, String> info = new Hashtable<String, String>();
        if (goodResponse) {
        try {
            int resAction = Utils.toInt(Utils.subArray(responseData, 0, 4));
            int resTrans = Utils.toInt(Utils.subArray(responseData, 4, 4));
            int interval = Utils.toInt(Utils.subArray(responseData, 8, 4));
            int leechers = Utils.toInt(Utils.subArray(responseData, 12, 4));
            int seeders = Utils.toInt(Utils.subArray(responseData, 16, 4));

            if (resAction != ERROR && resTrans == transactionID && resAction == ANNOUNCE) {

                info.put("udp", "tracker");
                info.put("seeders", String.valueOf(seeders));
                info.put("leechers", String.valueOf(leechers));
                info.put("interval", String.valueOf(interval));
                try {
                    for (int peer = 0; peer < leechers + seeders; peer++) {
                        InetAddress ip = Inet4Address.getByAddress(Utils.subArray(responseData, (20 + (6 * peer)), 4));
                        int port =  (int)Utils.toChar(Utils.subArray(responseData, (24 + (6 * peer)), 2));

                        if (port != 0) {
                            info.put(ip.getHostAddress(), String.valueOf(port));
                        }
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    Log.w(TAG, "Too many peers returned, some were dropped");
                }
            } else if (resAction == ERROR) {
                error(responseData);
                info = null;
            } else {
                torrent.setErrorMessage("Unable to announce, invalid request");
                Log.i(TAG, "ANNOUNCE-E: A:" + resAction + " T: " + resTrans + " (" + transactionID + ") I: " + interval + " P: " + seeders + "/" + leechers);
                info = null;
            }
        } catch (Exception e) {
            torrent.setErrorMessage("Unable to announce with tracker " + host);
            Log.e(TAG, "ANNOUCE-EX: " + e.getClass().getSimpleName() + " - " + e.getMessage());
            info = null;
        }
    }

这里是辅助函数:

public static int toInt(final byte[] input) {
    return ByteBuffer.wrap(input).getInt();
}

public static char toChar(final byte[] input) {
    return ByteBuffer.wrap(input).getChar();
}

public static long toLong(byte[] input) {
    return ByteBuffer.wrap(input).getLong();
}

public static short toShort(byte[] input) {
    return ByteBuffer.wrap(input).getShort();
}

此代码确实会显示 79.31.92.101:49378 和 79.168.1.215:65535 等主机,因此看起来正确,但没有一个允许连接。我是否正在解析对等数据?

文档说明:

        Response:
        0           32-bit integer  action          1 // announce
        4           32-bit integer  transactionID
        8           32-bit integer  interval
        12          32-bit integer  leechers
        16          32-bit integer  seeders
        20 + 6 * n  32-bit integer  IP address
        24 + 6 * n  16-bit integer  TCP port (unsigned int)
        20 + 6 * N

文档链接: http://xbtt.sourceforge.net/udp_tracker_protocol.html

http://www.rasterbar.com/products/libtorrent/udp_tracker_protocol.html

【问题讨论】:

  • 请分享发送UDP请求到服务器的代码以及你是如何处理它的。

标签: java udp bittorrent


【解决方案1】:

很好,一些调整,我明白了。端口部分是正确的,IP 地址是关闭的。使用此行代替 InetAddress 行;

String ip = Utils.intToIp(Utils.toInt(Utils.subArray(responseData, (20 + (6 * peer)), 4)));

intToIP 是:

public static String intToIp(int i) {
    return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
}

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多