-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NTP offset definition in book and source code is incorrect #86
Comments
I've also run into this issue with the offset calculation being wrong. The code (now) derives the offset from the delay value, but the actual NTP algorithm has entirely different calculations for offset and delay. It is trivially obvious that the provided code does not work, if one sets the time out by more than a few minutes, and then attempts to use It appears the attempt to "fix #51", with the commit 39e24d7, ended up switching from a somewhat incorrect definition of offset, to the blantantly incorrect definition of offset being "half absolute value of delay" :-/ (As also noted in #51 (comment), after that commit, but it appears there was no followup.) As noted in this issue (#86) Table 9.2 in the book (page 317 in the eBook PDF) does show two different formulas for the offset and delay, which appear to be almost correct, taking into account the different variable naming (but as noted by the original poster in this issue, the final two offset terms are swapped in Table 9.2). Unfortunately there's also a mistake in the original post which gives the formula as:
but actually the delta (offset) needs to be:
Note the "+" in the middle, not the "-": the offset is supposed to be the average of the first server-to-us offset and the second server-to-us offset. However even with that fixed, the time does not get corrected much, as best I can tell because the correction calculation code assumes that we are milliseconds off: code/ch9/ch9-clock3/src/main.rs Lines 339 to 342 in 6ebd519
and seems to be clamping the offset milliseconds to 200ms (via Directly using the offset as milliseconds, gets very close to "step to current NTP time":
so it's unclear what the Which just leaves the broken OS error handling that assumes any error at all must have been caused by setting the time, resulting in:
because it fails to handle the fact that libc doesn't clear Also the entire example doesn't compile because the Ewen Example of actually setting the time by the right offset
|
In https://github.com/rust-in-action/code/blob/1st-edition/ch9/ch9-clock3/src/main.rs#L40-L50:
The definition of offset is incorrect (source on Wikipedia). Offset can be positive or negative, whereas delay is always positive. You can see that the equations above always return positive values.
Offset should
let delta = (self.t2 - self.t1) - (self.t3 - self.t4);
. Currently, it rearranges tolet delta = (self.t2 - self.t1) - (self.t4 - self.t3);
, i.e. the last two terms are swapped.(I see there was an attempt to correct in #51, but as the OP later noted, the PR did not fix the issue.)
The text was updated successfully, but these errors were encountered: