I have configured ntp to use gpsd as reference server on my machine. I am querying data from ntp directly, using udp socket. For ip address I use localhost and port_number = 123. Packet structure I use is:

typedef struct {
  uint8_t li_vn_mode;  // Eight bits. li, vn, and mode.
                       // li.   Two bits.   Leap indicator.
                       // vn.   Three bits. Version number of the protocol.
                       // mode. Three bits. Client will pick mode 3 for client.

  uint8_t stratum;  // Eight bits. Stratum level of the local clock.
  uint8_t poll;     // Eight bits. Maximum interval between successive messages.
  uint8_t precision;  // Eight bits. Precision of the local clock.

  uint32_t rootDelay;  // 32 bits. Total round trip delay time.
      rootDispersion;  // 32 bits. Max error aloud from primary clock source.
  uint32_t refId;      // 32 bits. Reference clock identifier.

  uint32_t refTm_s;  // 32 bits. Reference time-stamp seconds.
  uint32_t refTm_f;  // 32 bits. Reference time-stamp fraction of a second.

  uint32_t origTm_s;  // 32 bits. Originate time-stamp seconds.
  uint32_t origTm_f;  // 32 bits. Originate time-stamp fraction of a second.

  uint32_t rxTm_s;  // 32 bits. Received time-stamp seconds.
  uint32_t rxTm_f;  // 32 bits. Received time-stamp fraction of a second.

  uint32_t txTm_s;  // 32 bits and the most important field the client cares
                    // about. Transmit time-stamp seconds.
  uint32_t txTm_f;  // 32 bits. Transmit time-stamp fraction of a second.

} ntp_packet;  // Total: 384 bits or 48 bytes.

With this, I calculate offset using formula,

0.5 * ((server_receivetime - client_sendtime) + (server_sendtime - client_receivetime));

Now I stop gpsd service manually and run this program as well as ntpq -p . ntpq command shows me offset more than 10 seconds whereas my program will always give offset as 0.008 millisec. It seems that my program is using same clock for client and server. whereas ntpq is querying from somewhere else. How can I get the same offset as ntpq command?

If I use ip_address for server = "us.pool.ntp.org" I get offset around 12 millisecs.

  • I wouldn't expect any significant offset from a GPS time source. – Michael Hampton Aug 6 '19 at 2:41
  • 1
    Please edit your question to add ntpq -p output, which shows peers and their offsets. – John Mahowald Aug 6 '19 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.