这个 BitTorrent 客户端的片段验证过程中的错误在哪里?

发布于 2024-12-07 05:51:55 字数 4046 浏览 0 评论 0原文

我一直在业余时间用 C 语言编写 BitTorrent 客户端。然而,看似随机,我的客户将无法验证收到的 torrent 片段是否正确,并显示以下消息:

Failed to verify piece: #4

背景:.torrent 文件包含要下载的文件的相关元数据,包括 SHA1每个片段(要下载的文件的一个块)的哈希值,因此,在下载一个片段后,我获取其 SHA1 哈希值,并将其与 torrent 元数据文件提供的哈希值进行比较。

下面是处理验证的代码:

 int verify_piece (void* addr, uint64_t piece_length, unsigned char* piece_sha)
 {
     SHA_CTX sha;
     unsigned char sha1result[20];

     SHA1_Init(&sha);
     SHA1_Update(&sha, addr, piece_length);
     SHA1_Final(sha1result, &sha);

     if (memcmp(sha1result, piece_sha, 20)) {
          printf("Expected: ");
          print_sha1(piece_sha);
          printf("Received: ");
          print_sha1(sha1result);
          return 0;
     } else
          return 1;
  }

为了追踪错误,我安装了两个函数:write_in Correct_piece()write_ Correct_piece()。当 verify_piece() 失败时,将调用第一个函数 write_in Correct_piece()。它将验证失败的部分写入文件,以便我可以使用 hexdump 检查它。

 void write_incorrect_piece (void* piece, uint32_t piece_length, uint32_t index)
 {
     FILE* failed_piece = fopen("failed_piece.out", "w+");

     write(fileno(failed_piece), piece, piece_length);
     fclose(failed_piece);
     printf("ERROR: Wrote piece #%d to failed_piece.out for inspection with hexdump.\n",
            index);

     write_correct_piece(piece_length, index);
     exit(1);
 }

正如你所看到的,write_in Correct_piece()采用参数void*piece,这是一个指向验证失败的片段的指针,uint32_tpiece_length,这是片段的长度,uint32_t index 是验证失败的片段的索引。然后,它将错误的片段复制到名为 failed_piece.out 的文件中。

您会注意到,在调用 exit() 之前,write_in Correct_piece() 会调用函数 write_ Correct_piece()。这是我为帮助调试此错误而编写的第二个函数。它采用 torrent 客户端尝试下载的文件的已知良好版本 (known_good.iso),然后将相关片段复制到文件 ( Correct_piece.out) 中,以便我可以将其与包含错误片段的文件进行比较。

void write_correct_piece (uint32_t piece_length, uint32_t index)
{
     FILE* known_good = fopen("known_good.iso", "r+");
     FILE* correct_piece = fopen("correct_piece.out", "w+");

     /* get file size for MMAPing */
     struct stat st;
     stat("known_good.iso", &st);
     long size = st.st_size;

     void* addr = mmap(0,
                       size,
                       PROT_READ | PROT_WRITE,
                       MAP_SHARED,
                       fileno(known_good),
                       0);

     write(fileno(correct_piece), addr + piece_length * index, piece_length);
     munmap(addr, size);
     fclose(known_good);
     fclose(correct_piece);
}

然后,我取出这两个写入的文件,并使用 hexdump 对其进行格式化,以便输出是人类可读的,然后尝试通过 diff 比较它们,就像这样

$ hexdump failed_piece.out > failed_piece.dump
$ hexdump correct_piece.out > correct_piece.dump
$ diff correct_piece.dump failed_piece.dump

令我惊讶的是,diff 没有输出任何内容。这些文件完全相同。那么,为什么该片段的 SHA1 哈希值与 torrent 文件所期望的不同呢?我随后想,也许元数据文件中的 SHA1 哈希值不正确。如果是这样的话,我希望客户端总是无法验证第 4 部分,因此我修改了 torrent 客户端的调度程序,只尝试下载第 4 部分,看看它是否确实总是无法验证。

Successfully downloaded piece: #4

卧槽?现在我陷入了僵局。我认为可能有几个不同的原因导致了这种情况:

  • 客户端的网络代码中实际上存在一个错误,但我在 write_in Correct_piece()write_ Correct_piece() 中引入了一个错误> 这样无论输入的完整性如何,它都会生成两个相同的文件。
  • verify_piece() 中存在错误,导致其偶尔失败。
  • 我没有考虑到一些事情。

然而,在我的一生中,我找不到上述任何代码的任何问题,所以我希望你们好心人能够指出我所缺少的内容。

客户端的完整源代码位于 https://github.com/robertseaton/slug

编辑:verify_piece() 中将 strncmp() 替换为 memcmp() 并清理了 write_in Correct_piece ()write_ Correct_piece()

编辑:这是一个失败片段上的 SHA1 哈希值示例,当我手动比较它时,它看起来是正确的。正如您所看到的,哈希值根本不相似:

Expected: 239 66 216 164 16 120 73 24 1 236 116 173 144 85 243 152 160 165 64 231 
Received: 214 94 49 185 54 159 255 201 214 137 102 23 223 76 102 138 89 94 154 69 
Failed to verify piece: #13

I've been working on writing a BitTorrent client in C in my spare time. However -- seemingly at random -- my client will fail to verify that a received piece of a torrent is correct with the message:

Failed to verify piece: #4

Background: a .torrent file contains the relevant metadata for the file-to-be-downloaded, including a SHA1 hash of each piece (a block of the file-to-be-downloaded), so -- after downloading a piece -- I take its SHA1 hash and compare it to the one provided by the torrent's metadata file.

Here's the code that handles verification:

 int verify_piece (void* addr, uint64_t piece_length, unsigned char* piece_sha)
 {
     SHA_CTX sha;
     unsigned char sha1result[20];

     SHA1_Init(&sha);
     SHA1_Update(&sha, addr, piece_length);
     SHA1_Final(sha1result, &sha);

     if (memcmp(sha1result, piece_sha, 20)) {
          printf("Expected: ");
          print_sha1(piece_sha);
          printf("Received: ");
          print_sha1(sha1result);
          return 0;
     } else
          return 1;
  }

In an attempt to track down the bug, I rigged up two functions: write_incorrect_piece() and write_correct_piece(). The first function, write_incorrect_piece() is called when verify_piece() fails. It writes the piece that failed verification to a file so that I can inspect it with hexdump.

 void write_incorrect_piece (void* piece, uint32_t piece_length, uint32_t index)
 {
     FILE* failed_piece = fopen("failed_piece.out", "w+");

     write(fileno(failed_piece), piece, piece_length);
     fclose(failed_piece);
     printf("ERROR: Wrote piece #%d to failed_piece.out for inspection with hexdump.\n",
            index);

     write_correct_piece(piece_length, index);
     exit(1);
 }

As you can see, write_incorrect_piece() takes the parameters void* piece, which is a pointer to the piece which failed verification, uint32_t piece_length, which is the length of the piece, and uint32_t index which is the index of the piece which failed verification. It then copies the incorrect piece to a file called failed_piece.out.

You'll notice that before calling exit(), write_incorrect_piece() calls the function write_correct_piece(). This is the second function I wrote to help with debugging this error. It takes a known good version of the file that the torrent client is attempting to download (known_good.iso) and then copies the relevant piece to a file (correct_piece.out) so that I can compare it to the file containing the incorrect piece.

void write_correct_piece (uint32_t piece_length, uint32_t index)
{
     FILE* known_good = fopen("known_good.iso", "r+");
     FILE* correct_piece = fopen("correct_piece.out", "w+");

     /* get file size for MMAPing */
     struct stat st;
     stat("known_good.iso", &st);
     long size = st.st_size;

     void* addr = mmap(0,
                       size,
                       PROT_READ | PROT_WRITE,
                       MAP_SHARED,
                       fileno(known_good),
                       0);

     write(fileno(correct_piece), addr + piece_length * index, piece_length);
     munmap(addr, size);
     fclose(known_good);
     fclose(correct_piece);
}

I then took the two written files and formatted them with hexdump so that the output would be human readable and then tried to compare them via diff, like so

$ hexdump failed_piece.out > failed_piece.dump
$ hexdump correct_piece.out > correct_piece.dump
$ diff correct_piece.dump failed_piece.dump

To my surprise, diff didn't output anything. The files are exactly the same. Why, then, was the SHA1 hash of the piece different from the one expected by the torrent file? Perhaps, I then thought, the SHA1 hash from the metadata file was incorrect. If that's the case, I would expect the client to always fail to verify piece #4, so I modified the torrent client's scheduler to only attempt to download piece #4 to see if it does, in fact, always fail verification.

Successfully downloaded piece: #4

Wtf? Now I'm at an impasse. I figure a couple of different things could be causing this:

  • There actually is a bug in the client's network code, but I have introduced a bug in write_incorrect_piece() and write_correct_piece() so that it produces two identical files regardless of the integrity of the inputs.
  • There is a bug in verify_piece(), causing it to fail sporadically.
  • I have failed to consider something.

For the life of me, however, I can't find any problems with any of the above code, so I'm hoping you fine folks can point out what I'm missing.

The entire source of the client is at https://github.com/robertseaton/slug.

Edit: Replaced strncmp() with memcmp() in verify_piece() and cleaned up both write_incorrect_piece() and write_correct_piece().

Edit: Here's an example of the SHA1 hashes on a failed piece that seems correct when I manually diff it. As you can see, the hashes are not similar at all:

Expected: 239 66 216 164 16 120 73 24 1 236 116 173 144 85 243 152 160 165 64 231 
Received: 214 94 49 185 54 159 255 201 214 137 102 23 223 76 102 138 89 94 154 69 
Failed to verify piece: #13

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-12-14 05:51:55

好的,如果我们看一下 verify_piece() 的调用,我们会看到以下内容:

 if (verify_piece(p->torrent->mmap + index * p->torrent->piece_length,
     p->torrent->piece_length, p->torrent->pieces[index].sha1)) {

现在,我们简单地知道第一个和第二个参数是正确的,因为稍后程序调用时会重用它write_in Correct_piece() 我们已经验证了它的输出是正确的。因此,我们可以专注于第三个参数,p->torrent->pieces[index].sha1

乍一看,该参数看起来是正确的,因为我们在整个函数中使用了 index。但是,请考虑一下,如果 index (虽然在正确排序的数组上完全有效)被用作不再包含假定顺序的片段的数组的索引,该怎么办?

Viola! 罪魁祸首(在 __schedule() 中):

qsort(t->pieces, t->num_pieces, sizeof(struct Piece), rarest);

注释掉对 qsort() 的调用,客户端就会按预期运行。

Okay, so if we have a look at the invocation of verify_piece(), we see this:

 if (verify_piece(p->torrent->mmap + index * p->torrent->piece_length,
     p->torrent->piece_length, p->torrent->pieces[index].sha1)) {

Now, trivially, we know that the first and second parameters are correct since it is later reused when the program calls write_incorrect_piece() and we have already verified that its output is correct. So, we are free to focus on the third parameter, p->torrent->pieces[index].sha1.

At first glance, the parameter looks correct, since we are using index throughout the function. However, consider, what if index -- while perfectly valid on a correctly sorted array -- is being used as an index to an array that no longer contains the pieces in the assumed order?

Viola! Here's the culprit (in __schedule()):

qsort(t->pieces, t->num_pieces, sizeof(struct Piece), rarest);

Comment out the call to qsort() and the client runs as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文