summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bencoding.c6
-rw-r--r--src/dht.c28
-rw-r--r--src/main.c21
3 files changed, 45 insertions, 10 deletions
diff --git a/src/bencoding.c b/src/bencoding.c
index 991ac9c..0fb2588 100644
--- a/src/bencoding.c
+++ b/src/bencoding.c
@@ -328,7 +328,7 @@ char * b2json (char * dest, struct bencoding * b) {
if (b->type & num) {
char buf[512];
sprintf(buf, "%ld", b->intvalue);
- strncpy(dest, buf, strlen(buf));
+ strcpy(dest, buf);
return dest+strlen(buf);
}
if (b->type & (list | dict)) {
@@ -708,13 +708,13 @@ char * bencode (char * dest, struct bencoding * b) {
dest = bencode(dest, b->key);
if (b->type & num) {
sprintf(buf, "i%ld", b->intvalue);
- strncpy(dest, buf, strlen(buf));
+ strcpy(dest, buf);
dest += strlen(buf);
*dest++ = 'e';
}
if (b->type & string) {
sprintf(buf, "%zu:", b->valuelen);
- strncpy(dest, buf, strlen(buf));
+ strcpy(dest, buf);
dest += strlen(buf);
memcpy(dest, b->value, b->valuelen);
dest += b->valuelen;
diff --git a/src/dht.c b/src/dht.c
index 63adcdc..513a66f 100644
--- a/src/dht.c
+++ b/src/dht.c
@@ -507,6 +507,8 @@ struct dht {
unsigned rxrp;
unsigned txrp;
unsigned removed_torrents;
+ unsigned toomuch;
+ struct sockaddr_in6 insert_peer;
};
/**
@@ -720,6 +722,7 @@ void ping_node (struct dht * d, const struct sockaddr_in6 * a) {
struct dht * dht_init (const struct bencoding * c) {
struct dht * d = calloc(1, sizeof *d);
+ d->toomuch = 32727*1;
d->time = seconds();
d->log = stderr;
d->buckets = bucket_init();
@@ -755,7 +758,7 @@ struct dht * dht_init (const struct bencoding * c) {
#pragma GCC diagnostic pop
goto e;
}
-#define TOOMUCH (32727*1)
+#define TOOMUCH d->toomuch
unsigned pinged = 0;
if (c) {
const struct bencoding * id = bpath(c, "id");
@@ -1882,9 +1885,11 @@ void handle (struct dht * d, char * pkt, int len, struct sockaddr_in6 addr) {
d->possible_torrent(d, hash->value, torrent);
#pragma GCC diagnostic pop
unsigned i = 8;
+ struct bencoding * values = NULL;
if (torrent) {
struct peer * peer = torrent->peers;
- struct bencoding * values = calloc(1, sizeof *values);
+ values = calloc(1, sizeof *values);
+ values->key = bstr(strdup("values"));
values->type = list;
while (i-- && peer) { // TODO implement peer preference: prefer sending peers that responded to us
if (family(peer->addr.sin6_addr.s6_addr) != family(addr.sin6_addr.s6_addr)) // possible
@@ -1892,14 +1897,29 @@ void handle (struct dht * d, char * pkt, int len, struct sockaddr_in6 addr) {
if (peer->flags & (unreachable || protocolerror))
goto c;
struct bencoding * value = calloc(1, sizeof *value);
- memcpy((value->value = malloc((value->valuelen = ADDRLEN(family(peer->addr.sin6_addr.s6_addr))+2))), peer->addr.sin6_addr.s6_addr, ADDRLEN(family(peer->addr.sin6_addr.s6_addr)));
+ value->type |= string;
+ memcpy((value->value = malloc((value->valuelen = ADDRLEN(family(peer->addr.sin6_addr.s6_addr))+2))), peer->addr.sin6_addr.s6_addr+(family(peer->addr.sin6_addr.s6_addr) == AF_INET ? 12 : 0), ADDRLEN(family(peer->addr.sin6_addr.s6_addr)));
memcpy(value->value+ADDRLEN(family(peer->addr.sin6_addr.s6_addr)), &peer->addr.sin6_port, 2);
binsert(values, value);
c:
peer = peer->next;
}
- binsert(r, values);
}
+ if (d->insert_peer.sin6_family == AF_INET6 && family(d->insert_peer.sin6_addr.s6_addr) == family(addr.sin6_addr.s6_addr)) {
+ if (!values) {
+ values = calloc(1, sizeof *values);
+ values->key = bstr(strdup("values"));
+ values->type = list;
+ }
+ struct bencoding * value = calloc(1, sizeof *value);
+ value->type |= string;
+ memcpy((value->value = malloc((value->valuelen = ADDRLEN(family(d->insert_peer.sin6_addr.s6_addr))+2))), d->insert_peer.sin6_addr.s6_addr+(family(d->insert_peer.sin6_addr.s6_addr) == AF_INET ? 12 : 0), ADDRLEN(family(d->insert_peer.sin6_addr.s6_addr)));
+ memcpy(value->value+ADDRLEN(family(d->insert_peer.sin6_addr.s6_addr)), &d->insert_peer.sin6_port, 2);
+ raise(SIGINT);
+ binsert(values, value);
+ }
+ if (values)
+ binsert(r, values);
struct bencoding * tok = calloc(1, sizeof *tok);
tok->type = string;
tok->key = bstr(strdup("token"));
diff --git a/src/main.c b/src/main.c
index 199bfab..0949e4b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -9,6 +9,7 @@
#include <poll.h>
#include <sys/time.h>
#define S0(x) (x ? x : "")
+#define So(x, o) (x ? x : o)
#define TORRENT_USERDATA struct dht * dht;
#define DHT_USERDATA struct pollfd ** pollfds; size_t * pollfds_size; nfds_t * nfds;
#include <dht.c>
@@ -45,10 +46,10 @@ void found_torrent (struct dht * d __attribute__((unused)), const unsigned char
L(debug, d, "%s%s", buf, t ? " stored" : " new");
if (t) {
if (!t->type)
- t->ttl = seconds()+256;
+ t->ttl = seconds()+atoi(So(getenv("TTL"), "256"));
t->type |= info | peers;
} else {
- if (last_added + 2 > seconds()) {
+ if (last_added + atoi(So(getenv("COOLDOWN"), "2")) > seconds()) {
L(debug, d, "not adding a torrent this fast");
return;
}
@@ -56,7 +57,7 @@ void found_torrent (struct dht * d __attribute__((unused)), const unsigned char
t = torrent_init();
memcpy(t->hash, h, 20);
t->type |= info | peers;
- t->ttl = seconds()+256;
+ t->ttl = seconds()+atoi(So(getenv("TTL"), "256"));
add_torrent(d, t);
}
}
@@ -121,6 +122,20 @@ int main (int argc, char ** argv) {
dht = dht_init(config);
free_bencoding(config);
config = NULL;
+ if (getenv("INSERT_PEER")) {
+ dht->insert_peer.sin6_family = AF_INET6;
+ dht->insert_peer.sin6_port = htons(atoi(So(getenv("INSERT_PEER_PORT"), "1337")));
+ if (inet_pton(AF_INET6, getenv("INSERT_PEER"), dht->insert_peer.sin6_addr.s6_addr) != 1) {
+ error_at_line(0, 0, __FILE__, __LINE__, "inet_pton(AF_INET6, getenv(INSERT_PEER)) != 1");
+ r = 14;
+ goto r;
+ }
+ error_at_line(0, 0, __FILE__, __LINE__, "feature INSERT_PEER not implemented completely -- bugs: travnik somehow starts sending packets to inserted peer ... perhaps he sends get_peers to himself?");
+ r = 15;
+ goto r;
+ }
+ if (getenv("TOOMUCH"))
+ dht->toomuch = atoi(getenv("TOOMUCH"));
dht->possible_torrent = found_torrent;
dht->connection = connection;
pollfds = malloc(sizeof *pollfds);