From e578d42d6c6be27f25001316be86880799c2c3a8 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 10 Feb 2021 11:12:02 +0100 Subject: [PATCH] refactor --- sink/Makefile | 3 +- sink/receiver.c | 83 --------------------------- sink/receiver.h | 8 --- sink/sink20169.c | 139 +++++++++++++++++++++++++++++++++++++-------- sink/sink20169.cfg | 2 + 5 files changed, 119 insertions(+), 116 deletions(-) delete mode 100644 sink/receiver.c delete mode 100644 sink/receiver.h diff --git a/sink/Makefile b/sink/Makefile index 4330ab7..8af2cac 100644 --- a/sink/Makefile +++ b/sink/Makefile @@ -3,8 +3,7 @@ BUILD_DIR = build C_SOURCES = \ ../cube/User/Src/sha256.c \ sink20169.c \ -logging.c \ -receiver.c +logging.c C_INCLUDES = \ diff --git a/sink/receiver.c b/sink/receiver.c deleted file mode 100644 index 5dac935..0000000 --- a/sink/receiver.c +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - - -int receiver(config_t *cfg) { - config_setting_t *devicesConfig = config_lookup(cfg, "devices"); - if (devicesConfig == NULL) { - logmsg(LOG_ERR, "receiver: no devices configuration found"); - return -1; - } - - int sockfd; - struct sockaddr_in servaddr, cliaddr; - socklen_t cliaddrlen = sizeof(cliaddr); - - sockfd = socket(AF_INET, SOCK_DGRAM, 0); - - memset(&servaddr, 0, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - servaddr.sin_port = htons(20169); - - bind(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr)); - - t_minuteBuffer buf; - - while (1) { - int n = recvfrom(sockfd, buf.b, sizeof(buf.b), MSG_TRUNC, - (struct sockaddr *) &cliaddr, &cliaddrlen); - logmsg(LOG_INFO, "received %d octets from %04x", n, cliaddr.sin_addr.s_addr); - - if (n != sizeof(buf.b)) { - logmsg(LOG_INFO, "Illegal packet size: %d", n); - continue; - } - - config_setting_t *deviceConfig = config_setting_get_member(devicesConfig, buf.s.deviceId); - if (deviceConfig == NULL) { - logmsg(LOG_INFO, "Unknown device: %s", buf.s.deviceId); - continue; - } - - const char *sharedSecret; - if (! config_setting_lookup_string(deviceConfig, "sharedSecret", &sharedSecret)) { - logmsg(LOG_ERR, "No sharedsecret configured for device %s", buf.s.deviceId); - continue; - } - logmsg(LOG_INFO, "SharedSecret is %s", sharedSecret); - - if (strlen(sharedSecret) > SHA256_BLOCK_SIZE) { - logmsg(LOG_ERR, "Configured sharedsecret for device %s is too long", buf.s.deviceId); - continue; - } - - uint8_t receivedHash[SHA256_BLOCK_SIZE]; - memcpy(receivedHash, buf.s.hash, SHA256_BLOCK_SIZE); - memcpy(buf.s.hash, sharedSecret, SHA256_BLOCK_SIZE); - - SHA256_CTX ctx; - uint8_t calculatedHash[SHA256_BLOCK_SIZE]; - sha256_init(&ctx); - sha256_update(&ctx, buf.b, sizeof(buf.b)); - sha256_final(&ctx, calculatedHash); - - if (memcmp(receivedHash, calculatedHash, SHA256_BLOCK_SIZE) != 0) { - logmsg(LOG_INFO, "Invalid hash in msg for device %s", buf.s.deviceId); - continue; - } - - logmsg(LOG_INFO, "DeviceId: %s", buf.s.deviceId); - logmsg(LOG_INFO, "Location: %s", buf.s.location); - for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) { - logmsg(LOG_INFO, "Time: %lu, Frequency: %u", buf.s.events[j].timestamp, buf.s.events[j].frequency); - } - } - - return 0; -} diff --git a/sink/receiver.h b/sink/receiver.h deleted file mode 100644 index efd1d50..0000000 --- a/sink/receiver.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _RECEIVER_H_ -#define _RECEIVER_H_ - -#include - -int receiver(config_t *cfg); - -#endif // _RECEIVER_H_ diff --git a/sink/sink20169.c b/sink/sink20169.c index 264cb91..e04f42c 100644 --- a/sink/sink20169.c +++ b/sink/sink20169.c @@ -1,45 +1,138 @@ #include #include +#include #include #include #include -#include - -typedef struct { - char deviceId[sizeof(((t_configBlock*)0)->deviceId)]; - char sharedSecret[sizeof(((t_configBlock*)0)->sharedSecret)]; -} t_device; - -const t_device devices[] = { - { .deviceId = "MainsCnt01", .sharedSecret = "sharedSecretGanzGeheim" }, - { .deviceId = "", .sharedSecret = "" } -}; - config_t cfg; +config_setting_t *devicesConfig; + +int receiveSockFd; void readConfig() { - config_init(&cfg); - if (! config_read_file(&cfg, "./sink20169.cfg")) { - logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n", - config_error_file(&cfg), config_error_line(&cfg), - config_error_text(&cfg)); - config_destroy(&cfg); - exit(-1); - } + config_init(&cfg); + if (! config_read_file(&cfg, "./sink20169.cfg")) { + logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n", + config_error_file(&cfg), config_error_line(&cfg), + config_error_text(&cfg)); + config_destroy(&cfg); + exit(-1); + } + + devicesConfig = config_lookup(cfg, "devices"); + if (devicesConfig == NULL) { + logmsg(LOG_ERR, "receiver: no devices configuration found"); + exit(-2); + } } +void initReceiver() { + struct sockaddr_in servaddr; + + receiveSockFd = socket(AF_INET, SOCK_DGRAM, 0); + if (receiveSockFd == -1) { + logmsg(LOG_ERR, "failed to create receive socket: %d", errno); + exit(-3); + } + + int receivePort = 20169; + config_setting_lookup_int(&cfg, "receivePort", &receivePort); + if (receivePort < 1 || receivePort > 65535) { + logmsg(LOG_ERR, "illegal receive port configured"); + exit(-4); + } + + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(receivePort); + + if (-1 == bind(receiveSockFd, (const struct sockaddr *) &servaddr, sizeof(servaddr))) { + logmsg(LOG_ERR, "unable to bind receive: %d", errno); + exit(-5); + } +} + +int receiveAndVerify(t_minuteBuffer *buf) { + struct sockaddr_in servaddr, cliaddr; + socklen_t cliaddrlen = sizeof(cliaddr); + + int n = recvfrom(sockfd, buf->b, sizeof(buf->b), MSG_TRUNC, + (struct sockaddr *) &cliaddr, &cliaddrlen); + logmsg(LOG_INFO, "received %d octets from %04x", n, cliaddr.sin_addr.s_addr); + + if (n != sizeof(buf->b)) { + logmsg(LOG_INFO, "Illegal packet size: %d", n); + return -1; + } + + config_setting_t *deviceConfig = config_setting_get_member(devicesConfig, buf->s.deviceId); + if (deviceConfig == NULL) { + logmsg(LOG_INFO, "Unknown device: %s", buf->s.deviceId); + return -2; + } + + const char *sharedSecret; + if (! config_setting_lookup_string(deviceConfig, "sharedSecret", &sharedSecret)) { + logmsg(LOG_ERR, "No sharedsecret configured for device %s", buf->s.deviceId); + return -3; + } + logmsg(LOG_INFO, "SharedSecret is %s", sharedSecret); + + if (strlen(sharedSecret) >= SHA256_BLOCK_SIZE) { + logmsg(LOG_ERR, "Configured sharedsecret for device %s is too long", buf->s.deviceId); + return -4; + } + + uint8_t receivedHash[SHA256_BLOCK_SIZE]; + memcpy(receivedHash, buf->s.hash, SHA256_BLOCK_SIZE); + memcpy(buf->s.hash, sharedSecret, SHA256_BLOCK_SIZE); + + SHA256_CTX ctx; + uint8_t calculatedHash[SHA256_BLOCK_SIZE]; + sha256_init(&ctx); + sha256_update(&ctx, buf->b, sizeof(buf->b)); + sha256_final(&ctx, calculatedHash); + + if (memcmp(receivedHash, calculatedHash, SHA256_BLOCK_SIZE) != 0) { + logmsg(LOG_INFO, "Invalid hash in msg for device %s", buf->s.deviceId); + return -5; + } + + return 0; +} + +int send(t_minuteBuffer &buf) { + logmsg(LOG_INFO, "DeviceId: %s", buf->s.deviceId); + logmsg(LOG_INFO, "Location: %s", buf->s.location); + for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) { + logmsg(LOG_INFO, "Time: %lu, Frequency: %u", buf->s.events[j].timestamp, buf->s.events[j].frequency); + } + + return 0; +} int main() { readConfig(); - int res = receiver(&cfg); - if (res < 0) { - logmsg(LOG_ERR, "receiver failed to start, error: ", res); + initReceiver(); + + while (1) { + t_minuteBuffer buf; + + if (receiveAndVerify(&buf) < 0) { + logmsg(LOG_ERR, "error in receiveAndVerify"); + } else { + if (send(&buf) < 0) { + logmsg(LOG_ERR, "error in send"); + } + } } + close(receiveSockFd); config_destroy(&cfg); } diff --git a/sink/sink20169.cfg b/sink/sink20169.cfg index c65ce43..15f90b8 100644 --- a/sink/sink20169.cfg +++ b/sink/sink20169.cfg @@ -5,6 +5,8 @@ influxPort = 8086; influxDatabase = "smarthome2"; influxMeasurement = "mainsfrequency"; +receivePort = 20169; + devices = { MainsCnt01 = { sharedSecret = "Uj6*uKDp@8Kvfa4g5eRMLUfVsSuqjxW";