diff --git a/global.h b/global.h index 3d6541b..2ddc470 100644 --- a/global.h +++ b/global.h @@ -24,11 +24,6 @@ #ifndef __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 */ #define PSK_SIZE_BYTES 32 diff --git a/msg.h b/msg.h index c937df4..6174280 100644 --- a/msg.h +++ b/msg.h @@ -27,13 +27,19 @@ #include #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 { - uint8_t magic[16]; + uint8_t magic[UDP_MESSAGE_MAGIC_SIZE]; uint8_t host_uuid[16]; } __attribute__ ((packed)); struct udp_response_t { - uint8_t magic[16]; + uint8_t magic[UDP_MESSAGE_MAGIC_SIZE]; } __attribute__ ((packed)); struct msg_t { diff --git a/server.c b/server.c index bb88461..2f3c698 100644 --- a/server.c +++ b/server.c @@ -174,10 +174,11 @@ static void udp_handler_thread(void *vctx) { while (true) { 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; } - fprintf(stderr, "RXED!\n"); + fprintf(stderr, "RXED! query\n"); } } diff --git a/udp.c b/udp.c index 259e9cb..8228c40 100644 --- a/udp.c +++ b/udp.c @@ -62,15 +62,11 @@ int create_udp_socket(unsigned int listen_port, bool send_broadcast) { return sd; } - -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); - +bool wait_udp_message(int sd, int port, void *data, unsigned int max_length, struct sockaddr_in *source, unsigned int timeout_millis) { fprintf(stderr, "RECV...\n"); - ssize_t count=recvfrom(sd,data, max_length,0,(struct sockaddr*)&src_addr,&src_addr_len); - fprintf(stderr, "RECV %ld\n", count); - + socklen_t socklen = sizeof(struct sockaddr_in); + ssize_t rx_bytes = recvfrom(sd,data, max_length, 0, (struct sockaddr*)source, &socklen); + fprintf(stderr, "RECV %ld\n", rx_bytes); return true; } @@ -87,3 +83,14 @@ bool send_udp_broadcast_message(int sd, int port, const void *data, unsigned int } 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; +} diff --git a/udp.h b/udp.h index 6dcb1d8..c34a489 100644 --- a/udp.h +++ b/udp.h @@ -25,11 +25,13 @@ #define __UDP_H__ #include +#include "msg.h" /*************** AUTO GENERATED SECTION FOLLOWS ***************/ 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 wait_udp_query(int sd, int port, struct udp_query_t *query, struct sockaddr_in *source, unsigned int timeout_millis); /*************** AUTO GENERATED SECTION ENDS ***************/ #endif