+static uint32_t ntpd_get_refclock_id (struct info_peer_summary const *peer_info)
+{
+ uint32_t addr = ntohl (peer_info->srcadr);
+ uint32_t refclock_id = (addr >> 8) & 0x00FF;
+
+ return (refclock_id);
+}
+
+static int ntpd_get_name_from_address (char *buffer, size_t buffer_size,
+ struct info_peer_summary const *peer_info, _Bool do_reverse_lookup)
+{
+ struct sockaddr_storage sa;
+ socklen_t sa_len;
+ int flags = 0;
+ int status;
+
+ memset (&sa, 0, sizeof (sa));
+
+ if (peer_info->v6_flag)
+ {
+ struct sockaddr_in6 sa6;
+
+ assert (sizeof (sa) >= sizeof (sa6));
+
+ memset (&sa6, 0, sizeof (sa6));
+ sa6.sin6_family = AF_INET6;
+ sa6.sin6_port = htons (123);
+ memcpy (&sa6.sin6_addr, &peer_info->srcadr6,
+ sizeof (struct in6_addr));
+ sa_len = sizeof (sa6);
+
+ memcpy (&sa, &sa6, sizeof (sa6));
+ }
+ else
+ {
+ struct sockaddr_in sa4;
+
+ assert (sizeof (sa) >= sizeof (sa4));
+
+ memset (&sa4, 0, sizeof (sa4));
+ sa4.sin_family = AF_INET;
+ sa4.sin_port = htons (123);
+ memcpy (&sa4.sin_addr, &peer_info->srcadr,
+ sizeof (struct in_addr));
+ sa_len = sizeof (sa4);
+
+ memcpy (&sa, &sa4, sizeof (sa4));
+ }
+
+ if (!do_reverse_lookup)
+ flags |= NI_NUMERICHOST;
+
+ status = getnameinfo ((struct sockaddr const *) &sa, sa_len,
+ buffer, buffer_size,
+ NULL, 0, /* No port name */
+ flags);
+ if (status != 0)
+ {
+ char errbuf[1024];
+ ERROR ("ntpd plugin: getnameinfo failed: %s",
+ (status == EAI_SYSTEM)
+ ? sstrerror (errno, errbuf, sizeof (errbuf))
+ : gai_strerror (status));
+ return (-1);
+ }
+
+ return (0);
+} /* ntpd_get_name_from_address */
+
+static int ntpd_get_name_refclock (char *buffer, size_t buffer_size,
+ struct info_peer_summary const *peer_info)
+{
+ uint32_t refclock_id = ntpd_get_refclock_id (peer_info);
+ uint32_t unit_id = ntohl (peer_info->srcadr) & 0x00FF;
+
+ if (refclock_id >= refclock_names_num)
+ return (ntpd_get_name_from_address (buffer, buffer_size,
+ peer_info,
+ /* do_reverse_lookup = */ 0));
+
+ if (include_unit_id)
+ ssnprintf (buffer, buffer_size, "%s-%"PRIu32,
+ refclock_names[refclock_id], unit_id);
+ else
+ sstrncpy (buffer, refclock_names[refclock_id], buffer_size);
+
+ return (0);
+} /* int ntpd_get_name_refclock */
+
+static int ntpd_get_name (char *buffer, size_t buffer_size,
+ struct info_peer_summary const *peer_info)
+{
+ uint32_t addr = ntohl (peer_info->srcadr);
+
+ if (!peer_info->v6_flag && ((addr & REFCLOCK_MASK) == REFCLOCK_ADDR))
+ return (ntpd_get_name_refclock (buffer, buffer_size,
+ peer_info));
+ else
+ return (ntpd_get_name_from_address (buffer, buffer_size,
+ peer_info, do_reverse_lookups));
+} /* int ntpd_addr_to_name */
+