How to detect dns changes in C #?

I want to track DNS address changes. Therefore, I need to track DNS changes. Now I am doing this with a thread. I get dns and save the file and then compare it every 10 seconds, but I need a more specific solution. For example, is there some kind of event for this? This is the code:

GetDns:

public List<string> GetDns() { List<string> dns = new List<string>(); NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface networkInterface in networkInterfaces) { if (networkInterface.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipProperties = networkInterface.GetIPProperties(); IPAddressCollection dnsAddresses = ipProperties.DnsAddresses; foreach (IPAddress dnsAdress in dnsAddresses) { dns.Add(dnsAdress.ToString()); } } } return dns; } 

This is a comparison:

 string[] xmlDns = xmlData.GetDatas("DNSs", "Dns"); List<string> dns = getData.GetDns(); for (int i = 0; i < xmlDns.Length; i++) { if ( xmlDns[i].Equals( dns[i])) { this.Invoke(new MethodInvoker(delegate() { listBoxCheck.Items.Add(xmlDns[i] + " DNS was not changed."); })); } else { this.Invoke(new MethodInvoker(delegate() { listBoxCheck.Items.Add(xmlDns[i] + " DNS adress was changed as " + dns[i] ); })); } } 
+4
source share
3 answers

DNS is a query. There is no DNS server event on the client server. You could use the TTL metadata (time to live) that tells you when the DNS record expires.

Here is a library that provides an API to query this SimpleDNS dns server and documentation to get a TTL value .

+1
source

You can use WMI-Queries to query your DNS. Complete the query and compare the returned result with the previous result. A message appears when the value has changed (or trigger your event or something else).

You must use the System.Management namespace to do this, or perhaps the management namespace includes functions for viewing dns. But I'm not sure.

0
source

This is not quite the way @Candide writes that there is no event in DNS . In particular, for notification of change.

If you manage a DNS server, you can configure it to send NOTIFY messages to you as a client. Such messages (not guaranteed, but most often) are sent whenever the authoritative DNS server wants the slave servers to update the contents of the zone. Then you could respond to them as you prefer. Most DNS server implementations seem to send NOTIFY messages to the listed name servers by default, but can also be configured to send them to other hosts (for example, in BIND 9 this is done using the also-notify {} directive in the zone configuration) .

I'm not sure how you implement it; NOTIFY is part of the DNS protocol, so you should essentially write a limited-purpose DNS server implementation in order to listen and execute these messages. At the very least, you will need to control port 53 on UDP and TCP (I'm sure that notifications are almost always sent to UDP in practice, but the standard also allows TCP transport), sort any NOTIFY messages received.

In addition, since they are not guaranteed, you also need a backup mechanism.

Whether this is a viable option depends entirely on the situation.

0
source

Source: https://habr.com/ru/post/1443249/


All Articles