1
0
Fork 0

Compare commits

...

2 Commits

3 changed files with 72 additions and 20 deletions

View File

@ -8,10 +8,10 @@
#define ADDRBOOK_DELIM_COUNT 4
char *addrbook_entry_to_string(const addrbook_entry_t *const ent) {
size_t str_len = CRYPT_PKEY_HEXLEN + ent->name_len + ent->last_addr_len
size_t str_len = CRYPT_PKEY_HEXLEN + ent->alias_len + ent->last_addr_len
+ ent->last_port_len + ADDRBOOK_DELIM_COUNT;
char *str = (char *)malloc(str_len * sizeof(char));
sprintf(str, "%.128s\t%.*s\t%.*s\t%.*s\n", ent->pubk, ent->name_len, ent->name,
sprintf(str, "%.128s\t%.*s\t%.*s\t%.*s\n", ent->pubk, ent->alias_len, ent->alias,
ent->last_addr_len, ent->last_addr, ent->last_port_len, ent->last_port);
return str;
}
@ -23,8 +23,8 @@ int addrbook_entry_from_string(addrbook_entry_t *const ent, char * str) {
memcpy(ent->pubk, tok, CRYPT_PKEY_HEXLEN);
tok = strtok(NULL, "\t\n");
ent->name = strdup(tok);
ent->name_len = strlen(ent->name);
ent->alias = strdup(tok);
ent->alias_len = strlen(ent->alias);
tok = strtok(NULL, "\t\n");
ent->last_addr = strdup(tok);
@ -94,7 +94,7 @@ int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent) {
ab->entries = (addrbook_entry_t *)malloc(sizeof(addrbook_entry_t));
ab->entries_cap = 1;
ab->entries_len = 0;
} else if (addrbook_get(ab, ent->pubk) != NULL)
} else if (addrbook_get_by_pk(ab, ent->pubk) != NULL)
return -1;
if (ab->entries_len == ab->entries_cap)
@ -105,7 +105,7 @@ int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent) {
return 0;
}
addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]) {
addrbook_entry_t *addrbook_get_by_pk(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]) {
if (ab->entries == NULL)
return NULL;
@ -115,3 +115,14 @@ addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT
return NULL;
}
addrbook_entry_t *addrbook_get_by_alias(const addrbook_t *const ab, const char *const alias) {
if (ab->entries == NULL)
return NULL;
for (size_t i = 0; i < ab->entries_len; ++i)
if (strncmp(ab->entries[i].alias, alias, CRYPT_PKEY_HEXLEN) == 0)
return &ab->entries[i];
return NULL;
}

View File

@ -9,10 +9,10 @@
typedef struct addrbook_entry_t {
char pubk[CRYPT_PKEY_HEXLEN];
char *name;
char *alias;
char *last_addr;
char *last_port;
unsigned char name_len;
unsigned char alias_len;
unsigned char last_addr_len;
unsigned char last_port_len;
} addrbook_entry_t;
@ -32,6 +32,7 @@ int addrbook_store(addrbook_t *ab, FILE *out);
void addrbook_destroy(addrbook_t *const ab);
int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent);
addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]);
addrbook_entry_t *addrbook_get_by_pk(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]);
addrbook_entry_t *addrbook_get_by_alias(const addrbook_t *const ab, const char *const alias);
#endif /* _ADDRBOOK_H_ */

View File

@ -1,5 +1,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "addrbook.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 {
const char *addr;
const char *port;
const char *addressbook_path;
const char *key_path;
bool is_alias_passed;
char command;
};
@ -57,8 +62,8 @@ int main(int argc, const char **argv) {
return 1;
}
if (audio_init_soundsystem() == -1)
return 1;
// if (audio_init_soundsystem() == -1)
// return 1;
FILE *addrbook_file = fopen("addressbook", "r");
if (addrbook_file != NULL) {
@ -66,13 +71,34 @@ int main(int argc, const char **argv) {
fclose(addrbook_file);
}
FILE *pkey_file = fopen("tat_own_pkey", "r");
FILE *skey_file = fopen("tat_own_skey", "r");
if (opts.is_alias_passed) {
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 ((result = crypt_key_gen(&ok)) == -1) {
result = 1;
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 {
if (crypt_load_key(&ok, pkey_file, skey_file) == -1) {
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) {
if (argc == 1)
return -1;
int positional = 0;
int command_arg_num = 0;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'k': break;
case 'a': break;
case 'k': opts->key_path = argv[i+1]; ++i; continue;
case 'a': opts->addressbook_path = argv[i+1]; ++i; continue;
case 'v': return -2;
case 'h': return -3;
default:
@ -244,10 +270,11 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
}
} else {
if (opts->command == 'c' || opts->command == 's') {
if (positional == 1)
if (command_arg_num == 0)
opts->addr = argv[i];
else if (positional == 2)
else if (command_arg_num == 1)
opts->port = argv[i];
++command_arg_num;
} else {
switch (argv[i][0]) {
case 'c':
@ -257,9 +284,22 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
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;
}