ntp: Send a transmit time value that reflects current system time
authorGrant Erickson <marathon96@gmail.com>
Mon, 16 Jul 2012 16:48:00 +0000 (09:48 -0700)
committerDaniel Wagner <daniel.wagner@bmw-carit.de>
Tue, 17 Jul 2012 08:13:16 +0000 (10:13 +0200)
This sets the ntp request packet transmit time to the current system
time to reflect behavior implemented by ntpdate and ntpd in one-shot
mode.

src/ntp.c

index c7224a8..1916a72 100644 (file)
--- a/src/ntp.c
+++ b/src/ntp.c
@@ -117,8 +117,6 @@ static void send_packet(int fd, const char *server)
        msg.flags = NTP_FLAGS_ENCODE(NTP_FLAG_LI_NOWARNING, 4, NTP_FLAG_MD_CLIENT);
        msg.poll = 4;   // min
        msg.poll = 10;  // max
-       msg.xmttime.seconds = random();
-       msg.xmttime.fraction = random();
 
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
@@ -127,6 +125,9 @@ static void send_packet(int fd, const char *server)
 
        gettimeofday(&transmit_timeval, NULL);
 
+       msg.xmttime.seconds = htonl(transmit_timeval.tv_sec + OFFSET_1900_1970);
+       msg.xmttime.fraction = htonl(transmit_timeval.tv_usec * 1000);
+
        len = sendto(fd, &msg, sizeof(msg), MSG_DONTWAIT,
                                                &addr, sizeof(addr));
        if (len < 0) {