EonaCat.DnsTester/EonaCat.DnsTester/Helpers/DnsHelper.cs

119 lines
4.6 KiB
C#

using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EonaCat.DnsTester.Helpers
{
internal class DnsHelper
{
public static event EventHandler<string> Log;
public static void ProcessDns(string startOfUrl, string domain, int urlsTotal, Socket dnsSocket, byte[] bytesSend,
long startTime, int totalDnsToUse, ListView result)
{
var bytesReceived = new byte[512];
var totalReceived = 0;
// wait for a response up to timeout
while (totalReceived < urlsTotal * totalDnsToUse)
{
dnsSocket.Receive(bytesReceived);
// Check if the error is ours
if ((bytesReceived[0] != bytesSend[0] || bytesReceived[1] != 0x31) &&
bytesReceived[1] != 0x32)
continue;
if (bytesReceived[2] != 0x81 || bytesReceived[3] != 0x80) continue;
// Get the time now
var stopTime = DateTime.Now.Ticks;
var deltaTime = Convert.ToString((double)(stopTime - startTime) / 10000000);
// Decode the answers
// Find the URL that was returned
int dnsId = bytesReceived[1];
var receiveString = Encoding.ASCII.GetString(bytesReceived);
var index = 12;
int startOfUrlLength = bytesReceived[index];
index++;
startOfUrl = receiveString.Substring(index, startOfUrlLength);
index += startOfUrlLength;
int domainLength = bytesReceived[index];
index++;
domain = receiveString.Substring(index, domainLength);
index += domainLength;
index += 8;
// Get the record type
int ResponseType = bytesReceived[index];
index += 9;
// Get the IP address if applicable
var ipResponse = "";
switch (ResponseType)
{
case 1:
ipResponse =
$"{Convert.ToString(bytesReceived[index])}.{Convert.ToString(bytesReceived[index + 1])}.{Convert.ToString(bytesReceived[index + 2])}.{Convert.ToString(bytesReceived[index + 3])}";
break;
case 5:
ipResponse = "CNAME";
break;
case 6:
ipResponse = "SOA";
break;
}
SetStatus($"Answer: {startOfUrl}.{domain} : {ipResponse}");
// Find the url in the list
for (int i = 0; i < result.Items.Count; i++)
{
if (result.Items[i].Text != $"{startOfUrl}.{domain}") continue;
string sDeltaTime;
switch (dnsId)
{
case 49:
result.Items[i].SubItems[1].Text = Convert.ToString(ipResponse);
sDeltaTime = Convert.ToString(deltaTime);
result.Items[i].SubItems[2].Text =
sDeltaTime.Length > 5 ? sDeltaTime.Substring(0, 5) : sDeltaTime;
result.Items[i].ForeColor = System.Drawing.Color.Red;
result.EnsureVisible(i);
result.Update();
Application.DoEvents();
result.Items[i].ForeColor = System.Drawing.Color.Black;
break;
case 50:
result.Items[i].SubItems[3].Text = Convert.ToString(ipResponse);
sDeltaTime = Convert.ToString(deltaTime);
result.Items[i].SubItems[4].Text =
sDeltaTime.Length > 5 ? sDeltaTime.Substring(0, 5) : sDeltaTime;
result.Items[i].ForeColor = System.Drawing.Color.Red;
result.EnsureVisible(i);
result.Update();
Application.DoEvents();
result.Items[i].ForeColor = System.Drawing.Color.Black;
break;
}
totalReceived++;
}
}
}
private static void SetStatus(string text)
{
Log?.Invoke(null, text);
}
}
}