[Git][NTPsec/ntpsec][master] TESTFRAME: Factor out pacjet visualization so it can bwe unit tested.

Eric S. Raymond gitlab at mg.gitlab.com
Thu Sep 15 17:17:49 UTC 2016


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


Commits:
6bd4b034 by Eric S. Raymond at 2016-09-15T13:16:48-04:00
TESTFRAME: Factor out pacjet visualization so it can bwe unit tested.

- - - - -


4 changed files:

- include/ntpd.h
- + libntp/pktvis.c
- libntp/wscript
- ntpd/ntp_intercept.c


Changes:

=====================================
include/ntpd.h
=====================================
--- a/include/ntpd.h
+++ b/include/ntpd.h
@@ -239,6 +239,10 @@ extern	void	record_timing_stats (const char *);
 #endif
 extern	char *	fstostr(time_t);	/* NTP timescale seconds */
 
+/* ntpvis.c */
+void packet_dump(char *, size_t, sockaddr_u *, struct pkt *, size_t);
+size_t packet_undump(char *, int len, char *);
+
 /*
  * Signals we catch for debugging.
  */


=====================================
libntp/pktvis.c
=====================================
--- /dev/null
+++ b/libntp/pktvis.c
@@ -0,0 +1,123 @@
+/*
+ * pktvis.c - display format for NTP packets, and inverting it.
+ */
+#include <config.h>
+
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "ntpd.h"
+#include "ntp_assert.h"
+#include "ntp_fp.h"
+#include "lib_strbuf.h"
+
+#define BIGEND_BYTESHIFT(i, m)	(8 * ((m) - (i % (m))))
+#define BIGEND_GETBYTE(u32, i)	(((u32) >> BIGEND_BYTESHIFT(i, 4)) & 0xff)
+#define BIGEND_PUTBYTE(b, i)	(((b) & 0xff) << BIGEND_BYTESHIFT(i, 4))
+
+static char *lfpdump(l_fp *fp)
+{
+    char *buf;
+    uint64_t	np;
+
+    LIB_GETBUF(buf);
+
+    np = fp->l_ui;
+    np <<= FRACTION_PREC;
+    np |= fp->l_uf;
+
+    snprintf(buf, LIB_BUFLENGTH, "%" PRIu64, np);
+
+    return buf;
+}
+
+void packet_dump(char *buf, size_t buflen,
+			sockaddr_u *dest, struct pkt *pkt, size_t len)
+{
+    size_t i;
+    /*
+     * Format is three tokens: source address, packet, MAC token. 
+     *
+     * FIXME: struct pkt fields are in network byte order. Need to
+     * add htonl()/ntohl() calls here.
+     */
+    snprintf(buf, buflen, "%s %d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s ",
+	   socktoa(dest),
+	   pkt->li_vn_mode, pkt->stratum, pkt->ppoll, pkt->precision,
+	   pkt->rootdelay, pkt->rootdisp,
+	   pkt->refid,
+	   lfpdump(&pkt->reftime), lfpdump(&pkt->org),
+	   lfpdump(&pkt->rec), lfpdump(&pkt->xmt));
+
+    if (len == LEN_PKT_NOMAC)
+	strlcat(buf, "nomac", buflen);
+    else
+	/* dump MAC as len - LEN_PKT_NOMAC chars in hex */
+	for (i = 0; i + LEN_PKT_NOMAC < len; i++) {
+	    snprintf(buf + strlen(buf), buflen - strlen(buf),
+		     "%02x", BIGEND_GETBYTE(pkt->exten[i / sizeof(uint32_t)], i));
+	}
+}
+
+static void lfpload(char *str, l_fp *fp)
+{
+    uint64_t	np;
+
+    INSIST(sscanf(str, "%" PRIu64, &np) == 1);
+    
+    (fp)->l_uf = (np) & 0xFFFFFFFF;
+    (fp)->l_ui = (((np) >> FRACTION_PREC) & 0xFFFFFFFF);
+}
+
+static int packet_parse(char *pktbuf, struct pkt *pkt)
+{
+    char refbuf[32], orgbuf[32], recbuf[32], xmtbuf[32], macbuf[BUFSIZ];
+    int li_vn_mode = 0, stratum = 0, ppoll = 0, precision = 0;
+    size_t pktlen;
+
+    if (sscanf(pktbuf, "%d:%d:%d:%d:%u:%u:%u:%[^:]:%[^:]:%[^:]:%[^:] %s",
+		     &li_vn_mode, &stratum,
+		     &ppoll, &precision,
+		     &pkt->rootdelay, &pkt->rootdisp,
+		     &pkt->refid,
+	       refbuf, orgbuf, recbuf, xmtbuf, macbuf) != 11)
+	return -1;
+
+    /* extra transfers required because the struct members are int8_t */
+    pkt->li_vn_mode = (uint8_t)li_vn_mode;
+    pkt->stratum = (uint8_t)stratum;
+    pkt->ppoll = (uint8_t)ppoll;
+    pkt->precision = (int8_t)precision;
+    lfpload(refbuf, &pkt->reftime);
+    lfpload(orgbuf, &pkt->org);
+    lfpload(recbuf, &pkt->rec);
+    lfpload(xmtbuf, &pkt->xmt);
+
+    pktlen = LEN_PKT_NOMAC;
+    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;
+	    if (sscanf(macbuf + 2*i, "%02x", &hexval) != 1) {
+		return -1;
+	    }
+	    pkt->exten[i / sizeof(uint32_t)] |= BIGEND_PUTBYTE(hexval, i);
+	    ++pktlen;
+	}
+    }
+    return pktlen;
+}
+
+size_t packet_undump(char *bin, int len, char *pktbuf)
+{
+    struct pkt pkt;
+    int pktlen = packet_parse(pktbuf, &pkt);
+    INSIST((pktlen != -1) && len >= pktlen);
+    /* works because pkt fields and extension are in network byte order */
+    memcpy(bin, (char *)&pkt, sizeof(pkt));
+    memcpy(bin + sizeof(pkt), (char *)&pkt.exten, pktlen -  sizeof(pkt));
+    return pktlen;
+}
+
+/* end */


=====================================
libntp/wscript
=====================================
--- a/libntp/wscript
+++ b/libntp/wscript
@@ -29,6 +29,7 @@ def build(ctx):
 		"ntp_worker.c",
 		"numtoa.c",
 		"numtohost.c",
+		"pktvis.c",
 		"prettydate.c",
 		"recvbuff.c",
 		"refidsmear.c",


=====================================
ntpd/ntp_intercept.c
=====================================
--- a/ntpd/ntp_intercept.c
+++ b/ntpd/ntp_intercept.c
@@ -646,118 +646,6 @@ intercept_leapsec_load_file(
     return loaded;
 }
 
-static char *lfpdump(l_fp *fp)
-{
-    char *buf;
-    uint64_t	np;
-
-    LIB_GETBUF(buf);
-
-    np = fp->l_ui;
-    np <<= FRACTION_PREC;
-    np |= fp->l_uf;
-
-    snprintf(buf, LIB_BUFLENGTH, "%" PRIu64, np);
-
-    return buf;
-}
-
-#define BIGEND_BYTESHIFT(i, m)	(8 * ((m) - (i % (m))))
-#define BIGEND_GETBYTE(u32, i)	(((u32) >> BIGEND_BYTESHIFT(i, 4)) & 0xff)
-#define BIGEND_PUTBYTE(b, i)	(((b) & 0xff) << BIGEND_BYTESHIFT(i, 4))
-
-static void packet_dump(char *buf, size_t buflen,
-			sockaddr_u *dest, struct pkt *pkt, size_t len)
-{
-    size_t i;
-    /*
-     * Format is three tokens: source address, packet, MAC token. 
-     *
-     * FIXME: struct pkt fields are in network byte order. Need to
-     * add htonl()/ntohl() calls here.
-     */
-    snprintf(buf, buflen, "%s %d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s ",
-	   socktoa(dest),
-	   pkt->li_vn_mode, pkt->stratum, pkt->ppoll, pkt->precision,
-	   pkt->rootdelay, pkt->rootdisp,
-	   pkt->refid,
-	   lfpdump(&pkt->reftime), lfpdump(&pkt->org),
-	   lfpdump(&pkt->rec), lfpdump(&pkt->xmt));
-
-    if (len == LEN_PKT_NOMAC)
-	strlcat(buf, "nomac", buflen);
-    else
-	/* dump MAC as len - LEN_PKT_NOMAC chars in hex */
-	for (i = 0; i + LEN_PKT_NOMAC < len; i++) {
-	    snprintf(buf + strlen(buf), buflen - strlen(buf),
-		     "%02x", BIGEND_GETBYTE(pkt->exten[i / sizeof(uint32_t)], i));
-	}
-}
-
-static void lfpload(char *str, l_fp *fp)
-{
-    uint64_t	np;
-
-    if (sscanf(str, "%" PRIu64, &np) != 1)
-	replay_fail("bad fp format\n");
-
-    
-    (fp)->l_uf = (np) & 0xFFFFFFFF;					\
-    (fp)->l_ui = (((np) >> FRACTION_PREC) & 0xFFFFFFFF);		\
-}
-
-static size_t packet_parse(char *pktbuf, struct pkt *pkt)
-{
-    char refbuf[32], orgbuf[32], recbuf[32], xmtbuf[32], macbuf[BUFSIZ];
-    int li_vn_mode = 0, stratum = 0, ppoll = 0, precision = 0;
-    size_t pktlen;
-
-    if (sscanf(pktbuf, "%d:%d:%d:%d:%u:%u:%u:%[^:]:%[^:]:%[^:]:%[^:] %s",
-		     &li_vn_mode, &stratum,
-		     &ppoll, &precision,
-		     &pkt->rootdelay, &pkt->rootdisp,
-		     &pkt->refid,
-	       refbuf, orgbuf, recbuf, xmtbuf, macbuf) != 11)
-	replay_fail("garbled event format\n");
-
-    /* extra transfers required because the struct members are int8_t */
-    pkt->li_vn_mode = (uint8_t)li_vn_mode;
-    pkt->stratum = (uint8_t)stratum;
-    pkt->ppoll = (uint8_t)ppoll;
-    pkt->precision = (int8_t)precision;
-    lfpload(refbuf, &pkt->reftime);
-    lfpload(orgbuf, &pkt->org);
-    lfpload(recbuf, &pkt->rec);
-    lfpload(xmtbuf, &pkt->xmt);
-
-    pktlen = LEN_PKT_NOMAC;
-    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;
-	    if (sscanf(macbuf + 2*i, "%02x", &hexval) != 1) {
-		fprintf(stderr, "ntpd: bad hexval format at line %d\n", lineno);
-		exit(1);
-	    }
-	    pkt->exten[i / sizeof(uint32_t)] |= BIGEND_PUTBYTE(hexval, i);
-	    ++pktlen;
-	}
-    }
-    return pktlen;
-}
-
-static size_t packet_binpack(char *bin, int len, char *pktbuf)
-{
-    struct pkt pkt;
-    size_t pktlen = packet_parse(pktbuf, &pkt);
-    INSIST((size_t)len >= pktlen);
-    /* works because pkt fields and extension are in network byte order */
-    memcpy(bin, (char *)&pkt, sizeof(pkt));
-    memcpy(bin + sizeof(pkt), (char *)&pkt.exten, pktlen -  sizeof(pkt));
-    return pktlen;
-}
-
 void intercept_sendpkt(const char *legend,
 		  sockaddr_u *dest, struct interface *ep, int ttl,
 		  void *pkt, int len)
@@ -869,7 +757,7 @@ ssize_t intercept_recvfrom(int sockfd, void *buf, size_t len, int flags,
 	a = socktoa((sockaddr_u *)src_addr);
 	if (strcmp(a, raddr) != 0)
 	    replay_fail("expected address %s but saw %s\n", raddr, a);
-	recvlen = (int)packet_binpack(buf, len, pkt_dump);
+	recvlen = (int)packet_undump(buf, len, pkt_dump);
     } else {
 	recvlen = recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
 
@@ -912,7 +800,7 @@ ssize_t intercept_recvmsg(int sockfd, struct msghdr *msg, int flags)
 	a = socktoa((sockaddr_u *)msg->msg_name);
 	if (strcmp(a, raddr) != 0)
 	    replay_fail("expected address %s but saw %s\n", raddr, a);
-	recvlen = (int)packet_binpack(msg->msg_iov->iov_base,
+	recvlen = (int)packet_undump(msg->msg_iov->iov_base,
 				      msg->msg_iov->iov_len, pkt_dump);
     } else {
 	recvlen = recvmsg(sockfd, msg, flags);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/6bd4b0345a7c541955952be6097a0407c313efbe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20160915/d56c2e96/attachment.html>


More information about the vc mailing list