Receiving broadcast messages and plausibility-checking

Now we're receiving the client broadcasts on the server side and
checking if they match the magic number we're expecting.
This commit is contained in:
Johannes Bauer 2019-10-25 09:33:20 +02:00
parent 2f36b56417
commit 8c7c0e5870
5 changed files with 29 additions and 18 deletions

View File

@ -24,11 +24,6 @@
#ifndef __GLOBAL_H__ #ifndef __GLOBAL_H__
#define __GLOBAL_H__ #define __GLOBAL_H__
/* Magic is the prefix of announcement packages. It is the MD5SUM over the
* string "luksrku v2". This only changes when the protocol that is spoken
* changes. */
#define UDP_MESSAGE_MAGIC (const uint8_t[]){ 0x46, 0xf2, 0xf6, 0xc6, 0x63, 0x12, 0x2e, 0x00, 0xa0, 0x8a, 0xae, 0x42, 0x0c, 0x51, 0xf5, 0x65 }
/* Size in bytes of the PSK that is used for TLS */ /* Size in bytes of the PSK that is used for TLS */
#define PSK_SIZE_BYTES 32 #define PSK_SIZE_BYTES 32

10
msg.h
View File

@ -27,13 +27,19 @@
#include <stdint.h> #include <stdint.h>
#include "global.h" #include "global.h"
/* Magic is the prefix of announcement packages. It is the MD5SUM over the
* string "luksrku v2". This only changes when the protocol that is spoken
* changes. */
#define UDP_MESSAGE_MAGIC_SIZE 16
#define UDP_MESSAGE_MAGIC (const uint8_t[UDP_MESSAGE_MAGIC_SIZE]){ 0x46, 0xf2, 0xf6, 0xc6, 0x63, 0x12, 0x2e, 0x00, 0xa0, 0x8a, 0xae, 0x42, 0x0c, 0x51, 0xf5, 0x65 }
struct udp_query_t { struct udp_query_t {
uint8_t magic[16]; uint8_t magic[UDP_MESSAGE_MAGIC_SIZE];
uint8_t host_uuid[16]; uint8_t host_uuid[16];
} __attribute__ ((packed)); } __attribute__ ((packed));
struct udp_response_t { struct udp_response_t {
uint8_t magic[16]; uint8_t magic[UDP_MESSAGE_MAGIC_SIZE];
} __attribute__ ((packed)); } __attribute__ ((packed));
struct msg_t { struct msg_t {

View File

@ -174,10 +174,11 @@ static void udp_handler_thread(void *vctx) {
while (true) { while (true) {
struct udp_query_t rx_msg; struct udp_query_t rx_msg;
if (!wait_udp_broadcast_message(client->udp_sd, client->port, &rx_msg, sizeof(rx_msg), 1000)) { struct sockaddr_in origin;
if (!wait_udp_query(client->udp_sd, client->port, &rx_msg, &origin, 1000)) {
continue; continue;
} }
fprintf(stderr, "RXED!\n"); fprintf(stderr, "RXED! query\n");
} }
} }

23
udp.c
View File

@ -62,15 +62,11 @@ int create_udp_socket(unsigned int listen_port, bool send_broadcast) {
return sd; return sd;
} }
bool wait_udp_message(int sd, int port, void *data, unsigned int max_length, struct sockaddr_in *source, unsigned int timeout_millis) {
bool wait_udp_broadcast_message(int sd, int port, void *data, unsigned int max_length, unsigned int timeout_millis) {
struct sockaddr_storage src_addr;
socklen_t src_addr_len = sizeof(src_addr);
fprintf(stderr, "RECV...\n"); fprintf(stderr, "RECV...\n");
ssize_t count=recvfrom(sd,data, max_length,0,(struct sockaddr*)&src_addr,&src_addr_len); socklen_t socklen = sizeof(struct sockaddr_in);
fprintf(stderr, "RECV %ld\n", count); ssize_t rx_bytes = recvfrom(sd,data, max_length, 0, (struct sockaddr*)source, &socklen);
fprintf(stderr, "RECV %ld\n", rx_bytes);
return true; return true;
} }
@ -87,3 +83,14 @@ bool send_udp_broadcast_message(int sd, int port, const void *data, unsigned int
} }
return true; return true;
} }
bool wait_udp_query(int sd, int port, struct udp_query_t *query, struct sockaddr_in *source, unsigned int timeout_millis) {
bool rx_successful = wait_udp_message(sd, port, query, sizeof(struct udp_query_t), source, timeout_millis);
if (rx_successful) {
/* Also check if the message contains the correct magic */
if (!memcmp(query->magic, UDP_MESSAGE_MAGIC, UDP_MESSAGE_MAGIC_SIZE)) {
return true;
}
}
return false;
}

4
udp.h
View File

@ -25,11 +25,13 @@
#define __UDP_H__ #define __UDP_H__
#include <stdbool.h> #include <stdbool.h>
#include "msg.h"
/*************** AUTO GENERATED SECTION FOLLOWS ***************/ /*************** AUTO GENERATED SECTION FOLLOWS ***************/
int create_udp_socket(unsigned int listen_port, bool send_broadcast); int create_udp_socket(unsigned int listen_port, bool send_broadcast);
bool wait_udp_broadcast_message(int sd, int port, void *data, unsigned int max_length, unsigned int timeout_millis); bool wait_udp_message(int sd, int port, void *data, unsigned int max_length, struct sockaddr_in *source, unsigned int timeout_millis);
bool send_udp_broadcast_message(int sd, int port, const void *data, unsigned int length); bool send_udp_broadcast_message(int sd, int port, const void *data, unsigned int length);
bool wait_udp_query(int sd, int port, struct udp_query_t *query, struct sockaddr_in *source, unsigned int timeout_millis);
/*************** AUTO GENERATED SECTION ENDS ***************/ /*************** AUTO GENERATED SECTION ENDS ***************/
#endif #endif