tags:

views:

299

answers:

5

Hi!

I wrote a little program, which is working like a ping command(i use ICMPSendEcho2), but it gives back a return value, not only a text message. Now I have only one question. How can I programmatically check if a hostname exist or not? I mean if i want to ping computerA, and i don't even have a computerA then it should say what the originally ping says : "Ping request couldn't find host...". This means that there is no computer with that name. But if i ping computerB(when it's turned off) with my ping then it says Host not found. So my question is how can i decide that a computer doesn't exists, or it's only turned off?

Thanks in advance!

kampi

A: 

If a hostname doesn't exist at all, you should get a DNS resolver error when you try to resolve the host. This is distinct from receiving an error after pinging a known host which happens to be off/broken.

Charles Salvia
A: 

The only discrepancy between the two cases that I can think of is that when the computer is off but doesn't reply it must have resolved through some sort of name resolution system (netbios, dns, etc.).

If that is the case, you should be able to attempt to resolve the hostname first by using gethostbyname.

opello
I'm usin gethostbyname, but this isn't what i want. Because gethostbyname returns "host not found" when a computer (which exists in the domain) is just turned off and also reutnrs "hot not found" when i don't even have a computer with a given name. I need somehow to figure out how does this the originally ping do?
kampi
+1  A: 

You can use DNS name resolution to determine if a machine with a given hostname was ever registered in the past. This implies, but does not necessarily mean that the machine exists right now.

The same is true of any name resolution or directory service.

Ferruccio
@Ferruccio - it does not imply it in the logical sense. It merely hints it. Knowing that hostname is registered does not (logically) imply that the associated IP address was used at some time in the past, or even that there was any intention to use it.
Stephen C
"Ever registered in the past" is wrong. Try looking up "rhino.shadetreeinc.com" - it was registered in the past, but hasn't been published for at least five years, and you won't get a DNS hit on it. A positive response for a hostname says that hostname was "recently" published by that domain's authoritative nameserver. Here, "recently" means the nameserver's "time to live" value, which is often 24 hours.
Bob Murphy
True its like saying he is in the phone book he must be alive.
James Anderson
"registered" was a poor choice of words on my part. I meant that a DNS server had a record of that hostname. I did not mean a domain name officially registered through a registrar.
Ferruccio
+4  A: 

This problem has no solution. You simply cannot accurately distinguish between a computer that does not exist currently exist and one that does exist but you cannot 'ping'.

DNS doesn't help because that only tells you if there is a binding between an IP address and a DNS name. It doesn't tell you if there is currently a machine associated with that IP address.

The low level ARP and routing protocols don't help either. They allow you (or more likely your OS) to send network packets to the (nominally) right place, but they don't / cannot tell you if there is anything currently listening. The computer's ethernet cable could just have been unplugged, the computer could have just been powered off (but of course still exist), or it might have just been struck by a meteorite.

And I haven't even mentioned that there are things called firewalls that often actively try to prevent you from knowing that certain machines exist.

And there is the case where a machine is alive and using an IP address, but the IP address is not registered in DNS.

Stephen C
But he specifically asked "How can i programmatically check if a hostname exist or not?" DNS will tell you whether or not a hostname exists.
Charles Salvia
@Charles - no ... it will tell you if it is currently REGISTERED.
Stephen C
@Charles - also reread the last sentence of the original question, and tell me if you still think he is JUST interested in DNS names. I certainly don't!
Stephen C
What's the difference? If a *hostname* is registered with some DNS authority, then by definition it exists. That's separate from whether the actual machine associated with the "A record" in the DNS entry is currently off/broken/non-existent
Charles Salvia
By whose definition? If I have a hostname written on a piece of paper, it exists in a certain sense ... whether or not it is currently in DNS, DNS is working, I can talk to DNS, etc, etc. But anyway, as I just noted a machine can exist (and be pingable) without having any DNS name.
Stephen C
Although, the OP is somewhat confusing. From I can tell, he's basically asking "how can I tell if whatever.com is a valid hostname, versus whether the physical machine associated with whatever.com is currently off/broken.
Charles Salvia
@Charles - from what I can tell, the OP is simply unaware of the distinctions between DNS names, IP addresses and the various associated mechanisms that make this a difficult problem. Confirming his muddled understanding by just talking about DNS is doing him a major disservice.
Stephen C
@Stephen: I think we can all agree that the most useful definition of "existence" in regard to DNS hostnames would mean "registered with a DNS authority" rather than just written on a napkin. But regardless, you're obviously correct that a machine can exist without a DNS name. But what exactly is the OP asking? It seems to me he wants to know, how can I differentiate between the following 2 conditions: Condition A: Hostname does not exist. Condition B. Computer associated with Hostname is dead.
Charles Salvia
@Charles - re what he is asking, see my previous comment. When someone asks a muddled or misguided question, why insist that a literal answer is the best one? Even assuming that you are 100% correct about what the OP meant, what he asked and what he needs to know are (IMO) not the same thing.
Stephen C
+1  A: 

Hi!

It's much easier, than i thought! I only have to call the DNSQuery function, and if this will fail with error 9003 (which means DNS name doesn't exists) than i have what i wanted :)

Thanks for help for anyone!

kampi

kampi