From ddf1c09acb31b01ed71c70a53eff44ec2c7ba573 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Tue, 22 Nov 2016 23:00:18 +0100 Subject: [PATCH] MTA + MessageQuerry DNSResolver Projkect added --- SMTPServer/DNSResolver/DNSResolver.csproj | 25 +++++++ SMTPServer/DNSResolver/Program.cs | 9 +++ SMTPServer/SMTPServer.sln | 9 +++ .../Exceptions/NoMailsInQueueException.cs | 24 +++++++ SMTPServer/SMTPServer/MTACommands.cs | 49 ++++++++++++++ SMTPServer/SMTPServer/Mail.cs | 26 ++++++++ SMTPServer/SMTPServer/MailQueue.cs | 65 +++++++++++++++++++ SMTPServer/SMTPServer/MailTransferAgent.cs | 56 ++++++++++++++++ SMTPServer/SMTPServer/PortListener.cs | 1 + SMTPServer/SMTPServer/StartTcpConnection.cs | 58 ++++++++++++++++- 10 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 SMTPServer/DNSResolver/DNSResolver.csproj create mode 100644 SMTPServer/DNSResolver/Program.cs create mode 100644 SMTPServer/SMTPServer/Exceptions/NoMailsInQueueException.cs create mode 100644 SMTPServer/SMTPServer/MTACommands.cs create mode 100644 SMTPServer/SMTPServer/Mail.cs create mode 100644 SMTPServer/SMTPServer/MailQueue.cs create mode 100644 SMTPServer/SMTPServer/MailTransferAgent.cs diff --git a/SMTPServer/DNSResolver/DNSResolver.csproj b/SMTPServer/DNSResolver/DNSResolver.csproj new file mode 100644 index 0000000..35c4d2b --- /dev/null +++ b/SMTPServer/DNSResolver/DNSResolver.csproj @@ -0,0 +1,25 @@ + + + + + Exe + netcoreapp1.0 + + + + + + + + + + 1.0.1 + + + 1.0.0-alpha-20161104-2 + All + + + + + \ No newline at end of file diff --git a/SMTPServer/DNSResolver/Program.cs b/SMTPServer/DNSResolver/Program.cs new file mode 100644 index 0000000..104ecf0 --- /dev/null +++ b/SMTPServer/DNSResolver/Program.cs @@ -0,0 +1,9 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } +} \ No newline at end of file diff --git a/SMTPServer/SMTPServer.sln b/SMTPServer/SMTPServer.sln index 0591f69..8362e85 100644 --- a/SMTPServer/SMTPServer.sln +++ b/SMTPServer/SMTPServer.sln @@ -4,9 +4,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 15.0.25914.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTPServer", "SMTPServer\SMTPServer.csproj", "{ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}" + ProjectSection(ProjectDependencies) = postProject + {55342A63-351F-4908-B708-DC4111FF34E7} = {55342A63-351F-4908-B708-DC4111FF34E7} + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D43013F8-933F-4ADC-8943-08E91662A070}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DNSResolver", "DNSResolver\DNSResolver.csproj", "{55342A63-351F-4908-B708-DC4111FF34E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,6 +22,10 @@ Global {ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}.Debug|Any CPU.Build.0 = Debug|Any CPU {ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}.Release|Any CPU.ActiveCfg = Release|Any CPU {ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}.Release|Any CPU.Build.0 = Release|Any CPU + {55342A63-351F-4908-B708-DC4111FF34E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55342A63-351F-4908-B708-DC4111FF34E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55342A63-351F-4908-B708-DC4111FF34E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55342A63-351F-4908-B708-DC4111FF34E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SMTPServer/SMTPServer/Exceptions/NoMailsInQueueException.cs b/SMTPServer/SMTPServer/Exceptions/NoMailsInQueueException.cs new file mode 100644 index 0000000..9737eea --- /dev/null +++ b/SMTPServer/SMTPServer/Exceptions/NoMailsInQueueException.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SMTPServer.Exceptions +{ + class NoMailsInQueueException : Exception + { + public NoMailsInQueueException() : base() + { + + } + + public NoMailsInQueueException(string message) : base(message) + { + + } + + public NoMailsInQueueException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/SMTPServer/SMTPServer/MTACommands.cs b/SMTPServer/SMTPServer/MTACommands.cs new file mode 100644 index 0000000..30b1588 --- /dev/null +++ b/SMTPServer/SMTPServer/MTACommands.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace SMTPServer +{ + public class MTACommandsDict : Dictionary + { + public MTACommandsDict() + { + Add(MTACommands.HELO, "HELO"); + Add(MTACommands.DATA, "DATA"); + Add(MTACommands.EXPN, "EXPN"); + Add(MTACommands.HELP, "HELP"); + Add(MTACommands.MAIL_FROM, "MAIL FROM"); + Add(MTACommands.NOOP, "NOOP"); + Add(MTACommands.QUIT, "QUIT"); + Add(MTACommands.RCPT_TO, "RCPT TO"); + Add(MTACommands.RSET, "RSET"); + Add(MTACommands.SAML_FROM, "SAML FROM"); + Add(MTACommands.SEND_FROM, "SEND FROM"); + Add(MTACommands.SOML_FROM, "SOML FROM"); + Add(MTACommands.TURN, "TURN"); + Add(MTACommands.VERB, "VERB"); + Add(MTACommands.VRFY, "VRFY"); + } + } + + public enum MTACommands + { + HELO, + MAIL_FROM, + RCPT_TO, + DATA, + RSET, + QUIT, + HELP, + VRFY, + EXPN, + VERB, + NOOP, + TURN, + SEND_FROM, + SOML_FROM, + SAML_FROM + } +} \ No newline at end of file diff --git a/SMTPServer/SMTPServer/Mail.cs b/SMTPServer/SMTPServer/Mail.cs new file mode 100644 index 0000000..9ea534e --- /dev/null +++ b/SMTPServer/SMTPServer/Mail.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SMTPServer +{ + public class Mail + { + public string _To { get; private set; } + public string _From { get; private set; } + + public string _Subject { get; private set; } + public string _MessageId { get; private set; } + public string _Date { get; private set; } + + public string _MIME_Version { get; private set; } + + public string _Others { get; private set; } + + + public Mail(string mail) + { + + } + } +} diff --git a/SMTPServer/SMTPServer/MailQueue.cs b/SMTPServer/SMTPServer/MailQueue.cs new file mode 100644 index 0000000..ab77596 --- /dev/null +++ b/SMTPServer/SMTPServer/MailQueue.cs @@ -0,0 +1,65 @@ +using SMTPServer.Exceptions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace SMTPServer +{ + public class MailQueue + { + private static List _Mails = null; + + public static void AddToMesssageQueue(Mail mail) + { + lock (_Mails) + { + if (_Mails == null) + { + _Mails = new List(); + } + _Mails.Add(new QueueMail(mail)); + } + } + + public static QueueMail GetNextMail() + { + lock (_Mails) + { + if(_Mails.Count < 1) + { + throw new NoMailsInQueueException(); + } + + var m = _Mails[0]; + + _Mails.Remove(m); + m._Count++; + _Mails.Add(m); + + return m; + } + } + + public static void RemoveMailFromQueue(QueueMail mail) + { + lock (_Mails) + { + _Mails.Remove(mail); + } + } + + public class QueueMail + { + public DateTime _QueueEntered { get; private set; } + public int _Count { get; set; } + public Mail _Mail { get; private set; } + + public QueueMail (Mail mail) + { + _Mail = mail; + _QueueEntered = DateTime.Now; + _Count = 0; + } + } + } +} diff --git a/SMTPServer/SMTPServer/MailTransferAgent.cs b/SMTPServer/SMTPServer/MailTransferAgent.cs new file mode 100644 index 0000000..af8850a --- /dev/null +++ b/SMTPServer/SMTPServer/MailTransferAgent.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using SMTPServer.Exceptions; +using System.Net; + +namespace SMTPServer +{ + class MailTransferAgent + { + static MTACommandsDict _MTACommandsDict = new MTACommandsDict(); + static Thread _Thread; + + public static void StartMailTransferAgent() + { + _Thread = new Thread(new ThreadStart(MTA)); + _Thread.Start(); + } + + public static void MTA() + { + while (true) + { + try + { + var mail = MailQueue.GetNextMail(); + } catch (NoMailsInQueueException) { + Thread.Sleep(100); + continue; + } + + var charset = Encoding.UTF8; + + var dnsname = GetDNSName(""); + var client = new StartTcpConnection(25, new IPAddress(GetIpFromDNS(dnsname)), charset); + + if (!client.Connected) ; //ToDo Errorfall + } + } + + public static string GetDNSName(string mailTo) + { + var parts = mailTo.Split('@'); + var domain = parts[1]; + + return null; + } + + public static byte[] GetIpFromDNS(string dnsname) + { + + return null; + } + } +} diff --git a/SMTPServer/SMTPServer/PortListener.cs b/SMTPServer/SMTPServer/PortListener.cs index a85ffa5..e5621fe 100644 --- a/SMTPServer/SMTPServer/PortListener.cs +++ b/SMTPServer/SMTPServer/PortListener.cs @@ -53,6 +53,7 @@ namespace SMTPServer listener.Start(); _Listener = listener; Thread thread = new Thread(new ThreadStart(ListenerAsync)); + thread.Start(); } private async void ListenerAsync() diff --git a/SMTPServer/SMTPServer/StartTcpConnection.cs b/SMTPServer/SMTPServer/StartTcpConnection.cs index b97b533..7b1ca9a 100644 --- a/SMTPServer/SMTPServer/StartTcpConnection.cs +++ b/SMTPServer/SMTPServer/StartTcpConnection.cs @@ -1,16 +1,68 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Sockets; using System.Text; +using System.Threading; namespace SMTPServer { - class StartTcpConnection + class StartTcpConnection : Socket { - public StartTcpConnection(int port, IPAddress destination) + private List _Lines = new List(); + private string _Others { get; set; } + private Encoding _Encoding { get; set; } + + public StartTcpConnection(int port, IPAddress destination, Encoding encoding) : base (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { - + this.Connect(new IPEndPoint(destination, port)); + Thread readLines = new Thread(new ThreadStart(ReadClientInput)); } + private void ReadClientInput() + { + while (true) + { + byte[] buffer = new byte[this.ReceiveBufferSize]; + Receive(buffer); + + var str = _Encoding.GetString(buffer); + lock (_Others) + { + _Others += str; + } + + CheckLines(); + } + } + + private void CheckLines() + { + lock (_Others) + { + var line = ""; + foreach (char c in _Others) + { + if (c.Equals('\n')) + { + lock (_Lines) + { + _Lines.Add(line); + line = ""; + } + } + else line += c; + } + _Others = line; + } + } + + public string GetLine() + { + lock (_Lines) + { + return _Lines[0]; + } + } } }