diff --git a/src/main.c b/src/main.c index dc1c508..ad2a7cf 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "addrbook.h" #include "audio.h" @@ -57,15 +58,58 @@ void version(void) { "\nCopyright (c) Alexander \"Arav\" Andreev \n"); } +addrbook_t ab = {0}; +bool is_ab_changed = false; +audio_t aud = {0}; +crypt_key_t ok = {0}, rk = {0}; +crypt_session_t s = {0}; +net_t n = {0}; +struct options opts = {0}; + +bool is_cleanup_performed = false; + +void cleanup(void) { + if (!is_cleanup_performed) + is_cleanup_performed = true; + else + return; + + if (is_ab_changed) { + FILE *addrbook_file = fopen(opts.addressbook_path, "w+"); + if (addrbook_file != NULL) { + if (addrbook_store(&ab, addrbook_file) == -1) + fprintf(stderr, "Failed to store an addressbook: %s\n", strerror(errno)); + fclose(addrbook_file); + } + } + + options_destroy(&opts); + addrbook_destroy(&ab); + audio_destroy(&aud); + net_destroy(&n); + crypt_session_destroy(&s); + crypt_key_destroy(&ok); + crypt_key_destroy(&rk); + + // if (audio_terminate_soundsystem() == -1) + // return 1; +} + +void signal_handler(int signo) { + switch (signo) { + case SIGINT: + case SIGABRT: + case SIGTERM: + case SIGQUIT: + case SIGSEGV: + cleanup(); + exit(0); + return; + } +} + int main(int argc, const char **argv) { int result = 0; - struct options opts = {0}; - - addrbook_t ab = {0}; - bool is_ab_changed = false; - net_t n = {0}; - crypt_session_t s = {0}; - crypt_key_t ok = {0}, rk = {0}; switch (parse_argv(argc, argv, &opts)) { case -1: usage(); return 1; @@ -143,6 +187,17 @@ int main(int argc, const char **argv) { fclose(skey_file); } + if (signal(SIGINT, signal_handler) == SIG_ERR) + fprintf(stderr, "Cannot catch SIGINT.\n"); + if (signal(SIGABRT, signal_handler) == SIG_ERR) + fprintf(stderr, "Cannot catch SIGABRT.\n"); + if (signal(SIGTERM, signal_handler) == SIG_ERR) + fprintf(stderr, "Cannot catch SIGTERM.\n"); + if (signal(SIGQUIT, signal_handler) == SIG_ERR) + fprintf(stderr, "Cannot catch SIGQUIT.\n"); + if (signal(SIGSEGV, signal_handler) == SIG_ERR) + fprintf(stderr, "Cannot catch SIGSEGV.\n"); + (opts.mode == 'c') ? (result = net_client_init(&n, opts.addr, opts.port)) : (result = net_server_init(&n, opts.addr , opts.port)); @@ -154,14 +209,13 @@ int main(int argc, const char **argv) { : (result = server_handshake(&n, &s, &ok, &rk)); if (result != 0) goto cleanup; - + char rkph[CRYPT_PKEY_HEXLEN]; crypt_key_export_public(&rk, rkph); fprintf(stdout, "A connection with %s established!\n", rkph); - audio_t aud = {0}; unsigned char aud_buf[AUDIO_BUF_SIZE]; memset(aud_buf, 0, AUDIO_BUF_SIZE); @@ -185,25 +239,7 @@ int main(int argc, const char **argv) { cleanup: - if (is_ab_changed) { - addrbook_file = fopen(opts.addressbook_path, "w+"); - if (addrbook_file != NULL) { - if (addrbook_store(&ab, addrbook_file) == -1) - fprintf(stderr, "Failed to store an addressbook: %s\n", strerror(errno)); - fclose(addrbook_file); - } - } - - options_destroy(&opts); - addrbook_destroy(&ab); - audio_destroy(&aud); - net_destroy(&n); - crypt_session_destroy(&s); - crypt_key_destroy(&ok); - crypt_key_destroy(&rk); - - // if (audio_terminate_soundsystem() == -1) - // return 1; + cleanup(); return result; } @@ -360,8 +396,6 @@ int parse_argv(int argc, const char **argv, struct options *opts) { strncpy(opts->key_sec_path, config_dir, config_dir_len); strcat(opts->key_sec_path, "/own_sec_key"); - printf("%s:%s:%s\n", opts->addressbook_path, opts->key_pub_path, opts->key_sec_path); - free(config_dir); return 0;