tRename function names for readability - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e158a4f97b00d3b99b9d2d11a40af07db9883858
 (DIR) parent 56d2950fcc3d48250a2eed27cbc2c551f83ee0ec
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon, 23 Mar 2020 13:25:24 +0100
       
       Rename function names for readability
       
       Diffstat:
         M libeech.c                           |      97 ++++++++++++++++---------------
       
       1 file changed, 49 insertions(+), 48 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -70,38 +70,38 @@ static ssize_t pwprecv(struct peer *);
        static ssize_t pwpsend(struct peer *, char *, size_t);
        
        /* send, receive and verify handshakes */
       -static ssize_t pwptxhs(struct torrent *, struct peer *);
       -static ssize_t pwprxhs(struct peer *);
        static int pwphsck(struct torrent *, char *, long);
        
        /* send different PWP messages */
       -static ssize_t pwptxst(struct peer *, int);
       -static ssize_t pwptxhv(struct peer *, long);
       -static ssize_t pwptxbf(struct torrent *, struct peer *);
       -static ssize_t pwptxrq(struct torrent *, struct peer *);
       -static ssize_t pwptxcl(struct torrent *, struct peer *);
       -
       -/* receive different PWP messages */
       -static int pwprxst(struct peer *, int);
       -static int pwprxhv(struct peer *, size_t, char *);
       -static int pwprxbf(struct peer *, size_t, char *);
       -static int pwprxrq(struct torrent *, struct peer *, size_t, char *);
       -static int pwprxpc(struct torrent *, struct peer *, size_t, char *);
       +static ssize_t send_handshake(struct torrent *, struct peer *);
       +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_cancel(struct torrent *, struct peer *);
       +
       +/* callbacks for received PWP messages */
       +static ssize_t cb_handshake(struct peer *);
       +static int cb_state(struct peer *, int);
       +static int cb_have(struct peer *, size_t, char *);
       +static int cb_bitfield(struct peer *, size_t, char *);
       +static int cb_request(struct torrent *, struct peer *, size_t, char *);
       +static int cb_piece(struct torrent *, struct peer *, size_t, char *);
        
        /* various wrappers around PWP messages */
        static int catchup(struct torrent *, struct peer *);
        static int cancelrq(struct torrent *, struct peer *);
        
        /* handle all received PWP messages */
       -static int pwprxcb(struct torrent *, struct peer *, int, size_t, char *);
       +static int callbacks(struct torrent *, struct peer *, int, size_t, char *);
        
        /* functions to run when a peer can receive or has sent bytes over the wire */
        static int pwptx(struct torrent *, struct peer *);
        static int pwprx(struct torrent *, struct peer *);
        
        /* p2p network management */
       -static int netconn(char *, int);
       -static int netloop(struct torrent *, int);
       +static int p2p_connect(char *, int);
       +static int p2p_poll(struct torrent *, int);
        
        
        /* check wether a specific bit is set or not in a byte */
       t@@ -485,7 +485,7 @@ pwpsend(struct peer *p, char *m, size_t sz)
         * Peer ID: Our own ID, used for identification (or not at all)
         */
        static ssize_t
       -pwptxhs(struct torrent *t, struct peer *p)
       +send_handshake(struct torrent *t, struct peer *p)
        {
                char m[68];
        
       t@@ -504,7 +504,7 @@ pwptxhs(struct torrent *t, struct peer *p)
         * We cannot receive it as a standard PWP message.
         */
        static ssize_t
       -pwprxhs(struct peer *p)
       +cb_handshake(struct peer *p)
        {
                ssize_t r;
        
       t@@ -546,7 +546,7 @@ pwphsck(struct torrent *t, char *hs, long l)
         *
         */
        static ssize_t
       -pwptxst(struct peer *p, int t)
       +send_state(struct peer *p, int t)
        {
                char m[MSGSIZ];
        
       t@@ -563,7 +563,7 @@ pwptxst(struct peer *p, int t)
        
        /* Send a HAVE PWP message */
        static ssize_t
       -pwptxhv(struct peer *p, long n)
       +send_have(struct peer *p, long n)
        {
                char m[MSGSIZ];
        
       t@@ -586,7 +586,7 @@ pwptxhv(struct peer *p, long n)
         * messages here)
         */
        static ssize_t
       -pwptxbf(struct torrent *t, struct peer *p)
       +send_bitfield(struct torrent *t, struct peer *p)
        {
                char m[MSGSIZ];
                ssize_t l;
       t@@ -623,7 +623,7 @@ pwptxbf(struct torrent *t, struct peer *p)
         * TODO: Implement a function to select a piece from different algorithms
         */
        static ssize_t
       -pwptxrq(struct torrent *t, struct peer *p)
       +send_request(struct torrent *t, struct peer *p)
        {
                char m[MSGSIZ];
                ssize_t i, bl, bo;
       t@@ -679,7 +679,7 @@ pwptxrq(struct torrent *t, struct peer *p)
        }
        
        static ssize_t
       -pwptxcl(struct torrent *t, struct peer *p)
       +send_cancel(struct torrent *t, struct peer *p)
        {
                char m[MSGSIZ];
                ssize_t i, bl, bo;
       t@@ -729,7 +729,7 @@ pwptxcl(struct torrent *t, struct peer *p)
        
        /* Receive and treat a change of status from the peer */
        static int
       -pwprxst(struct peer *p, int type)
       +cb_state(struct peer *p, int type)
        {
                switch (type) {
                case CHOKE:
       t@@ -750,7 +750,7 @@ pwprxst(struct peer *p, int type)
        
        /* Receive and treat a HAVE message from the peer */
        static int
       -pwprxhv(struct peer *p, size_t sz, char *pl)
       +cb_have(struct peer *p, size_t sz, char *pl)
        {
                if (sz != 4)
                        return -1;
       t@@ -761,7 +761,7 @@ pwprxhv(struct peer *p, size_t sz, char *pl)
        
        /* Receive and save the BITFIELD sent by a peer */
        static int
       -pwprxbf(struct peer *p, size_t sz, char *pl)
       +cb_bitfield(struct peer *p, size_t sz, char *pl)
        {
                memcpy(p->bf, pl, sz);
                return 0;
       t@@ -769,7 +769,7 @@ pwprxbf(struct peer *p, size_t sz, char *pl)
        
        /* Receive a block REQUEST from a peer */
        static int
       -pwprxrq(struct torrent *t, struct peer *p, size_t sz, char *pl)
       +cb_request(struct torrent *t, struct peer *p, size_t sz, char *pl)
        {
                return 0;
        }
       t@@ -785,7 +785,7 @@ pwprxrq(struct torrent *t, struct peer *p, size_t sz, char *pl)
         * receive all blocks, but the hash doesn't match what we expect.
         */
        static int
       -pwprxpc(struct torrent *t, struct peer *p, size_t sz, char *pl)
       +cb_piece(struct torrent *t, struct peer *p, size_t sz, char *pl)
        {
                int i;
                size_t idx, off;
       t@@ -829,7 +829,7 @@ catchup(struct torrent *t, struct peer *p)
        
                for (i = 0; i < t->npiece; i++) {
                        if (bit(t->bf, i) && !bit(p->view, i)) {
       -                        pwptxhv(p, i);
       +                        send_have(p, i);
                                setbit(p->view, i);
                        }
                }
       t@@ -843,7 +843,7 @@ static int
        cancelrq(struct torrent *t, struct peer *p)
        {
                if (p->piece.n >= 0 && bit(t->bf, p->piece.n))
       -                pwptxcl(t, p);
       +                send_cancel(t, p);
        
                return 0;
        }
       t@@ -853,25 +853,26 @@ cancelrq(struct torrent *t, struct peer *p)
         * This function will run the appropriate function based on the message type
         */
        static int
       -pwprxcb(struct torrent *t, struct peer *p, int type, size_t sz, char *pl)
       +callbacks(struct torrent *t, struct peer *p, int type, size_t sz, char *pl)
        {
                switch (type) {
                        case CHOKE:
                        case UNCHOKE:
                        case INTERESTED:
                        case UNINTERESTED:
       -                        pwprxst(p, type);
       +                        cb_state(p, type);
                                break;
                        case HAVE:
       +                        cb_have(p, sz, pl);
                                break;
                        case BITFIELD:
       -                        pwprxbf(p, sz, pl);
       +                        cb_bitfield(p, sz, pl);
                                break;
                        case REQUEST:
       -                        pwprxrq(t, p, sz, pl);
       +                        cb_request(t, p, sz, pl);
                                break;
                        case PIECE:
       -                        pwprxpc(t, p, sz, pl);
       +                        cb_piece(t, p, sz, pl);
                                break;
                        case CANCEL:
                                /* not handled */
       t@@ -886,8 +887,8 @@ pwptx(struct torrent *t, struct peer *p)
        {
                /* send handshake */
                if (!(p->state & HANDSHAKESENT)) {
       -                pwptxhs(t, p);
       -                pwptxbf(t, p);
       +                send_handshake(t, p);
       +                send_bitfield(t, p);
                        p->state |= HANDSHAKESENT;
                }
        
       t@@ -896,12 +897,12 @@ pwptx(struct torrent *t, struct peer *p)
        
                if (p->state & LEECHING) {
                        if (!(p->state & AMINTERESTED)) {
       -                        pwptxst(p, INTERESTED);
       +                        send_state(p, INTERESTED);
                                p->state |= AMINTERESTED;
                        }
        
                        if (!(p->state & AMCHOKING))
       -                        pwptxrq(t, p);
       +                        send_request(t, p);
        
                        if (memcmp(p->piece.rq, p->piece.bl, PCENUM / 8 + !!(PCENUM % 8)))
                                cancelrq(t, p);
       t@@ -912,7 +913,7 @@ pwptx(struct torrent *t, struct peer *p)
                        if (memcmp(t->bf, p->view, PCENUM / 8 + !!(PCENUM % 8)))
                                catchup(t, p);
                        if (!(p->state & ISINTERESTED)) {
       -                        pwptxst(p, UNCHOKE);
       +                        send_state(p, UNCHOKE);
                                p->state &= ~(ISCHOKING);
                        }
                }
       t@@ -926,7 +927,7 @@ pwprx(struct torrent *t, struct peer *p)
        {
                /* receive and verify handshake */
                if (!(p->state & HANDSHAKERCVD)) {
       -                pwprxhs(p);
       +                cb_handshake(p);
                        if (p->rxbufsz >= 68 && pwphsck(t, p->rxbuf, p->rxbufsz)) {
                                delpeer(t->peers, p);
                                return -1;
       t@@ -934,7 +935,7 @@ pwprx(struct torrent *t, struct peer *p)
                        p->rxbufsz = 0;
                        p->state |= HANDSHAKERCVD;
                } else if (!pwprecv(p)) {
       -                pwprxcb(t, p, p->rxbuf[4], p->rxbufsz - 5, p->rxbuf+5);
       +                callbacks(t, p, p->rxbuf[4], p->rxbufsz - 5, p->rxbuf+5);
                        memset(p->rxbuf, 0, MSGSIZ);
                        p->rxbufsz = 0;
                }
       t@@ -944,7 +945,7 @@ pwprx(struct torrent *t, struct peer *p)
        
        /* Initiate connection with a peer */
        static int
       -netconn(char *host, int port)
       +p2p_connect(char *host, int port)
        {
                int fd = -1;
                int flags = 0;
       t@@ -976,7 +977,7 @@ netconn(char *host, int port)
        
        /* Poll() all peers for read/write readiness on each iteration */
        static int
       -netloop(struct torrent *t, int timeout)
       +p2p_poll(struct torrent *t, int timeout)
        {
                int r, npeer = 0;
                nfds_t i, nfds;
       t@@ -1091,7 +1092,7 @@ glch_addpeer(struct torrent *t, struct peer *p, char *host, int port)
                        return 0;
        
                addpeer(t->peers, p, host, port);
       -        p->fd = netconn(p->host, p->port);
       +        p->fd = p2p_connect(p->host, p->port);
                if (!p || p->fd < 0) {
                        free(p);
                        return -1;
       t@@ -1104,12 +1105,12 @@ glch_addpeer(struct torrent *t, struct peer *p, char *host, int port)
        
        /*
         * Perform one network iteration with all the peers, or timeout if
       - * noone replies
       + * no one replies
         */
        int
        glch_leech(struct torrent *t, int timeout)
        {
       -        return netloop(t, timeout);
       +        return p2p_poll(t, timeout);
        }
        
        /* Return the number of currently downloaded pieces */