Esempio n. 1
0
        public void Start()
        {
            if (System.Threading.Interlocked.CompareExchange(ref _Running, 1, 0) > 0)
            {
                return;
            }

            if (_LocalEndPoint == null)
            {
                try
                {
                    _Client = CreateClient(new IPEndPoint(IPAddress.IPv6Any, _Port));
                    // _ReceivePacketSize + 1); // +1 to check for > ReceivePacketSize
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.AddressFamilyNotSupported)
                    {
                        _Client = null;
                    }
                    else
                    {
                        throw e;
                    }
                }

                if (_Client == null)
                {
                    // IPv6 is not supported, use IPv4 instead
                    _Client = CreateClient(new IPEndPoint(IPAddress.Any, _Port));
                }
                else
                {
                    _ClientIPv4 = CreateClient(new IPEndPoint(IPAddress.Any, _Port));
                }
            }
            else
            {
                _Client = CreateClient(_LocalEndPoint);
            }
        }
Esempio n. 2
0
        private DTLS.Client CreateClient(EndPoint endPoint)
        {
            DTLS.Client result = new DTLS.Client(endPoint, _SupportedCipherSuites);

            if (!string.IsNullOrEmpty(_PSKIdentity) && !string.IsNullOrEmpty(_PSKSecret))
            {
                _SupportedCipherSuites.Clear();
                _SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CCM_8);
                _SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256);
                _SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
                Console.WriteLine("Using PSK Identity: " + _PSKIdentity);

                result.PSKIdentities.AddIdentity(System.Text.Encoding.UTF8.GetBytes(_PSKIdentity), StringUtils.HexStringToByteArray(_PSKSecret));
            }
            else if (!string.IsNullOrEmpty(_CertificateFile))
            {
                _SupportedCipherSuites.Clear();
                _SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
                _SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256);
                Console.WriteLine("Using Certificate: " + _CertificateFile);
                result.LoadCertificateFromPem(_CertificateFile);
            }
            else
            {
                Console.WriteLine("No Certificate or PSK supplied.");
            }

            result.DataReceived += new DTLS.Client.DataReceivedEventHandler(FireDataReceived);
            //if (_ReceiveBufferSize > 0)
            //{
            //    result.ReceiveBufferSize = _ReceiveBufferSize;
            //}
            //if (_SendBufferSize > 0)
            //{
            //    result.SendBufferSize = _SendBufferSize;
            //}
            return(result);
        }
        private DTLS.Client CreateClient(EndPoint endPoint)
        {
            DTLS.Client result = new DTLS.Client(endPoint, _SupportedCipherSuites);

            if (!string.IsNullOrEmpty(_PSKIdentity) && !string.IsNullOrEmpty(_PSKSecret))
            {
                _SupportedCipherSuites.Clear();
                _SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CCM_8);
                _SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256);
                _SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
                Console.WriteLine("Using PSK Identity: " + _PSKIdentity);

                result.PSKIdentities.AddIdentity(System.Text.Encoding.UTF8.GetBytes(_PSKIdentity), StringUtils.HexStringToByteArray(_PSKSecret));
            }
            else if (!string.IsNullOrEmpty(_CertificateFile))
            {
                _SupportedCipherSuites.Clear();
                _SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
                _SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256);
                Console.WriteLine("Using Certificate: " + _CertificateFile);
                result.LoadCertificateFromPem(_CertificateFile);
            }
            else
            {
                Console.WriteLine("No Certificate or PSK supplied.");
            }

            result.DataReceived += new DTLS.Client.DataReceivedEventHandler(FireDataReceived);
            //if (_ReceiveBufferSize > 0)
            //{
            //    result.ReceiveBufferSize = _ReceiveBufferSize;
            //}
            //if (_SendBufferSize > 0)
            //{
            //    result.SendBufferSize = _SendBufferSize;
            //}
            return result;
        }
        public void Stop()
        {
            if (System.Threading.Interlocked.Exchange(ref _Running, 0) == 0)
                return;

            if (_Client != null)
            {
                _Client.Stop();
                _Client = null;
            }
            if (_ClientIPv4 != null)
            {
                _ClientIPv4.Stop();
                _ClientIPv4 = null;
            }
        }
        public void Start()
        {
            if (System.Threading.Interlocked.CompareExchange(ref _Running, 1, 0) > 0)
                return;

            if (_LocalEndPoint == null)
            {
                try
                {
                    _Client = CreateClient(new IPEndPoint(IPAddress.IPv6Any, _Port));
                    // _ReceivePacketSize + 1); // +1 to check for > ReceivePacketSize
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.AddressFamilyNotSupported)
                        _Client = null;
                    else
                        throw e;
                }

                if (_Client == null)
                {
                    // IPv6 is not supported, use IPv4 instead
                    _Client = CreateClient(new IPEndPoint(IPAddress.Any, _Port));
                }
                else
                {
                    _ClientIPv4 = CreateClient(new IPEndPoint(IPAddress.Any, _Port));
                }
            }
            else
            {
                _Client = CreateClient(_LocalEndPoint);
            }
        }
Esempio n. 6
-1
        public void Send(byte[] data, System.Net.EndPoint ep)
        {
            DTLS.Client socket   = _Client;
            IPEndPoint  remoteEP = (IPEndPoint)ep;

            if (remoteEP.AddressFamily == AddressFamily.InterNetwork)
            {
                if (_ClientIPv4 != null)
                {
                    // use the separated socket of IPv4 to deal with IPv4 conversions.
                    socket = _ClientIPv4;
                }
                else if (_Client.LocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    remoteEP = new IPEndPoint(IPAddressExtensions.MapToIPv6(remoteEP.Address), remoteEP.Port);
                }
            }
            if (!_Connected)
            {
                socket.ConnectToServer(remoteEP);
                _Connected = true;
            }

            socket.Send(data);
        }
Esempio n. 7
-1
        public void Stop()
        {
            if (System.Threading.Interlocked.Exchange(ref _Running, 0) == 0)
            {
                return;
            }

            if (_Client != null)
            {
                _Client.Stop();
                _Client = null;
            }
            if (_ClientIPv4 != null)
            {
                _ClientIPv4.Stop();
                _ClientIPv4 = null;
            }
        }