commit 9302f6a12bb0a4fd3101b59618cb7d0a85ddfc39
parent 5db2f939954b07ada60e3c9723db633248a2389b
Author: sin <sin@2f30.org>
Date: Sun, 27 Dec 2015 18:10:20 +0000
cleanup peer parsing
we will implement dictionary parsing in the future
Diffstat:
M | torrentd.c | | | 47 | ++++++++++++++++++++++++++--------------------- |
1 file changed, 26 insertions(+), 21 deletions(-)
diff --git a/torrentd.c b/torrentd.c
@@ -1171,33 +1171,38 @@ parsepeers(struct torrent *t, struct buf *b)
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++;
+ if (peers->type == 's') {
+ 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]));
+ peer->amchoking = 1;
+ peer->peerchoking = 1;
+ printf("%s:%s\n", peer->hostname, peer->port);
+ t->npeers++;
+ }
+ } else if (peers->type == 'd') {
+ warnx("not implemented");
+ bfree(reply);
+ return -1;
+ } else {
+ warnx("unexpected peers type: %d", peers->type);
+ bfree(reply);
+ return -1;
}
free(reply);