[Git][NTPsec/ntpsec][master] Simplify passing configuration data to the peer-creation logic.

Eric S. Raymond gitlab at mg.gitlab.com
Sun Aug 27 19:32:41 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
0d7df5ab by Eric S. Raymond at 2017-08-27T15:30:10-04:00
Simplify passing configuration data to the peer-creation logic.

Before, everything passed tp newpeer was explicit arguments to it.
Now we pass in the struct peerctl control block created by config.
Where newpeer() is called by the protocol machine we assemble such a
block on the fly.

This change has two advantages:

(1) if we need to create new config items, adding them to the peer ctl
structure suffices to get them all the way into the newpeer()
logic. It's no longer required that we futz with the newpeer() calls
on the way down - though the passed-in values might need validation
or massaging un newpeer() itself.

(2) The protocol-machine calls are actually more readable this way,
because the code setting up the control blocks makes explicit semantic
information that was previously only implied by the order of arguments.

No logic changes in this commit.

- - - - -


6 changed files:

- include/ntp.h
- include/ntp_config.h
- include/ntpd.h
- ntpd/ntp_config.c
- ntpd/ntp_peer.c
- ntpd/ntp_proto.c


Changes:

=====================================
include/ntp.h
=====================================
--- a/include/ntp.h
+++ b/include/ntp.h
@@ -155,6 +155,24 @@ typedef struct __endpt {
 #define INT_BCASTXMIT	0x400   /* socket setup to allow broadcasts */
 
 /*
+ * Read-only control knobs for a peer structure.
+ * Packaging these makes context copies a bit more succinct.
+ */
+struct peer_ctl {
+	uint8_t		version;
+	int		flags;
+	uint8_t		minpoll;
+	uint8_t		maxpoll;
+	uint32_t	ttl;
+	keyid_t		peerkey;
+#ifdef REFCLOCK
+	uint32_t	baud;
+	char		*path;
+	char		*ppspath;
+#endif /* REFCLOCK */
+};
+
+/*
  * Define flasher bits (tests 1 through 11 in packet procedure)
  * These reveal the state at the last grumble from the peer and are
  * most handy for diagnosing problems, even if not strictly a state
@@ -198,7 +216,7 @@ typedef struct __endpt {
  *
  * The ttl field is overloaded; it's used in the refclock case to pass
  * in a mode byte that may contain a baud rate or subtype. Splitting
- * this fields would complicate some call sequences that are already
+ * this field would complicate some call sequences that are already
  * unpleasantly intricate.
  */
 struct peer {


=====================================
include/ntp_config.h
=====================================
--- a/include/ntp_config.h
+++ b/include/ntp_config.h
@@ -94,22 +94,6 @@ struct restrict_node_tag {
 
 typedef DECL_FIFO_ANCHOR(restrict_node) restrict_fifo;
 
-/*
- * Read-only control knobs for a peer structure.
- * Packaging these makes context copies a bit more succinct.
- */
-struct peer_ctl {
-	uint8_t		version;
-	int		flags;
-	uint8_t		minpoll;
-	uint8_t		maxpoll;
-	uint32_t	ttl;
-	keyid_t		peerkey;
-	uint32_t	baud;
-	char		*path;
-	char		*ppspath;
-};
-
 typedef struct peer_node_tag peer_node;
 struct peer_node_tag {
 	peer_node *	link;


=====================================
include/ntpd.h
=====================================
--- a/include/ntpd.h
+++ b/include/ntpd.h
@@ -131,9 +131,8 @@ extern	struct peer *findpeer	(struct recvbuf *, int, int *);
 extern	struct peer *findpeerbyassoc(associd_t);
 extern  void	set_peerdstadr	(struct peer *, endpt *);
 extern	struct peer *newpeer	(sockaddr_u *, const char *,
-				 endpt *, uint8_t, uint8_t,
-				 uint8_t, uint8_t, unsigned int, uint8_t, uint32_t,
-				 keyid_t, const bool);
+				 endpt *, uint8_t, struct peer_ctl *,
+				 uint8_t, const bool);
 extern	void	peer_update_hash (struct peer *);
 extern	void	peer_all_reset	(void);
 extern	void	peer_clr_stats	(void);


=====================================
ntpd/ntp_config.c
=====================================
--- a/ntpd/ntp_config.c
+++ b/ntpd/ntp_config.c
@@ -2628,9 +2628,8 @@ peer_config(
 	ctl->flags |= FLAG_CONFIG;
 	if (mode_ntpdate)
 		ctl->flags |= FLAG_IBURST;
-	return newpeer(srcadr, hostname, dstadr, hmode, ctl->version,
-		       ctl->minpoll, ctl->maxpoll, (unsigned int)ctl->flags,
-		       cast_flags, ctl->ttl, ctl->peerkey, true);
+	return newpeer(srcadr, hostname, dstadr, hmode,
+		       ctl, cast_flags, true);
 }
 
 


=====================================
ntpd/ntp_peer.c
=====================================
--- a/ntpd/ntp_peer.c
+++ b/ntpd/ntp_peer.c
@@ -627,13 +627,8 @@ newpeer(
 	const char *	hostname,
 	endpt *		dstadr,
 	uint8_t		hmode,
-	uint8_t		version,
-	uint8_t		minpoll,
-	uint8_t		maxpoll,
-	unsigned int	flags,
+	struct peer_ctl *ctl,
 	uint8_t		cast_flags,
-	uint32_t	ttl,
-	keyid_t		key,
 	bool		initializing1
 	)
 {
@@ -690,7 +685,7 @@ newpeer(
 	UNLINK_HEAD_SLIST(peer, peer_free, p_link);
 	peer_free_count--;
 	peer_associations++;
-	if (FLAG_PREEMPT & flags)
+	if (FLAG_PREEMPT & ctl->flags)
 		peer_preempt++;
 
 	/*
@@ -704,22 +699,22 @@ newpeer(
 	if (hostname != NULL)
 		peer->hostname = estrdup(hostname);
 	peer->hmode = hmode;
-	peer->version = version;
-	peer->flags = flags;
+	peer->version = ctl->version;
+	peer->flags = ctl->flags;
 	peer->cast_flags = cast_flags;
 	set_peerdstadr(peer, 
 		       select_peerinterface(peer, srcadr, dstadr));
 
-        if (NTP_MAXPOLL_UNK == maxpoll)
+        if (NTP_MAXPOLL_UNK == ctl->maxpoll)
 	    /* not set yet, set to default */
-	    maxpoll = NTP_MAXDPOLL;
+	    ctl->maxpoll = NTP_MAXDPOLL;
 	/*
          * minpoll is clamped not greater than NTP_MAXPOLL
          * maxpoll is clamped not less than NTP_MINPOLL
          * minpoll is clamped not greater than maxpoll.
 	 */
-	peer->minpoll = min(minpoll, NTP_MAXPOLL);
-	peer->maxpoll = max(maxpoll, NTP_MINPOLL);
+	peer->minpoll = min(ctl->minpoll, NTP_MAXPOLL);
+	peer->maxpoll = max(ctl->maxpoll, NTP_MINPOLL);
 	if (peer->minpoll > peer->maxpoll)
 		peer->minpoll = peer->maxpoll;
 
@@ -737,8 +732,8 @@ newpeer(
 	if ((MDF_BCAST & cast_flags) && peer->dstadr != NULL)
 		enable_broadcast(peer->dstadr, srcadr);
 
-	peer->ttl = ttl;
-	peer->keyid = key;
+	peer->ttl = ctl->ttl;
+	peer->keyid = ctl->peerkey;
 	peer->precision = sys_precision;
 	peer->hpoll = peer->minpoll;
 	if (cast_flags & MDF_POOL)
@@ -760,7 +755,7 @@ newpeer(
 	/*
 	 * Put the new peer in the hash tables.
 	 */
-	if ((MDF_UCAST & cast_flags) && !(FLAG_DNS & flags))
+	if ((MDF_UCAST & cast_flags) && !(FLAG_DNS & ctl->flags))
 		peer_update_hash(peer);
 	hash = peer->associd & NTP_HASH_MASK;
 	LINK_SLIST(assoc_hash[hash], peer, aid_link);


=====================================
ntpd/ntp_proto.c
=====================================
--- a/ntpd/ntp_proto.c
+++ b/ntpd/ntp_proto.c
@@ -720,6 +720,7 @@ handle_manycast(
 	bool request_already_authenticated
 	)
 {
+	struct peer_ctl mctl;
 	(void)request_already_authenticated;
 	(void)restrict_mask;
 
@@ -741,11 +742,15 @@ handle_manycast(
 		return;
 	}
 
+	memset(&mctl, '\0', sizeof(struct peer_ctl));
+	mctl.version = PKT_VERSION(pkt->li_vn_mode);
+	mctl.flags = FLAG_PREEMPT | (FLAG_IBURST & mpeer->flags);
+	mctl.minpoll = mpeer->minpoll;
+	mctl.maxpoll = mpeer->maxpoll;
+	mctl.ttl = 0;
+	mctl.peerkey = mpeer->keyid;
 	newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
-		MODE_CLIENT, PKT_VERSION(pkt->li_vn_mode),
-		mpeer->minpoll, mpeer->maxpoll,
-		FLAG_PREEMPT | (FLAG_IBURST & mpeer->flags),
-		MDF_UCAST | MDF_UCLNT, 0, mpeer->keyid, false);
+		MODE_CLIENT, &mctl, MDF_UCAST | MDF_UCLNT, false);
 }
 	
 void
@@ -2460,6 +2465,7 @@ dns_take_pool(
 	sockaddr_u *		rmtadr
 	)
 {
+	struct peer_ctl		pctl;
 	struct peer *		peer;
 	int			restrict_mask;
 	endpt *			lcladr;
@@ -2474,11 +2480,15 @@ dns_take_pool(
 	msyslog(LOG_INFO, "PROTO: Pool taking: %s", socktoa(rmtadr));
 
 	lcladr = findinterface(rmtadr);
+	memset(&pctl, '\0', sizeof(struct peer_ctl));
+	pctl.version = pool->version;
+	pctl.minpoll = pool->minpoll;
+	pctl.maxpoll = pool->maxpoll;
+	pctl.flags = FLAG_PREEMPT | (FLAG_IBURST & pool->flags);
+	pctl.ttl = 0;
+	pctl.peerkey = 0;
 	peer = newpeer(rmtadr, NULL, lcladr,
-		MODE_CLIENT, pool->version,
-		pool->minpoll, pool->maxpoll,
-		FLAG_PREEMPT | (FLAG_IBURST & pool->flags),
-		MDF_UCAST | MDF_UCLNT, 0, 0, false);
+		       MODE_CLIENT, &pctl, MDF_UCAST | MDF_UCLNT, false);
 	peer_xmit(peer);
 	if (peer->flags & FLAG_IBURST)
 	  peer->retry = NTP_RETRY;



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/0d7df5ab3610f4df06c0713031ad2d5698794786

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/0d7df5ab3610f4df06c0713031ad2d5698794786
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/20170827/b3e5d1a9/attachment.html>


More information about the vc mailing list