torrentd

simple torrent daemon
git clone git://git.2f30.org/torrentd
Log | Files | Refs | LICENSE

commit e8afeb7ad7cb399b2bf71806c009d5e0733a1137
parent c3af4b0ae222708c4ac23c5955910dc10e4c2991
Author: sin <sin@2f30.org>
Date:   Sun, 27 Dec 2015 17:55:21 +0000

move around shit

Diffstat:
Mstorrentd.c | 124+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 65 insertions(+), 59 deletions(-)

diff --git a/storrentd.c b/storrentd.c @@ -87,6 +87,7 @@ struct torrent { size_t downloaded; }; +/* util functions */ void *emalloc(size_t); void *ecalloc(size_t, size_t); uint32_t *newbit(int); @@ -112,6 +113,7 @@ int unpack(uint8_t *, size_t, char *, ...); int pack(uint8_t *, size_t, char *, ...); int dial(char *, char *); +/* ben parsing functions */ char *parse(char *, char *, struct ben **); char *bdecode(char *, char *, struct ben **); char *bstr2str(struct ben *); @@ -120,16 +122,20 @@ struct ben *dlook(struct ben *, struct ben *); struct ben *dlookstr(struct ben *, char *); void bfree(struct ben *); void bprint(struct ben *, int); -int parsepeers(struct torrent *, struct buf *); +/* torrent file handling */ void dumptorrent(struct torrent *); struct torrent *loadtorrent(char *); void unloadtorrent(struct torrent *); int piecehash(struct torrent *, long long, uint8_t *); char *peerid(void); +/* tracker related functionality */ int trackerget(struct torrent *, int); +/* peer handling */ +int parsepeers(struct torrent *, struct buf *); + jmp_buf savesp; void * @@ -805,64 +811,6 @@ bprint(struct ben *b, int indent) } } -int -parsepeers(struct torrent *t, struct buf *b) -{ - struct sockaddr_in sa; - struct ben *reply, *peers; - struct peer *peer; - char *p, *errstr; - - if (!bdecode(b->p, b->p + b->n, &reply)) - return -1; - - if (dlookstr(reply, "failure reason")) { - errstr = bstr2str(dlookstr(reply, "failure reason")); - warnx("tracker failure: %s", errstr); - bfree(reply); - free(errstr); - return -1; - } - - if (!(peers = dlookstr(reply, "peers"))) { - warnx("no peers field in tracker reply"); - bfree(reply); - return -1; - } - - if (peers->type != 's') { - warnx("expected compact reply"); - bfree(reply); - return -1; - } - - if (peers->len % 6) { - warnx("peers length needs to be a multiple of 6 bytes"); - bfree(reply); - return -1; - } - - for (p = peers->s; p < &peers->s[peers->len]; p += 6) { - t->peers = realloc(t->peers, (t->npeers + 1) * sizeof(*t->peers)); - if (!t->peers) - err(1, "realloc"); - peer = &t->peers[t->npeers]; - memset(peer, 0, sizeof(*peer)); - memcpy(&sa.sin_addr, p, 4); - inet_ntop(AF_INET, &sa.sin_addr, peer->hostname, - sizeof(peer->hostname)); - snprintf(peer->port, sizeof(peer->port), "%d", - (int)ntohs(*(short *)&p[4])); - printf("%s:%s\n", peer->hostname, peer->port); - peer->amchoking = 1; - peer->peerchoking = 1; - t->npeers++; - } - - free(reply); - return 0; -} - void calcinfohash(struct torrent *t) { @@ -1192,6 +1140,64 @@ err0: return r; } +int +parsepeers(struct torrent *t, struct buf *b) +{ + struct sockaddr_in sa; + struct ben *reply, *peers; + struct peer *peer; + char *p, *errstr; + + if (!bdecode(b->p, b->p + b->n, &reply)) + return -1; + + if (dlookstr(reply, "failure reason")) { + errstr = bstr2str(dlookstr(reply, "failure reason")); + warnx("tracker failure: %s", errstr); + bfree(reply); + free(errstr); + return -1; + } + + if (!(peers = dlookstr(reply, "peers"))) { + warnx("no peers field in tracker reply"); + bfree(reply); + return -1; + } + + if (peers->type != 's') { + warnx("expected compact reply"); + bfree(reply); + return -1; + } + + if (peers->len % 6) { + warnx("peers length needs to be a multiple of 6 bytes"); + bfree(reply); + return -1; + } + + for (p = peers->s; p < &peers->s[peers->len]; p += 6) { + t->peers = realloc(t->peers, (t->npeers + 1) * sizeof(*t->peers)); + if (!t->peers) + err(1, "realloc"); + peer = &t->peers[t->npeers]; + memset(peer, 0, sizeof(*peer)); + memcpy(&sa.sin_addr, p, 4); + inet_ntop(AF_INET, &sa.sin_addr, peer->hostname, + sizeof(peer->hostname)); + snprintf(peer->port, sizeof(peer->port), "%d", + (int)ntohs(*(short *)&p[4])); + printf("%s:%s\n", peer->hostname, peer->port); + peer->amchoking = 1; + peer->peerchoking = 1; + t->npeers++; + } + + free(reply); + return 0; +} + void usage(char *argv0) {