[Git][NTPsec/ntpsec][master] 4 commits: Dump refid field in hex.

Eric S. Raymond gitlab at mg.gitlab.com
Fri Sep 16 12:10:17 UTC 2016


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


Commits:
bf639508 by Eric S. Raymond at 2016-09-16T06:31:02-04:00
Dump refid field in hex.

- - - - -
ea47dc97 by Eric S. Raymond at 2016-09-16T06:46:17-04:00
Remove a dead macro.

- - - - -
a4863534 by Eric S. Raymond at 2016-09-16T07:26:02-04:00
TESTFRAME: Use pre-exising dump and load functions for lfp.

- - - - -
5381fca5 by Eric S. Raymond at 2016-09-16T08:09:32-04:00
TESTFRAME: Verified round-tripping of packet dumps.

- - - - -


3 changed files:

- include/timevalops.h
- libntp/pktvis.c
- tests/libntp/ntpvis.c


Changes:

=====================================
include/timevalops.h
=====================================
--- a/include/timevalops.h
+++ b/include/timevalops.h
@@ -87,13 +87,6 @@
 	((x)->tv_usec >= 0 && (x)->tv_usec < MICROSECONDS)
 
 /*
- * Convert milliseconds to a time stamp fraction.  Unused except for
- * refclock_leitch.c, so accompanying lookup tables were removed in
- * favor of reusing the microseconds conversion tables.
- */
-#define	MSUTOTSF(msu, tsf)	TVUTOTSF((msu) * 1000, tsf)
-
-/*
  * predicate: returns true if the microseconds are out-of-bounds
  * use like: int timeval_isdenormal(const struct timeval *x)
  */


=====================================
libntp/pktvis.c
=====================================
--- a/libntp/pktvis.c
+++ b/libntp/pktvis.c
@@ -15,36 +15,19 @@
 #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;
-}
+static const int LFP_MAX_PRECISION = 10;
 
 void packet_dump(char *buf, size_t buflen, struct pkt *pkt, size_t len)
 {
     size_t i;
-    /*
-     * FIXME: struct pkt 32-bit ints (uint32_t, u_fp) are in network
-     * byte order. Might need to add htonl()/ntohl() calls here for
-     * comprehensibility.
-     */
-    snprintf(buf, buflen, "%d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s:",
-	   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));
+    snprintf(buf, buflen, "%d:%d:%d:%d:%u:%u:%08x:%s:%s:%s:%s:",
+	     pkt->li_vn_mode, pkt->stratum, pkt->ppoll, pkt->precision,
+	     pkt->rootdelay, pkt->rootdisp,
+	     pkt->refid,
+	     lfptoa(&pkt->reftime, LFP_MAX_PRECISION),
+	     lfptoa(&pkt->org, LFP_MAX_PRECISION),
+	     lfptoa(&pkt->rec, LFP_MAX_PRECISION),
+	     lfptoa(&pkt->xmt, LFP_MAX_PRECISION));
 
     if (len == LEN_PKT_NOMAC)
 	strlcat(buf, "nomac", buflen);
@@ -56,28 +39,18 @@ void packet_dump(char *buf, size_t buflen, struct pkt *pkt, size_t len)
 	}
 }
 
-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",
+    if (sscanf(pktbuf, "%d:%d:%d:%d:%u:%u:%08x:%[^:]:%[^:]:%[^:]:%[^:]:%s",
 		     &li_vn_mode, &stratum,
 		     &ppoll, &precision,
 		     &pkt->rootdelay, &pkt->rootdisp,
 		     &pkt->refid,
-	       refbuf, orgbuf, recbuf, xmtbuf, macbuf) != 11)
+	       refbuf, orgbuf, recbuf, xmtbuf, macbuf) != 12)
 	return -1;
 
     /* extra transfers required because the struct members are int8_t */
@@ -85,10 +58,10 @@ static int packet_parse(char *pktbuf, struct pkt *pkt)
     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);
+    atolfp(refbuf, &pkt->reftime);
+    atolfp(orgbuf, &pkt->org);
+    atolfp(recbuf, &pkt->rec);
+    atolfp(xmtbuf, &pkt->xmt);
 
     pktlen = LEN_PKT_NOMAC;
     memset(pkt->exten, '\0', sizeof(pkt->exten));
@@ -100,8 +73,9 @@ static int packet_parse(char *pktbuf, struct pkt *pkt)
 		return -1;
 	    }
 	    pkt->exten[i / sizeof(uint32_t)] |= BIGEND_PUTBYTE(hexval, i);
-	    ++pktlen;
 	}
+
+	pktlen += i;
     }
     return pktlen;
 }
@@ -112,8 +86,7 @@ size_t packet_undump(char *bin, int len, char *pktbuf)
     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));
+    memcpy(bin, (char *)&pkt, pktlen);
     return pktlen;
 }
 


=====================================
tests/libntp/ntpvis.c
=====================================
--- a/tests/libntp/ntpvis.c
+++ b/tests/libntp/ntpvis.c
@@ -6,11 +6,16 @@
 
 TEST_GROUP(ntpvis);
 
-TEST_SETUP(ntpvis) {}
+#include "ntpd.h"
+#include "timevalops.h"
 
-TEST_TEAR_DOWN(ntpvis) {}
+/* second/usec pair to unsigned lfp */
+#define SECUSECTOTS(sec, usec, ts)		\
+	do { \
+		(ts)->l_ui = (u_long)sec; \
+		TVUTOTSF(usec, (ts)->l_uf); \
+	} while (false)
 
-#include "ntpd.h"
 
 static struct pkt ExamplePacket1 = {
     .li_vn_mode = 6,
@@ -20,15 +25,9 @@ static struct pkt ExamplePacket1 = {
     .rootdelay = 0,
     .rootdisp = 0,
     .refid = 0x47505300,	/* big-endian 'GPS\0' */
-#ifdef __unused__
-    .reftime = 0,
-    .org = 0,
-    .rec = 0,
-    .xmt = 0,
-#endif
     .exten = {0},
 };
-static char *ExampleDump1 = "6:2:3:-21:0:0:1196446464:0:0:0:0:nomac";
+static char *ExampleDump1 = "6:2:3:-21:0:0:47505300:1474021718.5261510001:0.0000000000:0.0000000000:0.0000000000:nomac";
 
 /* same as ExamplePacket1 but with 4 extension bytes */
 static struct pkt ExamplePacket2 = {
@@ -39,16 +38,17 @@ static struct pkt ExamplePacket2 = {
     .rootdelay = 0,
     .rootdisp = 0,
     .refid = 0x47505300,	/* big-endian 'GPS\0' */
-#ifdef __unused__
-    .reftime = 0,
-    .org = 0,
-    .rec = 0,
-    .xmt = 0,
-#endif
     .exten = {0x01020304},
 };
-static char *ExampleDump2 = "6:2:3:-21:0:0:1196446464:0:0:0:0:01020304";
+static char *ExampleDump2 = "6:2:3:-21:0:0:47505300:1474021718.5261510001:0.0000000000:0.0000000000:0.0000000000:01020304";
 
+TEST_SETUP(ntpvis) {
+    /* becomes lfp 1474021718.5261510001 */
+    SECUSECTOTS(1474021718, 526151, &ExamplePacket1.reftime); 
+    SECUSECTOTS(1474021718, 526151, &ExamplePacket2.reftime); 
+}
+
+TEST_TEAR_DOWN(ntpvis) {}
 
 TEST(ntpvis, PacketDump1) {
     char buf[BUFSIZ];
@@ -64,7 +64,29 @@ TEST(ntpvis, PacketDump2) {
     TEST_ASSERT_EQUAL_STRING(ExampleDump2, buf);
 }
 
+TEST(ntpvis, Roundtrip1) {
+    char buf1[BUFSIZ], buf2[BUFSIZ], bin[BUFSIZ];
+    size_t binlen;
+
+    packet_dump(buf1, sizeof(buf1), &ExamplePacket1, LEN_PKT_NOMAC);
+    binlen = packet_undump(bin, sizeof(bin), buf1);
+    packet_dump(buf2, sizeof(buf2), (struct pkt *)bin, binlen);
+    TEST_ASSERT_EQUAL_STRING(buf1, buf2);
+}
+
+TEST(ntpvis, Roundtrip2) {
+    char buf1[BUFSIZ], buf2[BUFSIZ], bin[BUFSIZ];
+    size_t binlen;
+
+    packet_dump(buf1, sizeof(buf1), &ExamplePacket2, LEN_PKT_NOMAC);
+    binlen = packet_undump(bin, sizeof(bin), buf1);
+    packet_dump(buf2, sizeof(buf2), (struct pkt *)bin, binlen);
+    TEST_ASSERT_EQUAL_STRING(buf1, buf2);
+}
+
 TEST_GROUP_RUNNER(ntpvis) {
     RUN_TEST_CASE(ntpvis, PacketDump1);
     RUN_TEST_CASE(ntpvis, PacketDump2);
+    RUN_TEST_CASE(ntpvis, Roundtrip1);
+    RUN_TEST_CASE(ntpvis, Roundtrip2);
 }



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/81816b30a9c6b0fe59b012a83460f8b53e035db9...5381fca5c197f6a57301b0642b4e7bfe6980c731
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20160916/277bc117/attachment.html>


More information about the vc mailing list