File tree 1 file changed +9
-3
lines changed
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)
54
54
udp.stop ();
55
55
return 0 ;
56
56
}
57
-
57
+
58
58
uint8_t ntp_packet_buf[NTP_PACKET_SIZE];
59
59
udp.read (ntp_packet_buf, NTP_PACKET_SIZE);
60
60
udp.stop ();
61
61
62
62
unsigned long const highWord = word (ntp_packet_buf[40 ], ntp_packet_buf[41 ]);
63
63
unsigned long const lowWord = word (ntp_packet_buf[42 ], ntp_packet_buf[43 ]);
64
64
unsigned long const secsSince1900 = highWord << 16 | lowWord;
65
+
66
+ /* Check for corrupted NTP response */
67
+ if (secsSince1900 == 0 ) {
68
+ return 0 ;
69
+ }
70
+
65
71
unsigned long const seventyYears = 2208988800UL ;
66
72
unsigned long const epoch = secsSince1900 - seventyYears;
67
73
@@ -75,7 +81,7 @@ unsigned long NTPUtils::getTime(UDP & udp)
75
81
void NTPUtils::sendNTPpacket (UDP & udp)
76
82
{
77
83
uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0 };
78
-
84
+
79
85
ntp_packet_buf[0 ] = 0b11100011 ;
80
86
ntp_packet_buf[1 ] = 0 ;
81
87
ntp_packet_buf[2 ] = 6 ;
@@ -84,7 +90,7 @@ void NTPUtils::sendNTPpacket(UDP & udp)
84
90
ntp_packet_buf[13 ] = 0x4E ;
85
91
ntp_packet_buf[14 ] = 49 ;
86
92
ntp_packet_buf[15 ] = 52 ;
87
-
93
+
88
94
udp.beginPacket (NTP_TIME_SERVER, NTP_TIME_SERVER_PORT);
89
95
udp.write (ntp_packet_buf, NTP_PACKET_SIZE);
90
96
udp.endPacket ();
You can’t perform that action at this time.
0 commit comments