开发者

getting the domain name by code in Linux

I am using getdomainname() and gethostbyname() to try to get the domain of the computer so I can show the correct information on my program. However sometimes these functions don't return the correct information.

Is there any other way (in plain C) to get the domain name in Linux?

Edit: just to make it a bit more clear: I want to check if the computer is part of a domain. If it is, get the domain name.

Currently I am using the functions mentioned above. Are there any others?

@unwind: please DO NOT edit this question for "brevity" if I would like to say thanks I'll say开发者_运维问答 thanks.

Thanks!


If you want to get the (Internet) domain name there are certain issues you need to think about.

A computer can have multiple network interfaces, in fact it almost certainly has at least two including the loopback interface. Each interface has an IP address (possibly more than one) and each IP address can be mapped to from any number of DNS names and entries in the hosts file.

So which, if any of the many possible domain names that getdomainname() returns depends on a whole raft of configuration issues. e.g. which IP address is configured to be the primary address, whether the hosts file is used in preference to DNS, whether the hosts file is correctly configured, whether the IP address has a reverse lookup set and lots of other issues.

For instance, it is fairly common to misconfigure the hosts file. If you see an entry in it like:

192.168.1.1  foohost foohost.example.com

that is wrong. The first host name on a line is the canonical name (for the interface) and subsequent entries are merely aliases. If you want the domain to come out as example.com rather than nothing, it needs to look like this:

192.168.1.1  foohost.example.com foohost

Also, every IP address on the Internet should ideally have a reverse lookup record in DNS which maps the IP address to a hostname and domain. However, there is no rule to say it has to exist or to say that it has to be the domain by which you SSH'd in or pointed your web browser at.

On any given computer, there are many reasons why the domain name is not what you expect.


Unfortunately this information is not always set correctly. First of all, complain to your system administrator.

If all that fails, with something like the following you may get a field res->ai_canonname with a canonical hostname and then also iterate over all IP addresses:

struct addrinfo *res = NULL;
struct addrinfo hints = {
   .ai_family = AF_UNSPEC,
   .ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | (name ? 0 : AI_PASSIVE),
    .ai_socktype = SOCK_STREAM,
};
getaddrinfo(name, NULL, &hints, &res);
for (struct addrinfo *p = res; p; p = p->ai_next) {
 ...
}

You'd then somehow have to select which ones are interesting for you (avoid loopback etc) and try to find a hostname corresponding to one of these IP addresses. But as IP addresses do not necessarily correspond to a valid hostname, this might fail also.


To get the hostname of the computer you are running the program on:

  • uname
  • /etc/hostname
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜