tStore infohash and fix up some bencoding functions - libeech - bittorrent library
(HTM) git clone git://z3bra.org/libeech.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 34cb6c512c6609748925c64f4b8058ef3dc30e5d
(DIR) parent f06d22e78655ee04d1f813f2edbaf5f365a07cc0
(HTM) Author: z3bra <contactatz3bradotorg>
Date: Wed, 18 Oct 2017 08:31:26 +0200
Store infohash and fix up some bencoding functions
Diffstat:
M be.c | 10 +++++-----
M libeech.c | 13 ++++++++++++-
M libeech.h | 1 +
3 files changed, 18 insertions(+), 6 deletions(-)
---
(DIR) diff --git a/be.c b/be.c
t@@ -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++;
}
t@@ -177,7 +177,7 @@ belistnext(struct be *b)
return 0;
}
- return benext(b) > 0;
+ return !(benext(b) > 0);
}
int
t@@ -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
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;
t@@ -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);
(DIR) diff --git a/libeech.c b/libeech.c
t@@ -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;
t@@ -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;
(DIR) diff --git a/libeech.h b/libeech.h
t@@ -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 *);