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>
27 static char *local_domain(void)
29 static char *domain = NULL;
37 if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS)
39 cp = strchr(hostbuf, '.');
41 domain = strdup(++cp);
47 hp = gethostbyname(name.nodename);
50 cp = strchr(hp->h_name, '.');
52 domain = strdup(++cp);
63 * Canonicalize hostname:
64 * if it is in double-quotes, then strip the quotes and return the name.
65 * if it is in the namespace, call the nameserver to expand it
66 * otherwise uppercase it and append the default domain (using an, er,
67 * undocumented global of the nameserver).
69 * Assumes that host was allocated using malloc(); it may be freed or
70 * realloc'ed, so the old pointer should not be considered valid.
73 char *canonicalize_hostname(char *host)
80 if (len > 2 && host[0] == '"' && host[len - 1] == '"')
82 tbuf = malloc(len - 1);
85 strncpy(tbuf, host + 1, len - 2);
91 if (strchr(host, '*') || strchr(host, '?') || *host == '[')
94 hp = gethostbyname(host);
98 host = realloc(host, strlen(hp->h_name) + 1);
100 strcpy(host, hp->h_name);
105 /* can't get name from nameserver; fix up the format a bit */
106 cp = strchr(host, '.');
109 tbuf = malloc(strlen(host) + strlen(local_domain()) + 2);
112 sprintf(tbuf, "%s.%s", host, local_domain());
116 else if (strcasecmp(cp + 1, local_domain()) != 0)
119 /* This is a host in our local domain, so capitalize it. */
120 for (cp = host; *cp; cp++)