summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2020-12-05 06:08:44 +0100
committerDirk Engling <erdgeist@erdgeist.org>2020-12-05 06:08:44 +0100
commitc08a84212ecbe380f0548d58255650deda5e0558 (patch)
tree67bc280b25620c20b604776074a47eca875e5a3d
parent92de4cedd3ae0a088c87e38d3a9560d3b3cfd54f (diff)
Make sender use getaddrinfo to lookup loggin host name
-rw-r--r--sender.c48
1 files 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 @@
5#include <inttypes.h> 5#include <inttypes.h>
6#include <arpa/inet.h> 6#include <arpa/inet.h>
7#include <unistd.h> 7#include <unistd.h>
8#include <netdb.h>
8 9
9#include <mbedtls/pk.h> 10#include <mbedtls/pk.h>
10#include <mbedtls/entropy.h> 11#include <mbedtls/entropy.h>
11#include "mbedtls/ctr_drbg.h" 12#include "mbedtls/ctr_drbg.h"
12#include "mbedtls/gcm.h" 13#include "mbedtls/gcm.h"
13 14
14const unsigned short PORT = 58132;
15 15
16static const unsigned char pubkey[] = 16static const unsigned char pubkey[] =
17"-----BEGIN PUBLIC KEY-----\n" 17"-----BEGIN PUBLIC KEY-----\n"
@@ -25,10 +25,17 @@ static const unsigned char pubkey[] =
25"-----END PUBLIC KEY----- \n"; 25"-----END PUBLIC KEY----- \n";
26 26
27static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH"; 27static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH";
28static const char *logging_host = "localhost";
29static const char *logging_port = "58132";
30
31static struct sockaddr_storage logging_host_address;
32static socklen_t logging_host_address_len = 0;
33static int logging_socket = -1;
28 34
29enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 }; 35enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 };
30static uint64_t session_id = 0x0123456789abcdef; 36static uint64_t session_id = 0x0123456789abcdef;
31static uint8_t aes_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; 37static uint8_t aes_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
38static mbedtls_gcm_context ctx;
32 39
33void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, size_t len) { 40void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, size_t len) {
34 uint8_t iv[GCM_IV_LENGTH]; 41 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,
40 memcpy(output + 1, (uint8_t*)&session_id, SESSION_ID_LENGTH); 47 memcpy(output + 1, (uint8_t*)&session_id, SESSION_ID_LENGTH);
41 memcpy(output + 1 + SESSION_ID_LENGTH, iv, GCM_IV_LENGTH); 48 memcpy(output + 1 + SESSION_ID_LENGTH, iv, GCM_IV_LENGTH);
42 49
43 mbedtls_gcm_context ctx;
44 mbedtls_gcm_init(&ctx);
45 mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH);
46 50
47 if (!mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, len, iv, GCM_IV_LENGTH, (uint8_t*)&session_id, SESSION_ID_LENGTH, 51 if (!mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, len, iv, GCM_IV_LENGTH, (uint8_t*)&session_id, SESSION_ID_LENGTH,
48 text, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH, GCM_TAG_LENGTH, 52 text, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH, GCM_TAG_LENGTH,
49 output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH)) { 53 output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH)) {
50 struct sockaddr_in to; 54 sendto(sock, output, total_length, 0, (struct sockaddr*)&logging_host_address, logging_host_address_len);
51 memset(&to, 0, sizeof(to));
52 to.sin_family = AF_INET;
53 to.sin_addr.s_addr = inet_addr("127.0.0.1");
54 to.sin_port = htons(PORT);
55
56 sendto(sock, output, total_length, 0, (struct sockaddr*)&to, sizeof(to));
57 } 55 }
58 56
59 mbedtls_gcm_free(&ctx); 57 mbedtls_gcm_free(&ctx);
@@ -80,13 +78,10 @@ void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) {
80 78
81 mbedtls_pk_free(&pk); 79 mbedtls_pk_free(&pk);
82 80
83 struct sockaddr_in to; 81 sendto(sock, output, olen + 1 + SESSION_ID_LENGTH, 0, (struct sockaddr*)&logging_host_address, logging_host_address_len);
84 memset(&to, 0, sizeof(to));
85 to.sin_family = AF_INET;
86 to.sin_addr.s_addr = inet_addr("127.0.0.1");
87 to.sin_port = htons(PORT);
88 82
89 sendto(sock, output, olen + 1 + SESSION_ID_LENGTH, 0, (struct sockaddr*)&to, sizeof(to)); 83 mbedtls_gcm_init(&ctx);
84 mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH);
90} 85}
91 86
92int main() { 87int main() {
@@ -97,7 +92,24 @@ int main() {
97 mbedtls_ctr_drbg_init(&ctr_drbg); 92 mbedtls_ctr_drbg_init(&ctr_drbg);
98 mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); 93 mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp));
99 94
100 int sock = socket(AF_INET, SOCK_DGRAM, 0); 95 struct addrinfo hints, *result, *rp;
96 memset (&hints, 0, sizeof (hints));
97 hints.ai_socktype = SOCK_DGRAM;
98
99 int sock = -1, res = getaddrinfo(logging_host, logging_port, &hints, &result);
100 if (res != 0)
101 errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res));
102
103 for (rp = result; rp != NULL; rp = rp->ai_next) {
104 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
105 if (sock == -1)
106 continue;
107 memcpy(&logging_host_address, rp->ai_addr, rp->ai_addrlen);
108 logging_host_address_len = rp->ai_addrlen;
109 }
110 if (sock == -1)
111 errx(EXIT_FAILURE, "Can't open socket");
112 freeaddrinfo(result);
101 113
102 new_session(sock, &ctr_drbg); 114 new_session(sock, &ctr_drbg);
103 115