16

I don't know how to write applications in C, but I need a tiny program that does:

lh = gethostbyname("localhost");
output = lh->h_name;

output variable is to be printed.

The above code is used in PHP MongoDB database driver to get the hostname of the computer (hostname is part of an input to generate an unique ID). I'm skeptical that this will return the hostname, so I'd like some proof.

Any code examples would be most helpful.

Happy day.

jww
  • 97,681
  • 90
  • 411
  • 885
Matic
  • 469
  • 2
  • 8
  • 16

3 Answers3

26
#include <stdio.h>
#include <netdb.h>


int main(int argc, char *argv[])
{
    struct hostent *lh = gethostbyname("localhost");

    if (lh)
        puts(lh->h_name);
    else
        herror("gethostbyname");

    return 0;
}

It is not a very reliable way of determining the hostname, though it may sometimes work. (what it returns depends on how /etc/hosts is set up). If you have a line like:

127.0.0.1    foobar    localhost

...then it will return "foobar". If you have it the other way around though, which is also common, then it will just return "localhost". A more reliable way is to use the gethostname() function:

#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    char hostname[HOST_NAME_MAX + 1];

    hostname[HOST_NAME_MAX] = 0;
    if (gethostname(hostname, HOST_NAME_MAX) == 0)
        puts(hostname);
    else
        perror("gethostname");

    return 0;
}
caf
  • 233,326
  • 40
  • 323
  • 462
  • 2
    You should pass `sizeof(hostname) -1` as length to `gethostname()`. Otherwise you might end up with no null-termination if truncation occurs. According to my manpage: "POSIX.1 says that if such truncation occurs, then it is unspecified whether the returned buffer includes a terminating null byte." – scai Jan 24 '17 at 12:08
  • @scai: Thanks, fixed. – caf Jan 24 '17 at 13:40
8

In C/UNIX, the equivalent would be something like:

#include <stdio.h>
#include <netdb.h>

int main (int argc, char *argv[]) {
    struct hostent *hstnm;
    if (argc != 2) {
        fprintf(stderr, "usage: %s hostname\n", argv[0]);
        return 1;
    }
    hstnm = gethostbyname (argv[1]);
    if (!hstnm)
        return 1;
    printf ("Name: %s\n", hstnm->h_name);
    return 0;
}

and the proof that it works:

$ hstnm localhost
Name: demon-a21pht

But try it yourself. Provided you have the correct environment, it should be fine.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

what is wrong?

h_name

The official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IPv4 address.

Andrey
  • 59,039
  • 12
  • 119
  • 163
  • And of course DNS is not going to be used for "localhost", so it's the local hosts file that's relevant. *If* the entry in the local hosts file has both "localhost" and the real hostname associated with the same address (127.0.0.1) *and* the real hostname is the first entry, then h_name will return the real hostname. – David Gelhar May 19 '10 at 13:02
  • @David Gelhar: localhost is not a name of computer. localhost resolves to 127.0.0.1 but there is no back conversion. http://en.wikipedia.org/wiki/Localhost – Andrey May 19 '10 at 13:33
  • right, but the point is that if /etc/hosts looks like `127.0.0.1 realname.com localhost` then `gethostbyname("localhost")` will return "realname.com" in h_name, but if the order is reversed, it will return "localhost" (see caf's answer). – David Gelhar May 19 '10 at 14:06