[Git][NTPsec/ntpsec][master] 2 commits: Isolate some protocol code from the receive buffer structure.

Eric S. Raymond gitlab at mg.gitlab.com
Mon Jan 28 18:14:00 UTC 2019


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


Commits:
1e29b0ed by Eric S. Raymond at 2019-01-28T17:46:25Z
Isolate some protocol code from the receive buffer structure.

- - - - -
fe0b6791 by Eric S. Raymond at 2019-01-28T18:12:31Z
All knowledge of the packet wire format is now confined to ntp_proto.c.

- - - - -


6 changed files:

- include/ntp.h
- include/nts.h
- include/recvbuff.h
- ntpd/ntp_io.c
- ntpd/ntp_proto.c
- ntpd/nts.c


Changes:

=====================================
include/ntp.h
=====================================
@@ -410,27 +410,6 @@ struct exten {
         uint8_t *body;
 };
 
-/* This is the old, insane way of representing packets. It'll gradually
-   be phased out and removed. Packets are simply pulled off the wire and
-   then type-punned into this structure, so all fields are in network
-   byte order. Note that there is no pack pragma. The only reason this
-   ever worked at all is that all the fields are self-aligned, so no ABI
-   has been evil enough to insert padding between fields. */
-struct pkt {
-	uint8_t	li_vn_mode;	/* peer leap indicator */
-	uint8_t	stratum;	/* peer stratum */
-	uint8_t	ppoll;		/* peer poll interval */
-	int8_t	precision;	/* peer clock precision */
-	u_fp	rootdelay;	/* roundtrip delay to primary source */
-	u_fp	rootdisp;	/* dispersion to primary source*/
-	char	refid[REFIDLEN];	/* reference id */
-	l_fp_w	reftime;	/* last update time */
-	l_fp_w	org;		/* originate time stamp */
-	l_fp_w	rec;		/* receive time stamp */
-	l_fp_w	xmt;		/* transmit time stamp */
-	uint32_t	exten[(MAX_MAC_LEN + MAX_EXT_LEN) / sizeof(uint32_t)];
-} __attribute__ ((aligned));
-
 /* pythonize-header: stop ignoring */
 
 /*


=====================================
include/nts.h
=====================================
@@ -10,7 +10,7 @@ int nts_client_ke_request(void);
 int nts_server_ke_verify(void);
 int nts_client_ke_verify(struct nts_client_t *);
 int nts_daily(void);
-int nts_validate(struct pkt *, struct nts_client_t *);
-int nts_decorate(struct pkt *, struct nts_client_t *);
+int nts_validate(struct parsed_pkt *, struct nts_client_t *);
+int nts_decorate(struct parsed_pkt *, struct nts_client_t *);
 
 #endif	/* NTS_H */


=====================================
include/recvbuff.h
=====================================
@@ -40,12 +40,7 @@ struct recvbuf {
 	SOCKET		fd;		/* fd on which it was received */
 	l_fp		recv_time;	/* time of arrival */
 	size_t		recv_length;	/* number of octets received */
-	union {
-		struct pkt	X_recv_pkt;
-		uint8_t		X_recv_buffer[RX_BUFF_SIZE];
-	} recv_space;
-#define	recv_pkt		recv_space.X_recv_pkt
-#define	recv_buffer		recv_space.X_recv_buffer
+	uint8_t		recv_buffer[RX_BUFF_SIZE];
 	struct parsed_pkt pkt;  /* host-order copy of data from wire */
 	int used;		/* reference count */
 	bool keyid_present;


=====================================
ntpd/ntp_io.c
=====================================
@@ -2165,8 +2165,8 @@ read_network_packet(
 
 	fromlen = sizeof(rb->recv_srcadr);
 
-	iovec.iov_base        = &rb->recv_space;
-	iovec.iov_len         = sizeof(rb->recv_space);
+	iovec.iov_base        = &rb->recv_buffer;
+	iovec.iov_len         = sizeof(rb->recv_buffer);
 	memset(&msghdr, '\0', sizeof(msghdr));
 	msghdr.msg_name       = &rb->recv_srcadr;
 	msghdr.msg_namelen    = fromlen;


=====================================
ntpd/ntp_proto.c
=====================================
@@ -19,6 +19,30 @@
 #endif
 #include <unistd.h>
 
+/* This is the old, insane way of representing packets,noqw only used here fot
+   parsing data off the wire. It'll gradually be phased out and removed. The 
+   publuc view of packets is struct parsed_pkt.
+
+   Packets are simply pulled off the wire and
+   then type-punned into this structure, so all fields are in network
+   byte order. Note that there is no pack pragma. The only reason this
+   ever worked at all is that all the fields are self-aligned, so no ABI
+   has been evil enough to insert padding between fields. */
+struct pkt {
+	uint8_t	li_vn_mode;	/* peer leap indicator */
+	uint8_t	stratum;	/* peer stratum */
+	uint8_t	ppoll;		/* peer poll interval */
+	int8_t	precision;	/* peer clock precision */
+	u_fp	rootdelay;	/* roundtrip delay to primary source */
+	u_fp	rootdisp;	/* dispersion to primary source*/
+	char	refid[REFIDLEN];	/* reference id */
+	l_fp_w	reftime;	/* last update time */
+	l_fp_w	org;		/* originate time stamp */
+	l_fp_w	rec;		/* receive time stamp */
+	l_fp_w	xmt;		/* transmit time stamp */
+	uint32_t	exten[(MAX_MAC_LEN + MAX_EXT_LEN) / sizeof(uint32_t)];
+} __attribute__ ((aligned));
+
 /*
  * Byte order conversion
  */
@@ -201,10 +225,10 @@ is_vn_mode_acceptable(
 	)
 {
 	return rbufp->recv_length >= 1 &&
-	    PKT_VERSION(rbufp->recv_space.X_recv_buffer[0]) >= 1 &&
-	    PKT_VERSION(rbufp->recv_space.X_recv_buffer[0]) <= 4 &&
-	    PKT_MODE(rbufp->recv_space.X_recv_buffer[0]) != MODE_PRIVATE &&
-	    PKT_MODE(rbufp->recv_space.X_recv_buffer[0]) != MODE_UNSPEC;
+	    PKT_VERSION(rbufp->recv_buffer[0]) >= 1 &&
+	    PKT_VERSION(rbufp->recv_buffer[0]) <= 4 &&
+	    PKT_MODE(rbufp->recv_buffer[0]) != MODE_PRIVATE &&
+	    PKT_MODE(rbufp->recv_buffer[0]) != MODE_UNSPEC;
 }
 
 static bool
@@ -213,8 +237,8 @@ is_control_packet(
 	)
 {
 	return rbufp->recv_length >= 1 &&
-	    PKT_VERSION(rbufp->recv_space.X_recv_buffer[0]) <= 4 &&
-	    PKT_MODE(rbufp->recv_space.X_recv_buffer[0]) == MODE_CONTROL;
+	    PKT_VERSION(rbufp->recv_buffer[0]) <= 4 &&
+	    PKT_MODE(rbufp->recv_buffer[0]) == MODE_CONTROL;
 }
 
 /* There used to be a calloc/free for each received packet.
@@ -224,16 +248,16 @@ is_control_packet(
 */
 static void
 free_extens(
-	struct recvbuf *rbufp
+	struct parsed_pkt *pkt
 	)
 {
-	if(rbufp->pkt.extensions != NULL) {
-		for(size_t i = 0; i < rbufp->pkt.num_extensions; i++) {
-			free(rbufp->pkt.extensions[i].body);
-			rbufp->pkt.extensions[i].body = NULL;
+	if(pkt->extensions != NULL) {
+		for(size_t i = 0; i < pkt->num_extensions; i++) {
+			free(pkt->extensions[i].body);
+			pkt->extensions[i].body = NULL;
 		}
-		free(rbufp->pkt.extensions);
-		rbufp->pkt.extensions = NULL;
+		free(pkt->extensions);
+		pkt->extensions = NULL;
 	}
 }
 
@@ -245,7 +269,7 @@ parse_packet(
 	REQUIRE(rbufp != NULL);
 
 	size_t recv_length = rbufp->recv_length;
-	uint8_t const* recv_buf = rbufp->recv_space.X_recv_buffer;
+	uint8_t const* recv_buf = rbufp->recv_buffer;
 
 	if(recv_length < LEN_PKT_NOMAC) {
 		/* Data is too short to possibly be a valid packet. */
@@ -392,7 +416,7 @@ parse_packet(
 
 	return true;
   fail:
-	free_extens(rbufp);
+	free_extens(&rbufp->pkt);
 	return false;
 }
 
@@ -432,11 +456,11 @@ is_crypto_nak(
 }
 
 static bool is_kod(
-	struct recvbuf const* rbufp
+	struct parsed_pkt *pkt
 	)
 {
-	return PKT_LEAP(rbufp->pkt.li_vn_mode) == LEAP_NOTINSYNC &&
-	    PKT_TO_STRATUM(rbufp->pkt.stratum) == STRATUM_UNSPEC;
+	return PKT_LEAP(pkt->li_vn_mode) == LEAP_NOTINSYNC &&
+	    PKT_TO_STRATUM(pkt->stratum) == STRATUM_UNSPEC;
 }
 
 /* Check the restrictions which can be checked just based on the source
@@ -455,7 +479,7 @@ static bool check_early_restrictions(
 	    rbufp->recv_length < 1 ||
 	    ((restrict_mask & RES_VERSION) &&
 	     (rbufp->recv_length < 1 ||
-	      PKT_VERSION(rbufp->recv_space.X_recv_buffer[0]) != NTP_VERSION));
+	      PKT_VERSION(rbufp->recv_buffer[0]) != NTP_VERSION));
 }
 
 static void
@@ -521,7 +545,7 @@ handle_procpkt(
 	peer->outcount = 0;
 	outcount--;
 
-	if(is_kod(rbufp)) {
+	if(is_kod(&rbufp->pkt)) {
 		if(!memcmp(rbufp->pkt.refid, "RATE", REFIDLEN)) {
 			peer->selbroken++;
 			report_event(PEVNT_RATE, peer, NULL);
@@ -739,7 +763,7 @@ receive(
 			   have to do this screwy buffer-length
 			   arithmetic in order to call it. */
 			!authdecrypt(auth,
-				 (uint32_t*)rbufp->recv_space.X_recv_buffer,
+				 (uint32_t*)rbufp->recv_buffer,
 				 (int)(rbufp->recv_length - (rbufp->mac_len + 4)),
 				 (int)(rbufp->mac_len + 4))) {
 
@@ -780,7 +804,7 @@ receive(
 	}
 
   done:
-	free_extens(rbufp);
+	free_extens(&rbufp->pkt);
 }
 
 /*


=====================================
ntpd/nts.c
=====================================
@@ -93,7 +93,7 @@ int nts_daily(void)
  * side, the nts_client pointer is expected to be NULL as there is no
  * per-client server state.
  */
-int nts_validate(struct pkt *pkt, struct nts_client_t *nts_client)
+int nts_validate(struct parsed_pkt *pkt, struct nts_client_t *nts_client)
 {
 	UNUSED_ARG(pkt);
 	UNUSED_ARG(nts_client);
@@ -106,7 +106,7 @@ int nts_validate(struct pkt *pkt, struct nts_client_t *nts_client)
  * the nts_client pointer is expected to be NULL as there is no
  * per-client server state.
  */
-int nts_decorate(struct pkt *pkt, struct nts_client_t *nts_client)
+int nts_decorate(struct parsed_pkt *pkt, struct nts_client_t *nts_client)
 {
 	UNUSED_ARG(pkt);
 	UNUSED_ARG(nts_client);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/ee7677d0cff27c9e208cc3716db41f51bf29c1fb...fe0b6791ace8c2d74dea7ad8b62a763463cb9256

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/ee7677d0cff27c9e208cc3716db41f51bf29c1fb...fe0b6791ace8c2d74dea7ad8b62a763463cb9256
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/20190128/ac1429c9/attachment-0001.html>


More information about the vc mailing list