[chrony-dev] [RFC PATCH 4/9] ntp: allow sources with same address and different port

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


With the ptpport directive, a server responds to requests on both the
NTP port and the PTP port, and an NTP source uses NTP over PTP if it is
specified with the port option set to the PTP port. The table of sources
is keyed by the IP address only.

A server or peer cannot be specified with both ports at the same time.
The second source is rejected as already in use, or it stays unresolved
if specified by name. NTP over PTP cannot be enabled incrementally, or
with NTP as a fallback, without giving the server a second address.

Identify sources by their address and port. Include the port in the
hash of the table and require both to match. Change the functions
specifying a source by its address to accept the address with port,
where a zero port selects the source using the NTP port, or any source
with the address. Requests from chronyc use a zero port for now, which
keeps their current behaviour.
---
 cmdmon.c                |  76 ++++++++++++--------
 ntp_sources.c           | 156 +++++++++++++++++++++-------------------
 ntp_sources.h           |  32 +++++----
 sources.c               |  33 +++++----
 sources.h               |   7 +-
 test/unit/ntp_sources.c | 129 +++++++++++++++++++++++++++------
 test/unit/sources.c     |  28 ++++++++
 7 files changed, 306 insertions(+), 155 deletions(-)

diff --git a/cmdmon.c b/cmdmon.c
index 2a79c26..ad67b24 100644
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -317,9 +317,10 @@ handle_burst(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_minpoll(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_minpoll.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_minpoll.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyMinpoll(&address,
                          ntohl(rx_message->data.modify_minpoll.new_minpoll)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -330,9 +331,10 @@ handle_modify_minpoll(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_maxpoll(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_minpoll.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_minpoll.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyMaxpoll(&address,
                          ntohl(rx_message->data.modify_minpoll.new_minpoll)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -343,9 +345,10 @@ handle_modify_maxpoll(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_maxdelay(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_maxdelay.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_maxdelay.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyMaxdelay(&address,
         UTI_FloatNetworkToHost(rx_message->data.modify_maxdelay.new_max_delay)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -356,9 +359,10 @@ handle_modify_maxdelay(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_maxdelayratio(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_maxdelayratio.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_maxdelayratio.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyMaxdelayratio(&address,
         UTI_FloatNetworkToHost(rx_message->data.modify_maxdelayratio.new_max_delay_ratio)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -369,9 +373,10 @@ handle_modify_maxdelayratio(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_maxdelaydevratio(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_maxdelaydevratio.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_maxdelaydevratio.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyMaxdelaydevratio(&address,
         UTI_FloatNetworkToHost(rx_message->data.modify_maxdelaydevratio.new_max_delay_dev_ratio)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -382,9 +387,10 @@ handle_modify_maxdelaydevratio(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_minstratum(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_minstratum.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_minstratum.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyMinstratum(&address,
                             ntohl(rx_message->data.modify_minstratum.new_min_stratum)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -395,9 +401,10 @@ handle_modify_minstratum(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_polltarget(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr address;
+  NTP_Remote_Address address;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_polltarget.address, &address);
+  UTI_IPNetworkToHost(&rx_message->data.modify_polltarget.address, &address.ip_addr);
+  address.port = 0;
   if (!NSR_ModifyPolltarget(&address,
                             ntohl(rx_message->data.modify_polltarget.new_poll_target)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -497,6 +504,7 @@ static void
 handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
   RPT_SourceReport report;
+  NTP_Remote_Address addr;
   struct timespec now_corr;
 
   /* Get data */
@@ -504,7 +512,9 @@ 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:
-        if (!NSR_ReportSource(&report.ip_addr, &report))
+        addr.ip_addr = report.ip_addr;
+        addr.port = report.port;
+        if (!NSR_ReportSource(&addr, &report))
           return;
         break;
       case SRC_REFCLOCK:
@@ -755,11 +765,12 @@ static void
 handle_del_source(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
   NSR_Status status;
-  IPAddr ip_addr;
+  NTP_Remote_Address addr;
   
-  UTI_IPNetworkToHost(&rx_message->data.del_source.ip_addr, &ip_addr);
+  UTI_IPNetworkToHost(&rx_message->data.del_source.ip_addr, &addr.ip_addr);
+  addr.port = 0;
   
-  status = NSR_RemoveSource(&ip_addr);
+  status = NSR_RemoveSource(&addr);
   switch (status) {
     case NSR_Success:
       break;
@@ -1154,6 +1165,7 @@ handle_ntp_data(CMD_Request *rx_message, CMD_Reply *tx_message)
   RPT_NTPReport report;
 
   UTI_IPNetworkToHost(&rx_message->data.ntp_data.ip_addr, &report.remote_addr);
+  report.remote_port = 0;
 
   if (!NSR_GetNTPReport(&report)) {
     tx_message->status = htons(STT_NOSUCHSOURCE);
@@ -1209,10 +1221,11 @@ handle_shutdown(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_ntp_source_name(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  IPAddr addr;
+  NTP_Remote_Address addr;
   char *name;
 
-  UTI_IPNetworkToHost(&rx_message->data.ntp_source_name.ip_addr, &addr);
+  UTI_IPNetworkToHost(&rx_message->data.ntp_source_name.ip_addr, &addr.ip_addr);
+  addr.port = 0;
   name = NSR_GetName(&addr);
 
   if (!name) {
@@ -1257,11 +1270,12 @@ static void
 handle_auth_data(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
   RPT_AuthReport report;
-  IPAddr ip_addr;
+  NTP_Remote_Address addr;
 
-  UTI_IPNetworkToHost(&rx_message->data.auth_data.ip_addr, &ip_addr);
+  UTI_IPNetworkToHost(&rx_message->data.auth_data.ip_addr, &addr.ip_addr);
+  addr.port = 0;
 
-  if (!NSR_GetAuthReport(&ip_addr, &report)) {
+  if (!NSR_GetAuthReport(&addr, &report)) {
     tx_message->status = htons(STT_NOSUCHSOURCE);
     return;
   }
@@ -1337,14 +1351,15 @@ handle_modify_selectopts(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
   int mask, options;
   uint32_t ref_id;
-  IPAddr ip_addr;
+  IPSockAddr addr;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_select_opts.address, &ip_addr);
+  UTI_IPNetworkToHost(&rx_message->data.modify_select_opts.address, &addr.ip_addr);
+  addr.port = 0;
   ref_id = ntohl(rx_message->data.modify_select_opts.ref_id);
   mask = ntohl(rx_message->data.modify_select_opts.mask);
   options = convert_addsrc_select_options(ntohl(rx_message->data.modify_select_opts.options));
 
-  if (!SRC_ModifySelectOptions(&ip_addr, ref_id, options, mask))
+  if (!SRC_ModifySelectOptions(&addr, ref_id, options, mask))
     tx_message->status = htons(STT_NOSUCHSOURCE);
 }
 
@@ -1353,16 +1368,17 @@ handle_modify_selectopts(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_modify_offset(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
+  NTP_Remote_Address addr;
   uint32_t ref_id;
-  IPAddr ip_addr;
   double offset;
 
-  UTI_IPNetworkToHost(&rx_message->data.modify_offset.address, &ip_addr);
+  UTI_IPNetworkToHost(&rx_message->data.modify_offset.address, &addr.ip_addr);
+  addr.port = 0;
   ref_id = ntohl(rx_message->data.modify_offset.ref_id);
   offset = UTI_FloatNetworkToHost(rx_message->data.modify_offset.new_offset);
 
-  if ((ip_addr.family != IPADDR_UNSPEC && !NSR_ModifyOffset(&ip_addr, offset)) ||
-      (ip_addr.family == IPADDR_UNSPEC && !RCL_ModifyOffset(ref_id, offset)))
+  if ((addr.ip_addr.family != IPADDR_UNSPEC && !NSR_ModifyOffset(&addr, offset)) ||
+      (addr.ip_addr.family == IPADDR_UNSPEC && !RCL_ModifyOffset(ref_id, offset)))
     tx_message->status = htons(STT_NOSUCHSOURCE);
 }
 
diff --git a/ntp_sources.c b/ntp_sources.c
index 7545d4f..2adf63f 100644
--- a/ntp_sources.c
+++ b/ntp_sources.c
@@ -232,14 +232,14 @@ NSR_Finalise(void)
 }
 
 /* ================================================== */
-/* Find a slot matching an IP address.  It is assumed that there can
-   only ever be one record for a particular IP address. */
+/* Find a slot matching an IP address and port.  It is assumed that there can
+   only ever be one record for a particular address and port.  Return 1 if
+   found, or 0 and an empty slot if a valid address was provided. */
 
 static int
-find_slot(IPAddr *ip_addr, int *slot)
+find_slot(NTP_Remote_Address *remote_addr, int *slot)
 {
   SourceRecord *record;
-  IPSockAddr addr;
   uint32_t hash;
   unsigned int i, size;
 
@@ -247,7 +247,7 @@ find_slot(IPAddr *ip_addr, int *slot)
 
   *slot = 0;
   
-  switch (ip_addr->family) {
+  switch (remote_addr->ip_addr.family) {
     case IPADDR_INET4:
     case IPADDR_INET6:
     case IPADDR_ID:
@@ -256,9 +256,7 @@ find_slot(IPAddr *ip_addr, int *slot)
       return 0;
   }
 
-  addr.ip_addr = *ip_addr;
-  addr.port = 0;
-  hash = UTI_IPSockAddrToHash(&addr);
+  hash = UTI_IPSockAddrToHash(remote_addr);
 
   for (i = 0; i < size / 2; i++) {
     /* Use quadratic probing */
@@ -268,7 +266,8 @@ find_slot(IPAddr *ip_addr, int *slot)
     if (!record->remote_addr)
       break;
 
-    if (UTI_CompareIPs(&record->remote_addr->ip_addr, ip_addr, NULL) == 0)
+    if (UTI_CompareIPs(&record->remote_addr->ip_addr, &remote_addr->ip_addr, NULL) == 0 &&
+        record->remote_addr->port == remote_addr->port)
       return 1;
   }
 
@@ -276,18 +275,32 @@ find_slot(IPAddr *ip_addr, int *slot)
 }
 
 /* ================================================== */
-/* Find a slot matching an IP address and port. The function returns:
-   0 => IP not matched, empty slot returned if a valid address was provided
-   1 => Only IP matched, port doesn't match
-   2 => Both IP and port matched. */
+/* Find a slot matching an IP address and port.  If the port is zero, prefer
+   a source using the standard NTP port, or return any source with the
+   address. */
 
 static int
-find_slot2(NTP_Remote_Address *remote_addr, int *slot)
+find_matching_slot(NTP_Remote_Address *remote_addr, int *slot)
 {
-  if (!find_slot(&remote_addr->ip_addr, slot))
-    return 0;
+  NTP_Remote_Address addr;
+  SourceRecord *record;
+
+  if (remote_addr->port != 0)
+    return find_slot(remote_addr, slot);
+
+  addr = *remote_addr;
+  addr.port = NTP_PORT;
+  if (find_slot(&addr, slot))
+    return 1;
 
-  return get_record(*slot)->remote_addr->port == remote_addr->port ? 2 : 1;
+  for (*slot = 0; *slot < ARR_GetSize(records); (*slot)++) {
+    record = get_record(*slot);
+    if (record->remote_addr &&
+        UTI_CompareIPs(&record->remote_addr->ip_addr, &remote_addr->ip_addr, NULL) == 0)
+      return 1;
+  }
+
+  return 0;
 }
 
 /* ================================================== */
@@ -328,7 +341,7 @@ rehash_records(void)
     if (!temp_records[i].remote_addr)
       continue;
 
-    if (find_slot2(temp_records[i].remote_addr, &slot) != 0)
+    if (find_slot(temp_records[i].remote_addr, &slot))
       assert(0);
 
     *get_record(slot) = temp_records[i];
@@ -374,8 +387,8 @@ add_source(NTP_Remote_Address *remote_addr, char *name, int family, NTP_Source_T
 
   assert(initialised);
 
-  /* Find empty bin & check that we don't have the address already */
-  if (find_slot2(remote_addr, &slot) != 0) {
+  /* Find empty bin & check that we don't have the address and port already */
+  if (find_slot(remote_addr, &slot)) {
     return NSR_AlreadyInUse;
   } else if (!name && !UTI_IsIPReal(&remote_addr->ip_addr)) {
     /* Name is required for non-real addresses */
@@ -392,7 +405,7 @@ add_source(NTP_Remote_Address *remote_addr, char *name, int family, NTP_Source_T
 
       if (!check_hashtable_size(n_sources, ARR_GetSize(records))) {
         rehash_records();
-        if (find_slot2(remote_addr, &slot) != 0)
+        if (find_slot(remote_addr, &slot))
           assert(0);
       }
 
@@ -437,19 +450,16 @@ static NSR_Status
 change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr,
                       int replacement)
 {
-  int slot1, slot2, found;
+  int slot1, slot2;
   SourceRecord *record;
   LOG_Severity severity;
   char *name;
 
-  found = find_slot2(old_addr, &slot1);
-  if (found != 2)
+  if (!find_slot(old_addr, &slot1))
     return NSR_NoSuchSource;
 
-  /* Make sure there is no other source using the new address (with the same
-     or different port), but allow a source to have its port changed */
-  found = find_slot2(new_addr, &slot2);
-  if (found == 2 || (found != 0 && slot1 != slot2))
+  /* Make sure there is no source using the new address and port */
+  if (find_slot(new_addr, &slot2))
     return NSR_AlreadyInUse;
 
   assert(!record_lock);
@@ -482,10 +492,10 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
   name = record->name;
   severity = UTI_IsIPReal(&old_addr->ip_addr) ? LOGS_INFO : LOGS_DEBUG;
 
-  if (found == 0) {
-    /* The hash table must be rebuilt for the changed address */
-    rehash_records();
+  /* The hash table must be rebuilt for the changed address */
+  rehash_records();
 
+  if (UTI_CompareIPs(&old_addr->ip_addr, &new_addr->ip_addr, NULL) != 0) {
     LOG(severity, "Source %s %s %s (%s)", UTI_IPToString(&old_addr->ip_addr),
         replacement ? "replaced with" : "changed to",
         UTI_IPToString(&new_addr->ip_addr), name);
@@ -549,7 +559,7 @@ process_resolved_name(struct UnresolvedSource *us, IPAddr *ip_addrs, int n_addrs
     assert(us->pool_id == INVALID_POOL);
 
     for (i = 0; i < n_addrs; i++) {
-      if (find_slot2(&us->address, &slot) == 2 &&
+      if (find_slot(&us->address, &slot) &&
           UTI_CompareIPs(&get_record(slot)->resolved_addr, &ip_addrs[i], NULL) == 0) {
         DEBUG_LOG("%s still fresh", UTI_IPToString(&us->address.ip_addr));
         return;
@@ -603,7 +613,7 @@ is_resolved(struct UnresolvedSource *us)
   } else {
     /* If the address is no longer present, it was removed or replaced
        (i.e. resolved) */
-    return find_slot2(&us->address, &slot) == 0;
+    return !find_slot(&us->address, &slot);
   }
 }
 
@@ -1015,17 +1025,15 @@ clean_source_record(SourceRecord *record)
 
 /* ================================================== */
 
-/* Procedure to remove a source.  We don't bother whether the port
-   address is matched - we're only interested in removing a record for
-   the right IP address. */
+/* Procedure to remove a source */
 NSR_Status
-NSR_RemoveSource(IPAddr *address)
+NSR_RemoveSource(NTP_Remote_Address *address)
 {
   int slot;
 
   assert(initialised);
 
-  if (find_slot(address, &slot) == 0)
+  if (!find_matching_slot(address, &slot))
     return NSR_NoSuchSource;
 
   log_source(get_record(slot), 0, 0);
@@ -1110,7 +1118,7 @@ resolve_source_replacement(SourceRecord *record, int refreshment)
 /* ================================================== */
 
 void
-NSR_HandleBadSource(IPAddr *address)
+NSR_HandleBadSource(NTP_Remote_Address *address)
 {
   static double next_replacement = 0.0;
   SourceRecord *record;
@@ -1119,7 +1127,7 @@ NSR_HandleBadSource(IPAddr *address)
   double now;
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return;
 
   record = get_record(slot);
@@ -1210,8 +1218,8 @@ NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr, NTP_Remote_Address *new
   if (!UTI_IsIPReal(&old_addr->ip_addr) || !UTI_IsIPReal(&new_addr->ip_addr))
     return NSR_InvalidAF;
 
-  if (UTI_CompareIPs(&old_addr->ip_addr, &new_addr->ip_addr, NULL) != 0 &&
-      find_slot(&new_addr->ip_addr, &slot))
+  if ((UTI_CompareIPs(&old_addr->ip_addr, &new_addr->ip_addr, NULL) != 0 ||
+       old_addr->port != new_addr->port) && find_slot(new_addr, &slot))
     return NSR_AlreadyInUse;
 
   /* If a record is being modified (e.g. by change_source_address(), or the
@@ -1260,11 +1268,11 @@ static void remove_pool_sources(int pool_id, int tentative, int unresolved)
 /* ================================================== */
 
 uint32_t
-NSR_GetLocalRefid(IPAddr *address)
+NSR_GetLocalRefid(NTP_Remote_Address *address)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   return NCR_GetLocalRefid(get_record(slot)->data);
@@ -1273,11 +1281,11 @@ NSR_GetLocalRefid(IPAddr *address)
 /* ================================================== */
 
 char *
-NSR_GetName(IPAddr *address)
+NSR_GetName(NTP_Remote_Address *address)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return NULL;
 
   return get_record(slot)->name;
@@ -1299,8 +1307,7 @@ NSR_ProcessRx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
 
   /* Avoid unnecessary lookup if the packet cannot be a response from our
      source.  Otherwise, it must match both IP address and port number. */
-  if (NTP_LVM_TO_MODE(message->lvm) != MODE_CLIENT &&
-      find_slot2(remote_addr, &slot) == 2) {
+  if (NTP_LVM_TO_MODE(message->lvm) != MODE_CLIENT && find_slot(remote_addr, &slot)) {
     record = get_record(slot);
 
     if (!NCR_ProcessRxKnown(record->data, local_addr, rx_ts, message, length))
@@ -1340,8 +1347,7 @@ NSR_ProcessTx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
 
   /* Avoid unnecessary lookup if the packet cannot be a request to our
      source.  Otherwise, it must match both IP address and port number. */
-  if (NTP_LVM_TO_MODE(message->lvm) != MODE_SERVER &&
-      find_slot2(remote_addr, &slot) == 2) {
+  if (NTP_LVM_TO_MODE(message->lvm) != MODE_SERVER && find_slot(remote_addr, &slot)) {
     record = get_record(slot);
     NCR_ProcessTxKnown(record->data, local_addr, tx_ts, message, length);
   } else {
@@ -1416,11 +1422,11 @@ NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity
 /* ================================================== */
 
 int
-NSR_ModifyMinpoll(IPAddr *address, int new_minpoll)
+NSR_ModifyMinpoll(NTP_Remote_Address *address, int new_minpoll)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyMinpoll(get_record(slot)->data, new_minpoll);
@@ -1430,11 +1436,11 @@ NSR_ModifyMinpoll(IPAddr *address, int new_minpoll)
 /* ================================================== */
 
 int
-NSR_ModifyMaxpoll(IPAddr *address, int new_maxpoll)
+NSR_ModifyMaxpoll(NTP_Remote_Address *address, int new_maxpoll)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyMaxpoll(get_record(slot)->data, new_maxpoll);
@@ -1444,11 +1450,11 @@ NSR_ModifyMaxpoll(IPAddr *address, int new_maxpoll)
 /* ================================================== */
 
 int
-NSR_ModifyMaxdelay(IPAddr *address, double new_max_delay)
+NSR_ModifyMaxdelay(NTP_Remote_Address *address, double new_max_delay)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyMaxdelay(get_record(slot)->data, new_max_delay);
@@ -1458,11 +1464,11 @@ NSR_ModifyMaxdelay(IPAddr *address, double new_max_delay)
 /* ================================================== */
 
 int
-NSR_ModifyMaxdelayratio(IPAddr *address, double new_max_delay_ratio)
+NSR_ModifyMaxdelayratio(NTP_Remote_Address *address, double new_max_delay_ratio)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyMaxdelayratio(get_record(slot)->data, new_max_delay_ratio);
@@ -1472,11 +1478,11 @@ NSR_ModifyMaxdelayratio(IPAddr *address, double new_max_delay_ratio)
 /* ================================================== */
 
 int
-NSR_ModifyMaxdelaydevratio(IPAddr *address, double new_max_delay_dev_ratio)
+NSR_ModifyMaxdelaydevratio(NTP_Remote_Address *address, double new_max_delay_dev_ratio)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyMaxdelaydevratio(get_record(slot)->data, new_max_delay_dev_ratio);
@@ -1486,11 +1492,11 @@ NSR_ModifyMaxdelaydevratio(IPAddr *address, double new_max_delay_dev_ratio)
 /* ================================================== */
 
 int
-NSR_ModifyMinstratum(IPAddr *address, int new_min_stratum)
+NSR_ModifyMinstratum(NTP_Remote_Address *address, int new_min_stratum)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyMinstratum(get_record(slot)->data, new_min_stratum);
@@ -1500,11 +1506,11 @@ NSR_ModifyMinstratum(IPAddr *address, int new_min_stratum)
 /* ================================================== */
 
 int
-NSR_ModifyOffset(IPAddr *address, double new_offset)
+NSR_ModifyOffset(NTP_Remote_Address *address, double new_offset)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyOffset(get_record(slot)->data, new_offset);
@@ -1514,11 +1520,11 @@ NSR_ModifyOffset(IPAddr *address, double new_offset)
 /* ================================================== */
 
 int
-NSR_ModifyPolltarget(IPAddr *address, int new_poll_target)
+NSR_ModifyPolltarget(NTP_Remote_Address *address, int new_poll_target)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ModifyPolltarget(get_record(slot)->data, new_poll_target);
@@ -1554,11 +1560,11 @@ NSR_InitiateSampleBurst(int n_good_samples, int n_total_samples,
 /* ================================================== */
 
 int
-NSR_ReportSource(IPAddr *ip_addr, RPT_SourceReport *report)
+NSR_ReportSource(NTP_Remote_Address *address, RPT_SourceReport *report)
 {
   int slot;
 
-  if (!find_slot(ip_addr, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_ReportSource(get_record(slot)->data, report);
@@ -1568,11 +1574,11 @@ NSR_ReportSource(IPAddr *ip_addr, RPT_SourceReport *report)
 /* ================================================== */
 
 int
-NSR_GetAuthReport(IPAddr *address, RPT_AuthReport *report)
+NSR_GetAuthReport(NTP_Remote_Address *address, RPT_AuthReport *report)
 {
   int slot;
 
-  if (!find_slot(address, &slot))
+  if (!find_matching_slot(address, &slot))
     return 0;
 
   NCR_GetAuthReport(get_record(slot)->data, report);
@@ -1580,15 +1586,19 @@ NSR_GetAuthReport(IPAddr *address, RPT_AuthReport *report)
 }
 
 /* ================================================== */
-/* The ip address is assumed to be completed on input, that is how we
-   identify the source record. */
+/* The ip address and port (zero if unknown) are assumed to be completed
+   on input, that is how we identify the source record. */
 
 int
 NSR_GetNTPReport(RPT_NTPReport *report)
 {
+  NTP_Remote_Address remote_addr;
   int slot;
 
-  if (!find_slot(&report->remote_addr, &slot))
+  remote_addr.ip_addr = report->remote_addr;
+  remote_addr.port = report->remote_port;
+
+  if (!find_matching_slot(&remote_addr, &slot))
     return 0;
 
   NCR_GetNTPReport(get_record(slot)->data, report);
diff --git a/ntp_sources.h b/ntp_sources.h
index d16a2a1..7f0dbdd 100644
--- a/ntp_sources.h
+++ b/ntp_sources.h
@@ -81,8 +81,12 @@ extern void NSR_StartSources(void);
 /* Start new sources automatically */
 extern void NSR_AutoStartSources(void);
 
+/* In the functions below, sources are specified by their address and port.
+   If the port is zero, the source using the standard NTP port is selected,
+   or any source with the address if there is no such source. */
+
 /* Procedure to remove a source */
-extern NSR_Status NSR_RemoveSource(IPAddr *address);
+extern NSR_Status NSR_RemoveSource(NTP_Remote_Address *address);
 
 /* Procedure to remove all sources matching a configuration ID */
 extern void NSR_RemoveSourcesById(uint32_t conf_id);
@@ -91,7 +95,7 @@ extern void NSR_RemoveSourcesById(uint32_t conf_id);
 extern void NSR_RemoveAllSources(void);
 
 /* Procedure to try to find a replacement for a bad source */
-extern void NSR_HandleBadSource(IPAddr *address);
+extern void NSR_HandleBadSource(NTP_Remote_Address *address);
 
 /* Procedure to resolve all names again */
 extern void NSR_RefreshAddresses(void);
@@ -102,11 +106,11 @@ extern NSR_Status NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr,
                                              NTP_Remote_Address *new_addr);
 
 /* Procedure to get local reference ID corresponding to a source */
-extern uint32_t NSR_GetLocalRefid(IPAddr *address);
+extern uint32_t NSR_GetLocalRefid(NTP_Remote_Address *address);
 
 /* Procedure to get the name of a source as it was specified (it may be
    an IP address) */
-extern char *NSR_GetName(IPAddr *address);
+extern char *NSR_GetName(NTP_Remote_Address *address);
 
 /* This routine is called by ntp_io when a new packet arrives off the network */
 extern void NSR_ProcessRx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
@@ -128,27 +132,27 @@ extern void NSR_Finalise(void);
    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);
+extern int NSR_ModifyMinpoll(NTP_Remote_Address *address, int new_minpoll);
 
-extern int NSR_ModifyMaxpoll(IPAddr *address, int new_maxpoll);
+extern int NSR_ModifyMaxpoll(NTP_Remote_Address *address, int new_maxpoll);
 
-extern int NSR_ModifyMaxdelay(IPAddr *address, double new_max_delay);
+extern int NSR_ModifyMaxdelay(NTP_Remote_Address *address, double new_max_delay);
 
-extern int NSR_ModifyMaxdelayratio(IPAddr *address, double new_max_delay_ratio);
+extern int NSR_ModifyMaxdelayratio(NTP_Remote_Address *address, double new_max_delay_ratio);
 
-extern int NSR_ModifyMaxdelaydevratio(IPAddr *address, double new_max_delay_ratio);
+extern int NSR_ModifyMaxdelaydevratio(NTP_Remote_Address *address, double new_max_delay_ratio);
 
-extern int NSR_ModifyMinstratum(IPAddr *address, int new_min_stratum);
+extern int NSR_ModifyMinstratum(NTP_Remote_Address *address, int new_min_stratum);
 
-extern int NSR_ModifyOffset(IPAddr *address, double new_offset);
+extern int NSR_ModifyOffset(NTP_Remote_Address *address, double new_offset);
 
-extern int NSR_ModifyPolltarget(IPAddr *address, int new_poll_target);
+extern int NSR_ModifyPolltarget(NTP_Remote_Address *address, int new_poll_target);
 
 extern int NSR_InitiateSampleBurst(int n_good_samples, int n_total_samples, IPAddr *mask, IPAddr *address);
 
-extern int NSR_ReportSource(IPAddr *ip_addr, RPT_SourceReport *report);
+extern int NSR_ReportSource(NTP_Remote_Address *address, RPT_SourceReport *report);
 
-extern int NSR_GetAuthReport(IPAddr *address, RPT_AuthReport *report);
+extern int NSR_GetAuthReport(NTP_Remote_Address *address, RPT_AuthReport *report);
 
 extern int NSR_GetNTPReport(RPT_NTPReport *report);
 
diff --git a/sources.c b/sources.c
index 3df5ba6..f3b5c5f 100644
--- a/sources.c
+++ b/sources.c
@@ -480,7 +480,7 @@ handle_bad_source(SRC_Instance inst)
 {
   if (inst->type == SRC_NTP) {
     DEBUG_LOG("Bad source status=%c", get_status_char(inst->status));
-    NSR_HandleBadSource(&inst->addr->ip_addr);
+    NSR_HandleBadSource(inst->addr);
   }
 }
 
@@ -639,7 +639,7 @@ log_selection_source(LOG_Severity severity, SRC_Instance inst, const char *forma
   s[1] = 's';
 
   name = source_to_string(inst);
-  ntp_name = inst->type == SRC_NTP ? NSR_GetName(&inst->addr->ip_addr) : NULL;
+  ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->addr) : NULL;
 
   if (ntp_name && strcmp(name, ntp_name) != 0)
     snprintf(buf, sizeof (buf), "%s (%s)", name, ntp_name);
@@ -1090,7 +1090,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
 
   /* If no selectable source is available, consider the orphan source */
   if (!n_sel_sources && orphan_source != INVALID_SOURCE) {
-    uint32_t local_ref_id = NSR_GetLocalRefid(&sources[orphan_source]->addr->ip_addr);
+    uint32_t local_ref_id = NSR_GetLocalRefid(sources[orphan_source]->addr);
 
     if (!local_ref_id) {
       LOG(LOGS_ERR, "Unknown local refid in orphan mode");
@@ -1557,7 +1557,7 @@ save_source(SRC_Instance inst)
   if (!f)
     return;
 
-  ntp_name = inst->type == SRC_NTP ? NSR_GetName(&inst->addr->ip_addr) : ".";
+  ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->addr) : ".";
 
   if (fprintf(f, "%s%s\n%d %o %d %d %d\n",
               DUMP_IDENTIFIER, ntp_name, inst->authenticated,
@@ -1608,7 +1608,7 @@ load_source(SRC_Instance inst)
   if (!f)
     return;
 
-  ntp_name = inst->type == SRC_NTP ? NSR_GetName(&inst->addr->ip_addr) : NULL;
+  ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->addr) : NULL;
 
   if (!fgets(line, sizeof (line), f) || strcmp(line, DUMP_IDENTIFIER) != 0 ||
       !fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 1 ||
@@ -1751,29 +1751,36 @@ SRC_ActiveSources(void)
 /* ================================================== */
 
 static SRC_Instance
-find_source(IPAddr *ip, uint32_t ref_id)
+find_source(IPSockAddr *addr, uint32_t ref_id)
 {
+  SRC_Instance inst = NULL;
   int i;
 
   for (i = 0; i < n_sources; i++) {
-    if ((ip->family != IPADDR_UNSPEC && sources[i]->type == SRC_NTP &&
-         UTI_CompareIPs(ip, &sources[i]->addr->ip_addr, NULL) == 0) ||
-        (ip->family == IPADDR_UNSPEC && sources[i]->type == SRC_REFCLOCK &&
-         ref_id == sources[i]->ref_id))
+    if (addr->ip_addr.family != IPADDR_UNSPEC && sources[i]->type == SRC_NTP &&
+        UTI_CompareIPs(&addr->ip_addr, &sources[i]->addr->ip_addr, NULL) == 0) {
+      /* Without a port, prefer the source using the NTP port */
+      if (sources[i]->addr->port == (addr->port != 0 ? addr->port : NTP_PORT))
+        return sources[i];
+      if (addr->port == 0 && !inst)
+        inst = sources[i];
+    } else if (addr->ip_addr.family == IPADDR_UNSPEC && sources[i]->type == SRC_REFCLOCK &&
+               ref_id == sources[i]->ref_id) {
       return sources[i];
+    }
   }
 
-  return NULL;
+  return inst;
 }
 
 /* ================================================== */
 
 int
-SRC_ModifySelectOptions(IPAddr *ip, uint32_t ref_id, int options, int mask)
+SRC_ModifySelectOptions(IPSockAddr *addr, uint32_t ref_id, int options, int mask)
 {
   SRC_Instance inst;
 
-  inst = find_source(ip, ref_id);
+  inst = find_source(addr, ref_id);
   if (!inst)
     return 0;
 
diff --git a/sources.h b/sources.h
index ef9e3e0..2d26529 100644
--- a/sources.h
+++ b/sources.h
@@ -132,9 +132,10 @@ extern int SRC_IsReachable(SRC_Instance inst);
 extern int SRC_ReadNumberOfSources(void);
 extern int SRC_ActiveSources(void);
 
-/* Modify selection options of an NTP source specified by address, or
-   refclock specified by its reference ID */
-extern int SRC_ModifySelectOptions(IPAddr *ip, uint32_t ref_id, int options, int mask);
+/* Modify selection options of an NTP source specified by address and port
+   (preferring the NTP port if zero), or refclock specified by its reference
+   ID */
+extern int SRC_ModifySelectOptions(IPSockAddr *addr, uint32_t ref_id, int options, int mask);
 
 extern int SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now);
 extern int SRC_ReportSourcestats(int index, RPT_SourcestatsReport *report, struct timespec *now);
diff --git a/test/unit/ntp_sources.c b/test/unit/ntp_sources.c
index 4094b21..9625840 100644
--- a/test/unit/ntp_sources.c
+++ b/test/unit/ntp_sources.c
@@ -91,10 +91,13 @@ update_random_address(NTP_Remote_Address *addr, int rand_bits)
   return status == NSR_Success;
 }
 
+static int random_address_updates = 1;
+
 static void
 change_remote_address(NCR_Instance inst, NTP_Remote_Address *remote_addr, int ntp_only)
 {
-  int update = !ntp_only && random() % 4 == 0, update_pos = random() % 2, r = 0;
+  int update = random_address_updates && !ntp_only && random() % 4 == 0;
+  int update_pos = random() % 2, r = 0;
 
   TEST_CHECK(record_lock);
 
@@ -125,7 +128,7 @@ test_unit(void)
   char source_line[] = "127.0.0.1 offline", conf[] = "port 0", name[64];
   int i, j, k, family, slot, found, pool, prev_n;
   uint32_t hash = 0, conf_id;
-  NTP_Remote_Address addrs[256], addr;
+  NTP_Remote_Address addrs[256], addr, addr2;
   NTP_Local_Address local_addr;
   NTP_Local_Timestamp local_ts;
   struct UnresolvedSource *us;
@@ -159,12 +162,17 @@ test_unit(void)
     for (j = 0; j < sizeof (addrs) / sizeof (addrs[0]); j++) {
       while (1) {
         do {
-          TST_GetRandomAddress(&addrs[j].ip_addr, IPADDR_UNSPEC, -1);
-          addrs[j].port = 0;
+          /* Reuse an address of a previous source with a different port */
+          if (j > 0 && random() % 4 == 0)
+            addrs[j].ip_addr = addrs[random() % j].ip_addr;
+          else
+            TST_GetRandomAddress(&addrs[j].ip_addr, IPADDR_UNSPEC, -1);
+          addrs[j].port = random() % 1023 + 1;
         } while (UTI_IPSockAddrToHash(&addrs[j]) % (1U << i) != hash % (1U << i));
 
         for (k = 0; k < j; k++)
-          if (UTI_CompareIPs(&addrs[k].ip_addr, &addrs[j].ip_addr, NULL) == 0)
+          if (UTI_CompareIPs(&addrs[k].ip_addr, &addrs[j].ip_addr, NULL) == 0 &&
+              addrs[k].port == addrs[j].port)
             break;
         if (k == j)
           break;
@@ -173,29 +181,34 @@ test_unit(void)
       if (!j)
         hash = UTI_IPSockAddrToHash(&addrs[j]);
 
-      DEBUG_LOG("adding source %s hash %"PRIu32, UTI_IPToString(&addrs[j].ip_addr),
+      DEBUG_LOG("adding source %s hash %"PRIu32, UTI_IPSockAddrToString(&addrs[j]),
                 UTI_IPSockAddrToHash(&addrs[j]) % (1U << i));
 
-      addrs[j].port = random() % 1024;
-
       status = NSR_AddSource(&addrs[j], random() % 2 ? NTP_SERVER : NTP_PEER,
                              &source.params, NULL);
       TEST_CHECK(status == NSR_Success);
       TEST_CHECK(n_sources == j + 1);
 
-      TEST_CHECK(strcmp(NSR_GetName(&addrs[j].ip_addr), UTI_IPToString(&addrs[j].ip_addr)) == 0);
+      TEST_CHECK(strcmp(NSR_GetName(&addrs[j]), UTI_IPToString(&addrs[j].ip_addr)) == 0);
 
       for (k = 0; k <= j; k++) {
         addr = addrs[k];
-        found = find_slot2(&addr, &slot);
-        TEST_CHECK(found == 2);
+        found = find_slot(&addr, &slot);
+        TEST_CHECK(found);
         TEST_CHECK(!UTI_CompareIPs(&get_record(slot)->remote_addr->ip_addr,
                                    &addr.ip_addr, NULL));
-        addr.port++;
-        found = find_slot2(&addr, &slot);
-        TEST_CHECK(found == 1);
+        TEST_CHECK(get_record(slot)->remote_addr->port == addr.port);
+
+        addr.port = 0;
+        found = find_matching_slot(&addr, &slot);
+        TEST_CHECK(found);
         TEST_CHECK(!UTI_CompareIPs(&get_record(slot)->remote_addr->ip_addr,
                                    &addr.ip_addr, NULL));
+
+        addr.port = addrs[k].port + 1024;
+        found = find_slot(&addr, &slot);
+        TEST_CHECK(!found);
+        TEST_CHECK(!find_matching_slot(&addr, &slot));
       }
 
       status = NSR_AddSource(&addrs[j], NTP_SERVER, &source.params, &conf_id);
@@ -203,12 +216,12 @@ test_unit(void)
     }
 
     for (j = 0; j < sizeof (addrs) / sizeof (addrs[0]); j++) {
-      DEBUG_LOG("removing source %s", UTI_IPToString(&addrs[j].ip_addr));
-      NSR_RemoveSource(&addrs[j].ip_addr);
+      DEBUG_LOG("removing source %s", UTI_IPSockAddrToString(&addrs[j]));
+      NSR_RemoveSource(&addrs[j]);
 
       for (k = 0; k < sizeof (addrs) / sizeof (addrs[0]); k++) {
-        found = find_slot2(&addrs[k], &slot);
-        TEST_CHECK(found == (k <= j ? 0 : 2));
+        found = find_slot(&addrs[k], &slot);
+        TEST_CHECK(found == (k > j));
       }
     }
   }
@@ -232,7 +245,7 @@ test_unit(void)
       prev_n = n_sources;
 
       DEBUG_LOG("%d/%d adding source %s pool=%d", i, j, name, pool);
-      status = NSR_AddSourceByName(name, family, 0, pool,
+      status = NSR_AddSourceByName(name, family, random() % 2 ? 123 : 319, pool,
                                    random() % 2 ? NTP_SERVER : NTP_PEER,
                                    &source.params, &conf_id);
       TEST_CHECK(status == NSR_UnresolvedName);
@@ -247,8 +260,8 @@ test_unit(void)
       if (pool) {
         TEST_CHECK(us->address.ip_addr.family == IPADDR_UNSPEC && us->pool_id >= 0);
       } else {
-        TEST_CHECK(strcmp(NSR_GetName(&us->address.ip_addr), name) == 0);
-        TEST_CHECK(find_slot2(&us->address, &slot) == 2);
+        TEST_CHECK(strcmp(NSR_GetName(&us->address), name) == 0);
+        TEST_CHECK(find_slot(&us->address, &slot));
         TEST_CHECK(get_record(slot)->family == family);
       }
 
@@ -300,7 +313,7 @@ test_unit(void)
                           &local_ts, &msg, 0);
             break;
           case 2:
-            NSR_HandleBadSource(&get_record(slot)->remote_addr->ip_addr);
+            NSR_HandleBadSource(get_record(slot)->remote_addr);
             break;
           case 3:
             NSR_SetConnectivity(NULL, &get_record(slot)->remote_addr->ip_addr, SRC_OFFLINE);
@@ -360,6 +373,78 @@ test_unit(void)
     TEST_CHECK(!unresolved_sources);
   }
 
+  /* Sources with the same name and different ports resolving to the same
+     address (e.g. NTP and NTP-over-PTP) */
+  random_address_updates = 0;
+  TEST_CHECK(n_sources == 0);
+  for (i = 0; i < 2; i++) {
+    status = NSR_AddSourceByName("ntp.example.net", IPADDR_UNSPEC, i ? 319 : 123, 0,
+                                 i ? NTP_PEER : NTP_SERVER, &source.params, &conf_id);
+    TEST_CHECK(status == NSR_UnresolvedName);
+  }
+  TEST_CHECK(n_sources == 2);
+
+  TST_GetRandomAddress(&addr.ip_addr, IPADDR_UNSPEC, -1);
+
+  for (i = 0; i < 1000 && unresolved_sources; i++) {
+    if (!requested_name) {
+      if (resolving_id) {
+        SCH_RemoveTimeout(resolving_id);
+        resolve_sources_timeout(NULL);
+      } else {
+        NSR_ResolveSources();
+      }
+    }
+    if (requested_name) {
+      requested_name = NULL;
+      (resolve_handler)(DNS_Success, 1, &addr.ip_addr, resolve_handler_arg);
+    }
+  }
+
+  TEST_CHECK(!unresolved_sources);
+  TEST_CHECK(n_sources == 2);
+
+  for (i = 0; i < 2; i++) {
+    addr.port = i ? 319 : 123;
+    TEST_CHECK(find_slot(&addr, &slot));
+    TEST_CHECK(strcmp(get_record(slot)->name, "ntp.example.net") == 0);
+    TEST_CHECK(strcmp(NSR_GetName(&addr), "ntp.example.net") == 0);
+  }
+
+  addr.port = 0;
+  TEST_CHECK(find_matching_slot(&addr, &slot));
+  TEST_CHECK(get_record(slot)->remote_addr->port == 123);
+
+  addr.port = 123;
+  addr2 = addr;
+  addr2.port = 319;
+  TEST_CHECK(NSR_UpdateSourceNtpAddress(&addr, &addr2) == NSR_AlreadyInUse);
+  addr2.port = 4123;
+  TEST_CHECK(NSR_UpdateSourceNtpAddress(&addr, &addr2) == NSR_Success);
+  TEST_CHECK(!find_slot(&addr, &slot));
+  TEST_CHECK(find_slot(&addr2, &slot));
+  addr.port = 319;
+  TEST_CHECK(find_slot(&addr, &slot));
+  TEST_CHECK(n_sources == 2);
+
+  addr.port = 0;
+  TEST_CHECK(NSR_ModifyMinpoll(&addr, 4));
+  addr.port = 319;
+  TEST_CHECK(NSR_ModifyMinpoll(&addr, 5));
+  addr.port = 123;
+  TEST_CHECK(!NSR_ModifyMinpoll(&addr, 6));
+  TEST_CHECK(NSR_RemoveSource(&addr) == NSR_NoSuchSource);
+
+  addr.port = 319;
+  TEST_CHECK(NSR_RemoveSource(&addr) == NSR_Success);
+  TEST_CHECK(n_sources == 1);
+  TEST_CHECK(find_slot(&addr2, &slot));
+
+  addr.port = 0;
+  TEST_CHECK(NSR_RemoveSource(&addr) == NSR_Success);
+  TEST_CHECK(n_sources == 0);
+  TEST_CHECK(NSR_RemoveSource(&addr) == NSR_NoSuchSource);
+
   NSR_Finalise();
   REF_Finalise();
   NCR_Finalise();
diff --git a/test/unit/sources.c b/test/unit/sources.c
index 0e3678f..9701c3b 100644
--- a/test/unit/sources.c
+++ b/test/unit/sources.c
@@ -281,6 +281,34 @@ test_unit(void)
     }
   }
 
+  /* Sources with the same address and different ports */
+  TST_GetRandomAddress(&addrs[0].ip_addr, IPADDR_UNSPEC, -1);
+  addrs[0].port = 123;
+  addrs[1] = addrs[0];
+  addrs[1].port = 319;
+
+  for (j = 0; j < 2; j++)
+    srcs[j] = SRC_CreateNewInstance(UTI_IPToRefid(&addrs[j].ip_addr), SRC_NTP, 0, 0, &addrs[j],
+                                    SRC_DEFAULT_MINSAMPLES, SRC_DEFAULT_MAXSAMPLES, 0.0, 1.0,
+                                    SRC_DEFAULT_MAXUNREACH);
+
+  addrs[2] = addrs[0];
+  addrs[2].port = 319;
+  TEST_CHECK(SRC_ModifySelectOptions(&addrs[2], 0, SRC_SELECT_PREFER, SRC_SELECT_PREFER));
+  TEST_CHECK(!(srcs[0]->conf_sel_options & SRC_SELECT_PREFER));
+  TEST_CHECK(srcs[1]->conf_sel_options & SRC_SELECT_PREFER);
+
+  addrs[2].port = 0;
+  TEST_CHECK(SRC_ModifySelectOptions(&addrs[2], 0, SRC_SELECT_TRUST, SRC_SELECT_TRUST));
+  TEST_CHECK(srcs[0]->conf_sel_options & SRC_SELECT_TRUST);
+  TEST_CHECK(!(srcs[1]->conf_sel_options & SRC_SELECT_TRUST));
+
+  addrs[2].port = 124;
+  TEST_CHECK(!SRC_ModifySelectOptions(&addrs[2], 0, 0, SRC_SELECT_TRUST));
+
+  for (j = 1; j >= 0; j--)
+    SRC_DestroyInstance(srcs[j]);
+
   NSR_Finalise();
   REF_Finalise();
   SRC_Finalise();
-- 
2.43.0


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/