MTA + MessageQuerry

DNSResolver Projkect added
This commit is contained in:
Fabian Stamm 2016-11-22 23:00:18 +01:00
parent 23836945c9
commit ddf1c09acb
10 changed files with 319 additions and 3 deletions

View File

@ -0,0 +1,25 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,9 @@
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

View File

@ -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

View File

@ -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)
{
}
}
}

View File

@ -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<MTACommands, String>
{
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
}
}

View File

@ -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)
{
}
}
}

View File

@ -0,0 +1,65 @@
using SMTPServer.Exceptions;
using System;
using System.Collections.Generic;
using System.Text;
namespace SMTPServer
{
public class MailQueue
{
private static List<QueueMail> _Mails = null;
public static void AddToMesssageQueue(Mail mail)
{
lock (_Mails)
{
if (_Mails == null)
{
_Mails = new List<QueueMail>();
}
_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;
}
}
}
}

View File

@ -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;
}
}
}

View File

@ -53,6 +53,7 @@ namespace SMTPServer
listener.Start();
_Listener = listener;
Thread thread = new Thread(new ThreadStart(ListenerAsync));
thread.Start();
}
private async void ListenerAsync()

View File

@ -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<string> _Lines = new List<string>();
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];
}
}
}
}