[Git][NTPsec/ntpsec][master] 2 commits: TESTFRAME: intercept recvmsg operations.

Eric S. Raymond gitlab at mg.gitlab.com
Wed Sep 14 03:03:53 UTC 2016


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


Commits:
0f831999 by Eric S. Raymond at 2016-09-13T21:28:03-04:00
TESTFRAME: intercept recvmsg operations.

- - - - -
6b8787e8 by Eric S. Raymond at 2016-09-13T23:02:56-04:00
TESTFRAME: intercept select.

- - - - -


3 changed files:

- ntpd/ntp_intercept.c
- ntpd/ntp_intercept.h
- ntpd/ntp_io.c


Changes:

=====================================
ntpd/ntp_intercept.c
=====================================
--- a/ntpd/ntp_intercept.c
+++ b/ntpd/ntp_intercept.c
@@ -754,6 +754,38 @@ void intercept_sendpkt(const char *legend,
     }
 }
 
+int intercept_select(int nfds, fd_set *readfds)
+{
+    char pkt_dump[BUFSIZ];
+    int nfound;
+
+    if (mode == replay)
+    {
+	get_operation("select ");
+	/* FIXME: replay implementation here */
+	return 0;
+    } else {
+	nfound = select(nfds + 1, readfds, NULL, NULL, NULL);
+
+	if (mode == capture)
+	{
+	    int fd;
+	    snprintf(pkt_dump, sizeof(pkt_dump),
+		     "select %d:", nfound);
+	    for (fd = 0; fd <= nfds; fd++)
+		if (FD_ISSET(fd, readfds))
+		    snprintf(pkt_dump + strlen(pkt_dump),
+			     sizeof(pkt_dump) - strlen(pkt_dump),
+			     " %d", fd);
+	    strlcat(pkt_dump, "\n", sizeof(pkt_dump));
+
+	    fputs(pkt_dump, stdout);
+	}
+
+	return nfound;
+    }
+}
+
 ssize_t intercept_recvfrom(int sockfd, void *buf, size_t len, int flags,
                         struct sockaddr *src_addr, socklen_t *addrlen)
 {
@@ -766,20 +798,56 @@ ssize_t intercept_recvfrom(int sockfd, void *buf, size_t len, int flags,
 	/* FIXME: replay implementation here */
 	recvlen = 0;  /* squish compiler warning */
     } else {
-	char *cp;
-	snprintf(pkt_dump, sizeof(pkt_dump),
-		 "recvfrom %d %0x %s",
-		 sockfd, flags, socktoa((sockaddr_u *)src_addr));
-	for (cp = (char *)buf; cp < (char *)buf + len; cp++)
-	    snprintf(pkt_dump + strlen(pkt_dump),
-		     sizeof(pkt_dump) - strlen(pkt_dump),
-		     "%0x", *cp);
-	strlcat(pkt_dump, "\n", sizeof(pkt_dump));
-    
 	recvlen = recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
 
 	if (mode == capture)
+	{
+	    char *cp;
+	    snprintf(pkt_dump, sizeof(pkt_dump),
+		     "recvfrom %d %0x %s",
+		     sockfd, flags, socktoa((sockaddr_u *)src_addr));
+	    for (cp = (char *)buf; cp < (char *)buf + recvlen; cp++)
+		snprintf(pkt_dump + strlen(pkt_dump),
+			 sizeof(pkt_dump) - strlen(pkt_dump),
+			 "%0x", *cp);
+	    strlcat(pkt_dump, "\n", sizeof(pkt_dump));
+
+	    fputs(pkt_dump, stdout);
+	}
+    }
+
+    return recvlen;
+}
+
+ssize_t intercept_recvmsg(int sockfd, struct msghdr *msg, int flags)
+{
+    char pkt_dump[BUFSIZ];
+    ssize_t recvlen;
+
+    if (mode == replay)
+    {
+	get_operation("recvmsg ");
+	/* FIXME: replay implementation here */
+	recvlen = 0;  /* squish compiler warning */
+    } else {
+	recvlen = recvmsg(sockfd, msg, flags);
+
+	if (mode == capture) {
+	    char *cp;
+	    snprintf(pkt_dump, sizeof(pkt_dump),
+		     "recvmsg %d %0x %s",
+		     sockfd, msg->msg_flags,
+		     socktoa((sockaddr_u *)(&msg->msg_name)));
+	    for (cp = (char *)msg->msg_iov->iov_base;
+		 cp < (char *)msg->msg_iov->iov_base + recvlen;
+		 cp++)
+		snprintf(pkt_dump + strlen(pkt_dump),
+			 sizeof(pkt_dump) - strlen(pkt_dump),
+			 "%0x", *cp);
+	    strlcat(pkt_dump, "\n", sizeof(pkt_dump));
+
 	    fputs(pkt_dump, stdout);
+	}
     }
 
     return recvlen;


=====================================
ntpd/ntp_intercept.h
=====================================
--- a/ntpd/ntp_intercept.h
+++ b/ntpd/ntp_intercept.h
@@ -23,8 +23,10 @@ void intercept_get_systime(const char *, l_fp *);
 long intercept_ntp_random(const char *);
 void intercept_sendpkt(const char *,
 		       sockaddr_u *, struct interface *, int, void *, int);
+int intercept_select(int nfds, fd_set *);
 ssize_t intercept_recvfrom(int, void *,
 			   size_t, int, struct sockaddr *, socklen_t *);
+ssize_t intercept_recvmsg(int sockfd, struct msghdr *msg, int flags);
 SOCKET intercept_open_socket(sockaddr_u *, bool, bool, endpt *);
 bool intercept_drift_read(const char *, double *);
 void intercept_drift_write(char *, double);


=====================================
ntpd/ntp_io.c
=====================================
--- a/ntpd/ntp_io.c
+++ b/ntpd/ntp_io.c
@@ -3347,7 +3347,7 @@ read_network_packet(
 	msghdr.msg_flags      = 0;
 	msghdr.msg_control    = (void *)&control;
 	msghdr.msg_controllen = sizeof(control);
-	rb->recv_length       = recvmsg(fd, &msghdr, 0);
+	rb->recv_length       = intercept_recvmsg(fd, &msghdr, 0);
 #endif
 
 	buflen = rb->recv_length;
@@ -3445,8 +3445,7 @@ io_handler(void)
 	 * Mostly harmless, I think.
 	 */
 	rdfdes = activefds;
-	nfound = select(maxactivefd + 1, &rdfdes, NULL,
-			NULL, NULL);
+	nfound = intercept_select(maxactivefd, &rdfdes);
 	if (nfound > 0) {
 		l_fp ts;
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/84e7748ce703700f65fe2e923bf2420c0d0e0917...6b8787e861643f7e71522f2b65fa671cce145332
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20160914/41c75b88/attachment.html>


More information about the vc mailing list