1
0

Added parse_argv(), version() funcs. A usage() func was changed a little, but it is not its final form. Made char **argv const. Introduced struct options.

Added some code for audio transfer, just initialisation and deinit.
This commit is contained in:
Alexander Andreev 2024-03-30 03:17:13 +04:00
parent 0ac13359f1
commit 4611322ee9
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -1,10 +1,12 @@
// #include "audio.h"
#include "addrbook.h"
#include "crypt.h"
#include "net.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "addrbook.h"
#include "audio.h"
#include "crypt.h"
#include "net.h"
#define VERSION "0.0.0"
#define CHANNELS 1 #define CHANNELS 1
#define SAMPLE_RATE 48000 #define SAMPLE_RATE 48000
@ -16,66 +18,87 @@
int client_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk); 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); int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk);
struct options {
const char *addr;
const char *port;
char command;
};
int parse_argv(int argc, const char **argv, struct options *opts);
void usage(void) { void usage(void) {
fprintf(stderr, "tetatet c|s ADDR PORT\n"); fprintf(stderr, "tetatet -vh c|s ADDR PORT -k KEY_PATH -a ADDRESSBOOK_FILE\n");
} }
int main(int argc, char **argv) { void version(void) {
fprintf(stdout,
"tetatet ver. " VERSION
"\nCopyright (c) Alexander \"Arav\" Andreev <me@arav.su>\n");
}
int main(int argc, const char **argv) {
int result = 0;
struct options opts = {0};
addrbook_t ab = {0}; addrbook_t ab = {0};
net_t n = {0};
crypt_session_t s = {0};
crypt_key_t ok = {0}, rk = {0};
FILE *in = fopen("addressbook_key", "r"); result = parse_argv(argc, argv, &opts);
if (in == NULL) { switch (result) {
fprintf(stderr, "An addressbook file wasn't open: %s\n", strerror(errno)); case -1:
return 1; usage();
} return 1;
case -2:
addrbook_load(&ab, in); version();
return 0;
for (size_t i = 0; i < ab.entries_len; ++i) case -3:
printf("%ld: %.128s %s %s %s\n", i, ab.entries[i].pubk, ab.entries[i].name, ab.entries[i].last_addr, ab.entries[i].last_port); usage();
return 0;
fclose(in);
addrbook_destroy(&ab);
return 0;
if (argc < 4) {
usage();
return -1;
} }
if (sodium_init() == -1) { if (sodium_init() == -1) {
return -1; fprintf(stderr, "Cannot initialise libsodium.\n");
return 1;
} }
// if (audio_init_soundsystem() == -1) if (audio_init_soundsystem() == -1)
// return -1; return 1;
char direction = argv[1][0]; FILE *addrbook_file = fopen("addressbook", "r");
char *addr = argv[2]; if (addrbook_file != NULL) {
char *port = argv[3]; addrbook_load(&ab, addrbook_file);
fclose(addrbook_file);
if (direction != 'c' && direction != 's') {
usage();
return -1;
} }
int result = 0; FILE *pkey_file = fopen("tat_own_pkey", "r");
net_t n; FILE *skey_file = fopen("tat_own_skey", "r");
crypt_session_t s; if (pkey_file == NULL || skey_file == NULL) {
crypt_key_t ok, rk; if ((result = crypt_key_gen(&ok)) == -1) {
result = 1;
goto cleanup;
}
} else {
if (crypt_load_key(&ok, pkey_file, skey_file) == -1) {
fprintf(stderr, "Cannot load the keys.");
result = 1;
goto cleanup;
}
if ((result = crypt_key_gen(&ok)) == -1) if (pkey_file != NULL)
goto cleanup; fclose(pkey_file);
if (skey_file != NULL)
fclose(skey_file);
}
(direction == 'c') ? (opts.command == 'c') ?
(result = net_client_init(&n, addr, port)) (result = net_client_init(&n, opts.addr, opts.port))
: (result = net_server_init(&n, addr, port)); : (result = net_server_init(&n, opts.addr , opts.port));
if (result != 0) if (result != 0)
goto cleanup; goto cleanup;
(direction == 'c') ? (opts.command == 'c') ?
(result = client_handshake(&n, &s, &ok, &rk)) (result = client_handshake(&n, &s, &ok, &rk))
: (result = server_handshake(&n, &s, &ok, &rk)); : (result = server_handshake(&n, &s, &ok, &rk));
if (result != 0) if (result != 0)
@ -85,18 +108,41 @@ int main(int argc, char **argv) {
crypt_key_export_public(&rk, rkph); crypt_key_export_public(&rk, rkph);
fprintf(stderr, "A connection with %s established!\n", 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);
audio_init_default(&aud, CHANNELS, SAMPLE_RATE, FRAMES_PER_BUFFFER);
(opts.command == 'c') ?
(audio_stream_input_toggle(&aud))
: (audio_stream_output_toggle(&aud));
// for (size_t i = 0; i < (20 * SAMPLE_RATE)/FRAMES_PER_BUFFFER; ++i) {
// if (opts.command == 'c') {
// audio_read(&aud, aud_buf, AUDIO_BUF_SIZE);
// } else {
// //
// }
// }
(opts.command == 'c') ?
(audio_stream_input_toggle(&aud))
: (audio_stream_output_toggle(&aud));
cleanup: cleanup:
addrbook_destroy(&ab); addrbook_destroy(&ab);
audio_destroy(&aud);
net_destroy(&n); net_destroy(&n);
crypt_session_destroy(&s); crypt_session_destroy(&s);
crypt_key_destroy(&ok); crypt_key_destroy(&ok);
crypt_key_destroy(&rk); crypt_key_destroy(&rk);
// if (audio_terminate_soundsystem() == -1) if (audio_terminate_soundsystem() == -1)
// return -1; return 1;
return result; return result;
} }
@ -184,5 +230,42 @@ int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
free(hello); free(hello);
return 0;
}
int parse_argv(int argc, const char **argv, struct options *opts) {
if (argc == 1)
return -1;
int positional = 0;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'k': break;
case 'a': break;
case 'v': return -2;
case 'h': return -3;
default:
return -1;
}
} else {
if (opts->command == 'c' || opts->command == 's') {
if (positional == 1)
opts->addr = argv[i];
else if (positional == 2)
opts->port = argv[i];
} else {
switch (argv[i][0]) {
case 'c':
case 's':
opts->command = argv[i][0];
break;
default: return -1;
}
}
++positional;
}
}
return 0; return 0;
} }