tCleanup code / remove useless "static" keywords - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 93a726cdb20ad6b3b46f0cda4d7436cc262f5d9f
 (DIR) parent 1caf6fccd437de0e14100d0138163ee20003750a
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Tue, 24 Mar 2020 12:52:52 +0100
       
       Cleanup code / remove useless "static" keywords
       
       Diffstat:
         M libeech.c                           |     147 +++++++++++++------------------
       
       1 file changed, 62 insertions(+), 85 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -55,25 +55,24 @@ static long torrentsize(struct torrent *);
        static long torrentfiles(struct torrent *);
        static int chktorrent(struct be *);
        
       -/* helpers to deal with pieces/blocks */
       -static long readpiece(struct torrent *, struct piece *, long);
       -static long writepiece(struct torrent *, struct piece *);
       -static int chkpiece(struct torrent *, struct piece *, long);
       -
        /* manage a list of peers */
        static struct peer * addpeer(struct peer *, struct peer *, char *, int);
        static struct peer * getpeer(struct peer *, char *, int);
        static struct peer * delpeer(struct peer *, struct peer *);
        static int peercnt(struct peer *);
        
       +/* verify handshake validity */
       +static int hsck(struct torrent *, char *, long);
       +
       +/* helpers to deal with pieces/blocks */
       +static long readpiece(struct torrent *, struct piece *, long);
       +static long writepiece(struct torrent *, struct piece *);
       +static int chkpiece(struct torrent *, struct piece *, long);
       +
        /* handle peer wire protocol (PWP) messages */
        static ssize_t pwprecv(struct peer *);
        static ssize_t pwpsend(struct peer *, char *, size_t);
        
       -/* receive and verify handshakes */
       -static ssize_t hsrecv(struct peer *);
       -static int hsck(struct torrent *, char *, long);
       -
        /* send different PWP messages */
        static ssize_t send_handshake(struct torrent *, struct peer *);
        static ssize_t send_state(struct peer *, int);
       t@@ -84,12 +83,12 @@ static ssize_t send_piece(struct torrent *, struct peer *, size_t, size_t, size_
        static ssize_t send_cancel(struct torrent *, struct peer *);
        
        /* callbacks for received PWP messages */
       +static int cb_handshake(struct torrent *, 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 *);
       -static int cb_handshake(struct torrent *, struct peer *);
        
        /* various wrappers around PWP messages */
        static int catchup(struct torrent *, struct peer *);
       t@@ -108,14 +107,14 @@ static int p2p_poll(struct torrent *, int);
        
        
        /* check wether a specific bit is set or not in a byte */
       -static int
       +int
        bit(char *bits, long off)
        {
                return !!(bits[off / 8] & (1 << (7 - off%8)));
        }
        
        /* set a bit in a byte to one */
       -static char *
       +char *
        setbit(char *bits, long off)
        {
                bits[off / 8] |= (1 << (7 - off%8));
       t@@ -123,7 +122,7 @@ setbit(char *bits, long off)
        }
        
        /* set a bit in a byte to zero */
       -static char *
       +char *
        clrbit(char *bits, long off)
        {
                bits[off / sizeof(*bits)] &= ~(1 << (7 - off%8));
       t@@ -131,7 +130,7 @@ clrbit(char *bits, long off)
        }
        
        /* generate a random peer ID */
       -static char *
       +char *
        peerid()
        {
                int n;
       t@@ -148,7 +147,7 @@ peerid()
        }
        
        /* Calculate full torrent size */
       -static long
       +long
        torrentsize(struct torrent *t)
        {
                int i;
       t@@ -180,7 +179,7 @@ torrentsize(struct torrent *t)
         * or "files" attribute in the torrent, removing one more malloc()
         * in the library.
         */
       -static long
       +long
        torrentfiles(struct torrent *t)
        {
                int i;
       t@@ -223,7 +222,7 @@ torrentfiles(struct torrent *t)
        }
        
        /* Verify that all mandatory keys of a torrent are present */
       -static int
       +int
        chktorrent(struct be *b)
        {
                struct be i;
       t@@ -272,7 +271,7 @@ chktorrent(struct be *b)
        }
        
        /* read a piece from a metafile (named after the SHA1 infohash) */
       -static long
       +long
        readpiece(struct torrent *t, struct piece *p, long n)
        {
                char hex[41];
       t@@ -297,7 +296,7 @@ readpiece(struct torrent *t, struct piece *p, long n)
        }
        
        /* write a piece to a metafile (named after the SHA1 infohash) */
       -static long
       +long
        writepiece(struct torrent *t, struct piece *p)
        {
                int fd;
       t@@ -331,7 +330,7 @@ writepiece(struct torrent *t, struct piece *p)
        }
        
        /* Verify that a piece matches its SHA1 hash */
       -static int
       +int
        chkpiece(struct torrent *t, struct piece *p, long n)
        {
                char hash[20];
       t@@ -341,7 +340,7 @@ chkpiece(struct torrent *t, struct piece *p, long n)
        }
        
        /* Initialize a peer and add it to the peer list */
       -static struct peer *
       +struct peer *
        addpeer(struct peer *pl, struct peer *p, char *host, int port)
        {
                p->fd = -1;
       t@@ -358,7 +357,7 @@ addpeer(struct peer *pl, struct peer *p, char *host, int port)
        }
        
        /* Find a peer by its host + port */
       -static struct peer *
       +struct peer *
        getpeer(struct peer *pl, char *host, int port)
        {
                struct peer *p;
       t@@ -377,7 +376,7 @@ getpeer(struct peer *pl, char *host, int port)
         * it instead.
         * Memory tracking of those peers is on the caller responsibility.
         */
       -static struct peer *
       +struct peer *
        delpeer(struct peer *pl, struct peer *p)
        {
                struct peer *tmp;
       t@@ -394,7 +393,7 @@ delpeer(struct peer *pl, struct peer *p)
        }
        
        /* Count the number of peers currently added to this torrent */
       -static int
       +int
        peercnt(struct peer *pl)
        {
                return (pl ? 1 + peercnt(pl->next) : 0);
       t@@ -424,7 +423,7 @@ peercnt(struct peer *pl)
         * When it returns 0, it means the message is fully read and can be
         * parsed.
         */
       -static ssize_t
       + ssize_t
        pwprecv(struct peer *p)
        {
                ssize_t l, r;
       t@@ -454,7 +453,7 @@ pwprecv(struct peer *p)
         * Wrapper to the send() syscall. It will loop to send the full message,
         * until everything is sent, or an error occurs.
         */
       -static ssize_t
       +ssize_t
        pwpsend(struct peer *p, char *m, size_t sz)
        {
                size_t s = 0;
       t@@ -471,6 +470,25 @@ pwpsend(struct peer *p, char *m, size_t sz)
                return s;
        }
        
       +
       +/* Verify that a handshake is valid according to a torrent */
       +int
       +hsck(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;
       +}
       +
        /*
         * Create and send a handshake message. From the Bittorrent spec:
         *
       t@@ -486,7 +504,7 @@ pwpsend(struct peer *p, char *m, size_t sz)
         * Info hash: SHA1 hash of the full "info" key from the torrent
         * Peer ID: Our own ID, used for identification (or not at all)
         */
       -static ssize_t
       +ssize_t
        send_handshake(struct torrent *t, struct peer *p)
        {
                char m[68];
       t@@ -501,46 +519,6 @@ send_handshake(struct torrent *t, struct peer *p)
        }
        
        /*
       - * Wrapper to the recv() syscall to receive handshakes
       - * Handshake messages size is fixed in BTP/1.0 to 68 bytes.
       - * We cannot receive it as a standard PWP message.
       - */
       -static ssize_t
       -hsrecv(struct peer *p)
       -{
       -        ssize_t l, r;
       -
       -        /* expected handshake length */
       -        l = 68;
       -
       -        while ((r = recv(p->fd, p->rxbuf + p->rxbufsz, l - p->rxbufsz, 0)) > 0)
       -                p->rxbufsz += r;
       -
       -        if (r < 0 && errno != EAGAIN)
       -                return -1;
       -
       -        return l - p->rxbufsz;
       -}
       -
       -/* Verify that a handshake is valid according to a torrent */
       -static int
       -hsck(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;
       -}
       -
       -/*
         * Send a change of status PWP message.
         * These messages have no payload and can be sent the same way.
         *
       t@@ -550,7 +528,7 @@ hsck(struct torrent *t, char *hs, long l)
         *         3: UNINTERESTED
         *
         */
       -static ssize_t
       +ssize_t
        send_state(struct peer *p, int t)
        {
                char m[MSGSIZ];
       t@@ -567,7 +545,7 @@ send_state(struct peer *p, int t)
        }
        
        /* Send a HAVE PWP message */
       -static ssize_t
       +ssize_t
        send_have(struct peer *p, long n)
        {
                char m[MSGSIZ];
       t@@ -590,7 +568,7 @@ send_have(struct peer *p, long n)
         * Send a BITFIELD message with our full bitfield (no cheating with HAVE
         * messages here)
         */
       -static ssize_t
       +ssize_t
        send_bitfield(struct torrent *t, struct peer *p)
        {
                char m[MSGSIZ];
       t@@ -627,7 +605,7 @@ send_bitfield(struct torrent *t, struct peer *p)
         *
         * TODO: Implement a function to select a piece from different algorithms
         */
       -static ssize_t
       +ssize_t
        send_request(struct torrent *t, struct peer *p)
        {
                char m[MSGSIZ];
       t@@ -717,7 +695,7 @@ send_piece(struct torrent *t, struct peer *p, size_t i, size_t bl, size_t bo)
                return 0;
        }
        
       -static ssize_t
       +ssize_t
        send_cancel(struct torrent *t, struct peer *p)
        {
                char m[MSGSIZ];
       t@@ -767,7 +745,7 @@ send_cancel(struct torrent *t, struct peer *p)
        }
        
        /* Receive and treat a change of status from the peer */
       -static int
       +int
        cb_state(struct peer *p, int type)
        {
                switch (type) {
       t@@ -788,7 +766,7 @@ cb_state(struct peer *p, int type)
        }
        
        /* Receive and treat a HAVE message from the peer */
       -static int
       +int
        cb_have(struct peer *p, size_t sz, char *pl)
        {
                if (sz != 4)
       t@@ -799,7 +777,7 @@ cb_have(struct peer *p, size_t sz, char *pl)
        }
        
        /* Receive and save the BITFIELD sent by a peer */
       -static int
       +int
        cb_bitfield(struct peer *p, size_t sz, char *pl)
        {
                memcpy(p->bf, pl, sz);
       t@@ -807,7 +785,7 @@ cb_bitfield(struct peer *p, size_t sz, char *pl)
        }
        
        /* Receive a block REQUEST from a peer */
       -static int
       +int
        cb_request(struct torrent *t, struct peer *p, size_t sz, char *pl)
        {
                ssize_t i, bo, bl;
       t@@ -833,13 +811,12 @@ cb_request(struct torrent *t, struct peer *p, size_t sz, char *pl)
         * TODO: Keep track of received blocks, so we can discard a piece if we
         * receive all blocks, but the hash doesn't match what we expect.
         */
       -static int
       +int
        cb_piece(struct torrent *t, struct peer *p, size_t sz, char *pl)
        {
                int i;
                size_t idx, off;
                char *blk, bl[BLKNUM / 8 + !!(BLKNUM % 8)];
       -        struct peer *b;
        
                idx = ntohl(U32(pl));
                off = ntohl(U32(pl+4));
       t@@ -883,7 +860,7 @@ cb_handshake(struct torrent *t, struct peer *p)
        /*
         * Get a peer to catch up new pieces we had received
         */
       -static int
       +int
        catchup(struct torrent *t, struct peer *p)
        {
                long i;
       t@@ -900,7 +877,7 @@ catchup(struct torrent *t, struct peer *p)
        /*
         * Sent CANCEL messages for all pieces still requested that we have
         */
       -static int
       +int
        cancelrq(struct torrent *t, struct peer *p)
        {
                if (p->piece.n >= 0 && bit(t->bf, p->piece.n))
       t@@ -913,7 +890,7 @@ cancelrq(struct torrent *t, struct peer *p)
         * PWP message received callback
         * This function will run the appropriate function based on the message type
         */
       -static int
       +int
        callbacks(struct torrent *t, struct peer *p, int type, size_t sz, char *pl)
        {
                switch (type) {
       t@@ -946,7 +923,7 @@ callbacks(struct torrent *t, struct peer *p, int type, size_t sz, char *pl)
        }
        
        /* Logic to send messages when peer is ready */
       -static int
       +int
        pwptx(struct torrent *t, struct peer *p)
        {
                /* send handshake */
       t@@ -986,7 +963,7 @@ pwptx(struct torrent *t, struct peer *p)
        }
        
        /* Logic to receive messages when peer sent data */
       -static int
       +int
        pwprx(struct torrent *t, struct peer *p)
        {
                int type;
       t@@ -1007,7 +984,7 @@ pwprx(struct torrent *t, struct peer *p)
        }
        
        /* Initiate connection with a peer */
       -static int
       +int
        p2p_connect(char *host, int port)
        {
                int fd = -1;
       t@@ -1039,7 +1016,7 @@ p2p_connect(char *host, int port)
        }
        
        /* Poll() all peers for read/write readiness on each iteration */
       -static int
       +int
        p2p_poll(struct torrent *t, int timeout)
        {
                int r, npeer = 0;