Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To get a fully qualified name for a machine, we must first get the local hostname, and then lookup the canonical name.</p> <p>The easiest way to do this is by first getting the local hostname using <code>uname()</code> or <code>gethostname()</code> and then performing a lookup with <code>gethostbyname()</code> and looking at the <code>h_name</code> member of the struct it returns. If you are using ANSI c, you <em>must</em> use <code>uname()</code> instead of <code>gethostname()</code>.</p> <p>Example:</p> <pre><code>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-&gt;h_name); </code></pre> <p>Unfortunately, <code>gethostbyname()</code> is deprecated in the current POSIX specification, as it doesn't play well with IPv6. A more modern version of this code would use <code>getaddrinfo()</code>.</p> <p>Example:</p> <pre><code>struct addrinfo hints, *info, *p; int gai_result; char hostname[1024]; hostname[1023] = '\0'; gethostname(hostname, 1023); memset(&amp;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", &amp;hints, &amp;info)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_result)); exit(1); } for(p = info; p != NULL; p = p-&gt;ai_next) { printf("hostname: %s\n", p-&gt;ai_canonname); } freeaddrinfo(info); </code></pre> <p>Of course, this will only work if the machine has a FQDN to give - if not, the result of the <code>getaddrinfo()</code> ends up being the same as the unqualified hostname.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload