MailServer/MailServer/IMAP/Server/IMAP_Server/IMAP_Server.Session.cs
2010-07-10 13:13:50 +00:00

58 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MailServer.IMAP.Server
{
public partial class IMAP_Server
{
/// <summary>
/// Initialize and start new session here. Session isn't added to session list automatically,
/// session must add itself to server session list by calling AddSession().
/// </summary>
/// <param name="socket">Connected client socket.</param>
/// <param name="bindInfo">BindInfo what accepted socket.</param>
protected override void InitNewSession(Socket socket, IPBindInfo bindInfo)
{
// Check maximum conncurent connections from 1 IP.
if (m_MaxConnectionsPerIP > 0)
{
lock (this.Sessions)
{
int nSessions = 0;
foreach (SocketServerSession s in this.Sessions)
{
IPEndPoint ipEndpoint = s.RemoteEndPoint;
if (ipEndpoint != null)
{
if (ipEndpoint.Address.Equals(((IPEndPoint)socket.RemoteEndPoint).Address))
{
nSessions++;
}
}
// Maimum allowed exceeded
if (nSessions >= m_MaxConnectionsPerIP)
{
socket.Send(System.Text.Encoding.ASCII.GetBytes("* NO Maximum connections from your IP address is exceeded, try again later !\r\n"));
socket.Shutdown(SocketShutdown.Both);
socket.Close();
return;
}
}
}
}
string sessionID = Guid.NewGuid().ToString();
SocketEx socketEx = new SocketEx(socket);
if (LogCommands)
{
socketEx.Logger = new SocketLogger(socket, this.SessionLog);
socketEx.Logger.SessionID = sessionID;
}
IMAP_Session session = new IMAP_Session(sessionID, socketEx, bindInfo, this);
}
}
}