From: Matthew Endsley Date: Mon, 1 Feb 2021 21:53:52 +0000 (-0800) Subject: Add stub DTLS client connection X-Git-Tag: 1.0.0~20^2~18^2~1 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=dd204c30623a9fd589e7e25591de94f3cd0a4135;p=rhonda%2Fimpostor.hazel.git Add stub DTLS client connection --- diff --git a/Hazel/Dtls/DtlsConnectionListener.cs b/Hazel/Dtls/DtlsConnectionListener.cs index 72eabc8..600b0fa 100644 --- a/Hazel/Dtls/DtlsConnectionListener.cs +++ b/Hazel/Dtls/DtlsConnectionListener.cs @@ -108,7 +108,6 @@ namespace Hazel.Dtls this.CanHandleApplicationData = false; this.QueuedApplicationDataMessage.Clear(); - this.CurrentEpoch = new CurrentEpoch(); this.CurrentEpoch.NextOutgoingSequence = 2; // Account for our ClientHelloVerify this.CurrentEpoch.NextExpectedSequence = nextExpectedSequenceNumber; this.CurrentEpoch.PreviousSequenceWindowBitmask = 0; @@ -116,7 +115,6 @@ namespace Hazel.Dtls this.CurrentEpoch.ServerFinishedVerification.SecureClear(); this.CurrentEpoch.ExpectedClientFinishedVerification.SecureClear(); - this.NextEpoch = new NextEpoch(); this.NextEpoch.State = HandshakeState.ExpectingHello; this.NextEpoch.RecordProtection = null; this.NextEpoch.ClientRandom = new byte[Random.Size]; diff --git a/Hazel/Dtls/DtlsUnityConnection.cs b/Hazel/Dtls/DtlsUnityConnection.cs new file mode 100644 index 0000000..b4a71e9 --- /dev/null +++ b/Hazel/Dtls/DtlsUnityConnection.cs @@ -0,0 +1,70 @@ +using Hazel.Udp; +using System.Net; +using System.Security.Cryptography; + +namespace Hazel.Dtls +{ + /// + /// Connects to a UDP-DTLS server + /// + /// + public abstract class DtlsUnityConnection : UnityUdpClientConnection + { + private RSA[] serverPublicKeys; + + /// + /// Create a new instance of the DTLS connection + /// + /// + public DtlsUnityConnection(IPEndPoint remoteEndPoint, IPMode ipMode = IPMode.IPv4) + : base(remoteEndPoint, ipMode) + { + } + + /// + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + } + + /// + /// Set the list of server public keys + /// + /// + /// List of public keys of authentic servers + /// + public void SetPublicKeys(RSA[] serverPublicKeys) + { + if (this.serverPublicKeys != null) + { + foreach (RSA publicKey in this.serverPublicKeys) + { + publicKey?.Dispose(); + } + } + + this.serverPublicKeys = serverPublicKeys; + } + + /// + /// Abort the existing connection and restart the process + /// + protected override void RestartConnection() + { + throw new System.NotImplementedException(); + base.RestartConnection(); + } + + /// + protected override void WriteBytesToConnection(byte[] bytes, int length) + { + throw new System.NotImplementedException(); + } + + /// + protected internal override void HandleReceive(MessageReader message, int bytesReceived) + { + throw new System.NotImplementedException(); + } + } +} diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index e091add..9b15a5a 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -80,6 +80,7 @@ + diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index e28118a..b57b3a4 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net.Sockets; namespace Hazel.Udp @@ -122,7 +122,7 @@ namespace Hazel.Udp /// Handles the receiving of data. /// /// The buffer containing the bytes received. - protected internal void HandleReceive(MessageReader message, int bytesReceived) + protected internal virtual void HandleReceive(MessageReader message, int bytesReceived) { ushort id; switch (message.Buffer[0]) diff --git a/Hazel/Udp/UnityUdpClientConnection.cs b/Hazel/Udp/UnityUdpClientConnection.cs index 2a08859..c5fdcec 100644 --- a/Hazel/Udp/UnityUdpClientConnection.cs +++ b/Hazel/Udp/UnityUdpClientConnection.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net; using System.Net.Sockets; using System.Threading; @@ -32,6 +32,15 @@ namespace Hazel.Udp } public void FixedUpdate() + { + this.ResendPacketsIfNeeded(); + } + + protected virtual void RestartConnection() + { + } + + protected virtual void ResendPacketsIfNeeded() { base.ManageReliablePackets(); } @@ -106,6 +115,8 @@ namespace Hazel.Udp throw new HazelException("A SocketException occurred while binding to the port.", e); } + this.RestartConnection(); + try { StartListeningForData(); @@ -252,4 +263,4 @@ namespace Hazel.Udp base.Dispose(disposing); } } -} \ No newline at end of file +}