Compare commits
4 Commits
597cc3d293
...
124bad3938
Author | SHA1 | Date |
---|---|---|
Alexander Andreev | 124bad3938 | |
Alexander Andreev | 67513cc729 | |
Alexander Andreev | 85feeebd8c | |
Alexander Andreev | 8d2f97a87d |
8
Makefile
8
Makefile
|
@ -3,7 +3,6 @@ TARGET = tetatet
|
|||
CC = cc
|
||||
|
||||
DESTDIR :=
|
||||
DESTDIR = /
|
||||
PREFIX := /usr/local
|
||||
|
||||
SRC_DIR := src
|
||||
|
@ -25,6 +24,13 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
|
|||
$(OBJ_DIR):
|
||||
mkdir -p $@
|
||||
|
||||
install: | $(TARGET)
|
||||
strip tetatet
|
||||
install -m0755 tetatet $(DESTDIR)$(PREFIX)/bin/tetatet
|
||||
|
||||
uninstall:
|
||||
rm -f $(DESTDIR)$(PREFIX)/bin/tetatet
|
||||
|
||||
clean:
|
||||
rm -f ${TARGET}
|
||||
rm -f obj/*.o
|
104
src/main.c
104
src/main.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "addrbook.h"
|
||||
#include "audio.h"
|
||||
|
@ -57,15 +58,58 @@ void version(void) {
|
|||
"\nCopyright (c) Alexander \"Arav\" Andreev <me@arav.su>\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;
|
||||
}
|
||||
|
@ -348,17 +384,17 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
opts->addressbook_path = (char *)malloc(config_dir_len + 11 + 1);
|
||||
opts->addressbook_path = (char *)malloc(config_dir_len + 12 + 1);
|
||||
strncpy(opts->addressbook_path, config_dir, config_dir_len);
|
||||
strcat(opts->addressbook_path, "addressbook");
|
||||
strcat(opts->addressbook_path, "/addressbook");
|
||||
|
||||
opts->key_pub_path = (char *)malloc(config_dir_len + 11 + 1);
|
||||
opts->key_pub_path = (char *)malloc(config_dir_len + 12 + 1);
|
||||
strncpy(opts->key_pub_path, config_dir, config_dir_len);
|
||||
strcat(opts->key_pub_path, "own_pub_key");
|
||||
strcat(opts->key_pub_path, "/own_pub_key");
|
||||
|
||||
opts->key_sec_path = (char *)malloc(config_dir_len + 11 + 1);
|
||||
opts->key_sec_path = (char *)malloc(config_dir_len + 12 + 1);
|
||||
strncpy(opts->key_sec_path, config_dir, config_dir_len);
|
||||
strcat(opts->key_sec_path, "own_sec_key");
|
||||
strcat(opts->key_sec_path, "/own_sec_key");
|
||||
|
||||
free(config_dir);
|
||||
|
||||
|
|
14
src/net.c
14
src/net.c
|
@ -1,6 +1,7 @@
|
|||
#include "net.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
|
||||
|
@ -99,6 +100,19 @@ void net_destroy(net_t *n) {
|
|||
n->inaddr_len = sizeof(n->inaddr);
|
||||
}
|
||||
|
||||
bool net_set_timeout(net_t *const n, int usec) {
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = usec;
|
||||
|
||||
if (setsockopt(n->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
|
||||
return false;
|
||||
if (setsockopt(n->fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ssize_t net_send(net_t *const n, const char *const buf, int buf_len) {
|
||||
if (n->fd == -1 || n->raddr.sa_family == AF_UNSPEC)
|
||||
return -1;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef _NET_H_
|
||||
#define _NET_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
typedef struct net_t {
|
||||
|
@ -13,6 +14,8 @@ int net_client_init(net_t *const n, const char *const raddr, const char *const r
|
|||
int net_server_init(net_t *const n, const char *const laddr, const char *const lport);
|
||||
void net_destroy(net_t *n);
|
||||
|
||||
bool net_set_timeout(net_t *const n, int usec);
|
||||
|
||||
ssize_t net_send(net_t *const n, const char *const buf, int buf_len);
|
||||
ssize_t net_recv(net_t *const n, void *const buf, size_t buf_len);
|
||||
|
||||
|
|
Loading…
Reference in New Issue