Skip to content

Commit 7120482

Browse files
authored
Merge pull request #536 from pennam/ntp-fix
NTP: discard corrupted NTP response
2 parents 3c60e0d + f6f4468 commit 7120482

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/utility/time/NTPUtils.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ unsigned long NTPUtils::getTime(UDP & udp)
5454
udp.stop();
5555
return 0;
5656
}
57-
57+
5858
uint8_t ntp_packet_buf[NTP_PACKET_SIZE];
5959
udp.read(ntp_packet_buf, NTP_PACKET_SIZE);
6060
udp.stop();
6161

6262
unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]);
6363
unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]);
6464
unsigned long const secsSince1900 = highWord << 16 | lowWord;
65+
66+
/* Check for corrupted NTP response */
67+
if(secsSince1900 == 0) {
68+
return 0;
69+
}
70+
6571
unsigned long const seventyYears = 2208988800UL;
6672
unsigned long const epoch = secsSince1900 - seventyYears;
6773

@@ -75,7 +81,7 @@ unsigned long NTPUtils::getTime(UDP & udp)
7581
void NTPUtils::sendNTPpacket(UDP & udp)
7682
{
7783
uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0};
78-
84+
7985
ntp_packet_buf[0] = 0b11100011;
8086
ntp_packet_buf[1] = 0;
8187
ntp_packet_buf[2] = 6;
@@ -84,7 +90,7 @@ void NTPUtils::sendNTPpacket(UDP & udp)
8490
ntp_packet_buf[13] = 0x4E;
8591
ntp_packet_buf[14] = 49;
8692
ntp_packet_buf[15] = 52;
87-
93+
8894
udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT);
8995
udp.write(ntp_packet_buf, NTP_PACKET_SIZE);
9096
udp.endPacket();

0 commit comments

Comments
 (0)