Here are some critical codes. The DefaultPort is 53.
public DNServer(int port = DefaultPort, int ttl = DefaultTTL, int cacheSize = DefaultCacheSize)
{
dnsCache = new Dictionary<string, DNSSet>();
this.ttl = ttl;
this.cacheSize = cacheSize;
udpListener = new UdpClient(port);
}
public void Start()
{
while (true)
{
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any,53);
byte[] requestBytes = udpListener.Receive(ref clientEndPoint);
DNSRequest request = DNSRequest.Parse(requestBytes);
DNSResponse response = ProcessRequest(request);
byte[] responseBytes = response.GetBytes();
udpListener.Send(responseBytes, responseBytes.Length, clientEndPoint);
}
}
I am trying to simulate a local DNS server with C#. When I started the application, flushed the DNS cache and visited a new webpage, it just got stuck in udpListener.Receive(). Wireshark can detect the DNS request successfully.
The port has been opened. After I started the application, use 'netstat -ano' and it shows the port 53 is active.
UDP 0.0.0.0:53 *:* 27660
The expected result should be that when I am opening a new page in my browser, the application can detect the DNS request sent by my computer.