Exemplo n.º 1
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);
         }
     }
 }
Exemplo n.º 2
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);
         }
     }
 }
Exemplo n.º 3
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));
            }
 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);
         }
     }
 }
Exemplo n.º 5
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");
                }
            }
Exemplo n.º 6
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);
                }
            }
Exemplo n.º 7
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);
         }
     }
 }
Exemplo n.º 8
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);
         }
     }
 }
Exemplo n.º 9
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);
         }
     }
 }
Exemplo n.º 10
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);
                    }
                }
            }
Exemplo n.º 11
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);
                    }
                }
            }
Exemplo n.º 12
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);
                    }
                }
            }