[Git][NTPsec/ntpsec][master] 3 commits: Replay refactoring, and an error message improvement.

Eric S. Raymond gitlab at mg.gitlab.com
Thu Dec 17 14:49:04 UTC 2015


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


Commits:
0bfe0309 by Eric S. Raymond at 2015-12-16T05:58:37Z
Replay refactoring, and an error message improvement.

- - - - -
646531c5 by Eric S. Raymond at 2015-12-16T05:59:27Z
Suppress some unneeded captures. Explanation in the diff.

- - - - -
8b0f5a7a by Eric S. Raymond at 2015-12-17T09:48:41Z
Avoid a core dump in the interface layers due to signedness problem.

Fixes a bug reported by Hal Murray: ntpd -p was core-dumping ntpd.

- - - - -


3 changed files:

- ntpd/ntp_intercept.c
- ntpd/ntp_io.c
- ntpd/ntp_proto.c


Changes:

=====================================
ntpd/ntp_intercept.c
=====================================
--- a/ntpd/ntp_intercept.c
+++ b/ntpd/ntp_intercept.c
@@ -636,12 +636,49 @@ static void packet_dump(char *buf, size_t buflen,
 	strlcat(buf, "nomac", buflen);
     else
 	/* dump MAC as len - LEN_PKT_NOMAC chars in hex */
-	for (i = 0; i < len - LEN_PKT_NOMAC; i++) {
+	for (i = 0; i + LEN_PKT_NOMAC < len; i++) {
 	    snprintf(buf + strlen(buf), buflen - strlen(buf),
 		     "%02x", pkt->exten[i]);
 	}
 }
 
+static void packet_parse(char *pktbuf, char *macbuf, struct pkt *pkt)
+{
+    char refbuf[32], orgbuf[32], recbuf[32], xmtbuf[32];
+    int li_vn_mode = 0, stratum = 0, ppoll = 0, precision = 0;
+
+    if (sscanf(pktbuf, "%d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s",
+	       &li_vn_mode, &stratum,
+	       &ppoll, &precision,
+	       &pkt->rootdelay, &pkt->rootdisp,
+	       &pkt->refid,
+	       refbuf, orgbuf, recbuf, xmtbuf) != 7)
+    {
+	fprintf(stderr, "ntpd: malformed packet dump at line %d\n",
+		lineno);
+	exit(1);
+    }
+    /* extra transfers required because the struct members are int8_t */
+    pkt->li_vn_mode = li_vn_mode;
+    pkt->stratum = stratum;
+    pkt->ppoll = ppoll;
+    pkt->precision = precision;
+    atolfp(refbuf, &pkt->reftime);
+    atolfp(orgbuf, &pkt->org);
+    atolfp(recbuf, &pkt->rec);
+    atolfp(xmtbuf, &pkt->xmt);
+
+    memset(pkt->exten, '\0', sizeof(pkt->exten));
+    if (strcmp(macbuf, "nomac") != 0) {
+	size_t i;
+	for (i = 0; i < strlen(macbuf)/2; i++) {
+	    int hexval;
+	    sscanf(macbuf + 2*i, "%02x", &hexval);
+	    pkt->exten[i] = hexval & 0xff;
+	}
+    }
+}
+
 static void recvbuf_dump(char *buf, size_t buflen, struct recvbuf *rbufp)
 {
     char pkt_dump[BUFSIZ];
@@ -704,13 +741,30 @@ void intercept_replay(void)
 	get_operation(NULL);
 	if (strncmp(linebuf, "finish", 6) == 0)
 	    break;
+#if 0
+	else if (strncmp(linebuf, "sendpkt ", 9) == 0)
+	{
+	    struct pkt pkt;
+	    char legend[BUFSIZ], recvbuf[BUFSIZ], destbuf[BUFSIZ];
+	    char pktbuf[BUFSIZ], macbuf[BUFSIZ];
+
+	    if (sscanf(linebuf, "sendpkt %x %s %s %s %s",
+		       legend, recvbuf, destbuf, pktbuf, macbuf) != 5)
+	    {
+		fprintf(stderr, "ntpd: bad receive format at line %d\n", lineno);
+		exit(1);
+	    }
+
+	    packet_parse(recvbuf, macbuf, &pkt);
+
+
+	}
+#endif
 	else if (strncmp(linebuf, "receive ", 8) == 0)
 	{
 	    struct recvbuf rbuf;
 	    struct pkt *pkt;
-	    int li_vn_mode = 0, stratum = 0, ppoll = 0, precision = 0;
 	    char recvbuf[BUFSIZ], srcbuf[BUFSIZ], pktbuf[BUFSIZ], macbuf[BUFSIZ];
-	    char refbuf[32], orgbuf[32], recbuf[32], xmtbuf[32];
 
 	    if (sscanf(linebuf, "receive %x %s %s %s %s",
 		       &rbuf.cast_flags, recvbuf, srcbuf, pktbuf, macbuf) != 5)
@@ -726,36 +780,7 @@ void intercept_replay(void)
 	    }
 
 	    pkt = &rbuf.recv_pkt;
-	    if (sscanf(pktbuf, "%d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s",
-		       &li_vn_mode, &stratum,
-		       &ppoll, &precision,
-		       &pkt->rootdelay, &pkt->rootdisp,
-		       &pkt->refid,
-		       refbuf, orgbuf, recbuf, xmtbuf) != 7)
-	    {
-		fprintf(stderr, "ntpd: malformed packet dump at line %d\n",
-			lineno);
-		exit(1);
-	    }
-	    /* extra transfers required because the struct members are int8_t */
-	    pkt->li_vn_mode = li_vn_mode;
-	    pkt->stratum = stratum;
-	    pkt->ppoll = ppoll;
-	    pkt->precision = precision;
-	    atolfp(refbuf, &pkt->reftime);
-	    atolfp(orgbuf, &pkt->org);
-	    atolfp(recbuf, &pkt->rec);
-	    atolfp(xmtbuf, &pkt->xmt);
-
-	    memset(pkt->exten, '\0', sizeof(pkt->exten));
-	    if (strcmp(macbuf, "nomac") != 0) {
-		size_t i;
-		for (i = 0; i < strlen(macbuf)/2; i++) {
-		    int hexval;
-		    sscanf(macbuf + 2*i, "%02x", &hexval);
-		    pkt->exten[i] = hexval & 0xff;
-		}
-	    }
+	    packet_parse(recvbuf, macbuf, pkt);
 
 	    /*
 	     * If the packet doesn't dump identically to how it came in,
@@ -775,7 +800,13 @@ void intercept_replay(void)
 	}
 	else
 	{
-	    fprintf(stderr, "ntpd: unexpected operation at line %d\n", lineno);
+	    char errbuf[BUFSIZ], *cp;
+	    strlcpy(errbuf, linebuf, sizeof(errbuf));
+	    cp = strchr(errbuf, ' ');
+	    if (cp != NULL)
+		*cp = '\0';
+	    fprintf(stderr, "ntpd: unexpected operation '%s' at line %d\n",
+		    errbuf, lineno);
 	    exit(1);
 	}
     }


=====================================
ntpd/ntp_io.c
=====================================
--- a/ntpd/ntp_io.c
+++ b/ntpd/ntp_io.c
@@ -3583,7 +3583,12 @@ io_handler(void)
 	if (nfound > 0) {
 		l_fp ts;
 
-		intercept_get_systime(__func__, &ts);
+		/*
+		 * Doesn't need to be intercepted, because the time
+		 * algorithms don't use it.  It's strictly internal
+		 * to the I/O handling.
+		 */
+		get_systime(&ts);
 
 		input_handler(&ts);
 	} else if (nfound == -1 && errno != EINTR) {


=====================================
ntpd/ntp_proto.c
=====================================
--- a/ntpd/ntp_proto.c
+++ b/ntpd/ntp_proto.c
@@ -3103,9 +3103,11 @@ peer_xmit(
 #endif	/* !ENABLE_AUTOKEY */
 
 		/*
-		 * Transmit a-priori timestamps
+		 * Transmit a-priori timestamps.  We don't need to log these
+		 * for replay because the timestamp gets stuffed in the packet
+		 * which is what's visible in the replay log.
 		 */
-		intercept_get_systime(__func__, &xmt_tx);
+		get_systime(&xmt_tx);
 		if (peer->flip == 0) {	/* basic mode */
 			peer->aorg = xmt_tx;
 			HTONL_FP(&xmt_tx, &xpkt.xmt);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/d363c6050c73a86405b17d68e55299710ee9c7d3...8b0f5a7a5348a21197dc074f754c151f17f54943
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20151217/1e45c46b/attachment.html>


More information about the vc mailing list