| | 1 | | using System.Net.Sockets; |
| | 2 | | using System.Net; |
| | 3 | | using System; |
| | 4 | |
|
| | 5 | | namespace NtpServiceLibrary |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Provides methods for retrieving time from an NTP server. |
| | 9 | | /// </summary> |
| | 10 | | public class NtpTime |
| | 11 | | { |
| | 12 | | public const int ServerTimeout = 5000; |
| | 13 | |
|
| | 14 | | private class NtpPacket |
| | 15 | | { |
| | 16 | | public enum Version |
| | 17 | | { |
| | 18 | | Version2, |
| | 19 | | Version3 |
| | 20 | |
|
| | 21 | | } |
| | 22 | | public enum Mode |
| | 23 | | { |
| | 24 | | Client, |
| | 25 | | Server |
| | 26 | |
|
| | 27 | | } |
| | 28 | | public const uint BufferSize = 48; |
| | 29 | | public const int IntPartOffset = 40; |
| | 30 | | public const int FractPartOffset = 44; |
| 0 | 31 | | public byte[] Buffer { get; } |
| | 32 | |
|
| 0 | 33 | | public NtpPacket(Version version, Mode mode) |
| 0 | 34 | | { |
| 0 | 35 | | Buffer = new byte[BufferSize]; |
| 0 | 36 | | switch (mode) |
| | 37 | | { |
| | 38 | | case Mode.Client: |
| 0 | 39 | | if (version == Version.Version3) |
| 0 | 40 | | { |
| 0 | 41 | | Buffer[0] = 0x1B; |
| 0 | 42 | | } |
| | 43 | | else |
| 0 | 44 | | { |
| 0 | 45 | | throw new NotImplementedException(); |
| | 46 | | } |
| 0 | 47 | | break; |
| | 48 | | case Mode.Server: |
| 0 | 49 | | throw new NotImplementedException(); |
| | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public static DateTime Parse(byte[] data) |
| 0 | 54 | | { |
| 0 | 55 | | if (data == null || data.Length < BufferSize) |
| 0 | 56 | | throw new ArgumentException("Invalid NTP response"); |
| | 57 | |
|
| 0 | 58 | | uint intPart = SwapEndianness(BitConverter.ToUInt32(data, IntPartOffset)); |
| 0 | 59 | | uint fractPart = SwapEndianness(BitConverter.ToUInt32(data, FractPartOffset)); |
| | 60 | |
|
| 0 | 61 | | double milliseconds = intPart * 1000.0 + (fractPart * 1000.0 / (1UL << 32)); |
| 0 | 62 | | return new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milliseconds); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | private static uint SwapEndianness(uint x) |
| 0 | 66 | | { |
| 0 | 67 | | return (x >> 24) | |
| 0 | 68 | | ((x & 0x00FF0000) >> 8) | |
| 0 | 69 | | ((x & 0x0000FF00) << 8) | |
| 0 | 70 | | (x << 24); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public int Size |
| | 74 | | { |
| 0 | 75 | | get { return Buffer.Length; } |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Retrieves the current time from an NTP server over UDP. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="ntpServer">Hostname or IP address of the NTP server.</param> |
| | 83 | | /// <param name="ntpPort">UDP port, typically 123.</param> |
| | 84 | | /// <returns>UTC DateTime.</returns> |
| | 85 | | public static DateTime RetrieveNTPTime(string ntpServer, int ntpPort) |
| 0 | 86 | | { |
| 0 | 87 | | if (string.IsNullOrWhiteSpace(ntpServer)) |
| 0 | 88 | | throw new ArgumentNullException(nameof(ntpServer)); |
| | 89 | |
|
| | 90 | | try |
| 0 | 91 | | { |
| 0 | 92 | | using (UdpClient client = new UdpClient()) |
| 0 | 93 | | { |
| 0 | 94 | | client.Client.ReceiveTimeout = ServerTimeout; |
| | 95 | |
|
| 0 | 96 | | client.Connect(ntpServer, ntpPort); |
| | 97 | |
|
| 0 | 98 | | NtpPacket packet = new NtpPacket(NtpPacket.Version.Version3, NtpPacket.Mode.Client); |
| | 99 | |
|
| 0 | 100 | | client.Send(packet.Buffer, packet.Size); |
| | 101 | |
|
| 0 | 102 | | IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); |
| | 103 | |
|
| 0 | 104 | | return NtpPacket.Parse(client.Receive(ref remoteEndPoint)); |
| | 105 | |
|
| | 106 | | } |
| | 107 | | } |
| 0 | 108 | | catch (SocketException ex) |
| 0 | 109 | | { |
| 0 | 110 | | throw new InvalidOperationException($"Failed to contact NTP server {ntpServer}:{ntpPort}", ex); |
| | 111 | | } |
| 0 | 112 | | } |
| | 113 | | } |
| | 114 | | } |