diff --git a/src/ntp_node_time.c b/src/ntp_node_time.c index 9526eedb..720ae3a3 100644 --- a/src/ntp_node_time.c +++ b/src/ntp_node_time.c @@ -86,18 +86,19 @@ static struct NTP_NODE_PEER* add_peer(struct NTP_NODE_TIME* np, uint64_t node_id struct NTP_NODE_PEER* p = &np->peers[np->peer_count]; p->node_id = node_id; p->offset_us = 0; + p->has_offset = 0; np->peer_count++; return p; } -static void check_drift(uint64_t node_id, int64_t offset_us) { - int64_t abs_us = offset_us < 0 ? -offset_us : offset_us; +static void check_drift(uint64_t node_id, int64_t offset_delta_us) { + int64_t abs_us = offset_delta_us < 0 ? -offset_delta_us : offset_delta_us; if (abs_us > NTP_NODE_DRIFT_ERROR_US) { - DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "NTP_node: clock drift >10s with node %012llx: %lldus", - (unsigned long long)node_id, (long long)offset_us); + DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "NTP_node: clock offset changed by >10s with node %012llx: %lldus", + (unsigned long long)node_id, (long long)offset_delta_us); } else if (abs_us > NTP_NODE_DRIFT_WARN_US) { - DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "NTP_node: clock drift >2s with node %012llx: %lldus", - (unsigned long long)node_id, (long long)offset_us); + DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "NTP_node: clock offset changed by >2s with node %012llx: %lldus", + (unsigned long long)node_id, (long long)offset_delta_us); } } @@ -135,9 +136,14 @@ static void ntp_node_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry) { struct NTP_NODE_TIME* np = &inst->ntp_node; struct NTP_NODE_PEER* peer = find_peer(np, conn->peer_node_id); if (!peer) peer = add_peer(np, conn->peer_node_id); - if (peer) peer->offset_us = peer_offset; + if (!peer) return; - check_drift(conn->peer_node_id, peer_offset); + if (peer->has_offset) { + int64_t offset_delta = peer_offset - peer->offset_us; + check_drift(conn->peer_node_id, offset_delta); + } + peer->offset_us = peer_offset; + peer->has_offset = 1; int was_unsynced = !ntp_time_is_synced(inst); diff --git a/src/ntp_node_time.h b/src/ntp_node_time.h index a760ceff..b53f9e1e 100644 --- a/src/ntp_node_time.h +++ b/src/ntp_node_time.h @@ -17,6 +17,7 @@ struct UTUN_INSTANCE; struct NTP_NODE_PEER { uint64_t node_id; int64_t offset_us; // my_time_recv - peer_time (positive = my clock ahead) + int has_offset; // first offset already stored, subsequent deltas are real drift }; struct NTP_NODE_TIME {