Skip to content
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 timestamp construction is incorrect #34

Open
ccbrown opened this issue May 26, 2022 · 0 comments
Open

NTP timestamp construction is incorrect #34

ccbrown opened this issue May 26, 2022 · 0 comments

Comments

@ccbrown
Copy link

ccbrown commented May 26, 2022

This library currently constructs NTP timestamps like so:

const NTP_SCALE: f64 = std::u32::MAX as f64;

impl From<Instant> for protocol::TimestampFormat {
    fn from(t: Instant) -> Self {
        let sec = t.secs() + EPOCH_DELTA;
        let frac = t.subsec_nanos() as f64 * NTP_SCALE / 1e9;
        protocol::TimestampFormat {
            seconds: sec as u32,
            fraction: frac as u32,
        }
    }
}

RFC 5905 does it like so:

#define FRAC       4294967296.             /* 2^32 as a double */
#define U2LFP(a)   (((unsigned long long) \
                       ((a).tv_sec + JAN_1970) << 32) + \
                       (unsigned long long) \
                       ((a).tv_usec / 1e6 * FRAC))

The key difference is this library is multiplying the fractional component by 4294967295 (std::u32::MAX) while the RFC dictates it should be multiplied by 4294967296. Hence this library's timestamps are sometimes slightly off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant