summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/main.c b/src/main.c
index d6c9fb3..7527921 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,7 +12,7 @@
#define TORRENT_USERDATA struct dht * dht;
#define DHT_USERDATA struct pollfd ** pollfds; size_t * pollfds_size; nfds_t * nfds;
#include <dht.c>
-#define DISCONNECTION_MIXIN_BOTTOM if (t->dl->flags & goodmeta) t->type &= ~info;
+#define DISCONNECTION_MIXIN_BOTTOM if (t->dl->flags & goodmeta) t->type &= ~(info | peers);
#include <tcp.c>
int samomor = 0;
int periodično = 0;
@@ -31,11 +31,20 @@ void handler (int s) {
break;
}
}
-void found_torrent (struct dht * d __attribute__((unused)), const unsigned char * h) {
- char buf[41];
+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(user, d, "magnet:?xt=urn:btih:%s", buf);
+ L(debug, d, "magnet:?xt=urn:btih:%s%s", buf, t ? " stored" : "");
+ if (t) {
+ struct stat statbuf;
+ strcat(buf, ".torrent");
+ if (!stat(buf, &statbuf)) {
+ L(expected, d, "%s already exists", buf);
+ return;
+ } else
+ t->type |= info | peers;
+ }
}
int main (int argc, char ** argv) {
size_t pollfds_size = 1;
@@ -66,13 +75,16 @@ int main (int argc, char ** argv) {
error_at_line(8, errno, __FILE__, __LINE__, "sigaddset(SIGTERM)");
if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) == -1)
error_at_line(9, errno, __FILE__, __LINE__, "sigprocmask");
- /* struct itimerval itimerval = {
+ struct itimerval itimerval = {
.it_interval = {
- .tv_sec = 60
+ .tv_sec = PERIODIC
+ },
+ .it_value = {
+ .tv_sec = PERIODIC
}
};
if (setitimer(ITIMER_REAL, &itimerval, NULL) == -1)
- error_at_line(9, errno, __FILE__, __LINE__, "setitimer"); */
+ error_at_line(9, errno, __FILE__, __LINE__, "setitimer");
if (argc != 1+1)
error_at_line(10, 0, __FILE__, __LINE__, "%s configfile.ben", S0(argv[0]));
int cf = open(argv[1], O_RDWR | O_CLOEXEC | O_CREAT, 00664);
@@ -107,7 +119,7 @@ int main (int argc, char ** argv) {
torrent->type = /* (useless, since we have no listening system yet) announce | */ peers | info;
add_torrent(dht, torrent);
periodic(dht);
- alarm(PERIODIC);
+ // alarm(PERIODIC);
w:
while (poll(pollfds, nfds, -1) != -1) // can't timeout
work(dht);
@@ -120,7 +132,7 @@ w:
}
if (periodično) {
periodično = 0;
- alarm(PERIODIC);
+ // alarm(PERIODIC);
periodic(dht);
goto w;
}
@@ -131,7 +143,6 @@ w:
break;
default:
error_at_line(0, errno, __FILE__, __LINE__, "poll");
- raise(SIGINT);
r = 115;
goto r;
}
@@ -168,6 +179,6 @@ w:
error_at_line(0, errno, __FILE__, __LINE__, "close(cf)");
if (pollfds)
free(pollfds);
- fprintf(stderr, "exiting cleanly with status %d\n", r);
+ fprintf(stderr, "exiting cleanly with status %d node_init_count=%u node_free_count=%u\n", r, node_init_count, node_free_count);
return r;
}