1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/lib/system/ntpclient/ntpclient.c

311 lines
8.8 KiB
C
Raw Permalink Normal View History

2019-09-22 07:25:58 -07:00
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
2019-09-22 07:25:58 -07:00
*
* 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.
*/
2020-04-17 05:25:32 +01:00
static const uint32_t botable[] =
{ 300, 500, 650, 800, 800, 900, 1000, 1100, 1500 };
2019-09-22 07:25:58 -07:00
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));
2019-09-22 07:25:58 -07:00
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);
2019-09-22 07:25:58 -07:00
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));
2019-09-22 07:25:58 -07:00
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;
2019-09-22 07:25:58 -07:00
uint64_t ns;
switch (reason) {
case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
if (v)
break;
lwsl_debug("%s: LWS_CALLBACK_PROTOCOL_INIT:\n", __func__);
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
lws_protocol_vh_priv_zalloc(wsi->a.vhost, wsi->a.protocol,
2019-09-22 07:25:58 -07:00
sizeof(*v));
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
v = (struct vhd_ntpc *)lws_protocol_vh_priv_get(wsi->a.vhost,
wsi->a.protocol);
2019-09-22 07:25:58 -07:00
v->context = lws_get_context(wsi);
v->vhost = lws_get_vhost(wsi);
v->protocol = lws_get_protocol(wsi);
v->context->ntpclient_priv = v;
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
if (!lws_system_get_ops(wsi->a.context) ||
!lws_system_get_ops(wsi->a.context)->set_clock) {
2020-06-22 20:05:06 +01:00
#if !defined(LWS_ESP_PLATFORM)
2019-09-22 07:25:58 -07:00
lwsl_err("%s: set up system ops for set_clock\n",
__func__);
2020-06-22 20:05:06 +01:00
#endif
2019-09-22 07:25:58 -07:00
// 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);
2020-06-22 20:05:06 +01:00
if (!v->ntp_server_ads || v->ntp_server_ads[0] == '\0')
v->ntp_server_ads = "pool.ntp.org";
2019-09-22 07:25:58 -07:00
lwsl_notice("%s: using ntp server %s\n", __func__,
2020-06-22 20:05:06 +01:00
v->ntp_server_ads);
2019-09-22 07:25:58 -07:00
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:
2020-01-02 08:32:23 +00:00
lwsl_info("%s: CONNECTION_ERROR\n", __func__);
goto do_close;
2019-09-22 07:25:58 -07:00
case LWS_CALLBACK_RAW_CLOSE:
2020-01-02 08:32:23 +00:00
lwsl_debug("%s: LWS_CALLBACK_RAW_CLOSE\n", __func__);
do_close:
2019-09-22 07:25:58 -07:00
v->wsi_udp = NULL;
/* cancel any pending write retry */
lws_sul_cancel(&v->sul_write);
2019-09-22 07:25:58 -07:00
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;
2019-09-22 07:25:58 -07:00
ns = (ns * 1000000000) + lws_ser_ru32be(((uint8_t *)in) + 44);
/*
* Compute the step
*/
gettimeofday(&t1, NULL);
2019-09-22 07:25:58 -07:00
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)
2020-06-22 20:05:06 +01:00
{
struct timeval t;
t.tv_sec = (unsigned long long)ns / 1000000000;
t.tv_usec = (ns % 1000000000) / 1000;
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
lws_sul_nonmonotonic_adjust(wsi->a.context, delta_us);
2020-06-22 20:05:06 +01:00
settimeofday(&t, NULL);
}
#endif
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
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);
2019-09-22 07:25:58 -07:00
v->set_time = 1;
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
lws_state_transition_steps(&wsi->a.context->mgr_system,
2019-09-22 07:25:58 -07:00
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:
fakewsi: replace with smaller substructure Currently we always reserve a fakewsi per pt so events that don't have a related actual wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks that look reasonable to user protocol handler code expecting a valid wsi every time. This patch splits out stuff that user callbacks often unconditionally expect to be in a wsi, like context pointer, vhost pointer etc into a substructure, which is composed into struct lws at the top of it. Internal references (struct lws is opaque, so there are only internal references) are all updated to go via the substructre, the compiler should make that a NOP. Helpers are added when fakewsi is used and referenced. If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before, although the helpers improve consistency by zeroing down the substructure. There is a huge amount of user code out there over the last 10 years that did not always have the minimal examples to follow, some of it does some unexpected things. If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then create a struct lws_a (the substructure) on the stack, zero it down (but it is only like 4 pointers) and prepare it with whatever we know like the context. Then we cast it to a struct lws * and use it in the user protocol handler call. In this case, the remainder of the struct lws is undefined. However the amount of old protocol handlers that might touch things outside of the substructure in PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is significant on constrained devices. User handlers should not be touching everything in a wsi every time anyway, there are several cases where there is no valid wsi to do the call with. Dereference of things outside the substructure should only happen when the callback reason shows there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
lws_retry_sul_schedule(wsi->a.context, 0, &v->sul_conn, &bo,
2019-09-22 07:25:58 -07:00
lws_ntpc_retry_conn,
&v->retry_count_conn);
return -1;
default:
break;
}
return 0;
cancel_conn_timer:
lws_sul_cancel(&v->sul_conn);
2019-09-22 07:25:58 -07:00
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);
}
2019-09-22 07:25:58 -07:00
struct lws_protocols lws_system_protocol_ntpc =
{ "lws-ntpclient", callback_ntpc, 0, 128, 0, NULL, 0 };
2019-09-22 07:25:58 -07:00