]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: add function to get local reference ID
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 30 Mar 2016 09:02:04 +0000 (11:02 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 31 Mar 2016 14:01:02 +0000 (16:01 +0200)
When a valid NTP reply is received, save the local address (e.g. from
IP_PKTINFO), so the reference ID which would the source use for this
host can be calculated when needed.

ntp_core.c
ntp_core.h
ntp_sources.c
ntp_sources.h

index f13ea16884ceeff57236220786525121d79f1d56..8bcd292eaa07f67d2b4e4363269d46c5a1d99b29 100644 (file)
@@ -602,6 +602,7 @@ NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr)
     close_client_socket(inst);
   else {
     NIO_CloseServerSocket(inst->local_addr.sock_fd);
+    inst->local_addr.ip_addr.family = IPADDR_UNSPEC;
     inst->local_addr.sock_fd = NIO_OpenServerSocket(remote_addr);
   }
 
@@ -961,6 +962,7 @@ static void
 transmit_timeout(void *arg)
 {
   NCR_Instance inst = (NCR_Instance) arg;
+  NTP_Local_Address local_addr;
   int sent;
 
   inst->tx_timeout_id = 0;
@@ -996,6 +998,10 @@ transmit_timeout(void *arg)
     inst->local_addr.sock_fd = NIO_OpenClientSocket(&inst->remote_addr);
   }
 
+  /* Don't require the packet to be sent from the same address as before */
+  local_addr.ip_addr.family = IPADDR_UNSPEC;
+  local_addr.sock_fd = inst->local_addr.sock_fd;
+
   /* Check whether we need to 'warm up' the link to the other end by
      sending an NTP exchange to ensure both ends' ARP caches are
      primed.  On loaded systems this might also help ensure that bits
@@ -1009,7 +1015,7 @@ transmit_timeout(void *arg)
        as the reply will be ignored */
     transmit_packet(MODE_CLIENT, inst->local_poll, inst->version, 0, 0,
                     &inst->remote_orig, &inst->local_rx, NULL, NULL,
-                    &inst->remote_addr, &inst->local_addr);
+                    &inst->remote_addr, &local_addr);
 
     inst->presend_done = 1;
 
@@ -1027,7 +1033,7 @@ transmit_timeout(void *arg)
                          &inst->remote_orig,
                          &inst->local_rx, &inst->local_tx, &inst->local_ntp_tx,
                          &inst->remote_addr,
-                         &inst->local_addr);
+                         &local_addr);
 
   ++inst->tx_count;
 
@@ -1450,6 +1456,9 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins
        server and the socket can be closed */
     close_client_socket(inst);
 
+    /* Update the local address */
+    inst->local_addr.ip_addr = local_addr->ip_addr;
+
     requeue_transmit = 1;
   }
 
@@ -2008,6 +2017,14 @@ NCR_GetRemoteAddress(NCR_Instance inst)
 
 /* ================================================== */
 
+uint32_t
+NCR_GetLocalRefid(NCR_Instance inst)
+{
+  return UTI_IPToRefid(&inst->local_addr.ip_addr);
+}
+
+/* ================================================== */
+
 int NCR_IsSyncPeer(NCR_Instance inst)
 {
   return SRC_IsSyncPeer(inst->source);
index db624e19bf8951a759c5428d62669b01a07b24f9..d0af70f096962fcadbac8e530626889309887a5a 100644 (file)
@@ -105,6 +105,8 @@ extern void NCR_IncrementActivityCounters(NCR_Instance inst, int *online, int *o
 
 extern NTP_Remote_Address *NCR_GetRemoteAddress(NCR_Instance instance);
 
+extern uint32_t NCR_GetLocalRefid(NCR_Instance inst);
+
 extern int NCR_IsSyncPeer(NCR_Instance instance);
 
 extern void NCR_AddBroadcastDestination(IPAddr *addr, unsigned short port, int interval);
index 96fb14296ca3a885b1c40f532d8d4775077ba486..1ea193682e08e6772848cff658101ba90fb89492 100644 (file)
@@ -733,6 +733,26 @@ static void remove_tentative_pool_sources(int pool)
     rehash_records();
 }
 
+/* ================================================== */
+
+uint32_t
+NSR_GetLocalRefid(IPAddr *address)
+{
+  NTP_Remote_Address remote_addr;
+  int slot, found;
+
+  remote_addr.ip_addr = *address;
+  remote_addr.port = 0;
+
+  find_slot(&remote_addr, &slot, &found);
+  if (!found)
+    return 0;
+
+  return NCR_GetLocalRefid(get_record(slot)->data);
+}
+
+/* ================================================== */
+
 /* This routine is called by ntp_io when a new packet arrives off the network,
    possibly with an authentication tail */
 void
index 9bd2d3bdd39d757cde79377c0121da22e04a2a37..4d9bbbed6e8e712ea1d15bf7cb5af07796baed41 100644 (file)
@@ -83,6 +83,9 @@ extern void NSR_HandleBadSource(IPAddr *address);
 /* Procedure to resolve all names again */
 extern void NSR_RefreshAddresses(void);
 
+/* Procedure to get local reference ID corresponding to a source */
+extern uint32_t NSR_GetLocalRefid(IPAddr *address);
+
 /* This routine is called by ntp_io when a new packet arrives off the network */
 extern void NSR_ProcessReceive(NTP_Packet *message, struct timeval *now, double now_err, NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr, int length);