// dump the current settings private static void dumpSettings() { // base/network AppGlobals.writeConsole("Host name..................: {0}", AppGlobals.hostName); AppGlobals.writeConsole("listen IP..................: {0}", AppGlobals.listenAddress); AppGlobals.writeConsole("listen port................: {0}", AppGlobals.listenPort); AppGlobals.writeConsole("Receive timeout............: {0}", AppGlobals.receiveTimeout); // hardlimits AppGlobals.writeConsole("Max errors.................: {0}", AppGlobals.maxSmtpErr); AppGlobals.writeConsole("Max NOOP...................: {0}", AppGlobals.maxSmtpNoop); AppGlobals.writeConsole("Max VRFY/EXPN..............: {0}", AppGlobals.maxSmtpVrfy); AppGlobals.writeConsole("Max RCPT TO................: {0}", AppGlobals.maxSmtpRcpt); // sessions AppGlobals.writeConsole("Max messages per session...: {0}", AppGlobals.maxMessages); AppGlobals.writeConsole("Max parallel sessions......: {0}", AppGlobals.maxSessions); // messages AppGlobals.writeConsole("Store message data.........: {0}", AppGlobals.storeData); AppGlobals.writeConsole("Storage path...............: {0}", AppGlobals.storePath); AppGlobals.writeConsole("Max message size...........: {0}", AppGlobals.maxDataSize); // logs AppGlobals.writeConsole("Logfiles path..............: {0}", AppGlobals.logPath); AppGlobals.writeConsole("Verbose logging............: {0}", AppGlobals.logVerbose); // tarpitting AppGlobals.writeConsole("Initial banner delay.......: {0}", AppGlobals.bannerDelay); AppGlobals.writeConsole("Error delay................: {0}", AppGlobals.errorDelay); // filtering/rejecting AppGlobals.writeConsole("Do tempfail (4xx) on DATA..: {0}", AppGlobals.doTempFail); AppGlobals.writeConsole("Check for early talkers....: {0}", AppGlobals.earlyTalkers); // DNS filtering AppGlobals.writeConsole("DNS Whitelists.............: {0}", AppGlobals.whiteLists.Length); AppGlobals.writeConsole("DNS Blacklists.............: {0}", AppGlobals.blackLists.Length); // local domains/mailboxes AppGlobals.writeConsole("Local domains..............: {0}", AppGlobals.LocalDomains.Count); AppGlobals.writeConsole("Local mailboxes............: {0}", AppGlobals.LocalMailBoxes.Count); }
// closes the socket, terminates the session private void closeSession() { if (null != this._client) { if (this._client.Connected) { sleepDown(25); } try { this._client.Close(); this._client = null; } catch { } if (!string.IsNullOrEmpty(this._clientIP)) { AppGlobals.writeConsole("client {0} disconnected, sess={1}, ID={2}.", this._clientIP, this._sessCount, this._sessionID); } } this._initOk = false; long sesscount = AppGlobals.removeSession(); resetSession(); }
private string _mailDom = null; // domain part of a mail address #endregion #region "instance" // init public SMTPsession(TcpClient client) { try { this._sessCount = AppGlobals.addSession(); this._sessionID = AppGlobals.sessionID(); this._hostName = AppGlobals.hostName; if (null != AppGlobals.LocalDomains) { this._mailDomains = AppGlobals.LocalDomains; } if (null != AppGlobals.LocalMailBoxes) { this._mailBoxes = AppGlobals.LocalMailBoxes; } this._client = client; this._clientIP = this._client.Client.RemoteEndPoint.ToString(); int i = this._clientIP.IndexOf(':'); if (-1 != i) { this._clientIP = this._clientIP.Substring(0, i); } this._client.ReceiveTimeout = AppGlobals.receiveTimeout; this._stream = this._client.GetStream(); this._reader = new StreamReader(this._stream); this._writer = new StreamWriter(this._stream); this._writer.NewLine = "\r\n"; this._writer.AutoFlush = true; AppGlobals.writeConsole("client {0} connected, sess={1}, ID={2}.", this._clientIP, this._sessCount, this._sessionID); this._initOk = true; } catch (Exception ex) { AppGlobals.writeConsole("SMTPsession::Exception: " + ex.Message); closeSession(); } }
// main entry point static int Main(string[] args) { // our internal stuff IPAddress listenAddr = IPAddress.Loopback; int listenPort = 25; int retCode = 0; // load the config loadConfig(); // tell we're starting up and, if verbose, dump config parameters AppGlobals.writeConsole("{0} {1} starting up (NET {2})", AppGlobals.appName, AppGlobals.appVersion, AppGlobals.appRuntime); if (AppGlobals.logVerbose) { dumpSettings(); } // setup the listening IP:port listenAddr = AppGlobals.listenIP; listenPort = AppGlobals.listenPort; // try starting the listener try { listener = new TcpListener(listenAddr, listenPort); listener.Start(); } catch (Exception ex) { AppGlobals.writeConsole("Listener::Error: " + ex.Message); return(1); } // tell we're ready to accept connections AppGlobals.writeConsole("Listening for connections on {0}:{1}", listenAddr, listenPort); // run until interrupted (Ctrl-C in our case) while (!timeToStop) { try { // wait for an incoming connection, accept it and spawn a thread to handle it SMTPsession handler = new SMTPsession(listener.AcceptTcpClient()); Thread thread = new System.Threading.Thread(new ThreadStart(handler.handleSession)); thread.Start(); } catch (Exception ex) { // we got an error retCode = 2; AppGlobals.writeConsole("Handler::Error: " + ex.Message); timeToStop = true; } } // finalize if (null != listener) { try { listener.Stop(); } catch { } } return(retCode); }