---------------------
PatchSet 2854 
Date: 2001/08/25 08:18:21
Author: serassio
Branch: cygwin
Tag: (none) 
Log:
New hosts file handling

Members: 
	src/cf.data.pre:1.10.2.7->1.10.2.8 
	src/defines.h:1.3.22.12->1.3.22.13 
	src/globals.h:1.5.12.10->1.5.12.11 
	src/tools.c:1.7.2.17->1.7.2.18 
	src/win32.c:1.1.50.11->1.1.50.12 

Index: squid/src/cf.data.pre
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/cf.data.pre,v
retrieving revision 1.10.2.7
retrieving revision 1.10.2.8
diff -u -r1.10.2.7 -r1.10.2.8
--- squid/src/cf.data.pre	12 Aug 2001 16:15:02 -0000	1.10.2.7
+++ squid/src/cf.data.pre	25 Aug 2001 08:18:21 -0000	1.10.2.8
@@ -1,6 +1,6 @@
 
 #
-# $Id: cf.data.pre,v 1.10.2.7 2001/08/12 16:15:02 serassio Exp $
+# $Id: cf.data.pre,v 1.10.2.8 2001/08/25 08:18:21 serassio Exp $
 #
 #
 # SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -1102,13 +1102,13 @@
 
 NAME: hosts_file
 TYPE: string
-DEFAULT: /etc/hosts
+DEFAULT: $SYSNETDB/hosts
 LOC: Config.etcHostsPath
 DOC_START
 	Location of the host-local IP name-address associations
-	database.  Most Operating Systems have such a file: under
-	Un*X it's by default in /etc/hosts MS-Windows NT/2000 places
-	that in %SystemRoot%(by default
+	database. Most Operating Systems have such a file: 
+	under Un*X it's by default in /etc/hosts, 
+	MS-Windows NT/2000 places that in %SystemRoot%(by default
 	c:\winnt)\system32\drivers\etc\hosts, while Windows 9x/ME
 	places that in %windir%(usually c:\windows)\hosts
 
@@ -1118,9 +1118,12 @@
 	character are comments.
 
 	The file is checked at startup and upon configuration.  If
-	set to 'none', it won't be checked.  If append_domain is
-	used, that domain will be added to domain-local (i.e. not
-	containing any dot character) host definitions.
+	set to 'none', it won't be checked. If set to special value
+	"$SYSNETDB/hosts", it will be searched in the default Operating
+	System location specified above.
+	If append_domain is used, that domain will be added to 
+	domain-local (i.e. not containing any dot character) host
+	definitions.
 DOC_END
 
 NAME: diskd_program
Index: squid/src/defines.h
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/defines.h,v
retrieving revision 1.3.22.12
retrieving revision 1.3.22.13
diff -u -r1.3.22.12 -r1.3.22.13
--- squid/src/defines.h	19 Aug 2001 16:27:15 -0000	1.3.22.12
+++ squid/src/defines.h	25 Aug 2001 08:18:21 -0000	1.3.22.13
@@ -1,6 +1,6 @@
 
 /*
- * $Id: defines.h,v 1.3.22.12 2001/08/19 16:27:15 serassio Exp $
+ * $Id: defines.h,v 1.3.22.13 2001/08/25 08:18:21 serassio Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -293,6 +293,13 @@
 #define O_BINARY 0
 #endif
 
+#define DEFAULT_SYS_NETWORK_DB_PATH "/etc"
+#if defined(_SQUID_MSWIN)
+#define DEFAULT_HOSTS_FILENAME "\\hosts"
+#else
+#define DEFAULT_HOSTS_FILENAME "/hosts"
+#endif
+
 /* CygWin & Windows NT Port */
 #if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
 #define _WIN_OS_UNKNOWN	0
Index: squid/src/globals.h
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/globals.h,v
retrieving revision 1.5.12.10
retrieving revision 1.5.12.11
diff -u -r1.5.12.10 -r1.5.12.11
--- squid/src/globals.h	6 May 2001 17:44:02 -0000	1.5.12.10
+++ squid/src/globals.h	25 Aug 2001 08:18:21 -0000	1.5.12.11
@@ -1,6 +1,6 @@
 
 /*
- * $Id: globals.h,v 1.5.12.10 2001/05/06 17:44:02 serassio Exp $
+ * $Id: globals.h,v 1.5.12.11 2001/08/25 08:18:21 serassio Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -154,6 +154,7 @@
 extern RemovalPolicy *mem_policy;
 extern hash_table *proxy_auth_username_cache;	/* NULL */
 extern int incoming_sockets_accepted;
+extern char *sys_network_db_path;	/* NULL */
 #if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
 extern unsigned int WIN32_OS_version;	/* 0 */
 extern char *WIN32_OS_string;
Index: squid/src/tools.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/tools.c,v
retrieving revision 1.7.2.17
retrieving revision 1.7.2.18
diff -u -r1.7.2.17 -r1.7.2.18
--- squid/src/tools.c	19 Aug 2001 16:27:15 -0000	1.7.2.17
+++ squid/src/tools.c	25 Aug 2001 08:18:21 -0000	1.7.2.18
@@ -1,6 +1,6 @@
 
 /*
- * $Id: tools.c,v 1.7.2.17 2001/08/19 16:27:15 serassio Exp $
+ * $Id: tools.c,v 1.7.2.18 2001/08/25 08:18:21 serassio Exp $
  *
  * DEBUG: section 21    Misc Functions
  * AUTHOR: Harvest Derived
@@ -1007,53 +1007,29 @@
     FILE *fp;
     char buf[1024];
     char buf2[512];
+    char hosts_file[FILENAME_MAX];
     char *nt = buf;
     char *lt = buf;
     char *addr = buf;
     char *host = NULL;
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
-    char *systemroot = NULL;
-#endif
     if (NULL == Config.etcHostsPath)
 	return;
     if (0 == strcmp(Config.etcHostsPath, "none"))
 	return;
-    fp = fopen(Config.etcHostsPath, "r");
+    if (0 == strcmp(Config.etcHostsPath, "$SYSNETDB/hosts")) {
+	if (sys_network_db_path == NULL)
+	   sys_network_db_path = xstrdup(DEFAULT_SYS_NETWORK_DB_PATH);
+	strcpy(hosts_file, sys_network_db_path);
+	strcat(hosts_file, DEFAULT_HOSTS_FILENAME); 
+    }
+    else
+    	strcpy(hosts_file, Config.etcHostsPath);
+    debug(1, 5) ("etc_hosts: host files is '%s'\n", hosts_file);
+    fp = fopen(hosts_file, "r");
     if (fp == NULL) {
-#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
-	switch (WIN32_OS_version) {
-	case _WIN_OS_WINNT:
-	case _WIN_OS_WIN2K:
-	case _WIN_OS_WINXP:
-	    if ((systemroot=getenv("SYSTEMROOT")) != NULL) {
-		strcpy(buf,systemroot);
-	    	strcat(buf,"\\system32\\drivers\\etc\\hosts");
-	    }
-	    break;
-	case _WIN_OS_WIN95:
-	case _WIN_OS_WIN98:
-	case _WIN_OS_WINME:
-	    if ((systemroot=getenv("WINDIR")) != NULL) {
-		strcpy(buf,systemroot);
-	    	strcat(buf,"\\hosts");
-	    }
-	    break;
-	}
-	if (systemroot != NULL) {
-	    safe_free(Config.etcHostsPath);
-            Config.etcHostsPath=xstrdup(buf);
-	    fp = fopen(Config.etcHostsPath, "r");
-	}
-	if (fp == NULL) {
-	    debug(1, 1) ("parseEtcHosts: %s: %s\n",
-		Config.etcHostsPath, xstrerror());
-	    return;
-	}
-#else
 	debug(1, 1) ("parseEtcHosts: %s: %s\n",
-	    Config.etcHostsPath, xstrerror());
+	    hosts_file, xstrerror());
 	return;
-#endif
     }
 #if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_)
     setmode(fileno(fp), O_TEXT);
Index: squid/src/win32.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/win32.c,v
retrieving revision 1.1.50.11
retrieving revision 1.1.50.12
diff -u -r1.1.50.11 -r1.1.50.12
--- squid/src/win32.c	19 Aug 2001 16:27:15 -0000	1.1.50.11
+++ squid/src/win32.c	25 Aug 2001 08:18:21 -0000	1.1.50.12
@@ -28,11 +28,57 @@
 #include <windows.h>
 
 static unsigned int GetOSVersion();
+static void Set_sys_network_db_path();
+
+#define DATABASEPATH "DataBasePath"
+
+static char TCP_REGKEY[256] = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
 
 /* ====================================================================== */
 /* LOCAL FUNCTIONS */
 /* ====================================================================== */
 
+static void
+Set_sys_network_db_path()
+{
+    HKEY hndKey;
+    char buf[1024];
+    DWORD Size = 0;
+#ifdef _SQUID_CYGWIN_
+    FILE * fp;
+    /* Checks if exist /etc/hosts on Cygwin */	
+    strcpy(buf, DEFAULT_SYS_NETWORK_DB_PATH);
+    strcat(buf, DEFAULT_HOSTS_FILENAME);
+    if ((fp = fopen(buf, "r")) != NULL) {
+    	fclose (fp);
+	return;
+    }
+#endif
+    switch (WIN32_OS_version) {
+    case _WIN_OS_WINNT:
+    case _WIN_OS_WIN2K:
+    case _WIN_OS_WINXP:
+	safe_free(sys_network_db_path);
+	/* get hosts file location from Windows Registry */
+	if (RegOpenKey(HKEY_LOCAL_MACHINE, TCP_REGKEY, &hndKey) == ERROR_SUCCESS) {
+	    DWORD Type = 0;
+	    RegQueryValueEx(hndKey, DATABASEPATH, NULL, &Type, NULL, &Size);
+	    RegQueryValueEx(hndKey, DATABASEPATH, NULL, &Type, buf, &Size);
+	    RegCloseKey(hndKey);
+	}			
+	break;
+    case _WIN_OS_WIN95:
+    case _WIN_OS_WIN98:
+    case _WIN_OS_WINME:
+	strcpy(buf,"%WINDIR%");
+	break;
+    }
+    Size = ExpandEnvironmentStrings(buf, NULL, 1) + 1;
+    safe_free(sys_network_db_path);
+    sys_network_db_path = xmalloc(Size);
+    ExpandEnvironmentStrings(buf, sys_network_db_path, Size);
+}
+
 static unsigned int
 GetOSVersion()
 {
@@ -78,7 +124,7 @@
     default:
 	break;
     }
-    WIN32_OS_string=xstrdup("Unknown Windows system");
+    WIN32_OS_string = xstrdup("Unknown Windows system");
     return _WIN_OS_UNKNOWN;
 }
 
@@ -100,6 +146,8 @@
 	return 1;
     if (atexit(WIN32_Exit) != 0)
 	return 1;
+    Set_sys_network_db_path();
     return 0;
 }
 #endif
+
