Skip to content

Commit

Permalink
NTPClient: ignore invalid timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
fooker committed Feb 3, 2020
1 parent 8883768 commit be8ae17
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions Sming/Core/Network/NtpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,34 @@ void NtpClient::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
uint8_t ver = (versionMode & 0b00111000) >> 3;
uint8_t mode = (versionMode & 0x07);

if(mode == NTP_MODE_SERVER && (ver == NTP_VERSION || ver == (NTP_VERSION - 1))) {
//Most likely a correct NTP packet received.
if(mode != NTP_MODE_SERVER || (ver != NTP_VERSION && ver != (NTP_VERSION - 1))) {
// Got a dodgy packet so treat it as we would a response failure
startTimer(NTP_RESPONSE_TIMEOUT_MS);
return;
}

uint32_t timestamp;
pbuf_copy_partial(buf, &timestamp, sizeof(timestamp), 40); // Copy only timestamp.
timestamp = ntohl(timestamp);
//Most likely a correct NTP packet received.
uint32_t timestamp;
pbuf_copy_partial(buf, &timestamp, sizeof(timestamp), 40); // Copy only timestamp.
timestamp = ntohl(timestamp);

// Unix time starts on Jan 1 1970, subtract 70 years:
uint32_t epoch = timestamp - 0x83AA7E80;
if (timestamp == 0) {
// Got a invalid timestamp - retry
startTimer(NTP_RESPONSE_TIMEOUT_MS);
return;
}

if(autoUpdateSystemClock) {
SystemClock.setTime(epoch, eTZ_UTC); // update systemclock utc value
}
// Unix time starts on Jan 1 1970, subtract 70 years:
uint32_t epoch = timestamp - 0x83AA7E80;

if(delegateCompleted) {
delegateCompleted(*this, epoch);
}
if(autoUpdateSystemClock) {
SystemClock.setTime(epoch, eTZ_UTC); // update systemclock utc value
}

// If auto query is enabled, schedule the next check
setAutoQuery(autoQueryEnabled);
} else {
// Got a dodgy packet so treat it as we would a response failure
startTimer(NTP_RESPONSE_TIMEOUT_MS);
if(delegateCompleted) {
delegateCompleted(*this, epoch);
}

// If auto query is enabled, schedule the next check
setAutoQuery(autoQueryEnabled);
}

0 comments on commit be8ae17

Please sign in to comment.