diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2022-12-11 21:21:20 +0100 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2022-12-11 21:21:20 +0100 |
commit | cb207e2c31359a6c6259c86d05e474addf2476f0 (patch) | |
tree | 92168c070415d94848b74e1393cf25d5becf9ad7 /src/main.c | |
parent | untested for compilation, dht.c -- TODO: DNS+bootstrap (diff) | |
download | travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.tar travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.tar.gz travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.tar.bz2 travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.tar.lz travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.tar.xz travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.tar.zst travnik-cb207e2c31359a6c6259c86d05e474addf2476f0.zip |
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 56 |
1 files changed, 54 insertions, 2 deletions
@@ -1,7 +1,59 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <error.h> +#include <fcntl.h> +#include <signal.h> +#define S0(x) (x ? x : "") #include <dht.c> -int main (void) { - struct dht * dht = dht_init(); +int main (int argc, char ** argv) { + int r = 0; + if (argc != 1+1) + error_at_line(1, 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(2, 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 = 3; + 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 = 4; + goto r; + } + struct bencoding * config = bdecode(cfr, statbuf.st_size, replace); + struct dht * dht = dht_init(config); + free_bencoding(config); + config = persistent(dht); + dht_free(dht); + if (cfr && munmap(cfr, statbuf.st_size) == -1) { + error_at_line(0, errno, __FILE__, __LINE__, "munmap(cf, %ld)", statbuf.st_size); + r = 105; + 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 = 106; + 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 = 107; + goto r; + } + bencode(cfr, config); + free_bencoding(config); + r: + if (cfr && munmap(cfr, statbuf.st_size) == -1) + 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)"); + return r; } |