DnsServer: added code to ignore ICMP port unreachable responses which creates SocketException in ReceiveFrom(). other minor changes done.

This commit is contained in:
Shreyas Zare
2017-10-14 20:15:28 +05:30
parent 617ba5e532
commit 5bc49a85da

View File

@@ -35,6 +35,8 @@ namespace DnsServerCore
const int TCP_SOCKET_SEND_TIMEOUT = 30000;
const int TCP_SOCKET_RECV_TIMEOUT = 60000;
readonly IPEndPoint _localEP;
readonly Socket _udpListener;
readonly Thread _udpListenerThread;
@@ -65,15 +67,16 @@ namespace DnsServerCore
public DnsServer(IPEndPoint localEP)
{
_localEP = localEP;
_dnsCache = new DnsCache(_cacheZoneRoot);
_udpListener = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
_udpListener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
_udpListener.Bind(localEP);
_udpListener.Bind(_localEP);
_tcpListener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
_tcpListener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
_tcpListener.Bind(localEP);
_tcpListener.Bind(_localEP);
_tcpListener.Listen(10);
//start reading query packets
@@ -131,6 +134,16 @@ namespace DnsServerCore
else
remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
#region this code ignores ICMP port unreachable responses which creates SocketException in ReceiveFrom()
const uint IOC_IN = 0x80000000;
const uint IOC_VENDOR = 0x18000000;
const uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
_udpListener.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
#endregion
while (true)
{
bytesRecv = _udpListener.ReceiveFrom(recvBufferStream.Buffer, ref remoteEP);
@@ -344,6 +357,9 @@ namespace DnsServerCore
#region properties
public IPEndPoint LocalEP
{ get { return _localEP; } }
public Zone AuthoritativeZoneRoot
{ get { return _authoritativeZoneRoot; } }