chronyc commands specify NTP sources by address or name, and commands
like sources, sourcestats, selectdata, authdata, and tracking print the
address or name of the sources. The command protocol carries the port
of NTP sources in addresses.

A user cannot select only one of multiple sources with the same address
and different ports, and the sources are printed with the same name.

Accept an address with port in commands specifying an NTP source, in
the form address:port, [address]:port (IPv6), or name:port. Without the
port, the commands select the source using the standard NTP port. Print
the port after the name of a source if it is not the standard NTP port.
---
 client.c | 190 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 116 insertions(+), 74 deletions(-)

diff --git a/client.c b/client.c
index 09962e1..7bc019a 100644
--- a/client.c
+++ b/client.c
@@ -465,14 +465,31 @@ parse_source_address(char *word, IPAddr *address)
 
 /* ================================================== */
 
+/* Parse an address of an NTP source with an optional port (e.g. 1.2.3.4:319,
+   [1234::5678]:319, or ntp.example.net:319).  A zero port is returned if not
+   specified. */
+
 static int
-parse_source_address_or_refid(char *s, IPAddr *address, uint32_t *ref_id)
+parse_source_sock_address(char *word, IPSockAddr *address)
 {
-  address->family = IPADDR_UNSPEC;
+  char *host;
+
+  host = UTI_SplitPort(word, &address->port);
+
+  return host && parse_source_address(host, &address->ip_addr);
+}
+
+/* ================================================== */
+
+static int
+parse_source_address_or_refid(char *s, IPSockAddr *address, uint32_t *ref_id)
+{
+  address->ip_addr.family = IPADDR_UNSPEC;
+  address->port = 0;
   *ref_id = 0;
 
   /* Don't allow hostnames to avoid conflicts with reference IDs */
-  if (UTI_StringToIdIP(s, address) || UTI_StringToIP(s, address))
+  if (UTI_StringToIdIP(s, &address->ip_addr) || UTI_StringToIPSockAddr(s, 
address))
     return 1;
 
   if (CPS_ParseRefid(s, ref_id) > 0)
@@ -577,7 +594,7 @@ process_cmd_onoffline(CMD_Request *msg, char *line)
 /* ================================================== */
 
 static int
-read_address_integer(char *line, IPAddr *address, int *value)
+read_address_integer(char *line, IPSockAddr *address, int *value)
 {
   char *hostname;
   int ok = 0;
@@ -589,7 +606,7 @@ read_address_integer(char *line, IPAddr *address, int 
*value)
     LOG(LOGS_ERR, "Invalid syntax for address value");
     ok = 0;
   } else {
-    if (!parse_source_address(hostname, address)) {
+    if (!parse_source_sock_address(hostname, address)) {
       LOG(LOGS_ERR, "Could not get address for hostname");
       ok = 0;
     } else {
@@ -605,7 +622,7 @@ read_address_integer(char *line, IPAddr *address, int 
*value)
 /* ================================================== */
 
 static int
-read_address_double(char *line, IPAddr *address, double *value)
+read_address_double(char *line, IPSockAddr *address, double *value)
 {
   char *hostname;
   int ok = 0;
@@ -617,7 +634,7 @@ read_address_double(char *line, IPAddr *address, double 
*value)
     LOG(LOGS_ERR, "Invalid syntax for address value");
     ok = 0;
   } else {
-    if (!parse_source_address(hostname, address)) {
+    if (!parse_source_sock_address(hostname, address)) {
       LOG(LOGS_ERR, "Could not get address for hostname");
       ok = 0;
     } else {
@@ -635,12 +652,12 @@ read_address_double(char *line, IPAddr *address, double 
*value)
 static int
 process_cmd_minpoll(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   int minpoll;
   int ok;
   
   if (read_address_integer(line, &address, &minpoll)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_minpoll.address);
+    UTI_IPSockAddrHostToNetwork(&address, &msg->data.modify_minpoll.address);
     msg->data.modify_minpoll.new_minpoll = htonl(minpoll);
     msg->command = htons(REQ_MODIFY_MINPOLL);
     ok = 1;
@@ -657,12 +674,12 @@ process_cmd_minpoll(CMD_Request *msg, char *line)
 static int
 process_cmd_maxpoll(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   int maxpoll;
   int ok;
   
   if (read_address_integer(line, &address, &maxpoll)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_maxpoll.address);
+    UTI_IPSockAddrHostToNetwork(&address, &msg->data.modify_maxpoll.address);
     msg->data.modify_maxpoll.new_maxpoll = htonl(maxpoll);
     msg->command = htons(REQ_MODIFY_MAXPOLL);
     ok = 1;
@@ -679,12 +696,12 @@ process_cmd_maxpoll(CMD_Request *msg, char *line)
 static int
 process_cmd_maxdelay(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   double max_delay;
   int ok;
   
   if (read_address_double(line, &address, &max_delay)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_maxdelay.address);
+    UTI_IPSockAddrHostToNetwork(&address, &msg->data.modify_maxdelay.address);
     msg->data.modify_maxdelay.new_max_delay = 
UTI_FloatHostToNetwork(max_delay);
     msg->command = htons(REQ_MODIFY_MAXDELAY);
     ok = 1;
@@ -701,12 +718,12 @@ process_cmd_maxdelay(CMD_Request *msg, char *line)
 static int
 process_cmd_maxdelaydevratio(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   double max_delay_dev_ratio;
   int ok;
   
   if (read_address_double(line, &address, &max_delay_dev_ratio)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_maxdelaydevratio.address);
+    UTI_IPSockAddrHostToNetwork(&address, 
&msg->data.modify_maxdelaydevratio.address);
     msg->data.modify_maxdelayratio.new_max_delay_ratio = 
UTI_FloatHostToNetwork(max_delay_dev_ratio);
     msg->command = htons(REQ_MODIFY_MAXDELAYDEVRATIO);
     ok = 1;
@@ -723,12 +740,12 @@ process_cmd_maxdelaydevratio(CMD_Request *msg, char *line)
 static int
 process_cmd_maxdelayratio(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   double max_delay_ratio;
   int ok;
   
   if (read_address_double(line, &address, &max_delay_ratio)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_maxdelayratio.address);
+    UTI_IPSockAddrHostToNetwork(&address, 
&msg->data.modify_maxdelayratio.address);
     msg->data.modify_maxdelayratio.new_max_delay_ratio = 
UTI_FloatHostToNetwork(max_delay_ratio);
     msg->command = htons(REQ_MODIFY_MAXDELAYRATIO);
     ok = 1;
@@ -745,12 +762,12 @@ process_cmd_maxdelayratio(CMD_Request *msg, char *line)
 static int
 process_cmd_minstratum(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   int min_stratum;
   int ok;
   
   if (read_address_integer(line, &address, &min_stratum)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_minstratum.address);
+    UTI_IPSockAddrHostToNetwork(&address, 
&msg->data.modify_minstratum.address);
     msg->data.modify_minstratum.new_min_stratum = htonl(min_stratum);
     msg->command = htons(REQ_MODIFY_MINSTRATUM);
     ok = 1;
@@ -767,12 +784,12 @@ process_cmd_minstratum(CMD_Request *msg, char *line)
 static int
 process_cmd_polltarget(CMD_Request *msg, char *line)
 {
-  IPAddr address;
+  IPSockAddr address;
   int poll_target;
   int ok;
   
   if (read_address_integer(line, &address, &poll_target)) {
-    UTI_IPHostToNetwork(&address, &msg->data.modify_polltarget.address);
+    UTI_IPSockAddrHostToNetwork(&address, 
&msg->data.modify_polltarget.address);
     msg->data.modify_polltarget.new_poll_target = htonl(poll_target);
     msg->command = htons(REQ_MODIFY_POLLTARGET);
     ok = 1;
@@ -1137,7 +1154,7 @@ process_cmd_delete(CMD_Request *msg, char *line)
 {
   char *hostname;
   int ok = 0;
-  IPAddr address;
+  IPSockAddr address;
 
   msg->command = htons(REQ_DEL_SOURCE);
   hostname = line;
@@ -1147,11 +1164,11 @@ process_cmd_delete(CMD_Request *msg, char *line)
     LOG(LOGS_ERR, "Invalid syntax for address");
     ok = 0;
   } else {
-    if (!parse_source_address(hostname, &address)) {
+    if (!parse_source_sock_address(hostname, &address)) {
       LOG(LOGS_ERR, "Could not get address for hostname");
       ok = 0;
     } else {
-      UTI_IPHostToNetwork(&address, &msg->data.del_source.ip_addr);
+      UTI_IPSockAddrHostToNetwork(&address, &msg->data.del_source.ip_addr);
       ok = 1;
     }
   }
@@ -2079,14 +2096,14 @@ print_info_field(const char *format, ...)
 /* ================================================== */
 
 static int
-get_source_name(IPAddr *ip_addr, char *buf, int size)
+get_source_name(IPSockAddr *addr, char *buf, int size)
 {
   CMD_Request request;
   CMD_Reply reply;
   int i;
 
   request.command = htons(REQ_NTP_SOURCE_NAME);
-  UTI_IPHostToNetwork(ip_addr, &request.data.ntp_source_name.ip_addr);
+  UTI_IPSockAddrHostToNetwork(addr, &request.data.ntp_source_name.ip_addr);
   if (!request_reply(&request, &reply, RPY_NTP_SOURCE_NAME, 0) ||
       reply.data.ntp_source_name.name[sizeof (reply.data.ntp_source_name.name) 
- 1] != '\0' ||
       snprintf(buf, size, "%s", (char *)reply.data.ntp_source_name.name) >= 
size)
@@ -2105,21 +2122,43 @@ get_source_name(IPAddr *ip_addr, char *buf, int size)
 
 static void
 format_name(char *buf, int size, int trunc_dns, int ref, uint32_t ref_id,
-            int source, IPAddr *ip_addr)
+            int source, IPSockAddr *addr)
 {
+  char name[256], port[8];
+  int len, port_len;
+
   if (ref) {
     snprintf(buf, size, "%s", UTI_RefidToString(ref_id));
-  } else if (source && source_names) {
-    if (!get_source_name(ip_addr, buf, size))
+    return;
+  }
+
+  if (source && source_names) {
+    if (!get_source_name(addr, name, sizeof (name))) {
       snprintf(buf, size, "?");
+      return;
+    }
+    trunc_dns = 0;
   } else if (no_dns || csv_mode) {
-    snprintf(buf, size, "%s", UTI_IPToString(ip_addr));
+    snprintf(name, sizeof (name), "%s", UTI_IPToString(&addr->ip_addr));
+    trunc_dns = 0;
   } else {
-    DNS_IPAddress2Name(ip_addr, buf, size);
-    if (trunc_dns > 0 && strlen(buf) > trunc_dns) {
-      buf[trunc_dns - 1] = '>';
-      buf[trunc_dns] = '\0';
-    }
+    DNS_IPAddress2Name(&addr->ip_addr, name, sizeof (name));
+  }
+
+  /* Include the port of an NTP source if it is not the standard port */
+  if (addr->port != 0 && addr->port != NTP_PORT)
+    snprintf(port, sizeof (port), ":%hu", addr->port);
+  else
+    port[0] = '\0';
+  port_len = strlen(port);
+
+  snprintf(buf, size, port_len > 0 && strchr(name, ':') ? "[%s]%s" : "%s%s", 
name, port);
+
+  /* Truncate the name, but keep the port */
+  len = strlen(buf);
+  if (trunc_dns > port_len && len > trunc_dns) {
+    memmove(buf + trunc_dns - port_len, buf + len - port_len, port_len + 1);
+    buf[trunc_dns - port_len - 1] = '>';
   }
 }
 
@@ -2147,15 +2186,15 @@ parse_sources_options(char *line, int *all, int 
*verbose)
 static int
 process_cmd_sourcename(char *line)
 {
-  IPAddr ip_addr;
+  IPSockAddr addr;
   char name[256];
 
-  if (!parse_source_address(line, &ip_addr)) {
+  if (!parse_source_sock_address(line, &addr)) {
     LOG(LOGS_ERR, "Could not read address");
     return 0;
   }
 
-  if (!get_source_name(&ip_addr, name, sizeof (name)))
+  if (!get_source_name(&addr, name, sizeof (name)))
     return 0;
 
   print_report("%s\n", name, REPORT_END);
@@ -2170,7 +2209,7 @@ process_cmd_sources(char *line)
 {
   CMD_Request request;
   CMD_Reply reply;
-  IPAddr ip_addr;
+  IPSockAddr addr;
   uint32_t i, mode, n_sources;
   char name[256], mode_ch, state_ch;
   int all, verbose, ref;
@@ -2209,12 +2248,13 @@ process_cmd_sources(char *line)
       return 0;
 
     mode = ntohs(reply.data.source_data.mode);
-    UTI_IPNetworkToHost(&reply.data.source_data.ip_addr, &ip_addr);
-    if (!all && ip_addr.family == IPADDR_ID)
+    UTI_IPSockAddrNetworkToHost(&reply.data.source_data.ip_addr, &addr);
+    if (!all && addr.ip_addr.family == IPADDR_ID)
       continue;
 
-    ref = mode == RPY_SD_MD_REF && ip_addr.family == IPADDR_INET4;
-    format_name(name, sizeof (name), 25 + wide, ref, ref ? ip_addr.addr.in4 : 
0, 1, &ip_addr);
+    ref = mode == RPY_SD_MD_REF && addr.ip_addr.family == IPADDR_INET4;
+    format_name(name, sizeof (name), 25 + wide, ref, ref ? 
addr.ip_addr.addr.in4 : 0, 1,
+                &addr);
 
     switch (mode) {
       case RPY_SD_MD_CLIENT:
@@ -2283,7 +2323,7 @@ process_cmd_sourcestats(char *line)
   uint32_t i, n_sources;
   int all, verbose;
   char name[256];
-  IPAddr ip_addr;
+  IPSockAddr addr;
 
   parse_sources_options(line, &all, &verbose);
 
@@ -2318,12 +2358,12 @@ process_cmd_sourcestats(char *line)
     if (!request_reply(&request, &reply, RPY_SOURCESTATS, 0))
       return 0;
 
-    UTI_IPNetworkToHost(&reply.data.sourcestats.ip_addr, &ip_addr);
-    if (!all && ip_addr.family == IPADDR_ID)
+    UTI_IPSockAddrNetworkToHost(&reply.data.sourcestats.ip_addr, &addr);
+    if (!all && addr.ip_addr.family == IPADDR_ID)
       continue;
 
-    format_name(name, sizeof (name), 25 + wide, ip_addr.family == 
IPADDR_UNSPEC,
-                ntohl(reply.data.sourcestats.ref_id), 1, &ip_addr);
+    format_name(name, sizeof (name), 25 + wide, addr.ip_addr.family == 
IPADDR_UNSPEC,
+                ntohl(reply.data.sourcestats.ref_id), 1, &addr);
 
     print_report("%*s %3U %3U  %I %+P %P  %+S  %S\n",
                  -25 - wide, name,
@@ -2347,7 +2387,7 @@ process_cmd_tracking(char *line)
 {
   CMD_Request request;
   CMD_Reply reply;
-  IPAddr ip_addr;
+  IPSockAddr addr;
   uint32_t ref_id;
   char name[256];
   struct timespec ref_time;
@@ -2358,9 +2398,9 @@ process_cmd_tracking(char *line)
 
   ref_id = ntohl(reply.data.tracking.ref_id);
 
-  UTI_IPNetworkToHost(&reply.data.tracking.ip_addr, &ip_addr);
+  UTI_IPSockAddrNetworkToHost(&reply.data.tracking.ip_addr, &addr);
   format_name(name, sizeof (name), sizeof (name),
-              ip_addr.family == IPADDR_UNSPEC, ref_id, 1, &ip_addr);
+              addr.ip_addr.family == IPADDR_UNSPEC, ref_id, 1, &addr);
 
   UTI_TimespecNetworkToHost(&reply.data.tracking.ref_time, &ref_time);
 
@@ -2401,7 +2441,7 @@ process_cmd_authdata(char *line)
 {
   CMD_Request request;
   CMD_Reply reply;
-  IPAddr ip_addr;
+  IPSockAddr addr;
   uint32_t i, source_mode, n_sources;
   int all, verbose;
   const char *mode_str;
@@ -2440,8 +2480,8 @@ process_cmd_authdata(char *line)
     if (source_mode != RPY_SD_MD_CLIENT && source_mode != RPY_SD_MD_PEER)
       continue;
 
-    UTI_IPNetworkToHost(&reply.data.source_data.ip_addr, &ip_addr);
-    if (!all && ip_addr.family == IPADDR_ID)
+    UTI_IPSockAddrNetworkToHost(&reply.data.source_data.ip_addr, &addr);
+    if (!all && addr.ip_addr.family == IPADDR_ID)
       continue;
 
     request.command = htons(REQ_AUTH_DATA);
@@ -2449,7 +2489,7 @@ process_cmd_authdata(char *line)
     if (!request_reply(&request, &reply, RPY_AUTH_DATA, 0))
       return 0;
 
-    format_name(name, sizeof (name), 25 + wide, 0, 0, 1, &ip_addr);
+    format_name(name, sizeof (name), 25 + wide, 0, 0, 1, &addr);
 
     switch (ntohs(reply.data.auth_data.mode)) {
       case RPY_AD_MD_NONE:
@@ -2490,6 +2530,7 @@ process_cmd_ntpdata(char *line)
   CMD_Request request;
   CMD_Reply reply;
   IPAddr remote_addr, local_addr;
+  IPSockAddr source_addr;
   struct timespec ref_time;
   uint32_t i, n_sources;
   uint16_t mode;
@@ -2508,7 +2549,7 @@ process_cmd_ntpdata(char *line)
 
   for (i = 0; i < n_sources; i++) {
     if (specified_addr) {
-      if (!parse_source_address(line, &remote_addr)) {
+      if (!parse_source_sock_address(line, &source_addr)) {
         LOG(LOGS_ERR, "Could not get address for hostname");
         return 0;
       }
@@ -2522,13 +2563,13 @@ process_cmd_ntpdata(char *line)
       if (mode != RPY_SD_MD_CLIENT && mode != RPY_SD_MD_PEER)
         continue;
 
-      UTI_IPNetworkToHost(&reply.data.source_data.ip_addr, &remote_addr);
-      if (!UTI_IsIPReal(&remote_addr))
+      UTI_IPSockAddrNetworkToHost(&reply.data.source_data.ip_addr, 
&source_addr);
+      if (!UTI_IsIPReal(&source_addr.ip_addr))
         continue;
     }
 
     request.command = htons(REQ_NTP_DATA);
-    UTI_IPHostToNetwork(&remote_addr, &request.data.ntp_data.ip_addr);
+    UTI_IPSockAddrHostToNetwork(&source_addr, &request.data.ntp_data.ip_addr);
     if (!request_reply(&request, &reply, RPY_NTP_DATA2, 0))
       return 0;
 
@@ -2617,7 +2658,7 @@ process_cmd_selectdata(char *line)
   uint32_t i, n_sources;
   int all, verbose, conf_options, eff_options;
   char name[256];
-  IPAddr ip_addr;
+  IPSockAddr addr;
 
   parse_sources_options(line, &all, &verbose);
 
@@ -2651,12 +2692,12 @@ process_cmd_selectdata(char *line)
     if (!request_reply(&request, &reply, RPY_SELECT_DATA, 0))
       return 0;
 
-    UTI_IPNetworkToHost(&reply.data.select_data.ip_addr, &ip_addr);
-    if (!all && ip_addr.family == IPADDR_ID)
+    UTI_IPSockAddrNetworkToHost(&reply.data.select_data.ip_addr, &addr);
+    if (!all && addr.ip_addr.family == IPADDR_ID)
       continue;
 
-    format_name(name, sizeof (name), 25 + wide, ip_addr.family == 
IPADDR_UNSPEC,
-                ntohl(reply.data.select_data.ref_id), 1, &ip_addr);
+    format_name(name, sizeof (name), 25 + wide, addr.ip_addr.family == 
IPADDR_UNSPEC,
+                ntohl(reply.data.select_data.ref_id), 1, &addr);
 
     conf_options = ntohs(reply.data.select_data.conf_options);
     eff_options = ntohs(reply.data.select_data.eff_options);
@@ -2828,7 +2869,7 @@ process_cmd_clients(char *line)
 {
   CMD_Request request;
   CMD_Reply reply;
-  IPAddr ip;
+  IPSockAddr addr;
   uint32_t i, n_clients, next_index, n_indices, min_hits, reset;
   RPY_ClientAccesses_Client *client;
   char header[94], name[50], *opt, *arg;
@@ -2877,14 +2918,15 @@ process_cmd_clients(char *line)
     for (i = 0; i < n_clients && i < MAX_CLIENT_ACCESSES; i++) {
       client = &reply.data.client_accesses_by_index.clients[i];
 
-      UTI_IPNetworkToHost(&client->ip, &ip);
+      UTI_IPNetworkToHost(&client->ip, &addr.ip_addr);
+      addr.port = 0;
 
       /* UNSPEC means the record could not be found in the daemon's tables.
          We shouldn't ever generate this case, but ignore it if we do. */
-      if (ip.family == IPADDR_UNSPEC)
+      if (addr.ip_addr.family == IPADDR_UNSPEC)
         continue;
 
-      format_name(name, sizeof (name), 25 + wide, 0, 0, 0, &ip);
+      format_name(name, sizeof (name), 25 + wide, 0, 0, 0, &addr);
 
       print_report("%*s  %6U  %5U  %C  %C  %I  %6U  %5U  %C  %I\n",
                    -25 - wide, name,
@@ -3063,20 +3105,20 @@ static int
 process_cmd_offset(CMD_Request *msg, char *line)
 {
   uint32_t ref_id;
-  IPAddr ip_addr;
+  IPSockAddr addr;
   double offset;
   char *src;
 
   src = line;
   line = CPS_SplitWord(line);
 
-  if (!parse_source_address_or_refid(src, &ip_addr, &ref_id) ||
+  if (!parse_source_address_or_refid(src, &addr, &ref_id) ||
       sscanf(line, "%lf", &offset) != 1) {
     LOG(LOGS_ERR, "Invalid syntax for offset command");
     return 0;
   }
 
-  UTI_IPHostToNetwork(&ip_addr, &msg->data.modify_offset.address);
+  UTI_IPSockAddrHostToNetwork(&addr, &msg->data.modify_offset.address);
   msg->data.modify_offset.ref_id = htonl(ref_id);
   msg->data.modify_offset.new_offset = UTI_FloatHostToNetwork(offset);
 
@@ -3163,13 +3205,13 @@ process_cmd_selectopts(CMD_Request *msg, char *line)
 {
   int mask, options, option;
   uint32_t ref_id;
-  IPAddr ip_addr;
+  IPSockAddr addr;
   char *src, *opt;
 
   src = line;
   line = CPS_SplitWord(line);
 
-  if (!parse_source_address_or_refid(src, &ip_addr, &ref_id)) {
+  if (!parse_source_address_or_refid(src, &addr, &ref_id)) {
     LOG(LOGS_ERR, "Invalid syntax for selectopts command");
     return 0;
   }
@@ -3190,7 +3232,7 @@ process_cmd_selectopts(CMD_Request *msg, char *line)
       options |= option;
   }
 
-  UTI_IPHostToNetwork(&ip_addr, &msg->data.modify_select_opts.address);
+  UTI_IPSockAddrHostToNetwork(&addr, &msg->data.modify_select_opts.address);
   msg->data.modify_select_opts.ref_id = htonl(ref_id);
   msg->data.modify_select_opts.mask = htonl(mask);
   msg->data.modify_select_opts.options = 
htonl(convert_addsrc_sel_options(options));
-- 
2.43.0


-- 
To unsubscribe email [email protected] with "unsubscribe" 
in the subject.
For help email [email protected] with "help" in the 
subject.
Trouble?  Email [email protected].

Reply via email to