libeech

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

commit 5868460df17098197b391fac804998f67ef696cf
parent 30c885bf1a9870d5d0d983ad719511eaccd052d4
Author: z3bra <contactatz3bradotorg>
Date:   Sun Oct 22 10:21:00 +0200

New function to check torrent entegrity

Diffstat:
libeech.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
torrent.c | 9+++++----
2 files changed, 57 insertions(+), 4 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -14,6 +14,7 @@ /* helpers to retrieve attributes from metadata */ static char * peerid(); static long torrentsize(struct torrent *); +static int chktorrent(struct be *); /* Initialization of the torrent struct */ static int loadtracker(struct torrent *); @@ -88,12 +89,63 @@ loadpeerid(struct torrent *t) return 0; } +static int +chktorrent(struct be *b) +{ + struct be i; + struct be f; + + if (bekv(b, "announce", 8, NULL) < 0) { + printf("Missing key: %s\n", "announce"); + return -1; + } + if (bekv(b, "info", 4, &i) < 0) { + printf("Missing key: info\n"); + return -1; + } + if (bekv(&i, "name", 4, NULL) < 0) { + printf("Missing key: name\n"); + return -1; + } + if (bekv(&i, "pieces", 6, NULL) < 0) { + printf("Missing key: pieces\n"); + return -1; + } + if (bekv(&i, "piece length", 12, NULL) < 0) { + printf("piece Missing key: length\n"); + return -1; + } + + if (!bekv(&i, "files", 5, &f)) { + while(!belistnext(&f) && !belistover(&f)) { + if (bekv(&f, "path", 4, NULL) < 0) { + printf("piece Missing files/path\n"); + return -1; + } + if (bekv(&f, "length", 6, NULL) < 0) { + printf("piece Missing files/length\n"); + return -1; + } + } + } else { + if (bekv(&i, "length", 6, NULL) < 0) { + printf("Missing key: length\n"); + return -1; + } + } + + return 0; +} + int glch_loadtorrent(struct torrent *t, char *b, size_t s) { if (beinit(&t->be, b, s) < 0) return -1; + if (chktorrent(&t->be) < 0) + return -1; + if (bekv(&t->be, "info", 4, &t->info) < 0) return -1; diff --git a/torrent.c b/torrent.c @@ -52,10 +52,11 @@ main(int argc, char *argv[]) if (s < 0) return -1; - if (!glch_loadtorrent(&t, b, s)) { - printf("Peer ID: %s\n", t.id); - printf("Tracker: %s\n", t.tr); - } + if (glch_loadtorrent(&t, b, s) < 0) + return -1; + + printf("Peer ID: %s\n", t.id); + printf("Tracker: %s\n", t.tr); return 0; }