cmdmon: refactor handling of sources report

The NSR_ReportSource() and RCL_ReportSource() functions assume that the
provided report already has some data prefilled by SRC_ReportSource()
and it's assumed these functions cannot fail.

Change them to accept the required data (refid and IP address) as
a parameter, remove unneeded parameters, and return an error status
(if the refid/address doesn't exist) to be handled in cmdmon
handle_source_data(). Also, catch unexpected values of the source state
and mode to make chronyc report an error instead of incorrect data.
This commit is contained in:
Miroslav Lichvar
2025-11-19 14:12:09 +01:00
parent 662484cd0a
commit 0fec9e5cfa
8 changed files with 32 additions and 28 deletions

View File

@@ -504,15 +504,18 @@ handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message)
if (SRC_ReportSource(ntohl(rx_message->data.source_data.index), &report, &now_corr)) {
switch (SRC_GetType(ntohl(rx_message->data.source_data.index))) {
case SRC_NTP:
NSR_ReportSource(&report, &now_corr);
if (!NSR_ReportSource(&report.ip_addr, &report))
return;
break;
case SRC_REFCLOCK:
RCL_ReportSource(&report, &now_corr);
if (report.ip_addr.family != IPADDR_INET4 ||
!RCL_ReportSource(report.ip_addr.addr.in4, &report))
return;
break;
default:
return;
}
tx_message->reply = htons(RPY_SOURCE_DATA);
UTI_IPHostToNetwork(&report.ip_addr, &tx_message->data.source_data.ip_addr);
tx_message->data.source_data.stratum = htons(report.stratum);
tx_message->data.source_data.poll = htons(report.poll);
@@ -535,6 +538,8 @@ handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message)
case RPT_SELECTED:
tx_message->data.source_data.state = htons(RPY_SD_ST_SELECTED);
break;
default:
return;
}
switch (report.mode) {
case RPT_NTP_CLIENT:
@@ -546,6 +551,8 @@ handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message)
case RPT_LOCAL_REFERENCE:
tx_message->data.source_data.mode = htons(RPY_SD_MD_REF);
break;
default:
return;
}
tx_message->data.source_data.flags = htons(0);
tx_message->data.source_data.reachability = htons(report.reachability);
@@ -553,6 +560,8 @@ handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message)
tx_message->data.source_data.orig_latest_meas = UTI_FloatHostToNetwork(report.orig_latest_meas);
tx_message->data.source_data.latest_meas = UTI_FloatHostToNetwork(report.latest_meas);
tx_message->data.source_data.latest_meas_err = UTI_FloatHostToNetwork(report.latest_meas_err);
tx_message->reply = htons(RPY_SOURCE_DATA);
} else {
tx_message->status = htons(STT_NOSUCHSOURCE);
}