libeech

BitTorrent library
git clone git://z3bra.org/libeech.git
Log | Files | Refs | README | LICENSE

commit 51e138421357f7f58a511bb0eb9656aa9ed1674c
parent 03ccf2b025a14a05633ab312cdad29745d5071db
Author: z3bra <contactatz3bradotorg>
Date:   Mon,  9 Jul 2018 09:04:02 +0200

Check piece only when all blocks are received

Diffstat:
libeech.c | 17++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/libeech.c b/libeech.c @@ -736,18 +736,29 @@ pwprxrq(struct torrent *t, struct peer *p, size_t sz, char *pl) static int pwprxpc(struct torrent *t, struct peer *p, size_t sz, char *pl) { + int i; size_t idx, off; - char *blk; + char *blk, bl[BLKNUM / 8 + !!(BLKNUM % 8)]; struct peer *b; idx = ntohl(U32(pl)); off = ntohl(U32(pl+4)); blk = pl + 8; + /* calculate a full piece block bitfield, for comparison */ + memset(bl, 0, BLKNUM / 8 + !!(BLKNUM % 8)); + for (i = 0; i < (p->piece.sz / BLKSIZ + !!(p->piece.sz % BLKSIZ)); i++) + setbit(bl, i); + memcpy(p->piece.blks + off, blk, sz - 8); + setbit(p->piece.bl, off / BLKSIZ); + + /* Check piece hash and write it on disk when all blocks are received */ + if (!memcmp(p->piece.bl, bl, BLKNUM / 8 + !!(BLKNUM % 8))) { + if (!chkpiece(t, &p->piece, idx) && !writepiece(t, &(p->piece))) + setbit(t->bf, p->piece.n); - if (!chkpiece(t, &p->piece, idx) && !writepiece(t, &(p->piece))) { - setbit(t->bf, p->piece.n); + /* Reset piece number, no matter what */ p->piece.n = -1; }