Source code of Windows XP (NT5)
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.

411 lines
11 KiB

  1. //-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: DLBBKUP.CPP
  7. //
  8. // Contents: Implementation for CIpBackUpDlg
  9. //
  10. // Notes: CIpBackUpDlg is the modal dialog to handle the fallback static
  11. // TCP/IP settings
  12. //
  13. // Author: nsun 02/15/2000
  14. //-----------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "tcpipobj.h"
  18. #include "ncatlui.h"
  19. #include "ncstl.h"
  20. #include "ncui.h"
  21. #include "ncreg.h"
  22. #include "resource.h"
  23. #include "tcpconst.h"
  24. //#include "tcphelp.h"
  25. #include "tcpmacro.h"
  26. #include "tcputil.h"
  27. #include "ncbase.h"
  28. #include "dlgbkup.h"
  29. #include "tcperror.h"
  30. //
  31. // CIpBackUpDlg
  32. //
  33. CIpBackUpDlg::CIpBackUpDlg (CTcpipcfg * ptcpip,
  34. const DWORD* adwHelpIDs) :
  35. m_ptcpip(ptcpip),
  36. m_fModified(FALSE)
  37. {
  38. m_pAdapterInfo = ptcpip->GetConnectionAdapterInfo();
  39. m_adwHelpIDs = adwHelpIDs;
  40. }
  41. CIpBackUpDlg::~CIpBackUpDlg()
  42. {
  43. }
  44. LRESULT CIpBackUpDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& fHandled)
  45. {
  46. // limit the field ranges for the address fields
  47. m_ipAddr.Create(m_hWnd, IDC_BKUP_IPADDR);
  48. m_ipAddr.SetFieldRange(0, c_iIPADDR_FIELD_1_LOW, c_iIPADDR_FIELD_1_HIGH);
  49. m_ipMask.Create(m_hWnd, IDC_BKUP_SUBNET);
  50. m_ipDefGw.Create(m_hWnd, IDC_BKUP_GATEWAY);
  51. m_ipDefGw.SetFieldRange(0, c_iIPADDR_FIELD_1_LOW, c_iIPADDR_FIELD_1_HIGH);
  52. m_ipPrefferredDns.Create(m_hWnd, IDC_BKUP_PREF_DNS);
  53. m_ipPrefferredDns.SetFieldRange(0, c_iIPADDR_FIELD_1_LOW, c_iIPADDR_FIELD_1_HIGH);
  54. m_ipAlternateDns.Create(m_hWnd, IDC_BKUP_ALT_DNS);
  55. m_ipAlternateDns.SetFieldRange(0, c_iIPADDR_FIELD_1_LOW, c_iIPADDR_FIELD_1_HIGH);
  56. m_ipPrefferredWins.Create(m_hWnd, IDC_BKUP_WINS1);
  57. m_ipPrefferredWins.SetFieldRange(0, c_iIPADDR_FIELD_1_LOW, c_iIPADDR_FIELD_1_HIGH);
  58. m_ipAlternateWins.Create(m_hWnd, IDC_BKUP_WINS2);
  59. m_ipAlternateWins.SetFieldRange(0, c_iIPADDR_FIELD_1_LOW, c_iIPADDR_FIELD_1_HIGH);
  60. int nIdRdbutton;
  61. if (m_pAdapterInfo->m_BackupInfo.m_fAutoNet)
  62. {
  63. nIdRdbutton = IDC_BKUP_RD_AUTO;
  64. }
  65. else
  66. {
  67. nIdRdbutton = IDC_BKUP_RD_USER;
  68. }
  69. CheckDlgButton(IDC_BKUP_RD_AUTO,
  70. m_pAdapterInfo->m_BackupInfo.m_fAutoNet ? BST_CHECKED : BST_UNCHECKED);
  71. CheckDlgButton(IDC_BKUP_RD_USER,
  72. m_pAdapterInfo->m_BackupInfo.m_fAutoNet ? BST_UNCHECKED : BST_CHECKED);
  73. if (!m_pAdapterInfo->m_BackupInfo.m_fAutoNet)
  74. {
  75. m_ipAddr.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strIpAddr.c_str());
  76. m_ipMask.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strSubnetMask.c_str());
  77. m_ipDefGw.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strDefGw.c_str());
  78. m_ipPrefferredDns.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strPreferredDns.c_str());
  79. m_ipAlternateDns.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strAlternateDns.c_str());
  80. m_ipPrefferredWins.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strPreferredWins.c_str());
  81. m_ipAlternateWins.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strAlternateWins.c_str());
  82. }
  83. EnableControls();
  84. m_fModified = FALSE;
  85. return 0;
  86. }
  87. LRESULT CIpBackUpDlg::OnDestroyDialog(UINT uMsg, WPARAM wParam,
  88. LPARAM lParam, BOOL& fHandled)
  89. {
  90. return 0;
  91. }
  92. LRESULT CIpBackUpDlg::OnContextMenu(UINT uMsg, WPARAM wParam,
  93. LPARAM lParam, BOOL& fHandled)
  94. {
  95. ShowContextHelp(m_hWnd, HELP_CONTEXTMENU, m_adwHelpIDs);
  96. return 0;
  97. }
  98. LRESULT CIpBackUpDlg::OnHelp(UINT uMsg, WPARAM wParam,
  99. LPARAM lParam, BOOL& fHandled)
  100. {
  101. LPHELPINFO lphi = reinterpret_cast<LPHELPINFO>(lParam);
  102. Assert(lphi);
  103. if (HELPINFO_WINDOW == lphi->iContextType)
  104. {
  105. ShowContextHelp(static_cast<HWND>(lphi->hItemHandle), HELP_WM_HELP,
  106. m_adwHelpIDs);
  107. }
  108. return 0;
  109. }
  110. LRESULT CIpBackUpDlg::OnCancel(int idCtrl, LPNMHDR pnmh, BOOL& fHandled)
  111. {
  112. return 0;
  113. }
  114. LRESULT CIpBackUpDlg::OnKillActive(int idCtrl, LPNMHDR pnmh, BOOL& fHandled)
  115. {
  116. BOOL nResult = PSNRET_NOERROR;
  117. BOOL fError = FALSE; // Allow page to lose active status
  118. HWND hWndFocus = 0;
  119. // If the ip address and subnet mask on this page mismatch,
  120. // just raise error and do not update the UI
  121. if (m_ipAddr.IsBlank() && !m_ipMask.IsBlank())
  122. {
  123. NcMsgBox(m_hWnd, IDS_MSFT_TCP_TEXT, IDS_INVALID_NO_IP,
  124. MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  125. hWndFocus = (HWND) m_ipAddr;
  126. fError = TRUE;
  127. }
  128. else if (!m_ipAddr.IsBlank() && m_ipMask.IsBlank())
  129. {
  130. NcMsgBox(m_hWnd, IDS_MSFT_TCP_TEXT, IDS_INVALID_NOSUBNET,
  131. MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  132. hWndFocus = (HWND) m_ipMask;
  133. fError = TRUE;
  134. }
  135. // Now, update in memory structure
  136. if (!fError && m_fModified)
  137. {
  138. UpdateInfo();
  139. if (!m_pAdapterInfo->m_BackupInfo.m_fAutoNet)
  140. {
  141. IP_VALIDATION_ERR err = IsValidIpandSubnet(m_pAdapterInfo->m_BackupInfo.m_strIpAddr.c_str(),
  142. m_pAdapterInfo->m_BackupInfo.m_strSubnetMask.c_str());
  143. // validate IP address and subnet mask
  144. if (ERR_NONE != err)
  145. {
  146. NcMsgBox(m_hWnd, IDS_MSFT_TCP_TEXT, GetIPValidationErrorMessageID(err),
  147. MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  148. fError = TRUE;
  149. hWndFocus = (HWND) m_ipMask;
  150. }
  151. if (!fError && !IsContiguousSubnet(m_pAdapterInfo->m_BackupInfo.m_strSubnetMask.c_str()))
  152. {
  153. NcMsgBox(m_hWnd, IDS_MSFT_TCP_TEXT, IDS_ERROR_UNCONTIGUOUS_SUBNET,
  154. MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  155. fError = TRUE;
  156. hWndFocus = (HWND) m_ipMask;
  157. }
  158. }
  159. }
  160. //we need to change focus to the control that contains invalidate data
  161. if (fError && hWndFocus)
  162. ::SetFocus(hWndFocus);
  163. ::SetWindowLongPtr(m_hWnd, DWLP_MSGRESULT, fError);
  164. return fError;
  165. }
  166. LRESULT CIpBackUpDlg::OnApply(int idCtrl, LPNMHDR pnmh, BOOL& fHandled)
  167. {
  168. BOOL nResult = PSNRET_NOERROR;
  169. if (m_fModified)
  170. {
  171. m_pAdapterInfo->m_fBackUpSettingChanged = TRUE;
  172. m_ptcpip->SetSecondMemoryModified();
  173. m_fModified = FALSE; // this page is no longer modified
  174. }
  175. ::SetWindowLongPtr(m_hWnd, DWLP_MSGRESULT, nResult);
  176. return nResult;
  177. }
  178. LRESULT CIpBackUpDlg::OnIpFieldChange(int idCtrl, LPNMHDR pnmh, BOOL& fHandled)
  179. {
  180. LPNMIPADDRESS lpnmipa;
  181. int iLow = c_iIpLow;
  182. int iHigh = c_iIpHigh;
  183. switch(idCtrl)
  184. {
  185. case IDC_BKUP_IPADDR:
  186. case IDC_BKUP_GATEWAY:
  187. case IDC_BKUP_PREF_DNS:
  188. case IDC_BKUP_ALT_DNS:
  189. case IDC_BKUP_WINS1:
  190. case IDC_BKUP_WINS2:
  191. lpnmipa = (LPNMIPADDRESS) pnmh;
  192. if (0==lpnmipa->iField)
  193. {
  194. iLow = c_iIPADDR_FIELD_1_LOW;
  195. iHigh = c_iIPADDR_FIELD_1_HIGH;
  196. };
  197. IpCheckRange(lpnmipa,
  198. m_hWnd,
  199. iLow,
  200. iHigh,
  201. (IDC_BKUP_IPADDR == idCtrl || IDC_BKUP_GATEWAY == idCtrl)
  202. );
  203. break;
  204. case IDC_BKUP_SUBNET:
  205. lpnmipa = (LPNMIPADDRESS) pnmh;
  206. IpCheckRange(lpnmipa, m_hWnd, iLow, iHigh);
  207. break;
  208. default:
  209. break;
  210. }
  211. return 0;
  212. }
  213. void CIpBackUpDlg::UpdateInfo()
  214. {
  215. m_pAdapterInfo->m_BackupInfo.m_fAutoNet = IsDlgButtonChecked(IDC_BKUP_RD_AUTO);
  216. if (!m_pAdapterInfo->m_BackupInfo.m_fAutoNet)
  217. {
  218. GetIpCtrlAddress(m_ipAddr, &m_pAdapterInfo->m_BackupInfo.m_strIpAddr);
  219. GetIpCtrlAddress(m_ipMask, &m_pAdapterInfo->m_BackupInfo.m_strSubnetMask);
  220. GetIpCtrlAddress(m_ipDefGw, &m_pAdapterInfo->m_BackupInfo.m_strDefGw);
  221. GetIpCtrlAddress(m_ipPrefferredDns, &m_pAdapterInfo->m_BackupInfo.m_strPreferredDns);
  222. GetIpCtrlAddress(m_ipAlternateDns, &m_pAdapterInfo->m_BackupInfo.m_strAlternateDns);
  223. GetIpCtrlAddress(m_ipPrefferredWins, &m_pAdapterInfo->m_BackupInfo.m_strPreferredWins);
  224. GetIpCtrlAddress(m_ipAlternateWins, &m_pAdapterInfo->m_BackupInfo.m_strAlternateWins);
  225. }
  226. }
  227. LRESULT CIpBackUpDlg::OnAutoNet(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& fHandled)
  228. {
  229. switch(wNotifyCode)
  230. {
  231. case BN_CLICKED:
  232. case BN_DOUBLECLICKED:
  233. EnableControls();
  234. break;
  235. }
  236. return 0;
  237. }
  238. LRESULT CIpBackUpDlg::OnUseConfig(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& fHandled)
  239. {
  240. switch(wNotifyCode)
  241. {
  242. case BN_CLICKED:
  243. case BN_DOUBLECLICKED:
  244. EnableControls();
  245. break;
  246. }
  247. return 0;
  248. }
  249. LRESULT CIpBackUpDlg::OnIpctrl(WORD wNotifyCode, WORD wID,
  250. HWND hWndCtl, BOOL& fHandled)
  251. {
  252. switch (wNotifyCode)
  253. {
  254. case EN_CHANGE:
  255. PageModified();
  256. break;
  257. }
  258. return 0;
  259. }
  260. LRESULT CIpBackUpDlg::OnIpAddrSub(WORD wNotifyCode, WORD wID,
  261. HWND hWndCtl, BOOL& fHandled)
  262. {
  263. switch (wNotifyCode)
  264. {
  265. case EN_CHANGE:
  266. PageModified();
  267. break;
  268. case EN_SETFOCUS:
  269. // if the subnet mask is blank, create a mask and insert it into
  270. // the control
  271. if (!m_ipAddr.IsBlank() && m_ipMask.IsBlank())
  272. {
  273. tstring strSubnetMask;
  274. tstring strIpAddress;
  275. m_ipAddr.GetAddress(&strIpAddress);
  276. // generate the mask and update the control, and internal structure
  277. GenerateSubnetMask(m_ipAddr, &strSubnetMask);
  278. m_ipMask.SetAddress(strSubnetMask.c_str());
  279. ReplaceFirstAddress(&(m_pAdapterInfo->m_vstrSubnetMask),
  280. strSubnetMask.c_str());
  281. }
  282. break;
  283. }
  284. return 0;
  285. }
  286. void CIpBackUpDlg::EnableControls()
  287. {
  288. BOOL fEnableUserConfig = !IsDlgButtonChecked(IDC_BKUP_RD_AUTO);
  289. ::EnableWindow(GetDlgItem(IDC_BKUP_IPADDR_TEXT), fEnableUserConfig);
  290. ::EnableWindow(GetDlgItem(IDC_BKUP_IPADDR), fEnableUserConfig);
  291. ::EnableWindow(GetDlgItem(IDC_BKUP_SUBNET_TEXT), fEnableUserConfig);
  292. ::EnableWindow(GetDlgItem(IDC_BKUP_SUBNET), fEnableUserConfig);
  293. ::EnableWindow(GetDlgItem(IDC_BKUP_GATEWAY_TEXT), fEnableUserConfig);
  294. ::EnableWindow(GetDlgItem(IDC_BKUP_GATEWAY), fEnableUserConfig);
  295. ::EnableWindow(GetDlgItem(IDC_BKUP_PREF_DNS_TEXT), fEnableUserConfig);
  296. ::EnableWindow(GetDlgItem(IDC_BKUP_PREF_DNS), fEnableUserConfig);
  297. ::EnableWindow(GetDlgItem(IDC_BKUP_ALT_DNS_TEXT), fEnableUserConfig);
  298. ::EnableWindow(GetDlgItem(IDC_BKUP_ALT_DNS), fEnableUserConfig);
  299. ::EnableWindow(GetDlgItem(IDC_BKUP_WINS1_TEXT), fEnableUserConfig);
  300. ::EnableWindow(GetDlgItem(IDC_BKUP_WINS1), fEnableUserConfig);
  301. ::EnableWindow(GetDlgItem(IDC_WINS2_TEXT), fEnableUserConfig);
  302. ::EnableWindow(GetDlgItem(IDC_BKUP_WINS2), fEnableUserConfig);
  303. if (fEnableUserConfig)
  304. {
  305. m_ipAddr.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strIpAddr.c_str());
  306. m_ipMask.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strSubnetMask.c_str());
  307. m_ipDefGw.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strDefGw.c_str());
  308. m_ipPrefferredDns.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strPreferredDns.c_str());
  309. m_ipAlternateDns.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strAlternateDns.c_str());
  310. m_ipPrefferredWins.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strPreferredWins.c_str());
  311. m_ipAlternateWins.SetAddress(m_pAdapterInfo->m_BackupInfo.m_strAlternateWins.c_str());
  312. }
  313. else
  314. {
  315. m_ipAddr.ClearAddress();
  316. m_ipMask.ClearAddress();
  317. m_ipDefGw.ClearAddress();
  318. m_ipPrefferredDns.ClearAddress();
  319. m_ipAlternateDns.ClearAddress();
  320. m_ipPrefferredWins.ClearAddress();
  321. m_ipAlternateWins.ClearAddress();
  322. }
  323. }
  324. void CIpBackUpDlg::GetIpCtrlAddress(IpControl & IpCtrl, tstring * pstr)
  325. {
  326. Assert(pstr);
  327. if (IpCtrl.IsBlank())
  328. {
  329. *pstr = c_szEmpty;
  330. }
  331. else
  332. {
  333. IpCtrl.GetAddress(pstr);
  334. }
  335. }