Initial NTP protocol functionality

This commit is contained in:
Dan Paulat 2025-08-09 01:19:51 -05:00
parent 5a8ebfa7ae
commit 94d81c0c6b
7 changed files with 318 additions and 5 deletions

View file

@ -0,0 +1,32 @@
#pragma once
#include <memory>
#include <string_view>
namespace scwx::network
{
/**
* @brief NTP Client
*/
class NtpClient
{
public:
explicit NtpClient();
~NtpClient();
NtpClient(const NtpClient&) = delete;
NtpClient& operator=(const NtpClient&) = delete;
NtpClient(NtpClient&&) noexcept;
NtpClient& operator=(NtpClient&&) noexcept;
void Open(std::string_view host, std::string_view service);
void Poll();
private:
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace scwx::network

View file

@ -0,0 +1,61 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <span>
namespace scwx::types::ntp
{
/* Adapted from:
* https://github.com/lettier/ntpclient/blob/master/source/c/main.c
*
* Copyright (c) 2014 David Lettier
* Copyright (c) 2020 Krystian Stasiowski
* Distributed under the BSD 3-Clause License (See
* https://github.com/lettier/ntpclient/blob/master/LICENSE)
*/
#pragma pack(push, 1)
struct NtpPacket
{
union
{
std::uint8_t li_vn_mode;
struct
{
std::uint8_t mode : 3; // Client will pick mode 3 for client.
std::uint8_t vn : 3; // Version number of the protocol.
std::uint8_t li : 2; // Leap indicator.
} fields;
};
std::uint8_t stratum; // Stratum level of the local clock.
std::uint8_t poll; // Maximum interval between successive messages.
std::uint8_t precision; // Precision of the local clock.
std::uint32_t rootDelay; // Total round trip delay time.
std::uint32_t rootDispersion; // Max error aloud from primary clock source.
std::uint32_t refId; // Reference clock identifier.
std::uint32_t refTm_s; // Reference time-stamp seconds.
std::uint32_t refTm_f; // Reference time-stamp fraction of a second.
std::uint32_t origTm_s; // Originate time-stamp seconds.
std::uint32_t origTm_f; // Originate time-stamp fraction of a second.
std::uint32_t rxTm_s; // Received time-stamp seconds.
std::uint32_t rxTm_f; // Received time-stamp fraction of a second.
std::uint32_t txTm_s; // The most important field the client cares about.
// Transmit time-stamp seconds.
std::uint32_t txTm_f; // Transmit time-stamp fraction of a second.
static NtpPacket Parse(const std::span<std::uint8_t> data);
};
// Total: 48 bytes.
#pragma pack(pop)
} // namespace scwx::types::ntp