views:

158

answers:

4

A simple question. How do I test if my current DNS is working or down? Would it involve polling? That I fear would take up resources and also require execution on a separate thread.

A: 

Yes it would involve looking up something, resolving a host name or some other DNS function. In addition you'd need to ensure that the host you're trying to resolve has not been cached in the local DNS cache too.

The Dns.GetHostByName function performs a lookup, but that doesn't mention if it will use the local cache. It's likely that it does, as it probably uses the OS lookup functionality under the hood.

blowdart
Yes I believe it caches the lookup too. Any way that I could get around the caching?
Prasannah
Try a different host name. You could use one that doesn't exist - the lookup wouldn't be cached, you'd just get a not found every time.
blowdart
A: 

try to resolve a domain name using dns.

Benny
A: 

Several things:

Polling 1:

Because of the possibility to get back bached values you probably want to look up non-existent random domains (guid.somedomainyoucontrol.com maybe) and check if you get a timeout or an expected NXDOMAIN result.

Polling 2:

Regarding your fear that this might cost a lot of resources: You really have to explain what you want to do here for a real answer, but: DNS is a very lightweight protocol. I don't think that it takes a huge hit on your client or server, depending on the polling interval. Heck, there are even "TCP over DNS" tunnels out there, that really stress what DNS can do and they don't usually destroy the network for others either or DOS the server. Probably the best thing would be to settle on a conservative polling timeout and resolving a subdomain below some domain you own, probably even directly querying a nameserver that you control.

"Connection":

DNS uses UDP, most of the time. It can switch to or be used via TCP, but that's not the "normal" usage. So the term "connection" is probably misleading. You can just test for any point in time if you can resolve hosts right now.

Benjamin Podszun
A: 

If you're trying to do this in C# code check out the

namespace System.Net

documentation found here: http://msdn.microsoft.com/en-us/library/system.net.aspx

...which includes the class...

public static class Dns

documentation found here: http://msdn.microsoft.com/en-us/library/system.net.dns.aspx

which will allow your to write code such as:

IPHostEntry hostInfo = Dns.GetHostByName("www.contoso.com");

There is additional functionality in the Dns class that may be of use to you depending on what you're trying to accomplish.

This doesn't provide a way to check if your "DNS is down" directly but one would assume if you can't resolve a website that should always be up (e.g. www.microsoft.com or www.google.com) then your dns is down or you have lost your connection to the internet. If this isn't what you're looking for please comment up a little more detail and we'll see if we can help.

Dave Jellison
Sorry blowdart, I somehow missed your comment when answering.There are also 2 open-source projects for tracerouting...http://stackoverflow.com/questions/142614/traceroute-and-ping-in-c
Dave Jellison
GetHostByName is deprecated, use GetHostEntry.
Trillian
Ah Trillian is correct, missed the Obsolete flag :(
Dave Jellison