[Git][NTPsec/ntpsec][master] 2 commits: Simplified replay implementation of send and receive.

Eric S. Raymond gitlab at mg.gitlab.com
Sun Dec 13 11:59:22 UTC 2015


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


Commits:
b6e96fe6 by Eric S. Raymond at 2015-12-13T06:02:08Z
Simplified replay implementation of send and receive.

- - - - -
f9e35ff6 by Eric S. Raymond at 2015-12-13T06:58:58Z
First complete version of replay interpreter ready for test.

- - - - -


1 changed file:

- ntpd/ntp_intercept.c


Changes:

=====================================
ntpd/ntp_intercept.c
=====================================
--- a/ntpd/ntp_intercept.c
+++ b/ntpd/ntp_intercept.c
@@ -210,36 +210,49 @@ static bool pump(const char *fn, const char *lead, const char *trail, FILE *ofp)
 	}
 }
 
-
-void intercept_getconfig(const char *configfile)
+static void file_replay(const char *configfile, char *delimiter, char *tempfile)
 {
-    if (mode != replay)
-	/* this can be null if the default config doesn't exist */
-	configfile = getconfig(configfile);
+    FILE *tfp;
 
-    if (configfile != NULL && mode != none)
-	pump(configfile, "startconfig\n", "endconfig\n", stdout);
+    tfp = fopen(tempfile, "w");
+    if (tfp == NULL) {
+	fprintf(stderr,
+		"ntpd: replay failed, can't copy %s\n", configfile);
+	exit(1);
+    }
+    for (;;) {
+	char *nextline = fgets(linebuf, sizeof(linebuf), stdin);
 
+	++lineno;
+	if (nextline == NULL) {
+	    fprintf(stderr,
+		    "ntpd: replay failed, unexpected EOF at line %d\n", lineno);
+	    exit(1);
+	}
+	if (strncmp(linebuf, delimiter, strlen(delimiter)) == 0)
+	    break;
+	fputs(linebuf, tfp);
+    }
+    fclose(tfp);
+}
+
+void intercept_getconfig(const char *configfile)
+{
     if (mode == replay) {
 	char tempfile[PATH_MAX];
-	FILE *tfp;
 
 	stats_control = false;	/* suppress writing stats files */
 	get_operation("startconfig");
 	snprintf(tempfile, sizeof(tempfile), ".fake_ntp_config_%d", getpid());
-	tfp = fopen(tempfile, "w");
-	for (;;) {
-	    char *nextline = fgets(linebuf, sizeof(linebuf), stdin);
-	    if (nextline == NULL) {
-		fputs("ntpd: replay failed, unexpected EOF in config\n", stderr);
-		exit(1);
-	    }
-	    if (strncmp(linebuf, "endconfig", 9) == 0)
-		break;
-	    fputs(linebuf, tfp);
-	}	    
+	file_replay(configfile, "endconfig", tempfile);
 	getconfig(tempfile);
 	unlink(tempfile);
+    } else {
+	/* this can be null if the default config doesn't exist */
+	configfile = getconfig(configfile);
+
+	if (configfile != NULL && mode != none)
+	    pump(configfile, "startconfig\n", "endconfig\n", stdout);
     }
 }
 
@@ -256,7 +269,7 @@ void intercept_get_systime(const char *legend, l_fp *now)
     if (mode == replay) {
 	int sec, subsec;
 	char expecting[BUFSIZ];
-	get_operation("systime");
+	get_operation("systime ");
 	if (sscanf(linebuf, "systime %s %d.%d", expecting, &sec, &subsec) != 3) {
 	    fprintf(stderr, "ntpd: garbled systime format, line %d\n", lineno);
 	    exit(1);
@@ -289,6 +302,7 @@ long intercept_ntp_random(const char *legend)
 	 * as all the environment-altering functions that call ntp_random()
 	 * are themselves intercepted.
 	 */
+	get_operation("random ");
 	if (sscanf(linebuf, "random %s %ld", expecting, &roll) != 2) {
 	    fprintf(stderr, "ntpd: garbled random format, line %d\n", lineno);
 	    exit(1);
@@ -323,7 +337,7 @@ bool intercept_drift_read(const char *drift_file, double *drift)
 {
     if (mode == replay) {
 	float df;
-	get_operation("drift_read");
+	get_operation("drift_read ");
 	if (strstr(linebuf, "false") != NULL)
 	    return false;
 	/*
@@ -373,7 +387,7 @@ void intercept_drift_write(char *driftfile, double drift)
 	 * replay mode, so just check that we're writing out the same drift. 
 	 */
 	float df;
-	get_operation("drift-write");
+	get_operation("drift-write ");
 	/* See the comment of drift-read chwxcking. */ 
 	if (sscanf(linebuf, "drift-write %f'", &df) != 1) {
 	    fprintf(stderr, "ntpd: garbled drift-write format, line %d\n",lineno);
@@ -423,7 +437,7 @@ int intercept_adjtime(const struct timeval *ntv, struct timeval *otv)
 {
     if (mode == replay) {
 	struct timeval rntv, rotv;
-	get_operation("adjtime");
+	get_operation("adjtime ");
 	/* bletch - likely to foo up on 32-bit machines */
 	if (sscanf(linebuf, "adjtime %ld %ld %ld %ld",
 		   &rntv.tv_sec, &rntv.tv_usec,
@@ -491,7 +505,7 @@ int intercept_ntp_adjtime(struct timex *tx)
     if (mode == replay)
     {
 	struct timex rtx;
-	get_operation("ntp_adjtime");
+	get_operation("ntp_adjtime ");
 	if (sscanf(linebuf, "ntp_adtime " ADJFMT " %d",
 		   &rtx.modes,
 		   &rtx.offset,
@@ -555,42 +569,65 @@ int intercept_ntp_adjtime(struct timex *tx)
 
 int intercept_set_tod(struct timespec *tvs)
 {
+    char newset[BUFSIZ];
+    snprintf(newset, sizeof(newset),
+	     "set_tod %ld %ld\n", (long)tvs->tv_sec, (long)tvs->tv_nsec);
+    
     if (mode == replay) {
 	get_operation("set_tod");
-	/* FIXME: more replay logic goes here */
+	if (strcmp(linebuf, newset) != 0) {
+	    fprintf(stderr, "ntpd: line %d, set_tod mismatch saw %s\n",
+		    lineno, newset);
+	    exit(1);
+	}
+	return 0;
     }
     else {
 	if (mode == capture)
-	    printf("set_tod %ld %ld\n", (long)tvs->tv_sec, (long)tvs->tv_nsec);
+	    fputs(newset, stdout);
 	return ntp_set_tod(tvs);
     }
-    return 0;
 }
 
 bool
 intercept_leapsec_load_file(
-	const char  * fname,
-	struct stat * sb_old,
+	const char  *leapsecfile,
+	struct stat *sb_old,
 	bool   force,
 	bool   logall)
 {
     bool loaded = true;
 
-    if (mode != replay)
-	loaded = leapsec_load_file(fname, sb_old, force, logall);
+    if (mode == replay) {
+	char tempfile[PATH_MAX];
 
-    if (mode == capture)
-	pump(fname, "startleapsec\n", "endleapsec\n", stdout);
+	get_operation("startleapsec");
+	snprintf(tempfile, sizeof(tempfile), ".fake_leapsec_file_%d", getpid());
+	file_replay(leapsecfile, "endleapsec", tempfile);
+	loaded = leapsec_load_file(tempfile, sb_old, force, logall);
+	unlink(tempfile);
+    } else {
+	loaded = leapsec_load_file(leapsecfile, sb_old, force, logall);
+
+	if (mode == capture)
+	    pump(leapsecfile, "startleapsec\n", "endleapsec\n", stdout);
+    }
 
-    /* FIXME: replay logic goes here */
-    
     return loaded;
 }
 
-static void packet_dump(sockaddr_u *dest, struct pkt *pkt, int len)
+static void packet_dump(char *buf, size_t buflen,
+			sockaddr_u *dest, struct pkt *pkt, int len)
 {
+    /*
+     * Order is: cast flags, receipt time, interface name, source
+     * address, packet, length.  Cast flags are only kept because
+     * they change the ntpq display, they have no implications for
+     * the protocol machine.  We don't dump srcadr because only
+     * the parse clock uses that.
+     */
     size_t i;
-    printf("%s %d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s",
+    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,
 	   /* FIXME: might be better to dump these in fixed-point */
@@ -607,54 +644,68 @@ void intercept_sendpkt(const char *legend,
 		  sockaddr_u *dest, struct interface *ep, int ttl,
 		  struct pkt *pkt, int len)
 {
-    if (mode != replay)
+    char pkt_dump[BUFSIZ], newpacket[BUFSIZ];
+    packet_dump(pkt_dump, sizeof(pkt_dump), dest, pkt, len);
+    snprintf(newpacket, sizeof(newpacket), "sendpkt %s %s\n", legend, pkt_dump);
+
+    if (mode == replay)
+    {
+	get_operation("sendpkt ");
+	if (strcmp(linebuf, newpacket) != 0) {
+	    fprintf(stderr, "ntpd: line %d, sendpkt mismatch saw %s\n",
+		    lineno, pkt_dump);
+	    exit(1);
+	}
+    } else {
 	sendpkt(dest, ep, ttl, pkt, len);
 
-    if (mode != none) {
-	printf("sendpkt \"%s\" ", legend);
-	packet_dump(dest, pkt, len);
-	fputs("\n", stdout);
+	if (mode == capture)
+	    fputs(newpacket, stdout);
     }
-
-    /* FIXME: replay logic goes here */
 }
 
 void intercept_receive(struct recvbuf *rbufp)
 {
-    if (mode != none) {
-	/*
-	 * Order is: cast flags, receipt time, interface name, source
-	 * address, packet, length.  Cast flags are only kept because
-	 * they change the ntpq display, they have no implications for
-	 * the protocol machine.  We don't dump srcadr because only
-	 * the parse clock uses that.
-	 */
-	printf("receive %0x %s %s ",
-	       rbufp->cast_flags,
-	       lfpdump(&rbufp->recv_time),
-	       rbufp->dstadr->name);
-	packet_dump(&rbufp->recv_srcadr, &rbufp->recv_pkt, rbufp->recv_length);
-	fputs("\n", stdout);
-    }
+    char pkt_dump[BUFSIZ], newpacket[BUFSIZ];
+    packet_dump(pkt_dump, sizeof(pkt_dump),
+		&rbufp->recv_srcadr,
+		&rbufp->recv_pkt, rbufp->recv_length);
+    snprintf(newpacket, sizeof(newpacket),
+	     "receive %0x %s %s\n",
+	     rbufp->cast_flags, lfpdump(&rbufp->recv_time), pkt_dump);
 
-    if (mode != replay)
-	receive(rbufp);
+    if (mode == replay) {
+	get_operation("receive ");
+	if (strcmp(linebuf, newpacket) != 0) {
+	    fprintf(stderr, "ntpd: line %d, receive mismatch saw %s\n",
+		    lineno, newpacket);
+	    exit(1);
+	}
+    } else {
+	if (mode == capture)
+	    fputs(newpacket, stdout);
 
-    /* FIXME: replay logic goes here */
+	receive(rbufp);
+    }
 }
 
 void
-intercept_getauthkeys(
-	const char  * fname)
+intercept_getauthkeys(const char *authkeysfile)
 {
-    if (mode != replay)
-	getauthkeys(fname);
+    if (mode == replay) {
+	char tempfile[PATH_MAX];
 
-    if (mode == capture) {
-	pump(fname, "startauthkeys", "endauthkeys", stdout);
-    }
+	get_operation("startauthkeys");
+	snprintf(tempfile, sizeof(tempfile), ".fake_ntp_authkeys_%d", getpid());
+	file_replay(authkeysfile, "endauthkeys", tempfile);
+	getauthkeys(tempfile);
+	unlink(tempfile);
+    } else {
+	getauthkeys(authkeysfile);
 
-    /* FIXME: replay logic goes here */
+	if (mode == capture)
+	    pump(authkeysfile, "startauthkeys\n", "endauthkeys\n", stdout);
+    }
 }
 
 void intercept_exit(int sig)



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/2a62a835720abda7fab87740dd05ae284893c3dc...f9e35ff6dda2b8b383f46de06cbf103dd6955439
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20151213/eff1f15a/attachment.html>


More information about the vc mailing list