libeech

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

commit b30bde52988a54111ddb043a062bf5332d44e9b0
parent a191041cb05e1a0da2f597436fd393d355c4f9c9
Author: z3bra <contactatz3bradotorg>
Date:   Thu Oct 19 14:29:23 +0200

Remove ability to read files

This should be implemented by the client

Diffstat:
libeech.c | 51+++------------------------------------------------
libeech.h | 19+++++++++++++++++--
torrent.c | 41+++++++++++++++++++++++++++++++++++++++--
3 files changed, 59 insertions(+), 52 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -6,10 +6,6 @@ #include <string.h> #include <time.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> - #include "be.h" #include "sha1.h" #include "util.h" @@ -21,7 +17,6 @@ static char * infohash(struct torrent *); static long torrentsize(struct torrent *); /* Initialization of the torrent struct */ -static int loadfile(struct torrent *, char *); static int loadtracker(struct torrent *); static int loadpeerid(struct torrent *); @@ -87,28 +82,6 @@ torrentsize(struct torrent *t) return sz; } -/* Read torrent file into a memory buffer */ -static int -loadfile(struct torrent *t, char *path) -{ - FILE *f; - char *b; - struct stat sb; - - if (stat(path, &sb)) - return -1; - if (!(f = fopen(path, "r"))) - return -1; - b = malloc(sb.st_size); - if (!b) - return -1; - fread(b, 1, sb.st_size, f); - b[sb.st_size - 1] = '\0'; - beinit(&t->be, b, sb.st_size); - fclose(f); - return 0; -} - /* Retrieve the "announce" key */ static int loadtracker(struct torrent *t) @@ -137,31 +110,13 @@ loadpeerid(struct torrent *t) } int -glch_loadinfo(struct torrent *t, char *b, size_t s) +glch_loadtorrent(struct torrent *t, char *b, size_t s) { if (beinit(&t->be, b, s) < 0) return -1; - if (loadpeerid(t) < 0) - return -1; - - t->ul = 0; - t->dl = 0; - - return 0; -} - -int -glch_loadtorrent(struct torrent *t, char *f) -{ - if (loadfile(t, f) < 0) - return -1; - - if (loadtracker(t) < 0) - return -1; - - if (loadpeerid(t) < 0) - return -1; + loadpeerid(t); + loadtracker(t); t->ul = 0; t->dl = 0; diff --git a/libeech.h b/libeech.h @@ -7,13 +7,28 @@ #define BLKSIZ 16384 #define MSGSIZ ((BLKSIZ) + 13) +enum { + CONNECTED = 1 << 0, + HANDSHAKESENT = 1 << 1, + HANDSHAKERCVD = 1 << 2, + AMCHOKING = 1 << 3, + AMINTERESTED = 1 << 4, + ISCHOKING = 1 << 5, + ISINTERESTED = 1 << 6, +}; + +struct peer { + int fd; + int state; +}; + struct torrent { char id[21]; char tr[PATH_MAX]; struct be be; + long sz; long dl; long ul; }; -int glch_loadinfo(struct torrent *, char *, size_t); -int glch_loadtorrent(struct torrent *, char *); +int glch_loadtorrent(struct torrent *, char *, size_t); diff --git a/torrent.c b/torrent.c @@ -2,11 +2,44 @@ #include <stdlib.h> #include <libeech.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + #include "util.h" +long +readfile(char *f, char **b) +{ + long r; + FILE *fh; + char *sp; + struct stat sb; + + if (stat(f, &sb) < 0) + return -1; + + fh = fopen(f, "r"); + if (!f) + return -1; + + sp = malloc(sb.st_size); + if (!sp) + return -1; + + r = fread(sp, 1, sb.st_size, fh); + fclose(fh); + + *b = sp; + + return sb.st_size; +} + int main(int argc, char *argv[]) { + long s; + char *b; char hex[41]; struct torrent t; @@ -15,10 +48,14 @@ main(int argc, char *argv[]) return -1; } - if (!glch_loadtorrent(&t, argv[1])) { + s = readfile(argv[1], &b); + if (s < 0) + return -1; + + if (!glch_loadtorrent(&t, b, s)) { printf("Peer ID: %s\n", t.id); printf("Tracker: %s\n", t.tr); } - + return 0; }