From c08a84212ecbe380f0548d58255650deda5e0558 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 5 Dec 2020 06:08:44 +0100 Subject: Make sender use getaddrinfo to lookup loggin host name --- sender.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/sender.c b/sender.c index 19a3bed..c56cb8c 100644 --- a/sender.c +++ b/sender.c @@ -5,13 +5,13 @@ #include #include #include +#include #include #include #include "mbedtls/ctr_drbg.h" #include "mbedtls/gcm.h" -const unsigned short PORT = 58132; static const unsigned char pubkey[] = "-----BEGIN PUBLIC KEY-----\n" @@ -25,10 +25,17 @@ static const unsigned char pubkey[] = "-----END PUBLIC KEY----- \n"; static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH"; +static const char *logging_host = "localhost"; +static const char *logging_port = "58132"; + +static struct sockaddr_storage logging_host_address; +static socklen_t logging_host_address_len = 0; +static int logging_socket = -1; enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 }; static uint64_t session_id = 0x0123456789abcdef; static uint8_t aes_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; +static mbedtls_gcm_context ctx; void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, size_t len) { uint8_t iv[GCM_IV_LENGTH]; @@ -40,20 +47,11 @@ void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, memcpy(output + 1, (uint8_t*)&session_id, SESSION_ID_LENGTH); memcpy(output + 1 + SESSION_ID_LENGTH, iv, GCM_IV_LENGTH); - mbedtls_gcm_context ctx; - mbedtls_gcm_init(&ctx); - mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); if (!mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, len, iv, GCM_IV_LENGTH, (uint8_t*)&session_id, SESSION_ID_LENGTH, text, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH, GCM_TAG_LENGTH, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH)) { - struct sockaddr_in to; - memset(&to, 0, sizeof(to)); - to.sin_family = AF_INET; - to.sin_addr.s_addr = inet_addr("127.0.0.1"); - to.sin_port = htons(PORT); - - sendto(sock, output, total_length, 0, (struct sockaddr*)&to, sizeof(to)); + sendto(sock, output, total_length, 0, (struct sockaddr*)&logging_host_address, logging_host_address_len); } mbedtls_gcm_free(&ctx); @@ -80,13 +78,10 @@ void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) { mbedtls_pk_free(&pk); - struct sockaddr_in to; - memset(&to, 0, sizeof(to)); - to.sin_family = AF_INET; - to.sin_addr.s_addr = inet_addr("127.0.0.1"); - to.sin_port = htons(PORT); + sendto(sock, output, olen + 1 + SESSION_ID_LENGTH, 0, (struct sockaddr*)&logging_host_address, logging_host_address_len); - sendto(sock, output, olen + 1 + SESSION_ID_LENGTH, 0, (struct sockaddr*)&to, sizeof(to)); + mbedtls_gcm_init(&ctx); + mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); } int main() { @@ -97,7 +92,24 @@ int main() { mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); - int sock = socket(AF_INET, SOCK_DGRAM, 0); + struct addrinfo hints, *result, *rp; + memset (&hints, 0, sizeof (hints)); + hints.ai_socktype = SOCK_DGRAM; + + int sock = -1, res = getaddrinfo(logging_host, logging_port, &hints, &result); + if (res != 0) + errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res)); + + for (rp = result; rp != NULL; rp = rp->ai_next) { + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (sock == -1) + continue; + memcpy(&logging_host_address, rp->ai_addr, rp->ai_addrlen); + logging_host_address_len = rp->ai_addrlen; + } + if (sock == -1) + errx(EXIT_FAILURE, "Can't open socket"); + freeaddrinfo(result); new_session(sock, &ctr_drbg); -- cgit v1.2.3