Пример #1
0
        internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCallback callback, object state, string host, int port)
        {
            ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, host, port, outerResult, callback, state);

            result.GetConnection();
            return(result);
        }
 static void SendStartTlsCallback(IAsyncResult result)     //7
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             StartTlsCommand.EndSend(result);
             TlsStream TlsStream = new TlsStream(
                 thisPtr.connection.pooledStream.ServicePoint.Host,
                 thisPtr.connection.pooledStream.NetworkStream,
                 ServicePointManager.CheckCertificateRevocationList,
                 (SslProtocols)ServicePointManager.SecurityProtocol,
                 thisPtr.connection.ClientCertificates,
                 thisPtr.connection.pooledStream.ServicePoint,
                 thisPtr.connection.client, thisPtr.m_OuterResult.ContextCopy);
             thisPtr.connection.pooledStream.NetworkStream = TlsStream;
             thisPtr.connection.responseReader             = new SmtpReplyReaderFactory(thisPtr.connection.pooledStream.NetworkStream);
             thisPtr.SendEHello();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Пример #3
0
 private static void AuthenticateContinueCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             LineInfo info = AuthCommand.EndSend(result);
             if ((int)info.StatusCode == 235)
             {
                 thisPtr._connection._authenticationModules[thisPtr._currentModule].CloseContext(thisPtr._connection);
                 thisPtr._connection._isConnected = true;
                 thisPtr.InvokeCallback();
                 return;
             }
             else if ((int)info.StatusCode == 334)
             {
                 thisPtr._authResponse = info.Line;
                 if (!thisPtr.AuthenticateContinue())
                 {
                     return;
                 }
             }
             thisPtr.Authenticate();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Пример #4
0
 private static void HandshakeCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             try
             {
                 LineInfo info = thisPtr._connection.Reader.CurrentReader.EndReadLine(result);
                 if (info.StatusCode != SmtpStatusCode.ServiceReady)
                 {
                     thisPtr.InvokeCallback(new SmtpException(info.StatusCode, info.Line, true));
                     return;
                 }
                 if (!thisPtr.SendEHello())
                 {
                     return;
                 }
             }
             catch (SmtpException)
             {
                 if (!thisPtr.SendHello())
                 {
                     return;
                 }
             }
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Пример #5
0
            internal static void End(IAsyncResult result)
            {
                ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result;
                object connectResult = thisPtr.InternalWaitForCompletion();

                if (connectResult is Exception e)
                {
                    ExceptionDispatchInfo.Throw(e);
                }
            }
Пример #6
0
            internal static void End(IAsyncResult result)
            {
                ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result;
                object connectResult = thisPtr.InternalWaitForCompletion();

                if (connectResult is Exception)
                {
                    throw (Exception)connectResult;
                }
            }
Пример #7
0
            private static void ConnectionCreatedCallback(object request, object state)
            {
                GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::ConnectionCreatedCallback");
                ConnectAndHandshakeAsyncResult ConnectAndHandshakeAsyncResult = (ConnectAndHandshakeAsyncResult)request;

                if (state is Exception)
                {
                    ConnectAndHandshakeAsyncResult.InvokeCallback((Exception)state);
                    return;
                }
                SmtpPooledStream pooledStream = (SmtpPooledStream)(PooledStream)state;


                try
                {
                    while (pooledStream.creds != null && pooledStream.creds != ConnectAndHandshakeAsyncResult.connection.credentials)
                    {
                        GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::Connect pooledStream has wrong creds " + ValidationHelper.HashString(pooledStream));
                        ConnectAndHandshakeAsyncResult.connection.connectionPool.PutConnection(pooledStream,
                                                                                               pooledStream.Owner, ConnectAndHandshakeAsyncResult.connection.Timeout, false);
                        pooledStream = (SmtpPooledStream)ConnectAndHandshakeAsyncResult.connection.connectionPool.GetConnection((object)ConnectAndHandshakeAsyncResult, ConnectAndHandshakeAsyncResult.m_ConnectionCreatedCallback, ConnectAndHandshakeAsyncResult.connection.Timeout);
                        if (pooledStream == null)
                        {
                            GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::Connect returning asynchronously");
                            return;
                        }
                    }
                    if (Logging.On)
                    {
                        Logging.Associate(Logging.Web, ConnectAndHandshakeAsyncResult.connection, pooledStream);
                    }
                    pooledStream.Owner = ConnectAndHandshakeAsyncResult.connection; //needs to be updated for gc reasons
                    pooledStream.creds = ConnectAndHandshakeAsyncResult.connection.credentials;


                    lock (ConnectAndHandshakeAsyncResult.connection) {
                        //if we were cancelled while getting the connection, we should close and return
                        if (ConnectAndHandshakeAsyncResult.connection.isClosed)
                        {
                            ConnectAndHandshakeAsyncResult.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, ConnectAndHandshakeAsyncResult.connection.Timeout, false);
                            GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::ConnectionCreatedCallback Connect was aborted " + ValidationHelper.HashString(pooledStream));
                            ConnectAndHandshakeAsyncResult.InvokeCallback(null);
                            return;
                        }
                        ConnectAndHandshakeAsyncResult.connection.pooledStream = pooledStream;
                    }

                    ConnectAndHandshakeAsyncResult.Handshake();
                }
                catch (Exception e)
                {
                    ConnectAndHandshakeAsyncResult.InvokeCallback(e);
                }
                GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::ConnectionCreatedCallback pooledStream#" + ValidationHelper.HashString(pooledStream));
            }
Пример #8
0
 private static void SendStartTlsCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             StartTlsCommand.EndSend(result);
             thisPtr.TlsStreamAuthenticate();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Пример #9
0
 static void SendHelloCallback(IAsyncResult result)     //7
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             HelloCommand.EndSend(result);
             thisPtr.Authenticate();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Пример #10
0
 private static void TlsStreamAuthenticateCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             (thisPtr._connection._networkStream as TlsStream).EndAuthenticateAsClient(result);
             thisPtr._connection._responseReader = new SmtpReplyReaderFactory(thisPtr._connection._networkStream);
             thisPtr.SendEHello();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Пример #11
0
            private static void ConnectionCreatedCallback(object request, object state)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Enter(null, request);
                }
                ConnectAndHandshakeAsyncResult ConnectAndHandshakeAsyncResult = (ConnectAndHandshakeAsyncResult)request;

                if (state is Exception)
                {
                    ConnectAndHandshakeAsyncResult.InvokeCallback((Exception)state);
                    return;
                }

                try
                {
                    lock (ConnectAndHandshakeAsyncResult._connection)
                    {
                        //if we were cancelled while getting the connection, we should close and return
                        if (ConnectAndHandshakeAsyncResult._connection._isClosed)
                        {
                            ConnectAndHandshakeAsyncResult._connection.ReleaseConnection();
                            if (NetEventSource.IsEnabled)
                            {
                                NetEventSource.Info(null, $"Connect was aborted: {request}");
                            }
                            ConnectAndHandshakeAsyncResult.InvokeCallback(null);
                            return;
                        }
                    }

                    ConnectAndHandshakeAsyncResult.Handshake();
                }
                catch (Exception e)
                {
                    ConnectAndHandshakeAsyncResult.InvokeCallback(e);
                }

                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Exit(null, request);
                }
            }
Пример #12
0
            private static void ConnectionCreatedCallback(object request, object state)
            {
                if (GlobalLog.IsEnabled)
                {
                    GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(request) + "::ConnectionCreatedCallback");
                }
                ConnectAndHandshakeAsyncResult ConnectAndHandshakeAsyncResult = (ConnectAndHandshakeAsyncResult)request;

                if (state is Exception)
                {
                    ConnectAndHandshakeAsyncResult.InvokeCallback((Exception)state);
                    return;
                }

                try
                {
                    lock (ConnectAndHandshakeAsyncResult._connection)
                    {
                        //if we were cancelled while getting the connection, we should close and return
                        if (ConnectAndHandshakeAsyncResult._connection._isClosed)
                        {
                            ConnectAndHandshakeAsyncResult._connection.ReleaseConnection();
                            if (GlobalLog.IsEnabled)
                            {
                                GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(request) + "::ConnectionCreatedCallback Connect was aborted ");
                            }
                            ConnectAndHandshakeAsyncResult.InvokeCallback(null);
                            return;
                        }
                    }

                    ConnectAndHandshakeAsyncResult.Handshake();
                }
                catch (Exception e)
                {
                    ConnectAndHandshakeAsyncResult.InvokeCallback(e);
                }

                if (GlobalLog.IsEnabled)
                {
                    GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(request) + "::ConnectionCreatedCallback");
                }
            }
        internal IAsyncResult BeginGetConnection(ServicePoint servicePoint, ContextAwareResult outerResult, AsyncCallback callback, object state)
        {
            if (Logging.On)
            {
                Logging.Associate(Logging.Web, this, servicePoint);
            }
            if ((this.EnableSsl && (this.ClientCertificates != null)) && (this.ClientCertificates.Count > 0))
            {
                this.connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, this.ClientCertificates.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), m_CreateConnectionCallback);
            }
            else
            {
                this.connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, "", m_CreateConnectionCallback);
            }
            ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, servicePoint.Host, servicePoint.Port, outerResult, callback, state);

            result.GetConnection(false);
            return(result);
        }
Пример #14
0
            private static void InitializeConnectionCallback(IAsyncResult result)
            {
                if (!result.CompletedSynchronously)
                {
                    ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
                    try
                    {
                        thisPtr._connection.EndInitializeConnection(result);
                        if (NetEventSource.IsEnabled)
                        {
                            NetEventSource.Info(null, $"Connect returned {thisPtr}");
                        }

                        thisPtr.Handshake();
                    }
                    catch (Exception e)
                    {
                        thisPtr.InvokeCallback(e);
                    }
                }
            }
Пример #15
0
            private static void InitializeConnectionCallback(IAsyncResult result)
            {
                if (!result.CompletedSynchronously)
                {
                    ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
                    thisPtr._connection.EndInitializeConnection(result);
                    if (GlobalLog.IsEnabled)
                    {
                        GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(thisPtr) + "::Connect returned" + LoggingHash.HashString(thisPtr));
                    }

                    try
                    {
                        thisPtr.Handshake();
                    }
                    catch (Exception e)
                    {
                        thisPtr.InvokeCallback(e);
                    }
                }
            }
Пример #16
0
            private static void SendEHelloCallback(IAsyncResult result)
            {
                if (!result.CompletedSynchronously)
                {
                    ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
                    try
                    {
                        try
                        {
                            thisPtr._connection._extensions = EHelloCommand.EndSend(result);
                            thisPtr._connection.ParseExtensions(thisPtr._connection._extensions);

                            // If we already have a SSlStream, this is the second EHLO cmd
                            // that we sent after TLS handshake compelted. So skip TLS and
                            // continue with Authenticate.
                            if (thisPtr._connection._networkStream is TlsStream)
                            {
                                thisPtr.Authenticate();
                                return;
                            }
                        }

                        catch (SmtpException e)
                        {
                            if ((e.StatusCode != SmtpStatusCode.CommandUnrecognized) &&
                                (e.StatusCode != SmtpStatusCode.CommandNotImplemented))
                            {
                                throw;
                            }

                            if (!thisPtr.SendHello())
                            {
                                return;
                            }
                        }


                        if (thisPtr._connection.EnableSsl)
                        {
                            if (!thisPtr._connection._serverSupportsStartTls)
                            {
                                // Either TLS is already established or server does not support TLS
                                if (!(thisPtr._connection._networkStream is TlsStream))
                                {
                                    throw new SmtpException(SR.MailServerDoesNotSupportStartTls);
                                }
                            }

                            thisPtr.SendStartTls();
                        }
                        else
                        {
                            thisPtr.Authenticate();
                        }
                    }
                    catch (Exception e)
                    {
                        thisPtr.InvokeCallback(e);
                    }
                }
            }
 internal IAsyncResult BeginGetConnection(ServicePoint servicePoint, ContextAwareResult outerResult, AsyncCallback callback, object state)
 {
     if (Logging.On)
     {
         Logging.Associate(Logging.Web, this, servicePoint);
     }
     if ((this.EnableSsl && (this.ClientCertificates != null)) && (this.ClientCertificates.Count > 0))
     {
         this.connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, this.ClientCertificates.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), m_CreateConnectionCallback);
     }
     else
     {
         this.connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, "", m_CreateConnectionCallback);
     }
     ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, servicePoint.Host, servicePoint.Port, outerResult, callback, state);
     result.GetConnection(false);
     return result;
 }
Пример #18
0
 internal void EndGetConnection(IAsyncResult result)
 {
     ConnectAndHandshakeAsyncResult.End(result);
 }
Пример #19
0
 internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCallback callback, object state, string host, int port)
 {
     ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, host, port, outerResult, callback, state);
     result.GetConnection();
     return result;
 }
Пример #20
0
        internal IAsyncResult BeginGetConnection(ServicePoint servicePoint, ContextAwareResult outerResult, AsyncCallback callback, object state)
        {
            if (Logging.On) Logging.Associate(Logging.Web, this, servicePoint);
            Debug.Assert(servicePoint != null, "servicePoint was null from SmtpTransport");

            if (EnableSsl && ClientCertificates != null && ClientCertificates.Count > 0)
                connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, ClientCertificates.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), m_CreateConnectionCallback);
            else
                connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, "", m_CreateConnectionCallback);

            ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, servicePoint.Host, servicePoint.Port, outerResult, callback, state);
            result.GetConnection(false);
            return result;
        }