Another update of main.c.
This commit is contained in:
parent
8b67936227
commit
68cb43795b
62
src/main.c
62
src/main.c
@ -1,5 +1,7 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include "addrbook.h"
|
#include "addrbook.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
@ -21,6 +23,9 @@ int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
|
|||||||
struct options {
|
struct options {
|
||||||
const char *addr;
|
const char *addr;
|
||||||
const char *port;
|
const char *port;
|
||||||
|
const char *addressbook_path;
|
||||||
|
const char *key_path;
|
||||||
|
bool is_alias_passed;
|
||||||
char command;
|
char command;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -57,8 +62,8 @@ int main(int argc, const char **argv) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (audio_init_soundsystem() == -1)
|
// if (audio_init_soundsystem() == -1)
|
||||||
return 1;
|
// return 1;
|
||||||
|
|
||||||
FILE *addrbook_file = fopen("addressbook", "r");
|
FILE *addrbook_file = fopen("addressbook", "r");
|
||||||
if (addrbook_file != NULL) {
|
if (addrbook_file != NULL) {
|
||||||
@ -66,13 +71,34 @@ int main(int argc, const char **argv) {
|
|||||||
fclose(addrbook_file);
|
fclose(addrbook_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *pkey_file = fopen("tat_own_pkey", "r");
|
if (opts.is_alias_passed) {
|
||||||
FILE *skey_file = fopen("tat_own_skey", "r");
|
addrbook_entry_t *aec = addrbook_get_by_alias(&ab, opts.addr);
|
||||||
|
if (aec == NULL) {
|
||||||
|
fprintf(stderr, "An alias \"%s\" is not in an addressbook!\n", opts.addr);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "An alias \"%s\" was found in an addressbook!\n", opts.addr);
|
||||||
|
opts.addr = aec->last_addr;
|
||||||
|
opts.port = aec->last_port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *pkey_file = fopen("tat_own_pub_key", "r");
|
||||||
|
FILE *skey_file = fopen("tat_own_sec_key", "r");
|
||||||
if (pkey_file == NULL || skey_file == NULL) {
|
if (pkey_file == NULL || skey_file == NULL) {
|
||||||
if ((result = crypt_key_gen(&ok)) == -1) {
|
if ((result = crypt_key_gen(&ok)) == -1) {
|
||||||
result = 1;
|
result = 1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pkey_file = fopen("tat_own_pub_key", "w+");
|
||||||
|
skey_file = fopen("tat_own_sec_key", "w+");
|
||||||
|
|
||||||
|
crypt_store_key(&ok, pkey_file, skey_file);
|
||||||
|
|
||||||
|
fclose(pkey_file);
|
||||||
|
fclose(skey_file);
|
||||||
|
printf("A new cryptographical keys was written.\n");
|
||||||
} else {
|
} else {
|
||||||
if (crypt_load_key(&ok, pkey_file, skey_file) == -1) {
|
if (crypt_load_key(&ok, pkey_file, skey_file) == -1) {
|
||||||
fprintf(stderr, "Cannot load the keys.");
|
fprintf(stderr, "Cannot load the keys.");
|
||||||
@ -230,13 +256,13 @@ int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
|
|||||||
int parse_argv(int argc, const char **argv, struct options *opts) {
|
int parse_argv(int argc, const char **argv, struct options *opts) {
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
return -1;
|
return -1;
|
||||||
int positional = 0;
|
int command_arg_num = 0;
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
if (argv[i][0] == '-') {
|
if (argv[i][0] == '-') {
|
||||||
switch (argv[i][1]) {
|
switch (argv[i][1]) {
|
||||||
case 'k': break;
|
case 'k': opts->key_path = argv[i+1]; ++i; continue;
|
||||||
case 'a': break;
|
case 'a': opts->addressbook_path = argv[i+1]; ++i; continue;
|
||||||
case 'v': return -2;
|
case 'v': return -2;
|
||||||
case 'h': return -3;
|
case 'h': return -3;
|
||||||
default:
|
default:
|
||||||
@ -244,10 +270,11 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (opts->command == 'c' || opts->command == 's') {
|
if (opts->command == 'c' || opts->command == 's') {
|
||||||
if (positional == 1)
|
if (command_arg_num == 0)
|
||||||
opts->addr = argv[i];
|
opts->addr = argv[i];
|
||||||
else if (positional == 2)
|
else if (command_arg_num == 1)
|
||||||
opts->port = argv[i];
|
opts->port = argv[i];
|
||||||
|
++command_arg_num;
|
||||||
} else {
|
} else {
|
||||||
switch (argv[i][0]) {
|
switch (argv[i][0]) {
|
||||||
case 'c':
|
case 'c':
|
||||||
@ -257,9 +284,22 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
|
|||||||
default: return -1;
|
default: return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
++positional;
|
if (command_arg_num == 1) {
|
||||||
}
|
struct sockaddr test = {0};
|
||||||
|
int r = inet_pton(AF_INET, opts->addr, &test);
|
||||||
|
if (r == -1)
|
||||||
|
r = inet_pton(AF_INET6, opts->addr, &test);
|
||||||
|
opts->is_alias_passed = r != 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// opts->is_alias_passed = ((opts->addr[0]>>4) & 0xf) != 0x3;
|
||||||
|
|
||||||
|
if (opts->command == '\0' || opts->addr[0] == '\0'
|
||||||
|
|| (!opts->is_alias_passed && opts->port[0] == '\0'))
|
||||||
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user