torrentd

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

commit 471d82a235cae10cf01e93fe0e919b8a90945c90
parent 57520ec2e6773913e8515da2b10e134904c54f8a
Author: sin <sin@2f30.org>
Date:   Mon, 21 Dec 2015 16:50:46 +0000

Commit shit

Diffstat:
Mstorrent.c | 2++
Mstorrent.h | 7++++++-
Mtorrent.c | 7+++++++
Mtracker.c | 42++++++++++++++++++++++++++++++++++++++++++
Mutil.c | 28++++++++++++++++++++++++----
5 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/storrent.c b/storrent.c @@ -15,6 +15,7 @@ usage(char *argv0) int main(int argc, char *argv[]) { + struct ben *peers; struct torrent *t; if (argc != 2) @@ -22,6 +23,7 @@ main(int argc, char *argv[]) if (!(t = loadtorrent(argv[1]))) exit(1); dumptorrent(t); + tracker_get_peers(t, &peers); unloadtorrent(t); exit(0); } diff --git a/storrent.h b/storrent.h @@ -54,6 +54,10 @@ void dumptorrent(struct torrent *); struct torrent *loadtorrent(char *); void unloadtorrent(struct torrent *); int piecehash(struct torrent *, long long, uint8_t *); +char *peerid(void); + +/* tracker.c */ +int tracker_get_peers(struct torrent *, struct ben **); /* util.c */ void *emalloc(size_t); @@ -70,7 +74,8 @@ void sha1sum(uint8_t *, unsigned long, uint8_t *); int readfile(char *, char **, size_t *); char *urlencode(char *, size_t); char *urldecode(char *); -void writeall(int, const void *, size_t); +ssize_t writeall(int, const void *, size_t); +ssize_t readall(int, void *, size_t); #undef strlcpy size_t strlcpy(char *, const char *, size_t); #undef strlcat diff --git a/torrent.c b/torrent.c @@ -250,3 +250,10 @@ piecehash(struct torrent *t, long long n, uint8_t *md) memcpy(md, t->pieces->start + n * 20, 20); return 0; } + +char * +peerid(void) +{ + static char id[] = "-DP4242-000000000000"; + return id; +} diff --git a/tracker.c b/tracker.c @@ -1 +1,43 @@ /* See LICENSE file for copyright and license details. */ +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include "storrent.h" + +int +tracker_get_peers(struct torrent *t, struct ben **peers) +{ + char *url, *host, *port, *path; + char *infohash, *id; + char buf[8192]; + int r, s; + + *peers = NULL; + if ((url = parseurl(t->announcers[0].urls[0], &host, &port, &path)) < 0) + return -1; + + infohash = urlencode((char *)t->infohash, 20); + id = urlencode(peerid(), 20); + r = snprintf(buf, sizeof(buf), + "GET /%s?info_hash=%s&peer_id=%s&port=6881&uploaded=0&" + "downloaded=0&left=%zu&event=started\r\n\r\n", + path, infohash, id, t->files[0].len); + if (r < 0 || (size_t)r >= sizeof(buf)) + goto err0; + + if ((s = dial(host, port)) < 0) + goto err0; + + writeall(s, buf, r); + r = readall(s, buf, sizeof(buf) - 1); + buf[r] = '\0'; + + return 0; +err0: + free(url); + free(infohash); + free(id); + return -1; +} diff --git a/util.c b/util.c @@ -179,22 +179,42 @@ urldecode(char *s) return buf; } -void +ssize_t writeall(int fd, const void *buf, size_t count) { const char *p = buf; ssize_t n; - size_t wrote = 0; + size_t total = 0; while (count > 0) { - n = write(fd, p + wrote, count); + n = write(fd, p + total, count); if (n < 0) err(1, "write"); else if (n == 0) break; - wrote += n; + total += n; + count -= n; + } + return total; +} + +ssize_t +readall(int fd, void *buf, size_t count) +{ + char *p = buf; + ssize_t n; + size_t total = 0; + + while (count > 0) { + n = read(fd, p + total, count); + if (n < 0) + err(1, "read"); + else if (n == 0) + break; + total += n; count -= n; } + return total; } size_t