-
Notifications
You must be signed in to change notification settings - Fork 803
Expand file tree
/
Copy pathSNTPLookup.cs
More file actions
147 lines (112 loc) · 4.61 KB
/
SNTPLookup.cs
File metadata and controls
147 lines (112 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using ControlzEx.Standard;
using NETworkManager.Utilities;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NETworkManager.Models.Network;
public sealed class SNTPLookup
{
#region Variables
private readonly SNTPLookupSettings _settings;
#endregion
#region Constructor
public SNTPLookup(SNTPLookupSettings settings)
{
_settings = settings;
}
#endregion
#region Events
public event EventHandler<SNTPLookupResultArgs> ResultReceived;
private void OnResultReceived(SNTPLookupResultArgs e)
{
ResultReceived?.Invoke(this, e);
}
public event EventHandler<SNTPLookupErrorArgs> LookupError;
private void OnLookupError(SNTPLookupErrorArgs e)
{
LookupError?.Invoke(this, e);
}
public event EventHandler LookupComplete;
private void OnLookupComplete()
{
LookupComplete?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Methods
private static SNTPDateTime GetNetworkTimeRfc2030(IPEndPoint server, int timeout = 4000)
{
var ntpData = new byte[48]; // RFC 2030
ntpData[0] = 0x1B; // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
var udpClient = new UdpClient(server.AddressFamily);
udpClient.Client.SendTimeout = timeout;
udpClient.Client.ReceiveTimeout = timeout;
udpClient.Connect(server);
var localStartTime = DateTime.Now.ToUniversalTime();
udpClient.Send(ntpData, ntpData.Length);
ntpData = udpClient.Receive(ref server);
var localEndTime = DateTime.Now.ToUniversalTime();
udpClient.Close();
var intPart = ((ulong)ntpData[40] << 24) | ((ulong)ntpData[41] << 16) | ((ulong)ntpData[42] << 8) | ntpData[43];
var fractionPart = ((ulong)ntpData[44] << 24) | ((ulong)ntpData[45] << 16) | ((ulong)ntpData[46] << 8) |
ntpData[47];
var milliseconds = intPart * 1000 + fractionPart * 1000 / 0x100000000L;
var networkTime = new DateTime(1900, 1, 1).AddMilliseconds((long)milliseconds);
// Calculate local offset with local start/end time and network time in seconds
var roundTripDelayTicks = localEndTime.Ticks - localStartTime.Ticks;
var offsetInSeconds = (localStartTime.Ticks + roundTripDelayTicks / 2 - networkTime.Ticks) /
TimeSpan.TicksPerSecond;
return new SNTPDateTime
{
LocalStartTime = localStartTime,
LocalEndTime = localEndTime,
NetworkTime = networkTime,
RoundTripDelay = roundTripDelayTicks / TimeSpan.TicksPerMillisecond,
Offset = offsetInSeconds
};
}
public void QueryAsync(IEnumerable<ServerConnectionInfo> servers, bool dnsResolveHostnamePreferIPv4)
{
Task.Run(() =>
{
Parallel.ForEach(servers, server =>
{
// NTP requires an IP address to connect to
IPAddress serverIP = null;
if (RegexHelper.IPv4AddressRegex().IsMatch(server.Server) ||
Regex.IsMatch(server.Server, RegexHelper.IPv6AddressRegex))
{
serverIP = IPAddress.Parse(server.Server);
}
else
{
using var dnsResolverTask =
DNSClientHelper.ResolveAorAaaaAsync(server.Server, dnsResolveHostnamePreferIPv4);
// Wait for task inside a Parallel.Foreach
dnsResolverTask.Wait();
if (dnsResolverTask.Result.HasError)
{
OnLookupError(new SNTPLookupErrorArgs(
DNSClientHelper.FormatDNSClientResultError(server.Server, dnsResolverTask.Result), true));
return;
}
serverIP = dnsResolverTask.Result.Value;
}
try
{
var dateTime = GetNetworkTimeRfc2030(new IPEndPoint(serverIP, server.Port), _settings.Timeout);
OnResultReceived(new SNTPLookupResultArgs(
new SNTPLookupInfo(server.Server, $"{serverIP}:{server.Port}", dateTime)));
}
catch (Exception ex)
{
OnLookupError(new SNTPLookupErrorArgs(server.Server, $"{serverIP}:{server.Port}", ex.Message));
}
});
OnLookupComplete();
});
}
#endregion
}