File: ntpclient.c

package info (click to toggle)
libwebsockets 4.3.5-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,404 kB
  • sloc: ansic: 194,409; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (310 lines) | stat: -rw-r--r-- 8,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
 /*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "private-lib-core.h"

#define LWSNTPC_LI_NONE			0
#define LWSNTPC_VN_3			3
#define LWSNTPC_MODE_CLIENT		3

struct vhd_ntpc {
	struct lws_context		*context;
	struct lws_vhost		*vhost;
	const struct lws_protocols	*protocol;
	lws_sorted_usec_list_t		sul_conn;
	lws_sorted_usec_list_t		sul_write; /* track write retries */
	const char			*ntp_server_ads;
	struct lws			*wsi_udp;
	uint16_t			retry_count_conn;
	uint16_t			retry_count_write;

	char				set_time;
};

/*
 * Without a valid ntp we won't be able to do anything requiring client tls.
 *
 * We have our own outer backoff scheme that just keeps retrying dns lookup
 * and the transaction forever.
 */

static const uint32_t botable[] =
		{ 300, 500, 650, 800, 800, 900, 1000, 1100, 1500 };
static const lws_retry_bo_t bo = {
	botable, LWS_ARRAY_SIZE(botable), LWS_RETRY_CONCEAL_ALWAYS, 0, 0, 20 };

/*
 * Once we resolved the remote server (implying we should have network),
 * we use a different policy on the wsi itself that gives it a few tries before
 * failing the wsi and using to outer retry policy to get dns to a different
 * server in the pool and try fresh
 */

static const uint32_t botable2[] = { 1000, 1250, 5000 /* in case dog slow */ };
static const lws_retry_bo_t bo2 = {
	botable2, LWS_ARRAY_SIZE(botable2), LWS_ARRAY_SIZE(botable2),
	/* don't conceal after the last table entry */ 0, 0, 20 };

static void
lws_ntpc_retry_conn(struct lws_sorted_usec_list *sul)
{
	struct vhd_ntpc *v = lws_container_of(sul, struct vhd_ntpc, sul_conn);

	lwsl_debug("%s: wsi_udp: %s\n", __func__, lws_wsi_tag(v->wsi_udp));

	if (v->wsi_udp || !lws_dll2_is_detached(&v->sul_conn.list))
		return;

	/* create the UDP socket aimed at the server */

	lwsl_notice("%s: server %s\n", __func__, v->ntp_server_ads);

	v->retry_count_write = 0;
	v->wsi_udp = lws_create_adopt_udp(v->vhost, v->ntp_server_ads, 123, 0,
					  v->protocol->name, NULL, NULL, NULL,
					  &bo2, "ntpclient");
	lwsl_debug("%s: created wsi_udp: %s\n", __func__, lws_wsi_tag(v->wsi_udp));
	if (!v->wsi_udp) {
		lwsl_err("%s: unable to create udp skt\n", __func__);

		lws_retry_sul_schedule(v->context, 0, &v->sul_conn, &bo,
				       lws_ntpc_retry_conn, &v->retry_count_conn);
	}
}

static void
lws_ntpc_retry_write(struct lws_sorted_usec_list *sul)
{
	struct vhd_ntpc *v = lws_container_of(sul, struct vhd_ntpc, sul_write);

	lwsl_debug("%s\n", __func__);

	if (v && v->wsi_udp)
		lws_callback_on_writable(v->wsi_udp);
}

static int
callback_ntpc(struct lws *wsi, enum lws_callback_reasons reason, void *user,
	      void *in, size_t len)
{
	struct vhd_ntpc *v = (struct vhd_ntpc *)
			lws_protocol_vh_priv_get(lws_get_vhost(wsi),
						 lws_get_protocol(wsi));
	uint8_t pkt[LWS_PRE + 48];
	struct timeval t1;
	int64_t delta_us;
	uint64_t ns;

	switch (reason) {

	case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
		if (v)
			break;

		lwsl_debug("%s: LWS_CALLBACK_PROTOCOL_INIT:\n", __func__);
		lws_protocol_vh_priv_zalloc(wsi->a.vhost, wsi->a.protocol,
					    sizeof(*v));
		v = (struct vhd_ntpc *)lws_protocol_vh_priv_get(wsi->a.vhost,
								wsi->a.protocol);
		v->context = lws_get_context(wsi);
		v->vhost = lws_get_vhost(wsi);
		v->protocol = lws_get_protocol(wsi);

		v->context->ntpclient_priv = v;

		if (!lws_system_get_ops(wsi->a.context) ||
		    !lws_system_get_ops(wsi->a.context)->set_clock) {
#if !defined(LWS_ESP_PLATFORM)
			lwsl_err("%s: set up system ops for set_clock\n",
					__func__);
#endif

		//	return -1;
		}

		/* register our lws_system notifier */

		v->ntp_server_ads = "pool.ntp.org";
		lws_plat_ntpclient_config(v->context);
		lws_system_blob_get_single_ptr(lws_system_get_blob(
				v->context, LWS_SYSBLOB_TYPE_NTP_SERVER, 0),
				(const uint8_t **)&v->ntp_server_ads);
		if (!v->ntp_server_ads || v->ntp_server_ads[0] == '\0')
			v->ntp_server_ads = "pool.ntp.org";

		lwsl_notice("%s: using ntp server %s\n", __func__,
			  v->ntp_server_ads);
		break;

	case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
		if (!v)
			break;
		if (v->wsi_udp)
			lws_set_timeout(v->wsi_udp, 1, LWS_TO_KILL_ASYNC);
		v->wsi_udp = NULL;
		goto cancel_conn_timer;

	/* callbacks related to raw socket descriptor */

        case LWS_CALLBACK_RAW_ADOPT:
		lwsl_debug("%s: LWS_CALLBACK_RAW_ADOPT\n", __func__);
		lws_callback_on_writable(wsi);
		break;

        case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
		lwsl_info("%s: CONNECTION_ERROR\n", __func__);
		goto do_close;

	case LWS_CALLBACK_RAW_CLOSE:
		lwsl_debug("%s: LWS_CALLBACK_RAW_CLOSE\n", __func__);
do_close:
		v->wsi_udp = NULL;

		/* cancel any pending write retry */
		lws_sul_cancel(&v->sul_write);

		if (v->set_time)
			goto cancel_conn_timer;

		lws_retry_sul_schedule(v->context, 0, &v->sul_conn, &bo,
				       lws_ntpc_retry_conn,
				       &v->retry_count_conn);
		break;

	case LWS_CALLBACK_RAW_RX:

		if (len != 48)
			return 0; /* ignore it */

		/*
		 * First get the seconds, corrected for the ntp epoch of 1900
		 * vs the unix epoch of 1970.  Then shift the seconds up by 1bn
		 * and add in the ns
		 */

		ns = (uint64_t)lws_ser_ru32be(((uint8_t *)in) + 40) - (uint64_t)2208988800;
		ns = (ns * 1000000000) + lws_ser_ru32be(((uint8_t *)in) + 44);

		/*
		 * Compute the step
		 */

		gettimeofday(&t1, NULL);

		delta_us = ((int64_t)ns / 1000) -
				((t1.tv_sec * LWS_US_PER_SEC) + t1.tv_usec);

		lwsl_notice("%s: Unix time: %llu, step: %lldus\n", __func__,
				(unsigned long long)ns / 1000000000,
				(long long)delta_us);

#if defined(LWS_PLAT_FREERTOS)
		{
			struct timeval t;

			t.tv_sec = (unsigned long long)ns / 1000000000;
			t.tv_usec = (ns % 1000000000) / 1000;

			lws_sul_nonmonotonic_adjust(wsi->a.context, delta_us);

			settimeofday(&t, NULL);
		}
#endif
		if (lws_system_get_ops(wsi->a.context) &&
		    lws_system_get_ops(wsi->a.context)->set_clock)
			lws_system_get_ops(wsi->a.context)->set_clock((int64_t)ns / 1000);

		v->set_time = 1;
		lws_state_transition_steps(&wsi->a.context->mgr_system,
					   LWS_SYSTATE_OPERATIONAL);

		/* close the wsi */
		return -1;

	case LWS_CALLBACK_RAW_WRITEABLE:

		/*
		 * UDP is not reliable, it can be locally dropped, or dropped
		 * by any intermediary or the remote peer.  So even though we
		 * will do the write in a moment, we schedule another request
		 * for rewrite according to the wsi retry policy.
		 *
		 * If the result came before, we'll cancel it in the close flow.
		 *
		 * If we have already reached the end of our concealed retries
		 * in the policy, just close without another write.
		 */
		if (lws_dll2_is_detached(&v->sul_write.list) &&
		    lws_retry_sul_schedule_retry_wsi(wsi, &v->sul_write,
						     lws_ntpc_retry_write,
						     &v->retry_count_write)) {
			/* we have reached the end of our concealed retries */
			lwsl_warn("%s: concealed retries done, failing\n", __func__);
			goto retry_conn;
		}

		memset(pkt + LWS_PRE, 0, sizeof(pkt) - LWS_PRE);
		pkt[LWS_PRE] = (LWSNTPC_LI_NONE << 6) |
			       (LWSNTPC_VN_3 << 3) |
			       (LWSNTPC_MODE_CLIENT << 0);

		if (lws_write(wsi, pkt + LWS_PRE, sizeof(pkt) - LWS_PRE, 0) ==
						  sizeof(pkt) - LWS_PRE)
			break;

		lwsl_err("%s: Failed to write ntp client req\n", __func__);

retry_conn:
		lws_retry_sul_schedule(wsi->a.context, 0, &v->sul_conn, &bo,
				       lws_ntpc_retry_conn,
				       &v->retry_count_conn);

		return -1;

	default:
		break;
	}

	return 0;


cancel_conn_timer:
	lws_sul_cancel(&v->sul_conn);

	return 0;
}

void
lws_ntpc_trigger(struct lws_context *ctx)
{
	struct vhd_ntpc *v = (struct vhd_ntpc *)ctx->ntpclient_priv;

	lwsl_notice("%s\n", __func__);
	v->retry_count_conn = 0;
	lws_ntpc_retry_conn(&v->sul_conn);
}

struct lws_protocols lws_system_protocol_ntpc =
	{ "lws-ntpclient", callback_ntpc, 0, 128, 0, NULL, 0 };