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.

534 lines
14 KiB

  1. /*
  2. File tcpipui.c
  3. Dialog that edits the tcpip properties.
  4. Paul Mayfield, 10/9/97
  5. */
  6. #include "rassrv.h"
  7. #define IDH_DISABLEHELP ((DWORD)-1)
  8. // Help maps
  9. static const DWORD phmTcpipui[] =
  10. {
  11. CID_NetTab_Tcpipui_CB_ExposeNetwork, IDH_NetTab_Tcpipui_CB_ExposeNetwork,
  12. CID_NetTab_Tcpipui_EB_Start, IDH_NetTab_Tcpipui_EB_Start,
  13. CID_NetTab_Tcpipui_RB_Dhcp, IDH_NetTab_Tcpipui_RB_Dhcp,
  14. CID_NetTab_Tcpipui_RB_StaticPool, IDH_NetTab_Tcpipui_RB_StaticPool,
  15. CID_NetTab_Tcpipui_EB_Mask, IDH_NetTab_Tcpipui_EB_Mask,
  16. CID_NetTab_Tcpipui_CB_CallerSpec, IDH_NetTab_Tcpipui_CB_CallerSpec,
  17. CID_NetTab_Tcpipui_EB_Range, IDH_DISABLEHELP,
  18. CID_NetTab_Tcpipui_EB_Total, IDH_DISABLEHELP,
  19. 0, 0
  20. };
  21. // Error reporting
  22. void
  23. TcpipUiDisplayError(
  24. HWND hwnd,
  25. DWORD dwErr)
  26. {
  27. ErrDisplayError(
  28. hwnd,
  29. dwErr,
  30. ERR_TCPIPPROP_CATAGORY,
  31. 0,
  32. Globals.dwErrorData);
  33. }
  34. // Converts a dword ip address (in host order) to a wide character string
  35. //
  36. DWORD
  37. TcpipDwordToAddr(
  38. DWORD dwAddr,
  39. PWCHAR pszAddr)
  40. {
  41. wsprintfW(
  42. pszAddr,
  43. L"%d.%d.%d.%d",
  44. FIRST_IPADDRESS (dwAddr),
  45. SECOND_IPADDRESS(dwAddr),
  46. THIRD_IPADDRESS (dwAddr),
  47. FOURTH_IPADDRESS(dwAddr));
  48. return NO_ERROR;
  49. }
  50. //
  51. // Returns NO_ERROR if the given address is a valid IP pool.
  52. // The offending component is returned in lpdwErrReason.
  53. // See RASIP_F_* values
  54. //
  55. DWORD
  56. TcpipUiValidatePool(
  57. IN DWORD dwAddress,
  58. IN DWORD dwEnd,
  59. OUT LPDWORD lpdwErrReason
  60. )
  61. {
  62. DWORD i, dwMaskMask;
  63. DWORD dwLowIp, dwHighIp, dwErr;
  64. // Initialize
  65. //
  66. dwLowIp = MAKEIPADDRESS(1,0,0,0);
  67. dwHighIp = MAKEIPADDRESS(224,0,0,0);
  68. // Make sure that the netId is a valid class
  69. //
  70. if ((dwAddress < dwLowIp) ||
  71. (dwAddress >= dwHighIp) ||
  72. (FIRST_IPADDRESS(dwAddress) == 127))
  73. {
  74. *lpdwErrReason = SID_TCPIP_InvalidNetId;
  75. return ERROR_BAD_FORMAT;
  76. }
  77. // Make sure the pool base is not more specific than
  78. // the mask
  79. //
  80. if (dwAddress >= dwEnd)
  81. {
  82. *lpdwErrReason = SID_TCPIP_NetidMaskSame;
  83. return ERROR_BAD_FORMAT;
  84. }
  85. return NO_ERROR;
  86. }
  87. // Enables/disables windows in the dialog box depending
  88. // on the tcpip parameters
  89. //
  90. DWORD
  91. TcpipEnableWindows(
  92. HWND hwndDlg,
  93. TCPIP_PARAMS * pTcpipParams)
  94. {
  95. HWND hwndStart = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Start);
  96. HWND hwndMask = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Mask);
  97. if (hwndStart)
  98. {
  99. EnableWindow(hwndStart, !pTcpipParams->bUseDhcp);
  100. }
  101. if (hwndMask)
  102. {
  103. EnableWindow(hwndMask, !pTcpipParams->bUseDhcp);
  104. }
  105. return NO_ERROR;
  106. }
  107. // Generates number formatting data
  108. //
  109. DWORD
  110. TcpipGenerateNumberFormatter (
  111. NUMBERFMT * pNumFmt)
  112. {
  113. CHAR pszNeg[64], pszLz[2];
  114. ZeroMemory (pNumFmt->lpDecimalSep, 4);
  115. pNumFmt->NumDigits = 0;
  116. pNumFmt->Grouping = 3;
  117. GetLocaleInfoA (LOCALE_SYSTEM_DEFAULT,
  118. LOCALE_ILZERO,
  119. pszLz,
  120. 2);
  121. GetLocaleInfoW (LOCALE_SYSTEM_DEFAULT,
  122. LOCALE_STHOUSAND,
  123. pNumFmt->lpThousandSep,
  124. 2);
  125. GetLocaleInfoA (LOCALE_SYSTEM_DEFAULT,
  126. LOCALE_INEGNUMBER,
  127. pszNeg,
  128. 2);
  129. pNumFmt->LeadingZero = atoi(pszLz);
  130. pNumFmt->NegativeOrder = atoi(pszNeg);
  131. return NO_ERROR;
  132. }
  133. // Formats an unsigned number with commas, etc.
  134. //
  135. PWCHAR
  136. TcpipFormatDword(
  137. DWORD dwVal)
  138. {
  139. static WCHAR pszRet[64], pszDSep[2], pszTSep[2] = {0,0};
  140. static NUMBERFMT NumberFmt = {0,0,0,pszDSep,pszTSep,0};
  141. static BOOL bInitialized = FALSE;
  142. WCHAR pszNum[64];
  143. // Stringize the number
  144. wsprintfW (pszNum, L"%u", dwVal);
  145. pszRet[0] = (WCHAR)0;
  146. // Initialize number formatting
  147. if (!bInitialized)
  148. {
  149. TcpipGenerateNumberFormatter (&NumberFmt);
  150. bInitialized = TRUE;
  151. }
  152. // Get the local version of this
  153. GetNumberFormatW (
  154. LOCALE_SYSTEM_DEFAULT,
  155. 0,
  156. pszNum,
  157. &NumberFmt,
  158. pszRet,
  159. sizeof(pszRet) / sizeof(WCHAR));
  160. return pszRet;
  161. }
  162. // Sets the range and total incoming clients fields of the tcpip properties
  163. // dialog.
  164. //
  165. DWORD
  166. TcpipReCalcPoolSize(
  167. HWND hwndDlg)
  168. {
  169. HWND hwndStart, hwndEnd, hwndTotal;
  170. DWORD dwStart = 0, dwEnd = 0, dwTotal=0;
  171. WCHAR pszBuf[256];
  172. hwndStart = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Start);
  173. hwndEnd = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Mask);
  174. hwndTotal = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Total);
  175. if (hwndStart)
  176. {
  177. SendMessage(hwndStart, IP_GETADDRESS, 0, (LPARAM)&dwStart);
  178. }
  179. if (hwndEnd)
  180. {
  181. SendMessage(hwndEnd, IP_GETADDRESS, 0, (LPARAM)&dwEnd);
  182. }
  183. //For whistler bug 281545 gangz
  184. //
  185. if ( 0 == dwStart )
  186. {
  187. dwTotal = dwEnd - dwStart;
  188. }
  189. else
  190. {
  191. if (dwEnd >= dwStart)
  192. {
  193. dwTotal = dwEnd - dwStart + 1;
  194. }
  195. else
  196. {
  197. dwTotal = 0;
  198. }
  199. }
  200. if (hwndTotal)
  201. {
  202. SetWindowTextW(hwndTotal, TcpipFormatDword(dwTotal) );
  203. }
  204. return NO_ERROR;
  205. }
  206. // Initializes the Tcpip Properties Dialog
  207. //
  208. DWORD
  209. TcpipInitDialog(
  210. HWND hwndDlg,
  211. LPARAM lParam)
  212. {
  213. LPSTR pszAddr;
  214. TCPIP_PARAMS * pTcpipParams = (TCPIP_PARAMS *)
  215. (((PROT_EDIT_DATA*)lParam)->pbData);
  216. HWND hwndFrom, hwndTo;
  217. WCHAR pszAddrW[256];
  218. // Store the parameters with the window handle
  219. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
  220. // Set the network exposure check
  221. SendDlgItemMessage(
  222. hwndDlg,
  223. CID_NetTab_Tcpipui_CB_ExposeNetwork,
  224. BM_SETCHECK,
  225. (((PROT_EDIT_DATA*)lParam)->bExpose) ? BST_CHECKED : BST_UNCHECKED,
  226. 0);
  227. // Set the address assignmnet radio buttons
  228. SendDlgItemMessage(
  229. hwndDlg,
  230. CID_NetTab_Tcpipui_RB_Dhcp,
  231. BM_SETCHECK,
  232. (pTcpipParams->bUseDhcp) ? BST_CHECKED : BST_UNCHECKED,
  233. 0);
  234. // Set the address assignmnet radio buttons
  235. SendDlgItemMessage(
  236. hwndDlg,
  237. CID_NetTab_Tcpipui_RB_StaticPool,
  238. BM_SETCHECK,
  239. (pTcpipParams->bUseDhcp) ? BST_UNCHECKED : BST_CHECKED,
  240. 0);
  241. // Set the "allow caller to specify ip address" check
  242. SendDlgItemMessage(
  243. hwndDlg,
  244. CID_NetTab_Tcpipui_CB_CallerSpec,
  245. BM_SETCHECK,
  246. (pTcpipParams->bCaller) ? BST_CHECKED : BST_UNCHECKED,
  247. 0);
  248. // Set the text of the ip addresses
  249. if (pTcpipParams->dwPoolStart != 0)
  250. {
  251. hwndFrom = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Start);
  252. hwndTo = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Mask);
  253. TcpipDwordToAddr(pTcpipParams->dwPoolStart, pszAddrW);
  254. if (hwndFrom)
  255. {
  256. SetWindowText(hwndFrom, pszAddrW);
  257. }
  258. TcpipDwordToAddr(pTcpipParams->dwPoolEnd, pszAddrW);
  259. if (hwndTo)
  260. {
  261. SetWindowText(hwndTo, pszAddrW);
  262. }
  263. }
  264. // Enable/disable windows as per the settings
  265. TcpipEnableWindows(hwndDlg, pTcpipParams);
  266. return NO_ERROR;
  267. }
  268. // Gets the settings from the ui and puts them into
  269. // the tcpip parameter structure.
  270. //
  271. DWORD
  272. TcpipGetUISettings(
  273. IN HWND hwndDlg,
  274. OUT PROT_EDIT_DATA * pEditData,
  275. OUT LPDWORD lpdwFrom,
  276. OUT LPDWORD lpdwTo)
  277. {
  278. TCPIP_PARAMS * pTcpipParams =
  279. (TCPIP_PARAMS *) pEditData->pbData;
  280. HWND hwndFrom, hwndTo;
  281. hwndFrom = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Start);
  282. hwndTo = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Mask);
  283. pEditData->bExpose =
  284. SendDlgItemMessage(
  285. hwndDlg,
  286. CID_NetTab_Tcpipui_CB_ExposeNetwork,
  287. BM_GETCHECK,
  288. 0,
  289. 0) == BST_CHECKED;
  290. if (hwndFrom)
  291. {
  292. SendMessage(
  293. hwndFrom,
  294. IP_GETADDRESS,
  295. 0,
  296. (LPARAM)&pTcpipParams->dwPoolStart);
  297. }
  298. if (hwndTo)
  299. {
  300. SendMessage(
  301. hwndTo,
  302. IP_GETADDRESS,
  303. 0,
  304. (LPARAM)&pTcpipParams->dwPoolEnd);
  305. }
  306. *lpdwFrom = pTcpipParams->dwPoolStart;
  307. *lpdwTo = pTcpipParams->dwPoolEnd;
  308. return NO_ERROR;
  309. }
  310. DWORD
  311. TcpipUiHandleOk(
  312. IN HWND hwndDlg)
  313. {
  314. PROT_EDIT_DATA* pData = NULL;
  315. TCPIP_PARAMS * pParams = NULL;
  316. DWORD dwErr, dwId = SID_TCPIP_InvalidPool;
  317. DWORD dwStart = 0, dwEnd = 0;
  318. PWCHAR pszMessage = NULL;
  319. MSGARGS MsgArgs;
  320. // Get the context
  321. //
  322. pData = (PROT_EDIT_DATA*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  323. if (pData == NULL)
  324. {
  325. return ERROR_CAN_NOT_COMPLETE;
  326. }
  327. // Read values from the UI
  328. //
  329. dwErr = TcpipGetUISettings(hwndDlg, pData, &dwStart, &dwEnd);
  330. if (dwErr != NO_ERROR)
  331. {
  332. return dwErr;
  333. }
  334. // Validate the pool if one was entered
  335. //
  336. pParams = (TCPIP_PARAMS *)pData->pbData;
  337. if (pParams->bUseDhcp == FALSE)
  338. {
  339. dwErr = TcpipUiValidatePool(
  340. dwStart,
  341. dwEnd,
  342. &dwId);
  343. if (dwErr != NO_ERROR)
  344. {
  345. ZeroMemory(&MsgArgs, sizeof(MsgArgs));
  346. MsgArgs.dwFlags = MB_OK;
  347. MsgDlgUtil(
  348. hwndDlg,
  349. dwId,
  350. &MsgArgs,
  351. Globals.hInstDll,
  352. SID_DEFAULT_MSG_TITLE);
  353. }
  354. }
  355. return dwErr;
  356. }
  357. // Dialog proc that governs the tcpip settings dialog
  358. INT_PTR
  359. CALLBACK
  360. TcpipSettingsDialogProc (
  361. HWND hwndDlg,
  362. UINT uMsg,
  363. WPARAM wParam,
  364. LPARAM lParam)
  365. {
  366. switch (uMsg) {
  367. case WM_INITDIALOG:
  368. TcpipInitDialog(hwndDlg, lParam);
  369. return FALSE;
  370. case WM_HELP:
  371. case WM_CONTEXTMENU:
  372. {
  373. RasSrvHelp (hwndDlg, uMsg, wParam, lParam, phmTcpipui);
  374. break;
  375. }
  376. case WM_DESTROY:
  377. // Cleanup the work done at WM_INITDIALOG
  378. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, 0);
  379. break;
  380. case WM_COMMAND:
  381. {
  382. TCPIP_PARAMS * pTcpipParams = (TCPIP_PARAMS *)
  383. (((PROT_EDIT_DATA*)
  384. GetWindowLongPtr(hwndDlg, GWLP_USERDATA))->pbData);
  385. switch (wParam)
  386. {
  387. case IDOK:
  388. if (TcpipUiHandleOk(hwndDlg) == NO_ERROR)
  389. {
  390. EndDialog(hwndDlg, 1);
  391. }
  392. break;
  393. case IDCANCEL:
  394. EndDialog(hwndDlg, 0);
  395. break;
  396. case CID_NetTab_Tcpipui_RB_Dhcp:
  397. pTcpipParams->bUseDhcp = TRUE;
  398. TcpipEnableWindows(hwndDlg, pTcpipParams);
  399. break;
  400. case CID_NetTab_Tcpipui_RB_StaticPool:
  401. pTcpipParams->bUseDhcp = FALSE;
  402. TcpipEnableWindows(hwndDlg, pTcpipParams);
  403. break;
  404. case CID_NetTab_Tcpipui_CB_CallerSpec:
  405. pTcpipParams->bCaller = (BOOL)
  406. SendDlgItemMessage(
  407. hwndDlg,
  408. CID_NetTab_Tcpipui_CB_CallerSpec,
  409. BM_GETCHECK,
  410. 0,
  411. 0);
  412. break;
  413. }
  414. // Recal the pool size as appropriate
  415. //
  416. if (HIWORD(wParam) == EN_CHANGE)
  417. {
  418. if (LOWORD(wParam) == CID_NetTab_Tcpipui_EB_Start ||
  419. LOWORD(wParam) == CID_NetTab_Tcpipui_EB_Mask)
  420. {
  421. TcpipReCalcPoolSize(hwndDlg);
  422. }
  423. }
  424. break;
  425. }
  426. }
  427. return FALSE;
  428. }
  429. // Edits tcp ip protocol properties
  430. //
  431. DWORD
  432. TcpipEditProperties(
  433. HWND hwndParent,
  434. PROT_EDIT_DATA * pEditData,
  435. BOOL * pbCommit)
  436. {
  437. DWORD dwErr;
  438. int ret;
  439. // Initialize the ip address custom controls
  440. IpAddrInit(Globals.hInstDll, SID_TCPIP_TITLE, SID_TCPIP_BADRANGE);
  441. // Popup the dialog box
  442. ret = (int) DialogBoxParam(
  443. Globals.hInstDll,
  444. MAKEINTRESOURCE(DID_NetTab_Tcpipui),
  445. hwndParent,
  446. TcpipSettingsDialogProc,
  447. (LPARAM)pEditData);
  448. if (ret == -1)
  449. {
  450. TcpipUiDisplayError(hwndParent, ERR_TCPIP_CANT_DISPLAY);
  451. }
  452. // If ok was pressed, save off the new settings
  453. *pbCommit = FALSE;
  454. if (ret && ret != -1)
  455. {
  456. *pbCommit = TRUE;
  457. }
  458. return NO_ERROR;
  459. }