From f2ffa5a92d66b69767c9225616f19679465a55b0 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 24 May 2020 21:23:13 +0200 Subject: [PATCH] The maximum unreliable packet size is 65535 bytes. This matches how real UDP works and avoids 16 bit integer overflow. --- src/utcp.c | 2 +- src/utcp_priv.h | 2 +- test/channels-udp.c | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/utcp.c b/src/utcp.c index dc5a2fcf..c4305cd4 100644 --- a/src/utcp.c +++ b/src/utcp.c @@ -1147,7 +1147,7 @@ static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr, } // Ensure reassembled packet are not larger than 64 kiB - if(hdr->wnd >= MAX_UNRELIABLE_SIZE || hdr->wnd + len > MAX_UNRELIABLE_SIZE) { + if(hdr->wnd > MAX_UNRELIABLE_SIZE || hdr->wnd + len > MAX_UNRELIABLE_SIZE) { return; } diff --git a/src/utcp_priv.h b/src/utcp_priv.h index 197fd268..7ef14778 100644 --- a/src/utcp_priv.h +++ b/src/utcp_priv.h @@ -42,7 +42,7 @@ #define DEFAULT_RCVBUFSIZE 0 #define DEFAULT_MAXRCVBUFSIZE 131072 -#define MAX_UNRELIABLE_SIZE 65536 +#define MAX_UNRELIABLE_SIZE 65535 #define DEFAULT_MTU 1000 #define USEC_PER_SEC 1000000L diff --git a/test/channels-udp.c b/test/channels-udp.c index 4e628c01..f4ddbe2b 100644 --- a/test/channels-udp.c +++ b/test/channels-udp.c @@ -55,10 +55,10 @@ static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *chann // We expect always the same amount of data from the server. assert(mesh->priv); struct client *client = mesh->priv; - assert(len == 512 || len == 65536); + assert(len == 512 || len == 65535); client->received += len; - if(len == 65536) { + if(len == 65535) { client->got_large_packet = true; } } @@ -142,16 +142,15 @@ int main(void) { // Check that we can send up to 65535 bytes without errors - char large_data[65536] = ""; + char large_data[65535] = ""; for(int i = 0; i < 3; i++) { - assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data) + 1) == -1); assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data)) == sizeof(large_data)); } - // Assert that packets larger than 64 kiB are not allowed + // Assert that any larger packets are not allowed - assert(meshlink_channel_send(server, channels[0], large_data, 65537) == -1); + assert(meshlink_channel_send(server, channels[0], large_data, 65536) == -1); // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz) -- 2.39.5