ntp: allow online/offline state to be selected by connectability

Allow SRC_MAYBE_ONLINE to be specified for new NTP sources and
connectivity setting to select between SRC_ONLINE and SRC_OFFLINE
according to the result of the connect() system call, i.e. check whether
the client has a route to send its requests.
This commit is contained in:
Miroslav Lichvar 2018-05-24 15:17:53 +02:00
parent ce6b896948
commit 8b9021bf34
4 changed files with 27 additions and 1 deletions

View file

@ -603,7 +603,9 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar
result->rx_timeout_id = 0; result->rx_timeout_id = 0;
result->tx_timeout_id = 0; result->tx_timeout_id = 0;
result->tx_suspended = 1; result->tx_suspended = 1;
result->opmode = params->connectivity == SRC_ONLINE ? MD_ONLINE : MD_OFFLINE; result->opmode = params->connectivity == SRC_ONLINE ||
(params->connectivity == SRC_MAYBE_ONLINE &&
NIO_IsServerConnectable(remote_addr)) ? MD_ONLINE : MD_OFFLINE;
result->local_poll = result->minpoll; result->local_poll = result->minpoll;
result->poll_score = 0.0; result->poll_score = 0.0;
zero_local_timestamp(&result->local_tx); zero_local_timestamp(&result->local_tx);
@ -2281,6 +2283,9 @@ NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity)
s = UTI_IPToString(&inst->remote_addr.ip_addr); s = UTI_IPToString(&inst->remote_addr.ip_addr);
if (connectivity == SRC_MAYBE_ONLINE)
connectivity = NIO_IsServerConnectable(&inst->remote_addr) ? SRC_ONLINE : SRC_OFFLINE;
switch (connectivity) { switch (connectivity) {
case SRC_ONLINE: case SRC_ONLINE:
switch (inst->opmode) { switch (inst->opmode) {

View file

@ -574,6 +574,23 @@ NIO_IsServerSocket(int sock_fd)
/* ================================================== */ /* ================================================== */
int
NIO_IsServerConnectable(NTP_Remote_Address *remote_addr)
{
int sock_fd, r;
sock_fd = prepare_separate_client_socket(remote_addr->ip_addr.family);
if (sock_fd == INVALID_SOCK_FD)
return 0;
r = connect_socket(sock_fd, remote_addr);
close_socket(sock_fd);
return r;
}
/* ================================================== */
static void static void
process_message(struct msghdr *hdr, int length, int sock_fd) process_message(struct msghdr *hdr, int length, int sock_fd)
{ {

View file

@ -53,6 +53,9 @@ extern void NIO_CloseServerSocket(int sock_fd);
/* Function to check if socket is a server socket */ /* Function to check if socket is a server socket */
extern int NIO_IsServerSocket(int sock_fd); extern int NIO_IsServerSocket(int sock_fd);
/* Function to check if client packets can be sent to a server */
extern int NIO_IsServerConnectable(NTP_Remote_Address *remote_addr);
/* Function to transmit a packet */ /* Function to transmit a packet */
extern int NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr, extern int NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
NTP_Local_Address *local_addr, int length, int process_tx); NTP_Local_Address *local_addr, int length, int process_tx);

View file

@ -32,6 +32,7 @@
typedef enum { typedef enum {
SRC_OFFLINE, SRC_OFFLINE,
SRC_ONLINE, SRC_ONLINE,
SRC_MAYBE_ONLINE,
} SRC_Connectivity; } SRC_Connectivity;
typedef struct { typedef struct {