tImmediately send piece upon REQUEST - libeech - bittorrent library
(HTM) git clone git://z3bra.org/libeech.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 1caf6fccd437de0e14100d0138163ee20003750a
(DIR) parent 3698a0b5554fca72a59bbad6d7942387a168071b
(HTM) Author: Willy Goiffon <dev@z3bra.org>
Date: Tue, 24 Mar 2020 11:18:42 +0100
Immediately send piece upon REQUEST
Diffstat:
M libeech.c | 47 ++++++++++++++++++++++++++++++-
1 file changed, 46 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/libeech.c b/libeech.c
t@@ -80,6 +80,7 @@ static ssize_t send_state(struct peer *, int);
static ssize_t send_have(struct peer *, long);
static ssize_t send_bitfield(struct torrent *, struct peer *);
static ssize_t send_request(struct torrent *, struct peer *);
+static ssize_t send_piece(struct torrent *, struct peer *, size_t, size_t, size_t);
static ssize_t send_cancel(struct torrent *, struct peer *);
/* callbacks for received PWP messages */
t@@ -682,6 +683,40 @@ send_request(struct torrent *t, struct peer *p)
return 0;
}
+ssize_t
+send_piece(struct torrent *t, struct peer *p, size_t i, size_t bl, size_t bo)
+{
+ char m[MSGSIZ];
+ struct piece piece;
+
+ if (readpiece(t, &piece, i) < 0)
+ return -1;
+
+ if (bo + bl > (size_t)piece.sz)
+ return -1;
+
+ memset(m, 0, MSGSIZ);
+
+ /* Message length */
+ U32(m) = htonl(bl + 9);
+
+ /* Message type */
+ m[4] = PIECE;
+
+ /* Piece index */
+ U32(m+5) = htonl(i);
+
+ /* Block offset */
+ U32(m+9) = htonl(bo);
+
+ /* Block length */
+ U32(m+13) = htonl(bl);
+
+ pwpsend(p, m, bl + 9);
+
+ return 0;
+}
+
static ssize_t
send_cancel(struct torrent *t, struct peer *p)
{
t@@ -775,7 +810,17 @@ cb_bitfield(struct peer *p, size_t sz, char *pl)
static int
cb_request(struct torrent *t, struct peer *p, size_t sz, char *pl)
{
- return 0;
+ ssize_t i, bo, bl;
+
+ i = ntohl(U32(pl));
+ bo = ntohl(U32(pl+4));
+ bl = ntohl(U32(pl+8));
+
+ /* exit if we don't have the piece */
+ if (!bit(t->bf, i))
+ return -1;
+
+ return send_piece(t, p, i, bo, bl);
}
/*