From 9e31c5077d849485edefd05833c0fe5e031cbdd5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 21 Sep 2022 17:38:37 +0200 Subject: [PATCH] lwip: Document 32 bit NTP timestamp converstion to 64b time_t --- components/lwip/apps/sntp/sntp.c | 32 +++++++++++++++++------------ tools/ci/check_copyright_ignore.txt | 1 - 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/components/lwip/apps/sntp/sntp.c b/components/lwip/apps/sntp/sntp.c index 677574d200..ef3bb7c82c 100644 --- a/components/lwip/apps/sntp/sntp.c +++ b/components/lwip/apps/sntp/sntp.c @@ -1,16 +1,8 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -120,6 +112,17 @@ bool sntp_restart(void) void sntp_set_system_time(uint32_t sec, uint32_t us) { + // Note: SNTP/NTP timestamp is defined as 64-bit fixed point int + // in seconds from 1900 (integer part is the first 32 bits) + // which overflows in 2036. + // The lifetime of the NTP timestamps has been extended by convention + // of the MSB bit 0 to span between 1968 and 2104. This is implemented + // in lwip sntp module, so this API returns number of seconds/milliseconds + // representing dates range from 1968 to 2104. + // (see: RFC-4330#section-3 and https://github.com/lwip-tcpip/lwip/blob/239918cc/src/apps/sntp/sntp.c#L129-L134) + // Warning: Here, we convert the 32 bit NTP timestamp to 64 bit representation + // of system time (time_t). This won't work for timestamps in future + // after some time in 2104 struct timeval tv = { .tv_sec = sec, .tv_usec = us }; sntp_sync_time(&tv); } @@ -128,6 +131,9 @@ void sntp_get_system_time(uint32_t *sec, uint32_t *us) { struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; gettimeofday(&tv, NULL); + // Warning: Here, we convert 64 bit representation of system time (time_t) to + // 32 bit NTP timestamp. This won't work for future timestamps after some time in 2104 + // (see: RFC-4330#section-3) *(sec) = tv.tv_sec; *(us) = tv.tv_usec; sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 1a83f09b8f..0ebabda0d5 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -792,7 +792,6 @@ components/log/esp_log_private.h components/log/host_test/log_test/main/log_test.cpp components/log/log_linux.c components/lwip/apps/ping/ping.c -components/lwip/apps/sntp/sntp.c components/lwip/include/apps/dhcpserver/dhcpserver_options.h components/lwip/include/apps/esp_ping.h components/lwip/include/apps/ping/ping.h