123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- #if !(!UNITY_EDITOR&&UNITY_WSA_10_0&&!ENABLE_IL2CPP)
- #if SSL
- #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
- using Microsoft.SPOT.Net.Security;
- #else
- using System.Net.Security;
- using System.Security.Authentication;
- #endif
- #endif
- using System.Net.Sockets;
- using System.Net;
- using System.Security.Cryptography.X509Certificates;
- using System;
- using System.Net.Security;
- using System.Security.Authentication;
- namespace uPLibrary.Networking.M2Mqtt
- {
-
-
-
- public class MqttNetworkChannel : IMqttNetworkChannel
- {
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
- private readonly RemoteCertificateValidationCallback userCertificateValidationCallback;
- private readonly LocalCertificateSelectionCallback userCertificateSelectionCallback;
- #endif
-
- private string remoteHostName;
- private IPAddress remoteIpAddress;
- private int remotePort;
-
- private Socket socket;
-
- private bool secure;
-
- private X509Certificate caCert;
-
- private X509Certificate serverCert;
-
- private X509Certificate clientCert;
-
- private MqttSslProtocols sslProtocol;
-
-
-
- public string RemoteHostName { get { return this.remoteHostName; } }
-
-
-
- public IPAddress RemoteIpAddress { get { return this.remoteIpAddress; } }
-
-
-
- public int RemotePort { get { return this.remotePort; } }
- #if SSL
-
- private SslStream sslStream;
- #if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
- private NetworkStream netStream;
- #endif
- #endif
-
-
-
- public bool DataAvailable
- {
- get
- {
- #if SSL
- #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
- if (secure)
- return this.sslStream.DataAvailable;
- else
- return (this.socket.Available > 0);
- #else
- if (secure)
- return this.netStream.DataAvailable;
- else
- return (this.socket.Available > 0);
- #endif
- #else
- return (this.socket.Available > 0);
- #endif
- }
- }
-
-
-
-
- public MqttNetworkChannel(Socket socket)
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
- : this(socket, false, null, MqttSslProtocols.None, null, null)
- #else
- : this(socket, false, null, MqttSslProtocols.None)
- #endif
- {
- }
-
-
-
-
-
-
-
-
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
-
-
- public MqttNetworkChannel(Socket socket, bool secure, X509Certificate serverCert, MqttSslProtocols sslProtocol,
- RemoteCertificateValidationCallback userCertificateValidationCallback,
- LocalCertificateSelectionCallback userCertificateSelectionCallback)
- #else
- public MqttNetworkChannel(Socket socket, bool secure, X509Certificate serverCert, MqttSslProtocols sslProtocol)
- #endif
- {
- this.socket = socket;
- this.secure = secure;
- this.serverCert = serverCert;
- this.sslProtocol = sslProtocol;
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
- this.userCertificateValidationCallback = userCertificateValidationCallback;
- this.userCertificateSelectionCallback = userCertificateSelectionCallback;
- #endif
- }
-
-
-
-
-
- public MqttNetworkChannel(string remoteHostName, int remotePort)
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
- : this(remoteHostName, remotePort, false, null, null, MqttSslProtocols.None, null, null)
- #else
- : this(remoteHostName, remotePort, false, null, null, MqttSslProtocols.None)
- #endif
- {
- }
-
-
-
-
-
-
-
-
-
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
-
-
- public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol,
- RemoteCertificateValidationCallback userCertificateValidationCallback,
- LocalCertificateSelectionCallback userCertificateSelectionCallback)
- #else
- public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol)
- #endif
- {
- IPAddress remoteIpAddress = null;
- try
- {
-
- remoteIpAddress = IPAddress.Parse(remoteHostName);
- }
- catch
- {
- }
-
- if (remoteIpAddress == null)
- {
- IPHostEntry hostEntry = Dns.GetHostEntry(remoteHostName);
- if ((hostEntry != null) && (hostEntry.AddressList.Length > 0))
- {
-
-
- int i = 0;
- while (hostEntry.AddressList[i] == null) i++;
- remoteIpAddress = hostEntry.AddressList[i];
- }
- else
- {
- throw new Exception("No address found for the remote host name");
- }
- }
- this.remoteHostName = remoteHostName;
- this.remoteIpAddress = remoteIpAddress;
- this.remotePort = remotePort;
- this.secure = secure;
- this.caCert = caCert;
- this.clientCert = clientCert;
- this.sslProtocol = sslProtocol;
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK)
- this.userCertificateValidationCallback = userCertificateValidationCallback;
- this.userCertificateSelectionCallback = userCertificateSelectionCallback;
- #endif
- }
-
-
-
- public void Connect()
- {
- this.socket = new Socket(this.remoteIpAddress.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp);
-
- this.socket.Connect(new IPEndPoint(this.remoteIpAddress, this.remotePort));
- #if SSL
-
- if (secure)
- {
-
- #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
- this.sslStream = new SslStream(this.socket);
- #else
- this.netStream = new NetworkStream(this.socket);
- this.sslStream = new SslStream(this.netStream, false, this.userCertificateValidationCallback, this.userCertificateSelectionCallback);
- #endif
-
- #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
- this.sslStream.AuthenticateAsClient(this.remoteHostName,
- this.clientCert,
- new X509Certificate[] { this.caCert },
- SslVerification.CertificateRequired,
- MqttSslUtility.ToSslPlatformEnum(this.sslProtocol));
- #else
- X509CertificateCollection clientCertificates = null;
-
- if (this.clientCert != null)
- clientCertificates = new X509CertificateCollection(new X509Certificate[] { this.clientCert });
- this.sslStream.AuthenticateAsClient(this.remoteHostName,
- clientCertificates,
- MqttSslUtility.ToSslPlatformEnum(this.sslProtocol),
- false);
-
- #endif
- }
- #endif
- }
-
-
-
-
-
- public int Send(byte[] buffer)
- {
- #if SSL
- if (this.secure)
- {
- this.sslStream.Write(buffer, 0, buffer.Length);
- this.sslStream.Flush();
- return buffer.Length;
- }
- else
- return this.socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
- #else
- return this.socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
- #endif
- }
-
-
-
-
-
- public int Receive(byte[] buffer)
- {
- #if SSL
- if (this.secure)
- {
-
- int idx = 0, read = 0;
- while (idx < buffer.Length)
- {
-
-
- read = this.sslStream.Read(buffer, idx, buffer.Length - idx);
- if (read == 0)
- return 0;
- idx += read;
- }
- return buffer.Length;
- }
- else
- {
-
- int idx = 0, read = 0;
- while (idx < buffer.Length)
- {
-
-
- read = this.socket.Receive(buffer, idx, buffer.Length - idx, SocketFlags.None);
- if (read == 0)
- return 0;
- idx += read;
- }
- return buffer.Length;
- }
- #else
-
- int idx = 0, read = 0;
- while (idx < buffer.Length)
- {
-
-
- read = this.socket.Receive(buffer, idx, buffer.Length - idx, SocketFlags.None);
- if (read == 0)
- return 0;
- idx += read;
- }
- return buffer.Length;
- #endif
- }
-
-
-
-
-
-
- public int Receive(byte[] buffer, int timeout)
- {
-
- if (this.socket.Poll(timeout * 1000, SelectMode.SelectRead))
- {
- return this.Receive(buffer);
- }
- else
- {
- return 0;
- }
- }
-
-
-
- public void Close()
- {
- #if SSL
- if (this.secure)
- {
- #if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
- this.netStream.Close();
- #endif
- this.sslStream.Close();
- }
- this.socket.Close();
- #else
- this.socket.Close();
- #endif
- }
-
-
-
- public void Accept()
- {
- #if SSL
-
- if (secure)
- {
- #if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
- this.netStream = new NetworkStream(this.socket);
- this.sslStream = new SslStream(this.netStream, false, this.userCertificateValidationCallback, this.userCertificateSelectionCallback);
- this.sslStream.AuthenticateAsServer(this.serverCert, false, MqttSslUtility.ToSslPlatformEnum(this.sslProtocol), false);
- #endif
- }
- return;
- #else
- return;
- #endif
- }
- }
-
-
-
- public static class IPAddressUtility
- {
-
-
-
-
-
- public static AddressFamily GetAddressFamily(this IPAddress ipAddress)
- {
- #if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
- return ipAddress.AddressFamily;
- #else
- return (ipAddress.ToString().IndexOf(':') != -1) ?
- AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
- #endif
- }
- }
-
-
-
- public static class MqttSslUtility
- {
- #if (UNITY_EDITOR || !NET_4_6)
- public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol)
- {
- switch (mqttSslProtocol)
- {
- case MqttSslProtocols.None:
- return SslProtocols.None;
- case MqttSslProtocols.SSLv3:
- return SslProtocols.Ssl3;
- case MqttSslProtocols.TLSv1_0:
- return SslProtocols.Tls;
- case MqttSslProtocols.TLSv1_1:
- case MqttSslProtocols.TLSv1_2:
- default:
- throw new ArgumentException("SSL/TLS protocol version not supported");
- }
- }
- #elif (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !COMPACT_FRAMEWORK)
- public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol)
- {
- switch (mqttSslProtocol)
- {
- case MqttSslProtocols.None:
- return SslProtocols.None;
- case MqttSslProtocols.SSLv3:
- return SslProtocols.Ssl3;
- case MqttSslProtocols.TLSv1_0:
- return SslProtocols.Tls;
- case MqttSslProtocols.TLSv1_1:
- return SslProtocols.Tls11;
- case MqttSslProtocols.TLSv1_2:
- return SslProtocols.Tls12;
- default:
- throw new ArgumentException("SSL/TLS protocol version not supported");
- }
- }
- #elif (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
- public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol)
- {
- switch (mqttSslProtocol)
- {
- case MqttSslProtocols.None:
- return SslProtocols.None;
- case MqttSslProtocols.SSLv3:
- return SslProtocols.SSLv3;
- case MqttSslProtocols.TLSv1_0:
- return SslProtocols.TLSv1;
- case MqttSslProtocols.TLSv1_1:
- case MqttSslProtocols.TLSv1_2:
- default:
- throw new ArgumentException("SSL/TLS protocol version not supported");
- }
- }
- #endif
- }
- }
- #endif
|