diff options
author | Guy Harris <guy@alum.mit.edu> | 2005-01-07 10:20:17 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2005-01-07 10:20:17 +0000 |
commit | 081f202ae37407b052c2896f99cfa2bc1199b40c (patch) | |
tree | b9c9956945309b76c8f318f4e4e8b3a080eb0240 /epan/addr_resolv.c | |
parent | 87919fc4ef8b448d4403da378e80ebfa74b8254f (diff) | |
download | wireshark-081f202ae37407b052c2896f99cfa2bc1199b40c.tar.gz |
Move the open and close of the hosts file into "read_hosts_file()".
On Windows, if we don't get the SYSTEMROOT environment variable, try
various locations where the hosts file is known to exist.
svn path=/trunk/; revision=12972
Diffstat (limited to 'epan/addr_resolv.c')
-rw-r--r-- | epan/addr_resolv.c | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/epan/addr_resolv.c b/epan/addr_resolv.c index b945bff0c2..4c41e93bc1 100644 --- a/epan/addr_resolv.c +++ b/epan/addr_resolv.c @@ -1504,9 +1504,10 @@ static guint ipxnet_addr_lookup(const gchar *name, gboolean *success) } /* ipxnet_addr_lookup */ #ifdef HAVE_GNU_ADNS -static void -read_hosts_file (FILE *hf) +static gboolean +read_hosts_file (const char *hostspath) { + FILE *hf; char *line = NULL; int size = 0; gchar *cp; @@ -1519,6 +1520,9 @@ read_hosts_file (FILE *hf) * See the hosts(4) or hosts(5) man page for hosts file format * (not available on all systems). */ + if ((hf = fopen(hostspath, "r")) == NULL) + return FALSE; + while (fgetline(&line, &size, hf) >= 0) { if ((cp = strchr(line, '#'))) *cp = '\0'; @@ -1561,6 +1565,9 @@ read_hosts_file (FILE *hf) } if (line != NULL) g_free(line); + + fclose(hf); + return TRUE; } /* read_hosts_file */ #endif @@ -1573,8 +1580,6 @@ read_hosts_file (FILE *hf) void host_name_lookup_init(void) { - FILE *hf; - char *hostspath; #ifdef WIN32 char *sysroot; static char rootpath[] = "\\system32\\drivers\\etc\\hosts"; @@ -1582,23 +1587,42 @@ host_name_lookup_init(void) { #ifdef WIN32 sysroot = getenv("SYSTEMROOT"); - /* getenv() returns NULL, if requested environment variable can't be found */ - if(sysroot != NULL) { - hostspath = g_strconcat(sysroot, rootpath, NULL); - + if (sysroot != NULL) { + /* The file should be under SYSTEMROOT */ + hostspath = g_strconcat(sysroot, rootpath, NULL); + read_hosts_file(hostspath); + g_free(hostspath); + } else { + /* + * This might be Windows OT (95/98/Me), or it might be NT + * (NT 4.0/2K/XP/etc.) with SYSTEMROOT not set. + * + * The hosts file is apparently in c:\windows\hosts in + * OT, and under the system root in NT, but the system + * root in NT might be c:\winnt or c:\windows (the latter + * is claimed to be where it is in XP Home, presumably + * for compatibility with OT). + * + * We try all three of them until we either succeed or run + * out of files. + * + * XXX - should we use WINDIR in this case, rather than + * guessing? Should we use WINDIR in *all* cases? + * If so, what should we use if WINDIR is null? Should + * we just punt rather than using a default location? + * Windows OT and NT put hosts in a different subdirectory; + * does that mean we can't just use WINDIR, we need to + * check whether it's OT or NT? + */ + if (!read_hosts_file("c:\\windows\\hosts")) { + if (!read_hosts_file("c:\\winnt\\system32\\drivers\\etc\\hosts")) + read_hosts_file("c:\\windows\\system32\\drivers\\etc\\hosts"); + } + } #else - hostspath = g_strdup("/etc/hosts"); + read_hosts_file("/etc/hosts"); #endif - if ((hf = fopen(hostspath, "r")) != NULL) { - read_hosts_file(hf); - fclose(hf); - } - g_free(hostspath); - -#ifdef WIN32 - } /* endif(sysroot != NULL) */ -#endif /* XXX - Any flags we should be using? */ /* XXX - We could provide config settings for DNS servers, and pass them to ADNS with adns_init_strcfg */ |