summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-01-01 22:28:35 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-01-01 22:28:35 +0100
commitad9c2c45fa85f877fa0880269369e6dc9492a87d (patch)
treee20d53a11f060cad20c8423f53921054358c59e4 /src/main.c
parentfixed bdecoding strncpy->memcpy +etc (diff)
downloadtravnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.tar
travnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.tar.gz
travnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.tar.bz2
travnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.tar.lz
travnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.tar.xz
travnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.tar.zst
travnik-ad9c2c45fa85f877fa0880269369e6dc9492a87d.zip
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c62
1 files changed, 45 insertions, 17 deletions
diff --git a/src/main.c b/src/main.c
index c14af38..f613e4e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,6 +12,7 @@
#include <dht.c>
int samomor = 0;
int periodično = 0;
+int sigusr1 = 0;
void handler (int s) {
switch (s) {
case SIGINT:
@@ -21,8 +22,17 @@ void handler (int s) {
case SIGALRM:
periodično++;
break;
+ case SIGUSR1:
+ sigusr1++;
+ break;
}
}
+void found_torrent (struct dht * d __attribute__((unused)), const unsigned char * h) {
+ char buf[41];
+ bin2hex(buf, h, 20);
+ buf[40] = '\0';
+ L(stdout, "magnet:?xt=urn:btih:%s", buf);
+}
int main (int argc, char ** argv) {
int r = 0;
struct dht * dht = NULL;
@@ -36,25 +46,44 @@ int main (int argc, char ** argv) {
error_at_line(2, errno, __FILE__, __LINE__, "sigaction(SIGINT)");
if (sigaction(SIGTERM, &sigact, NULL) == -1)
error_at_line(3, errno, __FILE__, __LINE__, "sigaction(SIGTERM)");
+ if (sigaction(SIGUSR1, &sigact, NULL) == -1)
+ error_at_line(4, errno, __FILE__, __LINE__, "sigaction(SIGUSR1)");
+ struct itimerval itimerval = {
+ .it_interval = {
+ .tv_sec = 13*60
+ }
+ };
+ if (setitimer(ITIMER_REAL, &itimerval, NULL))
+ error_at_line(5, errno, __FILE__, __LINE__, "setitimer");
+ sigset_t sigset;
+ if (sigemptyset(&sigset) == -1)
+ error_at_line(6, errno, __FILE__, __LINE__, "sigemptyset");
+ if (sigaddset(&sigset, SIGUSR1) == -1)
+ error_at_line(7, errno, __FILE__, __LINE__, "sigaddset(SIGUSR1)");
+ if (sigaddset(&sigset, SIGALRM) == -1)
+ error_at_line(8, errno, __FILE__, __LINE__, "sigaddset(SIGALRM)");
+ if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) == -1)
+ error_at_line(9, errno, __FILE__, __LINE__, "sigprocmask");
if (argc != 1+1)
- error_at_line(4, 0, __FILE__, __LINE__, "%s configfile.ben", S0(argv[0]));
+ error_at_line(10, 0, __FILE__, __LINE__, "%s configfile.ben > possible_torrents.L", S0(argv[0]));
int cf = open(argv[1], O_RDWR | O_CLOEXEC | O_CREAT, 00664);
if (cf == -1)
- error_at_line(5, errno, __FILE__, __LINE__, "open(%s)", argv[1]);
+ error_at_line(11, errno, __FILE__, __LINE__, "open(%s)", argv[1]);
struct stat statbuf;
if (fstat(cf, &statbuf) == -1) {
error_at_line(0, errno, __FILE__, __LINE__, "fstat(cf, &statbuf)");
- r = 6;
+ r = 12;
goto r;
}
char * cfr = NULL;
if (statbuf.st_size && !(cfr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, cf, 0))) {
error_at_line(0, errno, __FILE__, __LINE__, "mmap(NULL, %ld, PROT_READ, MAP_SHARED, cf, 0)", statbuf.st_size);
- r = 7;
+ r = 13;
goto r;
}
struct bencoding * config = bdecode(cfr, statbuf.st_size, replace);
dht = dht_init(config);
+ dht->possible_torrent = found_torrent;
free_bencoding(config);
struct torrent * torrent = calloc(1, sizeof *torrent);
memcpy(torrent->hash, "\xdd\x82\x55\xec\xdc\x7c\xa5\x5f\xb0\xbb\xf8\x13\x23\xd8\x70\x62\xdb\x1f\x6d\x1c", 20);
@@ -65,17 +94,15 @@ int main (int argc, char ** argv) {
.fd = dht->socket,
.events = POLLIN
};
- struct itimerval itimerval = {
- .it_interval = {
- .tv_sec = 13*60
- }
- };
- setitimer(ITIMER_REAL, &itimerval, NULL);
w:
- while (poll(&pollfd, 1, -1) == 1) {
+ while (poll(&pollfd, 1, -1) == 1)
work(dht);
- }
if (errno == EINTR) {
+ if (sigusr1) {
+ sigusr1 = 0;
+ dht_print(stdout, dht);
+ goto w;
+ }
if (periodično) {
periodično = 0;
work(dht);
@@ -83,11 +110,11 @@ w:
}
if (!samomor) {
error_at_line(0, errno, __FILE__, __LINE__, "poll");
- r = 108;
+ r = 114;
}
} else {
error_at_line(0, errno, __FILE__, __LINE__, "poll");
- r = 109;
+ r = 115;
goto r;
}
config = persistent(dht);
@@ -95,18 +122,18 @@ w:
dht = NULL;
if (cfr && munmap(cfr, statbuf.st_size) == -1) {
error_at_line(0, errno, __FILE__, __LINE__, "munmap(cf, %ld)", statbuf.st_size);
- r = 110;
+ r = 116;
goto r;
}
cfr = NULL;
if (ftruncate(cf, (statbuf.st_size = bencode_length(config))) == -1) {
error_at_line(0, errno, __FILE__, __LINE__, "ftruncate(cf, %ld)", statbuf.st_size);
- r = 111;
+ r = 117;
goto r;
}
if (!(cfr = mmap(NULL, statbuf.st_size, PROT_WRITE, MAP_SHARED, cf, 0))) {
error_at_line(0, errno, __FILE__, __LINE__, "mmap(NULL, %ld, PROT_READ, MAP_SHARED, cf, 0)", statbuf.st_size);
- r = 112;
+ r = 118;
goto r;
}
bencode(cfr, config);
@@ -121,5 +148,6 @@ 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);
return r;
}