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.

511 lines
13 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. EnableWindow(
  96. GetDlgItem(
  97. hwndDlg,
  98. CID_NetTab_Tcpipui_EB_Start),
  99. !pTcpipParams->bUseDhcp);
  100. EnableWindow(
  101. GetDlgItem(
  102. hwndDlg,
  103. CID_NetTab_Tcpipui_EB_Mask),
  104. !pTcpipParams->bUseDhcp);
  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. SendMessage(hwndStart, IP_GETADDRESS, 0, (LPARAM)&dwStart);
  176. SendMessage(hwndEnd, IP_GETADDRESS, 0, (LPARAM)&dwEnd);
  177. //For whistler bug 281545 gangz
  178. //
  179. if ( 0 == dwStart )
  180. {
  181. dwTotal = dwEnd - dwStart;
  182. }
  183. else
  184. {
  185. if (dwEnd >= dwStart)
  186. {
  187. dwTotal = dwEnd - dwStart + 1;
  188. }
  189. else
  190. {
  191. dwTotal = 0;
  192. }
  193. }
  194. SetWindowTextW(hwndTotal, TcpipFormatDword(dwTotal) );
  195. return NO_ERROR;
  196. }
  197. // Initializes the Tcpip Properties Dialog
  198. //
  199. DWORD
  200. TcpipInitDialog(
  201. HWND hwndDlg,
  202. LPARAM lParam)
  203. {
  204. LPSTR pszAddr;
  205. TCPIP_PARAMS * pTcpipParams = (TCPIP_PARAMS *)
  206. (((PROT_EDIT_DATA*)lParam)->pbData);
  207. HWND hwndFrom, hwndTo;
  208. WCHAR pszAddrW[256];
  209. // Store the parameters with the window handle
  210. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
  211. // Set the network exposure check
  212. SendDlgItemMessage(
  213. hwndDlg,
  214. CID_NetTab_Tcpipui_CB_ExposeNetwork,
  215. BM_SETCHECK,
  216. (((PROT_EDIT_DATA*)lParam)->bExpose) ? BST_CHECKED : BST_UNCHECKED,
  217. 0);
  218. // Set the address assignmnet radio buttons
  219. SendDlgItemMessage(
  220. hwndDlg,
  221. CID_NetTab_Tcpipui_RB_Dhcp,
  222. BM_SETCHECK,
  223. (pTcpipParams->bUseDhcp) ? BST_CHECKED : BST_UNCHECKED,
  224. 0);
  225. // Set the address assignmnet radio buttons
  226. SendDlgItemMessage(
  227. hwndDlg,
  228. CID_NetTab_Tcpipui_RB_StaticPool,
  229. BM_SETCHECK,
  230. (pTcpipParams->bUseDhcp) ? BST_UNCHECKED : BST_CHECKED,
  231. 0);
  232. // Set the "allow caller to specify ip address" check
  233. SendDlgItemMessage(
  234. hwndDlg,
  235. CID_NetTab_Tcpipui_CB_CallerSpec,
  236. BM_SETCHECK,
  237. (pTcpipParams->bCaller) ? BST_CHECKED : BST_UNCHECKED,
  238. 0);
  239. // Set the text of the ip addresses
  240. if (pTcpipParams->dwPoolStart != 0)
  241. {
  242. hwndFrom = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Start);
  243. hwndTo = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Mask);
  244. TcpipDwordToAddr(pTcpipParams->dwPoolStart, pszAddrW);
  245. SetWindowText(hwndFrom, pszAddrW);
  246. TcpipDwordToAddr(pTcpipParams->dwPoolEnd, pszAddrW);
  247. SetWindowText(hwndTo, pszAddrW);
  248. }
  249. // Enable/disable windows as per the settings
  250. TcpipEnableWindows(hwndDlg, pTcpipParams);
  251. return NO_ERROR;
  252. }
  253. // Gets the settings from the ui and puts them into
  254. // the tcpip parameter structure.
  255. //
  256. DWORD
  257. TcpipGetUISettings(
  258. IN HWND hwndDlg,
  259. OUT PROT_EDIT_DATA * pEditData,
  260. OUT LPDWORD lpdwFrom,
  261. OUT LPDWORD lpdwTo)
  262. {
  263. TCPIP_PARAMS * pTcpipParams =
  264. (TCPIP_PARAMS *) pEditData->pbData;
  265. HWND hwndFrom, hwndTo;
  266. hwndFrom = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Start);
  267. hwndTo = GetDlgItem(hwndDlg, CID_NetTab_Tcpipui_EB_Mask);
  268. pEditData->bExpose =
  269. SendDlgItemMessage(
  270. hwndDlg,
  271. CID_NetTab_Tcpipui_CB_ExposeNetwork,
  272. BM_GETCHECK,
  273. 0,
  274. 0) == BST_CHECKED;
  275. SendMessage(
  276. hwndFrom,
  277. IP_GETADDRESS,
  278. 0,
  279. (LPARAM)&pTcpipParams->dwPoolStart);
  280. SendMessage(
  281. hwndTo,
  282. IP_GETADDRESS,
  283. 0,
  284. (LPARAM)&pTcpipParams->dwPoolEnd);
  285. *lpdwFrom = pTcpipParams->dwPoolStart;
  286. *lpdwTo = pTcpipParams->dwPoolEnd;
  287. return NO_ERROR;
  288. }
  289. DWORD
  290. TcpipUiHandleOk(
  291. IN HWND hwndDlg)
  292. {
  293. PROT_EDIT_DATA* pData = NULL;
  294. TCPIP_PARAMS * pParams = NULL;
  295. DWORD dwErr, dwId = SID_TCPIP_InvalidPool;
  296. DWORD dwStart = 0, dwEnd = 0;
  297. PWCHAR pszMessage = NULL;
  298. MSGARGS MsgArgs;
  299. // Get the context
  300. //
  301. pData = (PROT_EDIT_DATA*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  302. if (pData == NULL)
  303. {
  304. return ERROR_CAN_NOT_COMPLETE;
  305. }
  306. // Read values from the UI
  307. //
  308. dwErr = TcpipGetUISettings(hwndDlg, pData, &dwStart, &dwEnd);
  309. if (dwErr != NO_ERROR)
  310. {
  311. return dwErr;
  312. }
  313. // Validate the pool if one was entered
  314. //
  315. pParams = (TCPIP_PARAMS *)pData->pbData;
  316. if (pParams->bUseDhcp == FALSE)
  317. {
  318. dwErr = TcpipUiValidatePool(
  319. dwStart,
  320. dwEnd,
  321. &dwId);
  322. if (dwErr != NO_ERROR)
  323. {
  324. ZeroMemory(&MsgArgs, sizeof(MsgArgs));
  325. MsgArgs.dwFlags = MB_OK;
  326. MsgDlgUtil(
  327. hwndDlg,
  328. dwId,
  329. &MsgArgs,
  330. Globals.hInstDll,
  331. SID_DEFAULT_MSG_TITLE);
  332. }
  333. }
  334. return dwErr;
  335. }
  336. // Dialog proc that governs the tcpip settings dialog
  337. INT_PTR
  338. CALLBACK
  339. TcpipSettingsDialogProc (
  340. HWND hwndDlg,
  341. UINT uMsg,
  342. WPARAM wParam,
  343. LPARAM lParam)
  344. {
  345. switch (uMsg) {
  346. case WM_INITDIALOG:
  347. TcpipInitDialog(hwndDlg, lParam);
  348. return FALSE;
  349. case WM_HELP:
  350. case WM_CONTEXTMENU:
  351. {
  352. RasSrvHelp (hwndDlg, uMsg, wParam, lParam, phmTcpipui);
  353. break;
  354. }
  355. case WM_DESTROY:
  356. // Cleanup the work done at WM_INITDIALOG
  357. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, 0);
  358. break;
  359. case WM_COMMAND:
  360. {
  361. TCPIP_PARAMS * pTcpipParams = (TCPIP_PARAMS *)
  362. (((PROT_EDIT_DATA*)
  363. GetWindowLongPtr(hwndDlg, GWLP_USERDATA))->pbData);
  364. switch (wParam)
  365. {
  366. case IDOK:
  367. if (TcpipUiHandleOk(hwndDlg) == NO_ERROR)
  368. {
  369. EndDialog(hwndDlg, 1);
  370. }
  371. break;
  372. case IDCANCEL:
  373. EndDialog(hwndDlg, 0);
  374. break;
  375. case CID_NetTab_Tcpipui_RB_Dhcp:
  376. pTcpipParams->bUseDhcp = TRUE;
  377. TcpipEnableWindows(hwndDlg, pTcpipParams);
  378. break;
  379. case CID_NetTab_Tcpipui_RB_StaticPool:
  380. pTcpipParams->bUseDhcp = FALSE;
  381. TcpipEnableWindows(hwndDlg, pTcpipParams);
  382. break;
  383. case CID_NetTab_Tcpipui_CB_CallerSpec:
  384. pTcpipParams->bCaller = (BOOL)
  385. SendDlgItemMessage(
  386. hwndDlg,
  387. CID_NetTab_Tcpipui_CB_CallerSpec,
  388. BM_GETCHECK,
  389. 0,
  390. 0);
  391. break;
  392. }
  393. // Recal the pool size as appropriate
  394. //
  395. if (HIWORD(wParam) == EN_CHANGE)
  396. {
  397. if (LOWORD(wParam) == CID_NetTab_Tcpipui_EB_Start ||
  398. LOWORD(wParam) == CID_NetTab_Tcpipui_EB_Mask)
  399. {
  400. TcpipReCalcPoolSize(hwndDlg);
  401. }
  402. }
  403. break;
  404. }
  405. }
  406. return FALSE;
  407. }
  408. // Edits tcp ip protocol properties
  409. //
  410. DWORD
  411. TcpipEditProperties(
  412. HWND hwndParent,
  413. PROT_EDIT_DATA * pEditData,
  414. BOOL * pbCommit)
  415. {
  416. DWORD dwErr;
  417. int ret;
  418. // Initialize the ip address custom controls
  419. IpAddrInit(Globals.hInstDll, SID_TCPIP_TITLE, SID_TCPIP_BADRANGE);
  420. // Popup the dialog box
  421. ret = (int) DialogBoxParam(
  422. Globals.hInstDll,
  423. MAKEINTRESOURCE(DID_NetTab_Tcpipui),
  424. hwndParent,
  425. TcpipSettingsDialogProc,
  426. (LPARAM)pEditData);
  427. if (ret == -1)
  428. {
  429. TcpipUiDisplayError(hwndParent, ERR_TCPIP_CANT_DISPLAY);
  430. }
  431. // If ok was pressed, save off the new settings
  432. *pbCommit = FALSE;
  433. if (ret && ret != -1)
  434. {
  435. *pbCommit = TRUE;
  436. }
  437. return NO_ERROR;
  438. }