diff --git a/ntp_core.c b/ntp_core.c index 68d929f..686f640 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -541,8 +541,6 @@ NCR_ResetInstance(NCR_Instance instance) void NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr) { - SRC_SelectOption sel_option; - inst->remote_addr = *remote_addr; inst->tx_count = 0; inst->presend_done = 0; @@ -552,11 +550,10 @@ NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr) else inst->local_addr.sock_fd = NIO_GetServerSocket(remote_addr); - /* Replace sources and sourcestats instances */ - sel_option = SRC_GetSelectOption(inst->source); - SRC_DestroyInstance(inst->source); - inst->source = SRC_CreateNewInstance(UTI_IPToRefid(&remote_addr->ip_addr), SRC_NTP, - sel_option, &inst->remote_addr.ip_addr); + /* Update the reference ID and reset the source/sourcestats instances */ + SRC_SetRefid(inst->source, UTI_IPToRefid(&remote_addr->ip_addr), + &inst->remote_addr.ip_addr); + SRC_ResetInstance(inst->source); } /* ================================================== */ diff --git a/sources.c b/sources.c index e88fc47..ba18bb5 100644 --- a/sources.c +++ b/sources.c @@ -231,20 +231,14 @@ SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_SelectOpt } sources[n_sources] = result; + result->index = n_sources; - result->leap_status = LEAP_Normal; - result->ref_id = ref_id; - result->ip_addr = addr; - result->active = 0; - result->updates = 0; - result->reachability = 0; - result->reachability_size = 0; - result->distant = 0; - result->status = SRC_BAD_STATS; result->type = type; - result->sel_score = 1.0; result->sel_option = sel_option; + SRC_SetRefid(result, ref_id, addr); + SRC_ResetInstance(result); + n_sources++; return result; @@ -278,6 +272,33 @@ void SRC_DestroyInstance(SRC_Instance instance) --selected_source_index; } +/* ================================================== */ + +void +SRC_ResetInstance(SRC_Instance instance) +{ + instance->leap_status = LEAP_Normal; + instance->active = 0; + instance->updates = 0; + instance->reachability = 0; + instance->reachability_size = 0; + instance->distant = 0; + instance->status = SRC_BAD_STATS; + instance->sel_score = 1.0; + + SST_ResetInstance(instance->stats); +} + +/* ================================================== */ + +void +SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr) +{ + instance->ref_id = ref_id; + instance->ip_addr = addr; + SST_SetRefid(instance->stats, ref_id, addr); +} + /* ================================================== */ /* Function to get the range of frequencies, relative to the given source, that we believe the local clock lies within. The return @@ -1318,10 +1339,3 @@ SRC_Samples(SRC_Instance inst) } /* ================================================== */ - -SRC_SelectOption SRC_GetSelectOption(SRC_Instance inst) -{ - return inst->sel_option; -} - -/* ================================================== */ diff --git a/sources.h b/sources.h index d9a0315..654b3c1 100644 --- a/sources.h +++ b/sources.h @@ -74,6 +74,11 @@ extern SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_Se extern void SRC_DestroyInstance(SRC_Instance instance); +/* Function to reset a source */ +extern void SRC_ResetInstance(SRC_Instance instance); + +/* Function to change the sources's reference ID and IP address */ +extern void SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr); /* Function to get the range of frequencies, relative to the given source, that we believe the local clock lies within. The return @@ -174,7 +179,5 @@ extern SRC_Type SRC_GetType(int index); extern int SRC_Samples(SRC_Instance inst); -extern SRC_SelectOption SRC_GetSelectOption(SRC_Instance inst); - #endif /* GOT_SOURCES_H */ diff --git a/sourcestats.c b/sourcestats.c index 537e62c..de5f312 100644 --- a/sourcestats.c +++ b/sourcestats.c @@ -182,9 +182,8 @@ SST_CreateInstance(uint32_t refid, IPAddr *addr) { SST_Stats inst; inst = MallocNew(struct SST_Stats_Record); - inst->refid = refid; - inst->ip_addr = addr; + SST_SetRefid(inst, refid, addr); SST_ResetInstance(inst); return inst; @@ -220,6 +219,15 @@ SST_ResetInstance(SST_Stats inst) inst->nruns = 0; } +/* ================================================== */ + +void +SST_SetRefid(SST_Stats inst, uint32_t refid, IPAddr *addr) +{ + inst->refid = refid; + inst->ip_addr = addr; +} + /* ================================================== */ /* This function is called to prune the register down when it is full. For now, just discard the oldest sample. */ diff --git a/sourcestats.h b/sourcestats.h index e080f97..ad9f5d8 100644 --- a/sourcestats.h +++ b/sourcestats.h @@ -46,6 +46,9 @@ extern void SST_DeleteInstance(SST_Stats inst); /* This function resets an instance */ extern void SST_ResetInstance(SST_Stats inst); +/* This function changes the reference ID and IP address */ +extern void SST_SetRefid(SST_Stats inst, uint32_t refid, IPAddr *addr); + /* This function accumulates a single sample into the statistics handler sample_time is the epoch at which the sample is to be considered to