1

Possible Duplicate:
Get the IP Address of local computer
Obtaining local IP address using getaddrinfo() C function?

I need to account for IPs other than 127.0.0.1, and implementation should also detect hosts file entries or other methods.

Is there any way/api/function to directly check if a particular host resolves to the local machine?

Community
  • 1
  • 1
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • possible duplicate of [Get the IP Address of local computer](http://stackoverflow.com/questions/122208/get-the-ip-address-of-local-computer), [Obtaining local IP address using getaddrinfo() C function?](http://stackoverflow.com/questions/2146191/obtaining-local-ip-address-using-getaddrinfo-c-function) – Roman R. Jun 24 '12 at 18:10
  • 1
    I disagree regarding the duplicate determination - That other question asks how to enumerate local addresses. This is a much more specific question. IMO a simple test is to create a UDP socket and bind it to the address in question. If it succeeds it is local, otherwise it is not. You dont need to scan a list of N address if your interest is just in one! – nhed May 30 '14 at 14:22

1 Answers1

2

Depends on the OS. But you enumerate IP interfaces, check what IP each is configured for, then you can compare IP addresses against your list of local IPs.

To do this on host names you will need to resolve host names to IPs using DNS, and then check against local IP interfaces as above.

I don't know of any OS functions that just do this for you.

Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59