Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
16 KiB

  1. //***********************************************************************
  2. // settings.cpp
  3. //
  4. // This file contains the implementation of the "Settings" dialog class.
  5. //
  6. //
  7. // Author: SEA
  8. //
  9. // History:
  10. // Febuary-1996 Larry A. French
  11. // Modified the code to fix various problems. Regrettably, this
  12. // file still contains a fair amount of legacy code that I didn't
  13. // have time to fully rewrite. Also, I did not have time to go
  14. // though and fully comment the code.
  15. //
  16. //
  17. // Copyright (C) 1995, 1996 Microsoft Corporation. All rights reserved.
  18. //
  19. //************************************************************************
  20. // settings.cpp : implementation file
  21. //
  22. #include "stdafx.h"
  23. #include "eventrap.h"
  24. #include "settings.h"
  25. #include "globals.h"
  26. #include "trapreg.h"
  27. // This macro handles comparing bool values for the cases where TRUE can be and
  28. // non-zero value.
  29. #define BOOLS_ARE_DIFFERENT(b1, b2) (((b1) & (!(b2))) || ((!(b1)) & (b2)))
  30. #ifdef _DEBUG
  31. #undef THIS_FILE
  32. static char BASED_CODE THIS_FILE[] = __FILE__;
  33. #endif
  34. UINT _thrRun(CTrapSettingsDlg *trapDlg)
  35. {
  36. return trapDlg->thrRun();
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CTrapSettingsDlg dialog
  40. UINT CTrapSettingsDlg::thrRun()
  41. {
  42. HANDLE hEvents[2];
  43. DWORD retCode;
  44. CRegistryKey regkey;
  45. CRegistryValue regval;
  46. BOOL bThrottleIsTripped = FALSE;
  47. hEvents[0] = (HANDLE)m_evRegNotification;
  48. hEvents[1] = (HANDLE)m_evTermination;
  49. if (!g_reg.m_regkeySnmp.GetSubKey(SZ_REGKEY_PARAMETERS, regkey))
  50. return 0;
  51. do
  52. {
  53. m_evRegNotification.SetEvent();
  54. if (RegNotifyChangeKeyValue(
  55. regkey.m_hkeyOpen,
  56. TRUE,
  57. REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_NAME,
  58. (HANDLE)m_evRegNotification,
  59. TRUE) == ERROR_SUCCESS)
  60. {
  61. if (regkey.GetValue(SZ_REGKEY_PARAMS_THRESHOLD, regval) &&
  62. *(DWORD*)regval.m_pData == THROTTLE_TRIPPED)
  63. PostMessage(WM_UIREQUEST, UICMD_ENABLE_RESET, TRUE);
  64. else
  65. PostMessage(WM_UIREQUEST, UICMD_ENABLE_RESET, FALSE);
  66. }
  67. } while(WaitForMultipleObjects(2, hEvents, FALSE, INFINITE) == WAIT_OBJECT_0);
  68. regkey.Close();
  69. return 0;
  70. }
  71. CTrapSettingsDlg::CTrapSettingsDlg(CWnd* pParent /*=NULL*/)
  72. : CDialog(CTrapSettingsDlg::IDD, pParent)
  73. {
  74. //{{AFX_DATA_INIT(CTrapSettingsDlg)
  75. m_bLimitMsgLength = FALSE;
  76. //}}AFX_DATA_INIT
  77. }
  78. #define I_MAX_LONG 0x7fffffffL
  79. #define I_MIN_TRAPCOUNT 2
  80. #define I_MAX_TRAPCOUNT 9999999
  81. #define I_MIN_SECONDS 1
  82. #define I_MAX_SECONDS 9999999
  83. #define I_MIN_MESSAGE_LENGTH 400
  84. #define I_MAX_MESSAGE_LENGTH 0x7fff
  85. SCODE CTrapSettingsDlg::GetMessageLength(LONG* pnChars)
  86. {
  87. CString sValue;
  88. CButton* pbtnLimit = (CButton*)GetDlgItem(IDC_LIMITMSGLNGTH);
  89. BOOL bLimitEnabled = (pbtnLimit->GetCheck() == 1) ? TRUE : FALSE;
  90. m_edtMessageLength.GetWindowText(sValue);
  91. SCODE sc;
  92. LONG nChars = _ttol(sValue);
  93. sc = AsciiToLong(sValue, &nChars);
  94. if (FAILED(sc))
  95. {
  96. // They shouldn't have garbage in this edit control even if
  97. // they haven't selected a message limit. Let the user fix it.
  98. AfxMessageBox(IDS_ERR_SETTINGS_MESSAGELENGTH_NOT_INT);
  99. m_edtMessageLength.SetFocus();
  100. m_edtMessageLength.SetSel(0, -1);
  101. return E_FAIL;
  102. }
  103. if (bLimitEnabled)
  104. {
  105. if (nChars < I_MIN_MESSAGE_LENGTH || nChars > I_MAX_MESSAGE_LENGTH)
  106. {
  107. if (pbtnLimit->GetCheck() == 1)
  108. {
  109. CString sError;
  110. CString sRangeMessage;
  111. sError.LoadString(IDS_SETTINGS_MESSAGE_LENGTH_RANGE);
  112. GenerateRangeMessage(sRangeMessage, I_MIN_MESSAGE_LENGTH, I_MAX_MESSAGE_LENGTH);
  113. sError += sRangeMessage;
  114. AfxMessageBox(sError);
  115. sValue.Format(_T("%u"),nChars);
  116. m_edtMessageLength.SetWindowText(sValue);
  117. m_edtMessageLength.SetFocus();
  118. m_edtMessageLength.SetSel(0, -1);
  119. return E_FAIL;
  120. }
  121. }
  122. }
  123. *pnChars = nChars;
  124. return S_OK;
  125. }
  126. SCODE CTrapSettingsDlg::GetTrapsPerSecond(LONG* pnTraps, LONG* pnSeconds)
  127. {
  128. CString sSeconds;
  129. CString sTraps;
  130. CString sError;
  131. CString sRangeMessage;
  132. LONG nTraps;
  133. LONG nSeconds;
  134. SCODE sc;
  135. // First make sure that the trap count and seconds fields don't have garbage in them.
  136. // If a non-integer value is specified, force the user to fix it regardless of whether
  137. // or not the throttle is enabled.
  138. m_edtTrapCount.GetWindowText(sTraps);
  139. sc = AsciiToLong(sTraps, &nTraps);
  140. if (FAILED(sc))
  141. {
  142. AfxMessageBox(IDS_ERR_SETTINGS_TRAPCOUNT_NOT_INT);
  143. m_edtTrapCount.SetFocus();
  144. m_edtTrapCount.SetSel(0, -1);
  145. return E_FAIL;
  146. }
  147. m_edtSeconds.GetWindowText(sSeconds);
  148. sc = AsciiToLong(sSeconds, &nSeconds);
  149. if (FAILED(sc))
  150. {
  151. AfxMessageBox(IDS_ERR_SETTINGS_TRAPSECONDS_NOT_INT);
  152. m_edtSeconds.SetFocus();
  153. m_edtSeconds.SetSel(0, -1);
  154. return E_FAIL;
  155. }
  156. BOOL bThrottleEnabled;
  157. if (GetCheckedRadioButton(IDC_RADIO_ENABLE, IDC_RADIO_DISABLE) == IDC_RADIO_ENABLE)
  158. bThrottleEnabled = TRUE;
  159. else
  160. bThrottleEnabled = FALSE;
  161. if (bThrottleEnabled)
  162. {
  163. if (nTraps < I_MIN_TRAPCOUNT || nTraps > I_MAX_TRAPCOUNT)
  164. {
  165. sError.LoadString(IDS_ERR_SETTINGS_TRAPCOUNT_RANGE);
  166. GenerateRangeMessage(sRangeMessage, I_MIN_TRAPCOUNT, I_MAX_TRAPCOUNT);
  167. sError += sRangeMessage;
  168. AfxMessageBox(sError);
  169. sTraps.Format(_T("%u"), nTraps);
  170. m_edtTrapCount.SetWindowText(sTraps);
  171. m_edtTrapCount.SetFocus();
  172. m_edtTrapCount.SetSel(0, -1);
  173. return E_FAIL;
  174. }
  175. if (nSeconds < I_MIN_SECONDS || nSeconds > I_MAX_SECONDS)
  176. {
  177. sError.LoadString(IDS_SETTINGS_TRAPSECONDS_RANGE);
  178. GenerateRangeMessage(sRangeMessage, I_MIN_SECONDS, I_MAX_SECONDS);
  179. sError += sRangeMessage;
  180. AfxMessageBox(sError);
  181. sSeconds.Format(_T("%u"),nSeconds);
  182. m_edtSeconds.SetWindowText(sSeconds);
  183. m_edtSeconds.SetFocus();
  184. m_edtSeconds.SetSel(0, -1);
  185. return E_FAIL;
  186. }
  187. }
  188. *pnTraps = nTraps;
  189. *pnSeconds = nSeconds;
  190. return S_OK;
  191. }
  192. void CTrapSettingsDlg::TerminateBackgroundThread()
  193. {
  194. if (m_pthRegNotification)
  195. {
  196. m_evTermination.SetEvent();
  197. WaitForSingleObject(m_pthRegNotification->m_hThread, INFINITE);
  198. }
  199. }
  200. void CTrapSettingsDlg::DoDataExchange(CDataExchange* pDX)
  201. {
  202. CDialog::DoDataExchange(pDX);
  203. //{{AFX_DATA_MAP(CTrapSettingsDlg)
  204. DDX_Control(pDX, IDC_STAT_TRAP_LENGTH, m_statTrapLength);
  205. DDX_Control(pDX, IDC_EDIT_MESSAGELENGTH, m_edtMessageLength);
  206. DDX_Control(pDX, IDC_EDIT_TRAP_SECONDS, m_edtSeconds);
  207. DDX_Control(pDX, IDC_EDIT_TRAP_COUNT, m_edtTrapCount);
  208. DDX_Control(pDX, IDC_MSGLENGTHSPN, m_spinMessageLength);
  209. DDX_Control(pDX, IDC_BUTTON_RESET, m_btnResetThrottle);
  210. DDX_Check(pDX, IDC_LIMITMSGLNGTH, m_bLimitMsgLength);
  211. //}}AFX_DATA_MAP
  212. CString sValue;
  213. if (pDX->m_bSaveAndValidate) {
  214. // Saving the value trapsize, seconds, and trapcount is handled by
  215. // CTrapSettingsDlg::OnOK so that it can set the focus back to the
  216. // offending item if the value is out of range. If the data transfer
  217. // fails here, the focus is always set back to the dialog and not
  218. // the offending item (is there a way around this?)
  219. }
  220. else {
  221. m_spinMessageLength.SetRange(I_MIN_MESSAGE_LENGTH, I_MAX_MESSAGE_LENGTH);
  222. m_spinMessageLength.SetPos(g_reg.m_params.m_trapsize.m_dwMaxTrapSize);
  223. DecString(sValue, g_reg.m_params.m_throttle.m_nSeconds);
  224. m_edtSeconds.SetWindowText(sValue);
  225. DecString(sValue, g_reg.m_params.m_throttle.m_nTraps);
  226. m_edtTrapCount.SetWindowText(sValue);
  227. }
  228. }
  229. BEGIN_MESSAGE_MAP(CTrapSettingsDlg, CDialog)
  230. //{{AFX_MSG_MAP(CTrapSettingsDlg)
  231. ON_BN_CLICKED(IDC_LIMITMSGLNGTH, OnLimitMessageLength)
  232. ON_BN_CLICKED(IDC_RADIO_DISABLE, OnRadioDisable)
  233. ON_BN_CLICKED(IDC_RADIO_ENABLE, OnRadioEable)
  234. ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
  235. ON_COMMAND(ID_HELP, OnHelp)
  236. ON_WM_HELPINFO()
  237. ON_WM_CONTEXTMENU()
  238. ON_WM_CLOSE()
  239. ON_MESSAGE(WM_UIREQUEST, OnUIRequest)
  240. //}}AFX_MSG_MAP
  241. END_MESSAGE_MAP()
  242. /////////////////////////////////////////////////////////////////////////////
  243. // CTrapSettingsDlg message handlers
  244. LRESULT CTrapSettingsDlg::OnUIRequest(WPARAM cmd, LPARAM lParam)
  245. {
  246. switch(cmd)
  247. {
  248. case UICMD_ENABLE_RESET:
  249. m_btnResetThrottle.EnableWindow((BOOL)lParam);
  250. break;
  251. default:
  252. break;
  253. }
  254. return (LRESULT)0;
  255. }
  256. void CTrapSettingsDlg::OnLimitMessageLength()
  257. {
  258. // The LimitMsgLength checkbox was clicked.
  259. // Enable/disable the edit control.
  260. // Get the controls.
  261. CButton* pbtnLimitBox = (CButton*) GetDlgItem(IDC_LIMITMSGLNGTH);
  262. CButton *pRadio1 = (CButton*)GetDlgItem(IDC_RADIO1);
  263. CButton *pRadio2 = (CButton*)GetDlgItem(IDC_RADIO2);
  264. // It's checked; enable
  265. if (pbtnLimitBox->GetCheck() == 1)
  266. {
  267. m_edtMessageLength.EnableWindow();
  268. pRadio1->EnableWindow();
  269. pRadio2->EnableWindow();
  270. GetDlgItem(IDC_STATIC_BYTES)->EnableWindow();
  271. m_statTrapLength.EnableWindow();
  272. }
  273. // Disable
  274. else
  275. {
  276. m_edtMessageLength.EnableWindow(FALSE);
  277. pRadio1->EnableWindow(FALSE);
  278. pRadio2->EnableWindow(FALSE);
  279. GetDlgItem(IDC_STATIC_BYTES)->EnableWindow(FALSE);
  280. m_statTrapLength.EnableWindow(FALSE);
  281. }
  282. }
  283. BOOL CTrapSettingsDlg::OnInitDialog()
  284. {
  285. CDialog::OnInitDialog();
  286. CButton *pRadio1 = (CButton*)GetDlgItem(IDC_RADIO1);
  287. CButton *pRadio2 = (CButton*)GetDlgItem(IDC_RADIO2);
  288. m_statTrapLength.EnableWindow(m_bLimitMsgLength);
  289. m_edtMessageLength.EnableWindow(m_bLimitMsgLength);
  290. if (m_bLimitMsgLength)
  291. {
  292. pRadio1->EnableWindow();
  293. pRadio2->EnableWindow();
  294. GetDlgItem(IDC_STATIC_BYTES)->EnableWindow();
  295. }
  296. // Disable
  297. else
  298. {
  299. pRadio1->EnableWindow(FALSE);
  300. pRadio2->EnableWindow(FALSE);
  301. GetDlgItem(IDC_STATIC_BYTES)->EnableWindow(FALSE);
  302. }
  303. if (m_bTrimMessagesFirst)
  304. CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO2);
  305. else
  306. CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO1);
  307. if (m_bThrottleEnabled)
  308. CheckRadioButton(IDC_RADIO_ENABLE, IDC_RADIO_DISABLE, IDC_RADIO_ENABLE);
  309. else
  310. CheckRadioButton(IDC_RADIO_ENABLE, IDC_RADIO_DISABLE, IDC_RADIO_DISABLE);
  311. EnableThrottleWindows(m_bThrottleEnabled);
  312. m_pthRegNotification = AfxBeginThread((AFX_THREADPROC)_thrRun, this);
  313. return TRUE; // return TRUE unless you set the focus to a control
  314. // EXCEPTION: OCX Property Pages should return FALSE
  315. }
  316. void CTrapSettingsDlg::OnOK()
  317. {
  318. LONG nchMessageLength;
  319. LONG nTraps;
  320. LONG nSeconds;
  321. SCODE sc = GetMessageLength(&nchMessageLength);
  322. if (FAILED(sc))
  323. {
  324. return;
  325. }
  326. sc = GetTrapsPerSecond(&nTraps, &nSeconds);
  327. if (FAILED(sc))
  328. {
  329. return;
  330. }
  331. // Pull various values off of the dialog and store them into member variables.
  332. // Note that there are other member variables that are set directly
  333. // as a response to user input.
  334. //===========================================================================
  335. m_bTrimMessagesFirst = (GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO2) == IDC_RADIO2);
  336. m_bThrottleEnabled = (GetCheckedRadioButton(IDC_RADIO_ENABLE, IDC_RADIO_DISABLE) == IDC_RADIO_ENABLE);
  337. if (g_reg.m_params.m_trapsize.m_dwMaxTrapSize != (DWORD) nchMessageLength) {
  338. g_reg.SetDirty(TRUE);
  339. g_reg.m_params.m_trapsize.m_dwMaxTrapSize = nchMessageLength;
  340. }
  341. if(g_reg.m_params.m_throttle.m_nSeconds != nSeconds) {
  342. g_reg.SetDirty(TRUE);
  343. g_reg.m_params.m_throttle.m_nSeconds = nSeconds;
  344. }
  345. if (g_reg.m_params.m_throttle.m_nTraps != nTraps) {
  346. g_reg.SetDirty(TRUE);
  347. g_reg.m_params.m_throttle.m_nTraps = nTraps;
  348. }
  349. TerminateBackgroundThread();
  350. CDialog::OnOK();
  351. }
  352. BOOL CTrapSettingsDlg::EditSettings()
  353. {
  354. m_bLimitMsgLength = g_reg.m_params.m_trapsize.m_bTrimFlag;
  355. m_bTrimMessagesFirst = g_reg.m_params.m_trapsize.m_bTrimMessages;
  356. m_bThrottleEnabled = g_reg.m_params.m_throttle.m_bIsEnabled;
  357. // Save the data.
  358. if (DoModal() == IDOK)
  359. {
  360. if (BOOLS_ARE_DIFFERENT(g_reg.m_params.m_trapsize.m_bTrimFlag, m_bLimitMsgLength)) {
  361. g_reg.m_params.m_trapsize.m_bTrimFlag = m_bLimitMsgLength;
  362. g_reg.SetDirty(TRUE);
  363. }
  364. if (BOOLS_ARE_DIFFERENT(g_reg.m_params.m_trapsize.m_bTrimMessages, m_bTrimMessagesFirst)) {
  365. g_reg.m_params.m_trapsize.m_bTrimMessages = m_bTrimMessagesFirst;
  366. g_reg.SetDirty(TRUE);
  367. }
  368. if (BOOLS_ARE_DIFFERENT(g_reg.m_params.m_throttle.m_bIsEnabled, m_bThrottleEnabled)) {
  369. g_reg.m_params.m_throttle.m_bIsEnabled = m_bThrottleEnabled;
  370. g_reg.SetDirty(TRUE);
  371. }
  372. return TRUE;
  373. }
  374. else {
  375. return FALSE;
  376. }
  377. }
  378. void CTrapSettingsDlg::OnRadioDisable()
  379. {
  380. EnableThrottleWindows(FALSE);
  381. }
  382. void CTrapSettingsDlg::OnRadioEable()
  383. {
  384. EnableThrottleWindows(TRUE);
  385. }
  386. void CTrapSettingsDlg::EnableThrottleWindows(BOOL bEnableThrottle)
  387. {
  388. m_edtSeconds.EnableWindow(bEnableThrottle);
  389. GetDlgItem(IDC_STATIC_MSG)->EnableWindow(bEnableThrottle);
  390. GetDlgItem(IDC_STATIC_NTRAPS)->EnableWindow(bEnableThrottle);
  391. GetDlgItem(IDC_STATIC_INTERVAL)->EnableWindow(bEnableThrottle);
  392. m_edtTrapCount.EnableWindow(bEnableThrottle);
  393. }
  394. //****************************************************************
  395. // CTrapSettingsDlg::OnButtonReset
  396. //
  397. // Reset the extension agent so that it starts sending traps again.
  398. // The extension agent will stop sending traps if the throttle limit
  399. // is exceeded (more than x number of traps per second are set).
  400. //
  401. // Parameters:
  402. // None.
  403. //
  404. // Returns.
  405. // Nothing.
  406. //
  407. //*****************************************************************
  408. void CTrapSettingsDlg::OnButtonReset()
  409. {
  410. if (SUCCEEDED(g_reg.m_params.ResetExtensionAgent())) {
  411. m_btnResetThrottle.EnableWindow(FALSE);
  412. }
  413. }
  414. BOOL CTrapSettingsDlg::OnHelpInfo(HELPINFO *pHelpInfo)
  415. {
  416. if (pHelpInfo->iContextType == HELPINFO_WINDOW &&
  417. pHelpInfo->iCtrlId != IDC_STATIC_MSG &&
  418. pHelpInfo->iCtrlId != IDC_STATIC_BYTES)
  419. {
  420. ::WinHelp ((HWND)pHelpInfo->hItemHandle,
  421. AfxGetApp()->m_pszHelpFilePath,
  422. HELP_WM_HELP,
  423. (ULONG_PTR)g_aHelpIDs_IDD_SETTINGSDLG);
  424. }
  425. return TRUE;
  426. }
  427. void CTrapSettingsDlg::OnContextMenu(CWnd* pWnd, CPoint point)
  428. {
  429. if (this == pWnd)
  430. return;
  431. ::WinHelp (pWnd->m_hWnd,
  432. AfxGetApp()->m_pszHelpFilePath,
  433. HELP_CONTEXTMENU,
  434. (ULONG_PTR)g_aHelpIDs_IDD_SETTINGSDLG);
  435. }
  436. void CTrapSettingsDlg::OnClose()
  437. {
  438. TerminateBackgroundThread();
  439. CDialog::OnClose();
  440. }
  441. void CTrapSettingsDlg::OnCancel()
  442. {
  443. TerminateBackgroundThread();
  444. CDialog::OnCancel();
  445. }