Exemplo n.º 1
0
        private void ProcessClient(TcpClient client, TcpListener listener, ScopedActivity activity)
        {
            ListenerInfo info = _ListenerInfos[listener];
            try
            {
                info.Clients.Add(client);

                //gather some metrics...
                activity.Log("Client: {0}", client.Client.RemoteEndPoint);
                activity.Log("Active Connections for Listener: {0}", info.Clients.Count);

                //run validation checks...
                if (ValidateClient(client, activity))
                {
                    //process client
                    activity.Log("Exchanging Commands");
                    using (SmtpStreamProcessor clientProcessor = new SmtpStreamProcessor(client.GetStream(), info.SecurityMode, ServerCertificate))
                    {
                        clientProcessor.Process();
                    }
                }
            }
            finally
            {
                info.Clients.Remove(client);
                client.Close();
            }
        }
Exemplo n.º 2
0
 private bool ValidateClient(TcpClient client, ScopedActivity activity)
 {
     bool retVal = false;
     activity.Log("Checking if client IP Address is black listed");
     IPEndPoint clientEndpoint = (IPEndPoint)client.Client.RemoteEndPoint;
     if (!BlackListedIpAddresses.Contains(clientEndpoint.Address))
     {
         //address is not black listed
         activity.Log("Address {0} is allowed, client passes validation checks", clientEndpoint.Address);
         retVal = true;
     }
     else
     {
         //address is black listed
         activity.Log("Not Processing Client, Client IP Address is black listed: {0}", clientEndpoint.Address);
     }
     return retVal;
 }
Exemplo n.º 3
0
        private void AcceptClient(IAsyncResult result)
        {
            using(ScopedActivity localActivity = new ScopedActivity("Accepting Client Connection"))
            {
                TcpClient client = null;
                TcpListener listener = (TcpListener)result.AsyncState;

                //this section is critical for the robustness of the application.
                //it should only accept the client and begin accepting new connections
                //in the finally block
                try
                {
                    localActivity.Log("Listener: {0}", listener.LocalEndpoint);
                    client = listener.EndAcceptTcpClient(result);
                }
                catch (Exception ex)
                {
                    localActivity.LogException(ex, "An exception occurred while accepting client connection");
                }
                finally
                {
                    listener.BeginAcceptTcpClient(AcceptClient, listener);
                }

                //if client was successfully accepted, continue the processing chain
                if (client != null)
                {
                    ProcessClient(client, listener, localActivity);
                }
            }
        }