libeech

BitTorrent library
git clone git://z3bra.org/libeech.git
Log | Files | Refs | README | LICENSE

commit 1f684681b6e996a18378c7567a1fa0b1fe903a45
parent 06340e51868101e64aa9275708e54b89b3c498f5
Author: z3bra <contactatz3bradotorg>
Date:   Wed Oct 18 23:56:38 +0200

Helper to retrieve infohash from metadata

Diffstat:
libeech.c | 63++++++++++++++++++++++++++++++++++-----------------------------
libeech.h | 3++-
torrent.c | 1-
3 files changed, 36 insertions(+), 31 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -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 */ @@ -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) @@ -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; } @@ -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; } diff --git a/libeech.h b/libeech.h @@ -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); diff --git a/torrent.c b/torrent.c @@ -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;