3 * Canonicalize a hostname
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/utsname.h>
18 #include <netinet/in.h>
28 * Canonicalize hostname:
29 * if it is in double-quotes, then strip the quotes and return the name.
30 * if it is in the namespace, call the nameserver to expand it
31 * otherwise uppercase it and append the default domain (using an, er,
32 * undocumented global of the nameserver).
34 * Assumes that host was allocated using malloc(); it may be freed or
35 * realloc'ed, so the old pointer should not be considered valid.
38 char *canonicalize_hostname(char *host)
46 if (len > 2 && host[0] == '"' && host[len - 1] == '"')
48 tbuf = malloc(len - 1);
51 strncpy(tbuf, host + 1, len - 2);
57 if (strchr(host, '*') || strchr(host, '?') || !strcmp(host, "[NONE]"))
60 hp = gethostbyname(host);
64 host = realloc(host, strlen(hp->h_name) + 1);
66 strcpy(host, hp->h_name);
71 /* can't get name from nameserver; fix up the format a bit */
72 for (cp = host; *cp; cp++)
76 has_dot |= (*cp == '.');
80 static char *domain = NULL;
86 if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS)
88 cp = strchr(hostbuf, '.');
90 domain = strdup(++cp);
96 hp = gethostbyname(name.nodename);
99 cp = strchr(hp->h_name, '.');
101 domain = strdup(++cp);
107 tbuf = malloc(strlen(host) + strlen(domain) + 2);
110 sprintf(tbuf, "%s.%s", host, domain);