From bbcc429f68c7624ada4a24f1a103fc34be8d72f8 Mon Sep 17 00:00:00 2001 From: Chritzel Date: Tue, 11 Apr 2017 17:31:01 +0200 Subject: [PATCH] Added isValid() and modified implementation of forceUpdate() --- NTPClient.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 7b1a4e2..ce90e3a 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -60,6 +60,30 @@ void NTPClient::begin(int port) { this->_udpSetup = true; } +bool NTPClient::isValid(byte * ntpPacket) +{ + //Perform a few validity checks on the packet + if((ntpPacket[0] & 0b11000000) == 0b11000000) //Check for LI=UNSYNC + return false; + + if((ntpPacket[0] & 0b00111000) >> 3 < 0b100) //Check for Version >= 4 + return false; + + if((ntpPacket[0] & 0b00000111) != 0b100) //Check for Mode == Server + return false; + + if((ntpPacket[1] < 1) || (ntpPacket[1] > 15)) //Check for valid Stratum + return false; + + if( ntpPacket[16] == 0 && ntpPacket[17] == 0 && + ntpPacket[18] == 0 && ntpPacket[19] == 0 && + ntpPacket[20] == 0 && ntpPacket[21] == 0 && + ntpPacket[22] == 0 && ntpPacket[22] == 0) //Check for ReferenceTimestamp != 0 + return false; + + return true; +} + bool NTPClient::forceUpdate() { #ifdef DEBUG_NTPClient Serial.println("Update from NTP Server"); @@ -73,14 +97,20 @@ bool NTPClient::forceUpdate() { do { delay ( 10 ); cb = this->_udp->parsePacket(); + + if(cb > 0) + { + this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); + if(!this->isValid(this->_packetBuffer)) + cb = 0; + } + if (timeout > 100) return false; // timeout after 1000 ms timeout++; } while (cb == 0); this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time - this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); - unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); // combine the four bytes (two words) into a long integer