Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the current machine's full hostname in C (hostname and domain information)?

In a C project (POSIX), how do I get the fully qualified name for the current system?

For example, I can get just the hostname of my machine by doing gethostname() from unistd.h. This might give me machine3 in return, but I'm actually looking for machine3.somedomain.com for example.

How do I go about getting this information? I do not want to use a call to system() to do this, if possible.

like image 813
Zxaos Avatar asked Feb 02 '09 20:02

Zxaos


People also ask

How do I find my full hostname?

On your Windows PC, follow these steps to find your FQDN: Launch the Control Panel by searching for "Control Panel" in the Start Menu, or by typing Win+R and typing "control.exe" in the Run menu. On the System Information screen, you will see both the hostname and FQDN of your machine.

How do I find the hostname of an IP address?

Querying DNS Click the Windows Start button, then "All Programs" and "Accessories." Right-click on "Command Prompt" and choose "Run as Administrator." Type "nslookup %ipaddress%" in the black box that appears on the screen, substituting %ipaddress% with the IP address for which you want to find the hostname.

What is hostname in computer?

In computer networking, a hostname (archaically nodename) is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication, such as the World Wide Web.


1 Answers

To get a fully qualified name for a machine, we must first get the local hostname, and then lookup the canonical name.

The easiest way to do this is by first getting the local hostname using uname() or gethostname() and then performing a lookup with gethostbyname() and looking at the h_name member of the struct it returns. If you are using ANSI c, you must use uname() instead of gethostname().

Example:

char hostname[1024]; hostname[1023] = '\0'; gethostname(hostname, 1023); printf("Hostname: %s\n", hostname); struct hostent* h; h = gethostbyname(hostname); printf("h_name: %s\n", h->h_name); 

Unfortunately, gethostbyname() is deprecated in the current POSIX specification, as it doesn't play well with IPv6. A more modern version of this code would use getaddrinfo().

Example:

struct addrinfo hints, *info, *p; int gai_result;  char hostname[1024]; hostname[1023] = '\0'; gethostname(hostname, 1023);  memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; /*either IPV4 or IPV6*/ hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME;  if ((gai_result = getaddrinfo(hostname, "http", &hints, &info)) != 0) {     fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_result));     exit(1); }  for(p = info; p != NULL; p = p->ai_next) {     printf("hostname: %s\n", p->ai_canonname); }  freeaddrinfo(info); 

Of course, this will only work if the machine has a FQDN to give - if not, the result of the getaddrinfo() ends up being the same as the unqualified hostname.

like image 148
Zxaos Avatar answered Sep 18 '22 23:09

Zxaos