[Git][NTPsec/ntpsec][master] Structified ntp_restrict.c related globals.

James Browning gitlab at mg.gitlab.com
Wed Aug 21 00:39:06 UTC 2019



James Browning pushed to branch master at NTPsec / ntpsec


Commits:
79341a8a by Ian Bruene at 2019-08-20T22:00:21Z
Structified ntp_restrict.c related globals.

- - - - -


8 changed files:

- include/ntpd.h
- ntpd/ntp_config.c
- ntpd/ntp_control.c
- ntpd/ntp_loopfilter.c
- ntpd/ntp_monitor.c
- ntpd/ntp_proto.c
- ntpd/ntp_restrict.c
- tests/ntpd/restrict.c


Changes:

=====================================
include/ntpd.h
=====================================
@@ -407,10 +407,13 @@ extern volatile struct signals_detected sig_flags;
 
 
 /* ntp_restrict.c */
-extern restrict_u *	restrictlist4;	/* IPv4 restriction list */
-extern restrict_u *	restrictlist6;	/* IPv6 restriction list */
-extern int		ntp_minpkt;
-extern uint8_t		ntp_minpoll;
+struct restriction_data {
+  restrict_u *restrictlist4; /* IPv4 restriction list */
+  restrict_u *restrictlist6; /* IPv6 restriction list */
+  int        ntp_minpkt;     /* minimum (log 2 s) */
+  uint8_t    ntp_minpoll;    /* increment (log 2 s) */
+};
+extern struct restriction_data rstrct;
 
 /* ntp_signd.c */
 


=====================================
ntpd/ntp_config.c
=====================================
@@ -1593,7 +1593,7 @@ config_access(
 		case T_Average:
 			if (0 <= my_opt->value.i &&
 			    my_opt->value.i <= UCHAR_MAX)
-				ntp_minpoll = (uint8_t)my_opt->value.u;
+				rstrct.ntp_minpoll = (uint8_t)my_opt->value.u;
 			else
 				msyslog(LOG_ERR,
 					"CONFIG: discard average %d out of range, ignored.",
@@ -1601,7 +1601,7 @@ config_access(
 			break;
 
 		case T_Minimum:
-			ntp_minpkt = my_opt->value.i;
+			rstrct.ntp_minpkt = my_opt->value.i;
 			break;
 
 		case T_Monitor:


=====================================
ntpd/ntp_control.c
=====================================
@@ -1587,7 +1587,7 @@ ctl_putsys(
 #endif	/* ENABLE_LEAP_SMEAR */
 
 	case CS_RATE:
-		ctl_putuint(sys_var[CS_RATE].text, ntp_minpoll);
+		ctl_putuint(sys_var[CS_RATE].text, rstrct.ntp_minpoll);
 		break;
 
 	case CS_MRU_ENABLED:
@@ -3973,8 +3973,8 @@ read_addr_restrictions(
 	UNUSED_ARG(rbufp);
 
 	idx = 0;
-	send_restrict_list(restrictlist4, false, &idx);
-	send_restrict_list(restrictlist6, true, &idx);
+	send_restrict_list(rstrct.restrictlist4, false, &idx);
+	send_restrict_list(rstrct.restrictlist6, true, &idx);
 	ctl_flushpkt(0);
 }
 


=====================================
ntpd/ntp_loopfilter.c
=====================================
@@ -214,7 +214,7 @@ init_loopfilter(void) {
 	/*
 	 * Initialize state variables.
 	 */
-	clkstate.sys_poll = ntp_minpoll;
+	clkstate.sys_poll = rstrct.ntp_minpoll;
 	clkstate.clock_jitter = LOGTOD(sys_vars.sys_precision);
 	freq_cnt = (int)clock_minstep;
 }


=====================================
ntpd/ntp_monitor.c
=====================================
@@ -64,15 +64,6 @@ static  mon_entry *mon_free;		/* free list or null if none */
 static	uint64_t mru_alloc;		/* mru list + free list count */
 static	uint64_t mon_mem_increments;	/* times called malloc() */
 
-/*
- * Parameters of the RES_LIMITED restriction option. We define headway
- * as the idle time between packets. A packet is discarded if the
- * headway is less than the minimum, as well as if the average headway
- * is less than eight times the increment.
- */
-int	ntp_minpkt = NTP_MINPKT;	/* minimum (log 2 s) */
-uint8_t	ntp_minpoll = NTP_MINPOLL;	/* increment (log 2 s) */
-
 static	void	mon_getmoremem(void);
 static	void	remove_from_hash(mon_entry *);
 static	void	mon_free_entry(mon_entry *);
@@ -354,7 +345,7 @@ ntp_monitor(
 		 */
 		mon->leak -= interval;
 		mon->leak = max(0, mon->leak);
-		head = 1 << ntp_minpoll;
+		head = 1 << rstrct.ntp_minpoll;
 		leak = mon->leak + head;
 		limit = NTP_SHIFT * head;
 
@@ -375,7 +366,7 @@ ntp_monitor(
 		 * This rate-limits the KoDs to no less than the average
 		 * headway.
 		 */
-		if (interval + 1 >= ntp_minpkt && leak < limit) {
+		if (interval + 1 >= rstrct.ntp_minpkt && leak < limit) {
 			mon->leak = leak - 2;
 			restrict_mask &= ~(RES_LIMITED | RES_KOD);
 		} else if (mon->leak < limit)


=====================================
ntpd/ntp_proto.c
=====================================
@@ -1102,7 +1102,7 @@ poll_update(
 	 * reference clock, otherwise 2 s.
 	 */
 	utemp = current_time + (unsigned long)max(peer->throttle - (NTP_SHIFT - 1) *
-	    (1 << peer->cfg.minpoll), ntp_minpkt);
+	    (1 << peer->cfg.minpoll), rstrct.ntp_minpkt);
 	if (peer->burst > 0) {
 		if (peer->nextdate > current_time)
 			return;
@@ -1141,7 +1141,7 @@ poll_update(
 		else
 			peer->nextdate = utemp;
 		if (peer->throttle > (1 << peer->cfg.minpoll))
-			peer->nextdate += (unsigned long)ntp_minpkt;
+			peer->nextdate += (unsigned long)rstrct.ntp_minpkt;
 	}
 	DPRINT(2, ("poll_update: at %u %s poll %d burst %d retry %d head %d early %u next %u\n",
 		   current_time, socktoa(&peer->srcadr), peer->hpoll,
@@ -1200,7 +1200,7 @@ peer_clear(
 	if (initializing1) {
 		peer->nextdate += (unsigned long)peer_associations;
 	} else if (MODE_PASSIVE == peer->hmode) {
-		peer->nextdate += (unsigned long)ntp_minpkt;
+		peer->nextdate += (unsigned long)rstrct.ntp_minpkt;
 	} else {
 	    /*
 	     * Randomizing the next poll interval used to be done with
@@ -2157,7 +2157,7 @@ fast_xmit(
 		xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
 		    PKT_VERSION(rbufp->pkt.li_vn_mode), xmode);
 		xpkt.stratum = STRATUM_PKT_UNSPEC;
-		xpkt.ppoll = max(rbufp->pkt.ppoll, ntp_minpoll);
+		xpkt.ppoll = max(rbufp->pkt.ppoll, rstrct.ntp_minpoll);
 		xpkt.precision = rbufp->pkt.precision;
 		memcpy(&xpkt.refid, "RATE", REFIDLEN);
 		xpkt.rootdelay = htonl(rbufp->pkt.rootdelay);
@@ -2193,7 +2193,7 @@ fast_xmit(
 		xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_vars.sys_leap,
 		    PKT_VERSION(rbufp->pkt.li_vn_mode), xmode);
 		xpkt.stratum = STRATUM_TO_PKT(sys_vars.sys_stratum);
-		xpkt.ppoll = max(rbufp->pkt.ppoll, ntp_minpoll);
+		xpkt.ppoll = max(rbufp->pkt.ppoll, rstrct.ntp_minpoll);
 		xpkt.precision = sys_vars.sys_precision;
 		xpkt.refid = sys_vars.sys_refid;
 		xpkt.rootdelay = HTONS_FP(DTOUFP(sys_vars.sys_rootdelay));


=====================================
ntpd/ntp_restrict.c
=====================================
@@ -61,8 +61,17 @@
 /*
  * The restriction list
  */
-restrict_u *restrictlist4;
-restrict_u *restrictlist6;
+struct restriction_data rstrct = {
+  /*
+   * (MOVED FROM ntp_monitor.c)
+   * Parameters of the RES_LIMITED restriction option. We define headway
+   * as the idle time between packets. A packet is discarded if the
+   * headway is less than the minimum, as well as if the average headway
+   * is less than eight times the increment. 
+   */
+  .ntp_minpkt = NTP_MINPKT,   /* minimum (log 2 s) */
+  .ntp_minpoll = NTP_MINPOLL, /* increment (log 2 s) */
+};
 static int restrictcount;	/* count in the restrict lists */
 
 /*
@@ -145,8 +154,8 @@ init_restrict(void)
 	 * behavior as but reversed implementation compared to the docs.
 	 *
 	 */
-	LINK_SLIST(restrictlist4, &restrict_def4, link);
-	LINK_SLIST(restrictlist6, &restrict_def6, link);
+	LINK_SLIST(rstrct.restrictlist4, &restrict_def4, link);
+	LINK_SLIST(rstrct.restrictlist6, &restrict_def6, link);
 	restrictcount = 2;
 }
 
@@ -215,9 +224,9 @@ free_res(
 		dec_res_limited();
 
 	if (v6)
-		plisthead = &restrictlist6;
+		plisthead = &rstrct.restrictlist6;
 	else
-		plisthead = &restrictlist4;
+		plisthead = &rstrct.restrictlist4;
 	UNLINK_SLIST(unlinked, *plisthead, res, link, restrict_u);
 	INSIST(unlinked == res);
 
@@ -260,7 +269,7 @@ match_restrict4_addr(
 	restrict_u *	res;
 	restrict_u *	next;
 
-	for (res = restrictlist4; res != NULL; res = next) {
+	for (res = rstrct.restrictlist4; res != NULL; res = next) {
 		next = res->link;
 		if (res->expire &&
 		    res->expire <= current_time)
@@ -285,7 +294,7 @@ match_restrict6_addr(
 	restrict_u *	next;
 	struct in6_addr	masked;
 
-	for (res = restrictlist6; res != NULL; res = next) {
+	for (res = rstrct.restrictlist6; res != NULL; res = next) {
 		next = res->link;
 		INSIST(next != res);
 		if (res->expire &&
@@ -321,10 +330,10 @@ match_restrict_entry(
 	size_t cb;
 
 	if (v6) {
-		rlist = restrictlist6;
+		rlist = rstrct.restrictlist6;
 		cb = sizeof(pmatch->u.v6);
 	} else {
-		rlist = restrictlist4;
+		rlist = rstrct.restrictlist4;
 		cb = sizeof(pmatch->u.v4);
 	}
 
@@ -543,12 +552,12 @@ hack_restrict(
 				res = alloc_res6();
 				memcpy(res, &match,
 				       V6_SIZEOF_RESTRICT_U);
-				plisthead = &restrictlist6;
+				plisthead = &rstrct.restrictlist6;
 			} else {
 				res = alloc_res4();
 				memcpy(res, &match,
 				       V4_SIZEOF_RESTRICT_U);
-				plisthead = &restrictlist4;
+				plisthead = &rstrct.restrictlist4;
 			}
 			LINK_SORT_SLIST(
 				*plisthead, res,


=====================================
tests/ntpd/restrict.c
=====================================
@@ -36,7 +36,7 @@ TEST_TEAR_DOWN(hackrestrict) {
 	restrict_u *current;
 
 	do {
-		UNLINK_HEAD_SLIST(current, restrictlist4, link);
+		UNLINK_HEAD_SLIST(current, rstrct.restrictlist4, link);
 		if (current != NULL)
 		{
 			*current = *empty_restrict;
@@ -44,7 +44,7 @@ TEST_TEAR_DOWN(hackrestrict) {
 	} while (current != NULL);
 
 	do {
-		UNLINK_HEAD_SLIST(current, restrictlist6, link);
+		UNLINK_HEAD_SLIST(current, rstrct.restrictlist6, link);
 		if (current != NULL)
 		{
 			*current = *empty_restrict;
@@ -63,17 +63,17 @@ TEST(hackrestrict, RestrictionsAreEmptyAfterInit) {
 	memset(rl4, 0, sizeof(restrict_u));
 	memset(rl6, 0, sizeof(restrict_u));
 
-	TEST_ASSERT_EQUAL(rl4->hitcount, restrictlist4->hitcount);
-	TEST_ASSERT_EQUAL(rl4->flags, restrictlist4->flags);
-	TEST_ASSERT_EQUAL(rl4->mflags, restrictlist4->mflags);
-	TEST_ASSERT_EQUAL(rl4->expire, restrictlist4->expire);
-	TEST_ASSERT_EQUAL(rl4->u.v4.addr, restrictlist4->u.v4.addr);
-	TEST_ASSERT_EQUAL(rl4->u.v4.mask, restrictlist4->u.v4.mask);
-
-	TEST_ASSERT_EQUAL(rl6->hitcount, restrictlist6->hitcount);
-	TEST_ASSERT_EQUAL(rl6->flags, restrictlist6->flags);
-	TEST_ASSERT_EQUAL(rl6->mflags, restrictlist6->mflags);
-	TEST_ASSERT_EQUAL(rl6->expire, restrictlist6->expire);
+	TEST_ASSERT_EQUAL(rl4->hitcount, rstrct.restrictlist4->hitcount);
+	TEST_ASSERT_EQUAL(rl4->flags, rstrct.restrictlist4->flags);
+	TEST_ASSERT_EQUAL(rl4->mflags, rstrct.restrictlist4->mflags);
+	TEST_ASSERT_EQUAL(rl4->expire, rstrct.restrictlist4->expire);
+	TEST_ASSERT_EQUAL(rl4->u.v4.addr, rstrct.restrictlist4->u.v4.addr);
+	TEST_ASSERT_EQUAL(rl4->u.v4.mask, rstrct.restrictlist4->u.v4.mask);
+
+	TEST_ASSERT_EQUAL(rl6->hitcount, rstrct.restrictlist6->hitcount);
+	TEST_ASSERT_EQUAL(rl6->flags, rstrct.restrictlist6->flags);
+	TEST_ASSERT_EQUAL(rl6->mflags, rstrct.restrictlist6->mflags);
+	TEST_ASSERT_EQUAL(rl6->expire, rstrct.restrictlist6->expire);
 
 	free(rl4);
 	free(rl6);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/79341a8a834583de7676f2926926a6e9e0ba89b7

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/79341a8a834583de7676f2926926a6e9e0ba89b7
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190821/8500b28e/attachment-0001.htm>


More information about the vc mailing list