summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c82
1 files changed, 51 insertions, 31 deletions
diff --git a/src/main.c b/src/main.c
index 70b4d3e..d6c9fb3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -9,7 +9,11 @@
#include <poll.h>
#include <sys/time.h>
#define S0(x) (x ? x : "")
+#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;
+#include <tcp.c>
int samomor = 0;
int periodično = 0;
int sigusr1 = 0;
@@ -31,9 +35,12 @@ void found_torrent (struct dht * d __attribute__((unused)), const unsigned char
char buf[41];
bin2hex(buf, h, 20);
buf[40] = '\0';
- L(stdout, "magnet:?xt=urn:btih:%s", buf);
+ L(user, d, "magnet:?xt=urn:btih:%s", buf);
}
int main (int argc, char ** argv) {
+ size_t pollfds_size = 1;
+ nfds_t nfds = 1;
+ struct pollfd * pollfds = NULL;
int r = 0;
struct dht * dht = NULL;
struct sigaction sigact = {
@@ -55,8 +62,10 @@ int main (int argc, char ** argv) {
error_at_line(6, errno, __FILE__, __LINE__, "sigaddset(SIGUSR1)");
if (sigaddset(&sigset, SIGALRM) == -1)
error_at_line(7, errno, __FILE__, __LINE__, "sigaddset(SIGALRM)");
+ if (sigaddset(&sigset, SIGTERM) == -1)
+ error_at_line(8, errno, __FILE__, __LINE__, "sigaddset(SIGTERM)");
if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) == -1)
- error_at_line(8, errno, __FILE__, __LINE__, "sigprocmask");
+ error_at_line(9, errno, __FILE__, __LINE__, "sigprocmask");
/* struct itimerval itimerval = {
.it_interval = {
.tv_sec = 60
@@ -65,7 +74,7 @@ int main (int argc, char ** argv) {
if (setitimer(ITIMER_REAL, &itimerval, NULL) == -1)
error_at_line(9, errno, __FILE__, __LINE__, "setitimer"); */
if (argc != 1+1)
- error_at_line(10, 0, __FILE__, __LINE__, "%s configfile.ben >> possible_torrents.L", S0(argv[0]));
+ 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);
if (cf == -1)
error_at_line(11, errno, __FILE__, __LINE__, "open(%s)", argv[1]);
@@ -83,39 +92,48 @@ int main (int argc, char ** argv) {
}
struct bencoding * config = bdecode(cfr, statbuf.st_size, replace);
dht = dht_init(config);
- dht->possible_torrent = found_torrent;
free_bencoding(config);
+ dht->possible_torrent = found_torrent;
+ dht->connection = connection;
+ pollfds = malloc(sizeof *pollfds);
+ pollfds[0].fd = dht->socket;
+ pollfds[0].events = POLLIN;
+ dht->pollfds = &pollfds;
+ 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;
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 = announce | peers;
+ torrent->type = /* (useless, since we have no listening system yet) announce | */ peers | info;
add_torrent(dht, torrent);
- struct pollfd pollfd = {
- .fd = dht->socket,
- .events = POLLIN
- };
+ periodic(dht);
+ alarm(PERIODIC);
w:
- alarm(13*60);
- while (poll(&pollfd, 1, -1) == 1)
+ while (poll(pollfds, nfds, -1) != -1) // can't timeout
work(dht);
- if (errno == EINTR) {
- if (sigusr1) {
- sigusr1 = 0;
- dht_print(stdout, dht);
- goto w;
- }
- if (periodično) {
- periodično = 0;
- work(dht);
- goto w;
- }
- if (!samomor) {
- error_at_line(0, errno, __FILE__, __LINE__, "poll");
- r = 114;
- }
- } else {
- error_at_line(0, errno, __FILE__, __LINE__, "poll");
- r = 115;
- goto r;
+ switch (errno) {
+ case EINTR:
+ if (sigusr1) {
+ sigusr1 = 0;
+ dht_print(stdout, dht);
+ goto w;
+ }
+ if (periodično) {
+ periodično = 0;
+ alarm(PERIODIC);
+ periodic(dht);
+ goto w;
+ }
+ if (!samomor) {
+ error_at_line(0, errno, __FILE__, __LINE__, "poll");
+ r = 114;
+ }
+ break;
+ default:
+ error_at_line(0, errno, __FILE__, __LINE__, "poll");
+ raise(SIGINT);
+ r = 115;
+ goto r;
}
config = persistent(dht);
dht_free(dht);
@@ -148,6 +166,8 @@ w:
error_at_line(0, errno, __FILE__, __LINE__, "munmap(cf, %ld)", statbuf.st_size);
if (close(cf) == -1)
error_at_line(0, errno, __FILE__, __LINE__, "close(cf)");
- L(stderr, "exiting cleanly with status %d\n", r);
+ if (pollfds)
+ free(pollfds);
+ fprintf(stderr, "exiting cleanly with status %d\n", r);
return r;
}