1

我正在编写基于 Qt 的客户端应用程序。它使用 连接到远程服务器QTcpSocket。在发送任何实际数据之前,它需要发送登录信息,即 zlib 压缩的 json。

据我从服务器来源了解到,为了使一切正常工作,我需要在 4 个字节之后发送 X 个字节的压缩数据,其中包含未压缩数据的长度。

在服务器端解压缩如下所示:

/* look at first 32 bits of buffer, which contains uncompressed len */
unc_len = le32toh(*((uint32_t *)buf));
if (unc_len > CLI_MAX_MSG)
    return NULL;

/* alloc buffer for uncompressed data */
obj_unc = malloc(unc_len + 1);
if (!obj_unc)
    return NULL;

/* decompress buffer (excluding first 32 bits) */
comp_p = buf + 4;
if (uncompress(obj_unc, &dest_len, comp_p, buflen - 4) != Z_OK)
    goto out;
if (dest_len != unc_len)
    goto out;
memcpy(obj_unc + unc_len, &zero, 1);    /* null terminate */

我正在使用 Qt 内置的 zlib 压缩 json(我刚刚下载了头文件并将其放在 mingw 的include文件夹中):

char json[] = "{\"version\":1,\"user\":\"test\"}";
char pass[] = "test";

std::auto_ptr<Bytef> message(new Bytef[             // allocate memory for:
                             sizeof(ubbp_header)    //  + msg header
                             + sizeof(uLongf)       //  + uncompressed data size
                             + strlen(json)         //  + compressed data itself
                             + 64                   //  + reserve (if compressed size > uncompressed size)
                             + SHA256_DIGEST_LENGTH]);//+ SHA256 digest

uLongf unc_len = strlen(json);
uLongf enc_len = strlen(json) + 64;

// header goes first, so server will determine that we want to login
Bytef* pHdr = message.get();

// after that: uncompressed data length and data itself
Bytef* pLen = pHdr + sizeof(ubbp_header);
Bytef* pDat = pLen + sizeof(uLongf);

// hash of compressed message updated with user pass
Bytef* pSha;

if (Z_OK != compress(pLen, &enc_len, (Bytef*)json, unc_len))
{
    qDebug("Compression failed.");
    return false;
}

完整功能代码在这里: http: //pastebin.com/hMY2C4n5

即使服务器正确收到未压缩的长度,uncompress()返回Z_BUF_ERROR.

PS:我实际上正在编写 pushpool 的客户端来弄清楚它的二进制协议是如何工作的。我在官方比特币论坛上问过这个问题,但没有运气。http://forum.bitcoin.org/index.php?topic=24257.0

4

1 回答 1

1

原来这是服务器端的错误。比特币论坛线程中的更多详细信息。

于 2012-06-07T10:11:12.340 回答