From b1352010c4a9464d4e5716c314e817ec4f8bb155 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 28 Mar 2025 11:03:55 +0100 Subject: [PATCH 1/2] NTP: discard corrupted NTP response --- src/utility/time/NTPUtils.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utility/time/NTPUtils.cpp b/src/utility/time/NTPUtils.cpp index 24c626a73..ae0a482c8 100644 --- a/src/utility/time/NTPUtils.cpp +++ b/src/utility/time/NTPUtils.cpp @@ -62,6 +62,12 @@ unsigned long NTPUtils::getTime(UDP & udp) unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]); unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]); unsigned long const secsSince1900 = highWord << 16 | lowWord; + + /* Check for corrupted NTP response */ + if(secsSince1900 == 0) { + return 0; + } + unsigned long const seventyYears = 2208988800UL; unsigned long const epoch = secsSince1900 - seventyYears; From f6f4468202c84c546c7f0a8d49d8a39d658f3aa2 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 28 Mar 2025 11:04:13 +0100 Subject: [PATCH 2/2] NTP: remove spaces --- src/utility/time/NTPUtils.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utility/time/NTPUtils.cpp b/src/utility/time/NTPUtils.cpp index ae0a482c8..3069ae823 100644 --- a/src/utility/time/NTPUtils.cpp +++ b/src/utility/time/NTPUtils.cpp @@ -54,7 +54,7 @@ unsigned long NTPUtils::getTime(UDP & udp) udp.stop(); return 0; } - + uint8_t ntp_packet_buf[NTP_PACKET_SIZE]; udp.read(ntp_packet_buf, NTP_PACKET_SIZE); udp.stop(); @@ -81,7 +81,7 @@ unsigned long NTPUtils::getTime(UDP & udp) void NTPUtils::sendNTPpacket(UDP & udp) { uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0}; - + ntp_packet_buf[0] = 0b11100011; ntp_packet_buf[1] = 0; ntp_packet_buf[2] = 6; @@ -90,7 +90,7 @@ void NTPUtils::sendNTPpacket(UDP & udp) ntp_packet_buf[13] = 0x4E; ntp_packet_buf[14] = 49; ntp_packet_buf[15] = 52; - + udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT); udp.write(ntp_packet_buf, NTP_PACKET_SIZE); udp.endPacket();