This commit is contained in:
Wolfgang Hottgenroth 2021-02-10 11:12:02 +01:00
parent 525a920e84
commit 25302f8019
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
5 changed files with 119 additions and 116 deletions

View File

@ -3,8 +3,7 @@ BUILD_DIR = build
C_SOURCES = \ C_SOURCES = \
../cube/User/Src/sha256.c \ ../cube/User/Src/sha256.c \
sink20169.c \ sink20169.c \
logging.c \ logging.c
receiver.c
C_INCLUDES = \ C_INCLUDES = \

View File

@ -1,83 +0,0 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sinkStruct.h>
#include <logging.h>
#include <libconfig.h>
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;
}

View File

@ -1,8 +0,0 @@
#ifndef _RECEIVER_H_
#define _RECEIVER_H_
#include <libconfig.h>
int receiver(config_t *cfg);
#endif // _RECEIVER_H_

View File

@ -1,45 +1,138 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <libconfig.h> #include <libconfig.h>
#include <sinkStruct.h> #include <sinkStruct.h>
#include <logging.h> #include <logging.h>
#include <receiver.h>
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_t cfg;
config_setting_t *devicesConfig;
int receiveSockFd;
void readConfig() { void readConfig() {
config_init(&cfg); config_init(&cfg);
if (! config_read_file(&cfg, "./sink20169.cfg")) { if (! config_read_file(&cfg, "./sink20169.cfg")) {
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n", logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
config_error_file(&cfg), config_error_line(&cfg), config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg)); config_error_text(&cfg));
config_destroy(&cfg); config_destroy(&cfg);
exit(-1); 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() { int main() {
readConfig(); readConfig();
int res = receiver(&cfg); initReceiver();
if (res < 0) {
logmsg(LOG_ERR, "receiver failed to start, error: ", res); 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); config_destroy(&cfg);
} }

View File

@ -5,6 +5,8 @@ influxPort = 8086;
influxDatabase = "smarthome2"; influxDatabase = "smarthome2";
influxMeasurement = "mainsfrequency"; influxMeasurement = "mainsfrequency";
receivePort = 20169;
devices = { devices = {
MainsCnt01 = { MainsCnt01 = {
sharedSecret = "Uj6*uKDp@8Kvfa4g5eRMLUfVsSuqjxW"; sharedSecret = "Uj6*uKDp@8Kvfa4g5eRMLUfVsSuqjxW";