1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * SNTP client module
4195972f6Sopenharmony_ci */
5195972f6Sopenharmony_ci
6195972f6Sopenharmony_ci/*
7195972f6Sopenharmony_ci * Copyright (c) 2007-2009 Frédéric Bernon, Simon Goldschmidt
8195972f6Sopenharmony_ci * All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
11195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
12195972f6Sopenharmony_ci *
13195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
14195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
15195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
16195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
17195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
18195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
19195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
20195972f6Sopenharmony_ci *
21195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30195972f6Sopenharmony_ci * OF SUCH DAMAGE.
31195972f6Sopenharmony_ci *
32195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
33195972f6Sopenharmony_ci *
34195972f6Sopenharmony_ci * Author: Frédéric Bernon, Simon Goldschmidt
35195972f6Sopenharmony_ci */
36195972f6Sopenharmony_ci
37195972f6Sopenharmony_ci
38195972f6Sopenharmony_ci/**
39195972f6Sopenharmony_ci * @defgroup sntp SNTP
40195972f6Sopenharmony_ci * @ingroup apps
41195972f6Sopenharmony_ci *
42195972f6Sopenharmony_ci * This is simple "SNTP" client for the lwIP raw API.
43195972f6Sopenharmony_ci * It is a minimal implementation of SNTPv4 as specified in RFC 4330.
44195972f6Sopenharmony_ci *
45195972f6Sopenharmony_ci * For a list of some public NTP servers, see this link:
46195972f6Sopenharmony_ci * http://support.ntp.org/bin/view/Servers/NTPPoolServers
47195972f6Sopenharmony_ci *
48195972f6Sopenharmony_ci * @todo:
49195972f6Sopenharmony_ci * - complete SNTP_CHECK_RESPONSE checks 3 and 4
50195972f6Sopenharmony_ci */
51195972f6Sopenharmony_ci
52195972f6Sopenharmony_ci#include "lwip/apps/sntp.h"
53195972f6Sopenharmony_ci
54195972f6Sopenharmony_ci#include "lwip/opt.h"
55195972f6Sopenharmony_ci#include "lwip/timeouts.h"
56195972f6Sopenharmony_ci#include "lwip/udp.h"
57195972f6Sopenharmony_ci#include "lwip/dns.h"
58195972f6Sopenharmony_ci#include "lwip/ip_addr.h"
59195972f6Sopenharmony_ci#include "lwip/pbuf.h"
60195972f6Sopenharmony_ci#include "lwip/dhcp.h"
61195972f6Sopenharmony_ci
62195972f6Sopenharmony_ci#include <string.h>
63195972f6Sopenharmony_ci#include <time.h>
64195972f6Sopenharmony_ci
65195972f6Sopenharmony_ci#if LWIP_UDP
66195972f6Sopenharmony_ci
67195972f6Sopenharmony_ci/* Handle support for more than one server via SNTP_MAX_SERVERS */
68195972f6Sopenharmony_ci#if SNTP_MAX_SERVERS > 1
69195972f6Sopenharmony_ci#define SNTP_SUPPORT_MULTIPLE_SERVERS 1
70195972f6Sopenharmony_ci#else /* NTP_MAX_SERVERS > 1 */
71195972f6Sopenharmony_ci#define SNTP_SUPPORT_MULTIPLE_SERVERS 0
72195972f6Sopenharmony_ci#endif /* NTP_MAX_SERVERS > 1 */
73195972f6Sopenharmony_ci
74195972f6Sopenharmony_ci#ifndef SNTP_SUPPRESS_DELAY_CHECK
75195972f6Sopenharmony_ci#if SNTP_UPDATE_DELAY < 15000
76195972f6Sopenharmony_ci#error "SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds (define SNTP_SUPPRESS_DELAY_CHECK to disable this error)!"
77195972f6Sopenharmony_ci#endif
78195972f6Sopenharmony_ci#endif
79195972f6Sopenharmony_ci
80195972f6Sopenharmony_ci/* the various debug levels for this file */
81195972f6Sopenharmony_ci#define SNTP_DEBUG_TRACE        (SNTP_DEBUG | LWIP_DBG_TRACE)
82195972f6Sopenharmony_ci#define SNTP_DEBUG_STATE        (SNTP_DEBUG | LWIP_DBG_STATE)
83195972f6Sopenharmony_ci#define SNTP_DEBUG_WARN         (SNTP_DEBUG | LWIP_DBG_LEVEL_WARNING)
84195972f6Sopenharmony_ci#define SNTP_DEBUG_WARN_STATE   (SNTP_DEBUG | LWIP_DBG_LEVEL_WARNING | LWIP_DBG_STATE)
85195972f6Sopenharmony_ci#define SNTP_DEBUG_SERIOUS      (SNTP_DEBUG | LWIP_DBG_LEVEL_SERIOUS)
86195972f6Sopenharmony_ci
87195972f6Sopenharmony_ci#define SNTP_ERR_KOD                1
88195972f6Sopenharmony_ci
89195972f6Sopenharmony_ci/* SNTP protocol defines */
90195972f6Sopenharmony_ci#define SNTP_MSG_LEN                48
91195972f6Sopenharmony_ci
92195972f6Sopenharmony_ci#define SNTP_OFFSET_LI_VN_MODE      0
93195972f6Sopenharmony_ci#define SNTP_LI_MASK                0xC0
94195972f6Sopenharmony_ci#define SNTP_LI_NO_WARNING          (0x00 << 6)
95195972f6Sopenharmony_ci#define SNTP_LI_LAST_MINUTE_61_SEC  (0x01 << 6)
96195972f6Sopenharmony_ci#define SNTP_LI_LAST_MINUTE_59_SEC  (0x02 << 6)
97195972f6Sopenharmony_ci#define SNTP_LI_ALARM_CONDITION     (0x03 << 6) /* (clock not synchronized) */
98195972f6Sopenharmony_ci
99195972f6Sopenharmony_ci#define SNTP_VERSION_MASK           0x38
100195972f6Sopenharmony_ci#define SNTP_VERSION                (4/* NTP Version 4*/<<3)
101195972f6Sopenharmony_ci
102195972f6Sopenharmony_ci#define SNTP_MODE_MASK              0x07
103195972f6Sopenharmony_ci#define SNTP_MODE_CLIENT            0x03
104195972f6Sopenharmony_ci#define SNTP_MODE_SERVER            0x04
105195972f6Sopenharmony_ci#define SNTP_MODE_BROADCAST         0x05
106195972f6Sopenharmony_ci
107195972f6Sopenharmony_ci#define SNTP_OFFSET_STRATUM         1
108195972f6Sopenharmony_ci#define SNTP_STRATUM_KOD            0x00
109195972f6Sopenharmony_ci
110195972f6Sopenharmony_ci#define SNTP_OFFSET_ORIGINATE_TIME  24
111195972f6Sopenharmony_ci#define SNTP_OFFSET_RECEIVE_TIME    32
112195972f6Sopenharmony_ci#define SNTP_OFFSET_TRANSMIT_TIME   40
113195972f6Sopenharmony_ci
114195972f6Sopenharmony_ci/* Number of seconds between 1970 and Feb 7, 2036 06:28:16 UTC (epoch 1) */
115195972f6Sopenharmony_ci#define DIFF_SEC_1970_2036          ((u32_t)2085978496L)
116195972f6Sopenharmony_ci
117195972f6Sopenharmony_ci/** Convert NTP timestamp fraction to microseconds.
118195972f6Sopenharmony_ci */
119195972f6Sopenharmony_ci#ifndef SNTP_FRAC_TO_US
120195972f6Sopenharmony_ci# if LWIP_HAVE_INT64
121195972f6Sopenharmony_ci#  define SNTP_FRAC_TO_US(f)        ((u32_t)(((u64_t)(f) * 1000000UL) >> 32))
122195972f6Sopenharmony_ci# else
123195972f6Sopenharmony_ci#  define SNTP_FRAC_TO_US(f)        ((u32_t)(f) / 4295)
124195972f6Sopenharmony_ci# endif
125195972f6Sopenharmony_ci#endif /* !SNTP_FRAC_TO_US */
126195972f6Sopenharmony_ci
127195972f6Sopenharmony_ci/* Configure behaviour depending on native, microsecond or second precision.
128195972f6Sopenharmony_ci * Treat NTP timestamps as signed two's-complement integers. This way,
129195972f6Sopenharmony_ci * timestamps that have the MSB set simply become negative offsets from
130195972f6Sopenharmony_ci * the epoch (Feb 7, 2036 06:28:16 UTC). Representable dates range from
131195972f6Sopenharmony_ci * 1968 to 2104.
132195972f6Sopenharmony_ci */
133195972f6Sopenharmony_ci#ifndef SNTP_SET_SYSTEM_TIME_NTP
134195972f6Sopenharmony_ci# ifdef SNTP_SET_SYSTEM_TIME_US
135195972f6Sopenharmony_ci#  define SNTP_SET_SYSTEM_TIME_NTP(s, f) \
136195972f6Sopenharmony_ci    SNTP_SET_SYSTEM_TIME_US((u32_t)((s) + DIFF_SEC_1970_2036), SNTP_FRAC_TO_US(f))
137195972f6Sopenharmony_ci# else
138195972f6Sopenharmony_ci#  define SNTP_SET_SYSTEM_TIME_NTP(s, f) \
139195972f6Sopenharmony_ci    SNTP_SET_SYSTEM_TIME((u32_t)((s) + DIFF_SEC_1970_2036))
140195972f6Sopenharmony_ci# endif
141195972f6Sopenharmony_ci#endif /* !SNTP_SET_SYSTEM_TIME_NTP */
142195972f6Sopenharmony_ci
143195972f6Sopenharmony_ci/* Get the system time either natively as NTP timestamp or convert from
144195972f6Sopenharmony_ci * Unix time in seconds and microseconds. Take care to avoid overflow if the
145195972f6Sopenharmony_ci * microsecond value is at the maximum of 999999. Also add 0.5 us fudge to
146195972f6Sopenharmony_ci * avoid special values like 0, and to mask round-off errors that would
147195972f6Sopenharmony_ci * otherwise break round-trip conversion identity.
148195972f6Sopenharmony_ci */
149195972f6Sopenharmony_ci#ifndef SNTP_GET_SYSTEM_TIME_NTP
150195972f6Sopenharmony_ci# define SNTP_GET_SYSTEM_TIME_NTP(s, f) do { \
151195972f6Sopenharmony_ci    u32_t sec_, usec_; \
152195972f6Sopenharmony_ci    SNTP_GET_SYSTEM_TIME(sec_, usec_); \
153195972f6Sopenharmony_ci    (s) = (s32_t)(sec_ - DIFF_SEC_1970_2036); \
154195972f6Sopenharmony_ci    (f) = usec_ * 4295 - ((usec_ * 2143) >> 16) + 2147; \
155195972f6Sopenharmony_ci  } while (0)
156195972f6Sopenharmony_ci#endif /* !SNTP_GET_SYSTEM_TIME_NTP */
157195972f6Sopenharmony_ci
158195972f6Sopenharmony_ci/* Start offset of the timestamps to extract from the SNTP packet */
159195972f6Sopenharmony_ci#define SNTP_OFFSET_TIMESTAMPS \
160195972f6Sopenharmony_ci    (SNTP_OFFSET_TRANSMIT_TIME + 8 - sizeof(struct sntp_timestamps))
161195972f6Sopenharmony_ci
162195972f6Sopenharmony_ci/* Round-trip delay arithmetic helpers */
163195972f6Sopenharmony_ci#if SNTP_COMP_ROUNDTRIP
164195972f6Sopenharmony_ci# if !LWIP_HAVE_INT64
165195972f6Sopenharmony_ci#  error "SNTP round-trip delay compensation requires 64-bit arithmetic"
166195972f6Sopenharmony_ci# endif
167195972f6Sopenharmony_ci# define SNTP_SEC_FRAC_TO_S64(s, f) \
168195972f6Sopenharmony_ci    ((s64_t)(((u64_t)(s) << 32) | (u32_t)(f)))
169195972f6Sopenharmony_ci# define SNTP_TIMESTAMP_TO_S64(t) \
170195972f6Sopenharmony_ci    SNTP_SEC_FRAC_TO_S64(lwip_ntohl((t).sec), lwip_ntohl((t).frac))
171195972f6Sopenharmony_ci#endif /* SNTP_COMP_ROUNDTRIP */
172195972f6Sopenharmony_ci
173195972f6Sopenharmony_ci/**
174195972f6Sopenharmony_ci * 64-bit NTP timestamp, in network byte order.
175195972f6Sopenharmony_ci */
176195972f6Sopenharmony_cistruct sntp_time {
177195972f6Sopenharmony_ci  u32_t sec;
178195972f6Sopenharmony_ci  u32_t frac;
179195972f6Sopenharmony_ci};
180195972f6Sopenharmony_ci
181195972f6Sopenharmony_ci/**
182195972f6Sopenharmony_ci * Timestamps to be extracted from the NTP header.
183195972f6Sopenharmony_ci */
184195972f6Sopenharmony_cistruct sntp_timestamps {
185195972f6Sopenharmony_ci#if SNTP_COMP_ROUNDTRIP || SNTP_CHECK_RESPONSE >= 2
186195972f6Sopenharmony_ci  struct sntp_time orig;
187195972f6Sopenharmony_ci  struct sntp_time recv;
188195972f6Sopenharmony_ci#endif
189195972f6Sopenharmony_ci  struct sntp_time xmit;
190195972f6Sopenharmony_ci};
191195972f6Sopenharmony_ci
192195972f6Sopenharmony_ci/**
193195972f6Sopenharmony_ci * SNTP packet format (without optional fields)
194195972f6Sopenharmony_ci * Timestamps are coded as 64 bits:
195195972f6Sopenharmony_ci * - signed 32 bits seconds since Feb 07, 2036, 06:28:16 UTC (epoch 1)
196195972f6Sopenharmony_ci * - unsigned 32 bits seconds fraction (2^32 = 1 second)
197195972f6Sopenharmony_ci */
198195972f6Sopenharmony_ci#ifdef PACK_STRUCT_USE_INCLUDES
199195972f6Sopenharmony_ci#  include "arch/bpstruct.h"
200195972f6Sopenharmony_ci#endif
201195972f6Sopenharmony_ciPACK_STRUCT_BEGIN
202195972f6Sopenharmony_cistruct sntp_msg {
203195972f6Sopenharmony_ci  PACK_STRUCT_FLD_8(u8_t  li_vn_mode);
204195972f6Sopenharmony_ci  PACK_STRUCT_FLD_8(u8_t  stratum);
205195972f6Sopenharmony_ci  PACK_STRUCT_FLD_8(u8_t  poll);
206195972f6Sopenharmony_ci  PACK_STRUCT_FLD_8(u8_t  precision);
207195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t root_delay);
208195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t root_dispersion);
209195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t reference_identifier);
210195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t reference_timestamp[2]);
211195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t originate_timestamp[2]);
212195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t receive_timestamp[2]);
213195972f6Sopenharmony_ci  PACK_STRUCT_FIELD(u32_t transmit_timestamp[2]);
214195972f6Sopenharmony_ci} PACK_STRUCT_STRUCT;
215195972f6Sopenharmony_ciPACK_STRUCT_END
216195972f6Sopenharmony_ci#ifdef PACK_STRUCT_USE_INCLUDES
217195972f6Sopenharmony_ci#  include "arch/epstruct.h"
218195972f6Sopenharmony_ci#endif
219195972f6Sopenharmony_ci
220195972f6Sopenharmony_ci/* function prototypes */
221195972f6Sopenharmony_cistatic void sntp_request(void *arg);
222195972f6Sopenharmony_ci
223195972f6Sopenharmony_ci/** The operating mode */
224195972f6Sopenharmony_cistatic u8_t sntp_opmode;
225195972f6Sopenharmony_ci
226195972f6Sopenharmony_ci/** The UDP pcb used by the SNTP client */
227195972f6Sopenharmony_cistatic struct udp_pcb *sntp_pcb;
228195972f6Sopenharmony_ci/** Names/Addresses of servers */
229195972f6Sopenharmony_cistruct sntp_server {
230195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
231195972f6Sopenharmony_ci  const char *name;
232195972f6Sopenharmony_ci#endif /* SNTP_SERVER_DNS */
233195972f6Sopenharmony_ci  ip_addr_t addr;
234195972f6Sopenharmony_ci#if SNTP_MONITOR_SERVER_REACHABILITY
235195972f6Sopenharmony_ci  /** Reachability shift register as described in RFC 5905 */
236195972f6Sopenharmony_ci  u8_t reachability;
237195972f6Sopenharmony_ci#endif /* SNTP_MONITOR_SERVER_REACHABILITY */
238195972f6Sopenharmony_ci};
239195972f6Sopenharmony_cistatic struct sntp_server sntp_servers[SNTP_MAX_SERVERS];
240195972f6Sopenharmony_ci
241195972f6Sopenharmony_ci#if SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6
242195972f6Sopenharmony_cistatic u8_t sntp_set_servers_from_dhcp;
243195972f6Sopenharmony_ci#endif /* SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6 */
244195972f6Sopenharmony_ci#if SNTP_SUPPORT_MULTIPLE_SERVERS
245195972f6Sopenharmony_ci/** The currently used server (initialized to 0) */
246195972f6Sopenharmony_cistatic u8_t sntp_current_server;
247195972f6Sopenharmony_ci#else /* SNTP_SUPPORT_MULTIPLE_SERVERS */
248195972f6Sopenharmony_ci#define sntp_current_server 0
249195972f6Sopenharmony_ci#endif /* SNTP_SUPPORT_MULTIPLE_SERVERS */
250195972f6Sopenharmony_ci
251195972f6Sopenharmony_ci#if SNTP_RETRY_TIMEOUT_EXP
252195972f6Sopenharmony_ci#define SNTP_RESET_RETRY_TIMEOUT() sntp_retry_timeout = SNTP_RETRY_TIMEOUT
253195972f6Sopenharmony_ci/** Retry time, initialized with SNTP_RETRY_TIMEOUT and doubled with each retry. */
254195972f6Sopenharmony_cistatic u32_t sntp_retry_timeout;
255195972f6Sopenharmony_ci#else /* SNTP_RETRY_TIMEOUT_EXP */
256195972f6Sopenharmony_ci#define SNTP_RESET_RETRY_TIMEOUT()
257195972f6Sopenharmony_ci#define sntp_retry_timeout SNTP_RETRY_TIMEOUT
258195972f6Sopenharmony_ci#endif /* SNTP_RETRY_TIMEOUT_EXP */
259195972f6Sopenharmony_ci
260195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 1
261195972f6Sopenharmony_ci/** Saves the last server address to compare with response */
262195972f6Sopenharmony_cistatic ip_addr_t sntp_last_server_address;
263195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 1 */
264195972f6Sopenharmony_ci
265195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 2
266195972f6Sopenharmony_ci/** Saves the last timestamp sent (which is sent back by the server)
267195972f6Sopenharmony_ci * to compare against in response. Stored in network byte order. */
268195972f6Sopenharmony_cistatic struct sntp_time sntp_last_timestamp_sent;
269195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 2 */
270195972f6Sopenharmony_ci
271195972f6Sopenharmony_ci#if defined(LWIP_DEBUG) && !defined(sntp_format_time)
272195972f6Sopenharmony_ci/* Debug print helper. */
273195972f6Sopenharmony_cistatic const char *
274195972f6Sopenharmony_cisntp_format_time(s32_t sec)
275195972f6Sopenharmony_ci{
276195972f6Sopenharmony_ci  time_t ut;
277195972f6Sopenharmony_ci  ut = (u32_t)((u32_t)sec + DIFF_SEC_1970_2036);
278195972f6Sopenharmony_ci  return ctime(&ut);
279195972f6Sopenharmony_ci}
280195972f6Sopenharmony_ci#endif /* LWIP_DEBUG && !sntp_format_time */
281195972f6Sopenharmony_ci
282195972f6Sopenharmony_ci/**
283195972f6Sopenharmony_ci * SNTP processing of received timestamp
284195972f6Sopenharmony_ci */
285195972f6Sopenharmony_cistatic void
286195972f6Sopenharmony_cisntp_process(const struct sntp_timestamps *timestamps)
287195972f6Sopenharmony_ci{
288195972f6Sopenharmony_ci  s32_t sec;
289195972f6Sopenharmony_ci  u32_t frac;
290195972f6Sopenharmony_ci
291195972f6Sopenharmony_ci  sec  = (s32_t)lwip_ntohl(timestamps->xmit.sec);
292195972f6Sopenharmony_ci  frac = lwip_ntohl(timestamps->xmit.frac);
293195972f6Sopenharmony_ci
294195972f6Sopenharmony_ci#if SNTP_COMP_ROUNDTRIP
295195972f6Sopenharmony_ci# if SNTP_CHECK_RESPONSE >= 2
296195972f6Sopenharmony_ci  if (timestamps->recv.sec != 0 || timestamps->recv.frac != 0)
297195972f6Sopenharmony_ci# endif
298195972f6Sopenharmony_ci  {
299195972f6Sopenharmony_ci    s32_t dest_sec;
300195972f6Sopenharmony_ci    u32_t dest_frac;
301195972f6Sopenharmony_ci    u32_t step_sec;
302195972f6Sopenharmony_ci
303195972f6Sopenharmony_ci    /* Get the destination time stamp, i.e. the current system time */
304195972f6Sopenharmony_ci    SNTP_GET_SYSTEM_TIME_NTP(dest_sec, dest_frac);
305195972f6Sopenharmony_ci
306195972f6Sopenharmony_ci    step_sec = (dest_sec < sec) ? ((u32_t)sec - (u32_t)dest_sec)
307195972f6Sopenharmony_ci               : ((u32_t)dest_sec - (u32_t)sec);
308195972f6Sopenharmony_ci    /* In order to avoid overflows, skip the compensation if the clock step
309195972f6Sopenharmony_ci     * is larger than about 34 years. */
310195972f6Sopenharmony_ci    if ((step_sec >> 30) == 0) {
311195972f6Sopenharmony_ci      s64_t t1, t2, t3, t4;
312195972f6Sopenharmony_ci
313195972f6Sopenharmony_ci      t4 = SNTP_SEC_FRAC_TO_S64(dest_sec, dest_frac);
314195972f6Sopenharmony_ci      t3 = SNTP_SEC_FRAC_TO_S64(sec, frac);
315195972f6Sopenharmony_ci      t1 = SNTP_TIMESTAMP_TO_S64(timestamps->orig);
316195972f6Sopenharmony_ci      t2 = SNTP_TIMESTAMP_TO_S64(timestamps->recv);
317195972f6Sopenharmony_ci      /* Clock offset calculation according to RFC 4330 */
318195972f6Sopenharmony_ci      t4 += ((t2 - t1) + (t3 - t4)) / 2;
319195972f6Sopenharmony_ci
320195972f6Sopenharmony_ci      sec  = (s32_t)((u64_t)t4 >> 32);
321195972f6Sopenharmony_ci      frac = (u32_t)((u64_t)t4);
322195972f6Sopenharmony_ci    }
323195972f6Sopenharmony_ci  }
324195972f6Sopenharmony_ci#endif /* SNTP_COMP_ROUNDTRIP */
325195972f6Sopenharmony_ci
326195972f6Sopenharmony_ci  SNTP_SET_SYSTEM_TIME_NTP(sec, frac);
327195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(frac); /* might be unused if only seconds are set */
328195972f6Sopenharmony_ci  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %" U32_F " us\n",
329195972f6Sopenharmony_ci                                 sntp_format_time(sec), SNTP_FRAC_TO_US(frac)));
330195972f6Sopenharmony_ci}
331195972f6Sopenharmony_ci
332195972f6Sopenharmony_ci/**
333195972f6Sopenharmony_ci * Initialize request struct to be sent to server.
334195972f6Sopenharmony_ci */
335195972f6Sopenharmony_cistatic void
336195972f6Sopenharmony_cisntp_initialize_request(struct sntp_msg *req)
337195972f6Sopenharmony_ci{
338195972f6Sopenharmony_ci  memset(req, 0, SNTP_MSG_LEN);
339195972f6Sopenharmony_ci  req->li_vn_mode = SNTP_LI_NO_WARNING | SNTP_VERSION | SNTP_MODE_CLIENT;
340195972f6Sopenharmony_ci
341195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 2 || SNTP_COMP_ROUNDTRIP
342195972f6Sopenharmony_ci  {
343195972f6Sopenharmony_ci    s32_t secs;
344195972f6Sopenharmony_ci    u32_t sec, frac;
345195972f6Sopenharmony_ci    /* Get the transmit timestamp */
346195972f6Sopenharmony_ci    SNTP_GET_SYSTEM_TIME_NTP(secs, frac);
347195972f6Sopenharmony_ci    sec  = lwip_htonl((u32_t)secs);
348195972f6Sopenharmony_ci    frac = lwip_htonl(frac);
349195972f6Sopenharmony_ci
350195972f6Sopenharmony_ci# if SNTP_CHECK_RESPONSE >= 2
351195972f6Sopenharmony_ci    sntp_last_timestamp_sent.sec  = sec;
352195972f6Sopenharmony_ci    sntp_last_timestamp_sent.frac = frac;
353195972f6Sopenharmony_ci# endif
354195972f6Sopenharmony_ci    req->transmit_timestamp[0] = sec;
355195972f6Sopenharmony_ci    req->transmit_timestamp[1] = frac;
356195972f6Sopenharmony_ci  }
357195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 2 || SNTP_COMP_ROUNDTRIP */
358195972f6Sopenharmony_ci}
359195972f6Sopenharmony_ci
360195972f6Sopenharmony_ci/**
361195972f6Sopenharmony_ci * Retry: send a new request (and increase retry timeout).
362195972f6Sopenharmony_ci *
363195972f6Sopenharmony_ci * @param arg is unused (only necessary to conform to sys_timeout)
364195972f6Sopenharmony_ci */
365195972f6Sopenharmony_cistatic void
366195972f6Sopenharmony_cisntp_retry(void *arg)
367195972f6Sopenharmony_ci{
368195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
369195972f6Sopenharmony_ci
370195972f6Sopenharmony_ci  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_retry: Next request will be sent in %"U32_F" ms\n",
371195972f6Sopenharmony_ci                                 sntp_retry_timeout));
372195972f6Sopenharmony_ci
373195972f6Sopenharmony_ci  /* set up a timer to send a retry and increase the retry delay */
374195972f6Sopenharmony_ci  sys_untimeout(sntp_request, NULL);
375195972f6Sopenharmony_ci  sys_timeout(sntp_retry_timeout, sntp_request, NULL);
376195972f6Sopenharmony_ci
377195972f6Sopenharmony_ci#if SNTP_RETRY_TIMEOUT_EXP
378195972f6Sopenharmony_ci  {
379195972f6Sopenharmony_ci    u32_t new_retry_timeout;
380195972f6Sopenharmony_ci    /* increase the timeout for next retry */
381195972f6Sopenharmony_ci    new_retry_timeout = sntp_retry_timeout << 1;
382195972f6Sopenharmony_ci    /* limit to maximum timeout and prevent overflow */
383195972f6Sopenharmony_ci    if ((new_retry_timeout <= SNTP_RETRY_TIMEOUT_MAX) &&
384195972f6Sopenharmony_ci        (new_retry_timeout > sntp_retry_timeout)) {
385195972f6Sopenharmony_ci      sntp_retry_timeout = new_retry_timeout;
386195972f6Sopenharmony_ci    } else {
387195972f6Sopenharmony_ci      sntp_retry_timeout = SNTP_RETRY_TIMEOUT_MAX;
388195972f6Sopenharmony_ci    }
389195972f6Sopenharmony_ci  }
390195972f6Sopenharmony_ci#endif /* SNTP_RETRY_TIMEOUT_EXP */
391195972f6Sopenharmony_ci}
392195972f6Sopenharmony_ci
393195972f6Sopenharmony_ci#if SNTP_SUPPORT_MULTIPLE_SERVERS
394195972f6Sopenharmony_ci/**
395195972f6Sopenharmony_ci * If Kiss-of-Death is received (or another packet parsing error),
396195972f6Sopenharmony_ci * try the next server or retry the current server and increase the retry
397195972f6Sopenharmony_ci * timeout if only one server is available.
398195972f6Sopenharmony_ci * (implicitly, SNTP_MAX_SERVERS > 1)
399195972f6Sopenharmony_ci *
400195972f6Sopenharmony_ci * @param arg is unused (only necessary to conform to sys_timeout)
401195972f6Sopenharmony_ci */
402195972f6Sopenharmony_cistatic void
403195972f6Sopenharmony_cisntp_try_next_server(void *arg)
404195972f6Sopenharmony_ci{
405195972f6Sopenharmony_ci  u8_t old_server, i;
406195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
407195972f6Sopenharmony_ci
408195972f6Sopenharmony_ci  old_server = sntp_current_server;
409195972f6Sopenharmony_ci  for (i = 0; i < SNTP_MAX_SERVERS - 1; i++) {
410195972f6Sopenharmony_ci    sntp_current_server++;
411195972f6Sopenharmony_ci    if (sntp_current_server >= SNTP_MAX_SERVERS) {
412195972f6Sopenharmony_ci      sntp_current_server = 0;
413195972f6Sopenharmony_ci    }
414195972f6Sopenharmony_ci    if (!ip_addr_isany(&sntp_servers[sntp_current_server].addr)
415195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
416195972f6Sopenharmony_ci        || (sntp_servers[sntp_current_server].name != NULL)
417195972f6Sopenharmony_ci#endif
418195972f6Sopenharmony_ci       ) {
419195972f6Sopenharmony_ci      LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_try_next_server: Sending request to server %"U16_F"\n",
420195972f6Sopenharmony_ci                                     (u16_t)sntp_current_server));
421195972f6Sopenharmony_ci      /* new server: reset retry timeout */
422195972f6Sopenharmony_ci      SNTP_RESET_RETRY_TIMEOUT();
423195972f6Sopenharmony_ci      /* instantly send a request to the next server */
424195972f6Sopenharmony_ci      sntp_request(NULL);
425195972f6Sopenharmony_ci      return;
426195972f6Sopenharmony_ci    }
427195972f6Sopenharmony_ci  }
428195972f6Sopenharmony_ci  /* no other valid server found */
429195972f6Sopenharmony_ci  sntp_current_server = old_server;
430195972f6Sopenharmony_ci  sntp_retry(NULL);
431195972f6Sopenharmony_ci}
432195972f6Sopenharmony_ci#else /* SNTP_SUPPORT_MULTIPLE_SERVERS */
433195972f6Sopenharmony_ci/* Always retry on error if only one server is supported */
434195972f6Sopenharmony_ci#define sntp_try_next_server    sntp_retry
435195972f6Sopenharmony_ci#endif /* SNTP_SUPPORT_MULTIPLE_SERVERS */
436195972f6Sopenharmony_ci
437195972f6Sopenharmony_ci/** UDP recv callback for the sntp pcb */
438195972f6Sopenharmony_cistatic void
439195972f6Sopenharmony_cisntp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
440195972f6Sopenharmony_ci{
441195972f6Sopenharmony_ci  struct sntp_timestamps timestamps;
442195972f6Sopenharmony_ci  u8_t mode;
443195972f6Sopenharmony_ci  u8_t stratum;
444195972f6Sopenharmony_ci  err_t err;
445195972f6Sopenharmony_ci
446195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
447195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(pcb);
448195972f6Sopenharmony_ci
449195972f6Sopenharmony_ci  err = ERR_ARG;
450195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 1
451195972f6Sopenharmony_ci  /* check server address and port */
452195972f6Sopenharmony_ci  if (((sntp_opmode != SNTP_OPMODE_POLL) || ip_addr_cmp(addr, &sntp_last_server_address)) &&
453195972f6Sopenharmony_ci      (port == SNTP_PORT))
454195972f6Sopenharmony_ci#else /* SNTP_CHECK_RESPONSE >= 1 */
455195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(addr);
456195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(port);
457195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 1 */
458195972f6Sopenharmony_ci  {
459195972f6Sopenharmony_ci    /* process the response */
460195972f6Sopenharmony_ci    if (p->tot_len == SNTP_MSG_LEN) {
461195972f6Sopenharmony_ci      mode = pbuf_get_at(p, SNTP_OFFSET_LI_VN_MODE) & SNTP_MODE_MASK;
462195972f6Sopenharmony_ci      /* if this is a SNTP response... */
463195972f6Sopenharmony_ci      if (((sntp_opmode == SNTP_OPMODE_POLL)       && (mode == SNTP_MODE_SERVER)) ||
464195972f6Sopenharmony_ci          ((sntp_opmode == SNTP_OPMODE_LISTENONLY) && (mode == SNTP_MODE_BROADCAST))) {
465195972f6Sopenharmony_ci        stratum = pbuf_get_at(p, SNTP_OFFSET_STRATUM);
466195972f6Sopenharmony_ci
467195972f6Sopenharmony_ci        if (stratum == SNTP_STRATUM_KOD) {
468195972f6Sopenharmony_ci          /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
469195972f6Sopenharmony_ci          err = SNTP_ERR_KOD;
470195972f6Sopenharmony_ci          LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Received Kiss-of-Death\n"));
471195972f6Sopenharmony_ci        } else {
472195972f6Sopenharmony_ci          pbuf_copy_partial(p, &timestamps, sizeof(timestamps), SNTP_OFFSET_TIMESTAMPS);
473195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 2
474195972f6Sopenharmony_ci          /* check originate_timetamp against sntp_last_timestamp_sent */
475195972f6Sopenharmony_ci          if (timestamps.orig.sec != sntp_last_timestamp_sent.sec ||
476195972f6Sopenharmony_ci              timestamps.orig.frac != sntp_last_timestamp_sent.frac) {
477195972f6Sopenharmony_ci            LWIP_DEBUGF(SNTP_DEBUG_WARN,
478195972f6Sopenharmony_ci                        ("sntp_recv: Invalid originate timestamp in response\n"));
479195972f6Sopenharmony_ci          } else
480195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 2 */
481195972f6Sopenharmony_ci            /* @todo: add code for SNTP_CHECK_RESPONSE >= 3 and >= 4 here */
482195972f6Sopenharmony_ci          {
483195972f6Sopenharmony_ci            /* correct answer */
484195972f6Sopenharmony_ci            err = ERR_OK;
485195972f6Sopenharmony_ci          }
486195972f6Sopenharmony_ci        }
487195972f6Sopenharmony_ci      } else {
488195972f6Sopenharmony_ci        LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid mode in response: %"U16_F"\n", (u16_t)mode));
489195972f6Sopenharmony_ci        /* wait for correct response */
490195972f6Sopenharmony_ci        err = ERR_TIMEOUT;
491195972f6Sopenharmony_ci      }
492195972f6Sopenharmony_ci    } else {
493195972f6Sopenharmony_ci      LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid packet length: %"U16_F"\n", p->tot_len));
494195972f6Sopenharmony_ci    }
495195972f6Sopenharmony_ci  }
496195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 1
497195972f6Sopenharmony_ci  else {
498195972f6Sopenharmony_ci    /* packet from wrong remote address or port, wait for correct response */
499195972f6Sopenharmony_ci    err = ERR_TIMEOUT;
500195972f6Sopenharmony_ci  }
501195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 1 */
502195972f6Sopenharmony_ci
503195972f6Sopenharmony_ci  pbuf_free(p);
504195972f6Sopenharmony_ci
505195972f6Sopenharmony_ci  if (err == ERR_OK) {
506195972f6Sopenharmony_ci    /* correct packet received: process it it */
507195972f6Sopenharmony_ci    sntp_process(&timestamps);
508195972f6Sopenharmony_ci
509195972f6Sopenharmony_ci#if SNTP_MONITOR_SERVER_REACHABILITY
510195972f6Sopenharmony_ci    /* indicate that server responded */
511195972f6Sopenharmony_ci    sntp_servers[sntp_current_server].reachability |= 1;
512195972f6Sopenharmony_ci#endif /* SNTP_MONITOR_SERVER_REACHABILITY */
513195972f6Sopenharmony_ci    /* Set up timeout for next request (only if poll response was received)*/
514195972f6Sopenharmony_ci    if (sntp_opmode == SNTP_OPMODE_POLL) {
515195972f6Sopenharmony_ci      u32_t sntp_update_delay;
516195972f6Sopenharmony_ci      sys_untimeout(sntp_try_next_server, NULL);
517195972f6Sopenharmony_ci      sys_untimeout(sntp_request, NULL);
518195972f6Sopenharmony_ci
519195972f6Sopenharmony_ci      /* Correct response, reset retry timeout */
520195972f6Sopenharmony_ci      SNTP_RESET_RETRY_TIMEOUT();
521195972f6Sopenharmony_ci
522195972f6Sopenharmony_ci      sntp_update_delay = (u32_t)SNTP_UPDATE_DELAY;
523195972f6Sopenharmony_ci      sys_timeout(sntp_update_delay, sntp_request, NULL);
524195972f6Sopenharmony_ci      LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Scheduled next time request: %"U32_F" ms\n",
525195972f6Sopenharmony_ci                                     sntp_update_delay));
526195972f6Sopenharmony_ci    }
527195972f6Sopenharmony_ci  } else if (err == SNTP_ERR_KOD) {
528195972f6Sopenharmony_ci    /* KOD errors are only processed in case of an explicit poll response */
529195972f6Sopenharmony_ci    if (sntp_opmode == SNTP_OPMODE_POLL) {
530195972f6Sopenharmony_ci      /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
531195972f6Sopenharmony_ci      sntp_try_next_server(NULL);
532195972f6Sopenharmony_ci    }
533195972f6Sopenharmony_ci  } else {
534195972f6Sopenharmony_ci    /* ignore any broken packet, poll mode: retry after timeout to avoid flooding */
535195972f6Sopenharmony_ci  }
536195972f6Sopenharmony_ci}
537195972f6Sopenharmony_ci
538195972f6Sopenharmony_ci/** Actually send an sntp request to a server.
539195972f6Sopenharmony_ci *
540195972f6Sopenharmony_ci * @param server_addr resolved IP address of the SNTP server
541195972f6Sopenharmony_ci */
542195972f6Sopenharmony_cistatic void
543195972f6Sopenharmony_cisntp_send_request(const ip_addr_t *server_addr)
544195972f6Sopenharmony_ci{
545195972f6Sopenharmony_ci  struct pbuf *p;
546195972f6Sopenharmony_ci
547195972f6Sopenharmony_ci  LWIP_ASSERT("server_addr != NULL", server_addr != NULL);
548195972f6Sopenharmony_ci
549195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_TRANSPORT, SNTP_MSG_LEN, PBUF_RAM);
550195972f6Sopenharmony_ci  if (p != NULL) {
551195972f6Sopenharmony_ci    struct sntp_msg *sntpmsg = (struct sntp_msg *)p->payload;
552195972f6Sopenharmony_ci    LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_send_request: Sending request to server\n"));
553195972f6Sopenharmony_ci    /* initialize request message */
554195972f6Sopenharmony_ci    sntp_initialize_request(sntpmsg);
555195972f6Sopenharmony_ci    /* send request */
556195972f6Sopenharmony_ci    udp_sendto(sntp_pcb, p, server_addr, SNTP_PORT);
557195972f6Sopenharmony_ci    /* free the pbuf after sending it */
558195972f6Sopenharmony_ci    pbuf_free(p);
559195972f6Sopenharmony_ci#if SNTP_MONITOR_SERVER_REACHABILITY
560195972f6Sopenharmony_ci    /* indicate new packet has been sent */
561195972f6Sopenharmony_ci    sntp_servers[sntp_current_server].reachability <<= 1;
562195972f6Sopenharmony_ci#endif /* SNTP_MONITOR_SERVER_REACHABILITY */
563195972f6Sopenharmony_ci    /* set up receive timeout: try next server or retry on timeout */
564195972f6Sopenharmony_ci    sys_untimeout(sntp_try_next_server, NULL);
565195972f6Sopenharmony_ci    sys_timeout((u32_t)SNTP_RECV_TIMEOUT, sntp_try_next_server, NULL);
566195972f6Sopenharmony_ci#if SNTP_CHECK_RESPONSE >= 1
567195972f6Sopenharmony_ci    /* save server address to verify it in sntp_recv */
568195972f6Sopenharmony_ci    ip_addr_copy(sntp_last_server_address, *server_addr);
569195972f6Sopenharmony_ci#endif /* SNTP_CHECK_RESPONSE >= 1 */
570195972f6Sopenharmony_ci  } else {
571195972f6Sopenharmony_ci    LWIP_DEBUGF(SNTP_DEBUG_SERIOUS, ("sntp_send_request: Out of memory, trying again in %"U32_F" ms\n",
572195972f6Sopenharmony_ci                                     (u32_t)SNTP_RETRY_TIMEOUT));
573195972f6Sopenharmony_ci    /* out of memory: set up a timer to send a retry */
574195972f6Sopenharmony_ci    sys_untimeout(sntp_request, NULL);
575195972f6Sopenharmony_ci    sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_request, NULL);
576195972f6Sopenharmony_ci  }
577195972f6Sopenharmony_ci}
578195972f6Sopenharmony_ci
579195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
580195972f6Sopenharmony_ci/**
581195972f6Sopenharmony_ci * DNS found callback when using DNS names as server address.
582195972f6Sopenharmony_ci */
583195972f6Sopenharmony_cistatic void
584195972f6Sopenharmony_cisntp_dns_found(const char *hostname, const ip_addr_t *ipaddr, void *arg)
585195972f6Sopenharmony_ci{
586195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(hostname);
587195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
588195972f6Sopenharmony_ci
589195972f6Sopenharmony_ci  if (ipaddr != NULL) {
590195972f6Sopenharmony_ci    /* Address resolved, send request */
591195972f6Sopenharmony_ci    LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_dns_found: Server address resolved, sending request\n"));
592195972f6Sopenharmony_ci    sntp_servers[sntp_current_server].addr = *ipaddr;
593195972f6Sopenharmony_ci    sntp_send_request(ipaddr);
594195972f6Sopenharmony_ci  } else {
595195972f6Sopenharmony_ci    /* DNS resolving failed -> try another server */
596195972f6Sopenharmony_ci    LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, ("sntp_dns_found: Failed to resolve server address resolved, trying next server\n"));
597195972f6Sopenharmony_ci    sntp_try_next_server(NULL);
598195972f6Sopenharmony_ci  }
599195972f6Sopenharmony_ci}
600195972f6Sopenharmony_ci#endif /* SNTP_SERVER_DNS */
601195972f6Sopenharmony_ci
602195972f6Sopenharmony_ci/**
603195972f6Sopenharmony_ci * Send out an sntp request.
604195972f6Sopenharmony_ci *
605195972f6Sopenharmony_ci * @param arg is unused (only necessary to conform to sys_timeout)
606195972f6Sopenharmony_ci */
607195972f6Sopenharmony_cistatic void
608195972f6Sopenharmony_cisntp_request(void *arg)
609195972f6Sopenharmony_ci{
610195972f6Sopenharmony_ci  ip_addr_t sntp_server_address;
611195972f6Sopenharmony_ci  err_t err;
612195972f6Sopenharmony_ci
613195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
614195972f6Sopenharmony_ci
615195972f6Sopenharmony_ci  /* initialize SNTP server address */
616195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
617195972f6Sopenharmony_ci  if (sntp_servers[sntp_current_server].name) {
618195972f6Sopenharmony_ci    /* always resolve the name and rely on dns-internal caching & timeout */
619195972f6Sopenharmony_ci    ip_addr_set_zero(&sntp_servers[sntp_current_server].addr);
620195972f6Sopenharmony_ci    err = dns_gethostbyname(sntp_servers[sntp_current_server].name, &sntp_server_address,
621195972f6Sopenharmony_ci                            sntp_dns_found, NULL);
622195972f6Sopenharmony_ci    if (err == ERR_INPROGRESS) {
623195972f6Sopenharmony_ci      /* DNS request sent, wait for sntp_dns_found being called */
624195972f6Sopenharmony_ci      LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_request: Waiting for server address to be resolved.\n"));
625195972f6Sopenharmony_ci      return;
626195972f6Sopenharmony_ci    } else if (err == ERR_OK) {
627195972f6Sopenharmony_ci      sntp_servers[sntp_current_server].addr = sntp_server_address;
628195972f6Sopenharmony_ci    }
629195972f6Sopenharmony_ci  } else
630195972f6Sopenharmony_ci#endif /* SNTP_SERVER_DNS */
631195972f6Sopenharmony_ci  {
632195972f6Sopenharmony_ci    sntp_server_address = sntp_servers[sntp_current_server].addr;
633195972f6Sopenharmony_ci    err = (ip_addr_isany_val(sntp_server_address)) ? ERR_ARG : ERR_OK;
634195972f6Sopenharmony_ci  }
635195972f6Sopenharmony_ci
636195972f6Sopenharmony_ci  if (err == ERR_OK) {
637195972f6Sopenharmony_ci    LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_request: current server address is %s\n",
638195972f6Sopenharmony_ci                                   ipaddr_ntoa(&sntp_server_address)));
639195972f6Sopenharmony_ci    sntp_send_request(&sntp_server_address);
640195972f6Sopenharmony_ci  } else {
641195972f6Sopenharmony_ci    /* address conversion failed, try another server */
642195972f6Sopenharmony_ci    LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, ("sntp_request: Invalid server address, trying next server.\n"));
643195972f6Sopenharmony_ci    sys_untimeout(sntp_try_next_server, NULL);
644195972f6Sopenharmony_ci    sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_try_next_server, NULL);
645195972f6Sopenharmony_ci  }
646195972f6Sopenharmony_ci}
647195972f6Sopenharmony_ci
648195972f6Sopenharmony_ci/**
649195972f6Sopenharmony_ci * @ingroup sntp
650195972f6Sopenharmony_ci * Initialize this module.
651195972f6Sopenharmony_ci * Send out request instantly or after SNTP_STARTUP_DELAY(_FUNC).
652195972f6Sopenharmony_ci */
653195972f6Sopenharmony_civoid
654195972f6Sopenharmony_cisntp_init(void)
655195972f6Sopenharmony_ci{
656195972f6Sopenharmony_ci  /* LWIP_ASSERT_CORE_LOCKED(); is checked by udp_new() */
657195972f6Sopenharmony_ci  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_init: SNTP initialised\n"));
658195972f6Sopenharmony_ci
659195972f6Sopenharmony_ci#ifdef SNTP_SERVER_ADDRESS
660195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
661195972f6Sopenharmony_ci  sntp_setservername(0, SNTP_SERVER_ADDRESS);
662195972f6Sopenharmony_ci#else
663195972f6Sopenharmony_ci#error SNTP_SERVER_ADDRESS string not supported SNTP_SERVER_DNS==0
664195972f6Sopenharmony_ci#endif
665195972f6Sopenharmony_ci#endif /* SNTP_SERVER_ADDRESS */
666195972f6Sopenharmony_ci
667195972f6Sopenharmony_ci  if (sntp_pcb == NULL) {
668195972f6Sopenharmony_ci    sntp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
669195972f6Sopenharmony_ci    LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);
670195972f6Sopenharmony_ci    if (sntp_pcb != NULL) {
671195972f6Sopenharmony_ci      udp_recv(sntp_pcb, sntp_recv, NULL);
672195972f6Sopenharmony_ci
673195972f6Sopenharmony_ci      if (sntp_opmode == SNTP_OPMODE_POLL) {
674195972f6Sopenharmony_ci        SNTP_RESET_RETRY_TIMEOUT();
675195972f6Sopenharmony_ci#if SNTP_STARTUP_DELAY
676195972f6Sopenharmony_ci        sys_timeout((u32_t)SNTP_STARTUP_DELAY_FUNC, sntp_request, NULL);
677195972f6Sopenharmony_ci#else
678195972f6Sopenharmony_ci        sntp_request(NULL);
679195972f6Sopenharmony_ci#endif
680195972f6Sopenharmony_ci      } else if (sntp_opmode == SNTP_OPMODE_LISTENONLY) {
681195972f6Sopenharmony_ci        ip_set_option(sntp_pcb, SOF_BROADCAST);
682195972f6Sopenharmony_ci        udp_bind(sntp_pcb, IP_ANY_TYPE, SNTP_PORT);
683195972f6Sopenharmony_ci      }
684195972f6Sopenharmony_ci    }
685195972f6Sopenharmony_ci  }
686195972f6Sopenharmony_ci}
687195972f6Sopenharmony_ci
688195972f6Sopenharmony_ci/**
689195972f6Sopenharmony_ci * @ingroup sntp
690195972f6Sopenharmony_ci * Stop this module.
691195972f6Sopenharmony_ci */
692195972f6Sopenharmony_civoid
693195972f6Sopenharmony_cisntp_stop(void)
694195972f6Sopenharmony_ci{
695195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
696195972f6Sopenharmony_ci  if (sntp_pcb != NULL) {
697195972f6Sopenharmony_ci#if SNTP_MONITOR_SERVER_REACHABILITY
698195972f6Sopenharmony_ci    u8_t i;
699195972f6Sopenharmony_ci    for (i = 0; i < SNTP_MAX_SERVERS; i++) {
700195972f6Sopenharmony_ci      sntp_servers[i].reachability = 0;
701195972f6Sopenharmony_ci    }
702195972f6Sopenharmony_ci#endif /* SNTP_MONITOR_SERVER_REACHABILITY */
703195972f6Sopenharmony_ci    sys_untimeout(sntp_request, NULL);
704195972f6Sopenharmony_ci    sys_untimeout(sntp_try_next_server, NULL);
705195972f6Sopenharmony_ci    udp_remove(sntp_pcb);
706195972f6Sopenharmony_ci    sntp_pcb = NULL;
707195972f6Sopenharmony_ci  }
708195972f6Sopenharmony_ci}
709195972f6Sopenharmony_ci
710195972f6Sopenharmony_ci/**
711195972f6Sopenharmony_ci * @ingroup sntp
712195972f6Sopenharmony_ci * Get enabled state.
713195972f6Sopenharmony_ci */
714195972f6Sopenharmony_ciu8_t sntp_enabled(void)
715195972f6Sopenharmony_ci{
716195972f6Sopenharmony_ci  return (sntp_pcb != NULL) ? 1 : 0;
717195972f6Sopenharmony_ci}
718195972f6Sopenharmony_ci
719195972f6Sopenharmony_ci/**
720195972f6Sopenharmony_ci * @ingroup sntp
721195972f6Sopenharmony_ci * Sets the operating mode.
722195972f6Sopenharmony_ci * @param operating_mode one of the available operating modes
723195972f6Sopenharmony_ci */
724195972f6Sopenharmony_civoid
725195972f6Sopenharmony_cisntp_setoperatingmode(u8_t operating_mode)
726195972f6Sopenharmony_ci{
727195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
728195972f6Sopenharmony_ci  LWIP_ASSERT("Invalid operating mode", operating_mode <= SNTP_OPMODE_LISTENONLY);
729195972f6Sopenharmony_ci  LWIP_ASSERT("Operating mode must not be set while SNTP client is running", sntp_pcb == NULL);
730195972f6Sopenharmony_ci  sntp_opmode = operating_mode;
731195972f6Sopenharmony_ci}
732195972f6Sopenharmony_ci
733195972f6Sopenharmony_ci/**
734195972f6Sopenharmony_ci * @ingroup sntp
735195972f6Sopenharmony_ci * Gets the operating mode.
736195972f6Sopenharmony_ci */
737195972f6Sopenharmony_ciu8_t
738195972f6Sopenharmony_cisntp_getoperatingmode(void)
739195972f6Sopenharmony_ci{
740195972f6Sopenharmony_ci  return sntp_opmode;
741195972f6Sopenharmony_ci}
742195972f6Sopenharmony_ci
743195972f6Sopenharmony_ci#if SNTP_MONITOR_SERVER_REACHABILITY
744195972f6Sopenharmony_ci/**
745195972f6Sopenharmony_ci * @ingroup sntp
746195972f6Sopenharmony_ci * Gets the server reachability shift register as described in RFC 5905.
747195972f6Sopenharmony_ci *
748195972f6Sopenharmony_ci * @param idx the index of the NTP server
749195972f6Sopenharmony_ci */
750195972f6Sopenharmony_ciu8_t
751195972f6Sopenharmony_cisntp_getreachability(u8_t idx)
752195972f6Sopenharmony_ci{
753195972f6Sopenharmony_ci  if (idx < SNTP_MAX_SERVERS) {
754195972f6Sopenharmony_ci    return sntp_servers[idx].reachability;
755195972f6Sopenharmony_ci  }
756195972f6Sopenharmony_ci  return 0;
757195972f6Sopenharmony_ci}
758195972f6Sopenharmony_ci#endif /* SNTP_MONITOR_SERVER_REACHABILITY */
759195972f6Sopenharmony_ci
760195972f6Sopenharmony_ci#if SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6
761195972f6Sopenharmony_ci/**
762195972f6Sopenharmony_ci * Config SNTP server handling by IP address, name, or DHCP; clear table
763195972f6Sopenharmony_ci * @param set_servers_from_dhcp enable or disable getting server addresses from dhcp
764195972f6Sopenharmony_ci */
765195972f6Sopenharmony_civoid
766195972f6Sopenharmony_cisntp_servermode_dhcp(int set_servers_from_dhcp)
767195972f6Sopenharmony_ci{
768195972f6Sopenharmony_ci  u8_t new_mode = set_servers_from_dhcp ? 1 : 0;
769195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
770195972f6Sopenharmony_ci  if (sntp_set_servers_from_dhcp != new_mode) {
771195972f6Sopenharmony_ci    sntp_set_servers_from_dhcp = new_mode;
772195972f6Sopenharmony_ci  }
773195972f6Sopenharmony_ci}
774195972f6Sopenharmony_ci#endif /* SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6 */
775195972f6Sopenharmony_ci
776195972f6Sopenharmony_ci/**
777195972f6Sopenharmony_ci * @ingroup sntp
778195972f6Sopenharmony_ci * Initialize one of the NTP servers by IP address
779195972f6Sopenharmony_ci *
780195972f6Sopenharmony_ci * @param idx the index of the NTP server to set must be < SNTP_MAX_SERVERS
781195972f6Sopenharmony_ci * @param server IP address of the NTP server to set
782195972f6Sopenharmony_ci */
783195972f6Sopenharmony_civoid
784195972f6Sopenharmony_cisntp_setserver(u8_t idx, const ip_addr_t *server)
785195972f6Sopenharmony_ci{
786195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
787195972f6Sopenharmony_ci  if (idx < SNTP_MAX_SERVERS) {
788195972f6Sopenharmony_ci    if (server != NULL) {
789195972f6Sopenharmony_ci      sntp_servers[idx].addr = (*server);
790195972f6Sopenharmony_ci    } else {
791195972f6Sopenharmony_ci      ip_addr_set_zero(&sntp_servers[idx].addr);
792195972f6Sopenharmony_ci    }
793195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
794195972f6Sopenharmony_ci    sntp_servers[idx].name = NULL;
795195972f6Sopenharmony_ci#endif
796195972f6Sopenharmony_ci  }
797195972f6Sopenharmony_ci}
798195972f6Sopenharmony_ci
799195972f6Sopenharmony_ci#if LWIP_DHCP && SNTP_GET_SERVERS_FROM_DHCP
800195972f6Sopenharmony_ci/**
801195972f6Sopenharmony_ci * Initialize one of the NTP servers by IP address, required by DHCP
802195972f6Sopenharmony_ci *
803195972f6Sopenharmony_ci * @param num the index of the NTP server to set must be < SNTP_MAX_SERVERS
804195972f6Sopenharmony_ci * @param server IP address of the NTP server to set
805195972f6Sopenharmony_ci */
806195972f6Sopenharmony_civoid
807195972f6Sopenharmony_cidhcp_set_ntp_servers(u8_t num, const ip4_addr_t *server)
808195972f6Sopenharmony_ci{
809195972f6Sopenharmony_ci  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp: %s %u.%u.%u.%u as NTP server #%u via DHCP\n",
810195972f6Sopenharmony_ci                                 (sntp_set_servers_from_dhcp ? "Got" : "Rejected"),
811195972f6Sopenharmony_ci                                 ip4_addr1(server), ip4_addr2(server), ip4_addr3(server), ip4_addr4(server), num));
812195972f6Sopenharmony_ci  if (sntp_set_servers_from_dhcp && num) {
813195972f6Sopenharmony_ci    u8_t i;
814195972f6Sopenharmony_ci    for (i = 0; (i < num) && (i < SNTP_MAX_SERVERS); i++) {
815195972f6Sopenharmony_ci      ip_addr_t addr;
816195972f6Sopenharmony_ci      ip_addr_copy_from_ip4(addr, server[i]);
817195972f6Sopenharmony_ci      sntp_setserver(i, &addr);
818195972f6Sopenharmony_ci    }
819195972f6Sopenharmony_ci    for (i = num; i < SNTP_MAX_SERVERS; i++) {
820195972f6Sopenharmony_ci      sntp_setserver(i, NULL);
821195972f6Sopenharmony_ci    }
822195972f6Sopenharmony_ci  }
823195972f6Sopenharmony_ci}
824195972f6Sopenharmony_ci#endif /* LWIP_DHCP && SNTP_GET_SERVERS_FROM_DHCP */
825195972f6Sopenharmony_ci
826195972f6Sopenharmony_ci#if LWIP_IPV6_DHCP6 && SNTP_GET_SERVERS_FROM_DHCPV6
827195972f6Sopenharmony_ci/**
828195972f6Sopenharmony_ci * Initialize one of the NTP servers by IP address, required by DHCPV6
829195972f6Sopenharmony_ci *
830195972f6Sopenharmony_ci * @param num the number of NTP server addresses to set must be < SNTP_MAX_SERVERS
831195972f6Sopenharmony_ci * @param server array of IP address of the NTP servers to set
832195972f6Sopenharmony_ci */
833195972f6Sopenharmony_civoid
834195972f6Sopenharmony_cidhcp6_set_ntp_servers(u8_t num_ntp_servers, ip_addr_t* ntp_server_addrs)
835195972f6Sopenharmony_ci{
836195972f6Sopenharmony_ci  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp: %s %u NTP server(s) via DHCPv6\n",
837195972f6Sopenharmony_ci                                 (sntp_set_servers_from_dhcp ? "Got" : "Rejected"),
838195972f6Sopenharmony_ci                                 num_ntp_servers));
839195972f6Sopenharmony_ci  if (sntp_set_servers_from_dhcp && num_ntp_servers) {
840195972f6Sopenharmony_ci    u8_t i;
841195972f6Sopenharmony_ci    for (i = 0; (i < num_ntp_servers) && (i < SNTP_MAX_SERVERS); i++) {
842195972f6Sopenharmony_ci      LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp: NTP server %u: %s\n",
843195972f6Sopenharmony_ci                                     i, ipaddr_ntoa(&ntp_server_addrs[i])));
844195972f6Sopenharmony_ci      sntp_setserver(i, &ntp_server_addrs[i]);
845195972f6Sopenharmony_ci    }
846195972f6Sopenharmony_ci    for (i = num_ntp_servers; i < SNTP_MAX_SERVERS; i++) {
847195972f6Sopenharmony_ci      sntp_setserver(i, NULL);
848195972f6Sopenharmony_ci    }
849195972f6Sopenharmony_ci  }
850195972f6Sopenharmony_ci}
851195972f6Sopenharmony_ci#endif /* LWIP_DHCPv6 && SNTP_GET_SERVERS_FROM_DHCPV6 */
852195972f6Sopenharmony_ci
853195972f6Sopenharmony_ci/**
854195972f6Sopenharmony_ci * @ingroup sntp
855195972f6Sopenharmony_ci * Obtain one of the currently configured by IP address (or DHCP) NTP servers
856195972f6Sopenharmony_ci *
857195972f6Sopenharmony_ci * @param idx the index of the NTP server
858195972f6Sopenharmony_ci * @return IP address of the indexed NTP server or "ip_addr_any" if the NTP
859195972f6Sopenharmony_ci *         server has not been configured by address (or at all).
860195972f6Sopenharmony_ci */
861195972f6Sopenharmony_ciconst ip_addr_t *
862195972f6Sopenharmony_cisntp_getserver(u8_t idx)
863195972f6Sopenharmony_ci{
864195972f6Sopenharmony_ci  if (idx < SNTP_MAX_SERVERS) {
865195972f6Sopenharmony_ci    return &sntp_servers[idx].addr;
866195972f6Sopenharmony_ci  }
867195972f6Sopenharmony_ci  return IP_ADDR_ANY;
868195972f6Sopenharmony_ci}
869195972f6Sopenharmony_ci
870195972f6Sopenharmony_ci#if SNTP_SERVER_DNS
871195972f6Sopenharmony_ci/**
872195972f6Sopenharmony_ci * Initialize one of the NTP servers by name
873195972f6Sopenharmony_ci *
874195972f6Sopenharmony_ci * @param idx the index of the NTP server to set must be < SNTP_MAX_SERVERS
875195972f6Sopenharmony_ci * @param server DNS name of the NTP server to set, to be resolved at contact time
876195972f6Sopenharmony_ci */
877195972f6Sopenharmony_civoid
878195972f6Sopenharmony_cisntp_setservername(u8_t idx, const char *server)
879195972f6Sopenharmony_ci{
880195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
881195972f6Sopenharmony_ci  if (idx < SNTP_MAX_SERVERS) {
882195972f6Sopenharmony_ci    sntp_servers[idx].name = server;
883195972f6Sopenharmony_ci  }
884195972f6Sopenharmony_ci}
885195972f6Sopenharmony_ci
886195972f6Sopenharmony_ci/**
887195972f6Sopenharmony_ci * Obtain one of the currently configured by name NTP servers.
888195972f6Sopenharmony_ci *
889195972f6Sopenharmony_ci * @param idx the index of the NTP server
890195972f6Sopenharmony_ci * @return IP address of the indexed NTP server or NULL if the NTP
891195972f6Sopenharmony_ci *         server has not been configured by name (or at all)
892195972f6Sopenharmony_ci */
893195972f6Sopenharmony_ciconst char *
894195972f6Sopenharmony_cisntp_getservername(u8_t idx)
895195972f6Sopenharmony_ci{
896195972f6Sopenharmony_ci  if (idx < SNTP_MAX_SERVERS) {
897195972f6Sopenharmony_ci    return sntp_servers[idx].name;
898195972f6Sopenharmony_ci  }
899195972f6Sopenharmony_ci  return NULL;
900195972f6Sopenharmony_ci}
901195972f6Sopenharmony_ci#endif /* SNTP_SERVER_DNS */
902195972f6Sopenharmony_ci
903195972f6Sopenharmony_ci#endif /* LWIP_UDP */
904