libeech

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

commit 34cb6c512c6609748925c64f4b8058ef3dc30e5d
parent f06d22e78655ee04d1f813f2edbaf5f365a07cc0
Author: z3bra <contactatz3bradotorg>
Date:   Wed Oct 18 08:31:26 +0200

Store infohash and fix up some bencoding functions

Diffstat:
be.c | 10+++++-----
libeech.c | 13++++++++++++-
libeech.h | 1+
3 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/be.c b/be.c @@ -122,7 +122,7 @@ belist(struct be *b, size_t *n) beinit(&i, b->off, b->end - b->off + 1); - while(belistover(&i)) { + while(!belistover(&i)) { belistnext(&i); c++; } @@ -177,7 +177,7 @@ belistnext(struct be *b) return 0; } - return benext(b) > 0; + return !(benext(b) > 0); } int @@ -197,7 +197,7 @@ bedictnext(struct be *b, char **k, size_t *l, struct be *v) if (benext(b) > 0 && v) beinit(v, b->off, b->end - b->off + 1); - return benext(b) > 0; + return !(benext(b) > 0); } ssize_t @@ -262,7 +262,7 @@ bekv(struct be *b, char *k, size_t l, struct be *v) beinit(&i, b->off, b->end - b->off + 1); /* search the data 'till the end */ - while (!belistover(&i) && bedictnext(&i, &key, &klen, v)) { + while (!belistover(&i) && !bedictnext(&i, &key, &klen, v)) { /* we found our key! */ if (!strncmp(k, key, MIN(l, klen))) return 0; @@ -288,7 +288,7 @@ bepath(struct be *b, char **p, size_t l) beinit(&i, b->off, b->end - b->off + 1); - while(belistnext(&i) && !belistover(&i)) { + while(!belistnext(&i) && !belistover(&i)) { if (!bestr(&i, &s, &r)) continue; strncat(*p, "/", l); diff --git a/libeech.c b/libeech.c @@ -39,7 +39,9 @@ glch_loadtorrent(struct torrent *t, char *path) { FILE *f; char *b, *sp; + struct be be; struct stat sb; + /* read torrent file into a memory buffer */ if (stat(path, &sb)) return -1; @@ -48,15 +50,24 @@ glch_loadtorrent(struct torrent *t, char *path) b = malloc(sb.st_size); fread(b, 1, sb.st_size, f); b[sb.st_size - 1] = '\0'; + beinit(&t->be, b, sb.st_size); fclose(f); /* retrieve "announce" key */ - beinit(&t->be, b, sb.st_size); sp = bekstr(&t->be, "announce", 8); if (!sp) return -1; memcpy(t->tr, sp, strlen(sp)); + /* store SHA1 of the info key */ + if (bekv(&t->be, "info", 4, &be) < 0) + return -1; + sp = be.off; + if (benext(&be) < 0) + return -1; + sha1((unsigned char *)sp, be.off - sp, t->infohash); + + /* generate a random peer ID */ sp = peerid(); if (!sp) return -1; diff --git a/libeech.h b/libeech.h @@ -11,6 +11,7 @@ struct torrent { char id[21]; char tr[PATH_MAX]; struct be be; + unsigned char infohash[20]; }; int glch_loadtorrent(struct torrent *, char *);