tAdd pwprq() for requesting pieces - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e225e5d0cb91814ee69fa1ca0ed2d912791719fd
 (DIR) parent 92e07b92638cf8d3884da8aa60724057e2a6545f
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Sat, 30 Jun 2018 01:28:22 +0200
       
       Add pwprq() for requesting pieces
       
       Diffstat:
         M libeech.c                           |      64 +++++++++++++++++++++++++++++++
         M libeech.h                           |       1 +
       
       2 files changed, 65 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -67,6 +67,7 @@ static int pwphsck(struct torrent *, char *, long);
        
        static ssize_t pwpbf(struct torrent *, struct peer *);
        static ssize_t pwpchst(struct peer *, int);
       +static ssize_t pwprq(struct torrent *, struct peer *);
        
        static int pwptx(struct torrent *, struct peer *);
        static int pwprx(struct torrent *, struct peer *);
       t@@ -290,6 +291,7 @@ addpeer(struct peer *pl, char *host, int port)
                p->next = pl;
                p->port = port;
                p->rxbufsz = 0;
       +        p->piece.n = -1;
                memcpy(p->host, host, HOST_NAME_MAX);
                memset(p->bf, 0, PCENUM / 8);
                memset(p->rxbuf, 0, MSGSIZ);
       t@@ -447,6 +449,65 @@ pwpchst(struct peer *p, int t)
                return send(p->fd, m, 5, MSG_NOSIGNAL);
        }
        
       +static ssize_t
       +pwprq(struct torrent *t, struct peer *p)
       +{
       +        int i;
       +        char m[MSGSIZ];
       +        ssize_t bl, off = 0;
       +
       +        /* Find a missing piece we can get from the peer */
       +        for (i = 0; i >= t->npiece || (!bit(t->bf, i) && bit(p->bf, i)); i++);
       +
       +        p->piece.n = i;
       +        p->piece.sz = (i == t->npiece - 1) ? t->sz % t->psz : t->psz;
       +
       +        printf("Requesting piece of size %ld\n", p->piece.sz);
       +
       +        i = 0;
       +        while (i < p->piece.sz) {
       +                bl = BLKSIZ;
       +                if (i + BLKSIZ > p->piece.sz)
       +                        bl = p->piece.sz - i;
       +
       +                memset(m, 0, MSGSIZ);
       +                off = 0;
       +                /* Message length */
       +                m[off++] = (13 >> 24) & 0xff;
       +                m[off++] = (13 >> 16) & 0xff;
       +                m[off++] = (13 >> 8)  & 0xff;
       +                m[off++] = (13 >> 0)  & 0xff;
       +
       +                /* Message type */
       +                m[off++] = REQUEST;
       +
       +                /* Piece index */
       +                m[off++] = (p->piece.n >> 24) & 0xff;
       +                m[off++] = (p->piece.n >> 16) & 0xff;
       +                m[off++] = (p->piece.n >> 8)  & 0xff;
       +                m[off++] = (p->piece.n >> 0)  & 0xff;
       +
       +                /* Block offset */
       +                m[off++] = (i >> 24) & 0xff;
       +                m[off++] = (i >> 16) & 0xff;
       +                m[off++] = (i >> 8)  & 0xff;
       +                m[off++] = (i >> 0)  & 0xff;
       +
       +                /* Block length */
       +                m[off++] = (bl >> 24) & 0xff;
       +                m[off++] = (bl >> 16) & 0xff;
       +                m[off++] = (bl >> 8)  & 0xff;
       +                m[off++] = (bl >> 0)  & 0xff;
       +
       +                send(p->fd, m, 17, MSG_NOSIGNAL);
       +                printf("REQUEST #%ld: %ld/%ld\n", p->piece.n, i + bl, p->piece.sz);
       +
       +                i += bl;
       +        }
       +
       +        return 0;
       +}
       +
        static int
        pwptx(struct torrent *t, struct peer *p)
        {
       t@@ -462,6 +523,9 @@ pwptx(struct torrent *t, struct peer *p)
                        p->state |= AMINTERESTED;
                }
        
       +        if ((p->state & LEECHING) && !(p->state & AMCHOKING) && p->piece.n < 0)
       +                pwprq(t, p);
       +
                return 0;
        }
        
 (DIR) diff --git a/libeech.h b/libeech.h
       t@@ -25,6 +25,7 @@ struct piece {
                ssize_t sz;
                char blks[PCESIZ];
                char bf[PCESIZ / BLKSIZ];
       +        char rq[PCESIZ / BLKSIZ];
        };
        
        struct peer {