tHelper to retrieve infohash from metadata - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 1f684681b6e996a18378c7567a1fa0b1fe903a45
 (DIR) parent 06340e51868101e64aa9275708e54b89b3c498f5
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Wed, 18 Oct 2017 23:56:38 +0200
       
       Helper to retrieve infohash from metadata
       
       Diffstat:
         M libeech.c                           |      63 +++++++++++++++++--------------
         M libeech.h                           |       3 ++-
         M torrent.c                           |       1 -
       
       3 files changed, 36 insertions(+), 31 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -15,29 +15,48 @@
        #include "util.h"
        #include "libeech.h"
        
       +/* helpers to retrieve attributes from metadata */
       +static char * peerid();
       +static unsigned char * infohash(struct torrent *);
        
        /* Initialization of the torrent struct */
       -static char * peerid();
        static int loadfile(struct torrent *, char *);
        static int loadtracker(struct torrent *);
       -static int loadinfohash(struct torrent *);
        static int loadpeerid(struct torrent *);
        
        static char *
        peerid()
        {
                int n;
       -        char hexa[40];
       +        char hex[40];
                unsigned char hash[20];
       -        static char peerid[21] = "-GT0000-XXXXXXXXXXXX";
       +        static char id[21] = "-GT0000-XXXXXXXXXXXX";
        
                srand(time(NULL)); /* good-enough seed */
                n = rand();
       -        snprintf(hexa, 40, "%08x%08x\n", n, ~n);
       -        sha1((unsigned char *)hexa, 16, hash);
       -        memcpy(peerid + 8, tohex(hash, hexa, 12), 12);
       +        snprintf(hex, 40, "%08x%08x\n", n, ~n);
       +        sha1((unsigned char *)hex, 16, hash);
       +        memcpy(id + 8, tohex(hash, hex, 12), 12);
        
       -        return peerid;
       +        return id;
       +}
       +
       +/* Calculate the SHA1 hash of the "info" key */
       +static unsigned char *
       +infohash(struct torrent *t)
       +{
       +        char *sp;
       +        struct be be;
       +        static unsigned char hash[20];
       +
       +        if (bekv(&t->be, "info", 4, &be) < 0)
       +                return NULL;
       +        sp = be.off;
       +        if (benext(&be) < 0)
       +                return NULL;
       +        sha1((unsigned char *)sp, be.off - sp, hash);
       +
       +        return hash;
        }
        
        /* Read torrent file into a memory buffer */
       t@@ -75,21 +94,6 @@ loadtracker(struct torrent *t)
                return 0;
        }
        
       -/* Calculate the SHA1 hash of the "info" key */
       -static int
       -loadinfohash(struct torrent *t)
       -{
       -        char *sp;
       -        struct be be;
       -
       -        if (bekv(&t->be, "info", 4, &be) < 0)
       -                return -1;
       -        sp = be.off;
       -        if (benext(&be) < 0)
       -                return -1;
       -        return sha1((unsigned char *)sp, be.off - sp, t->infohash);
       -}
       -
        /* Generate a random peer ID */
        static int
        loadpeerid(struct torrent *t)
       t@@ -110,11 +114,12 @@ glch_loadinfo(struct torrent *t, char *b, size_t s)
                if (beinit(&t->be, b, s) < 0)
                        return -1;
        
       -        if (loadinfohash(t) < 0)
       -                return -1;
       -
                if (loadpeerid(t) < 0)
                        return -1;
       +
       +        t->ul = 0;
       +        t->dl = 0;
       +
                return 0;
        }
        
       t@@ -127,11 +132,11 @@ glch_loadtorrent(struct torrent *t, char *f)
                if (loadtracker(t) < 0)
                        return -1;
        
       -        if (loadinfohash(t) < 0)
       -                return -1;
       -
                if (loadpeerid(t) < 0)
                        return -1;
        
       +        t->ul = 0;
       +        t->dl = 0;
       +
                return 0;
        }
 (DIR) diff --git a/libeech.h b/libeech.h
       t@@ -11,7 +11,8 @@ struct torrent {
                char id[21];
                char tr[PATH_MAX];
                struct be be;
       -        unsigned char infohash[20];
       +        long dl;
       +        long ul;
        };
        
        int glch_loadinfo(struct torrent *, char *, size_t);
 (DIR) diff --git a/torrent.c b/torrent.c
       t@@ -18,7 +18,6 @@ main(int argc, char *argv[])
                if (!glch_loadtorrent(&t, argv[1])) {
                        printf("Peer ID: %s\n", t.id);
                        printf("Tracker: %s\n", t.tr);
       -                printf("Infokey: %s\n", tohex(t.infohash, hex, 20));
                }
                        
                return 0;