alexreinert / ARSoft.Tools.Net

This project contains a complete managed .Net SPF validation, SenderID validation and a dns client and dns server implementation written in C#.
Apache License 2.0
168 stars 81 forks source link

Exception has found after migration. #31

Closed pil78 closed 10 months ago

pil78 commented 1 year ago

Hi, during usage of you library sometimes we track the exception: System.Exception: Error parsing dns query ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.ArraySegment1.System.Collections.Generic.IList.get_Item(Int32 index) at ARSoft.Tools.Net.Dns.DnsMessageBase.ParseDomainName(IList1 resultData, Int32 currentPosition, Int32& uncompressedLabelBytes) at ARSoft.Tools.Net.Dns.DnsQuestion.Parse(IList1 data, Int32& currentPosition) at ARSoft.Tools.Net.Dns.DnsMessageBase.ParseQuestionSection(IList1 data, Int32& currentPosition, Int32 recordCount) at ARSoft.Tools.Net.Dns.DnsMessageBase.ParseInternal(IList1 data, SelectTsigKey tsigKeySelector, Byte[] originalMac) at ARSoft.Tools.Net.Dns.DnsMessageBase.CreateByFlag(IList1 package, SelectTsigKey tsigKeySelector, Byte[] originalMac) at ARSoft.Tools.Net.Dns.DnsServer.ProcessRawPackageAsync(IServerConnection connection, DnsReceivedRawPackage queryPackage, CancellationToken token) --- End of inner exception stack trace --- at ARSoft.Tools.Net.Dns.DnsServer.ProcessRawPackageAsync(IServerConnection connection, DnsReceivedRawPackage queryPackage, CancellationToken token)

Our dev-ops engineer managed to reproduce this with small powershell sceipt: `function Send-UdpDatagram { Param ([string] $EndPoint, [int] $Port, [string] $Message)

  $IP = [System.Net.Dns]::GetHostAddresses($EndPoint) 
  $Address = [System.Net.IPAddress]::Parse($IP) 
  $EndPoints = New-Object System.Net.IPEndPoint($Address, $Port) 
  $Socket = New-Object System.Net.Sockets.UDPClient 
  $EncodedText = [Text.Encoding]::ASCII.GetBytes($Message) 
  $SendMessage = $Socket.Send($EncodedText, 12, $EndPoints)
  $Socket.Close() 

}

While ($True){ Write-Host "Send Message" Send-UdpDatagram -EndPoint "127.0.0.1" -Port 53 -Message "F/*/**966jgFF" }` Could you please check this and fix, because I have no ways to override or implement this by myself.

pil78 commented 1 year ago

PS: This has started after migration our application from 2.x.x to 3.x.x version. (We changed our code from old .net framework to .net 6)

alexreinert commented 1 year ago

The data you provided contains a malformed DNS message, so what should happen with that. In my tests this will not crash the server, only the ExceptionThrown event is fired and it is up to you, to handle the situation. One solution would be to just ignore that exception in the EventHandler.

pil78 commented 1 year ago

Thanks for answering. Yep, I handled the error and ignoring - what I did for now.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.