views:

129

answers:

1

I'm using the SystemConfiguration framework to get the current IP from the dynamic store in my app.

Is it possible to get the hostname from the dynamic store as well? The current IP could be local, so I'm trying to get the returned value to be something like "mymac.local" or "2adg3.dsl.lgtpmi.sbcglobal.net", depending on how I'm connected to the net.

I tried SCDynamicStoreCopyLocalHostName(NULL), but it only returns the computer name itself (and not with the .local part), even if the IP resolves to a hostname that's accessible to everyone on the internet.

+1  A: 

You could try

[[NSHost currentHost] names]

I just tried it and it returns all local hostnames of my machine.

Update:
This is a good blog post that fixes some NSHost issues:
http://cocoawithlove.com/2009/11/drop-in-fix-for-problems-with-nshost.html

weichsel
I was about to suggest `gethostbyname2(`*3*`)`, but this likely returns the same results and is ten times easier.
Jeremy W. Sherman
I should have mentioned that [[NSHost currentHost] names] does exactly what I want, but everyone says to not use NSHost because it's blocking and it's not thread-safe. I've been looking into CFHost, but it doesn't return local names like "mymac.local".
K_T
I added a link to my original answer, that shows some workarounds for NSHost.
weichsel