I want to monitor the dns address changes. So i need to track dns changes. I am doing it with thread right now. I get dns and save it file and then i compare they every 10 sec but i need more specific solution. For exampe, is there any event for that? 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 the compare:
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] );
}));
}
}
It's not quite true as @Candide wrote that there is no event in DNS. Particularly for change notification.
If you control the DNS server, you can set it up to send NOTIFY messages to you as a client. Such messages are (not guaranteed to be, but most often) sent whenever an authoritative DNS server wants slave servers to update the zone content. You could then react to those in whatever way you prefer. Most DNS server implementations probably only send NOTIFY messages to listed name servers by default, but can also be configured to send them to other hosts (in BIND 9, for example, this is done using the
also-notify {}
directive in the zone configuration).I'm not sure how you would implement it; NOTIFY is a part of the DNS protocol, so you'd essentially be writing a limited-purpose DNS server implementation to listen for and act on those messages. You would at the very least need to monitor port 53 on UDP and TCP (I do believe that notifies are almost always sent on UDP in practice but that the standard also allows for TCP transport), sort out any NOTIFY messages received, and act on them.
Also, since they are not guaranteed to be delivered, you also need a fallback mechanism.
Whether this is a viable option depends entirely on the situation.
You could use WMI-Queries to query your DNS. Loop the query and compare the returned result with a previous result. Popping up a messagebox when the value has changed (or fire your event or something).
You have to use the System.Management namespace in order to accomplish this or maybe the management namespace includes functions for looking at the dns. But this I am not certain off.
DNS is request based. There's no event from the DNS server to the client. You could potentially use the TTL (time to live) metadata, which tells you when the DNS record will expire.
Here's a library that exposes API to query a given dns server SimpleDNS and the documentation for getting the TTL value.