This commit is contained in:
2021-02-08 18:07:17 +01:00
parent 82e4b08937
commit 38ae182326
3 changed files with 60 additions and 6 deletions

View File

@ -19,4 +19,9 @@ typedef struct __attribute__((__packed__)) {
t_event events[SECONDS_PER_MINUTE]; t_event events[SECONDS_PER_MINUTE];
} t_minuteStruct; } t_minuteStruct;
typedef union {
t_minuteStruct s;
uint8_t b[sizeof(t_minuteStruct)];
} t_minuteBuffer;
#endif // _SINKSTRUCT_H_ #endif // _SINKSTRUCT_H_

View File

@ -27,10 +27,6 @@ volatile static uint32_t mainsCntCnt = 0;
static t_seconds *seconds; static t_seconds *seconds;
typedef union {
t_minuteStruct s;
uint8_t b[sizeof(t_minuteStruct)];
} t_minuteBuffer;
#define NUM_OF_MINUTE_BUFFERS 2 #define NUM_OF_MINUTE_BUFFERS 2
t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS]; t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS];

View File

@ -4,6 +4,20 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h> #include <string.h>
#include <sinkStruct.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 = NULL, .sharedSecret = NULL }
};
int main() { int main() {
int sockfd; int sockfd;
@ -19,13 +33,52 @@ int main() {
bind(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr)); bind(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr));
uint8_t buf[800]; t_minuteBuffer buf;
while (1) { while (1) {
int n = recvfrom(sockfd, buf, sizeof(buf), MSG_TRUNC, int n = recvfrom(sockfd, buf.b, sizeof(buf.b), MSG_TRUNC,
(struct sockaddr *) &cliaddr, &cliaddrlen); (struct sockaddr *) &cliaddr, &cliaddrlen);
printf("received %d octets from %04x\n", printf("received %d octets from %04x\n",
n, cliaddr.sin_addr.s_addr); n, cliaddr.sin_addr.s_addr);
if (n != sizeof(buf.b)) {
printf("Illegal packet size: %d\n", n);
continue;
}
uint8_t i = 0;
while (1) {
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);
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) {
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: %llu, Frequency: %lu\n", buf.s.events[j].timestamp, buf.s.events[j].frequency);
}
printf("\n");
break;
} else {
printf("Unknown device\n");
}
}
} }
} }