[ntp:bk-ntp-dev-send] BitKeeper diffs

Harlan Stenn stenn at deacon.udel.edu
Thu Dec 1 04:58:38 UTC 2011


#### ChangeSet ####
2011-11-30 04:00:32+00:00, davehart at shiny.ad.hartbrothers.com
  Floor calculation of sys_rootdisp at sys_mindisp in clock_update (from
    Dave Mills).

==== ChangeLog ====
2011-11-30 04:00:30+00:00, davehart at shiny.ad.hartbrothers.com +2 -0
  Floor calculation of sys_rootdisp at sys_mindisp in clock_update (from
    Dave Mills).

--- 1.1062/ChangeLog	2011-11-29 17:08:29 -05:00
+++ 1.1063/ChangeLog	2011-11-29 23:00:30 -05:00
@@ -12,6 +12,8 @@
 * from 4.2.6p5-RC2: Exclude not-yet-determined sys_refid from use in
   loopback TEST12 (from Dave Mills).
 * from 4.2.6p5-RC2: Never send KoD rate limiting response to MODE_SERVER.
+* Floor calculation of sys_rootdisp at sys_mindisp in clock_update (from
+  Dave Mills).
 * Restore 4.2.6 clock_combine() weighting to ntp-dev, reverting to pre-
   4.2.7p70 method while also avoiding divide-by-zero (from Dave Mills).
 * Round l_fp traffic interval when converting to integer in rate limit

==== ntpd/ntp_proto.c ====
2011-11-30 04:00:30+00:00, davehart at shiny.ad.hartbrothers.com +6 -3
  Floor calculation of sys_rootdisp at sys_mindisp in clock_update (from
    Dave Mills).

--- 1.331/ntpd/ntp_proto.c	2011-11-29 17:08:30 -05:00
+++ 1.332/ntpd/ntp_proto.c	2011-11-29 23:00:30 -05:00
@@ -1811,9 +1811,12 @@ clock_update(
 		sys_refid = peer->refid;
 	else
 		sys_refid = addr2refid(&peer->srcadr);
-	dtemp = sys_jitter + fabs(sys_offset) + peer->disp + clock_phi *
-	    (current_time - peer->update);
-	sys_rootdisp = dtemp + peer->rootdisp;
+	dtemp = fabs(sys_offset) + peer->disp + peer->rootdisp +
+	    clock_phi * (current_time - peer->update) + sys_jitter;
+	if (dtemp > sys_mindisp)
+		sys_rootdisp = dtemp;
+	else
+		sys_rootdisp = sys_mindisp;
 	sys_rootdelay = peer->delay + peer->rootdelay;
 	sys_reftime = peer->dst;
 

#### ChangeSet ####
2011-11-29 21:58:49+00:00, davehart at shiny.ad.hartbrothers.com
  [Bug 2073] Correct ntpq billboard's MODE_PASSIVE t from 'u' to 'S'.
  Round l_fp traffic interval when converting to integer in rate limit
    and KoD calculation.

==== ChangeLog ====
2011-11-29 21:58:47+00:00, davehart at shiny.ad.hartbrothers.com +4 -1
  [Bug 2073] Correct ntpq billboard's MODE_PASSIVE t from 'u' to 'S'.
  Round l_fp traffic interval when converting to integer in rate limit
    and KoD calculation.

--- 1.1060/ChangeLog	2011-11-26 19:52:00 -05:00
+++ 1.1061/ChangeLog	2011-11-29 16:58:47 -05:00
@@ -8,10 +8,13 @@
   duplicate ephemeral associations without broadcastdelay.
 * [Bug 2072] from 4.2.6p5-RC2: Orphan parent selection metric needs
   ntohl().
+* [Bug 2073] Correct ntpq billboard's MODE_PASSIVE t from 'u' to 'S'.
 * from 4.2.6p5-RC2: Exclude not-yet-determined sys_refid from use in
   loopback TEST12 (from Dave Mills).
 * Restore 4.2.6 clock_combine() weighting to ntp-dev, reverting to pre-
-  4.2.7p70 logic and avoiding divide-by-zero (from Dave Mills).
+  4.2.7p70 method while also avoiding divide-by-zero (from Dave Mills).
+* Round l_fp traffic interval when converting to integer in rate limit
+  and KoD calculation.
 (4.2.7p236) 2011/11/16 Released by Harlan Stenn <stenn at ntp.org>
 * Documentation updates from Dave Mills.
 (4.2.7p235) 2011/11/16 Released by Harlan Stenn <stenn at ntp.org>

==== include/ntp.h ====
2011-11-29 21:58:47+00:00, davehart at shiny.ad.hartbrothers.com +1 -1
  MDF_UCLNT indicates preemptible, not ephemeral (comment fix).

--- 1.200/include/ntp.h	2011-11-25 15:41:46 -05:00
+++ 1.201/include/ntp.h	2011-11-29 16:58:47 -05:00
@@ -762,7 +762,7 @@ struct mon_data {
 #define	MDF_POOL	0x08	/* pool client solicitor */
 #define MDF_ACAST	0x10	/* manycast client solicitor */
 #define	MDF_BCLNT	0x20	/* eph. broadcast/multicast client */
-#define MDF_UCLNT	0x40	/* eph. manycast or pool client */
+#define MDF_UCLNT	0x40	/* preemptible manycast or pool client */
 /*
  * In the context of struct peer in ntpd, three of the cast_flags bits
  * represent configured associations which never receive packets, and

==== ntpd/ntp_monitor.c ====
2011-11-29 21:58:47+00:00, davehart at shiny.ad.hartbrothers.com +5 -1
  Round l_fp traffic interval when converting to integer in rate limit
    and KoD calculation.

--- 1.39/ntpd/ntp_monitor.c	2011-05-23 00:26:29 -04:00
+++ 1.40/ntpd/ntp_monitor.c	2011-11-29 16:58:47 -05:00
@@ -310,6 +310,7 @@ ntp_monitor(
 	u_short	flags
 	)
 {
+	l_fp		interval_fp;
 	struct pkt *	pkt;
 	mon_entry *	mon;
 	mon_entry *	oldest;
@@ -318,7 +319,6 @@ ntp_monitor(
 	u_short		restrict_mask;
 	u_char		mode;
 	u_char		version;
-	l_fp		interval_fp;
 	int		interval;
 	int		head;		/* headway increment */
 	int		leak;		/* new headway */
@@ -345,6 +345,8 @@ ntp_monitor(
 	if (mon != NULL) {
 		interval_fp = rbufp->recv_time;
 		L_SUB(&interval_fp, &mon->last);
+		/* add one-half second to round up */
+		L_ADDUF(&interval_fp, 0x80000000);
 		interval = interval_fp.l_i;
 		mon->last = rbufp->recv_time;
 		NSRCPORT(&mon->rmtadr) = NSRCPORT(&rbufp->recv_srcadr);
@@ -440,6 +442,8 @@ ntp_monitor(
 		if (oldest != NULL) {
 			interval_fp = rbufp->recv_time;
 			L_SUB(&interval_fp, &oldest->last);
+			/* add one-half second to round up */
+			L_ADDUF(&interval_fp, 0x80000000);
 			oldest_age = interval_fp.l_i;
 		}
 		/* note -1 is legal for mru_maxage (disables) */

==== ntpd/ntp_peer.c ====
2011-11-29 21:58:47+00:00, davehart at shiny.ad.hartbrothers.com +20 -2
  provide debug trace output of newpeer()/findexistingpeer() search
    for duplicate associations.

--- 1.150/ntpd/ntp_peer.c	2011-11-26 07:11:54 -05:00
+++ 1.151/ntpd/ntp_peer.c	2011-11-29 16:58:47 -05:00
@@ -206,6 +206,13 @@ findexistingpeer_addr(
 {
 	struct peer *peer;
 
+	DPRINTF(2, ("findexistingpeer_addr(%s, %s, %d, 0x%x)\n",
+		sptoa(addr),
+		(start_peer)
+		    ? sptoa(&start_peer->srcadr)
+		    : "NULL",
+		mode, (u_int)cast_flags));
+
 	/*
 	 * start_peer is included so we can locate instances of the
 	 * same peer through different interfaces in the hash table.
@@ -222,11 +229,17 @@ findexistingpeer_addr(
 		peer = start_peer->adr_link;
 	
 	while (peer != NULL) {
+		DPRINTF(3, ("%s %s %d %d 0x%x 0x%x ", sptoa(addr),
+			sptoa(&peer->srcadr), mode, peer->hmode,
+			(u_int)cast_flags, (u_int)peer->cast_flags));
  		if ((-1 == mode || peer->hmode == mode ||
 		     ((MDF_BCLNT & peer->cast_flags) &&
 		      (MDF_BCLNT & cast_flags))) &&
-		    ADDR_PORT_EQ(addr, &peer->srcadr))
+		    ADDR_PORT_EQ(addr, &peer->srcadr)) {
+			DPRINTF(3, ("found.\n"));
 			break;
+		}
+		DPRINTF(3, ("\n"));
 		peer = peer->adr_link;
 	}
 
@@ -788,8 +801,13 @@ newpeer(
 	 * multicast) and preemptible (manycast and pool) client
 	 * associations.
 	 */
-	if (peer != NULL)
+	if (peer != NULL) {
+		DPRINTF(2, ("newpeer(%s) found existing association\n",
+			(hostname)
+			    ? hostname
+			    : stoa(srcadr)));
 		return NULL;
+	}
 
 	/*
 	 * Allocate a new peer structure. Some dirt here, since some of

==== ntpd/ntp_proto.c ====
2011-11-29 21:58:47+00:00, davehart at shiny.ad.hartbrothers.com +12 -10
  Avoid duplicate "no servers found".

--- 1.329/ntpd/ntp_proto.c	2011-11-26 19:52:00 -05:00
+++ 1.330/ntpd/ntp_proto.c	2011-11-29 16:58:47 -05:00
@@ -312,9 +312,10 @@ transmit(
 				peer_ntpdate--;
 				if (peer_ntpdate == 0) {
 					msyslog(LOG_NOTICE,
-					    "ntpd: no servers found");
-					printf(
 					    "ntpd: no servers found\n");
+					if (!msyslog_term)
+						printf(
+						    "ntpd: no servers found\n");
 					exit (0);
 				}
 			}
@@ -551,8 +552,10 @@ receive(
 		sys_limitrejected++;
 		if (!(restrict_mask & RES_KOD) || hismode ==
 		    MODE_BROADCAST)
+			//if (MODE_SERVER == hismode)
+			//	DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
+			//		stoa(&rbufp->recv_srcadr)));
 			return;			/* rate exceeded */
-
 		if (hismode == MODE_CLIENT)
 			fast_xmit(rbufp, MODE_SERVER, skeyid,
 			    restrict_mask);
@@ -1079,12 +1082,14 @@ receive(
 		}
 
 		/*
-		 * Do not respond if synchronized and stratum is either
+		 * Do not respond if synchronized and if stratum is
 		 * below the floor or at or above the ceiling. Note,
 		 * this allows an unsynchronized peer to synchronize to
 		 * us. It would be very strange if he did and then was
 		 * nipped, but that could only happen if we were
-		 * operating at the top end of the range.
+		 * operating at the top end of the range.  It also means
+		 * we will spin an ephemeral association in response to
+		 * MODE_ACTIVE KoDs, which will time out eventually.
 		 */
 		if (hisleap != LEAP_NOTINSYNC && (hisstratum <
 		    sys_floor || hisstratum >= sys_ceiling)) {
@@ -1993,7 +1998,7 @@ poll_update(
 	 * sending in a burst, use the earliest time. When not in a
 	 * burst but with a reply pending, send at the earliest time
 	 * unless the next scheduled time has not advanced. This can
-	 * only happen if multiple replies are peinding in the same
+	 * only happen if multiple replies are pending in the same
 	 * response interval. Otherwise, send at the later of the next
 	 * scheduled time and the earliest time.
 	 *
@@ -2466,8 +2471,7 @@ clock_select(void)
 
 			localmet = ntohl(peer->dstadr->addr_refid);
 			peermet = ntohl(addr2refid(&peer->srcadr));
-			if (peermet < orphmet &&
-			    (current_time <= orphwait || peermet < localmet)) {
+			if (peermet < localmet && peermet < orphmet) {
 				typeorphan = peer;
 				orphmet = peermet;
 			}
@@ -2915,8 +2919,6 @@ clock_combine(
 	y = z = w = 0;
 	for (i = 0; i < npeers; i++) {
 		d = root_distance(peers[i]);
-		if (0. == d)
-			d = 1e-6;	/* hart avoid div by 0 */
 		x = 1. / d;
 		y += x;
 		z += peers[i]->offset * x;

==== ntpq/ntpq-subs.c ====
2011-11-29 21:58:47+00:00, davehart at shiny.ad.hartbrothers.com +6 -2
  [Bug 2073] Correct ntpq billboard's MODE_PASSIVE t from 'u' to 'S'.

--- 1.94/ntpq/ntpq-subs.c	2011-11-26 07:11:54 -05:00
+++ 1.95/ntpq/ntpq-subs.c	2011-11-29 16:58:47 -05:00
@@ -1730,8 +1730,12 @@ doprintpeers(
 		break;
 
 	case MODE_ACTIVE:
-		type = 's';		/* symmetric */
-		break;
+		type = 's';		/* symmetric active */
+		break;			/* configured */
+
+	case MODE_PASSIVE:
+		type = 'S';		/* symmetric passive */
+		break;			/* ephemeral */
 	}
 
 	/*

#### ChangeSet ####
2011-11-29 13:35:16+00:00, davehart at shiny.ad.hartbrothers.com
  merge cleanup

==== ChangeLog ====
2011-11-29 13:35:16+00:00, davehart at shiny.ad.hartbrothers.com +0 -1
  merge cleanup

--- 1.496.26.177/ChangeLog	2011-11-29 08:33:59 -05:00
+++ 1.496.26.178/ChangeLog	2011-11-29 08:35:16 -05:00
@@ -11,7 +11,6 @@
   (from David Mills).
 * Never send KoD rate limiting response to MODE_SERVER response.
 
-
 ---
 (4.2.6p5-RC1) 2011/10/18 Released by Harlan Stenn <stenn at ntp.org>
 

#### ChangeSet ####
2011-11-29 13:33:26+00:00, davehart at shiny.ad.hartbrothers.com
  Never send KoD rate limiting response to MODE_SERVER response.

==== ChangeLog ====
2011-11-29 13:33:25+00:00, davehart at shiny.ad.hartbrothers.com +3 -1
  Never send KoD rate limiting response to MODE_SERVER response.

--- 1.496.61.1/ChangeLog	2011-11-25 05:23:55 -05:00
+++ 1.496.61.2/ChangeLog	2011-11-29 08:33:25 -05:00
@@ -6,7 +6,9 @@
 * [Bug 2069] broadcastclient, multicastclient spin up duplicate
   ephemeral associations without broadcastdelay.
 * Exclude not-yet-determined sys_refid from use in loopback TEST12
-  (from David Mills).  
+  (from David Mills).
+* Never send KoD rate limiting response to MODE_SERVER response.
+
 ---
 (4.2.6p5-RC1) 2011/10/18 Released by Harlan Stenn <stenn at ntp.org>
 

==== ntpd/ntp_crypto.c ====
2011-11-29 13:33:25+00:00, davehart at shiny.ad.hartbrothers.com +1 -1
  Use () to enforce intended precendence of & and && operators

--- 1.146/ntpd/ntp_crypto.c	2009-11-07 17:46:40 -05:00
+++ 1.146.1.1/ntpd/ntp_crypto.c	2011-11-29 08:33:25 -05:00
@@ -855,7 +855,7 @@ crypto_recv(
 			 * protocol. A protocol restart will occur at
 			 * the next ASSOC message.
 			 */
-			if (peer->cast_flags & MDF_BCLNT &&
+			if ((peer->cast_flags & MDF_BCLNT) &&
 			    peer->assoc != associd)
 				break;
 

==== ntpd/ntp_proto.c ====
2011-11-29 13:33:25+00:00, davehart at shiny.ad.hartbrothers.com +3 -3
  Never send KoD rate limiting response to MODE_SERVER response.

--- 1.297.3.1/ntpd/ntp_proto.c	2011-11-25 05:23:56 -05:00
+++ 1.297.3.2/ntpd/ntp_proto.c	2011-11-29 08:33:25 -05:00
@@ -470,8 +470,8 @@ receive(
 	restrict_mask = ntp_monitor(rbufp, restrict_mask);
 	if (restrict_mask & RES_LIMITED) {
 		sys_limitrejected++;
-		if (!(restrict_mask & RES_KOD) || hismode ==
-		    MODE_BROADCAST)
+		if (!(restrict_mask & RES_KOD) || MODE_BROADCAST ==
+		    hismode || MODE_SERVER == hismode)
 			return;			/* rate exceeded */
 
 		if (hismode == MODE_CLIENT)
@@ -3418,7 +3418,7 @@ local_refid(
 {
 	endpt *	unicast_ep;
 
-	if (!(INT_MCASTIF & p->dstadr->flags))
+	if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
 		unicast_ep = p->dstadr;
 	else
 		unicast_ep = findinterface(&p->srcadr);

#### ChangeSet ####
2011-11-27 00:39:11+00:00, davehart at shiny.ad.hartbrothers.com
  [Bug 2072] Orphan parent selection metric needs ntohl().

==== ChangeLog ====
2011-11-27 00:39:10+00:00, davehart at shiny.ad.hartbrothers.com +1 -0
  [Bug 2072] Orphan parent selection metric needs ntohl().

--- 1.496.26.174/ChangeLog	2011-11-21 04:35:27 -05:00
+++ 1.496.62.1/ChangeLog	2011-11-26 19:39:10 -05:00
@@ -1,6 +1,7 @@
 ---
 
 * [Bug 2050] Orphan mode stratum counting to infinity.
+* [Bug 2072] Orphan parent selection metric needs ntohl().
 
 ---
 (4.2.6p5-RC1) 2011/10/18 Released by Harlan Stenn <stenn at ntp.org>

==== libntp/a_md5encrypt.c ====
2011-11-27 00:39:10+00:00, davehart at shiny.ad.hartbrothers.com +1 -0
  [Bug 2072] Orphan parent selection metric needs ntohl().

--- 1.26.1.1/libntp/a_md5encrypt.c	2010-05-10 09:16:16 -04:00
+++ 1.26.1.2/libntp/a_md5encrypt.c	2011-11-26 19:39:10 -05:00
@@ -87,6 +87,7 @@ MD5authdecrypt(
  * Calculate the reference id from the address. If it is an IPv4
  * address, use it as is. If it is an IPv6 address, do a md5 on
  * it and use the bottom 4 bytes.
+ * The result is in network byte order.
  */
 u_int32
 addr2refid(sockaddr_u *addr)

==== ntpd/ntp_proto.c ====
2011-11-27 00:39:10+00:00, davehart at shiny.ad.hartbrothers.com +3 -3
  [Bug 2072] Orphan parent selection metric needs ntohl().

--- 1.297.2.4/ntpd/ntp_proto.c	2011-11-21 04:35:27 -05:00
+++ 1.297.4.1/ntpd/ntp_proto.c	2011-11-26 19:39:10 -05:00
@@ -2291,10 +2291,10 @@ clock_select(void)
 			 */
 			if (peer->stratum == sys_orphan) {
 				u_int32	localmet;
-				u_int32 peermet;
+				u_int32	peermet;
 
-				localmet = peer->dstadr->addr_refid;
-				peermet = addr2refid(&peer->srcadr);
+				localmet = ntohl(peer->dstadr->addr_refid);
+				peermet = ntohl(addr2refid(&peer->srcadr));
 				if (peermet < localmet &&
 				    peermet < orphmet) {
 					typeorphan = peer;

#### ChangeSet ####
2011-11-26 23:06:48+00:00, davehart at shiny.ad.hartbrothers.com
  more merge cleanup

==== include/lib_strbuf.h ====
2011-11-26 23:06:48+00:00, davehart at shiny.ad.hartbrothers.com +4 -0
  guard against multiple inclusion (more merge cleanup)

--- 1.10/include/lib_strbuf.h	2011-01-18 12:09:37 -05:00
+++ 1.11/include/lib_strbuf.h	2011-11-26 18:06:48 -05:00
@@ -1,6 +1,8 @@
 /*
  * lib_strbuf.h - definitions for routines which use the common string buffers
  */
+#ifndef LIB_STRBUF_H
+#define LIB_STRBUF_H
 
 #include <ntp_types.h>
 #include <ntp_malloc.h>			/* for ZERO() */
@@ -26,3 +28,5 @@ extern int lib_inited;
 		(bufp) = &lib_stringbuf[lib_nextbuf++][0];	\
 		lib_nextbuf %= COUNTOF(lib_stringbuf);		\
 	} while (FALSE)
+
+#endif	/* LIB_STRBUF_H */

==== include/ntp_fp.h ====
2011-11-26 23:06:48+00:00, davehart at shiny.ad.hartbrothers.com +1 -1
  more merge cleanup

--- 1.25/include/ntp_fp.h	2011-08-19 12:06:27 -04:00
+++ 1.26/include/ntp_fp.h	2011-11-26 18:06:48 -05:00
@@ -265,7 +265,7 @@ typedef u_int32 u_fp;
 			q_tmp = ~(u_int64)ldexp(-d_tmp, 32) + 1; \
 		else \
 			q_tmp = (u_int64)ldexp(d_tmp, 32); \
-		(r_uf) = (u_int32)(q_tmp & UINT32_MAX); \
+		(r_uf) = (u_int32)(q_tmp & U_INT32_MAX); \
 		(r_ui) = (u_int32)(q_tmp >> 32); \
 	} while(0)
 

==== include/ntp_types.h ====
2011-11-26 23:06:48+00:00, davehart at shiny.ad.hartbrothers.com +11 -11
  more merge cleanup

--- 1.29/include/ntp_types.h	2011-11-26 15:06:46 -05:00
+++ 1.30/include/ntp_types.h	2011-11-26 18:06:48 -05:00
@@ -63,22 +63,22 @@ typedef unsigned int u_int;
 #ifdef HAVE_UINT32_T
 # ifndef HAVE_INT32
    typedef	int32_t		int32;
-#  ifndef INT32_MIN
-#   define INT32_MIN INT32_T_MIN
-#  endif
-#  ifndef INT32_MAX
-#   define INT32_MAX INT32_T_MAX
-#  endif
 # endif
 # ifndef HAVE_U_INT32
    typedef	uint32_t	u_int32;
-#  define U_INT32_MAX UINT32_T_MAX
+#  ifndef U_INT32_MAX
+#   define U_INT32_MAX UINT32_MAX
+#  endif
 # endif
 #elif (SIZEOF_INT == 4)
 # if !defined(HAVE_INT32) && !defined(int32)
    typedef	int		int32;
-#  define INT32_MIN INT_MIN
-#  define INT32_MAX INT_MAX
+#  ifndef INT32_MIN
+#   define INT32_MIN INT_MIN
+#  endif
+#  ifndef INT32_MAX
+#   define INT32_MAX INT_MAX
+#  endif
 # endif
 # if !defined(HAVE_U_INT32) && !defined(u_int32)
    typedef	unsigned	u_int32;
@@ -108,8 +108,8 @@ typedef unsigned int u_int;
 # endif
 #endif	/* !HAVE_UINT32_T && SIZEOF_INT != 4 */
 
-#ifndef UINT32_MAX
-#define UINT32_MAX	0xffffffff
+#ifndef U_INT32_MAX
+#define U_INT32_MAX	0xffffffff
 #endif
 
 

#### ChangeSet ####
2011-11-26 19:43:12+00:00, davehart at shiny.ad.hartbrothers.com
  Avoid redefinition warnings for INT32_MIN, INT32_MAX, U_INT32_MAX.

==== include/ntp_types.h ====
2011-11-26 19:43:11+00:00, davehart at shiny.ad.hartbrothers.com +18 -6
  Avoid redefinition warnings for INT32_MIN, INT32_MAX, U_INT32_MAX.

--- 1.14.1.2/include/ntp_types.h	2011-11-21 04:35:27 -05:00
+++ 1.14.1.3/include/ntp_types.h	2011-11-26 14:43:11 -05:00
@@ -57,23 +57,35 @@ typedef unsigned int u_int;
 #if (SIZEOF_INT == 4)
 # ifndef int32
 #  define int32 int
-#  define INT32_MIN INT_MIN
-#  define INT32_MAX INT_MAX
+#  ifndef INT32_MIN
+#   define INT32_MIN INT_MIN
+#  endif
+#  ifndef INT32_MAX
+#   define INT32_MAX INT_MAX
+#  endif
 # endif
 # ifndef u_int32
 #  define u_int32 unsigned int
-#  define U_INT32_MAX UINT_MAX
+#  ifndef U_INT32_MAX
+#   define U_INT32_MAX UINT_MAX
+#  endif
 # endif
 #else /* not sizeof(int) == 4 */
 # if (SIZEOF_LONG == 4)
 #  ifndef int32
 #   define int32 long
-#   define INT32_MIN LONG_MIN
-#   define INT32_MAX LONG_MAX
+#   ifndef INT32_MIN
+#    define INT32_MIN LONG_MIN
+#   endif
+#   ifndef INT32_MAX
+#    define INT32_MAX LONG_MAX
+#   endif
 #  endif
 #  ifndef u_int32
 #   define u_int32 unsigned long
-#   define U_INT32_MAX ULONG_MAX
+#   ifndef U_INT32_MAX
+#    define U_INT32_MAX ULONG_MAX
+#   endif
 #  endif
 # else /* not sizeof(long) == 4 */
 #  include "Bletch: what's 32 bits on this machine?"

#### ChangeSet ####
2011-11-26 12:50:07+00:00, davehart at shiny.ad.hartbrothers.com
  Restore 4.2.6 clock_combine() weighting to ntp-dev, reverting
   to pre-4.2.7p70 logic and avoiding divide-by-zero (from Dave Mills).

==== ChangeLog ====
2011-11-26 12:50:06+00:00, davehart at shiny.ad.hartbrothers.com +3 -1
  Restore 4.2.6 clock_combine() weighting to ntp-dev, reverting
   to pre-4.2.7p70 logic and avoiding divide-by-zero (from Dave Mills).

--- 1.1058/ChangeLog	2011-11-26 05:14:42 -05:00
+++ 1.1059/ChangeLog	2011-11-26 07:50:06 -05:00
@@ -7,7 +7,9 @@
 * [Bug 2069] from 4.2.6p5-RC2: broadcastclient, multicastclient spin up
   duplicate ephemeral associations without broadcastdelay.
 * from 4.2.6p5-RC2: Exclude not-yet-determined sys_refid from use in
-  loopback TEST12 (from David Mills).  
+  loopback TEST12 (from Dave Mills).
+* Restore 4.2.6 clock_combine() weighting to ntp-dev, reverting to pre-
+  4.2.7p70 logic and avoiding divide-by-zero (from Dave Mills).
 (4.2.7p236) 2011/11/16 Released by Harlan Stenn <stenn at ntp.org>
 * Documentation updates from Dave Mills.
 (4.2.7p235) 2011/11/16 Released by Harlan Stenn <stenn at ntp.org>

==== ntpd/ntp_proto.c ====
2011-11-26 12:50:06+00:00, davehart at shiny.ad.hartbrothers.com +5 -5
  Restore 4.2.6 clock_combine() weighting to ntp-dev, reverting
   to pre-4.2.7p70 logic and avoiding divide-by-zero (from Dave Mills).

--- 1.327/ntpd/ntp_proto.c	2011-11-26 07:11:54 -05:00
+++ 1.328/ntpd/ntp_proto.c	2011-11-26 07:50:06 -05:00
@@ -2903,9 +2903,6 @@ clock_select(void)
 }
 
 
-/*
- * clock_combine - compute system offset and jitter from selected peers
- */
 static void
 clock_combine(
 	struct peer **peers,	/* survivor list */
@@ -2913,11 +2910,14 @@ clock_combine(
 	)
 {
 	int	i;
-	double	x, y, z, w;
+	double	d, x, y, z, w;
 
 	y = z = w = 0;
 	for (i = 0; i < npeers; i++) {
-		x = max(sys_maxdist - root_distance(peers[i]), sys_mindisp);
+		d = root_distance(peers[i]);
+		if (0. == d)
+			d = 1e-6;	/* hart avoid div by 0 */
+		x = 1. / d;
 		y += x;
 		z += peers[i]->offset * x;
 		w += SQUARE(peers[i]->offset - peers[0]->offset) * x;

#### ChangeSet ####
2011-11-26 12:11:56+00:00, davehart at shiny.ad.hartbrothers.com
  merge cleanup

==== ntpd/ntp_control.c ====
2011-11-26 12:11:53+00:00, davehart at shiny.ad.hartbrothers.com +6 -6
  merge cleanup

--- 1.175/ntpd/ntp_control.c	2011-11-26 05:14:43 -05:00
+++ 1.176/ntpd/ntp_control.c	2011-11-26 07:11:53 -05:00
@@ -2404,16 +2404,16 @@ ctl_putpeer(
 
 	case CP_REFID:
 #ifdef REFCLOCK
-		if (peer->flags & FLAG_REFCLOCK) {
-			ctl_putrefid(peer_var[varid].text, peer->refid);
+		if (p->flags & FLAG_REFCLOCK) {
+			ctl_putrefid(peer_var[id].text, p->refid);
 			break;
 		}
 #endif
-		if (peer->stratum > 1 && peer->stratum < STRATUM_UNSPEC)
-			ctl_putadr(peer_var[varid].text, peer->refid,
+		if (p->stratum > 1 && p->stratum < STRATUM_UNSPEC)
+			ctl_putadr(peer_var[id].text, p->refid,
 				   NULL);
 		else
-			ctl_putrefid(peer_var[varid].text, peer->refid);
+			ctl_putrefid(peer_var[id].text, p->refid);
 		break;
 
 	case CP_REFTIME:
@@ -2691,7 +2691,7 @@ ctl_putclock(
 					   pcs->fudgeval2, NULL);
 			else
 				ctl_putrefid(clock_var[id].text,
-					     clock_stat->fudgeval2);
+					     pcs->fudgeval2);
 		}
 		break;
 

==== ntpd/ntp_io.c ====
2011-11-26 12:11:53+00:00, davehart at shiny.ad.hartbrothers.com +0 -11
  merge cleanup

--- 1.362/ntpd/ntp_io.c	2011-11-25 15:41:47 -05:00
+++ 1.363/ntpd/ntp_io.c	2011-11-26 07:11:53 -05:00
@@ -4290,17 +4290,6 @@ localaddrtoa(
 }
 
 
-const char *
-localaddrtoa(
-	endpt *la
-	)
-{
-	return (NULL == la)
-		   ? "<null>"
-		   : stoa(&la->sin);
-}
-
-
 #ifdef HAS_ROUTING_SOCKET
 # ifndef UPDATE_GRACE
 #  define UPDATE_GRACE	2	/* wait UPDATE_GRACE seconds before scanning */

==== ntpd/ntp_peer.c ====
2011-11-26 12:11:54+00:00, davehart at shiny.ad.hartbrothers.com +6 -4
  merge cleanup

--- 1.149/ntpd/ntp_peer.c	2011-11-26 05:14:43 -05:00
+++ 1.150/ntpd/ntp_peer.c	2011-11-26 07:11:54 -05:00
@@ -115,7 +115,8 @@ static struct peer init_peer_alloc[INIT_
 static struct peer *	findexistingpeer_name(const char *, u_short,
 					      struct peer *, int);
 static struct peer *	findexistingpeer_addr(sockaddr_u *,
-					      struct peer *, int);
+					      struct peer *, int,
+					      u_char);
 static void		free_peer(struct peer *, int);
 static void		getmorepeermem(void);
 static int		score(struct peer *);
@@ -199,7 +200,7 @@ struct peer *		
 findexistingpeer_addr(
 	sockaddr_u *	addr,
 	struct peer *	start_peer,
-	int		mode
+	int		mode,
 	u_char		cast_flags
 	)
 {
@@ -760,7 +761,7 @@ newpeer(
 	 * structure.
 	 */
 	if (dstadr != NULL) {
-		peer = findexistingpeer(srcadr, hostname, hmode,
+		peer = findexistingpeer(srcadr, hostname, NULL, hmode,
 					cast_flags);
 		while (peer != NULL) {
 			if (peer->dstadr == dstadr ||
@@ -777,7 +778,8 @@ newpeer(
 		}
 	} else {
 		/* no endpt address given */
-		peer = findexistingpeer(srcadr, hostname, hmode, cast_flags);
+		peer = findexistingpeer(srcadr, hostname, NULL, hmode,
+					cast_flags);
 	}
 
 	/*

==== ntpd/ntp_proto.c ====
2011-11-26 12:11:54+00:00, davehart at shiny.ad.hartbrothers.com +1 -1
  merge cleanup

--- 1.326/ntpd/ntp_proto.c	2011-11-26 05:14:44 -05:00
+++ 1.327/ntpd/ntp_proto.c	2011-11-26 07:11:54 -05:00
@@ -3582,7 +3582,7 @@ pool_xmit(
 		/* copy_addrinfo_list ai_addr points to a sockaddr_u */
 		rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
 		pool->ai = pool->ai->ai_next;
-		p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT);
+		p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0);
 	} while (p != NULL && pool->ai != NULL);
 	if (p != NULL)
 		return;	/* out of addresses, re-query DNS next poll */

==== ntpq/ntpq-subs.c ====
2011-11-26 12:11:54+00:00, davehart at shiny.ad.hartbrothers.com +28 -18
  merge cleanup

--- 1.93/ntpq/ntpq-subs.c	2011-11-26 05:14:44 -05:00
+++ 1.94/ntpq/ntpq-subs.c	2011-11-26 07:11:54 -05:00
@@ -13,7 +13,6 @@
 extern char *	chosts[];
 extern char	currenthost[];
 extern int	currenthostisnum;
-extern int	numhosts;
 size_t		maxhostlen;
 
 /*
@@ -1571,6 +1570,8 @@ doprintpeers(
 	int c;
 	int len;
 	int have_srchost;
+	int have_dstadr;
+	int have_da_rid;
 	int have_jitter;
 	sockaddr_u srcadr;
 	sockaddr_u dstadr;
@@ -1580,7 +1581,7 @@ doprintpeers(
 	u_long srcport = 0;
 	u_int32 u32;
 	const char *dstadr_refid = "0.0.0.0";
-	char *serverlocal;
+	const char *serverlocal;
 	size_t drlen;
 	u_long stratum = 0;
 	long ppoll = 0;
@@ -1601,6 +1602,8 @@ doprintpeers(
 	get_systime(&ts);
 	
 	have_srchost = FALSE;
+	have_dstadr = FALSE;
+	have_da_rid = FALSE;
 	have_jitter = FALSE;
 	ZERO_SOCK(&srcadr);
 	ZERO_SOCK(&dstadr);
@@ -1632,9 +1635,10 @@ doprintpeers(
 		} else if (!strcmp("dstadr", name)) {
 			if (decodenetnum(value, &dum_store)) {
 				type = decodeaddrtype(&dum_store);
-				havevar[HAVE_DSTADR] = 1;
+				have_dstadr = TRUE;
 				dstadr = dum_store;
 				if (pvl == opeervarlist) {
+					have_da_rid = TRUE;
 					dstadr_refid = trunc_left(stoa(&dstadr), 15);
 				}
 			}
@@ -1642,6 +1646,7 @@ doprintpeers(
 			decodeint(value, &hmode);
 		} else if (!strcmp("refid", name)) {
 			if (pvl == peervarlist) {
+				have_da_rid = TRUE;
 				drlen = strlen(value);
 				if (0 == drlen) {
 					dstadr_refid = "";
@@ -1659,7 +1664,7 @@ doprintpeers(
 						dstadr_refid =
 						    stoa(&refidadr);
 				} else {
-					havevar[HAVE_REFID] = 0;
+					have_da_rid = FALSE;
 				}
 			}
 		} else if (!strcmp("stratum", name)) {
@@ -1738,7 +1743,7 @@ doprintpeers(
 	else
 		c = flash2[CTL_PEER_STATVAL(rstatus) & 0x3];
 	if (numhosts > 1) {
-		if (peervarlist == pvl && havevar[HAVE_DSTADR]) {
+		if (peervarlist == pvl && have_dstadr) {
 			serverlocal = nntohost_col(&dstadr,
 			    (size_t)min(LIB_BUFLENGTH - 1, maxhostlen),
 			    TRUE);
@@ -1756,8 +1761,12 @@ doprintpeers(
 			strlcpy(clock_name, nntohost(&srcadr),
 				sizeof(clock_name));
 		fprintf(fp, "%c%-15.15s ", c, clock_name);
-		drlen = strlen(dstadr_refid);
-		makeascii(drlen, dstadr_refid, fp);
+		if (!have_da_rid) {
+			drlen = 0;
+		} else {
+			drlen = strlen(dstadr_refid);
+			makeascii(drlen, dstadr_refid, fp);
+		}
 		while (drlen++ < 15)
 			fputc(' ', fp);
 		fprintf(fp,
@@ -1834,20 +1843,20 @@ dopeers(
 	int af
 	)
 {
-	int		i;
+	u_int		u;
 	char		fullname[LENHOSTNAME];
 	sockaddr_u	netnum;
-	char *		name_or_num;
+	const char *	name_or_num;
 	size_t		sl;
 
 	if (!dogetassoc(fp))
 		return;
 
-	for (i = 0; i < numhosts; ++i) {
-		if (getnetnum(chosts[i], &netnum, fullname, af)) {
+	for (u = 0; u < numhosts; u++) {
+		if (getnetnum(chosts[u], &netnum, fullname, af)) {
 			name_or_num = nntohost(&netnum);
 			sl = strlen(name_or_num);
-			maxhostlen = max(maxhostlen, (int)sl);
+			maxhostlen = max(maxhostlen, sl);
 		}
 	}
 	if (numhosts > 1)
@@ -1856,22 +1865,23 @@ dopeers(
 	fprintf(fp,
 		"     remote           refid      st t when poll reach   delay   offset  jitter\n");
 	if (numhosts > 1)
-		for (i = 0; i <= maxhostlen; ++i)
+		for (u = 0; u <= maxhostlen; u++)
 			fprintf(fp, "=");
 	fprintf(fp,
 		"==============================================================================\n");
 
-	for (i = 0; i < numassoc; i++) {
+	for (u = 0; u < numassoc; u++) {
 		if (!showall &&
-		    !(CTL_PEER_STATVAL(assoc_cache[i].status)
+		    !(CTL_PEER_STATVAL(assoc_cache[u].status)
 		      & (CTL_PST_CONFIG|CTL_PST_REACH))) {
 			if (debug)
-				fprintf(stderr, "eliding [%d]\n", assoc_cache[i].assid);
+				fprintf(stderr, "eliding [%d]\n",
+					(int)assoc_cache[u].assid);
 			continue;
 		}
-		if (!dogetpeers(peervarlist, (int)assoc_cache[i].assid, fp, af)) {
+		if (!dogetpeers(peervarlist, (int)assoc_cache[u].assid,
+				fp, af))
 			return;
-		}
 	}
 	return;
 }

==== ntpq/ntpq.c ====
2011-11-26 12:11:54+00:00, davehart at shiny.ad.hartbrothers.com +3 -3
  merge cleanup

--- 1.142/ntpq/ntpq.c	2011-11-26 05:14:44 -05:00
+++ 1.143/ntpq/ntpq.c	2011-11-26 07:11:54 -05:00
@@ -1769,7 +1769,7 @@ nntohost_col(
 {
 	const char *	out;
 
-	if (!showhostnames || SOCK_UNSPEC(netnum)) {
+	if (!showhostnames || SOCK_UNSPEC(addr)) {
 		if (preserve_lowaddrbits)
 			out = trunc_left(stoa(addr), width);
 		else
@@ -2730,7 +2730,7 @@ asciize(
  *	"too long"	->	"too l"
  * Used for hostnames.
  */
-char *
+const char *
 trunc_right(
 	const char *	src,
 	size_t		width
@@ -2758,7 +2758,7 @@ trunc_right(
  *	"too long"	->	"_long"
  * Used for local IPv6 addresses, where low bits differentiate.
  */
-char *
+const char *
 trunc_left(
 	const char *	src,
 	size_t		width

==== ntpq/ntpq.h ====
2011-11-26 12:11:54+00:00, davehart at shiny.ad.hartbrothers.com +2 -2
  merge cleanup

--- 1.25/ntpq/ntpq.h	2011-11-26 05:14:44 -05:00
+++ 1.26/ntpq/ntpq.h	2011-11-26 07:11:54 -05:00
@@ -142,5 +142,5 @@ extern	int	decodetime	(char *, l_fp *);
 extern	void	printvars	(int, const char *, int, int, int, FILE *);
 extern	int	decodeint	(char *, long *);
 extern	void	makeascii	(int, const char *, FILE *);
-extern	char *	trunc_left	(const char *, size_t);
-extern	char *	trunc_right	(const char *, size_t);
+extern	const char * trunc_left	(const char *, size_t);
+extern	const char * trunc_right(const char *, size_t);

#### ChangeSet ####
2011-11-25 10:23:58+00:00, davehart at shiny.ad.hartbrothers.com
  [Bug 2059] optional billboard column "server" does not honor -n.
  [Bug 2066] ntpq lopeers ipv6 "local" column overrun.
  [Bug 2068] ntpd sends nonprintable stratum 16 refid to ntpq.
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.
  Exclude not-yet-determined sys_refid from use in loopback TEST12
    (from David Mills).

==== ChangeLog ====
2011-11-25 10:23:55+00:00, davehart at shiny.ad.hartbrothers.com +9 -0
  [Bug 2059] optional billboard column "server" does not honor -n.
  [Bug 2066] ntpq lopeers ipv6 "local" column overrun.
  [Bug 2068] ntpd sends nonprintable stratum 16 refid to ntpq.
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.
  Exclude not-yet-determined sys_refid from use in loopback TEST12
    (from David Mills).

--- 1.496.26.173/ChangeLog	2011-10-18 19:14:50 -04:00
+++ 1.496.61.1/ChangeLog	2011-11-25 05:23:55 -05:00
@@ -1,4 +1,13 @@
 ---
+
+* [Bug 2059] optional billboard column "server" does not honor -n.
+* [Bug 2066] ntpq lopeers ipv6 "local" column overrun.
+* [Bug 2068] ntpd sends nonprintable stratum 16 refid to ntpq.
+* [Bug 2069] broadcastclient, multicastclient spin up duplicate
+  ephemeral associations without broadcastdelay.
+* Exclude not-yet-determined sys_refid from use in loopback TEST12
+  (from David Mills).  
+---
 (4.2.6p5-RC1) 2011/10/18 Released by Harlan Stenn <stenn at ntp.org>
 
 * [Bug 2034] Listening address configuration with prefix misapplied.

==== include/ntp.h ====
2011-11-25 10:23:55+00:00, davehart at shiny.ad.hartbrothers.com +1 -0
  Add peer flag FLAG_BC_VOL indicating the broadcastclient delay
    calibration is still in progress.

--- 1.168.1.6/include/ntp.h	2011-05-15 15:19:22 -04:00
+++ 1.168.1.7/include/ntp.h	2011-11-25 05:23:55 -05:00
@@ -445,6 +445,7 @@ struct peer {
 #define	FLAG_PREEMPT	0x0002	/* preemptable association */
 #define	FLAG_AUTHENTIC	0x0004	/* last message was authentic */
 #define	FLAG_REFCLOCK	0x0008	/* this is actually a reference clock */
+#define	FLAG_BC_VOL	0x0010	/* broadcast client volleying */
 #define	FLAG_PREFER	0x0020	/* prefer peer */
 #define	FLAG_BURST	0x0040	/* burst mode */
 #define	FLAG_PPS	0x0080	/* steered by PPS */

==== include/ntp_refclock.h ====
2011-11-25 10:23:55+00:00, davehart at shiny.ad.hartbrothers.com +1 -1
  [Bug 2068] ntpd sends nonprintable stratum 16 refid to ntpq.

--- 1.24.1.1/include/ntp_refclock.h	2011-05-24 23:44:31 -04:00
+++ 1.24.1.2/include/ntp_refclock.h	2011-11-25 05:23:55 -05:00
@@ -110,7 +110,7 @@ struct refclockstat {
 	double	fudgetime1;	/* configure fudge time1 */
 	double	fudgetime2;	/* configure fudge time2 */
 	int32	fudgeval1;	/* configure fudge value1 */
-	int32	fudgeval2;	/* configure fudge value2 */
+	u_int32	fudgeval2;	/* configure fudge value2 */
 	u_char	currentstatus;	/* clock status */
 	u_char	lastevent;	/* last exception event */
 	u_char	leap;		/* leap bits */

==== include/ntp_request.h ====
2011-11-25 10:23:55+00:00, davehart at shiny.ad.hartbrothers.com +2 -2
  [Bug 2068] ntpd sends nonprintable stratum 16 refid to ntpq.

--- 1.40/include/ntp_request.h	2009-11-11 19:51:33 -05:00
+++ 1.40.1.1/include/ntp_request.h	2011-11-25 05:23:55 -05:00
@@ -823,7 +823,7 @@ struct info_clock {
 	l_fp fudgetime1;
 	l_fp fudgetime2;
 	int32 fudgeval1;
-	int32 fudgeval2;
+	u_int32 fudgeval2;
 };
 
 
@@ -834,7 +834,7 @@ struct conf_fudge {
 	u_int32 clockadr;
 	u_int32 which;
 	l_fp fudgetime;
-	int32 fudgeval_flags;
+	u_int32 fudgeval_flags;
 };
 
 #define	FUDGE_TIME1	1

==== include/ntpd.h ====
2011-11-25 10:23:55+00:00, davehart at shiny.ad.hartbrothers.com +3 -1
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.
  backport latoa() -> localaddrtoa(endpt *) from 4.2.7

--- 1.137.2.6/include/ntpd.h	2011-08-09 00:27:24 -04:00
+++ 1.137.2.7/include/ntpd.h	2011-11-25 05:23:55 -05:00
@@ -107,6 +107,8 @@ extern	void	block_io_and_alarm	(void);
 #define UNBLOCK_IO_AND_ALARM()
 #define BLOCK_IO_AND_ALARM()
 #endif
+#define		latoa(pif)	localaddrtoa(pif)
+extern const char * localaddrtoa(endpt *);
 
 /* ntp_loopfilter.c */
 extern	void	init_loopfilter(void);
@@ -126,7 +128,7 @@ extern  void    ntp_monclearinterface (s
 
 /* ntp_peer.c */
 extern	void	init_peer	(void);
-extern	struct peer *findexistingpeer (sockaddr_u *, struct peer *, int);
+extern	struct peer *findexistingpeer (sockaddr_u *, struct peer *, int, u_char);
 extern	struct peer *findpeer	(struct recvbuf *, int, int *);
 extern	struct peer *findpeerbyassoc (u_int);
 extern  void	set_peerdstadr	(struct peer *peer, struct interface *interface);

==== ntpd/ntp_config.c ====
2011-11-25 10:23:55+00:00, davehart at shiny.ad.hartbrothers.com +1 -1
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.

--- 1.241.1.9/ntpd/ntp_config.c	2011-08-09 00:27:24 -04:00
+++ 1.241.1.10/ntpd/ntp_config.c	2011-11-25 05:23:55 -05:00
@@ -3589,7 +3589,7 @@ config_unpeers(
 				DPRINTF(1, ("searching for %s\n", stoa(&peeraddr)));
 
 				while (!found) {
-					peer = findexistingpeer(&peeraddr, peer, -1);
+					peer = findexistingpeer(&peeraddr, peer, -1, 0);
 					if (!peer)
 						break;
 					if (peer->flags & FLAG_CONFIG)

==== ntpd/ntp_control.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +50 -37
  [Bug 2068] ntpd sends nonprintable stratum 16 refid to ntpq.

--- 1.129.1.6/ntpd/ntp_control.c	2010-12-14 14:40:16 -05:00
+++ 1.129.1.7/ntpd/ntp_control.c	2011-11-25 05:23:56 -05:00
@@ -59,7 +59,7 @@ static	void	ctl_putint	(const char *, lo
 static	void	ctl_putts	(const char *, l_fp *);
 static	void	ctl_putadr	(const char *, u_int32,
 				 sockaddr_u *);
-static	void	ctl_putid	(const char *, char *);
+static	void	ctl_putrefid	(const char *, u_int32);
 static	void	ctl_putarray	(const char *, double *, int);
 static	void	ctl_putsys	(int);
 static	void	ctl_putpeer	(int, struct peer *);
@@ -1254,29 +1254,43 @@ ctl_putadr(
 	ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
 }
 
+
 /*
- * ctl_putid - write a tagged clock ID into the response
+ * ctl_putrefid - send a u_int32 refid as printable text
  */
 static void
-ctl_putid(
-	const char *tag,
-	char *id
+ctl_putrefid(
+	const char *	tag,
+	u_int32		refid
 	)
 {
-	register char *cp;
-	register const char *cq;
-	char buffer[200];
-
-	cp = buffer;
-	cq = tag;
-	while (*cq != '\0')
-		*cp++ = *cq++;
-
-	*cp++ = '=';
-	NTP_INSIST((cp - buffer) < sizeof(buffer));
-	snprintf(cp, sizeof(buffer) - (cp - buffer), "%.4s", id);
-	cp += strlen(cp);
-	ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
+	char	output[16];
+	char *	optr;
+	char *	oplim;
+	char *	iptr;
+	char *	iplim;
+	char *	past_eq;
+
+	optr = output;
+	oplim = output + sizeof(output);
+	while (optr < oplim && '\0' != *tag)
+		*optr++ = *tag++;
+	if (optr < oplim) {
+		*optr++ = '=';
+		past_eq = optr;
+	}
+	if (!(optr < oplim))
+		return;
+	iptr = (char *)&refid;
+	iplim = iptr + sizeof(refid);
+	for (; optr < oplim && iptr < iplim; iptr++, optr++)
+		if (isprint(*iptr))
+			*optr = *iptr;
+		else
+			*optr = '.';
+	if (!(optr <= oplim))
+		optr = past_eq;
+	ctl_putdata(output, (u_int)(optr - output), FALSE);
 }
 
 
@@ -1353,10 +1367,9 @@ ctl_putsys(
 
 	    case CS_REFID:
 		if (sys_stratum > 1 && sys_stratum < STRATUM_UNSPEC)
-			ctl_putadr(sys_var[CS_REFID].text, sys_refid, NULL);
+			ctl_putadr(sys_var[varid].text, sys_refid, NULL);
 		else
-			ctl_putid(sys_var[CS_REFID].text,
-				  (char *)&sys_refid);
+			ctl_putrefid(sys_var[varid].text, sys_refid);
 		break;
 
 	    case CS_REFTIME:
@@ -1678,18 +1691,17 @@ ctl_putpeer(
 		break;
 
 	    case CP_REFID:
+#ifdef REFCLOCK
 		if (peer->flags & FLAG_REFCLOCK) {
-			ctl_putid(peer_var[CP_REFID].text,
-				  (char *)&peer->refid);
-		} else {
-			if (peer->stratum > 1 && peer->stratum <
-			    STRATUM_UNSPEC)
-				ctl_putadr(peer_var[CP_REFID].text,
-					   peer->refid, NULL);
-			else
-				ctl_putid(peer_var[CP_REFID].text,
-					  (char *)&peer->refid);
+			ctl_putrefid(peer_var[varid].text, peer->refid);
+			break;
 		}
+#endif
+		if (peer->stratum > 1 && peer->stratum < STRATUM_UNSPEC)
+			ctl_putadr(peer_var[varid].text, peer->refid,
+				   NULL);
+		else
+			ctl_putrefid(peer_var[varid].text, peer->refid);
 		break;
 
 	    case CP_REFTIME:
@@ -1923,8 +1935,9 @@ ctl_putclock(
 		break;
 
 	    case CC_FUDGETIME2:
-		if (mustput || (clock_stat->haveflags & CLK_HAVETIME2)) 			ctl_putdbl(clock_var[CC_FUDGETIME2].text,
-													   clock_stat->fudgetime2 * 1e3);
+		if (mustput || (clock_stat->haveflags & CLK_HAVETIME2))
+			ctl_putdbl(clock_var[CC_FUDGETIME2].text,
+				   clock_stat->fudgetime2 * 1e3);
 		break;
 
 	    case CC_FUDGEVAL1:
@@ -1937,10 +1950,10 @@ ctl_putclock(
 		if (mustput || (clock_stat->haveflags & CLK_HAVEVAL2)) {
 			if (clock_stat->fudgeval1 > 1)
 				ctl_putadr(clock_var[CC_FUDGEVAL2].text,
-					   (u_int32)clock_stat->fudgeval2, NULL);
+					   clock_stat->fudgeval2, NULL);
 			else
-				ctl_putid(clock_var[CC_FUDGEVAL2].text,
-					  (char *)&clock_stat->fudgeval2);
+				ctl_putrefid(clock_var[CC_FUDGEVAL2].text,
+					     clock_stat->fudgeval2);
 		}
 		break;
 

==== ntpd/ntp_io.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +11 -0
  backport latoa() -> localaddrtoa(endpt *) from 4.2.7

--- 1.306.1.16/ntpd/ntp_io.c	2011-10-17 16:14:43 -04:00
+++ 1.306.1.17/ntpd/ntp_io.c	2011-11-25 05:23:56 -05:00
@@ -4366,6 +4366,17 @@ find_flagged_addr_in_list(
 }
 
 
+const char *
+localaddrtoa(
+	endpt *la
+	)
+{
+	return (NULL == la)
+		   ? "<null>"
+		   : stoa(&la->sin);
+}
+
+
 #ifdef HAS_ROUTING_SOCKET
 # ifndef UPDATE_GRACE
 #  define UPDATE_GRACE	2	/* wait UPDATE_GRACE seconds before scanning */

==== ntpd/ntp_peer.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +40 -21
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.

--- 1.126.1.3/ntpd/ntp_peer.c	2010-12-20 02:54:11 -05:00
+++ 1.126.1.4/ntpd/ntp_peer.c	2011-11-25 05:23:56 -05:00
@@ -183,9 +183,10 @@ getmorepeermem(void)
  */
 struct peer *
 findexistingpeer(
-	sockaddr_u *addr,
-	struct peer *start_peer,
-	int mode
+	sockaddr_u *	addr,
+	struct peer *	start_peer,
+	int		mode,
+	u_char		cast_flags
 	)
 {
 	register struct peer *peer;
@@ -193,6 +194,12 @@ findexistingpeer(
 	/*
 	 * start_peer is included so we can locate instances of the
 	 * same peer through different interfaces in the hash table.
+	 * Without MDF_BCLNT, a match requires the same mode and remote
+	 * address.  MDF_BCLNT associations start out as MODE_CLIENT
+	 * if broadcastdelay is not specified, and switch to
+	 * MODE_BCLIENT after estimating the one-way delay.  Duplicate
+	 * associations are expanded in definition to match any other
+	 * MDF_BCLNT with the same srcadr (remote, unicast address).
 	 */
 	if (NULL == start_peer)
 		peer = peer_hash[NTP_HASH_ADDR(addr)];
@@ -200,18 +207,21 @@ findexistingpeer(
 		peer = start_peer->next;
 	
 	while (peer != NULL) {
-		if (SOCK_EQ(addr, &peer->srcadr)
-		    && NSRCPORT(addr) == NSRCPORT(&peer->srcadr)
-		    && (-1 == mode || peer->hmode == mode))
+ 		if (ADDR_PORT_EQ(addr, &peer->srcadr)
+		    && (-1 == mode || peer->hmode == mode ||
+			((MDF_BCLNT & peer->cast_flags) &&
+			 (MDF_BCLNT & cast_flags))))
 			break;
 		peer = peer->next;
 	}
-	return (peer);
+
+	return peer;
 }
 
 
 /*
- * findpeer - find and return a peer in the hash table.
+ * findpeer - find and return a peer match for a received datagram in
+ *	      the peer_hash table.
  */
 struct peer *
 findpeer(
@@ -280,8 +290,11 @@ findpeer(
 		*action = MATCH_ASSOC(NO_PEER, pkt_mode);
 	} else if (p->dstadr != rbufp->dstadr) {
 		set_peerdstadr(p, rbufp->dstadr);
-		DPRINTF(1, ("changed %s local address to match response",
-			    stoa(&p->srcadr)));
+		if (p->dstadr == rbufp->dstadr) {
+			DPRINTF(1, ("Changed %s local address to match response\n",
+				    stoa(&p->srcadr)));
+			return findpeer(rbufp, pkt_mode, action);
+		}
 	}
 	return p;
 }
@@ -538,13 +551,12 @@ set_peerdstadr(
 	if (p->dstadr == dstadr)
 		return;
 
-	if (dstadr != NULL && (MDF_BCLNT & p->cast_flags) &&
-	    (dstadr->flags & INT_MCASTIF) && p->burst) {
-		/*
-		 * don't accept updates to a true multicast
-		 * reception interface while a BCLNT peer is
-		 * running it's unicast protocol
-		 */
+	/*
+	 * Don't accept updates to a separate multicast receive-only
+	 * endpt while a BCLNT peer is running its unicast protocol.
+	 */
+	if (dstadr != NULL && (FLAG_BC_VOL & p->flags) &&
+	    (INT_MCASTIF & dstadr->flags) && MODE_CLIENT == p->hmode) {
 		return;
 	}
 	if (p->dstadr != NULL) {
@@ -754,23 +766,30 @@ newpeer(
 	 * actual interface, because that's what gets put into the peer
 	 * structure.
 	 */
-	peer = findexistingpeer(srcadr, NULL, hmode);
 	if (dstadr != NULL) {
+		peer = findexistingpeer(srcadr, NULL, hmode, cast_flags);
 		while (peer != NULL) {
-			if (peer->dstadr == dstadr)
+			if (peer->dstadr == dstadr ||
+			    ((MDF_BCLNT & cast_flags) &&
+			     (MDF_BCLNT & peer->cast_flags)))
 				break;
 
 			if (dstadr == ANY_INTERFACE_CHOOSE(srcadr) &&
 			    peer->dstadr == findinterface(srcadr))
 				break;
 
-			peer = findexistingpeer(srcadr, peer, hmode);
+			peer = findexistingpeer(srcadr, peer, hmode,
+						cast_flags);
 		}
+	} else {
+		/* no endpt address given */
+		peer = findexistingpeer(srcadr, NULL, hmode, cast_flags);
 	}
 
 	/*
 	 * If a peer is found, this would be a duplicate and we don't
-	 * allow that. This is mostly to avoid duplicate pool
+	 * allow that. This avoids duplicate ephemeral (broadcast/
+	 * multicast) and preemptible (manycast and pool) client
 	 * associations.
 	 */
 	if (peer != NULL)

==== ntpd/ntp_proto.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +53 -14
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.
  Exclude not-yet-determined sys_refid from use in loopback TEST12
    (from David Mills).

--- 1.297.2.3/ntpd/ntp_proto.c	2011-05-15 15:19:22 -04:00
+++ 1.297.3.1/ntpd/ntp_proto.c	2011-11-25 05:23:56 -05:00
@@ -125,6 +125,7 @@ static	void	fast_xmit	(struct recvbuf *,
 				    int);
 static	void	clock_update	(struct peer *);
 static	int	default_get_precision (void);
+static	int	local_refid	(struct peer *);
 static	int	peer_unfit	(struct peer *);
 
 
@@ -309,6 +310,7 @@ receive(
 	u_int32	opcode = 0;		/* extension field opcode */
 	sockaddr_u *dstadr_sin; 	/* active runway */
 	struct peer *peer2;		/* aux peer structure pointer */
+	endpt *	match_ep;		/* newpeer() local address */
 	l_fp	p_org;			/* origin timestamp */
 	l_fp	p_rec;			/* receive timestamp */
 	l_fp	p_xmt;			/* transmit timestamp */
@@ -849,6 +851,20 @@ receive(
 #endif /* OPENSSL */
 
 		/*
+		 * Broadcasts received via a multicast address may
+		 * arrive after a unicast volley has begun
+		 * with the same remote address.  newpeer() will not
+		 * find duplicate associations on other local endpoints
+		 * if a non-NULL endpoint is supplied.  multicastclient
+		 * ephemeral associations are unique across all local
+		 * endpoints.
+		 */
+		if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
+			match_ep = rbufp->dstadr;
+		else
+			match_ep = NULL;
+
+		/*
 		 * Determine whether to execute the initial volley.
 		 */
 		if (sys_bdelay != 0) {
@@ -867,10 +883,11 @@ receive(
 			 * Do not execute the volley. Start out in
 			 * broadcast client mode.
 			 */
-			if ((peer = newpeer(&rbufp->recv_srcadr,
-			    rbufp->dstadr, MODE_BCLIENT, hisversion,
-			    pkt->ppoll, pkt->ppoll, 0, 0, 0,
-			    skeyid)) == NULL) {
+			peer = newpeer(&rbufp->recv_srcadr, match_ep,
+			    MODE_BCLIENT, hisversion, pkt->ppoll,
+			    pkt->ppoll, FLAG_PREEMPT, MDF_BCLNT, 0,
+			    skeyid);
+			if (NULL == peer) {
 				sys_restricted++;
 				return;		/* ignore duplicate */
 
@@ -889,10 +906,11 @@ receive(
 		 * packet, normally 6 (64 s) and that the poll interval
 		 * is fixed at this value.
 		 */
-		if ((peer = newpeer(&rbufp->recv_srcadr, rbufp->dstadr,
+		peer = newpeer(&rbufp->recv_srcadr, match_ep,
 		    MODE_CLIENT, hisversion, pkt->ppoll, pkt->ppoll,
-		    FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT, 0,
-		    skeyid)) == NULL) {
+		    FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
+		    0, skeyid);
+		if (NULL == peer) {
 			sys_restricted++;
 			return;			/* ignore duplicate */
 		}
@@ -1495,8 +1513,8 @@ process_packet(
 		 * timestamp. This works for both basic and interleaved
 		 * modes.
 		 */
-		if (peer->cast_flags & MDF_BCLNT) {
-			peer->cast_flags &= ~MDF_BCLNT;
+		if (FLAG_BC_VOL & peer->flags) {
+			peer->flags &= ~FLAG_BC_VOL;
 			peer->delay = (peer->offset - p_offset) * 2;
 		}
 		p_del = peer->delay;
@@ -1602,8 +1620,8 @@ process_packet(
 	 * client mode when the client is fit and the autokey dance is
 	 * complete.
 	 */
-	if ((peer->cast_flags & MDF_BCLNT) && !(peer_unfit(peer) &
-	    TEST11)) {
+	if ((FLAG_BC_VOL & peer->flags) && MODE_CLIENT == peer->hmode &&
+	    !(TEST11 & peer_unfit(peer))) {	/* distance exceeded */
 #ifdef OPENSSL
 		if (peer->flags & FLAG_SKEY) {
 			if (!(~peer->crypto & CRYPTO_FLAG_ALL))
@@ -3390,6 +3408,29 @@ key_expire(
 
 
 /*
+ * local_refid(peer) - check peer refid to avoid selecting peers
+ *		       currently synced to this ntpd.
+ */
+static int
+local_refid(
+	struct peer *	p
+	)
+{
+	endpt *	unicast_ep;
+
+	if (!(INT_MCASTIF & p->dstadr->flags))
+		unicast_ep = p->dstadr;
+	else
+		unicast_ep = findinterface(&p->srcadr);
+
+	if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
+		return TRUE;
+	else
+		return FALSE;
+}
+
+
+/*
  * Determine if the peer is unfit for synchronization
  *
  * A peer is unfit for synchronization if
@@ -3429,9 +3470,7 @@ peer_unfit(
 	 * server as the local peer but only if the remote peer is
 	 * neither a reference clock nor an orphan.
 	 */
-	if (peer->stratum > 1 && peer->refid != htonl(LOOPBACKADR) &&
-	    (peer->refid == (peer->dstadr ? peer->dstadr->addr_refid :
-	    0) || peer->refid == sys_refid))
+	if (peer->stratum > 1 && local_refid(peer))
 		rval |= TEST12;		/* synchronization loop */
 
 	/*

==== ntpd/ntp_refclock.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +2 -2
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.

--- 1.92.1.2/ntpd/ntp_refclock.c	2010-12-14 14:40:16 -05:00
+++ 1.92.1.3/ntpd/ntp_refclock.c	2011-11-25 05:23:56 -05:00
@@ -1041,7 +1041,7 @@ refclock_control(
 	clktype = (u_char)REFCLOCKTYPE(srcadr);
 	unit = REFCLOCKUNIT(srcadr);
 
-	peer = findexistingpeer(srcadr, NULL, -1);
+	peer = findexistingpeer(srcadr, NULL, -1, 0);
 
 	if (NULL == peer || NULL == peer->procptr)
 		return;
@@ -1140,7 +1140,7 @@ refclock_buginfo(
 	clktype = (u_char) REFCLOCKTYPE(srcadr);
 	unit = REFCLOCKUNIT(srcadr);
 
-	peer = findexistingpeer(srcadr, NULL, -1);
+	peer = findexistingpeer(srcadr, NULL, -1, 0);
 
 	if (NULL == peer || NULL == peer->procptr)
 		return;

==== ntpd/ntp_request.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +13 -11
  [Bug 2069] broadcastclient, multicastclient spin up duplicate
    ephemeral associations without broadcastdelay.

--- 1.82.1.5/ntpd/ntp_request.c	2011-08-26 23:34:09 -04:00
+++ 1.82.1.6/ntpd/ntp_request.c	2011-11-25 05:23:56 -05:00
@@ -852,7 +852,8 @@ peer_info (
 		addr.sa.sa_len = SOCKLEN(&addr);
 #endif
 		ipl++;
-		if ((pp = findexistingpeer(&addr, (struct peer *)0, -1)) == 0)
+		pp = findexistingpeer(&addr, NULL, -1, 0);
+		if (NULL == pp)
 			continue;
 		if (IS_IPV6(srcadr)) {
 			if (pp->dstadr)
@@ -990,7 +991,8 @@ peer_stats (
 		ipl = (struct info_peer_list *)((char *)ipl +
 		    INFO_ITEMSIZE(inpkt->mbz_itemsize));
 
-		if ((pp = findexistingpeer(&addr, (struct peer *)0, -1)) == NULL)
+		pp = findexistingpeer(&addr, NULL, -1, 0);
+		if (NULL == pp)
 			continue;
 
 		DPRINTF(1, ("peer_stats: found %s\n", stoa(&addr)));
@@ -1575,7 +1577,7 @@ do_unconf(
 		DPRINTF(1, ("searching for %s\n", stoa(&peeraddr)));
 
 		while (!found) {
-			peer = findexistingpeer(&peeraddr, peer, -1);
+			peer = findexistingpeer(&peeraddr, peer, -1, 0);
 			if (!peer)
 				break;
 			if (peer->flags & FLAG_CONFIG)
@@ -1618,7 +1620,7 @@ do_unconf(
 		peer = NULL;
 
 		while (!found) {
-			peer = findexistingpeer(&peeraddr, peer, -1);
+			peer = findexistingpeer(&peeraddr, peer, -1, 0);
 			if (!peer)
 				break;
 			if (peer->flags & FLAG_CONFIG)
@@ -2135,7 +2137,7 @@ reset_peer(
 #ifdef ISC_PLATFORM_HAVESALEN
 		peeraddr.sa.sa_len = SOCKLEN(&peeraddr);
 #endif
-		peer = findexistingpeer(&peeraddr, NULL, -1);
+		peer = findexistingpeer(&peeraddr, NULL, -1, 0);
 		if (NULL == peer)
 			bad++;
 		cp = (struct conf_unpeer *)((char *)cp +
@@ -2166,10 +2168,10 @@ reset_peer(
 #ifdef ISC_PLATFORM_HAVESALEN
 		peeraddr.sa.sa_len = SOCKLEN(&peeraddr);
 #endif
-		peer = findexistingpeer(&peeraddr, NULL, -1);
+		peer = findexistingpeer(&peeraddr, NULL, -1, 0);
 		while (peer != NULL) {
 			peer_reset(peer);
-			peer = findexistingpeer(&peeraddr, peer, -1);
+			peer = findexistingpeer(&peeraddr, peer, -1, 0);
 		}
 		cp = (struct conf_unpeer *)((char *)cp +
 		    INFO_ITEMSIZE(inpkt->mbz_itemsize));
@@ -2682,7 +2684,7 @@ get_clock_info(
 	while (items-- > 0) {
 		NSRCADR(&addr) = *clkaddr++;
 		if (!ISREFCLOCKADR(&addr) ||
-		    findexistingpeer(&addr, NULL, -1) == NULL) {
+		    findexistingpeer(&addr, NULL, -1, 0) == NULL) {
 			req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
 			return;
 		}
@@ -2706,7 +2708,7 @@ get_clock_info(
 		DTOLFP(clock_stat.fudgetime2, &ltmp);
 		HTONL_FP(&ltmp, &ic->fudgetime2);
 		ic->fudgeval1 = htonl((u_int32)clock_stat.fudgeval1);
-		ic->fudgeval2 = htonl((u_int32)clock_stat.fudgeval2);
+		ic->fudgeval2 = htonl(clock_stat.fudgeval2);
 
 		free_varlist(clock_stat.kv_list);
 
@@ -2746,7 +2748,7 @@ set_clock_fudge(
 #endif
 		SET_PORT(&addr, NTP_PORT);
 		if (!ISREFCLOCKADR(&addr) ||
-		    findexistingpeer(&addr, NULL, -1) == 0) {
+		    findexistingpeer(&addr, NULL, -1, 0) == 0) {
 			req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
 			return;
 		}
@@ -2821,7 +2823,7 @@ get_clkbug_info(
 	while (items-- > 0) {
 		NSRCADR(&addr) = *clkaddr++;
 		if (!ISREFCLOCKADR(&addr) ||
-		    findexistingpeer(&addr, NULL, -1) == 0) {
+		    findexistingpeer(&addr, NULL, -1, 0) == 0) {
 			req_ack(srcadr, inter, inpkt, INFO_ERR_NODATA);
 			return;
 		}

==== ntpq/ntpq-subs.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +42 -22
  [Bug 2059] optional billboard column "server" does not honor -n.
  [Bug 2066] ntpq lopeers ipv6 "local" column overrun.

--- 1.39.2.5/ntpq/ntpq-subs.c	2010-12-14 14:40:17 -05:00
+++ 1.39.2.6/ntpq/ntpq-subs.c	2011-11-25 05:23:56 -05:00
@@ -13,6 +13,7 @@
 
 extern char *	chosts[];
 extern char currenthost[];
+extern int currenthostisnum;
 extern int	numhosts;
 int 	maxhostlen;
 
@@ -1442,8 +1443,10 @@ doprintpeers(
 
 	sockaddr_u srcadr;
 	sockaddr_u dstadr;
+	sockaddr_u refidadr;
 	u_long srcport = 0;
 	char *dstadr_refid = "0.0.0.0";
+	char *serverlocal;
 	size_t drlen;
 	u_long stratum = 0;
 	long ppoll = 0;
@@ -1490,10 +1493,10 @@ doprintpeers(
 			case CP_DSTADR:
 			if (decodenetnum(value, &dum_store)) {
 				type = decodeaddrtype(&dum_store);
+				havevar[HAVE_DSTADR] = 1;
+				dstadr = dum_store;
 				if (pvl == opeervarlist) {
-					havevar[HAVE_DSTADR] = 1;
-					dstadr = dum_store;
-					dstadr_refid = stoa(&dstadr);
+					dstadr_refid = trunc_left(stoa(&dstadr), 15);
 				}
 			}
 			break;
@@ -1509,15 +1512,15 @@ doprintpeers(
 					refid_string[i] = '.';
 					refid_string[i+1] = '\0';
 					dstadr_refid = refid_string;
-				} else if (decodenetnum(value, &dstadr)) {
-					if (SOCK_UNSPEC(&dstadr))
+				} else if (decodenetnum(value, &refidadr)) {
+					if (SOCK_UNSPEC(&refidadr))
 						dstadr_refid = "0.0.0.0";
-					else if (ISREFCLOCKADR(&dstadr))
+					else if (ISREFCLOCKADR(&refidadr))
 						dstadr_refid =
-						    refnumtoa(&dstadr);
+						    refnumtoa(&refidadr);
 					else
 						dstadr_refid =
-						    stoa(&dstadr);
+						    stoa(&refidadr);
 				} else {
 					havevar[HAVE_REFID] = 0;
 				}
@@ -1595,8 +1598,20 @@ doprintpeers(
 		c = flash3[CTL_PEER_STATVAL(rstatus) & 0x7];
 	else
 		c = flash2[CTL_PEER_STATVAL(rstatus) & 0x3];
-	if (numhosts > 1)
-		fprintf(fp, "%-*s ", maxhostlen, currenthost);
+	if (numhosts > 1) {
+		if (peervarlist == pvl && havevar[HAVE_DSTADR]) {
+			serverlocal = nntohost_col(&dstadr,
+			    (size_t)min(LIB_BUFLENGTH - 1, maxhostlen),
+			    TRUE);
+		} else {
+			if (currenthostisnum)
+				serverlocal = trunc_left(currenthost,
+							 maxhostlen);
+			else
+				serverlocal = currenthost;
+		}
+		fprintf(fp, "%-*s ", maxhostlen, serverlocal);
+	}
 	if (AF_UNSPEC == af || AF(&srcadr) == af) {
 		strncpy(clock_name, nntohost(&srcadr), sizeof(clock_name));		
 		fprintf(fp, "%c%-15.15s ", c, clock_name);
@@ -1694,27 +1709,32 @@ dopeers(
 	int af
 	)
 {
-	register int i;
-	char fullname[LENHOSTNAME];
-	sockaddr_u netnum;
+	int		i;
+	char		fullname[LENHOSTNAME];
+	sockaddr_u	netnum;
+	char *		name_or_num;
+	size_t		sl;
 
 	if (!dogetassoc(fp))
 		return;
 
 	for (i = 0; i < numhosts; ++i) {
-		if (getnetnum(chosts[i], &netnum, fullname, af))
-			if ((int)strlen(fullname) > maxhostlen)
-				maxhostlen = strlen(fullname);
+		if (getnetnum(chosts[i], &netnum, fullname, af)) {
+			name_or_num = nntohost(&netnum);
+			sl = strlen(name_or_num);
+			maxhostlen = max(maxhostlen, (int)sl);
+		}
 	}
 	if (numhosts > 1)
-		(void) fprintf(fp, "%-*.*s ", maxhostlen, maxhostlen, "server");
-	(void) fprintf(fp,
-			   "     remote           refid      st t when poll reach   delay   offset  jitter\n");
+		fprintf(fp, "%-*.*s ", maxhostlen, maxhostlen,
+			"server (local)");
+	fprintf(fp,
+		"     remote           refid      st t when poll reach   delay   offset  jitter\n");
 	if (numhosts > 1)
 		for (i = 0; i <= maxhostlen; ++i)
-		(void) fprintf(fp, "=");
-	(void) fprintf(fp,
-			   "==============================================================================\n");
+			fprintf(fp, "=");
+	fprintf(fp,
+		"==============================================================================\n");
 
 	for (i = 0; i < numassoc; i++) {
 		if (!showall &&

==== ntpq/ntpq.c ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +94 -9
  [Bug 2059] optional billboard column "server" does not honor -n.

--- 1.97.1.9/ntpq/ntpq.c	2011-07-27 17:40:59 -04:00
+++ 1.97.1.10/ntpq/ntpq.c	2011-11-25 05:23:56 -05:00
@@ -379,6 +379,7 @@ struct sock_timeval tvout = { DEFTIMEOUT
 struct sock_timeval tvsout = { DEFSTIMEOUT, 0 };/* secondary time out */
 l_fp delay_time;				/* delay time */
 char currenthost[LENHOSTNAME];			/* current host name */
+int currenthostisnum;				/* is prior text from IP? */
 struct sockaddr_in hostaddr = { 0 };		/* host address */
 int showhostnames = 1;				/* show host names by default */
 
@@ -657,7 +658,7 @@ openhost(
 	 * give it an IPv4 address to lookup.
 	 */
 	strcpy(service, "ntp");
-	memset((char *)&hints, 0, sizeof(struct addrinfo));
+	ZERO(hints);
 	hints.ai_family = ai_fam_templ;
 	hints.ai_protocol = IPPROTO_UDP;
 	hints.ai_socktype = SOCK_DGRAM;
@@ -687,13 +688,14 @@ openhost(
 		return 0;
 	}
 
-	if (ai->ai_canonname == NULL) {
+	if (!showhostnames || ai->ai_canonname == NULL) {
 		strncpy(temphost, 
 			stoa((sockaddr_u *)ai->ai_addr),
 			LENHOSTNAME);
-
+		currenthostisnum = TRUE;
 	} else {
 		strncpy(temphost, ai->ai_canonname, LENHOSTNAME);
+		currenthostisnum = FALSE;
 	}
 	temphost[LENHOSTNAME-1] = '\0';
 
@@ -1879,12 +1881,39 @@ nntohost(
 	sockaddr_u *netnum
 	)
 {
-	if (!showhostnames)
-		return stoa(netnum);
-	else if (ISREFCLOCKADR(netnum))
-		return refnumtoa(netnum);
-	else
-		return socktohost(netnum);
+	return nntohost_col(netnum, LIB_BUFLENGTH - 1, FALSE);
+}
+
+
+/*
+ * nntohost_col - convert network number to host name in fixed width.
+ *		  This routine enforces the showhostnames setting.
+ *		  When displaying hostnames longer than the width,
+ *		  the first part of the hostname is displayed.  When
+ *		  displaying numeric addresses longer than the width,
+ *		  Such as IPv6 addresses, the caller decides whether
+ *		  the first or last of the numeric address is used.
+ */
+char *
+nntohost_col(
+	sockaddr_u *	addr,
+	size_t		width,
+	int		preserve_lowaddrbits
+	)
+{
+	const char *	out;
+
+	if (!showhostnames) {
+		if (preserve_lowaddrbits)
+			out = trunc_left(stoa(addr), width);
+		else
+			out = trunc_right(stoa(addr), width);
+	} else if (ISREFCLOCKADR(addr)) {
+		out = refnumtoa(addr);
+	} else {
+		out = trunc_right(socktohost(addr), width);
+	}
+	return out;
 }
 
 
@@ -2815,6 +2844,62 @@ asciize(
 {
 	makeascii(length, data, fp);
 	putc('\n', fp);
+}
+
+
+/*
+ * truncate string to fit clipping excess at end.
+ *	"too long"	->	"too l"
+ * Used for hostnames.
+ */
+char *
+trunc_right(
+	const char *	src,
+	size_t		width
+	)
+{
+	size_t	sl;
+	char *	out;
+
+	
+	sl = strlen(src);
+	if (sl > width && LIB_BUFLENGTH - 1 > width && width > 0) {
+		LIB_GETBUF(out);
+		memcpy(out, src, width);
+		out[width] = '\0';
+
+		return out;
+	}
+
+	return src;
+}
+
+
+/*
+ * truncate string to fit by preserving right side and using '_' to hint
+ *	"too long"	->	"_long"
+ * Used for local IPv6 addresses, where low bits differentiate.
+ */
+char *
+trunc_left(
+	const char *	src,
+	size_t		width
+	)
+{
+	size_t	sl;
+	char *	out;
+
+
+	sl = strlen(src);
+	if (sl > width && LIB_BUFLENGTH - 1 > width && width > 1) {
+		LIB_GETBUF(out);
+		out[0] = '_';
+		memcpy(&out[1], &src[sl + 1 - width], width);
+
+		return out;
+	}
+
+	return src;
 }
 
 

==== ntpq/ntpq.h ====
2011-11-25 10:23:56+00:00, davehart at shiny.ad.hartbrothers.com +4 -0
  [Bug 2059] optional billboard column "server" does not honor -n.
  [Bug 2066] ntpq lopeers ipv6 "local" column overrun.

--- 1.10.1.4/ntpq/ntpq.h	2010-12-14 14:40:17 -05:00
+++ 1.10.1.5/ntpq/ntpq.h	2011-11-25 05:23:56 -05:00
@@ -6,6 +6,7 @@
 #include "ntp_control.h"
 #include "ntp_string.h"
 #include "ntp_malloc.h"
+#include "lib_strbuf.h"
 
 /*
  * Maximum number of arguments
@@ -91,6 +92,7 @@ extern	int	doquery		(int, associd_t, int
 extern	int	doqueryex	(int, associd_t, int, int, char *,
 				 u_short *, int *, const char **, int);
 extern	char *	nntohost	(sockaddr_u *);
+extern	char *	nntohost_col	(sockaddr_u *, size_t, int);
 extern	int	decodets	(char *, l_fp *);
 extern	int	decodeuint	(char *, u_long *);
 extern	int	nextvar		(int *, const char **, char **, char **);
@@ -99,3 +101,5 @@ extern	void	printvars	(int, const char *
 extern	int	decodeint	(char *, long *);
 extern	int	findvar		(char *, struct ctl_var *, int code);
 extern	void	makeascii	(int, const char *, FILE *);
+extern	char *	trunc_left	(const char *, size_t);
+extern	char *	trunc_right	(const char *, size_t);

#### ChangeSet ####
2011-11-21 09:35:28+00:00, davehart at shiny.ad.hartbrothers.com
  [Bug 2050] Orphan mode stratum counting to infinity.

==== ChangeLog ====
2011-11-21 09:35:27+00:00, davehart at shiny.ad.hartbrothers.com +4 -0
  [Bug 2050] Orphan mode stratum counting to infinity.

--- 1.496.26.173/ChangeLog	2011-10-18 19:14:50 -04:00
+++ 1.496.26.174/ChangeLog	2011-11-21 04:35:27 -05:00
@@ -1,4 +1,8 @@
 ---
+
+* [Bug 2050] Orphan mode stratum counting to infinity.
+
+---
 (4.2.6p5-RC1) 2011/10/18 Released by Harlan Stenn <stenn at ntp.org>
 
 * [Bug 2034] Listening address configuration with prefix misapplied.

==== include/ntp_types.h ====
2011-11-21 09:35:27+00:00, davehart at shiny.ad.hartbrothers.com +6 -0
  [Bug 2050] Orphan mode stratum counting to infinity.

--- 1.14.1.1/include/ntp_types.h	2011-05-24 23:44:31 -04:00
+++ 1.14.1.2/include/ntp_types.h	2011-11-21 04:35:27 -05:00
@@ -57,17 +57,23 @@ typedef unsigned int u_int;
 #if (SIZEOF_INT == 4)
 # ifndef int32
 #  define int32 int
+#  define INT32_MIN INT_MIN
+#  define INT32_MAX INT_MAX
 # endif
 # ifndef u_int32
 #  define u_int32 unsigned int
+#  define U_INT32_MAX UINT_MAX
 # endif
 #else /* not sizeof(int) == 4 */
 # if (SIZEOF_LONG == 4)
 #  ifndef int32
 #   define int32 long
+#   define INT32_MIN LONG_MIN
+#   define INT32_MAX LONG_MAX
 #  endif
 #  ifndef u_int32
 #   define u_int32 unsigned long
+#   define U_INT32_MAX ULONG_MAX
 #  endif
 # else /* not sizeof(long) == 4 */
 #  include "Bletch: what's 32 bits on this machine?"

==== ntpd/ntp_proto.c ====
2011-11-21 09:35:27+00:00, davehart at shiny.ad.hartbrothers.com +31 -8
  [Bug 2050] Orphan mode stratum counting to infinity.

--- 1.297.2.3/ntpd/ntp_proto.c	2011-05-15 15:19:22 -04:00
+++ 1.297.2.4/ntpd/ntp_proto.c	2011-11-21 04:35:27 -05:00
@@ -2204,7 +2204,7 @@ clock_select(void)
 	double	high, low;
 	double	seljitter;
 	double	synch[NTP_MAXASSOC], error[NTP_MAXASSOC];
-	double	orphdist = 1e10;
+	double	orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
 	struct peer *osys_peer = NULL;
 	struct peer *sys_prefer = NULL;	/* prefer peer */
 	struct peer *typesystem = NULL;
@@ -2279,20 +2279,43 @@ clock_select(void)
 				continue;
 
 			/*
-			 * If this is an orphan, choose the one with
-			 * the lowest metric defined as the IPv4 address
-			 * or the first 64 bits of the hashed IPv6 address.
+			 * If this peer is an orphan parent, elect the
+			 * one with the lowest metric defined as the
+			 * IPv4 address or the first 64 bits of the
+			 * hashed IPv6 address.  To ensure convergence
+			 * on the same selected orphan, consider as
+			 * well that this system may have the lowest
+			 * metric and be the orphan parent.  If this
+			 * system wins, sys_peer will be NULL to trigger
+			 * orphan mode in timer().
 			 */
 			if (peer->stratum == sys_orphan) {
-				double	ftemp;
+				u_int32	localmet;
+				u_int32 peermet;
 
-				ftemp = addr2refid(&peer->srcadr);
-				if (ftemp < orphdist) {
+				localmet = peer->dstadr->addr_refid;
+				peermet = addr2refid(&peer->srcadr);
+				if (peermet < localmet &&
+				    peermet < orphmet) {
 					typeorphan = peer;
-					orphdist = ftemp;
+					orphmet = peermet;
 				}
 				continue;
 			}
+
+			/*
+			 * If this peer could have the orphan parent
+			 * as a synchronization ancestor, exclude it
+			 * from selection to avoid forming a 
+			 * synchronization loop within the orphan mesh,
+			 * triggering stratum climb to infinity 
+			 * instability.  Peers at stratum higher than
+			 * the orphan stratum could have the orphan
+			 * parent in ancestry so are excluded.
+			 * See http://bugs.ntp.org/2050
+			 */
+			if (peer->stratum > sys_orphan)
+				continue;
 #ifdef REFCLOCK
 			/*
 			 * The following are special cases. We deal

==== ntpd/ntp_timer.c ====
2011-11-21 09:35:27+00:00, davehart at shiny.ad.hartbrothers.com +2 -2
  [Bug 2050] Orphan mode stratum counting to infinity.

--- 1.56/ntpd/ntp_timer.c	2009-12-07 05:58:18 -05:00
+++ 1.56.1.1/ntpd/ntp_timer.c	2011-11-21 04:35:27 -05:00
@@ -302,8 +302,8 @@ timer(void)
 
 	/*
 	 * Orphan mode is active when enabled and when no servers less
-	 * than the orphan statum are available. A server with no other
-	 * synchronization source is an orphan It shows offset zero and
+	 * than the orphan stratum are available. A server with no other
+	 * synchronization source is an orphan. It shows offset zero and
 	 * reference ID the loopback address.
 	 */
 	if (sys_orphan < STRATUM_UNSPEC && sys_peer == NULL) {


More information about the bk-ntp-dev-send mailing list