Switch off ICMP redirect processing.
authorThomas Lopatic <thomas@lopatic.de>
Sun, 21 Nov 2004 00:26:48 +0000 (00:26 +0000)
committerThomas Lopatic <thomas@lopatic.de>
Sun, 21 Nov 2004 00:26:48 +0000 (00:26 +0000)
gui/win32/Main/Frontend.cpp

index 2255ff1..a1c0030 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
- * $Id: Frontend.cpp,v 1.2 2004/09/15 11:18:41 tlopatic Exp $\r
+ * $Id: Frontend.cpp,v 1.3 2004/11/21 00:26:48 tlopatic Exp $\r
  * Copyright (C) 2004 Thomas Lopatic (thomas@lopatic.de)\r
  *\r
  * This file is part of olsr.org.\r
@@ -42,8 +42,40 @@ CFrontendApp::CFrontendApp()
 \r
 CFrontendApp theApp;\r
 \r
+static int SetEnableRedirKey(unsigned long New)\r
+{\r
+       HKEY Key;\r
+       unsigned long Type;\r
+       unsigned long Len;\r
+       unsigned long Old;\r
+       \r
+       if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE,\r
+               "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",\r
+               0, KEY_READ | KEY_WRITE, &Key) != ERROR_SUCCESS)\r
+               return -1;\r
+       \r
+       Len = sizeof (Old);\r
+       \r
+       if (::RegQueryValueEx(Key, "EnableICMPRedirect", NULL, &Type,\r
+               (unsigned char *)&Old, &Len) != ERROR_SUCCESS ||\r
+               Type != REG_DWORD)\r
+               Old = 1;\r
+       \r
+       if (::RegSetValueEx(Key, "EnableICMPRedirect", 0, REG_DWORD,\r
+               (unsigned char *)&New, sizeof (New)))\r
+       {\r
+               ::RegCloseKey(Key);\r
+               return -1;\r
+       }\r
+       \r
+       ::RegCloseKey(Key);\r
+       return Old;\r
+}\r
+\r
 BOOL CFrontendApp::InitInstance()\r
 {\r
+       int Res;\r
+\r
 #ifdef _AFXDLL\r
        Enable3dControls();\r
 #else\r
@@ -59,6 +91,77 @@ BOOL CFrontendApp::InitInstance()
 \r
        m_pMainWnd = &dlg;\r
 \r
+       Res = SetEnableRedirKey(0);\r
+\r
+       if (Res == 1)\r
+       {\r
+               Res = AfxMessageBox("- WARNING -\n\n"\r
+                       "The OLSR software has just switched off the processing of incoming ICMP "\r
+                       " redirect messages in your registry.\n\n"\r
+                       " Please REBOOT your computer for this change to take effect.\n\n"\r
+                       " Do you want to allow the OLSR software to reboot your computer now?\n\n"\r
+                       " (Please say \"Yes\".)", MB_YESNO | MB_ICONEXCLAMATION);\r
+\r
+               if (Res == IDYES)\r
+               {\r
+                       HANDLE Proc;\r
+                       HMODULE Lib;\r
+                       BOOL (*Open)(HANDLE, DWORD, HANDLE *);\r
+                       BOOL (*Lookup)(char *, char *, LUID *);\r
+                       BOOL (*Adjust)(HANDLE, BOOL, TOKEN_PRIVILEGES *, DWORD,\r
+                               TOKEN_PRIVILEGES *, DWORD *);\r
+                       HANDLE Token;\r
+\r
+                       Lib = ::LoadLibrary("advapi32.dll");\r
+\r
+                       if (Lib != NULL)\r
+                       {\r
+                               Open = (BOOL (*)(HANDLE, DWORD, HANDLE *))\r
+                                       ::GetProcAddress(Lib, "OpenProcessToken");\r
+\r
+                               Lookup = (BOOL (*)(char *, char *, LUID *))\r
+                                       ::GetProcAddress(Lib, "LookupPrivilegeValueA");\r
+\r
+                               Adjust = (BOOL (*)(HANDLE, BOOL, TOKEN_PRIVILEGES *, DWORD,\r
+                                       TOKEN_PRIVILEGES *, DWORD *))\r
+                                       ::GetProcAddress(Lib, "AdjustTokenPrivileges");\r
+\r
+                               if (Open != NULL && Lookup != NULL && Adjust != NULL)\r
+                               {\r
+                                       struct\r
+                                       {\r
+                                               DWORD Count;\r
+                                               LUID_AND_ATTRIBUTES Priv;\r
+                                       }\r
+                                       TokPriv;\r
+\r
+                                       Proc = ::GetCurrentProcess();\r
+\r
+                                       if (!Open(Proc, TOKEN_ALL_ACCESS, &Token))\r
+                                               AfxMessageBox("OpenProcessToken() failed.");\r
+\r
+                                       else if (!Lookup("", "SeShutdownPrivilege", &TokPriv.Priv.Luid))\r
+                                               AfxMessageBox("LookupPrivilegeValue() failed.");\r
+\r
+                                       else\r
+                                       {\r
+                                               TokPriv.Count = 1;\r
+                                               TokPriv.Priv.Attributes = SE_PRIVILEGE_ENABLED;\r
+\r
+                                               if (!Adjust(Token, FALSE, (TOKEN_PRIVILEGES *)&TokPriv,\r
+                                                       0, NULL, NULL))\r
+                                                       AfxMessageBox("AdjustTokenPrivilege() failed.");\r
+                                       }\r
+                               }\r
+\r
+                               ::FreeLibrary(Lib);\r
+                       }\r
+\r
+                       ::ExitWindowsEx(EWX_REBOOT, 0);\r
+                       ::TerminateProcess(Proc, 0);\r
+               }\r
+       }\r
+\r
        dlg.DoModal();\r
 \r
        return FALSE;\r