1
0
Fork 0
tetatet/src/main.c

192 lines
4.1 KiB
C

#include <portaudio.h>
#include <sodium/core.h>
#include <sodium/crypto_box.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
// #include "audio.h"
#include "crypt.h"
#include "net.h"
#define CHANNELS 1
#define SAMPLE_RATE 48000
#define FRAMES_PER_BUFFFER 960
#define AUDIO_BUF_SIZE (FRAMES_PER_BUFFFER * CHANNELS)
#define NET_BUF_SIZE 1024
int client_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk);
int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk);
void usage(void) {
fprintf(stderr, "tetatet c|s ADDR PORT\n");
}
int main(int argc, char **argv) {
int ret = 0;
if (argc < 4) {
usage();
return -1;
}
if (sodium_init() == -1) {
return -1;
}
// if (audio_init_soundsystem() == -1)
// return -1;
char direction = argv[1][0];
char *addr = argv[2];
char *port = argv[3];
net_t n;
crypt_session_t s;
crypt_key_t ok, rk;
if (crypt_key_gen(&ok) == -1) {
ret = -1;
goto cleanup;
}
if (direction == 'c') {
if (net_client_init(&n, addr, port) == -1) {
ret = -1;
goto cleanup;
}
int res = client_handshake(&n, &s, &ok, &rk);
if (res != 0) {
ret = res;
goto cleanup;
}
} else if (direction == 's') {
if (net_server_init(&n, addr, port) == -1) {
ret = -1;
goto cleanup;
}
int res = server_handshake(&n, &s, &ok, &rk);
if (res != 0) {
ret = -1;
goto cleanup;
}
} else {
usage();
}
fprintf(stderr, "A connection established!\n");
cleanup:
net_destroy(&n);
crypt_session_destroy(&s);
crypt_key_destroy(&ok);
crypt_key_destroy(&rk);
// if (audio_terminate_soundsystem() == -1)
// return -1;
return ret;
}
int client_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk) {
char buffer[NET_BUF_SIZE];
ssize_t bytes = 0;
unsigned char *hello = NULL;
unsigned char *nonce = NULL;
if ((hello = crypt_hello(ok)) == NULL)
return -1;
if (net_send(n, (char *)hello, CRYPT_HELLO_LEN) == -1) {
free(hello);
return -1;
}
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1) {
free(hello);
return -1;
}
if (CRYPT_HELLO_LEN != bytes) {
free(hello);
return -1;
}
if (crypt_key_from_hex_public(rk, buffer) == -1) {
free(hello);
return -1;
}
if (crypt_hello_verify((unsigned char *)buffer, rk) == -1) {
free(hello);
return -2;
}
if ((nonce = crypt_hello_get_nonce(hello, (unsigned char *)buffer, true)) == NULL) {
free(hello);
return -1;
}
free(hello);
if (crypt_session_init(s, ok, rk, nonce, true) == -1) {
free(nonce);
return -3;
}
free(nonce);
return 0;
}
int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk) {
char buffer[NET_BUF_SIZE];
ssize_t bytes = 0;
unsigned char *hello = NULL;
unsigned char *nonce = NULL;
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1)
return -1;
if (CRYPT_HELLO_LEN != bytes)
return -1;
if (crypt_key_from_hex_public(rk, buffer) == -1)
return -1;
if (crypt_hello_verify((unsigned char *)buffer, rk) == -1)
return -2;
if ((hello = crypt_hello(ok)) == NULL)
return -1;
if ((nonce = crypt_hello_get_nonce(hello, (unsigned char *)buffer, false)) == NULL) {
free(hello);
return -1;
}
if (crypt_session_init(s, ok, rk, nonce, false) == -1) {
free(nonce);
return -3;
}
n->raddr = n->inaddr;
n->raddr_len = n->inaddr_len;
if (net_send(n, (char *)hello, CRYPT_HELLO_LEN) == -1) {
free(hello);
return -1;
}
free(hello);
free(nonce);
return 0;
}