tDo not create peer in addpeer() - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit ce235dc3c86e16ce041a209277ebfa9cc50848d2
 (DIR) parent fad1cd5caca4176138b2378e02f18c782ecd886f
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Thu,  5 Jul 2018 13:56:30 +0200
       
       Do not create peer in addpeer()
       
       Diffstat:
         M libeech.c                           |      24 +++++-------------------
         M libeech.h                           |       2 +-
         M torrent.c                           |       3 ++-
       
       3 files changed, 8 insertions(+), 21 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -60,7 +60,7 @@ 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 *, char *, int);
       +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 *);
       t@@ -332,22 +332,10 @@ chkpiece(struct torrent *t, struct piece *p, long n)
                return memcmp(t->ph + n*20, hash, 20);
        }
        
       -/*
       - * Create a new peer struct, and add it to the list of peers
       - *
       - * TODO: Remove the malloc() and have the caller pass the `struct peer`
       - * to add as an argument.
       - * Memory tracking of those peers is on the caller responsibility.
       - */
       +/* Initialize a peer and add it to the peer list */
        static struct peer *
       -addpeer(struct peer *pl, char *host, int port)
       +addpeer(struct peer *pl, struct peer *p, char *host, int port)
        {
       -        struct peer *p;
       -
       -        p = malloc(sizeof(*p));
       -        if (!p)
       -                return NULL;
       -
                p->fd = -1;
                p->state = 0;
                p->next = pl;
       t@@ -972,15 +960,13 @@ glch_loadtorrent(struct torrent *t, char *b, size_t s)
         * RETURN VALUE: returns 0 if connection is established, -1 otherwise
         */
        int
       -glch_addpeer(struct torrent *t, char *host, int port)
       +glch_addpeer(struct torrent *t, struct peer *p, char *host, int port)
        {
       -        struct peer *p;
       -
                /* ignore request if peer already exists */
                if (getpeer(t->peers, host, port))
                        return 0;
        
       -        p = addpeer(t->peers, host, port);
       +        addpeer(t->peers, p, host, port);
                p->fd = netconn(p->host, p->port);
                if (!p || p->fd < 0) {
                        free(p);
 (DIR) diff --git a/libeech.h b/libeech.h
       t@@ -64,5 +64,5 @@ struct torrent {
        };
        
        int glch_loadtorrent(struct torrent *, char *, size_t);
       -int glch_addpeer(struct torrent *, char *, int);
       +int glch_addpeer(struct torrent *, struct peer *, char *, int);
        int glch_leech(struct torrent *, int);
 (DIR) diff --git a/torrent.c b/torrent.c
       t@@ -53,6 +53,7 @@ main(int argc, char *argv[])
                char *b, *h = "localhost";
                char hex[41];
                struct torrent t;
       +        struct peer peer;
        
                ARGBEGIN {
                case 'h':
       t@@ -81,7 +82,7 @@ main(int argc, char *argv[])
                for (i=0; i<t.nfile; i++)
                        printf("\t%s\n", t.files[i].path);
        
       -        glch_addpeer(&t, h, p);
       +        glch_addpeer(&t, &peer, h, p);
                for (;;)
                        glch_leech(&t, 5000);