Ejemplo n.º 1
0
        private bool GetSslData(int socketIndex, out SslStreamData sslData)
        {
            sslData = null;

            lock (_sslStreams)
            {
                if (!_sslStreams.ContainsKey(socketIndex))
                {
                    return(false);
                }

                sslData = _sslStreams[socketIndex];
            }
            return(true);
        }
Ejemplo n.º 2
0
        public int Connect(int socket, IntPtr szTargetHost, int sslContextHandle)
        {
            SslStreamData ssd;

            if (GetSslData(socket, out ssd))
            {
                switch (ssd.m_state)
                {
                case SslStreamData.SslAsyncState.Processing:
                    return((int)SocketError.WouldBlock);

                case SslStreamData.SslAsyncState.Finished:
                    ssd.m_state = SslStreamData.SslAsyncState.InUse;
                    _socketsDriver.SetSslSocket(socket);
                    return((int)SocketError.Success);

                case SslStreamData.SslAsyncState.Failed:
                    ssd.m_state = SslStreamData.SslAsyncState.Init;
                    Console.Error.WriteLine("Connect Failed...");
                    return((int)SocketError.SocketError);

                case SslStreamData.SslAsyncState.InUse:
                    Console.Error.WriteLine("Connect InUse...");
                    return((int)SocketError.Success);
                }
            }
            else
            {
                ssd = new SslStreamData();

                lock (_sslStreams)
                {
                    _sslStreams[socket] = ssd;
                }
            }

            ssd.m_state = SslStreamData.SslAsyncState.Processing;

            try
            {
                Socket sock = null;

                if (!_socketsDriver.GetSocket(socket, out sock))
                {
                    return((int)SocketError.SocketError);
                }

                NetworkStream ns     = new NetworkStream(sock);
                SslStream     stream = new SslStream(ns, true);

                ssd.m_stream = stream;

                SslData sd = _sslDataCollection[sslContextHandle];

                string targHost = Marshal.PtrToStringAnsi(szTargetHost);

                X509CertificateCollection certs = new X509CertificateCollection();

                if (sd.m_cert != null)
                {
                    certs.Add(sd.m_cert);
                }

                IAsyncResult iar = stream.BeginAuthenticateAsClient(targHost, certs, sd.m_sslVersion, false, new AsyncCallback(EndSslConnect), socket);
            }
            catch (Exception ae)
            {
                ssd.m_state = SslStreamData.SslAsyncState.Init;
                Console.Error.WriteLine("BeginAuthenticateAsClient " + ae.Message);
                return((int)SocketError.SocketError);
            }

            return((int)SocketError.WouldBlock);
        }
Ejemplo n.º 3
0
        public int Accept(int socket, int sslContextHandle)
        {
            SslStreamData ssd;

            if (GetSslData(socket, out ssd))
            {
                switch (ssd.m_state)
                {
                case SslStreamData.SslAsyncState.Processing:
                    return((int)SocketError.WouldBlock);

                case SslStreamData.SslAsyncState.Finished:
                    ssd.m_state = SslStreamData.SslAsyncState.InUse;
                    _socketsDriver.SetSslSocket(socket);
                    return((int)SocketError.Success);

                case SslStreamData.SslAsyncState.Failed:
                    ssd.m_state = SslStreamData.SslAsyncState.Init;
                    Console.Error.WriteLine("Accept Failed...");
                    return((int)SocketError.SocketError);

                case SslStreamData.SslAsyncState.InUse:
                    Console.Error.WriteLine("Accept InUse...");
                    return((int)SocketError.SocketError);
                }
            }
            else
            {
                ssd = new SslStreamData();

                lock (_sslStreams)
                {
                    _sslStreams[socket] = ssd;
                }
            }

            ssd.m_state = SslStreamData.SslAsyncState.Processing;

            try
            {
                Socket sock = null;

                if (!_socketsDriver.GetSocket(socket, out sock))
                {
                    return((int)SocketError.SocketError);
                }

                NetworkStream ns     = new NetworkStream(sock);
                SslStream     stream = new SslStream(ns, true);

                ssd.m_stream = stream;

                SslData sd = _sslDataCollection[sslContextHandle];

                stream.BeginAuthenticateAsServer(sd.m_cert, sd.m_sslVerify == SslVerification.CertificateRequired, sd.m_sslVersion, false, new AsyncCallback(EndSslAccept), socket);
            }
            catch (Exception ae)
            {
                ssd.m_state = SslStreamData.SslAsyncState.Init;
                Console.Error.WriteLine("BeginAuthenticateAsServer " + ae.Message);
                return((int)SocketError.SocketError);
            }

            return((int)SocketError.WouldBlock);
        }