From e8062b7ff1e8b0578b914e7ad982e7f310452786 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Sat, 23 Mar 2019 15:49:06 +0100 Subject: [PATCH] ntp: add function to update source NTP address This will allow a source to have its address changed due to NTS-KE server negotiation, which allows the NTS-KE server to have a different address than the NTP server. --- ntp_core.c | 2 +- ntp_core.h | 3 ++- ntp_sources.c | 21 +++++++++++++++++---- ntp_sources.h | 4 ++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ntp_core.c b/ntp_core.c index 7f48d9a..9ce0426 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -691,7 +691,7 @@ NCR_ResetPoll(NCR_Instance instance) /* ================================================== */ void -NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr) +NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr, int ntp_only) { memset(&inst->report, 0, sizeof (inst->report)); NCR_ResetInstance(inst); diff --git a/ntp_core.h b/ntp_core.h index 522d59b..78b6ede 100644 --- a/ntp_core.h +++ b/ntp_core.h @@ -74,7 +74,8 @@ extern void NCR_ResetInstance(NCR_Instance inst); extern void NCR_ResetPoll(NCR_Instance instance); /* Change the remote address of an instance */ -extern void NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr); +extern void NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr, + int ntp_only); /* This routine is called when a new packet arrives off the network, and it relates to a source we have an ongoing protocol exchange with */ diff --git a/ntp_sources.c b/ntp_sources.c index 6d10463..2b1240c 100644 --- a/ntp_sources.c +++ b/ntp_sources.c @@ -343,7 +343,8 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type, So /* ================================================== */ static NSR_Status -replace_source(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr) +change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr, + int replacement) { int slot1, slot2, found; SourceRecord *record; @@ -361,7 +362,7 @@ replace_source(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr) return NSR_AlreadyInUse; record = get_record(slot1); - NCR_ChangeRemoteAddress(record->data, new_addr); + NCR_ChangeRemoteAddress(record->data, new_addr, !replacement); record->remote_addr = NCR_GetRemoteAddress(record->data); if (!UTI_IsIPReal(&old_addr->ip_addr) && UTI_IsIPReal(&new_addr->ip_addr)) { if (auto_start_sources) @@ -384,7 +385,8 @@ replace_source(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr) /* The hash table must be rebuilt for the changed address */ rehash_records(); - LOG(severity, "Source %s replaced with %s (%s)", UTI_IPToString(&old_addr->ip_addr), + LOG(severity, "Source %s %s %s (%s)", UTI_IPToString(&old_addr->ip_addr), + replacement ? "replaced with" : "changed to", UTI_IPToString(&new_addr->ip_addr), name ? name : ""); } else { LOG(severity, "Source %s (%s) changed port to %d", @@ -404,7 +406,7 @@ replace_source_connectable(NTP_Remote_Address *old_addr, NTP_Remote_Address *new return 0; } - if (replace_source(old_addr, new_addr) == NSR_AlreadyInUse) + if (change_source_address(old_addr, new_addr, 1) == NSR_AlreadyInUse) return 0; return 1; @@ -864,6 +866,17 @@ NSR_RefreshAddresses(void) /* ================================================== */ +NSR_Status +NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr) +{ + if (new_addr->ip_addr.family == IPADDR_UNSPEC) + return NSR_InvalidAF; + + return change_source_address(old_addr, new_addr, 0); +} + +/* ================================================== */ + static void remove_pool_sources(int pool, int tentative, int unresolved) { SourceRecord *record; diff --git a/ntp_sources.h b/ntp_sources.h index 686678e..ce50a68 100644 --- a/ntp_sources.h +++ b/ntp_sources.h @@ -87,6 +87,10 @@ extern void NSR_HandleBadSource(IPAddr *address); /* Procedure to resolve all names again */ extern void NSR_RefreshAddresses(void); +/* Procedure to update the address of a source */ +extern NSR_Status NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr, + NTP_Remote_Address *new_addr); + /* Procedure to get local reference ID corresponding to a source */ extern uint32_t NSR_GetLocalRefid(IPAddr *address);