[ntpsec commit] Forward-port [Bug 2778] Implement "apeers" ntpq command to include associd.

Eric S. Raymond esr at ntpsec.org
Thu Oct 22 21:45:15 UTC 2015


Module:    ntpsec
Branch:    master
Commit:    64667fba7f62463da2c7bc23ba3b3537893d6371
Changeset: http://git.ntpsec.org/ntpsec/commit/?id=64667fba7f62463da2c7bc23ba3b3537893d6371

Author:    Eric S. Raymond <esr at thyrsus.com>
Date:      Thu Oct 22 17:43:48 2015 -0400

Forward-port [Bug 2778] Implement "apeers"  ntpq command to include associd.

This is a feature, but should be safe as it only touches ntpq.

---

 NEWS                        |   1 +
 docs/includes/ntpq-body.txt |  11 ++++
 ntpq/ntpq-subs.c            | 151 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 157 insertions(+), 6 deletions(-)

diff --git a/NEWS b/NEWS
index 05daa2a..b7f00aa 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,7 @@ on user-visible changes.
 
 == Forward-ported bugfixes from NTP Classic ==
 
+* [Bug 2778] Implement "apeers"  ntpq command to include associd.
 * [Bug 2836] DFC77 patches from Frank Kardel to make decoding more
   robust, and require 2 consecutive timestamps to be consistent.
 * [Bug 2845] Harden memory allocation in ntpd; implement and
diff --git a/docs/includes/ntpq-body.txt b/docs/includes/ntpq-body.txt
index 97ef577..8671148 100644
--- a/docs/includes/ntpq-body.txt
+++ b/docs/includes/ntpq-body.txt
@@ -342,6 +342,17 @@ server, +B+: broadcast server, +M+: multicast server
 |+jitter+        |jitter
 |=======================================================================
 
++apeers+::
+  Display a list of peers in the form:
++
+-------------------------------------------------------------------
+[tally]remote refid assid st t when pool reach delay offset jitter
+-------------------------------------------------------------------
++
+where the output is just like the +peers+ command except that the
++refid+ is displayed in hex format and the association number is also
+displayed.
+
 +pstats+ _assocID_::
   Show the statistics for the peer with the given _assocID_.
 
diff --git a/ntpq/ntpq-subs.c b/ntpq/ntpq-subs.c
index b83757a..313974f 100644
--- a/ntpq/ntpq-subs.c
+++ b/ntpq/ntpq-subs.c
@@ -59,6 +59,8 @@ static	bool	doprintpeers	(struct varlist *, int, int, int, const char *, FILE *,
 static	bool	dogetpeers	(struct varlist *, associd_t, FILE *, int);
 static	void	dopeers 	(int, FILE *, int);
 static	void	peers		(struct parse *, FILE *);
+static	void	doapeers 	(int, FILE *, int);
+static	void	apeers		(struct parse *, FILE *);
 static	void	lpeers		(struct parse *, FILE *);
 static	void	doopeers	(int, FILE *, int);
 static	void	opeers		(struct parse *, FILE *);
@@ -155,6 +157,9 @@ struct xcmd opcmds[] = {
 	{ "peers",  peers,      { OPT|IP_VERSION, NO, NO, NO },
 	  { "-4|-6", "", "", "" },
 	  "obtain and print a list of the server's peers [IP version]" },
+	{ "apeers",  apeers,      { OPT|IP_VERSION, NO, NO, NO },
+	  { "-4|-6", "", "", "" },
+	  "obtain and print a list of the server's peers and their assocIDs [IP version]" },
 	{ "lpeers", lpeers,     { OPT|IP_VERSION, NO, NO, NO },
 	  { "-4|-6", "", "", "" },
 	  "obtain and print a list of all peers and clients [IP version]" },
@@ -1553,6 +1558,26 @@ struct varlist peervarlist[] = {
 	{ 0,		0 }
 };
 
+struct varlist apeervarlist[] = {
+	{ "srcadr",	0 },	/* 0 */
+	{ "refid",	0 },	/* 1 */
+	{ "assid",	0 },	/* 2 */
+	{ "stratum",	0 },	/* 3 */
+	{ "hpoll",	0 },	/* 4 */
+	{ "ppoll",	0 },	/* 5 */
+	{ "reach",	0 },	/* 6 */
+	{ "delay",	0 },	/* 7 */
+	{ "offset",	0 },	/* 8 */
+	{ "jitter",	0 },	/* 9 */
+	{ "dispersion", 0 },	/* 10 */
+	{ "rec",	0 },	/* 11 */
+	{ "reftime",	0 },	/* 12 */
+	{ "srcport",	0 },	/* 13 */
+	{ "hmode",	0 },	/* 14 */
+	{ "srchost",	0 },	/* 15 */
+	{ 0,		0 }
+};
+
 
 /*
  * Decode an incoming data buffer and print a line in the peer list
@@ -1623,7 +1648,7 @@ doprintpeers(
 				fprintf(stderr, "malformed %s=%s\n",
 					name, value);
 		} else if (!strcmp("srchost", name)) {
-			if (pvl == peervarlist) {
+			if (pvl == peervarlist || pvl == apeervarlist) {
 				len = strlen(value);
 				if (2 < len &&
 				    (size_t)len < sizeof(clock_name)) {
@@ -1669,6 +1694,35 @@ doprintpeers(
 				} else {
 					have_da_rid = false;
 				}
+			} else if (pvl == apeervarlist) {
+				have_da_rid = true;
+				drlen = strlen(value);
+				if (0 == drlen) {
+					dstadr_refid = "";
+				} else if (drlen <= 4) {
+					ZERO(u32);
+					memcpy(&u32, value, drlen);
+					dstadr_refid = refid_str(u32, 1);
+					//fprintf(stderr, "apeervarlist S1 refid: value=<%s>\n", value);
+				} else if (decodenetnum(value, &refidadr)) {
+					if (SOCK_UNSPEC(&refidadr))
+						dstadr_refid = "0.0.0.0";
+					else if (ISREFCLOCKADR(&refidadr))
+						dstadr_refid =
+						    refnumtoa(&refidadr);
+					else {
+						char *buf = emalloc(10);
+						int i = ntohl(refidadr.sa4.sin_addr.s_addr);
+
+						snprintf(buf, 10,
+							"%0x", i);
+						dstadr_refid = buf;
+					//fprintf(stderr, "apeervarlist refid: value=<%x>\n", i);
+					}
+					//fprintf(stderr, "apeervarlist refid: value=<%s>\n", value);
+				} else {
+					have_da_rid = false;
+				}
 			}
 		} else if (!strcmp("stratum", name)) {
 			decodeuint(value, &stratum);
@@ -1685,8 +1739,8 @@ doprintpeers(
 		} else if (!strcmp("offset", name)) {
 			decodetime(value, &estoffset);
 		} else if (!strcmp("jitter", name)) {
-			if (pvl == peervarlist &&
-			    decodetime(value, &estjitter))
+		    if ((pvl == peervarlist || pvl == apeervarlist)
+			    && decodetime(value, &estjitter))
 				have_jitter = true;
 		} else if (!strcmp("rootdisp", name) ||
 			   !strcmp("dispersion", name)) {
@@ -1699,6 +1753,8 @@ doprintpeers(
 		} else if (!strcmp("reftime", name)) {
 			if (!decodets(value, &reftime))
 				L_CLR(&reftime);
+		} else {
+			// fprintf(stderr, "UNRECOGNIZED name=%s ", name);
 		}
 	}
 
@@ -1750,7 +1806,8 @@ doprintpeers(
 	else
 		c = flash2[CTL_PEER_STATVAL(rstatus) & 0x3];
 	if (numhosts > 1) {
-		if (peervarlist == pvl && have_dstadr) {
+		if ((pvl == peervarlist || pvl == apeervarlist)
+		    && have_dstadr) {
 			serverlocal = nntohost_col(&dstadr,
 			    (size_t)min(LIB_BUFLENGTH - 1, maxhostlen),
 			    true);
@@ -1777,8 +1834,14 @@ doprintpeers(
 			drlen = strlen(dstadr_refid);
 			makeascii(drlen, dstadr_refid, fp);
 		}
-		while (drlen++ < 15)
-			fputc(' ', fp);
+		if (pvl == apeervarlist) {
+			while (drlen++ < 9)
+				fputc(' ', fp);
+			fprintf(fp, "%-6d", associd);
+		} else {
+			while (drlen++ < 15)
+				fputc(' ', fp);
+		}
 		fprintf(fp,
 			" %2ld %c %4.4s %4.4s  %3lo  %7.7s %8.7s %7.7s\n",
 			stratum, type,
@@ -1898,6 +1961,60 @@ dopeers(
 
 
 /*
+ * doapeers - print a peer spreadsheet with assocIDs
+ */
+static void
+doapeers(
+	int showall,
+	FILE *fp,
+	int af
+	)
+{
+	u_int		u;
+	char		fullname[HOST_NAME_MAX];
+	sockaddr_u	netnum;
+	const char *	name_or_num;
+	size_t		sl;
+
+	if (!dogetassoc(fp))
+		return;
+
+	for (u = 0; u < numhosts; u++) {
+		if (getnetnum(chosts[u].name, &netnum, fullname, af)) {
+			name_or_num = nntohost(&netnum);
+			sl = strlen(name_or_num);
+			maxhostlen = max(maxhostlen, sl);
+		}
+	}
+	if (numhosts > 1)
+		fprintf(fp, "%-*.*s ", (int)maxhostlen, (int)maxhostlen,
+			"server (local)");
+	fprintf(fp,
+		"     remote       refid   assid  st t when poll reach   delay   offset  jitter\n");
+	if (numhosts > 1)
+		for (u = 0; u <= maxhostlen; u++)
+			fprintf(fp, "=");
+	fprintf(fp,
+		"==============================================================================\n");
+
+	for (u = 0; u < numassoc; u++) {
+		if (!showall &&
+		    !(CTL_PEER_STATVAL(assoc_cache[u].status)
+		      & (CTL_PST_CONFIG|CTL_PST_REACH))) {
+			if (debug)
+				fprintf(stderr, "eliding [%d]\n",
+					(int)assoc_cache[u].assid);
+			continue;
+		}
+		if (!dogetpeers(apeervarlist, (int)assoc_cache[u].assid,
+				fp, af))
+			return;
+	}
+	return;
+}
+
+
+/*
  * peers - print a peer spreadsheet
  */
 /*ARGSUSED*/
@@ -1920,6 +2037,28 @@ peers(
 
 
 /*
+ * apeers - print a peer spreadsheet, with assocIDs
+ */
+/*ARGSUSED*/
+static void
+apeers(
+	struct parse *pcmd,
+	FILE *fp
+	)
+{
+	int af = 0;
+
+	if (pcmd->nargs == 1) {
+		if (pcmd->argval->ival == 6)
+			af = AF_INET6;
+		else
+			af = AF_INET;
+	}
+	doapeers(0, fp, af);
+}
+
+
+/*
  * lpeers - print a peer spreadsheet including all fuzzball peers
  */
 /*ARGSUSED*/



More information about the vc mailing list