This commit is contained in:
Wolfgang Hottgenroth 2021-02-09 18:32:58 +01:00
parent d86174fc93
commit b240f5b3e4
7 changed files with 147 additions and 147 deletions

View File

@ -2,14 +2,17 @@ BUILD_DIR = build
C_SOURCES = \
../cube/User/Src/sha256.c \
sink20169.c
sink20169.c \
logging.c \
receiver.c
C_INCLUDES = \
-I../cube/User/Inc
CC = gcc
CFLAGS = $(C_INCLUDES) -Wall -Werror -std=c99
LDFLAGS =
LDFLAGS = -lconfig
TARGET = sink20169
all: $(BUILD_DIR)/$(TARGET)

13
sink/logging.c Normal file
View File

@ -0,0 +1,13 @@
#include <syslog.h>
#include <stdarg.h>
void logmsg(int prio, const char* format, ...) {
va_list vl;
openlog("counter", 0, LOG_LOCAL0);
va_start(vl, format);
vsyslog(prio, format, vl);
va_end(vl);
closelog();
}

8
sink/logging.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _LOGGING_H_
#define _LOGGING_H_
#include <syslog.h>
void logmsg(int prio, const char* format, ...);
#endif // _LOGGING_H_

82
sink/receiver.c Normal file
View File

@ -0,0 +1,82 @@
#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_lookup(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;
}
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;
}

8
sink/receiver.h Normal file
View File

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

View File

@ -1,10 +1,8 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sinkStruct.h>
#include <logging.h>
#include <libconfig.h>
typedef struct {
@ -17,149 +15,25 @@ const t_device devices[] = {
{ .deviceId = "", .sharedSecret = "" }
};
config_t cfg;
void readConfig() {
config_init(&cfg);
if (! config_read_file(&cfg, "/etc/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);
}
}
int main() {
int sockfd;
struct sockaddr_in servaddr, cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
readConfig();
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);
printf("received %d octets from %04x\n",
n, cliaddr.sin_addr.s_addr);
if (n != sizeof(buf.b)) {
printf("Illegal packet size: %d\n", n);
continue;
int res = receiver(cfg);
if (res < 0) {
logmsg(LOG_ERR, "receiver failed to start, error: ", res);
}
uint8_t i = 0;
while (1) {
if (strlen(devices[i].deviceId) == 0) {
break;
}
if (strncmp(devices[i].deviceId, buf.s.deviceId, sizeof(((t_configBlock*)0)->deviceId)) == 0) {
printf("Device found: %s\n", devices[i].deviceId);
uint8_t receivedHash[SHA256_BLOCK_SIZE];
memcpy(receivedHash, buf.s.hash, SHA256_BLOCK_SIZE);
memcpy(buf.s.hash, devices[i].sharedSecret, SHA256_BLOCK_SIZE);
printf("recv. hash, 1. half is %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
receivedHash[0],
receivedHash[1],
receivedHash[2],
receivedHash[3],
receivedHash[4],
receivedHash[5],
receivedHash[6],
receivedHash[7],
receivedHash[8],
receivedHash[9],
receivedHash[10],
receivedHash[11],
receivedHash[12],
receivedHash[13],
receivedHash[14],
receivedHash[15]
);
printf("recv. hash, 2. half is %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
receivedHash[16],
receivedHash[17],
receivedHash[18],
receivedHash[19],
receivedHash[20],
receivedHash[21],
receivedHash[22],
receivedHash[23],
receivedHash[24],
receivedHash[25],
receivedHash[26],
receivedHash[27],
receivedHash[28],
receivedHash[29],
receivedHash[30],
receivedHash[31]
);
SHA256_CTX ctx;
uint8_t calculatedHash[SHA256_BLOCK_SIZE];
sha256_init(&ctx);
sha256_update(&ctx, buf.b, sizeof(buf.b));
sha256_final(&ctx, calculatedHash);
printf("calc. hash, 1. half is %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
calculatedHash[0],
calculatedHash[1],
calculatedHash[2],
calculatedHash[3],
calculatedHash[4],
calculatedHash[5],
calculatedHash[6],
calculatedHash[7],
calculatedHash[8],
calculatedHash[9],
calculatedHash[10],
calculatedHash[11],
calculatedHash[12],
calculatedHash[13],
calculatedHash[14],
calculatedHash[15]
);
printf("calc. hash, 2. half is %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
calculatedHash[16],
calculatedHash[17],
calculatedHash[18],
calculatedHash[19],
calculatedHash[20],
calculatedHash[21],
calculatedHash[22],
calculatedHash[23],
calculatedHash[24],
calculatedHash[25],
calculatedHash[26],
calculatedHash[27],
calculatedHash[28],
calculatedHash[29],
calculatedHash[30],
calculatedHash[31]
);
if (memcmp(receivedHash, calculatedHash, SHA256_BLOCK_SIZE) != 0) {
printf("Invalid hash\n");
}
printf("DeviceId: %s\n", buf.s.deviceId);
printf("Location: %s\n", buf.s.location);
for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) {
printf("Time: %lu, Frequency: %u\n", buf.s.events[j].timestamp, buf.s.events[j].frequency);
}
printf("\n");
break;
} else {
printf("Unknown device\n");
}
i++;
}
}
}

12
sink/sink20169.cfg Normal file
View File

@ -0,0 +1,12 @@
influxUser = "";
influxPass = "";
influxServer = "127.0.0.1";
influxPort = 8086;
influxDatabase = "smarthome2";
influxMeasurement = "mainsfrequency";
devices = {
MainsCnt1 = {
sharedSecret = "strengGeheim";
};
};