tpwphsck() verify handshake - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 8da0e6a47d6a1ef74286752a48fa0d67c33cfe34
 (DIR) parent d8cb67c19cd23514c75e2697b7da2389d03d5c10
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Fri,  3 Nov 2017 17:48:07 +0100
       
       pwphsck() verify handshake
       
       Diffstat:
         M libeech.c                           |      26 ++++++++++++++++++++++++--
       
       1 file changed, 24 insertions(+), 2 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -32,6 +32,7 @@ static int peercnt(struct peer *);
        /* handle peer wire protocol (PWP) messages */
        ssize_t pwphs(struct torrent *, struct peer *);
        ssize_t pwphsrecv(struct peer *);
       +int pwphsck(struct torrent *, char *, long);
        
        static char *
        peerid()
       t@@ -208,7 +209,9 @@ addpeer(struct peer *pl, char *host, int port)
                p->state = 0;
                p->next = pl;
                p->port = port;
       +        p->buflen = 0;
                memcpy(p->host, host, HOST_NAME_MAX);
       +        memset(p->buf, 0, MSGSIZ);
        
                return p;
        }
       t@@ -267,14 +270,33 @@ pwphsrecv(struct peer *p)
                ssize_t r;
        
                /* read message length, sent as 4 bytes integer */
       -        p->buflen = 0;
       -        while (p->buflen < 68 && (r = recv(p->fd, p->buf, 68, 0)) > 0)
       +        while ((r = recv(p->fd, p->buf, 68 - p->buflen, 0)) > 0)
                        p->buflen += r;
        
       +        if (r < 0)
       +                return -1;
       +
                return p->buflen;
        }
        
        int
       +pwphsck(struct torrent *t, char *hs, long l)
       +{
       +        if (l != 68)
       +                return -1;
       +        if (hs[0] != 19)
       +                return -1;
       +        if (memcmp(hs+1, "BitTorrent protocol", 19))
       +                return -1;
       +        if (memcmp(hs+28, t->ih, 20))
       +                return -1;
       +        if (!memcmp(hs+48, t->id, 20))
       +                return -1;
       +
       +        return 0;
       +}
       +
       +int
        glch_loadtorrent(struct torrent *t, char *b, size_t s)
        {
                struct be v;