Reputation: 1895
Other than getdomainname()
is there any other way to get the domain name on a Mac using C?
I can use alternatively Cocoa APIs. Thanks.
Upvotes: 0
Views: 170
Reputation: 311606
If you're looking for the DNS name that corresponds to the local system, I don't think getdomainname()
is your friend. According to the documentation, this is for obtaining the NIS domain name of a system:
Getdomainname() returns the standard NIS domain name for the current
host, as previously set by setdomainname(). The parameter namelen speci-fies specifies
fies the size of the name array. The returned name is null-terminated
unless insufficient space is provided.
And in fact, it's largely accurate to say that a host doesn't have a DNS domain name. Each interface on your system has an address, and there may be a DNS record that resolves to that address, but the truth is that (a) there may not be a corresponding DNS record, and (b) there may be a DNS record that resolves to your host and to other systems as well. For example, the hostname "google.com" resolves to a slew of difference addresses:
$ host -t A google.com
google.com has address 74.125.226.244
google.com has address 74.125.226.240
google.com has address 74.125.226.241
google.com has address 74.125.226.242
google.com has address 74.125.226.243
And of course your system may have multiple IP addresses associated with it (e.g., both wired and wireless).
If you're willing to cross your fingers and hope, your best bet is usually to determine your local ip address via a call to getsockname()
on an open connection, and then do a reverse DNS lookup on that address to find a corresponding name in DNS.
Upvotes: 1