[chrony-dev] [RFC PATCH 1/9] util: add functions for addresses of NTP sources with port

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


NTP sources are identified by IP address in log messages, in chronyc
input and output, and in the command protocol, which transfers addresses
as IPAddr with an unused 16-bit padding field that is always zero.

Supporting multiple sources with the same address and different ports
requires printing the port, parsing it from user input, and transferring
it between chronyc and chronyd.

Add UTI_NtpAddressToString() printing the port only if it's not the
standard NTP port, UTI_SplitPort() splitting an optional port from an
address or name (IPv6 addresses in brackets), UTI_StringToIPSockAddr()
parsing an address with an optional port, and functions converting an
address with port to and from the command protocol format, which carries
the port in the padding field.
---
 test/unit/util.c | 82 ++++++++++++++++++++++++++++++++++++++++++++-
 util.c           | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 util.h           | 19 +++++++++++
 3 files changed, 186 insertions(+), 1 deletion(-)

diff --git a/test/unit/util.c b/test/unit/util.c
index 6365a5d..7f8ad0d 100644
--- a/test/unit/util.c
+++ b/test/unit/util.c
@@ -39,7 +39,8 @@ test_unit(void)
   struct timeval tv;
   double x, y, nan, inf;
   IPAddr ip, ip2, ip3;
-  IPSockAddr ip_saddr;
+  IPSockAddr ip_saddr, ip_saddr2;
+  uint16_t port;
   Integer64 integer64;
   Timespec tspec;
   Float f;
@@ -527,7 +528,86 @@ test_unit(void)
   ip_saddr.port = 12345;
   s = UTI_IPSockAddrToString(&ip_saddr);
   TEST_CHECK(strcmp(s, "1.2.3.4:12345") == 0);
+  s = UTI_NtpAddressToString(&ip_saddr);
+  TEST_CHECK(strcmp(s, "1.2.3.4:12345") == 0);
+  ip_saddr.port = 123;
+  s = UTI_NtpAddressToString(&ip_saddr);
+  TEST_CHECK(strcmp(s, "1.2.3.4") == 0);
+
+  TEST_CHECK(UTI_StringToIPSockAddr("1.2.3.4", &ip_saddr2));
+  TEST_CHECK(ip_saddr2.ip_addr.family == IPADDR_INET4);
+  TEST_CHECK(ip_saddr2.ip_addr.addr.in4 == 0x01020304);
+  TEST_CHECK(ip_saddr2.port == 0);
+  TEST_CHECK(UTI_StringToIPSockAddr("1.2.3.4:319", &ip_saddr2));
+  TEST_CHECK(ip_saddr2.ip_addr.family == IPADDR_INET4);
+  TEST_CHECK(ip_saddr2.ip_addr.addr.in4 == 0x01020304);
+  TEST_CHECK(ip_saddr2.port == 319);
+  TEST_CHECK(!UTI_StringToIPSockAddr("1.2.3.4:", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("1.2.3.4:0", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("1.2.3.4:65536", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("1.2.3.4:-1", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("1.2.3.4:12a", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("[1.2.3.4]:123", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("1.2.3:123", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("name:123", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("", &ip_saddr2));
+
+  snprintf(buf, sizeof (buf), "ntp.test:319");
+  s = UTI_SplitPort(buf, &port);
+  TEST_CHECK(s == buf && strcmp(s, "ntp.test") == 0 && port == 319);
+  snprintf(buf, sizeof (buf), "ntp.test");
+  s = UTI_SplitPort(buf, &port);
+  TEST_CHECK(s == buf && strcmp(s, "ntp.test") == 0 && port == 0);
+  snprintf(buf, sizeof (buf), "[::1]:123");
+  s = UTI_SplitPort(buf, &port);
+  TEST_CHECK(s == buf + 1 && strcmp(s, "::1") == 0 && port == 123);
+  snprintf(buf, sizeof (buf), "ntp.test:x");
+  TEST_CHECK(!UTI_SplitPort(buf, &port));
+
+  ip_saddr.port = 65535;
+  s = UTI_IPSockAddrToString(&ip_saddr);
+  TEST_CHECK(UTI_StringToIPSockAddr(s, &ip_saddr2));
+  TEST_CHECK(UTI_CompareIPs(&ip_saddr.ip_addr, &ip_saddr2.ip_addr, NULL) == 0);
+  TEST_CHECK(ip_saddr.port == ip_saddr2.port);
 
+#ifdef FEAT_IPV6
+  TEST_CHECK(UTI_StringToIPSockAddr("1234::5678", &ip_saddr2));
+  TEST_CHECK(ip_saddr2.ip_addr.family == IPADDR_INET6);
+  TEST_CHECK(ip_saddr2.port == 0);
+  TEST_CHECK(UTI_StringToIPSockAddr("[1234::5678]", &ip_saddr2));
+  TEST_CHECK(ip_saddr2.ip_addr.family == IPADDR_INET6);
+  TEST_CHECK(ip_saddr2.port == 0);
+  TEST_CHECK(UTI_StringToIPSockAddr("[1234::5678]:319", &ip_saddr2));
+  TEST_CHECK(ip_saddr2.ip_addr.family == IPADDR_INET6);
+  TEST_CHECK(ip_saddr2.ip_addr.addr.in6[0] == 0x12 && ip_saddr2.ip_addr.addr.in6[15] == 0x78);
+  TEST_CHECK(ip_saddr2.port == 319);
+  s = UTI_NtpAddressToString(&ip_saddr2);
+  TEST_CHECK(strcmp(s, "[1234::5678]:319") == 0);
+  TEST_CHECK(!UTI_StringToIPSockAddr("[1234::5678", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("[1234::5678]x", &ip_saddr2));
+  TEST_CHECK(!UTI_StringToIPSockAddr("[1234::5678]:", &ip_saddr2));
+#endif
+
+  for (i = 0; i < 100; i++) {
+    TST_GetRandomAddress(&ip_saddr.ip_addr, IPADDR_UNSPEC, -1);
+    ip_saddr.port = random() % 65536;
+    UTI_IPSockAddrHostToNetwork(&ip_saddr, &ip2);
+    TEST_CHECK(ip2._pad == htons(ip_saddr.port));
+    UTI_IPNetworkToHost(&ip2, &ip3);
+    TEST_CHECK(ip3._pad == 0);
+    TEST_CHECK(UTI_CompareIPs(&ip_saddr.ip_addr, &ip3, NULL) == 0);
+    UTI_IPSockAddrNetworkToHost(&ip2, &ip_saddr2);
+    TEST_CHECK(UTI_CompareIPs(&ip_saddr.ip_addr, &ip_saddr2.ip_addr, NULL) == 0);
+    TEST_CHECK(ip_saddr2.ip_addr._pad == 0);
+    TEST_CHECK(ip_saddr2.port == ip_saddr.port);
+
+    UTI_IPHostToNetwork(&ip_saddr.ip_addr, &ip2);
+    UTI_IPSockAddrNetworkToHost(&ip2, &ip_saddr2);
+    TEST_CHECK(ip_saddr2.port == 0);
+  }
+
+  ip_saddr.ip_addr.family = IPADDR_INET4;
+  ip_saddr.ip_addr.addr.in4 = 0x01020304;
   ip = ip_saddr.ip_addr;
   s = UTI_IPSubnetToString(&ip, 10);
   TEST_CHECK(strcmp(s, "1.2.3.4/10") == 0);
diff --git a/util.c b/util.c
index feee443..38516ea 100644
--- a/util.c
+++ b/util.c
@@ -574,6 +574,92 @@ UTI_IPSockAddrToString(const IPSockAddr *sa)
 
 /* ================================================== */
 
+char *
+UTI_NtpAddressToString(const IPSockAddr *sa)
+{
+  if (sa->port == NTP_PORT)
+    return UTI_IPToString(&sa->ip_addr);
+
+  return UTI_IPSockAddrToString(sa);
+}
+
+/* ================================================== */
+
+char *
+UTI_SplitPort(char *addr, uint16_t *port)
+{
+  char *host, *s, *end;
+  unsigned long x;
+
+  host = addr;
+  *port = 0;
+
+  if (addr[0] == '[') {
+    /* IPv6 address in brackets, optionally followed by a port */
+    host = addr + 1;
+    s = strchr(host, ']');
+    if (!s || (s[1] != '\0' && s[1] != ':'))
+      return NULL;
+    *s++ = '\0';
+  } else {
+    /* Only an address or name with a single colon can have a port */
+    s = strchr(addr, ':');
+    if (s && strchr(s + 1, ':'))
+      s = NULL;
+  }
+
+  if (!s || *s == '\0')
+    return host;
+
+  *s++ = '\0';
+  x = strtoul(s, &end, 10);
+  if (!isdigit((unsigned char)*s) || *end != '\0' || x < 1 || x > 65535)
+    return NULL;
+
+  *port = x;
+
+  return host;
+}
+
+/* ================================================== */
+
+int
+UTI_StringToIPSockAddr(const char *addr, IPSockAddr *sa)
+{
+  char buf[BUFFER_LENGTH], *host;
+
+  if (snprintf(buf, sizeof (buf), "%s", addr) >= sizeof (buf))
+    return 0;
+
+  host = UTI_SplitPort(buf, &sa->port);
+
+  /* Allow brackets only around IPv6 addresses */
+  return host && UTI_StringToIP(host, &sa->ip_addr) &&
+         (host == buf || sa->ip_addr.family == IPADDR_INET6);
+}
+
+/* ================================================== */
+
+void
+UTI_IPSockAddrHostToNetwork(const IPSockAddr *src, IPAddr *dest)
+{
+  UTI_IPHostToNetwork(&src->ip_addr, dest);
+  dest->_pad = htons(src->port);
+}
+
+/* ================================================== */
+
+void
+UTI_IPSockAddrNetworkToHost(const IPAddr *src, IPSockAddr *dest)
+{
+  uint16_t port = ntohs(src->_pad);
+
+  UTI_IPNetworkToHost(src, &dest->ip_addr);
+  dest->port = port;
+}
+
+/* ================================================== */
+
 char *
 UTI_IPSubnetToString(IPAddr *subnet, int bits)
 {
diff --git a/util.h b/util.h
index 7af0016..3bebe34 100644
--- a/util.h
+++ b/util.h
@@ -129,6 +129,25 @@ extern int UTI_CompareIPs(const IPAddr *a, const IPAddr *b, const IPAddr *mask);
 
 extern char *UTI_IPSockAddrToString(const IPSockAddr *sa);
 
+/* Convert an address of an NTP source to string, including the port only
+   if it is different from the standard NTP port */
+extern char *UTI_NtpAddressToString(const IPSockAddr *sa);
+
+/* Split an optional port from an address or name in the form name:port or
+   [address]:port.  Return the address or name, which is terminated in the
+   buffer, or NULL if the port is invalid.  The port is set to zero if not
+   specified. */
+extern char *UTI_SplitPort(char *addr, uint16_t *port);
+
+/* Parse an IP address with an optional port (e.g. 1.2.3.4, 1.2.3.4:123,
+   [1234::5678]:123).  The port is set to zero if not specified. */
+extern int UTI_StringToIPSockAddr(const char *addr, IPSockAddr *sa);
+
+/* Convert an IP address and port to/from the format of the command protocol,
+   which carries the port in the otherwise unused padding of the address */
+extern void UTI_IPSockAddrHostToNetwork(const IPSockAddr *src, IPAddr *dest);
+extern void UTI_IPSockAddrNetworkToHost(const IPAddr *src, IPSockAddr *dest);
+
 extern char *UTI_IPSubnetToString(IPAddr *subnet, int bits);
 
 extern char *UTI_TimeToLogForm(time_t t);
-- 
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/