[chrony-dev] [RFC PATCH 2/9] util: include port in hash of addresses

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


UTI_IPToHash() hashes an IP address. It is used in the table of NTP
sources and the table of clients in clientlog.

A server cannot be identified by its IP address alone, e.g. with NTP and
NTP over PTP using different ports, and a hash of the address alone
encourages such identification.

Replace UTI_IPToHash() with UTI_IPSockAddrToHash(), which includes the
port in the hash. Use a zero port for clients, which are identified by
their address only, and for NTP sources for now.
---
 clientlog.c             |  7 ++++++-
 ntp_sources.c           |  5 ++++-
 test/unit/ntp_sources.c | 11 ++++++-----
 test/unit/util.c        | 16 +++++++++++-----
 util.c                  |  5 ++++-
 util.h                  |  2 +-
 6 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/clientlog.c b/clientlog.c
index fbdb14b..12d06c0 100644
--- a/clientlog.c
+++ b/clientlog.c
@@ -238,13 +238,18 @@ get_record(IPAddr *ip)
   uint32_t last_hit = 0, oldest_hit = 0;
   Record *record, *oldest_record;
   unsigned int first, i, j;
+  IPSockAddr addr;
 
   if (!active || (ip->family != IPADDR_INET4 && ip->family != IPADDR_INET6))
     return NULL;
 
+  /* Clients are identified by their address only */
+  addr.ip_addr = *ip;
+  addr.port = 0;
+
   while (1) {
     /* Get index of the first record in the slot */
-    first = UTI_IPToHash(ip) % slots * SLOT_SIZE;
+    first = UTI_IPSockAddrToHash(&addr) % slots * SLOT_SIZE;
 
     for (i = 0, oldest_record = NULL; i < SLOT_SIZE; i++) {
       record = ARR_GetElement(records, first + i);
diff --git a/ntp_sources.c b/ntp_sources.c
index c120118..7545d4f 100644
--- a/ntp_sources.c
+++ b/ntp_sources.c
@@ -239,6 +239,7 @@ static int
 find_slot(IPAddr *ip_addr, int *slot)
 {
   SourceRecord *record;
+  IPSockAddr addr;
   uint32_t hash;
   unsigned int i, size;
 
@@ -255,7 +256,9 @@ find_slot(IPAddr *ip_addr, int *slot)
       return 0;
   }
 
-  hash = UTI_IPToHash(ip_addr);
+  addr.ip_addr = *ip_addr;
+  addr.port = 0;
+  hash = UTI_IPSockAddrToHash(&addr);
 
   for (i = 0; i < size / 2; i++) {
     /* Use quadratic probing */
diff --git a/test/unit/ntp_sources.c b/test/unit/ntp_sources.c
index 5038675..4094b21 100644
--- a/test/unit/ntp_sources.c
+++ b/test/unit/ntp_sources.c
@@ -160,7 +160,8 @@ test_unit(void)
       while (1) {
         do {
           TST_GetRandomAddress(&addrs[j].ip_addr, IPADDR_UNSPEC, -1);
-        } while (UTI_IPToHash(&addrs[j].ip_addr) % (1U << i) != hash % (1U << i));
+          addrs[j].port = 0;
+        } 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)
@@ -169,13 +170,13 @@ test_unit(void)
           break;
       }
 
-      addrs[j].port = random() % 1024;
-
       if (!j)
-        hash = UTI_IPToHash(&addrs[j].ip_addr);
+        hash = UTI_IPSockAddrToHash(&addrs[j]);
 
       DEBUG_LOG("adding source %s hash %"PRIu32, UTI_IPToString(&addrs[j].ip_addr),
-                UTI_IPToHash(&addrs[j].ip_addr) % (1U << i));
+                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);
diff --git a/test/unit/util.c b/test/unit/util.c
index 7f8ad0d..b215221 100644
--- a/test/unit/util.c
+++ b/test/unit/util.c
@@ -384,18 +384,21 @@ test_unit(void)
   s = UTI_RefidToString(0x41424344);
   TEST_CHECK(strcmp(s, "ABCD") == 0);
 
+  ip_saddr.port = 123;
   ip.family = IPADDR_UNSPEC;
   s = UTI_IPToString(&ip);
   TEST_CHECK(strcmp(s, "[UNSPEC]") == 0);
   TEST_CHECK(UTI_IPToRefid(&ip) == 0);
-  TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+  ip_saddr.ip_addr = ip;
+  TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
 
   ip.family = IPADDR_INET4;
   ip.addr.in4 = 0x7f010203;
   s = UTI_IPToString(&ip);
   TEST_CHECK(strcmp(s, "127.1.2.3") == 0);
   TEST_CHECK(UTI_IPToRefid(&ip) == 0x7f010203);
-  TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+  ip_saddr.ip_addr = ip;
+  TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
 
   ip.family = IPADDR_INET6;
   memset(&ip.addr.in6, 0, sizeof (ip.addr.in6));
@@ -408,20 +411,23 @@ test_unit(void)
   TEST_CHECK(strcmp(s, "ab00:0000:0000:0000:0000:0000:0000:00cd") == 0);
 #endif
   TEST_CHECK(UTI_IPToRefid(&ip) == 0x5f9aa602);
-  TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+  ip_saddr.ip_addr = ip;
+  TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
 
   ip.family = IPADDR_ID;
   ip.addr.id = 12345;
   s = UTI_IPToString(&ip);
   TEST_CHECK(strcmp(s, "ID#0000012345") == 0);
   TEST_CHECK(UTI_IPToRefid(&ip) == 0);
-  TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+  ip_saddr.ip_addr = ip;
+  TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
 
   ip.family = IPADDR_UNSPEC + 10;
   s = UTI_IPToString(&ip);
   TEST_CHECK(strcmp(s, "[UNKNOWN]") == 0);
   TEST_CHECK(UTI_IPToRefid(&ip) == 0);
-  TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+  ip_saddr.ip_addr = ip;
+  TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
 
   TEST_CHECK(UTI_StringToIP("200.4.5.6", &ip));
   TEST_CHECK(ip.family == IPADDR_INET4);
diff --git a/util.c b/util.c
index 38516ea..cff4c3a 100644
--- a/util.c
+++ b/util.c
@@ -435,9 +435,10 @@ UTI_IPToRefid(const IPAddr *ip)
 /* ================================================== */
 
 uint32_t
-UTI_IPToHash(const IPAddr *ip)
+UTI_IPSockAddrToHash(const IPSockAddr *sa)
 {
   static uint32_t seed = 0;
+  const IPAddr *ip = &sa->ip_addr;
   const unsigned char *addr;
   unsigned int i, len;
   uint32_t hash;
@@ -467,6 +468,8 @@ UTI_IPToHash(const IPAddr *ip)
   /* Calculate the FNV-1a 32-bit hash */
   for (i = 0, hash = seed; i < len; i++)
     hash = (hash ^ addr[i]) * 16777619;
+  hash = (hash ^ (sa->port & 0xff)) * 16777619;
+  hash = (hash ^ (sa->port >> 8)) * 16777619;
 
   return hash;
 }
diff --git a/util.h b/util.h
index 3bebe34..21cccc2 100644
--- a/util.h
+++ b/util.h
@@ -122,7 +122,7 @@ extern int UTI_IsStringIP(const char *string);
 extern int UTI_StringToIdIP(const char *addr, IPAddr *ip);
 extern int UTI_IsIPReal(const IPAddr *ip);
 extern uint32_t UTI_IPToRefid(const IPAddr *ip);
-extern uint32_t UTI_IPToHash(const IPAddr *ip);
+extern uint32_t UTI_IPSockAddrToHash(const IPSockAddr *sa);
 extern void UTI_IPHostToNetwork(const IPAddr *src, IPAddr *dest);
 extern void UTI_IPNetworkToHost(const IPAddr *src, IPAddr *dest);
 extern int UTI_CompareIPs(const IPAddr *a, const IPAddr *b, const IPAddr *mask);
-- 
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/