File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff 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)
7581void 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 ();
You can’t perform that action at this time.
0 commit comments