summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c47
1 files changed, 39 insertions, 8 deletions
diff --git a/src/main.c b/src/main.c
index 7527921..4368e21 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,19 +31,33 @@ void handler (int s) {
break;
}
}
+time_t last_added = 0;
void found_torrent (struct dht * d __attribute__((unused)), const unsigned char * h, struct torrent * t) {
char buf[128];
bin2hex(buf, h, 20);
buf[40] = '\0';
- L(debug, d, "magnet:?xt=urn:btih:%s%s", buf, t ? " stored" : "");
+ struct stat statbuf;
+ strcat(buf, ".torrent");
+ if (!stat(buf, &statbuf)) {
+ L(expected, d, "%s already exists", buf);
+ return;
+ }
+ L(debug, d, "%s%s", buf, t ? " stored" : " new");
if (t) {
- struct stat statbuf;
- strcat(buf, ".torrent");
- if (!stat(buf, &statbuf)) {
- L(expected, d, "%s already exists", buf);
+ if (!t->type)
+ t->ttl = seconds()+512;
+ t->type |= info | peers;
+ } else {
+ if (last_added + 10 > seconds()) {
+ L(debug, d, "not adding a torrent this fast");
return;
- } else
- t->type |= info | peers;
+ }
+ last_added = seconds();
+ t = torrent_init();
+ memcpy(t->hash, h, 20);
+ t->type |= info | peers;
+ t->ttl = seconds()+128;
+ add_torrent(d, t);
}
}
int main (int argc, char ** argv) {
@@ -114,6 +128,8 @@ int main (int argc, char ** argv) {
dht->pollfds_size = &pollfds_size;
dht->nfds = &nfds;
dht->verbosity |= (getenv("TRAVNIK_INCOMING_DHT") ? incoming_dht : 0) | (getenv("TRAVNIK_OUTGOING_DHT") ? outgoing_dht : 0) | expected | debug;
+ dht->torrents_max = K;
+ dht->peers_per_torrent_max = K;
struct torrent * torrent = torrent_init();
memcpy(torrent->hash, "\xdd\x82\x55\xec\xdc\x7c\xa5\x5f\xb0\xbb\xf8\x13\x23\xd8\x70\x62\xdb\x1f\x6d\x1c", 20);
torrent->type = /* (useless, since we have no listening system yet) announce | */ peers | info;
@@ -121,8 +137,22 @@ int main (int argc, char ** argv) {
periodic(dht);
// alarm(PERIODIC);
w:
- while (poll(pollfds, nfds, -1) != -1) // can't timeout
+ while (poll(pollfds, nfds, -1) != -1) { // can't timeout
+ if (sigusr1) {
+ sigusr1 = 0;
+ dht_print(stdout, dht);
+ goto w;
+ }
+ if (periodično) {
+ periodično = 0;
+ // alarm(PERIODIC);
+ periodic(dht);
+ goto w;
+ }
+ if (samomor)
+ goto s;
work(dht);
+ }
switch (errno) {
case EINTR:
if (sigusr1) {
@@ -146,6 +176,7 @@ w:
r = 115;
goto r;
}
+ s:
config = persistent(dht);
dht_free(dht);
dht = NULL;