[ntp:hackers] review of offset calculation in simple querying client

Mark Martinec Mark.Martinec at ijs.si
Wed May 3 16:34:24 UTC 2006


Sean,

> currently, the offset calculation looks lsomething like this:
>
> static inline double calc_offset(const ntp_message *m, const struct timeval
> *t){ double client_tx, peer_rx, peer_tx, client_rx, rtdelay;
> 	client_tx = NTP64asDOUBLE(m->origts);
> 	peer_rx = NTP64asDOUBLE(m->rxts);
> 	peer_tx = NTP64asDOUBLE(m->txts);
> 	client_rx=TVasDOUBLE((*t));
> 	rtdelay=NTP32asDOUBLE(m->rtdelay);
> 	return (.5*((peer_tx-client_rx)+(peer_rx-client_tx))-rtdelay);
> }
>
> assuming that the macros in question do what they're supposed to do,
> is there anything fundamentally wrong with how i'm calculating the
> offset?  any insight would be appreciated!

Why are you subtracting rtdelay?

The formula (as given in rfc1305) is:
  delay = (Ti - Ti-3) - (Ti-1 - Ti-2)
  offset = ( (Ti-2 - Ti-3) + (Ti-1 - Ti) ) / 2

or in your parlance (I guess):
  delay = (client_rx - client_tx) - (peer_tx - peer_rx)
  offset = ( (peer_rx - client_tx) + (peer_tx - client_rx) ) / 2

The other thing that is fundamentally wrong is converting timestamps
to double first, then doing arithmetic on them. This limits precision
of the result to about 0.1 microsecond.

  Mark


More information about the hackers mailing list