diff --git a/client.c b/client.c index 3c605c1..774343e 100644 --- a/client.c +++ b/client.c @@ -1105,7 +1105,7 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line) msg->data.ntp_source.asymmetry = UTI_FloatHostToNetwork(data.params.asymmetry); msg->data.ntp_source.offset = UTI_FloatHostToNetwork(data.params.offset); msg->data.ntp_source.flags = htonl( - (data.params.online ? REQ_ADDSRC_ONLINE : 0) | + (data.params.connectivity == SRC_ONLINE ? REQ_ADDSRC_ONLINE : 0) | (data.params.auto_offline ? REQ_ADDSRC_AUTOOFFLINE : 0) | (data.params.iburst ? REQ_ADDSRC_IBURST : 0) | (data.params.interleaved ? REQ_ADDSRC_INTERLEAVED : 0) | diff --git a/cmdmon.c b/cmdmon.c index 3bd8d91..01cf49e 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -424,7 +424,7 @@ handle_online(CMD_Request *rx_message, CMD_Reply *tx_message) UTI_IPNetworkToHost(&rx_message->data.online.mask, &mask); UTI_IPNetworkToHost(&rx_message->data.online.address, &address); - if (!NSR_TakeSourcesOnline(&mask, &address)) + if (!NSR_SetConnectivity(&mask, &address, SRC_ONLINE)) tx_message->status = htons(STT_NOSUCHSOURCE); } @@ -437,7 +437,7 @@ handle_offline(CMD_Request *rx_message, CMD_Reply *tx_message) UTI_IPNetworkToHost(&rx_message->data.offline.mask, &mask); UTI_IPNetworkToHost(&rx_message->data.offline.address, &address); - if (!NSR_TakeSourcesOffline(&mask, &address)) + if (!NSR_SetConnectivity(&mask, &address, SRC_OFFLINE)) tx_message->status = htons(STT_NOSUCHSOURCE); } @@ -797,7 +797,8 @@ handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_m params.asymmetry = UTI_FloatNetworkToHost(rx_message->data.ntp_source.asymmetry); params.offset = UTI_FloatNetworkToHost(rx_message->data.ntp_source.offset); - params.online = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ? 1 : 0; + params.connectivity = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ? + SRC_ONLINE : SRC_OFFLINE; params.auto_offline = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_AUTOOFFLINE ? 1 : 0; params.iburst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_IBURST ? 1 : 0; params.interleaved = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_INTERLEAVED ? 1 : 0; diff --git a/cmdparse.c b/cmdparse.c index a228530..5f9a4e0 100644 --- a/cmdparse.c +++ b/cmdparse.c @@ -48,7 +48,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) src->port = SRC_DEFAULT_PORT; src->params.minpoll = SRC_DEFAULT_MINPOLL; src->params.maxpoll = SRC_DEFAULT_MAXPOLL; - src->params.online = 1; + src->params.connectivity = SRC_ONLINE; src->params.auto_offline = 0; src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL; src->params.burst = 0; @@ -90,7 +90,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) } else if (!strcasecmp(cmd, "iburst")) { src->params.iburst = 1; } else if (!strcasecmp(cmd, "offline")) { - src->params.online = 0; + src->params.connectivity = SRC_OFFLINE; } else if (!strcasecmp(cmd, "noselect")) { src->params.sel_options |= SRC_SELECT_NOSELECT; } else if (!strcasecmp(cmd, "prefer")) { diff --git a/conf.c b/conf.c index 3859d74..ef6db08 100644 --- a/conf.c +++ b/conf.c @@ -1411,7 +1411,7 @@ CNF_AddInitSources(void) ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i); ntp_addr.port = cps_source.port; cps_source.params.iburst = 1; - cps_source.params.online = 0; + cps_source.params.connectivity = SRC_OFFLINE; NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params); } diff --git a/ntp_core.c b/ntp_core.c index c82d792..45956e7 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -603,7 +603,7 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar result->rx_timeout_id = 0; result->tx_timeout_id = 0; result->tx_suspended = 1; - result->opmode = params->online ? MD_ONLINE : MD_OFFLINE; + result->opmode = params->connectivity == SRC_ONLINE ? MD_ONLINE : MD_OFFLINE; result->local_poll = result->minpoll; result->poll_score = 0.0; zero_local_timestamp(&result->local_tx); @@ -1143,7 +1143,7 @@ transmit_timeout(void *arg) /* With auto_offline take the source offline on 2nd missed reply */ if (inst->auto_offline && inst->tx_count >= 2) - NCR_TakeSourceOffline(inst); + NCR_SetConnectivity(inst, SRC_OFFLINE); if (inst->opmode == MD_OFFLINE) { return; @@ -2275,52 +2275,60 @@ NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double dof /* ================================================== */ void -NCR_TakeSourceOnline(NCR_Instance inst) +NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity) { - switch (inst->opmode) { - case MD_ONLINE: - /* Nothing to do */ + char *s; + + s = UTI_IPToString(&inst->remote_addr.ip_addr); + + switch (connectivity) { + case SRC_ONLINE: + switch (inst->opmode) { + case MD_ONLINE: + /* Nothing to do */ + break; + case MD_OFFLINE: + LOG(LOGS_INFO, "Source %s online", s); + inst->opmode = MD_ONLINE; + NCR_ResetInstance(inst); + start_initial_timeout(inst); + break; + case MD_BURST_WAS_ONLINE: + /* Will revert */ + break; + case MD_BURST_WAS_OFFLINE: + inst->opmode = MD_BURST_WAS_ONLINE; + LOG(LOGS_INFO, "Source %s online", s); + break; + default: + assert(0); + } break; - case MD_OFFLINE: - LOG(LOGS_INFO, "Source %s online", UTI_IPToString(&inst->remote_addr.ip_addr)); - inst->opmode = MD_ONLINE; - NCR_ResetInstance(inst); - start_initial_timeout(inst); - break; - case MD_BURST_WAS_ONLINE: - /* Will revert */ - break; - case MD_BURST_WAS_OFFLINE: - inst->opmode = MD_BURST_WAS_ONLINE; - LOG(LOGS_INFO, "Source %s online", UTI_IPToString(&inst->remote_addr.ip_addr)); + case SRC_OFFLINE: + switch (inst->opmode) { + case MD_ONLINE: + LOG(LOGS_INFO, "Source %s offline", s); + take_offline(inst); + break; + case MD_OFFLINE: + break; + case MD_BURST_WAS_ONLINE: + inst->opmode = MD_BURST_WAS_OFFLINE; + LOG(LOGS_INFO, "Source %s offline", s); + break; + case MD_BURST_WAS_OFFLINE: + break; + default: + assert(0); + } break; + default: + assert(0); } } /* ================================================== */ -void -NCR_TakeSourceOffline(NCR_Instance inst) -{ - switch (inst->opmode) { - case MD_ONLINE: - LOG(LOGS_INFO, "Source %s offline", UTI_IPToString(&inst->remote_addr.ip_addr)); - take_offline(inst); - break; - case MD_OFFLINE: - break; - case MD_BURST_WAS_ONLINE: - inst->opmode = MD_BURST_WAS_OFFLINE; - LOG(LOGS_INFO, "Source %s offline", UTI_IPToString(&inst->remote_addr.ip_addr)); - break; - case MD_BURST_WAS_OFFLINE: - break; - } - -} - -/* ================================================== */ - void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll) { diff --git a/ntp_core.h b/ntp_core.h index f788d68..522d59b 100644 --- a/ntp_core.h +++ b/ntp_core.h @@ -99,12 +99,9 @@ extern void NCR_ProcessTxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Addr /* Slew receive and transmit times in instance records */ extern void NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double doffset); -/* Take a particular source online (i.e. start sampling it) */ -extern void NCR_TakeSourceOnline(NCR_Instance inst); - -/* Take a particular source offline (i.e. stop sampling it, without - marking it unreachable in the source selection stuff) */ -extern void NCR_TakeSourceOffline(NCR_Instance inst); +/* Take a particular source online (i.e. start sampling it) or offline + (i.e. stop sampling it) */ +extern void NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity); extern void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll); diff --git a/ntp_sources.c b/ntp_sources.c index e7fb60f..fa21654 100644 --- a/ntp_sources.c +++ b/ntp_sources.c @@ -860,48 +860,14 @@ slew_sources(struct timespec *raw, /* ================================================== */ int -NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address) -{ - SourceRecord *record; - unsigned int i; - int any; - - NSR_ResolveSources(); - - any = 0; - for (i = 0; i < ARR_GetSize(records); i++) { - record = get_record(i); - if (record->remote_addr) { - if (address->family == IPADDR_UNSPEC || - !UTI_CompareIPs(&record->remote_addr->ip_addr, address, mask)) { - any = 1; - NCR_TakeSourceOnline(record->data); - } - } - } - - if (address->family == IPADDR_UNSPEC) { - struct UnresolvedSource *us; - - for (us = unresolved_sources; us; us = us->next) { - if (us->replacement) - continue; - any = 1; - us->new_source.params.online = 1; - } - } - - return any; -} - -/* ================================================== */ - -int -NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address) +NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity) { SourceRecord *record, *syncpeer; unsigned int i, any; + if (connectivity != SRC_OFFLINE) + NSR_ResolveSources(); + any = 0; syncpeer = NULL; for (i = 0; i < ARR_GetSize(records); i++) { @@ -914,15 +880,14 @@ NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address) syncpeer = record; continue; } - NCR_TakeSourceOffline(record->data); + NCR_SetConnectivity(record->data, connectivity); } } } - /* Take sync peer offline as last to avoid reference switching */ - if (syncpeer) { - NCR_TakeSourceOffline(syncpeer->data); - } + /* Set the sync peer last to avoid unnecessary reference switching */ + if (syncpeer) + NCR_SetConnectivity(syncpeer->data, connectivity); if (address->family == IPADDR_UNSPEC) { struct UnresolvedSource *us; @@ -931,7 +896,7 @@ NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address) if (us->replacement) continue; any = 1; - us->new_source.params.online = 0; + us->new_source.params.connectivity = connectivity; } } diff --git a/ntp_sources.h b/ntp_sources.h index 23e9612..16b62be 100644 --- a/ntp_sources.h +++ b/ntp_sources.h @@ -102,14 +102,9 @@ extern void NSR_Initialise(void); extern void NSR_Finalise(void); /* This routine is used to indicate that sources whose IP addresses - match a particular subnet should be set online again. Returns a - flag indicating whether any hosts matched the address */ -extern int NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address); - -/* This routine is used to indicate that sources whose IP addresses - match a particular subnet should be set offline. Returns a flag - indicating whether any hosts matched the address */ -extern int NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address); + match a particular subnet should be set online or offline. It returns + a flag indicating whether any hosts matched the address. */ +extern int NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity); extern int NSR_ModifyMinpoll(IPAddr *address, int new_minpoll); diff --git a/srcparams.h b/srcparams.h index 7a73bad..48fa19c 100644 --- a/srcparams.h +++ b/srcparams.h @@ -29,10 +29,15 @@ #include "sources.h" +typedef enum { + SRC_OFFLINE, + SRC_ONLINE, +} SRC_Connectivity; + typedef struct { int minpoll; int maxpoll; - int online; + SRC_Connectivity connectivity; int auto_offline; int presend_minpoll; int burst; diff --git a/stubs.c b/stubs.c index eac81c0..00b07ac 100644 --- a/stubs.c +++ b/stubs.c @@ -254,13 +254,7 @@ NSR_GetLocalRefid(IPAddr *address) } int -NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address) -{ - return 0; -} - -int -NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address) +NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity) { return 0; }