Skip to content

Commit

Permalink
src/message_client: simplify by FAIL macros
Browse files Browse the repository at this point in the history
  • Loading branch information
fbrausse committed Aug 1, 2019
1 parent 54e8c17 commit c77b01c
Showing 1 changed file with 51 additions and 108 deletions.
159 changes: 51 additions & 108 deletions src/message_client.c
Original file line number Diff line number Diff line change
@@ -1,76 +1,55 @@
#include <ctype.h> // toupper
#include <stdio.h> // printf, getline
#include <stdlib.h> // exit codes
#include <sys/types.h> // socket
#include <sys/socket.h> // socket
#include <netdb.h> // getaddrinfo
#include <string.h> // memset, strlen
#include <unistd.h> // write


#include "axc.h"

#define PORT "5555"
#define FAIL0(lbl,...) do { ret = EXIT_FAILURE; fprintf(stderr, __VA_ARGS__); goto lbl; } while (0)
#define FAIL(...) FAIL0(cleanup,__VA_ARGS__)

int main(void) {
printf("sup\n");
printf("initializing context for alice...\n");
axc_context * ctx_a_p;
if (axc_context_create(&ctx_a_p)) {
fprintf(stderr, "failed to create axc context\n");
return EXIT_FAILURE;
}
int ret = EXIT_SUCCESS;
if (axc_context_create(&ctx_a_p))
FAIL0(cleanup_none, "failed to create axc context\n");

axc_context_set_log_func(ctx_a_p, axc_default_log);
axc_context_set_log_level(ctx_a_p, AXC_LOG_DEBUG);

char * db_a_fn = "client/a.sqlite";
if (axc_context_set_db_fn(ctx_a_p, db_a_fn, strlen(db_a_fn))) {
fprintf(stderr, "failed to set db filename\n");
return EXIT_FAILURE;
}
if (axc_context_set_db_fn(ctx_a_p, db_a_fn, strlen(db_a_fn)))
FAIL0(cleanup_a, "failed to set db filename\n");

printf("set db fn\n");

if (axc_init(ctx_a_p)) {
fprintf(stderr, "failed to init axc\n");
return EXIT_FAILURE;
}
if (axc_init(ctx_a_p))
FAIL0(cleanup_a, "failed to init axc\n");

printf("installing client for alice...\n");
if (axc_install(ctx_a_p)) {
fprintf(stderr, "failed to install axc\n");
axc_cleanup(ctx_a_p);
return EXIT_FAILURE;
}
if (axc_install(ctx_a_p))
FAIL0(cleanup_a, "failed to install axc\n");

printf("initializing context for bob...\n");
axc_context * ctx_b_p;
if (axc_context_create(&ctx_b_p)) {
fprintf(stderr, "failed to create axc context\n");
return EXIT_FAILURE;
}
if (axc_context_create(&ctx_b_p))
FAIL0(cleanup_a, "failed to create axc context\n");

char * db_b_fn = "client/b.sqlite";
if (axc_context_set_db_fn(ctx_b_p, db_b_fn, strlen(db_b_fn))) {
fprintf(stderr, "failed to set db filename\n");
return EXIT_FAILURE;
}
if (axc_context_set_db_fn(ctx_b_p, db_b_fn, strlen(db_b_fn)))
FAIL("failed to set db filename\n");

axc_context_set_log_func(ctx_b_p, axc_default_log);
axc_context_set_log_level(ctx_b_p, AXC_LOG_DEBUG);

if (axc_init(ctx_b_p)) {
fprintf(stderr, "failed to init axc\n");
return EXIT_FAILURE;
}
if (axc_init(ctx_b_p))
FAIL("failed to init axc\n");

printf("installing client for bob...\n");
if (axc_install(ctx_b_p)) {
fprintf(stderr, "failed to install axc\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_install(ctx_b_p))
FAIL("failed to install axc\n");

axc_address addr_a = {
.name = "alice",
Expand All @@ -88,11 +67,8 @@ int main(void) {
if (!axc_session_exists_initiated(&addr_b, ctx_a_p)) {
printf("creating session between alice and bob\n");
axc_bundle *bundle_bob;
if (axc_bundle_collect(AXC_PRE_KEYS_AMOUNT, ctx_b_p, &bundle_bob)) {
fprintf(stderr, "failed to collect bob's bundle\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_bundle_collect(AXC_PRE_KEYS_AMOUNT, ctx_b_p, &bundle_bob))
FAIL("failed to collect bob's bundle\n");
// addr_b.device_id = axc_bundle_get_reg_id(bundle_bob);
if (axc_session_from_bundle(axc_buf_list_item_get_id(axc_bundle_get_pre_key_list(bundle_bob)),
axc_buf_list_item_get_buf(axc_bundle_get_pre_key_list(bundle_bob)),
Expand All @@ -101,86 +77,57 @@ int main(void) {
axc_bundle_get_signature(bundle_bob),
axc_bundle_get_identity_key(bundle_bob),
&addr_b,
ctx_a_p)) {
fprintf(stderr, "failed to create session from bob's bundle\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
ctx_a_p))
FAIL("failed to create session from bob's bundle\n");
axc_bundle_destroy(bundle_bob);
axc_buf * msg_buf_p = axc_buf_create((const uint8_t *)"hello", strlen("hello") + 1);
if (!msg_buf_p) {
fprintf(stderr, "failed to create 'hello' msg buffer\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (!msg_buf_p)
FAIL("failed to create 'hello' msg buffer\n");

axc_buf * ct_buf_p;
if (axc_message_encrypt_and_serialize(msg_buf_p, &addr_b, ctx_a_p, &ct_buf_p)) {
fprintf(stderr, "failed to encrypt 'hello' message\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_encrypt_and_serialize(msg_buf_p, &addr_b, ctx_a_p, &ct_buf_p))
FAIL("failed to encrypt 'hello' message\n");

uint32_t alice_id;
if (axc_get_device_id(ctx_a_p, &alice_id)) {
fprintf(stderr, "failed to retrieve alice's device_id\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_get_device_id(ctx_a_p, &alice_id))
FAIL("failed to retrieve alice's device_id\n");
addr_a.device_id = alice_id;

axc_buf * pt_buf_p;
if (axc_pre_key_message_process(ct_buf_p, &addr_a, ctx_b_p, &pt_buf_p)) {
fprintf(stderr, "failed to process 'hello' pre_key_message\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_pre_key_message_process(ct_buf_p, &addr_a, ctx_b_p, &pt_buf_p))
FAIL("failed to process 'hello' pre_key_message\n");

axc_buf_free(ct_buf_p);
axc_buf_free(pt_buf_p);

if (axc_message_encrypt_and_serialize(msg_buf_p, &addr_a, ctx_b_p, &ct_buf_p)) {
fprintf(stderr, "failed encrypting 2nd 'hello' message\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_decrypt_from_serialized(ct_buf_p, &addr_b, ctx_a_p, &pt_buf_p)) {
fprintf(stderr, "failed decrypting 3rd 'hello' message\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_encrypt_and_serialize(msg_buf_p, &addr_a, ctx_b_p, &ct_buf_p))
FAIL("failed encrypting 2nd 'hello' message\n");
if (axc_message_decrypt_from_serialized(ct_buf_p, &addr_b, ctx_a_p, &pt_buf_p))
FAIL("failed decrypting 2nd 'hello' message\n");

axc_buf_free(ct_buf_p);
axc_buf_free(pt_buf_p);

axc_buf_free(msg_buf_p);

printf("session created on each side\n");
} else {
printf("session exists.\n");
uint32_t alice_id;
if (axc_get_device_id(ctx_a_p, &alice_id)) {
fprintf(stderr, "failed to retrieve alice's device_id\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_get_device_id(ctx_a_p, &alice_id))
FAIL("failed to retrieve alice's device_id\n");
addr_a.device_id = alice_id;
}
printf("now trying to ready to 'send' and 'receive' messages\n");

char * line = (void *) 0;
size_t len = 0;
printf("enter message: ");
//goto cleanup;
while(getline(&line, &len, stdin) > 0) {
axc_buf * ciphertext_p;
{
axc_buf * msg_p = axc_buf_create((uint8_t *) line, strlen(line) + 1);
if (axc_message_encrypt_and_serialize(msg_p, &addr_b, ctx_a_p, &ciphertext_p)) {
fprintf(stderr, "failed to encrypt message from alice to bob\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_encrypt_and_serialize(msg_p, &addr_b, ctx_a_p, &ciphertext_p))
FAIL("failed to encrypt message from alice to bob\n");
printf("encrypted message from alice to bob: %s\n", line);
axc_buf_free(msg_p);
}
Expand All @@ -195,11 +142,8 @@ int main(void) {

axc_buf * upper_buf;
axc_buf * plaintext_p;
if (axc_message_decrypt_from_serialized(ciphertext_p, &addr_a, ctx_b_p, &plaintext_p)) {
fprintf(stderr, "failed to decrypt message from alice to bob\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_decrypt_from_serialized(ciphertext_p, &addr_a, ctx_b_p, &plaintext_p))
FAIL("failed to decrypt message from alice to bob\n");
axc_buf_free(ciphertext_p);

printf("decrypted message: %s\n", axc_buf_get_data(plaintext_p));
Expand All @@ -213,11 +157,8 @@ int main(void) {
upper_buf = axc_buf_create((uint8_t *) upper, strlen(upper) + 1);
axc_buf_free(plaintext_p);

if (axc_message_encrypt_and_serialize(upper_buf, &addr_a, ctx_b_p, &ciphertext_p)) {
fprintf(stderr, "failed to encrypt message from bob to alice\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_encrypt_and_serialize(upper_buf, &addr_a, ctx_b_p, &ciphertext_p))
FAIL("failed to encrypt message from bob to alice\n");
axc_buf_free(upper_buf);

buf = signal_buffer_data(ciphertext_p);
Expand All @@ -228,11 +169,8 @@ int main(void) {
}
printf("\n");

if (axc_message_decrypt_from_serialized(ciphertext_p, &addr_b, ctx_a_p, &plaintext_p)) {
fprintf(stderr, "failed to decrypt message from bob to alice\n");
axc_cleanup(ctx_b_p);
return EXIT_FAILURE;
}
if (axc_message_decrypt_from_serialized(ciphertext_p, &addr_b, ctx_a_p, &plaintext_p))
FAIL("failed to decrypt message from bob to alice\n");
axc_buf_free(ciphertext_p);

printf("received reply from bob: %s\n", axc_buf_get_data(plaintext_p));
Expand All @@ -243,6 +181,11 @@ int main(void) {
free(line);

printf("done, exiting.\n");
axc_cleanup(ctx_a_p);

cleanup:
axc_cleanup(ctx_b_p);
cleanup_a:
axc_cleanup(ctx_a_p);
cleanup_none:
return ret;
}

0 comments on commit c77b01c

Please sign in to comment.