]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: allow online/offline state to be selected by connectability
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 24 May 2018 13:17:53 +0000 (15:17 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Fri, 25 May 2018 08:53:21 +0000 (10:53 +0200)
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.

ntp_core.c
ntp_io.c
ntp_io.h
srcparams.h

index 45956e70c2116cbc6d8bc1748a440204b544b4e1..c9d096972bb6edf2e867b482d1c7d39b0770c227 100644 (file)
@@ -603,7 +603,9 @@ 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->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->poll_score = 0.0;
   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);
 
+  if (connectivity == SRC_MAYBE_ONLINE)
+    connectivity = NIO_IsServerConnectable(&inst->remote_addr) ? SRC_ONLINE : SRC_OFFLINE;
+
   switch (connectivity) {
     case SRC_ONLINE:
       switch (inst->opmode) {
index 3a3398ac90d160d5324ce50c142d0f3285becccc..8df093ed458d2a3372ee2cfa70b13606dad35899 100644 (file)
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -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
 process_message(struct msghdr *hdr, int length, int sock_fd)
 {
index 1bdcf12d12710964b92cd21cefa3f2fd5af72e91..628f7360871d67b0ee2033a892c7c1e375b39a3d 100644 (file)
--- a/ntp_io.h
+++ b/ntp_io.h
@@ -53,6 +53,9 @@ extern void NIO_CloseServerSocket(int sock_fd);
 /* Function to check if socket is a server socket */
 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 */
 extern int NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
                           NTP_Local_Address *local_addr, int length, int process_tx);
index 48fa19c5ead30aca836583727696249cb849997d..e11edc68df79a61bef39521aea5b324e283e45c3 100644 (file)
@@ -32,6 +32,7 @@
 typedef enum {
   SRC_OFFLINE,
   SRC_ONLINE,
+  SRC_MAYBE_ONLINE,
 } SRC_Connectivity;
 
 typedef struct {