Repository navigation
Expand file tree
/
Copy pathDialer.cs
More file actions
506 lines (448 loc) · 15.8 KB
/
Copy pathDialer.cs
File metadata and controls
506 lines (448 loc) · 15.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using RakNet.Messages;
using MsgId = RakNet.Messages.Id;
namespace RakNet;
/// <summary>
/// Interface for a custom upstream dialer that creates a UDP socket bound to
/// a specific local address. Mirrors Go's UpstreamDialer interface, typically
/// used to bind outgoing connections to a specific local address.
/// </summary>
public interface IUpstreamDialer
{
Socket CreateUdpSocket(IPEndPoint remoteAddr);
}
/// <summary>
/// Default upstream dialer that binds to any available local port.
/// </summary>
public class DefaultUpstreamDialer : IUpstreamDialer
{
public Socket CreateUdpSocket(IPEndPoint remoteAddr)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 0));
return socket;
}
}
/// <summary>
/// Upstream dialer that binds to a specific local address before connecting.
/// Equivalent to Go's net.Dialer with LocalAddr set.
/// </summary>
public class LocalAddressDialer : IUpstreamDialer
{
private readonly IPEndPoint _localAddr;
public LocalAddressDialer(IPEndPoint localAddr)
{
_localAddr = localAddr;
}
public Socket CreateUdpSocket(IPEndPoint remoteAddr)
{
var family = remoteAddr.AddressFamily == AddressFamily.InterNetworkV6
? AddressFamily.InterNetworkV6
: AddressFamily.InterNetwork;
var socket = new Socket(family, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(_localAddr);
return socket;
}
}
/// <summary>
/// Dialer provides configuration options for connecting to a RakNet server.
/// </summary>
public class Dialer
{
public Action<string>? ErrorLog { get; set; }
public int MaxTransientErrors { get; set; }
public ushort MaxMTU { get; set; }
/// <summary>
/// UpstreamDialer overrides the default UDP socket creation. Set to a
/// LocalAddressDialer to bind outgoing connections to a specific local
/// address. Defaults to DefaultUpstreamDialer (binds to any port).
/// </summary>
public IUpstreamDialer? UpstreamDialer { get; set; }
public TimeSpan PingTimeout => TimeSpan.FromSeconds(5);
public TimeSpan DialTimeout => TimeSpan.FromSeconds(10);
public byte[] Ping(string address)
{
return PingWithTimeout(address, PingTimeout);
}
public byte[] PingWithTimeout(string address, TimeSpan timeout)
{
using var cts = new CancellationTokenSource(timeout);
return PingContext(cts.Token, address);
}
public byte[] PingContext(CancellationToken token, string address)
{
var endpoint = ResolveAddress(address);
using var socket = CreateSocketFor(endpoint);
socket.ReceiveTimeout = (int)Math.Min(5000, PingTimeout.TotalMilliseconds);
var ping = new UnconnectedPing
{
PingTime = Conn.Timestamp(),
ClientGuid = Conn.NextDialerId()
};
socket.SendTo(ping.Marshal(), SocketFlags.None, endpoint);
byte[] buffer = new byte[RakNetConstants.MaxMTUSize];
while (!token.IsCancellationRequested)
{
EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
int n;
try
{
n = socket.ReceiveFrom(buffer, SocketFlags.None, ref remote);
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut)
{
continue;
}
catch (SocketException ex) when (IsTransientUDPReadError(ex))
{
continue;
}
if (n == 0 || buffer[0] != Id.UnconnectedPong)
throw new Exception($"non-pong packet found (id = {buffer[0]})");
var pong = new UnconnectedPong();
pong.Unmarshal(buffer.AsSpan(1, n - 1));
return pong.Data ?? Array.Empty<byte>();
}
throw new OperationCanceledException(token);
}
public Conn Dial(string address)
{
return DialTimeoutInternal(address, DialTimeout);
}
public Conn DialTimeoutInternal(string address, TimeSpan timeout)
{
using var cts = new CancellationTokenSource(timeout);
return DialContext(cts.Token, address);
}
public Conn DialContext(CancellationToken token, string address)
{
if (MaxTransientErrors == 0) MaxTransientErrors = 10;
var endpoint = ResolveAddress(address);
// Socket ownership transfers to Conn — do NOT dispose here.
var socket = CreateSocketFor(endpoint);
var state = new ConnState
{
Socket = socket,
RemoteAddr = endpoint,
Id = Conn.NextDialerId(),
MaxTransientErrors = MaxTransientErrors,
MaxMTU = MaxMTU
};
try
{
state.DiscoverMTU(token);
state.OpenConnection(token);
}
catch
{
socket.Dispose();
throw;
}
return Connect(token, state);
}
private Conn Connect(CancellationToken token, ConnState state)
{
var conn = new Conn(state.Socket, state.RemoteAddr, state.MTU,
new DialerConnectionHandler(ErrorLog));
var err = conn.SendInternal(new ConnectionRequest
{
ClientGuid = state.Id,
RequestTime = Conn.Timestamp()
});
if (err != null)
throw new Exception("send connection request", err);
var listenThread = new Thread(() => ClientListen(conn, state.Socket))
{
IsBackground = true,
Name = "RakNet-Listen"
};
listenThread.Start();
try
{
while (!conn.IsConnected)
{
if (token.IsCancellationRequested)
{
conn.CloseImmediately();
throw new OperationCanceledException(token);
}
Thread.Sleep(50);
}
state.Socket.ReceiveTimeout = -1;
return conn;
}
catch (OperationCanceledException)
{
conn.CloseImmediately();
throw;
}
}
private static void ClientListen(Conn rakConn, Socket socket)
{
byte[] buffer = new byte[RakNetConstants.MaxMTUSize];
try
{
while (true)
{
EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
int n;
try
{
n = socket.ReceiveFrom(buffer, SocketFlags.None, ref remote);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.TimedOut) continue;
if (IsTransientUDPReadError(ex)) continue;
return;
}
catch (ObjectDisposedException)
{
return;
}
if (n > 0)
{
try
{
rakConn.Receive(buffer.AsSpan(0, n).ToArray());
}
catch (Exception)
{
// Non-fatal: RakNet recovers via retransmission or
// timeout-based close in the ticking thread.
}
}
}
}
catch (ThreadInterruptedException) { }
}
internal static bool IsTransientUDPReadError(SocketException ex)
{
return ex.SocketErrorCode is SocketError.ConnectionRefused
or SocketError.HostUnreachable
or SocketError.NetworkUnreachable
or SocketError.ConnectionReset
or SocketError.ConnectionAborted;
}
internal static IPEndPoint ResolveAddress(string address)
{
int colon = address.LastIndexOf(':');
if (colon < 0) throw new ArgumentException($"invalid address: {address}");
string host = address.Substring(0, colon);
if (!int.TryParse(address.AsSpan(colon + 1), out int port))
throw new ArgumentException($"invalid port in address: {address}");
if (IPAddress.TryParse(host, out var ip))
return new IPEndPoint(ip, port);
var addresses = Dns.GetHostAddresses(host);
if (addresses.Length == 0)
throw new ArgumentException($"could not resolve host: {host}");
return new IPEndPoint(addresses[0], port);
}
internal static Socket CreateUdpSocket()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 0));
SocketTuning.Enlarge(socket);
SocketTuning.DisableUdpConnReset(socket);
return socket;
}
internal Socket CreateSocketFor(IPEndPoint remoteAddr)
{
var socket = UpstreamDialer != null
? UpstreamDialer.CreateUdpSocket(remoteAddr)
: CreateUdpSocket();
SocketTuning.Enlarge(socket);
return socket;
}
internal static ushort[] MtuSizesFor(ushort maxMTU)
{
const ushort minSupportedMTU = 576;
maxMTU = Conn.ClampMTU(maxMTU, minSupportedMTU);
ushort[] defaultSizes = { RakNetConstants.MaxMTUSize, 1200, minSupportedMTU };
if (maxMTU == RakNetConstants.MaxMTUSize) return defaultSizes;
List<ushort> result = new() { maxMTU };
foreach (var s in defaultSizes)
{
if (s < maxMTU) result.Add(s);
}
return result.ToArray();
}
}
/// <summary>
/// Internal state during the RakNet connection handshake. Holds the UDP socket,
/// remote address, discovered MTU, and security/cookie info.
/// </summary>
internal class ConnState
{
public Socket Socket = null!;
public IPEndPoint RemoteAddr = null!;
public long Id;
public ushort MTU;
public ushort MaxMTU;
public bool ServerSecurity;
public uint Cookie;
public int TransientErrorCount;
public int MaxTransientErrors;
private static readonly ushort[] DefaultMtuSizes = { RakNetConstants.MaxMTUSize, 1200, 576 };
public void DiscoverMTU(CancellationToken token)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(token);
var cancelToken = cts.Token;
// Start sending request1 packets in a background thread.
var requestThread = new Thread(() => Request1(cancelToken, Dialer.MtuSizesFor(MaxMTU)))
{
IsBackground = true,
Name = "RakNet-MTU-Discovery"
};
requestThread.Start();
byte[] buffer = new byte[RakNetConstants.MaxMTUSize];
SetReadTimeout(token);
while (true)
{
if (cancelToken.IsCancellationRequested)
{
cts.Cancel();
throw new OperationCanceledException(token);
}
int n;
try
{
EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
n = Socket.ReceiveFrom(buffer, SocketFlags.None, ref remote);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.TimedOut)
continue;
if (Dialer.IsTransientUDPReadError(ex) &&
(MaxTransientErrors == -1 || TransientErrorCount < MaxTransientErrors))
{
TransientErrorCount++;
continue;
}
throw new Exception("discover MTU: read error", ex);
}
if (n == 0) continue;
switch (buffer[0])
{
case MsgId.OpenConnectionReply1:
var response = new OpenConnectionReply1();
response.Unmarshal(buffer.AsSpan(1, n - 1));
ServerSecurity = response.ServerHasSecurity;
Cookie = response.Cookie;
if (response.ServerGuid == 0 || response.MTU < 400 || response.MTU > 1500)
{
OpenConnectionRequest2(response.MTU);
continue;
}
MTU = response.MTU;
cts.Cancel();
return;
case MsgId.IncompatibleProtocolVersion:
var incProto = new IncompatibleProtocolVersion();
incProto.Unmarshal(buffer.AsSpan(1, n - 1));
cts.Cancel();
throw new Exception(
$"mismatched protocol: client protocol = {RakNetConstants.ProtocolVersion}, server protocol = {incProto.ServerProtocol}");
}
}
}
private void Request1(CancellationToken token, ushort[] sizes)
{
foreach (var size in sizes)
{
for (int j = 0; j < 4; j++)
{
OpenConnectionRequest1(size);
try { Thread.Sleep(500); } catch { }
if (token.IsCancellationRequested) return;
}
}
}
private void OpenConnectionRequest1(ushort mtu)
{
var pk = new OpenConnectionRequest1
{
ClientProtocol = RakNetConstants.ProtocolVersion,
MTU = mtu
};
Socket.SendTo(pk.Marshal(), SocketFlags.None, RemoteAddr);
}
public void OpenConnection(CancellationToken token)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(token);
var cancelToken = cts.Token;
var requestThread = new Thread(() => Request2(cancelToken, MTU))
{
IsBackground = true,
Name = "RakNet-OpenConn"
};
requestThread.Start();
byte[] buffer = new byte[RakNetConstants.MaxMTUSize];
SetReadTimeout(token);
while (true)
{
if (cancelToken.IsCancellationRequested)
{
cts.Cancel();
throw new OperationCanceledException(token);
}
int n;
try
{
EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
n = Socket.ReceiveFrom(buffer, SocketFlags.None, ref remote);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.TimedOut)
continue;
if (Dialer.IsTransientUDPReadError(ex) &&
(MaxTransientErrors == -1 || TransientErrorCount < MaxTransientErrors))
{
TransientErrorCount++;
continue;
}
throw new Exception("open connection: read error", ex);
}
if (n == 0) continue;
if (buffer[0] != MsgId.OpenConnectionReply2) continue;
var pk = new OpenConnectionReply2();
pk.Unmarshal(buffer.AsSpan(1, n - 1));
MTU = pk.MTU;
cts.Cancel();
return;
}
}
private void Request2(CancellationToken token, ushort mtu)
{
while (true)
{
OpenConnectionRequest2(mtu);
try { Thread.Sleep(500); } catch { }
if (token.IsCancellationRequested) return;
}
}
internal void OpenConnectionRequest2(ushort mtu)
{
var pk = new OpenConnectionRequest2
{
ServerAddress = RemoteAddr,
MTU = mtu,
ClientGuid = Id,
ServerHasSecurity = ServerSecurity,
Cookie = Cookie
};
Socket.SendTo(pk.Marshal(), SocketFlags.None, RemoteAddr);
}
private void SetReadTimeout(CancellationToken token)
{
// Use a 500ms receive timeout so we can periodically check the
// cancellation token for overall deadline expiry.
Socket.ReceiveTimeout = 500;
}
}