]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Windows port: Handle notification of IP address changes for dial-up connections
authorserassio <>
Fri, 25 Jan 2008 02:20:43 +0000 (02:20 +0000)
committerserassio <>
Fri, 25 Jan 2008 02:20:43 +0000 (02:20 +0000)
On Windows 2000 and later, the NotifyAddrChange() function allow a
process to be notified of the changes in the system IP addresses table.
This patch generate a reconfigure request after any notification, this
allow the hot addition/reconfiguration of network interfaces without
manually restart/reconfigure Squid.

src/WinSvc.cc
src/main.cc
src/protos.h

index 1d2fab2e024f1b5286f1d798e258f4349dd49070..9b75cf5dbd44688ad80ed0d1c2ae9332676b4e51 100755 (executable)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: WinSvc.cc,v 1.3 2007/04/28 22:26:37 hno Exp $
+ * $Id: WinSvc.cc,v 1.4 2008/01/24 19:20:43 serassio Exp $
  *
  * Windows support
  * AUTHOR: Guido Serassio <serassio@squid-cache.org>
@@ -69,6 +69,11 @@ static int s_iInitCount = 0;
 #endif
 
 static int Squid_Aborting = 0;
+static HANDLE NotifyAddrChange_thread = INVALID_HANDLE_VALUE;
+
+#undef NotifyAddrChange
+typedef DWORD(WINAPI * PFNotifyAddrChange) (OUT PHANDLE, IN LPOVERLAPPED);
+#define NOTIFYADDRCHANGE "NotifyAddrChange"
 
 #if USE_WIN32_SERVICE
 static SERVICE_STATUS svcStatus;
@@ -385,6 +390,17 @@ WIN32_Abort(int sig)
     WIN32_Exit();
 }
 
+void
+WIN32_IpAddrChangeMonitorExit()
+{
+    DWORD status = ERROR_SUCCESS;
+
+    if (NotifyAddrChange_thread == INVALID_HANDLE_VALUE) {
+       TerminateThread(NotifyAddrChange_thread, status);
+       CloseHandle(NotifyAddrChange_thread);
+    }
+}
+
 void
 WIN32_Exit()
 {
@@ -406,12 +422,57 @@ WIN32_Exit()
         DeleteCriticalSection(dbg_mutex);
 
     WIN32_ExceptionHandlerCleanup();
+    WIN32_IpAddrChangeMonitorExit();
 
 #endif
 
     _exit(0);
 }
 
+#ifdef _SQUID_MSWIN_
+static DWORD WINAPI
+WIN32_IpAddrChangeMonitor(LPVOID lpParam)
+{
+    DWORD Result;
+    HMODULE IPHLPAPIHandle;
+    PFNotifyAddrChange NotifyAddrChange;
+
+    if ((IPHLPAPIHandle = GetModuleHandle("IPHLPAPI")) == NULL)
+       IPHLPAPIHandle = LoadLibrary("IPHLPAPI");
+    NotifyAddrChange = (PFNotifyAddrChange) GetProcAddress(IPHLPAPIHandle, NOTIFYADDRCHANGE);
+
+    while (1) {
+       Result = NotifyAddrChange(NULL, NULL);
+       if (Result != NO_ERROR) {
+           debug(1, 1) ("NotifyAddrChange error %ld\n", Result);
+           return 1;
+       }
+       debug(1, 1) ("Notification of IP address change received, requesting Squid reconfiguration ...\n");
+       reconfigure(SIGHUP);
+    }
+    return 0;
+}
+
+DWORD
+WIN32_IpAddrChangeMonitorInit()
+{
+    DWORD status = ERROR_SUCCESS;
+    DWORD threadID = 0, ThrdParam = 0;
+
+    if (WIN32_run_mode == _WIN_SQUID_RUN_MODE_SERVICE) {
+       NotifyAddrChange_thread = CreateThread(NULL, 0, WIN32_IpAddrChangeMonitor,
+           &ThrdParam, 0, &threadID);
+       if (NotifyAddrChange_thread == NULL) {
+           status = GetLastError();
+           NotifyAddrChange_thread = INVALID_HANDLE_VALUE;
+           debug(1, 1) ("Failed to start IP monitor thread.\n");
+       } else
+           debug(1, 2) ("Starting IP monitor thread [%li] ...\n", threadID);
+    }
+    return status;
+}
+#endif
+
 int WIN32_Subsystem_Init(int * argc, char *** argv)
 {
 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
index 183b0040c532d9f67b97511199b9f425e325ac17..e96beca6d416b882e8f9aa6c7ef017032fd5a748 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: main.cc,v 1.455 2008/01/22 17:13:36 rousskov Exp $
+ * $Id: main.cc,v 1.456 2008/01/24 19:20:43 serassio Exp $
  *
  * DEBUG: section 1     Startup and Main Loop
  * AUTHOR: Harvest Derived
@@ -828,6 +828,10 @@ mainInitialize(void)
     if (WIN32_Socks_initialized)
         debugs(1, 1, "Windows sockets initialized");
 
+    if (WIN32_OS_version > _WIN_OS_WINNT) {
+       WIN32_IpAddrChangeMonitorInit();
+    }
+
 #endif
 
     if (!configured_once)
index 96679933517698a123302d32a1e0f163338143f4..6b6d742f6bc7da09c1bcfb1be5de6c47ff445bbf 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: protos.h,v 1.553 2008/01/21 20:59:44 serassio Exp $
+ * $Id: protos.h,v 1.554 2008/01/24 19:20:43 serassio Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -770,8 +770,8 @@ SQUIDCEXTERN int WIN32_pipe(int[2]);
 SQUIDCEXTERN int WIN32_getrusage(int, struct rusage *);
 SQUIDCEXTERN void WIN32_ExceptionHandlerInit(void);
 
-SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set
-                                      );
+SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set);
+SQUIDCEXTERN DWORD WIN32_IpAddrChangeMonitorInit();
 
 #endif